Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
InstallerCommandHandler
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 3
110
0.00% covered (danger)
0.00%
0 / 1
 auto_upgrade_extended_yith_plugins
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
 upgrade_extended_yith_plugin
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 install_premium_plugin
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2namespace NewfoldLabs\WP\Module\Installer\WPCLI\Handlers;
3
4use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller;
5use NewfoldLabs\WP\Module\Installer\Services\PluginUpgrader;
6use WP_CLI;
7
8/**
9 * Class InstallerCommandHandler
10 *
11 * Handles WP-CLI custom commands for the Installer module.
12 */
13class InstallerCommandHandler {
14    /**
15     * Triggers the upgrade of a set of pre-defined extended YITH plugins.
16     *
17     * This command runs the upgrade process for YITH plugins, upgrading them from extended versions
18     * to their corresponding premium versions, and outputs the status of each upgrade in JSON format.
19     *
20     * ## EXAMPLES
21     *
22     *     wp installer auto_upgrade_extended_yith_plugins
23     *
24     * @return void
25     */
26    public function auto_upgrade_extended_yith_plugins() {
27        $all_upgrades_successful = true;
28        $plugin_upgrade_statuses = PluginUpgrader::upgrade_extended_yith_plugins();
29        foreach ( $plugin_upgrade_statuses as $plugin_slug => $status_info ) {
30            if ( ! $status_info['upgraded'] ) {
31                $all_upgrades_successful = false;
32            }
33
34            $status_json = wp_json_encode(
35                array(
36                    'slug'    => $plugin_slug,
37                    'status'  => $status_info['upgraded'],
38                    'message' => $status_info['message'],
39                )
40            );
41
42            WP_CLI::log( $status_json );
43        }
44
45        if ( $all_upgrades_successful ) {
46            WP_CLI::success( __( 'YITH plugin upgrade process completed successfully.', 'wp-module-installer' ) );
47        } else {
48            WP_CLI::error( __( 'YITH plugin upgrade process completed, but some upgrades failed. Please check the logs.', 'wp-module-installer' ) );
49        }
50    }
51
52    /**
53     * Triggers the upgrade of a single extended YITH plugin.
54     *
55     * This command upgrades a specific YITH plugin from its extended version
56     * to its corresponding premium version and outputs the result in JSON format.
57     *
58     * ## EXAMPLES
59     *
60     *     wp installer upgrade_extended_yith_plugin <extended_yith_plugin_slug>
61     *
62     * @param array $args Arguments passed from the command line. First argument is the plugin slug.
63     *
64     * @return void
65     */
66    public function upgrade_extended_yith_plugin( $args ) {
67        $plugin_slug = $args[0];
68
69        $status_info = PluginUpgrader::upgrade_extended_yith_plugin( $plugin_slug );
70        $status_json = wp_json_encode(
71            array(
72                'slug'    => $plugin_slug,
73                'status'  => $status_info['upgraded'],
74                'message' => $status_info['message'],
75            )
76        );
77        WP_CLI::log( $status_json );
78
79        if ( $status_info['upgraded'] ) {
80            WP_CLI::success( 'Plugin upgrade completed successfully.' );
81        } else {
82            WP_CLI::error( 'Plugin upgrade failed. Please check the logs for more details.' );
83        }
84    }
85
86    /**
87     * Triggers the installation and activation of a premium plugin.
88     *
89     * This command provisions a license, installs the premium plugin, and optionally activates it
90     * based on the activation parameter passed. It outputs the status of the process.
91     *
92     * ## OPTIONS
93     *
94     * <premium_slug>
95     * : The slug of the premium plugin to be installed.
96     *
97     * <provider>
98     * : The name of the provider for the premium plugin.
99     *
100     * [--activate]
101     * : Optional flag to activate the plugin after installation.
102     *
103     * ## EXAMPLES
104     *
105     *     wp installer install_premium_plugin <premium_slug> <provider> --activate
106     *     wp installer install_premium_plugin <premium_slug> <provider>
107     *
108     * @param array $args Arguments passed from the command line. First argument is the plugin slug.
109     * @param array $assoc_args Associative arguments (like --activate).
110     *
111     * @return void
112     */
113    public function install_premium_plugin( $args, $assoc_args ) {
114        $premium_slug = $args[0];
115        $provider     = $args[1];
116        $activate     = isset( $assoc_args['activate'] );
117
118        // Ensure both the plugin slug and provider are not empty
119        if ( empty( $premium_slug ) || empty( $provider ) ) {
120            WP_CLI::error( __( 'Both plugin slug and provider name are required.', 'wp-module-installer' ) );
121            return;
122        }
123
124        // Call the function to provision, install, and (optionally) activate the premium plugin
125        $status = PluginInstaller::install_premium_plugin( $premium_slug, $provider, $activate );
126
127        // Handle error or success response
128        if ( is_wp_error( $status ) ) {
129            WP_CLI::error( $status->get_error_message() );
130        } else {
131            WP_CLI::success( $status->get_data()['message'] );
132        }
133    }
134}