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