No Description

misc.php 43KB

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