|
|
|
@ -49,6 +49,11 @@ class Helper_Script_Manager {
|
|
|
|
|
// Replace '[wp_path]' in the Helper Script with the WordPress installation location. Allows the Helper Script to find WordPress.
|
|
|
|
|
$script_body = str_replace( '[wp_path]', addslashes( ABSPATH ), $script_body );
|
|
|
|
|
|
|
|
|
|
$wp_filesystem = self::get_wp_filesystem();
|
|
|
|
|
if ( ! $wp_filesystem ) {
|
|
|
|
|
return new \WP_Error( 'install_failed', 'Failed to install Helper Script' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a jetpack-temp directory for the Helper Script.
|
|
|
|
|
$temp_directory = self::create_temp_directory();
|
|
|
|
|
if ( \is_wp_error( $temp_directory ) ) {
|
|
|
|
@ -62,11 +67,11 @@ class Helper_Script_Manager {
|
|
|
|
|
$file_name = 'jp-helper-' . $file_key . '.php';
|
|
|
|
|
$file_path = trailingslashit( $temp_directory['path'] ) . $file_name;
|
|
|
|
|
|
|
|
|
|
if ( ! file_exists( $file_path ) ) {
|
|
|
|
|
if ( ! $wp_filesystem->exists( $file_path ) ) {
|
|
|
|
|
// Attempt to write helper script.
|
|
|
|
|
if ( ! self::put_contents( $file_path, $script_body ) ) {
|
|
|
|
|
if ( file_exists( $file_path ) ) {
|
|
|
|
|
unlink( $file_path );
|
|
|
|
|
if ( $wp_filesystem->exists( $file_path ) ) {
|
|
|
|
|
$wp_filesystem->delete( $file_path );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
@ -96,7 +101,12 @@ class Helper_Script_Manager {
|
|
|
|
|
* @return boolean True if the file is deleted (or does not exist).
|
|
|
|
|
*/
|
|
|
|
|
public static function delete_helper_script( $path ) {
|
|
|
|
|
if ( ! file_exists( $path ) ) {
|
|
|
|
|
$wp_filesystem = self::get_wp_filesystem();
|
|
|
|
|
if ( ! $wp_filesystem ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! $wp_filesystem->exists( $path ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -105,7 +115,7 @@ class Helper_Script_Manager {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return unlink( $path );
|
|
|
|
|
return $wp_filesystem->delete( $path );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -138,16 +148,21 @@ class Helper_Script_Manager {
|
|
|
|
|
* @param int|null $expiry_time If specified, only delete scripts older than $expiry_time.
|
|
|
|
|
*/
|
|
|
|
|
public static function cleanup_helper_scripts( $expiry_time = null ) {
|
|
|
|
|
$wp_filesystem = self::get_wp_filesystem();
|
|
|
|
|
if ( ! $wp_filesystem ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( self::get_install_locations() as $directory => $url ) {
|
|
|
|
|
$temp_dir = trailingslashit( $directory ) . self::TEMP_DIRECTORY;
|
|
|
|
|
|
|
|
|
|
if ( is_dir( $temp_dir ) ) {
|
|
|
|
|
if ( $wp_filesystem->is_dir( $temp_dir ) ) {
|
|
|
|
|
// Find expired helper scripts and delete them.
|
|
|
|
|
$helper_scripts = glob( trailingslashit( $temp_dir ) . 'jp-helper-*.php' );
|
|
|
|
|
$helper_scripts = $wp_filesystem->dirlist( $temp_dir );
|
|
|
|
|
if ( is_array( $helper_scripts ) ) {
|
|
|
|
|
foreach ( $helper_scripts as $filename ) {
|
|
|
|
|
if ( null === $expiry_time || filemtime( $filename ) < $expiry_time ) {
|
|
|
|
|
self::delete_helper_script( $filename );
|
|
|
|
|
foreach ( $helper_scripts as $entry ) {
|
|
|
|
|
if ( preg_match( '/^jp-helper-*\.php$/', $entry['name'] ) && ( null === $expiry_time || $entry['lastmodunix'] < $expiry_time ) ) {
|
|
|
|
|
self::delete_helper_script( trailingslashit( $temp_dir ) . $entry['name'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -168,14 +183,18 @@ class Helper_Script_Manager {
|
|
|
|
|
* @return boolean True if the directory is deleted
|
|
|
|
|
*/
|
|
|
|
|
private static function delete_empty_helper_directory( $dir ) {
|
|
|
|
|
if ( ! is_dir( $dir ) ) {
|
|
|
|
|
$wp_filesystem = self::get_wp_filesystem();
|
|
|
|
|
if ( ! $wp_filesystem ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! $wp_filesystem->is_dir( $dir ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tally the files in the target directory, and reject if there are too many.
|
|
|
|
|
$glob_path = trailingslashit( $dir ) . '*';
|
|
|
|
|
$dir_contents = glob( $glob_path );
|
|
|
|
|
if ( count( $dir_contents ) > 2 ) {
|
|
|
|
|
$dir_contents = $wp_filesystem->dirlist( $dir );
|
|
|
|
|
if ( $dir_contents === false || count( $dir_contents ) > 2 ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -185,8 +204,9 @@ class Helper_Script_Manager {
|
|
|
|
|
'index.php' => self::INDEX_FILE,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach ( $dir_contents as $path ) {
|
|
|
|
|
$basename = basename( $path );
|
|
|
|
|
foreach ( $dir_contents as $entry ) {
|
|
|
|
|
$basename = $entry['name'];
|
|
|
|
|
$path = trailingslashit( $dir ) . $basename;
|
|
|
|
|
if ( ! isset( $allowed_files[ $basename ] ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -196,14 +216,15 @@ class Helper_Script_Manager {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! unlink( $path ) ) {
|
|
|
|
|
if ( ! $wp_filesystem->delete( $path ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the directory is now empty, delete it.
|
|
|
|
|
if ( count( glob( $glob_path ) ) === 0 ) {
|
|
|
|
|
return rmdir( $dir );
|
|
|
|
|
$dir_contents = $wp_filesystem->dirlist( $dir );
|
|
|
|
|
if ( $dir_contents === false || count( $dir_contents ) === 0 ) {
|
|
|
|
|
return $wp_filesystem->rmdir( $dir );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
@ -218,16 +239,21 @@ class Helper_Script_Manager {
|
|
|
|
|
* @return WP_Error|array Array containing the url and path of the temp directory if successful, WP_Error if not.
|
|
|
|
|
*/
|
|
|
|
|
private static function create_temp_directory() {
|
|
|
|
|
$wp_filesystem = self::get_wp_filesystem();
|
|
|
|
|
if ( ! $wp_filesystem ) {
|
|
|
|
|
return new \WP_Error( 'temp_directory', 'Failed to create jetpack-temp directory' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( self::get_install_locations() as $directory => $url ) {
|
|
|
|
|
// Check if the install location is writeable.
|
|
|
|
|
if ( ! is_writeable( $directory ) ) {
|
|
|
|
|
if ( ! $wp_filesystem->is_writable( $directory ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create if one doesn't already exist.
|
|
|
|
|
$temp_dir = trailingslashit( $directory ) . self::TEMP_DIRECTORY;
|
|
|
|
|
if ( ! is_dir( $temp_dir ) ) {
|
|
|
|
|
if ( ! mkdir( $temp_dir ) ) {
|
|
|
|
|
if ( ! $wp_filesystem->is_dir( $temp_dir ) ) {
|
|
|
|
|
if ( ! $wp_filesystem->mkdir( $temp_dir ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -271,13 +297,8 @@ class Helper_Script_Manager {
|
|
|
|
|
* @return boolean True if successfully written.
|
|
|
|
|
*/
|
|
|
|
|
private static function put_contents( $file_path, $contents ) {
|
|
|
|
|
global $wp_filesystem;
|
|
|
|
|
|
|
|
|
|
if ( ! function_exists( '\\WP_Filesystem' ) ) {
|
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/file.php';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! \WP_Filesystem() ) {
|
|
|
|
|
$wp_filesystem = self::get_wp_filesystem();
|
|
|
|
|
if ( ! $wp_filesystem ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -295,13 +316,8 @@ class Helper_Script_Manager {
|
|
|
|
|
* @return boolean True if the file exists, is readable, and the header matches.
|
|
|
|
|
*/
|
|
|
|
|
private static function verify_file_header( $file_path, $expected_header ) {
|
|
|
|
|
global $wp_filesystem;
|
|
|
|
|
|
|
|
|
|
if ( ! function_exists( '\\WP_Filesystem' ) ) {
|
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/file.php';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! \WP_Filesystem() ) {
|
|
|
|
|
$wp_filesystem = self::get_wp_filesystem();
|
|
|
|
|
if ( ! $wp_filesystem ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -343,4 +359,25 @@ class Helper_Script_Manager {
|
|
|
|
|
return $install_locations;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the WP_Filesystem.
|
|
|
|
|
*
|
|
|
|
|
* @return \WP_Filesystem|null
|
|
|
|
|
*/
|
|
|
|
|
private static function get_wp_filesystem() {
|
|
|
|
|
global $wp_filesystem;
|
|
|
|
|
|
|
|
|
|
if ( ! $wp_filesystem ) {
|
|
|
|
|
if ( ! function_exists( '\\WP_Filesystem' ) ) {
|
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/file.php';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! \WP_Filesystem() ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $wp_filesystem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|