Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
JetpackBoost
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 add_to_runtime
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
 is_jetpackpremium_active
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 prefetch_jetpack_boost
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 handle_jetpack_boost_default_values
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace NewfoldLabs\WP\Module\Performance\JetpackBoost;
4
5use NewfoldLabs\WP\ModuleLoader\Container;
6
7use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller;
8use Automattic\Jetpack\My_Jetpack\Products\Boost;
9
10/**
11 * Handles link prefetch functionality.
12 */
13class JetpackBoost {
14
15    /**
16     * Dependency injection container.
17     *
18     * @var Container
19     */
20    protected $container;
21
22    /**
23     * Constructor.
24     *
25     * @param Container $container The dependency injection container.
26     */
27    public function __construct( Container $container ) {
28        $this->container = $container;
29
30        add_filter( 'newfold-runtime', array( $this, 'add_to_runtime' ), 100 );
31
32        add_action( 'admin_head', array( $this, 'prefetch_jetpack_boost' ) );
33
34        // Set default values for JetPack Boost on fresh installation.
35        add_action( 'admin_init', array( $this, 'handle_jetpack_boost_default_values' ) );
36    }
37
38    /**
39     * Add to Newfold SDK runtime.
40     *
41     * @param array $sdk SDK data.
42     * @return array SDK data.
43     */
44    public function add_to_runtime( $sdk ) {
45        $is_jetpack_boost_enabled = is_plugin_active( 'jetpack-boost/jetpack-boost.php' );
46
47        if ( isset( $_GET['page'] ) && 'nfd-performance' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
48            delete_option( 'jb_get_started' );
49        }
50
51        $values = array(
52            'is_active'                 => $is_jetpack_boost_enabled,
53            'jetpack_premium_is_active' => $this->is_jetpackpremium_active(),
54            'critical_css'              => $is_jetpack_boost_enabled ? get_option( 'jetpack_boost_status_critical-css' ) : false,
55            'blocking_js'               => $is_jetpack_boost_enabled ? get_option( 'jetpack_boost_status_render-blocking-js' ) : false,
56            'minify_js'                 => $is_jetpack_boost_enabled ? get_option( 'jetpack_boost_status_minify-js', false ) : false,
57            'minify_js_excludes'        => implode( ',', get_option( 'jetpack_boost_ds_minify_js_excludes', array( 'jquery', 'jquery-core', 'underscore', 'backbone' ) ) ),
58            'minify_css'                => $is_jetpack_boost_enabled ? get_option( 'jetpack_boost_status_minify-css', false ) : false,
59            'minify_css_excludes'       => implode( ',', get_option( 'jetpack_boost_ds_minify_css_excludes', array( 'admin-bar', 'dashicons', 'elementor-app' ) ) ),
60            'install_token'             => PluginInstaller::rest_get_plugin_install_hash(),
61        );
62
63        return array_merge( $sdk, array( 'jetpackboost' => $values ) );
64    }
65
66    /**
67     * Check if Jetpack Boost premium is active.
68     *
69     * @return boolean
70     */
71    public function is_jetpackpremium_active() {
72        if ( ! class_exists( Boost::class ) ) {
73            return false;
74        }
75
76        $info = Boost::get_info();
77        return array_key_exists( 'is_upgradable', $info )
78            ? ! $info['is_upgradable']
79            : false;
80    }
81
82    /**
83     * Prefetch for JetPack Boost page.
84     *
85     * @return void
86     */
87    public function prefetch_jetpack_boost() {
88        if ( is_plugin_active( 'jetpack-boost/jetpack-boost.php' ) ) {
89            $admin_url = admin_url( 'admin.php?page=jetpack-boost' );
90            echo '<link rel="prefetch" href="' . esc_url( $admin_url ) . '">' . "\n";
91        }
92    }
93
94    /**
95     * Set default values for JetPack Boost.
96     *
97     * @return void
98     */
99    public function handle_jetpack_boost_default_values() {
100        if ( $this->container->has( 'isFreshInstallation' ) && $this->container->get( 'isFreshInstallation' ) && is_plugin_active( 'jetpack-boost/jetpack-boost.php' ) ) {
101            update_option( 'jetpack_boost_status_render-blocking-js', true );
102        }
103    }
104}