Skip to content

Commit

Permalink
Plugin Installer: remove excessive dependencies (#37430)
Browse files Browse the repository at this point in the history
* Extract the `is_current_request_activating_plugin_from_plugins_screen()` method from Plugin Installer to Status package.
* Remove explicit Plugin Installer package dependencies from standalone plugins (it's still required by My Jetpack though).
* Add explicit Status package dependency to standalone plugins (it's already loaded by other packages though).
  • Loading branch information
sergeymitr committed May 20, 2024
1 parent 282025b commit 1f28a65
Show file tree
Hide file tree
Showing 48 changed files with 281 additions and 181 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: deprecated

Extract the 'is_current_request_activating_plugin_from_plugins_screen' method into Status package.
5 changes: 3 additions & 2 deletions projects/packages/plugins-installer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"license": "GPL-2.0-or-later",
"require": {
"php": ">=7.0",
"automattic/jetpack-a8c-mc-stats": "@dev"
"automattic/jetpack-a8c-mc-stats": "@dev",
"automattic/jetpack-status": "@dev"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.1.0",
Expand Down Expand Up @@ -40,7 +41,7 @@
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-trunk": "0.3.x-dev"
"dev-trunk": "0.4.x-dev"
},
"mirror-repo": "Automattic/jetpack-plugins-installer",
"changelogger": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,53 +242,14 @@ public static function get_plugins() {
/**
* Determine if the current request is activating a plugin from the plugins page.
*
* @deprecated $$next-version$$
* @see Paths::is_current_request_activating_plugin_from_plugins_screen()
*
* @param string $plugin Plugin file path to check.
* @return bool
*/
public static function is_current_request_activating_plugin_from_plugins_screen( $plugin ) {
// Filter out common async request contexts
if (
wp_doing_ajax() ||
( defined( 'REST_REQUEST' ) && REST_REQUEST ) ||
( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
( defined( 'WP_CLI' ) && WP_CLI )
) {
return false;
}

if ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
$request_file = esc_url_raw( wp_unslash( $_SERVER['SCRIPT_NAME'] ) );
} elseif ( isset( $_SERVER['REQUEST_URI'] ) ) {
list( $request_file ) = explode( '?', esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
} else {
return false;
}

// Not the plugins page
if ( strpos( $request_file, 'wp-admin/plugins.php' ) === false ) {
return false;
}

// Same method to get the action as used by plugins.php
$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
$action = $wp_list_table->current_action();

// Not a singular activation
// This also means that if the plugin is activated as part of a group ( bulk activation ), this function will return false here.
if ( 'activate' !== $action ) {
return false;
}

// Check the nonce associated with the plugin activation
// We are not changing any data here, so this is not super necessary, it's just a best practice before using the form data from $_REQUEST.
check_admin_referer( 'activate-plugin_' . $plugin );

// Not the right plugin
$requested_plugin = isset( $_REQUEST['plugin'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['plugin'] ) ) : null;
if ( $requested_plugin !== $plugin ) {
return false;
}

return true;
_deprecated_function( __METHOD__, '$$next-version$$', 'Automattic\\Jetpack\\Paths::is_current_request_activating_plugin_from_plugins_screen()' );
return ( new Paths() )->is_current_request_activating_plugin_from_plugins_screen( $plugin );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add the 'is_current_request_activating_plugin_from_plugins_screen' method extracted from the Plugin Install package.
2 changes: 1 addition & 1 deletion projects/packages/status/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"link-template": "https://github.com/Automattic/jetpack-status/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "3.0.x-dev"
"dev-trunk": "3.1.x-dev"
},
"dependencies": {
"test-only": [
Expand Down
53 changes: 53 additions & 0 deletions projects/packages/status/src/class-paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,57 @@ public function admin_url( $args = null ) {
$url = add_query_arg( $args, admin_url( 'admin.php' ) );
return $url;
}

/**
* Determine if the current request is activating a plugin from the plugins page.
*
* @param string $plugin Plugin file path to check.
* @return bool
*/
public function is_current_request_activating_plugin_from_plugins_screen( $plugin ) {
// Filter out common async request contexts
if (
wp_doing_ajax() ||
( defined( 'REST_REQUEST' ) && REST_REQUEST ) ||
( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
( defined( 'WP_CLI' ) && WP_CLI )
) {
return false;
}

if ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
$request_file = esc_url_raw( wp_unslash( $_SERVER['SCRIPT_NAME'] ) );
} elseif ( isset( $_SERVER['REQUEST_URI'] ) ) {
list( $request_file ) = explode( '?', esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
} else {
return false;
}

// Not the plugins page
if ( strpos( $request_file, 'wp-admin/plugins.php' ) === false ) {
return false;
}

// Same method to get the action as used by plugins.php
$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
$action = $wp_list_table->current_action();

// Not a singular activation
// This also means that if the plugin is activated as part of a group ( bulk activation ), this function will return false here.
if ( 'activate' !== $action ) {
return false;
}

// Check the nonce associated with the plugin activation
// We are not changing any data here, so this is not super necessary, it's just a best practice before using the form data from $_REQUEST.
check_admin_referer( 'activate-plugin_' . $plugin );

// Not the right plugin
$requested_plugin = isset( $_REQUEST['plugin'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['plugin'] ) ) : null;
if ( $requested_plugin !== $plugin ) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Automattic for Agencies Client
* Plugin URI: https://wordpress.org/plugins/automattic-for-agencies-client
* Description: Securely connect your clients’ sites to the Automattic for Agencies Sites Dashboard. Manage your sites from one place and see what needs attention.
* Version: 0.1.1-alpha
* Version: 0.2.0-alpha
* Author: Automattic
* Author URI: https://jetpack.com/
* License: GPLv2 or later
Expand Down Expand Up @@ -107,7 +107,7 @@ function ( $actions ) {
function jetpack_starter_plugin_activation( $plugin ) {
if (
AUTOMATTIC_FOR_AGENCIES_CLIENT_ROOT_FILE_RELATIVE_PATH === $plugin &&
\Automattic\Jetpack\Plugins_Installer::is_current_request_activating_plugin_from_plugins_screen( AUTOMATTIC_FOR_AGENCIES_CLIENT_ROOT_FILE_RELATIVE_PATH )
( new \Automattic\Jetpack\Paths() )->is_current_request_activating_plugin_from_plugins_screen( AUTOMATTIC_FOR_AGENCIES_CLIENT_ROOT_FILE_RELATIVE_PATH )
) {
wp_safe_redirect( esc_url( admin_url( 'options-general.php?page=' . AUTOMATTIC_FOR_AGENCIES_CLIENT_SLUG ) ) );
exit;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: removed

Remove the Plugin Installer package dependency.
6 changes: 3 additions & 3 deletions projects/plugins/automattic-for-agencies-client/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"automattic/jetpack-connection": "@dev",
"automattic/jetpack-identity-crisis": "@dev",
"automattic/jetpack-plugin-deactivation": "@dev",
"automattic/jetpack-plugins-installer": "@dev",
"automattic/jetpack-sync": "@dev"
"automattic/jetpack-sync": "@dev",
"automattic/jetpack-status": "@dev"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.1.0",
Expand Down Expand Up @@ -73,6 +73,6 @@
"automattic/jetpack-autoloader": true,
"automattic/jetpack-composer-plugin": true
},
"autoloader-suffix": "c4802e05bbcf59fd3b6350e8d3e5482c_automattic_for_agencies_clientⓥ0_1_1_alpha"
"autoloader-suffix": "c4802e05bbcf59fd3b6350e8d3e5482c_automattic_for_agencies_clientⓥ0_2_0_alpha"
}
}
60 changes: 4 additions & 56 deletions projects/plugins/automattic-for-agencies-client/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Remove the explicit Plugin Install package dependency.
4 changes: 2 additions & 2 deletions projects/plugins/backup/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"automattic/jetpack-autoloader": "@dev",
"automattic/jetpack-backup": "@dev",
"automattic/jetpack-composer-plugin": "@dev",
"automattic/jetpack-plugins-installer": "@dev",
"automattic/jetpack-my-jetpack": "@dev"
"automattic/jetpack-my-jetpack": "@dev",
"automattic/jetpack-status": "@dev"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.1.0",
Expand Down
13 changes: 7 additions & 6 deletions projects/plugins/backup/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion projects/plugins/backup/jetpack-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function () {
function jetpack_backup_plugin_activation( $plugin ) {
if (
JETPACK_BACKUP_PLUGIN_ROOT_FILE_RELATIVE_PATH === $plugin &&
\Automattic\Jetpack\Plugins_Installer::is_current_request_activating_plugin_from_plugins_screen( JETPACK_BACKUP_PLUGIN_ROOT_FILE_RELATIVE_PATH )
( new \Automattic\Jetpack\Paths() )->is_current_request_activating_plugin_from_plugins_screen( JETPACK_BACKUP_PLUGIN_ROOT_FILE_RELATIVE_PATH )
) {
wp_safe_redirect( esc_url( admin_url( 'admin.php?page=jetpack-backup' ) ) );
exit;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Remove the explicit Plugin Install dependency.
2 changes: 1 addition & 1 deletion projects/plugins/boost/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"automattic/jetpack-image-cdn": "@dev",
"automattic/jetpack-my-jetpack": "@dev",
"automattic/jetpack-plugin-deactivation": "@dev",
"automattic/jetpack-plugins-installer": "@dev",
"automattic/jetpack-status": "@dev",
"automattic/jetpack-sync": "@dev",
"automattic/jetpack-wp-js-data-sync": "@dev",
"tedivm/jshrink": "1.4.0",
Expand Down

0 comments on commit 1f28a65

Please sign in to comment.