wp/wp-admin/includes/misc.php
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     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.1
       
   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() {
       
   565 	global $_wp_admin_css_colors, $user_id; ?>
       
   566 <fieldset><legend class="screen-reader-text"><span><?php _e('Admin Color Scheme')?></span></legend>
       
   567 <?php
       
   568 $current_color = get_user_option('admin_color', $user_id);
       
   569 if ( empty($current_color) )
       
   570 	$current_color = 'fresh';
       
   571 foreach ( $_wp_admin_css_colors as $color => $color_info ): ?>
       
   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); ?> />
       
   573 	<table class="color-palette">
       
   574 	<tr>
       
   575 	<?php foreach ( $color_info->colors as $html_color ): ?>
       
   576 	<td style="background-color: <?php echo esc_attr( $html_color ); ?>" title="<?php echo esc_attr( $color ); ?>">&nbsp;</td>
       
   577 	<?php endforeach; ?>
       
   578 	</tr>
       
   579 	</table>
       
   580 
       
   581 	<label for="admin_color_<?php echo esc_attr( $color ); ?>"><?php echo esc_html( $color_info->name ); ?></label>
       
   582 </div>
       
   583 	<?php endforeach; ?>
       
   584 </fieldset>
       
   585 <?php
       
   586 }
       
   587 
       
   588 function _ipad_meta() {
       
   589 	if ( wp_is_mobile() ) {
       
   590 		?>
       
   591 		<meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
       
   592 		<?php
       
   593 	}
       
   594 }
       
   595 add_action('admin_head', '_ipad_meta');
       
   596 
       
   597 /**
       
   598  * Check lock status for posts displayed on the Posts screen
       
   599  *
       
   600  * @since 3.6
       
   601  */
       
   602 function wp_check_locked_posts( $response, $data, $screen_id ) {
       
   603 	$checked = array();
       
   604 
       
   605 	if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
       
   606 		foreach ( $data['wp-check-locked-posts'] as $key ) {
       
   607 			if ( ! $post_id = absint( substr( $key, 5 ) ) )
       
   608 				continue;
       
   609 
       
   610 			if ( ( $user_id = wp_check_post_lock( $post_id ) ) && ( $user = get_userdata( $user_id ) ) && current_user_can( 'edit_post', $post_id ) ) {
       
   611 				$send = array( 'text' => sprintf( __( '%s is currently editing' ), $user->display_name ) );
       
   612 
       
   613 				if ( ( $avatar = get_avatar( $user->ID, 18 ) ) && preg_match( "|src='([^']+)'|", $avatar, $matches ) )
       
   614 					$send['avatar_src'] = $matches[1];
       
   615 
       
   616 				$checked[$key] = $send;
       
   617 			}
       
   618 		}
       
   619 	}
       
   620 
       
   621 	if ( ! empty( $checked ) )
       
   622 		$response['wp-check-locked-posts'] = $checked;
       
   623 
       
   624 	return $response;
       
   625 }
       
   626 add_filter( 'heartbeat_received', 'wp_check_locked_posts', 10, 3 );
       
   627 
       
   628 /**
       
   629  * Check lock status on the New/Edit Post screen and refresh the lock
       
   630  *
       
   631  * @since 3.6
       
   632  */
       
   633 function wp_refresh_post_lock( $response, $data, $screen_id ) {
       
   634 	if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
       
   635 		$received = $data['wp-refresh-post-lock'];
       
   636 		$send = array();
       
   637 
       
   638 		if ( ! $post_id = absint( $received['post_id'] ) )
       
   639 			return $response;
       
   640 
       
   641 		if ( ! current_user_can('edit_post', $post_id) )
       
   642 			return $response;
       
   643 
       
   644 		if ( ( $user_id = wp_check_post_lock( $post_id ) ) && ( $user = get_userdata( $user_id ) ) ) {
       
   645 			$error = array(
       
   646 				'text' => sprintf( __( '%s has taken over and is currently editing.' ), $user->display_name )
       
   647 			);
       
   648 
       
   649 			if ( $avatar = get_avatar( $user->ID, 64 ) ) {
       
   650 				if ( preg_match( "|src='([^']+)'|", $avatar, $matches ) )
       
   651 					$error['avatar_src'] = $matches[1];
       
   652 			}
       
   653 
       
   654 			$send['lock_error'] = $error;
       
   655 		} else {
       
   656 			if ( $new_lock = wp_set_post_lock( $post_id ) )
       
   657 				$send['new_lock'] = implode( ':', $new_lock );
       
   658 		}
       
   659 
       
   660 		$response['wp-refresh-post-lock'] = $send;
       
   661 	}
       
   662 
       
   663 	return $response;
       
   664 }
       
   665 add_filter( 'heartbeat_received', 'wp_refresh_post_lock', 10, 3 );
       
   666 
       
   667 /**
       
   668  * Check nonce expiration on the New/Edit Post screen and refresh if needed
       
   669  *
       
   670  * @since 3.6
       
   671  */
       
   672 function wp_refresh_post_nonces( $response, $data, $screen_id ) {
       
   673 	if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
       
   674 		$received = $data['wp-refresh-post-nonces'];
       
   675 		$response['wp-refresh-post-nonces'] = array( 'check' => 1 );
       
   676 
       
   677 		if ( ! $post_id = absint( $received['post_id'] ) )
       
   678 			return $response;
       
   679 
       
   680 		if ( ! current_user_can( 'edit_post', $post_id ) || empty( $received['post_nonce'] ) )
       
   681 			return $response;
       
   682 
       
   683 		if ( 2 === wp_verify_nonce( $received['post_nonce'], 'update-post_' . $post_id ) ) {
       
   684 			$response['wp-refresh-post-nonces'] = array(
       
   685 				'replace' => array(
       
   686 					'autosavenonce' => wp_create_nonce('autosave'),
       
   687 					'getpermalinknonce' => wp_create_nonce('getpermalink'),
       
   688 					'samplepermalinknonce' => wp_create_nonce('samplepermalink'),
       
   689 					'closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
       
   690 					'_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
       
   691 					'_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
       
   692 				),
       
   693 				'heartbeatNonce' => wp_create_nonce( 'heartbeat-nonce' ),
       
   694 			);
       
   695 		}
       
   696 	}
       
   697 
       
   698 	return $response;
       
   699 }
       
   700 add_filter( 'heartbeat_received', 'wp_refresh_post_nonces', 10, 3 );