Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
18 / 45
58.33% covered (warning)
58.33%
7 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Plugins
40.00% covered (danger)
40.00%
18 / 45
58.33% covered (warning)
58.33%
7 / 12
79.42
0.00% covered (danger)
0.00%
0 / 1
 get_wp_slugs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_urls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_domains
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_status_codes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 get_squashed
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 get_approved
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 check_approved
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 wc_prevent_redirect_on_activation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 wpseo_prevent_redirect_on_activation
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 wpseo_premium_prevent_redirect_on_activation
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 toggle_jetpack_module
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace NewfoldLabs\WP\Module\Installer\Data;
3
4/**
5 * List of Plugin Slugs/URLs/Domains
6 */
7final class Plugins {
8    /**
9     * A value of true indicates that the slug/url/domain has been approved.
10     * A value of null indicates that the slug/url/domain has not been approved
11     * (or) has been temporarily deactivated.
12     *
13     * @var array
14     */
15    protected static $wp_slugs = array(
16        'jetpack'                           => array(
17            'approved' => true,
18            'path'     => 'jetpack/jetpack.php',
19        ),
20        'jetpack-boost'                     => array(
21            'approved' => true,
22            'path'     => 'jetpack-boost/jetpack-boost.php',
23        ),
24        'jetpack-protect'                   => array(
25            'approved' => true,
26            'path'     => 'jetpack-protect/jetpack-protect.php',
27        ),
28        'woocommerce'                       => array(
29            'approved'              => true,
30            'path'                  => 'woocommerce/woocommerce.php',
31            'post_install_callback' => array( __CLASS__, 'wc_prevent_redirect_on_activation' ),
32        ),
33        'wordpress-seo'                     => array(
34            'approved'              => true,
35            'path'                  => 'wordpress-seo/wp-seo.php',
36            'post_install_callback' => array( __CLASS__, 'wpseo_prevent_redirect_on_activation' ),
37        ),
38        'wordpress-seo-premium'             => array(
39            'approved'              => true,
40            'path'                  => 'wordpress-seo-premium/wp-seo-premium.php',
41            'post_install_callback' => array( __CLASS__, 'wpseo_premium_prevent_redirect_on_activation' ),
42        ),
43        'wpforms-lite'                      => array(
44            'approved' => true,
45            'path'     => 'wpforms-lite/wpforms.php',
46        ),
47        'google-analytics-for-wordpress'    => array(
48            'approved' => true,
49            'path'     => 'google-analytics-for-wordpress/googleanalytics.php',
50        ),
51        'optinmonster'                      => array(
52            'approved' => true,
53            'path'     => 'optinmonster/optin-monster-wp-api.php',
54        ),
55        'yith-woocommerce-ajax-search'      => array(
56            'approved' => true,
57            'path'     => 'yith-woocommerce-ajax-search/init.php',
58        ),
59        'creative-mail-by-constant-contact' => array(
60            'approved' => true,
61            'path'     => 'creative-mail-by-constant-contact/creative-mail-plugin.php',
62        ),
63    );
64
65    /**
66     * Contains a list of zip url's with a unique "nfd_slug" for each.
67     *
68     * @var array
69     */
70    protected static $nfd_slugs = array(
71        'nfd_slug_endurance_page_cache'                  => array(
72            'approved' => true,
73            'url'      => 'https://raw.githubusercontent.com/bluehost/endurance-page-cache/production/endurance-page-cache.php',
74            'path'     => WP_CONTENT_DIR . '/mu-plugins/endurance-page-cache.php',
75        ),
76        'nfd_slug_yith_woocommerce_customize_myaccount_page' => array(
77            'approved' => true,
78            'url'      => 'https://hiive.cloud/workers/plugin-downloads/yith-woocommerce-customize-myaccount-page',
79            'path'     => 'yith-woocommerce-customize-myaccount-page-extended/init.php',
80        ),
81        'nfd_slug_yith_woocommerce_gift_cards'           => array(
82            'approved' => true,
83            'url'      => 'https://hiive.cloud/workers/plugin-downloads/yith-woocommerce-gift-cards',
84            'path'     => 'yith-woocommerce-gift-cards-extended/init.php',
85        ),
86        'nfd_slug_ecomdash_wordpress_plugin'             => array(
87            'approved' => true,
88            'url'      => 'https://hiive.cloud/workers/plugin-downloads/ecomdash-wordpress-plugin',
89            'path'     => 'ecomdash-wordpress-plugin/ecomdash-plugin.php',
90        ),
91        'nfd_slug_yith_paypal_payments_for_woocommerce'  => array(
92            'approved' => true,
93            'url'      => 'https://hiive.cloud/workers/plugin-downloads/yith-paypal-payments-for-woocommerce',
94            'path'     => 'yith-paypal-payments-for-woocommerce-extended/init.php',
95        ),
96        'nfd_slug_yith_shippo_shippings_for_woocommerce' => array(
97            'approved' => true,
98            'url'      => 'https://hiive.cloud/workers/plugin-downloads/yith-shippo-shippings-for-woocommerce',
99            'path'     => 'yith-shippo-shippings-for-woocommerce-extended/init.php',
100        ),
101        'nfd_slug_yith_woocommerce_ajax_product_filter'  => array(
102            'approved' => true,
103            'url'      => 'https://hiive.cloud/workers/plugin-downloads/yith-woocommerce-ajax-product-filter',
104            'path'     => 'yith-woocommerce-ajax-product-filter-extended/init.php',
105        ),
106        'nfd_slug_yith_woocommerce_booking'              => array(
107            'approved' => true,
108            'url'      => 'https://hiive.cloud/workers/plugin-downloads/yith-woocommerce-booking',
109            'path'     => 'yith-woocommerce-booking-extended/init.php',
110        ),
111        'nfd_slug_yith_woocommerce_wishlist'             => array(
112            'approved' => true,
113            'url'      => 'https://hiive.cloud/workers/plugin-downloads/yith-woocommerce-wishlist',
114            'path'     => 'yith-woocommerce-wishlist-extended/init.php',
115        ),
116        'nfd_slug_woo_razorpay'                          => array(
117            'approved' => true,
118            'url'      => 'https://hiive.cloud/workers/plugin-downloads/razorpay',
119            'path'     => 'woo-razorpay/woo-razorpay.php',
120        ),
121        'nfd_slug_wonder_cart'                           => array(
122            'approved' => true,
123            'url'      => 'https://hiive.cloud/workers/plugin-downloads/wonder-cart',
124            'path'     => 'wonder-cart/init.php',
125        ),
126        'nfd_slug_yith_stripe_payments_for_woocommerce'  => array(
127            'approved' => true,
128            'url'      => 'https://hiive.cloud/workers/plugin-downloads/yith-stripe-payments-for-woocommerce',
129            'path'     => 'yith-stripe-payments-for-woocommerce-extended/init.php',
130        ),
131        'nfd_slug_payments_shipping'                       => array(
132            'approved' => true,
133            'url'      => 'https://hiive.cloud/workers/plugin-downloads/payments-shipping',
134            'path'     => 'payments-shipping/payments-shipping.php',
135        ),
136    );
137
138    // [TODO] Think about deprecating this approach and move to nfd_slugs for url based installs.
139    /**
140     * Contains a whitelist of zip url's.
141     *
142     * @var array
143     */
144    protected static $urls = array(
145        'https://downloads.wordpress.org/plugin/google-analytics-for-wordpress.8.5.3.zip' => true,
146    );
147    /**
148     * Contains a list of approved domains for zip based installs.
149     *
150     * @var array
151     */
152    protected static $domains = array(
153        'downloads.wordpress.org' => true,
154        'nonapproveddomain.com'   => null,
155        'hiive.cloud'             => true,
156    );
157    /**
158     * Holds the possible status codes for a plugin.
159     *
160     * @var array $status_codes Possible plugin status codes including:
161     *     'unknown'       - The plugin's status cannot be determined.
162     *     'installed'     - The plugin is installed but not activated.
163     *     'active'        - The plugin is installed and active.
164     *     'not_installed' - The plugin is not installed on the system.
165     */
166    protected static $status_codes = array(
167        'unknown'       => 'unknown',
168        'installed'     => 'installed',
169        'active'        => 'active',
170        'not_installed' => 'not_installed',
171    );
172
173    /**
174     * Returns a list of whitelisted WordPress Plugin slugs.
175     *
176     * @return array
177     */
178    public static function get_wp_slugs() {
179        return self::$wp_slugs;
180    }
181
182    /**
183     * Returns a list of whitelisted Plugin URL's.
184     *
185     * @return array
186     */
187    public static function get_urls() {
188        return self::$urls;
189    }
190
191    /**
192     * Returns a list of whitelisted Plugin URL domains.
193     *
194     * @return array
195     */
196    public static function get_domains() {
197        return self::$domains;
198    }
199
200    /**
201     * Retrieves the array of plugin status codes.
202     *
203     * @return array
204     */
205    public static function get_status_codes() {
206        return self::$status_codes;
207    }
208
209    /**
210     * Use this return value for a faster search of slug/url/domain.
211     *
212     * @return array
213     */
214    public static function get() {
215        return array(
216            'wp_slugs'     => self::$wp_slugs,
217            'nfd_slugs'    => self::$nfd_slugs,
218            'urls'         => self::$urls,
219            'domains'      => self::$domains,
220            'status_codes' => self::$status_codes,
221        );
222    }
223
224    /**
225     * Use this for finding the path for installed plugins.
226     *
227     * @return array
228     */
229    public static function get_squashed() {
230        return array_merge(
231            array_filter( self::$wp_slugs, array( __CLASS__, 'check_approved' ) ),
232            array_filter( self::$nfd_slugs, array( __CLASS__, 'check_approved' ) )
233        );
234    }
235
236    /**
237     * Get approved slugs/urls/domains
238     *
239     * @return array
240     */
241    public static function get_approved() {
242        return array(
243            'wp_slugs'  => array_keys( array_filter( self::$wp_slugs, array( __CLASS__, 'check_approved' ) ) ),
244            'nfd_slugs' => array_keys( array_filter( self::$nfd_slugs, array( __CLASS__, 'check_approved' ) ) ),
245            'urls'      => array_keys( self::$urls, true, true ),
246            'domains'   => array_keys( self::$domains, true, true ),
247        );
248    }
249
250    /**
251     * Checks if a Plugin slug has been approved.
252     *
253     * @param array $value The Plugin slug that will be checked.
254     * @return boolean
255     */
256    private static function check_approved( $value ) {
257        return true === $value['approved'];
258    }
259
260    /**
261     * Prevent redirect to woo wizard after activation of woocommerce.
262     *
263     * @return void
264     */
265    public static function wc_prevent_redirect_on_activation() {
266        delete_transient( '_wc_activation_redirect' );
267    }
268
269
270    /**
271     * Prevent redirect to YOAST page after activation (free version).
272     *
273     * @return void
274     */
275    public static function wpseo_prevent_redirect_on_activation() {
276        $wpseo_options = get_option( 'wpseo' );
277
278        if ( is_array( $wpseo_options ) ) {
279            $wpseo_options['should_redirect_after_install_free'] = true;
280            if ( isset( $wpseo_options['activation_redirect_timestamp_free'] ) ) {
281                unset( $wpseo_options['activation_redirect_timestamp_free'] );
282            }
283            update_option( 'wpseo', $wpseo_options );
284        }
285    }
286
287    /**
288     * Prevent redirect to YOAST page after activation (premium version).
289     *
290     * @return void
291     */
292    public static function wpseo_premium_prevent_redirect_on_activation() {
293        $wpseo_options = get_option( 'wpseo_premium' );
294
295        if ( is_array( $wpseo_options ) ) {
296            $wpseo_options['should_redirect_after_install'] = true;
297            if ( isset( $wpseo_options['activation_redirect_timestamp'] ) ) {
298                unset( $wpseo_options['activation_redirect_timestamp'] );
299            }
300            update_option( 'wpseo_premium', $wpseo_options );
301        }
302    }
303
304    /**
305     * Activate or Deactivate Jetpack modules.
306     *
307     * @param string $module the name of the module to activate
308     * @param string $active the status of the module, pass true to activate and false to deactivate
309     *
310     * @return boolean
311     */
312    public static function toggle_jetpack_module( $module, $active = true ) {
313        $request = new \WP_REST_Request(
314            'POST',
315            '/jetpack/v4/settings'
316        );
317        $request->set_header( 'Content-Type', 'application/json' );
318        $request->set_body( wp_json_encode( array( $module => $active ) ) );
319        $response = rest_do_request( $request );
320
321        if ( 200 !== $response->status ) {
322            return false;
323        }
324        return true;
325    }
326}