Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PluginUpgrader
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 upgrade_extended_yith_plugins
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 upgrade_extended_yith_plugin
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2namespace NewfoldLabs\WP\Module\Installer\Services;
3
4use NewfoldLabs\WP\Module\Installer\Data\Plugins;
5use NewfoldLabs\WP\Module\PLS\Utilities\PLSUtility;
6
7/**
8 * Class PluginUpgrader
9 *
10 * This class is responsible for upgrading plugins.
11 */
12class PluginUpgrader {
13
14    /**
15     * Tries to upgrade the extended YITH plugins to their corresponding premium versions.
16     *
17     * @return array
18     */
19    public static function upgrade_extended_yith_plugins() {
20        // Define the list of extended YITH plugins to upgrade.
21        $yith_plugins_to_upgrade = array(
22            'nfd_slug_yith_woocommerce_ajax_product_filter',
23            'nfd_slug_yith_woocommerce_wishlist',
24            'nfd_slug_yith_woocommerce_booking',
25            'nfd_slug_yith_woocommerce_gift_cards',
26            'nfd_slug_yith_woocommerce_customize_myaccount_page',
27            'yith-woocommerce-ajax-search',
28        );
29
30        // Array to store the status of each plugin's upgrade process
31        $yith_plugins_upgrade_status = array();
32
33        foreach ( $yith_plugins_to_upgrade as $index => $extended_slug ) {
34            $upgrade_status                                = self::upgrade_extended_yith_plugin( $extended_slug );
35            $yith_plugins_upgrade_status[ $extended_slug ] = $upgrade_status;
36        }
37
38        return $yith_plugins_upgrade_status;
39    }
40
41    /**
42     * Upgrades a single extended YITH plugin to its corresponding premium version.
43     *
44     * @param string $extended_slug The slug of the extended plugin.
45     *
46     * @return array Contains the status of the upgrade process with 'upgraded' and 'message' keys.
47     */
48    public static function upgrade_extended_yith_plugin( $extended_slug ) {
49        // Define the list of extended YITH plugins and their corresponding premium versions.
50        $yith_plugins_to_upgrade = array(
51            'nfd_slug_yith_woocommerce_ajax_product_filter' => 'yith-woocommerce-ajax-navigation',
52            'nfd_slug_yith_woocommerce_wishlist'   => 'yith-woocommerce-wishlist',
53            'nfd_slug_yith_woocommerce_booking'    => 'yith-woocommerce-booking',
54            'nfd_slug_yith_woocommerce_gift_cards' => 'yith-woocommerce-gift-cards',
55            'nfd_slug_yith_woocommerce_customize_myaccount_page' => 'yith-woocommerce-customize-myaccount-page',
56            'yith-woocommerce-ajax-search'         => 'yith-woocommerce-ajax-search',
57        );
58
59        // Initialize status array for the plugin upgrade process
60        $upgrade_status = array(
61            'upgraded' => false,
62            'message'  => '',
63        );
64
65        // Get plugin status codes for comparison
66        $status_codes = Plugins::get_status_codes();
67
68        // Check if the extended plugin exists in the list
69        if ( ! isset( $yith_plugins_to_upgrade[ $extended_slug ] ) ) {
70            $upgrade_status['message'] = __( 'Invalid extended plugin slug provided.', 'wp-module-installer' );
71            return $upgrade_status;
72        }
73
74        // Get the corresponding premium plugin slug
75        $premium_slug = $yith_plugins_to_upgrade[ $extended_slug ];
76
77        // Get the status of the extended plugin
78        $extended_status = PluginInstaller::get_plugin_status( $extended_slug );
79
80        // Check if the extended plugin's status is unknown or not installed
81        if ( $status_codes['unknown'] === $extended_status ) {
82            $upgrade_status['message'] = __( 'Could not determine the status of the extended plugin.', 'wp-module-installer' );
83            return $upgrade_status;
84        }
85
86        if ( $status_codes['not_installed'] === $extended_status ) {
87            $upgrade_status['message'] = __( 'Extended plugin is not installed.', 'wp-module-installer' );
88            return $upgrade_status;
89        }
90
91        // Check if the premium plugin should be activated after installation
92        $should_activate = ( $status_codes['active'] === $extended_status );
93
94        // Deactivate the extended version of the plugin if the premium plugin needs to be activated
95        if ( $should_activate ) {
96            $deactivation_response = PluginInstaller::deactivate( $extended_slug );
97            if ( is_wp_error( $deactivation_response ) ) {
98                $upgrade_status['message'] = __( 'Failed to deactivate the extended plugin: ', 'wp-module-installer' ) . $extended_slug;
99                return $upgrade_status;
100            }
101        }
102
103        // Use the install_premium_plugin function to install and activate the premium plugin
104        $install_status = PluginInstaller::install_premium_plugin( $premium_slug, 'yith', $should_activate );
105        if ( is_wp_error( $install_status ) ) {
106            $upgrade_status['message'] = $install_status->get_error_message();
107
108            // Reactivate the extended plugin if premium installation failed and it was deactivated
109            if ( $should_activate ) {
110                $reactivation_response = PluginInstaller::activate( $extended_slug );
111                if ( is_wp_error( $reactivation_response ) ) {
112                    $upgrade_status['message'] .= __( ' Also Failed to reactivate the extended plugin: ', 'wp-module-installer' ) . $extended_slug;
113                }
114            }
115
116            return $upgrade_status;
117        }
118
119        // Mark as successfully upgraded
120        $upgrade_status['upgraded'] = true;
121        $upgrade_status['message']  = __( 'Successfully provisioned and installed: ', 'wp-module-installer' ) . $premium_slug;
122
123        // Attempt to uninstall the extended version of the plugin
124        $uninstall_status = PluginUninstaller::uninstall( $extended_slug );
125        if ( is_wp_error( $uninstall_status ) ) {
126            $upgrade_status['message'] = __( 'Successfully installed the premium plugin, but there was an error uninstalling the extended plugin. Manual cleanup may be required.', 'wp-module-installer' );
127            return $upgrade_status;
128        }
129
130        // Return the status of the upgrade process
131        return $upgrade_status;
132    }
133}