PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/APP/wp-admin/includes/misc.php

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