Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default value for JetPack Boost #76

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions includes/Performance.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use function NewfoldLabs\WP\Module\Performance\is_settings_page;

use function NewfoldLabs\WP\ModuleLoader\container;

/**
* Main class for the performance module.
*/
Expand Down Expand Up @@ -88,6 +90,9 @@ public function __construct( Container $container ) {
}

add_filter( 'newfold-runtime', array( $this, 'add_to_runtime' ), 100 );

// Set default values for JetPack Boost on fresh installation.
add_action( 'admin_init', array( $this, 'handle_jetpack_boost_default_values' ) );
}

/**
Expand Down Expand Up @@ -327,12 +332,12 @@ public function enqueue_scripts() {
public function add_to_runtime( $sdk ) {
$values = array(
'jetpack_boost_is_active' => defined( 'JETPACK_BOOST_VERSION' ),
'jetpack_boost_premium_is_active' => $this->isJetPackBoostActive(),
'jetpack_boost_critical_css' => get_option( 'jetpack_boost_status_critical-css' ),
'jetpack_boost_blocking_js' => get_option( 'jetpack_boost_status_render-blocking-js' ),
'jetpack_boost_minify_js' => get_option( 'jetpack_boost_status_minify-js', array() ),
'jetpack_boost_premium_is_active' => $this->isJetPackBoostPremiumActive(),
'jetpack_boost_critical_css' => get_option( 'jetpack_boost_status_critical-css', false ),
'jetpack_boost_blocking_js' => get_option( 'jetpack_boost_status_render-blocking-js', true ),
'jetpack_boost_minify_js' => get_option( 'jetpack_boost_status_minify-js', false ),
'jetpack_boost_minify_js_excludes' => implode( ',', get_option( 'jetpack_boost_ds_minify_js_excludes', array( 'jquery', 'jquery-core', 'underscore', 'backbone' ) ) ),
'jetpack_boost_minify_css' => get_option( 'jetpack_boost_status_minify-css', array() ),
'jetpack_boost_minify_css' => get_option( 'jetpack_boost_status_minify-css', false ),
'jetpack_boost_minify_css_excludes' => implode( ',', get_option( 'jetpack_boost_ds_minify_css_excludes', array( 'admin-bar', 'dashicons', 'elementor-app' ) ) ),
'install_token' => PluginInstaller::rest_get_plugin_install_hash(),
'skip404' => getSkip404Option(),
Expand All @@ -347,7 +352,7 @@ public function add_to_runtime( $sdk ) {
*
* @return boolean
*/
public function isJetPackBoostActive() {
public function isJetPackBoostPremiumActive() {
$exists = false;
if ( class_exists( 'Automattic\Jetpack\Current_Plan' ) ) {
$products = Current_Plan::get_products();
Expand All @@ -361,4 +366,15 @@ public function isJetPackBoostActive() {

return $exists;
}

/**
* Set default values for JetPack Boost.
*
* @return void
*/
public function handle_jetpack_boost_default_values() {
if ( container()->has( 'isFreshInstallation' ) && container()->get( 'isFreshInstallation' ) && is_plugin_active( 'jetpack-boost/jetpack-boost.php' ) ) {
update_option( 'jetpack_boost_status_render-blocking-js', true );
}
}
}
22 changes: 22 additions & 0 deletions includes/PerformanceFeatureHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use NewfoldLabs\WP\Module\Performance\CacheTypes\File;
use NewfoldLabs\WP\Module\Performance\CacheTypes\Skip404;
use NewfoldLabs\WP\Module\Performance\ResponseHeaderManager;
use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller;

use function NewfoldLabs\WP\Module\Performance\getCacheLevel;
use function NewfoldLabs\WP\ModuleLoader\container;

/**
* Add activation/deactivation hooks for the performance feature.
Expand All @@ -22,6 +24,7 @@ public function __construct() {
if ( function_exists( 'add_action' ) ) {
add_action( 'newfold_container_set', array( $this, 'pluginHooks' ) );
add_action( 'plugins_loaded', array( $this, 'hooks' ) );
add_action( 'activated_plugin', array( $this, 'activate_jetpack_boost' ) );
}
}

Expand Down Expand Up @@ -66,4 +69,23 @@ public function onDeactivation() {
$responseHeaderManager = new ResponseHeaderManager();
$responseHeaderManager->removeAllHeaders();
}


/**
* Activate Jetpack Boost automatically on fresh installation
*
* @param string $plugin Plugin just activated.
* @return void
*/
public function activate_jetpack_boost( $plugin ) {
if ( container()->plugin()->basename === $plugin &&
container()->has( 'isFreshInstallation' ) &&
container()->get( 'isFreshInstallation' ) &&
isset( $_REQUEST['action'] ) && // phpcs:ignore WordPress.Security.NonceVerification
'activate' === $_REQUEST['action'] // phpcs:ignore WordPress.Security.NonceVerification
) {
add_filter( 'wp_doing_ajax', '__return_true' );
PluginInstaller::install( 'jetpack-boost', true );
}
}
}
Loading