Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
WC_Services_Installer
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 7
380
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 on_jetpack_loaded
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 try_install
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
90
 error_notice
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 install
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 activate
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3use Automattic\Jetpack\Plugins_Installer;
4
5if ( ! defined( 'ABSPATH' ) ) {
6    exit( 0 );
7}
8
9/**
10 * Installs and activates the WooCommerce Services plugin.
11 */
12class WC_Services_Installer {
13
14    /**
15     * The instance of the Jetpack class.
16     *
17     * @var Jetpack
18     */
19    private $jetpack;
20
21    /**
22     * The singleton instance of this class.
23     *
24     * @var WC_Services_Installer
25     */
26    private static $instance = null;
27
28    /**
29     * Returns the singleton instance of this class.
30     *
31     * @return object The WC_Services_Installer object.
32     */
33    public static function init() {
34        if ( self::$instance === null ) {
35            self::$instance = new WC_Services_Installer();
36        }
37        return self::$instance;
38    }
39
40    /**
41     * Constructor
42     */
43    public function __construct() {
44        add_action( 'jetpack_loaded', array( $this, 'on_jetpack_loaded' ) );
45        if ( ! empty( $_GET['wc-services-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
46            add_action( 'admin_notices', array( $this, 'error_notice' ) );
47        }
48
49        if ( isset( $_GET['wc-services-action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
50            add_action( 'admin_init', array( $this, 'try_install' ) );
51        }
52    }
53
54    /**
55     * Runs on Jetpack being ready to load its packages.
56     *
57     * @param Jetpack $jetpack object.
58     */
59    public function on_jetpack_loaded( $jetpack ) {
60        $this->jetpack = $jetpack;
61    }
62
63    /**
64     * Verify the intent to install WooCommerce Services, and kick off installation.
65     */
66    public function try_install() {
67        if ( ! isset( $_GET['wc-services-action'] ) ) {
68            return;
69        }
70        check_admin_referer( 'wc-services-install' );
71
72        $result = false;
73
74        switch ( $_GET['wc-services-action'] ) {
75            case 'install':
76                if ( current_user_can( 'install_plugins' ) ) {
77                    $this->jetpack->stat( 'jitm', 'wooservices-install-' . JETPACK__VERSION );
78                    $result = $this->install();
79                    if ( $result ) {
80                        $result = $this->activate();
81                    }
82                }
83                break;
84
85            case 'activate':
86                if ( current_user_can( 'activate_plugins' ) ) {
87                    $this->jetpack->stat( 'jitm', 'wooservices-activate-' . JETPACK__VERSION );
88                    $result = $this->activate();
89                }
90                break;
91        }
92
93        if ( isset( $_GET['redirect'] ) ) {
94            $redirect = home_url( esc_url_raw( wp_unslash( $_GET['redirect'] ) ) );
95        } else {
96            $redirect = admin_url();
97        }
98
99        if ( $result ) {
100            $this->jetpack->stat( 'jitm', 'wooservices-activated-' . JETPACK__VERSION );
101        } else {
102            $redirect = add_query_arg( 'wc-services-install-error', true, $redirect );
103        }
104
105        wp_safe_redirect( $redirect );
106
107        exit( 0 );
108    }
109
110    /**
111     * Notify the user that the installation of WooCommerce Services failed.
112     */
113    public function error_notice() {
114        wp_admin_notice(
115            esc_html__( 'There was an error installing WooCommerce Services.', 'jetpack' ),
116            array(
117                'type'        => 'error',
118                'dismissible' => true,
119            )
120        );
121    }
122
123    /**
124     * Download and install the WooCommerce Services plugin.
125     *
126     * @return bool result of installation
127     */
128    private function install() {
129        $result = Plugins_Installer::install_plugin( 'woocommerce-services' );
130
131        if ( is_wp_error( $result ) ) {
132            return false;
133        } else {
134            return true;
135        }
136    }
137
138    /**
139     * Activate the WooCommerce Services plugin.
140     *
141     * @return bool result of activation
142     */
143    private function activate() {
144        $result = activate_plugin( 'woocommerce-services/woocommerce-services.php' );
145
146        // Activate_plugin() returns null on success.
147        return $result === null;
148    }
149}
150
151WC_Services_Installer::init();