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