add wp-rocket
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DelayJS\Admin;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
|
||||
class Settings {
|
||||
/**
|
||||
* Array of defaults scripts to delay
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $defaults = [
|
||||
'getbutton.io',
|
||||
'//a.omappapi.com/app/js/api.min.js',
|
||||
'feedbackcompany.com/includes/widgets/feedback-company-widget.min.js',
|
||||
'snap.licdn.com/li.lms-analytics/insight.min.js',
|
||||
'static.ads-twitter.com/uwt.js',
|
||||
'platform.twitter.com/widgets.js',
|
||||
'twq(',
|
||||
'/sdk.js#xfbml',
|
||||
'static.leadpages.net/leadbars/current/embed.js',
|
||||
'translate.google.com/translate_a/element.js',
|
||||
'widget.manychat.com',
|
||||
'xfbml.customerchat.js',
|
||||
'static.hotjar.com/c/hotjar-',
|
||||
'smartsuppchat.com/loader.js',
|
||||
'grecaptcha.execute',
|
||||
'Tawk_API',
|
||||
'shareaholic',
|
||||
'sharethis',
|
||||
'simple-share-buttons-adder',
|
||||
'addtoany',
|
||||
'font-awesome',
|
||||
'wpdiscuz',
|
||||
'cookie-law-info',
|
||||
'pinit.js',
|
||||
'/gtag/js',
|
||||
'gtag(',
|
||||
'/gtm.js',
|
||||
'/gtm-',
|
||||
'fbevents.js',
|
||||
'fbq(',
|
||||
'google-analytics.com/analytics.js',
|
||||
'ga( \'',
|
||||
'ga(\'',
|
||||
'adsbygoogle.js',
|
||||
'ShopifyBuy',
|
||||
'widget.trustpilot.com/bootstrap',
|
||||
'ft.sdk.min.js',
|
||||
'apps.elfsight.com/p/platform.js',
|
||||
'livechatinc.com/tracking.js',
|
||||
'LiveChatWidget',
|
||||
'/busting/facebook-tracking/',
|
||||
'olark',
|
||||
'pixel-caffeine/build/frontend.js',
|
||||
];
|
||||
|
||||
/**
|
||||
* Instance of options handler.
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Creates an instance of the class.
|
||||
*
|
||||
* @param Options_Data $options WP Rocket Options instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options ) {
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the delay JS options to the WP Rocket options array
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_options( $options ) {
|
||||
$options = (array) $options;
|
||||
|
||||
$options['delay_js'] = 1;
|
||||
$options['delay_js_scripts'] = $this->defaults;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the data to populate the view for the restore defaults button
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_button_data() {
|
||||
return [
|
||||
'type' => 'button',
|
||||
'action' => 'rocket_delay_js_restore_defaults',
|
||||
'attributes' => [
|
||||
'label' => __( 'Restore Defaults', 'rocket' ),
|
||||
'attributes' => [
|
||||
'class' => 'wpr-button wpr-button--icon wpr-button--purple wpr-icon-refresh',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the delay_js option to zero when updating to 3.7
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @param string $old_version Previous plugin version.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set_option_on_update( $old_version ) {
|
||||
if ( version_compare( $old_version, '3.7', '>' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$options = get_option( 'wp_rocket_settings', [] );
|
||||
|
||||
$options['delay_js'] = 0;
|
||||
$options['delay_js_scripts'] = $this->defaults;
|
||||
|
||||
update_option( 'wp_rocket_settings', $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update delay_js options when updating to ver 3.7.4
|
||||
*
|
||||
* @since 3.7.4
|
||||
*
|
||||
* @param string $old_version Old plugin version.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function option_update_3_7_4( $old_version ) {
|
||||
if ( version_compare( $old_version, '3.7.4', '>' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$options = get_option( 'wp_rocket_settings', [] );
|
||||
$delay_js_scripts = array_flip( $options['delay_js_scripts'] );
|
||||
|
||||
if ( isset( $delay_js_scripts['adsbygoogle'] ) ) {
|
||||
$delay_js_scripts['adsbygoogle.js'] = $delay_js_scripts['adsbygoogle'];
|
||||
|
||||
unset( $delay_js_scripts['adsbygoogle'] );
|
||||
}
|
||||
|
||||
$options['delay_js_scripts'] = array_values( array_flip( $delay_js_scripts ) );
|
||||
|
||||
update_option( 'wp_rocket_settings', $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update delay_js options when updating to ver 3.7.2.
|
||||
*
|
||||
* @since 3.7.2
|
||||
*
|
||||
* @param string $old_version Old plugin version.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function option_update_3_7_2( $old_version ) {
|
||||
if ( version_compare( $old_version, '3.7.2', '>' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$options = get_option( 'wp_rocket_settings', [] );
|
||||
|
||||
$delay_js_scripts = array_flip( $options['delay_js_scripts'] );
|
||||
|
||||
if (
|
||||
isset( $delay_js_scripts['fbq('] )
|
||||
&&
|
||||
! isset( $delay_js_scripts['pixel-caffeine/build/frontend.js'] )
|
||||
) {
|
||||
$delay_js_scripts['pixel-caffeine/build/frontend.js'] = '';
|
||||
}
|
||||
|
||||
if ( isset( $delay_js_scripts['google.com/recaptcha/api.js'] ) ) {
|
||||
unset( $delay_js_scripts['google.com/recaptcha/api.js'] );
|
||||
}
|
||||
|
||||
if ( isset( $delay_js_scripts['widget.trustpilot.com'] ) ) {
|
||||
$delay_js_scripts['widget.trustpilot.com/bootstrap'] = $delay_js_scripts['widget.trustpilot.com'];
|
||||
|
||||
unset( $delay_js_scripts['widget.trustpilot.com'] );
|
||||
}
|
||||
|
||||
$options['delay_js_scripts'] = array_values( array_flip( $delay_js_scripts ) );
|
||||
|
||||
update_option( 'wp_rocket_settings', $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores the delay_js_scripts option to the default value
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public function restore_defaults() {
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return implode( "\n", $this->defaults );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\DelayJS\Admin;
|
||||
|
||||
use WP_Rocket\Abstract_Render;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
class Subscriber extends Abstract_Render implements Subscriber_Interface {
|
||||
/**
|
||||
* Settings instance
|
||||
*
|
||||
* @var Settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param Settings $settings Settings instance.
|
||||
* @param string $template_path Template path.
|
||||
*/
|
||||
public function __construct( Settings $settings, $template_path ) {
|
||||
parent::__construct( $template_path );
|
||||
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber listens to.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'rocket_first_install_options' => 'add_options',
|
||||
'rocket_after_textarea_field_delay_js_scripts' => 'display_restore_defaults_button',
|
||||
'wp_rocket_upgrade' => [
|
||||
[ 'set_option_on_update', 13, 2 ],
|
||||
[ 'option_update_3_7_2', 13, 2 ],
|
||||
[ 'option_update_3_7_4', 13, 2 ],
|
||||
],
|
||||
'wp_ajax_rocket_restore_delay_js_defaults' => 'restore_defaults',
|
||||
'rocket_safe_mode_reset_options' => 'add_options',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the delay JS options to the WP Rocket options array
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_options( $options ) {
|
||||
return $this->settings->add_options( $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the restore defaults button under the textarea field
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function display_restore_defaults_button() {
|
||||
$data = $this->settings->get_button_data();
|
||||
|
||||
$this->render_action_button(
|
||||
$data['type'],
|
||||
$data['action'],
|
||||
$data['attributes']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the delay_js option to zero when updating to 3.7
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @param string $new_version New plugin version.
|
||||
* @param string $old_version Previous plugin version.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set_option_on_update( $new_version, $old_version ) {
|
||||
$this->settings->set_option_on_update( $old_version );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the delay_js options when updating to 3.7.2.
|
||||
*
|
||||
* @since 3.7.2
|
||||
*
|
||||
* @param string $new_version New plugin version.
|
||||
* @param string $old_version Old plugin version.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function option_update_3_7_2( $new_version, $old_version ) {
|
||||
$this->settings->option_update_3_7_2( $old_version );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the delay_js options when updating to 3.7.4
|
||||
*
|
||||
* @since 3.7.4
|
||||
*
|
||||
* @param string $new_version New plugin version.
|
||||
* @param string $old_version Old plugin version.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function option_update_3_7_4( $new_version, $old_version ) {
|
||||
$this->settings->option_update_3_7_4( $old_version );
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX callback to restore the default value for the delay JS scripts
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function restore_defaults() {
|
||||
check_ajax_referer( 'rocket-ajax', 'nonce', true );
|
||||
|
||||
$result = $this->settings->restore_defaults();
|
||||
|
||||
if ( false === $result ) {
|
||||
wp_send_json_error();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
wp_send_json_success( $result );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user