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