angelovcom.net

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

file.php (86327B)


      1 <?php
      2 /**
      3  * Filesystem API: Top-level functionality
      4  *
      5  * Functions for reading, writing, modifying, and deleting files on the file system.
      6  * Includes functionality for theme-specific files as well as operations for uploading,
      7  * archiving, and rendering output when necessary.
      8  *
      9  * @package WordPress
     10  * @subpackage Filesystem
     11  * @since 2.3.0
     12  */
     13 
     14 /** The descriptions for theme files. */
     15 $wp_file_descriptions = array(
     16 	'functions.php'         => __( 'Theme Functions' ),
     17 	'header.php'            => __( 'Theme Header' ),
     18 	'footer.php'            => __( 'Theme Footer' ),
     19 	'sidebar.php'           => __( 'Sidebar' ),
     20 	'comments.php'          => __( 'Comments' ),
     21 	'searchform.php'        => __( 'Search Form' ),
     22 	'404.php'               => __( '404 Template' ),
     23 	'link.php'              => __( 'Links Template' ),
     24 	// Archives.
     25 	'index.php'             => __( 'Main Index Template' ),
     26 	'archive.php'           => __( 'Archives' ),
     27 	'author.php'            => __( 'Author Template' ),
     28 	'taxonomy.php'          => __( 'Taxonomy Template' ),
     29 	'category.php'          => __( 'Category Template' ),
     30 	'tag.php'               => __( 'Tag Template' ),
     31 	'home.php'              => __( 'Posts Page' ),
     32 	'search.php'            => __( 'Search Results' ),
     33 	'date.php'              => __( 'Date Template' ),
     34 	// Content.
     35 	'singular.php'          => __( 'Singular Template' ),
     36 	'single.php'            => __( 'Single Post' ),
     37 	'page.php'              => __( 'Single Page' ),
     38 	'front-page.php'        => __( 'Homepage' ),
     39 	'privacy-policy.php'    => __( 'Privacy Policy Page' ),
     40 	// Attachments.
     41 	'attachment.php'        => __( 'Attachment Template' ),
     42 	'image.php'             => __( 'Image Attachment Template' ),
     43 	'video.php'             => __( 'Video Attachment Template' ),
     44 	'audio.php'             => __( 'Audio Attachment Template' ),
     45 	'application.php'       => __( 'Application Attachment Template' ),
     46 	// Embeds.
     47 	'embed.php'             => __( 'Embed Template' ),
     48 	'embed-404.php'         => __( 'Embed 404 Template' ),
     49 	'embed-content.php'     => __( 'Embed Content Template' ),
     50 	'header-embed.php'      => __( 'Embed Header Template' ),
     51 	'footer-embed.php'      => __( 'Embed Footer Template' ),
     52 	// Stylesheets.
     53 	'style.css'             => __( 'Stylesheet' ),
     54 	'editor-style.css'      => __( 'Visual Editor Stylesheet' ),
     55 	'editor-style-rtl.css'  => __( 'Visual Editor RTL Stylesheet' ),
     56 	'rtl.css'               => __( 'RTL Stylesheet' ),
     57 	// Other.
     58 	'my-hacks.php'          => __( 'my-hacks.php (legacy hacks support)' ),
     59 	'.htaccess'             => __( '.htaccess (for rewrite rules )' ),
     60 	// Deprecated files.
     61 	'wp-layout.css'         => __( 'Stylesheet' ),
     62 	'wp-comments.php'       => __( 'Comments Template' ),
     63 	'wp-comments-popup.php' => __( 'Popup Comments Template' ),
     64 	'comments-popup.php'    => __( 'Popup Comments' ),
     65 );
     66 
     67 /**
     68  * Gets the description for standard WordPress theme files.
     69  *
     70  * @since 1.5.0
     71  *
     72  * @global array $wp_file_descriptions Theme file descriptions.
     73  * @global array $allowed_files        List of allowed files.
     74  *
     75  * @param string $file Filesystem path or filename.
     76  * @return string Description of file from $wp_file_descriptions or basename of $file if description doesn't exist.
     77  *                Appends 'Page Template' to basename of $file if the file is a page template.
     78  */
     79 function get_file_description( $file ) {
     80 	global $wp_file_descriptions, $allowed_files;
     81 
     82 	$dirname   = pathinfo( $file, PATHINFO_DIRNAME );
     83 	$file_path = $allowed_files[ $file ];
     84 
     85 	if ( isset( $wp_file_descriptions[ basename( $file ) ] ) && '.' === $dirname ) {
     86 		return $wp_file_descriptions[ basename( $file ) ];
     87 	} elseif ( file_exists( $file_path ) && is_file( $file_path ) ) {
     88 		$template_data = implode( '', file( $file_path ) );
     89 
     90 		if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) ) {
     91 			/* translators: %s: Template name. */
     92 			return sprintf( __( '%s Page Template' ), _cleanup_header_comment( $name[1] ) );
     93 		}
     94 	}
     95 
     96 	return trim( basename( $file ) );
     97 }
     98 
     99 /**
    100  * Gets the absolute filesystem path to the root of the WordPress installation.
    101  *
    102  * @since 1.5.0
    103  *
    104  * @return string Full filesystem path to the root of the WordPress installation.
    105  */
    106 function get_home_path() {
    107 	$home    = set_url_scheme( get_option( 'home' ), 'http' );
    108 	$siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' );
    109 
    110 	if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) {
    111 		$wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */
    112 		$pos                 = strripos( str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) );
    113 		$home_path           = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos );
    114 		$home_path           = trailingslashit( $home_path );
    115 	} else {
    116 		$home_path = ABSPATH;
    117 	}
    118 
    119 	return str_replace( '\\', '/', $home_path );
    120 }
    121 
    122 /**
    123  * Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep.
    124  *
    125  * The depth of the recursiveness can be controlled by the $levels param.
    126  *
    127  * @since 2.6.0
    128  * @since 4.9.0 Added the `$exclusions` parameter.
    129  *
    130  * @param string   $folder     Optional. Full path to folder. Default empty.
    131  * @param int      $levels     Optional. Levels of folders to follow, Default 100 (PHP Loop limit).
    132  * @param string[] $exclusions Optional. List of folders and files to skip.
    133  * @return string[]|false Array of files on success, false on failure.
    134  */
    135 function list_files( $folder = '', $levels = 100, $exclusions = array() ) {
    136 	if ( empty( $folder ) ) {
    137 		return false;
    138 	}
    139 
    140 	$folder = trailingslashit( $folder );
    141 
    142 	if ( ! $levels ) {
    143 		return false;
    144 	}
    145 
    146 	$files = array();
    147 
    148 	$dir = @opendir( $folder );
    149 
    150 	if ( $dir ) {
    151 		while ( ( $file = readdir( $dir ) ) !== false ) {
    152 			// Skip current and parent folder links.
    153 			if ( in_array( $file, array( '.', '..' ), true ) ) {
    154 				continue;
    155 			}
    156 
    157 			// Skip hidden and excluded files.
    158 			if ( '.' === $file[0] || in_array( $file, $exclusions, true ) ) {
    159 				continue;
    160 			}
    161 
    162 			if ( is_dir( $folder . $file ) ) {
    163 				$files2 = list_files( $folder . $file, $levels - 1 );
    164 				if ( $files2 ) {
    165 					$files = array_merge( $files, $files2 );
    166 				} else {
    167 					$files[] = $folder . $file . '/';
    168 				}
    169 			} else {
    170 				$files[] = $folder . $file;
    171 			}
    172 		}
    173 
    174 		closedir( $dir );
    175 	}
    176 
    177 	return $files;
    178 }
    179 
    180 /**
    181  * Gets the list of file extensions that are editable in plugins.
    182  *
    183  * @since 4.9.0
    184  *
    185  * @param string $plugin Path to the plugin file relative to the plugins directory.
    186  * @return string[] Array of editable file extensions.
    187  */
    188 function wp_get_plugin_file_editable_extensions( $plugin ) {
    189 
    190 	$default_types = array(
    191 		'bash',
    192 		'conf',
    193 		'css',
    194 		'diff',
    195 		'htm',
    196 		'html',
    197 		'http',
    198 		'inc',
    199 		'include',
    200 		'js',
    201 		'json',
    202 		'jsx',
    203 		'less',
    204 		'md',
    205 		'patch',
    206 		'php',
    207 		'php3',
    208 		'php4',
    209 		'php5',
    210 		'php7',
    211 		'phps',
    212 		'phtml',
    213 		'sass',
    214 		'scss',
    215 		'sh',
    216 		'sql',
    217 		'svg',
    218 		'text',
    219 		'txt',
    220 		'xml',
    221 		'yaml',
    222 		'yml',
    223 	);
    224 
    225 	/**
    226 	 * Filters the list of file types allowed for editing in the plugin editor.
    227 	 *
    228 	 * @since 2.8.0
    229 	 * @since 4.9.0 Added the `$plugin` parameter.
    230 	 *
    231 	 * @param string[] $default_types An array of editable plugin file extensions.
    232 	 * @param string   $plugin        Path to the plugin file relative to the plugins directory.
    233 	 */
    234 	$file_types = (array) apply_filters( 'editable_extensions', $default_types, $plugin );
    235 
    236 	return $file_types;
    237 }
    238 
    239 /**
    240  * Gets the list of file extensions that are editable for a given theme.
    241  *
    242  * @since 4.9.0
    243  *
    244  * @param WP_Theme $theme Theme object.
    245  * @return string[] Array of editable file extensions.
    246  */
    247 function wp_get_theme_file_editable_extensions( $theme ) {
    248 
    249 	$default_types = array(
    250 		'bash',
    251 		'conf',
    252 		'css',
    253 		'diff',
    254 		'htm',
    255 		'html',
    256 		'http',
    257 		'inc',
    258 		'include',
    259 		'js',
    260 		'json',
    261 		'jsx',
    262 		'less',
    263 		'md',
    264 		'patch',
    265 		'php',
    266 		'php3',
    267 		'php4',
    268 		'php5',
    269 		'php7',
    270 		'phps',
    271 		'phtml',
    272 		'sass',
    273 		'scss',
    274 		'sh',
    275 		'sql',
    276 		'svg',
    277 		'text',
    278 		'txt',
    279 		'xml',
    280 		'yaml',
    281 		'yml',
    282 	);
    283 
    284 	/**
    285 	 * Filters the list of file types allowed for editing in the theme editor.
    286 	 *
    287 	 * @since 4.4.0
    288 	 *
    289 	 * @param string[] $default_types An array of editable theme file extensions.
    290 	 * @param WP_Theme $theme         The current theme object.
    291 	 */
    292 	$file_types = apply_filters( 'wp_theme_editor_filetypes', $default_types, $theme );
    293 
    294 	// Ensure that default types are still there.
    295 	return array_unique( array_merge( $file_types, $default_types ) );
    296 }
    297 
    298 /**
    299  * Prints file editor templates (for plugins and themes).
    300  *
    301  * @since 4.9.0
    302  */
    303 function wp_print_file_editor_templates() {
    304 	?>
    305 	<script type="text/html" id="tmpl-wp-file-editor-notice">
    306 		<div class="notice inline notice-{{ data.type || 'info' }} {{ data.alt ? 'notice-alt' : '' }} {{ data.dismissible ? 'is-dismissible' : '' }} {{ data.classes || '' }}">
    307 			<# if ( 'php_error' === data.code ) { #>
    308 				<p>
    309 					<?php
    310 					printf(
    311 						/* translators: 1: Line number, 2: File path. */
    312 						__( 'Your PHP code changes were rolled back due to an error on line %1$s of file %2$s. Please fix and try saving again.' ),
    313 						'{{ data.line }}',
    314 						'{{ data.file }}'
    315 					);
    316 					?>
    317 				</p>
    318 				<pre>{{ data.message }}</pre>
    319 			<# } else if ( 'file_not_writable' === data.code ) { #>
    320 				<p>
    321 					<?php
    322 					printf(
    323 						/* translators: %s: Documentation URL. */
    324 						__( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
    325 						__( 'https://wordpress.org/support/article/changing-file-permissions/' )
    326 					);
    327 					?>
    328 				</p>
    329 			<# } else { #>
    330 				<p>{{ data.message || data.code }}</p>
    331 
    332 				<# if ( 'lint_errors' === data.code ) { #>
    333 					<p>
    334 						<# var elementId = 'el-' + String( Math.random() ); #>
    335 						<input id="{{ elementId }}"  type="checkbox">
    336 						<label for="{{ elementId }}"><?php _e( 'Update anyway, even though it might break your site?' ); ?></label>
    337 					</p>
    338 				<# } #>
    339 			<# } #>
    340 			<# if ( data.dismissible ) { #>
    341 				<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php _e( 'Dismiss' ); ?></span></button>
    342 			<# } #>
    343 		</div>
    344 	</script>
    345 	<?php
    346 }
    347 
    348 /**
    349  * Attempts to edit a file for a theme or plugin.
    350  *
    351  * When editing a PHP file, loopback requests will be made to the admin and the homepage
    352  * to attempt to see if there is a fatal error introduced. If so, the PHP change will be
    353  * reverted.
    354  *
    355  * @since 4.9.0
    356  *
    357  * @param string[] $args {
    358  *     Args. Note that all of the arg values are already unslashed. They are, however,
    359  *     coming straight from `$_POST` and are not validated or sanitized in any way.
    360  *
    361  *     @type string $file       Relative path to file.
    362  *     @type string $plugin     Path to the plugin file relative to the plugins directory.
    363  *     @type string $theme      Theme being edited.
    364  *     @type string $newcontent New content for the file.
    365  *     @type string $nonce      Nonce.
    366  * }
    367  * @return true|WP_Error True on success or `WP_Error` on failure.
    368  */
    369 function wp_edit_theme_plugin_file( $args ) {
    370 	if ( empty( $args['file'] ) ) {
    371 		return new WP_Error( 'missing_file' );
    372 	}
    373 
    374 	if ( 0 !== validate_file( $args['file'] ) ) {
    375 		return new WP_Error( 'bad_file' );
    376 	}
    377 
    378 	if ( ! isset( $args['newcontent'] ) ) {
    379 		return new WP_Error( 'missing_content' );
    380 	}
    381 
    382 	if ( ! isset( $args['nonce'] ) ) {
    383 		return new WP_Error( 'missing_nonce' );
    384 	}
    385 
    386 	$file    = $args['file'];
    387 	$content = $args['newcontent'];
    388 
    389 	$plugin    = null;
    390 	$theme     = null;
    391 	$real_file = null;
    392 
    393 	if ( ! empty( $args['plugin'] ) ) {
    394 		$plugin = $args['plugin'];
    395 
    396 		if ( ! current_user_can( 'edit_plugins' ) ) {
    397 			return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit plugins for this site.' ) );
    398 		}
    399 
    400 		if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) {
    401 			return new WP_Error( 'nonce_failure' );
    402 		}
    403 
    404 		if ( ! array_key_exists( $plugin, get_plugins() ) ) {
    405 			return new WP_Error( 'invalid_plugin' );
    406 		}
    407 
    408 		if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) {
    409 			return new WP_Error( 'bad_plugin_file_path', __( 'Sorry, that file cannot be edited.' ) );
    410 		}
    411 
    412 		$editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
    413 
    414 		$real_file = WP_PLUGIN_DIR . '/' . $file;
    415 
    416 		$is_active = in_array(
    417 			$plugin,
    418 			(array) get_option( 'active_plugins', array() ),
    419 			true
    420 		);
    421 
    422 	} elseif ( ! empty( $args['theme'] ) ) {
    423 		$stylesheet = $args['theme'];
    424 
    425 		if ( 0 !== validate_file( $stylesheet ) ) {
    426 			return new WP_Error( 'bad_theme_path' );
    427 		}
    428 
    429 		if ( ! current_user_can( 'edit_themes' ) ) {
    430 			return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit templates for this site.' ) );
    431 		}
    432 
    433 		$theme = wp_get_theme( $stylesheet );
    434 		if ( ! $theme->exists() ) {
    435 			return new WP_Error( 'non_existent_theme', __( 'The requested theme does not exist.' ) );
    436 		}
    437 
    438 		if ( ! wp_verify_nonce( $args['nonce'], 'edit-theme_' . $stylesheet . '_' . $file ) ) {
    439 			return new WP_Error( 'nonce_failure' );
    440 		}
    441 
    442 		if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) {
    443 			return new WP_Error(
    444 				'theme_no_stylesheet',
    445 				__( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message()
    446 			);
    447 		}
    448 
    449 		$editable_extensions = wp_get_theme_file_editable_extensions( $theme );
    450 
    451 		$allowed_files = array();
    452 		foreach ( $editable_extensions as $type ) {
    453 			switch ( $type ) {
    454 				case 'php':
    455 					$allowed_files = array_merge( $allowed_files, $theme->get_files( 'php', -1 ) );
    456 					break;
    457 				case 'css':
    458 					$style_files                = $theme->get_files( 'css', -1 );
    459 					$allowed_files['style.css'] = $style_files['style.css'];
    460 					$allowed_files              = array_merge( $allowed_files, $style_files );
    461 					break;
    462 				default:
    463 					$allowed_files = array_merge( $allowed_files, $theme->get_files( $type, -1 ) );
    464 					break;
    465 			}
    466 		}
    467 
    468 		// Compare based on relative paths.
    469 		if ( 0 !== validate_file( $file, array_keys( $allowed_files ) ) ) {
    470 			return new WP_Error( 'disallowed_theme_file', __( 'Sorry, that file cannot be edited.' ) );
    471 		}
    472 
    473 		$real_file = $theme->get_stylesheet_directory() . '/' . $file;
    474 
    475 		$is_active = ( get_stylesheet() === $stylesheet || get_template() === $stylesheet );
    476 
    477 	} else {
    478 		return new WP_Error( 'missing_theme_or_plugin' );
    479 	}
    480 
    481 	// Ensure file is real.
    482 	if ( ! is_file( $real_file ) ) {
    483 		return new WP_Error( 'file_does_not_exist', __( 'File does not exist! Please double check the name and try again.' ) );
    484 	}
    485 
    486 	// Ensure file extension is allowed.
    487 	$extension = null;
    488 	if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) {
    489 		$extension = strtolower( $matches[1] );
    490 		if ( ! in_array( $extension, $editable_extensions, true ) ) {
    491 			return new WP_Error( 'illegal_file_type', __( 'Files of this type are not editable.' ) );
    492 		}
    493 	}
    494 
    495 	$previous_content = file_get_contents( $real_file );
    496 
    497 	if ( ! is_writable( $real_file ) ) {
    498 		return new WP_Error( 'file_not_writable' );
    499 	}
    500 
    501 	$f = fopen( $real_file, 'w+' );
    502 
    503 	if ( false === $f ) {
    504 		return new WP_Error( 'file_not_writable' );
    505 	}
    506 
    507 	$written = fwrite( $f, $content );
    508 	fclose( $f );
    509 
    510 	if ( false === $written ) {
    511 		return new WP_Error( 'unable_to_write', __( 'Unable to write to file.' ) );
    512 	}
    513 
    514 	wp_opcache_invalidate( $real_file, true );
    515 
    516 	if ( $is_active && 'php' === $extension ) {
    517 
    518 		$scrape_key   = md5( rand() );
    519 		$transient    = 'scrape_key_' . $scrape_key;
    520 		$scrape_nonce = (string) rand();
    521 		// It shouldn't take more than 60 seconds to make the two loopback requests.
    522 		set_transient( $transient, $scrape_nonce, 60 );
    523 
    524 		$cookies       = wp_unslash( $_COOKIE );
    525 		$scrape_params = array(
    526 			'wp_scrape_key'   => $scrape_key,
    527 			'wp_scrape_nonce' => $scrape_nonce,
    528 		);
    529 		$headers       = array(
    530 			'Cache-Control' => 'no-cache',
    531 		);
    532 
    533 		/** This filter is documented in wp-includes/class-wp-http-streams.php */
    534 		$sslverify = apply_filters( 'https_local_ssl_verify', false );
    535 
    536 		// Include Basic auth in loopback requests.
    537 		if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
    538 			$headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
    539 		}
    540 
    541 		// Make sure PHP process doesn't die before loopback requests complete.
    542 		set_time_limit( 300 );
    543 
    544 		// Time to wait for loopback requests to finish.
    545 		$timeout = 100;
    546 
    547 		$needle_start = "###### wp_scraping_result_start:$scrape_key ######";
    548 		$needle_end   = "###### wp_scraping_result_end:$scrape_key ######";
    549 
    550 		// Attempt loopback request to editor to see if user just whitescreened themselves.
    551 		if ( $plugin ) {
    552 			$url = add_query_arg( compact( 'plugin', 'file' ), admin_url( 'plugin-editor.php' ) );
    553 		} elseif ( isset( $stylesheet ) ) {
    554 			$url = add_query_arg(
    555 				array(
    556 					'theme' => $stylesheet,
    557 					'file'  => $file,
    558 				),
    559 				admin_url( 'theme-editor.php' )
    560 			);
    561 		} else {
    562 			$url = admin_url();
    563 		}
    564 
    565 		if ( function_exists( 'session_status' ) && PHP_SESSION_ACTIVE === session_status() ) {
    566 			// Close any active session to prevent HTTP requests from timing out
    567 			// when attempting to connect back to the site.
    568 			session_write_close();
    569 		}
    570 
    571 		$url                    = add_query_arg( $scrape_params, $url );
    572 		$r                      = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
    573 		$body                   = wp_remote_retrieve_body( $r );
    574 		$scrape_result_position = strpos( $body, $needle_start );
    575 
    576 		$loopback_request_failure = array(
    577 			'code'    => 'loopback_request_failed',
    578 			'message' => __( 'Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.' ),
    579 		);
    580 		$json_parse_failure       = array(
    581 			'code' => 'json_parse_error',
    582 		);
    583 
    584 		$result = null;
    585 
    586 		if ( false === $scrape_result_position ) {
    587 			$result = $loopback_request_failure;
    588 		} else {
    589 			$error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
    590 			$error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
    591 			$result       = json_decode( trim( $error_output ), true );
    592 			if ( empty( $result ) ) {
    593 				$result = $json_parse_failure;
    594 			}
    595 		}
    596 
    597 		// Try making request to homepage as well to see if visitors have been whitescreened.
    598 		if ( true === $result ) {
    599 			$url                    = home_url( '/' );
    600 			$url                    = add_query_arg( $scrape_params, $url );
    601 			$r                      = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
    602 			$body                   = wp_remote_retrieve_body( $r );
    603 			$scrape_result_position = strpos( $body, $needle_start );
    604 
    605 			if ( false === $scrape_result_position ) {
    606 				$result = $loopback_request_failure;
    607 			} else {
    608 				$error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
    609 				$error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
    610 				$result       = json_decode( trim( $error_output ), true );
    611 				if ( empty( $result ) ) {
    612 					$result = $json_parse_failure;
    613 				}
    614 			}
    615 		}
    616 
    617 		delete_transient( $transient );
    618 
    619 		if ( true !== $result ) {
    620 			// Roll-back file change.
    621 			file_put_contents( $real_file, $previous_content );
    622 			wp_opcache_invalidate( $real_file, true );
    623 
    624 			if ( ! isset( $result['message'] ) ) {
    625 				$message = __( 'Something went wrong.' );
    626 			} else {
    627 				$message = $result['message'];
    628 				unset( $result['message'] );
    629 			}
    630 
    631 			return new WP_Error( 'php_error', $message, $result );
    632 		}
    633 	}
    634 
    635 	if ( $theme instanceof WP_Theme ) {
    636 		$theme->cache_delete();
    637 	}
    638 
    639 	return true;
    640 }
    641 
    642 
    643 /**
    644  * Returns a filename of a temporary unique file.
    645  *
    646  * Please note that the calling function must unlink() this itself.
    647  *
    648  * The filename is based off the passed parameter or defaults to the current unix timestamp,
    649  * while the directory can either be passed as well, or by leaving it blank, default to a writable
    650  * temporary directory.
    651  *
    652  * @since 2.6.0
    653  *
    654  * @param string $filename Optional. Filename to base the Unique file off. Default empty.
    655  * @param string $dir      Optional. Directory to store the file in. Default empty.
    656  * @return string A writable filename.
    657  */
    658 function wp_tempnam( $filename = '', $dir = '' ) {
    659 	if ( empty( $dir ) ) {
    660 		$dir = get_temp_dir();
    661 	}
    662 
    663 	if ( empty( $filename ) || in_array( $filename, array( '.', '/', '\\' ), true ) ) {
    664 		$filename = uniqid();
    665 	}
    666 
    667 	// Use the basename of the given file without the extension as the name for the temporary directory.
    668 	$temp_filename = basename( $filename );
    669 	$temp_filename = preg_replace( '|\.[^.]*$|', '', $temp_filename );
    670 
    671 	// If the folder is falsey, use its parent directory name instead.
    672 	if ( ! $temp_filename ) {
    673 		return wp_tempnam( dirname( $filename ), $dir );
    674 	}
    675 
    676 	// Suffix some random data to avoid filename conflicts.
    677 	$temp_filename .= '-' . wp_generate_password( 6, false );
    678 	$temp_filename .= '.tmp';
    679 	$temp_filename  = $dir . wp_unique_filename( $dir, $temp_filename );
    680 
    681 	$fp = @fopen( $temp_filename, 'x' );
    682 
    683 	if ( ! $fp && is_writable( $dir ) && file_exists( $temp_filename ) ) {
    684 		return wp_tempnam( $filename, $dir );
    685 	}
    686 
    687 	if ( $fp ) {
    688 		fclose( $fp );
    689 	}
    690 
    691 	return $temp_filename;
    692 }
    693 
    694 /**
    695  * Makes sure that the file that was requested to be edited is allowed to be edited.
    696  *
    697  * Function will die if you are not allowed to edit the file.
    698  *
    699  * @since 1.5.0
    700  *
    701  * @param string   $file          File the user is attempting to edit.
    702  * @param string[] $allowed_files Optional. Array of allowed files to edit.
    703  *                                `$file` must match an entry exactly.
    704  * @return string|void Returns the file name on success, dies on failure.
    705  */
    706 function validate_file_to_edit( $file, $allowed_files = array() ) {
    707 	$code = validate_file( $file, $allowed_files );
    708 
    709 	if ( ! $code ) {
    710 		return $file;
    711 	}
    712 
    713 	switch ( $code ) {
    714 		case 1:
    715 			wp_die( __( 'Sorry, that file cannot be edited.' ) );
    716 
    717 			// case 2 :
    718 			// wp_die( __('Sorry, can&#8217;t call files with their real path.' ));
    719 
    720 		case 3:
    721 			wp_die( __( 'Sorry, that file cannot be edited.' ) );
    722 	}
    723 }
    724 
    725 /**
    726  * Handles PHP uploads in WordPress.
    727  *
    728  * Sanitizes file names, checks extensions for mime type, and moves the file
    729  * to the appropriate directory within the uploads directory.
    730  *
    731  * @access private
    732  * @since 4.0.0
    733  *
    734  * @see wp_handle_upload_error
    735  *
    736  * @param string[]       $file      Reference to a single element of `$_FILES`.
    737  *                                  Call the function once for each uploaded file.
    738  * @param array|false    $overrides {
    739  *     An array of override parameters for this file, or boolean false if none are provided.
    740  *
    741  *     @type callable $upload_error_handler     Function to call when there is an error during the upload process.
    742  *                                              @see wp_handle_upload_error().
    743  *     @type callable $unique_filename_callback Function to call when determining a unique file name for the file.
    744  *                                              @see wp_unique_filename().
    745  *     @type string[] $upload_error_strings     The strings that describe the error indicated in
    746  *                                              `$_FILES[{form field}]['error']`.
    747  *     @type bool     $test_form                Whether to test that the `$_POST['action']` parameter is as expected.
    748  *     @type bool     $test_size                Whether to test that the file size is greater than zero bytes.
    749  *     @type bool     $test_type                Whether to test that the mime type of the file is as expected.
    750  *     @type string[] $mimes                    Array of allowed mime types keyed by their file extension regex.
    751  * }
    752  * @param string         $time      Time formatted in 'yyyy/mm'.
    753  * @param string         $action    Expected value for `$_POST['action']`.
    754  * @return string[] On success, returns an associative array of file attributes.
    755  *                  On failure, returns `$overrides['upload_error_handler']( &$file, $message )`
    756  *                  or `array( 'error' => $message )`.
    757  */
    758 function _wp_handle_upload( &$file, $overrides, $time, $action ) {
    759 	// The default error handler.
    760 	if ( ! function_exists( 'wp_handle_upload_error' ) ) {
    761 		function wp_handle_upload_error( &$file, $message ) {
    762 			return array( 'error' => $message );
    763 		}
    764 	}
    765 
    766 	/**
    767 	 * Filters the data for a file before it is uploaded to WordPress.
    768 	 *
    769 	 * The dynamic portion of the hook name, `$action`, refers to the post action.
    770 	 *
    771 	 * Possible hook names include:
    772 	 *
    773 	 *  - `wp_handle_sideload_prefilter`
    774 	 *  - `wp_handle_upload_prefilter`
    775 	 *
    776 	 * @since 2.9.0 as 'wp_handle_upload_prefilter'.
    777 	 * @since 4.0.0 Converted to a dynamic hook with `$action`.
    778 	 *
    779 	 * @param string[] $file An array of data for the file. Reference to a single element of `$_FILES`.
    780 	 */
    781 	$file = apply_filters( "{$action}_prefilter", $file );
    782 
    783 	/**
    784 	 * Filters the override parameters for a file before it is uploaded to WordPress.
    785 	 *
    786 	 * The dynamic portion of the hook name, `$action`, refers to the post action.
    787 	 *
    788 	 * Possible hook names include:
    789 	 *
    790 	 *  - `wp_handle_sideload_overrides`
    791 	 *  - `wp_handle_upload_overrides`
    792 	 *
    793 	 * @since 5.7.0
    794 	 *
    795 	 * @param array|false $overrides An array of override parameters for this file. Boolean false if none are
    796 	 *                               provided. @see _wp_handle_upload().
    797 	 * @param string[]    $file      An array of data for the file. Reference to a single element of `$_FILES`.
    798 	 */
    799 	$overrides = apply_filters( "{$action}_overrides", $overrides, $file );
    800 
    801 	// You may define your own function and pass the name in $overrides['upload_error_handler'].
    802 	$upload_error_handler = 'wp_handle_upload_error';
    803 	if ( isset( $overrides['upload_error_handler'] ) ) {
    804 		$upload_error_handler = $overrides['upload_error_handler'];
    805 	}
    806 
    807 	// You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully.
    808 	if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] ) {
    809 		return call_user_func_array( $upload_error_handler, array( &$file, $file['error'] ) );
    810 	}
    811 
    812 	// Install user overrides. Did we mention that this voids your warranty?
    813 
    814 	// You may define your own function and pass the name in $overrides['unique_filename_callback'].
    815 	$unique_filename_callback = null;
    816 	if ( isset( $overrides['unique_filename_callback'] ) ) {
    817 		$unique_filename_callback = $overrides['unique_filename_callback'];
    818 	}
    819 
    820 	/*
    821 	 * This may not have originally been intended to be overridable,
    822 	 * but historically has been.
    823 	 */
    824 	if ( isset( $overrides['upload_error_strings'] ) ) {
    825 		$upload_error_strings = $overrides['upload_error_strings'];
    826 	} else {
    827 		// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
    828 		$upload_error_strings = array(
    829 			false,
    830 			sprintf(
    831 				/* translators: 1: upload_max_filesize, 2: php.ini */
    832 				__( 'The uploaded file exceeds the %1$s directive in %2$s.' ),
    833 				'upload_max_filesize',
    834 				'php.ini'
    835 			),
    836 			sprintf(
    837 				/* translators: %s: MAX_FILE_SIZE */
    838 				__( 'The uploaded file exceeds the %s directive that was specified in the HTML form.' ),
    839 				'MAX_FILE_SIZE'
    840 			),
    841 			__( 'The uploaded file was only partially uploaded.' ),
    842 			__( 'No file was uploaded.' ),
    843 			'',
    844 			__( 'Missing a temporary folder.' ),
    845 			__( 'Failed to write file to disk.' ),
    846 			__( 'File upload stopped by extension.' ),
    847 		);
    848 	}
    849 
    850 	// All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
    851 	$test_form = isset( $overrides['test_form'] ) ? $overrides['test_form'] : true;
    852 	$test_size = isset( $overrides['test_size'] ) ? $overrides['test_size'] : true;
    853 
    854 	// If you override this, you must provide $ext and $type!!
    855 	$test_type = isset( $overrides['test_type'] ) ? $overrides['test_type'] : true;
    856 	$mimes     = isset( $overrides['mimes'] ) ? $overrides['mimes'] : false;
    857 
    858 	// A correct form post will pass this test.
    859 	if ( $test_form && ( ! isset( $_POST['action'] ) || $_POST['action'] !== $action ) ) {
    860 		return call_user_func_array( $upload_error_handler, array( &$file, __( 'Invalid form submission.' ) ) );
    861 	}
    862 
    863 	// A successful upload will pass this test. It makes no sense to override this one.
    864 	if ( isset( $file['error'] ) && $file['error'] > 0 ) {
    865 		return call_user_func_array( $upload_error_handler, array( &$file, $upload_error_strings[ $file['error'] ] ) );
    866 	}
    867 
    868 	// A properly uploaded file will pass this test. There should be no reason to override this one.
    869 	$test_uploaded_file = 'wp_handle_upload' === $action ? is_uploaded_file( $file['tmp_name'] ) : @is_readable( $file['tmp_name'] );
    870 	if ( ! $test_uploaded_file ) {
    871 		return call_user_func_array( $upload_error_handler, array( &$file, __( 'Specified file failed upload test.' ) ) );
    872 	}
    873 
    874 	$test_file_size = 'wp_handle_upload' === $action ? $file['size'] : filesize( $file['tmp_name'] );
    875 	// A non-empty file will pass this test.
    876 	if ( $test_size && ! ( $test_file_size > 0 ) ) {
    877 		if ( is_multisite() ) {
    878 			$error_msg = __( 'File is empty. Please upload something more substantial.' );
    879 		} else {
    880 			$error_msg = sprintf(
    881 				/* translators: 1: php.ini, 2: post_max_size, 3: upload_max_filesize */
    882 				__( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your %1$s file or by %2$s being defined as smaller than %3$s in %1$s.' ),
    883 				'php.ini',
    884 				'post_max_size',
    885 				'upload_max_filesize'
    886 			);
    887 		}
    888 
    889 		return call_user_func_array( $upload_error_handler, array( &$file, $error_msg ) );
    890 	}
    891 
    892 	// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
    893 	if ( $test_type ) {
    894 		$wp_filetype     = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
    895 		$ext             = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
    896 		$type            = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
    897 		$proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
    898 
    899 		// Check to see if wp_check_filetype_and_ext() determined the filename was incorrect.
    900 		if ( $proper_filename ) {
    901 			$file['name'] = $proper_filename;
    902 		}
    903 
    904 		if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
    905 			return call_user_func_array( $upload_error_handler, array( &$file, __( 'Sorry, this file type is not permitted for security reasons.' ) ) );
    906 		}
    907 
    908 		if ( ! $type ) {
    909 			$type = $file['type'];
    910 		}
    911 	} else {
    912 		$type = '';
    913 	}
    914 
    915 	/*
    916 	 * A writable uploads dir will pass this test. Again, there's no point
    917 	 * overriding this one.
    918 	 */
    919 	$uploads = wp_upload_dir( $time );
    920 	if ( ! ( $uploads && false === $uploads['error'] ) ) {
    921 		return call_user_func_array( $upload_error_handler, array( &$file, $uploads['error'] ) );
    922 	}
    923 
    924 	$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
    925 
    926 	// Move the file to the uploads dir.
    927 	$new_file = $uploads['path'] . "/$filename";
    928 
    929 	/**
    930 	 * Filters whether to short-circuit moving the uploaded file after passing all checks.
    931 	 *
    932 	 * If a non-null value is returned from the filter, moving the file and any related
    933 	 * error reporting will be completely skipped.
    934 	 *
    935 	 * @since 4.9.0
    936 	 *
    937 	 * @param mixed    $move_new_file If null (default) move the file after the upload.
    938 	 * @param string[] $file          An array of data for a single file.
    939 	 * @param string   $new_file      Filename of the newly-uploaded file.
    940 	 * @param string   $type          Mime type of the newly-uploaded file.
    941 	 */
    942 	$move_new_file = apply_filters( 'pre_move_uploaded_file', null, $file, $new_file, $type );
    943 
    944 	if ( null === $move_new_file ) {
    945 		if ( 'wp_handle_upload' === $action ) {
    946 			$move_new_file = @move_uploaded_file( $file['tmp_name'], $new_file );
    947 		} else {
    948 			// Use copy and unlink because rename breaks streams.
    949 			// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
    950 			$move_new_file = @copy( $file['tmp_name'], $new_file );
    951 			unlink( $file['tmp_name'] );
    952 		}
    953 
    954 		if ( false === $move_new_file ) {
    955 			if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
    956 				$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
    957 			} else {
    958 				$error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
    959 			}
    960 
    961 			return $upload_error_handler(
    962 				$file,
    963 				sprintf(
    964 					/* translators: %s: Destination file path. */
    965 					__( 'The uploaded file could not be moved to %s.' ),
    966 					$error_path
    967 				)
    968 			);
    969 		}
    970 	}
    971 
    972 	// Set correct file permissions.
    973 	$stat  = stat( dirname( $new_file ) );
    974 	$perms = $stat['mode'] & 0000666;
    975 	chmod( $new_file, $perms );
    976 
    977 	// Compute the URL.
    978 	$url = $uploads['url'] . "/$filename";
    979 
    980 	if ( is_multisite() ) {
    981 		clean_dirsize_cache( $new_file );
    982 	}
    983 
    984 	/**
    985 	 * Filters the data array for the uploaded file.
    986 	 *
    987 	 * @since 2.1.0
    988 	 *
    989 	 * @param array  $upload {
    990 	 *     Array of upload data.
    991 	 *
    992 	 *     @type string $file Filename of the newly-uploaded file.
    993 	 *     @type string $url  URL of the newly-uploaded file.
    994 	 *     @type string $type Mime type of the newly-uploaded file.
    995 	 * }
    996 	 * @param string $context The type of upload action. Values include 'upload' or 'sideload'.
    997 	 */
    998 	return apply_filters(
    999 		'wp_handle_upload',
   1000 		array(
   1001 			'file' => $new_file,
   1002 			'url'  => $url,
   1003 			'type' => $type,
   1004 		),
   1005 		'wp_handle_sideload' === $action ? 'sideload' : 'upload'
   1006 	);
   1007 }
   1008 
   1009 /**
   1010  * Wrapper for _wp_handle_upload().
   1011  *
   1012  * Passes the {@see 'wp_handle_upload'} action.
   1013  *
   1014  * @since 2.0.0
   1015  *
   1016  * @see _wp_handle_upload()
   1017  *
   1018  * @param array       $file      Reference to a single element of `$_FILES`.
   1019  *                               Call the function once for each uploaded file.
   1020  * @param array|false $overrides Optional. An associative array of names => values
   1021  *                               to override default variables. Default false.
   1022  * @param string      $time      Optional. Time formatted in 'yyyy/mm'. Default null.
   1023  * @return array On success, returns an associative array of file attributes.
   1024  *               On failure, returns `$overrides['upload_error_handler']( &$file, $message )`
   1025  *               or `array( 'error' => $message )`.
   1026  */
   1027 function wp_handle_upload( &$file, $overrides = false, $time = null ) {
   1028 	/*
   1029 	 *  $_POST['action'] must be set and its value must equal $overrides['action']
   1030 	 *  or this:
   1031 	 */
   1032 	$action = 'wp_handle_upload';
   1033 	if ( isset( $overrides['action'] ) ) {
   1034 		$action = $overrides['action'];
   1035 	}
   1036 
   1037 	return _wp_handle_upload( $file, $overrides, $time, $action );
   1038 }
   1039 
   1040 /**
   1041  * Wrapper for _wp_handle_upload().
   1042  *
   1043  * Passes the {@see 'wp_handle_sideload'} action.
   1044  *
   1045  * @since 2.6.0
   1046  *
   1047  * @see _wp_handle_upload()
   1048  *
   1049  * @param array       $file      Reference to a single element of `$_FILES`.
   1050  *                               Call the function once for each uploaded file.
   1051  * @param array|false $overrides Optional. An associative array of names => values
   1052  *                               to override default variables. Default false.
   1053  * @param string      $time      Optional. Time formatted in 'yyyy/mm'. Default null.
   1054  * @return array On success, returns an associative array of file attributes.
   1055  *               On failure, returns `$overrides['upload_error_handler']( &$file, $message )`
   1056  *               or `array( 'error' => $message )`.
   1057  */
   1058 function wp_handle_sideload( &$file, $overrides = false, $time = null ) {
   1059 	/*
   1060 	 *  $_POST['action'] must be set and its value must equal $overrides['action']
   1061 	 *  or this:
   1062 	 */
   1063 	$action = 'wp_handle_sideload';
   1064 	if ( isset( $overrides['action'] ) ) {
   1065 		$action = $overrides['action'];
   1066 	}
   1067 
   1068 	return _wp_handle_upload( $file, $overrides, $time, $action );
   1069 }
   1070 
   1071 /**
   1072  * Downloads a URL to a local temporary file using the WordPress HTTP API.
   1073  *
   1074  * Please note that the calling function must unlink() the file.
   1075  *
   1076  * @since 2.5.0
   1077  * @since 5.2.0 Signature Verification with SoftFail was added.
   1078  *
   1079  * @param string $url                    The URL of the file to download.
   1080  * @param int    $timeout                The timeout for the request to download the file.
   1081  *                                       Default 300 seconds.
   1082  * @param bool   $signature_verification Whether to perform Signature Verification.
   1083  *                                       Default false.
   1084  * @return string|WP_Error Filename on success, WP_Error on failure.
   1085  */
   1086 function download_url( $url, $timeout = 300, $signature_verification = false ) {
   1087 	// WARNING: The file is not automatically deleted, the script must unlink() the file.
   1088 	if ( ! $url ) {
   1089 		return new WP_Error( 'http_no_url', __( 'Invalid URL Provided.' ) );
   1090 	}
   1091 
   1092 	$url_filename = basename( parse_url( $url, PHP_URL_PATH ) );
   1093 
   1094 	$tmpfname = wp_tempnam( $url_filename );
   1095 	if ( ! $tmpfname ) {
   1096 		return new WP_Error( 'http_no_file', __( 'Could not create temporary file.' ) );
   1097 	}
   1098 
   1099 	$response = wp_safe_remote_get(
   1100 		$url,
   1101 		array(
   1102 			'timeout'  => $timeout,
   1103 			'stream'   => true,
   1104 			'filename' => $tmpfname,
   1105 		)
   1106 	);
   1107 
   1108 	if ( is_wp_error( $response ) ) {
   1109 		unlink( $tmpfname );
   1110 		return $response;
   1111 	}
   1112 
   1113 	$response_code = wp_remote_retrieve_response_code( $response );
   1114 
   1115 	if ( 200 !== $response_code ) {
   1116 		$data = array(
   1117 			'code' => $response_code,
   1118 		);
   1119 
   1120 		// Retrieve a sample of the response body for debugging purposes.
   1121 		$tmpf = fopen( $tmpfname, 'rb' );
   1122 
   1123 		if ( $tmpf ) {
   1124 			/**
   1125 			 * Filters the maximum error response body size in `download_url()`.
   1126 			 *
   1127 			 * @since 5.1.0
   1128 			 *
   1129 			 * @see download_url()
   1130 			 *
   1131 			 * @param int $size The maximum error response body size. Default 1 KB.
   1132 			 */
   1133 			$response_size = apply_filters( 'download_url_error_max_body_size', KB_IN_BYTES );
   1134 
   1135 			$data['body'] = fread( $tmpf, $response_size );
   1136 			fclose( $tmpf );
   1137 		}
   1138 
   1139 		unlink( $tmpfname );
   1140 
   1141 		return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data );
   1142 	}
   1143 
   1144 	$content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );
   1145 
   1146 	if ( $content_md5 ) {
   1147 		$md5_check = verify_file_md5( $tmpfname, $content_md5 );
   1148 
   1149 		if ( is_wp_error( $md5_check ) ) {
   1150 			unlink( $tmpfname );
   1151 			return $md5_check;
   1152 		}
   1153 	}
   1154 
   1155 	// If the caller expects signature verification to occur, check to see if this URL supports it.
   1156 	if ( $signature_verification ) {
   1157 		/**
   1158 		 * Filters the list of hosts which should have Signature Verification attempted on.
   1159 		 *
   1160 		 * @since 5.2.0
   1161 		 *
   1162 		 * @param string[] $hostnames List of hostnames.
   1163 		 */
   1164 		$signed_hostnames = apply_filters( 'wp_signature_hosts', array( 'wordpress.org', 'downloads.wordpress.org', 's.w.org' ) );
   1165 
   1166 		$signature_verification = in_array( parse_url( $url, PHP_URL_HOST ), $signed_hostnames, true );
   1167 	}
   1168 
   1169 	// Perform signature valiation if supported.
   1170 	if ( $signature_verification ) {
   1171 		$signature = wp_remote_retrieve_header( $response, 'x-content-signature' );
   1172 
   1173 		if ( ! $signature ) {
   1174 			// Retrieve signatures from a file if the header wasn't included.
   1175 			// WordPress.org stores signatures at $package_url.sig.
   1176 
   1177 			$signature_url = false;
   1178 			$url_path      = parse_url( $url, PHP_URL_PATH );
   1179 
   1180 			if ( '.zip' === substr( $url_path, -4 ) || '.tar.gz' === substr( $url_path, -7 ) ) {
   1181 				$signature_url = str_replace( $url_path, $url_path . '.sig', $url );
   1182 			}
   1183 
   1184 			/**
   1185 			 * Filters the URL where the signature for a file is located.
   1186 			 *
   1187 			 * @since 5.2.0
   1188 			 *
   1189 			 * @param false|string $signature_url The URL where signatures can be found for a file, or false if none are known.
   1190 			 * @param string $url                 The URL being verified.
   1191 			 */
   1192 			$signature_url = apply_filters( 'wp_signature_url', $signature_url, $url );
   1193 
   1194 			if ( $signature_url ) {
   1195 				$signature_request = wp_safe_remote_get(
   1196 					$signature_url,
   1197 					array(
   1198 						'limit_response_size' => 10 * KB_IN_BYTES, // 10KB should be large enough for quite a few signatures.
   1199 					)
   1200 				);
   1201 
   1202 				if ( ! is_wp_error( $signature_request ) && 200 === wp_remote_retrieve_response_code( $signature_request ) ) {
   1203 					$signature = explode( "\n", wp_remote_retrieve_body( $signature_request ) );
   1204 				}
   1205 			}
   1206 		}
   1207 
   1208 		// Perform the checks.
   1209 		$signature_verification = verify_file_signature( $tmpfname, $signature, basename( parse_url( $url, PHP_URL_PATH ) ) );
   1210 	}
   1211 
   1212 	if ( is_wp_error( $signature_verification ) ) {
   1213 		if (
   1214 			/**
   1215 			 * Filters whether Signature Verification failures should be allowed to soft fail.
   1216 			 *
   1217 			 * WARNING: This may be removed from a future release.
   1218 			 *
   1219 			 * @since 5.2.0
   1220 			 *
   1221 			 * @param bool   $signature_softfail If a softfail is allowed.
   1222 			 * @param string $url                The url being accessed.
   1223 			 */
   1224 			apply_filters( 'wp_signature_softfail', true, $url )
   1225 		) {
   1226 			$signature_verification->add_data( $tmpfname, 'softfail-filename' );
   1227 		} else {
   1228 			// Hard-fail.
   1229 			unlink( $tmpfname );
   1230 		}
   1231 
   1232 		return $signature_verification;
   1233 	}
   1234 
   1235 	return $tmpfname;
   1236 }
   1237 
   1238 /**
   1239  * Calculates and compares the MD5 of a file to its expected value.
   1240  *
   1241  * @since 3.7.0
   1242  *
   1243  * @param string $filename     The filename to check the MD5 of.
   1244  * @param string $expected_md5 The expected MD5 of the file, either a base64-encoded raw md5,
   1245  *                             or a hex-encoded md5.
   1246  * @return bool|WP_Error True on success, false when the MD5 format is unknown/unexpected,
   1247  *                       WP_Error on failure.
   1248  */
   1249 function verify_file_md5( $filename, $expected_md5 ) {
   1250 	if ( 32 === strlen( $expected_md5 ) ) {
   1251 		$expected_raw_md5 = pack( 'H*', $expected_md5 );
   1252 	} elseif ( 24 === strlen( $expected_md5 ) ) {
   1253 		$expected_raw_md5 = base64_decode( $expected_md5 );
   1254 	} else {
   1255 		return false; // Unknown format.
   1256 	}
   1257 
   1258 	$file_md5 = md5_file( $filename, true );
   1259 
   1260 	if ( $file_md5 === $expected_raw_md5 ) {
   1261 		return true;
   1262 	}
   1263 
   1264 	return new WP_Error(
   1265 		'md5_mismatch',
   1266 		sprintf(
   1267 			/* translators: 1: File checksum, 2: Expected checksum value. */
   1268 			__( 'The checksum of the file (%1$s) does not match the expected checksum value (%2$s).' ),
   1269 			bin2hex( $file_md5 ),
   1270 			bin2hex( $expected_raw_md5 )
   1271 		)
   1272 	);
   1273 }
   1274 
   1275 /**
   1276  * Verifies the contents of a file against its ED25519 signature.
   1277  *
   1278  * @since 5.2.0
   1279  *
   1280  * @param string       $filename            The file to validate.
   1281  * @param string|array $signatures          A Signature provided for the file.
   1282  * @param string|false $filename_for_errors Optional. A friendly filename for errors.
   1283  * @return bool|WP_Error True on success, false if verification not attempted,
   1284  *                       or WP_Error describing an error condition.
   1285  */
   1286 function verify_file_signature( $filename, $signatures, $filename_for_errors = false ) {
   1287 	if ( ! $filename_for_errors ) {
   1288 		$filename_for_errors = wp_basename( $filename );
   1289 	}
   1290 
   1291 	// Check we can process signatures.
   1292 	if ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) || ! in_array( 'sha384', array_map( 'strtolower', hash_algos() ), true ) ) {
   1293 		return new WP_Error(
   1294 			'signature_verification_unsupported',
   1295 			sprintf(
   1296 				/* translators: %s: The filename of the package. */
   1297 				__( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
   1298 				'<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
   1299 			),
   1300 			( ! function_exists( 'sodium_crypto_sign_verify_detached' ) ? 'sodium_crypto_sign_verify_detached' : 'sha384' )
   1301 		);
   1302 	}
   1303 
   1304 	// Check for a edge-case affecting PHP Maths abilities.
   1305 	if (
   1306 		! extension_loaded( 'sodium' ) &&
   1307 		in_array( PHP_VERSION_ID, array( 70200, 70201, 70202 ), true ) &&
   1308 		extension_loaded( 'opcache' )
   1309 	) {
   1310 		// Sodium_Compat isn't compatible with PHP 7.2.0~7.2.2 due to a bug in the PHP Opcache extension, bail early as it'll fail.
   1311 		// https://bugs.php.net/bug.php?id=75938
   1312 		return new WP_Error(
   1313 			'signature_verification_unsupported',
   1314 			sprintf(
   1315 				/* translators: %s: The filename of the package. */
   1316 				__( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
   1317 				'<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
   1318 			),
   1319 			array(
   1320 				'php'    => phpversion(),
   1321 				// phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
   1322 				'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
   1323 			)
   1324 		);
   1325 	}
   1326 
   1327 	// Verify runtime speed of Sodium_Compat is acceptable.
   1328 	if ( ! extension_loaded( 'sodium' ) && ! ParagonIE_Sodium_Compat::polyfill_is_fast() ) {
   1329 		$sodium_compat_is_fast = false;
   1330 
   1331 		// Allow for an old version of Sodium_Compat being loaded before the bundled WordPress one.
   1332 		if ( method_exists( 'ParagonIE_Sodium_Compat', 'runtime_speed_test' ) ) {
   1333 			// Run `ParagonIE_Sodium_Compat::runtime_speed_test()` in optimized integer mode, as that's what WordPress utilises during signing verifications.
   1334 			// phpcs:disable WordPress.NamingConventions.ValidVariableName
   1335 			$old_fastMult                      = ParagonIE_Sodium_Compat::$fastMult;
   1336 			ParagonIE_Sodium_Compat::$fastMult = true;
   1337 			$sodium_compat_is_fast             = ParagonIE_Sodium_Compat::runtime_speed_test( 100, 10 );
   1338 			ParagonIE_Sodium_Compat::$fastMult = $old_fastMult;
   1339 			// phpcs:enable
   1340 		}
   1341 
   1342 		// This cannot be performed in a reasonable amount of time.
   1343 		// https://github.com/paragonie/sodium_compat#help-sodium_compat-is-slow-how-can-i-make-it-fast
   1344 		if ( ! $sodium_compat_is_fast ) {
   1345 			return new WP_Error(
   1346 				'signature_verification_unsupported',
   1347 				sprintf(
   1348 					/* translators: %s: The filename of the package. */
   1349 					__( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
   1350 					'<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
   1351 				),
   1352 				array(
   1353 					'php'                => phpversion(),
   1354 					// phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
   1355 					'sodium'             => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
   1356 					'polyfill_is_fast'   => false,
   1357 					'max_execution_time' => ini_get( 'max_execution_time' ),
   1358 				)
   1359 			);
   1360 		}
   1361 	}
   1362 
   1363 	if ( ! $signatures ) {
   1364 		return new WP_Error(
   1365 			'signature_verification_no_signature',
   1366 			sprintf(
   1367 				/* translators: %s: The filename of the package. */
   1368 				__( 'The authenticity of %s could not be verified as no signature was found.' ),
   1369 				'<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
   1370 			),
   1371 			array(
   1372 				'filename' => $filename_for_errors,
   1373 			)
   1374 		);
   1375 	}
   1376 
   1377 	$trusted_keys = wp_trusted_keys();
   1378 	$file_hash    = hash_file( 'sha384', $filename, true );
   1379 
   1380 	mbstring_binary_safe_encoding();
   1381 
   1382 	$skipped_key       = 0;
   1383 	$skipped_signature = 0;
   1384 
   1385 	foreach ( (array) $signatures as $signature ) {
   1386 		$signature_raw = base64_decode( $signature );
   1387 
   1388 		// Ensure only valid-length signatures are considered.
   1389 		if ( SODIUM_CRYPTO_SIGN_BYTES !== strlen( $signature_raw ) ) {
   1390 			$skipped_signature++;
   1391 			continue;
   1392 		}
   1393 
   1394 		foreach ( (array) $trusted_keys as $key ) {
   1395 			$key_raw = base64_decode( $key );
   1396 
   1397 			// Only pass valid public keys through.
   1398 			if ( SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES !== strlen( $key_raw ) ) {
   1399 				$skipped_key++;
   1400 				continue;
   1401 			}
   1402 
   1403 			if ( sodium_crypto_sign_verify_detached( $signature_raw, $file_hash, $key_raw ) ) {
   1404 				reset_mbstring_encoding();
   1405 				return true;
   1406 			}
   1407 		}
   1408 	}
   1409 
   1410 	reset_mbstring_encoding();
   1411 
   1412 	return new WP_Error(
   1413 		'signature_verification_failed',
   1414 		sprintf(
   1415 			/* translators: %s: The filename of the package. */
   1416 			__( 'The authenticity of %s could not be verified.' ),
   1417 			'<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
   1418 		),
   1419 		// Error data helpful for debugging:
   1420 		array(
   1421 			'filename'    => $filename_for_errors,
   1422 			'keys'        => $trusted_keys,
   1423 			'signatures'  => $signatures,
   1424 			'hash'        => bin2hex( $file_hash ),
   1425 			'skipped_key' => $skipped_key,
   1426 			'skipped_sig' => $skipped_signature,
   1427 			'php'         => phpversion(),
   1428 			// phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
   1429 			'sodium'      => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
   1430 		)
   1431 	);
   1432 }
   1433 
   1434 /**
   1435  * Retrieves the list of signing keys trusted by WordPress.
   1436  *
   1437  * @since 5.2.0
   1438  *
   1439  * @return string[] Array of base64-encoded signing keys.
   1440  */
   1441 function wp_trusted_keys() {
   1442 	$trusted_keys = array();
   1443 
   1444 	if ( time() < 1617235200 ) {
   1445 		// WordPress.org Key #1 - This key is only valid before April 1st, 2021.
   1446 		$trusted_keys[] = 'fRPyrxb/MvVLbdsYi+OOEv4xc+Eqpsj+kkAS6gNOkI0=';
   1447 	}
   1448 
   1449 	// TODO: Add key #2 with longer expiration.
   1450 
   1451 	/**
   1452 	 * Filters the valid signing keys used to verify the contents of files.
   1453 	 *
   1454 	 * @since 5.2.0
   1455 	 *
   1456 	 * @param string[] $trusted_keys The trusted keys that may sign packages.
   1457 	 */
   1458 	return apply_filters( 'wp_trusted_keys', $trusted_keys );
   1459 }
   1460 
   1461 /**
   1462  * Unzips a specified ZIP file to a location on the filesystem via the WordPress
   1463  * Filesystem Abstraction.
   1464  *
   1465  * Assumes that WP_Filesystem() has already been called and set up. Does not extract
   1466  * a root-level __MACOSX directory, if present.
   1467  *
   1468  * Attempts to increase the PHP memory limit to 256M before uncompressing. However,
   1469  * the most memory required shouldn't be much larger than the archive itself.
   1470  *
   1471  * @since 2.5.0
   1472  *
   1473  * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   1474  *
   1475  * @param string $file Full path and filename of ZIP archive.
   1476  * @param string $to   Full path on the filesystem to extract archive to.
   1477  * @return true|WP_Error True on success, WP_Error on failure.
   1478  */
   1479 function unzip_file( $file, $to ) {
   1480 	global $wp_filesystem;
   1481 
   1482 	if ( ! $wp_filesystem || ! is_object( $wp_filesystem ) ) {
   1483 		return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
   1484 	}
   1485 
   1486 	// Unzip can use a lot of memory, but not this much hopefully.
   1487 	wp_raise_memory_limit( 'admin' );
   1488 
   1489 	$needed_dirs = array();
   1490 	$to          = trailingslashit( $to );
   1491 
   1492 	// Determine any parent directories needed (of the upgrade directory).
   1493 	if ( ! $wp_filesystem->is_dir( $to ) ) { // Only do parents if no children exist.
   1494 		$path = preg_split( '![/\\\]!', untrailingslashit( $to ) );
   1495 		for ( $i = count( $path ); $i >= 0; $i-- ) {
   1496 			if ( empty( $path[ $i ] ) ) {
   1497 				continue;
   1498 			}
   1499 
   1500 			$dir = implode( '/', array_slice( $path, 0, $i + 1 ) );
   1501 			if ( preg_match( '!^[a-z]:$!i', $dir ) ) { // Skip it if it looks like a Windows Drive letter.
   1502 				continue;
   1503 			}
   1504 
   1505 			if ( ! $wp_filesystem->is_dir( $dir ) ) {
   1506 				$needed_dirs[] = $dir;
   1507 			} else {
   1508 				break; // A folder exists, therefore we don't need to check the levels below this.
   1509 			}
   1510 		}
   1511 	}
   1512 
   1513 	/**
   1514 	 * Filters whether to use ZipArchive to unzip archives.
   1515 	 *
   1516 	 * @since 3.0.0
   1517 	 *
   1518 	 * @param bool $ziparchive Whether to use ZipArchive. Default true.
   1519 	 */
   1520 	if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) {
   1521 		$result = _unzip_file_ziparchive( $file, $to, $needed_dirs );
   1522 		if ( true === $result ) {
   1523 			return $result;
   1524 		} elseif ( is_wp_error( $result ) ) {
   1525 			if ( 'incompatible_archive' !== $result->get_error_code() ) {
   1526 				return $result;
   1527 			}
   1528 		}
   1529 	}
   1530 	// Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
   1531 	return _unzip_file_pclzip( $file, $to, $needed_dirs );
   1532 }
   1533 
   1534 /**
   1535  * Attempts to unzip an archive using the ZipArchive class.
   1536  *
   1537  * This function should not be called directly, use `unzip_file()` instead.
   1538  *
   1539  * Assumes that WP_Filesystem() has already been called and set up.
   1540  *
   1541  * @since 3.0.0
   1542  * @access private
   1543  *
   1544  * @see unzip_file()
   1545  *
   1546  * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   1547  *
   1548  * @param string   $file        Full path and filename of ZIP archive.
   1549  * @param string   $to          Full path on the filesystem to extract archive to.
   1550  * @param string[] $needed_dirs A partial list of required folders needed to be created.
   1551  * @return true|WP_Error True on success, WP_Error on failure.
   1552  */
   1553 function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
   1554 	global $wp_filesystem;
   1555 
   1556 	$z = new ZipArchive();
   1557 
   1558 	$zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS );
   1559 
   1560 	if ( true !== $zopen ) {
   1561 		return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) );
   1562 	}
   1563 
   1564 	$uncompressed_size = 0;
   1565 
   1566 	for ( $i = 0; $i < $z->numFiles; $i++ ) {
   1567 		$info = $z->statIndex( $i );
   1568 
   1569 		if ( ! $info ) {
   1570 			return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
   1571 		}
   1572 
   1573 		if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory.
   1574 			continue;
   1575 		}
   1576 
   1577 		// Don't extract invalid files:
   1578 		if ( 0 !== validate_file( $info['name'] ) ) {
   1579 			continue;
   1580 		}
   1581 
   1582 		$uncompressed_size += $info['size'];
   1583 
   1584 		$dirname = dirname( $info['name'] );
   1585 
   1586 		if ( '/' === substr( $info['name'], -1 ) ) {
   1587 			// Directory.
   1588 			$needed_dirs[] = $to . untrailingslashit( $info['name'] );
   1589 		} elseif ( '.' !== $dirname ) {
   1590 			// Path to a file.
   1591 			$needed_dirs[] = $to . untrailingslashit( $dirname );
   1592 		}
   1593 	}
   1594 
   1595 	/*
   1596 	 * disk_free_space() could return false. Assume that any falsey value is an error.
   1597 	 * A disk that has zero free bytes has bigger problems.
   1598 	 * Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
   1599 	 */
   1600 	if ( wp_doing_cron() ) {
   1601 		$available_space = @disk_free_space( WP_CONTENT_DIR );
   1602 
   1603 		if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
   1604 			return new WP_Error(
   1605 				'disk_full_unzip_file',
   1606 				__( 'Could not copy files. You may have run out of disk space.' ),
   1607 				compact( 'uncompressed_size', 'available_space' )
   1608 			);
   1609 		}
   1610 	}
   1611 
   1612 	$needed_dirs = array_unique( $needed_dirs );
   1613 
   1614 	foreach ( $needed_dirs as $dir ) {
   1615 		// Check the parent folders of the folders all exist within the creation array.
   1616 		if ( untrailingslashit( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist).
   1617 			continue;
   1618 		}
   1619 
   1620 		if ( strpos( $dir, $to ) === false ) { // If the directory is not within the working directory, skip it.
   1621 			continue;
   1622 		}
   1623 
   1624 		$parent_folder = dirname( $dir );
   1625 
   1626 		while ( ! empty( $parent_folder )
   1627 			&& untrailingslashit( $to ) !== $parent_folder
   1628 			&& ! in_array( $parent_folder, $needed_dirs, true )
   1629 		) {
   1630 			$needed_dirs[] = $parent_folder;
   1631 			$parent_folder = dirname( $parent_folder );
   1632 		}
   1633 	}
   1634 
   1635 	asort( $needed_dirs );
   1636 
   1637 	// Create those directories if need be:
   1638 	foreach ( $needed_dirs as $_dir ) {
   1639 		// Only check to see if the Dir exists upon creation failure. Less I/O this way.
   1640 		if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) {
   1641 			return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) );
   1642 		}
   1643 	}
   1644 	unset( $needed_dirs );
   1645 
   1646 	for ( $i = 0; $i < $z->numFiles; $i++ ) {
   1647 		$info = $z->statIndex( $i );
   1648 
   1649 		if ( ! $info ) {
   1650 			return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
   1651 		}
   1652 
   1653 		if ( '/' === substr( $info['name'], -1 ) ) { // Directory.
   1654 			continue;
   1655 		}
   1656 
   1657 		if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
   1658 			continue;
   1659 		}
   1660 
   1661 		// Don't extract invalid files:
   1662 		if ( 0 !== validate_file( $info['name'] ) ) {
   1663 			continue;
   1664 		}
   1665 
   1666 		$contents = $z->getFromIndex( $i );
   1667 
   1668 		if ( false === $contents ) {
   1669 			return new WP_Error( 'extract_failed_ziparchive', __( 'Could not extract file from archive.' ), $info['name'] );
   1670 		}
   1671 
   1672 		if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE ) ) {
   1673 			return new WP_Error( 'copy_failed_ziparchive', __( 'Could not copy file.' ), $info['name'] );
   1674 		}
   1675 	}
   1676 
   1677 	$z->close();
   1678 
   1679 	return true;
   1680 }
   1681 
   1682 /**
   1683  * Attempts to unzip an archive using the PclZip library.
   1684  *
   1685  * This function should not be called directly, use `unzip_file()` instead.
   1686  *
   1687  * Assumes that WP_Filesystem() has already been called and set up.
   1688  *
   1689  * @since 3.0.0
   1690  * @access private
   1691  *
   1692  * @see unzip_file()
   1693  *
   1694  * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   1695  *
   1696  * @param string   $file        Full path and filename of ZIP archive.
   1697  * @param string   $to          Full path on the filesystem to extract archive to.
   1698  * @param string[] $needed_dirs A partial list of required folders needed to be created.
   1699  * @return true|WP_Error True on success, WP_Error on failure.
   1700  */
   1701 function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
   1702 	global $wp_filesystem;
   1703 
   1704 	mbstring_binary_safe_encoding();
   1705 
   1706 	require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
   1707 
   1708 	$archive = new PclZip( $file );
   1709 
   1710 	$archive_files = $archive->extract( PCLZIP_OPT_EXTRACT_AS_STRING );
   1711 
   1712 	reset_mbstring_encoding();
   1713 
   1714 	// Is the archive valid?
   1715 	if ( ! is_array( $archive_files ) ) {
   1716 		return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), $archive->errorInfo( true ) );
   1717 	}
   1718 
   1719 	if ( 0 === count( $archive_files ) ) {
   1720 		return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) );
   1721 	}
   1722 
   1723 	$uncompressed_size = 0;
   1724 
   1725 	// Determine any children directories needed (From within the archive).
   1726 	foreach ( $archive_files as $file ) {
   1727 		if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory.
   1728 			continue;
   1729 		}
   1730 
   1731 		$uncompressed_size += $file['size'];
   1732 
   1733 		$needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) );
   1734 	}
   1735 
   1736 	/*
   1737 	 * disk_free_space() could return false. Assume that any falsey value is an error.
   1738 	 * A disk that has zero free bytes has bigger problems.
   1739 	 * Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
   1740 	 */
   1741 	if ( wp_doing_cron() ) {
   1742 		$available_space = @disk_free_space( WP_CONTENT_DIR );
   1743 
   1744 		if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
   1745 			return new WP_Error(
   1746 				'disk_full_unzip_file',
   1747 				__( 'Could not copy files. You may have run out of disk space.' ),
   1748 				compact( 'uncompressed_size', 'available_space' )
   1749 			);
   1750 		}
   1751 	}
   1752 
   1753 	$needed_dirs = array_unique( $needed_dirs );
   1754 
   1755 	foreach ( $needed_dirs as $dir ) {
   1756 		// Check the parent folders of the folders all exist within the creation array.
   1757 		if ( untrailingslashit( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist).
   1758 			continue;
   1759 		}
   1760 
   1761 		if ( strpos( $dir, $to ) === false ) { // If the directory is not within the working directory, skip it.
   1762 			continue;
   1763 		}
   1764 
   1765 		$parent_folder = dirname( $dir );
   1766 
   1767 		while ( ! empty( $parent_folder )
   1768 			&& untrailingslashit( $to ) !== $parent_folder
   1769 			&& ! in_array( $parent_folder, $needed_dirs, true )
   1770 		) {
   1771 			$needed_dirs[] = $parent_folder;
   1772 			$parent_folder = dirname( $parent_folder );
   1773 		}
   1774 	}
   1775 
   1776 	asort( $needed_dirs );
   1777 
   1778 	// Create those directories if need be:
   1779 	foreach ( $needed_dirs as $_dir ) {
   1780 		// Only check to see if the dir exists upon creation failure. Less I/O this way.
   1781 		if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) {
   1782 			return new WP_Error( 'mkdir_failed_pclzip', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) );
   1783 		}
   1784 	}
   1785 	unset( $needed_dirs );
   1786 
   1787 	// Extract the files from the zip.
   1788 	foreach ( $archive_files as $file ) {
   1789 		if ( $file['folder'] ) {
   1790 			continue;
   1791 		}
   1792 
   1793 		if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
   1794 			continue;
   1795 		}
   1796 
   1797 		// Don't extract invalid files:
   1798 		if ( 0 !== validate_file( $file['filename'] ) ) {
   1799 			continue;
   1800 		}
   1801 
   1802 		if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE ) ) {
   1803 			return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] );
   1804 		}
   1805 	}
   1806 
   1807 	return true;
   1808 }
   1809 
   1810 /**
   1811  * Copies a directory from one location to another via the WordPress Filesystem
   1812  * Abstraction.
   1813  *
   1814  * Assumes that WP_Filesystem() has already been called and setup.
   1815  *
   1816  * @since 2.5.0
   1817  *
   1818  * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   1819  *
   1820  * @param string   $from      Source directory.
   1821  * @param string   $to        Destination directory.
   1822  * @param string[] $skip_list An array of files/folders to skip copying.
   1823  * @return true|WP_Error True on success, WP_Error on failure.
   1824  */
   1825 function copy_dir( $from, $to, $skip_list = array() ) {
   1826 	global $wp_filesystem;
   1827 
   1828 	$dirlist = $wp_filesystem->dirlist( $from );
   1829 
   1830 	if ( false === $dirlist ) {
   1831 		return new WP_Error( 'dirlist_failed_copy_dir', __( 'Directory listing failed.' ), basename( $to ) );
   1832 	}
   1833 
   1834 	$from = trailingslashit( $from );
   1835 	$to   = trailingslashit( $to );
   1836 
   1837 	foreach ( (array) $dirlist as $filename => $fileinfo ) {
   1838 		if ( in_array( $filename, $skip_list, true ) ) {
   1839 			continue;
   1840 		}
   1841 
   1842 		if ( 'f' === $fileinfo['type'] ) {
   1843 			if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
   1844 				// If copy failed, chmod file to 0644 and try again.
   1845 				$wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE );
   1846 
   1847 				if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
   1848 					return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename );
   1849 				}
   1850 			}
   1851 
   1852 			wp_opcache_invalidate( $to . $filename );
   1853 		} elseif ( 'd' === $fileinfo['type'] ) {
   1854 			if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
   1855 				if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) {
   1856 					return new WP_Error( 'mkdir_failed_copy_dir', __( 'Could not create directory.' ), $to . $filename );
   1857 				}
   1858 			}
   1859 
   1860 			// Generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list.
   1861 			$sub_skip_list = array();
   1862 
   1863 			foreach ( $skip_list as $skip_item ) {
   1864 				if ( 0 === strpos( $skip_item, $filename . '/' ) ) {
   1865 					$sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
   1866 				}
   1867 			}
   1868 
   1869 			$result = copy_dir( $from . $filename, $to . $filename, $sub_skip_list );
   1870 
   1871 			if ( is_wp_error( $result ) ) {
   1872 				return $result;
   1873 			}
   1874 		}
   1875 	}
   1876 
   1877 	return true;
   1878 }
   1879 
   1880 /**
   1881  * Initializes and connects the WordPress Filesystem Abstraction classes.
   1882  *
   1883  * This function will include the chosen transport and attempt connecting.
   1884  *
   1885  * Plugins may add extra transports, And force WordPress to use them by returning
   1886  * the filename via the {@see 'filesystem_method_file'} filter.
   1887  *
   1888  * @since 2.5.0
   1889  *
   1890  * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   1891  *
   1892  * @param array|false  $args                         Optional. Connection args, These are passed
   1893  *                                                   directly to the `WP_Filesystem_*()` classes.
   1894  *                                                   Default false.
   1895  * @param string|false $context                      Optional. Context for get_filesystem_method().
   1896  *                                                   Default false.
   1897  * @param bool         $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
   1898  *                                                   Default false.
   1899  * @return bool|null True on success, false on failure,
   1900  *                   null if the filesystem method class file does not exist.
   1901  */
   1902 function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
   1903 	global $wp_filesystem;
   1904 
   1905 	require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
   1906 
   1907 	$method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership );
   1908 
   1909 	if ( ! $method ) {
   1910 		return false;
   1911 	}
   1912 
   1913 	if ( ! class_exists( "WP_Filesystem_$method" ) ) {
   1914 
   1915 		/**
   1916 		 * Filters the path for a specific filesystem method class file.
   1917 		 *
   1918 		 * @since 2.6.0
   1919 		 *
   1920 		 * @see get_filesystem_method()
   1921 		 *
   1922 		 * @param string $path   Path to the specific filesystem method class file.
   1923 		 * @param string $method The filesystem method to use.
   1924 		 */
   1925 		$abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method );
   1926 
   1927 		if ( ! file_exists( $abstraction_file ) ) {
   1928 			return;
   1929 		}
   1930 
   1931 		require_once $abstraction_file;
   1932 	}
   1933 	$method = "WP_Filesystem_$method";
   1934 
   1935 	$wp_filesystem = new $method( $args );
   1936 
   1937 	/*
   1938 	 * Define the timeouts for the connections. Only available after the constructor is called
   1939 	 * to allow for per-transport overriding of the default.
   1940 	 */
   1941 	if ( ! defined( 'FS_CONNECT_TIMEOUT' ) ) {
   1942 		define( 'FS_CONNECT_TIMEOUT', 30 );
   1943 	}
   1944 	if ( ! defined( 'FS_TIMEOUT' ) ) {
   1945 		define( 'FS_TIMEOUT', 30 );
   1946 	}
   1947 
   1948 	if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
   1949 		return false;
   1950 	}
   1951 
   1952 	if ( ! $wp_filesystem->connect() ) {
   1953 		return false; // There was an error connecting to the server.
   1954 	}
   1955 
   1956 	// Set the permission constants if not already set.
   1957 	if ( ! defined( 'FS_CHMOD_DIR' ) ) {
   1958 		define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
   1959 	}
   1960 	if ( ! defined( 'FS_CHMOD_FILE' ) ) {
   1961 		define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
   1962 	}
   1963 
   1964 	return true;
   1965 }
   1966 
   1967 /**
   1968  * Determines which method to use for reading, writing, modifying, or deleting
   1969  * files on the filesystem.
   1970  *
   1971  * The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets
   1972  * (Via Sockets class, or `fsockopen()`). Valid values for these are: 'direct', 'ssh2',
   1973  * 'ftpext' or 'ftpsockets'.
   1974  *
   1975  * The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php`,
   1976  * or filtering via {@see 'filesystem_method'}.
   1977  *
   1978  * @link https://wordpress.org/support/article/editing-wp-config-php/#wordpress-upgrade-constants
   1979  *
   1980  * Plugins may define a custom transport handler, See WP_Filesystem().
   1981  *
   1982  * @since 2.5.0
   1983  *
   1984  * @global callable $_wp_filesystem_direct_method
   1985  *
   1986  * @param array  $args                         Optional. Connection details. Default empty array.
   1987  * @param string $context                      Optional. Full path to the directory that is tested
   1988  *                                             for being writable. Default empty.
   1989  * @param bool   $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
   1990  *                                             Default false.
   1991  * @return string The transport to use, see description for valid return values.
   1992  */
   1993 function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) {
   1994 	// Please ensure that this is either 'direct', 'ssh2', 'ftpext', or 'ftpsockets'.
   1995 	$method = defined( 'FS_METHOD' ) ? FS_METHOD : false;
   1996 
   1997 	if ( ! $context ) {
   1998 		$context = WP_CONTENT_DIR;
   1999 	}
   2000 
   2001 	// If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it.
   2002 	if ( WP_LANG_DIR === $context && ! is_dir( $context ) ) {
   2003 		$context = dirname( $context );
   2004 	}
   2005 
   2006 	$context = trailingslashit( $context );
   2007 
   2008 	if ( ! $method ) {
   2009 
   2010 		$temp_file_name = $context . 'temp-write-test-' . str_replace( '.', '-', uniqid( '', true ) );
   2011 		$temp_handle    = @fopen( $temp_file_name, 'w' );
   2012 		if ( $temp_handle ) {
   2013 
   2014 			// Attempt to determine the file owner of the WordPress files, and that of newly created files.
   2015 			$wp_file_owner   = false;
   2016 			$temp_file_owner = false;
   2017 			if ( function_exists( 'fileowner' ) ) {
   2018 				$wp_file_owner   = @fileowner( __FILE__ );
   2019 				$temp_file_owner = @fileowner( $temp_file_name );
   2020 			}
   2021 
   2022 			if ( false !== $wp_file_owner && $wp_file_owner === $temp_file_owner ) {
   2023 				/*
   2024 				 * WordPress is creating files as the same owner as the WordPress files,
   2025 				 * this means it's safe to modify & create new files via PHP.
   2026 				 */
   2027 				$method                                  = 'direct';
   2028 				$GLOBALS['_wp_filesystem_direct_method'] = 'file_owner';
   2029 			} elseif ( $allow_relaxed_file_ownership ) {
   2030 				/*
   2031 				 * The $context directory is writable, and $allow_relaxed_file_ownership is set,
   2032 				 * this means we can modify files safely in this directory.
   2033 				 * This mode doesn't create new files, only alter existing ones.
   2034 				 */
   2035 				$method                                  = 'direct';
   2036 				$GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership';
   2037 			}
   2038 
   2039 			fclose( $temp_handle );
   2040 			@unlink( $temp_file_name );
   2041 		}
   2042 	}
   2043 
   2044 	if ( ! $method && isset( $args['connection_type'] ) && 'ssh' === $args['connection_type'] && extension_loaded( 'ssh2' ) ) {
   2045 		$method = 'ssh2';
   2046 	}
   2047 	if ( ! $method && extension_loaded( 'ftp' ) ) {
   2048 		$method = 'ftpext';
   2049 	}
   2050 	if ( ! $method && ( extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) ) {
   2051 		$method = 'ftpsockets'; // Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread.
   2052 	}
   2053 
   2054 	/**
   2055 	 * Filters the filesystem method to use.
   2056 	 *
   2057 	 * @since 2.6.0
   2058 	 *
   2059 	 * @param string $method                       Filesystem method to return.
   2060 	 * @param array  $args                         An array of connection details for the method.
   2061 	 * @param string $context                      Full path to the directory that is tested for being writable.
   2062 	 * @param bool   $allow_relaxed_file_ownership Whether to allow Group/World writable.
   2063 	 */
   2064 	return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership );
   2065 }
   2066 
   2067 /**
   2068  * Displays a form to the user to request for their FTP/SSH details in order
   2069  * to connect to the filesystem.
   2070  *
   2071  * All chosen/entered details are saved, excluding the password.
   2072  *
   2073  * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467)
   2074  * to specify an alternate FTP/SSH port.
   2075  *
   2076  * Plugins may override this form by returning true|false via the {@see 'request_filesystem_credentials'} filter.
   2077  *
   2078  * @since 2.5.0
   2079  * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
   2080  *
   2081  * @global string $pagenow
   2082  *
   2083  * @param string        $form_post                    The URL to post the form to.
   2084  * @param string        $type                         Optional. Chosen type of filesystem. Default empty.
   2085  * @param bool|WP_Error $error                        Optional. Whether the current request has failed
   2086  *                                                    to connect, or an error object. Default false.
   2087  * @param string        $context                      Optional. Full path to the directory that is tested
   2088  *                                                    for being writable. Default empty.
   2089  * @param array         $extra_fields                 Optional. Extra `POST` fields to be checked
   2090  *                                                    for inclusion in the post. Default null.
   2091  * @param bool          $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
   2092  *                                                    Default false.
   2093  * @return bool|array True if no filesystem credentials are required,
   2094  *                    false if they are required but have not been provided,
   2095  *                    array of credentials if they are required and have been provided.
   2096  */
   2097 function request_filesystem_credentials( $form_post, $type = '', $error = false, $context = '', $extra_fields = null, $allow_relaxed_file_ownership = false ) {
   2098 	global $pagenow;
   2099 
   2100 	/**
   2101 	 * Filters the filesystem credentials.
   2102 	 *
   2103 	 * Returning anything other than an empty string will effectively short-circuit
   2104 	 * output of the filesystem credentials form, returning that value instead.
   2105 	 *
   2106 	 * A filter should return true if no filesystem credentials are required, false if they are required but have not been
   2107 	 * provided, or an array of credentials if they are required and have been provided.
   2108 	 *
   2109 	 * @since 2.5.0
   2110 	 * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
   2111 	 *
   2112 	 * @param mixed         $credentials                  Credentials to return instead. Default empty string.
   2113 	 * @param string        $form_post                    The URL to post the form to.
   2114 	 * @param string        $type                         Chosen type of filesystem.
   2115 	 * @param bool|WP_Error $error                        Whether the current request has failed to connect,
   2116 	 *                                                    or an error object.
   2117 	 * @param string        $context                      Full path to the directory that is tested for
   2118 	 *                                                    being writable.
   2119 	 * @param array         $extra_fields                 Extra POST fields.
   2120 	 * @param bool          $allow_relaxed_file_ownership Whether to allow Group/World writable.
   2121 	 */
   2122 	$req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership );
   2123 
   2124 	if ( '' !== $req_cred ) {
   2125 		return $req_cred;
   2126 	}
   2127 
   2128 	if ( empty( $type ) ) {
   2129 		$type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership );
   2130 	}
   2131 
   2132 	if ( 'direct' === $type ) {
   2133 		return true;
   2134 	}
   2135 
   2136 	if ( is_null( $extra_fields ) ) {
   2137 		$extra_fields = array( 'version', 'locale' );
   2138 	}
   2139 
   2140 	$credentials = get_option(
   2141 		'ftp_credentials',
   2142 		array(
   2143 			'hostname' => '',
   2144 			'username' => '',
   2145 		)
   2146 	);
   2147 
   2148 	$submitted_form = wp_unslash( $_POST );
   2149 
   2150 	// Verify nonce, or unset submitted form field values on failure.
   2151 	if ( ! isset( $_POST['_fs_nonce'] ) || ! wp_verify_nonce( $_POST['_fs_nonce'], 'filesystem-credentials' ) ) {
   2152 		unset(
   2153 			$submitted_form['hostname'],
   2154 			$submitted_form['username'],
   2155 			$submitted_form['password'],
   2156 			$submitted_form['public_key'],
   2157 			$submitted_form['private_key'],
   2158 			$submitted_form['connection_type']
   2159 		);
   2160 	}
   2161 
   2162 	$ftp_constants = array(
   2163 		'hostname'    => 'FTP_HOST',
   2164 		'username'    => 'FTP_USER',
   2165 		'password'    => 'FTP_PASS',
   2166 		'public_key'  => 'FTP_PUBKEY',
   2167 		'private_key' => 'FTP_PRIKEY',
   2168 	);
   2169 
   2170 	// If defined, set it to that. Else, if POST'd, set it to that. If not, set it to an empty string.
   2171 	// Otherwise, keep it as it previously was (saved details in option).
   2172 	foreach ( $ftp_constants as $key => $constant ) {
   2173 		if ( defined( $constant ) ) {
   2174 			$credentials[ $key ] = constant( $constant );
   2175 		} elseif ( ! empty( $submitted_form[ $key ] ) ) {
   2176 			$credentials[ $key ] = $submitted_form[ $key ];
   2177 		} elseif ( ! isset( $credentials[ $key ] ) ) {
   2178 			$credentials[ $key ] = '';
   2179 		}
   2180 	}
   2181 
   2182 	// Sanitize the hostname, some people might pass in odd data.
   2183 	$credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); // Strip any schemes off.
   2184 
   2185 	if ( strpos( $credentials['hostname'], ':' ) ) {
   2186 		list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 );
   2187 		if ( ! is_numeric( $credentials['port'] ) ) {
   2188 			unset( $credentials['port'] );
   2189 		}
   2190 	} else {
   2191 		unset( $credentials['port'] );
   2192 	}
   2193 
   2194 	if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' === FS_METHOD ) ) {
   2195 		$credentials['connection_type'] = 'ssh';
   2196 	} elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) { // Only the FTP Extension understands SSL.
   2197 		$credentials['connection_type'] = 'ftps';
   2198 	} elseif ( ! empty( $submitted_form['connection_type'] ) ) {
   2199 		$credentials['connection_type'] = $submitted_form['connection_type'];
   2200 	} elseif ( ! isset( $credentials['connection_type'] ) ) { // All else fails (and it's not defaulted to something else saved), default to FTP.
   2201 		$credentials['connection_type'] = 'ftp';
   2202 	}
   2203 
   2204 	if ( ! $error
   2205 		&& ( ! empty( $credentials['hostname'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['password'] )
   2206 			|| 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] )
   2207 		)
   2208 	) {
   2209 		$stored_credentials = $credentials;
   2210 
   2211 		if ( ! empty( $stored_credentials['port'] ) ) { // Save port as part of hostname to simplify above code.
   2212 			$stored_credentials['hostname'] .= ':' . $stored_credentials['port'];
   2213 		}
   2214 
   2215 		unset(
   2216 			$stored_credentials['password'],
   2217 			$stored_credentials['port'],
   2218 			$stored_credentials['private_key'],
   2219 			$stored_credentials['public_key']
   2220 		);
   2221 
   2222 		if ( ! wp_installing() ) {
   2223 			update_option( 'ftp_credentials', $stored_credentials );
   2224 		}
   2225 
   2226 		return $credentials;
   2227 	}
   2228 
   2229 	$hostname        = isset( $credentials['hostname'] ) ? $credentials['hostname'] : '';
   2230 	$username        = isset( $credentials['username'] ) ? $credentials['username'] : '';
   2231 	$public_key      = isset( $credentials['public_key'] ) ? $credentials['public_key'] : '';
   2232 	$private_key     = isset( $credentials['private_key'] ) ? $credentials['private_key'] : '';
   2233 	$port            = isset( $credentials['port'] ) ? $credentials['port'] : '';
   2234 	$connection_type = isset( $credentials['connection_type'] ) ? $credentials['connection_type'] : '';
   2235 
   2236 	if ( $error ) {
   2237 		$error_string = __( '<strong>Error</strong>: Could not connect to the server. Please verify the settings are correct.' );
   2238 		if ( is_wp_error( $error ) ) {
   2239 			$error_string = esc_html( $error->get_error_message() );
   2240 		}
   2241 		echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
   2242 	}
   2243 
   2244 	$types = array();
   2245 	if ( extension_loaded( 'ftp' ) || extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) {
   2246 		$types['ftp'] = __( 'FTP' );
   2247 	}
   2248 	if ( extension_loaded( 'ftp' ) ) { // Only this supports FTPS.
   2249 		$types['ftps'] = __( 'FTPS (SSL)' );
   2250 	}
   2251 	if ( extension_loaded( 'ssh2' ) ) {
   2252 		$types['ssh'] = __( 'SSH2' );
   2253 	}
   2254 
   2255 	/**
   2256 	 * Filters the connection types to output to the filesystem credentials form.
   2257 	 *
   2258 	 * @since 2.9.0
   2259 	 * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
   2260 	 *
   2261 	 * @param string[]      $types       Types of connections.
   2262 	 * @param array         $credentials Credentials to connect with.
   2263 	 * @param string        $type        Chosen filesystem method.
   2264 	 * @param bool|WP_Error $error       Whether the current request has failed to connect,
   2265 	 *                                   or an error object.
   2266 	 * @param string        $context     Full path to the directory that is tested for being writable.
   2267 	 */
   2268 	$types = apply_filters( 'fs_ftp_connection_types', $types, $credentials, $type, $error, $context );
   2269 	?>
   2270 <form action="<?php echo esc_url( $form_post ); ?>" method="post">
   2271 <div id="request-filesystem-credentials-form" class="request-filesystem-credentials-form">
   2272 	<?php
   2273 	// Print a H1 heading in the FTP credentials modal dialog, default is a H2.
   2274 	$heading_tag = 'h2';
   2275 	if ( 'plugins.php' === $pagenow || 'plugin-install.php' === $pagenow ) {
   2276 		$heading_tag = 'h1';
   2277 	}
   2278 	echo "<$heading_tag id='request-filesystem-credentials-title'>" . __( 'Connection Information' ) . "</$heading_tag>";
   2279 	?>
   2280 <p id="request-filesystem-credentials-desc">
   2281 	<?php
   2282 	$label_user = __( 'Username' );
   2283 	$label_pass = __( 'Password' );
   2284 	_e( 'To perform the requested action, WordPress needs to access your web server.' );
   2285 	echo ' ';
   2286 	if ( ( isset( $types['ftp'] ) || isset( $types['ftps'] ) ) ) {
   2287 		if ( isset( $types['ssh'] ) ) {
   2288 			_e( 'Please enter your FTP or SSH credentials to proceed.' );
   2289 			$label_user = __( 'FTP/SSH Username' );
   2290 			$label_pass = __( 'FTP/SSH Password' );
   2291 		} else {
   2292 			_e( 'Please enter your FTP credentials to proceed.' );
   2293 			$label_user = __( 'FTP Username' );
   2294 			$label_pass = __( 'FTP Password' );
   2295 		}
   2296 		echo ' ';
   2297 	}
   2298 	_e( 'If you do not remember your credentials, you should contact your web host.' );
   2299 
   2300 	$hostname_value = esc_attr( $hostname );
   2301 	if ( ! empty( $port ) ) {
   2302 		$hostname_value .= ":$port";
   2303 	}
   2304 
   2305 	$password_value = '';
   2306 	if ( defined( 'FTP_PASS' ) ) {
   2307 		$password_value = '*****';
   2308 	}
   2309 	?>
   2310 </p>
   2311 <label for="hostname">
   2312 	<span class="field-title"><?php _e( 'Hostname' ); ?></span>
   2313 	<input name="hostname" type="text" id="hostname" aria-describedby="request-filesystem-credentials-desc" class="code" placeholder="<?php esc_attr_e( 'example: www.wordpress.org' ); ?>" value="<?php echo $hostname_value; ?>"<?php disabled( defined( 'FTP_HOST' ) ); ?> />
   2314 </label>
   2315 <div class="ftp-username">
   2316 	<label for="username">
   2317 		<span class="field-title"><?php echo $label_user; ?></span>
   2318 		<input name="username" type="text" id="username" value="<?php echo esc_attr( $username ); ?>"<?php disabled( defined( 'FTP_USER' ) ); ?> />
   2319 	</label>
   2320 </div>
   2321 <div class="ftp-password">
   2322 	<label for="password">
   2323 		<span class="field-title"><?php echo $label_pass; ?></span>
   2324 		<input name="password" type="password" id="password" value="<?php echo $password_value; ?>"<?php disabled( defined( 'FTP_PASS' ) ); ?> />
   2325 		<?php
   2326 		if ( ! defined( 'FTP_PASS' ) ) {
   2327 			_e( 'This password will not be stored on the server.' );}
   2328 		?>
   2329 	</label>
   2330 </div>
   2331 <fieldset>
   2332 <legend><?php _e( 'Connection Type' ); ?></legend>
   2333 	<?php
   2334 	$disabled = disabled( ( defined( 'FTP_SSL' ) && FTP_SSL ) || ( defined( 'FTP_SSH' ) && FTP_SSH ), true, false );
   2335 	foreach ( $types as $name => $text ) :
   2336 		?>
   2337 	<label for="<?php echo esc_attr( $name ); ?>">
   2338 		<input type="radio" name="connection_type" id="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $name ); ?>" <?php checked( $name, $connection_type ); ?> <?php echo $disabled; ?> />
   2339 		<?php echo $text; ?>
   2340 	</label>
   2341 		<?php
   2342 	endforeach;
   2343 	?>
   2344 </fieldset>
   2345 	<?php
   2346 	if ( isset( $types['ssh'] ) ) {
   2347 		$hidden_class = '';
   2348 		if ( 'ssh' !== $connection_type || empty( $connection_type ) ) {
   2349 			$hidden_class = ' class="hidden"';
   2350 		}
   2351 		?>
   2352 <fieldset id="ssh-keys"<?php echo $hidden_class; ?>>
   2353 <legend><?php _e( 'Authentication Keys' ); ?></legend>
   2354 <label for="public_key">
   2355 	<span class="field-title"><?php _e( 'Public Key:' ); ?></span>
   2356 	<input name="public_key" type="text" id="public_key" aria-describedby="auth-keys-desc" value="<?php echo esc_attr( $public_key ); ?>"<?php disabled( defined( 'FTP_PUBKEY' ) ); ?> />
   2357 </label>
   2358 <label for="private_key">
   2359 	<span class="field-title"><?php _e( 'Private Key:' ); ?></span>
   2360 	<input name="private_key" type="text" id="private_key" value="<?php echo esc_attr( $private_key ); ?>"<?php disabled( defined( 'FTP_PRIKEY' ) ); ?> />
   2361 </label>
   2362 <p id="auth-keys-desc"><?php _e( 'Enter the location on the server where the public and private keys are located. If a passphrase is needed, enter that in the password field above.' ); ?></p>
   2363 </fieldset>
   2364 		<?php
   2365 	}
   2366 
   2367 	foreach ( (array) $extra_fields as $field ) {
   2368 		if ( isset( $submitted_form[ $field ] ) ) {
   2369 			echo '<input type="hidden" name="' . esc_attr( $field ) . '" value="' . esc_attr( $submitted_form[ $field ] ) . '" />';
   2370 		}
   2371 	}
   2372 
   2373 	// Make sure the `submit_button()` function is available during the REST API call
   2374 	// from WP_Site_Health_Auto_Updates::test_check_wp_filesystem_method().
   2375 	if ( ! function_exists( 'submit_button' ) ) {
   2376 		require_once ABSPATH . '/wp-admin/includes/template.php';
   2377 	}
   2378 	?>
   2379 	<p class="request-filesystem-credentials-action-buttons">
   2380 		<?php wp_nonce_field( 'filesystem-credentials', '_fs_nonce', false, true ); ?>
   2381 		<button class="button cancel-button" data-js-action="close" type="button"><?php _e( 'Cancel' ); ?></button>
   2382 		<?php submit_button( __( 'Proceed' ), '', 'upgrade', false ); ?>
   2383 	</p>
   2384 </div>
   2385 </form>
   2386 	<?php
   2387 	return false;
   2388 }
   2389 
   2390 /**
   2391  * Prints the filesystem credentials modal when needed.
   2392  *
   2393  * @since 4.2.0
   2394  */
   2395 function wp_print_request_filesystem_credentials_modal() {
   2396 	$filesystem_method = get_filesystem_method();
   2397 
   2398 	ob_start();
   2399 	$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
   2400 	ob_end_clean();
   2401 
   2402 	$request_filesystem_credentials = ( 'direct' !== $filesystem_method && ! $filesystem_credentials_are_stored );
   2403 	if ( ! $request_filesystem_credentials ) {
   2404 		return;
   2405 	}
   2406 	?>
   2407 	<div id="request-filesystem-credentials-dialog" class="notification-dialog-wrap request-filesystem-credentials-dialog">
   2408 		<div class="notification-dialog-background"></div>
   2409 		<div class="notification-dialog" role="dialog" aria-labelledby="request-filesystem-credentials-title" tabindex="0">
   2410 			<div class="request-filesystem-credentials-dialog-content">
   2411 				<?php request_filesystem_credentials( site_url() ); ?>
   2412 			</div>
   2413 		</div>
   2414 	</div>
   2415 	<?php
   2416 }
   2417 
   2418 /**
   2419  * Attempts to clear the opcode cache for an individual PHP file.
   2420  *
   2421  * This function can be called safely without having to check the file extension
   2422  * or availability of the OPcache extension.
   2423  *
   2424  * Whether or not invalidation is possible is cached to improve performance.
   2425  *
   2426  * @since 5.5.0
   2427  *
   2428  * @link https://www.php.net/manual/en/function.opcache-invalidate.php
   2429  *
   2430  * @param string $filepath Path to the file, including extension, for which the opcode cache is to be cleared.
   2431  * @param bool   $force    Invalidate even if the modification time is not newer than the file in cache.
   2432  *                         Default false.
   2433  * @return bool True if opcache was invalidated for `$filepath`, or there was nothing to invalidate.
   2434  *              False if opcache invalidation is not available, or is disabled via filter.
   2435  */
   2436 function wp_opcache_invalidate( $filepath, $force = false ) {
   2437 	static $can_invalidate = null;
   2438 
   2439 	/*
   2440 	 * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value.
   2441 	 *
   2442 	 * First, check to see if the function is available to call, then if the host has restricted
   2443 	 * the ability to run the function to avoid a PHP warning.
   2444 	 *
   2445 	 * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`.
   2446 	 *
   2447 	 * If the host has this set, check whether the path in `opcache.restrict_api` matches
   2448 	 * the beginning of the path of the origin file.
   2449 	 *
   2450 	 * `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but `realpath()`
   2451 	 * is necessary because `SCRIPT_FILENAME` can be a relative path when run from CLI.
   2452 	 *
   2453 	 * For more details, see:
   2454 	 * - https://www.php.net/manual/en/opcache.configuration.php
   2455 	 * - https://www.php.net/manual/en/reserved.variables.server.php
   2456 	 * - https://core.trac.wordpress.org/ticket/36455
   2457 	 */
   2458 	if ( null === $can_invalidate
   2459 		&& function_exists( 'opcache_invalidate' )
   2460 		&& ( ! ini_get( 'opcache.restrict_api' )
   2461 			|| stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 )
   2462 	) {
   2463 		$can_invalidate = true;
   2464 	}
   2465 
   2466 	// If invalidation is not available, return early.
   2467 	if ( ! $can_invalidate ) {
   2468 		return false;
   2469 	}
   2470 
   2471 	// Verify that file to be invalidated has a PHP extension.
   2472 	if ( '.php' !== strtolower( substr( $filepath, -4 ) ) ) {
   2473 		return false;
   2474 	}
   2475 
   2476 	/**
   2477 	 * Filters whether to invalidate a file from the opcode cache.
   2478 	 *
   2479 	 * @since 5.5.0
   2480 	 *
   2481 	 * @param bool   $will_invalidate Whether WordPress will invalidate `$filepath`. Default true.
   2482 	 * @param string $filepath        The path to the PHP file to invalidate.
   2483 	 */
   2484 	if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) {
   2485 		return opcache_invalidate( $filepath, $force );
   2486 	}
   2487 
   2488 	return false;
   2489 }