PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/misc.php

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