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,214 @@
<?php
namespace WP_Rocket\Engine\Media\Embeds;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\ThirdParty\ReturnTypesTrait;
/**
* Event subscriber to control Embeds behavior.
*
* @since 3.7 Moved to new architecture.
* @since 3.2
*/
class EmbedsSubscriber implements Subscriber_Interface {
use ReturnTypesTrait;
/**
* The Options Data instance.
*
* @var Options_Data
*/
private $options;
/**
* EmbedsSubscriber constructor.
*
* @param Options_Data $options An Options Data instance.
*/
public function __construct( Options_Data $options ) {
$this->options = $options;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.7
*
* @return array
*/
public static function get_subscribed_events() {
return [
'init' => [ 'remove_wp_vars_and_hooks', 9999 ],
'rest_endpoints' => 'remove_embed_endpoint',
'oembed_response_data' => 'empty_oembed_response_data',
'embed_oembed_discover' => 'return_false',
'rewrite_rules_array' => 'remove_embeds_rewrite_rules',
'enqueue_block_editor_assets' => 'enqueue_disable_embeds_script',
'wp_default_scripts' => 'remove_wp_embed_dependency',
];
}
/**
* Remove WP Query Vars and hooks relating to embeds.
*
* Replaces old architecture's rocket_disable_embeds_init().
*
* @since 3.7
*
* @return void
*/
public function remove_wp_vars_and_hooks() {
if ( ! $this->can_disable_embeds() ) {
return;
}
global $wp;
// Remove the embed query var.
$wp->public_query_vars = array_diff(
$wp->public_query_vars,
[
'embed',
]
);
// Don't filter oEmbed results.
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
// Remove oEmbed discovery links.
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
// Remove oEmbed-specific JavaScript from the front-end and back-end.
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
// Remove filter of the oEmbed result before any HTTP requests are made.
remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 );
}
/**
* Remove all rewrite rules related to embeds.
*
* @since 3.7 Moved to new architecture.
* @since 2.10
*
* @param array $rules WordPress rewrite rules.
*
* @return array Rewrite rules without embeds rules.
*/
public function remove_embeds_rewrite_rules( $rules ) {
if ( empty( $rules ) || ! $this->can_disable_embeds() ) {
return $rules;
}
foreach ( $rules as $rule => $rewrite ) {
if ( false !== strpos( $rewrite, 'embed=true' ) ) {
unset( $rules[ $rule ] );
}
}
return $rules;
}
/**
* Remove the oembed/1.0/embed REST route.
*
* @since 3.7 Moved to new architecture.
* @since 3.3.3
*
* @param array $endpoints Registered REST API endpoints.
*
* @return array Filtered REST API endpoints.
*/
public function remove_embed_endpoint( $endpoints ) {
if ( ! $this->can_disable_embeds() ) {
return $endpoints;
}
unset( $endpoints['/oembed/1.0/embed'] );
return $endpoints;
}
/**
* Disables sending internal oEmbed response data in proxy endpoint.
*
* @since 3.7 Moved to new architecture.
* @since 3.3.3
*
* @param array $data The response data.
*
* @return array Response data
*/
public function empty_oembed_response_data( $data ) {
if (
! rocket_get_constant( 'REST_REQUEST' )
||
! $this->can_disable_embeds()
) {
return $data;
}
return [];
}
/**
* Enqueue JavaScript for the block editor.
*
* This is used to unregister the `core-embed/wordpress` block type.
*
* @since 3.7 Moved to new architecture.
* @since 3.3.3
*
* @return void
*/
public function enqueue_disable_embeds_script() {
if ( ! $this->can_disable_embeds() ) {
return;
}
wp_enqueue_script(
'rocket-disable-embeds',
rocket_get_constant( WP_ROCKET_ASSETS_JS_URL ) . 'editor/editor.js',
[
'wp-edit-post',
'wp-editor',
'wp-dom',
],
'1.0',
true
);
}
/**
* Remove wp-embed dependency of core packages.
*
* @since 3.7 Moved to new architecture.
* @since 3.3.3
*
* @param \WP_Scripts $scripts WP_Scripts instance, passed by reference.
*/
public function remove_wp_embed_dependency( $scripts ) {
if ( ! $this->can_disable_embeds() ) {
return;
}
if ( ! empty( $scripts->registered['wp-edit-post'] ) ) {
$scripts->registered['wp-edit-post']->deps = array_diff(
$scripts->registered['wp-edit-post']->deps,
[ 'wp-embed' ]
);
}
}
/**
* Check for embeds enabled.
*
* @since 3.7
*
* @return bool
*/
private function can_disable_embeds() {
return ! rocket_bypass() && (bool) $this->options->get( 'embeds', 0 );
}
}

