PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/endomorphosis/falkenstein
PHP | 770 lines | 483 code | 93 blank | 194 comment | 119 complexity | f57ff0377fc5699f0852a88d4bdc55e2 MD5 | raw file
  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. $functions = array();
  287. $ignore_functions = array();
  288. for ( $t = 0, $count = count( $tokens ); $t < $count; $t++ ) {
  289. if ( !is_array( $tokens[$t] ) ) continue;
  290. if ( T_STRING == $tokens[$t][0] && ( '(' == $tokens[ $t + 1 ] || '(' == $tokens[ $t + 2 ] ) ) {
  291. // If it's a function or class defined locally, there's not going to be any docs available
  292. 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] ) ) {
  293. $ignore_functions[] = $tokens[$t][1];
  294. }
  295. // Add this to our stack of unique references
  296. $functions[] = $tokens[$t][1];
  297. }
  298. }
  299. $functions = array_unique( $functions );
  300. sort( $functions );
  301. $ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
  302. $ignore_functions = array_unique( $ignore_functions );
  303. $out = array();
  304. foreach ( $functions as $function ) {
  305. if ( in_array( $function, $ignore_functions ) )
  306. continue;
  307. $out[] = $function;
  308. }
  309. return $out;
  310. }
  311. /**
  312. * Saves option for number of rows when listing posts, pages, comments, etc.
  313. *
  314. * @since 2.8
  315. **/
  316. function set_screen_options() {
  317. if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) ) {
  318. check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
  319. if ( !$user = wp_get_current_user() )
  320. return;
  321. $option = $_POST['wp_screen_options']['option'];
  322. $value = $_POST['wp_screen_options']['value'];
  323. if ( $option != sanitize_key( $option ) )
  324. return;
  325. $map_option = $option;
  326. $type = str_replace('edit_', '', $map_option);
  327. $type = str_replace('_per_page', '', $type);
  328. if ( in_array( $type, get_taxonomies() ) )
  329. $map_option = 'edit_tags_per_page';
  330. elseif ( in_array( $type, get_post_types() ) )
  331. $map_option = 'edit_per_page';
  332. else
  333. $option = str_replace('-', '_', $option);
  334. switch ( $map_option ) {
  335. case 'edit_per_page':
  336. case 'users_per_page':
  337. case 'edit_comments_per_page':
  338. case 'upload_per_page':
  339. case 'edit_tags_per_page':
  340. case 'plugins_per_page':
  341. // Network admin
  342. case 'sites_network_per_page':
  343. case 'users_network_per_page':
  344. case 'site_users_network_per_page':
  345. case 'plugins_network_per_page':
  346. case 'themes_network_per_page':
  347. case 'site_themes_network_per_page':
  348. $value = (int) $value;
  349. if ( $value < 1 || $value > 999 )
  350. return;
  351. break;
  352. default:
  353. $value = apply_filters('set-screen-option', false, $option, $value);
  354. if ( false === $value )
  355. return;
  356. break;
  357. }
  358. update_user_meta($user->ID, $option, $value);
  359. wp_safe_redirect( remove_query_arg( array('pagenum', 'apage', 'paged'), wp_get_referer() ) );
  360. exit;
  361. }
  362. }
  363. /**
  364. * Check if rewrite rule for WordPress already exists in the IIS 7+ configuration file
  365. *
  366. * @since 2.8.0
  367. *
  368. * @return bool
  369. * @param string $filename The file path to the configuration file
  370. */
  371. function iis7_rewrite_rule_exists($filename) {
  372. if ( ! file_exists($filename) )
  373. return false;
  374. if ( ! class_exists('DOMDocument') )
  375. return false;
  376. $doc = new DOMDocument();
  377. if ( $doc->load($filename) === false )
  378. return false;
  379. $xpath = new DOMXPath($doc);
  380. $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
  381. if ( $rules->length == 0 )
  382. return false;
  383. else
  384. return true;
  385. }
  386. /**
  387. * Delete WordPress rewrite rule from web.config file if it exists there
  388. *
  389. * @since 2.8.0
  390. *
  391. * @param string $filename Name of the configuration file
  392. * @return bool
  393. */
  394. function iis7_delete_rewrite_rule($filename) {
  395. // If configuration file does not exist then rules also do not exist so there is nothing to delete
  396. if ( ! file_exists($filename) )
  397. return true;
  398. if ( ! class_exists('DOMDocument') )
  399. return false;
  400. $doc = new DOMDocument();
  401. $doc->preserveWhiteSpace = false;
  402. if ( $doc -> load($filename) === false )
  403. return false;
  404. $xpath = new DOMXPath($doc);
  405. $rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
  406. if ( $rules->length > 0 ) {
  407. $child = $rules->item(0);
  408. $parent = $child->parentNode;
  409. $parent->removeChild($child);
  410. $doc->formatOutput = true;
  411. saveDomDocument($doc, $filename);
  412. }
  413. return true;
  414. }
  415. /**
  416. * Add WordPress rewrite rule to the IIS 7+ configuration file.
  417. *
  418. * @since 2.8.0
  419. *
  420. * @param string $filename The file path to the configuration file
  421. * @param string $rewrite_rule The XML fragment with URL Rewrite rule
  422. * @return bool
  423. */
  424. function iis7_add_rewrite_rule($filename, $rewrite_rule) {
  425. if ( ! class_exists('DOMDocument') )
  426. return false;
  427. // If configuration file does not exist then we create one.
  428. if ( ! file_exists($filename) ) {
  429. $fp = fopen( $filename, 'w');
  430. fwrite($fp, '<configuration/>');
  431. fclose($fp);
  432. }
  433. $doc = new DOMDocument();
  434. $doc->preserveWhiteSpace = false;
  435. if ( $doc->load($filename) === false )
  436. return false;
  437. $xpath = new DOMXPath($doc);
  438. // First check if the rule already exists as in that case there is no need to re-add it
  439. $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
  440. if ( $wordpress_rules->length > 0 )
  441. return true;
  442. // Check the XPath to the rewrite rule and create XML nodes if they do not exist
  443. $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
  444. if ( $xmlnodes->length > 0 ) {
  445. $rules_node = $xmlnodes->item(0);
  446. } else {
  447. $rules_node = $doc->createElement('rules');
  448. $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
  449. if ( $xmlnodes->length > 0 ) {
  450. $rewrite_node = $xmlnodes->item(0);
  451. $rewrite_node->appendChild($rules_node);
  452. } else {
  453. $rewrite_node = $doc->createElement('rewrite');
  454. $rewrite_node->appendChild($rules_node);
  455. $xmlnodes = $xpath->query('/configuration/system.webServer');
  456. if ( $xmlnodes->length > 0 ) {
  457. $system_webServer_node = $xmlnodes->item(0);
  458. $system_webServer_node->appendChild($rewrite_node);
  459. } else {
  460. $system_webServer_node = $doc->createElement('system.webServer');
  461. $system_webServer_node->appendChild($rewrite_node);
  462. $xmlnodes = $xpath->query('/configuration');
  463. if ( $xmlnodes->length > 0 ) {
  464. $config_node = $xmlnodes->item(0);
  465. $config_node->appendChild($system_webServer_node);
  466. } else {
  467. $config_node = $doc->createElement('configuration');
  468. $doc->appendChild($config_node);
  469. $config_node->appendChild($system_webServer_node);
  470. }
  471. }
  472. }
  473. }
  474. $rule_fragment = $doc->createDocumentFragment();
  475. $rule_fragment->appendXML($rewrite_rule);
  476. $rules_node->appendChild($rule_fragment);
  477. $doc->encoding = "UTF-8";
  478. $doc->formatOutput = true;
  479. saveDomDocument($doc, $filename);
  480. return true;
  481. }
  482. /**
  483. * Saves the XML document into a file
  484. *
  485. * @since 2.8.0
  486. *
  487. * @param DOMDocument $doc
  488. * @param string $filename
  489. */
  490. function saveDomDocument($doc, $filename) {
  491. $config = $doc->saveXML();
  492. $config = preg_replace("/([^\r])\n/", "$1\r\n", $config);
  493. $fp = fopen($filename, 'w');
  494. fwrite($fp, $config);
  495. fclose($fp);
  496. }
  497. /**
  498. * Display the default admin color scheme picker (Used in user-edit.php)
  499. *
  500. * @since 3.0.0
  501. */
  502. function admin_color_scheme_picker( $user_id ) {
  503. global $_wp_admin_css_colors;
  504. ksort( $_wp_admin_css_colors );
  505. if ( isset( $_wp_admin_css_colors['fresh'] ) ) {
  506. // Set Default ('fresh') and Light should go first.
  507. $_wp_admin_css_colors = array_filter( array_merge( array( 'fresh' => '', 'light' => '' ), $_wp_admin_css_colors ) );
  508. }
  509. $current_color = get_user_option( 'admin_color', $user_id );
  510. if ( empty( $current_color ) || ! isset( $_wp_admin_css_colors[ $current_color ] ) ) {
  511. $current_color = 'fresh';
  512. }
  513. ?>
  514. <fieldset id="color-picker" class="scheme-list">
  515. <legend class="screen-reader-text"><span><?php _e( 'Admin Color Scheme' ); ?></span></legend>
  516. <?php
  517. wp_nonce_field( 'save-color-scheme', 'color-nonce', false );
  518. foreach ( $_wp_admin_css_colors as $color => $color_info ) :
  519. ?>
  520. <div class="color-option <?php echo ( $color == $current_color ) ? 'selected' : ''; ?>">
  521. <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 ); ?> />
  522. <input type="hidden" class="css_url" value="<?php echo esc_url( $color_info->url ); ?>" />
  523. <input type="hidden" class="icon_colors" value="<?php echo esc_attr( json_encode( array( 'icons' => $color_info->icon_colors ) ) ); ?>" />
  524. <label for="admin_color_<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $color_info->name ); ?></label>
  525. <table class="color-palette">
  526. <tr>
  527. <?php
  528. foreach ( $color_info->colors as $html_color ) {
  529. ?>
  530. <td style="background-color: <?php echo esc_attr( $html_color ); ?>">&nbsp;</td>
  531. <?php
  532. }
  533. ?>
  534. </tr>
  535. </table>
  536. </div>
  537. <?php
  538. endforeach;
  539. ?>
  540. </fieldset>
  541. <?php
  542. }
  543. function wp_color_scheme_settings() {
  544. global $_wp_admin_css_colors;
  545. $color_scheme = get_user_option( 'admin_color' );
  546. // It's possible to have a color scheme set that is no longer registered.
  547. if ( empty( $_wp_admin_css_colors[ $color_scheme ] ) ) {
  548. $color_scheme = 'fresh';
  549. }
  550. if ( ! empty( $_wp_admin_css_colors[ $color_scheme ]->icon_colors ) ) {
  551. $icon_colors = $_wp_admin_css_colors[ $color_scheme ]->icon_colors;
  552. } elseif ( ! empty( $_wp_admin_css_colors['fresh']->icon_colors ) ) {
  553. $icon_colors = $_wp_admin_css_colors['fresh']->icon_colors;
  554. } else {
  555. // Fall back to the default set of icon colors if the default scheme is missing.
  556. $icon_colors = array( 'base' => '#999', 'focus' => '#2ea2cc', 'current' => '#fff' );
  557. }
  558. echo '<script type="text/javascript">var _wpColorScheme = ' . json_encode( array( 'icons' => $icon_colors ) ) . ";</script>\n";
  559. }
  560. add_action( 'admin_head', 'wp_color_scheme_settings' );
  561. function _ipad_meta() {
  562. if ( wp_is_mobile() ) {
  563. ?>
  564. <meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
  565. <?php
  566. }
  567. }
  568. add_action('admin_head', '_ipad_meta');
  569. /**
  570. * Check lock status for posts displayed on the Posts screen
  571. *
  572. * @since 3.6
  573. */
  574. function wp_check_locked_posts( $response, $data, $screen_id ) {
  575. $checked = array();
  576. if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
  577. foreach ( $data['wp-check-locked-posts'] as $key ) {
  578. if ( ! $post_id = absint( substr( $key, 5 ) ) )
  579. continue;
  580. if ( ( $user_id = wp_check_post_lock( $post_id ) ) && ( $user = get_userdata( $user_id ) ) && current_user_can( 'edit_post', $post_id ) ) {
  581. $send = array( 'text' => sprintf( __( '%s is currently editing' ), $user->display_name ) );
  582. if ( ( $avatar = get_avatar( $user->ID, 18 ) ) && preg_match( "|src='([^']+)'|", $avatar, $matches ) )
  583. $send['avatar_src'] = $matches[1];
  584. $checked[$key] = $send;
  585. }
  586. }
  587. }
  588. if ( ! empty( $checked ) )
  589. $response['wp-check-locked-posts'] = $checked;
  590. return $response;
  591. }
  592. add_filter( 'heartbeat_received', 'wp_check_locked_posts', 10, 3 );
  593. /**
  594. * Check lock status on the New/Edit Post screen and refresh the lock
  595. *
  596. * @since 3.6
  597. */
  598. function wp_refresh_post_lock( $response, $data, $screen_id ) {
  599. if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
  600. $received = $data['wp-refresh-post-lock'];
  601. $send = array();
  602. if ( ! $post_id = absint( $received['post_id'] ) )
  603. return $response;
  604. if ( ! current_user_can('edit_post', $post_id) )
  605. return $response;
  606. if ( ( $user_id = wp_check_post_lock( $post_id ) ) && ( $user = get_userdata( $user_id ) ) ) {
  607. $error = array(
  608. 'text' => sprintf( __( '%s has taken over and is currently editing.' ), $user->display_name )
  609. );
  610. if ( $avatar = get_avatar( $user->ID, 64 ) ) {
  611. if ( preg_match( "|src='([^']+)'|", $avatar, $matches ) )
  612. $error['avatar_src'] = $matches[1];
  613. }
  614. $send['lock_error'] = $error;
  615. } else {
  616. if ( $new_lock = wp_set_post_lock( $post_id ) )
  617. $send['new_lock'] = implode( ':', $new_lock );
  618. }
  619. $response['wp-refresh-post-lock'] = $send;
  620. }
  621. return $response;
  622. }
  623. add_filter( 'heartbeat_received', 'wp_refresh_post_lock', 10, 3 );
  624. /**
  625. * Check nonce expiration on the New/Edit Post screen and refresh if needed
  626. *
  627. * @since 3.6
  628. */
  629. function wp_refresh_post_nonces( $response, $data, $screen_id ) {
  630. if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
  631. $received = $data['wp-refresh-post-nonces'];
  632. $response['wp-refresh-post-nonces'] = array( 'check' => 1 );
  633. if ( ! $post_id = absint( $received['post_id'] ) )
  634. return $response;
  635. if ( ! current_user_can( 'edit_post', $post_id ) || empty( $received['post_nonce'] ) )
  636. return $response;
  637. if ( 2 === wp_verify_nonce( $received['post_nonce'], 'update-post_' . $post_id ) ) {
  638. $response['wp-refresh-post-nonces'] = array(
  639. 'replace' => array(
  640. 'autosavenonce' => wp_create_nonce('autosave'),
  641. 'getpermalinknonce' => wp_create_nonce('getpermalink'),
  642. 'samplepermalinknonce' => wp_create_nonce('samplepermalink'),
  643. 'closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
  644. '_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
  645. '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
  646. ),
  647. 'heartbeatNonce' => wp_create_nonce( 'heartbeat-nonce' ),
  648. );
  649. }
  650. }
  651. return $response;
  652. }
  653. add_filter( 'heartbeat_received', 'wp_refresh_post_nonces', 10, 3 );
  654. /**
  655. * Disable suspension of Heartbeat on the Add/Edit Post screens.
  656. *
  657. * @since 3.8.0
  658. *
  659. * @param array $settings An array of Heartbeat settings.
  660. * @return array Filtered Heartbeat settings.
  661. */
  662. function wp_heartbeat_set_suspension( $settings ) {
  663. global $pagenow;
  664. if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
  665. $settings['suspension'] = 'disable';
  666. }
  667. return $settings;
  668. }
  669. add_filter( 'heartbeat_settings', 'wp_heartbeat_set_suspension' );