WC_Install::background_installer() – Install a plugin from .org in the background via a cron job (used by installer – opt in).

You appear to be a bot. Output may be restricted

Description

Install a plugin from .org in the background via a cron job (used by installer – opt in).

Usage

WC_Install::background_installer( $plugin_to_install_id, $plugin_to_install );

Parameters

$plugin_to_install_id
( string ) required – Plugin ID.
$plugin_to_install
( array ) required – Plugin information.

Returns

void

Source

File name: woocommerce/includes/class-wc-install.php
Lines:

1 to 100 of 136
  public static function background_installer( $plugin_to_install_id, $plugin_to_install ) {
    // Explicitly clear the event.
    $args = func_get_args();

    if ( ! empty( $plugin_to_install['repo-slug'] ) ) {
      require_once ABSPATH . 'wp-admin/includes/file.php';
      require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
      require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
      require_once ABSPATH . 'wp-admin/includes/plugin.php';

      WP_Filesystem();

      $skin              = new Automatic_Upgrader_Skin();
      $upgrader          = new WP_Upgrader( $skin );
      $installed_plugins = array_reduce( array_keys( get_plugins() ), array( __CLASS__, 'associate_plugin_file' ) );
      if ( empty( $installed_plugins ) ) {
        $installed_plugins = array();
      }
      $plugin_slug = $plugin_to_install['repo-slug'];
      $plugin_file = isset( $plugin_to_install['file'] ) ? $plugin_to_install['file'] : $plugin_slug . '.php';
      $installed   = false;
      $activate    = false;

      // See if the plugin is installed already.
      if ( isset( $installed_plugins[ $plugin_file ] ) ) {
        $installed = true;
        $activate  = ! is_plugin_active( $installed_plugins[ $plugin_file ] );
      }

      // Install this thing!
      if ( ! $installed ) {
        // Suppress feedback.
        ob_start();

        try {
          $plugin_information = plugins_api(
            'plugin_information',
            array(
              'slug'   => $plugin_slug,
              'fields' => array(
                'short_description' => false,
                'sections'          => false,
                'requires'          => false,
                'rating'            => false,
                'ratings'           => false,
                'downloaded'        => false,
                'last_updated'      => false,
                'added'             => false,
                'tags'              => false,
                'homepage'          => false,
                'donate_link'       => false,
                'author_profile'    => false,
                'author'            => false,
              ),
            )
          );

          if ( is_wp_error( $plugin_information ) ) {
            throw new Exception( $plugin_information->get_error_message() );
          }

          $package  = $plugin_information->download_link;
          $download = $upgrader->download_package( $package );

          if ( is_wp_error( $download ) ) {
            throw new Exception( $download->get_error_message() );
          }

          $working_dir = $upgrader->unpack_package( $download, true );

          if ( is_wp_error( $working_dir ) ) {
            throw new Exception( $working_dir->get_error_message() );
          }

          $result = $upgrader->install_package(
            array(
              'source'                      => $working_dir,
              'destination'                 => WP_PLUGIN_DIR,
              'clear_destination'           => false,
              'abort_if_destination_exists' => false,
              'clear_working'               => true,
              'hook_extra'                  => array(
                'type'   => 'plugin',
                'action' => 'install',
              ),
            )
          );

          if ( is_wp_error( $result ) ) {
            throw new Exception( $result->get_error_message() );
          }

          $activate = true;

        } catch ( Exception $e ) {
          WC_Admin_Notices::add_custom_notice(
            $plugin_to_install_id . '_install_error',
            sprintf(
              // translators: 1: plugin name, 2: error message, 3: URL to install plugin manually.
              __( '%1$s could not be installed (%2$s). <a href="%3$s">Please install it manually by clicking here.</a>', 'woocommerce' ),
 

 View on GitHub View on Trac