misc.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  1. <?php
  2. /**
  3. * Misc WordPress Administration API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Returns whether the server is running Apache with the mod_rewrite module loaded.
  10. *
  11. * @since 2.0.0
  12. *
  13. * @return bool Whether the server is running Apache with the mod_rewrite module loaded.
  14. */
  15. function got_mod_rewrite() {
  16. $got_rewrite = apache_mod_loaded( 'mod_rewrite', true );
  17. /**
  18. * Filters whether Apache and mod_rewrite are present.
  19. *
  20. * This filter was previously used to force URL rewriting for other servers,
  21. * like nginx. Use the {@see 'got_url_rewrite'} filter in got_url_rewrite() instead.
  22. *
  23. * @since 2.5.0
  24. *
  25. * @see got_url_rewrite()
  26. *
  27. * @param bool $got_rewrite Whether Apache and mod_rewrite are present.
  28. */
  29. return apply_filters( 'got_rewrite', $got_rewrite );
  30. }
  31. /**
  32. * Returns whether the server supports URL rewriting.
  33. *
  34. * Detects Apache's mod_rewrite, IIS 7.0+ permalink support, and nginx.
  35. *
  36. * @since 3.7.0
  37. *
  38. * @global bool $is_nginx
  39. *
  40. * @return bool Whether the server supports URL rewriting.
  41. */
  42. function got_url_rewrite() {
  43. $got_url_rewrite = ( got_mod_rewrite() || $GLOBALS['is_nginx'] || iis7_supports_permalinks() );
  44. /**
  45. * Filters whether URL rewriting is available.
  46. *
  47. * @since 3.7.0
  48. *
  49. * @param bool $got_url_rewrite Whether URL rewriting is available.
  50. */
  51. return apply_filters( 'got_url_rewrite', $got_url_rewrite );
  52. }
  53. /**
  54. * Extracts strings from between the BEGIN and END markers in the .htaccess file.
  55. *
  56. * @since 1.5.0
  57. *
  58. * @param string $filename Filename to extract the strings from.
  59. * @param string $marker The marker to extract the strings from.
  60. * @return array An array of strings from a file (.htaccess) from between BEGIN and END markers.
  61. */
  62. function extract_from_markers( $filename, $marker ) {
  63. $result = array();
  64. if ( ! file_exists( $filename ) ) {
  65. return $result;
  66. }
  67. $markerdata = explode( "\n", implode( '', file( $filename ) ) );
  68. $state = false;
  69. foreach ( $markerdata as $markerline ) {
  70. if ( false !== strpos( $markerline, '# END ' . $marker ) ) {
  71. $state = false;
  72. }
  73. if ( $state ) {
  74. if ( '#' === substr( $markerline, 0, 1 ) ) {
  75. continue;
  76. }
  77. $result[] = $markerline;
  78. }
  79. if ( false !== strpos( $markerline, '# BEGIN ' . $marker ) ) {
  80. $state = true;
  81. }
  82. }
  83. return $result;
  84. }
  85. /**
  86. * Inserts an array of strings into a file (.htaccess), placing it between
  87. * BEGIN and END markers.
  88. *
  89. * Replaces existing marked info. Retains surrounding
  90. * data. Creates file if none exists.
  91. *
  92. * @since 1.5.0
  93. *
  94. * @param string $filename Filename to alter.
  95. * @param string $marker The marker to alter.
  96. * @param array|string $insertion The new content to insert.
  97. * @return bool True on write success, false on failure.
  98. */
  99. function insert_with_markers( $filename, $marker, $insertion ) {
  100. if ( ! file_exists( $filename ) ) {
  101. if ( ! is_writable( dirname( $filename ) ) ) {
  102. return false;
  103. }
  104. if ( ! touch( $filename ) ) {
  105. return false;
  106. }
  107. } elseif ( ! is_writeable( $filename ) ) {
  108. return false;
  109. }
  110. if ( ! is_array( $insertion ) ) {
  111. $insertion = explode( "\n", $insertion );
  112. }
  113. $switched_locale = switch_to_locale( get_locale() );
  114. $instructions = sprintf(
  115. /* translators: 1: Marker. */
  116. __(
  117. 'The directives (lines) between `BEGIN %1$s` and `END %1$s` are
  118. dynamically generated, and should only be modified via WordPress filters.
  119. Any changes to the directives between these markers will be overwritten.'
  120. ),
  121. $marker
  122. );
  123. $instructions = explode( "\n", $instructions );
  124. foreach ( $instructions as $line => $text ) {
  125. $instructions[ $line ] = '# ' . $text;
  126. }
  127. /**
  128. * Filters the inline instructions inserted before the dynamically generated content.
  129. *
  130. * @since 5.3.0
  131. *
  132. * @param string[] $instructions Array of lines with inline instructions.
  133. * @param string $marker The marker being inserted.
  134. */
  135. $instructions = apply_filters( 'insert_with_markers_inline_instructions', $instructions, $marker );
  136. if ( $switched_locale ) {
  137. restore_previous_locale();
  138. }
  139. $insertion = array_merge( $instructions, $insertion );
  140. $start_marker = "# BEGIN {$marker}";
  141. $end_marker = "# END {$marker}";
  142. $fp = fopen( $filename, 'r+' );
  143. if ( ! $fp ) {
  144. return false;
  145. }
  146. // Attempt to get a lock. If the filesystem supports locking, this will block until the lock is acquired.
  147. flock( $fp, LOCK_EX );
  148. $lines = array();
  149. while ( ! feof( $fp ) ) {
  150. $lines[] = rtrim( fgets( $fp ), "\r\n" );
  151. }
  152. // Split out the existing file into the preceding lines, and those that appear after the marker
  153. $pre_lines = array();
  154. $post_lines = array();
  155. $existing_lines = array();
  156. $found_marker = false;
  157. $found_end_marker = false;
  158. foreach ( $lines as $line ) {
  159. if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) {
  160. $found_marker = true;
  161. continue;
  162. } elseif ( ! $found_end_marker && false !== strpos( $line, $end_marker ) ) {
  163. $found_end_marker = true;
  164. continue;
  165. }
  166. if ( ! $found_marker ) {
  167. $pre_lines[] = $line;
  168. } elseif ( $found_marker && $found_end_marker ) {
  169. $post_lines[] = $line;
  170. } else {
  171. $existing_lines[] = $line;
  172. }
  173. }
  174. // Check to see if there was a change
  175. if ( $existing_lines === $insertion ) {
  176. flock( $fp, LOCK_UN );
  177. fclose( $fp );
  178. return true;
  179. }
  180. // Generate the new file data
  181. $new_file_data = implode(
  182. "\n",
  183. array_merge(
  184. $pre_lines,
  185. array( $start_marker ),
  186. $insertion,
  187. array( $end_marker ),
  188. $post_lines
  189. )
  190. );
  191. // Write to the start of the file, and truncate it to that length
  192. fseek( $fp, 0 );
  193. $bytes = fwrite( $fp, $new_file_data );
  194. if ( $bytes ) {
  195. ftruncate( $fp, ftell( $fp ) );
  196. }
  197. fflush( $fp );
  198. flock( $fp, LOCK_UN );
  199. fclose( $fp );
  200. return (bool) $bytes;
  201. }
  202. /**
  203. * Updates the htaccess file with the current rules if it is writable.
  204. *
  205. * Always writes to the file if it exists and is writable to ensure that we
  206. * blank out old rules.
  207. *
  208. * @since 1.5.0
  209. *
  210. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  211. *
  212. * @return bool|null True on write success, false on failure. Null in multisite.
  213. */
  214. function save_mod_rewrite_rules() {
  215. if ( is_multisite() ) {
  216. return;
  217. }
  218. global $wp_rewrite;
  219. // Ensure get_home_path() is declared.
  220. require_once( ABSPATH . 'wp-admin/includes/file.php' );
  221. $home_path = get_home_path();
  222. $htaccess_file = $home_path . '.htaccess';
  223. /*
  224. * If the file doesn't already exist check for write access to the directory
  225. * and whether we have some rules. Else check for write access to the file.
  226. */
  227. if ( ( ! file_exists( $htaccess_file ) && is_writable( $home_path ) && $wp_rewrite->using_mod_rewrite_permalinks() ) || is_writable( $htaccess_file ) ) {
  228. if ( got_mod_rewrite() ) {
  229. $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
  230. return insert_with_markers( $htaccess_file, 'WordPress', $rules );
  231. }
  232. }
  233. return false;
  234. }
  235. /**
  236. * Updates the IIS web.config file with the current rules if it is writable.
  237. * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
  238. *
  239. * @since 2.8.0
  240. *
  241. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  242. *
  243. * @return bool|null True on write success, false on failure. Null in multisite.
  244. */
  245. function iis7_save_url_rewrite_rules() {
  246. if ( is_multisite() ) {
  247. return;
  248. }
  249. global $wp_rewrite;
  250. // Ensure get_home_path() is declared.
  251. require_once( ABSPATH . 'wp-admin/includes/file.php' );
  252. $home_path = get_home_path();
  253. $web_config_file = $home_path . 'web.config';
  254. // Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
  255. if ( iis7_supports_permalinks() && ( ( ! file_exists( $web_config_file ) && win_is_writable( $home_path ) && $wp_rewrite->using_mod_rewrite_permalinks() ) || win_is_writable( $web_config_file ) ) ) {
  256. $rule = $wp_rewrite->iis7_url_rewrite_rules( false );
  257. if ( ! empty( $rule ) ) {
  258. return iis7_add_rewrite_rule( $web_config_file, $rule );
  259. } else {
  260. return iis7_delete_rewrite_rule( $web_config_file );
  261. }
  262. }
  263. return false;
  264. }
  265. /**
  266. * Update the "recently-edited" file for the plugin or theme editor.
  267. *
  268. * @since 1.5.0
  269. *
  270. * @param string $file
  271. */
  272. function update_recently_edited( $file ) {
  273. $oldfiles = (array) get_option( 'recently_edited' );
  274. if ( $oldfiles ) {
  275. $oldfiles = array_reverse( $oldfiles );
  276. $oldfiles[] = $file;
  277. $oldfiles = array_reverse( $oldfiles );
  278. $oldfiles = array_unique( $oldfiles );
  279. if ( 5 < count( $oldfiles ) ) {
  280. array_pop( $oldfiles );
  281. }
  282. } else {
  283. $oldfiles[] = $file;
  284. }
  285. update_option( 'recently_edited', $oldfiles );
  286. }
  287. /**
  288. * Makes a tree structure for the theme editor's file list.
  289. *
  290. * @since 4.9.0
  291. * @access private
  292. *
  293. * @param array $allowed_files List of theme file paths.
  294. * @return array Tree structure for listing theme files.
  295. */
  296. function wp_make_theme_file_tree( $allowed_files ) {
  297. $tree_list = array();
  298. foreach ( $allowed_files as $file_name => $absolute_filename ) {
  299. $list = explode( '/', $file_name );
  300. $last_dir = &$tree_list;
  301. foreach ( $list as $dir ) {
  302. $last_dir =& $last_dir[ $dir ];
  303. }
  304. $last_dir = $file_name;
  305. }
  306. return $tree_list;
  307. }
  308. /**
  309. * Outputs the formatted file list for the theme editor.
  310. *
  311. * @since 4.9.0
  312. * @access private
  313. *
  314. * @global string $relative_file Name of the file being edited relative to the
  315. * theme directory.
  316. * @global string $stylesheet The stylesheet name of the theme being edited.
  317. *
  318. * @param array|string $tree List of file/folder paths, or filename.
  319. * @param int $level The aria-level for the current iteration.
  320. * @param int $size The aria-setsize for the current iteration.
  321. * @param int $index The aria-posinset for the current iteration.
  322. */
  323. function wp_print_theme_file_tree( $tree, $level = 2, $size = 1, $index = 1 ) {
  324. global $relative_file, $stylesheet;
  325. if ( is_array( $tree ) ) {
  326. $index = 0;
  327. $size = count( $tree );
  328. foreach ( $tree as $label => $theme_file ) :
  329. $index++;
  330. if ( ! is_array( $theme_file ) ) {
  331. wp_print_theme_file_tree( $theme_file, $level, $index, $size );
  332. continue;
  333. }
  334. ?>
  335. <li role="treeitem" aria-expanded="true" tabindex="-1"
  336. aria-level="<?php echo esc_attr( $level ); ?>"
  337. aria-setsize="<?php echo esc_attr( $size ); ?>"
  338. aria-posinset="<?php echo esc_attr( $index ); ?>">
  339. <span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text"><?php _e( 'folder' ); ?></span><span aria-hidden="true" class="icon"></span></span>
  340. <ul role="group" class="tree-folder"><?php wp_print_theme_file_tree( $theme_file, $level + 1, $index, $size ); ?></ul>
  341. </li>
  342. <?php
  343. endforeach;
  344. } else {
  345. $filename = $tree;
  346. $url = add_query_arg(
  347. array(
  348. 'file' => rawurlencode( $tree ),
  349. 'theme' => rawurlencode( $stylesheet ),
  350. ),
  351. self_admin_url( 'theme-editor.php' )
  352. );
  353. ?>
  354. <li role="none" class="<?php echo esc_attr( $relative_file === $filename ? 'current-file' : '' ); ?>">
  355. <a role="treeitem" tabindex="<?php echo esc_attr( $relative_file === $filename ? '0' : '-1' ); ?>"
  356. href="<?php echo esc_url( $url ); ?>"
  357. aria-level="<?php echo esc_attr( $level ); ?>"
  358. aria-setsize="<?php echo esc_attr( $size ); ?>"
  359. aria-posinset="<?php echo esc_attr( $index ); ?>">
  360. <?php
  361. $file_description = esc_html( get_file_description( $filename ) );
  362. if ( $file_description !== $filename && wp_basename( $filename ) !== $file_description ) {
  363. $file_description .= '<br /><span class="nonessential">(' . esc_html( $filename ) . ')</span>';
  364. }
  365. if ( $relative_file === $filename ) {
  366. echo '<span class="notice notice-info">' . $file_description . '</span>';
  367. } else {
  368. echo $file_description;
  369. }
  370. ?>
  371. </a>
  372. </li>
  373. <?php
  374. }
  375. }
  376. /**
  377. * Makes a tree structure for the plugin editor's file list.
  378. *
  379. * @since 4.9.0
  380. * @access private
  381. *
  382. * @param array $plugin_editable_files List of plugin file paths.
  383. * @return array Tree structure for listing plugin files.
  384. */
  385. function wp_make_plugin_file_tree( $plugin_editable_files ) {
  386. $tree_list = array();
  387. foreach ( $plugin_editable_files as $plugin_file ) {
  388. $list = explode( '/', preg_replace( '#^.+?/#', '', $plugin_file ) );
  389. $last_dir = &$tree_list;
  390. foreach ( $list as $dir ) {
  391. $last_dir =& $last_dir[ $dir ];
  392. }
  393. $last_dir = $plugin_file;
  394. }
  395. return $tree_list;
  396. }
  397. /**
  398. * Outputs the formatted file list for the plugin editor.
  399. *
  400. * @since 4.9.0
  401. * @access private
  402. *
  403. * @param array|string $tree List of file/folder paths, or filename.
  404. * @param string $label Name of file or folder to print.
  405. * @param int $level The aria-level for the current iteration.
  406. * @param int $size The aria-setsize for the current iteration.
  407. * @param int $index The aria-posinset for the current iteration.
  408. */
  409. function wp_print_plugin_file_tree( $tree, $label = '', $level = 2, $size = 1, $index = 1 ) {
  410. global $file, $plugin;
  411. if ( is_array( $tree ) ) {
  412. $index = 0;
  413. $size = count( $tree );
  414. foreach ( $tree as $label => $plugin_file ) :
  415. $index++;
  416. if ( ! is_array( $plugin_file ) ) {
  417. wp_print_plugin_file_tree( $plugin_file, $label, $level, $index, $size );
  418. continue;
  419. }
  420. ?>
  421. <li role="treeitem" aria-expanded="true" tabindex="-1"
  422. aria-level="<?php echo esc_attr( $level ); ?>"
  423. aria-setsize="<?php echo esc_attr( $size ); ?>"
  424. aria-posinset="<?php echo esc_attr( $index ); ?>">
  425. <span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text"><?php _e( 'folder' ); ?></span><span aria-hidden="true" class="icon"></span></span>
  426. <ul role="group" class="tree-folder"><?php wp_print_plugin_file_tree( $plugin_file, '', $level + 1, $index, $size ); ?></ul>
  427. </li>
  428. <?php
  429. endforeach;
  430. } else {
  431. $url = add_query_arg(
  432. array(
  433. 'file' => rawurlencode( $tree ),
  434. 'plugin' => rawurlencode( $plugin ),
  435. ),
  436. self_admin_url( 'plugin-editor.php' )
  437. );
  438. ?>
  439. <li role="none" class="<?php echo esc_attr( $file === $tree ? 'current-file' : '' ); ?>">
  440. <a role="treeitem" tabindex="<?php echo esc_attr( $file === $tree ? '0' : '-1' ); ?>"
  441. href="<?php echo esc_url( $url ); ?>"
  442. aria-level="<?php echo esc_attr( $level ); ?>"
  443. aria-setsize="<?php echo esc_attr( $size ); ?>"
  444. aria-posinset="<?php echo esc_attr( $index ); ?>">
  445. <?php
  446. if ( $file === $tree ) {
  447. echo '<span class="notice notice-info">' . esc_html( $label ) . '</span>';
  448. } else {
  449. echo esc_html( $label );
  450. }
  451. ?>
  452. </a>
  453. </li>
  454. <?php
  455. }
  456. }
  457. /**
  458. * Flushes rewrite rules if siteurl, home or page_on_front changed.
  459. *
  460. * @since 2.1.0
  461. *
  462. * @param string $old_value
  463. * @param string $value
  464. */
  465. function update_home_siteurl( $old_value, $value ) {
  466. if ( wp_installing() ) {
  467. return;
  468. }
  469. if ( is_multisite() && ms_is_switched() ) {
  470. delete_option( 'rewrite_rules' );
  471. } else {
  472. flush_rewrite_rules();
  473. }
  474. }
  475. /**
  476. * Resets global variables based on $_GET and $_POST
  477. *
  478. * This function resets global variables based on the names passed
  479. * in the $vars array to the value of $_POST[$var] or $_GET[$var] or ''
  480. * if neither is defined.
  481. *
  482. * @since 2.0.0
  483. *
  484. * @param array $vars An array of globals to reset.
  485. */
  486. function wp_reset_vars( $vars ) {
  487. foreach ( $vars as $var ) {
  488. if ( empty( $_POST[ $var ] ) ) {
  489. if ( empty( $_GET[ $var ] ) ) {
  490. $GLOBALS[ $var ] = '';
  491. } else {
  492. $GLOBALS[ $var ] = $_GET[ $var ];
  493. }
  494. } else {
  495. $GLOBALS[ $var ] = $_POST[ $var ];
  496. }
  497. }
  498. }
  499. /**
  500. * Displays the given administration message.
  501. *
  502. * @since 2.1.0
  503. *
  504. * @param string|WP_Error $message
  505. */
  506. function show_message( $message ) {
  507. if ( is_wp_error( $message ) ) {
  508. if ( $message->get_error_data() && is_string( $message->get_error_data() ) ) {
  509. $message = $message->get_error_message() . ': ' . $message->get_error_data();
  510. } else {
  511. $message = $message->get_error_message();
  512. }
  513. }
  514. echo "<p>$message</p>\n";
  515. wp_ob_end_flush_all();
  516. flush();
  517. }
  518. /**
  519. * @since 2.8.0
  520. *
  521. * @param string $content
  522. * @return array
  523. */
  524. function wp_doc_link_parse( $content ) {
  525. if ( ! is_string( $content ) || empty( $content ) ) {
  526. return array();
  527. }
  528. if ( ! function_exists( 'token_get_all' ) ) {
  529. return array();
  530. }
  531. $tokens = token_get_all( $content );
  532. $count = count( $tokens );
  533. $functions = array();
  534. $ignore_functions = array();
  535. for ( $t = 0; $t < $count - 2; $t++ ) {
  536. if ( ! is_array( $tokens[ $t ] ) ) {
  537. continue;
  538. }
  539. if ( T_STRING == $tokens[ $t ][0] && ( '(' == $tokens[ $t + 1 ] || '(' == $tokens[ $t + 2 ] ) ) {
  540. // If it's a function or class defined locally, there's not going to be any docs available
  541. if ( ( isset( $tokens[ $t - 2 ][1] ) && in_array( $tokens[ $t - 2 ][1], array( 'function', 'class' ) ) ) || ( isset( $tokens[ $t - 2 ][0] ) && T_OBJECT_OPERATOR == $tokens[ $t - 1 ][0] ) ) {
  542. $ignore_functions[] = $tokens[ $t ][1];
  543. }
  544. // Add this to our stack of unique references
  545. $functions[] = $tokens[ $t ][1];
  546. }
  547. }
  548. $functions = array_unique( $functions );
  549. sort( $functions );
  550. /**
  551. * Filters the list of functions and classes to be ignored from the documentation lookup.
  552. *
  553. * @since 2.8.0
  554. *
  555. * @param string[] $ignore_functions Array of names of functions and classes to be ignored.
  556. */
  557. $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
  558. $ignore_functions = array_unique( $ignore_functions );
  559. $out = array();
  560. foreach ( $functions as $function ) {
  561. if ( in_array( $function, $ignore_functions ) ) {
  562. continue;
  563. }
  564. $out[] = $function;
  565. }
  566. return $out;
  567. }
  568. /**
  569. * Saves option for number of rows when listing posts, pages, comments, etc.
  570. *
  571. * @since 2.8.0
  572. */
  573. function set_screen_options() {
  574. if ( isset( $_POST['wp_screen_options'] ) && is_array( $_POST['wp_screen_options'] ) ) {
  575. check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
  576. $user = wp_get_current_user();
  577. if ( ! $user ) {
  578. return;
  579. }
  580. $option = $_POST['wp_screen_options']['option'];
  581. $value = $_POST['wp_screen_options']['value'];
  582. if ( $option != sanitize_key( $option ) ) {
  583. return;
  584. }
  585. $map_option = $option;
  586. $type = str_replace( 'edit_', '', $map_option );
  587. $type = str_replace( '_per_page', '', $type );
  588. if ( in_array( $type, get_taxonomies() ) ) {
  589. $map_option = 'edit_tags_per_page';
  590. } elseif ( in_array( $type, get_post_types() ) ) {
  591. $map_option = 'edit_per_page';
  592. } else {
  593. $option = str_replace( '-', '_', $option );
  594. }
  595. switch ( $map_option ) {
  596. case 'edit_per_page':
  597. case 'users_per_page':
  598. case 'edit_comments_per_page':
  599. case 'upload_per_page':
  600. case 'edit_tags_per_page':
  601. case 'plugins_per_page':
  602. case 'export_personal_data_requests_per_page':
  603. case 'remove_personal_data_requests_per_page':
  604. // Network admin
  605. case 'sites_network_per_page':
  606. case 'users_network_per_page':
  607. case 'site_users_network_per_page':
  608. case 'plugins_network_per_page':
  609. case 'themes_network_per_page':
  610. case 'site_themes_network_per_page':
  611. $value = (int) $value;
  612. if ( $value < 1 || $value > 999 ) {
  613. return;
  614. }
  615. break;
  616. default:
  617. /**
  618. * Filters a screen option value before it is set.
  619. *
  620. * The filter can also be used to modify non-standard [items]_per_page
  621. * settings. See the parent function for a full list of standard options.
  622. *
  623. * Returning false to the filter will skip saving the current option.
  624. *
  625. * @since 2.8.0
  626. *
  627. * @see set_screen_options()
  628. *
  629. * @param bool $keep Whether to save or skip saving the screen option value. Default false.
  630. * @param string $option The option name.
  631. * @param int $value The number of rows to use.
  632. */
  633. $value = apply_filters( 'set-screen-option', false, $option, $value ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  634. if ( false === $value ) {
  635. return;
  636. }
  637. break;
  638. }
  639. update_user_meta( $user->ID, $option, $value );
  640. $url = remove_query_arg( array( 'pagenum', 'apage', 'paged' ), wp_get_referer() );
  641. if ( isset( $_POST['mode'] ) ) {
  642. $url = add_query_arg( array( 'mode' => $_POST['mode'] ), $url );
  643. }
  644. wp_safe_redirect( $url );
  645. exit;
  646. }
  647. }
  648. /**
  649. * Check if rewrite rule for WordPress already exists in the IIS 7+ configuration file
  650. *
  651. * @since 2.8.0
  652. *
  653. * @return bool
  654. * @param string $filename The file path to the configuration file
  655. */
  656. function iis7_rewrite_rule_exists( $filename ) {
  657. if ( ! file_exists( $filename ) ) {
  658. return false;
  659. }
  660. if ( ! class_exists( 'DOMDocument', false ) ) {
  661. return false;
  662. }
  663. $doc = new DOMDocument();
  664. if ( $doc->load( $filename ) === false ) {
  665. return false;
  666. }
  667. $xpath = new DOMXPath( $doc );
  668. $rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
  669. if ( $rules->length == 0 ) {
  670. return false;
  671. } else {
  672. return true;
  673. }
  674. }
  675. /**
  676. * Delete WordPress rewrite rule from web.config file if it exists there
  677. *
  678. * @since 2.8.0
  679. *
  680. * @param string $filename Name of the configuration file
  681. * @return bool
  682. */
  683. function iis7_delete_rewrite_rule( $filename ) {
  684. // If configuration file does not exist then rules also do not exist so there is nothing to delete
  685. if ( ! file_exists( $filename ) ) {
  686. return true;
  687. }
  688. if ( ! class_exists( 'DOMDocument', false ) ) {
  689. return false;
  690. }
  691. $doc = new DOMDocument();
  692. $doc->preserveWhiteSpace = false;
  693. if ( $doc->load( $filename ) === false ) {
  694. return false;
  695. }
  696. $xpath = new DOMXPath( $doc );
  697. $rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
  698. if ( $rules->length > 0 ) {
  699. $child = $rules->item( 0 );
  700. $parent = $child->parentNode;
  701. $parent->removeChild( $child );
  702. $doc->formatOutput = true;
  703. saveDomDocument( $doc, $filename );
  704. }
  705. return true;
  706. }
  707. /**
  708. * Add WordPress rewrite rule to the IIS 7+ configuration file.
  709. *
  710. * @since 2.8.0
  711. *
  712. * @param string $filename The file path to the configuration file
  713. * @param string $rewrite_rule The XML fragment with URL Rewrite rule
  714. * @return bool
  715. */
  716. function iis7_add_rewrite_rule( $filename, $rewrite_rule ) {
  717. if ( ! class_exists( 'DOMDocument', false ) ) {
  718. return false;
  719. }
  720. // If configuration file does not exist then we create one.
  721. if ( ! file_exists( $filename ) ) {
  722. $fp = fopen( $filename, 'w' );
  723. fwrite( $fp, '<configuration/>' );
  724. fclose( $fp );
  725. }
  726. $doc = new DOMDocument();
  727. $doc->preserveWhiteSpace = false;
  728. if ( $doc->load( $filename ) === false ) {
  729. return false;
  730. }
  731. $xpath = new DOMXPath( $doc );
  732. // First check if the rule already exists as in that case there is no need to re-add it
  733. $wordpress_rules = $xpath->query( '/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]' );
  734. if ( $wordpress_rules->length > 0 ) {
  735. return true;
  736. }
  737. // Check the XPath to the rewrite rule and create XML nodes if they do not exist
  738. $xmlnodes = $xpath->query( '/configuration/system.webServer/rewrite/rules' );
  739. if ( $xmlnodes->length > 0 ) {
  740. $rules_node = $xmlnodes->item( 0 );
  741. } else {
  742. $rules_node = $doc->createElement( 'rules' );
  743. $xmlnodes = $xpath->query( '/configuration/system.webServer/rewrite' );
  744. if ( $xmlnodes->length > 0 ) {
  745. $rewrite_node = $xmlnodes->item( 0 );
  746. $rewrite_node->appendChild( $rules_node );
  747. } else {
  748. $rewrite_node = $doc->createElement( 'rewrite' );
  749. $rewrite_node->appendChild( $rules_node );
  750. $xmlnodes = $xpath->query( '/configuration/system.webServer' );
  751. if ( $xmlnodes->length > 0 ) {
  752. $system_webServer_node = $xmlnodes->item( 0 );
  753. $system_webServer_node->appendChild( $rewrite_node );
  754. } else {
  755. $system_webServer_node = $doc->createElement( 'system.webServer' );
  756. $system_webServer_node->appendChild( $rewrite_node );
  757. $xmlnodes = $xpath->query( '/configuration' );
  758. if ( $xmlnodes->length > 0 ) {
  759. $config_node = $xmlnodes->item( 0 );
  760. $config_node->appendChild( $system_webServer_node );
  761. } else {
  762. $config_node = $doc->createElement( 'configuration' );
  763. $doc->appendChild( $config_node );
  764. $config_node->appendChild( $system_webServer_node );
  765. }
  766. }
  767. }
  768. }
  769. $rule_fragment = $doc->createDocumentFragment();
  770. $rule_fragment->appendXML( $rewrite_rule );
  771. $rules_node->appendChild( $rule_fragment );
  772. $doc->encoding = 'UTF-8';
  773. $doc->formatOutput = true;
  774. saveDomDocument( $doc, $filename );
  775. return true;
  776. }
  777. /**
  778. * Saves the XML document into a file
  779. *
  780. * @since 2.8.0
  781. *
  782. * @param DOMDocument $doc
  783. * @param string $filename
  784. */
  785. function saveDomDocument( $doc, $filename ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  786. $config = $doc->saveXML();
  787. $config = preg_replace( "/([^\r])\n/", "$1\r\n", $config );
  788. $fp = fopen( $filename, 'w' );
  789. fwrite( $fp, $config );
  790. fclose( $fp );
  791. }
  792. /**
  793. * Display the default admin color scheme picker (Used in user-edit.php)
  794. *
  795. * @since 3.0.0
  796. *
  797. * @global array $_wp_admin_css_colors
  798. *
  799. * @param int $user_id User ID.
  800. */
  801. function admin_color_scheme_picker( $user_id ) {
  802. global $_wp_admin_css_colors;
  803. ksort( $_wp_admin_css_colors );
  804. if ( isset( $_wp_admin_css_colors['fresh'] ) ) {
  805. // Set Default ('fresh') and Light should go first.
  806. $_wp_admin_css_colors = array_filter(
  807. array_merge(
  808. array(
  809. 'fresh' => '',
  810. 'light' => '',
  811. ),
  812. $_wp_admin_css_colors
  813. )
  814. );
  815. }
  816. $current_color = get_user_option( 'admin_color', $user_id );
  817. if ( empty( $current_color ) || ! isset( $_wp_admin_css_colors[ $current_color ] ) ) {
  818. $current_color = 'fresh';
  819. }
  820. ?>
  821. <fieldset id="color-picker" class="scheme-list">
  822. <legend class="screen-reader-text"><span><?php _e( 'Admin Color Scheme' ); ?></span></legend>
  823. <?php
  824. wp_nonce_field( 'save-color-scheme', 'color-nonce', false );
  825. foreach ( $_wp_admin_css_colors as $color => $color_info ) :
  826. ?>
  827. <div class="color-option <?php echo ( $color == $current_color ) ? 'selected' : ''; ?>">
  828. <input name="admin_color" id="admin_color_<?php echo esc_attr( $color ); ?>" type="radio" value="<?php echo esc_attr( $color ); ?>" class="tog" <?php checked( $color, $current_color ); ?> />
  829. <input type="hidden" class="css_url" value="<?php echo esc_url( $color_info->url ); ?>" />
  830. <input type="hidden" class="icon_colors" value="<?php echo esc_attr( wp_json_encode( array( 'icons' => $color_info->icon_colors ) ) ); ?>" />
  831. <label for="admin_color_<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $color_info->name ); ?></label>
  832. <table class="color-palette">
  833. <tr>
  834. <?php
  835. foreach ( $color_info->colors as $html_color ) {
  836. ?>
  837. <td style="background-color: <?php echo esc_attr( $html_color ); ?>">&nbsp;</td>
  838. <?php
  839. }
  840. ?>
  841. </tr>
  842. </table>
  843. </div>
  844. <?php
  845. endforeach;
  846. ?>
  847. </fieldset>
  848. <?php
  849. }
  850. /**
  851. *
  852. * @global array $_wp_admin_css_colors
  853. */
  854. function wp_color_scheme_settings() {
  855. global $_wp_admin_css_colors;
  856. $color_scheme = get_user_option( 'admin_color' );
  857. // It's possible to have a color scheme set that is no longer registered.
  858. if ( empty( $_wp_admin_css_colors[ $color_scheme ] ) ) {
  859. $color_scheme = 'fresh';
  860. }
  861. if ( ! empty( $_wp_admin_css_colors[ $color_scheme ]->icon_colors ) ) {
  862. $icon_colors = $_wp_admin_css_colors[ $color_scheme ]->icon_colors;
  863. } elseif ( ! empty( $_wp_admin_css_colors['fresh']->icon_colors ) ) {
  864. $icon_colors = $_wp_admin_css_colors['fresh']->icon_colors;
  865. } else {
  866. // Fall back to the default set of icon colors if the default scheme is missing.
  867. $icon_colors = array(
  868. 'base' => '#a0a5aa',
  869. 'focus' => '#00a0d2',
  870. 'current' => '#fff',
  871. );
  872. }
  873. echo '<script type="text/javascript">var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ) ) . ";</script>\n";
  874. }
  875. /**
  876. * @since 3.3.0
  877. */
  878. function _ipad_meta() {
  879. if ( wp_is_mobile() ) {
  880. ?>
  881. <meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
  882. <?php
  883. }
  884. }
  885. /**
  886. * Check lock status for posts displayed on the Posts screen
  887. *
  888. * @since 3.6.0
  889. *
  890. * @param array $response The Heartbeat response.
  891. * @param array $data The $_POST data sent.
  892. * @param string $screen_id The screen id.
  893. * @return array The Heartbeat response.
  894. */
  895. function wp_check_locked_posts( $response, $data, $screen_id ) {
  896. $checked = array();
  897. if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
  898. foreach ( $data['wp-check-locked-posts'] as $key ) {
  899. $post_id = absint( substr( $key, 5 ) );
  900. if ( ! $post_id ) {
  901. continue;
  902. }
  903. $user_id = wp_check_post_lock( $post_id );
  904. if ( $user_id ) {
  905. $user = get_userdata( $user_id );
  906. if ( $user && current_user_can( 'edit_post', $post_id ) ) {
  907. $send = array(
  908. /* translators: %s: User's display name. */
  909. 'text' => sprintf( __( '%s is currently editing' ), $user->display_name ),
  910. );
  911. $avatar = get_avatar( $user->ID, 18 );
  912. if ( $avatar && preg_match( "|src='([^']+)'|", $avatar, $matches ) ) {
  913. $send['avatar_src'] = $matches[1];
  914. }
  915. $checked[ $key ] = $send;
  916. }
  917. }
  918. }
  919. }
  920. if ( ! empty( $checked ) ) {
  921. $response['wp-check-locked-posts'] = $checked;
  922. }
  923. return $response;
  924. }
  925. /**
  926. * Check lock status on the New/Edit Post screen and refresh the lock
  927. *
  928. * @since 3.6.0
  929. *
  930. * @param array $response The Heartbeat response.
  931. * @param array $data The $_POST data sent.
  932. * @param string $screen_id The screen id.
  933. * @return array The Heartbeat response.
  934. */
  935. function wp_refresh_post_lock( $response, $data, $screen_id ) {
  936. if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
  937. $received = $data['wp-refresh-post-lock'];
  938. $send = array();
  939. $post_id = absint( $received['post_id'] );
  940. if ( ! $post_id ) {
  941. return $response;
  942. }
  943. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  944. return $response;
  945. }
  946. $user_id = wp_check_post_lock( $post_id );
  947. $user = get_userdata( $user_id );
  948. if ( $user ) {
  949. $error = array(
  950. /* translators: %s: User's display name. */
  951. 'text' => sprintf( __( '%s has taken over and is currently editing.' ), $user->display_name ),
  952. );
  953. $avatar = get_avatar( $user->ID, 64 );
  954. if ( $avatar ) {
  955. if ( preg_match( "|src='([^']+)'|", $avatar, $matches ) ) {
  956. $error['avatar_src'] = $matches[1];
  957. }
  958. }
  959. $send['lock_error'] = $error;
  960. } else {
  961. $new_lock = wp_set_post_lock( $post_id );
  962. if ( $new_lock ) {
  963. $send['new_lock'] = implode( ':', $new_lock );
  964. }
  965. }
  966. $response['wp-refresh-post-lock'] = $send;
  967. }
  968. return $response;
  969. }
  970. /**
  971. * Check nonce expiration on the New/Edit Post screen and refresh if needed
  972. *
  973. * @since 3.6.0
  974. *
  975. * @param array $response The Heartbeat response.
  976. * @param array $data The $_POST data sent.
  977. * @param string $screen_id The screen id.
  978. * @return array The Heartbeat response.
  979. */
  980. function wp_refresh_post_nonces( $response, $data, $screen_id ) {
  981. if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
  982. $received = $data['wp-refresh-post-nonces'];
  983. $response['wp-refresh-post-nonces'] = array( 'check' => 1 );
  984. $post_id = absint( $received['post_id'] );
  985. if ( ! $post_id ) {
  986. return $response;
  987. }
  988. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  989. return $response;
  990. }
  991. $response['wp-refresh-post-nonces'] = array(
  992. 'replace' => array(
  993. 'getpermalinknonce' => wp_create_nonce( 'getpermalink' ),
  994. 'samplepermalinknonce' => wp_create_nonce( 'samplepermalink' ),
  995. 'closedpostboxesnonce' => wp_create_nonce( 'closedpostboxes' ),
  996. '_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
  997. '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
  998. ),
  999. );
  1000. }
  1001. return $response;
  1002. }
  1003. /**
  1004. * Add the latest Heartbeat and REST-API nonce to the Heartbeat response.
  1005. *
  1006. * @since 5.0.0
  1007. *
  1008. * @param array $response The Heartbeat response.
  1009. * @return array The Heartbeat response.
  1010. */
  1011. function wp_refresh_heartbeat_nonces( $response ) {
  1012. // Refresh the Rest API nonce.
  1013. $response['rest_nonce'] = wp_create_nonce( 'wp_rest' );
  1014. // Refresh the Heartbeat nonce.
  1015. $response['heartbeat_nonce'] = wp_create_nonce( 'heartbeat-nonce' );
  1016. return $response;
  1017. }
  1018. /**
  1019. * Disable suspension of Heartbeat on the Add/Edit Post screens.
  1020. *
  1021. * @since 3.8.0
  1022. *
  1023. * @global string $pagenow
  1024. *
  1025. * @param array $settings An array of Heartbeat settings.
  1026. * @return array Filtered Heartbeat settings.
  1027. */
  1028. function wp_heartbeat_set_suspension( $settings ) {
  1029. global $pagenow;
  1030. if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
  1031. $settings['suspension'] = 'disable';
  1032. }
  1033. return $settings;
  1034. }
  1035. /**
  1036. * Autosave with heartbeat
  1037. *
  1038. * @since 3.9.0
  1039. *
  1040. * @param array $response The Heartbeat response.
  1041. * @param array $data The $_POST data sent.
  1042. * @return array The Heartbeat response.
  1043. */
  1044. function heartbeat_autosave( $response, $data ) {
  1045. if ( ! empty( $data['wp_autosave'] ) ) {
  1046. $saved = wp_autosave( $data['wp_autosave'] );
  1047. if ( is_wp_error( $saved ) ) {
  1048. $response['wp_autosave'] = array(
  1049. 'success' => false,
  1050. 'message' => $saved->get_error_message(),
  1051. );
  1052. } elseif ( empty( $saved ) ) {
  1053. $response['wp_autosave'] = array(
  1054. 'success' => false,
  1055. 'message' => __( 'Error while saving.' ),
  1056. );
  1057. } else {
  1058. /* translators: Draft saved date format, see https://secure.php.net/date */
  1059. $draft_saved_date_format = __( 'g:i:s a' );
  1060. $response['wp_autosave'] = array(
  1061. 'success' => true,
  1062. /* translators: %s: Date and time. */
  1063. 'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ),
  1064. );
  1065. }
  1066. }
  1067. return $response;
  1068. }
  1069. /**
  1070. * Remove single-use URL parameters and create canonical link based on new URL.
  1071. *
  1072. * Remove specific query string parameters from a URL, create the canonical link,
  1073. * put it in the admin header, and change the current URL to match.
  1074. *
  1075. * @since 4.2.0
  1076. */
  1077. function wp_admin_canonical_url() {
  1078. $removable_query_args = wp_removable_query_args();
  1079. if ( empty( $removable_query_args ) ) {
  1080. return;
  1081. }
  1082. // Ensure we're using an absolute URL.
  1083. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  1084. $filtered_url = remove_query_arg( $removable_query_args, $current_url );
  1085. ?>
  1086. <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
  1087. <script>
  1088. if ( window.history.replaceState ) {
  1089. window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
  1090. }
  1091. </script>
  1092. <?php
  1093. }
  1094. /**
  1095. * Send a referrer policy header so referrers are not sent externally from administration screens.
  1096. *
  1097. * @since 4.9.0
  1098. */
  1099. function wp_admin_headers() {
  1100. $policy = 'strict-origin-when-cross-origin';
  1101. /**
  1102. * Filters the admin referrer policy header value.
  1103. *
  1104. * @since 4.9.0
  1105. * @since 4.9.5 The default value was changed to 'strict-origin-when-cross-origin'.
  1106. *
  1107. * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
  1108. *
  1109. * @param string $policy The admin referrer policy header value. Default 'strict-origin-when-cross-origin'.
  1110. */
  1111. $policy = apply_filters( 'admin_referrer_policy', $policy );
  1112. header( sprintf( 'Referrer-Policy: %s', $policy ) );
  1113. }
  1114. /**
  1115. * Outputs JS that reloads the page if the user navigated to it with the Back or Forward button.
  1116. *
  1117. * Used on the Edit Post and Add New Post screens. Needed to ensure the page is not loaded from browser cache,
  1118. * so the post title and editor content are the last saved versions. Ideally this script should run first in the head.
  1119. *
  1120. * @since 4.6.0
  1121. */
  1122. function wp_page_reload_on_back_button_js() {
  1123. ?>
  1124. <script>
  1125. if ( typeof performance !== 'undefined' && performance.navigation && performance.navigation.type === 2 ) {
  1126. document.location.reload( true );
  1127. }
  1128. </script>
  1129. <?php
  1130. }
  1131. /**
  1132. * Send a confirmation request email when a change of site admin email address is attempted.
  1133. *
  1134. * The new site admin address will not become active until confirmed.
  1135. *
  1136. * @since 3.0.0
  1137. * @since 4.9.0 This function was moved from wp-admin/includes/ms.php so it's no longer Multisite specific.
  1138. *
  1139. * @param string $old_value The old site admin email address.
  1140. * @param string $value The proposed new site admin email address.
  1141. */
  1142. function update_option_new_admin_email( $old_value, $value ) {
  1143. if ( $value == get_option( 'admin_email' ) || ! is_email( $value ) ) {
  1144. return;
  1145. }
  1146. $hash = md5( $value . time() . wp_rand() );
  1147. $new_admin_email = array(
  1148. 'hash' => $hash,
  1149. 'newemail' => $value,
  1150. );
  1151. update_option( 'adminhash', $new_admin_email );
  1152. $switched_locale = switch_to_locale( get_user_locale() );
  1153. /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
  1154. $email_text = __(
  1155. 'Howdy ###USERNAME###,
  1156. You recently requested to have the administration email address on
  1157. your site changed.
  1158. If this is correct, please click on the following link to change it:
  1159. ###ADMIN_URL###
  1160. You can safely ignore and delete this email if you do not want to
  1161. take this action.
  1162. This email has been sent to ###EMAIL###
  1163. Regards,
  1164. All at ###SITENAME###
  1165. ###SITEURL###'
  1166. );
  1167. /**
  1168. * Filters the text of the email sent when a change of site admin email address is attempted.
  1169. *
  1170. * The following strings have a special meaning and will get replaced dynamically:
  1171. * ###USERNAME### The current user's username.
  1172. * ###ADMIN_URL### The link to click on to confirm the email change.
  1173. * ###EMAIL### The proposed new site admin email address.
  1174. * ###SITENAME### The name of the site.
  1175. * ###SITEURL### The URL to the site.
  1176. *
  1177. * @since MU (3.0.0)
  1178. * @since 4.9.0 This filter is no longer Multisite specific.
  1179. *
  1180. * @param string $email_text Text in the email.
  1181. * @param array $new_admin_email {
  1182. * Data relating to the new site admin email address.
  1183. *
  1184. * @type string $hash The secure hash used in the confirmation link URL.
  1185. * @type string $newemail The proposed new site admin email address.
  1186. * }
  1187. */
  1188. $content = apply_filters( 'new_admin_email_content', $email_text, $new_admin_email );
  1189. $current_user = wp_get_current_user();
  1190. $content = str_replace( '###USERNAME###', $current_user->user_login, $content );
  1191. $content = str_replace( '###ADMIN_URL###', esc_url( self_admin_url( 'options.php?adminhash=' . $hash ) ), $content );
  1192. $content = str_replace( '###EMAIL###', $value, $content );
  1193. $content = str_replace( '###SITENAME###', wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $content );
  1194. $content = str_replace( '###SITEURL###', home_url(), $content );
  1195. wp_mail(
  1196. $value,
  1197. sprintf(
  1198. /* translators: New admin email address notification email subject. %s: Site title. */
  1199. __( '[%s] New Admin Email Address' ),
  1200. wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES )
  1201. ),
  1202. $content
  1203. );
  1204. if ( $switched_locale ) {
  1205. restore_previous_locale();
  1206. }
  1207. }
  1208. /**
  1209. * Appends '(Draft)' to draft page titles in the privacy page dropdown
  1210. * so that unpublished content is obvious.
  1211. *
  1212. * @since 4.9.8
  1213. * @access private
  1214. *
  1215. * @param string $title Page title.
  1216. * @param WP_Post $page Page data object.
  1217. *
  1218. * @return string Page title.
  1219. */
  1220. function _wp_privacy_settings_filter_draft_page_titles( $title, $page ) {
  1221. if ( 'draft' === $page->post_status && 'privacy' === get_current_screen()->id ) {
  1222. /* translators: %s: Page title. */
  1223. $title = sprintf( __( '%s (Draft)' ), $title );
  1224. }
  1225. return $title;
  1226. }
  1227. /**
  1228. * Checks if the user needs to update PHP.
  1229. *
  1230. * @since 5.1.0
  1231. * @since 5.1.1 Added the {@see 'wp_is_php_version_acceptable'} filter.
  1232. *
  1233. * @return array|false $response Array of PHP version data. False on failure.
  1234. */
  1235. function wp_check_php_version() {
  1236. $version = phpversion();
  1237. $key = md5( $version );
  1238. $response = get_site_transient( 'php_check_' . $key );
  1239. if ( false === $response ) {
  1240. $url = 'http://api.wordpress.org/core/serve-happy/1.0/';
  1241. if ( wp_http_supports( array( 'ssl' ) ) ) {
  1242. $url = set_url_scheme( $url, 'https' );
  1243. }
  1244. $url = add_query_arg( 'php_version', $version, $url );
  1245. $response = wp_remote_get( $url );
  1246. if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
  1247. return false;
  1248. }
  1249. /**
  1250. * Response should be an array with:
  1251. * 'recommended_version' - string - The PHP version recommended by WordPress.
  1252. * 'is_supported' - boolean - Whether the PHP version is actively supported.
  1253. * 'is_secure' - boolean - Whether the PHP version receives security updates.
  1254. * 'is_acceptable' - boolean - Whether the PHP version is still acceptable for WordPress.
  1255. */
  1256. $response = json_decode( wp_remote_retrieve_body( $response ), true );
  1257. if ( ! is_array( $response ) ) {
  1258. return false;
  1259. }
  1260. set_site_transient( 'php_check_' . $key, $response, WEEK_IN_SECONDS );
  1261. }
  1262. if ( isset( $response['is_acceptable'] ) && $response['is_acceptable'] ) {
  1263. /**
  1264. * Filters whether the active PHP version is considered acceptable by WordPress.
  1265. *
  1266. * Returning false will trigger a PHP version warning to show up in the admin dashboard to administrators.
  1267. *
  1268. * This filter is only run if the wordpress.org Serve Happy API considers the PHP version acceptable, ensuring
  1269. * that this filter can only make this check stricter, but not loosen it.
  1270. *
  1271. * @since 5.1.1
  1272. *
  1273. * @param bool $is_acceptable Whether the PHP version is considered acceptable. Default true.
  1274. * @param string $version PHP version checked.
  1275. */
  1276. $response['is_acceptable'] = (bool) apply_filters( 'wp_is_php_version_acceptable', true, $version );
  1277. }
  1278. return $response;
  1279. }