Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/Utilitie...
File: PluginInstaller.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Internal\Utilities;
[2] Fix | Delete
[3] Fix | Delete
use Automattic\WooCommerce\Internal\RegisterHooksInterface;
[4] Fix | Delete
use Automattic\WooCommerce\Utilities\{ PluginUtil, StringUtil };
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* This class allows installing a plugin programmatically.
[8] Fix | Delete
*
[9] Fix | Delete
* Information about plugins installed in that way will be stored in a 'woocommerce_autoinstalled_plugins' option,
[10] Fix | Delete
* and a notice will be shown under the plugin name in the plugins list indicating that it was automatically
[11] Fix | Delete
* installed (these notices can be disabled with the 'woocommerce_show_autoinstalled_plugin_notices' hook).
[12] Fix | Delete
*
[13] Fix | Delete
* Currently it's only possible to install new plugins, not to upgrade or reinstall already installed plugins.
[14] Fix | Delete
*
[15] Fix | Delete
* The 'upgrader_process_complete' hook is used to remove the autoinstall information from any plugin that is later
[16] Fix | Delete
* upgraded or reinstalled by any means other than the usage of this class.
[17] Fix | Delete
*/
[18] Fix | Delete
class PluginInstaller implements RegisterHooksInterface {
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Flag indicating that a plugin install is in progress, so the upgrader_process_complete hook must be ignored.
[22] Fix | Delete
*
[23] Fix | Delete
* @var bool
[24] Fix | Delete
*/
[25] Fix | Delete
private bool $installing_plugin = false;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Attach hooks used by the class.
[29] Fix | Delete
*/
[30] Fix | Delete
public function register() {
[31] Fix | Delete
add_action( 'after_plugin_row', array( $this, 'handle_plugin_list_rows' ), 10, 2 );
[32] Fix | Delete
add_action( 'upgrader_process_complete', array( $this, 'handle_upgrader_process_complete' ), 10, 2 );
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Programmatically installs a plugin. Upgrade/reinstall of already existing plugins is not supported.
[37] Fix | Delete
* The plugin source must be the WordPress.org plugins directory.
[38] Fix | Delete
*
[39] Fix | Delete
* $metadata can contain anything, but the following keys are recognized by the code that renders the notice
[40] Fix | Delete
* in the plugins list:
[41] Fix | Delete
*
[42] Fix | Delete
* - 'installed_by': defaults to 'WooCommerce' if not present.
[43] Fix | Delete
* - 'info_link': if present, a "More information" link will be included in the notice.
[44] Fix | Delete
*
[45] Fix | Delete
* If 'installed_by' is supplied and it's not 'WooCommerce' (case-insensitive), an exception will be thrown
[46] Fix | Delete
* if the code calling this method is not in a WooCommerce core file (in 'includes' or in 'src').
[47] Fix | Delete
*
[48] Fix | Delete
* Information about plugins successfully installed with this method will be kept in an option named
[49] Fix | Delete
* 'woocommerce_autoinstalled_plugins'. Keys will be the plugin name and values will be associative arrays
[50] Fix | Delete
* with these keys: 'plugin_name', 'version', 'date' and 'metadata' (same meaning as in the returned array).
[51] Fix | Delete
*
[52] Fix | Delete
* A log entry will be created with the result of the process and all the installer messages
[53] Fix | Delete
* (source: 'plugin_auto_installs'). In multisite this log entry will be created on each site.
[54] Fix | Delete
*
[55] Fix | Delete
* The returned array will contain the following (only 'install_ok' and 'messages' if the installation fails):
[56] Fix | Delete
*
[57] Fix | Delete
* - 'install_ok', a boolean.
[58] Fix | Delete
* - 'messages', all the messages generated by the installer.
[59] Fix | Delete
* - 'plugin_name', in the form of 'directory/file.php' (taken from the instance of PluginInstaller used).
[60] Fix | Delete
* - 'version', of the plugin that has been installed.
[61] Fix | Delete
* - 'date', ISO-formatted installation date.
[62] Fix | Delete
* - 'metadata', as supplied (except the 'plugin_name' key) and only if not empty.
[63] Fix | Delete
*
[64] Fix | Delete
* If the plugin is already in the process of being installed (can happen in multisite), the returned array
[65] Fix | Delete
* will contain only one key: 'already_installing', with a value of true.
[66] Fix | Delete
*
[67] Fix | Delete
* @param string $plugin_url URL or file path of the plugin to install.
[68] Fix | Delete
* @param array $metadata Metadata to store if the installation succeeds.
[69] Fix | Delete
* @return array Information about the installation result.
[70] Fix | Delete
* @throws \InvalidArgumentException Source doesn't start with 'https://downloads.wordpress.org/', or installer name is 'WooCommerce' but caller is not WooCommerce core code.
[71] Fix | Delete
*/
[72] Fix | Delete
public function install_plugin( string $plugin_url, array $metadata = array() ): array {
[73] Fix | Delete
$this->installing_plugin = true;
[74] Fix | Delete
[75] Fix | Delete
$plugins_being_installed = get_site_option( 'woocommerce_autoinstalling_plugins', array() );
[76] Fix | Delete
if ( in_array( $plugin_url, $plugins_being_installed, true ) ) {
[77] Fix | Delete
return array( 'already_installing' => true );
[78] Fix | Delete
}
[79] Fix | Delete
$plugins_being_installed[] = $plugin_url;
[80] Fix | Delete
update_site_option( 'woocommerce_autoinstalling_plugins', $plugins_being_installed );
[81] Fix | Delete
[82] Fix | Delete
try {
[83] Fix | Delete
return $this->install_plugin_core( $plugin_url, $metadata );
[84] Fix | Delete
} finally {
[85] Fix | Delete
$plugins_being_installed = array_diff( $plugins_being_installed, array( $plugin_url ) );
[86] Fix | Delete
if ( empty( $plugins_being_installed ) ) {
[87] Fix | Delete
delete_site_option( 'woocommerce_autoinstalling_plugins' );
[88] Fix | Delete
} else {
[89] Fix | Delete
update_site_option( 'woocommerce_autoinstalling_plugins', $plugins_being_installed );
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
$this->installing_plugin = false;
[93] Fix | Delete
}
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
/**
[97] Fix | Delete
* Core version of 'install_plugin' (it doesn't handle the $installing_plugin flag).
[98] Fix | Delete
*
[99] Fix | Delete
* @param string $plugin_url URL or file path of the plugin to install.
[100] Fix | Delete
* @param array $metadata Metadata to store if the installation succeeds.
[101] Fix | Delete
* @return array Information about the installation result.
[102] Fix | Delete
* @throws \InvalidArgumentException Source doesn't start with 'https://downloads.wordpress.org/', or installer name is 'WooCommerce' but caller is not WooCommerce core code.
[103] Fix | Delete
*/
[104] Fix | Delete
private function install_plugin_core( string $plugin_url, array $metadata ): array {
[105] Fix | Delete
if ( ! StringUtil::starts_with( $plugin_url, 'https://downloads.wordpress.org/', false ) ) {
[106] Fix | Delete
throw new \InvalidArgumentException( "Only installs from the WordPress.org plugins directory (plugin URL starting with 'https://downloads.wordpress.org/') are allowed." );
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
$installed_by = $metadata['installed_by'] ?? 'WooCommerce';
[110] Fix | Delete
if ( 0 === strcasecmp( 'WooCommerce', $installed_by ) ) {
[111] Fix | Delete
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
[112] Fix | Delete
$calling_file = StringUtil::normalize_local_path_slashes( debug_backtrace()[1]['file'] ?? '' ); // [1], not [0], because the immediate caller is the install_plugin method.
[113] Fix | Delete
if ( ! StringUtil::starts_with( $calling_file, StringUtil::normalize_local_path_slashes( WC_ABSPATH . 'includes/' ) ) && ! StringUtil::starts_with( $calling_file, StringUtil::normalize_local_path_slashes( WC_ABSPATH . 'src/' ) ) ) {
[114] Fix | Delete
throw new \InvalidArgumentException( "If the value of 'installed_by' is 'WooCommerce', the caller of the method must be a WooCommerce core class or function." );
[115] Fix | Delete
}
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
if ( ! class_exists( \Automatic_Upgrader_Skin::class ) ) {
[119] Fix | Delete
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';
[120] Fix | Delete
include_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php';
[121] Fix | Delete
}
[122] Fix | Delete
$skin = new \Automatic_Upgrader_Skin();
[123] Fix | Delete
[124] Fix | Delete
if ( ! class_exists( \Plugin_Upgrader::class ) ) {
[125] Fix | Delete
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
[126] Fix | Delete
}
[127] Fix | Delete
$upgrader = new \Plugin_Upgrader( $skin );
[128] Fix | Delete
[129] Fix | Delete
$install_ok = $upgrader->install( $plugin_url );
[130] Fix | Delete
[131] Fix | Delete
$result = array( 'messages' => $skin->get_upgrade_messages() );
[132] Fix | Delete
[133] Fix | Delete
if ( $install_ok ) {
[134] Fix | Delete
if ( ! function_exists( 'get_plugins' ) ) {
[135] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/plugin.php';
[136] Fix | Delete
}
[137] Fix | Delete
$plugin_name = $upgrader->plugin_info();
[138] Fix | Delete
$plugin_version = get_plugins()[ $plugin_name ]['Version'];
[139] Fix | Delete
[140] Fix | Delete
$result['plugin_name'] = $plugin_name;
[141] Fix | Delete
$plugin_data = array(
[142] Fix | Delete
'version' => $plugin_version,
[143] Fix | Delete
'date' => current_time( 'mysql' ),
[144] Fix | Delete
);
[145] Fix | Delete
if ( ! empty( $metadata ) ) {
[146] Fix | Delete
$plugin_data['metadata'] = $metadata;
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
$auto_installed_plugins = get_site_option( 'woocommerce_autoinstalled_plugins', array() );
[150] Fix | Delete
$auto_installed_plugins[ $plugin_name ] = $plugin_data;
[151] Fix | Delete
update_site_option( 'woocommerce_autoinstalled_plugins', $auto_installed_plugins );
[152] Fix | Delete
[153] Fix | Delete
$auto_installed_plugins_history = get_site_option( 'woocommerce_history_of_autoinstalled_plugins', array() );
[154] Fix | Delete
if ( ! isset( $auto_installed_plugins_history[ $plugin_name ] ) ) {
[155] Fix | Delete
$auto_installed_plugins_history[ $plugin_name ] = $plugin_data;
[156] Fix | Delete
update_site_option( 'woocommerce_history_of_autoinstalled_plugins', $auto_installed_plugins_history );
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
$post_install = function () use ( $plugin_name, $plugin_version, $installed_by, $plugin_url, $plugin_data ) {
[160] Fix | Delete
$log_context = array(
[161] Fix | Delete
'source' => 'plugin_auto_installs',
[162] Fix | Delete
'recorded_data' => $plugin_data,
[163] Fix | Delete
);
[164] Fix | Delete
[165] Fix | Delete
wc_get_logger()->info( "Plugin $plugin_name v{$plugin_version} installed by $installed_by, source: $plugin_url", $log_context );
[166] Fix | Delete
};
[167] Fix | Delete
} else {
[168] Fix | Delete
$messages = $skin->get_upgrade_messages();
[169] Fix | Delete
$post_install = function () use ( $plugin_url, $installed_by, $messages ) {
[170] Fix | Delete
$log_context = array(
[171] Fix | Delete
'source' => 'plugin_auto_installs',
[172] Fix | Delete
'installer_messages' => $messages,
[173] Fix | Delete
);
[174] Fix | Delete
wc_get_logger()->error( "$installed_by failed to install plugin from source: $plugin_url", $log_context );
[175] Fix | Delete
};
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
if ( is_multisite() ) {
[179] Fix | Delete
// We log the install in the main site, unless the main site doesn't have WooCommerce installed;
[180] Fix | Delete
// in that case we fallback to logging in the current site.
[181] Fix | Delete
switch_to_blog( get_main_site_id() );
[182] Fix | Delete
if ( self::woocommerce_is_active_in_current_site() ) {
[183] Fix | Delete
$post_install();
[184] Fix | Delete
restore_current_blog();
[185] Fix | Delete
} else {
[186] Fix | Delete
restore_current_blog();
[187] Fix | Delete
$post_install();
[188] Fix | Delete
}
[189] Fix | Delete
} else {
[190] Fix | Delete
$post_install();
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
$result['install_ok'] = $install_ok ?? false;
[194] Fix | Delete
return $result;
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
/**
[198] Fix | Delete
* Check if WooCommerce is installed and active in the current blog.
[199] Fix | Delete
* This is useful for multisite installs when a blog other than the one running this code is selected with 'switch_to_blog'.
[200] Fix | Delete
*
[201] Fix | Delete
* @return bool True if WooCommerce is installed and active in the current blog, false otherwise.
[202] Fix | Delete
*/
[203] Fix | Delete
private static function woocommerce_is_active_in_current_site(): bool {
[204] Fix | Delete
$active_valid_plugins = wc_get_container()->get( PluginUtil::class )->get_all_active_valid_plugins();
[205] Fix | Delete
[206] Fix | Delete
return ! empty(
[207] Fix | Delete
array_filter(
[208] Fix | Delete
$active_valid_plugins,
[209] Fix | Delete
fn( $plugin ) => substr_compare( $plugin, '/woocommerce.php', -strlen( '/woocommerce.php' ) ) === 0
[210] Fix | Delete
)
[211] Fix | Delete
);
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
/**
[215] Fix | Delete
* Handler for the 'plugin_list_rows' hook, it will display a notice under the name of the plugins
[216] Fix | Delete
* that have been installed using this class (unless the 'woocommerce_show_autoinstalled_plugin_notices' filter
[217] Fix | Delete
* returns false) in the plugins list page.
[218] Fix | Delete
*
[219] Fix | Delete
* @param string $plugin_file Name of the plugin.
[220] Fix | Delete
* @param array $plugin_data Plugin data.
[221] Fix | Delete
*
[222] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[223] Fix | Delete
*/
[224] Fix | Delete
public function handle_plugin_list_rows( $plugin_file, $plugin_data ) {
[225] Fix | Delete
global $wp_list_table;
[226] Fix | Delete
[227] Fix | Delete
if ( is_null( $wp_list_table ) ) {
[228] Fix | Delete
return;
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
/**
[232] Fix | Delete
* Filter to suppress the notice about autoinstalled plugins in the plugins list page.
[233] Fix | Delete
*
[234] Fix | Delete
* @since 8.8.0
[235] Fix | Delete
*
[236] Fix | Delete
* @param bool $display_notice Whether notices should be displayed or not.
[237] Fix | Delete
* @returns bool
[238] Fix | Delete
*/
[239] Fix | Delete
if ( ! apply_filters( 'woocommerce_show_autoinstalled_plugin_notices', '__return_true' ) ) {
[240] Fix | Delete
return;
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
$auto_installed_plugins_info = get_site_option( 'woocommerce_autoinstalled_plugins', array() );
[244] Fix | Delete
$current_plugin_info = $auto_installed_plugins_info[ $plugin_file ] ?? null;
[245] Fix | Delete
if ( is_null( $current_plugin_info ) || $current_plugin_info['version'] !== $plugin_data['Version'] ) {
[246] Fix | Delete
return;
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
$installed_by = $current_plugin_info['metadata']['installed_by'] ?? 'WooCommerce';
[250] Fix | Delete
$info_link = $current_plugin_info['metadata']['info_link'] ?? null;
[251] Fix | Delete
if ( $info_link ) {
[252] Fix | Delete
/* translators: 1 = who installed the plugin, 2 = ISO-formatted date and time, 3 = URL */
[253] Fix | Delete
$message = sprintf( __( 'Plugin installed by %1$s on %2$s. <a target="_blank" href="%3$s">More information</a>', 'woocommerce' ), $installed_by, $current_plugin_info['date'], $info_link );
[254] Fix | Delete
} else {
[255] Fix | Delete
/* translators: 1 = who installed the plugin, 2 = ISO-formatted date and time */
[256] Fix | Delete
$message = sprintf( __( 'Plugin installed by %1$s on %2$s.', 'woocommerce' ), $installed_by, $current_plugin_info['date'] );
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
$columns_count = $wp_list_table->get_column_count();
[260] Fix | Delete
$is_active = is_plugin_active( $plugin_file );
[261] Fix | Delete
$is_active_class = $is_active ? 'active' : 'inactive';
[262] Fix | Delete
$is_active_td_style = $is_active ? "style='border-left: 4px solid #72aee6;'" : '';
[263] Fix | Delete
[264] Fix | Delete
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
[265] Fix | Delete
?>
[266] Fix | Delete
<tr class='plugin-update-tr update <?php echo $is_active_class; ?>' data-plugin='<?php echo $plugin_file; ?>' data-plugin-row-type='feature-incomp-warn'>
[267] Fix | Delete
<td colspan='<?php echo $columns_count; ?>' class='plugin-update'<?php echo $is_active_td_style; ?>>
[268] Fix | Delete
<div class='notice inline notice-success notice-alt'>
[269] Fix | Delete
<p>
[270] Fix | Delete
ℹ️ <?php echo $message; ?>
[271] Fix | Delete
</p>
[272] Fix | Delete
</div>
[273] Fix | Delete
</td>
[274] Fix | Delete
</tr>
[275] Fix | Delete
<?php
[276] Fix | Delete
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
/**
[280] Fix | Delete
* Handler for the 'upgrader_process_complete' hook. It's used to remove the autoinstalled plugin information
[281] Fix | Delete
* for plugins that are upgraded or reinstalled manually (or more generally, by using any install method
[282] Fix | Delete
* other than this class).
[283] Fix | Delete
*
[284] Fix | Delete
* @param \WP_Upgrader $upgrader The upgrader class that has performed the plugin upgrade/reinstall.
[285] Fix | Delete
* @param array $hook_extra Extra information about the upgrade process.
[286] Fix | Delete
*
[287] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[288] Fix | Delete
*/
[289] Fix | Delete
public function handle_upgrader_process_complete( \WP_Upgrader $upgrader, array $hook_extra ) {
[290] Fix | Delete
if ( $this->installing_plugin || ! ( $upgrader instanceof \Plugin_Upgrader ) || ( 'plugin' !== ( $hook_extra['type'] ?? null ) ) ) {
[291] Fix | Delete
return;
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
$auto_installed_plugins = get_site_option( 'woocommerce_autoinstalled_plugins' );
[295] Fix | Delete
if ( ! $auto_installed_plugins ) {
[296] Fix | Delete
return;
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
if ( $hook_extra['bulk'] ?? false ) {
[300] Fix | Delete
$updated_plugin_names = $hook_extra['plugins'] ?? array();
[301] Fix | Delete
} else {
[302] Fix | Delete
$updated_plugin_names = array( $upgrader->plugin_info() );
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
$auto_installed_plugin_names = array_keys( $auto_installed_plugins );
[306] Fix | Delete
$updated_auto_installed_plugin_names = array_intersect( $auto_installed_plugin_names, $updated_plugin_names );
[307] Fix | Delete
[308] Fix | Delete
if ( empty( $updated_auto_installed_plugin_names ) ) {
[309] Fix | Delete
return;
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
$new_auto_installed_plugins = array_diff_key( $auto_installed_plugins, array_flip( $updated_auto_installed_plugin_names ) );
[313] Fix | Delete
[314] Fix | Delete
if ( empty( $new_auto_installed_plugins ) ) {
[315] Fix | Delete
delete_site_option( 'woocommerce_autoinstalled_plugins' );
[316] Fix | Delete
} else {
[317] Fix | Delete
update_site_option( 'woocommerce_autoinstalled_plugins', $new_auto_installed_plugins );
[318] Fix | Delete
}
[319] Fix | Delete
}
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function