PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/misc.php

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