Automattic\WooCommerce\Admin

PluginsHelper::install_plugins()public staticWC 1.0

Install an array of plugins.

Метод класса: PluginsHelper{}

Возвращает

Массив.

Использование

$result = PluginsHelper::install_plugins( $plugins, $logger );
$plugins(массив) (обязательный)
Plugins to install.
$logger(PluginsInstallLogger|null)
an optional logger.
По умолчанию: null

Код PluginsHelper::install_plugins() WC 8.9.3

public static function install_plugins( $plugins, PluginsInstallLogger $logger = null ) {
	/**
	 * Filter the list of plugins to install.
	 *
	 * @param array $plugins A list of the plugins to install.
	 *
	 * @since 6.4.0
	 */
	$plugins = apply_filters( 'woocommerce_admin_plugins_pre_install', $plugins );

	if ( empty( $plugins ) || ! is_array( $plugins ) ) {
		return new WP_Error(
			'woocommerce_plugins_invalid_plugins',
			__( 'Plugins must be a non-empty array.', 'woocommerce' )
		);
	}

	require_once ABSPATH . 'wp-admin/includes/plugin.php';
	include_once ABSPATH . '/wp-admin/includes/admin.php';
	include_once ABSPATH . '/wp-admin/includes/plugin-install.php';
	include_once ABSPATH . '/wp-admin/includes/plugin.php';
	include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
	include_once ABSPATH . '/wp-admin/includes/class-plugin-upgrader.php';

	$existing_plugins   = self::get_installed_plugins_paths();
	$installed_plugins  = array();
	$results            = array();
	$time               = array();
	$errors             = new WP_Error();
	$install_start_time = time();

	foreach ( $plugins as $plugin ) {
		$slug = sanitize_key( $plugin );
		$logger && $logger->install_requested( $plugin );

		if ( isset( $existing_plugins[ $slug ] ) ) {
			$installed_plugins[] = $plugin;
			$logger && $logger->installed( $plugin, 0 );
			continue;
		}

		$start_time = microtime( true );

		$api = plugins_api(
			'plugin_information',
			array(
				'slug'   => $slug,
				'fields' => array(
					'sections' => false,
				),
			)
		);

		if ( is_wp_error( $api ) ) {
			$properties = array(
				'error_message'     => sprintf(
					// translators: %s: plugin slug (example: woocommerce-services).
					__(
						'The requested plugin `%s` could not be installed. Plugin API call failed.',
						'woocommerce'
					),
					$slug
				),
				'api_error_message' => $api->get_error_message(),
				'slug'              => $slug,
			);
			wc_admin_record_tracks_event( 'install_plugin_error', $properties );

			/**
			 * Action triggered when a plugin API call failed.
			 *
			 * @param string $slug The plugin slug.
			 * @param WP_Error $api The API response.
			 *
			 * @since 6.4.0
			 */
			do_action( 'woocommerce_plugins_install_api_error', $slug, $api );

			$error_message = sprintf(
			/* translators: %s: plugin slug (example: woocommerce-services) */
				__( 'The requested plugin `%s` could not be installed. Plugin API call failed.', 'woocommerce' ),
				$slug
			);

			$errors->add( $plugin, $error_message );
			$logger && $logger->add_error( $plugin, $error_message );

			continue;
		}

		$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
		$result   = $upgrader->install( $api->download_link );
		// result can be false or WP_Error.
		$results[ $plugin ] = $result;
		$time[ $plugin ]    = round( ( microtime( true ) - $start_time ) * 1000 );

		if ( is_wp_error( $result ) || is_null( $result ) ) {
			$properties = array(
				'error_message'         => sprintf(
					/* translators: %s: plugin slug (example: woocommerce-services) */
					__(
						'The requested plugin `%s` could not be installed.',
						'woocommerce'
					),
					$slug
				),
				'slug'                  => $slug,
				'api_version'           => $api->version,
				'api_download_link'     => $api->download_link,
				'upgrader_skin_message' => implode( ',', $upgrader->skin->get_upgrade_messages() ),
				'result'                => is_wp_error( $result ) ? $result->get_error_message() : 'null',
			);
			wc_admin_record_tracks_event( 'install_plugin_error', $properties );

			/**
			 * Action triggered when a plugin installation fails.
			 *
			 * @param string $slug The plugin slug.
			 * @param object $api The plugin API object.
			 * @param WP_Error|null $result The result of the plugin installation.
			 * @param Plugin_Upgrader $upgrader The plugin upgrader.
			 *
			 * @since 6.4.0
			 */
			do_action( 'woocommerce_plugins_install_error', $slug, $api, $result, $upgrader );

			$install_error_message = sprintf(
			/* translators: %s: plugin slug (example: woocommerce-services) */
				__( 'The requested plugin `%s` could not be installed. Upgrader install failed.', 'woocommerce' ),
				$slug
			);
			$errors->add(
				$plugin,
				$install_error_message
			);
			$logger && $logger->add_error( $plugin, $install_error_message );

			continue;
		}

		$installed_plugins[] = $plugin;
		$logger && $logger->installed( $plugin, $time[ $plugin ] );
	}

	$data = array(
		'installed' => $installed_plugins,
		'results'   => $results,
		'errors'    => $errors,
		'time'      => $time,
	);

	$logger && $logger->complete( array_merge( $data, array( 'start_time' => $install_start_time ) ) );

	return $data;
}