View File

@@ -0,0 +1,95 @@
<?php
namespace WP_Rocket\Engine\Media\Emojis;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Event subscriber to control Emoji behavior.
*
* @since 3.7
*/
class EmojisSubscriber implements Subscriber_Interface {
/**
* The Options Data instance.
*
* @var Options_Data
*/
private $options;
/**
* EmojisSubscriber constructor.
*
* @param Options_Data $options An Options Data instance.
*/
public function __construct( Options_Data $options ) {
$this->options = $options;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.7
*
* @return array
*/
public static function get_subscribed_events() {
return [
'init' => 'disable_emoji',
'tiny_mce_plugins' => 'disable_emoji_tinymce',
];
}
/**
* Disable the emoji functionality to reduce then number of external HTTP requests.
*
* @since 3.7 Moved to new architecture.
* @since 2.7
*/
public function disable_emoji() {
if ( ! $this->can_disable_emoji() ) {
return;
}
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'emoji_svg_url', '__return_false' );
}
/**
* Remove the tinymce emoji plugin.
*
* @since 3.7 Moved to new architecture.
* @since 2.7
*
* @param array $plugins Plugins loaded for TinyMCE.
*
* @return array
*/
public function disable_emoji_tinymce( $plugins ) {
if ( ! $this->can_disable_emoji() ) {
return $plugins;
}
if ( is_array( $plugins ) ) {
return array_diff( $plugins, [ 'wpemoji' ] );
}
return [];
}
/**
* Check for emoji option enabled & not bypassed.
*
* @since 3.7
*
* @return bool
*/
private function can_disable_emoji() {
return ! rocket_bypass() && (bool) $this->options->get( 'emoji', 0 );
}
}

View File

