wp/wp-admin/includes/misc.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
    15  */
    15  */
    16 function got_mod_rewrite() {
    16 function got_mod_rewrite() {
    17 	$got_rewrite = apache_mod_loaded('mod_rewrite', true);
    17 	$got_rewrite = apache_mod_loaded('mod_rewrite', true);
    18 
    18 
    19 	/**
    19 	/**
    20 	 * Filter whether Apache and mod_rewrite are present.
    20 	 * Filters 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 {@see 'got_url_rewrite'} filter in got_url_rewrite() instead.
    24 	 *
    24 	 *
    25 	 * @since 2.5.0
    25 	 * @since 2.5.0
    26 	 *
    26 	 *
    27 	 * @see got_url_rewrite()
    27 	 * @see got_url_rewrite()
    28 	 *
    28 	 *
    36  *
    36  *
    37  * Detects Apache's mod_rewrite, IIS 7.0+ permalink support, and nginx.
    37  * Detects Apache's mod_rewrite, IIS 7.0+ permalink support, and nginx.
    38  *
    38  *
    39  * @since 3.7.0
    39  * @since 3.7.0
    40  *
    40  *
       
    41  * @global bool $is_nginx
       
    42  *
    41  * @return bool Whether the server supports URL rewriting.
    43  * @return bool Whether the server supports URL rewriting.
    42  */
    44  */
    43 function got_url_rewrite() {
    45 function got_url_rewrite() {
    44 	$got_url_rewrite = ( got_mod_rewrite() || $GLOBALS['is_nginx'] || iis7_supports_permalinks() );
    46 	$got_url_rewrite = ( got_mod_rewrite() || $GLOBALS['is_nginx'] || iis7_supports_permalinks() );
    45 
    47 
    46 	/**
    48 	/**
    47 	 * Filter whether URL rewriting is available.
    49 	 * Filters whether URL rewriting is available.
    48 	 *
    50 	 *
    49 	 * @since 3.7.0
    51 	 * @since 3.7.0
    50 	 *
    52 	 *
    51 	 * @param bool $got_url_rewrite Whether URL rewriting is available.
    53 	 * @param bool $got_url_rewrite Whether URL rewriting is available.
    52 	 */
    54 	 */
    53 	return apply_filters( 'got_url_rewrite', $got_url_rewrite );
    55 	return apply_filters( 'got_url_rewrite', $got_url_rewrite );
    54 }
    56 }
    55 
    57 
    56 /**
    58 /**
    57  * {@internal Missing Short Description}}
    59  * Extracts strings from between the BEGIN and END markers in the .htaccess file.
    58  *
    60  *
    59  * @since 1.5.0
    61  * @since 1.5.0
    60  *
    62  *
    61  * @param string $filename
    63  * @param string $filename
    62  * @param string $marker
    64  * @param string $marker
    63  * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers.
    65  * @return array An array of strings from a file (.htaccess ) from between BEGIN and END markers.
    64  */
    66  */
    65 function extract_from_markers( $filename, $marker ) {
    67 function extract_from_markers( $filename, $marker ) {
    66 	$result = array ();
    68 	$result = array ();
    67 
    69 
    68 	if (!file_exists( $filename ) ) {
    70 	if ( ! file_exists( $filename ) ) {
    69 		return $result;
    71 		return $result;
    70 	}
    72 	}
    71 
    73 
    72 	if ( $markerdata = explode( "\n", implode( '', file( $filename ) ) ));
    74 	$markerdata = explode( "\n", implode( '', file( $filename ) ) );
    73 	{
    75 
    74 		$state = false;
    76 	$state = false;
    75 		foreach ( $markerdata as $markerline ) {
    77 	foreach ( $markerdata as $markerline ) {
    76 			if (strpos($markerline, '# END ' . $marker) !== false)
    78 		if ( false !== strpos( $markerline, '# END ' . $marker ) ) {
    77 				$state = false;
    79 			$state = false;
    78 			if ( $state )
    80 		}
    79 				$result[] = $markerline;
    81 		if ( $state ) {
    80 			if (strpos($markerline, '# BEGIN ' . $marker) !== false)
    82 			$result[] = $markerline;
    81 				$state = true;
    83 		}
       
    84 		if ( false !== strpos( $markerline, '# BEGIN ' . $marker ) ) {
       
    85 			$state = true;
    82 		}
    86 		}
    83 	}
    87 	}
    84 
    88 
    85 	return $result;
    89 	return $result;
    86 }
    90 }
    87 
    91 
    88 /**
    92 /**
    89  * {@internal Missing Short Description}}
       
    90  *
       
    91  * Inserts an array of strings into a file (.htaccess ), placing it between
    93  * Inserts an array of strings into a file (.htaccess ), placing it between
    92  * BEGIN and END markers. Replaces existing marked info. Retains surrounding
    94  * BEGIN and END markers.
       
    95  *
       
    96  * Replaces existing marked info. Retains surrounding
    93  * data. Creates file if none exists.
    97  * data. Creates file if none exists.
    94  *
    98  *
    95  * @since 1.5.0
    99  * @since 1.5.0
    96  *
   100  *
    97  * @param string $filename
   101  * @param string       $filename  Filename to alter.
    98  * @param string $marker
   102  * @param string       $marker    The marker to alter.
    99  * @param array  $insertion
   103  * @param array|string $insertion The new content to insert.
   100  * @return bool True on write success, false on failure.
   104  * @return bool True on write success, false on failure.
   101  */
   105  */
   102 function insert_with_markers( $filename, $marker, $insertion ) {
   106 function insert_with_markers( $filename, $marker, $insertion ) {
   103 	if (!file_exists( $filename ) || is_writeable( $filename ) ) {
   107 	if ( ! file_exists( $filename ) ) {
   104 		if (!file_exists( $filename ) ) {
   108 		if ( ! is_writable( dirname( $filename ) ) ) {
   105 			$markerdata = '';
   109 			return false;
       
   110 		}
       
   111 		if ( ! touch( $filename ) ) {
       
   112 			return false;
       
   113 		}
       
   114 	} elseif ( ! is_writeable( $filename ) ) {
       
   115 		return false;
       
   116 	}
       
   117 
       
   118 	if ( ! is_array( $insertion ) ) {
       
   119 		$insertion = explode( "\n", $insertion );
       
   120 	}
       
   121 
       
   122 	$start_marker = "# BEGIN {$marker}";
       
   123 	$end_marker   = "# END {$marker}";
       
   124 
       
   125 	$fp = fopen( $filename, 'r+' );
       
   126 	if ( ! $fp ) {
       
   127 		return false;
       
   128 	}
       
   129 
       
   130 	// Attempt to get a lock. If the filesystem supports locking, this will block until the lock is acquired.
       
   131 	flock( $fp, LOCK_EX );
       
   132 
       
   133 	$lines = array();
       
   134 	while ( ! feof( $fp ) ) {
       
   135 		$lines[] = rtrim( fgets( $fp ), "\r\n" );
       
   136 	}
       
   137 
       
   138 	// Split out the existing file into the preceding lines, and those that appear after the marker
       
   139 	$pre_lines = $post_lines = $existing_lines = array();
       
   140 	$found_marker = $found_end_marker = false;
       
   141 	foreach ( $lines as $line ) {
       
   142 		if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) {
       
   143 			$found_marker = true;
       
   144 			continue;
       
   145 		} elseif ( ! $found_end_marker && false !== strpos( $line, $end_marker ) ) {
       
   146 			$found_end_marker = true;
       
   147 			continue;
       
   148 		}
       
   149 		if ( ! $found_marker ) {
       
   150 			$pre_lines[] = $line;
       
   151 		} elseif ( $found_marker && $found_end_marker ) {
       
   152 			$post_lines[] = $line;
   106 		} else {
   153 		} else {
   107 			$markerdata = explode( "\n", implode( '', file( $filename ) ) );
   154 			$existing_lines[] = $line;
   108 		}
   155 		}
   109 
   156 	}
   110 		if ( !$f = @fopen( $filename, 'w' ) )
   157 
   111 			return false;
   158 	// Check to see if there was a change
   112 
   159 	if ( $existing_lines === $insertion ) {
   113 		$foundit = false;
   160 		flock( $fp, LOCK_UN );
   114 		if ( $markerdata ) {
   161 		fclose( $fp );
   115 			$state = true;
   162 
   116 			foreach ( $markerdata as $n => $markerline ) {
       
   117 				if (strpos($markerline, '# BEGIN ' . $marker) !== false)
       
   118 					$state = false;
       
   119 				if ( $state ) {
       
   120 					if ( $n + 1 < count( $markerdata ) )
       
   121 						fwrite( $f, "{$markerline}\n" );
       
   122 					else
       
   123 						fwrite( $f, "{$markerline}" );
       
   124 				}
       
   125 				if (strpos($markerline, '# END ' . $marker) !== false) {
       
   126 					fwrite( $f, "# BEGIN {$marker}\n" );
       
   127 					if ( is_array( $insertion ))
       
   128 						foreach ( $insertion as $insertline )
       
   129 							fwrite( $f, "{$insertline}\n" );
       
   130 					fwrite( $f, "# END {$marker}\n" );
       
   131 					$state = true;
       
   132 					$foundit = true;
       
   133 				}
       
   134 			}
       
   135 		}
       
   136 		if (!$foundit) {
       
   137 			fwrite( $f, "\n# BEGIN {$marker}\n" );
       
   138 			foreach ( $insertion as $insertline )
       
   139 				fwrite( $f, "{$insertline}\n" );
       
   140 			fwrite( $f, "# END {$marker}\n" );
       
   141 		}
       
   142 		fclose( $f );
       
   143 		return true;
   163 		return true;
   144 	} else {
   164 	}
   145 		return false;
   165 
   146 	}
   166 	// Generate the new file data
       
   167 	$new_file_data = implode( "\n", array_merge(
       
   168 		$pre_lines,
       
   169 		array( $start_marker ),
       
   170 		$insertion,
       
   171 		array( $end_marker ),
       
   172 		$post_lines
       
   173 	) );
       
   174 
       
   175 	// Write to the start of the file, and truncate it to that length
       
   176 	fseek( $fp, 0 );
       
   177 	$bytes = fwrite( $fp, $new_file_data );
       
   178 	if ( $bytes ) {
       
   179 		ftruncate( $fp, ftell( $fp ) );
       
   180 	}
       
   181 	fflush( $fp );
       
   182 	flock( $fp, LOCK_UN );
       
   183 	fclose( $fp );
       
   184 
       
   185 	return (bool) $bytes;
   147 }
   186 }
   148 
   187 
   149 /**
   188 /**
   150  * Updates the htaccess file with the current rules if it is writable.
   189  * Updates the htaccess file with the current rules if it is writable.
   151  *
   190  *
   152  * Always writes to the file if it exists and is writable to ensure that we
   191  * Always writes to the file if it exists and is writable to ensure that we
   153  * blank out old rules.
   192  * blank out old rules.
   154  *
   193  *
   155  * @since 1.5.0
   194  * @since 1.5.0
       
   195  *
       
   196  * @global WP_Rewrite $wp_rewrite
       
   197  *
       
   198  * @return bool|null True on write success, false on failure. Null in multisite.
   156  */
   199  */
   157 function save_mod_rewrite_rules() {
   200 function save_mod_rewrite_rules() {
   158 	if ( is_multisite() )
   201 	if ( is_multisite() )
   159 		return;
   202 		return;
   160 
   203 
   161 	global $wp_rewrite;
   204 	global $wp_rewrite;
   162 
   205 
   163 	$home_path = get_home_path();
   206 	// Ensure get_home_path() is declared.
   164 	$htaccess_file = $home_path.'.htaccess';
   207 	require_once( ABSPATH . 'wp-admin/includes/file.php' );
       
   208 
       
   209 	$home_path     = get_home_path();
       
   210 	$htaccess_file = $home_path . '.htaccess';
   165 
   211 
   166 	/*
   212 	/*
   167 	 * If the file doesn't already exist check for write access to the directory
   213 	 * 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.
   214 	 * and whether we have some rules. Else check for write access to the file.
   169 	 */
   215 	 */
   181  * Updates the IIS web.config file with the current rules if it is writable.
   227  * Updates the IIS web.config file with the current rules if it is writable.
   182  * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
   228  * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
   183  *
   229  *
   184  * @since 2.8.0
   230  * @since 2.8.0
   185  *
   231  *
   186  * @return bool True if web.config was updated successfully
   232  * @global WP_Rewrite $wp_rewrite
       
   233  *
       
   234  * @return bool|null True on write success, false on failure. Null in multisite.
   187  */
   235  */
   188 function iis7_save_url_rewrite_rules(){
   236 function iis7_save_url_rewrite_rules(){
   189 	if ( is_multisite() )
   237 	if ( is_multisite() )
   190 		return;
   238 		return;
   191 
   239 
   192 	global $wp_rewrite;
   240 	global $wp_rewrite;
   193 
   241 
   194 	$home_path = get_home_path();
   242 	// Ensure get_home_path() is declared.
       
   243 	require_once( ABSPATH . 'wp-admin/includes/file.php' );
       
   244 
       
   245 	$home_path       = get_home_path();
   195 	$web_config_file = $home_path . 'web.config';
   246 	$web_config_file = $home_path . 'web.config';
   196 
   247 
   197 	// Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
   248 	// Using win_is_writable() instead of is_writable() because of a bug in Windows PHP
   198 	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) ) ) {
   249 	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) ) ) {
   199 		$rule = $wp_rewrite->iis7_url_rewrite_rules(false, '', '');
   250 		$rule = $wp_rewrite->iis7_url_rewrite_rules(false, '', '');
   205 	}
   256 	}
   206 	return false;
   257 	return false;
   207 }
   258 }
   208 
   259 
   209 /**
   260 /**
   210  * {@internal Missing Short Description}}
   261  * Update the "recently-edited" file for the plugin or theme editor.
   211  *
   262  *
   212  * @since 1.5.0
   263  * @since 1.5.0
   213  *
   264  *
   214  * @param string $file
   265  * @param string $file
   215  */
   266  */
   227 	}
   278 	}
   228 	update_option( 'recently_edited', $oldfiles );
   279 	update_option( 'recently_edited', $oldfiles );
   229 }
   280 }
   230 
   281 
   231 /**
   282 /**
   232  * If siteurl, home or page_on_front changed, flush rewrite rules.
   283  * Makes a tree structure for the Theme Editor's file list.
       
   284  *
       
   285  * @since 4.9.0
       
   286  * @access private
       
   287  *
       
   288  * @param array $allowed_files List of theme file paths.
       
   289  * @return array Tree structure for listing theme files.
       
   290  */
       
   291 function wp_make_theme_file_tree( $allowed_files ) {
       
   292 	$tree_list = array();
       
   293 	foreach ( $allowed_files as $file_name => $absolute_filename ) {
       
   294 		$list = explode( '/', $file_name );
       
   295 		$last_dir = &$tree_list;
       
   296 		foreach ( $list as $dir ) {
       
   297 			$last_dir =& $last_dir[ $dir ];
       
   298 		}
       
   299 		$last_dir = $file_name;
       
   300 	}
       
   301 	return $tree_list;
       
   302 }
       
   303 
       
   304 /**
       
   305  * Outputs the formatted file list for the Theme Editor.
       
   306  *
       
   307  * @since 4.9.0
       
   308  * @access private
       
   309  *
       
   310  * @param array|string $tree  List of file/folder paths, or filename.
       
   311  * @param int          $level The aria-level for the current iteration.
       
   312  * @param int          $size  The aria-setsize for the current iteration.
       
   313  * @param int          $index The aria-posinset for the current iteration.
       
   314  */
       
   315 function wp_print_theme_file_tree( $tree, $level = 2, $size = 1, $index = 1 ) {
       
   316 	global $relative_file, $stylesheet;
       
   317 
       
   318 	if ( is_array( $tree ) ) {
       
   319 		$index = 0;
       
   320 		$size = count( $tree );
       
   321 		foreach ( $tree as $label => $theme_file ) :
       
   322 			$index++;
       
   323 			if ( ! is_array( $theme_file ) ) {
       
   324 				wp_print_theme_file_tree( $theme_file, $level, $index, $size );
       
   325 				continue;
       
   326 			}
       
   327 			?>
       
   328 			<li role="treeitem" aria-expanded="true" tabindex="-1"
       
   329 				aria-level="<?php echo esc_attr( $level ); ?>"
       
   330 				aria-setsize="<?php echo esc_attr( $size ); ?>"
       
   331 				aria-posinset="<?php echo esc_attr( $index ); ?>">
       
   332 				<span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text"><?php _e( 'folder' ); ?></span><span aria-hidden="true" class="icon"></span></span>
       
   333 				<ul role="group" class="tree-folder"><?php wp_print_theme_file_tree( $theme_file, $level + 1, $index, $size ); ?></ul>
       
   334 			</li>
       
   335 			<?php
       
   336 		endforeach;
       
   337 	} else {
       
   338 		$filename = $tree;
       
   339 		$url = add_query_arg(
       
   340 			array(
       
   341 				'file' => rawurlencode( $tree ),
       
   342 				'theme' => rawurlencode( $stylesheet ),
       
   343 			),
       
   344 			self_admin_url( 'theme-editor.php' )
       
   345 		);
       
   346 		?>
       
   347 		<li role="none" class="<?php echo esc_attr( $relative_file === $filename ? 'current-file' : '' ); ?>">
       
   348 			<a role="treeitem" tabindex="<?php echo esc_attr( $relative_file === $filename ? '0' : '-1' ); ?>"
       
   349 				href="<?php echo esc_url( $url ); ?>"
       
   350 				aria-level="<?php echo esc_attr( $level ); ?>"
       
   351 				aria-setsize="<?php echo esc_attr( $size ); ?>"
       
   352 				aria-posinset="<?php echo esc_attr( $index ); ?>">
       
   353 				<?php
       
   354 				$file_description = esc_html( get_file_description( $filename ) );
       
   355 				if ( $file_description !== $filename && basename( $filename ) !== $file_description ) {
       
   356 					$file_description .= '<br /><span class="nonessential">(' . esc_html( $filename ) . ')</span>';
       
   357 				}
       
   358 
       
   359 				if ( $relative_file === $filename ) {
       
   360 					echo '<span class="notice notice-info">' . $file_description . '</span>';
       
   361 				} else {
       
   362 					echo $file_description;
       
   363 				}
       
   364 				?>
       
   365 			</a>
       
   366 		</li>
       
   367 		<?php
       
   368 	}
       
   369 }
       
   370 
       
   371 /**
       
   372  * Makes a tree structure for the Plugin Editor's file list.
       
   373  *
       
   374  * @since 4.9.0
       
   375  * @access private
       
   376  *
       
   377  * @param string $plugin_editable_files List of plugin file paths.
       
   378  * @return array Tree structure for listing plugin files.
       
   379  */
       
   380 function wp_make_plugin_file_tree( $plugin_editable_files ) {
       
   381 	$tree_list = array();
       
   382 	foreach ( $plugin_editable_files as $plugin_file ) {
       
   383 		$list = explode( '/', preg_replace( '#^.+?/#', '', $plugin_file ) );
       
   384 		$last_dir = &$tree_list;
       
   385 		foreach ( $list as $dir ) {
       
   386 			$last_dir =& $last_dir[ $dir ];
       
   387 		}
       
   388 		$last_dir = $plugin_file;
       
   389 	}
       
   390 	return $tree_list;
       
   391 }
       
   392 
       
   393 /**
       
   394  * Outputs the formatted file list for the Plugin Editor.
       
   395  *
       
   396  * @since 4.9.0
       
   397  * @access private
       
   398  *
       
   399  * @param array|string $tree  List of file/folder paths, or filename.
       
   400  * @param string       $label Name of file or folder to print.
       
   401  * @param int          $level The aria-level for the current iteration.
       
   402  * @param int          $size  The aria-setsize for the current iteration.
       
   403  * @param int          $index The aria-posinset for the current iteration.
       
   404  */
       
   405 function wp_print_plugin_file_tree( $tree, $label = '', $level = 2, $size = 1, $index = 1 ) {
       
   406 	global $file, $plugin;
       
   407 	if ( is_array( $tree ) ) {
       
   408 		$index = 0;
       
   409 		$size = count( $tree );
       
   410 		foreach ( $tree as $label => $plugin_file ) :
       
   411 			$index++;
       
   412 			if ( ! is_array( $plugin_file ) ) {
       
   413 				wp_print_plugin_file_tree( $plugin_file, $label, $level, $index, $size );
       
   414 				continue;
       
   415 			}
       
   416 			?>
       
   417 			<li role="treeitem" aria-expanded="true" tabindex="-1"
       
   418 				aria-level="<?php echo esc_attr( $level ); ?>"
       
   419 				aria-setsize="<?php echo esc_attr( $size ); ?>"
       
   420 				aria-posinset="<?php echo esc_attr( $index ); ?>">
       
   421 				<span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text"><?php _e( 'folder' ); ?></span><span aria-hidden="true" class="icon"></span></span>
       
   422 				<ul role="group" class="tree-folder"><?php wp_print_plugin_file_tree( $plugin_file, '', $level + 1, $index, $size ); ?></ul>
       
   423 			</li>
       
   424 			<?php
       
   425 		endforeach;
       
   426 	} else {
       
   427 		$url = add_query_arg(
       
   428 			array(
       
   429 				'file' => rawurlencode( $tree ),
       
   430 				'plugin' => rawurlencode( $plugin ),
       
   431 			),
       
   432 			self_admin_url( 'plugin-editor.php' )
       
   433 		);
       
   434 		?>
       
   435 		<li role="none" class="<?php echo esc_attr( $file === $tree ? 'current-file' : '' ); ?>">
       
   436 			<a role="treeitem" tabindex="<?php echo esc_attr( $file === $tree ? '0' : '-1' ); ?>"
       
   437 				href="<?php echo esc_url( $url ); ?>"
       
   438 				aria-level="<?php echo esc_attr( $level ); ?>"
       
   439 				aria-setsize="<?php echo esc_attr( $size ); ?>"
       
   440 				aria-posinset="<?php echo esc_attr( $index ); ?>">
       
   441 				<?php
       
   442 				if ( $file === $tree ) {
       
   443 					echo '<span class="notice notice-info">' . esc_html( $label ) . '</span>';
       
   444 				} else {
       
   445 					echo esc_html( $label );
       
   446 				}
       
   447 				?>
       
   448 			</a>
       
   449 		</li>
       
   450 		<?php
       
   451 	}
       
   452 }
       
   453 
       
   454 /**
       
   455  * Flushes rewrite rules if siteurl, home or page_on_front changed.
   233  *
   456  *
   234  * @since 2.1.0
   457  * @since 2.1.0
   235  *
   458  *
   236  * @param string $old_value
   459  * @param string $old_value
   237  * @param string $value
   460  * @param string $value
   238  */
   461  */
   239 function update_home_siteurl( $old_value, $value ) {
   462 function update_home_siteurl( $old_value, $value ) {
   240 	if ( defined( "WP_INSTALLING" ) )
   463 	if ( wp_installing() )
   241 		return;
   464 		return;
   242 
   465 
   243 	// If home changed, write rewrite rules to new location.
   466 	if ( is_multisite() && ms_is_switched() ) {
   244 	flush_rewrite_rules();
   467 		delete_option( 'rewrite_rules' );
   245 }
   468 	} else {
   246 
   469 		flush_rewrite_rules();
   247 add_action( 'update_option_home', 'update_home_siteurl', 10, 2 );
   470 	}
   248 add_action( 'update_option_siteurl', 'update_home_siteurl', 10, 2 );
   471 }
   249 add_action( 'update_option_page_on_front', 'update_home_siteurl', 10, 2 );
   472 
   250 
       
   251 /**
       
   252  * Shorten an URL, to be used as link text
       
   253  *
       
   254  * @since 1.2.0
       
   255  *
       
   256  * @param string $url
       
   257  * @return string
       
   258  */
       
   259 function url_shorten( $url ) {
       
   260 	$short_url = str_replace( array( 'http://', 'www.' ), '', $url );
       
   261 	$short_url = untrailingslashit( $short_url );
       
   262 	if ( strlen( $short_url ) > 35 )
       
   263 		$short_url = substr( $short_url, 0, 32 ) . '&hellip;';
       
   264 	return $short_url;
       
   265 }
       
   266 
   473 
   267 /**
   474 /**
   268  * Resets global variables based on $_GET and $_POST
   475  * Resets global variables based on $_GET and $_POST
   269  *
   476  *
   270  * This function resets global variables based on the names passed
   477  * This function resets global variables based on the names passed
   288 		}
   495 		}
   289 	}
   496 	}
   290 }
   497 }
   291 
   498 
   292 /**
   499 /**
   293  * {@internal Missing Short Description}}
   500  * Displays the given administration message.
   294  *
   501  *
   295  * @since 2.1.0
   502  * @since 2.1.0
   296  *
   503  *
   297  * @param string|WP_Error $message
   504  * @param string|WP_Error $message
   298  */
   505  */
   306 	echo "<p>$message</p>\n";
   513 	echo "<p>$message</p>\n";
   307 	wp_ob_end_flush_all();
   514 	wp_ob_end_flush_all();
   308 	flush();
   515 	flush();
   309 }
   516 }
   310 
   517 
       
   518 /**
       
   519  * @since 2.8.0
       
   520  *
       
   521  * @param string $content
       
   522  * @return array
       
   523  */
   311 function wp_doc_link_parse( $content ) {
   524 function wp_doc_link_parse( $content ) {
   312 	if ( !is_string( $content ) || empty( $content ) )
   525 	if ( !is_string( $content ) || empty( $content ) )
   313 		return array();
   526 		return array();
   314 
   527 
   315 	if ( !function_exists('token_get_all') )
   528 	if ( !function_exists('token_get_all') )
   336 
   549 
   337 	$functions = array_unique( $functions );
   550 	$functions = array_unique( $functions );
   338 	sort( $functions );
   551 	sort( $functions );
   339 
   552 
   340 	/**
   553 	/**
   341 	 * Filter the list of functions and classes to be ignored from the documentation lookup.
   554 	 * Filters the list of functions and classes to be ignored from the documentation lookup.
   342 	 *
   555 	 *
   343 	 * @since 2.8.0
   556 	 * @since 2.8.0
   344 	 *
   557 	 *
   345 	 * @param array $ignore_functions Functions and classes to be ignored.
   558 	 * @param array $ignore_functions Functions and classes to be ignored.
   346 	 */
   559 	 */
   391 			case 'users_per_page':
   604 			case 'users_per_page':
   392 			case 'edit_comments_per_page':
   605 			case 'edit_comments_per_page':
   393 			case 'upload_per_page':
   606 			case 'upload_per_page':
   394 			case 'edit_tags_per_page':
   607 			case 'edit_tags_per_page':
   395 			case 'plugins_per_page':
   608 			case 'plugins_per_page':
       
   609 			case 'export_personal_data_requests_per_page':
       
   610 			case 'remove_personal_data_requests_per_page':
   396 			// Network admin
   611 			// Network admin
   397 			case 'sites_network_per_page':
   612 			case 'sites_network_per_page':
   398 			case 'users_network_per_page':
   613 			case 'users_network_per_page':
   399 			case 'site_users_network_per_page':
   614 			case 'site_users_network_per_page':
   400 			case 'plugins_network_per_page':
   615 			case 'plugins_network_per_page':
   405 					return;
   620 					return;
   406 				break;
   621 				break;
   407 			default:
   622 			default:
   408 
   623 
   409 				/**
   624 				/**
   410 				 * Filter a screen option value before it is set.
   625 				 * Filters a screen option value before it is set.
   411 				 *
   626 				 *
   412 				 * The filter can also be used to modify non-standard [items]_per_page
   627 				 * 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.
   628 				 * settings. See the parent function for a full list of standard options.
   414 				 *
   629 				 *
   415 				 * Returning false to the filter will skip saving the current option.
   630 				 * Returning false to the filter will skip saving the current option.
   428 					return;
   643 					return;
   429 				break;
   644 				break;
   430 		}
   645 		}
   431 
   646 
   432 		update_user_meta($user->ID, $option, $value);
   647 		update_user_meta($user->ID, $option, $value);
   433 		wp_safe_redirect( remove_query_arg( array('pagenum', 'apage', 'paged'), wp_get_referer() ) );
   648 
       
   649 		$url = remove_query_arg( array( 'pagenum', 'apage', 'paged' ), wp_get_referer() );
       
   650 		if ( isset( $_POST['mode'] ) ) {
       
   651 			$url = add_query_arg( array( 'mode' => $_POST['mode'] ), $url );
       
   652 		}
       
   653 
       
   654 		wp_safe_redirect( $url );
   434 		exit;
   655 		exit;
   435 	}
   656 	}
   436 }
   657 }
   437 
   658 
   438 /**
   659 /**
   444  * @param string $filename The file path to the configuration file
   665  * @param string $filename The file path to the configuration file
   445  */
   666  */
   446 function iis7_rewrite_rule_exists($filename) {
   667 function iis7_rewrite_rule_exists($filename) {
   447 	if ( ! file_exists($filename) )
   668 	if ( ! file_exists($filename) )
   448 		return false;
   669 		return false;
   449 	if ( ! class_exists('DOMDocument') )
   670 	if ( ! class_exists( 'DOMDocument', false ) ) {
   450 		return false;
   671 		return false;
       
   672 	}
   451 
   673 
   452 	$doc = new DOMDocument();
   674 	$doc = new DOMDocument();
   453 	if ( $doc->load($filename) === false )
   675 	if ( $doc->load($filename) === false )
   454 		return false;
   676 		return false;
   455 	$xpath = new DOMXPath($doc);
   677 	$xpath = new DOMXPath($doc);
   456 	$rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
   678 	$rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]');
   457 	if ( $rules->length == 0 )
   679 	if ( $rules->length == 0 )
   458 		return false;
   680 		return false;
   459 	else
   681 	else
   460 		return true;
   682 		return true;
   461 }
   683 }
   471 function iis7_delete_rewrite_rule($filename) {
   693 function iis7_delete_rewrite_rule($filename) {
   472 	// If configuration file does not exist then rules also do not exist so there is nothing to delete
   694 	// If configuration file does not exist then rules also do not exist so there is nothing to delete
   473 	if ( ! file_exists($filename) )
   695 	if ( ! file_exists($filename) )
   474 		return true;
   696 		return true;
   475 
   697 
   476 	if ( ! class_exists('DOMDocument') )
   698 	if ( ! class_exists( 'DOMDocument', false ) ) {
   477 		return false;
   699 		return false;
       
   700 	}
   478 
   701 
   479 	$doc = new DOMDocument();
   702 	$doc = new DOMDocument();
   480 	$doc->preserveWhiteSpace = false;
   703 	$doc->preserveWhiteSpace = false;
   481 
   704 
   482 	if ( $doc -> load($filename) === false )
   705 	if ( $doc -> load($filename) === false )
   483 		return false;
   706 		return false;
   484 	$xpath = new DOMXPath($doc);
   707 	$xpath = new DOMXPath($doc);
   485 	$rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
   708 	$rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]');
   486 	if ( $rules->length > 0 ) {
   709 	if ( $rules->length > 0 ) {
   487 		$child = $rules->item(0);
   710 		$child = $rules->item(0);
   488 		$parent = $child->parentNode;
   711 		$parent = $child->parentNode;
   489 		$parent->removeChild($child);
   712 		$parent->removeChild($child);
   490 		$doc->formatOutput = true;
   713 		$doc->formatOutput = true;
   501  * @param string $filename The file path to the configuration file
   724  * @param string $filename The file path to the configuration file
   502  * @param string $rewrite_rule The XML fragment with URL Rewrite rule
   725  * @param string $rewrite_rule The XML fragment with URL Rewrite rule
   503  * @return bool
   726  * @return bool
   504  */
   727  */
   505 function iis7_add_rewrite_rule($filename, $rewrite_rule) {
   728 function iis7_add_rewrite_rule($filename, $rewrite_rule) {
   506 	if ( ! class_exists('DOMDocument') )
   729 	if ( ! class_exists( 'DOMDocument', false ) ) {
   507 		return false;
   730 		return false;
       
   731 	}
   508 
   732 
   509 	// If configuration file does not exist then we create one.
   733 	// If configuration file does not exist then we create one.
   510 	if ( ! file_exists($filename) ) {
   734 	if ( ! file_exists($filename) ) {
   511 		$fp = fopen( $filename, 'w');
   735 		$fp = fopen( $filename, 'w');
   512 		fwrite($fp, '<configuration/>');
   736 		fwrite($fp, '<configuration/>');
   520 		return false;
   744 		return false;
   521 
   745 
   522 	$xpath = new DOMXPath($doc);
   746 	$xpath = new DOMXPath($doc);
   523 
   747 
   524 	// First check if the rule already exists as in that case there is no need to re-add it
   748 	// First check if the rule already exists as in that case there is no need to re-add it
   525 	$wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
   749 	$wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]');
   526 	if ( $wordpress_rules->length > 0 )
   750 	if ( $wordpress_rules->length > 0 )
   527 		return true;
   751 		return true;
   528 
   752 
   529 	// Check the XPath to the rewrite rule and create XML nodes if they do not exist
   753 	// Check the XPath to the rewrite rule and create XML nodes if they do not exist
   530 	$xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
   754 	$xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
   591 
   815 
   592 /**
   816 /**
   593  * Display the default admin color scheme picker (Used in user-edit.php)
   817  * Display the default admin color scheme picker (Used in user-edit.php)
   594  *
   818  *
   595  * @since 3.0.0
   819  * @since 3.0.0
       
   820  *
       
   821  * @global array $_wp_admin_css_colors
       
   822  *
       
   823  * @param int $user_id User ID.
   596  */
   824  */
   597 function admin_color_scheme_picker( $user_id ) {
   825 function admin_color_scheme_picker( $user_id ) {
   598 	global $_wp_admin_css_colors;
   826 	global $_wp_admin_css_colors;
   599 
   827 
   600 	ksort( $_wp_admin_css_colors );
   828 	ksort( $_wp_admin_css_colors );
   644 	?>
   872 	?>
   645 	</fieldset>
   873 	</fieldset>
   646 	<?php
   874 	<?php
   647 }
   875 }
   648 
   876 
       
   877 /**
       
   878  *
       
   879  * @global array $_wp_admin_css_colors
       
   880  */
   649 function wp_color_scheme_settings() {
   881 function wp_color_scheme_settings() {
   650 	global $_wp_admin_css_colors;
   882 	global $_wp_admin_css_colors;
   651 
   883 
   652 	$color_scheme = get_user_option( 'admin_color' );
   884 	$color_scheme = get_user_option( 'admin_color' );
   653 
   885 
   660 		$icon_colors = $_wp_admin_css_colors[ $color_scheme ]->icon_colors;
   892 		$icon_colors = $_wp_admin_css_colors[ $color_scheme ]->icon_colors;
   661 	} elseif ( ! empty( $_wp_admin_css_colors['fresh']->icon_colors ) ) {
   893 	} elseif ( ! empty( $_wp_admin_css_colors['fresh']->icon_colors ) ) {
   662 		$icon_colors = $_wp_admin_css_colors['fresh']->icon_colors;
   894 		$icon_colors = $_wp_admin_css_colors['fresh']->icon_colors;
   663 	} else {
   895 	} else {
   664 		// Fall back to the default set of icon colors if the default scheme is missing.
   896 		// 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' );
   897 		$icon_colors = array( 'base' => '#82878c', 'focus' => '#00a0d2', 'current' => '#fff' );
   666 	}
   898 	}
   667 
   899 
   668 	echo '<script type="text/javascript">var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ) ) . ";</script>\n";
   900 	echo '<script type="text/javascript">var _wpColorScheme = ' . wp_json_encode( array( 'icons' => $icon_colors ) ) . ";</script>\n";
   669 }
   901 }
   670 add_action( 'admin_head', 'wp_color_scheme_settings' );
   902 
   671 
   903 /**
       
   904  * @since 3.3.0
       
   905  */
   672 function _ipad_meta() {
   906 function _ipad_meta() {
   673 	if ( wp_is_mobile() ) {
   907 	if ( wp_is_mobile() ) {
   674 		?>
   908 		?>
   675 		<meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
   909 		<meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1">
   676 		<?php
   910 		<?php
   677 	}
   911 	}
   678 }
   912 }
   679 add_action('admin_head', '_ipad_meta');
       
   680 
   913 
   681 /**
   914 /**
   682  * Check lock status for posts displayed on the Posts screen
   915  * Check lock status for posts displayed on the Posts screen
   683  *
   916  *
   684  * @since 3.6.0
   917  * @since 3.6.0
       
   918  *
       
   919  * @param array  $response  The Heartbeat response.
       
   920  * @param array  $data      The $_POST data sent.
       
   921  * @param string $screen_id The screen id.
       
   922  * @return array The Heartbeat response.
   685  */
   923  */
   686 function wp_check_locked_posts( $response, $data, $screen_id ) {
   924 function wp_check_locked_posts( $response, $data, $screen_id ) {
   687 	$checked = array();
   925 	$checked = array();
   688 
   926 
   689 	if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
   927 	if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
   705 	if ( ! empty( $checked ) )
   943 	if ( ! empty( $checked ) )
   706 		$response['wp-check-locked-posts'] = $checked;
   944 		$response['wp-check-locked-posts'] = $checked;
   707 
   945 
   708 	return $response;
   946 	return $response;
   709 }
   947 }
   710 add_filter( 'heartbeat_received', 'wp_check_locked_posts', 10, 3 );
       
   711 
   948 
   712 /**
   949 /**
   713  * Check lock status on the New/Edit Post screen and refresh the lock
   950  * Check lock status on the New/Edit Post screen and refresh the lock
   714  *
   951  *
   715  * @since 3.6.0
   952  * @since 3.6.0
       
   953  *
       
   954  * @param array  $response  The Heartbeat response.
       
   955  * @param array  $data      The $_POST data sent.
       
   956  * @param string $screen_id The screen id.
       
   957  * @return array The Heartbeat response.
   716  */
   958  */
   717 function wp_refresh_post_lock( $response, $data, $screen_id ) {
   959 function wp_refresh_post_lock( $response, $data, $screen_id ) {
   718 	if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
   960 	if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
   719 		$received = $data['wp-refresh-post-lock'];
   961 		$received = $data['wp-refresh-post-lock'];
   720 		$send = array();
   962 		$send = array();
   744 		$response['wp-refresh-post-lock'] = $send;
   986 		$response['wp-refresh-post-lock'] = $send;
   745 	}
   987 	}
   746 
   988 
   747 	return $response;
   989 	return $response;
   748 }
   990 }
   749 add_filter( 'heartbeat_received', 'wp_refresh_post_lock', 10, 3 );
       
   750 
   991 
   751 /**
   992 /**
   752  * Check nonce expiration on the New/Edit Post screen and refresh if needed
   993  * Check nonce expiration on the New/Edit Post screen and refresh if needed
   753  *
   994  *
   754  * @since 3.6.0
   995  * @since 3.6.0
       
   996  *
       
   997  * @param array  $response  The Heartbeat response.
       
   998  * @param array  $data      The $_POST data sent.
       
   999  * @param string $screen_id The screen id.
       
  1000  * @return array The Heartbeat response.
   755  */
  1001  */
   756 function wp_refresh_post_nonces( $response, $data, $screen_id ) {
  1002 function wp_refresh_post_nonces( $response, $data, $screen_id ) {
   757 	if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
  1003 	if ( array_key_exists( 'wp-refresh-post-nonces', $data ) ) {
   758 		$received = $data['wp-refresh-post-nonces'];
  1004 		$received = $data['wp-refresh-post-nonces'];
   759 		$response['wp-refresh-post-nonces'] = array( 'check' => 1 );
  1005 		$response['wp-refresh-post-nonces'] = array( 'check' => 1 );
   760 
  1006 
   761 		if ( ! $post_id = absint( $received['post_id'] ) )
  1007 		if ( ! $post_id = absint( $received['post_id'] ) ) {
   762 			return $response;
  1008 			return $response;
   763 
  1009 		}
   764 		if ( ! current_user_can( 'edit_post', $post_id ) || empty( $received['post_nonce'] ) )
  1010 
       
  1011 		if ( ! current_user_can( 'edit_post', $post_id ) ) {
   765 			return $response;
  1012 			return $response;
   766 
  1013 		}
   767 		if ( 2 === wp_verify_nonce( $received['post_nonce'], 'update-post_' . $post_id ) ) {
  1014 
   768 			$response['wp-refresh-post-nonces'] = array(
  1015 		$response['wp-refresh-post-nonces'] = array(
   769 				'replace' => array(
  1016 			'replace' => array(
   770 					'getpermalinknonce' => wp_create_nonce('getpermalink'),
  1017 				'getpermalinknonce' => wp_create_nonce('getpermalink'),
   771 					'samplepermalinknonce' => wp_create_nonce('samplepermalink'),
  1018 				'samplepermalinknonce' => wp_create_nonce('samplepermalink'),
   772 					'closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
  1019 				'closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
   773 					'_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
  1020 				'_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ),
   774 					'_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
  1021 				'_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ),
   775 				),
  1022 			),
   776 				'heartbeatNonce' => wp_create_nonce( 'heartbeat-nonce' ),
  1023 			'heartbeatNonce' => wp_create_nonce( 'heartbeat-nonce' ),
   777 			);
  1024 		);
   778 		}
       
   779 	}
  1025 	}
   780 
  1026 
   781 	return $response;
  1027 	return $response;
   782 }
  1028 }
   783 add_filter( 'heartbeat_received', 'wp_refresh_post_nonces', 10, 3 );
       
   784 
  1029 
   785 /**
  1030 /**
   786  * Disable suspension of Heartbeat on the Add/Edit Post screens.
  1031  * Disable suspension of Heartbeat on the Add/Edit Post screens.
   787  *
  1032  *
   788  * @since 3.8.0
  1033  * @since 3.8.0
       
  1034  *
       
  1035  * @global string $pagenow
   789  *
  1036  *
   790  * @param array $settings An array of Heartbeat settings.
  1037  * @param array $settings An array of Heartbeat settings.
   791  * @return array Filtered Heartbeat settings.
  1038  * @return array Filtered Heartbeat settings.
   792  */
  1039  */
   793 function wp_heartbeat_set_suspension( $settings ) {
  1040 function wp_heartbeat_set_suspension( $settings ) {
   797 		$settings['suspension'] = 'disable';
  1044 		$settings['suspension'] = 'disable';
   798 	}
  1045 	}
   799 
  1046 
   800 	return $settings;
  1047 	return $settings;
   801 }
  1048 }
   802 add_filter( 'heartbeat_settings', 'wp_heartbeat_set_suspension' );
       
   803 
  1049 
   804 /**
  1050 /**
   805  * Autosave with heartbeat
  1051  * Autosave with heartbeat
   806  *
  1052  *
   807  * @since 3.9.0
  1053  * @since 3.9.0
       
  1054  *
       
  1055  * @param array $response The Heartbeat response.
       
  1056  * @param array $data     The $_POST data sent.
       
  1057  * @return array The Heartbeat response.
   808  */
  1058  */
   809 function heartbeat_autosave( $response, $data ) {
  1059 function heartbeat_autosave( $response, $data ) {
   810 	if ( ! empty( $data['wp_autosave'] ) ) {
  1060 	if ( ! empty( $data['wp_autosave'] ) ) {
   811 		$saved = wp_autosave( $data['wp_autosave'] );
  1061 		$saved = wp_autosave( $data['wp_autosave'] );
   812 
  1062 
   813 		if ( is_wp_error( $saved ) ) {
  1063 		if ( is_wp_error( $saved ) ) {
   814 			$response['wp_autosave'] = array( 'success' => false, 'message' => $saved->get_error_message() );
  1064 			$response['wp_autosave'] = array( 'success' => false, 'message' => $saved->get_error_message() );
   815 		} elseif ( empty( $saved ) ) {
  1065 		} elseif ( empty( $saved ) ) {
   816 			$response['wp_autosave'] = array( 'success' => false, 'message' => __( 'Error while saving.' ) );
  1066 			$response['wp_autosave'] = array( 'success' => false, 'message' => __( 'Error while saving.' ) );
   817 		} else {
  1067 		} else {
   818 			/* translators: draft saved date format, see http://php.net/date */
  1068 			/* translators: draft saved date format, see https://secure.php.net/date */
   819 			$draft_saved_date_format = __( 'g:i:s a' );
  1069 			$draft_saved_date_format = __( 'g:i:s a' );
   820 			/* translators: %s: date and time */
  1070 			/* translators: %s: date and time */
   821 			$response['wp_autosave'] = array( 'success' => true, 'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ) );
  1071 			$response['wp_autosave'] = array( 'success' => true, 'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ) );
   822 		}
  1072 		}
   823 	}
  1073 	}
   824 
  1074 
   825 	return $response;
  1075 	return $response;
   826 }
  1076 }
   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 
  1077 
   846 /**
  1078 /**
   847  * Remove single-use URL parameters and create canonical link based on new URL.
  1079  * Remove single-use URL parameters and create canonical link based on new URL.
   848  *
  1080  *
   849  * Remove specific query string parameters from a URL, create the canonical link,
  1081  * 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.
  1082  * put it in the admin header, and change the current URL to match.
   851  *
  1083  *
   852  * @since 4.2.0
  1084  * @since 4.2.0
   853  */
  1085  */
   854 function wp_admin_canonical_url() {
  1086 function wp_admin_canonical_url() {
   855 	$removable_query_args = array(
  1087 	$removable_query_args = wp_removable_query_args();
   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 
  1088 
   873 	if ( empty( $removable_query_args ) ) {
  1089 	if ( empty( $removable_query_args ) ) {
   874 		return;
  1090 		return;
   875 	}
  1091 	}
   876 
  1092 
   884 			window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
  1100 			window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash );
   885 		}
  1101 		}
   886 	</script>
  1102 	</script>
   887 <?php
  1103 <?php
   888 }
  1104 }
   889 add_action( 'admin_head', 'wp_admin_canonical_url' );
  1105 
       
  1106 /**
       
  1107  * Send a referrer policy header so referrers are not sent externally from administration screens.
       
  1108  *
       
  1109  * @since 4.9.0
       
  1110  */
       
  1111 function wp_admin_headers() {
       
  1112 	$policy = 'strict-origin-when-cross-origin';
       
  1113 
       
  1114 	/**
       
  1115 	 * Filters the admin referrer policy header value.
       
  1116 	 *
       
  1117 	 * @since 4.9.0
       
  1118 	 * @since 4.9.5 The default value was changed to 'strict-origin-when-cross-origin'.
       
  1119 	 *
       
  1120 	 * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
       
  1121 	 *
       
  1122 	 * @param string $policy The admin referrer policy header value. Default 'strict-origin-when-cross-origin'.
       
  1123 	 */
       
  1124 	$policy = apply_filters( 'admin_referrer_policy', $policy );
       
  1125 
       
  1126 	header( sprintf( 'Referrer-Policy: %s', $policy ) );
       
  1127 }
       
  1128 
       
  1129 /**
       
  1130  * Outputs JS that reloads the page if the user navigated to it with the Back or Forward button.
       
  1131  *
       
  1132  * Used on the Edit Post and Add New Post screens. Needed to ensure the page is not loaded from browser cache,
       
  1133  * so the post title and editor content are the last saved versions. Ideally this script should run first in the head.
       
  1134  *
       
  1135  * @since 4.6.0
       
  1136  */
       
  1137 function wp_page_reload_on_back_button_js() {
       
  1138 	?>
       
  1139 	<script>
       
  1140 		if ( typeof performance !== 'undefined' && performance.navigation && performance.navigation.type === 2 ) {
       
  1141 			document.location.reload( true );
       
  1142 		}
       
  1143 	</script>
       
  1144 	<?php
       
  1145 }
       
  1146 
       
  1147 /**
       
  1148  * Send a confirmation request email when a change of site admin email address is attempted.
       
  1149  *
       
  1150  * The new site admin address will not become active until confirmed.
       
  1151  *
       
  1152  * @since 3.0.0
       
  1153  * @since 4.9.0 This function was moved from wp-admin/includes/ms.php so it's no longer Multisite specific.
       
  1154  *
       
  1155  * @param string $old_value The old site admin email address.
       
  1156  * @param string $value     The proposed new site admin email address.
       
  1157  */
       
  1158 function update_option_new_admin_email( $old_value, $value ) {
       
  1159 	if ( $value == get_option( 'admin_email' ) || ! is_email( $value ) ) {
       
  1160 		return;
       
  1161 	}
       
  1162 
       
  1163 	$hash = md5( $value . time() . wp_rand() );
       
  1164 	$new_admin_email = array(
       
  1165 		'hash'     => $hash,
       
  1166 		'newemail' => $value,
       
  1167 	);
       
  1168 	update_option( 'adminhash', $new_admin_email );
       
  1169 
       
  1170 	$switched_locale = switch_to_locale( get_user_locale() );
       
  1171 
       
  1172 	/* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
       
  1173 	$email_text = __( 'Howdy ###USERNAME###,
       
  1174 
       
  1175 You recently requested to have the administration email address on
       
  1176 your site changed.
       
  1177 
       
  1178 If this is correct, please click on the following link to change it:
       
  1179 ###ADMIN_URL###
       
  1180 
       
  1181 You can safely ignore and delete this email if you do not want to
       
  1182 take this action.
       
  1183 
       
  1184 This email has been sent to ###EMAIL###
       
  1185 
       
  1186 Regards,
       
  1187 All at ###SITENAME###
       
  1188 ###SITEURL###' );
       
  1189 
       
  1190 	/**
       
  1191 	 * Filters the text of the email sent when a change of site admin email address is attempted.
       
  1192 	 *
       
  1193 	 * The following strings have a special meaning and will get replaced dynamically:
       
  1194 	 * ###USERNAME###  The current user's username.
       
  1195 	 * ###ADMIN_URL### The link to click on to confirm the email change.
       
  1196 	 * ###EMAIL###     The proposed new site admin email address.
       
  1197 	 * ###SITENAME###  The name of the site.
       
  1198 	 * ###SITEURL###   The URL to the site.
       
  1199 	 *
       
  1200 	 * @since MU (3.0.0)
       
  1201 	 * @since 4.9.0 This filter is no longer Multisite specific.
       
  1202 	 *
       
  1203 	 * @param string $email_text      Text in the email.
       
  1204 	 * @param array  $new_admin_email {
       
  1205 	 *     Data relating to the new site admin email address.
       
  1206 	 *
       
  1207 	 *     @type string $hash     The secure hash used in the confirmation link URL.
       
  1208 	 *     @type string $newemail The proposed new site admin email address.
       
  1209 	 * }
       
  1210 	 */
       
  1211 	$content = apply_filters( 'new_admin_email_content', $email_text, $new_admin_email );
       
  1212 
       
  1213 	$current_user = wp_get_current_user();
       
  1214 	$content = str_replace( '###USERNAME###', $current_user->user_login, $content );
       
  1215 	$content = str_replace( '###ADMIN_URL###', esc_url( self_admin_url( 'options.php?adminhash=' . $hash ) ), $content );
       
  1216 	$content = str_replace( '###EMAIL###', $value, $content );
       
  1217 	$content = str_replace( '###SITENAME###', wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $content );
       
  1218 	$content = str_replace( '###SITEURL###', home_url(), $content );
       
  1219 
       
  1220 	wp_mail( $value, sprintf( __( '[%s] New Admin Email Address' ), wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) ), $content );
       
  1221 
       
  1222 	if ( $switched_locale ) {
       
  1223 		restore_previous_locale();
       
  1224 	}
       
  1225 }
       
  1226 
       
  1227 /**
       
  1228  * Appends '(Draft)' to draft page titles in the privacy page dropdown
       
  1229  * so that unpublished content is obvious.
       
  1230  *
       
  1231  * @since 4.9.8
       
  1232  * @access private
       
  1233  *
       
  1234  * @param string  $title Page title.
       
  1235  * @param WP_Post $page  Page data object.
       
  1236  *
       
  1237  * @return string Page title.
       
  1238  */
       
  1239 function _wp_privacy_settings_filter_draft_page_titles( $title, $page ) {
       
  1240 	if ( 'draft' === $page->post_status && 'privacy' === get_current_screen()->id ) {
       
  1241 		/* translators: %s: Page Title */
       
  1242 		$title = sprintf( __( '%s (Draft)' ), $title );
       
  1243 	}
       
  1244 
       
  1245 	return $title;
       
  1246 }
       
  1247 
       
  1248 /**
       
  1249  * WP_Privacy_Policy_Content class.
       
  1250  * TODO: move this to a new file.
       
  1251  *
       
  1252  * @since 4.9.6
       
  1253  */
       
  1254 final class WP_Privacy_Policy_Content {
       
  1255 
       
  1256 	private static $policy_content = array();
       
  1257 
       
  1258 	/**
       
  1259 	 * Constructor
       
  1260 	 *
       
  1261 	 * @since 4.9.6
       
  1262 	 */
       
  1263 	private function __construct() {}
       
  1264 
       
  1265 	/**
       
  1266 	 * Add content to the postbox shown when editing the privacy policy.
       
  1267 	 *
       
  1268 	 * Plugins and themes should suggest text for inclusion in the site's privacy policy.
       
  1269 	 * The suggested text should contain information about any functionality that affects user privacy,
       
  1270 	 * and will be shown in the Suggested Privacy Policy Content postbox.
       
  1271 	 *
       
  1272 	 * Intended for use from `wp_add_privacy_policy_content()`.
       
  1273 	 *
       
  1274 	 * $since 4.9.6
       
  1275 	 *
       
  1276 	 * @param string $plugin_name The name of the plugin or theme that is suggesting content for the site's privacy policy.
       
  1277 	 * @param string $policy_text The suggested content for inclusion in the policy.
       
  1278 	 */
       
  1279 	public static function add( $plugin_name, $policy_text ) {
       
  1280 		if ( empty( $plugin_name ) || empty( $policy_text ) ) {
       
  1281 			return;
       
  1282 		}
       
  1283 
       
  1284 		$data = array(
       
  1285 			'plugin_name' => $plugin_name,
       
  1286 			'policy_text' => $policy_text,
       
  1287 		);
       
  1288 
       
  1289 		if ( ! in_array( $data, self::$policy_content, true ) ) {
       
  1290 			self::$policy_content[] = $data;
       
  1291 		}
       
  1292 	}
       
  1293 
       
  1294 	/**
       
  1295 	 * Quick check if any privacy info has changed.
       
  1296 	 *
       
  1297 	 * @since 4.9.6
       
  1298 	 */
       
  1299 	public static function text_change_check() {
       
  1300 
       
  1301 		$policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
       
  1302 
       
  1303 		// The site doesn't have a privacy policy.
       
  1304 		if ( empty( $policy_page_id ) ) {
       
  1305 			return false;
       
  1306 		}
       
  1307 
       
  1308 		if ( ! current_user_can( 'edit_post', $policy_page_id ) ) {
       
  1309 			return false;
       
  1310 		}
       
  1311 
       
  1312 		$old = (array) get_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' );
       
  1313 
       
  1314 		// Updates are not relevant if the user has not reviewed any suggestions yet.
       
  1315 		if ( empty( $old ) ) {
       
  1316 			return false;
       
  1317 		}
       
  1318 
       
  1319 		$cached = get_option( '_wp_suggested_policy_text_has_changed' );
       
  1320 
       
  1321 		/*
       
  1322 		 * When this function is called before `admin_init`, `self::$policy_content`
       
  1323 		 * has not been populated yet, so use the cached result from the last
       
  1324 		 * execution instead.
       
  1325 		 */
       
  1326 		if ( ! did_action( 'admin_init' ) ) {
       
  1327 			return 'changed' === $cached;
       
  1328 		}
       
  1329 
       
  1330 		$new = self::$policy_content;
       
  1331 
       
  1332 		// Remove the extra values added to the meta.
       
  1333 		foreach ( $old as $key => $data ) {
       
  1334 			if ( ! empty( $data['removed'] ) ) {
       
  1335 				unset( $old[ $key ] );
       
  1336 				continue;
       
  1337 			}
       
  1338 
       
  1339 			$old[ $key ] = array(
       
  1340 				'plugin_name' => $data['plugin_name'],
       
  1341 				'policy_text' => $data['policy_text'],
       
  1342 			);
       
  1343 		}
       
  1344 
       
  1345 		// Normalize the order of texts, to facilitate comparison.
       
  1346 		sort( $old );
       
  1347 		sort( $new );
       
  1348 
       
  1349 		// The == operator (equal, not identical) was used intentionally.
       
  1350 		// See http://php.net/manual/en/language.operators.array.php
       
  1351 		if ( $new != $old ) {
       
  1352 			// A plugin was activated or deactivated, or some policy text has changed.
       
  1353 			// Show a notice on the relevant screens to inform the admin.
       
  1354 			add_action( 'admin_notices', array( 'WP_Privacy_Policy_Content', 'policy_text_changed_notice' ) );
       
  1355 			$state = 'changed';
       
  1356 		} else {
       
  1357 			$state = 'not-changed';
       
  1358 		}
       
  1359 
       
  1360 		// Cache the result for use before `admin_init` (see above).
       
  1361 		if ( $cached !== $state ) {
       
  1362 			update_option( '_wp_suggested_policy_text_has_changed', $state );
       
  1363 		}
       
  1364 
       
  1365 		return 'changed' === $state;
       
  1366 	}
       
  1367 
       
  1368 	/**
       
  1369 	 * Output a warning when some privacy info has changed.
       
  1370 	 *
       
  1371 	 * @since 4.9.6
       
  1372 	 */
       
  1373 	public static function policy_text_changed_notice() {
       
  1374 		global $post;
       
  1375 
       
  1376 		$screen = get_current_screen()->id;
       
  1377 
       
  1378 		if ( 'privacy' !== $screen ) {
       
  1379 			return;
       
  1380 		}
       
  1381 
       
  1382 		?>
       
  1383 		<div class="policy-text-updated notice notice-warning is-dismissible">
       
  1384 			<p><?php
       
  1385 				printf(
       
  1386 					/* translators: %s: Privacy Policy Guide URL */
       
  1387 					__( 'The suggested privacy policy text has changed. Please <a href="%s">review the guide</a> and update your privacy policy.' ),
       
  1388 					esc_url( admin_url( 'tools.php?wp-privacy-policy-guide=1' ) )
       
  1389 				);
       
  1390 			?></p>
       
  1391 		</div>
       
  1392 		<?php
       
  1393 	}
       
  1394 
       
  1395 	/**
       
  1396 	 * Update the cached policy info when the policy page is updated.
       
  1397 	 *
       
  1398 	 * @since 4.9.6
       
  1399 	 * @access private
       
  1400 	 */
       
  1401 	public static function _policy_page_updated( $post_id ) {
       
  1402 		$policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
       
  1403 
       
  1404 		if ( ! $policy_page_id || $policy_page_id !== (int) $post_id ) {
       
  1405 			return;
       
  1406 		}
       
  1407 
       
  1408 		// Remove updated|removed status.
       
  1409 		$old = (array) get_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' );
       
  1410 		$done = array();
       
  1411 		$update_cache = false;
       
  1412 
       
  1413 		foreach ( $old as $old_key => $old_data ) {
       
  1414 			if ( ! empty( $old_data['removed'] ) ) {
       
  1415 				// Remove the old policy text.
       
  1416 				$update_cache = true;
       
  1417 				continue;
       
  1418 			}
       
  1419 
       
  1420 			if ( ! empty( $old_data['updated'] ) ) {
       
  1421 				// 'updated' is now 'added'.
       
  1422 				$done[] = array(
       
  1423 					'plugin_name' => $old_data['plugin_name'],
       
  1424 					'policy_text' => $old_data['policy_text'],
       
  1425 					'added'       => $old_data['updated'],
       
  1426 				);
       
  1427 				$update_cache = true;
       
  1428 			} else {
       
  1429 				$done[] = $old_data;
       
  1430 			}
       
  1431 		}
       
  1432 
       
  1433 		if ( $update_cache ) {
       
  1434 			delete_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' );
       
  1435 			// Update the cache.
       
  1436 			foreach ( $done as $data ) {
       
  1437 				add_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content', $data );
       
  1438 			}
       
  1439 		}
       
  1440 	}
       
  1441 
       
  1442 	/**
       
  1443 	 * Check for updated, added or removed privacy policy information from plugins.
       
  1444 	 *
       
  1445 	 * Caches the current info in post_meta of the policy page.
       
  1446 	 *
       
  1447 	 * @since 4.9.6
       
  1448 	 *
       
  1449 	 * @return array The privacy policy text/informtion added by core and plugins.
       
  1450 	 */
       
  1451 	public static function get_suggested_policy_text() {
       
  1452 		$policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
       
  1453 		$checked = array();
       
  1454 		$time = time();
       
  1455 		$update_cache = false;
       
  1456 		$new = self::$policy_content;
       
  1457 		$old = array();
       
  1458 
       
  1459 		if ( $policy_page_id ) {
       
  1460 			$old = (array) get_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' );
       
  1461 		}
       
  1462 
       
  1463 		// Check for no-changes and updates.
       
  1464 		foreach ( $new as $new_key => $new_data ) {
       
  1465 			foreach ( $old as $old_key => $old_data ) {
       
  1466 				$found = false;
       
  1467 
       
  1468 				if ( $new_data['policy_text'] === $old_data['policy_text'] ) {
       
  1469 					// Use the new plugin name in case it was changed, translated, etc.
       
  1470 					if ( $old_data['plugin_name'] !== $new_data['plugin_name'] ) {
       
  1471 						$old_data['plugin_name'] = $new_data['plugin_name'];
       
  1472 						$update_cache = true;
       
  1473 					}
       
  1474 
       
  1475 					// A plugin was re-activated.
       
  1476 					if ( ! empty( $old_data['removed'] ) ) {
       
  1477 						unset( $old_data['removed'] );
       
  1478 						$old_data['added'] = $time;
       
  1479 						$update_cache = true;
       
  1480 					}
       
  1481 
       
  1482 					$checked[] = $old_data;
       
  1483 					$found = true;
       
  1484 				} elseif ( $new_data['plugin_name'] === $old_data['plugin_name'] ) {
       
  1485 					// The info for the policy was updated.
       
  1486 					$checked[] = array(
       
  1487 						'plugin_name' => $new_data['plugin_name'],
       
  1488 						'policy_text' => $new_data['policy_text'],
       
  1489 						'updated'     => $time,
       
  1490 					);
       
  1491 					$found = $update_cache = true;
       
  1492 				}
       
  1493 
       
  1494 				if ( $found ) {
       
  1495 					unset( $new[ $new_key ], $old[ $old_key ] );
       
  1496 					continue 2;
       
  1497 				}
       
  1498 			}
       
  1499 		}
       
  1500 
       
  1501 		if ( ! empty( $new ) ) {
       
  1502 			// A plugin was activated.
       
  1503 			foreach ( $new as $new_data ) {
       
  1504 				if ( ! empty( $new_data['plugin_name'] ) && ! empty( $new_data['policy_text'] ) ) {
       
  1505 					$new_data['added'] = $time;
       
  1506 					$checked[]         = $new_data;
       
  1507 				}
       
  1508 			}
       
  1509 			$update_cache = true;
       
  1510 		}
       
  1511 
       
  1512 		if ( ! empty( $old ) ) {
       
  1513 			// A plugin was deactivated.
       
  1514 			foreach ( $old as $old_data ) {
       
  1515 				if ( ! empty( $old_data['plugin_name'] ) && ! empty( $old_data['policy_text'] ) ) {
       
  1516 					$data = array(
       
  1517 						'plugin_name' => $old_data['plugin_name'],
       
  1518 						'policy_text' => $old_data['policy_text'],
       
  1519 						'removed'     => $time,
       
  1520 					);
       
  1521 
       
  1522 					$checked[] = $data;
       
  1523 				}
       
  1524 			}
       
  1525 			$update_cache = true;
       
  1526 		}
       
  1527 
       
  1528 		if ( $update_cache && $policy_page_id ) {
       
  1529 			delete_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content' );
       
  1530 			// Update the cache.
       
  1531 			foreach ( $checked as $data ) {
       
  1532 				add_post_meta( $policy_page_id, '_wp_suggested_privacy_policy_content', $data );
       
  1533 			}
       
  1534 		}
       
  1535 
       
  1536 		return $checked;
       
  1537 	}
       
  1538 
       
  1539 	/**
       
  1540 	 * Add a notice with a link to the guide when editing the privacy policy page.
       
  1541 	 *
       
  1542 	 * @since 4.9.6
       
  1543 	 *
       
  1544 	 * @param $post WP_Post The currently edited post.
       
  1545 	 */
       
  1546 	public static function notice( $post ) {
       
  1547 		if ( ! ( $post instanceof WP_Post ) ) {
       
  1548 			return;
       
  1549 		}
       
  1550 
       
  1551 		if ( ! current_user_can( 'manage_privacy_options' ) ) {
       
  1552 			return;
       
  1553 		}
       
  1554 
       
  1555 		$policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
       
  1556 
       
  1557 		if ( ! $policy_page_id || $policy_page_id != $post->ID ) {
       
  1558 			return;
       
  1559 		}
       
  1560 
       
  1561 		?>
       
  1562 		<div class="notice notice-warning inline wp-pp-notice">
       
  1563 			<p>
       
  1564 			<?php
       
  1565 			/* translators: 1: Privacy Policy guide URL, 2: additional link attributes, 3: accessibility text */
       
  1566 			printf(
       
  1567 				__( 'Need help putting together your new Privacy Policy page? <a href="%1$s" %2$s>Check out our guide%3$s</a> for recommendations on what content to include, along with policies suggested by your plugins and theme.' ),
       
  1568 				admin_url( 'tools.php?wp-privacy-policy-guide=1' ),
       
  1569 				'target="_blank"',
       
  1570 				sprintf(
       
  1571 					'<span class="screen-reader-text"> %s</span>',
       
  1572 					/* translators: accessibility text */
       
  1573 					__( '(opens in a new tab)' )
       
  1574 				)
       
  1575 			);
       
  1576 			?>
       
  1577 			</p>
       
  1578 		</div>
       
  1579 		<?php
       
  1580 
       
  1581 	}
       
  1582 
       
  1583 	/**
       
  1584 	 * Output the privacy policy guide together with content from the theme and plugins.
       
  1585 	 *
       
  1586 	 * @since 4.9.6
       
  1587 	 */
       
  1588 	public static function privacy_policy_guide() {
       
  1589 
       
  1590 		$content_array = self::get_suggested_policy_text();
       
  1591 
       
  1592 		$content = '';
       
  1593 		$toc = array( '<li><a href="#wp-privacy-policy-guide-introduction">' . __( 'Introduction' ) . '</a></li>' );
       
  1594 		$date_format = __( 'F j, Y' );
       
  1595 		$copy = __( 'Copy' );
       
  1596 		$return_to_top = '<a href="#" class="return-to-top">' . __( '&uarr; Return to Top' ) . '</a>';
       
  1597 
       
  1598 		foreach ( $content_array as $section ) {
       
  1599 			$class = $meta = $removed = '';
       
  1600 
       
  1601 			if ( ! empty( $section['removed'] ) ) {
       
  1602 				$class = ' text-removed';
       
  1603 				$date = date_i18n( $date_format, $section['removed'] );
       
  1604 				$meta  = sprintf( __( 'Removed %s.' ), $date );
       
  1605 
       
  1606 				$removed = __( 'You deactivated this plugin on %s and may no longer need this policy.' );
       
  1607 				$removed = '<div class="error inline"><p>' . sprintf( $removed, $date ) . '</p></div>';
       
  1608 			} elseif ( ! empty( $section['updated'] ) ) {
       
  1609 				$class = ' text-updated';
       
  1610 				$date = date_i18n( $date_format, $section['updated'] );
       
  1611 				$meta  = sprintf( __( 'Updated %s.' ), $date );
       
  1612 			}
       
  1613 
       
  1614 			if ( $meta ) {
       
  1615 				$meta = '<br><span class="privacy-text-meta">' . $meta . '</span>';
       
  1616 			}
       
  1617 
       
  1618 			$plugin_name = esc_html( $section['plugin_name'] );
       
  1619 			$toc_id = 'wp-privacy-policy-guide-' . sanitize_title( $plugin_name );
       
  1620 			$toc[] = sprintf( '<li><a href="#%1$s">%2$s</a>' . $meta . '</li>', $toc_id, $plugin_name );
       
  1621 
       
  1622 			$content .= '<div class="privacy-text-section' . $class . '">';
       
  1623 			$content .= '<a id="' . $toc_id . '">&nbsp;</a>';
       
  1624 			/* translators: %s: plugin name */
       
  1625 			$content .= '<h2>' . sprintf( __( 'Source: %s' ), $plugin_name ) . '</h2>';
       
  1626 			$content .= $removed;
       
  1627 
       
  1628 			$content .= '<div class="policy-text">' . $section['policy_text'] . '</div>';
       
  1629 			$content .= $return_to_top;
       
  1630 
       
  1631 			if ( empty( $section['removed'] ) ) {
       
  1632 				$content .= '<div class="privacy-text-actions">';
       
  1633 					$content .= '<button type="button" class="privacy-text-copy button">';
       
  1634 						$content .= $copy;
       
  1635 						$content .= '<span class="screen-reader-text">' . sprintf( __( 'Copy suggested policy text from %s.' ), $plugin_name ) . '</span>';
       
  1636 					$content .= '</button>';
       
  1637 				$content .= '</div>';
       
  1638 			}
       
  1639 
       
  1640 			$content .= "</div>\n"; // End of .privacy-text-section.
       
  1641 		}
       
  1642 
       
  1643 		if ( count( $toc ) > 2 ) {
       
  1644 			?>
       
  1645 			<div  class="privacy-text-box-toc">
       
  1646 				<p><?php _e( 'Table of Contents' ); ?></p>
       
  1647 				<ol>
       
  1648 					<?php echo implode( "\n", $toc ); ?>
       
  1649 				</ol>
       
  1650 			</div>
       
  1651 			<?php
       
  1652 		}
       
  1653 
       
  1654 		?>
       
  1655 		<div class="privacy-text-box">
       
  1656 			<div class="privacy-text-box-head">
       
  1657 				<a id="wp-privacy-policy-guide-introduction">&nbsp;</a>
       
  1658 				<h2><?php _e( 'Introduction' ); ?></h2>
       
  1659 				<p><?php _e( 'Hello,' ); ?></p>
       
  1660 				<p><?php _e( 'This text template will help you to create your web site&#8217;s privacy policy.' ); ?></p>
       
  1661 				<p><?php _e( 'We have suggested the sections you will need. Under each section heading you will find a short summary of what information you should provide, which will help you to get started. Some sections include suggested policy content, others will have to be completed with information from your theme and plugins.' ); ?></p>
       
  1662 				<p><?php _e( 'Please edit your privacy policy content, making sure to delete the summaries, and adding any information from your theme and plugins. Once you publish your policy page, remember to add it to your navigation menu.' ); ?></p>
       
  1663 				<p><?php _e( 'It is your responsibility to write a comprehensive privacy policy, to make sure it reflects all national and international legal requirements on privacy, and to keep your policy current and accurate.' ); ?></p>
       
  1664 			</div>
       
  1665 
       
  1666 			<div class="privacy-text-box-body">
       
  1667 				<?php echo $content; ?>
       
  1668 			</div>
       
  1669 		</div>
       
  1670 		<?php
       
  1671 	}
       
  1672 
       
  1673 	/**
       
  1674 	 * Return the default suggested privacy policy content.
       
  1675 	 *
       
  1676 	 * @since 4.9.6
       
  1677 	 *
       
  1678 	 * @param bool $descr Whether to include the descriptions under the section headings. Default false.
       
  1679 	 * @return string The default policy content.
       
  1680 	 */
       
  1681 	public static function get_default_content( $descr = false ) {
       
  1682 		$suggested_text = $descr ? '<strong class="privacy-policy-tutorial">' . __( 'Suggested text:' ) . ' </strong>' : '';
       
  1683 		$content = '';
       
  1684 
       
  1685 		// Start of the suggested privacy policy text.
       
  1686 		$descr && $content .=
       
  1687 			'<div class="wp-suggested-text">';
       
  1688 		$content .=
       
  1689 			'<h2>' . __( 'Who we are' ) . '</h2>';
       
  1690 		$descr && $content .=
       
  1691 			'<p class="privacy-policy-tutorial">' . __( 'In this section you should note your site URL, as well as the name of the company, organization, or individual behind it, and some accurate contact information.' ) . '</p>' .
       
  1692 			'<p class="privacy-policy-tutorial">' . __( 'The amount of information you may be required to show will vary depending on your local or national business regulations. You may, for example, be required to display a physical address, a registered address, or your company registration number.' ) . '</p>';
       
  1693 		$content .=
       
  1694 			/* translators: %s Site URL */
       
  1695 			'<p>' . $suggested_text . sprintf( __( 'Our website address is: %s.' ), get_bloginfo( 'url', 'display' ) ) . '</p>' .
       
  1696 
       
  1697 			'<h2>' . __( 'What personal data we collect and why we collect it' ) . '</h2>';
       
  1698 		$descr && $content .=
       
  1699 			'<p class="privacy-policy-tutorial">' . __( 'In this section you should note what personal data you collect from users and site visitors. This may include personal data, such as name, email address, personal account preferences; transactional data, such as purchase information; and technical data, such as information about cookies.' ) . '</p>' .
       
  1700 			'<p class="privacy-policy-tutorial">' . __( 'You should also note any collection and retention of sensitive personal data, such as data concerning health.' ) . '</p>' .
       
  1701 			'<p class="privacy-policy-tutorial">' . __( 'In addition to listing what personal data you collect, you need to note why you collect it. These explanations must note either the legal basis for your data collection and retention or the active consent the user has given.' ) . '</p>' .
       
  1702 			'<p class="privacy-policy-tutorial">' . __( 'Personal data is not just created by a user&#8217;s interactions with your site. Personal data is also generated from technical processes such as contact forms, comments, cookies, analytics, and third party embeds.' ) . '</p>' .
       
  1703 			'<p class="privacy-policy-tutorial">' . __( 'By default WordPress does not collect any personal data about visitors, and only collects the data shown on the User Profile screen from registered users. However some of your plugins may collect personal data. You should add the relevant information below.' ) . '</p>';
       
  1704 
       
  1705 		$content .=
       
  1706 			'<h3>' . __( 'Comments' ) . '</h3>';
       
  1707 		$descr && $content .=
       
  1708 			'<p class="privacy-policy-tutorial">' . __( 'In this subsection you should note what information is captured through comments. We have noted the data which WordPress collects by default.' ) . '</p>';
       
  1709 		$content .=
       
  1710 			'<p>' . $suggested_text . __( 'When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor&#8217;s IP address and browser user agent string to help spam detection.' ) . '</p>' .
       
  1711 			'<p>' . __( 'An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your comment, your profile picture is visible to the public in the context of your comment.' ) . '</p>' .
       
  1712 
       
  1713 			'<h3>' . __( 'Media' ) . '</h3>';
       
  1714 		$descr && $content .=
       
  1715 			'<p class="privacy-policy-tutorial">' . __( 'In this subsection you should note what information may be disclosed by users who can upload media files. All uploaded files are usually publicly accessible.' ) . '</p>';
       
  1716 		$content .=
       
  1717 			'<p>' . $suggested_text . __( 'If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.' ) . '</p>' .
       
  1718 
       
  1719 			'<h3>' . __( 'Contact forms' ) . '</h3>';
       
  1720 		$descr && $content .=
       
  1721 			'<p class="privacy-policy-tutorial">' . __( 'By default, WordPress does not include a contact form. If you use a contact form plugin, use this subsection to note what personal data is captured when someone submits a contact form, and how long you keep it. For example, you may note that you keep contact form submissions for a certain period for customer service purposes, but you do not use the information submitted through them for marketing purposes.' ) . '</p>';
       
  1722 
       
  1723 		$content .=
       
  1724 			'<h3>' . __( 'Cookies' ) . '</h3>';
       
  1725 		$descr && $content .=
       
  1726 			'<p class="privacy-policy-tutorial">' . __( 'In this subsection you should list the cookies your web site uses, including those set by your plugins, social media, and analytics. We have provided the cookies which WordPress installs by default.' ) . '</p>';
       
  1727 		$content .=
       
  1728 			'<p>' . $suggested_text . __( 'If you leave a comment on our site you may opt-in to saving your name, email address and website in cookies. These are for your convenience so that you do not have to fill in your details again when you leave another comment. These cookies will last for one year.' ) . '</p>' .
       
  1729 			'<p>' . __( 'If you have an account and you log in to this site, we will set a temporary cookie to determine if your browser accepts cookies. This cookie contains no personal data and is discarded when you close your browser.' ) . '</p>' .
       
  1730 			'<p>' . __( 'When you log in, we will also set up several cookies to save your login information and your screen display choices. Login cookies last for two days, and screen options cookies last for a year. If you select &quot;Remember Me&quot;, your login will persist for two weeks. If you log out of your account, the login cookies will be removed.' ) . '</p>' .
       
  1731 			'<p>' . __( 'If you edit or publish an article, an additional cookie will be saved in your browser. This cookie includes no personal data and simply indicates the post ID of the article you just edited. It expires after 1 day.' ) . '</p>' .
       
  1732 
       
  1733 			'<h3>' . __( 'Embedded content from other websites' ) . '</h3>' .
       
  1734 			'<p>' . $suggested_text . __( 'Articles on this site may include embedded content (e.g. videos, images, articles, etc.). Embedded content from other websites behaves in the exact same way as if the visitor has visited the other website.' ) . '</p>' .
       
  1735 			'<p>' . __( 'These websites may collect data about you, use cookies, embed additional third-party tracking, and monitor your interaction with that embedded content, including tracking your interaction with the embedded content if you have an account and are logged in to that website.' ) . '</p>' .
       
  1736 
       
  1737 			'<h3>' . __( 'Analytics' ) . '</h3>';
       
  1738 		$descr && $content .=
       
  1739 			'<p class="privacy-policy-tutorial">' . __( 'In this subsection you should note what analytics package you use, how users can opt out of analytics tracking, and a link to your analytics provider&#8217;s privacy policy, if any.' ) . '</p>' .
       
  1740 			'<p class="privacy-policy-tutorial">' . __( 'By default WordPress does not collect any analytics data. However, many web hosting accounts collect some anonymous analytics data. You may also have installed a WordPress plugin that provides analytics services. In that case, add information from that plugin here.' ) . '</p>';
       
  1741 
       
  1742 		$content .=
       
  1743 			'<h2>' . __( 'Who we share your data with' ) . '</h2>';
       
  1744 		$descr && $content .=
       
  1745 			'<p class="privacy-policy-tutorial">' . __( 'In this section you should name and list all third party providers with whom you share site data, including partners, cloud-based services, payment processors, and third party service providers, and note what data you share with them and why. Link to their own privacy policies if possible.' ) . '</p>' .
       
  1746 			'<p class="privacy-policy-tutorial">' . __( 'By default WordPress does not share any personal data with anyone.' ) . '</p>';
       
  1747 
       
  1748 		$content .=
       
  1749 			'<h2>' . __( 'How long we retain your data' ) . '</h2>';
       
  1750 		$descr && $content .=
       
  1751 			'<p class="privacy-policy-tutorial">' . __( 'In this section you should explain how long you retain personal data collected or processed by the web site. While it is your responsibility to come up with the schedule of how long you keep each dataset for and why you keep it, that information does need to be listed here. For example, you may want to say that you keep contact form entries for six months, analytics records for a year, and customer purchase records for ten years.' ) . '</p>';
       
  1752 		$content .=
       
  1753 			'<p>' . $suggested_text . __( 'If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognize and approve any follow-up comments automatically instead of holding them in a moderation queue.' ) . '</p>' .
       
  1754 			'<p>' . __( 'For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.' ) . '</p>' .
       
  1755 
       
  1756 			'<h2>' . __( 'What rights you have over your data' ) . '</h2>';
       
  1757 		$descr && $content .=
       
  1758 			'<p class="privacy-policy-tutorial">' . __( 'In this section you should explain what rights your users have over their data and how they can invoke those rights.' ) . '</p>';
       
  1759 		$content .=
       
  1760 			'<p>' . $suggested_text . __( 'If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.' ) . '</p>' .
       
  1761 
       
  1762 			'<h2>' . __( 'Where we send your data' ) . '</h2>';
       
  1763 		$descr && $content .=
       
  1764 			'<p class="privacy-policy-tutorial">' . __( 'In this section you should list all transfers of your site data outside the European Union and describe the means by which that data is safeguarded to European data protection standards. This could include your web hosting, cloud storage, or other third party services.' ) . '</p>' .
       
  1765 			'<p class="privacy-policy-tutorial">' . __( 'European data protection law requires data about European residents which is transferred outside the European Union to be safeguarded to the same standards as if the data was in Europe. So in addition to listing where data goes, you should describe how you ensure that these standards are met either by yourself or by your third party providers, whether that is through an agreement such as Privacy Shield, model clauses in your contracts, or binding corporate rules.' ) . '</p>';
       
  1766 		$content .=
       
  1767 			'<p>' . $suggested_text . __( 'Visitor comments may be checked through an automated spam detection service.' ) . '</p>' .
       
  1768 
       
  1769 			'<h2>' . __( 'Your contact information' ) . '</h2>';
       
  1770 		$descr && $content .=
       
  1771 			'<p class="privacy-policy-tutorial">' . __( 'In this section you should provide a contact method for privacy-specific concerns. If you are required to have a Data Protection Officer, list their name and full contact details here as well.' ) . '</p>';
       
  1772 
       
  1773 		$content .=
       
  1774 			'<h2>' . __( 'Additional information' ) . '</h2>';
       
  1775 		$descr && $content .=
       
  1776 			'<p class="privacy-policy-tutorial">' . __( 'If you use your site for commercial purposes and you engage in more complex collection or processing of personal data, you should note the following information in your privacy policy in addition to the information we have already discussed.' ) . '</p>';
       
  1777 
       
  1778 		$content .=
       
  1779 			'<h3>' . __( 'How we protect your data' ) . '</h3>';
       
  1780 		$descr && $content .=
       
  1781 			'<p class="privacy-policy-tutorial">' . __( 'In this section you should explain what measures you have taken to protect your users&#8217; data. This could include technical measures such as encryption; security measures such as two factor authentication; and measures such as staff training in data protection. If you have carried out a Privacy Impact Assessment, you can mention it here too.' ) . '</p>';
       
  1782 
       
  1783 		$content .=
       
  1784 			'<h3>' . __( 'What data breach procedures we have in place' ) . '</h3>';
       
  1785 		$descr && $content .=
       
  1786 			'<p class="privacy-policy-tutorial">' . __( 'In this section you should explain what procedures you have in place to deal with data breaches, either potential or real, such as internal reporting systems, contact mechanisms, or bug bounties.' ) . '</p>';
       
  1787 
       
  1788 		$content .=
       
  1789 			'<h3>' . __( 'What third parties we receive data from' ) . '</h3>';
       
  1790 		$descr && $content .=
       
  1791 			'<p class="privacy-policy-tutorial">' . __( 'If your web site receives data about users from third parties, including advertisers, this information must be included within the section of your privacy policy dealing with third party data.' ) . '</p>';
       
  1792 
       
  1793 		$content .=
       
  1794 			'<h3>' . __( 'What automated decision making and/or profiling we do with user data' ) . '</h3>';
       
  1795 		$descr && $content .=
       
  1796 			'<p class="privacy-policy-tutorial">' . __( 'If your web site provides a service which includes automated decision making - for example, allowing customers to apply for credit, or aggregating their data into an advertising profile - you must note that this is taking place, and include information about how that information is used, what decisions are made with that aggregated data, and what rights users have over decisions made without human intervention.' ) . '</p>';
       
  1797 
       
  1798 		$content .=
       
  1799 			'<h3>' . __( 'Industry regulatory disclosure requirements' ) . '</h3>';
       
  1800 		$descr && $content .=
       
  1801 			'<p class="privacy-policy-tutorial">' . __( 'If you are a member of a regulated industry, or if you are subject to additional privacy laws, you may be required to disclose that information here.' ) . '</p>' .
       
  1802 			'</div>';
       
  1803 		// End of the suggested privacy policy text.
       
  1804 
       
  1805 		/**
       
  1806 		 * Filters the default content suggested for inclusion in a privacy policy.
       
  1807 		 *
       
  1808 		 * @since 4.9.6
       
  1809 		 *
       
  1810 		 * @param $content string The default policy content.
       
  1811 		 */
       
  1812 		return apply_filters( 'wp_get_default_privacy_policy_content', $content );
       
  1813 	}
       
  1814 
       
  1815 	/**
       
  1816 	 * Add the suggested privacy policy text to the policy postbox.
       
  1817 	 *
       
  1818 	 * @since 4.9.6
       
  1819 	 */
       
  1820 	public static function add_suggested_content() {
       
  1821 		$content = self::get_default_content( true );
       
  1822 		wp_add_privacy_policy_content( __( 'WordPress' ), $content );
       
  1823 	}
       
  1824 }