wp/wp-admin/includes/misc.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    20 	 * Filter whether Apache and mod_rewrite are present.
    20 	 * Filter whether Apache and mod_rewrite are present.
    21 	 *
    21 	 *
    22 	 * This filter was previously used to force URL rewriting for other servers,
    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.
    23 	 * like nginx. Use the got_url_rewrite filter in got_url_rewrite() instead.
    24 	 *
    24 	 *
       
    25 	 * @since 2.5.0
       
    26 	 *
    25 	 * @see got_url_rewrite()
    27 	 * @see got_url_rewrite()
    26 	 *
    28 	 *
    27 	 * @since 2.5.0
       
    28 	 * @param bool $got_rewrite Whether Apache and mod_rewrite are present.
    29 	 * @param bool $got_rewrite Whether Apache and mod_rewrite are present.
    29 	 */
    30 	 */
    30 	return apply_filters('got_rewrite', $got_rewrite);
    31 	return apply_filters( 'got_rewrite', $got_rewrite );
    31 }
    32 }
    32 
    33 
    33 /**
    34 /**
    34  * Returns whether the server supports URL rewriting.
    35  * Returns whether the server supports URL rewriting.
    35  *
    36  *
    44 
    45 
    45 	/**
    46 	/**
    46 	 * Filter whether URL rewriting is available.
    47 	 * Filter whether URL rewriting is available.
    47 	 *
    48 	 *
    48 	 * @since 3.7.0
    49 	 * @since 3.7.0
       
    50 	 *
    49 	 * @param bool $got_url_rewrite Whether URL rewriting is available.
    51 	 * @param bool $got_url_rewrite Whether URL rewriting is available.
    50 	 */
    52 	 */
    51 	return apply_filters( 'got_url_rewrite', $got_url_rewrite );
    53 	return apply_filters( 'got_url_rewrite', $got_url_rewrite );
    52 }
    54 }
    53 
    55 
    54 /**
    56 /**
    55  * {@internal Missing Short Description}}
    57  * {@internal Missing Short Description}}
    56  *
    58  *
    57  * @since 1.5.0
    59  * @since 1.5.0
    58  *
    60  *
    59  * @param unknown_type $filename
    61  * @param string $filename
    60  * @param unknown_type $marker
    62  * @param string $marker
    61  * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers.
    63  * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers.
    62  */
    64  */
    63 function extract_from_markers( $filename, $marker ) {
    65 function extract_from_markers( $filename, $marker ) {
    64 	$result = array ();
    66 	$result = array ();
    65 
    67 
    90  * BEGIN and END markers. Replaces existing marked info. Retains surrounding
    92  * BEGIN and END markers. Replaces existing marked info. Retains surrounding
    91  * data. Creates file if none exists.
    93  * data. Creates file if none exists.
    92  *
    94  *
    93  * @since 1.5.0
    95  * @since 1.5.0
    94  *
    96  *
    95  * @param unknown_type $filename
    97  * @param string $filename
    96  * @param unknown_type $marker
    98  * @param string $marker
    97  * @param unknown_type $insertion
    99  * @param array  $insertion
    98  * @return bool True on write success, false on failure.
   100  * @return bool True on write success, false on failure.
    99  */
   101  */
   100 function insert_with_markers( $filename, $marker, $insertion ) {
   102 function insert_with_markers( $filename, $marker, $insertion ) {
   101 	if (!file_exists( $filename ) || is_writeable( $filename ) ) {
   103 	if (!file_exists( $filename ) || is_writeable( $filename ) ) {
   102 		if (!file_exists( $filename ) ) {
   104 		if (!file_exists( $filename ) ) {
   159 	global $wp_rewrite;
   161 	global $wp_rewrite;
   160 
   162 
   161 	$home_path = get_home_path();
   163 	$home_path = get_home_path();
   162 	$htaccess_file = $home_path.'.htaccess';
   164 	$htaccess_file = $home_path.'.htaccess';
   163 
   165 
   164 	// If the file doesn't already exist check for write access to the directory and whether we have some rules.
   166 	/*
   165 	// else check for write access to the file.
   167 	 * If the file doesn't already exist check for write access to the directory
       
   168 	 * and whether we have some rules. Else check for write access to the file.
       
   169 	 */
   166 	if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
   170 	if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
   167 		if ( got_mod_rewrite() ) {
   171 		if ( got_mod_rewrite() ) {
   168 			$rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
   172 			$rules = explode( "\n", $wp_rewrite->mod_rewrite_rules() );
   169 			return insert_with_markers( $htaccess_file, 'WordPress', $rules );
   173 			return insert_with_markers( $htaccess_file, 'WordPress', $rules );
   170 		}
   174 		}
   205 /**
   209 /**
   206  * {@internal Missing Short Description}}
   210  * {@internal Missing Short Description}}
   207  *
   211  *
   208  * @since 1.5.0
   212  * @since 1.5.0
   209  *
   213  *
   210  * @param unknown_type $file
   214  * @param string $file
   211  */
   215  */
   212 function update_recently_edited( $file ) {
   216 function update_recently_edited( $file ) {
   213 	$oldfiles = (array ) get_option( 'recently_edited' );
   217 	$oldfiles = (array ) get_option( 'recently_edited' );
   214 	if ( $oldfiles ) {
   218 	if ( $oldfiles ) {
   215 		$oldfiles = array_reverse( $oldfiles );
   219 		$oldfiles = array_reverse( $oldfiles );
   245 add_action( 'update_option_page_on_front', 'update_home_siteurl', 10, 2 );
   249 add_action( 'update_option_page_on_front', 'update_home_siteurl', 10, 2 );
   246 
   250 
   247 /**
   251 /**
   248  * Shorten an URL, to be used as link text
   252  * Shorten an URL, to be used as link text
   249  *
   253  *
   250  * @since 1.2.1
   254  * @since 1.2.0
   251  *
   255  *
   252  * @param string $url
   256  * @param string $url
   253  * @return string
   257  * @return string
   254  */
   258  */
   255 function url_shorten( $url ) {
   259 function url_shorten( $url ) {
   270  * @since 2.0.0
   274  * @since 2.0.0
   271  *
   275  *
   272  * @param array $vars An array of globals to reset.
   276  * @param array $vars An array of globals to reset.
   273  */
   277  */
   274 function wp_reset_vars( $vars ) {
   278 function wp_reset_vars( $vars ) {
   275 	for ( $i=0; $i<count( $vars ); $i += 1 ) {
   279 	foreach ( $vars as $var ) {
   276 		$var = $vars[$i];
   280 		if ( empty( $_POST[ $var ] ) ) {
   277 		global $$var;
   281 			if ( empty( $_GET[ $var ] ) ) {
   278 
   282 				$GLOBALS[ $var ] = '';
   279 		if ( empty( $_POST[$var] ) ) {
   283 			} else {
   280 			if ( empty( $_GET[$var] ) )
   284 				$GLOBALS[ $var ] = $_GET[ $var ];
   281 				$$var = '';
   285 			}
   282 			else
       
   283 				$$var = $_GET[$var];
       
   284 		} else {
   286 		} else {
   285 			$$var = $_POST[$var];
   287 			$GLOBALS[ $var ] = $_POST[ $var ];
   286 		}
   288 		}
   287 	}
   289 	}
   288 }
   290 }
   289 
   291 
   290 /**
   292 /**
   291  * {@internal Missing Short Description}}
   293  * {@internal Missing Short Description}}
   292  *
   294  *
   293  * @since 2.1.0
   295  * @since 2.1.0
   294  *
   296  *
   295  * @param unknown_type $message
   297  * @param string|WP_Error $message
   296  */
   298  */
   297 function show_message($message) {
   299 function show_message($message) {
   298 	if ( is_wp_error($message) ){
   300 	if ( is_wp_error($message) ){
   299 		if ( $message->get_error_data() && is_string( $message->get_error_data() ) )
   301 		if ( $message->get_error_data() && is_string( $message->get_error_data() ) )
   300 			$message = $message->get_error_message() . ': ' . $message->get_error_data();
   302 			$message = $message->get_error_message() . ': ' . $message->get_error_data();
   312 
   314 
   313 	if ( !function_exists('token_get_all') )
   315 	if ( !function_exists('token_get_all') )
   314 		return array();
   316 		return array();
   315 
   317 
   316 	$tokens = token_get_all( $content );
   318 	$tokens = token_get_all( $content );
       
   319 	$count = count( $tokens );
   317 	$functions = array();
   320 	$functions = array();
   318 	$ignore_functions = array();
   321 	$ignore_functions = array();
   319 	for ( $t = 0, $count = count( $tokens ); $t < $count; $t++ ) {
   322 	for ( $t = 0; $t < $count - 2; $t++ ) {
   320 		if ( !is_array( $tokens[$t] ) ) continue;
   323 		if ( ! is_array( $tokens[ $t ] ) ) {
   321 		if ( T_STRING == $tokens[$t][0] && ( '(' == $tokens[ $t + 1 ] || '(' == $tokens[ $t + 2 ] ) ) {
   324 			continue;
       
   325 		}
       
   326 
       
   327 		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
   328 			// 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] ) ) {
   329 			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];
   330 				$ignore_functions[] = $tokens[$t][1];
   325 			}
   331 			}
   326 			// Add this to our stack of unique references
   332 			// Add this to our stack of unique references
   328 		}
   334 		}
   329 	}
   335 	}
   330 
   336 
   331 	$functions = array_unique( $functions );
   337 	$functions = array_unique( $functions );
   332 	sort( $functions );
   338 	sort( $functions );
       
   339 
       
   340 	/**
       
   341 	 * Filter the list of functions and classes to be ignored from the documentation lookup.
       
   342 	 *
       
   343 	 * @since 2.8.0
       
   344 	 *
       
   345 	 * @param array $ignore_functions Functions and classes to be ignored.
       
   346 	 */
   333 	$ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
   347 	$ignore_functions = apply_filters( 'documentation_ignore_functions', $ignore_functions );
       
   348 
   334 	$ignore_functions = array_unique( $ignore_functions );
   349 	$ignore_functions = array_unique( $ignore_functions );
   335 
   350 
   336 	$out = array();
   351 	$out = array();
   337 	foreach ( $functions as $function ) {
   352 	foreach ( $functions as $function ) {
   338 		if ( in_array( $function, $ignore_functions ) )
   353 		if ( in_array( $function, $ignore_functions ) )
   344 }
   359 }
   345 
   360 
   346 /**
   361 /**
   347  * Saves option for number of rows when listing posts, pages, comments, etc.
   362  * Saves option for number of rows when listing posts, pages, comments, etc.
   348  *
   363  *
   349  * @since 2.8
   364  * @since 2.8.0
   350 **/
   365  */
   351 function set_screen_options() {
   366 function set_screen_options() {
   352 
   367 
   353 	if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) ) {
   368 	if ( isset($_POST['wp_screen_options']) && is_array($_POST['wp_screen_options']) ) {
   354 		check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
   369 		check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' );
   355 
   370 
   388 				$value = (int) $value;
   403 				$value = (int) $value;
   389 				if ( $value < 1 || $value > 999 )
   404 				if ( $value < 1 || $value > 999 )
   390 					return;
   405 					return;
   391 				break;
   406 				break;
   392 			default:
   407 			default:
   393 				$value = apply_filters('set-screen-option', false, $option, $value);
   408 
       
   409 				/**
       
   410 				 * Filter a screen option value before it is set.
       
   411 				 *
       
   412 				 * The filter can also be used to modify non-standard [items]_per_page
       
   413 				 * settings. See the parent function for a full list of standard options.
       
   414 				 *
       
   415 				 * Returning false to the filter will skip saving the current option.
       
   416 				 *
       
   417 				 * @since 2.8.0
       
   418 				 *
       
   419 				 * @see set_screen_options()
       
   420 				 *
       
   421 				 * @param bool|int $value  Screen option value. Default false to skip.
       
   422 				 * @param string   $option The option name.
       
   423 				 * @param int      $value  The number of rows to use.
       
   424 				 */
       
   425 				$value = apply_filters( 'set-screen-option', false, $option, $value );
       
   426 
   394 				if ( false === $value )
   427 				if ( false === $value )
   395 					return;
   428 					return;
   396 				break;
   429 				break;
   397 		}
   430 		}
   398 
   431 
   559 /**
   592 /**
   560  * Display the default admin color scheme picker (Used in user-edit.php)
   593  * Display the default admin color scheme picker (Used in user-edit.php)
   561  *
   594  *
   562  * @since 3.0.0
   595  * @since 3.0.0
   563  */
   596  */
   564 function admin_color_scheme_picker() {
   597 function admin_color_scheme_picker( $user_id ) {
   565 	global $_wp_admin_css_colors, $user_id; ?>
   598 	global $_wp_admin_css_colors;
   566 <fieldset><legend class="screen-reader-text"><span><?php _e('Admin Color Scheme')?></span></legend>
   599 
   567 <?php
   600 	ksort( $_wp_admin_css_colors );
   568 $current_color = get_user_option('admin_color', $user_id);
   601 
   569 if ( empty($current_color) )
   602 	if ( isset( $_wp_admin_css_colors['fresh'] ) ) {
   570 	$current_color = 'fresh';
   603 		// Set Default ('fresh') and Light should go first.
   571 foreach ( $_wp_admin_css_colors as $color => $color_info ): ?>
   604 		$_wp_admin_css_colors = array_filter( array_merge( array( 'fresh' => '', 'light' => '' ), $_wp_admin_css_colors ) );
   572 <div class="color-option"><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); ?> />
   605 	}
   573 	<table class="color-palette">
   606 
   574 	<tr>
   607 	$current_color = get_user_option( 'admin_color', $user_id );
   575 	<?php foreach ( $color_info->colors as $html_color ): ?>
   608 
   576 	<td style="background-color: <?php echo esc_attr( $html_color ); ?>" title="<?php echo esc_attr( $color ); ?>">&nbsp;</td>
   609 	if ( empty( $current_color ) || ! isset( $_wp_admin_css_colors[ $current_color ] ) ) {
   577 	<?php endforeach; ?>
   610 		$current_color = 'fresh';
   578 	</tr>
   611 	}
   579 	</table>
   612 
   580 
   613 	?>
   581 	<label for="admin_color_<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $color_info->name ); ?></label>
   614 	<fieldset id="color-picker" class="scheme-list">
   582 </div>
   615 		<legend class="screen-reader-text"><span><?php _e( 'Admin Color Scheme' ); ?></span></legend>
   583 	<?php endforeach; ?>
   616 		<?php
   584 </fieldset>
   617 		wp_nonce_field( 'save-color-scheme', 'color-nonce', false );
   585 <?php
   618 		foreach ( $_wp_admin_css_colors as $color => $color_info ) :
   586 }
   619 
       
   620 			?>
       
   621 			<div class="color-option <?php echo ( $color == $current_color ) ? 'selected' : ''; ?>">
       
   622 				<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 ); ?> />
       
   623 				<input type="hidden" class="css_url" value="<?php echo esc_url( $color_info->url ); ?>" />
       
   624 				<input type="hidden" class="icon_colors" value="<?php echo esc_attr( wp_json_encode( array( 'icons' => $color_info->icon_colors ) ) ); ?>" />
       
   625 				<label for="admin_color_<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $color_info->name ); ?></label>
       
   626 				<table class="color-palette">
       
   627 					<tr>
       
   628 					<?php
       
   629 
       
   630 					foreach ( $color_info->colors as $html_color ) {
       
   631 						?>
       
   632 						<td style="background-color: <?php echo esc_attr( $html_color ); ?>">&nbsp;</td>
       
   633 						<?php
       
   634 					}
       
   635 
       
   636 					?>
       
   637 					</tr>
       
   638 				</table>
       
   639 			</div>
       
   640 			<?php
       
   641 
       
   642 		endforeach;
       
   643 
       
   644 	?>
       
   645 	</fieldset>
       
   646 	<?php
       
   647 }
       
   648 
       
   649 function wp_color_scheme_settings() {
       
   650 	global $_wp_admin_css_colors;
       
   651 
       
   652 	$color_scheme = get_user_option( 'admin_color' );
       
   653 
       
   654 	// It's possible to have a color scheme set that is no longer registered.
       
   655 	if ( empty( $_wp_admin_css_colors[ $color_scheme ] ) ) {
       
   656 		$color_scheme = 'fresh';
       
   657 	}
       
   658 
       
   659 	if ( ! empty( $_wp_admin_css_colors[ $color_scheme ]->icon_colors ) ) {
       
   660 		$icon_colors = $_wp_admin_css_colors[ $color_scheme ]->icon_colors;
       
   661 	} elseif ( ! empty( $_wp_admin_css_colors['fresh']->icon_colors ) ) {
       
   662 		$icon_colors = $_wp_admin_css_colors['fresh']->icon_colors;
       
   663 	} else {
       
   664 		// Fall back to the default set of icon colors if the default scheme is missing.
       
   665 		$icon_colors = array( 'base' => '#999', 'focus' => '#00a0d2', 'current' => '#fff' );
       
   666 	}
       
   667 
       
   668 	echo '<script type="text/javascript">var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ) ) . ";</script>\n";
       
   669 }
       
   670 add_action( 'admin_head', 'wp_color_scheme_settings' );
   587 
   671 
   588 function _ipad_meta() {
   672 function _ipad_meta() {
   589 	if ( wp_is_mobile() ) {
   673 	if ( wp_is_mobile() ) {
   590 		?>
   674 		?>
   591 		<meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
   675 		<meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
   595 add_action('admin_head', '_ipad_meta');
   679 add_action('admin_head', '_ipad_meta');
   596 
   680 
   597 /**
   681 /**
   598  * Check lock status for posts displayed on the Posts screen
   682  * Check lock status for posts displayed on the Posts screen
   599  *
   683  *
   600  * @since 3.6
   684  * @since 3.6.0
   601  */
   685  */
   602 function wp_check_locked_posts( $response, $data, $screen_id ) {
   686 function wp_check_locked_posts( $response, $data, $screen_id ) {
   603 	$checked = array();
   687 	$checked = array();
   604 
   688 
   605 	if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
   689 	if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
   626 add_filter( 'heartbeat_received', 'wp_check_locked_posts', 10, 3 );
   710 add_filter( 'heartbeat_received', 'wp_check_locked_posts', 10, 3 );
   627 
   711 
   628 /**
   712 /**
   629  * Check lock status on the New/Edit Post screen and refresh the lock
   713  * Check lock status on the New/Edit Post screen and refresh the lock
   630  *
   714  *
   631  * @since 3.6
   715  * @since 3.6.0
   632  */
   716  */
   633 function wp_refresh_post_lock( $response, $data, $screen_id ) {
   717 function wp_refresh_post_lock( $response, $data, $screen_id ) {
   634 	if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
   718 	if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
   635 		$received = $data['wp-refresh-post-lock'];
   719 		$received = $data['wp-refresh-post-lock'];
   636 		$send = array();
   720 		$send = array();
   665 add_filter( 'heartbeat_received', 'wp_refresh_post_lock', 10, 3 );
   749 add_filter( 'heartbeat_received', 'wp_refresh_post_lock', 10, 3 );
   666 
   750 
   667 /**
   751 /**
   668  * Check nonce expiration on the New/Edit Post screen and refresh if needed
   752  * Check nonce expiration on the New/Edit Post screen and refresh if needed
   669  *
   753  *
   670  * @since 3.6
   754  * @since 3.6.0
   671  */
   755  */
   672 function wp_refresh_post_nonces( $response, $data, $screen_id ) {
   756 function wp_refresh_post_nonces( $response, $data, $screen_id ) {
   673 	if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
   757 	if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
   674 		$received = $data['wp-refresh-post-nonces'];
   758 		$received = $data['wp-refresh-post-nonces'];
   675 		$response['wp-refresh-post-nonces'] = array( 'check' => 1 );
   759 		$response['wp-refresh-post-nonces'] = array( 'check' => 1 );
   681 			return $response;
   765 			return $response;
   682 
   766 
   683 		if ( 2 === wp_verify_nonce( $received['post_nonce'], 'update-post_' . $post_id ) ) {
   767 		if ( 2 === wp_verify_nonce( $received['post_nonce'], 'update-post_' . $post_id ) ) {
   684 			$response['wp-refresh-post-nonces'] = array(
   768 			$response['wp-refresh-post-nonces'] = array(
   685 				'replace' => array(
   769 				'replace' => array(
   686 					'autosavenonce' => wp_create_nonce('autosave'),
       
   687 					'getpermalinknonce' => wp_create_nonce('getpermalink'),
   770 					'getpermalinknonce' => wp_create_nonce('getpermalink'),
   688 					'samplepermalinknonce' => wp_create_nonce('samplepermalink'),
   771 					'samplepermalinknonce' => wp_create_nonce('samplepermalink'),
   689 					'closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
   772 					'closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
   690 					'_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
   773 					'_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
   691 					'_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
   774 					'_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
   696 	}
   779 	}
   697 
   780 
   698 	return $response;
   781 	return $response;
   699 }
   782 }
   700 add_filter( 'heartbeat_received', 'wp_refresh_post_nonces', 10, 3 );
   783 add_filter( 'heartbeat_received', 'wp_refresh_post_nonces', 10, 3 );
       
   784 
       
   785 /**
       
   786  * Disable suspension of Heartbeat on the Add/Edit Post screens.
       
   787  *
       
   788  * @since 3.8.0
       
   789  *
       
   790  * @param array $settings An array of Heartbeat settings.
       
   791  * @return array Filtered Heartbeat settings.
       
   792  */
       
   793 function wp_heartbeat_set_suspension( $settings ) {
       
   794 	global $pagenow;
       
   795 
       
   796 	if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {
       
   797 		$settings['suspension'] = 'disable';
       
   798 	}
       
   799 
       
   800 	return $settings;
       
   801 }
       
   802 add_filter( 'heartbeat_settings', 'wp_heartbeat_set_suspension' );
       
   803 
       
   804 /**
       
   805  * Autosave with heartbeat
       
   806  *
       
   807  * @since 3.9.0
       
   808  */
       
   809 function heartbeat_autosave( $response, $data ) {
       
   810 	if ( ! empty( $data['wp_autosave'] ) ) {
       
   811 		$saved = wp_autosave( $data['wp_autosave'] );
       
   812 
       
   813 		if ( is_wp_error( $saved ) ) {
       
   814 			$response['wp_autosave'] = array( 'success' => false, 'message' => $saved->get_error_message() );
       
   815 		} elseif ( empty( $saved ) ) {
       
   816 			$response['wp_autosave'] = array( 'success' => false, 'message' => __( 'Error while saving.' ) );
       
   817 		} else {
       
   818 			/* translators: draft saved date format, see http://php.net/date */
       
   819 			$draft_saved_date_format = __( 'g:i:s a' );
       
   820 			/* translators: %s: date and time */
       
   821 			$response['wp_autosave'] = array( 'success' => true, 'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ) );
       
   822 		}
       
   823 	}
       
   824 
       
   825 	return $response;
       
   826 }
       
   827 // Run later as we have to set DOING_AUTOSAVE for back-compat
       
   828 add_filter( 'heartbeat_received', 'heartbeat_autosave', 500, 2 );
       
   829 
       
   830 /**
       
   831  * Disables autocomplete on the 'post' form (Add/Edit Post screens) for WebKit browsers,
       
   832  * as they disregard the autocomplete setting on the editor textarea. That can break the editor
       
   833  * when the user navigates to it with the browser's Back button. See #28037
       
   834  *
       
   835  * @since 4.0
       
   836  */
       
   837 function post_form_autocomplete_off() {
       
   838 	global $is_safari, $is_chrome;
       
   839 
       
   840 	if ( $is_safari || $is_chrome ) {
       
   841 		echo ' autocomplete="off"';
       
   842 	}
       
   843 }
       
   844 add_action( 'post_edit_form_tag', 'post_form_autocomplete_off' );
       
   845 
       
   846 /**
       
   847  * Remove single-use URL parameters and create canonical link based on new URL.
       
   848  *
       
   849  * Remove specific query string parameters from a URL, create the canonical link,
       
   850  * put it in the admin header, and change the current URL to match.
       
   851  *
       
   852  * @since 4.2.0
       
   853  */
       
   854 function wp_admin_canonical_url() {
       
   855 	$removable_query_args = array(
       
   856 		'message', 'settings-updated', 'saved',
       
   857 		'update', 'updated', 'activated',
       
   858 		'activate', 'deactivate', 'locked',
       
   859 		'deleted', 'trashed', 'untrashed',
       
   860 		'enabled', 'disabled', 'skipped',
       
   861 		'spammed', 'unspammed',
       
   862 	);
       
   863 
       
   864 	/**
       
   865 	 * Filter the list of URL parameters to remove.
       
   866 	 *
       
   867 	 * @since 4.2.0
       
   868 	 *
       
   869 	 * @param array $removable_query_args An array of parameters to remove from the URL.
       
   870 	 */
       
   871 	$removable_query_args = apply_filters( 'removable_query_args', $removable_query_args );
       
   872 
       
   873 	if ( empty( $removable_query_args ) ) {
       
   874 		return;
       
   875 	}
       
   876 
       
   877 	// Ensure we're using an absolute URL.
       
   878 	$current_url  = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
       
   879 	$filtered_url = remove_query_arg( $removable_query_args, $current_url );
       
   880 	?>
       
   881 	<link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
       
   882 	<script>
       
   883 		if ( window.history.replaceState ) {
       
   884 			window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
       
   885 		}
       
   886 	</script>
       
   887 <?php
       
   888 }
       
   889 add_action( 'admin_head', 'wp_admin_canonical_url' );