add wp-rocket

This commit is contained in:
nguyen dung
2022-02-18 19:09:35 +07:00
parent 39b8cb3612
commit 3110d00ee7
927 changed files with 271703 additions and 2 deletions

View File

@@ -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 );
}
}

View File

@@ -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 );
}
}

View File

@@ -0,0 +1,175 @@
<?php
namespace WP_Rocket\Engine\Optimization\DelayJS;
use WP_Rocket\Admin\Options_Data;
class HTML {
/**
* Plugin options instance.
*
* @since 3.7
*
* @var Options_Data
*/
protected $options;
/**
* Allowed scripts regex.
*
* @since 3.7
*
* @var string
*/
private $allowed_scripts = '';
/**
* Creates an instance of HTML.
*
* @since 3.7
*
* @param Options_Data $options Plugin options instance.
*/
public function __construct( Options_Data $options ) {
$this->options = $options;
}
/**
* Adjust HTML to have delay js structure.
*
* @param string $html Buffer html for the page.
*
* @return string
*/
public function delay_js( $html ) {
if ( ! $this->is_allowed() ) {
return $html;
}
$this->allowed_scripts = $this->prepare_allowed_scripts_regex();
if ( empty( $this->allowed_scripts ) ) {
return $html;
}
return $this->parse( $html );
}
/**
* Checks if is allowed to Delay JS.
*
* @since 3.7
*
* @return bool
*/
public function is_allowed() {
if ( rocket_bypass() ) {
return false;
}
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) {
return false;
}
if ( is_rocket_post_excluded_option( 'delay_js' ) ) {
return false;
}
return (bool) $this->options->get( 'delay_js', 0 );
}
/**
* Parse the html and add/remove attributes from specific scripts.
*
* @param string $html Buffer html for the page.
*
* @return string
*/
private function parse( $html ) {
$replaced_html = preg_replace_callback( '/<script\s*(?<attr>[^>]*)?>(?<content>.*)?<\/script>/Uims', [ $this, 'replace_scripts' ], $html );
if ( empty( $replaced_html ) ) {
return $html;
}
return $replaced_html;
}
/**
* Callback method for preg_replace_callback that is used to adjust attributes for specific scripts.
*
* @param array $matches Matches array for scripts regex.
*
* @return string
*/
public function replace_scripts( $matches ) {
if (
empty( $this->allowed_scripts )
||
(
! empty( $this->allowed_scripts )
&&
! preg_match( '#(' . $this->allowed_scripts . ')#', $matches[0] )
)
) {
return $matches[0];
}
$src = '';
$matches['attr'] = trim( $matches['attr'] );
if ( ! empty( $matches['attr'] ) ) {
if ( preg_match( '/src=(["\'])(.*?)\1/', $matches['attr'], $src_matches ) ) {
$src = $src_matches[2];
// Remove the src attribute.
$matches['attr'] = str_replace( $src_matches[0], '', $matches['attr'] );
}
}
if ( empty( $src ) ) {
// Get the JS content.
if ( ! empty( $matches['content'] ) ) {
$src = 'data:text/javascript;base64,' . base64_encode( $matches['content'] );// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
}
}
if ( empty( $src ) ) {
return $matches[0];
}
return "<script data-rocketlazyloadscript='{$src}' {$matches['attr']}></script>";
}
/**
* Prepare allowed scripts to be used as regex.
*
* @return string
*/
private function prepare_allowed_scripts_regex() {
$delay_js_scripts = $this->options->get( 'delay_js_scripts', [] );
/**
* Filters JS files to included into delay JS.
*
* @since 3.7
*
* @param array $delay_js_scripts List of allowed JS files.
*/
$delay_js_scripts = (array) apply_filters( 'rocket_delay_js_scripts', $delay_js_scripts );
if ( empty( $delay_js_scripts ) ) {
return '';
}
foreach ( $delay_js_scripts as $i => $delay_js_script ) {
$delay_js_scripts[ $i ] = preg_quote( str_replace( '#', '\#', $delay_js_script ), '#' );
}
return implode( '|', $delay_js_scripts );
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace WP_Rocket\Engine\Optimization\DelayJS;
use WP_Rocket\Engine\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service provider for the WP Rocket Delay JS
*
* @since 3.7
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'delay_js_settings',
'delay_js_admin_subscriber',
];
/**
* Registers the option array in the container
*
* @return void
*/
public function register() {
$this->getContainer()->add( 'delay_js_settings', 'WP_Rocket\Engine\Optimization\DelayJS\Admin\Settings' )
->withArgument( $this->getContainer()->get( 'options' ) );
$this->getContainer()->share( 'delay_js_admin_subscriber', 'WP_Rocket\Engine\Optimization\DelayJS\Admin\Subscriber' )
->withArgument( $this->getContainer()->get( 'delay_js_settings' ) )
->withArgument( $this->getContainer()->get( 'template_path' ) . '/settings' );
}
}

View File

@@ -0,0 +1,130 @@
<?php
namespace WP_Rocket\Engine\Optimization\DelayJS;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Subscriber implements Subscriber_Interface {
/**
* HTML instance.
*
* @since 3.7
*
* @var HTML
*/
private $html;
/**
* WP_Filesystem_Direct instance.
*
* @since 3.7
*
* @var \WP_Filesystem_Direct
*/
private $filesystem;
/**
* Script enqueued status.
*
* @since 3.7
* @var bool
*/
private $is_enqueued = false;
/**
* Subscriber constructor.
*
* @param HTML $html HTML Instance.
* @param \WP_Filesystem_Direct $filesystem The Filesystem object.
*/
public function __construct( HTML $html, $filesystem ) {
$this->html = $html;
$this->filesystem = $filesystem;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.7
*
* @return array
*/
public static function get_subscribed_events() {
return [
'rocket_buffer' => [
[ 'delay_js', 21 ],
],
'wp_enqueue_scripts' => 'add_delay_js_script',
];
}
/**
* Using html buffer get scripts to be delayed and adjust their html.
*
* @param string $buffer_html Html for the page.
*
* @return string
*/
public function delay_js( $buffer_html ) {
return $this->html->delay_js( $buffer_html );
}
/**
* Adds the inline script to the footer when the option is enabled.
*
* @since 3.7
*
* @return void
*/
public function add_delay_js_script() {
if ( $this->is_enqueued ) {
return;
}
if ( ! $this->html->is_allowed() ) {
return;
}
$js_assets_path = rocket_get_constant( 'WP_ROCKET_PATH' ) . 'assets/js/';
if ( ! wp_script_is( 'rocket-browser-checker' ) ) {
$checker_filename = rocket_get_constant( 'SCRIPT_DEBUG' ) ? 'browser-checker.js' : 'browser-checker.min.js';
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
wp_register_script(
'rocket-browser-checker',
'',
[],
'',
true
);
wp_enqueue_script( 'rocket-browser-checker' );
wp_add_inline_script(
'rocket-browser-checker',
$this->filesystem->get_contents( "{$js_assets_path}{$checker_filename}" )
);
}
// Register handle with no src to add the inline script after.
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
wp_register_script(
'rocket-delay-js',
'',
[
'rocket-browser-checker',
],
'',
true
);
wp_enqueue_script( 'rocket-delay-js' );
$script_filename = rocket_get_constant( 'SCRIPT_DEBUG' ) ? 'lazyload-scripts.js' : 'lazyload-scripts.min.js';
wp_add_inline_script(
'rocket-delay-js',
$this->filesystem->get_contents( "{$js_assets_path}{$script_filename}" )
);
$this->is_enqueued = true;
}
}