Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

939 rader
26 KiB

  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
  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
  59. * @param string $marker
  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. if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
  68. {
  69. $state = false;
  70. foreach ( $markerdata as $markerline ) {
  71. if (strpos($markerline, '# END ' . $marker) !== false)
  72. $state = false;
  73. if ( $state )
  74. $result[] = $markerline;
  75. if (strpos($markerline, '# BEGIN ' . $marker) !== false)
  76. $state = true;
  77. }
  78. }
  79. return $result;
  80. }
  81. /**
  82. * Inserts an array of strings into a file (.htaccess ), placing it between
  83. * BEGIN and END markers.
  84. *
  85. * Replaces existing marked info. Retains surrounding
  86. * data. Creates file if none exists.
  87. *
  88. * @since 1.5.0
  89. *
  90. * @param string $filename Filename to alter.
  91. * @param string $marker The marker to alter.
  92. * @param array|string $insertion The new content to insert.
  93. * @return bool True on write success, false on failure.
  94. */
  95. function insert_with_markers( $filename, $marker, $insertion ) {
  96. if ( ! file_exists( $filename ) ) {
  97. if ( ! is_writable( dirname( $filename ) ) ) {
  98. return false;
  99. }
  100. if ( ! touch( $filename ) ) {
  101. return false;
  102. }
  103. } elseif ( ! is_writeable( $filename ) ) {
  104. return false;
  105. }
  106. if ( ! is_array( $insertion ) ) {
  107. $insertion = explode( "\n", $insertion );
  108. }
  109. $start_marker = "# BEGIN {$marker}";
  110. $end_marker = "# END {$marker}";
  111. $fp = fopen( $filename, 'r+' );
  112. if ( ! $fp ) {
  113. return false;
  114. }
  115. // Attempt to get a lock. If the filesystem supports locking, this will block until the lock is acquired.
  116. flock( $fp, LOCK_EX );
  117. $lines = array();
  118. while ( ! feof( $fp ) ) {
  119. $lines[] = rtrim( fgets( $fp ), "\r\n" );
  120. }
  121. // Split out the existing file into the preceding lines, and those that appear after the marker
  122. $pre_lines = $post_lines = $existing_lines = array();
  123. $found_marker = $found_end_marker = false;
  124. foreach ( $lines as $line ) {
  125. if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) {
  126. $found_marker = true;
  127. continue;
  128. } elseif ( ! $found_end_marker && false !== strpos( $line, $end_marker ) ) {
  129. $found_end_marker = true;
  130. continue;
  131. }
  132. if ( ! $found_marker ) {
  133. $pre_lines[] = $line;
  134. } elseif ( $found_marker && $found_end_marker ) {
  135. $post_lines[] = $line;
  136. } else {
  137. $existing_lines[] = $line;
  138. }
  139. }
  140. // Check to see if there was a change
  141. if ( $existing_lines === $insertion ) {
  142. flock( $fp, LOCK_UN );
  143. fclose( $fp );
  144. return true;
  145. }
  146. // Generate the new file data
  147. $new_file_data = implode( "\n", array_merge(
  148. $pre_lines,
  149. array( $start_marker ),
  150. $insertion,
  151. array( $end_marker ),
  152. $post_lines
  153. ) );
  154. // Write to the start of the file, and truncate it to that length
  155. fseek( $fp, 0 );
  156. $bytes = fwrite( $fp, $new_file_data );
  157. if ( $bytes ) {
  158. ftruncate( $fp, ftell( $fp ) );
  159. }
  160. fflush( $fp );
  161. flock( $fp, LOCK_UN );
  162. fclose( $fp );
  163. return (bool) $bytes;
  164. }
  165. /**
  166. * Updates the htaccess file with the current rules if it is writable.
  167. *
  168. * Always writes to the file if it exists and is writable to ensure that we
  169. * blank out old rules.
  170. *
  171. * @since 1.5.0
  172. *
  173. * @global WP_Rewrite $wp_rewrite
  174. */
  175. function save_mod_rewrite_rules() {
  176. if ( is_multisite() )
  177. return;
  178. global $wp_rewrite;
  179. $home_path = get_home_path();
  180. $htaccess_file = $home_path.'.htaccess';
  181. /*
  182. * If the file doesn't already exist check for write access to the directory
  183. * and whether we have some rules. Else check for write access to the file.
  184. */
  185. if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
  186. if ( got_mod_rewrite() ) {
  187. $rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
  188. return insert_with_markers( $htaccess_file, 'WordPress', $rules );
  189. }
  190. }
  191. return false;
  192. }
  193. /**
  194. * Updates the IIS web.config file with the current rules if it is writable.
  195. * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
  196. *
  197. * @since 2.8.0
  198. *
  199. * @global WP_Rewrite $wp_rewrite
  200. *
  201. * @return bool True if web.config was updated successfully
  202. */
  203. function iis7_save_url_rewrite_rules(){
  204. if ( is_multisite() )
  205. return;
  206. global $wp_rewrite;
  207. $home_path = get_home_path();
  208. $web_config_file = $home_path . 'web.config';
  209. // Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
  210. 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) ) ) {
  211. $rule = $wp_rewrite->iis7_url_rewrite_rules(false, '', '');
  212. if ( ! empty($rule) ) {
  213. return iis7_add_rewrite_rule($web_config_file, $rule);
  214. } else {
  215. return iis7_delete_rewrite_rule($web_config_file);
  216. }
  217. }
  218. return false;
  219. }
  220. /**
  221. * Update the "recently-edited" file for the plugin or theme editor.
  222. *
  223. * @since 1.5.0
  224. *
  225. * @param string $file
  226. */
  227. function update_recently_edited( $file ) {
  228. $oldfiles = (array ) get_option( 'recently_edited' );
  229. if ( $oldfiles ) {
  230. $oldfiles = array_reverse( $oldfiles );
  231. $oldfiles[] = $file;
  232. $oldfiles = array_reverse( $oldfiles );
  233. $oldfiles = array_unique( $oldfiles );
  234. if ( 5 < count( $oldfiles ))
  235. array_pop( $oldfiles );
  236. } else {
  237. $oldfiles[] = $file;
  238. }
  239. update_option( 'recently_edited', $oldfiles );
  240. }
  241. /**
  242. * Flushes rewrite rules if siteurl, home or page_on_front changed.
  243. *
  244. * @since 2.1.0
  245. *
  246. * @param string $old_value
  247. * @param string $value
  248. */
  249. function update_home_siteurl( $old_value, $value ) {
  250. if ( wp_installing() )
  251. return;
  252. if ( is_multisite() && ms_is_switched() ) {
  253. delete_option( 'rewrite_rules' );
  254. } else {
  255. flush_rewrite_rules();
  256. }
  257. }
  258. /**
  259. * Resets global variables based on $_GET and $_POST
  260. *
  261. * This function resets global variables based on the names passed
  262. * in the $vars array to the value of $_POST[$var] or $_GET[$var] or ''
  263. * if neither is defined.
  264. *
  265. * @since 2.0.0
  266. *
  267. * @param array $vars An array of globals to reset.
  268. */
  269. function wp_reset_vars( $vars ) {
  270. foreach ( $vars as $var ) {
  271. if ( empty( $_POST[ $var ] ) ) {
  272. if ( empty( $_GET[ $var ] ) ) {
  273. $GLOBALS[ $var ] = '';
  274. } else {
  275. $GLOBALS[ $var ] = $_GET[ $var ];
  276. }
  277. } else {
  278. $GLOBALS[ $var ] = $_POST[ $var ];
  279. }
  280. }
  281. }
  282. /**
  283. * Displays the given administration message.
  284. *
  285. * @since 2.1.0
  286. *
  287. * @param string|WP_Error $message
  288. */
  289. function show_message($message) {
  290. if ( is_wp_error($message) ){
  291. if ( $message->get_error_data() && is_string( $message->get_error_data() ) )
  292. $message = $message->get_error_message() . ': ' . $message->get_error_data();
  293. else
  294. $message = $message->get_error_message();
  295. }
  296. echo "<p>$message</p>\n";
  297. wp_ob_end_flush_all();
  298. flush();
  299. }
  300. /**
  301. * @since 2.8.0
  302. *
  303. * @param string $content
  304. * @return array
  305. */
  306. function wp_doc_link_parse( $content ) {
  307. if ( !is_string( $content ) || empty( $content ) )
  308. return array();
  309. if ( !function_exists('token_get_all') )
  310. return array();
  311. $tokens = token_get_all( $content );
  312. $count = count( $tokens );
  313. $functions = array();
  314. $ignore_functions = array();
  315. for ( $t = 0; $t < $count - 2; $t++ ) {
  316. if ( ! is_array( $tokens[ $t ] ) ) {
  317. continue;
  318. }
  319. if ( T_STRING == $tokens[ $t ][0] && ( '(' == $tokens[ $t + 1 ] || '(' == $tokens[ $t + 2 ] ) ) {
  320. // If it's a function or class defined locally, there's not going to be any docs available
  321. 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] ) ) {
  322. $ignore_functions[] = $tokens[$t][1];
  323. }
  324. // Add this to our stack of unique references
  325. $functions[] = $tokens[$t][1];
  326. }
  327. }
  328. $functions = array_unique( $functions );
  329. sort( $functions );
  330. /**
  331. * Filters the list of functions and classes to be ignored from the documentation lookup.
  332. *
  333. * @since 2.8.0
  334. *
  335. * @param array $ignore_functions Functions and classes to be ignored.
  336. */
  337. $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
  338. $ignore_functions = array_unique( $ignore_functions );
  339. $out = array();
  340. foreach ( $functions as $function ) {
  341. if ( in_array( $function, $ignore_functions ) )
  342. continue;
  343. $out[] = $function;
  344. }
  345. return $out;
  346. }
  347. /**
  348. * Saves option for number of rows when listing posts, pages, comments, etc.
  349. *
  350. * @since 2.8.0
  351. */
  352. function set_screen_options() {
  353. if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) ) {
  354. check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
  355. if ( !$user = wp_get_current_user() )
  356. return;
  357. $option = $_POST['wp_screen_options']['option'];
  358. $value = $_POST['wp_screen_options']['value'];
  359. if ( $option != sanitize_key( $option ) )
  360. return;
  361. $map_option = $option;
  362. $type = str_replace('edit_', '', $map_option);
  363. $type = str_replace('_per_page', '', $type);
  364. if ( in_array( $type, get_taxonomies() ) )
  365. $map_option = 'edit_tags_per_page';
  366. elseif ( in_array( $type, get_post_types() ) )
  367. $map_option = 'edit_per_page';
  368. else
  369. $option = str_replace('-', '_', $option);
  370. switch ( $map_option ) {
  371. case 'edit_per_page':
  372. case 'users_per_page':
  373. case 'edit_comments_per_page':
  374. case 'upload_per_page':
  375. case 'edit_tags_per_page':
  376. case 'plugins_per_page':
  377. // Network admin
  378. case 'sites_network_per_page':
  379. case 'users_network_per_page':
  380. case 'site_users_network_per_page':
  381. case 'plugins_network_per_page':
  382. case 'themes_network_per_page':
  383. case 'site_themes_network_per_page':
  384. $value = (int) $value;
  385. if ( $value < 1 || $value > 999 )
  386. return;
  387. break;
  388. default:
  389. /**
  390. * Filters a screen option value before it is set.
  391. *
  392. * The filter can also be used to modify non-standard [items]_per_page
  393. * settings. See the parent function for a full list of standard options.
  394. *
  395. * Returning false to the filter will skip saving the current option.
  396. *
  397. * @since 2.8.0
  398. *
  399. * @see set_screen_options()
  400. *
  401. * @param bool|int $value Screen option value. Default false to skip.
  402. * @param string $option The option name.
  403. * @param int $value The number of rows to use.
  404. */
  405. $value = apply_filters( 'set-screen-option', false, $option, $value );
  406. if ( false === $value )
  407. return;
  408. break;
  409. }
  410. update_user_meta($user->ID, $option, $value);
  411. $url = remove_query_arg( array( 'pagenum', 'apage', 'paged' ), wp_get_referer() );
  412. if ( isset( $_POST['mode'] ) ) {
  413. $url = add_query_arg( array( 'mode' => $_POST['mode'] ), $url );
  414. }
  415. wp_safe_redirect( $url );
  416. exit;
  417. }
  418. }
  419. /**
  420. * Check if rewrite rule for WordPress already exists in the IIS 7+ configuration file
  421. *
  422. * @since 2.8.0
  423. *
  424. * @return bool
  425. * @param string $filename The file path to the configuration file
  426. */
  427. function iis7_rewrite_rule_exists($filename) {
  428. if ( ! file_exists($filename) )
  429. return false;
  430. if ( ! class_exists( 'DOMDocument', false ) ) {
  431. return false;
  432. }
  433. $doc = new DOMDocument();
  434. if ( $doc->load($filename) === false )
  435. return false;
  436. $xpath = new DOMXPath($doc);
  437. $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]');
  438. if ( $rules->length == 0 )
  439. return false;
  440. else
  441. return true;
  442. }
  443. /**
  444. * Delete WordPress rewrite rule from web.config file if it exists there
  445. *
  446. * @since 2.8.0
  447. *
  448. * @param string $filename Name of the configuration file
  449. * @return bool
  450. */
  451. function iis7_delete_rewrite_rule($filename) {
  452. // If configuration file does not exist then rules also do not exist so there is nothing to delete
  453. if ( ! file_exists($filename) )
  454. return true;
  455. if ( ! class_exists( 'DOMDocument', false ) ) {
  456. return false;
  457. }
  458. $doc = new DOMDocument();
  459. $doc->preserveWhiteSpace = false;
  460. if ( $doc -> load($filename) === false )
  461. return false;
  462. $xpath = new DOMXPath($doc);
  463. $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]');
  464. if ( $rules->length > 0 ) {
  465. $child = $rules->item(0);
  466. $parent = $child->parentNode;
  467. $parent->removeChild($child);
  468. $doc->formatOutput = true;
  469. saveDomDocument($doc, $filename);
  470. }
  471. return true;
  472. }
  473. /**
  474. * Add WordPress rewrite rule to the IIS 7+ configuration file.
  475. *
  476. * @since 2.8.0
  477. *
  478. * @param string $filename The file path to the configuration file
  479. * @param string $rewrite_rule The XML fragment with URL Rewrite rule
  480. * @return bool
  481. */
  482. function iis7_add_rewrite_rule($filename, $rewrite_rule) {
  483. if ( ! class_exists( 'DOMDocument', false ) ) {
  484. return false;
  485. }
  486. // If configuration file does not exist then we create one.
  487. if ( ! file_exists($filename) ) {
  488. $fp = fopen( $filename, 'w');
  489. fwrite($fp, '<configuration/>');
  490. fclose($fp);
  491. }
  492. $doc = new DOMDocument();
  493. $doc->preserveWhiteSpace = false;
  494. if ( $doc->load($filename) === false )
  495. return false;
  496. $xpath = new DOMXPath($doc);
  497. // First check if the rule already exists as in that case there is no need to re-add it
  498. $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]');
  499. if ( $wordpress_rules->length > 0 )
  500. return true;
  501. // Check the XPath to the rewrite rule and create XML nodes if they do not exist
  502. $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
  503. if ( $xmlnodes->length > 0 ) {
  504. $rules_node = $xmlnodes->item(0);
  505. } else {
  506. $rules_node = $doc->createElement('rules');
  507. $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
  508. if ( $xmlnodes->length > 0 ) {
  509. $rewrite_node = $xmlnodes->item(0);
  510. $rewrite_node->appendChild($rules_node);
  511. } else {
  512. $rewrite_node = $doc->createElement('rewrite');
  513. $rewrite_node->appendChild($rules_node);
  514. $xmlnodes = $xpath->query('/configuration/system.webServer');
  515. if ( $xmlnodes->length > 0 ) {
  516. $system_webServer_node = $xmlnodes->item(0);
  517. $system_webServer_node->appendChild($rewrite_node);
  518. } else {
  519. $system_webServer_node = $doc->createElement('system.webServer');
  520. $system_webServer_node->appendChild($rewrite_node);
  521. $xmlnodes = $xpath->query('/configuration');
  522. if ( $xmlnodes->length > 0 ) {
  523. $config_node = $xmlnodes->item(0);
  524. $config_node->appendChild($system_webServer_node);
  525. } else {
  526. $config_node = $doc->createElement('configuration');
  527. $doc->appendChild($config_node);
  528. $config_node->appendChild($system_webServer_node);
  529. }
  530. }
  531. }
  532. }
  533. $rule_fragment = $doc->createDocumentFragment();
  534. $rule_fragment->appendXML($rewrite_rule);
  535. $rules_node->appendChild($rule_fragment);
  536. $doc->encoding = "UTF-8";
  537. $doc->formatOutput = true;
  538. saveDomDocument($doc, $filename);
  539. return true;
  540. }
  541. /**
  542. * Saves the XML document into a file
  543. *
  544. * @since 2.8.0
  545. *
  546. * @param DOMDocument $doc
  547. * @param string $filename
  548. */
  549. function saveDomDocument($doc, $filename) {
  550. $config = $doc->saveXML();
  551. $config = preg_replace("/([^\r])\n/", "$1\r\n", $config);
  552. $fp = fopen($filename, 'w');
  553. fwrite($fp, $config);
  554. fclose($fp);
  555. }
  556. /**
  557. * Display the default admin color scheme picker (Used in user-edit.php)
  558. *
  559. * @since 3.0.0
  560. *
  561. * @global array $_wp_admin_css_colors
  562. *
  563. * @param int $user_id User ID.
  564. */
  565. function admin_color_scheme_picker( $user_id ) {
  566. global $_wp_admin_css_colors;
  567. ksort( $_wp_admin_css_colors );
  568. if ( isset( $_wp_admin_css_colors['fresh'] ) ) {
  569. // Set Default ('fresh') and Light should go first.
  570. $_wp_admin_css_colors = array_filter( array_merge( array( 'fresh' => '', 'light' => '' ), $_wp_admin_css_colors ) );
  571. }
  572. $current_color = get_user_option( 'admin_color', $user_id );
  573. if ( empty( $current_color ) || ! isset( $_wp_admin_css_colors[ $current_color ] ) ) {
  574. $current_color = 'fresh';
  575. }
  576. ?>
  577. <fieldset id="color-picker" class="scheme-list">
  578. <legend class="screen-reader-text"><span><?php _e( 'Admin Color Scheme' ); ?></span></legend>
  579. <?php
  580. wp_nonce_field( 'save-color-scheme', 'color-nonce', false );
  581. foreach ( $_wp_admin_css_colors as $color => $color_info ) :
  582. ?>
  583. <div class="color-option <?php echo ( $color == $current_color ) ? 'selected' : ''; ?>">
  584. <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 ); ?> />
  585. <input type="hidden" class="css_url" value="<?php echo esc_url( $color_info->url ); ?>" />
  586. <input type="hidden" class="icon_colors" value="<?php echo esc_attr( wp_json_encode( array( 'icons' => $color_info->icon_colors ) ) ); ?>" />
  587. <label for="admin_color_<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $color_info->name ); ?></label>
  588. <table class="color-palette">
  589. <tr>
  590. <?php
  591. foreach ( $color_info->colors as $html_color ) {
  592. ?>
  593. <td style="background-color: <?php echo esc_attr( $html_color ); ?>">&nbsp;</td>
  594. <?php
  595. }
  596. ?>
  597. </tr>
  598. </table>
  599. </div>
  600. <?php
  601. endforeach;
  602. ?>
  603. </fieldset>
  604. <?php
  605. }
  606. /**
  607. *
  608. * @global array $_wp_admin_css_colors
  609. */
  610. function wp_color_scheme_settings() {
  611. global $_wp_admin_css_colors;
  612. $color_scheme = get_user_option( 'admin_color' );
  613. // It's possible to have a color scheme set that is no longer registered.
  614. if ( empty( $_wp_admin_css_colors[ $color_scheme ] ) ) {
  615. $color_scheme = 'fresh';
  616. }
  617. if ( ! empty( $_wp_admin_css_colors[ $color_scheme ]->icon_colors ) ) {
  618. $icon_colors = $_wp_admin_css_colors[ $color_scheme ]->icon_colors;
  619. } elseif ( ! empty( $_wp_admin_css_colors['fresh']->icon_colors ) ) {
  620. $icon_colors = $_wp_admin_css_colors['fresh']->icon_colors;
  621. } else {
  622. // Fall back to the default set of icon colors if the default scheme is missing.
  623. $icon_colors = array( 'base' => '#82878c', 'focus' => '#00a0d2', 'current' => '#fff' );
  624. }
  625. echo '<script type="text/javascript">var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ) ) . ";</script>\n";
  626. }
  627. /**
  628. * @since 3.3.0
  629. */
  630. function _ipad_meta() {
  631. if ( wp_is_mobile() ) {
  632. ?>
  633. <meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
  634. <?php
  635. }
  636. }
  637. /**
  638. * Check lock status for posts displayed on the Posts screen
  639. *
  640. * @since 3.6.0
  641. *
  642. * @param array $response The Heartbeat response.
  643. * @param array $data The $_POST data sent.
  644. * @param string $screen_id The screen id.
  645. * @return array The Heartbeat response.
  646. */
  647. function wp_check_locked_posts( $response, $data, $screen_id ) {
  648. $checked = array();
  649. if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
  650. foreach ( $data['wp-check-locked-posts'] as $key ) {
  651. if ( ! $post_id = absint( substr( $key, 5 ) ) )
  652. continue;
  653. if ( ( $user_id = wp_check_post_lock( $post_id ) ) && ( $user = get_userdata( $user_id ) ) && current_user_can( 'edit_post', $post_id ) ) {
  654. $send = array( 'text' => sprintf( __( '%s is currently editing' ), $user->display_name ) );
  655. if ( ( $avatar = get_avatar( $user->ID, 18 ) ) && preg_match( "|src='([^']+)'|", $avatar, $matches ) )
  656. $send['avatar_src'] = $matches[1];
  657. $checked[$key] = $send;
  658. }
  659. }
  660. }
  661. if ( ! empty( $checked ) )
  662. $response['wp-check-locked-posts'] = $checked;
  663. return $response;
  664. }
  665. /**
  666. * Check lock status on the New/Edit Post screen and refresh the lock
  667. *
  668. * @since 3.6.0
  669. *
  670. * @param array $response The Heartbeat response.
  671. * @param array $data The $_POST data sent.
  672. * @param string $screen_id The screen id.
  673. * @return array The Heartbeat response.
  674. */
  675. function wp_refresh_post_lock( $response, $data, $screen_id ) {
  676. if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
  677. $received = $data['wp-refresh-post-lock'];
  678. $send = array();
  679. if ( ! $post_id = absint( $received['post_id'] ) )
  680. return $response;
  681. if ( ! current_user_can('edit_post', $post_id) )
  682. return $response;
  683. if ( ( $user_id = wp_check_post_lock( $post_id ) ) && ( $user = get_userdata( $user_id ) ) ) {
  684. $error = array(
  685. 'text' => sprintf( __( '%s has taken over and is currently editing.' ), $user->display_name )
  686. );
  687. if ( $avatar = get_avatar( $user->ID, 64 ) ) {
  688. if ( preg_match( "|src='([^']+)'|", $avatar, $matches ) )
  689. $error['avatar_src'] = $matches[1];
  690. }
  691. $send['lock_error'] = $error;
  692. } else {
  693. if ( $new_lock = wp_set_post_lock( $post_id ) )
  694. $send['new_lock'] = implode( ':', $new_lock );
  695. }
  696. $response['wp-refresh-post-lock'] = $send;
  697. }
  698. return $response;
  699. }
  700. /**
  701. * Check nonce expiration on the New/Edit Post screen and refresh if needed
  702. *
  703. * @since 3.6.0
  704. *
  705. * @param array $response The Heartbeat response.
  706. * @param array $data The $_POST data sent.
  707. * @param string $screen_id The screen id.
  708. * @return array The Heartbeat response.
  709. */
  710. function wp_refresh_post_nonces( $response, $data, $screen_id ) {
  711. if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
  712. $received = $data['wp-refresh-post-nonces'];
  713. $response['wp-refresh-post-nonces'] = array( 'check' => 1 );
  714. if ( ! $post_id = absint( $received['post_id'] ) ) {
  715. return $response;
  716. }
  717. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  718. return $response;
  719. }
  720. $response['wp-refresh-post-nonces'] = array(
  721. 'replace' => array(
  722. 'getpermalinknonce' => wp_create_nonce('getpermalink'),
  723. 'samplepermalinknonce' => wp_create_nonce('samplepermalink'),
  724. 'closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
  725. '_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
  726. '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
  727. ),
  728. 'heartbeatNonce' => wp_create_nonce( 'heartbeat-nonce' ),
  729. );
  730. }
  731. return $response;
  732. }
  733. /**
  734. * Disable suspension of Heartbeat on the Add/Edit Post screens.
  735. *
  736. * @since 3.8.0
  737. *
  738. * @global string $pagenow
  739. *
  740. * @param array $settings An array of Heartbeat settings.
  741. * @return array Filtered Heartbeat settings.
  742. */
  743. function wp_heartbeat_set_suspension( $settings ) {
  744. global $pagenow;
  745. if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
  746. $settings['suspension'] = 'disable';
  747. }
  748. return $settings;
  749. }
  750. /**
  751. * Autosave with heartbeat
  752. *
  753. * @since 3.9.0
  754. *
  755. * @param array $response The Heartbeat response.
  756. * @param array $data The $_POST data sent.
  757. * @return array The Heartbeat response.
  758. */
  759. function heartbeat_autosave( $response, $data ) {
  760. if ( ! empty( $data['wp_autosave'] ) ) {
  761. $saved = wp_autosave( $data['wp_autosave'] );
  762. if ( is_wp_error( $saved ) ) {
  763. $response['wp_autosave'] = array( 'success' => false, 'message' => $saved->get_error_message() );
  764. } elseif ( empty( $saved ) ) {
  765. $response['wp_autosave'] = array( 'success' => false, 'message' => __( 'Error while saving.' ) );
  766. } else {
  767. /* translators: draft saved date format, see https://secure.php.net/date */
  768. $draft_saved_date_format = __( 'g:i:s a' );
  769. /* translators: %s: date and time */
  770. $response['wp_autosave'] = array( 'success' => true, 'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ) );
  771. }
  772. }
  773. return $response;
  774. }
  775. /**
  776. * Remove single-use URL parameters and create canonical link based on new URL.
  777. *
  778. * Remove specific query string parameters from a URL, create the canonical link,
  779. * put it in the admin header, and change the current URL to match.
  780. *
  781. * @since 4.2.0
  782. */
  783. function wp_admin_canonical_url() {
  784. $removable_query_args = wp_removable_query_args();
  785. if ( empty( $removable_query_args ) ) {
  786. return;
  787. }
  788. // Ensure we're using an absolute URL.
  789. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  790. $filtered_url = remove_query_arg( $removable_query_args, $current_url );
  791. ?>
  792. <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
  793. <script>
  794. if ( window.history.replaceState ) {
  795. window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
  796. }
  797. </script>
  798. <?php
  799. }
  800. /**
  801. * Outputs JS that reloads the page if the user navigated to it with the Back or Forward button.
  802. *
  803. * Used on the Edit Post and Add New Post screens. Needed to ensure the page is not loaded from browser cache,
  804. * so the post title and editor content are the last saved versions. Ideally this script should run first in the head.
  805. *
  806. * @since 4.6.0
  807. */
  808. function wp_page_reload_on_back_button_js() {
  809. ?>
  810. <script>
  811. if ( typeof performance !== 'undefined' && performance.navigation && performance.navigation.type === 2 ) {
  812. document.location.reload( true );
  813. }
  814. </script>
  815. <?php
  816. }