Skip to content

Commit

Permalink
Backup: Write helper script to ABSPATH by default, just like we did b…
Browse files Browse the repository at this point in the history
…efore (#35508)

* Write helper script to ABSPATH, just like we did before

Writing to "wp-content/" has caused some issues, as some hosts don't allow PHP code from "wp-content/" to be accessed directly.

* Bump class namespace version

#34739

* changelog

* Make the test expect the right order of install locations

* Fix up project versions

* Add proxy `Package_Version` classes with different namespaces

* Revert "Add proxy `Package_Version` classes with different namespaces"

This reverts commit abd558f.

* Don't use namespace versioning in Package_Version, add <...>\V0001\Package_Version proxy

* Use correct path to Package_Version in the test

* Fix package name in a test

* Move test to Automattic\Jetpack\Backup namespace

* Move Package_Version's compatibility class to separate file

* Add (possibly) missing import

* Apply the same behavior to Automattic\Jetpack\Transport_Helper\Package_Version too

* Fix up project/package versions again

* Fix transport-helper's actions.php to point to the right Package_Version
  • Loading branch information
pypt authored and spsiddarthan committed Feb 15, 2024
1 parent 86e24d1 commit feb3521
Show file tree
Hide file tree
Showing 35 changed files with 142 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Write helper script to ABSPATH by default, just like we did before
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

use Exception;
use WP_Error;
Expand Down Expand Up @@ -123,9 +123,21 @@ public function install_locations() {

$locations = array();

// Prioritize trying to write to "wp-content/" and "wp-content/uploads/" first, because those locations are
// expected to be writable more often (unlike ABSPATH), and ABSPATH on some setups might have a weird value
// which doesn't point to document root.
// Prioritize ABSPATH first, because even though ABSPATH constant's value might be weird sometimes, it's the
// path where the PHP scripts will be most likely be able to get executed.

try {
if ( Throw_On_Errors::t_is_dir( ABSPATH ) ) {
$abspath_dir = Throw_On_Errors::t_realpath( ABSPATH );
$locations[ $abspath_dir ] = $abspath_url;
}
} catch ( Exception $exception ) {
$locations[ ABSPATH ] = new WP_Error(
'abspath_missing',
'Unable to access WordPress root "' . ABSPATH . '": ' . $exception->getMessage(),
array( 'status' => 500 )
);
}

try {
if ( Throw_On_Errors::t_is_dir( WP_CONTENT_DIR ) ) {
Expand Down Expand Up @@ -180,19 +192,6 @@ public function install_locations() {
);
}

try {
if ( Throw_On_Errors::t_is_dir( ABSPATH ) ) {
$abspath_dir = Throw_On_Errors::t_realpath( ABSPATH );
$locations[ $abspath_dir ] = $abspath_url;
}
} catch ( Exception $exception ) {
$locations[ ABSPATH ] = new WP_Error(
'abspath_missing',
'Unable to access WordPress root "' . ABSPATH . '": ' . $exception->getMessage(),
array( 'status' => 500 )
);
}

return $locations;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

/**
* Manage installation, deletion and cleanup of Helper Scripts to assist with backing up Jetpack Sites.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

use Exception;
use Throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

use WorDBless\BaseTestCase;
use WP_Error;
Expand Down Expand Up @@ -113,17 +113,17 @@ public function test_default_install_locations() {
$this->assertCount( 3, $default_install_locations );

$paths = array_keys( $default_install_locations );
$this->assertStringEndsWith( '/wordpress/wp-content', $paths[0] );
$this->assertStringEndsWith( '/wordpress/wp-content/uploads', $paths[1] );
$this->assertStringEndsWith( '/wordpress', $paths[2] );
$this->assertStringEndsWith( '/wordpress', $paths[0] );
$this->assertStringEndsWith( '/wordpress/wp-content', $paths[1] );
$this->assertStringEndsWith( '/wordpress/wp-content/uploads', $paths[2] );

$site_url = get_site_url();
$urls = array_values( $default_install_locations );
$this->assertSame(
array(
$site_url,
"$site_url/wp-content",
"$site_url/wp-content/uploads",
$site_url,
),
$urls
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

use Exception;
use PHPUnit\Framework\TestCase;
Expand Down
6 changes: 3 additions & 3 deletions projects/packages/backup/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
}

// Clean up expired Helper Scripts from a scheduled event.
$add_action( 'jetpack_backup_cleanup_helper_scripts', array( 'Automattic\\Jetpack\\Backup\\V0001\\Helper_Script_Manager', 'cleanup_expired_helper_scripts' ) );
$add_action( 'jetpack_backup_cleanup_helper_scripts', array( 'Automattic\\Jetpack\\Backup\\V0002\\Helper_Script_Manager', 'cleanup_expired_helper_scripts' ) );

// Register REST routes.
$add_action( 'rest_api_init', array( 'Automattic\\Jetpack\\Backup\\V0001\\REST_Controller', 'register_rest_routes' ) );
$add_action( 'rest_api_init', array( 'Automattic\\Jetpack\\Backup\\V0002\\REST_Controller', 'register_rest_routes' ) );

// Set up package version hook.
$add_filter( 'jetpack_package_versions', 'Automattic\\Jetpack\\Backup\\V0001\\Package_Version::send_package_version_to_tracker' );
$add_filter( 'jetpack_package_versions', 'Automattic\\Jetpack\\Backup\\Package_Version::send_package_version_to_tracker' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Write helper script to ABSPATH by default, just like we did before
2 changes: 1 addition & 1 deletion projects/packages/backup/src/class-initial-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

use Automattic\Jetpack\Connection\Plugin_Storage as Connection_Plugin_Storage;
use Automattic\Jetpack\Status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

use function get_option;
use function update_option;
Expand Down
4 changes: 2 additions & 2 deletions projects/packages/backup/src/class-jetpack-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

use Automattic\Jetpack\Admin_UI\Admin_Menu;
use Automattic\Jetpack\Assets;
use Automattic\Jetpack\Backup\V0001\Initial_State as Backup_Initial_State;
use Automattic\Jetpack\Backup\V0002\Initial_State as Backup_Initial_State;
use Automattic\Jetpack\Config;
use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
Expand Down
23 changes: 23 additions & 0 deletions projects/packages/backup/src/class-package-version-compat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* The Package_Version class's compatibility shim.
*
* @package automattic/jetpack-backup
*/

// Do *not* update the "V0001" namespace version on changes.
namespace Automattic\Jetpack\Backup\V0001;

/**
* Package_Version proxy class to accommodate upgrades from plugin version 2.4.
*
* Backup plugin version 2.4 had a versioned class defined ("Automattic\Jetpack\Backup\V0001\Package_Version"), so
* the "jetpack_package_versions" filter will try to look for the class with this namespace + name in the newer
* plugin's code.
*/
class Package_Version {
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
public static function send_package_version_to_tracker( $package_versions ) {
return \Automattic\Jetpack\Backup\Package_Version::send_package_version_to_tracker( $package_versions );
}
}
13 changes: 7 additions & 6 deletions projects/packages/backup/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
* @package automattic/jetpack-backup
*/

// After changing this file, consider increasing the version number ("VXXX") in all the files using this namespace, in
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup;

/**
* The Package_Version class.
*
* Does *not* use namespaced versioning ("VXXXX") because send_package_version_to_tracker() is used as a
* "jetpack_package_versions" filter, and said filter gets run during a plugin upgrade, so it always expects to
* find the "Package_Version" class with the same namespace, name, and interface.
*/
class Package_Version {

const PACKAGE_VERSION = '3.1.3';
const PACKAGE_VERSION = '3.1.4-alpha';

const PACKAGE_SLUG = 'backup';

Expand All @@ -29,6 +29,7 @@ class Package_Version {
*/
public static function send_package_version_to_tracker( $package_versions ) {
$package_versions[ self::PACKAGE_SLUG ] = self::PACKAGE_VERSION;

return $package_versions;
}
}
2 changes: 1 addition & 1 deletion projects/packages/backup/src/class-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Rest_Authentication;
Expand Down
7 changes: 2 additions & 5 deletions projects/packages/backup/tests/php/test-package-version.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName

// After changing this file, consider increasing the version number ("VXXX") in all the files using this namespace, in
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup;

use PHPUnit\Framework\TestCase;
use function add_filter;
use function apply_filters;

/**
* Unit tests for the Package_Version class.
Expand Down
4 changes: 2 additions & 2 deletions projects/packages/backup/tests/php/test-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -65,7 +65,7 @@ public function set_up() {
wp_set_current_user( 0 );

// Register REST routes.
add_action( 'rest_api_init', array( 'Automattic\\Jetpack\\Backup\\V0001\\REST_Controller', 'register_rest_routes' ) );
add_action( 'rest_api_init', array( 'Automattic\\Jetpack\\Backup\\V0002\\REST_Controller', 'register_rest_routes' ) );

do_action( 'rest_api_init' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Backup\V0001;
namespace Automattic\Jetpack\Backup\V0002;

use PHPUnit\Framework\TestCase;

Expand Down
6 changes: 3 additions & 3 deletions projects/packages/transport-helper/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
}

// Clean up expired Jetpack Helper Scripts from a scheduled event.
$add_action( 'jetpack_cleanup_helper_scripts', array( 'Automattic\\Jetpack\\Backup\\V0001\\Helper_Script_Manager', 'cleanup_expired_helper_scripts' ) );
$add_action( 'jetpack_cleanup_helper_scripts', array( 'Automattic\\Jetpack\\Backup\\V0002\\Helper_Script_Manager', 'cleanup_expired_helper_scripts' ) );

// Register REST routes.
$add_action( 'rest_api_init', array( 'Automattic\\Jetpack\\Transport_Helper\\V0001\\REST_Controller', 'register_rest_routes' ) );
$add_action( 'rest_api_init', array( 'Automattic\\Jetpack\\Transport_Helper\\V0002\\REST_Controller', 'register_rest_routes' ) );

// Set up package version hook.
$add_filter( 'jetpack_package_versions', 'Automattic\\Jetpack\\Transport_Helper\\V0001\\Package_Version::send_package_version_to_tracker' );
$add_filter( 'jetpack_package_versions', 'Automattic\\Jetpack\\Transport_Helper\\Package_Version::send_package_version_to_tracker' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Write helper script to ABSPATH by default, just like we did before
2 changes: 1 addition & 1 deletion projects/packages/transport-helper/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-transport-helper",
"version": "0.2.0",
"version": "0.2.1-alpha",
"description": "Package to help transport server communication",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/transport-helper/#readme",
"bugs": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* The Package_Version class's compatibility shim.
*
* @package automattic/jetpack-transport-helper
*/

// Do *not* update the "V0001" namespace version on changes.
namespace Automattic\Jetpack\Transport_Helper\V0001;

/**
* Package_Version proxy class to accommodate upgrades from plugin version 2.4.
*
* Backup plugin version 2.4 had a versioned class defined
* ("Automattic\Jetpack\Transport_Helper\V0001\Package_Version"), so the "jetpack_package_versions" filter will try to
* look for the class with this namespace + name in the newer plugin's code.
*/
class Package_Version {
// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
public static function send_package_version_to_tracker( $package_versions ) {
return \Automattic\Jetpack\Transport_Helper\Package_Version::send_package_version_to_tracker(
$package_versions
);
}
}
13 changes: 7 additions & 6 deletions projects/packages/transport-helper/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
* @package automattic/jetpack-transport-helper
*/

// After changing this file, consider increasing the version number ("VXXX") in all the files using this namespace, in
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Transport_Helper\V0001;
namespace Automattic\Jetpack\Transport_Helper;

/**
* The Package_Version class.
*
* Does *not* use namespaced versioning ("VXXXX") because send_package_version_to_tracker() is used as a
* "jetpack_package_versions" filter, and said filter gets run during a plugin upgrade, so it always expects to
* find the "Package_Version" class with the same namespace, name, and interface.
*/
class Package_Version {

const PACKAGE_VERSION = '0.2.0';
const PACKAGE_VERSION = '0.2.1-alpha';

const PACKAGE_SLUG = 'transport-helper';

Expand All @@ -29,6 +29,7 @@ class Package_Version {
*/
public static function send_package_version_to_tracker( $package_versions ) {
$package_versions[ self::PACKAGE_SLUG ] = self::PACKAGE_VERSION;

return $package_versions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide
// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin
// are installed, or in some other cases).
namespace Automattic\Jetpack\Transport_Helper\V0001;
namespace Automattic\Jetpack\Transport_Helper\V0002;

use Automattic\Jetpack\Backup\V0001\Helper_Script_Manager;
use Automattic\Jetpack\Backup\V0002\Helper_Script_Manager;
use Automattic\Jetpack\Connection\Rest_Authentication;
use WP_Error;
use WP_REST_Request;
Expand Down
Loading

0 comments on commit feb3521

Please sign in to comment.