Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Marketplace_Plugin_Installer
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 1
 install
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
90
1<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing
2/**
3 * Class that allows to install a marketplace plugin.
4 *
5 * @since 5.5.0
6 * @package WPCOM_Marketplace
7 */
8
9/**
10 * Class that allows to install a marketplace plugin by generating and running the installation commands.
11 */
12class Marketplace_Plugin_Installer extends Marketplace_Product_Installer {
13
14    /**
15     * Install the plugin.
16     *
17     * @return WP_Error|bool
18     */
19    public function install() {
20        // 1. Install the plugin dependencies.
21        $install_dependencies = $this->install_dependencies();
22        if ( is_wp_error( $install_dependencies ) ) {
23            return $install_dependencies;
24        }
25
26        // 2. Get the list of plugins to skip when installing the plugin.
27        $skip_plugins = $this->get_skip_plugins();
28        if ( is_wp_error( $skip_plugins ) ) {
29            return $skip_plugins;
30        }
31
32        // 3. Get the list of themes to skip when installing the plugin.
33        $skip_themes = $this->get_skip_themes();
34        if ( is_wp_error( $skip_themes ) ) {
35            return $skip_themes;
36        }
37
38        // 4. Generate and run the plugin installation command.
39        $plugin_install_commands = $this->command_helper->generate_plugin_install_commands(
40            $this->product_software->get_product_slug_or_url(),
41            $this->product_software->is_managed(),
42            $skip_plugins,
43            $skip_themes
44        );
45
46        foreach ( $plugin_install_commands as $command ) {
47            $plugin_install = $this->run_command( $command );
48            if ( is_wp_error( $plugin_install ) ) {
49                return $plugin_install;
50            }
51        }
52
53        // 5. Verify the plugin installation.
54        $expected_plugins = array_filter(
55            array( ...$this->product_software->get_plugin_dependencies(), $this->product_software->get_software_slug() )
56        );
57
58        $verify_plugin_installation_commands = $this->command_helper->generate_verify_plugin_installation_commands( $expected_plugins, $this->product_software->get_theme_dependencies() );
59
60        foreach ( $verify_plugin_installation_commands as $command ) {
61            $verify_installation = $this->run_command( $command );
62            if ( is_wp_error( $verify_installation ) ) {
63                return $verify_installation;
64            }
65
66            if ( $verify_installation->stdout !== 'active' ) {
67                return new WP_Error(
68                    'plugin_installation_failed',
69                    sprintf( '%s: Plugin installation failed. The plugin is not active.', $this->product_software->get_software_slug() ),
70                    $this->results
71                );
72            }
73        }
74
75        return true;
76    }
77}