@@ -0,0 +1,546 @@
<?php
namespace WP_Rocket\Engine\Media;
use WP_Rocket\Dependencies\Minify\JS;
use WP_Rocket\Dependencies\RocketLazyload\Assets;
use WP_Rocket\Dependencies\RocketLazyload\Image;
use WP_Rocket\Dependencies\RocketLazyload\Iframe;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Lazyload Subscriber
*
* @since 3.3
*/
class LazyloadSubscriber implements Subscriber_Interface {
const SCRIPT_VERSION = '16.1';
/**
* Options_Data instance
*
* @var Options_Data
*/
private $options;
/**
* Assets instance
*
* @var Assets
*/
private $assets;
/**
* Image instance
*
* @var Image
*/
private $image;
/**
* Iframe instance
*
* @var Iframe
*/
private $iframe;
/**
* Constructor
*
* @since 3.3
*
* @param Options_Data $options Options_Data instance.
* @param Assets $assets Assets instance.
* @param Image $image Image instance.
* @param Iframe $iframe Iframe instance.
*/
public function __construct( Options_Data $options, Assets $assets, Image $image, Iframe $iframe ) {
$this->options = $options;
$this->assets = $assets;
$this->image = $image;
$this->iframe = $iframe;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.3
*
* @return array
*/
public static function get_subscribed_events() {
return [
'wp_footer' => [
[ 'insert_lazyload_script', PHP_INT_MAX ],
[ 'insert_youtube_thumbnail_script', PHP_INT_MAX ],
],
'wp_head' => [ 'insert_nojs_style', PHP_INT_MAX ],
'wp_enqueue_scripts' => [ 'insert_youtube_thumbnail_style', PHP_INT_MAX ],
'rocket_buffer' => [ 'lazyload', 18 ],
'rocket_lazyload_html' => 'lazyload_responsive',
'init' => 'lazyload_smilies',
'wp' => 'deactivate_lazyload_on_specific_posts',
'wp_lazy_loading_enabled' => 'maybe_disable_core_lazyload',
];
}
/**
* Inserts the lazyload script in the footer
*
* @since 3.3
*
* @return void
*/
public function insert_lazyload_script() {
if ( ! $this->can_lazyload_images() && ! $this->can_lazyload_iframes() ) {
return;
}
if ( ! $this->should_lazyload() ) {
return;
}
/**
* Filters the use of the polyfill for intersectionObserver
*
* @since 3.3
* @author Remy Perona
*
* @param bool $polyfill True to use the polyfill, false otherwise.
*/
$polyfill = (bool) apply_filters( 'rocket_lazyload_polyfill', false );
$script_args = [
'base_url' => rocket_get_constant( 'WP_ROCKET_ASSETS_JS_URL' ) . 'lazyload/',
'version' => self::SCRIPT_VERSION,
'polyfill' => $polyfill,
];
$this->add_inline_script();
$this->assets->insertLazyloadScript( $script_args );
}
/**
* Adds the inline lazyload script
*
* @since 3.6
*
* @return void
*/
private function add_inline_script() {
$inline_script = $this->assets->getInlineLazyloadScript( $this->set_inline_script_args() );
if ( ! rocket_get_constant( 'SCRIPT_DEBUG' ) ) {
$inline_script = $this->minify_script( $inline_script );
}
echo '<script>' . $inline_script . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Sets the arguments array for the inline lazyload script
*
* @since 3.6
*
* @return array
*/
private function set_inline_script_args() {
/**
* Filters the threshold at which lazyload is triggered
*
* @since 1.2
* @author Remy Perona
*
* @param int $threshold Threshold value.
*/
$threshold = (int) apply_filters( 'rocket_lazyload_threshold', 300 );
$inline_args = [
'threshold' => $threshold,
];
/**
* Filters the use of native lazyload
*
* @since 3.4
* @author Remy Perona
*
* @param bool $use_native True to use native lazyload, false otherwise.
*/
if ( (bool) apply_filters( 'rocket_use_native_lazyload', false ) ) {
$inline_args['options'] = [
'use_native' => 'true',
];
$inline_args['elements']['loading'] = '[loading=lazy]';
}
if ( $this->options->get( 'lazyload', 0 ) ) {
$inline_args['elements']['image'] = 'img[data-lazy-src]';
$inline_args['elements']['background_image'] = '.rocket-lazyload';
}
if ( (bool) $this->options->get( 'lazyload_iframes', 0 ) ) {
$inline_args['elements']['iframe'] = 'iframe[data-lazy-src]';
}
/**
* Filters the arguments array for the lazyload script options
*
* @since 3.3
* @author Remy Perona
*
* @param array $inline_args Arguments used for the lazyload script options.
*/
return (array) apply_filters( 'rocket_lazyload_script_args', $inline_args );
}
/**
* Minifies the inline script
*
* @since 3.6
*
* @param string $script Inline script to minify.
* @return string
*/
private function minify_script( $script ) {
$minify = new JS( $script );
return $minify->minify();
}
/**
* Inserts the Youtube thumbnail script in the footer
*
* @since 3.3
*
* @return void
*/
public function insert_youtube_thumbnail_script() {
if ( ! $this->options->get( 'lazyload_youtube' ) || ! $this->can_lazyload_iframes() ) {
return;
}
if ( ! $this->should_lazyload() ) {
return;
}
/**
* Filters the resolution of the YouTube thumbnail
*
* @since 1.4.8
* @deprecated 3.3
* @author Arun Basil Lal
*
* @param string $thumbnail_resolution The resolution of the thumbnail. Accepted values: default, mqdefault, hqdefault, sddefault, maxresdefault
*/
$thumbnail_resolution = apply_filters_deprecated( 'rocket_youtube_thumbnail_resolution', [ 'hqdefault' ], '3.3', 'rocket_lazyload_youtube_thumbnail_resolution' );
/**
* Filters the resolution of the YouTube thumbnail
*
* @since 1.4.8
* @author Arun Basil Lal
*
* @param string $thumbnail_resolution The resolution of the thumbnail. Accepted values: default, mqdefault, hqdefault, sddefault, maxresdefault
*/
$thumbnail_resolution = apply_filters( 'rocket_lazyload_youtube_thumbnail_resolution', $thumbnail_resolution );
$this->assets->insertYoutubeThumbnailScript(
[
'resolution' => $thumbnail_resolution,
'lazy_image' => (bool) $this->options->get( 'lazyload' ),
]
);
}
/**
* Inserts the no JS CSS compatibility in the header
*
* @since 3.3
*
* @return void
*/
public function insert_nojs_style() {
if ( ! $this->should_lazyload() ) {
return;
}
if ( ! $this->can_lazyload_images() && ! $this->can_lazyload_iframes() ) {
return;
}
if ( ! $this->options->get( 'lazyload' ) && ! $this->options->get( 'lazyload_iframes' ) ) {
return;
}
$this->assets->insertNoJSCSS();
}
/**
* Inserts the Youtube thumbnail CSS in the header
*
* @since 3.3
*
* @return void
*/
public function insert_youtube_thumbnail_style() {
if ( ! $this->options->get( 'lazyload_youtube' ) || ! $this->can_lazyload_iframes() ) {
return;
}
if ( ! $this->should_lazyload() ) {
return;
}
$this->assets->insertYoutubeThumbnailCSS(
[
'base_url' => WP_ROCKET_ASSETS_URL,
'responsive_embeds' => current_theme_supports( 'responsive-embeds' ),
]
);
}
/**
* Checks if lazyload should be applied
*
* @since 3.3
*
* @return bool
*/
private function should_lazyload() {
if (
rocket_get_constant( 'REST_REQUEST', false )
||
rocket_get_constant( 'DONOTLAZYLOAD', false )
||
rocket_get_constant( 'DONOTROCKETOPTIMIZE', false )
) {
return false;
}
if (
is_admin()
||
is_feed()
||
is_preview()
) {
return false;
}
if (
is_search()
&&
// This filter is documented in inc/classes/Buffer/class-tests.php.
! (bool) apply_filters( 'rocket_cache_search', false )
) {
return false;
}
// Exclude Page Builders editors.
$excluded_parameters = [
'fl_builder',
'et_fb',
'ct_builder',
];
foreach ( $excluded_parameters as $excluded ) {
if ( isset( $_GET[ $excluded ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return false;
}
}
return true;
}
/**
* Applies lazyload on the provided content
*
* @since 3.3
*
* @param string $html HTML content.
* @return string
*/
public function lazyload( $html ) {
if ( ! $this->should_lazyload() ) {
return $html;
}
$buffer = $this->ignore_scripts( $html );
$buffer = $this->ignore_noscripts( $buffer );
if ( $this->can_lazyload_iframes() ) {
$args = [
'youtube' => $this->options->get( 'lazyload_youtube' ),
];
$html = $this->iframe->lazyloadIframes( $html, $buffer, $args );
}
if ( $this->can_lazyload_images() ) {
$html = $this->image->lazyloadPictures( $html, $buffer );
$html = $this->image->lazyloadImages( $html, $buffer );
/**
* Filters the application of lazyload on background images
*
* @since 3.3
*
* @param bool $lazyload True to apply, false otherwise.
*/
if ( apply_filters( 'rocket_lazyload_background_images', true ) ) {
$html = $this->image->lazyloadBackgroundImages( $html, $buffer );
}
}
return $html;
}
/**
* Applies lazyload on responsive images attributes srcset and sizes
*
* @since 3.3
*
* @param string $html Image HTML.
* @return string
*/
public function lazyload_responsive( $html ) {
return $this->image->lazyloadResponsiveAttributes( $html );
}
/**
* Applies lazyload on WordPress smilies
*
* @since 3.3
*
* @return void
*/
public function lazyload_smilies() {
if ( ! $this->should_lazyload() ) {
return;
}
if ( ! $this->options->get( 'lazyload' ) ) {
return;
}
$filters = [
'the_content' => 10,
'the_excerpt' => 10,
'comment_text' => 20,
];
foreach ( $filters as $filter => $prio ) {
if ( ! has_filter( $filter ) ) {
continue;
}
remove_filter( $filter, 'convert_smilies', $prio );
add_filter( $filter, [ $this->image, 'convertSmilies' ], $prio );
}
}
/**
* Prevents lazyload if the option is unchecked on the WP Rocket options metabox for a post
*
* @since 3.3
*
* @return void
*/
public function deactivate_lazyload_on_specific_posts() {
if ( is_rocket_post_excluded_option( 'lazyload' ) ) {
add_filter( 'do_rocket_lazyload', '__return_false' );
}
if ( is_rocket_post_excluded_option( 'lazyload_iframes' ) ) {
add_filter( 'do_rocket_lazyload_iframes', '__return_false' );
}
}
/**
* Disable WP core lazyload if our images lazyload is active
*
* @since 3.5
*
* @param bool $value Current value for the enabling variable.
* @return bool
*/
public function maybe_disable_core_lazyload( $value ) {
if ( false === $value ) {
return $value;
}
return ! (bool) $this->can_lazyload_images();
}
/**
* Remove inline scripts from the HTML to parse
*
* @since 3.3
*
* @param string $html HTML content.
* @return string
*/
private function ignore_scripts( $html ) {
return preg_replace( '/<script\b(?:[^>]*)>(?:.+)?<\/script>/Umsi', '', $html );
}
/**
* Checks if we can lazyload images.
*
* @since 3.3
*
* @return boolean
*/
private function can_lazyload_images() {
if ( ! $this->options->get( 'lazyload', 0 ) ) {
return false;
}
/**
* Filters the lazyload application on images
*
* @since 2.0
* @author Remy Perona
*
* @param bool $do_rocket_lazyload True to apply lazyload, false otherwise.
*/
return apply_filters( 'do_rocket_lazyload', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
}
/**
* Checks if we can lazyload iframes
*
* @since 3.3
*
* @return boolean
*/
private function can_lazyload_iframes() {
if ( ! $this->options->get( 'lazyload_iframes', 0 ) ) {
return false;
}
/**
* Filters the lazyload application on iframes
*
* @since 2.0
* @author Remy Perona
*
* @param bool $do_rocket_lazyload_iframes True to apply lazyload, false otherwise.
*/
return apply_filters( 'do_rocket_lazyload_iframes', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
}
/**
* Remove noscript tags from the HTML to parse
*
* @since 3.3
*
* @param string $html HTML content.
* @return string
*/
private function ignore_noscripts( $html ) {
return preg_replace( '#<noscript>(?:.+)</noscript>#Umsi', '', $html );
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace WP_Rocket\Engine\Media;
use WP_Rocket\Engine\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service provider for Media module
*
* @since 3.6
*/
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 = [
'lazyload_assets',
'lazyload_image',
'lazyload_iframe',
'lazyload_subscriber',
'embeds_subscriber',
'emojis_subscriber',
];
/**
* Registers the services in the container
*
* @since 3.6
*
* @return void
*/
public function register() {
$options = $this->getContainer()->get( 'options' );
$this->getContainer()->add( 'lazyload_assets', 'WP_Rocket\Dependencies\RocketLazyload\Assets' );
$this->getContainer()->add( 'lazyload_image', 'WP_Rocket\Dependencies\RocketLazyload\Image' );
$this->getContainer()->add( 'lazyload_iframe', 'WP_Rocket\Dependencies\RocketLazyload\Iframe' );
$this->getContainer()->share( 'lazyload_subscriber', 'WP_Rocket\Engine\Media\LazyloadSubscriber' )
->withArgument( $options )
->withArgument( $this->getContainer()->get( 'lazyload_assets' ) )
->withArgument( $this->getContainer()->get( 'lazyload_image' ) )
->withArgument( $this->getContainer()->get( 'lazyload_iframe' ) );
$this->getContainer()->share( 'embeds_subscriber', 'WP_Rocket\Engine\Media\Embeds\EmbedsSubscriber' )
->withArgument( $options );
$this->getContainer()->share( 'emojis_subscriber', 'WP_Rocket\Engine\Media\Emojis\EmojisSubscriber' )
->withArgument( $options );
}
}