Initial commit xmarticaims
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class_alias( '\WP_Rocket\deprecated\Engine\Media\Embeds\EmbedsSubscriber', '\WP_Rocket\Engine\Media\Embeds\EmbedsSubscriber' );
|
||||
class_alias( '\WP_Rocket\Engine\Admin\Database\Optimization','\WP_Rocket\Admin\Database\Optimization' );
|
||||
class_alias( '\WP_Rocket\Engine\Admin\Database\OptimizationProcess','\WP_Rocket\Admin\Database\Optimization_Process' );
|
||||
class_alias( '\WP_Rocket\Engine\Admin\Database\ServiceProvider','\WP_Rocket\ServiceProvider\Database' );
|
||||
class_alias( '\WP_Rocket\Engine\Admin\Database\Subscriber','\WP_Rocket\Subscriber\Admin\Database\Optimization_Subscriber' );
|
||||
|
||||
/**
|
||||
* Maybe reset opcache after WP Rocket update.
|
||||
*
|
||||
* @since 3.10.8 deprecated
|
||||
* @since 3.1
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param object $wp_upgrader Plugin_Upgrader instance.
|
||||
* @param array $hook_extra {
|
||||
* Array of bulk item update data.
|
||||
*
|
||||
* @type string $action Type of action. Default 'update'.
|
||||
* @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
|
||||
* @type bool $bulk Whether the update process is a bulk update. Default true.
|
||||
* @type array $plugins Array of the basename paths of the plugins' main files.
|
||||
* }
|
||||
*/
|
||||
function rocket_maybe_reset_opcache( $wp_upgrader, $hook_extra ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.10.8' );
|
||||
static $rocket_path;
|
||||
|
||||
if ( ! isset( $hook_extra['action'], $hook_extra['type'], $hook_extra['plugins'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 'update' !== $hook_extra['action'] || 'plugin' !== $hook_extra['type'] || ! is_array( $hook_extra['plugins'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$plugins = array_flip( $hook_extra['plugins'] );
|
||||
|
||||
if ( ! isset( $rocket_path ) ) {
|
||||
$rocket_path = plugin_basename( WP_ROCKET_FILE );
|
||||
}
|
||||
|
||||
if ( ! isset( $plugins[ $rocket_path ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_reset_opcache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset PHP opcache.
|
||||
*
|
||||
* @since 3.10.8 deprecated
|
||||
* @since 3.1
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
function rocket_reset_opcache() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.10.8' );
|
||||
static $can_reset;
|
||||
|
||||
/**
|
||||
* Triggers before WP Rocket tries to reset OPCache
|
||||
*
|
||||
* @since 3.2.5
|
||||
* @author Remy Perona
|
||||
*/
|
||||
do_action( 'rocket_before_reset_opcache' );
|
||||
|
||||
if ( ! isset( $can_reset ) ) {
|
||||
if ( ! function_exists( 'opcache_reset' ) ) {
|
||||
$can_reset = false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$restrict_api = ini_get( 'opcache.restrict_api' );
|
||||
|
||||
if ( $restrict_api && strpos( __FILE__, $restrict_api ) !== 0 ) {
|
||||
$can_reset = false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$can_reset = true;
|
||||
}
|
||||
|
||||
if ( ! $can_reset ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$opcache_reset = opcache_reset();
|
||||
|
||||
/**
|
||||
* Triggers after WP Rocket tries to reset OPCache
|
||||
*
|
||||
* @since 3.2.5
|
||||
* @author Remy Perona
|
||||
*/
|
||||
do_action( 'rocket_after_reset_opcache' );
|
||||
|
||||
return $opcache_reset;
|
||||
}
|
||||
|
||||
/**
|
||||
* This notice is displayed after purging OPcache
|
||||
*
|
||||
* @since 3.10.8 deprecated
|
||||
* @since 3.4.1
|
||||
* @author Soponar Cristina
|
||||
*/
|
||||
function rocket_opcache_purge_result() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.10.8' );
|
||||
|
||||
if ( ! current_user_can( 'rocket_purge_opcache' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! is_admin() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user_id = get_current_user_id();
|
||||
$notice = get_transient( $user_id . '_opcache_purge_result' );
|
||||
if ( ! $notice ) {
|
||||
return;
|
||||
}
|
||||
|
||||
delete_transient( $user_id . '_opcache_purge_result' );
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => $notice['result'],
|
||||
'message' => $notice['message'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge OPCache content in Admin Bar
|
||||
*
|
||||
* @since 3.10.8 deprecated
|
||||
* @since 2.7
|
||||
*/
|
||||
function do_admin_post_rocket_purge_opcache() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.10.8' );
|
||||
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'rocket_purge_opcache' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'rocket_purge_opcache' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$reset_opcache = rocket_reset_opcache();
|
||||
|
||||
if ( ! $reset_opcache ) {
|
||||
$op_purge_result = [
|
||||
'result' => 'error',
|
||||
'message' => __( 'OPcache purge failed.', 'rocket' ),
|
||||
];
|
||||
} else {
|
||||
$op_purge_result = [
|
||||
'result' => 'success',
|
||||
'message' => __( 'OPcache successfully purged', 'rocket' ),
|
||||
];
|
||||
}
|
||||
|
||||
set_transient( get_current_user_id() . '_opcache_purge_result', $op_purge_result );
|
||||
|
||||
wp_safe_redirect( esc_url_raw( wp_get_referer() ) );
|
||||
die();
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Add Yoast SEO sitemap option to WP Rocket default options
|
||||
*
|
||||
* @since 2.8
|
||||
* @since 3.11.1 deprecated
|
||||
*
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
* @return array Updated WP Rocket options array
|
||||
*/
|
||||
function rocket_add_yoast_seo_sitemap_option( $options ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.1' );
|
||||
|
||||
$options['yoast_xml_sitemap'] = 0;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize Yoast SEO sitemap option value
|
||||
*
|
||||
* @since 2.8
|
||||
* @since 3.11.1 deprecated
|
||||
*
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $inputs WP Rocket inputs array.
|
||||
* @return array Sanitized WP Rocket inputs array
|
||||
*/
|
||||
function rocket_yoast_seo_sitemap_option_sanitize( $inputs ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.1' );
|
||||
|
||||
$inputs['yoast_xml_sitemap'] = ! empty( $inputs['yoast_xml_sitemap'] ) ? 1 : 0;
|
||||
|
||||
return $inputs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Yoast SEO sitemap URL to the sitemaps to preload
|
||||
*
|
||||
* @since 2.8
|
||||
* @since 3.11.1 deprecated
|
||||
*
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $sitemaps Sitemaps to preload.
|
||||
* @return array Updated Sitemaps to preload
|
||||
*/
|
||||
function rocket_add_yoast_seo_sitemap( $sitemaps ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.1' );
|
||||
|
||||
if ( get_rocket_option( 'yoast_xml_sitemap', false ) ) {
|
||||
$sitemaps[] = WPSEO_Sitemaps_Router::get_base_url( 'sitemap_index.xml' );
|
||||
}
|
||||
|
||||
return $sitemaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Yoast SEO option to WP Rocket settings
|
||||
*
|
||||
* @since 2.8
|
||||
* @since 3.11.1 deprecated
|
||||
*
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $options WP Rocket settings array.
|
||||
* @return array Updated WP Rocket settings array
|
||||
*/
|
||||
function rocket_sitemap_preload_yoast_seo_option( $options ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.1' );
|
||||
|
||||
$options['yoast_xml_sitemap'] = [
|
||||
'type' => 'checkbox',
|
||||
'container_class' => [
|
||||
'wpr-field--children',
|
||||
],
|
||||
'label' => __( 'Yoast SEO XML sitemap', 'rocket' ),
|
||||
// translators: %s = Name of the plugin.
|
||||
'description' => sprintf( __( 'We automatically detected the sitemap generated by the %s plugin. You can check the option to preload it.', 'rocket' ), 'Yoast SEO' ),
|
||||
'parent' => 'sitemap_preload',
|
||||
'section' => 'preload_section',
|
||||
'page' => 'preload',
|
||||
'default' => 0,
|
||||
'sanitize_callback' => 'sanitize_checkbox',
|
||||
];
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear Kinsta cache when clearing WP Rocket cache
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function rocket_clean_kinsta_cache() {
|
||||
global $kinsta_cache;
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.1' );
|
||||
|
||||
if ( ! empty( $kinsta_cache->kinsta_cache_purge ) ) {
|
||||
$kinsta_cache->kinsta_cache_purge->purge_complete_caches();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Partially clear Kinsta cache when partially clearing WP Rocket cache
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param object $post Post object.
|
||||
* @return void
|
||||
*/
|
||||
function rocket_clean_kinsta_post_cache( $post ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.1' );
|
||||
global $kinsta_cache;
|
||||
$kinsta_cache->kinsta_cache_purge->initiate_purge( $post->ID, 'post' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears Kinsta cache for the homepage URL when using "Purge this URL" from the admin bar on the front end
|
||||
*
|
||||
* @since 3.0.4
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $root WP Rocket root cache path.
|
||||
* @param string $lang Current language.
|
||||
* @return void
|
||||
*/
|
||||
function rocket_clean_kinsta_cache_home( $root = '', $lang = '' ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.1' );
|
||||
$url = get_rocket_i18n_home_url( $lang );
|
||||
$url = trailingslashit( $url ) . 'kinsta-clear-cache/';
|
||||
|
||||
wp_remote_get(
|
||||
$url,
|
||||
[
|
||||
'blocking' => false,
|
||||
'timeout' => 0.01,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears Kinsta cache for a specific URL when using "Purge this URL" from the admin bar on the front end
|
||||
*
|
||||
* @since 3.0.4
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $url URL to purge.
|
||||
* @return void
|
||||
*/
|
||||
function rocket_clean_kinsta_cache_url( $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.1' );
|
||||
$url = trailingslashit( $url ) . 'kinsta-clear-cache/';
|
||||
|
||||
wp_remote_get(
|
||||
$url,
|
||||
[
|
||||
'blocking' => false,
|
||||
'timeout' => 0.01,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove WP Rocket functions on WP core action hooks to prevent triggering a double cache clear.
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function rocket_remove_partial_purge_hooks() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.1' );
|
||||
// WP core action hooks rocket_clean_post() gets hooked into.
|
||||
$clean_post_hooks = [
|
||||
// Disables the refreshing of partial cache when content is edited.
|
||||
'wp_trash_post',
|
||||
'delete_post',
|
||||
'clean_post_cache',
|
||||
'wp_update_comment_count',
|
||||
];
|
||||
|
||||
// Remove rocket_clean_post() from core action hooks.
|
||||
array_map(
|
||||
function( $hook ) {
|
||||
remove_action( $hook, 'rocket_clean_post' );
|
||||
},
|
||||
$clean_post_hooks
|
||||
);
|
||||
|
||||
remove_filter( 'rocket_clean_files', 'rocket_clean_files_users' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the rollback
|
||||
*
|
||||
* @since 3.11.5 deprecated
|
||||
* @since 2.4
|
||||
*/
|
||||
function rocket_rollback() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.5' );
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'rocket_rollback' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires before doing the rollback
|
||||
*/
|
||||
do_action( 'rocket_before_rollback' );
|
||||
|
||||
$plugin_transient = get_site_transient( 'update_plugins' );
|
||||
$plugin_folder = plugin_basename( dirname( WP_ROCKET_FILE ) );
|
||||
$plugin = $plugin_folder . '/' . basename( WP_ROCKET_FILE );
|
||||
|
||||
$plugin_transient->response[ $plugin ] = (object) [
|
||||
'slug' => $plugin_folder,
|
||||
'new_version' => WP_ROCKET_LASTVERSION,
|
||||
'url' => 'https://wp-rocket.me',
|
||||
'package' => sprintf( 'https://wp-rocket.me/%s/wp-rocket_%s.zip', get_rocket_option( 'consumer_key' ), WP_ROCKET_LASTVERSION ),
|
||||
];
|
||||
|
||||
set_site_transient( 'update_plugins', $plugin_transient );
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
|
||||
|
||||
// translators: %s is the plugin name.
|
||||
$title = sprintf( __( '%s Update Rollback', 'rocket' ), WP_ROCKET_PLUGIN_NAME );
|
||||
$nonce = 'upgrade-plugin_' . $plugin;
|
||||
$url = 'update.php?action=upgrade-plugin&plugin=' . rawurlencode( $plugin );
|
||||
$upgrader_skin = new Plugin_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'plugin' ) );
|
||||
$upgrader = new Plugin_Upgrader( $upgrader_skin );
|
||||
|
||||
remove_filter( 'site_transient_update_plugins', 'rocket_check_update', 1 );
|
||||
add_filter( 'update_plugin_complete_actions', 'rocket_rollback_add_return_link' );
|
||||
rocket_put_content( WP_CONTENT_DIR . '/advanced-cache.php', '' );
|
||||
|
||||
$upgrader->upgrade( $plugin );
|
||||
|
||||
wp_die(
|
||||
'',
|
||||
// translators: %s is the plugin name.
|
||||
esc_html( sprintf( __( '%s Update Rollback', 'rocket' ), WP_ROCKET_PLUGIN_NAME ) ),
|
||||
[
|
||||
'response' => 200,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* After a rollback has been done, replace the "return to" link by a link pointing to WP Rocket's tools page.
|
||||
* A link to the plugins page is kept in case the plugin is not reactivated correctly.
|
||||
*
|
||||
* @since 3.11.5 deprecated
|
||||
* @since 3.2.4
|
||||
* @author Grégory Viguier
|
||||
* @author Arun Basil Lal
|
||||
*
|
||||
* @param array $update_actions Array of plugin action links.
|
||||
* @return array The array of links where the "return to" link has been replaced.
|
||||
*/
|
||||
function rocket_rollback_add_return_link( $update_actions ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.11.5' );
|
||||
|
||||
if ( ! isset( $update_actions['plugins_page'] ) ) {
|
||||
return $update_actions;
|
||||
}
|
||||
|
||||
$update_actions['plugins_page'] = sprintf(
|
||||
/* translators: 1 and 3 are link openings, 2 is a link closing. */
|
||||
__( '%1$sReturn to WP Rocket%2$s or %3$sgo to Plugins page%2$s', 'rocket' ),
|
||||
'<a href="' . esc_url( admin_url( 'options-general.php?page=' . WP_ROCKET_PLUGIN_SLUG ) . '#tools' ) . '" target="_parent">',
|
||||
'</a>',
|
||||
'<a href="' . esc_url( admin_url( 'plugins.php' ) ) . '" target="_parent">'
|
||||
);
|
||||
|
||||
return $update_actions;
|
||||
}
|
||||
@@ -0,0 +1,727 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Add All in One SEO Sitemap option to WP Rocket options
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param Array $options Array of WP Rocket options.
|
||||
* @return Array Updated array of WP Rocket options
|
||||
*/
|
||||
function rocket_add_all_in_one_seo_sitemap_option( $options ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$options['all_in_one_seo_xml_sitemap'] = 0;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the AIO SEO option value
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param Array $inputs Array of inputs values.
|
||||
* @return Array Updated array of inputs $values
|
||||
*/
|
||||
function rocket_all_in_one_seo_sitemap_option_sanitize( $inputs ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$inputs['all_in_one_seo_xml_sitemap'] = ! empty( $inputs['all_in_one_seo_xml_sitemap'] ) ? 1 : 0;
|
||||
|
||||
return $inputs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add All in One SEO Sitemap sub-option on WP Rocket settings page
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param Array $options Array of WP Rocket options.
|
||||
* @return Array Updated array of WP Rocket options
|
||||
*/
|
||||
function rocket_sitemap_preload_all_in_one_seo_option( $options ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$options['all_in_one_seo_xml_sitemap'] = [
|
||||
'type' => 'checkbox',
|
||||
'container_class' => [
|
||||
'wpr-field--children',
|
||||
],
|
||||
'label' => __( 'All in One SEO XML sitemap', 'rocket' ),
|
||||
// translators: %s = Name of the plugin.
|
||||
'description' => sprintf( __( 'We automatically detected the sitemap generated by the %s plugin. You can check the option to preload it.', 'rocket' ), 'All in One SEO' ),
|
||||
'parent' => 'sitemap_preload',
|
||||
'section' => 'preload_section',
|
||||
'page' => 'preload',
|
||||
'default' => 0,
|
||||
'sanitize_callback' => 'sanitize_checkbox',
|
||||
];
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add sitemap option to WP Rocket settings
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 3.2.3
|
||||
*
|
||||
* @param array $options WP Rocket settings array.
|
||||
* @return array Updated WP Rocket settings array
|
||||
*/
|
||||
function rank_math_rocket_sitemap_preload_option( $options ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$options['rank_math_xml_sitemap'] = [
|
||||
'type' => 'checkbox',
|
||||
'container_class' => [
|
||||
'wpr-field--children',
|
||||
],
|
||||
'label' => __( 'Rank Math XML sitemap', 'rocket' ),
|
||||
// translators: %s = Name of the plugin.
|
||||
'description' => sprintf( __( 'We automatically detected the sitemap generated by the %s plugin. You can check the option to preload it.', 'rocket' ), 'Rank Math SEO' ),
|
||||
'parent' => 'sitemap_preload',
|
||||
'section' => 'preload_section',
|
||||
'page' => 'preload',
|
||||
'default' => 0,
|
||||
'sanitize_callback' => 'sanitize_checkbox',
|
||||
];
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add sitemap option to WP Rocket default options
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 3.2.3
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
* @return array Updated WP Rocket options array
|
||||
*/
|
||||
function rank_math_rocket_add_sitemap_option( $options ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$options['rank_math_xml_sitemap'] = 0;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize SEO sitemap option value
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 3.2.3
|
||||
*
|
||||
* @param array $inputs WP Rocket inputs array.
|
||||
* @return array Sanitized WP Rocket inputs array
|
||||
*/
|
||||
function rank_math_rocket_sitemap_option_sanitize( $inputs ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$inputs['rank_math_xml_sitemap'] = ! empty( $inputs['rank_math_xml_sitemap'] ) ? 1 : 0;
|
||||
|
||||
return $inputs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add SEOPress sitemap option to WP Rocket default options
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 3.3.6
|
||||
* @author Benjamin Denis
|
||||
* @source ./yoast-seo.php (Remy Perona)
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
* @return array Updated WP Rocket options array
|
||||
*/
|
||||
function rocket_add_seopress_sitemap_option( $options ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$options['seopress_xml_sitemap'] = 0;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize SEOPress sitemap option value
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 3.3.6
|
||||
* @author Benjamin Denis
|
||||
* @source ./yoast-seo.php (Remy Perona)
|
||||
*
|
||||
* @param array $inputs WP Rocket inputs array.
|
||||
* @return array Sanitized WP Rocket inputs array
|
||||
*/
|
||||
function rocket_seopress_sitemap_option_sanitize( $inputs ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$inputs['seopress_xml_sitemap'] = ! empty( $inputs['seopress_xml_sitemap'] ) ? 1 : 0;
|
||||
|
||||
return $inputs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add SEOPress option to WP Rocket settings
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 3.3.6
|
||||
* @author Benjamin Denis
|
||||
* @source ./yoast-seo.php (Remy Perona)
|
||||
*
|
||||
* @param array $options WP Rocket settings array.
|
||||
* @return array Updated WP Rocket settings array
|
||||
*/
|
||||
function rocket_sitemap_preload_seopress_option( $options ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$options['seopress_xml_sitemap'] = [
|
||||
'type' => 'checkbox',
|
||||
'container_class' => [
|
||||
'wpr-field--children',
|
||||
],
|
||||
'label' => __( 'SEOPress XML sitemap', 'rocket' ),
|
||||
// translators: %s = Name of the plugin.
|
||||
'description' => sprintf( __( 'We automatically detected the sitemap generated by the %s plugin. You can check the option to preload it.', 'rocket' ), 'SEOPress' ),
|
||||
'parent' => 'sitemap_preload',
|
||||
'section' => 'preload_section',
|
||||
'page' => 'preload',
|
||||
'default' => 0,
|
||||
'sanitize_callback' => 'sanitize_checkbox',
|
||||
];
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a sitemap option in WP Rocket for The SEO Framework.
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 3.2.1
|
||||
* @author Sybre Waaijer
|
||||
* @source ./yoast-seo.php (Remy Perona)
|
||||
*
|
||||
* @param array $options WP Rocket options array.
|
||||
* @return array Updated WP Rocket options array
|
||||
*/
|
||||
function rocket_add_tsf_seo_sitemap_option( $options ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$options['tsf_xml_sitemap'] = 0;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes the added sitemap option for The SEO Framework.
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 3.2.1
|
||||
* @author Sybre Waaijer
|
||||
* @source ./yoast-seo.php (Remy Perona)
|
||||
*
|
||||
* @param array $inputs WP Rocket inputs array.
|
||||
* @return array Sanitized WP Rocket inputs array
|
||||
*/
|
||||
function rocket_tsf_seo_sitemap_option_sanitize( $inputs ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$inputs['tsf_xml_sitemap'] = ! empty( $inputs['tsf_xml_sitemap'] ) ? 1 : 0;
|
||||
|
||||
return $inputs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add The SEO Framework SEO option to WP Rocket settings
|
||||
*
|
||||
* @since 3.12 deprecated
|
||||
* @since 3.2.1
|
||||
* @author Sybre Waaijer
|
||||
* @source ./yoast-seo.php (Remy Perona)
|
||||
*
|
||||
* @param array $options WP Rocket settings array.
|
||||
* @return array Updated WP Rocket settings array
|
||||
*/
|
||||
function rocket_sitemap_add_tsf_sitemap_to_preload_option( $options ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$options['tsf_xml_sitemap'] = [
|
||||
'type' => 'checkbox',
|
||||
'container_class' => [
|
||||
'wpr-field--children',
|
||||
],
|
||||
'label' => __( 'The SEO Framework XML sitemap', 'rocket' ),
|
||||
// translators: %s = Name of the plugin.
|
||||
'description' => sprintf( __( 'We automatically detected the sitemap generated by the %s plugin. You can check the option to preload it.', 'rocket' ), 'The SEO Framework' ),
|
||||
'parent' => 'sitemap_preload',
|
||||
'section' => 'preload_section',
|
||||
'page' => 'preload',
|
||||
'default' => 0,
|
||||
'sanitize_callback' => 'sanitize_checkbox',
|
||||
];
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Jetpack option to WP Rocket options
|
||||
*
|
||||
* @param Array $options WP Rocket options array.
|
||||
* @return Array Updated WP Rocket options array
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
*/
|
||||
function rocket_add_jetpack_sitemap_option($options)
|
||||
{
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$options['jetpack_xml_sitemap'] = 0;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize jetpack option value
|
||||
*
|
||||
* @param Array $inputs Array of inputs values.
|
||||
* @return Array Array of inputs values
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
*/
|
||||
function rocket_jetpack_sitemap_option_sanitize($inputs)
|
||||
{
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$inputs['jetpack_xml_sitemap'] = !empty($inputs['jetpack_xml_sitemap']) ? 1 : 0;
|
||||
|
||||
return $inputs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Jetpack sitemap to preload list
|
||||
*
|
||||
* @param Array $sitemaps Array of sitemaps to preload.
|
||||
* @return Array Updated Array of sitemaps to preload
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
*/
|
||||
function rocket_add_jetpack_sitemap($sitemaps)
|
||||
{
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
if (get_rocket_option('jetpack_xml_sitemap', false)) {
|
||||
$sitemaps['jetpack'] = jetpack_sitemap_uri();
|
||||
}
|
||||
|
||||
return $sitemaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Jetpack sub-option to WP Rocket settings page
|
||||
*
|
||||
* @param Array $options WP Rocket options array.
|
||||
* @return Array Updated WP Rocket options array
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
*/
|
||||
function rocket_sitemap_preload_jetpack_option($options)
|
||||
{
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$options[] = [
|
||||
'parent' => 'sitemap_preload',
|
||||
'type' => 'checkbox',
|
||||
'label' => __('Jetpack XML Sitemaps', 'rocket'),
|
||||
'label_for' => 'jetpack_xml_sitemap',
|
||||
'label_screen' => sprintf(__('Preload the sitemap from the Jetpack plugin', 'rocket'), 'Jetpack'),
|
||||
'default' => 0,
|
||||
];
|
||||
$options[] = [
|
||||
'parent' => 'sitemap_preload',
|
||||
'type' => 'helper_description',
|
||||
'name' => 'jetpack_xml_sitemap_desc',
|
||||
// translators: %s = plugin name, e.g. Yoast SEO.
|
||||
'description' => sprintf(__('We automatically detected the sitemap generated by the %s plugin. You can check the option to preload it.', 'rocket'), 'Jetpack'),
|
||||
];
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add the EU Cookie Law to the list of mandatory cookies before generating caching files.
|
||||
*
|
||||
* @param array $cookies List of mandatory cookies.
|
||||
* @author Jeremy Herve
|
||||
*
|
||||
* @since 2.10.1
|
||||
*/
|
||||
function rocket_add_jetpack_cookie_law_mandatory_cookie($cookies)
|
||||
{
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$cookies['jetpack-eu-cookie-law'] = 'eucookielaw';
|
||||
|
||||
return $cookies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Jetpack cookie when:
|
||||
* - Jetpack is active.
|
||||
* - Jetpack's Extra Sidebar Widgets module is active.
|
||||
* - The widget is active.
|
||||
* - the rocket_jetpack_eu_cookie_widget option is empty or not set.
|
||||
*
|
||||
* @since 2.10.1
|
||||
* @author Jeremy Herve
|
||||
*/
|
||||
function rocket_activate_jetpack_cookie_law()
|
||||
{
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
$rocket_jp_eu_cookie_widget = get_option('rocket_jetpack_eu_cookie_widget');
|
||||
|
||||
if (
|
||||
is_active_widget(false, false, 'eu_cookie_law_widget')
|
||||
&& empty($rocket_jp_eu_cookie_widget)
|
||||
) {
|
||||
add_filter('rocket_htaccess_mod_rewrite', '__return_false', 76);
|
||||
add_filter('rocket_cache_mandatory_cookies', 'rocket_add_jetpack_cookie_law_mandatory_cookie');
|
||||
|
||||
// Update the WP Rocket rules on the .htaccess file.
|
||||
flush_rocket_htaccess();
|
||||
|
||||
// Regenerate the config file.
|
||||
rocket_generate_config_file();
|
||||
|
||||
// Set the option, so this is not triggered again.
|
||||
update_option('rocket_jetpack_eu_cookie_widget', 1, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove cookies if Jetpack gets deactivated.
|
||||
*
|
||||
* @since 2.10.1
|
||||
* @author Jeremy Herve
|
||||
*/
|
||||
function rocket_remove_jetpack_cookie_law_mandatory_cookie()
|
||||
{
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
remove_filter('rocket_htaccess_mod_rewrite', '__return_false', 76);
|
||||
remove_filter('rocket_cache_mandatory_cookies', '_rocket_add_eu_cookie_law_mandatory_cookie');
|
||||
|
||||
// Update the WP Rocket rules on the .htaccess file.
|
||||
flush_rocket_htaccess();
|
||||
|
||||
// Regenerate the config file.
|
||||
rocket_generate_config_file();
|
||||
|
||||
// Delete our option.
|
||||
delete_option('rocket_jetpack_eu_cookie_widget');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add SEO sitemap URL to the sitemaps to preload
|
||||
*
|
||||
* @since 3.2.3
|
||||
*
|
||||
* @param array $sitemaps Sitemaps to preload.
|
||||
* @return array Updated Sitemaps to preload
|
||||
*/
|
||||
function rank_math_rocket_sitemap( $sitemaps ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
if ( get_rocket_option( 'rank_math_xml_sitemap', false ) ) {
|
||||
$sitemaps[] = \RankMath\Sitemap\Router::get_base_url( 'sitemap_index.xml' );
|
||||
}
|
||||
|
||||
return $sitemaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add All in One SEO Sitemap to the preload list
|
||||
*
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param Array $sitemaps Array of sitemaps to preload.
|
||||
* @return Array Updated array of sitemaps to preload
|
||||
*/
|
||||
function rocket_add_all_in_one_seo_sitemap( $sitemaps ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
if ( ! get_rocket_option( 'all_in_one_seo_xml_sitemap', false ) ) {
|
||||
return $sitemaps;
|
||||
}
|
||||
|
||||
$aioseo_v3 = defined( 'AIOSEOP_VERSION' );
|
||||
$aioseo_v4 = defined( 'AIOSEO_VERSION' ) && function_exists( 'aioseo' );
|
||||
|
||||
if ( ! $aioseo_v3 && ! $aioseo_v4 ) {
|
||||
return $sitemaps;
|
||||
}
|
||||
|
||||
$sitemap_enabled = false;
|
||||
if ( $aioseo_v3 ) {
|
||||
$aioseop_options = get_option( 'aioseop_options' );
|
||||
$sitemap_enabled = ( isset( $aioseop_options['modules']['aiosp_feature_manager_options']['aiosp_feature_manager_enable_sitemap'] ) && 'on' === $aioseop_options['modules']['aiosp_feature_manager_options']['aiosp_feature_manager_enable_sitemap'] ) || ( ! isset( $aioseop_options['modules']['aiosp_feature_manager_options'] ) && isset( $aioseop_options['modules']['aiosp_sitemap_options'] ) );
|
||||
}
|
||||
|
||||
if (
|
||||
( $aioseo_v3 && ! $sitemap_enabled ) ||
|
||||
( $aioseo_v4 && ! aioseo()->options->sitemap->general->enable )
|
||||
) {
|
||||
return $sitemaps;
|
||||
}
|
||||
|
||||
if ( $aioseo_v3 ) {
|
||||
$sitemaps[] = trailingslashit( home_url() ) . apply_filters( 'aiosp_sitemap_filename', 'sitemap' ) . '.xml'; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
} elseif ( $aioseo_v4 ) {
|
||||
$sitemaps[] = trailingslashit( home_url() ) . apply_filters( 'aioseo_sitemap_filename', 'sitemap' ) . '.xml'; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
}
|
||||
|
||||
return $sitemaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add SEOPress sitemap URL to the sitemaps to preload
|
||||
*
|
||||
* @since 3.3.6
|
||||
* @author Benjamin Denis
|
||||
* @source ./yoast-seo.php (Remy Perona)
|
||||
*
|
||||
* @param array $sitemaps Sitemaps to preload.
|
||||
* @return array Updated Sitemaps to preload
|
||||
*/
|
||||
function rocket_add_seopress_sitemap( $sitemaps ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
if ( get_rocket_option( 'seopress_xml_sitemap', false ) ) {
|
||||
$sitemaps[] = get_home_url() . '/sitemaps.xml';
|
||||
}
|
||||
|
||||
return $sitemaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs detection and adds extra compatibility for The SEO Framework plugin.
|
||||
*
|
||||
* @since 3.2.1
|
||||
* @since TODO Removed "conflicting sitemap detection" (detect_sitemap_plugin) call.
|
||||
* TSF always tries to output it now while trying to give WP Rewrite priority for display.
|
||||
* @author Sybre Waaijer
|
||||
*/
|
||||
function rocket_add_tsf_compat() {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
|
||||
$tsf = the_seo_framework();
|
||||
|
||||
// Either TSF < 3.1, or the plugin's silenced (soft-disabled) via a drop-in.
|
||||
if ( empty( $tsf->loaded ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Performs option & other checks.
|
||||
* 2. Checks for conflicting sitemap plugins that might prevent loading.
|
||||
*
|
||||
* These methods cache their output at runtime.
|
||||
*
|
||||
* @link https://github.com/wp-media/wp-rocket/issues/899
|
||||
*/
|
||||
if ( $tsf->can_run_sitemap() ) {
|
||||
rocket_add_tsf_sitemap_compat();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds compatibility for the sitemap functionality in The SEO Framework plugin.
|
||||
*
|
||||
* @since 3.2.1
|
||||
* @author Sybre Waaijer
|
||||
*/
|
||||
function rocket_add_tsf_sitemap_compat() {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
|
||||
add_filter( 'rocket_sitemap_preload_list', 'rocket_add_tsf_sitemap_to_preload' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds TSF sitemap URLs to preload.
|
||||
*
|
||||
* @since 3.2.1
|
||||
* @since TODO Added compatibility support for The SEO Framework v4.0+
|
||||
* @author Sybre Waaijer
|
||||
* @source ./yoast-seo.php (Remy Perona)
|
||||
*
|
||||
* @param array $sitemaps Sitemaps to preload.
|
||||
* @return array Updated Sitemaps to preload
|
||||
*/
|
||||
function rocket_add_tsf_sitemap_to_preload( $sitemaps ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
|
||||
if ( get_rocket_option( 'tsf_xml_sitemap', false ) ) {
|
||||
// The autoloader in TSF doesn't check for file_exists(). So, use version compare instead to prevent fatal errors.
|
||||
if ( version_compare( THE_SEO_FRAMEWORK_VERSION, '4.0', '>=' ) ) {
|
||||
// TSF 4.0+. Expect the class to exist indefinitely.
|
||||
|
||||
$sitemap_bridge = The_SEO_Framework\Bridges\Sitemap::get_instance();
|
||||
|
||||
foreach ( $sitemap_bridge->get_sitemap_endpoint_list() as $id => $data ) {
|
||||
// When the sitemap is good enough for a robots display, we determine it as valid for precaching.
|
||||
// Non-robots display types are among the stylesheet endpoint, or the Yoast SEO-compatible endpoint.
|
||||
// In other words, this enables support for ALL current and future public sitemap endpoints.
|
||||
if ( ! empty( $data['robots'] ) ) {
|
||||
$sitemaps[] = $sitemap_bridge->get_expected_sitemap_endpoint_url( $id );
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Deprecated. TSF <4.0.
|
||||
$sitemaps[] = the_seo_framework()->get_sitemap_xml_url();
|
||||
}
|
||||
}
|
||||
|
||||
return $sitemaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Launches the Homepage preload (helper function for backward compatibility)
|
||||
*
|
||||
* @since 2.6.4 Don't preload localhost & .dev domains
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $spider (default: 'cache-preload') The spider name: cache-preload or cache-json.
|
||||
* @param string $lang (default: '') The language code to preload.
|
||||
*
|
||||
* @return bool Status of preload.
|
||||
*/
|
||||
function run_rocket_bot( $spider = 'cache-preload', $lang = '' ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
if ( ! get_rocket_option( 'manual_preload' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$urls = [];
|
||||
|
||||
if ( ! $lang ) {
|
||||
$urls = get_rocket_i18n_uri();
|
||||
} else {
|
||||
$urls[] = get_rocket_i18n_home_url( $lang );
|
||||
}
|
||||
|
||||
$container = apply_filters( 'rocket_container', null );
|
||||
|
||||
if ( ! $container ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$controller = $container->get( 'preload_clean_controller' );
|
||||
|
||||
$controller->partial_clean( $urls );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Launches the sitemap preload (helper function for backward compatibility)
|
||||
*
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function run_rocket_sitemap_preload() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
_deprecated_function( __FUNCTION__, '3.12' );
|
||||
if ( ! get_rocket_option( 'manual_preload' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$container = apply_filters( 'rocket_container', null );
|
||||
|
||||
if ( ! $container ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$controller = $container->get( 'load_initial_sitemap_controller' );
|
||||
|
||||
$controller->load_initial_sitemap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Excludes Uncode init and ai-uncode JS files from minification/combine
|
||||
*
|
||||
* @since 3.12.3 deprecated
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $excluded_js Array of JS filepaths to be excluded.
|
||||
* @return array
|
||||
*/
|
||||
function rocket_exclude_js_uncode( $excluded_js ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12.3' );
|
||||
$excluded_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/init.js' );
|
||||
$excluded_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/min/init.min.js' );
|
||||
$excluded_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/init.min.js' );
|
||||
$excluded_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/ai-uncode.js' );
|
||||
$excluded_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/min/ai-uncode.min.js' );
|
||||
$excluded_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/ai-uncode.min.js' );
|
||||
|
||||
return $excluded_js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Excludes some Uncode inline scripts from combine JS
|
||||
*
|
||||
* @since 3.12.3 deprecated
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $inline_js Array of patterns to match for exclusion.
|
||||
* @return array
|
||||
*/
|
||||
function rocket_exclude_inline_js_uncode( $inline_js ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12.3' );
|
||||
$inline_js[] = 'SiteParameters';
|
||||
$inline_js[] = 'script-';
|
||||
$inline_js[] = 'initBox';
|
||||
$inline_js[] = 'initHeader';
|
||||
$inline_js[] = 'fixMenuHeight';
|
||||
|
||||
return $inline_js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Excludes Uncode JS files from defer JS
|
||||
*
|
||||
* @since 3.12.3 deprecated
|
||||
* @since 3.2.5
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $exclude_defer_js Array of JS filepaths to be excluded.
|
||||
* @return array
|
||||
*/
|
||||
function rocket_exclude_defer_js_uncode( $exclude_defer_js ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12.3' );
|
||||
$exclude_defer_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/init.js' );
|
||||
$exclude_defer_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/min/init.min.js' );
|
||||
$exclude_defer_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/init.min.js' );
|
||||
$exclude_defer_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/min/ai-uncode.min.js' );
|
||||
$exclude_defer_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/ai-uncode.min.js' );
|
||||
$exclude_defer_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/ai-uncode.js' );
|
||||
return $exclude_defer_js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Excludes Uncode JS files from delay JS
|
||||
*
|
||||
* @since 3.12.3 deprecated
|
||||
* @since 3.10.5
|
||||
*
|
||||
* @param array $exclude_delay_js Array of JS to be excluded.
|
||||
* @return array
|
||||
*/
|
||||
function rocket_exclude_delay_js_uncode( $exclude_delay_js ) {
|
||||
_deprecated_function( __FUNCTION__, '3.12.3' );
|
||||
$exclude_delay_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/init.js' );
|
||||
$exclude_delay_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/min/init.min.js' );
|
||||
$exclude_delay_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/init.min.js' );
|
||||
$exclude_delay_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/min/ai-uncode.min.js' );
|
||||
$exclude_delay_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/ai-uncode.min.js' );
|
||||
$exclude_delay_js[] = rocket_clean_exclude_file( get_template_directory_uri() . '/library/js/ai-uncode.js' );
|
||||
$exclude_delay_js[] = 'UNCODE\.';
|
||||
return $exclude_delay_js;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Add cookies when we activate any goetargetingWP plugin.
|
||||
*
|
||||
* @since 2.10.3
|
||||
* @author Damian Logghe
|
||||
*/
|
||||
function rocket_activate_geotargetingwp() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.13.3' );
|
||||
add_filter( 'rocket_htaccess_mod_rewrite', '__return_false', 72 );
|
||||
add_filter( 'rocket_cache_dynamic_cookies', 'rocket_add_geotargetingwp_dynamic_cookies' );
|
||||
add_filter( 'rocket_cache_mandatory_cookies', 'rocket_add_geotargetingwp_mandatory_cookie' );
|
||||
|
||||
// Update the WP Rocket rules on the .htaccess file.
|
||||
flush_rocket_htaccess();
|
||||
|
||||
// Regenerate the config file.
|
||||
rocket_generate_config_file();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove cookies when we deactivate the plugin.
|
||||
*
|
||||
* @since 2.10.3
|
||||
* @author Damian Logghe
|
||||
*/
|
||||
function rocket_deactivate_geotargetingwp() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.13.3' );
|
||||
// add into db a record saying we deactivated one of the family plugins.
|
||||
update_option( 'geotWP-deactivated', true );
|
||||
remove_filter( 'rocket_htaccess_mod_rewrite', '__return_false', 72 );
|
||||
remove_filter( 'rocket_cache_dynamic_cookies', 'rocket_add_geotargetingwp_dynamic_cookies' );
|
||||
remove_filter( 'rocket_cache_mandatory_cookies', 'rocket_add_geotargetingwp_mandatory_cookie' );
|
||||
|
||||
// Update the WP Rocket rules on the .htaccess file.
|
||||
flush_rocket_htaccess();
|
||||
|
||||
// Regenerate the config file.
|
||||
rocket_generate_config_file();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the GeotargetingWP cookies to generate caching files depending on their values.
|
||||
*
|
||||
* @since 2.10.3
|
||||
* @author Damian Logghe
|
||||
*
|
||||
* @param Array $cookies An array of cookies.
|
||||
* @return Array Updated array of cookies
|
||||
*/
|
||||
function rocket_add_geotargetingwp_dynamic_cookies( $cookies ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.13.3' );
|
||||
return rocket_add_geot_cookies( $cookies );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the GeotargetingWP cookies to the list of mandatory cookies before to generate caching files.
|
||||
*
|
||||
* @since 2.10.3
|
||||
* @author Damian Logghe
|
||||
*
|
||||
* @param Array $cookies An array of cookies.
|
||||
* @return Array Updated array of cookies
|
||||
*/
|
||||
function rocket_add_geotargetingwp_mandatory_cookie( $cookies ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.13.3' );
|
||||
return rocket_add_geot_cookies( $cookies );
|
||||
}
|
||||
|
||||
/**
|
||||
* Let users modify cache level by default set to country.
|
||||
*
|
||||
* @since 2.10.3
|
||||
* @author Damian Logghe
|
||||
*
|
||||
* @param Array $cookies An array of cookies.
|
||||
* @return Array Updated array of cookies
|
||||
*/
|
||||
function rocket_add_geot_cookies( $cookies ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.13.3' );
|
||||
// valid options are country, state, city.
|
||||
$enabled_cookies = apply_filters( 'rocket_geotargetingwp_enabled_cookies', [ 'country' ] );
|
||||
foreach ( $enabled_cookies as $enabled_cookie ) {
|
||||
if ( ! in_array( 'geot_rocket_' . $enabled_cookie, $cookies, true ) ) {
|
||||
$cookies[] = 'geot_rocket_' . $enabled_cookie;
|
||||
}
|
||||
}
|
||||
return $cookies;
|
||||
}
|
||||
@@ -0,0 +1,440 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Remove HTTP protocol on script, link, img and form tags.
|
||||
*
|
||||
* @since 2.7
|
||||
* @deprecated 3.14
|
||||
*
|
||||
* @param string $buffer HTML content.
|
||||
* @return string Updated HTML content
|
||||
*/
|
||||
function rocket_protocol_rewrite( $buffer ) {
|
||||
_deprecated_function( __FUNCTION__, '3.14' );
|
||||
|
||||
$re = "/(<(script|link|img|form)([^>]*)(href|src|action)=[\"'])https?:\\/\\//i";
|
||||
$subst = '$1//';
|
||||
$return = preg_replace( $re, $subst, $buffer );
|
||||
|
||||
if ( $return ) {
|
||||
$buffer = $return;
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove HTTP protocol on srcset attribute generated by WordPress
|
||||
*
|
||||
* @since 2.7
|
||||
* @deprecated 3.14
|
||||
*
|
||||
* @param array $sources an Array of images sources for srcset.
|
||||
* @return array Updated array of images sources
|
||||
*/
|
||||
function rocket_protocol_rewrite_srcset( $sources ) {
|
||||
_deprecated_function( __FUNCTION__, '3.14' );
|
||||
|
||||
if ( (bool) $sources ) {
|
||||
foreach ( $sources as $i => $source ) {
|
||||
$sources[ $i ]['url'] = str_replace( [ 'http:', 'https:' ], '', $source['url'] );
|
||||
}
|
||||
}
|
||||
|
||||
return $sources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if request is from Cloudflare
|
||||
*
|
||||
* @since 3.4.1
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function rocket_is_cloudflare() {
|
||||
_deprecated_function( __FUNCTION__, '3.14' );
|
||||
|
||||
if ( ! isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
|
||||
return false;
|
||||
}
|
||||
// Check if original ip has already been restored, e.g. by nginx - assume it was from cloudflare then.
|
||||
if ( isset( $_SERVER['REMOTE_ADDR'] ) && $_SERVER['REMOTE_ADDR'] === $_SERVER['HTTP_CF_CONNECTING_IP'] ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return rocket_is_cf_ip();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a request comes from a CloudFlare IP.
|
||||
*
|
||||
* @since 3.4.1
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function rocket_is_cf_ip() {
|
||||
_deprecated_function( __FUNCTION__, '3.14' );
|
||||
|
||||
// Store original remote address in $original_ip.
|
||||
$original_ip = filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP );
|
||||
if ( ! isset( $original_ip ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cf_ips_values = get_transient( 'rocket_cloudflare_ips' );
|
||||
|
||||
// Cloudflare IPS should always be populated because the code runs before loading Cloudflare addon.
|
||||
if ( false === $cf_ips_values ) {
|
||||
$cf_ips_values = (object) [
|
||||
'success' => true,
|
||||
'result' => (object) [],
|
||||
];
|
||||
|
||||
$cf_ips_values->result->ipv4_cidrs = [
|
||||
'103.21.244.0/22',
|
||||
'103.22.200.0/22',
|
||||
'103.31.4.0/22',
|
||||
'104.16.0.0/12',
|
||||
'108.162.192.0/18',
|
||||
'131.0.72.0/22',
|
||||
'141.101.64.0/18',
|
||||
'162.158.0.0/15',
|
||||
'172.64.0.0/13',
|
||||
'173.245.48.0/20',
|
||||
'188.114.96.0/20',
|
||||
'190.93.240.0/20',
|
||||
'197.234.240.0/22',
|
||||
'198.41.128.0/17',
|
||||
];
|
||||
|
||||
$cf_ips_values->result->ipv6_cidrs = [
|
||||
'2400:cb00::/32',
|
||||
'2405:8100::/32',
|
||||
'2405:b500::/32',
|
||||
'2606:4700::/32',
|
||||
'2803:f800::/32',
|
||||
'2c0f:f248::/32',
|
||||
'2a06:98c0::/29',
|
||||
];
|
||||
}
|
||||
|
||||
if ( strpos( $original_ip, ':' ) === false ) {
|
||||
$cf_ip_ranges = $cf_ips_values->result->ipv4_cidrs;
|
||||
foreach ( $cf_ip_ranges as $range ) {
|
||||
if ( rocket_ipv4_in_range( $original_ip, $range ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$cf_ip_ranges = $cf_ips_values->result->ipv6_cidrs;
|
||||
$ipv6 = get_rocket_ipv6_full( $original_ip );
|
||||
foreach ( $cf_ip_ranges as $range ) {
|
||||
if ( rocket_ipv6_in_range( $ipv6, $range ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fixes Cloudflare Flexible SSL redirect loop
|
||||
*
|
||||
* @since 3.4.1
|
||||
* @author Soponar Cristina
|
||||
*/
|
||||
function rocket_fix_cf_flexible_ssl() {
|
||||
_deprecated_function( __FUNCTION__, '3.14' );
|
||||
|
||||
$is_cf = rocket_is_cloudflare();
|
||||
if ( $is_cf ) {
|
||||
// Fixes Flexible SSL.
|
||||
if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
|
||||
$_SERVER['HTTPS'] = 'on';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* ip_in_range.php - Function to determine if an IP is located in a
|
||||
* specific range as specified via several alternative
|
||||
* formats.
|
||||
*
|
||||
* Network ranges can be specified as:
|
||||
* 1. Wildcard format: 1.2.3.*
|
||||
* 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
|
||||
* 3. Start-End IP format: 1.2.3.0-1.2.3.255
|
||||
*
|
||||
* Return value BOOLEAN : ip_in_range($ip, $range);
|
||||
*
|
||||
* Copyright 2008: Paul Gregg <pgregg@pgregg.com>
|
||||
* 10 January 2008
|
||||
* Version: 1.2
|
||||
*
|
||||
* Source website: http://www.pgregg.com/projects/php/ip_in_range/
|
||||
* Version 1.2
|
||||
*
|
||||
* This software is Donationware - if you feel you have benefited from
|
||||
* the use of this tool then please consider a donation. The value of
|
||||
* which is entirely left up to your discretion.
|
||||
* http://www.pgregg.com/donate/
|
||||
*
|
||||
* Please do not remove this header, or source attibution from this file.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by James Greene <james@cloudflare.com> to include IPV6 support
|
||||
* (original version only supported IPV4).
|
||||
* 21 May 2012
|
||||
*/
|
||||
|
||||
// In order to simplify working with IP addresses (in binary) and their
|
||||
// netmasks, it is easier to ensure that the binary strings are padded
|
||||
// with zeros out to 32 characters - IP addresses are 32 bit numbers
|
||||
function rocket_decbin32($dec) {
|
||||
_deprecated_function( __FUNCTION__, '3.14' );
|
||||
|
||||
return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
// This function takes 2 arguments, an IP address and a "range" in several
|
||||
// different formats.
|
||||
// Network ranges can be specified as:
|
||||
// 1. Wildcard format: 1.2.3.*
|
||||
// 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
|
||||
// 3. Start-End IP format: 1.2.3.0-1.2.3.255
|
||||
// The function will return true if the supplied IP is within the range.
|
||||
// Note little validation is done on the range inputs - it expects you to
|
||||
// use one of the above 3 formats.
|
||||
function rocket_ipv4_in_range($ip, $range) {
|
||||
_deprecated_function( __FUNCTION__, '3.14' );
|
||||
|
||||
if (strpos($range, '/') !== false) {
|
||||
// $range is in IP/NETMASK format
|
||||
list($range, $netmask) = explode('/', $range, 2);
|
||||
if (strpos($netmask, '.') !== false) {
|
||||
// $netmask is a 255.255.0.0 format
|
||||
$netmask = str_replace('*', '0', $netmask);
|
||||
$netmask_dec = ip2long($netmask);
|
||||
return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
|
||||
} else {
|
||||
// $netmask is a CIDR size block
|
||||
// fix the range argument
|
||||
$x = explode('.', $range);
|
||||
while(count($x)<4) $x[] = '0';
|
||||
list($a,$b,$c,$d) = $x;
|
||||
$range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
|
||||
$range_dec = ip2long($range);
|
||||
$ip_dec = ip2long($ip);
|
||||
|
||||
# Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
|
||||
#$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
|
||||
|
||||
# Strategy 2 - Use math to create it
|
||||
$wildcard_dec = pow(2, (32-$netmask)) - 1;
|
||||
$netmask_dec = ~ $wildcard_dec;
|
||||
|
||||
return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
|
||||
}
|
||||
} else {
|
||||
// range might be 255.255.*.* or 1.2.3.0-1.2.3.255
|
||||
if (strpos($range, '*') !==false) { // a.b.*.* format
|
||||
// Just convert to A-B format by setting * to 0 for A and 255 for B
|
||||
$lower = str_replace('*', '0', $range);
|
||||
$upper = str_replace('*', '255', $range);
|
||||
$range = "$lower-$upper";
|
||||
}
|
||||
|
||||
if (strpos($range, '-')!==false) { // A-B format
|
||||
list($lower, $upper) = explode('-', $range, 2);
|
||||
$lower_dec = (float)sprintf("%u",ip2long($lower));
|
||||
$upper_dec = (float)sprintf("%u",ip2long($upper));
|
||||
$ip_dec = (float)sprintf("%u",ip2long($ip));
|
||||
return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function rocket_ip2long6($ip) {
|
||||
_deprecated_function( __FUNCTION__, '3.14' );
|
||||
|
||||
if (substr_count($ip, '::')) {
|
||||
$ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip, ':')) . ':', $ip);
|
||||
}
|
||||
|
||||
$ip = explode(':', $ip);
|
||||
$r_ip = '';
|
||||
foreach ($ip as $v) {
|
||||
$r_ip .= str_pad( base_convert( preg_replace("/[^0-9a-fA-F]/", "", $v ), 16, 2 ), 16, 0, STR_PAD_LEFT );
|
||||
}
|
||||
|
||||
return base_convert($r_ip, 2, 10);
|
||||
}
|
||||
|
||||
// Get the ipv6 full format and return it as a decimal value.
|
||||
function get_rocket_ipv6_full($ip) {
|
||||
_deprecated_function( __FUNCTION__, '3.14' );
|
||||
|
||||
$pieces = explode ("/", $ip, 2);
|
||||
$left_piece = $pieces[0];
|
||||
$right_piece = null;
|
||||
if (count($pieces) > 1) $right_piece = $pieces[1];
|
||||
|
||||
// Extract out the main IP pieces
|
||||
$ip_pieces = explode("::", $left_piece, 2);
|
||||
$main_ip_piece = $ip_pieces[0];
|
||||
$last_ip_piece = "";
|
||||
if (count($ip_pieces) > 1) $last_ip_piece = $ip_pieces[1];
|
||||
|
||||
// Pad out the shorthand entries.
|
||||
$main_ip_pieces = explode(":", $main_ip_piece);
|
||||
foreach($main_ip_pieces as $key=>$val) {
|
||||
$main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
// Check to see if the last IP block (part after ::) is set
|
||||
$last_piece = "";
|
||||
$size = count($main_ip_pieces);
|
||||
if (trim($last_ip_piece) != "") {
|
||||
$last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
|
||||
|
||||
// Build the full form of the IPV6 address considering the last IP block set
|
||||
for ($i = $size; $i < 7; $i++) {
|
||||
$main_ip_pieces[$i] = "0000";
|
||||
}
|
||||
$main_ip_pieces[7] = $last_piece;
|
||||
}
|
||||
else {
|
||||
// Build the full form of the IPV6 address
|
||||
for ($i = $size; $i < 8; $i++) {
|
||||
$main_ip_pieces[$i] = "0000";
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild the final long form IPV6 address
|
||||
$final_ip = implode(":", $main_ip_pieces);
|
||||
|
||||
return rocket_ip2long6($final_ip);
|
||||
}
|
||||
|
||||
// Determine whether the IPV6 address is within range.
|
||||
// $ip is the IPV6 address in decimal format to check if its within the IP range created by the cloudflare IPV6 address, $range_ip.
|
||||
// $ip and $range_ip are converted to full IPV6 format.
|
||||
// Returns true if the IPV6 address, $ip, is within the range from $range_ip. False otherwise.
|
||||
function rocket_ipv6_in_range($ip, $range_ip) {
|
||||
_deprecated_function( __FUNCTION__, '3.14' );
|
||||
|
||||
$pieces = explode ("/", $range_ip, 2);
|
||||
$left_piece = $pieces[0];
|
||||
$right_piece = $pieces[1];
|
||||
|
||||
// Extract out the main IP pieces
|
||||
$ip_pieces = explode("::", $left_piece, 2);
|
||||
$main_ip_piece = $ip_pieces[0];
|
||||
$last_ip_piece = $ip_pieces[1];
|
||||
|
||||
// Pad out the shorthand entries.
|
||||
$main_ip_pieces = explode(":", $main_ip_piece);
|
||||
foreach($main_ip_pieces as $key=>$val) {
|
||||
$main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
// Create the first and last pieces that will denote the IPV6 range.
|
||||
$first = $main_ip_pieces;
|
||||
$last = $main_ip_pieces;
|
||||
|
||||
// Check to see if the last IP block (part after ::) is set
|
||||
$last_piece = "";
|
||||
$size = count($main_ip_pieces);
|
||||
if (trim($last_ip_piece) != "") {
|
||||
$last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
|
||||
|
||||
// Build the full form of the IPV6 address considering the last IP block set
|
||||
for ($i = $size; $i < 7; $i++) {
|
||||
$first[$i] = "0000";
|
||||
$last[$i] = "ffff";
|
||||
}
|
||||
$main_ip_pieces[7] = $last_piece;
|
||||
}
|
||||
else {
|
||||
// Build the full form of the IPV6 address
|
||||
for ($i = $size; $i < 8; $i++) {
|
||||
$first[$i] = "0000";
|
||||
$last[$i] = "ffff";
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild the final long form IPV6 address
|
||||
$first = rocket_ip2long6(implode(":", $first));
|
||||
$last = rocket_ip2long6(implode(":", $last));
|
||||
$in_range = ($ip >= $first && $ip <= $last);
|
||||
|
||||
return $in_range;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter plugin fetching API results to inject Imagify
|
||||
*
|
||||
* @since 2.10.7
|
||||
* @since 3.14.2 deprecated
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param object|WP_Error $result Response object or WP_Error.
|
||||
* @param string $action The type of information being requested from the Plugin Install API.
|
||||
* @param object $args Plugin API arguments.
|
||||
*
|
||||
* @return array Updated array of results
|
||||
*/
|
||||
function rocket_add_imagify_api_result( $result, $action, $args ) {
|
||||
if ( empty( $args->browse ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ( 'featured' !== $args->browse && 'recommended' !== $args->browse && 'popular' !== $args->browse ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ( ! isset( $result->info['page'] ) || 1 < $result->info['page'] ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ( is_plugin_active( 'imagify/imagify.php' ) || is_plugin_active_for_network( 'imagify/imagify.php' ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
// grab all slugs from the api results.
|
||||
$result_slugs = wp_list_pluck( $result->plugins, 'slug' );
|
||||
|
||||
if ( in_array( 'imagify', $result_slugs, true ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$query_args = [
|
||||
'slug' => 'imagify',
|
||||
'fields' => [
|
||||
'icons' => true,
|
||||
'active_installs' => true,
|
||||
'short_description' => true,
|
||||
'group' => true,
|
||||
],
|
||||
];
|
||||
$imagify_data = plugins_api( 'plugin_information', $query_args );
|
||||
|
||||
if ( is_wp_error( $imagify_data ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ( 'featured' === $args->browse ) {
|
||||
array_push( $result->plugins, $imagify_data );
|
||||
} else {
|
||||
array_unshift( $result->plugins, $imagify_data );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Add "Cache options" metabox
|
||||
*
|
||||
* @since 3.15 deprecated
|
||||
* @since 2.5
|
||||
*/
|
||||
function rocket_cache_options_meta_boxes() {
|
||||
|
||||
if ( ! rocket_can_display_options() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( current_user_can( 'rocket_manage_options' ) ) {
|
||||
$cpts = get_post_types(
|
||||
[
|
||||
'public' => true,
|
||||
],
|
||||
'objects'
|
||||
);
|
||||
unset( $cpts['attachment'] );
|
||||
|
||||
$cpts = apply_filters( 'rocket_metabox_options_post_types', $cpts );
|
||||
|
||||
foreach ( $cpts as $cpt => $cpt_object ) {
|
||||
$label = $cpt_object->labels->singular_name;
|
||||
add_meta_box( 'rocket_post_exclude', sprintf( __( 'WP Rocket Options', 'rocket' ), $label ), 'rocket_display_cache_options_meta_boxes', $cpt, 'side', 'core' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays some checkbox to de/activate some cache options
|
||||
*
|
||||
* @since 3.15 deprecated
|
||||
* @since 2.5
|
||||
*/
|
||||
function rocket_display_cache_options_meta_boxes() {
|
||||
if ( current_user_can( 'rocket_manage_options' ) ) {
|
||||
global $post, $pagenow;
|
||||
wp_nonce_field( 'rocket_box_option', '_rocketnonce', false, true );
|
||||
?>
|
||||
|
||||
<div class="misc-pub-section">
|
||||
<?php
|
||||
$reject_current_uri = false;
|
||||
if ( 'post-new.php' !== $pagenow ) {
|
||||
$rejected_uris = array_flip( get_rocket_option( 'cache_reject_uri', [] ) );
|
||||
$path = rocket_clean_exclude_file( get_permalink( $post->ID ) );
|
||||
|
||||
if ( isset( $rejected_uris[ $path ] ) ) {
|
||||
$reject_current_uri = true;
|
||||
}
|
||||
}
|
||||
?>
|
||||
<input name="rocket_post_nocache" id="rocket_post_nocache" type="checkbox" title="<?php esc_html_e( 'Never cache this page', 'rocket' ); ?>" <?php checked( $reject_current_uri, true ); ?>><label for="rocket_post_nocache"><?php esc_html_e( 'Never cache this page', 'rocket' ); ?></label>
|
||||
</div>
|
||||
|
||||
<div class="misc-pub-section">
|
||||
<p><?php esc_html_e( 'Activate these options on this post:', 'rocket' ); ?></p>
|
||||
<?php
|
||||
$fields = [];
|
||||
|
||||
$old_fields = $fields;
|
||||
|
||||
/**
|
||||
* WP Rocket Metabox fields on post edit page.
|
||||
*
|
||||
* @param string[] $fields Metaboxes fields.
|
||||
*/
|
||||
$fields = apply_filters( 'rocket_meta_boxes_fields', $fields );
|
||||
|
||||
if ( ! is_array( $fields ) ) {
|
||||
$fields = $old_fields;
|
||||
}
|
||||
|
||||
foreach ( $fields as $field => $label ) {
|
||||
$disabled = disabled( ! get_rocket_option( $field ), true, false );
|
||||
// translators: %s is the name of the option.
|
||||
$title = $disabled ? ' title="' . esc_attr( sprintf( __( 'Activate first the %s option.', 'rocket' ), $label ) ) . '"' : '';
|
||||
$class = $disabled ? ' class="rkt-disabled"' : '';
|
||||
$checked = ! $disabled ? checked( ! get_post_meta( $post->ID, '_rocket_exclude_' . $field, true ), true, false ) : '';
|
||||
?>
|
||||
|
||||
<input name="rocket_post_exclude_hidden[<?php echo esc_attr( $field ); ?>]" type="hidden" value="on">
|
||||
<input name="rocket_post_exclude[<?php echo esc_attr( $field ); ?>]" id="rocket_post_exclude_<?php echo esc_attr( $field ); ?>" type="checkbox"<?php echo $title; ?><?php echo $checked; ?><?php echo $disabled; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view. ?>>
|
||||
<label for="rocket_post_exclude_<?php echo esc_attr( $field ); ?>"<?php echo $title; ?><?php echo $class; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view. ?>><?php echo esc_html( $label ); ?></label><br>
|
||||
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
|
||||
<p class="rkt-note">
|
||||
<?php
|
||||
// translators: %1$s = opening strong tag, %2$s = closing strong tag.
|
||||
printf( esc_html__( '%1$sNote:%2$s None of these options will be applied if this post has been excluded from cache in the global cache settings.', 'rocket' ), '<strong>', '</strong>' );
|
||||
?>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
/**
|
||||
* Fires after WP Rocket’s metabox.
|
||||
*
|
||||
* @since 3.6
|
||||
*/
|
||||
do_action( 'rocket_after_options_metabox' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage the cache options from the metabox.
|
||||
*
|
||||
* @since 3.15 deprecated
|
||||
* @since 2.5
|
||||
*/
|
||||
function rocket_save_metabox_options() {
|
||||
if ( current_user_can( 'rocket_manage_options' ) &&
|
||||
isset( $_POST['post_ID'], $_POST['rocket_post_exclude_hidden'], $_POST['_rocketnonce'] ) ) {
|
||||
|
||||
check_admin_referer( 'rocket_box_option', '_rocketnonce' );
|
||||
|
||||
// No cache field.
|
||||
if ( isset( $_POST['post_status'] ) && 'publish' === $_POST['post_status'] ) {
|
||||
$new_cache_reject_uri = $cache_reject_uri = get_rocket_option( 'cache_reject_uri', [] ); // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found
|
||||
$rejected_uris = array_flip( $cache_reject_uri );
|
||||
$path = rocket_clean_exclude_file( get_permalink( (int) $_POST['post_ID'] ) );
|
||||
|
||||
if ( isset( $_POST['rocket_post_nocache'] ) ) {
|
||||
if ( ! isset( $rejected_uris[ $path ] ) ) {
|
||||
array_push( $new_cache_reject_uri, $path );
|
||||
}
|
||||
} else {
|
||||
if ( isset( $rejected_uris[ $path ] ) ) {
|
||||
unset( $new_cache_reject_uri[ $rejected_uris[ $path ] ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $new_cache_reject_uri !== $cache_reject_uri ) {
|
||||
// Update the "Never cache the following pages" option.
|
||||
update_rocket_option( 'cache_reject_uri', $new_cache_reject_uri );
|
||||
|
||||
// Update config file.
|
||||
rocket_generate_config_file();
|
||||
}
|
||||
}
|
||||
|
||||
// Options fields.
|
||||
$fields = [];
|
||||
|
||||
$old_fields = $fields;
|
||||
|
||||
/**
|
||||
* Metaboxes fields.
|
||||
*
|
||||
* @param string[] $fields Metaboxes fields.
|
||||
*/
|
||||
$fields = apply_filters( 'rocket_meta_boxes_fields', $fields );
|
||||
|
||||
if ( ! is_array( $old_fields ) ) {
|
||||
$fields = $old_fields;
|
||||
}
|
||||
|
||||
$fields = array_keys( $fields );
|
||||
|
||||
foreach ( $fields as $field ) {
|
||||
if ( isset( $_POST['rocket_post_exclude_hidden'][ $field ] ) ) {
|
||||
if ( isset( $_POST['rocket_post_exclude'][ $field ] ) ) {
|
||||
delete_post_meta( (int) $_POST['post_ID'], '_rocket_exclude_' . $field );
|
||||
} else {
|
||||
if ( get_rocket_option( $field ) ) {
|
||||
update_post_meta( (int) $_POST['post_ID'], '_rocket_exclude_' . $field, true );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,402 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Add a message about Imagify on the "Upload New Media" screen and WP Rocket options page.
|
||||
*
|
||||
* @since 2.7
|
||||
* @deprecated 3.2
|
||||
*/
|
||||
function rocket_imagify_notice() {
|
||||
_deprecated_function( __FUNCTION__, '3.2' );
|
||||
$current_screen = get_current_screen();
|
||||
|
||||
// Add the notice only on the "WP Rocket" settings, "Media Library" & "Upload New Media" screens.
|
||||
if ( 'admin_notices' === current_filter() && ( isset( $current_screen ) && 'settings_page_wprocket' !== $current_screen->base ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$boxes = get_user_meta( $GLOBALS['current_user']->ID, 'rocket_boxes', true );
|
||||
|
||||
if ( defined( 'IMAGIFY_VERSION' ) || in_array( __FUNCTION__, (array) $boxes, true ) || 1 === get_option( 'wp_rocket_dismiss_imagify_notice' ) || ! current_user_can( 'manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$imagify_plugin = 'imagify/imagify.php';
|
||||
$is_imagify_installed = rocket_is_plugin_installed( $imagify_plugin );
|
||||
|
||||
$action_url = $is_imagify_installed ?
|
||||
rocket_get_plugin_activation_link( $imagify_plugin )
|
||||
:
|
||||
wp_nonce_url( add_query_arg(
|
||||
array(
|
||||
'action' => 'install-plugin',
|
||||
'plugin' => 'imagify',
|
||||
),
|
||||
admin_url( 'update.php' )
|
||||
), 'install-plugin_imagify' );
|
||||
|
||||
$details_url = add_query_arg(
|
||||
array(
|
||||
'tab' => 'plugin-information',
|
||||
'plugin' => 'imagify',
|
||||
'TB_iframe' => true,
|
||||
'width' => 722,
|
||||
'height' => 949,
|
||||
),
|
||||
admin_url( 'plugin-install.php' )
|
||||
);
|
||||
|
||||
$classes = $is_imagify_installed ? '' : ' install-now';
|
||||
$cta_txt = $is_imagify_installed ? esc_html__( 'Activate Imagify', 'rocket' ) : esc_html__( 'Install Imagify for Free', 'rocket' );
|
||||
|
||||
$dismiss_url = wp_nonce_url(
|
||||
admin_url( 'admin-post.php?action=rocket_ignore&box=' . __FUNCTION__ ),
|
||||
'rocket_ignore_' . __FUNCTION__
|
||||
);
|
||||
?>
|
||||
|
||||
<div id="plugin-filter" class="updated plugin-card plugin-card-imagify rkt-imagify-notice">
|
||||
<a href="<?php echo $dismiss_url; ?>" class="rkt-cross"><span class="dashicons dashicons-no"></span></a>
|
||||
|
||||
<p class="rkt-imagify-logo">
|
||||
<img src="<?php echo WP_ROCKET_ASSETS_IMG_URL; ?>logo-imagify.png" srcset="<?php echo WP_ROCKET_ASSETS_IMG_URL; ?>logo-imagify.svg 2x" alt="Imagify" width="150" height="18">
|
||||
</p>
|
||||
<p class="rkt-imagify-msg">
|
||||
<?php _e( 'Speed up your website and boost your SEO by reducing image file sizes without losing quality with Imagify.', 'rocket' ); ?>
|
||||
</p>
|
||||
<p class="rkt-imagify-cta">
|
||||
<a data-slug="imagify" href="<?php echo $action_url; ?>" class="button button-primary<?php echo $classes; ?>"><?php echo $cta_txt; ?></a>
|
||||
<?php if ( ! $is_imagify_installed ) : ?>
|
||||
<br><a data-slug="imagify" data-name="Imagify Image Optimizer" class="thickbox open-plugin-details-modal" href="<?php echo $details_url; ?>"><?php _e( 'More details', 'rocket' ); ?></a>
|
||||
<?php endif; ?>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'run_rocket_preload_cache' ) ) :
|
||||
/**
|
||||
* Launches the preload
|
||||
*
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
* @deprecated 3.2
|
||||
*
|
||||
* @param string $spider The spider name.
|
||||
* @param bool $do_sitemap_preload Do the sitemap preload.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function run_rocket_preload_cache( $spider, $do_sitemap_preload = true ) {
|
||||
_deprecated_function( __FUNCTION__, '3.2' );
|
||||
|
||||
// Preload cache.
|
||||
run_rocket_bot( $spider );
|
||||
|
||||
if ( $do_sitemap_preload & get_rocket_option( 'sitemap_preload', false ) ) {
|
||||
$rocket_background_process = $GLOBALS['rocket_sitemap_background_process'];
|
||||
|
||||
if ( method_exists( $rocket_background_process, 'cancel_process' ) ) {
|
||||
$rocket_background_process->cancel_process();
|
||||
}
|
||||
|
||||
delete_transient( 'rocket_sitemap_preload_running' );
|
||||
delete_transient( 'rocket_sitemap_preload_complete' );
|
||||
run_rocket_sitemap_preload();
|
||||
}
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'do_rocket_bot_cache_json' ) ) :
|
||||
/**
|
||||
* Run WP Rocket Bot when a post is added, updated or deleted
|
||||
*
|
||||
* @since 1.3.2
|
||||
* @deprecated 3.2
|
||||
*/
|
||||
function do_rocket_bot_cache_json() {
|
||||
_deprecated_function( __FUNCTION__, '3.2' );
|
||||
return false;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_process_sitemap' ) ) {
|
||||
/**
|
||||
* Processes the sitemaps recursively
|
||||
*
|
||||
* @since 2.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $sitemap_url URL of the sitemap.
|
||||
* @param array $urls An array of URLs.
|
||||
* @return array Empty array or array containing URLs
|
||||
*/
|
||||
function rocket_process_sitemap( $sitemap_url, $urls = array() ) {
|
||||
_deprecated_function( __FUNCTION__, '3.2' );
|
||||
$tmp_urls = array();
|
||||
|
||||
/**
|
||||
* Filters the arguments for the sitemap preload request
|
||||
*
|
||||
* @since 2.10.8
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $args Arguments for the request.
|
||||
*/
|
||||
$args = apply_filters( 'rocket_preload_sitemap_request_args', array(
|
||||
'user-agent' => 'WP Rocket/Sitemaps',
|
||||
'sslverify' => apply_filters( 'https_local_ssl_verify', true ),
|
||||
) );
|
||||
|
||||
$sitemap = wp_remote_get( esc_url( $sitemap_url ), $args );
|
||||
|
||||
if ( is_wp_error( $sitemap ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$xml_data = wp_remote_retrieve_body( $sitemap );
|
||||
|
||||
if ( empty( $xml_data ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
libxml_use_internal_errors( true );
|
||||
|
||||
$xml = simplexml_load_string( $xml_data );
|
||||
|
||||
if ( false === $xml ) {
|
||||
libxml_clear_errors();
|
||||
return array();
|
||||
}
|
||||
|
||||
$url_count = count( $xml->url );
|
||||
|
||||
if ( $url_count > 0 ) {
|
||||
for ( $i = 0; $i < $url_count; $i++ ) {
|
||||
$page_url = (string) $xml->url[ $i ]->loc;
|
||||
$tmp_urls[] = $page_url;
|
||||
}
|
||||
} else {
|
||||
// Sub sitemap?
|
||||
$sitemap_children = count( $xml->sitemap );
|
||||
if ( $sitemap_children > 0 ) {
|
||||
for ( $i = 0; $i < $sitemap_children; $i++ ) {
|
||||
$sub_sitemap_url = (string) $xml->sitemap[ $i ]->loc;
|
||||
$urls = rocket_process_sitemap( $sub_sitemap_url, $urls );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$urls = array_merge( $urls, $tmp_urls );
|
||||
return $urls;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'rocket_sitemap_preload_complete' ) ) {
|
||||
/**
|
||||
* This notice is displayed after the sitemap preload is complete
|
||||
*
|
||||
* @since 2.11
|
||||
* @deprecated 3.2
|
||||
* @author Remy Perona
|
||||
*/
|
||||
function rocket_sitemap_preload_complete() {
|
||||
_deprecated_function( __FUNCTION__, '3.2' );
|
||||
$screen = get_current_screen();
|
||||
|
||||
/** This filter is documented in inc/admin-bar.php */
|
||||
if ( ! current_user_can( apply_filters( 'rocket_capacity', 'manage_options' ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 'settings_page_wprocket' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$result = get_transient( 'rocket_sitemap_preload_complete' );
|
||||
if ( false === $result ) {
|
||||
return;
|
||||
}
|
||||
|
||||
delete_transient( 'rocket_sitemap_preload_complete' );
|
||||
|
||||
rocket_notice_html( array(
|
||||
// translators: %d is the number of pages preloaded.
|
||||
'message' => sprintf( __( 'Sitemap preload: %d pages have been cached.', 'rocket' ), $result ),
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'rocket_sitemap_preload_running' ) ) {
|
||||
/**
|
||||
* This notice is displayed when the sitemap preload is running
|
||||
*
|
||||
* @since 2.11
|
||||
* @deprecated 3.2
|
||||
* @author Remy Perona
|
||||
*/
|
||||
function rocket_sitemap_preload_running() {
|
||||
_deprecated_function( __FUNCTION__, '3.2' );
|
||||
$screen = get_current_screen();
|
||||
|
||||
// This filter is documented in inc/admin-bar.php.
|
||||
if ( ! current_user_can( apply_filters( 'rocket_capacity', 'manage_options' ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 'settings_page_wprocket' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$running = get_transient( 'rocket_sitemap_preload_running' );
|
||||
if ( false === $running ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_notice_html( array(
|
||||
// translators: %d = Number of pages preloaded.
|
||||
'message' => sprintf( __( 'Sitemap preload: %d uncached pages have now been preloaded. (refresh to see progress)', 'rocket' ), $running ),
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'run_rocket_bot_after_clean_post' ) ) {
|
||||
/**
|
||||
* Actions to be done after the purge cache files of a post
|
||||
* By Default, this hook call the WP Rocket Bot (cache json)
|
||||
*
|
||||
* @deprecated 3.2
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @param object $post The post object.
|
||||
* @param array $purge_urls An array of URLs to clean.
|
||||
* @param string $lang The language to clean.
|
||||
*/
|
||||
function run_rocket_bot_after_clean_post( $post, $purge_urls, $lang ) {
|
||||
_deprecated_function( __FUNCTION__, '3.2' );
|
||||
// Run robot only if post is published.
|
||||
if ( 'publish' !== $post->post_status ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add Homepage URL to $purge_urls for bot crawl.
|
||||
array_push( $purge_urls, get_rocket_i18n_home_url( $lang ) );
|
||||
|
||||
// Add default WordPress feeds (posts & comments).
|
||||
array_push( $purge_urls, get_feed_link() );
|
||||
array_push( $purge_urls, get_feed_link( 'comments_' ) );
|
||||
|
||||
// Get the author page.
|
||||
$purge_author = array( get_author_posts_url( $post->post_author ) );
|
||||
|
||||
// Get all dates archive page.
|
||||
$purge_dates = get_rocket_post_dates_urls( $post->ID );
|
||||
|
||||
// Remove dates archives page and author page to preload cache.
|
||||
$purge_urls = array_diff( $purge_urls, $purge_dates, $purge_author );
|
||||
|
||||
// Create json file and run WP Rocket Bot.
|
||||
$json_encode_urls = '["' . implode( '","', array_filter( $purge_urls ) ) . '"]';
|
||||
if ( rocket_put_content( WP_ROCKET_PATH . 'cache.json', $json_encode_urls ) ) {
|
||||
global $do_rocket_bot_cache_json;
|
||||
$do_rocket_bot_cache_json = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'run_rocket_bot_after_clean_term' ) ) {
|
||||
/**
|
||||
* Actions to be done after the purge cache files of a term
|
||||
* By Default, this hook call the WP Rocket Bot (cache json)
|
||||
*
|
||||
* @deprecated 3.2
|
||||
* @since 2.6.8
|
||||
*
|
||||
* @param object $post The post object.
|
||||
* @param array $purge_urls An array of URLs to clean.
|
||||
* @param string $lang The language to clean.
|
||||
*/
|
||||
function run_rocket_bot_after_clean_term( $post, $purge_urls, $lang ) {
|
||||
_deprecated_function( __FUNCTION__, '3.2' );
|
||||
// Add Homepage URL to $purge_urls for bot crawl.
|
||||
array_push( $purge_urls, get_rocket_i18n_home_url( $lang ) );
|
||||
|
||||
// Create json file and run WP Rocket Bot.
|
||||
$json_encode_urls = '["' . implode( '","', array_filter( $purge_urls ) ) . '"]';
|
||||
if ( rocket_put_content( WP_ROCKET_PATH . 'cache.json', $json_encode_urls ) ) {
|
||||
global $do_rocket_bot_cache_json;
|
||||
$do_rocket_bot_cache_json = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'rocket_clean_directory_for_default_language_on_wpml' ) ) {
|
||||
/**
|
||||
* Conflict with WPML: Clear the homepage when the "Use directory for default language" option is activated.
|
||||
*
|
||||
* @since 2.6.8
|
||||
* @deprecated 3.2.4
|
||||
*/
|
||||
function rocket_clean_directory_for_default_language_on_wpml() {
|
||||
_deprecated_function( __FUNCTION__, '3.2.4' );
|
||||
$option = get_option( 'icl_sitepress_settings' );
|
||||
|
||||
if ( 1 === $option['language_negotiation_type'] && $option['urls']['directory_for_default_language'] ) {
|
||||
rocket_clean_files( home_url() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'rocket_fetch_and_cache_busting' ) ) {
|
||||
/**
|
||||
* Fetch and save the cache busting file content
|
||||
*
|
||||
* @since 2.10
|
||||
* @deprecated 3.2.5
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $src Original URL of the asset.
|
||||
* @param array $cache_busting_paths Paths used to generated the cache busting file.
|
||||
* @param string $abspath_src Absolute path to the asset.
|
||||
* @param string $current_filter Current filter value.
|
||||
* @return bool true if successful, false otherwise
|
||||
*/
|
||||
function rocket_fetch_and_cache_busting( $src, $cache_busting_paths, $abspath_src, $current_filter ) {
|
||||
_deprecated_function( __FUNCTION__, '3.2.5' );
|
||||
if ( wp_is_stream( $src ) ) {
|
||||
$response = wp_remote_get( $src );
|
||||
$content = wp_remote_retrieve_body( $response );
|
||||
} else {
|
||||
$content = rocket_direct_filesystem()->get_contents( $src );
|
||||
}
|
||||
|
||||
if ( ! $content ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( 'style_loader_src' === $current_filter ) {
|
||||
/**
|
||||
* Filters the Document Root path to use during CSS minification to rewrite paths
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param string The Document Root path.
|
||||
*/
|
||||
$document_root = apply_filters( 'rocket_min_documentRoot', wp_normalize_path( dirname( $_SERVER['SCRIPT_FILENAME'] ) ) );
|
||||
|
||||
// Rewrite import/url in CSS content to add the absolute path to the file.
|
||||
$content = Minify_CSS_UriRewriter::rewrite( $content, dirname( $abspath_src ), $document_root );
|
||||
}
|
||||
|
||||
if ( ! rocket_direct_filesystem()->is_dir( $cache_busting_paths['bustingpath'] ) ) {
|
||||
rocket_mkdir_p( $cache_busting_paths['bustingpath'] );
|
||||
}
|
||||
|
||||
rocket_mkdir_p( dirname( $cache_busting_paths['filepath'] ) );
|
||||
|
||||
return rocket_put_content( $cache_busting_paths['filepath'], $content );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,874 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
if ( ! function_exists( 'get_rocket_footprint' ) ) :
|
||||
/**
|
||||
* Get WP Rocket footprint
|
||||
*
|
||||
* @deprecated 3.3
|
||||
* @since 3.0.5 White label footprint if WP_ROCKET_WHITE_LABEL_FOOTPRINT is defined.
|
||||
* @since 2.0
|
||||
*
|
||||
* @param bool $debug (default: true) If true, adds the date of generation cache file.
|
||||
* @return string The footprint that will be printed
|
||||
*/
|
||||
function get_rocket_footprint( $debug = true ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\WP_Rocket\Buffer\Cache->get_rocket_footprint()' );
|
||||
$footprint = defined( 'WP_ROCKET_WHITE_LABEL_FOOTPRINT' ) ?
|
||||
"\n" . '<!-- Cached for great performance' :
|
||||
"\n" . '<!-- This website is like a Rocket, isn\'t it? Performance optimized by ' . WP_ROCKET_PLUGIN_NAME . '. Learn more: https://wp-rocket.me';
|
||||
if ( $debug ) {
|
||||
$footprint .= ' - Debug: cached@' . time();
|
||||
}
|
||||
$footprint .= ' -->';
|
||||
return $footprint;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_lazyload_script' ) ) :
|
||||
/**
|
||||
* Add lazyload options to the footer
|
||||
*
|
||||
* @deprecated 3.3
|
||||
* @since 2.11 load options in the footer and add filter for the treshold
|
||||
* @since 1.3.5 It's possible to exclude LazyLoad process by used do_rocket_lazyload filter
|
||||
* @since 1.1.0 This code is insert in head with inline script for more performance
|
||||
* @since 1.0
|
||||
*/
|
||||
function rocket_lazyload_script() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\RocketLazyload\Assets::insertLazyloadScript()' );
|
||||
if ( ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ( ! get_rocket_option( 'lazyload' ) && ! get_rocket_option( 'lazyload_iframes' ) ) || ( ! apply_filters( 'do_rocket_lazyload', true ) && ! apply_filters( 'do_rocket_lazyload_iframes', true ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
|
||||
$elements = [];
|
||||
|
||||
if ( get_rocket_option( 'lazyload' ) ) {
|
||||
$elements[] = 'img';
|
||||
}
|
||||
|
||||
if ( get_rocket_option( 'lazyload_iframes' ) ) {
|
||||
$elements[] = 'iframe';
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the threshold at which lazyload is triggered
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param int $threshold Threshold value.
|
||||
*/
|
||||
$threshold = apply_filters( 'rocket_lazyload_threshold', 300 );
|
||||
|
||||
echo '<script>(function(w, d){
|
||||
var b = d.getElementsByTagName("body")[0];
|
||||
var s = d.createElement("script"); s.async = true;
|
||||
s.src = !("IntersectionObserver" in w) ? "' . get_rocket_cdn_url( WP_ROCKET_FRONT_JS_URL, array( 'all', 'css_and_js', 'js' ) ) . 'lazyload-8.15.2' . $suffix . '.js" : "' . get_rocket_cdn_url( WP_ROCKET_FRONT_JS_URL, array( 'all', 'css_and_js', 'js' ) ) . 'lazyload-10.17' . $suffix . '.js";
|
||||
w.lazyLoadOptions = {
|
||||
elements_selector: "' . esc_attr( implode( ',', $elements ) ) . '",
|
||||
data_src: "lazy-src",
|
||||
data_srcset: "lazy-srcset",
|
||||
data_sizes: "lazy-sizes",
|
||||
skip_invisible: false,
|
||||
class_loading: "lazyloading",
|
||||
class_loaded: "lazyloaded",
|
||||
threshold: ' . esc_attr( $threshold ) . ',
|
||||
callback_load: function(element) {
|
||||
if ( element.tagName === "IFRAME" && element.dataset.rocketLazyload == "fitvidscompatible" ) {
|
||||
if (element.classList.contains("lazyloaded") ) {
|
||||
if (typeof window.jQuery != "undefined") {
|
||||
if (jQuery.fn.fitVids) {
|
||||
jQuery(element).parent().fitVids();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}; // Your options here. See "recipes" for more information about async.
|
||||
b.appendChild(s);
|
||||
}(window, document));
|
||||
|
||||
// Listen to the Initialized event
|
||||
window.addEventListener(\'LazyLoad::Initialized\', function (e) {
|
||||
// Get the instance and puts it in the lazyLoadInstance variable
|
||||
var lazyLoadInstance = e.detail.instance;
|
||||
|
||||
var observer = new MutationObserver(function(mutations) {
|
||||
mutations.forEach(function(mutation) {
|
||||
lazyLoadInstance.update();
|
||||
} );
|
||||
} );
|
||||
|
||||
var b = document.getElementsByTagName("body")[0];
|
||||
var config = { childList: true, subtree: true };
|
||||
|
||||
observer.observe(b, config);
|
||||
}, false);
|
||||
</script>';
|
||||
|
||||
if ( get_rocket_option( 'lazyload_youtube' ) ) {
|
||||
/**
|
||||
* Filters the resolution of the YouTube thumbnail
|
||||
*
|
||||
* @since 2.11.5
|
||||
* @author Arun Basil Lal
|
||||
*
|
||||
* @param string $thumbnail_resolution The resolution of the thumbnail. Accepted values: default, mqdefault, sddefault, hqdefault, maxresdefault
|
||||
*/
|
||||
$thumbnail_resolution = apply_filters( 'rocket_youtube_thumbnail_resolution', 'hqdefault' );
|
||||
|
||||
echo <<<HTML
|
||||
<script>function lazyLoadThumb(e){var t='<img src="https://i.ytimg.com/vi/ID/$thumbnail_resolution.jpg">',a='<div class="play"></div>';return t.replace("ID",e)+a}function lazyLoadYoutubeIframe(){var e=document.createElement("iframe"),t="https://www.youtube.com/embed/ID?autoplay=1";t+=0===this.dataset.query.length?'':'&'+this.dataset.query;e.setAttribute("src",t.replace("ID",this.dataset.id)),e.setAttribute("frameborder","0"),e.setAttribute("allowfullscreen","1"),this.parentNode.replaceChild(e,this)}document.addEventListener("DOMContentLoaded",function(){var e,t,a=document.getElementsByClassName("rll-youtube-player");for(t=0;t<a.length;t++)e=document.createElement("div"),e.setAttribute("data-id",a[t].dataset.id),e.setAttribute("data-query", a[t].dataset.query),e.innerHTML=lazyLoadThumb(a[t].dataset.id),e.onclick=lazyLoadYoutubeIframe,a[t].appendChild(e)});</script>
|
||||
HTML;
|
||||
}
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_lazyload_enqueue' ) ) :
|
||||
/**
|
||||
* Enqueue the CSS code for Youtube lazyload styling
|
||||
*
|
||||
* @deprecated 3.3
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*/
|
||||
function rocket_lazyload_enqueue() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\RocketLazyload\Assets::insertYoutubeThumbnailCSS()' );
|
||||
if ( ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ( ! get_rocket_option( 'lazyload_iframes' ) ) || ( ! apply_filters( 'do_rocket_lazyload', true ) && ! apply_filters( 'do_rocket_lazyload_iframes', true ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( get_rocket_option( 'lazyload_youtube' ) ) {
|
||||
$css = '.rll-youtube-player{position:relative;padding-bottom:56.23%;height:0;overflow:hidden;max-width:100%;background:#000;margin:5px}.rll-youtube-player iframe{position:absolute;top:0;left:0;width:100%;height:100%;z-index:100;background:0 0}.rll-youtube-player img{bottom:0;display:block;left:0;margin:auto;max-width:100%;width:100%;position:absolute;right:0;top:0;border:none;height:auto;cursor:pointer;-webkit-transition:.4s all;-moz-transition:.4s all;transition:.4s all}.rll-youtube-player img:hover{-webkit-filter:brightness(75%)}.rll-youtube-player .play{height:72px;width:72px;left:50%;top:50%;margin-left:-36px;margin-top:-36px;position:absolute;background:url(' . WP_ROCKET_FRONT_URL . 'img/youtube.png) no-repeat;cursor:pointer}';
|
||||
|
||||
wp_register_style( 'rocket-lazyload', false );
|
||||
wp_enqueue_style( 'rocket-lazyload' );
|
||||
wp_add_inline_style( 'rocket-lazyload', $css );
|
||||
}
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_lazyload_images' ) ) :
|
||||
/**
|
||||
* Replace Gravatar, thumbnails, images in post content and in widget text by LazyLoad
|
||||
*
|
||||
* @deprecated 3.3
|
||||
* @since 2.6 Add the get_image_tag filter
|
||||
* @since 2.2 Better regex pattern in a replace_callback
|
||||
* @since 1.3.5 It's possible to exclude LazyLoad process by used do_rocket_lazyload filter
|
||||
* @since 1.2.0 It's possible to not lazyload an image with data-no-lazy attribute
|
||||
* @since 1.1.0 Don't lazyload if the thumbnail has already been run through previously
|
||||
* @since 1.0.1 Add priority of hooks at maximum later with PHP_INT_MAX
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string Modified HTML content
|
||||
*/
|
||||
function rocket_lazyload_images( $html ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\RocketLazyload\Images::lazyloadImages()' );
|
||||
// Don't LazyLoad if process is stopped for these reasons.
|
||||
if ( ! get_rocket_option( 'lazyload' ) || ! apply_filters( 'do_rocket_lazyload', true ) || is_feed() || is_preview() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) || empty( $html ) || ( defined( 'DONOTLAZYLOAD' ) && DONOTLAZYLOAD ) || wp_script_is( 'twentytwenty-twentytwenty', 'enqueued' ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$html = preg_replace_callback( '#<img([^>]*) src=("(?:[^"]+)"|\'(?:[^\']+)\'|(?:[^ >]+))([^>]*)>#', 'rocket_lazyload_replace_callback', $html );
|
||||
|
||||
return $html;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_lazyload_replace_callback' ) ) :
|
||||
/**
|
||||
* Used to check if we have to LazyLoad this or not
|
||||
*
|
||||
* @deprecated 3.3
|
||||
* @since 2.5.5 Don't apply LazyLoad on images from WP Retina x2
|
||||
* @since 2.5 Don't apply LazyLoad on all images from LayerSlider
|
||||
* @since 2.4.2 Don't apply LazyLoad on all images from Media Grid
|
||||
* @since 2.3.11 Don't apply LazyLoad on all images from Timthumb
|
||||
* @since 2.3.10 Don't apply LazyLoad on all images from Revolution Slider & Justified Image Grid
|
||||
* @since 2.3.8 Don't apply LazyLoad on captcha from Really Simple CAPTCHA
|
||||
* @since 2.2
|
||||
*
|
||||
* @param array $matches Images matching the regex.
|
||||
* @return string Modified HTML content
|
||||
*/
|
||||
function rocket_lazyload_replace_callback( $matches ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\RocketLazyload\Images::lazyloadImages()' );
|
||||
// Don't apply LazyLoad on images from WP Retina x2.
|
||||
if ( function_exists( 'wr2x_picture_rewrite' ) ) {
|
||||
if ( wr2x_get_retina( trailingslashit( ABSPATH ) . wr2x_get_pathinfo_from_image_src( trim( $matches[2], '"' ) ) ) ) {
|
||||
return $matches[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the attributes used to prevent lazylad from being applied
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $excluded_attributes An array of excluded attributes.
|
||||
*/
|
||||
$excluded_attributes = apply_filters(
|
||||
'rocket_lazyload_excluded_attributes', array(
|
||||
'data-src=',
|
||||
'data-no-lazy=',
|
||||
'data-lazy-original=',
|
||||
'data-lazy-src=',
|
||||
'data-lazysrc=',
|
||||
'data-lazyload=',
|
||||
'data-bgposition=',
|
||||
'data-envira-src=',
|
||||
'fullurl=',
|
||||
'lazy-slider-img=',
|
||||
'data-srcset=',
|
||||
'class="ls-l',
|
||||
'class="ls-bg',
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the src used to prevent lazylad from being applied
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $excluded_src An array of excluded src.
|
||||
*/
|
||||
$excluded_src = apply_filters(
|
||||
'rocket_lazyload_excluded_src', array(
|
||||
'/wpcf7_captcha/',
|
||||
'timthumb.php?src',
|
||||
)
|
||||
);
|
||||
|
||||
if ( rocket_is_excluded_lazyload( $matches[1] . $matches[3], $excluded_attributes ) || rocket_is_excluded_lazyload( $matches[2], $excluded_src ) ) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the LazyLoad placeholder on src attribute
|
||||
*
|
||||
* @since 1.1
|
||||
*
|
||||
* @param string $placeholder Placeholder that will be printed.
|
||||
*/
|
||||
$placeholder = apply_filters( 'rocket_lazyload_placeholder', 'data:image/gif;base64,R0lGODdhAQABAPAAAP///wAAACwAAAAAAQABAEACAkQBADs=' );
|
||||
|
||||
$html = sprintf( '<img%1$s src="%4$s" data-lazy-src=%2$s%3$s>', $matches[1], $matches[2], $matches[3], $placeholder );
|
||||
|
||||
$html_noscript = sprintf( '<noscript><img%1$s src=%2$s%3$s></noscript>', $matches[1], $matches[2], $matches[3] );
|
||||
|
||||
/**
|
||||
* Filter the LazyLoad HTML output
|
||||
*
|
||||
* @since 1.0.2
|
||||
*
|
||||
* @param array $html Output that will be printed
|
||||
*/
|
||||
$html = apply_filters( 'rocket_lazyload_html', $html, true );
|
||||
|
||||
return $html . $html_noscript;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_is_excluded_lazyload' ) ) :
|
||||
/**
|
||||
* Determine if the current image should be excluded from lazyload
|
||||
*
|
||||
* @deprecated 3.3
|
||||
* @since 1.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $string String to search.
|
||||
* @param array $excluded_values Array of excluded values to search in the string.
|
||||
* @return bool True if one of the excluded values was found, false otherwise
|
||||
*/
|
||||
function rocket_is_excluded_lazyload( $string, $excluded_values ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\RocketLazyload\Images::isExcluded()' );
|
||||
foreach ( $excluded_values as $excluded_value ) {
|
||||
if ( strpos( $string, $excluded_value ) !== false ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_lazyload_smilies' ) ) :
|
||||
/**
|
||||
* Replace WordPress smilies by Lazy Load
|
||||
*
|
||||
* @since 3.3
|
||||
* @since 2.0 New system for replace smilies by Lazy Load
|
||||
* @since 1.3.5 It's possible to exclude LazyLoad process by used do_rocket_lazyload filter
|
||||
* @since 1.1.0 Don't lazy-load if the thumbnail has already been run through previously
|
||||
* @since 1.0.1 Add priority of hooks at maximum later with PHP_INT_MAX
|
||||
* @since 1.0
|
||||
*/
|
||||
function rocket_lazyload_smilies() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\WP_Rocket\Subscriber\Optimization\Lazyload_Subscriber::lazyload_smilies()' );
|
||||
if ( ! get_rocket_option( 'lazyload' ) || ! apply_filters( 'do_rocket_lazyload', true, 'smilies' ) || ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) || ( defined( 'DONOTLAZYLOAD' ) && DONOTLAZYLOAD ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
remove_filter( 'the_content', 'convert_smilies' );
|
||||
remove_filter( 'the_excerpt', 'convert_smilies' );
|
||||
remove_filter( 'comment_text', 'convert_smilies', 20 );
|
||||
|
||||
add_filter( 'the_content', 'rocket_convert_smilies' );
|
||||
add_filter( 'the_excerpt', 'rocket_convert_smilies' );
|
||||
add_filter( 'comment_text', 'rocket_convert_smilies', 20 );
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_convert_smilies' ) ) :
|
||||
/**
|
||||
* Convert text equivalent of smilies to images.
|
||||
*
|
||||
* @source convert_smilies() in /wp-includes/formattings.php
|
||||
* @since 2.0
|
||||
* @deprecated 3.3
|
||||
* @param string $text Text to process.
|
||||
* @return string Modified text
|
||||
*/
|
||||
function rocket_convert_smilies( $text ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\RocketLazyload\Image::convertSmilies()' );
|
||||
global $wp_smiliessearch;
|
||||
|
||||
if ( ! get_option( 'use_smilies' ) || empty( $wp_smiliessearch ) ) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
$output = '';
|
||||
// HTML loop taken from texturize function, could possible be consolidated.
|
||||
$textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between.
|
||||
$stop = count( $textarr );// loop stuff.
|
||||
|
||||
// Ignore proessing of specific tags.
|
||||
$tags_to_ignore = 'code|pre|style|script|textarea';
|
||||
$ignore_block_element = '';
|
||||
|
||||
for ( $i = 0; $i < $stop; $i++ ) {
|
||||
$content = $textarr[ $i ];
|
||||
|
||||
// If we're in an ignore block, wait until we find its closing tag.
|
||||
if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) {
|
||||
$ignore_block_element = $matches[1];
|
||||
}
|
||||
|
||||
// If it's not a tag and not in ignore block.
|
||||
if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] ) {
|
||||
$content = preg_replace_callback( $wp_smiliessearch, 'rocket_translate_smiley', $content );
|
||||
}
|
||||
|
||||
// did we exit ignore block.
|
||||
if ( '' !== $ignore_block_element && '</' . $ignore_block_element . '>' === $content ) {
|
||||
$ignore_block_element = '';
|
||||
}
|
||||
|
||||
$output .= $content;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_translate_smiley' ) ) :
|
||||
/**
|
||||
* Convert one smiley code to the icon graphic file equivalent.
|
||||
*
|
||||
* @source translate_smiley() in /wp-includes/formattings.php
|
||||
* @since 2.0
|
||||
* @deprecated 3.3
|
||||
*
|
||||
* @param array $matches An array of matching content.
|
||||
* @return string HTML code for smiley
|
||||
*/
|
||||
function rocket_translate_smiley( $matches ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\RocketLazyload\Image::translateSmiley()' );
|
||||
global $wpsmiliestrans;
|
||||
|
||||
if ( count( $matches ) === 0 ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$smiley = trim( reset( $matches ) );
|
||||
$img = $wpsmiliestrans[ $smiley ];
|
||||
|
||||
$matches = array();
|
||||
$ext = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false;
|
||||
$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
|
||||
|
||||
// Don't convert smilies that aren't images - they're probably emoji.
|
||||
if ( ! in_array( $ext, $image_exts, true ) ) {
|
||||
return $img;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the Smiley image URL before it's used in the image element.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
* @param string $smiley_url URL for the smiley image.
|
||||
* @param string $img Filename for the smiley image.
|
||||
* @param string $site_url Site URL, as returned by site_url().
|
||||
*/
|
||||
$src_url = apply_filters( 'smilies_src', includes_url( "images/smilies/$img" ), $img, site_url() );
|
||||
|
||||
// Don't LazyLoad if process is stopped for these reasons.
|
||||
if ( is_feed() || is_preview() ) {
|
||||
return sprintf( ' <img src="%s" alt="%s" class="wp-smiley" /> ', esc_url( $src_url ), esc_attr( $smiley ) );
|
||||
}
|
||||
|
||||
/** This filter is documented in inc/front/lazyload.php */
|
||||
$placeholder = apply_filters( 'rocket_lazyload_placeholder', 'data:image/gif;base64,R0lGODdhAQABAPAAAP///wAAACwAAAAAAQABAEACAkQBADs=' );
|
||||
|
||||
return sprintf( ' <img src="%s" data-lazy-src="%s" alt="%s" class="wp-smiley" /> ', $placeholder, esc_url( $src_url ), esc_attr( $smiley ) );
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_lazyload_iframes' ) ) :
|
||||
/**
|
||||
* Replace iframes by LazyLoad
|
||||
*
|
||||
* @deprecated 3.3
|
||||
* @since 2.6
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string Modified HTML content
|
||||
*/
|
||||
function rocket_lazyload_iframes( $html ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\RocketLazyload\Iframe::lazyloadIframes()' );
|
||||
// Don't LazyLoad if process is stopped for these reasons.
|
||||
if ( ! get_rocket_option( 'lazyload_iframes' ) || ! apply_filters( 'do_rocket_lazyload_iframes', true ) || is_feed() || is_preview() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || empty( $html ) || ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) || ( defined( 'DONOTLAZYLOAD' ) && DONOTLAZYLOAD ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
preg_match_all( '@<iframe(?<atts>\s.+)>.*</iframe>@iUs', $html, $matches, PREG_SET_ORDER );
|
||||
|
||||
if ( empty( $matches ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
foreach ( $matches as $iframe ) {
|
||||
// Don't mess with the Gravity Forms ajax iframe.
|
||||
if ( strpos( $iframe[0], 'gform_ajax_frame' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't lazyload if iframe has data-no-lazy attribute.
|
||||
if ( strpos( $iframe[0], 'data-no-lazy=' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't lazyload if iframe is google recaptcha fallback.
|
||||
if ( strpos( $iframe[0], 'recaptcha/api/fallback' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Given the previous regex pattern, $iframe['atts'] starts with a whitespace character.
|
||||
if ( ! preg_match( '@\ssrc\s*=\s*(\'|")(?<src>.*)\1@iUs', $iframe['atts'], $atts ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$iframe['src'] = trim( $atts['src'] );
|
||||
|
||||
if ( '' === $iframe['src'] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( get_rocket_option( 'lazyload_youtube' ) ) {
|
||||
$youtube_id = rocket_lazyload_get_youtube_id_from_url( $iframe['src'] );
|
||||
|
||||
if ( $youtube_id ) {
|
||||
$query = wp_parse_url( htmlspecialchars_decode( $iframe['src'] ), PHP_URL_QUERY );
|
||||
|
||||
/**
|
||||
* Filter the LazyLoad HTML output on Youtube iframes
|
||||
*
|
||||
* @since 2.11
|
||||
*
|
||||
* @param array $html Output that will be printed.
|
||||
*/
|
||||
$youtube_lazyload = apply_filters( 'rocket_lazyload_youtube_html', '<div class="rll-youtube-player" data-id="' . esc_attr( $youtube_id ) . '" data-query="' . esc_attr( $query ) . '"></div>' );
|
||||
$youtube_lazyload .= '<noscript>' . $iframe[0] . '</noscript>';
|
||||
|
||||
$html = str_replace( $iframe[0], $youtube_lazyload, $html );
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the LazyLoad placeholder on src attribute
|
||||
*
|
||||
* @since 2.11
|
||||
*
|
||||
* @param string $placeholder placeholder that will be printed.
|
||||
*/
|
||||
$placeholder = apply_filters( 'rocket_lazyload_placeholder', 'about:blank' );
|
||||
|
||||
$placeholder_atts = str_replace( $iframe['src'], $placeholder, $iframe['atts'] );
|
||||
$iframe_lazyload = str_replace( $iframe['atts'], $placeholder_atts . ' data-rocket-lazyload="fitvidscompatible" data-lazy-src="' . esc_url( $iframe['src'] ) . '"', $iframe[0] );
|
||||
|
||||
/**
|
||||
* Filter the LazyLoad HTML output on iframes
|
||||
*
|
||||
* @since 2.11
|
||||
*
|
||||
* @param array $html Output that will be printed.
|
||||
*/
|
||||
$iframe_lazyload = apply_filters( 'rocket_lazyload_iframe_html', $iframe_lazyload );
|
||||
$iframe_lazyload .= '<noscript>' . $iframe[0] . '</noscript>';
|
||||
|
||||
$html = str_replace( $iframe[0], $iframe_lazyload, $html );
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_deactivate_lazyload_on_specific_posts' ) ) :
|
||||
/**
|
||||
* Check if we need to exclude LazyLoad on specific posts
|
||||
*
|
||||
* @since 3.3
|
||||
* @since 2.5
|
||||
*/
|
||||
function rocket_deactivate_lazyload_on_specific_posts() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\WP_Rocket\Subscriber\Optimization\Lazyload_Subscriber::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' );
|
||||
}
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_lazyload_on_srcset' ) ) :
|
||||
/**
|
||||
* Compatibility with images with srcset attribute
|
||||
*
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @since 3.3
|
||||
* @since 2.8 Also add sizes to the data-lazy-* attributes to prevent error in W3C validator
|
||||
* @since 2.7
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string Modified HTML content
|
||||
*/
|
||||
function rocket_lazyload_on_srcset( $html ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\RocketLazyload\Image:lazyloadResponsiveAttributes()' );
|
||||
if ( preg_match( '/srcset=("(?:[^"]+)"|\'(?:[^\']+)\'|(?:[^ >]+))/i', $html ) ) {
|
||||
$html = str_replace( 'srcset=', 'data-lazy-srcset=', $html );
|
||||
}
|
||||
|
||||
if ( preg_match( '/sizes=("(?:[^"]+)"|\'(?:[^\']+)\'|(?:[^ >]+))/i', $html ) ) {
|
||||
$html = str_replace( 'sizes=', 'data-lazy-sizes=', $html );
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_lazyload_get_youtube_id_from_url' ) ) :
|
||||
/**
|
||||
* Gets youtube video ID from URL
|
||||
*
|
||||
* @author Remy Perona
|
||||
* @deprecated 3.3
|
||||
* @since 2.11
|
||||
*
|
||||
* @param string $url URL to parse.
|
||||
* @return string Youtube video id or false if none found.
|
||||
*/
|
||||
function rocket_lazyload_get_youtube_id_from_url( $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3', '\RocketLazyload\Iframe:getYoutubeIDFromURL()' );
|
||||
$pattern = '#^(?:https?:)?(?://)?(?:www\.)?(?:youtu\.be|youtube\.com|youtube-nocookie\.com)/(?:embed/|v/|watch/?\?v=)([\w-]{11})#iU';
|
||||
$result = preg_match( $pattern, $url, $matches );
|
||||
|
||||
if ( ! $result ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( 'videoseries' === $matches[1] ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $matches[1];
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_user_agent' ) ) :
|
||||
/**
|
||||
* Add Rocket informations into USER_AGENT
|
||||
*
|
||||
* @since 1.1.0
|
||||
* @deprecated 3.3.6
|
||||
*
|
||||
* @param string $user_agent User Agent value.
|
||||
* @return string WP Rocket user agent
|
||||
*/
|
||||
function rocket_user_agent( $user_agent ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3.6', '\WP_Rocket\Subscriber\Plugin\Updater_Api_Common_Subscriber->get_rocket_user_agent()' );
|
||||
|
||||
$consumer_key = '';
|
||||
if ( isset( $_POST[ WP_ROCKET_SLUG ]['consumer_key'] ) ) {
|
||||
$consumer_key = $_POST[ WP_ROCKET_SLUG ]['consumer_key'];
|
||||
} elseif ( '' !== (string) get_rocket_option( 'consumer_key' ) ) {
|
||||
$consumer_key = (string) get_rocket_option( 'consumer_key' );
|
||||
}
|
||||
|
||||
$consumer_email = '';
|
||||
if ( isset( $_POST[ WP_ROCKET_SLUG ]['consumer_email'] ) ) {
|
||||
$consumer_email = $_POST[ WP_ROCKET_SLUG ]['consumer_email'];
|
||||
} elseif ( '' !== (string) get_rocket_option( 'consumer_email' ) ) {
|
||||
$consumer_email = (string) get_rocket_option( 'consumer_email' );
|
||||
}
|
||||
|
||||
$bonus = ! get_rocket_option( 'do_beta' ) ? '' : '+';
|
||||
$php_version = preg_replace( '@^(\d\.\d+).*@', '\1', phpversion() );
|
||||
$new_ua = sprintf( '%s;WP-Rocket|%s%s|%s|%s|%s|%s;', $user_agent, WP_ROCKET_VERSION, $bonus, $consumer_key, $consumer_email, esc_url( home_url() ), $php_version );
|
||||
|
||||
return $new_ua;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_add_own_ua' ) ) :
|
||||
/**
|
||||
* Force our user agent header when we hit our urls
|
||||
*
|
||||
* @since 2.4
|
||||
* @deprecated 3.3.6
|
||||
*
|
||||
* @param array $request An array of request arguments.
|
||||
* @param string $url Requested URL.
|
||||
* @return array An array of requested arguments
|
||||
*/
|
||||
function rocket_add_own_ua( $request, $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3.6', '\WP_Rocket\Subscriber\Plugin\Updater_Api_Common_Subscriber->maybe_set_rocket_user_agent()' );
|
||||
|
||||
if ( ! is_string( $url ) ) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
if ( strpos( $url, 'wp-rocket.me' ) !== false ) {
|
||||
$request['user-agent'] = rocket_user_agent( $request['user-agent'] );
|
||||
}
|
||||
return $request;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_updates_exclude' ) ) :
|
||||
/**
|
||||
* Excludes WP Rocket from WP updates
|
||||
*
|
||||
* @since 1.0
|
||||
* @deprecated 3.3.6
|
||||
*
|
||||
* @param array $request An array of HTTP request arguments.
|
||||
* @param string $url The request URL.
|
||||
* @return array Updated array of HTTP request arguments.
|
||||
*/
|
||||
function rocket_updates_exclude( $request, $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3.6', '\WP_Rocket\Subscriber\Plugin\Updater_Subscriber->exclude_rocket_from_wp_updates()' );
|
||||
|
||||
if ( ! is_string( $url ) ) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
if ( 0 !== strpos( $url, 'http://api.wordpress.org/plugins/update-check' ) || ! isset( $request['body']['plugins'] ) ) {
|
||||
return $request; // Not a plugin update request. Stop immediately.
|
||||
}
|
||||
|
||||
$plugins = maybe_unserialize( $request['body']['plugins'] );
|
||||
|
||||
if ( isset( $plugins->plugins[ plugin_basename( WP_ROCKET_FILE ) ], $plugins->active[ array_search( plugin_basename( WP_ROCKET_FILE ), $plugins->active, true ) ] ) ) {
|
||||
unset( $plugins->plugins[ plugin_basename( WP_ROCKET_FILE ) ] );
|
||||
unset( $plugins->active[ array_search( plugin_basename( WP_ROCKET_FILE ), $plugins->active, true ) ] );
|
||||
}
|
||||
|
||||
$request['body']['plugins'] = maybe_serialize( $plugins );
|
||||
return $request;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_force_info' ) ) :
|
||||
/**
|
||||
* Hack the returned object
|
||||
*
|
||||
* @since 1.0
|
||||
* @deprecated 3.3.6
|
||||
*
|
||||
* @param false|object|array $bool The result object or array. Default false.
|
||||
* @param string $action The type of information being requested from the Plugin Install API.
|
||||
* @param object $args Plugin API arguments.
|
||||
* @return false|object|array Empty object if slug is WP Rocket, default value otherwise
|
||||
*/
|
||||
function rocket_force_info( $bool, $action, $args ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3.6', '\WP_Rocket\Subscriber\Plugin\Information_Subscriber->exclude_rocket_from_wp_info()' );
|
||||
|
||||
if ( 'plugin_information' === $action && 'wp-rocket' === $args->slug ) {
|
||||
return new stdClass();
|
||||
}
|
||||
return $bool;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_force_info_result' ) ) :
|
||||
/**
|
||||
* Hack the returned result with our content
|
||||
*
|
||||
* @since 1.0
|
||||
* @deprecated 3.3.6
|
||||
*
|
||||
* @param object|WP_Error $res Response object or WP_Error.
|
||||
* @param string $action The type of information being requested from the Plugin Install API.
|
||||
* @param object $args Plugin API arguments.
|
||||
* @return object|WP_Error Updated response object or WP_Error
|
||||
*/
|
||||
function rocket_force_info_result( $res, $action, $args ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3.6', '\WP_Rocket\Subscriber\Plugin\Information_Subscriber->add_rocket_info()' );
|
||||
|
||||
if ( 'plugin_information' === $action && isset( $args->slug ) && 'wp-rocket' === $args->slug && isset( $res->external ) && $res->external ) {
|
||||
|
||||
$request = wp_remote_post(
|
||||
WP_ROCKET_WEB_INFO, array(
|
||||
'timeout' => 30,
|
||||
'action' => 'plugin_information',
|
||||
'request' => serialize( $args ),
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_wp_error( $request ) ) {
|
||||
// translators: %s is an URL.
|
||||
$res = new WP_Error( 'plugins_api_failed', sprintf( __( 'An unexpected error occurred. Something may be wrong with WP-Rocket.me or this server’s configuration. If you continue to have problems, <a href="%s">contact support</a>.', 'rocket' ), rocket_get_external_url( 'support', array(
|
||||
'utm_source' => 'wp_plugin',
|
||||
'utm_medium' => 'wp_rocket',
|
||||
) ) ), $request->get_error_message() );
|
||||
} else {
|
||||
$res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
|
||||
|
||||
if ( ! is_object( $res ) && ! is_array( $res ) ) {
|
||||
// translators: %s is an URL.
|
||||
$res = new WP_Error( 'plugins_api_failed', sprintf( __( 'An unexpected error occurred. Something may be wrong with WP-Rocket.me or this server’s configuration. If you continue to have problems, <a href="%s">contact support</a>.', 'rocket' ), rocket_get_external_url( 'support', array(
|
||||
'utm_source' => 'wp_plugin',
|
||||
'utm_medium' => 'wp_rocket',
|
||||
) ) ), wp_remote_retrieve_body( $request ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_check_update' ) ) :
|
||||
/**
|
||||
* When WP sets the update_plugins site transient, we set our own transient, then see rocket_add_response_to_updates
|
||||
*
|
||||
* @since 2.6.5
|
||||
* @deprecated 3.3.6
|
||||
*
|
||||
* @param Object $value Site transient object.
|
||||
*/
|
||||
function rocket_check_update( $value ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3.6', '\WP_Rocket\Subscriber\Plugin\Updater_Subscriber->maybe_add_rocket_update_data()' );
|
||||
|
||||
$timer_update_wprocket = (int) get_site_transient( 'update_wprocket' );
|
||||
$temp_object = get_site_transient( 'update_wprocket_response' );
|
||||
if ( ( ! isset( $_GET['rocket_force_update'] ) || defined( 'WP_INSTALLING' ) ) &&
|
||||
( 12 * HOUR_IN_SECONDS ) > ( time() - $timer_update_wprocket ) // retry in 12 hours.
|
||||
) {
|
||||
if ( is_object( $value ) && false !== $temp_object ) {
|
||||
if ( version_compare( $temp_object->new_version, WP_ROCKET_VERSION ) > 0 ) {
|
||||
$value->response[ $temp_object->plugin ] = $temp_object;
|
||||
} else {
|
||||
delete_site_transient( 'update_wprocket_response' );
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ( isset( $_GET['rocket_force_update'] ) ) {
|
||||
$_SERVER['REQUEST_URI'] = remove_query_arg( 'rocket_force_update' );
|
||||
}
|
||||
|
||||
$plugin_folder = plugin_basename( dirname( WP_ROCKET_FILE ) );
|
||||
$plugin_file = basename( WP_ROCKET_FILE );
|
||||
$version = true;
|
||||
if ( ! $value ) {
|
||||
$value = new stdClass();
|
||||
$value->last_checked = time();
|
||||
}
|
||||
|
||||
$response = wp_remote_get(
|
||||
WP_ROCKET_WEB_CHECK, array(
|
||||
'timeout' => 30,
|
||||
)
|
||||
);
|
||||
if ( ! is_a( $response, 'WP_Error' ) && 200 === $response['response']['code'] && strlen( $response['body'] ) > 32 ) {
|
||||
|
||||
set_site_transient( 'update_wprocket', time() );
|
||||
|
||||
list( $version, $url ) = explode( '|', $response['body'] );
|
||||
if ( version_compare( $version, WP_ROCKET_VERSION ) <= 0 ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$temp_array = array(
|
||||
'slug' => $plugin_folder,
|
||||
'plugin' => $plugin_folder . '/' . $plugin_file,
|
||||
'new_version' => $version,
|
||||
'url' => 'https://wp-rocket.me',
|
||||
'package' => $url,
|
||||
);
|
||||
|
||||
$temp_object = (object) $temp_array;
|
||||
$value->response[ $plugin_folder . '/' . $plugin_file ] = $temp_object;
|
||||
|
||||
set_site_transient( 'update_wprocket_response', $temp_object );
|
||||
} else {
|
||||
set_site_transient( 'update_wprocket', ( time() + ( 11 * HOUR_IN_SECONDS ) ) ); // retry in 1 hour in case of error..
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
endif;
|
||||
|
||||
if ( ! function_exists( 'rocket_reset_check_update_timer' ) ) :
|
||||
/**
|
||||
* When WP deletes the update_plugins site transient or updates the plugins, we delete our own transients to avoid another 12 hours waiting
|
||||
*
|
||||
* @since 2.6.8
|
||||
* @deprecated 3.3.6
|
||||
*
|
||||
* @param string $transient Transient name.
|
||||
* @param object $value Transient object.
|
||||
*/
|
||||
function rocket_reset_check_update_timer( $transient = 'update_plugins', $value = null ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.3.6' );
|
||||
|
||||
// $value used by setted.
|
||||
if ( 'update_plugins' === $transient ) {
|
||||
if ( is_null( $value ) || is_object( $value ) && ! isset( $value->response ) ) {
|
||||
delete_site_transient( 'update_wprocket' );
|
||||
}
|
||||
}
|
||||
}
|
||||
endif;
|
||||
@@ -0,0 +1,719 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Get Zones linked to a Cloudflare account
|
||||
*
|
||||
* @since 2.9
|
||||
* @deprecated 3.4.1.2
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return Array List of zones or default no domain
|
||||
*/
|
||||
function get_rocket_cloudflare_zones() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4.1.2' );
|
||||
$cf_api_instance = get_rocket_cloudflare_api_instance();
|
||||
$domains = array(
|
||||
'' => __( 'Choose a domain from the list', 'rocket' ),
|
||||
);
|
||||
|
||||
if ( is_wp_error( $cf_api_instance ) ) {
|
||||
return $domains;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_zone_instance = new Cloudflare\Zone( $cf_api_instance );
|
||||
$cf_zones = $cf_zone_instance->zones( null, 'active', null, 50 );
|
||||
$cf_zones_list = $cf_zones->result;
|
||||
|
||||
if ( ! (bool) $cf_zones_list ) {
|
||||
$domains[] = __( 'No domain available in your Cloudflare account', 'rocket' );
|
||||
|
||||
return $domains;
|
||||
}
|
||||
|
||||
foreach ( $cf_zones_list as $cf_zone ) {
|
||||
$domains[ $cf_zone->name ] = $cf_zone->name;
|
||||
}
|
||||
|
||||
return $domains;
|
||||
} catch ( Exception $e ) {
|
||||
return $domains;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get CNAMES hosts
|
||||
*
|
||||
* @since 2.3
|
||||
* @deprecated 3.4
|
||||
*
|
||||
* @param string $zones CNAMES zones.
|
||||
* @return array $hosts CNAMES hosts
|
||||
*/
|
||||
function get_rocket_cnames_host( $zones = array( 'all' ) ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4', '\WP_Rocket\Subscriber\CDN\CDNSubscriber::get_cdn_hosts()' );
|
||||
$hosts = array();
|
||||
|
||||
$cnames = get_rocket_cdn_cnames( $zones );
|
||||
if ( $cnames ) {
|
||||
foreach ( $cnames as $cname ) {
|
||||
$cname = rocket_add_url_protocol( $cname );
|
||||
$hosts[] = rocket_extract_url_component( $cname, PHP_URL_HOST );
|
||||
}
|
||||
}
|
||||
|
||||
return $hosts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply CDN on CSS properties (background, background-image, @import, src:url (fonts))
|
||||
*
|
||||
* @since 2.6
|
||||
* @since 3.4
|
||||
*
|
||||
* @param string $buffer file content.
|
||||
* @return string modified file content
|
||||
*/
|
||||
function rocket_cdn_css_properties( $buffer ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4', '\WP_Rocket\Subscriber\CDN\CDN::rewrite_css_properties()' );
|
||||
|
||||
$zone = array(
|
||||
'all',
|
||||
'images',
|
||||
'css_and_js',
|
||||
'css',
|
||||
);
|
||||
$cnames = get_rocket_cdn_cnames( $zone );
|
||||
|
||||
/**
|
||||
* Filters the application of the CDN on CSS properties
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param bool true to apply CDN to properties, false otherwise
|
||||
*/
|
||||
$do_rocket_cdn_css_properties = apply_filters( 'do_rocket_cdn_css_properties', true );
|
||||
|
||||
if ( ! get_rocket_option( 'cdn' ) || ! $cnames || ! $do_rocket_cdn_css_properties ) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
preg_match_all( '/url\((?![\'"]?data)([^\)]+)\)/i', $buffer, $matches );
|
||||
|
||||
if ( is_array( $matches ) ) {
|
||||
$i = 0;
|
||||
foreach ( $matches[1] as $url ) {
|
||||
$url = trim( $url, " \t\n\r\0\x0B\"'" );
|
||||
/**
|
||||
* Filters the URL of the CSS property
|
||||
*
|
||||
* @since 2.8
|
||||
*
|
||||
* @param string $url URL of the CSS property
|
||||
*/
|
||||
$url = get_rocket_cdn_url( apply_filters( 'rocket_cdn_css_properties_url', $url ), $zone );
|
||||
$property = str_replace( $matches[1][ $i ], $url, $matches[0][ $i ] );
|
||||
$buffer = str_replace( $matches[0][ $i ], $property, $buffer );
|
||||
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply CDN on custom data attributes.
|
||||
*
|
||||
* @since 2.5.5
|
||||
* @deprecated 3.4
|
||||
*
|
||||
* @param string $html Original Output.
|
||||
* @return string $html Output that will be printed
|
||||
*/
|
||||
function rocket_add_cdn_on_custom_attr( $html ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
if ( preg_match( '/(data-lazy-src|data-lazyload|data-src|data-retina)=[\'"]?([^\'"\s>]+)[\'"]/i', $html, $matches ) ) {
|
||||
$html = str_replace( $matches[2], get_rocket_cdn_url( $matches[2], array( 'all', 'images' ) ), $html );
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Replace URL by CDN of all thumbnails and smilies.
|
||||
*
|
||||
* @since 2.1
|
||||
* @deprecated 3.4
|
||||
*
|
||||
* @param string $url URL of the file to replace the domain with the CDN.
|
||||
* @return string modified URL
|
||||
*/
|
||||
function rocket_cdn_file( $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
if ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$ext = pathinfo( $url, PATHINFO_EXTENSION );
|
||||
|
||||
if ( is_admin() || 'php' === $ext ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$filter = current_filter();
|
||||
|
||||
$rejected_files = get_rocket_cdn_reject_files();
|
||||
if ( 'template_directory_uri' === $filter && ! empty( $rejected_files ) ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
switch ( $filter ) {
|
||||
case 'wp_get_attachment_url':
|
||||
case 'wp_calculate_image_srcset':
|
||||
$zone = array( 'all', 'images' );
|
||||
break;
|
||||
case 'smilies_src':
|
||||
$zone = array( 'all', 'images' );
|
||||
break;
|
||||
case 'stylesheet_uri':
|
||||
case 'wp_minify_css_url':
|
||||
case 'wp_minify_js_url':
|
||||
case 'bwp_get_minify_src':
|
||||
$zone = array( 'all', 'css_and_js', $ext );
|
||||
break;
|
||||
default:
|
||||
$zone = array( 'all', $ext );
|
||||
break;
|
||||
}
|
||||
|
||||
$cnames = get_rocket_cdn_cnames( $zone );
|
||||
if ( $cnames ) {
|
||||
$url = get_rocket_cdn_url( $url, $zone );
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace URL by CDN of images displayed using wp_get_attachment_image_src
|
||||
*
|
||||
* @since 2.9.2
|
||||
* @deprecated 3.4
|
||||
* @author Remy Perona
|
||||
* @source https://github.com/wp-media/wp-rocket/issues/271#issuecomment-269849927
|
||||
*
|
||||
* @param array $image An array containing the src, width and height of the image.
|
||||
* @return array Array with updated src URL
|
||||
*/
|
||||
function rocket_cdn_attachment_image_src( $image ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
if ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
if ( ! (bool) $image ) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
if ( is_admin() || is_preview() || is_feed() ) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
$zones = array( 'all', 'images' );
|
||||
|
||||
if ( ! (bool) get_rocket_cdn_cnames( $zones ) ) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
$image[0] = get_rocket_cdn_url( $image[0], $zones );
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace srcset URLs by CDN URLs for WP responsive images
|
||||
*
|
||||
* @since WP 4.4
|
||||
* @since 2.6.14
|
||||
* @deprecated 3.4
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $sources multidimensional array containing srcset images urls.
|
||||
* @return array $sources
|
||||
*/
|
||||
function rocket_add_cdn_on_srcset( $sources ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
if ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
|
||||
return $sources;
|
||||
}
|
||||
|
||||
if ( (bool) $sources ) {
|
||||
foreach ( $sources as $width => $data ) {
|
||||
$sources[ $width ]['url'] = rocket_cdn_file( $data['url'] );
|
||||
}
|
||||
}
|
||||
return $sources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace URL by CDN of all images display in a post content or a widget text.
|
||||
*
|
||||
* @since 2.1
|
||||
* @deprecated 3.4
|
||||
*
|
||||
* @param string $html HTML content to parse.
|
||||
* @return string modified HTML content
|
||||
*/
|
||||
function rocket_cdn_images( $html ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
// Don't use CDN if the image is in admin, a feed or in a post preview.
|
||||
if ( is_admin() || is_feed() || is_preview() || empty( $html ) || defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$zone = array( 'all', 'images' );
|
||||
$cnames = get_rocket_cdn_cnames( $zone );
|
||||
|
||||
if ( $cnames ) {
|
||||
|
||||
$cnames = array_flip( $cnames );
|
||||
$wp_content_dirname = wp_parse_url( content_url(), PHP_URL_PATH );
|
||||
|
||||
$custom_media_uploads_dirname = '';
|
||||
$uploads_info = wp_upload_dir();
|
||||
|
||||
if ( ! empty( $uploads_info['baseurl'] ) ) {
|
||||
$custom_media_uploads_dirname = '|' . trailingslashit( wp_parse_url( $uploads_info['baseurl'], PHP_URL_PATH ) );
|
||||
}
|
||||
|
||||
// Get all images of the content.
|
||||
preg_match_all( '#<img([^>]+?)src=([\'"\\\]*)([^\'"\s\\\>]+)([\'"\\\]*)([^>]*)>#i', $html, $images_match );
|
||||
|
||||
foreach ( $images_match[3] as $k => $image_url ) {
|
||||
|
||||
$parse_url = get_rocket_parse_url( $image_url );
|
||||
$path = trim( $parse_url['path'] );
|
||||
$host = $parse_url['host'];
|
||||
|
||||
if ( empty( $path ) || ! preg_match( '#(' . $wp_content_dirname . $custom_media_uploads_dirname . '|wp-includes)#', $path ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( isset( $cnames[ $host ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Image path is relative, apply the host to it.
|
||||
if ( empty( $host ) ) {
|
||||
$image_url = home_url( '/' ) . ltrim( $image_url, '/' );
|
||||
$host = rocket_extract_url_component( $image_url, PHP_URL_HOST );
|
||||
}
|
||||
|
||||
// Check if the link isn't external.
|
||||
if ( rocket_extract_url_component( home_url(), PHP_URL_HOST ) !== $host ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if the URL isn't a DATA-URI.
|
||||
if ( false !== strpos( $image_url, 'data:image' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$html = str_replace(
|
||||
$images_match[0][ $k ],
|
||||
/**
|
||||
* Filter the image HTML output with the CDN link
|
||||
*
|
||||
* @since 2.5.5
|
||||
*
|
||||
* @param array $html Output that will be printed.
|
||||
*/
|
||||
apply_filters(
|
||||
'rocket_cdn_images_html',
|
||||
sprintf(
|
||||
'<img %1$s %2$s %3$s>',
|
||||
trim( $images_match[1][ $k ] ),
|
||||
'src=' . $images_match[2][ $k ] . get_rocket_cdn_url( $image_url, $zone ) . $images_match[4][ $k ],
|
||||
trim( $images_match[5][ $k ] )
|
||||
)
|
||||
),
|
||||
$html
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace URL by CDN of all inline styles containing url()
|
||||
*
|
||||
* @since 2.9
|
||||
* @deprecated 3.4
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $html HTML content of the page.
|
||||
* @return string modified HTML content
|
||||
*/
|
||||
function rocket_cdn_inline_styles( $html ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
if ( is_preview() || empty( $html ) || defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$zone = array(
|
||||
'all',
|
||||
'images',
|
||||
);
|
||||
|
||||
$cnames = get_rocket_cdn_cnames( $zone );
|
||||
if ( $cnames ) {
|
||||
preg_match_all( '/url\((?![\'\"]?data)[\"\']?([^\)\"\']+)[\"\']?\)/i', $html, $matches );
|
||||
|
||||
if ( (bool) $matches ) {
|
||||
foreach ( $matches[1] as $k => $url ) {
|
||||
$url = str_replace( array( ' ', '\t', '\n', '\r', '\0', '\x0B', '"', "'", '"', ''' ), '', $url );
|
||||
|
||||
if ( '#' === substr( $url, 0, 1 ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$url = get_rocket_cdn_url( $url, $zone );
|
||||
$property = str_replace( $matches[1][ $k ], $url, $matches[0][ $k ] );
|
||||
$html = str_replace( $matches[0][ $k ], $property, $html );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace URL by CDN for custom files
|
||||
*
|
||||
* @since 2.9
|
||||
* @deprecated 3.4
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $html HTML content of the page.
|
||||
* @return string modified HTML content
|
||||
*/
|
||||
function rocket_cdn_custom_files( $html ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
if ( is_preview() || empty( $html ) || defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$image_types = [
|
||||
'jpg',
|
||||
'jpeg',
|
||||
'jpe',
|
||||
'png',
|
||||
'gif',
|
||||
'webp',
|
||||
'bmp',
|
||||
'tiff',
|
||||
];
|
||||
|
||||
$other_types = [
|
||||
'mp3',
|
||||
'ogg',
|
||||
'mp4',
|
||||
'm4v',
|
||||
'avi',
|
||||
'mov',
|
||||
'flv',
|
||||
'swf',
|
||||
'webm',
|
||||
'pdf',
|
||||
'doc',
|
||||
'docx',
|
||||
'txt',
|
||||
'zip',
|
||||
'tar',
|
||||
'bz2',
|
||||
'tgz',
|
||||
'rar',
|
||||
];
|
||||
|
||||
$zones = array_filter( array_unique( get_rocket_option( 'cdn_zone', [] ) ) );
|
||||
|
||||
if ( empty( $zones ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
if ( ! in_array( 'all', $zones, true ) && ! in_array( 'images', $zones, true ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$cdn_zones = [];
|
||||
$file_types = [];
|
||||
|
||||
if ( in_array( 'images', $zones, true ) ) {
|
||||
$cdn_zones[] = 'images';
|
||||
$file_types = array_merge( $file_types, $image_types );
|
||||
}
|
||||
|
||||
if ( in_array( 'all', $zones, true ) ) {
|
||||
$cdn_zones[] = 'all';
|
||||
$file_types = array_merge( $file_types, $image_types, $other_types );
|
||||
}
|
||||
|
||||
$cnames = get_rocket_cdn_cnames( $cdn_zones );
|
||||
|
||||
if ( empty( $cnames ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the filetypes allowed for the CDN
|
||||
*
|
||||
* @since 2.9
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $filetypes Array of file types.
|
||||
*/
|
||||
$file_types = apply_filters( 'rocket_cdn_custom_filetypes', $file_types );
|
||||
$file_types = implode( '|', $file_types );
|
||||
|
||||
preg_match_all( '#<a[^>]+?href=[\'"]?([^"\'>]+\.(?:' . $file_types . '))[\'"]?[^>]*>#i', $html, $matches );
|
||||
|
||||
if ( ! (bool) $matches ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
foreach ( $matches[1] as $key => $url ) {
|
||||
$url = trim( $url, " \t\n\r\0\x0B\"'" );
|
||||
$url = get_rocket_cdn_url( $url, $cdn_zones );
|
||||
$src = str_replace( $matches[1][ $key ], $url, $matches[0][ $key ] );
|
||||
$html = str_replace( $matches[0][ $key ], $src, $html );
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace URL by CDN of all scripts and styles enqueues with WordPress functions
|
||||
*
|
||||
* @since 2.9 Only add protocol if $src is an absolute url
|
||||
* @since 2.1
|
||||
* @deprecated 3.4
|
||||
*
|
||||
* @param string $src URL of the file.
|
||||
* @return string modified URL
|
||||
*/
|
||||
function rocket_cdn_enqueue( $src ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
// Don't use CDN if in admin, in login page, in register page or in a post preview.
|
||||
if ( is_admin() || is_preview() || in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ), true ) || defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
|
||||
return $src;
|
||||
}
|
||||
|
||||
if ( rocket_extract_url_component( $src, PHP_URL_HOST ) !== '' ) {
|
||||
$src = rocket_add_url_protocol( $src );
|
||||
}
|
||||
|
||||
$zone = array( 'all', 'css_and_js' );
|
||||
|
||||
// Add only CSS zone.
|
||||
if ( 'style_loader_src' === current_filter() ) {
|
||||
$zone[] = 'css';
|
||||
}
|
||||
|
||||
// Add only JS zone.
|
||||
if ( 'script_loader_src' === current_filter() ) {
|
||||
$zone[] = 'js';
|
||||
}
|
||||
|
||||
$cnames = get_rocket_cdn_cnames( $zone );
|
||||
if ( $cnames ) {
|
||||
// Check if the path isn't empty.
|
||||
if ( trim( rocket_extract_url_component( $src, PHP_URL_PATH ), '/' ) !== '' ) {
|
||||
$src = get_rocket_cdn_url( $src, $zone );
|
||||
}
|
||||
}
|
||||
|
||||
return $src;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all files we don't allow to get in CDN.
|
||||
*
|
||||
* @since 2.5
|
||||
* @deprecated 3.4
|
||||
*
|
||||
* @return string A pipe-separated list of rejected files.
|
||||
*/
|
||||
function get_rocket_cdn_reject_files() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4', '\WP_Rocket\Subscriber\CDN\CDN::get_excluded_files()' );
|
||||
|
||||
$files = get_rocket_option( 'cdn_reject_files', [] );
|
||||
|
||||
/**
|
||||
* Filter the rejected files.
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @param array $files List of rejected files.
|
||||
*/
|
||||
$files = (array) apply_filters( 'rocket_cdn_reject_files', $files );
|
||||
$files = array_filter( $files );
|
||||
$files = array_flip( array_flip( $files ) );
|
||||
|
||||
return implode( '|', $files );
|
||||
}
|
||||
|
||||
/**
|
||||
* Conflict with Envira Gallery: changes the URL argument if using WP Rocket CDN and Envira
|
||||
*
|
||||
* @since 2.6.5
|
||||
* @since 3.4
|
||||
*
|
||||
* @param array $args An array of arguments.
|
||||
* @return array Updated array of arguments
|
||||
*/
|
||||
function rocket_cdn_resize_image_args_on_envira_gallery( $args ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
if ( ! isset( $args['url'] ) || (int) get_rocket_option( 'cdn' ) === 0 ) {
|
||||
return $args;
|
||||
}
|
||||
|
||||
$cnames_host = array_flip( get_rocket_cnames_host() );
|
||||
$url_host = rocket_extract_url_component( $args['url'], PHP_URL_HOST );
|
||||
$home_host = rocket_extract_url_component( home_url(), PHP_URL_HOST );
|
||||
|
||||
if ( isset( $cnames_host[ $url_host ] ) ) {
|
||||
$args['url'] = str_replace( $url_host, $home_host , $args['url'] );
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Conflict with Envira Gallery: changes the resized URL if using WP Rocket CDN and Envira
|
||||
*
|
||||
* @since 2.6.5
|
||||
* @since 3.4
|
||||
*
|
||||
* @param string $url Resized image URL.
|
||||
* @return string Resized image URL using the CDN URL
|
||||
*/
|
||||
function rocket_cdn_resized_url_on_envira_gallery( $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
if ( (int) get_rocket_option( 'cdn' ) === 0 ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$url = get_rocket_cdn_url( $url, array( 'all', 'images' ) );
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply CDN settings to Beaver Builder parallax.
|
||||
*
|
||||
* @since 3.2.1
|
||||
* @deprecated 3.4
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $attrs HTML attributes.
|
||||
* @return array
|
||||
*/
|
||||
function rocket_beaver_builder_add_cdn_to_parallax( $attrs ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
|
||||
if ( ! empty( $attrs['data-parallax-image'] ) ) {
|
||||
$attrs['data-parallax-image'] = get_rocket_cdn_url( $attrs['data-parallax-image'], [ 'all', 'images' ] );
|
||||
}
|
||||
|
||||
return $attrs;
|
||||
}
|
||||
|
||||
if ( class_exists( 'WR2X_Admin' ) ) :
|
||||
/**
|
||||
* Conflict with WP Retina x2: Apply CDN on srcset attribute.
|
||||
*
|
||||
* @since 2.9.1 Use global $wr2x_admin
|
||||
* @since 2.5.5
|
||||
* @deprecated 3.4
|
||||
*
|
||||
* @param string $url URL of the image.
|
||||
* @return string Updated URL with CDN
|
||||
*/
|
||||
function rocket_cdn_on_images_from_wp_retina_x2( $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
|
||||
global $wr2x_admin;
|
||||
|
||||
if ( ! method_exists( $wr2x_admin, 'is_pro' ) || ! $wr2x_admin->is_pro() ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$cdn_domain = get_option( 'wr2x_cdn_domain' );
|
||||
|
||||
if ( ! empty( $cdn_domain ) ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
return get_rocket_cdn_url( $url, array( 'all', 'images' ) );
|
||||
}
|
||||
endif;
|
||||
|
||||
/**
|
||||
* Conflict with Avada theme and WP Rocket CDN
|
||||
*
|
||||
* @since 2.6.1
|
||||
* @deprecated 3.4
|
||||
*
|
||||
* @param array $vars An array of variables.
|
||||
* @param string $handle Name of the avada resource.
|
||||
* @return array updated array of variables
|
||||
*/
|
||||
function rocket_fix_cdn_for_avada_theme( $vars, $handle ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
if ( 'avada-dynamic' === $handle && get_rocket_option( 'cdn' ) ) {
|
||||
$src = get_rocket_cdn_url( get_template_directory_uri() . '/assets/less/theme/dynamic.less' );
|
||||
$vars['template-directory'] = sprintf( '~"%s"', dirname( dirname( dirname( dirname( $src ) ) ) ) );
|
||||
$vars['lessurl'] = sprintf( '~"%s"', dirname( $src ) );
|
||||
}
|
||||
return $vars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Conflict with Aqua Resizer & IrishMiss Framework: Apply CDN without blank src!!
|
||||
*
|
||||
* @since 2.5.8 Add compatibility with IrishMiss Framework
|
||||
* @since 2.5.5
|
||||
* @deprecated 3.4
|
||||
*/
|
||||
function rocket_cdn_on_aqua_resizer() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
|
||||
if ( function_exists( 'aq_resize' ) || function_exists( 'miss_display_image' ) ) {
|
||||
remove_filter( 'wp_get_attachment_url' , 'rocket_cdn_file', PHP_INT_MAX );
|
||||
add_filter( 'rocket_lazyload_html', 'rocket_add_cdn_on_custom_attr' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Conflict with Revolution Slider & Master Slider: Apply CDN on data-lazyload|data-src attribute.
|
||||
*
|
||||
* @since 2.5.5
|
||||
* @deprecated 3.4
|
||||
*/
|
||||
function rocket_cdn_on_sliders_with_lazyload() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.4' );
|
||||
|
||||
if ( class_exists( 'RevSliderFront' ) || class_exists( 'Master_Slider' ) ) {
|
||||
add_filter( 'rocket_cdn_images_html', 'rocket_add_cdn_on_custom_attr' );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,901 @@
|
||||
<?php
|
||||
// phpcs:ignoreFile
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class aliases.
|
||||
*/
|
||||
class_alias( '\WP_Rocket\Engine\Admin\Settings\Page', '\WP_Rocket\Admin\Settings\Page' );
|
||||
class_alias( '\WP_Rocket\Engine\Admin\Settings\Render', '\WP_Rocket\Admin\Settings\Render' );
|
||||
class_alias( '\WP_Rocket\Engine\Admin\Settings\Settings', '\WP_Rocket\Admin\Settings\Settings' );
|
||||
class_alias( '\WP_Rocket\Engine\Admin\Settings\ServiceProvider', '\WP_Rocket\ServiceProvider\Settings' );
|
||||
class_alias( '\WP_Rocket\Engine\Admin\Settings\Subscriber', '\WP_Rocket\Subscriber\Admin\Settings\Page_Subscriber' );
|
||||
class_alias( '\WP_Rocket\Engine\Optimization\GoogleFonts\Combine', '\WP_Rocket\Optimization\CSS\Combine_Google_Fonts' );
|
||||
class_alias( '\WP_Rocket\Engine\Optimization\GoogleFonts\Subscriber', '\WP_Rocket\Subscriber\Optimization\Combine_Google_Fonts_Subscriber' );
|
||||
|
||||
//RocketCDN Start
|
||||
class_alias('\WP_Rocket\Engine\CDN\RocketCDN\AdminPageSubscriber', '\WP_Rocket\Subscriber\CDN\RocketCDN\AdminPageSubscriber');
|
||||
class_alias('\WP_Rocket\Engine\CDN\RocketCDN\APIClient', '\WP_Rocket\CDN\RocketCDN\APIClient');
|
||||
class_alias('\WP_Rocket\Engine\CDN\RocketCDN\CDNOptionsManager', '\WP_Rocket\CDN\RocketCDN\CDNOptionsManager');
|
||||
class_alias('\WP_Rocket\Engine\CDN\RocketCDN\DataManagerSubscriber', '\WP_Rocket\Subscriber\CDN\RocketCDN\DataManagerSubscriber');
|
||||
class_alias('\WP_Rocket\Engine\CDN\RocketCDN\NoticesSubscriber', '\WP_Rocket\Subscriber\CDN\RocketCDN\NoticesSubscriber');
|
||||
class_alias('\WP_Rocket\Engine\CDN\RocketCDN\RESTSubscriber', '\WP_Rocket\Subscriber\CDN\RocketCDN\RESTSubscriber');
|
||||
class_alias('\WP_Rocket\Engine\CDN\RocketCDN\ServiceProvider', '\WP_Rocket\ServiceProvider\RocketCDN');
|
||||
//RocketCDN End
|
||||
|
||||
/**
|
||||
* Removes Minification, DNS Prefetch, LazyLoad, Defer JS when on an AMP version of a post with the AMP for WordPress plugin from Auttomatic
|
||||
*
|
||||
* @since 2.8.10 Compatibility with wp_resource_hints in WP 4.6
|
||||
* @since 2.7
|
||||
* @deprecated 3.5.2
|
||||
*
|
||||
* @author Remy Perona
|
||||
*/
|
||||
function rocket_disable_options_on_amp() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5.2', '\WP_Rocket\ThirdParty\Plugins\Optimization\AMP::disable_options_on_amp()' );
|
||||
global $wp_filter;
|
||||
|
||||
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
|
||||
remove_filter( 'wp_resource_hints', 'rocket_dns_prefetch', 10, 2 );
|
||||
add_filter( 'do_rocket_lazyload', '__return_false' );
|
||||
unset( $wp_filter['rocket_buffer'] );
|
||||
|
||||
// this filter is documented in inc/front/protocol.php.
|
||||
$do_rocket_protocol_rewrite = apply_filters( 'do_rocket_protocol_rewrite', false ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
|
||||
if ( ( get_rocket_option( 'do_cloudflare', 0 ) && get_rocket_option( 'cloudflare_protocol_rewrite', 0 ) || $do_rocket_protocol_rewrite ) ) {
|
||||
remove_filter( 'rocket_buffer', 'rocket_protocol_rewrite', PHP_INT_MAX );
|
||||
remove_filter( 'wp_calculate_image_srcset', 'rocket_protocol_rewrite_srcset', PHP_INT_MAX );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Cloudflare input data
|
||||
*
|
||||
* @since 3.4.1
|
||||
* @deprecated 3.5
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param string $cf_email - Cloudflare email.
|
||||
* @param string $cf_api_key - Cloudflare API key.
|
||||
* @param string $cf_zone_id - Cloudflare zone ID.
|
||||
* @param bool $basic_validation - Bypass Cloudflare API user and zone validation.
|
||||
* @return Object - true if credentials are ok, WP_Error otherwise.
|
||||
*/
|
||||
function rocket_is_api_keys_valid_cloudflare( $cf_email, $cf_api_key, $cf_zone_id, $basic_validation = true ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::is_api_keys_valid()' );
|
||||
if ( ! function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' ) ) {
|
||||
return new WP_Error( 'curl_disabled', __( 'Curl is disabled on your server. Please ask your host to enable it. This is required for the Cloudflare Add-on to work correctly.', 'rocket' ) );
|
||||
}
|
||||
|
||||
if ( ! isset( $cf_email, $cf_api_key ) || empty( $cf_email ) || empty( $cf_api_key ) ) {
|
||||
return new WP_Error(
|
||||
'cloudflare_credentials_empty',
|
||||
sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Cloudflare email, API key and Zone ID are not set. Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! isset( $cf_zone_id ) || empty( $cf_zone_id ) ) {
|
||||
$msg = __( 'Missing Cloudflare Zone ID.', 'rocket' );
|
||||
|
||||
$msg .= ' ' . sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
return new WP_Error( 'cloudflare_no_zone_id', $msg );
|
||||
}
|
||||
|
||||
if ( $basic_validation ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_api_instance = new Cloudflare\Api( $cf_email, $cf_api_key );
|
||||
$cf_zone = $cf_api_instance->get( 'zones/' . $cf_zone_id );
|
||||
|
||||
if ( ! isset( $cf_zone->success ) || empty( $cf_zone->success ) ) {
|
||||
foreach ( $cf_zone->errors as $error ) {
|
||||
if ( 6003 === $error->code ) {
|
||||
$msg = __( 'Incorrect Cloudflare email address or API key.', 'rocket' );
|
||||
|
||||
$msg .= ' ' . sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
return new WP_Error( 'cloudflare_invalid_auth', $msg );
|
||||
}
|
||||
}
|
||||
$msg = __( 'Incorrect Cloudflare Zone ID.', 'rocket' );
|
||||
|
||||
$msg .= ' ' . sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
return new WP_Error( 'cloudflare_invalid_auth', $msg );
|
||||
}
|
||||
|
||||
if ( true === $cf_zone->success ) {
|
||||
$zone_found = false;
|
||||
$site_url = get_site_url();
|
||||
|
||||
if ( function_exists( 'domain_mapping_siteurl' ) ) {
|
||||
$site_url = domain_mapping_siteurl( $site_url );
|
||||
}
|
||||
|
||||
if ( ! empty( $cf_zone->result ) ) {
|
||||
$parsed_url = wp_parse_url( $site_url );
|
||||
if ( false !== strpos( strtolower( $parsed_url['host'] ), $cf_zone->result->name ) ) {
|
||||
$zone_found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $zone_found ) {
|
||||
$msg = __( 'It looks like your domain is not set up on Cloudflare.', 'rocket' );
|
||||
|
||||
$msg .= ' ' . sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
return new WP_Error( 'cloudflare_wrong_zone_id', $msg );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
$msg = __( 'Incorrect Cloudflare email address or API key.', 'rocket' );
|
||||
|
||||
$msg .= ' ' . sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
return new WP_Error( 'cloudflare_invalid_auth', $msg );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Cloudflare\Api instance
|
||||
*
|
||||
* @since 2.8.21
|
||||
* @deprecated 3.5
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @return Object Cloudflare\Api instance if crendentials are set, WP_Error otherwise
|
||||
*/
|
||||
function get_rocket_cloudflare_api_instance() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5' );
|
||||
if ( ! function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' ) ) {
|
||||
return new WP_Error( 'curl_disabled', __( 'Curl is disabled on your server. Please ask your host to enable it. This is required for the Cloudflare Add-on to work correctly.', 'rocket' ) );
|
||||
}
|
||||
|
||||
$cf_email = get_rocket_option( 'cloudflare_email', null );
|
||||
$cf_api_key = ( defined( 'WP_ROCKET_CF_API_KEY' ) ) ? WP_ROCKET_CF_API_KEY : get_rocket_option( 'cloudflare_api_key', null );
|
||||
|
||||
if ( ! isset( $cf_email, $cf_api_key ) ) {
|
||||
return new WP_Error(
|
||||
'cloudflare_credentials_empty',
|
||||
sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Cloudflare email and API key are not set. Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
)
|
||||
);
|
||||
}
|
||||
return new Cloudflare\Api( $cf_email, $cf_api_key );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Cloudflare\Api instance & the zone_id corresponding to the domain
|
||||
*
|
||||
* @since 2.8.21 Get the zone ID from the options
|
||||
* @since 2.8.18 Add try/catch to prevent fatal error Uncaugh Exception
|
||||
* @since 2.8.16 Update to Cloudflare API v4
|
||||
* @since 2.5
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @return Object Cloudflare instance & zone_id if credentials are correct, WP_Error otherwise
|
||||
*/
|
||||
function get_rocket_cloudflare_instance() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::get_instance()' );
|
||||
$cf_email = get_rocket_option( 'cloudflare_email', null );
|
||||
$cf_api_key = ( defined( 'WP_ROCKET_CF_API_KEY' ) ) ? WP_ROCKET_CF_API_KEY : get_rocket_option( 'cloudflare_api_key', null );
|
||||
$cf_zone_id = get_rocket_option( 'cloudflare_zone_id', null );
|
||||
$is_api_keys_valid_cf = rocket_is_api_keys_valid_cloudflare( $cf_email, $cf_api_key, $cf_zone_id, true );
|
||||
|
||||
if ( is_wp_error( $is_api_keys_valid_cf ) ) {
|
||||
return $is_api_keys_valid_cf;
|
||||
}
|
||||
|
||||
$cf_api_instance = get_rocket_cloudflare_api_instance();
|
||||
|
||||
$cf_instance = (object) [
|
||||
'auth' => $cf_api_instance,
|
||||
'zone_id' => $cf_zone_id,
|
||||
];
|
||||
|
||||
return $cf_instance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test the connection with Cloudflare
|
||||
*
|
||||
* @since 2.9
|
||||
* @deprecated 3.5
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @throws Exception If the connection to Cloudflare failed.
|
||||
* @return Object True if connection is successful, WP_Error otherwise
|
||||
*/
|
||||
function rocket_cloudflare_valid_auth() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5' );
|
||||
$cf_api_instance = get_rocket_cloudflare_api_instance();
|
||||
if ( is_wp_error( $cf_api_instance ) ) {
|
||||
return $cf_api_instance;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_zone_instance = new Cloudflare\Zone( $cf_api_instance );
|
||||
$cf_zones = $cf_zone_instance->zones();
|
||||
|
||||
if ( ! isset( $cf_zones->success ) || empty( $cf_zones->success ) ) {
|
||||
throw new Exception( __( 'Connection to Cloudflare failed', 'rocket' ) );
|
||||
}
|
||||
|
||||
if ( true === $cf_zones->success ) {
|
||||
return true;
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_invalid_auth', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get all the current Cloudflare settings for a given domain.
|
||||
*
|
||||
* @since 2.8.16 Update to Cloudflare API v4
|
||||
* @since 2.5
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @return mixed bool|Array Array of Cloudflare settings, false if any error connection to Cloudflare
|
||||
*/
|
||||
function get_rocket_cloudflare_settings() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::get_settings()' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the Cloudflare Development mode.
|
||||
*
|
||||
* @since 2.9 Now returns a value
|
||||
* @since 2.8.16 Update to Cloudflare API v4
|
||||
* @since 2.5
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @param string $mode Value for Cloudflare development mode.
|
||||
* @throws Exception If any error occurs when doing the API request.
|
||||
* @return mixed Object|String Mode value if the update is successful, WP_Error otherwise
|
||||
*/
|
||||
function set_rocket_cloudflare_devmode( $mode ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::set_devmode()' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the Cloudflare Caching level.
|
||||
*
|
||||
* @since 2.9 Now returns a value
|
||||
* @since 2.8.16 Update to Cloudflare API v4
|
||||
* @since 2.5
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @param string $mode Value for Cloudflare caching level.
|
||||
* @throws Exception If any error occurs when doing the API request.
|
||||
* @return mixed Object|String Mode value if the update is successful, WP_Error otherwise
|
||||
*/
|
||||
function set_rocket_cloudflare_cache_level( $mode ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::set_cache_level()' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Cloudflare Minification.
|
||||
*
|
||||
* @since 2.9 Now returns a value
|
||||
* @since 2.8.16 Update to Cloudflare API v4
|
||||
* @since 2.5
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @param string $mode Value for Cloudflare minification.
|
||||
* @throws Exception If any error occurs when doing the API request.
|
||||
* @return mixed Object|String Mode value if the update is successful, WP_Error otherwise
|
||||
*/
|
||||
function set_rocket_cloudflare_minify( $mode ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::set_minify()' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the Cloudflare Rocket Loader.
|
||||
*
|
||||
* @since 2.9 Now returns value
|
||||
* @since 2.8.16 Update to Cloudflare API v4
|
||||
* @since 2.5
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @param string $mode Value for Cloudflare Rocket Loader.
|
||||
* @throws Exception If any error occurs when doing the API request.
|
||||
* @return mixed Object|String Mode value if the update is successful, WP_Error otherwise
|
||||
*/
|
||||
function set_rocket_cloudflare_rocket_loader( $mode ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::set_rocket_loader()' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the Browser Cache TTL in Cloudflare.
|
||||
*
|
||||
* @since 2.9 Now returns value
|
||||
* @since 2.8.16
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @param string $mode Value for Cloudflare browser cache TTL.
|
||||
* @throws Exception If any error occurs when doing the API request.
|
||||
* @return mixed Object|String Mode value if the update is successful, WP_Error otherwise
|
||||
*/
|
||||
function set_rocket_cloudflare_browser_cache_ttl( $mode ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::set_browser_cache_ttl()' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Purge Cloudflare cache.
|
||||
*
|
||||
* @since 2.9 Now returns value
|
||||
* @since 2.8.16 Update to Cloudflare API v4
|
||||
* @since 2.5
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @throws Exception If any error occurs when doing the API request.
|
||||
* @return mixed Object|bool true if the purge is successful, WP_Error otherwise
|
||||
*/
|
||||
function rocket_purge_cloudflare() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::purge_cloudflare()' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Cloudflare IPs.
|
||||
*
|
||||
* @since 2.8.21 Save IPs in a transient to prevent calling the API everytime
|
||||
* @since 2.8.16
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @throws Exception If any error occurs when doing the API request.
|
||||
* @return Object Result of API request if successful, WP_Error otherwise
|
||||
*/
|
||||
function rocket_get_cloudflare_ips() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::get_cloudflare_ips()' );
|
||||
$cf_instance = get_rocket_cloudflare_api_instance();
|
||||
if ( is_wp_error( $cf_instance ) ) {
|
||||
return $cf_instance;
|
||||
}
|
||||
|
||||
$cf_ips = get_transient( 'rocket_cloudflare_ips' );
|
||||
if ( false === $cf_ips ) {
|
||||
try {
|
||||
$cf_ips_instance = new Cloudflare\IPs( $cf_instance );
|
||||
$cf_ips = $cf_ips_instance->ips();
|
||||
|
||||
if ( ! isset( $cf_ips->success ) || ! $cf_ips->success ) {
|
||||
throw new Exception( 'Error connecting to Cloudflare' );
|
||||
}
|
||||
|
||||
set_transient( 'rocket_cloudflare_ips', $cf_ips, 2 * WEEK_IN_SECONDS );
|
||||
} catch ( Exception $e ) {
|
||||
$cf_ips = (object) [
|
||||
'success' => true,
|
||||
'result' => (object) [],
|
||||
];
|
||||
|
||||
$cf_ips->result->ipv4_cidrs = [
|
||||
'103.21.244.0/22',
|
||||
'103.22.200.0/22',
|
||||
'103.31.4.0/22',
|
||||
'104.16.0.0/12',
|
||||
'108.162.192.0/18',
|
||||
'131.0.72.0/22',
|
||||
'141.101.64.0/18',
|
||||
'162.158.0.0/15',
|
||||
'172.64.0.0/13',
|
||||
'173.245.48.0/20',
|
||||
'188.114.96.0/20',
|
||||
'190.93.240.0/20',
|
||||
'197.234.240.0/22',
|
||||
'198.41.128.0/17',
|
||||
];
|
||||
|
||||
$cf_ips->result->ipv6_cidrs = [
|
||||
'2400:cb00::/32',
|
||||
'2405:8100::/32',
|
||||
'2405:b500::/32',
|
||||
'2606:4700::/32',
|
||||
'2803:f800::/32',
|
||||
'2c0f:f248::/32',
|
||||
'2a06:98c0::/29',
|
||||
];
|
||||
|
||||
set_transient( 'rocket_cloudflare_ips', $cf_ips, 2 * WEEK_IN_SECONDS );
|
||||
return $cf_ips;
|
||||
}
|
||||
}
|
||||
|
||||
return $cf_ips;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Real IP from CloudFlare
|
||||
*
|
||||
* @since 2.8.16 Uses CloudFlare API v4 to get CloudFlare IPs
|
||||
* @since 2.5.4
|
||||
*
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @source cloudflare.php - https://wordpress.org/plugins/cloudflare/
|
||||
*/
|
||||
function rocket_set_real_ip_cloudflare() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::set_real_ip()' );
|
||||
global $is_cf;
|
||||
|
||||
$is_cf = ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) ? true : false;
|
||||
|
||||
if ( ! $is_cf ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// only run this logic if the REMOTE_ADDR is populated, to avoid causing notices in CLI mode.
|
||||
if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
|
||||
$cf_ips_values = rocket_get_cloudflare_ips();
|
||||
|
||||
if ( is_wp_error( $cf_ips_values ) || ! isset( $cf_ips_values->success ) || ! $cf_ips_values->success ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( strpos( $_SERVER['REMOTE_ADDR'], ':' ) === false ) {
|
||||
$cf_ip_ranges = $cf_ips_values->result->ipv4_cidrs;
|
||||
|
||||
// IPV4: Update the REMOTE_ADDR value if the current REMOTE_ADDR value is in the specified range.
|
||||
foreach ( $cf_ip_ranges as $range ) {
|
||||
if ( rocket_ipv4_in_range( $_SERVER['REMOTE_ADDR'], $range ) ) {
|
||||
if ( $_SERVER['HTTP_CF_CONNECTING_IP'] ) {
|
||||
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
$cf_ip_ranges = $cf_ips_values->result->ipv6_cidrs;
|
||||
|
||||
$ipv6 = get_rocket_ipv6_full( $_SERVER['REMOTE_ADDR'] );
|
||||
foreach ( $cf_ip_ranges as $range ) {
|
||||
if ( rocket_ipv6_in_range( $ipv6, $range ) ) {
|
||||
if ( $_SERVER['HTTP_CF_CONNECTING_IP'] ) {
|
||||
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Let people know that the CF WP plugin is turned on.
|
||||
if ( ! headers_sent() ) {
|
||||
header( 'X-CF-Powered-By: WP Rocket ' . WP_ROCKET_VERSION );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This notice is displayed after purging the CloudFlare cache
|
||||
*
|
||||
* @since 2.9
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @deprecated 3.5
|
||||
*
|
||||
*/
|
||||
function rocket_cloudflare_purge_result() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::maybe_print_notice()' );
|
||||
global $current_user;
|
||||
if ( ! current_user_can( 'rocket_purge_cloudflare_cache' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! is_admin() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$notice = get_transient( $current_user->ID . '_cloudflare_purge_result' );
|
||||
if ( ! $notice ) {
|
||||
return;
|
||||
}
|
||||
|
||||
delete_transient( $current_user->ID . '_cloudflare_purge_result' );
|
||||
|
||||
rocket_notice_html( [
|
||||
'status' => $notice['result'],
|
||||
'message' => $notice['message'],
|
||||
] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge CloudFlare cache
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @deprecated 3.5
|
||||
*
|
||||
*/
|
||||
function do_admin_post_rocket_purge_cloudflare() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::do_purge_cloudflare()' );
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'rocket_purge_cloudflare' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'rocket_purge_cloudflare_cache' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Purge CloudFlare.
|
||||
$cf_purge = rocket_purge_cloudflare();
|
||||
|
||||
if ( is_wp_error( $cf_purge ) ) {
|
||||
$cf_purge_result = [
|
||||
'result' => 'error',
|
||||
// translators: %s = CloudFare API return message.
|
||||
'message' => sprintf( __( '<strong>WP Rocket:</strong> %s', 'rocket' ), $cf_purge->get_error_message() ),
|
||||
];
|
||||
} else {
|
||||
$cf_purge_result = [
|
||||
'result' => 'success',
|
||||
'message' => __( '<strong>WP Rocket:</strong> Cloudflare cache successfully purged.', 'rocket' ),
|
||||
];
|
||||
}
|
||||
|
||||
set_transient( get_current_user_id() . '_cloudflare_purge_result', $cf_purge_result );
|
||||
|
||||
wp_safe_redirect( esc_url_raw( wp_get_referer() ) );
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* This notice is displayed after modifying the CloudFlare settings
|
||||
*
|
||||
* @since 2.9
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @deprecated 3.5
|
||||
*/
|
||||
function rocket_cloudflare_update_settings() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', '\WP_Rocket\Subscriber\Tools\Cloudflare_Subscriber::maybe_print_update_settings_notice()' );
|
||||
global $current_user;
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 'settings_page_wprocket' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$notices = get_transient( $current_user->ID . '_cloudflare_update_settings' );
|
||||
if ( $notices ) {
|
||||
$errors = '';
|
||||
$success = '';
|
||||
delete_transient( $current_user->ID . '_cloudflare_update_settings' );
|
||||
foreach ( $notices as $notice ) {
|
||||
if ( 'error' === $notice['result'] ) {
|
||||
$errors .= $notice['message'] . '<br>';
|
||||
} elseif ( 'success' === $notice['result'] ) {
|
||||
$success .= $notice['message'] . '<br>';
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $success ) ) {
|
||||
rocket_notice_html( [
|
||||
'message' => $success,
|
||||
] );
|
||||
}
|
||||
|
||||
if ( ! empty( $errors ) ) {
|
||||
rocket_notice_html( [
|
||||
'status' => 'error',
|
||||
'message' => $success,
|
||||
] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge all the domain
|
||||
*
|
||||
* @since 2.6.8
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @param string $root The path of home cache file.
|
||||
* @param string $lang The current lang to purge.
|
||||
* @param string $url The home url.
|
||||
*/
|
||||
function rocket_varnish_clean_domain( $root, $lang, $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', 'WP_Rocket\Subscriber\Addons\Varnish\VarnishSubscriber::clean_domain()' );
|
||||
rocket_varnish_http_purge( trailingslashit( $url ) . '?vregex' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge a specific page
|
||||
*
|
||||
* @since 2.6.8
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @param string $url The url to purge.
|
||||
*/
|
||||
function rocket_varnish_clean_file( $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', 'WP_Rocket\Subscriber\Addons\Varnish\VarnishSubscriber::clean_file()' );
|
||||
rocket_varnish_http_purge( trailingslashit( $url ) . '?vregex' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge the homepage and its pagination
|
||||
*
|
||||
* @since 2.6.8
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @param string $root The path of home cache file.
|
||||
* @param string $lang The current lang to purge.
|
||||
*/
|
||||
function rocket_varnish_clean_home( $root, $lang ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', 'WP_Rocket\Subscriber\Addons\Varnish\VarnishSubscriber::clean_home()' );
|
||||
$home_url = trailingslashit( get_rocket_i18n_home_url( $lang ) );
|
||||
$home_pagination_url = $home_url . trailingslashit( $GLOBALS['wp_rewrite']->pagination_base ) . '?vregex';
|
||||
|
||||
rocket_varnish_http_purge( $home_url );
|
||||
rocket_varnish_http_purge( $home_pagination_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Varnish IP to localhost if Cloudflare is active
|
||||
*
|
||||
* @since 3.3.5
|
||||
* @deprecated 3.5
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function rocket_varnish_proxy_host() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', 'WP_Rocket\Subscriber\Addons\Cloudflare\CloudflareSubscriber::set_varnish_localhost()' );
|
||||
return 'localhost';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Host header to the website domain if Cloudflare is active
|
||||
*
|
||||
* @since 3.3.5
|
||||
* @deprecated 3.5
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function rocket_varnish_proxy_request_host() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', 'WP_Rocket\Subscriber\Addons\Cloudflare\CloudflareSubscriber::set_varnish_purge_request_host()' );
|
||||
return wp_parse_url( home_url(), PHP_URL_HOST );
|
||||
}
|
||||
|
||||
/**
|
||||
* Send data to Varnish
|
||||
*
|
||||
* @since 2.6.8
|
||||
* @deprecated 3.5
|
||||
*
|
||||
* @param string $url The URL to purge.
|
||||
* @return void
|
||||
*/
|
||||
function rocket_varnish_http_purge( $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5', 'WP_Rocket\Addons\Varnish\Varnish::purge()' );
|
||||
$parse_url = get_rocket_parse_url( $url );
|
||||
|
||||
$varnish_x_purgemethod = 'default';
|
||||
$regex = '';
|
||||
|
||||
if ( 'vregex' === $parse_url['query'] ) {
|
||||
$varnish_x_purgemethod = 'regex';
|
||||
$regex = '.*';
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the Varnish IP to call
|
||||
*
|
||||
* @since 2.6.8
|
||||
* @param string The Varnish IP
|
||||
*/
|
||||
$varnish_ip = apply_filters( 'rocket_varnish_ip', '' );
|
||||
|
||||
if ( defined( 'WP_ROCKET_VARNISH_IP' ) && ! $varnish_ip ) {
|
||||
$varnish_ip = WP_ROCKET_VARNISH_IP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the HTTP protocol (scheme)
|
||||
*
|
||||
* @since 2.7.3
|
||||
* @param string The HTTP protocol
|
||||
*/
|
||||
$scheme = apply_filters( 'rocket_varnish_http_purge_scheme', 'http' );
|
||||
|
||||
$parse_url['host'] = ( $varnish_ip ) ? $varnish_ip : $parse_url['host'];
|
||||
$purgeme = $scheme . '://' . $parse_url['host'] . $parse_url['path'] . $regex;
|
||||
|
||||
wp_remote_request(
|
||||
$purgeme,
|
||||
array(
|
||||
'method' => 'PURGE',
|
||||
'blocking' => false,
|
||||
'redirection' => 0,
|
||||
/**
|
||||
* Filters the headers to send with the Varnish purge request
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $headers Headers to send.
|
||||
*/
|
||||
'headers' => apply_filters(
|
||||
'rocket_varnish_purge_headers',
|
||||
[
|
||||
/**
|
||||
* Filters the host value passed in the request headers
|
||||
*
|
||||
* @since 2.8.15
|
||||
* @param string The host
|
||||
*/
|
||||
'host' => apply_filters( 'rocket_varnish_purge_request_host', $parse_url['host'] ),
|
||||
'X-Purge-Method' => $varnish_x_purgemethod,
|
||||
]
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a warning notice if WP Rocket scheduled events are not running properly
|
||||
*
|
||||
* @since 3.5.4 deprecated
|
||||
* @since 3.3.7
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function rocket_warning_cron() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5.4', 'WP_Rocket\Engine\Admin\HealthCheck::missed_cron()' );
|
||||
$screen = get_current_screen();
|
||||
|
||||
// This filter is documented in inc/admin-bar.php.
|
||||
if ( ! current_user_can( apply_filters( 'rocket_capacity', 'manage_options' ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 'settings_page_wprocket' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
|
||||
|
||||
if ( in_array( __FUNCTION__, (array) $boxes, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 0 === (int) get_rocket_option( 'purge_cron_interval' ) && 0 === get_rocket_option( 'async_css' ) && 0 === get_rocket_option( 'manual_preload' ) && 0 === get_rocket_option( 'schedule_automatic_cleanup' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$events = [
|
||||
'rocket_purge_time_event' => 'Scheduled Cache Purge',
|
||||
'rocket_database_optimization_time_event' => 'Scheduled Database Optimization',
|
||||
'rocket_database_optimization_cron_interval' => 'Database Optimization Process',
|
||||
'rocket_preload_cron_interval' => 'Preload',
|
||||
'rocket_critical_css_generation_cron_interval' => 'Critical Path CSS Generation Process',
|
||||
];
|
||||
|
||||
foreach ( $events as $event => $description ) {
|
||||
$timestamp = wp_next_scheduled( $event );
|
||||
|
||||
if ( false === $timestamp ) {
|
||||
unset( $events[ $event ] );
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $timestamp - time() > 0 ) {
|
||||
unset( $events[ $event ] );
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $events ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$message = '<p>' . _n( 'The following scheduled event failed to run. This may indicate the CRON system is not running properly, which can prevent some WP Rocket features from working as intended:', 'The following scheduled events failed to run. This may indicate the CRON system is not running properly, which can prevent some WP Rocket features from working as intended:', count( $events ), 'rocket' ) . '</p>';
|
||||
|
||||
$message .= '<ul>';
|
||||
|
||||
foreach ( $events as $description ) {
|
||||
$message .= '<li>' . $description . '</li>';
|
||||
}
|
||||
|
||||
$message .= '</ul>';
|
||||
$message .= '<p>' . __( 'Please contact your host to check if CRON is working.', 'rocket' ) . '</p>';
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'warning',
|
||||
'dismissible' => '',
|
||||
'message' => $message,
|
||||
'dismiss_button' => __FUNCTION__,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a link "Purge this cache" in the taxonomy edit area
|
||||
*
|
||||
* @since 3.5.5 deprecated
|
||||
* @since 1.0
|
||||
*
|
||||
* @param array $actions An array of row action links.
|
||||
* @param object $term The term object.
|
||||
* @return array Updated array of row action links
|
||||
*/
|
||||
function rocket_tag_row_actions( $actions, $term ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.5.5', 'WP_Rocket\Engine\Cache\AdminSubscriber::add_purge_term_link()' );
|
||||
global $taxnow;
|
||||
|
||||
if ( ! current_user_can( 'rocket_purge_terms' ) ) {
|
||||
return $actions;
|
||||
}
|
||||
|
||||
$url = wp_nonce_url( admin_url( 'admin-post.php?action=purge_cache&type=term-' . $term->term_id . '&taxonomy=' . $taxnow ), 'purge_cache_term-' . $term->term_id );
|
||||
$actions['rocket_purge'] = sprintf( '<a href="%s">%s</a>', $url, __( 'Clear this cache', 'rocket' ) );
|
||||
|
||||
return $actions;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,340 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Require deprecated classes.
|
||||
*/
|
||||
if ( ! class_exists( 'Minify_HTML' ) ) {
|
||||
require_once __DIR__ . '/vendors/classes/class-minify-html.php';
|
||||
}
|
||||
if ( ! class_exists( 'WP_Rocket\Subscriber\Optimization\Minify_HTML_Subscriber' ) ) {
|
||||
require_once __DIR__ . '/subscriber/admin/Optimization/class-minify-html-subscriber.php';
|
||||
}
|
||||
|
||||
class_alias( '\WP_Rocket\Engine\Heartbeat\HeartbeatSubscriber', '\WP_Rocket\Subscriber\Heartbeat_Subscriber' );
|
||||
|
||||
/**
|
||||
* Conflict with WP Serveur hosting: don't apply inline JS on all pages.
|
||||
*
|
||||
* @since 3.7 deprecated
|
||||
* @since 2.6.11
|
||||
*
|
||||
* @param array $html_options WP Rocket options array.
|
||||
*
|
||||
* @return array Updated WP Rocket options array.
|
||||
*/
|
||||
function rocket_deactivate_inline_js_on_wp_serveur( $html_options ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
if ( isset( $html_options['jsMinifier'] ) ) {
|
||||
unset( $html_options['jsMinifier'] );
|
||||
}
|
||||
|
||||
return $html_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Conflict with AppBanners: don't minify inline script when HTML minification is activated
|
||||
*
|
||||
* @since 3.7 deprecated
|
||||
* @since 2.2.4
|
||||
*
|
||||
* @param array $html_options An array of WP Rocket options.
|
||||
*
|
||||
* @return array Array without the inline js minify option
|
||||
*/
|
||||
function rocket_deactivate_js_minifier_with_appbanner( $html_options ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
if ( isset( $html_options['jsMinifier'] ) && class_exists( 'AppBanners' ) ) {
|
||||
unset( $html_options['jsMinifier'] );
|
||||
}
|
||||
|
||||
return $html_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate WP Rocket HTML Minification if Autoptimize HTML minification is enabled
|
||||
*
|
||||
* @since 3.7 deprecated
|
||||
* @since 2.9.5
|
||||
*
|
||||
* @param string $old_value Previous autoptimize option value.
|
||||
* @param string $value New autoptimize option value.
|
||||
*
|
||||
* @author Remy Perona
|
||||
*
|
||||
*/
|
||||
function rocket_maybe_deactivate_minify_html( $old_value, $value ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
if ( $value !== $old_value && 'on' === $value ) {
|
||||
update_rocket_option( 'minify_html', 0 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable WP Rocket HTML minification field if Autoptimize HTML minification is enabled
|
||||
*
|
||||
* @since 3.7 deprecated
|
||||
* @since 2.9.5
|
||||
* @return bool|null True if it is active
|
||||
* @author Remy Perona
|
||||
*
|
||||
*/
|
||||
function rocket_maybe_disable_minify_html() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
if ( is_plugin_active( 'autoptimize/autoptimize.php' ) && 'on' === get_option( 'autoptimize_html' ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Conflict with Revolution Slider: don't minify inline script when HTML minification is activated
|
||||
*
|
||||
* @since 3.7 deprecated
|
||||
* @since 2.6.8
|
||||
*
|
||||
* @param array $html_options WP Rocket options array.
|
||||
*
|
||||
* @return array Updated WP Rocket options
|
||||
*/
|
||||
function rocket_deactivate_js_minifier_with_revslider( $html_options ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
if ( isset( $html_options['jsMinifier'] ) && class_exists( 'RevSliderFront' ) ) {
|
||||
unset( $html_options['jsMinifier'] );
|
||||
}
|
||||
|
||||
return $html_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the emoji functionality to reduce then number of external HTTP requests.
|
||||
*
|
||||
* @since 3.7 Deprecated.
|
||||
* @since 2.7
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
function rocket_disable_emoji() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
|
||||
if ( rocket_bypass() ) {
|
||||
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 Deprecated.
|
||||
* @since 2.7
|
||||
*
|
||||
* @param array $plugins Plugins loaded for TinyMCE.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
function rocket_disable_emoji_tinymce( $plugins ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
if ( is_array( $plugins ) ) {
|
||||
return array_diff( $plugins, [ 'wpemoji' ] );
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable embeds on init.
|
||||
*
|
||||
* - Removes the needed query vars.
|
||||
* - Disables oEmbed discovery.
|
||||
* - Completely removes the related JavaScript.
|
||||
*
|
||||
* @since 3.7 Deprecated.
|
||||
* @since 2.10
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
function rocket_disable_embeds_init() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
if ( rocket_bypass() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $wp;
|
||||
|
||||
// Remove the embed query var.
|
||||
$wp->public_query_vars = array_diff(
|
||||
$wp->public_query_vars,
|
||||
[
|
||||
'embed',
|
||||
]
|
||||
);
|
||||
|
||||
// Remove the oembed/1.0/embed REST route.
|
||||
add_filter( 'rest_endpoints', 'rocket_disable_embeds_remove_embed_endpoint' );
|
||||
|
||||
// Disable handling of internal embeds in oembed/1.0/proxy REST route.
|
||||
add_filter( 'oembed_response_data', 'rocket_disable_embeds_filter_oembed_response_data' );
|
||||
|
||||
// Turn off oEmbed auto discovery.
|
||||
add_filter( 'embed_oembed_discover', '__return_false' );
|
||||
|
||||
// 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' );
|
||||
add_filter( 'tiny_mce_plugins', 'rocket_disable_embeds_tiny_mce_plugin' );
|
||||
|
||||
// Remove all embeds rewrite rules.
|
||||
add_filter( 'rewrite_rules_array', 'rocket_disable_embeds_rewrites' );
|
||||
|
||||
// Remove filter of the oEmbed result before any HTTP requests are made.
|
||||
remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 );
|
||||
|
||||
// Load block editor JavaScript.
|
||||
add_action( 'enqueue_block_editor_assets', 'rocket_disable_embeds_enqueue_block_editor_assets' );
|
||||
|
||||
// Remove wp-embed dependency of wp-edit-post script handle.
|
||||
add_action( 'wp_default_scripts', 'rocket_disable_embeds_remove_script_dependencies' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the 'wpembed' TinyMCE plugin.
|
||||
*
|
||||
* @since 3.7 Deprecated.
|
||||
* @since 2.10
|
||||
*
|
||||
* @param array $plugins List of TinyMCE plugins.
|
||||
*
|
||||
* @return array The modified list.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
function rocket_disable_embeds_tiny_mce_plugin( $plugins ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
return array_diff( $plugins, [ 'wpembed' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all rewrite rules related to embeds.
|
||||
*
|
||||
* @since 3.7 Deprecated.
|
||||
* @since 2.10
|
||||
*
|
||||
* @param array $rules WordPress rewrite rules.
|
||||
*
|
||||
* @return array Rewrite rules without embeds rules.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
function rocket_disable_embeds_rewrites( $rules ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
if ( empty( $rules ) ) {
|
||||
return $rules;
|
||||
}
|
||||
|
||||
foreach ( $rules as $rule => $rewrite ) {
|
||||
if ( false !== strpos( $rewrite, 'embed=true' ) ) {
|
||||
unset( $rules[ $rule ] );
|
||||
}
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the oembed/1.0/embed REST route.
|
||||
*
|
||||
* @since 3.6 Deprecated.
|
||||
* @since 3.3.3
|
||||
*
|
||||
* @param array $endpoints Registered REST API endpoints.
|
||||
*
|
||||
* @return array Filtered REST API endpoints.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
function rocket_disable_embeds_remove_embed_endpoint( $endpoints ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
unset( $endpoints['/oembed/1.0/embed'] );
|
||||
|
||||
return $endpoints;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables sending internal oEmbed response data in proxy endpoint.
|
||||
*
|
||||
* @since 3.7 Deprecated.
|
||||
* @since 3.3.3
|
||||
*
|
||||
* @param array $data The response data.
|
||||
*
|
||||
* @return array|false Response data or false if in a REST API context.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
function rocket_disable_embeds_filter_oembed_response_data( $data ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues JavaScript for the block editor.
|
||||
*
|
||||
* This is used to unregister the `core-embed/wordpress` block type.
|
||||
*
|
||||
* @since 3.7 Deprecated.
|
||||
* @since 3.3.3
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
function rocket_disable_embeds_enqueue_block_editor_assets() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
wp_enqueue_script(
|
||||
'rocket-disable-embeds',
|
||||
WP_ROCKET_ASSETS_JS_URL . 'editor/editor.js',
|
||||
[
|
||||
'wp-edit-post',
|
||||
'wp-editor',
|
||||
'wp-dom',
|
||||
],
|
||||
WP_ROCKET_VERSION,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes wp-embed dependency of core packages.
|
||||
*
|
||||
* @since 3.7 deprecated
|
||||
* @since 3.3.3
|
||||
*
|
||||
* @param \WP_Scripts $scripts WP_Scripts instance, passed by reference.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
function rocket_disable_embeds_remove_script_dependencies( $scripts ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.7' );
|
||||
if ( ! empty( $scripts->registered['wp-edit-post'] ) ) {
|
||||
$scripts->registered['wp-edit-post']->deps = array_diff(
|
||||
$scripts->registered['wp-edit-post']->deps,
|
||||
[ 'wp-embed' ]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,333 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class_alias( '\WP_Rocket\Engine\Cache\PurgeExpired\PurgeExpiredCache', '\WP_Rocket\Cache\Expired_Cache_Purge');
|
||||
class_alias( '\WP_Rocket\Engine\Cache\PurgeExpired\Subscriber', '\WP_Rocket\Subscriber\Cache\Expired_Cache_Purge_Subscriber');
|
||||
class_alias( '\WP_Rocket\Engine\Media\Lazyload\Subscriber', '\WP_Rocket\Engine\Media\LazyloadSubscriber');
|
||||
|
||||
if ( ! class_exists( 'WP_Rocket\Subscriber\Optimization\Dequeue_JQuery_Migrate_Subscriber' ) ) {
|
||||
require_once __DIR__ . '/subscriber/Optimization/class-dequeue-jquery-migrate-subscriber.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate WP Rocket lazyload if Avada lazyload is enabled
|
||||
*
|
||||
* @since 3.8.1 deprecated
|
||||
* @since 3.3.4
|
||||
*
|
||||
* @param string $old_value Previous Avada option value.
|
||||
* @param string $value New Avada option value.
|
||||
* @return void
|
||||
*/
|
||||
function rocket_avada_maybe_deactivate_lazyload( $old_value, $value ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.8.1', 'WP_Rocket\ThirdParty\Themes\Avada::maybe_deactivate_lazyload()' );
|
||||
|
||||
if (
|
||||
empty( $old_value['lazy_load'] )
|
||||
||
|
||||
( ! empty( $value['lazy_load'] ) && 'avada' === $value['lazy_load'] )
|
||||
) {
|
||||
update_rocket_option( 'lazyload', 0 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable WP Rocket lazyload field if Avada lazyload is enabled
|
||||
*
|
||||
* @since 3.8.1 deprecated
|
||||
* @since 3.3.4
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function rocket_avada_maybe_disable_lazyload() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.8.1', 'WP_Rocket\ThirdParty\Themes\Avada::maybe_disable_lazyload()' );
|
||||
|
||||
$avada_options = get_option( 'fusion_options' );
|
||||
$current_theme = wp_get_theme();
|
||||
|
||||
if ( 'Avada' !== $current_theme->get( 'Name' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $avada_options['lazy_load'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! empty( $avada_options['lazy_load'] && 'avada' !== $avada_options['lazy_load'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears WP Rocket's cache after Avada's Fusion Patcher flushes their caches
|
||||
*
|
||||
* @since 3.8.1 deprecated
|
||||
* @since 3.3.5
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function rocket_avada_clear_cache_fusion_patcher() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.8.1', 'WP_Rocket\ThirdParty\Themes\Avada::clear_cache_fusion_patcher()' );
|
||||
|
||||
rocket_clean_domain();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defer all JS files.
|
||||
*
|
||||
* @since 3.8 deprecated
|
||||
* @since 2.10
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $buffer HTML content.
|
||||
* @return string Updated HTML content
|
||||
*/
|
||||
function rocket_defer_js( $buffer ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.8', 'WP_Rocket\Engine\Optimization\DeferJS\DeferJS::defer_js()' );
|
||||
|
||||
if ( ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) || ( defined( 'DONOTASYNCCSS' ) && DONOTASYNCCSS ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! get_rocket_option( 'defer_all_js' ) ) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
if ( is_rocket_post_excluded_option( 'defer_all_js' ) ) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
$buffer_nocomments = preg_replace( '/<!--(.*)-->/Uis', '', $buffer );
|
||||
// Get all JS files with this regex.
|
||||
preg_match_all( '#<script\s+([^>]+[\s\'"])?src\s*=\s*[\'"]\s*?([^\'"]+\.js(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>#iU', $buffer_nocomments, $tags_match );
|
||||
|
||||
if ( ! isset( $tags_match[0] ) ) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
$exclude_defer_js = implode( '|', get_rocket_exclude_defer_js() );
|
||||
|
||||
foreach ( $tags_match[0] as $i => $tag ) {
|
||||
// Check if this file should be deferred.
|
||||
if ( preg_match( '#(' . $exclude_defer_js . ')#i', $tags_match[2][ $i ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't add defer if already async.
|
||||
if ( false !== strpos( $tags_match[1][ $i ], 'async' ) || false !== strpos( $tags_match[3][ $i ], 'async' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't add defer if already defer.
|
||||
if ( false !== strpos( $tags_match[1][ $i ], 'defer' ) || false !== strpos( $tags_match[3][ $i ], 'defer' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$deferred_tag = str_replace( '>', ' defer>', $tag );
|
||||
$buffer = str_replace( $tag, $deferred_tag, $buffer );
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of JS files to be excluded from defer JS.
|
||||
*
|
||||
* @since 3.8 deprecated
|
||||
* @since 2.10
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return array An array of URLs for the JS files to be excluded.
|
||||
*/
|
||||
function get_rocket_exclude_defer_js() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.8', 'WP_Rocket\Engine\Optimization\DeferJS\DeferJS::get_excluded()' );
|
||||
|
||||
$exclude_defer_js = [
|
||||
'gist.github.com',
|
||||
'content.jwplatform.com',
|
||||
'js.hsforms.net',
|
||||
'www.uplaunch.com',
|
||||
'google.com/recaptcha',
|
||||
'widget.reviews.co.uk',
|
||||
'verify.authorize.net/anetseal',
|
||||
'lib/admin/assets/lib/webfont/webfont.min.js',
|
||||
'app.mailerlite.com',
|
||||
'widget.reviews.io',
|
||||
'simplybook.(.*)/v2/widget/widget.js',
|
||||
'/wp-includes/js/dist/i18n.min.js',
|
||||
'/wp-content/plugins/wpfront-notification-bar/js/wpfront-notification-bar(.*).js',
|
||||
'/wp-content/plugins/oxygen/component-framework/vendor/aos/aos.js',
|
||||
'static.mailerlite.com/data/(.*).js',
|
||||
'cdn.voxpow.com/static/libs/v1/(.*).js',
|
||||
'cdn.voxpow.com/media/trackers/js/(.*).js',
|
||||
];
|
||||
|
||||
if ( get_rocket_option( 'defer_all_js', 0 ) && get_rocket_option( 'defer_all_js_safe', 0 ) ) {
|
||||
$jquery = site_url( wp_scripts()->registered['jquery-core']->src );
|
||||
$jetpack_jquery = 'c0.wp.com/c/(?:.+)/wp-includes/js/jquery/jquery.js';
|
||||
$googleapis_jquery = 'ajax.googleapis.com/ajax/libs/jquery/(?:.+)/jquery(?:\.min)?.js';
|
||||
$cdnjs_jquery = 'cdnjs.cloudflare.com/ajax/libs/jquery/(?:.+)/jquery(?:\.min)?.js';
|
||||
$code_jquery = 'code.jquery.com/jquery-.*(?:\.min|slim)?.js';
|
||||
|
||||
$exclude_defer_js[] = rocket_clean_exclude_file( $jquery );
|
||||
$exclude_defer_js[] = $jetpack_jquery;
|
||||
$exclude_defer_js[] = $googleapis_jquery;
|
||||
$exclude_defer_js[] = $cdnjs_jquery;
|
||||
$exclude_defer_js[] = $code_jquery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter list of Deferred JavaScript files
|
||||
*
|
||||
* @since 2.10
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $exclude_defer_js An array of URLs for the JS files to be excluded.
|
||||
*/
|
||||
$exclude_defer_js = apply_filters( 'rocket_exclude_defer_js', $exclude_defer_js );
|
||||
|
||||
foreach ( $exclude_defer_js as $i => $exclude ) {
|
||||
$exclude_defer_js[ $i ] = str_replace( '#', '\#', $exclude );
|
||||
}
|
||||
|
||||
return $exclude_defer_js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add width and height attributes on all images
|
||||
*
|
||||
* @since 3.8 deprecated
|
||||
* @since 2.2.2 This feature is enabled by a hook
|
||||
* @since 1.3.0 This process is called via the new filter rocket_buffer
|
||||
* @since 1.3.0 It's possible to not specify dimensions of an image with data-no-image-dimensions attribute
|
||||
* @since 1.1.2 Fix Bug : No conflit with Photon Plugin (Jetpack)
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @param string $buffer HTML content.
|
||||
* @return string Modified HTML content
|
||||
*/
|
||||
function rocket_specify_image_dimensions( $buffer ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.8', 'WP_Rocket\Engine\Media\ImagesSubscriber::specify_image_dimensions()' );
|
||||
/**
|
||||
* Filter images dimensions attributes
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param bool Do the job or not.
|
||||
*/
|
||||
if ( ! apply_filters( 'rocket_specify_image_dimensions', false ) ) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
// Get all images without width or height attribute.
|
||||
preg_match_all( '/<img(?:[^>](?!(height|width)=))*+>/i', $buffer, $images_match );
|
||||
|
||||
foreach ( $images_match[0] as $image ) {
|
||||
|
||||
// Don't touch lazy-load file (no conflit with Photon (Jetpack)).
|
||||
if ( strpos( $image, 'data-lazy-original' ) || strpos( $image, 'data-no-image-dimensions' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tmp = $image;
|
||||
|
||||
// Get link of the file.
|
||||
preg_match( '/src=[\'"]([^\'"]+)/', $image, $src_match );
|
||||
|
||||
// Get infos of the URL.
|
||||
$image_url = wp_parse_url( $src_match[1] );
|
||||
|
||||
// Check if the link isn't external.
|
||||
if ( empty( $image_url['host'] ) || rocket_remove_url_protocol( home_url() ) === $image_url['host'] ) {
|
||||
// Get image attributes.
|
||||
$sizes = getimagesize( ABSPATH . $image_url['path'] );
|
||||
} else {
|
||||
/**
|
||||
* Filter distant images dimensions attributes
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param bool Do the job or not
|
||||
*/
|
||||
if ( ini_get( 'allow_url_fopen' ) && apply_filters( 'rocket_specify_image_dimensions_for_distant', false ) ) {
|
||||
// Get image attributes.
|
||||
$sizes = getimagesize( $image_url['scheme'] . '://' . $image_url['host'] . $image_url['path'] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $sizes ) ) {
|
||||
// Add width and width attribute.
|
||||
$image = str_replace( '<img', '<img ' . $sizes[3], $image );
|
||||
|
||||
// Replace image with new attributes.
|
||||
$buffer = str_replace( $tmp, $image, $buffer );
|
||||
}
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Conflict with LayerSlider: don't add width and height attributes on all images
|
||||
*
|
||||
* @since 3.8 deprecated
|
||||
* @since 2.1
|
||||
*/
|
||||
function rocket_deactivate_specify_image_dimensions_with_layerslider() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.8', 'WP_Rocket\ThirdParty\Plugins\Slider\LayerSlider::get_subscribed_events()' );
|
||||
remove_filter( 'rocket_buffer', 'rocket_specify_image_dimensions' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add age-verified to the list of mandatory cookies
|
||||
*
|
||||
* @since 3.8.6 deprecated
|
||||
* @since 2.7
|
||||
*
|
||||
* @param Array $cookies Array of mandatory cookies.
|
||||
* @return Array Updated array of mandatory cookies
|
||||
*/
|
||||
function rocket_add_cache_mandatory_cookie_for_age_verify( $cookies ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.8.6' );
|
||||
|
||||
$cookies[] = 'age-verified';
|
||||
return $cookies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add age-verified cookie when we activate the plugin
|
||||
*
|
||||
* @since 3.8.6 deprecated
|
||||
* @since 2.7
|
||||
*/
|
||||
function rocket_activate_age_verify() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.8.6' );
|
||||
add_filter( 'rocket_htaccess_mod_rewrite', '__return_false', 18 );
|
||||
add_filter( 'rocket_cache_mandatory_cookies', 'rocket_add_cache_mandatory_cookie_for_age_verify' );
|
||||
|
||||
// Update the WP Rocket rules on the .htaccess file.
|
||||
flush_rocket_htaccess();
|
||||
|
||||
// Regenerate the config file.
|
||||
rocket_generate_config_file();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove age-verified cookie when we deactivate the plugin
|
||||
*
|
||||
* @since 3.8.6 deprecated
|
||||
* @since 2.7
|
||||
*/
|
||||
function rocket_deactivate_age_verify() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.8.6' );
|
||||
remove_filter( 'rocket_cache_mandatory_cookies', 'rocket_add_cache_mandatory_cookie_for_age_verify' );
|
||||
|
||||
// Update the WP Rocket rules on the .htaccess file.
|
||||
flush_rocket_htaccess();
|
||||
|
||||
// Regenerate the config file.
|
||||
rocket_generate_config_file();
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class_alias( '\WP_Rocket\ThirdParty\Hostings\LiteSpeed', '\WP_Rocket\Subscriber\Third_Party\Hostings\Litespeed_Subscriber');
|
||||
|
||||
/**
|
||||
* Changes the text on the Varnish one-click block.
|
||||
*
|
||||
* @since 3.9.1 deprecated
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $settings Field settings data.
|
||||
*
|
||||
* @return array modified field settings data.
|
||||
*/
|
||||
function rocket_godaddy_varnish_field( $settings ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.9.1', '\WP_Rocket\ThirdParty\Hostings\Godaddy::godaddy_varnish_field' );
|
||||
$settings['varnish_auto_purge']['title'] = sprintf(
|
||||
// Translators: %s = Hosting name.
|
||||
__( 'Your site is hosted on %s, we have enabled Varnish auto-purge for compatibility.', 'rocket' ),
|
||||
'GoDaddy'
|
||||
);
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove expiration on HTML to prevent issue with Varnish cache.
|
||||
*
|
||||
* @since 3.9.1 deprecated
|
||||
* @since 2.9.5
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $rules htaccess rules.
|
||||
* @return string Updated htaccess rules.
|
||||
*/
|
||||
function rocket_remove_html_expire_goddady( $rules ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.9.1', '\WP_Rocket\ThirdParty\Hostings\Godaddy::remove_html_expire_goddady' );
|
||||
$rules = preg_replace( '@\s*#\s*Your document html@', '', $rules );
|
||||
$rules = preg_replace( '@\s*ExpiresByType text/html\s*"access plus \d+ (seconds|minutes|hour|week|month|year)"@', '', $rules );
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the Varnish server to purge the cache with GoDaddy.
|
||||
*
|
||||
* @since 3.9.1 deprecated
|
||||
* @since 2.9.5
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function rocket_clean_domain_godaddy() {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.9.1', '\WP_Rocket\ThirdParty\Hostings\Godaddy::clean_domain_godaddy' );
|
||||
rocket_godaddy_request( 'BAN' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the Varnish server to purge a specific URL with GoDaddy.
|
||||
*
|
||||
* @since 3.9.1 deprecated
|
||||
* @since 2.9.5
|
||||
*
|
||||
* @param string $url URL to purge.
|
||||
* @return void
|
||||
*/
|
||||
function rocket_clean_file_godaddy( $url ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.9.1', '\WP_Rocket\ThirdParty\Hostings\Godaddy::clean_file_godaddy' );
|
||||
rocket_godaddy_request( 'PURGE', home_url( $url ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the Varnish server to purge the home with GoDaddy.
|
||||
*
|
||||
* @since 3.9.1 deprecated
|
||||
* @since 2.9.5
|
||||
*
|
||||
* @param string $root root URL.
|
||||
* @param string $lang language code.
|
||||
* @return void
|
||||
*/
|
||||
function rocket_clean_home_godaddy( $root, $lang ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.9.1', '\WP_Rocket\ThirdParty\Hostings\Godaddy::clean_home_godaddy' );
|
||||
$home_url = trailingslashit( get_rocket_i18n_home_url( $lang ) );
|
||||
$home_pagination_url = $home_url . trailingslashit( $GLOBALS['wp_rewrite']->pagination_base );
|
||||
|
||||
rocket_godaddy_request( 'PURGE', $home_url );
|
||||
rocket_godaddy_request( 'PURGE', $home_pagination_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the call to the Varnish server to purge
|
||||
*
|
||||
* @since 3.9.1 deprecated
|
||||
* @since 2.9.5
|
||||
* @source WPaaS\Cache
|
||||
*
|
||||
* @param string $method can be BAN or PURGE.
|
||||
* @param string $url URL to purge.
|
||||
* @return void
|
||||
*/
|
||||
function rocket_godaddy_request( $method, $url = null ) {
|
||||
_deprecated_function( __FUNCTION__ . '()', '3.9.0.4', '\WP_Rocket\ThirdParty\Hostings\Godaddy::godaddy_request' );
|
||||
if ( ! method_exists( 'WPaas\Plugin', 'vip' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( empty( $url ) ) {
|
||||
$url = home_url();
|
||||
}
|
||||
|
||||
$host = rocket_extract_url_component( $url, PHP_URL_HOST );
|
||||
$url = set_url_scheme( str_replace( $host, WPaas\Plugin::vip(), $url ), 'http' );
|
||||
|
||||
wp_cache_flush();
|
||||
|
||||
// This forces the APC cache to flush across the server.
|
||||
update_option( 'gd_system_last_cache_flush', time() );
|
||||
|
||||
wp_remote_request(
|
||||
esc_url_raw( $url ),
|
||||
[
|
||||
'method' => $method,
|
||||
'blocking' => false,
|
||||
'headers' => [
|
||||
'Host' => $host,
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\deprecated;
|
||||
|
||||
/**
|
||||
* Trait to use in a deprecated class, or a class containing deprecated methods.
|
||||
*/
|
||||
trait DeprecatedClassTrait {
|
||||
/**
|
||||
* Marks a class as deprecated and informs when it has been used.
|
||||
* Similar to _deprecated_constructor(), but with different strings.
|
||||
* The current behavior is to trigger a user error if `WP_DEBUG` is true.
|
||||
*
|
||||
* @since 3.6
|
||||
*
|
||||
* @param string $version The version of WordPress that deprecated the class.
|
||||
* @param string $replacement Optional. The method that should have been called. Default null.
|
||||
*/
|
||||
private static function deprecated_class( $version, $replacement = null ) {
|
||||
/**
|
||||
* Fires when a deprecated class is called.
|
||||
*
|
||||
* @since 3.6
|
||||
*
|
||||
* @param string $class The class containing the deprecated constructor.
|
||||
* @param string $version The version of WordPress that deprecated the class.
|
||||
* @param string $replacement Optional. The method that should have been called.
|
||||
*/
|
||||
do_action( 'rocket_deprecated_class_run', static::class, $version, $replacement );
|
||||
|
||||
if ( ! WP_DEBUG ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters whether to trigger an error for deprecated classes.
|
||||
* `WP_DEBUG` must be true in addition to the filter evaluating to true.
|
||||
*
|
||||
* @since 3.6
|
||||
*
|
||||
* @param bool $trigger Whether to trigger the error for deprecated classes. Default true.
|
||||
*/
|
||||
if ( ! apply_filters( 'rocket_deprecated_class_trigger_error', true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( function_exists( '__' ) ) {
|
||||
if ( ! empty( $replacement ) ) {
|
||||
/**
|
||||
* With replacement.
|
||||
*/
|
||||
$message = sprintf(
|
||||
/* translators: 1: PHP class name, 2: version number, 3: replacement class name. */
|
||||
__( 'The called class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', 'rocket' ),
|
||||
'<code>' . static::class . '</code>',
|
||||
'<strong>' . $version . '</strong>',
|
||||
'<code>' . $replacement . '</code>'
|
||||
);
|
||||
} else {
|
||||
/**
|
||||
* Without replacement.
|
||||
*/
|
||||
$message = sprintf(
|
||||
/* translators: 1: PHP class name, 2: version number. */
|
||||
__( 'The called class %1$s is <strong>deprecated</strong> since version %2$s!', 'rocket' ),
|
||||
'<code>' . static::class . '</code>',
|
||||
'<strong>' . $version . '</strong>'
|
||||
);
|
||||
}
|
||||
} elseif ( ! empty( $replacement ) ) {
|
||||
/**
|
||||
* With replacement.
|
||||
*/
|
||||
$message = sprintf(
|
||||
'The called class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
|
||||
'<code>' . static::class . '</code>',
|
||||
'<strong>' . $version . '</strong>',
|
||||
'<code>' . $replacement . '</code>'
|
||||
);
|
||||
} else {
|
||||
/**
|
||||
* Without replacement.
|
||||
*/
|
||||
$message = sprintf(
|
||||
'The called class %1$s is <strong>deprecated</strong> since version %2$s!',
|
||||
'<code>' . static::class . '</code>',
|
||||
'<strong>' . $version . '</strong>'
|
||||
);
|
||||
}
|
||||
|
||||
call_user_func( 'trigger_error', $message );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Addon\Busting;
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Addon\GoogleTracking\GoogleAnalytics;
|
||||
use WP_Rocket\Addon\GoogleTracking\GoogleTagManager;
|
||||
use WP_Rocket\Busting\Facebook_Pickles;
|
||||
use WP_Rocket\Busting\Facebook_SDK;
|
||||
|
||||
/**
|
||||
* Busting classes Factory
|
||||
*
|
||||
* @since 3.9 deprecated.
|
||||
* @since 3.6.2
|
||||
*/
|
||||
class BustingFactory {
|
||||
use DeprecatedClassTrait;
|
||||
|
||||
/**
|
||||
* Base cache busting filepath.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $busting_path;
|
||||
|
||||
/**
|
||||
* Base cache busting URL.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $busting_url;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $busting_path Base cache busting filepath.
|
||||
* @param string $busting_url Base cache busting URL.
|
||||
*/
|
||||
public function __construct( $busting_path, $busting_url ) {
|
||||
self::deprecated_class( '3.9' );
|
||||
$this->busting_path = $busting_path;
|
||||
$this->busting_url = $busting_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creator method
|
||||
*
|
||||
* @param string $type Type of busting class to create.
|
||||
* @return Busting_Interface
|
||||
*/
|
||||
public function type( $type ) {
|
||||
switch ( $type ) {
|
||||
case 'fbpix':
|
||||
return new Facebook_Pickles( $this->busting_path, $this->busting_url );
|
||||
case 'fbsdk':
|
||||
return new Facebook_SDK( $this->busting_path, $this->busting_url );
|
||||
case 'ga':
|
||||
return new GoogleAnalytics( $this->busting_path, $this->busting_url );
|
||||
case 'gtm':
|
||||
return new GoogleTagManager( $this->busting_path, $this->busting_url, new GoogleAnalytics( $this->busting_path, $this->busting_url ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
+478
@@ -0,0 +1,478 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Addon\Busting;
|
||||
|
||||
use WP_Rocket\Logger\Logger;
|
||||
|
||||
trait FileBustingTrait {
|
||||
/**
|
||||
* Saves the content of the URL to bust to the busting file if it doesn't exist yet.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access public
|
||||
*
|
||||
* @param string $url URL to get the content from.
|
||||
* @return bool
|
||||
*/
|
||||
public function save( $url ) {
|
||||
if ( $this->get_busting_version() ) {
|
||||
// We have a local copy.
|
||||
Logger::debug(
|
||||
'Found local file.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'path' => $this->get_busting_path(),
|
||||
]
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( $this->refresh_save( $url ) ) {
|
||||
// We downloaded a fresh copy.
|
||||
Logger::debug(
|
||||
'New copy downloaded.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'path' => $this->get_busting_path(),
|
||||
]
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the busting file.
|
||||
*
|
||||
* @since 3.1
|
||||
* @since 3.2.4 Handle versioning.
|
||||
* @access public
|
||||
* @author Remy Perona
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True on success. False on failure.
|
||||
*/
|
||||
public function delete() {
|
||||
$files = $this->get_all_files();
|
||||
|
||||
if ( false === $files ) {
|
||||
// Error.
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->file_version = null;
|
||||
|
||||
if ( ! $files ) {
|
||||
// No local files yet.
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->delete_files( array_keys( $files ) );
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** LOCAL FILE ============================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the version of the current busting file.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string|bool Version of the file. False if the file does not exist.
|
||||
*/
|
||||
protected function get_busting_version() {
|
||||
if ( ! empty( $this->file_version ) ) {
|
||||
return $this->file_version;
|
||||
}
|
||||
|
||||
$files = $this->get_all_files();
|
||||
|
||||
if ( ! $files ) {
|
||||
// Error or no local files yet.
|
||||
return false;
|
||||
}
|
||||
|
||||
// Since we're not supposed to have several files, return the first one.
|
||||
$this->file_version = reset( $files );
|
||||
|
||||
return $this->file_version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all cached files in the directory.
|
||||
* In a perfect world, there should be only one.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access private
|
||||
*
|
||||
* @return bool|array A list of file names (as array keys) and versions (as array values). False on failure.
|
||||
*/
|
||||
private function get_all_files() {
|
||||
$dir_path = rtrim( $this->busting_path, '\\/' );
|
||||
|
||||
if ( ! $this->filesystem->exists( $dir_path ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ( ! $this->filesystem->is_readable( $dir_path ) ) {
|
||||
Logger::error(
|
||||
'Directory is not readable.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'path' => $dir_path,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$pattern = '/' . sprintf(
|
||||
$this->escape_file_name( $this->filename_pattern ),
|
||||
'([a-f0-9]{32}|local)'
|
||||
) . '/';
|
||||
|
||||
$entries = _rocket_get_dir_files_by_regex( $dir_path, $pattern );
|
||||
|
||||
$list = [];
|
||||
foreach ( $entries as $entry ) {
|
||||
$filename = $entry->getFilename();
|
||||
|
||||
preg_match( $pattern, $filename, $file_details_match );
|
||||
if ( ! empty( $file_details_match[1] ) ) {
|
||||
$list[ $filename ] = $file_details_match[1];
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the final URL for the current cache busting file.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access protected
|
||||
*
|
||||
* @return string|bool URL of the file. False if the file does not exist.
|
||||
*/
|
||||
public function get_busting_url() {
|
||||
return $this->get_busting_file_url( $this->get_busting_version() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the current cache busting file.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string|bool URL of the file. False if the file does not exist.
|
||||
*/
|
||||
protected function get_busting_path() {
|
||||
return $this->get_busting_file_path( $this->get_busting_version() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the final URL for a cache busting file.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $version The file version.
|
||||
* @return string|bool URL of the file with this version. False if no versions are provided.
|
||||
*/
|
||||
private function get_busting_file_url( $version ) {
|
||||
if ( ! $version ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$filename = $this->get_busting_file_name( $version );
|
||||
|
||||
// This filter is documented in inc/functions/minify.php.
|
||||
return apply_filters( 'rocket_js_url', $this->busting_url . $filename );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the local file name.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $version The file version.
|
||||
* @return string|bool The name of the file with this version. False if no versions are provided.
|
||||
*/
|
||||
private function get_busting_file_name( $version ) {
|
||||
if ( ! $version ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return sprintf( $this->filename_pattern, $version );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the local file path.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $version The file version.
|
||||
* @return string|bool Path to the file with this version. False if no versions are provided.
|
||||
*/
|
||||
private function get_busting_file_path( $version ) {
|
||||
if ( ! $version ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->busting_path . $this->get_busting_file_name( $version );
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a file name, to be used in a regex pattern (delimiter is `/`).
|
||||
* `%s` conversion specifications are protected.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access private
|
||||
*
|
||||
* @param string $filename_pattern The file name.
|
||||
* @return string
|
||||
*/
|
||||
private function escape_file_name( $filename_pattern ) {
|
||||
return preg_quote( $filename_pattern, '/' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete busting files.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $files A list of file names.
|
||||
* @return bool True if files have been deleted (or no files have been provided). False on failure.
|
||||
*/
|
||||
private function delete_files( $files ) {
|
||||
if ( ! $files ) {
|
||||
// ¯\_(ツ)_/¯
|
||||
return true;
|
||||
}
|
||||
|
||||
$has_deleted = false;
|
||||
$error_paths = [];
|
||||
|
||||
foreach ( $files as $file_name ) {
|
||||
if ( ! $this->filesystem->delete( $this->busting_path . $file_name, false, 'f' ) ) {
|
||||
$error_paths[] = $this->busting_path . $file_name;
|
||||
} else {
|
||||
$has_deleted = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $error_paths ) {
|
||||
// Group all deletion errors into one log.
|
||||
Logger::error(
|
||||
'Local file(s) could not be deleted.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'paths' => $error_paths,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return $has_deleted;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** UPDATE THE LOCAL FILE =================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Add new contents to a file. If the file doesn't exist, it is created.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_path Path to the file to update.
|
||||
* @param string $file_contents New contents.
|
||||
* @return string|bool The file contents on success. False on failure.
|
||||
*/
|
||||
private function update_file_contents( $file_path, $file_contents ) {
|
||||
if ( ! $this->is_busting_dir_writable() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! rocket_put_content( $file_path, $file_contents ) ) {
|
||||
Logger::error(
|
||||
'Contents could not be written into file.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'path' => $file_path,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $file_contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the directory containing the busting file is writable.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_busting_dir_writable() {
|
||||
if ( ! $this->filesystem->exists( $this->busting_path ) ) {
|
||||
rocket_mkdir_p( $this->busting_path );
|
||||
}
|
||||
|
||||
if ( ! $this->filesystem->is_writable( $this->busting_path ) ) {
|
||||
Logger::error(
|
||||
'Directory is not writable.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'paths' => $this->busting_path,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** GET LOCAL/REMOTE CONTENTS =============================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get a file contents. If the file doesn't exist, new contents are fetched remotely.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_path Path to the file.
|
||||
* @param string $file_url URL to the remote file.
|
||||
* @return string|bool The contents on success, false on failure.
|
||||
*/
|
||||
private function get_file_or_remote_contents( $file_path, $file_url ) {
|
||||
$content = $this->get_file_contents( $file_path );
|
||||
|
||||
if ( $content ) {
|
||||
// We have a local file.
|
||||
return $content;
|
||||
}
|
||||
|
||||
return $this->get_remote_contents( $file_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a file contents.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_path Path to the file.
|
||||
* @return string|bool The contents on success, false on failure.
|
||||
*/
|
||||
private function get_file_contents( $file_path ) {
|
||||
if ( ! $this->filesystem->exists( $file_path ) ) {
|
||||
Logger::error(
|
||||
'Local file does not exist.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'path' => $file_path,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! $this->filesystem->is_readable( $file_path ) ) {
|
||||
Logger::error(
|
||||
'Local file is not readable.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'path' => $file_path,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$content = $this->filesystem->get_contents( $file_path );
|
||||
|
||||
if ( ! $content ) {
|
||||
Logger::error(
|
||||
'Local file is empty.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'path' => $file_path,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the contents of a URL.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $url The URL to request.
|
||||
* @return string|bool The contents on success. False on failure.
|
||||
*/
|
||||
private function get_remote_contents( $url ) {
|
||||
try {
|
||||
$response = wp_remote_get( $url );
|
||||
} catch ( Exception $e ) {
|
||||
Logger::error(
|
||||
'Remote file could not be fetched.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'url' => $url,
|
||||
'response' => $e->getMessage(),
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
Logger::error(
|
||||
'Remote file could not be fetched.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'url' => $url,
|
||||
'response' => $response->get_error_message(),
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$contents = wp_remote_retrieve_body( $response );
|
||||
|
||||
if ( ! $contents ) {
|
||||
Logger::error(
|
||||
'Remote file could not be fetched.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'url' => $url,
|
||||
'response' => $response,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
}
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Addon\FacebookTracking;
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Addon\Busting\BustingFactory;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
use WP_Rocket\Admin\Options_Data as Options;
|
||||
|
||||
/**
|
||||
* Event subscriber for Facebook tracking cache busting.
|
||||
*
|
||||
* @since 3.9 deprecated.
|
||||
* @since 3.2
|
||||
*/
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
use DeprecatedClassTrait;
|
||||
|
||||
/**
|
||||
* Name of the cron.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.2
|
||||
*/
|
||||
const CRON_NAME = 'rocket_facebook_tracking_cache_update';
|
||||
|
||||
/**
|
||||
* Instance of the Busting Factory class.
|
||||
*
|
||||
* @var BustingFactory
|
||||
* @since 3.2
|
||||
*/
|
||||
private $busting_factory;
|
||||
|
||||
/**
|
||||
* Instance of the Option_Data class.
|
||||
*
|
||||
* @var Options
|
||||
* @since 3.2
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.2
|
||||
*
|
||||
* @param BustingFactory $busting_factory Instance of the Busting Factory class.
|
||||
* @param Options $options Instance of the Options_Data class.
|
||||
*/
|
||||
public function __construct( BustingFactory $busting_factory, Options $options ) {
|
||||
self::deprecated_class( '3.9' );
|
||||
|
||||
$this->busting_factory = $busting_factory;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.2
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
$events = [
|
||||
'cron_schedules' => 'add_schedule',
|
||||
'init' => 'schedule_cache_update',
|
||||
self::CRON_NAME => 'update_cache',
|
||||
'rocket_purge_cache' => 'delete_cache',
|
||||
'rocket_buffer' => 'cache_busting_facebook_tracking',
|
||||
];
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add weekly interval to cron schedules.
|
||||
*
|
||||
* @since 3.2
|
||||
*
|
||||
* @param array $schedules An array of intervals used by cron jobs.
|
||||
* @return array
|
||||
*/
|
||||
public function add_schedule( $schedules ) {
|
||||
if ( ! $this->is_busting_active() ) {
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
$schedules['weekly'] = [
|
||||
'interval' => 604800,
|
||||
'display' => __( 'weekly', 'rocket' ),
|
||||
];
|
||||
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
/**
|
||||
* (Un)Schedule the auto-update of the cache busting files.
|
||||
*
|
||||
* @since 3.2
|
||||
*/
|
||||
public function schedule_cache_update() {
|
||||
$scheduled = wp_next_scheduled( self::CRON_NAME );
|
||||
|
||||
if ( ! $this->is_busting_active() ) {
|
||||
if ( $scheduled ) {
|
||||
wp_clear_scheduled_hook( self::CRON_NAME );
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! $scheduled ) {
|
||||
wp_schedule_event( time(), 'weekly', self::CRON_NAME );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the Facebook Pixel cache busting files.
|
||||
*
|
||||
* @since 3.2
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function update_cache() {
|
||||
if ( ! $this->is_busting_active() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$html = $this->busting_factory->type( 'fbsdk' )->refresh();
|
||||
|
||||
return $this->busting_factory->type( 'fbpix' )->refresh_all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Facebook Pixel cache busting files.
|
||||
*
|
||||
* @since 3.2
|
||||
* @since 3.6 Argument replacement.
|
||||
*
|
||||
* @param string $type Type of cache clearance: 'all', 'post', 'term', 'user', 'url'.
|
||||
* @return bool
|
||||
*/
|
||||
public function delete_cache( $type ) {
|
||||
if ( 'all' !== $type || ! $this->is_busting_active() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$html = $this->busting_factory->type( 'fbsdk' )->delete();
|
||||
|
||||
return $this->busting_factory->type( 'fbpix' )->delete_all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the cache busting on the HTML contents.
|
||||
*
|
||||
* @since 3.2
|
||||
*
|
||||
* @param string $html HTML contents.
|
||||
* @return string
|
||||
*/
|
||||
public function cache_busting_facebook_tracking( $html ) {
|
||||
if ( ! $this->is_allowed() ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$html = $this->busting_factory->type( 'fbsdk' )->replace_url( $html );
|
||||
|
||||
return $this->busting_factory->type( 'fbpix' )->replace_url( $html );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the cache busting should happen.
|
||||
*
|
||||
* @since 3.2
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_allowed() {
|
||||
if ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->is_busting_active();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the cache busting option is active.
|
||||
*
|
||||
* @since 3.2
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_busting_active() {
|
||||
return (bool) $this->options->get( 'facebook_pixel_cache', 0 );
|
||||
}
|
||||
}
|
||||
+250
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Addon\GoogleTracking;
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Addon\Busting\FileBustingTrait;
|
||||
use WP_Rocket\Busting\Abstract_Busting;
|
||||
use WP_Rocket\Logger\Logger;
|
||||
|
||||
/**
|
||||
* Manages the cache busting of the Google Analytics file.
|
||||
*
|
||||
* @since 3.9 deprecated
|
||||
* @since 3.1
|
||||
*/
|
||||
class GoogleAnalytics extends Abstract_Busting {
|
||||
use FileBustingTrait;
|
||||
use DeprecatedClassTrait;
|
||||
|
||||
/**
|
||||
* Context used for the logger.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.2.4
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
const LOGGER_CONTEXT = 'gg analytics';
|
||||
|
||||
/**
|
||||
* Google Analytics URL.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.1
|
||||
* @access protected
|
||||
* @author Remy Perona
|
||||
*/
|
||||
protected $url = 'https://www.google-analytics.com/analytics.js';
|
||||
|
||||
/**
|
||||
* File name (local).
|
||||
* %s is a "version": a md5 hash of the file contents.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.2.4
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $filename_pattern = 'ga-%s.js';
|
||||
|
||||
/**
|
||||
* Current file version (local): a md5 hash of the file contents.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.2.4
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $file_version;
|
||||
|
||||
/**
|
||||
* Flag to track the replacement.
|
||||
*
|
||||
* @var bool
|
||||
* @since 3.1
|
||||
* @access protected
|
||||
* @author Remy Perona
|
||||
*/
|
||||
protected $is_replaced = false;
|
||||
|
||||
/**
|
||||
* Filesystem object.
|
||||
*
|
||||
* @var object
|
||||
* @since 3.2.4
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $filesystem = false;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.1
|
||||
* @access public
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $busting_path Path to the busting directory.
|
||||
* @param string $busting_url URL of the busting directory.
|
||||
*/
|
||||
public function __construct( $busting_path, $busting_url ) {
|
||||
self::deprecated_class( '3.9' );
|
||||
|
||||
$this->busting_path = $busting_path . 'google-tracking/';
|
||||
$this->busting_url = $busting_url . 'google-tracking/';
|
||||
$this->filesystem = rocket_direct_filesystem();
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PUBLIC METHODS ========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Performs the replacement process.
|
||||
*
|
||||
* @since 3.1
|
||||
* @access public
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function replace_url( $html ) {
|
||||
$this->is_replaced = false;
|
||||
|
||||
$tag = $this->find( '<script\s*(?<attr>[^>]*)?>(?<content>.*)?<\/script>', $html );
|
||||
|
||||
if ( ! $tag ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
Logger::info(
|
||||
'GOOGLE ANALYTICS CACHING PROCESS STARTED.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'tag' => $tag,
|
||||
]
|
||||
);
|
||||
|
||||
if ( ! $this->save( $this->url ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$replace_tag = preg_replace( '/(?:https?:)?\/\/www\.google-analytics\.com\/analytics\.js/i', $this->get_busting_url(), $tag );
|
||||
$html = str_replace( $tag, $replace_tag, $html );
|
||||
|
||||
$this->is_replaced = true;
|
||||
|
||||
Logger::info(
|
||||
'Google Analytics caching process succeeded.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'file' => $this->get_busting_path(),
|
||||
]
|
||||
);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the replacement was sucessful or not.
|
||||
*
|
||||
* @since 3.1
|
||||
* @access public
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_replaced() {
|
||||
return $this->is_replaced;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the content of the URL to cache to the busting file.
|
||||
*
|
||||
* @since 3.2.4
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $url URL to get the content from.
|
||||
* @return bool
|
||||
*/
|
||||
public function refresh_save( $url ) {
|
||||
// Before doing anything, make sure the busting file can be created.
|
||||
if ( ! $this->is_busting_dir_writable() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get remote content.
|
||||
$content = $this->get_remote_contents( $url );
|
||||
|
||||
if ( ! $content ) {
|
||||
// Could not get the remote contents.
|
||||
return false;
|
||||
}
|
||||
|
||||
$version = md5( $content );
|
||||
$path = $this->get_busting_file_path( $version );
|
||||
|
||||
return $this->update_file_contents( $path, $content );
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** REMOTE FILE ============================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the Google Analytics URL.
|
||||
*
|
||||
* @since 3.1
|
||||
* @access public
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_url() {
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** VARIOUS INTERNAL TOOLS ================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Searches for element(s) in the DOM.
|
||||
*
|
||||
* @since 3.1
|
||||
* @access public
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $pattern Pattern to match.
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
protected function find( $pattern, $html ) {
|
||||
preg_match_all( '/' . $pattern . '/si', $html, $all_matches, PREG_SET_ORDER );
|
||||
|
||||
$matches = array_map(
|
||||
function( $match ) {
|
||||
|
||||
if (
|
||||
! preg_match( '/src\s*=\s*[\'"]\s*(?:https?:)?\/\/www\.google-analytics\.com\/analytics\.js\s*[\'"]/i', $match['attr'] . $match['content'] )
|
||||
&&
|
||||
false === strpos( $match['content'], 'GoogleAnalyticsObject' )
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
return $match[0];
|
||||
},
|
||||
$all_matches
|
||||
);
|
||||
|
||||
$matches = array_values( array_filter( $matches ) );
|
||||
|
||||
if ( ! $matches ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $matches[0];
|
||||
}
|
||||
}
|
||||
+205
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Addon\GoogleTracking;
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Addon\Busting\FileBustingTrait;
|
||||
use WP_Rocket\Logger\Logger;
|
||||
use WP_Rocket\Busting\Abstract_Busting;
|
||||
use WP_Filesystem_Direct;
|
||||
|
||||
/**
|
||||
* Manages the cache busting of the Google Tag Manager file
|
||||
*
|
||||
* @since 3.9 deprecated.
|
||||
* @since 3.1
|
||||
*/
|
||||
class GoogleTagManager extends Abstract_Busting {
|
||||
use FileBustingTrait;
|
||||
use DeprecatedClassTrait;
|
||||
|
||||
/**
|
||||
* Context used for the logger.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.2.4
|
||||
*/
|
||||
const LOGGER_CONTEXT = 'gg tag manager';
|
||||
|
||||
/**
|
||||
* File name (local).
|
||||
* %s is a "version": a md5 hash of the file contents.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.2.4
|
||||
* @access protected
|
||||
*/
|
||||
protected $filename_pattern = 'gtm-%s.js';
|
||||
|
||||
/**
|
||||
* Current file version (local): a md5 hash of the file contents.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.2.4
|
||||
* @access protected
|
||||
*/
|
||||
protected $file_version;
|
||||
|
||||
/**
|
||||
* Filesystem object.
|
||||
*
|
||||
* @var object
|
||||
* @since 3.2.4
|
||||
* @access protected
|
||||
*/
|
||||
protected $filesystem = false;
|
||||
|
||||
/**
|
||||
* Google Analytics object.
|
||||
*
|
||||
* @var object
|
||||
* @since 3.2.4
|
||||
* @access protected
|
||||
*/
|
||||
protected $ga_busting = false;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.1
|
||||
* @access public
|
||||
*
|
||||
* @param string $busting_path Path to the busting directory.
|
||||
* @param string $busting_url URL of the busting directory.
|
||||
* @param GoogleAnalytics $ga_busting A GoogleAnalytics instance.
|
||||
* @param WP_Filesystem_Direct $filesystem Instance of the filesystem handler.
|
||||
*/
|
||||
public function __construct( $busting_path, $busting_url, GoogleAnalytics $ga_busting, $filesystem = null ) {
|
||||
self::deprecated_class( '3.9' );
|
||||
|
||||
$blog_id = get_current_blog_id();
|
||||
$this->busting_path = $busting_path . $blog_id . '/';
|
||||
$this->busting_url = $busting_url . $blog_id . '/';
|
||||
$this->ga_busting = $ga_busting;
|
||||
$this->filesystem = is_null( $filesystem ) ? rocket_direct_filesystem() : $filesystem;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PUBLIC METHODS ========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Performs the replacement process.
|
||||
*
|
||||
* @since 3.1
|
||||
* @access public
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function replace_url( $html ) {
|
||||
$script = $this->find( '<script(\s+[^>]+)?\s+src\s*=\s*[\'"]\s*?((?:https?:)?\/\/www\.googletagmanager\.com(?:.+)?)\s*?[\'"]([^>]+)?\/?>', $html );
|
||||
|
||||
if ( ! $script ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
// replace relative protocol // with full https://.
|
||||
$gtm_url = preg_replace( '/^\/\//', 'https://', $script[2] );
|
||||
|
||||
Logger::info(
|
||||
'GOOGLE TAG MANAGER CACHING PROCESS STARTED.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'tag' => $script,
|
||||
]
|
||||
);
|
||||
|
||||
if ( ! $this->save( $gtm_url ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$replace_script = str_replace( $script[2], $this->get_busting_url(), $script[0] );
|
||||
$replace_script = str_replace( '<script', '<script data-no-minify="1"', $replace_script );
|
||||
$html = str_replace( $script[0], $replace_script, $html );
|
||||
|
||||
Logger::info(
|
||||
'Google Tag Manager caching process succeeded.',
|
||||
[
|
||||
self::LOGGER_CONTEXT,
|
||||
'file' => $this->get_busting_path(),
|
||||
]
|
||||
);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the content of the URL to cache to the busting file.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
*
|
||||
* @param string $url URL to get the content from.
|
||||
* @return bool
|
||||
*/
|
||||
public function refresh_save( $url ) {
|
||||
// Before doing anything, make sure the busting file can be created.
|
||||
if ( ! $this->is_busting_dir_writable() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get remote content.
|
||||
$content = $this->get_remote_contents( $url );
|
||||
|
||||
if ( ! $content ) {
|
||||
// Could not get the remote contents.
|
||||
return false;
|
||||
}
|
||||
|
||||
$version = md5( $content );
|
||||
$path = $this->get_busting_file_path( $version );
|
||||
$content = $this->replace_ga_url( $content );
|
||||
|
||||
return $this->update_file_contents( $path, $content );
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** VARIOUS INTERNAL TOOLS ================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Searches for element(s) in the DOM.
|
||||
*
|
||||
* @since 3.1
|
||||
* @access public
|
||||
*
|
||||
* @param string $pattern Pattern to match.
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
protected function find( $pattern, $html ) {
|
||||
preg_match_all( '/' . $pattern . '/Umsi', $html, $matches, PREG_SET_ORDER );
|
||||
|
||||
if ( empty( $matches ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the Google Analytics URL by the local copy inside the gtm-local.js file content
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param string $content JavaScript content.
|
||||
* @return string
|
||||
*/
|
||||
protected function replace_ga_url( $content ) {
|
||||
if ( ! $this->ga_busting->save( $this->ga_busting->get_url() ) ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return str_replace( $this->ga_busting->get_url(), $this->ga_busting->get_busting_url(), $content );
|
||||
}
|
||||
}
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Addon\GoogleTracking;
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
use WP_Rocket\Addon\Busting\BustingFactory;
|
||||
use WP_Rocket\Admin\Options_Data as Options;
|
||||
|
||||
/**
|
||||
* Event subscriber for Google tracking cache busting
|
||||
*
|
||||
* @since 3.9 deprecated.
|
||||
* @since 3.1
|
||||
*/
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
use DeprecatedClassTrait;
|
||||
|
||||
/**
|
||||
* Instance of the Busting Factory class
|
||||
*
|
||||
* @var BustingFactory
|
||||
*/
|
||||
private $busting_factory;
|
||||
|
||||
/**
|
||||
* Instance of the Option_Data class
|
||||
*
|
||||
* @var Options
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param BustingFactory $busting_factory Instance of the Busting Factory class.
|
||||
* @param Options $options Instance of the Option_Data class.
|
||||
*/
|
||||
public function __construct( BustingFactory $busting_factory, Options $options ) {
|
||||
self::deprecated_class( '3.9' );
|
||||
|
||||
$this->busting_factory = $busting_factory;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
$events = [
|
||||
'cron_schedules' => 'add_schedule',
|
||||
'init' => 'schedule_tracking_cache_update',
|
||||
'rocket_google_tracking_cache_update' => 'update_tracking_cache',
|
||||
'rocket_purge_cache' => 'delete_tracking_cache',
|
||||
'rocket_buffer' => 'cache_busting_google_tracking',
|
||||
];
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the cache busting on the HTML content
|
||||
*
|
||||
* Google Analytics replacement is performed first, and if no replacement occured, Google Tag Manager replacement is performed.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function cache_busting_google_tracking( $html ) {
|
||||
if ( ! $this->is_allowed() ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$processor = $this->busting_factory->type( 'ga' );
|
||||
$html = $processor->replace_url( $html );
|
||||
|
||||
$processor = $this->busting_factory->type( 'gtm' );
|
||||
$html = $processor->replace_url( $html );
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules the auto-update of Google Analytics cache busting file
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function schedule_tracking_cache_update() {
|
||||
if ( ! $this->is_busting_active() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! wp_next_scheduled( 'rocket_google_tracking_cache_update' ) ) {
|
||||
wp_schedule_event( time(), 'weekly', 'rocket_google_tracking_cache_update' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates Google Analytics cache busting file
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function update_tracking_cache() {
|
||||
if ( ! $this->is_busting_active() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$processor = $this->busting_factory->type( 'ga' );
|
||||
|
||||
return $processor->refresh_save( $processor->get_url() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds weekly interval to cron schedules
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param Array $schedules An array of intervals used by cron jobs.
|
||||
* @return Array
|
||||
*/
|
||||
public function add_schedule( $schedules ) {
|
||||
if ( ! $this->is_busting_active() ) {
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
$schedules['weekly'] = [
|
||||
'interval' => 604800,
|
||||
'display' => __( 'weekly', 'rocket' ),
|
||||
];
|
||||
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the GA busting file.
|
||||
*
|
||||
* @since 3.1
|
||||
* @since 3.6 Argument replacement.
|
||||
*
|
||||
* @param string $type Type of cache clearance: 'all', 'post', 'term', 'user', 'url'.
|
||||
* @return bool
|
||||
*/
|
||||
public function delete_tracking_cache( $type ) {
|
||||
if ( 'all' !== $type || ! $this->is_busting_active() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->busting_factory->type( 'gtm' )->delete();
|
||||
|
||||
return $this->busting_factory->type( 'ga' )->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the cache busting should happen
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
private function is_allowed() {
|
||||
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->is_busting_active();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the cache busting option is active.
|
||||
*
|
||||
* @since 3.6
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_busting_active() {
|
||||
return (bool) $this->options->get( 'google_analytics_cache', 0 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
namespace WP_Rocket\deprecated\Engine\Media\Embeds;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
use WP_Rocket\ThirdParty\ReturnTypesTrait;
|
||||
|
||||
/**
|
||||
* Event subscriber to control Embeds behavior.
|
||||
*
|
||||
* @since 3.10 deprecated
|
||||
* @since 3.7 Moved to new architecture.
|
||||
* @since 3.2
|
||||
*/
|
||||
class EmbedsSubscriber implements Subscriber_Interface {
|
||||
use DeprecatedClassTrait;
|
||||
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 ) {
|
||||
self::deprecated_class( '3.10' );
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
}
|
||||
}
|
||||
+365
@@ -0,0 +1,365 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Engine\Optimization\QueryString;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Engine\Optimization\AbstractOptimization;
|
||||
use WP_Rocket\Engine\Optimization\CSSTrait;
|
||||
|
||||
/**
|
||||
* Remove query string from static resources.
|
||||
*
|
||||
* @since 3.1
|
||||
* @since 3.6 Deprecated.
|
||||
* @author Remy Perona
|
||||
* @deprecated
|
||||
*/
|
||||
class Remove extends AbstractOptimization {
|
||||
use DeprecatedClassTrait;
|
||||
use CSSTrait;
|
||||
|
||||
/**
|
||||
* Plugin options instance.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* Cache busting base path
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $busting_path;
|
||||
|
||||
/**
|
||||
* Cache busting base URL
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $busting_url;
|
||||
|
||||
/**
|
||||
* Excluded files from optimization
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $excluded_files;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param Options_Data $options Plugin options instance.
|
||||
* @param string $busting_path Base cache busting files path.
|
||||
* @param string $busting_url Base cache busting files URL.
|
||||
*/
|
||||
public function __construct( Options_Data $options, $busting_path, $busting_url ) {
|
||||
self::deprecated_class( '3.6' );
|
||||
$this->options = $options;
|
||||
$this->busting_path = $busting_path . get_current_blog_id() . '/';
|
||||
$this->busting_url = $busting_url . get_current_blog_id() . '/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a regex-ready string with the excluded filepaths for the Remove Query Strings option
|
||||
*
|
||||
* @since 3.3.3
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_excluded_files() {
|
||||
static $excluded_files;
|
||||
|
||||
if ( isset( $excluded_files ) ) {
|
||||
return $excluded_files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters files to exclude from cache busting
|
||||
*
|
||||
* @since 2.9.3
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $excluded_files An array of filepath to exclude.
|
||||
*/
|
||||
$excluded_files = apply_filters( 'rocket_exclude_cache_busting', [] );
|
||||
|
||||
if ( empty( $excluded_files ) ) {
|
||||
$excluded_files = '';
|
||||
|
||||
return $excluded_files;
|
||||
}
|
||||
|
||||
foreach ( $excluded_files as $i => $excluded_file ) {
|
||||
// Escape character for future use in regex pattern.
|
||||
$excluded_files[ $i ] = str_replace( '#', '\#', $excluded_file );
|
||||
}
|
||||
|
||||
$excluded_files = implode( '|', $excluded_files );
|
||||
|
||||
return $excluded_files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove query strings for CSS files that have one
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function remove_query_strings_css( $html ) {
|
||||
$html_nocomments = preg_replace( '/<!--(.*)-->/Uis', '', $html );
|
||||
$styles = $this->find( '<link\s+([^>]+[\s\'"])?href\s*=\s*[\'"]\s*?([^\'"]+\.css(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>', $html_nocomments );
|
||||
|
||||
if ( ! $styles ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
foreach ( $styles as $style ) {
|
||||
$url = $style[2];
|
||||
|
||||
$url = $this->can_replace( $url );
|
||||
|
||||
if ( ! $url ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$optimized_url = $this->replace_url( $url, 'css' );
|
||||
|
||||
if ( ! $optimized_url ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$replace_style = str_replace( $style[2], $optimized_url, $style[0] );
|
||||
$html = str_replace( $style[0], $replace_style, $html );
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove query strings for JS files that have one
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function remove_query_strings_js( $html ) {
|
||||
$html_nocomments = $this->hide_comments( $html );
|
||||
$scripts = $this->find( '<script\s+([^>]+[\s\'"])?src\s*=\s*[\'"]\s*?([^\'"]+\.js(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>', $html_nocomments );
|
||||
|
||||
if ( ! $scripts ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
foreach ( $scripts as $script ) {
|
||||
$url = $script[2];
|
||||
|
||||
$url = $this->can_replace( $url );
|
||||
|
||||
if ( ! $url ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$optimized_url = $this->replace_url( $url, 'js' );
|
||||
|
||||
if ( ! $optimized_url ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$replace_script = str_replace( $script[2], $optimized_url, $script[0] );
|
||||
$html = str_replace( $script[0], $replace_script, $html );
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the CDN zones.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_zones() {
|
||||
return [ 'all', 'css_and_js', 'css', 'js' ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if we can optimize
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_allowed() {
|
||||
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! $this->options->get( 'remove_query_strings' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if we can perform the remove query string on that URL
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $url source URL.
|
||||
* @return bool\string
|
||||
*/
|
||||
protected function can_replace( $url ) {
|
||||
$parsed_url = get_rocket_parse_url( $url );
|
||||
|
||||
if ( empty( $parsed_url['query'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$version = get_bloginfo( 'version' );
|
||||
|
||||
if ( false !== strpos( $url, 'ver=' . $version ) ) {
|
||||
$url = rtrim( str_replace( [ 'ver=' . $version, '?&', '&&' ], [ '', '?', '&' ], $url ), '?&' );
|
||||
}
|
||||
|
||||
if ( $this->is_external_file( $url ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $this->is_excluded( $url ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the URL is excluded
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $url source URL.
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_excluded( $url ) {
|
||||
$excluded_files = $this->get_excluded_files();
|
||||
|
||||
if ( ! empty( $excluded_files ) && preg_match( '#^' . $excluded_files . '$#', rocket_clean_exclude_file( $url ) ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the original URL with the cache busting URL
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $url source URL.
|
||||
* @param string $extension file extension.
|
||||
* @return bool|string
|
||||
*/
|
||||
protected function replace_url( $url, $extension ) {
|
||||
$query = wp_parse_url( $url, PHP_URL_QUERY );
|
||||
|
||||
if ( empty( $query ) ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
// This filter is documented in /inc/classes/optimization/class-abstract-optimization.php.
|
||||
$internal_url = apply_filters( 'rocket_asset_url', $url, $this->get_zones() );
|
||||
$parsed_url = get_rocket_parse_url( $internal_url );
|
||||
$relative_src = ltrim( $parsed_url['path'] . '?' . $parsed_url['query'], '/' );
|
||||
$filename = preg_replace( '/\.(' . $extension . ')\?(?:timestamp|ver)=([^&]+)(?:.*)/', '-$2.$1', $relative_src );
|
||||
|
||||
if ( $relative_src === $filename ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$busting_file = $this->busting_path . $filename;
|
||||
$busting_url = $this->get_busting_url( $filename, $extension, $url );
|
||||
|
||||
if ( rocket_direct_filesystem()->is_readable( $busting_file ) ) {
|
||||
return $busting_url;
|
||||
}
|
||||
|
||||
$file = $this->get_file_path( $url );
|
||||
|
||||
if ( ! $file ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$busting_content = $this->get_file_content( $file );
|
||||
|
||||
if ( ! $busting_content ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( 'css' === $extension ) {
|
||||
$busting_content = $this->rewrite_paths( $file, $busting_file, $busting_content );
|
||||
}
|
||||
|
||||
if ( ! $this->write_file( $busting_content, $busting_file ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $busting_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the cache busting URL
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $filename Cache busting filename.
|
||||
* @param string $extension File extension.
|
||||
* @param string $original_url Original URL for the file.
|
||||
* @return string
|
||||
*/
|
||||
protected function get_busting_url( $filename, $extension, $original_url ) {
|
||||
$url = $this->busting_url . $filename;
|
||||
|
||||
switch ( $extension ) {
|
||||
case 'css':
|
||||
// This filter is documented in inc/classes/optimization/css/class-abstract-css-optimization.php.
|
||||
$url = apply_filters( 'rocket_css_url', $url, $original_url );
|
||||
break;
|
||||
case 'js':
|
||||
// This filter is documented in inc/classes/optimization/css/class-abstract-js-optimization.php.
|
||||
$url = apply_filters( 'rocket_js_url', $url, $original_url );
|
||||
break;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Engine\Optimization\QueryString;
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
/**
|
||||
* Hooks into WordPress to remove query strings for static files.
|
||||
*
|
||||
* @since 3.1
|
||||
* @since 3.6 Deprecated.
|
||||
* @author Remy Perona
|
||||
* @deprecated
|
||||
*/
|
||||
class RemoveSubscriber implements Subscriber_Interface {
|
||||
use DeprecatedClassTrait;
|
||||
|
||||
/**
|
||||
* Remove Query String instance.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @var Remove
|
||||
*/
|
||||
protected $remove_query_string;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param Remove $remove_query_string Remove Query String instance.
|
||||
*/
|
||||
public function __construct( Remove $remove_query_string ) {
|
||||
self::deprecated_class( '3.6' );
|
||||
$this->remove_query_string = $remove_query_string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'rocket_buffer' => [ 'process', 30 ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the HTML to fetch static files with a query string and remove it
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function process( $html ) {
|
||||
if ( ! $this->is_allowed() ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$html = $this->remove_query_string->remove_query_strings_css( $html );
|
||||
$html = $this->remove_query_string->remove_query_strings_js( $html );
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is allowed to remove query strings for static files.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_allowed() {
|
||||
return $this->remove_query_string->is_allowed();
|
||||
}
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Busting;
|
||||
|
||||
/**
|
||||
* Abstract class for assets busting
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*/
|
||||
abstract class Abstract_Busting {
|
||||
/**
|
||||
* Cache busting files base path
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $busting_path;
|
||||
|
||||
/**
|
||||
* Cache busting base URL
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $busting_url;
|
||||
|
||||
/**
|
||||
* Filename for the cache busting file.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $filename;
|
||||
|
||||
/**
|
||||
* Gets the content of an URL
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $url The URL to request.
|
||||
* @return string|bool
|
||||
*/
|
||||
protected function get_file_content( $url ) {
|
||||
$content = wp_remote_retrieve_body( wp_remote_get( $url ) );
|
||||
|
||||
if ( ! $content ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the content of the URL to bust to the busting file if it doesn't exist yet.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $url URL to get the content from.
|
||||
* @return bool
|
||||
*/
|
||||
public function save( $url ) {
|
||||
$path = $this->busting_path . $this->filename;
|
||||
|
||||
if ( \rocket_direct_filesystem()->exists( $path ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->refresh_save( $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the content of the URL to bust to the busting file.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $url URL to get the content from.
|
||||
* @return bool
|
||||
*/
|
||||
public function refresh_save( $url ) {
|
||||
$path = $this->busting_path . $this->filename;
|
||||
$content = $this->get_file_content( $url );
|
||||
|
||||
if ( ! $content ) {
|
||||
// If a previous version is present, it is kept in place.
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! \rocket_direct_filesystem()->exists( $this->busting_path ) ) {
|
||||
\rocket_mkdir_p( $this->busting_path );
|
||||
}
|
||||
|
||||
if ( ! \rocket_put_content( $path, $content ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the final URL for the cache busting file.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_busting_url() {
|
||||
// This filter is documented in inc/functions/minify.php.
|
||||
return apply_filters( 'rocket_js_url', $this->busting_url . $this->filename );
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the replacement process.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
abstract public function replace_url( $html );
|
||||
|
||||
/**
|
||||
* Searches for element(s) in the DOM
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $pattern Pattern to match.
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function find( $pattern, $html );
|
||||
}
|
||||
+1214
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,678 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Busting;
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Logger\Logger;
|
||||
|
||||
/**
|
||||
* Manages the cache busting of the Facebook SDK file.
|
||||
*
|
||||
* @since 3.9 deprecated.
|
||||
* @since 3.2
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Facebook_SDK extends Abstract_Busting {
|
||||
use DeprecatedClassTrait;
|
||||
|
||||
/**
|
||||
* Facebook SDK URL.
|
||||
* %s is a locale like "en_US".
|
||||
*
|
||||
* @var string
|
||||
* @since 3.2
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $url = 'https://connect.facebook.net/%s/sdk.js';
|
||||
|
||||
/**
|
||||
* Filename for the cache busting file.
|
||||
* %s is a locale like "en_US".
|
||||
*
|
||||
* @var string
|
||||
* @since 3.2
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $filename = 'fbsdk-%s.js';
|
||||
|
||||
/**
|
||||
* Flag to track the replacement.
|
||||
*
|
||||
* @var bool
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $is_replaced = false;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $busting_path Path to the busting directory.
|
||||
* @param string $busting_url URL of the busting directory.
|
||||
*/
|
||||
public function __construct( $busting_path, $busting_url ) {
|
||||
self::deprecated_class( '3.9' );
|
||||
|
||||
/** Warning: the file name and script URL are dynamic, and must be run through sprintf(). */
|
||||
$this->busting_path = $busting_path . 'facebook-tracking/';
|
||||
$this->busting_url = $busting_url . 'facebook-tracking/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the URL replacement process.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $html HTML contents.
|
||||
* @return string HTML contents.
|
||||
*/
|
||||
public function replace_url( $html ) {
|
||||
$this->is_replaced = false;
|
||||
|
||||
$tag = $this->find( '<script[^>]*?>(.*)<\/script>', $html );
|
||||
|
||||
if ( ! $tag ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
Logger::info(
|
||||
'FACEBOOK SDK CACHING PROCESS STARTED.',
|
||||
[
|
||||
'fb sdk',
|
||||
'tag' => $tag,
|
||||
]
|
||||
);
|
||||
|
||||
$locale = $this->get_locale_from_url( $tag );
|
||||
$remote_url = $this->get_url( $locale );
|
||||
|
||||
if ( ! $this->save( $remote_url ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$file_url = $this->get_busting_file_url( $locale );
|
||||
$replace_tag = preg_replace( '@(?:https?:)?//connect\.facebook\.net/[a-zA-Z_-]+/sdk\.js@i', $file_url, $tag, -1, $count );
|
||||
|
||||
if ( ! $count || false === strpos( $html, $tag ) ) {
|
||||
Logger::error( 'The local file URL could not be replaced in the page contents.', [ 'fb sdk' ] );
|
||||
return $html;
|
||||
}
|
||||
|
||||
$html = str_replace( $tag, $replace_tag, $html );
|
||||
$file_path = $this->get_busting_file_path( $locale );
|
||||
$xfbml = $this->get_xfbml_from_url( $tag ); // Default value should be set to false.
|
||||
$app_id = $this->get_appId_from_url( $tag ); // APP_ID is the only required value.
|
||||
$url_version = $this->get_version_from_url( $tag );
|
||||
$version = false === $url_version ? 'v5.0' : $url_version; // If version is not available set it to the latest: v.5.0.
|
||||
|
||||
if ( false !== $app_id ) {
|
||||
// Add FB async init.
|
||||
$fb_async_script = '<script>window.fbAsyncInit = function fbAsyncInit () {FB.init({appId: \'' . $app_id . '\',xfbml: ' . $xfbml . ',version: \'' . $version . '\'})}</script>';
|
||||
$html = str_replace( '</body>', $fb_async_script . '</body>', $html );
|
||||
}
|
||||
|
||||
$this->is_replaced = true;
|
||||
|
||||
/**
|
||||
* Triggered once the Facebook SDK URL has been replaced in the page contents.
|
||||
*
|
||||
* @since 3.2
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_url URL of the local main file.
|
||||
* @param string $file_path Path to the local file.
|
||||
*/
|
||||
do_action( 'rocket_after_facebook_sdk_url_replaced', $file_url, $file_path );
|
||||
|
||||
Logger::info(
|
||||
'Facebook SDK caching process succeeded.',
|
||||
[
|
||||
'fb sdk',
|
||||
'file' => $file_path,
|
||||
]
|
||||
);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the replacement was sucessful or not.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_replaced() {
|
||||
return $this->is_replaced;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** GRAB/MANIPULATE DATA IN CONTENTS ======================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Search for an element in the DOM.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $pattern Pattern to match.
|
||||
* @param string $html HTML contents.
|
||||
* @return string|bool The matched HTML on success. False if nothing is found.
|
||||
*/
|
||||
protected function find( $pattern, $html ) {
|
||||
preg_match_all( '/' . $pattern . '/Umsi', $html, $matches, PREG_SET_ORDER );
|
||||
|
||||
if ( empty( $matches ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( $matches as $match ) {
|
||||
if ( trim( $match[1] ) && preg_match( '@//connect\.facebook\.net/[a-zA-Z_-]+/sdk\.js@i', $match[1] ) ) {
|
||||
return $match[0];
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** UPDATE/SAVE A LOCAL FILE ================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Save the contents of a URL into a local file if it doesn't exist yet.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $url URL to get the contents from.
|
||||
* @return bool True on success. False on failure.
|
||||
*/
|
||||
public function save( $url ) {
|
||||
$locale = $this->get_locale_from_url( $url );
|
||||
$path = $this->get_busting_file_path( $locale );
|
||||
|
||||
if ( \rocket_direct_filesystem()->exists( $path ) ) {
|
||||
// If a previous version is present, keep it.
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->refresh_save( $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the contents of a URL into a local file.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $url URL to get the contents from.
|
||||
* @return bool True on success. False on failure.
|
||||
*/
|
||||
public function refresh_save( $url ) {
|
||||
$content = $this->get_file_content( $url );
|
||||
|
||||
if ( ! $content ) {
|
||||
// Error, we couldn't fetch the file contents.
|
||||
return false;
|
||||
}
|
||||
|
||||
$locale = $this->get_locale_from_url( $url );
|
||||
$path = $this->get_busting_file_path( $locale );
|
||||
|
||||
return (bool) $this->update_file_contents( $path, $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new contents to a file. If the file doesn't exist, it is created.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_path Path to the file to update.
|
||||
* @param string $file_contents New contents.
|
||||
* @return string|bool The file contents on success. False on failure.
|
||||
*/
|
||||
private function update_file_contents( $file_path, $file_contents ) {
|
||||
if ( ! \rocket_direct_filesystem()->exists( $this->busting_path ) ) {
|
||||
\rocket_mkdir_p( $this->busting_path );
|
||||
}
|
||||
|
||||
if ( ! \rocket_put_content( $file_path, $file_contents ) ) {
|
||||
Logger::error(
|
||||
'Contents could not be written into file.',
|
||||
[
|
||||
'fb sdk',
|
||||
'path' => $file_path,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggered once a file contents have been updated.
|
||||
*
|
||||
* @since 3.2
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_path Path to the file to update.
|
||||
* @param string $file_contents The file contents.
|
||||
*/
|
||||
do_action( 'rocket_after_facebook_sdk_file_updated', $file_path, $file_contents );
|
||||
|
||||
return $file_contents;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PUBLIC BULK ACTIONS ON LOCAL FILES ====================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Look for existing local files and update their contents if there's a new version available.
|
||||
* Actually, if a more recent version exists on the FB side, it will delete all local files and hit the home page to recreate them.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True on success. False on failure.
|
||||
*/
|
||||
public function refresh() {
|
||||
$files = $this->get_files();
|
||||
|
||||
if ( ! $files ) {
|
||||
// No files (or there's an error).
|
||||
return false !== $files;
|
||||
}
|
||||
|
||||
$error_paths = [];
|
||||
$pattern = $this->escape_file_name( $this->filename );
|
||||
$pattern = sprintf( $pattern, '(?<locale>[a-zA-Z_-]+)' );
|
||||
|
||||
foreach ( $files as $file ) {
|
||||
preg_match( '/^' . $pattern . '$/', $file, $matches );
|
||||
|
||||
$remote_url = $this->get_url( $matches['locale'] );
|
||||
|
||||
if ( ! $this->refresh_save( $remote_url ) ) {
|
||||
$error_paths[] = $this->get_busting_file_path( $matches['locale'] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $error_paths ) {
|
||||
Logger::error(
|
||||
'Local file(s) could not be updated.',
|
||||
[
|
||||
'fb sdk',
|
||||
'paths' => $error_paths,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggered once all local files have been updated (or not).
|
||||
*
|
||||
* @since 3.2
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $files An array of file names.
|
||||
* @param array $error_paths Paths to the files that couldn't be updated. An empty array if everything is fine.
|
||||
*/
|
||||
do_action( 'rocket_after_facebook_sdk_files_refresh', $files, $error_paths );
|
||||
|
||||
return ! $error_paths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all Facebook SDK busting files.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True on success. False on failure.
|
||||
*/
|
||||
public function delete() {
|
||||
$filesystem = \rocket_direct_filesystem();
|
||||
$files = $this->get_files();
|
||||
|
||||
if ( ! $files ) {
|
||||
// No files (or there's an error).
|
||||
return false !== $files;
|
||||
}
|
||||
|
||||
$error_paths = [];
|
||||
|
||||
foreach ( $files as $file_name ) {
|
||||
if ( ! $filesystem->delete( $this->busting_path . $file_name, false, 'f' ) ) {
|
||||
$error_paths[] = $this->busting_path . $file_name;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $error_paths ) {
|
||||
Logger::error(
|
||||
'Local file(s) could not be deleted.',
|
||||
[
|
||||
'fb sdk',
|
||||
'paths' => $error_paths,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggered once all local files have been deleted (or not).
|
||||
*
|
||||
* @since 3.2
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $files An array of file names.
|
||||
* @param array $error_paths Paths to the files that couldn't be deleted. An empty array if everything is fine.
|
||||
*/
|
||||
do_action( 'rocket_after_facebook_sdk_files_deleted', $files, $error_paths );
|
||||
|
||||
return ! $error_paths;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** SCAN FOR LOCAL FILES ==================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get all cached files in the directory.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array|bool A list of file names. False on failure.
|
||||
*/
|
||||
private function get_files() {
|
||||
$filesystem = \rocket_direct_filesystem();
|
||||
$dir_path = rtrim( $this->busting_path, '\\/' );
|
||||
|
||||
if ( ! $filesystem->exists( $dir_path ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ( ! $filesystem->is_writable( $dir_path ) ) {
|
||||
Logger::error(
|
||||
'Directory is not writable.',
|
||||
[
|
||||
'fb sdk',
|
||||
'path' => $dir_path,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$dir = $filesystem->dirlist( $dir_path );
|
||||
|
||||
if ( false === $dir ) {
|
||||
Logger::error(
|
||||
'Could not get the directory contents.',
|
||||
[
|
||||
'fb sdk',
|
||||
'path' => $dir_path,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! $dir ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$list = [];
|
||||
$pattern = $this->escape_file_name( $this->filename );
|
||||
$pattern = sprintf( $pattern, '[a-zA-Z_-]+' );
|
||||
|
||||
foreach ( $dir as $entry ) {
|
||||
if ( 'f' !== $entry['type'] ) {
|
||||
continue;
|
||||
}
|
||||
if ( preg_match( '/^' . $pattern . '$/', $entry['name'], $matches ) ) {
|
||||
$list[ $entry['name'] ] = $entry['name'];
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** REMOTE SDK FILE ========================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the remote Facebook SDK URL.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $locale A locale string, like 'en_US'.
|
||||
* @return string
|
||||
*/
|
||||
public function get_url( $locale ) {
|
||||
return sprintf( $this->url, $locale );
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the locale from a URL to bust.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $url Any string containing the URL to bust.
|
||||
* @return string|bool The locale on success. False on failure.
|
||||
*/
|
||||
private function get_locale_from_url( $url ) {
|
||||
$pattern = '@//connect\.facebook\.net/(?<locale>[a-zA-Z_-]+)/sdk\.js@i';
|
||||
|
||||
if ( ! preg_match( $pattern, $url, $matches ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $matches['locale'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract XFBML from a URL to bust.
|
||||
*
|
||||
* @since 3.4.3
|
||||
* @access private
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param string $url Any string containing the URL to bust.
|
||||
* @return string|bool The XFBML on success. False on failure.
|
||||
*/
|
||||
private function get_xfbml_from_url( $url ) {
|
||||
$pattern = '@//connect\.facebook\.net/(?<locale>[a-zA-Z_-]+)/sdk\.js#(?:.+&)?xfbml=(?<xfbml>[0-9]+)@i';
|
||||
|
||||
if ( ! preg_match( $pattern, $url, $matches ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $matches['xfbml'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract appId from a URL to bust.
|
||||
*
|
||||
* @since 3.4.3
|
||||
* @access private
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param string $url Any string containing the URL to bust.
|
||||
* @return string|bool The appId on success. False on failure.
|
||||
*/
|
||||
private function get_appId_from_url( $url ) {
|
||||
$pattern = '@//connect\.facebook\.net/(?<locale>[a-zA-Z_-]+)/sdk\.js#(?:.+&)?appId=(?<appId>[0-9]+)@i';
|
||||
|
||||
if ( ! preg_match( $pattern, $url, $matches ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $matches['appId'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract version from a URL to bust.
|
||||
*
|
||||
* @since 3.4.3
|
||||
* @access private
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param string $url Any string containing the URL to bust.
|
||||
* @return string|bool The version on success. False on failure.
|
||||
*/
|
||||
private function get_version_from_url( $url ) {
|
||||
$pattern = '@//connect\.facebook\.net/(?<locale>[a-zA-Z_-]+)/sdk\.js#(?:.+&)?version=(?<version>[a-zA-Z0-9.]+)@i';
|
||||
|
||||
if ( ! preg_match( $pattern, $url, $matches ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $matches['version'];
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** BUSTING FILE ============================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the local Facebook SDK URL.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $locale A locale string, like 'en_US'.
|
||||
* @return string
|
||||
*/
|
||||
private function get_busting_file_url( $locale ) {
|
||||
$filename = $this->get_busting_file_name( $locale );
|
||||
|
||||
// This filter is documented in inc/functions/minify.php.
|
||||
return apply_filters( 'rocket_js_url', apply_filters( 'rocket_facebook_sdk_url', $this->busting_url . $filename ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the local Facebook SDK file name.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $locale A locale string, like 'en_US'.
|
||||
* @return string
|
||||
*/
|
||||
private function get_busting_file_name( $locale ) {
|
||||
return sprintf( $this->filename, $locale );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the local Facebook SDK file path.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $locale A locale string, like 'en_US'.
|
||||
* @return string
|
||||
*/
|
||||
private function get_busting_file_path( $locale ) {
|
||||
return $this->busting_path . $this->get_busting_file_name( $locale );
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** TOOLS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the contents of a URL.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $url The URL to request.
|
||||
* @return string|bool The contents on success. False on failure.
|
||||
*/
|
||||
protected function get_file_content( $url ) {
|
||||
try {
|
||||
$response = wp_remote_get( $url );
|
||||
} catch ( \Exception $e ) {
|
||||
Logger::error(
|
||||
'Remote file could not be fetched.',
|
||||
[
|
||||
'fb sdk',
|
||||
'url' => $url,
|
||||
'response' => $e->getMessage(),
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
Logger::error(
|
||||
'Remote file could not be fetched.',
|
||||
[
|
||||
'fb sdk',
|
||||
'url' => $url,
|
||||
'response' => $response->get_error_message(),
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$contents = wp_remote_retrieve_body( $response );
|
||||
|
||||
if ( ! $contents ) {
|
||||
Logger::error(
|
||||
'Remote file could not be fetched.',
|
||||
[
|
||||
'fb sdk',
|
||||
'url' => $url,
|
||||
'response' => $response,
|
||||
]
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a file name, to be used in a regex pattern (delimiter is `/`).
|
||||
* `%s` conversion specifications are protected.
|
||||
*
|
||||
* @since 3.2
|
||||
* @access private
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_name The file name.
|
||||
* @return string
|
||||
*/
|
||||
private function escape_file_name( $file_name ) {
|
||||
$file_name = explode( '%s', $file_name );
|
||||
$file_name = array_map( 'preg_quote', $file_name );
|
||||
|
||||
return implode( '%s', $file_name );
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
+88
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Optimization;
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
use WP_Rocket\Admin\Options_Data as Options;
|
||||
use WP_Scripts;
|
||||
|
||||
/**
|
||||
* Dequeue jQuery Migrate
|
||||
*
|
||||
* @since 3.5
|
||||
* @author Soponar Cristina
|
||||
*/
|
||||
class Dequeue_JQuery_Migrate_Subscriber implements Subscriber_Interface {
|
||||
use DeprecatedClassTrait;
|
||||
/**
|
||||
* Plugin options
|
||||
*
|
||||
* @since 3.5
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @var Options
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 3.5
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param Options $options Plugin options.
|
||||
*/
|
||||
public function __construct( Options $options ) {
|
||||
self::deprecated_class( '3.8' );
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'wp_default_scripts' => [ 'dequeue_jquery_migrate' ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Dequeue jquery migrate
|
||||
*
|
||||
* @since 3.5
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param WP_Scripts $scripts WP_Scripts instance.
|
||||
* @return bool|void
|
||||
*/
|
||||
public function dequeue_jquery_migrate( $scripts ) {
|
||||
if ( ! $this->is_allowed() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! empty( $scripts->registered['jquery'] ) ) {
|
||||
$jquery_dependencies = $scripts->registered['jquery']->deps;
|
||||
$scripts->registered['jquery']->deps = array_diff( $jquery_dependencies, [ 'jquery-migrate' ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if dequeue jquery migrate option is enabled
|
||||
*
|
||||
* @since 3.5
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function is_allowed() {
|
||||
if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE', false ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! $this->options->get( 'dequeue_jquery_migrate' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Optimization;
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
use WP_Rocket\Admin\Options_Data as Options;
|
||||
use WP_Rocket\Dependencies\Minify;
|
||||
|
||||
/**
|
||||
* HTML minification subscriber
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*/
|
||||
class Minify_HTML_Subscriber implements Subscriber_Interface {
|
||||
use DeprecatedClassTrait;
|
||||
/**
|
||||
* Plugin options
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @var Options
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param Options $options Plugin options.
|
||||
*/
|
||||
public function __construct( Options $options ) {
|
||||
self::deprecated_class( '3.7' );
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'rocket_buffer' => [ 'process', 14 ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Minifies HTML
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function process( $html ) {
|
||||
if ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
if ( ! $this->options->get( 'minify_html' ) || \is_rocket_post_excluded_option( 'minify_html' ) ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$html_options = [
|
||||
'cssMinifier' => [ $this, 'minify_inline_css' ],
|
||||
];
|
||||
|
||||
/**
|
||||
* Filter options of minify inline HTML
|
||||
*
|
||||
* @since 1.1.12
|
||||
*
|
||||
* @param array $html_options Options of minify inline HTML.
|
||||
*/
|
||||
$html_options = apply_filters( 'rocket_minify_html_options', $html_options );
|
||||
|
||||
return \Minify_HTML::minify( $html, $html_options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Minifies inline CSS
|
||||
*
|
||||
* @since 1.1.6
|
||||
*
|
||||
* @param string $css HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function minify_inline_css( $css ) {
|
||||
$minify = new Minify\CSS( $css );
|
||||
return $minify->minify();
|
||||
}
|
||||
|
||||
/**
|
||||
* Minifies inline JavaScript
|
||||
*
|
||||
* @since 1.1.6
|
||||
*
|
||||
* @param string $javascript HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function minify_inline_js( $javascript ) {
|
||||
$minify = new Minify\JS( $javascript );
|
||||
return $minify->minify();
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Subscriber\Admin\Settings;
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
use WP_Rocket\Admin\Settings\Beacon;
|
||||
|
||||
/**
|
||||
* Beacon Subscriber to WordPress
|
||||
*
|
||||
* @since 3.2
|
||||
* @author Remy Perona
|
||||
*/
|
||||
class Beacon_Subscriber implements Subscriber_Interface {
|
||||
use DeprecatedClassTrait;
|
||||
/**
|
||||
* Beacon instance
|
||||
*
|
||||
* @var Beacon
|
||||
*/
|
||||
private $beacon;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Beacon $beacon Beacon instance.
|
||||
*/
|
||||
public function __construct( Beacon $beacon ) {
|
||||
self::deprecated_class( '3.6' );
|
||||
$this->beacon = $beacon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.2
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'admin_print_footer_scripts-settings_page_wprocket' => 'insert_script',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert HelpScout Beacon script
|
||||
*
|
||||
* @since 3.0
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert_script() {
|
||||
echo $this->beacon->insert_script(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
|
||||
}
|
||||
}
|
||||
+308
@@ -0,0 +1,308 @@
|
||||
<?php
|
||||
|
||||
use WP_Rocket\deprecated\DeprecatedClassTrait;
|
||||
|
||||
/**
|
||||
* Class Minify_HTML
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
/**
|
||||
* Compress HTML
|
||||
*
|
||||
* This is a heavy regex-based removal of whitespace, unnecessary comments and
|
||||
* tokens. IE conditional comments are preserved. There are also options to have
|
||||
* STYLE and SCRIPT blocks compressed by callback functions.
|
||||
*
|
||||
* A test suite is available.
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
*/
|
||||
class Minify_HTML
|
||||
{
|
||||
use DeprecatedClassTrait;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_jsCleanComments = true;
|
||||
|
||||
/**
|
||||
* "Minify" an HTML page
|
||||
*
|
||||
* @param string $html
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* 'cssMinifier' : (optional) callback function to process content of STYLE
|
||||
* elements.
|
||||
*
|
||||
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
|
||||
* elements. Note: the type attribute is ignored.
|
||||
*
|
||||
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
|
||||
* unset, minify will sniff for an XHTML doctype.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function minify($html, $options = array())
|
||||
{
|
||||
$min = new self($html, $options);
|
||||
|
||||
return $min->process();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a minifier object
|
||||
*
|
||||
* @param string $html
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* 'cssMinifier' : (optional) callback function to process content of STYLE
|
||||
* elements.
|
||||
*
|
||||
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
|
||||
* elements. Note: the type attribute is ignored.
|
||||
*
|
||||
* 'jsCleanComments' : (optional) whether to remove HTML comments beginning and end of script block
|
||||
*
|
||||
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
|
||||
* unset, minify will sniff for an XHTML doctype.
|
||||
*/
|
||||
public function __construct($html, $options = array())
|
||||
{
|
||||
self::deprecated_class( '3.7' );
|
||||
|
||||
$this->_html = str_replace("\r\n", "\n", trim($html));
|
||||
if (isset($options['xhtml'])) {
|
||||
$this->_isXhtml = (bool)$options['xhtml'];
|
||||
}
|
||||
if (isset($options['cssMinifier'])) {
|
||||
$this->_cssMinifier = $options['cssMinifier'];
|
||||
}
|
||||
if (isset($options['jsMinifier'])) {
|
||||
$this->_jsMinifier = $options['jsMinifier'];
|
||||
}
|
||||
if (isset($options['jsCleanComments'])) {
|
||||
$this->_jsCleanComments = (bool)$options['jsCleanComments'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Minify the markeup given in the constructor
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process()
|
||||
{
|
||||
if ($this->_isXhtml === null) {
|
||||
$this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
|
||||
}
|
||||
|
||||
$this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
|
||||
$this->_placeholders = array();
|
||||
|
||||
// replace SCRIPTs (and minify) with placeholders
|
||||
// preg_replace_callback - on errors the return is NULL
|
||||
// On big scripts PREG_BACKTRACK_LIMIT_ERROR is reached and causes the empty page
|
||||
$pregJs = preg_replace_callback(
|
||||
'/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
|
||||
,array($this, '_removeScriptCB')
|
||||
,$this->_html);
|
||||
|
||||
if ( isset($pregJs) && ! empty( $pregJs ) ) {
|
||||
$this->_html = $pregJs;
|
||||
}
|
||||
|
||||
// replace STYLEs (and minify) with placeholders
|
||||
// preg_replace_callback - on errors the return is NULL
|
||||
// On big scripts PREG_BACKTRACK_LIMIT_ERROR is reached and causes the empty page
|
||||
$pregCSS = preg_replace_callback(
|
||||
'/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/i'
|
||||
,array($this, '_removeStyleCB')
|
||||
,$this->_html);
|
||||
|
||||
if ( isset( $pregCSS ) && ! empty( $pregCSS ) ) {
|
||||
$this->_html = $pregCSS;
|
||||
}
|
||||
|
||||
// remove HTML comments (not containing IE conditional comments).
|
||||
$this->_html = preg_replace_callback(
|
||||
'/<!--([\\s\\S]*?)-->/'
|
||||
,array($this, '_commentCB')
|
||||
,$this->_html);
|
||||
|
||||
// replace PREs with placeholders
|
||||
$this->_html = preg_replace_callback('/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
|
||||
,array($this, '_removePreCB')
|
||||
,$this->_html);
|
||||
|
||||
// replace TEXTAREAs with placeholders
|
||||
$this->_html = preg_replace_callback(
|
||||
'/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
|
||||
,array($this, '_removeTextareaCB')
|
||||
,$this->_html);
|
||||
|
||||
// trim each line.
|
||||
// @todo take into account attribute values that span multiple lines.
|
||||
// Fixed attribute values which span on multiple lines. Causes double spaces " "
|
||||
$this->_html = preg_replace('/^\s+|\s+$/m', ' ', $this->_html);
|
||||
// Fixed double spaces. Replaced with a single space
|
||||
$this->_html = preg_replace('/\s+/', ' ', $this->_html);
|
||||
|
||||
// remove ws around block/undisplayed elements
|
||||
$this->_html = preg_replace('/\\s+(<\\/?(?:area|article|aside|base(?:font)?|blockquote|body'
|
||||
.'|canvas|caption|center|col(?:group)?|dd|dir|div|dl|dt|fieldset|figcaption|figure|footer|form'
|
||||
.'|frame(?:set)?|h[1-6]|head|header|hgroup|hr|html|legend|li|link|main|map|menu|meta|nav'
|
||||
.'|ol|opt(?:group|ion)|output|p|param|section|t(?:able|body|head|d|h||r|foot|itle)'
|
||||
.'|ul|video)\\b[^>]*>)/i', '$1', $this->_html);
|
||||
|
||||
// remove ws outside of all elements
|
||||
$this->_html = preg_replace_callback(
|
||||
'/>([^<]+)</'
|
||||
,array($this, '_outsideTagCB')
|
||||
,$this->_html);
|
||||
|
||||
// fill placeholders
|
||||
$this->_html = str_replace(
|
||||
array_keys($this->_placeholders)
|
||||
,array_values($this->_placeholders)
|
||||
,$this->_html
|
||||
);
|
||||
// issue 229: multi-pass to catch scripts that didn't get replaced in textareas
|
||||
$this->_html = str_replace(
|
||||
array_keys($this->_placeholders)
|
||||
,array_values($this->_placeholders)
|
||||
,$this->_html
|
||||
);
|
||||
|
||||
return $this->_html;
|
||||
}
|
||||
|
||||
protected function _commentCB($m)
|
||||
{
|
||||
return
|
||||
(
|
||||
false !== strpos($m[1], 'fwp-loop')
|
||||
||
|
||||
false !== strpos($m[1], 'ngg_resource_manager_marker')
|
||||
||
|
||||
0 === strpos($m[1], '[')
|
||||
||
|
||||
false !== strpos($m[1], '<![')
|
||||
||
|
||||
0 === strpos($m[1], 'esi')
|
||||
||
|
||||
0 === strpos($m[1], 'noindex')
|
||||
||
|
||||
0 === strpos($m[1], '/noindex')
|
||||
||
|
||||
0 === strpos($m[1], 'start_content')
|
||||
||
|
||||
0 === strpos($m[1], 'end_content')
|
||||
||
|
||||
0 === strpos($m[1], '{{WP_ROCKET_CONDITIONAL}}')
|
||||
)
|
||||
? $m[0]
|
||||
: '';
|
||||
}
|
||||
|
||||
protected function _reservePlace($content)
|
||||
{
|
||||
$placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
|
||||
$this->_placeholders[$placeholder] = $content;
|
||||
|
||||
return $placeholder;
|
||||
}
|
||||
|
||||
protected $_isXhtml = null;
|
||||
protected $_replacementHash = null;
|
||||
protected $_placeholders = array();
|
||||
protected $_cssMinifier = null;
|
||||
protected $_jsMinifier = null;
|
||||
|
||||
protected function _outsideTagCB($m)
|
||||
{
|
||||
return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
|
||||
}
|
||||
|
||||
protected function _removePreCB($m)
|
||||
{
|
||||
return $this->_reservePlace("<pre{$m[1]}");
|
||||
}
|
||||
|
||||
protected function _removeTextareaCB($m)
|
||||
{
|
||||
return $this->_reservePlace("<textarea{$m[1]}");
|
||||
}
|
||||
|
||||
protected function _removeStyleCB($m)
|
||||
{
|
||||
$openStyle = "<style{$m[1]}";
|
||||
$css = $m[2];
|
||||
// remove HTML comments
|
||||
$css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/u', '', $css);
|
||||
|
||||
// remove CDATA section markers
|
||||
$data = $this->_removeCdata($css);
|
||||
|
||||
// minify
|
||||
$minifier = $this->_cssMinifier
|
||||
? $this->_cssMinifier
|
||||
: 'trim';
|
||||
$css = call_user_func($minifier, $data['content']);
|
||||
|
||||
return $this->_reservePlace( $data['cdata']
|
||||
? "{$openStyle}/* <![CDATA[ */ {$css} /* ]]> */</style>"
|
||||
: "{$openStyle}{$css}</style>"
|
||||
);
|
||||
}
|
||||
|
||||
protected function _removeScriptCB($m)
|
||||
{
|
||||
$openScript = "<script{$m[2]}";
|
||||
$js = $m[3];
|
||||
|
||||
// whitespace surrounding? preserve at least one space
|
||||
$ws1 = ($m[1] === '') ? '' : ' ';
|
||||
$ws2 = ($m[4] === '') ? '' : ' ';
|
||||
|
||||
// remove HTML comments (and ending "//" if present)
|
||||
if ($this->_jsCleanComments) {
|
||||
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/u', '', $js);
|
||||
}
|
||||
|
||||
// remove CDATA section markers
|
||||
$data = $this->_removeCdata($js);
|
||||
|
||||
// minify
|
||||
$minifier = $this->_jsMinifier
|
||||
? $this->_jsMinifier
|
||||
: 'trim';
|
||||
$js = call_user_func($minifier, $data['content']);
|
||||
|
||||
return $this->_reservePlace($data['cdata']
|
||||
? "{$ws1}{$openScript}/* <![CDATA[ */ {$js} /* ]]> */</script>{$ws2}"
|
||||
: "{$ws1}{$openScript}{$js}</script>{$ws2}"
|
||||
);
|
||||
}
|
||||
|
||||
protected function _removeCdata($str)
|
||||
{
|
||||
$data = array();
|
||||
|
||||
if ( false !== strpos( $str, '<![CDATA[' ) ) {
|
||||
$data['content'] = str_replace( array( '/* <![CDATA[ */', '/* ]]> */' ), '', $str );
|
||||
$data['cdata'] = true;
|
||||
|
||||
} else {
|
||||
$data['content'] = $str;
|
||||
$data['cdata'] = false;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user