WC_Install::maybe_install_legacy_api_plugin()private staticWC 1.0

Install and activate the WooCommerce Legacy REST API plugin from the WordPress.org directory if all the following is true:

  1. We are in a WooCommerce upgrade process (not a new install).
  2. The woocommerce_skip_legacy_rest_api_plugin_auto_install returns false (which is the default).
  3. The plugin is not installed and active already (but see note about multisite below).
  4. The Legacy REST API is enabled in the site OR the site has at least one webhook defined that uses the Legacy REST API payload format (disabled webhooks also count).

In multisite setups it could happen that the plugin was installed by an installation process performed in another site. In this case we check if the plugin was autoinstalled in such a way, and if so we activate it if the conditions are fulfilled.

Method of the class: WC_Install{}

Return

null. Nothing (null).

Usage

$result = WC_Install::maybe_install_legacy_api_plugin();

WC_Install::maybe_install_legacy_api_plugin() code WC 9.3.3

private static function maybe_install_legacy_api_plugin() {
	if ( self::is_new_install() ) {
		return;
	}

	// Did we previously install this plugin?
	// We check both the woocommerce_history_of_autoinstalled_plugins options (introduced in 9.0)
	// and the woocommerce_autoinstalled_plugins option (info should still exist here if the plugin has been uninstalled but not manually reinstalled).
	$legacy_api_plugin          = 'woocommerce-legacy-rest-api/woocommerce-legacy-rest-api.php';
	$autoinstalled_plugins      = (array) get_site_option( 'woocommerce_history_of_autoinstalled_plugins', array() );
	$previously_installed_by_us = isset( $autoinstalled_plugins[ $legacy_api_plugin ] );
	if ( ! $previously_installed_by_us ) {
		$autoinstalled_plugins      = (array) get_site_option( 'woocommerce_autoinstalled_plugins', array() );
		$previously_installed_by_us = isset( $autoinstalled_plugins[ $legacy_api_plugin ] );
	}

	/**
	 * Filter to skip the automatic installation of the WooCommerce Legacy REST API plugin
	 * from the WordPress.org plugins directory.
	 *
	 * By default, this is true (skip installation) if we have a record of previously installing the legacy plugin,
	 * and false (do not skip) if we have no record of previously installing the plugin.
	 *
	 * @since 8.8.0
	 *
	 * @param bool $skip_auto_install False, defaulting to "don't skip the plugin automatic installation".
	 * @returns bool True to skip the plugin automatic installation, false to install the plugin if necessary.
	 */
	if ( apply_filters( 'woocommerce_skip_legacy_rest_api_plugin_auto_install', $previously_installed_by_us ) ) {
		return;
	}

	if ( ( 'yes' !== get_option( 'woocommerce_api_enabled' ) &&
		0 === wc_get_container()->get( Automattic\WooCommerce\Internal\Utilities\WebhookUtil::class )->get_legacy_webhooks_count( true ) ) ) {
		return;
	}

	wp_clean_plugins_cache();
	if ( ! function_exists( 'get_plugins' ) ) {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
	}
	if ( isset( get_plugins()[ $legacy_api_plugin ] ) ) {
		if ( ! $previously_installed_by_us ) {
			// The plugin was installed manually so let's not interfere.
			return;
		}

		if ( in_array( $legacy_api_plugin, wp_get_active_and_valid_plugins(), true ) ) {
			return;
		}

		// The plugin was automatically installed in a different installation process - can happen in multisite.
		$install_ok = true;
	} else {
		try {
			$install_result = wc_get_container()->get( PluginInstaller::class )->install_plugin(
				'https://downloads.wordpress.org/plugin/woocommerce-legacy-rest-api.latest-stable.zip',
				array(
					'info_link' => 'https://developer.woocommerce.com/2023/10/03/the-legacy-rest-api-will-move-to-a-dedicated-extension-in-woocommerce-9-0/',
				)
			);

			if ( $install_result['already_installing'] ?? null ) {
				// The plugin is in the process of being installed already (can happen in multisite),
				// but we still need to activate it for ourselves once it's installed.
				as_schedule_single_action( time() + 10, 'woocommerce_activate_legacy_rest_api_plugin' );
				return;
			}

			$install_ok = $install_result['install_ok'];
		} catch ( \Exception $ex ) {
			wc_get_logger()->error(
				'The autoinstall of the WooCommerce Legacy REST API plugin failed: ' . $ex->getMessage(),
				array(
					'source'    => 'plugin_auto_installs',
					'exception' => $ex,
				)
			);
			$install_ok = false;
		}
	}

	$plugin_page_url              = 'https://wordpress.org/plugins/woocommerce-legacy-rest-api/';
	$blog_post_url                = 'https://developer.woocommerce.com/2023/10/03/the-legacy-rest-api-will-move-to-a-dedicated-extension-in-woocommerce-9-0/';
	$site_legacy_api_settings_url = get_admin_url( null, '/admin.php?page=wc-settings&tab=advanced&section=legacy_api' );
	$site_webhooks_settings_url   = get_admin_url( null, '/admin.php?page=wc-settings&tab=advanced&section=webhooks' );
	$site_logs_url                = get_admin_url( null, '/admin.php?page=wc-status&tab=logs' );

	if ( $install_ok ) {
		$activation_result = activate_plugin( $legacy_api_plugin );
		if ( $activation_result instanceof \WP_Error ) {
			$message = sprintf(
			/* translators: 1 = URL of Legacy REST API plugin page, 2 = URL of Legacy API settings in current site, 3 = URL of webhooks settings in current site, 4 = URL of logs page in current site, 5 = URL of plugins page in current site, 6 = URL of blog post about the Legacy REST API removal */
				__( '⚠️ WooCommerce installed <a href="%1$s">the Legacy REST API plugin</a> because this site has <a href="%2$s">the Legacy REST API enabled</a> or has <a href="%3$s">legacy webhooks defined</a>, but it failed to activate it (see error details in <a href="%4$s">the WooCommerce logs</a>). Please go to <a href="%5$s">the plugins page</a> and activate it manually. <a href="%6$s">More information</a>', 'woocommerce' ),
				$plugin_page_url,
				$site_legacy_api_settings_url,
				$site_webhooks_settings_url,
				$site_logs_url,
				get_admin_url( null, '/plugins.php' ),
				$blog_post_url
			);
			$notice_name = 'woocommerce_legacy_rest_api_plugin_activation_failed';
			wc_get_logger()->error(
				__( 'WooCommerce installed the Legacy REST API plugin but failed to activate it, see context for more details.', 'woocommerce' ),
				array(
					'source' => 'plugin_auto_installs',
					'error'  => $activation_result,
				)
			);
		} else {
			$message = sprintf(
			/* translators: 1 = URL of Legacy REST API plugin page, 2 = URL of Legacy API settings in current site, 3 = URL of webhooks settings in current site, 4 = URL of blog post about the Legacy REST API removal */
				__( 'ℹ️ WooCommerce installed and activated <a href="%1$s">the Legacy REST API plugin</a> because this site has <a href="%2$s">the Legacy REST API enabled</a> or has <a href="%3$s">legacy webhooks defined</a>. <a href="%4$s">More information</a>', 'woocommerce' ),
				$plugin_page_url,
				$site_legacy_api_settings_url,
				$site_webhooks_settings_url,
				$blog_post_url
			);
			$notice_name = 'woocommerce_legacy_rest_api_plugin_activated';
			wc_get_logger()->info( 'WooCommerce activated the Legacy REST API plugin in this site.', array( 'source' => 'plugin_auto_installs' ) );
		}

		\WC_Admin_Notices::add_custom_notice( $notice_name, $message );
	} else {
		$message = sprintf(
			/* translators: 1 = URL of Legacy REST API plugin page, 2 = URL of Legacy API settings in current site, 3 = URL of webhooks settings in current site, 4 = URL of logs page in current site, 5 = URL of blog post about the Legacy REST API removal */
			__( '⚠️ WooCommerce attempted to install <a href="%1$s">the Legacy REST API plugin</a> because this site has <a href="%2$s">the Legacy REST API enabled</a> or has <a href="%3$s">legacy webhooks defined</a>, but the installation failed (see error details in <a href="%4$s">the WooCommerce logs</a>). Please install and activate the plugin manually. <a href="%5$s">More information</a>', 'woocommerce' ),
			$plugin_page_url,
			$site_legacy_api_settings_url,
			$site_webhooks_settings_url,
			$site_logs_url,
			$blog_post_url
		);

		\WC_Admin_Notices::add_custom_notice( 'woocommerce_legacy_rest_api_plugin_install_failed', $message );

		// Note that we aren't adding an entry to the error log because PluginInstaller->install_plugin will have done that already.
	}

	\WC_Admin_Notices::store_notices();
}