Commit inicial powerservice
This commit is contained in:
@@ -0,0 +1,467 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Link to the configuration page of the plugin, support & documentation
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param array $actions Array of links to display.
|
||||
* @return array Updated array of links
|
||||
*/
|
||||
function rocket_settings_action_links( $actions ) {
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
return $actions;
|
||||
}
|
||||
|
||||
array_unshift( $actions, sprintf( '<a href="%s">%s</a>', 'https://wp-rocket.me/support/?utm_source=wp_plugin&utm_medium=wp_rocket', __( 'Support', 'rocket' ) ) );
|
||||
|
||||
array_unshift( $actions, sprintf( '<a href="%s">%s</a>', get_rocket_documentation_url(), __( 'Docs', 'rocket' ) ) );
|
||||
|
||||
array_unshift( $actions, sprintf( '<a href="%s">%s</a>', get_rocket_faq_url(), __( 'FAQ', 'rocket' ) ) );
|
||||
|
||||
array_unshift( $actions, sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=' . WP_ROCKET_PLUGIN_SLUG ), __( 'Settings', 'rocket' ) ) );
|
||||
|
||||
return $actions;
|
||||
}
|
||||
add_filter( 'plugin_action_links_' . plugin_basename( WP_ROCKET_FILE ), 'rocket_settings_action_links' );
|
||||
|
||||
/**
|
||||
* Add a link "Renew your licence" when you can't do it automatically (expired licence but new version available)
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param array $plugin_meta An array of the plugin's metadata, including the version, author, author URI, and plugin URI.
|
||||
* @param string $plugin_file Path to the plugin file, relative to the plugins directory.
|
||||
* @return array Updated meta content if license is expired
|
||||
*/
|
||||
function rocket_plugin_row_meta( $plugin_meta, $plugin_file ) {
|
||||
if ( 'wp-rocket/wp-rocket.php' === $plugin_file ) {
|
||||
|
||||
$update_plugins = get_site_transient( 'update_plugins' );
|
||||
|
||||
if ( false !== $update_plugins && isset( $update_plugins->response[ $plugin_file ] ) && empty( $update_plugins->response[ $plugin_file ]->package ) ) {
|
||||
|
||||
$link = '<span class="dashicons dashicons-update rocket-dashicons"></span> <span class="rocket-renew">Renew your licence of WP Rocket to receive access to automatic upgrades and support.</span> <a href="http://wp-rocket.me" target="_blank" class="rocket-purchase">Purchase now</a>.';
|
||||
|
||||
$plugin_meta = array_merge( (array) $link, $plugin_meta );
|
||||
}
|
||||
}
|
||||
|
||||
return $plugin_meta;
|
||||
}
|
||||
add_action( 'plugin_row_meta', 'rocket_plugin_row_meta', 10, 2 );
|
||||
|
||||
/**
|
||||
* Add a link "Purge this cache" in the post edit area
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param array $actions An array of row action links.
|
||||
* @param object $post The post object.
|
||||
* @return array Updated array of row action links
|
||||
*/
|
||||
function rocket_post_row_actions( $actions, $post ) {
|
||||
|
||||
if ( ! rocket_can_display_options() ) {
|
||||
return $actions;
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'rocket_purge_posts' ) ) {
|
||||
return $actions;
|
||||
}
|
||||
|
||||
$cpts = get_post_types(
|
||||
[
|
||||
'public' => true,
|
||||
],
|
||||
'objects'
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the post type on row actions.
|
||||
*
|
||||
* @since 3.11.4
|
||||
*
|
||||
* @param array $cpts Post Types.
|
||||
*/
|
||||
$cpts = apply_filters( 'rocket_skip_post_row_actions', $cpts );
|
||||
|
||||
if ( ! isset( $cpts[ $post->post_type ] ) ) {
|
||||
return $actions;
|
||||
}
|
||||
|
||||
$url = wp_nonce_url( admin_url( 'admin-post.php?action=purge_cache&type=post-' . $post->ID ), 'purge_cache_post-' . $post->ID );
|
||||
$actions['rocket_purge'] = sprintf( '<a href="%s">%s</a>', $url, __( 'Clear this cache', 'rocket' ) );
|
||||
|
||||
return $actions;
|
||||
}
|
||||
add_filter( 'page_row_actions', 'rocket_post_row_actions', 10, 2 );
|
||||
add_filter( 'post_row_actions', 'rocket_post_row_actions', 10, 2 );
|
||||
|
||||
/**
|
||||
* Add a link "Purge this cache" in the user edit area
|
||||
*
|
||||
* @since 2.6.12
|
||||
* @param array $actions An array of row action links.
|
||||
* @param object $user The user object.
|
||||
* @return array Updated array of row action links
|
||||
*/
|
||||
function rocket_user_row_actions( $actions, $user ) {
|
||||
if ( ! current_user_can( 'rocket_purge_users' ) || ! get_rocket_option( 'cache_logged_user', false ) ) {
|
||||
return $actions;
|
||||
}
|
||||
|
||||
$url = wp_nonce_url( admin_url( 'admin-post.php?action=purge_cache&type=user-' . $user->ID ), 'purge_cache_user-' . $user->ID );
|
||||
$actions['rocket_purge'] = sprintf( '<a href="%s">%s</a>', $url, __( 'Clear this cache', 'rocket' ) );
|
||||
|
||||
return $actions;
|
||||
}
|
||||
add_filter( 'user_row_actions', 'rocket_user_row_actions', 10, 2 );
|
||||
|
||||
/**
|
||||
* Manage the dismissed boxes.
|
||||
*
|
||||
* @since 3.6 Reverse dependency with rocket_dismiss_box().
|
||||
* @since 2.4 Add a delete_transient on function name (box name).
|
||||
* @since 1.3.0 $args can replace $_GET when called internaly.
|
||||
* @since 1.1.10
|
||||
*
|
||||
* @param array $args An array of query args. Should not be used: see rocket_dismiss_box().
|
||||
*/
|
||||
function rocket_dismiss_boxes( $args = [] ) {
|
||||
global $pagenow;
|
||||
|
||||
$args = empty( $args ) ? $_GET : $args; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
if ( ! isset( $args['box'], $args['action'], $args['_wpnonce'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! wp_verify_nonce( $args['_wpnonce'], "{$args['action']}_{$args['box']}" ) ) {
|
||||
if ( rocket_get_constant( 'DOING_AJAX' ) ) {
|
||||
wp_send_json( [ 'error' => 1 ] );
|
||||
} else {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
rocket_dismiss_box( $args['box'] );
|
||||
|
||||
if ( 'admin-post.php' === $pagenow ) {
|
||||
if ( rocket_get_constant( 'DOING_AJAX' ) ) {
|
||||
wp_send_json( [ 'error' => 0 ] );
|
||||
} else {
|
||||
wp_safe_redirect( esc_url_raw( wp_get_referer() ) );
|
||||
rocket_get_constant( 'WP_ROCKET_IS_TESTING', false ) ? wp_die() : exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
add_action( 'wp_ajax_rocket_ignore', 'rocket_dismiss_boxes' );
|
||||
add_action( 'admin_post_rocket_ignore', 'rocket_dismiss_boxes' );
|
||||
|
||||
/**
|
||||
* Renew the plugin modification warning on plugin de/activation
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @param string $plugin plugin name.
|
||||
*/
|
||||
function rocket_dismiss_plugin_box( $plugin ) {
|
||||
if ( plugin_basename( WP_ROCKET_FILE ) !== $plugin ) {
|
||||
rocket_renew_box( 'rocket_warning_plugin_modification' );
|
||||
}
|
||||
}
|
||||
add_action( 'activated_plugin', 'rocket_dismiss_plugin_box' );
|
||||
add_action( 'deactivated_plugin', 'rocket_dismiss_plugin_box' );
|
||||
|
||||
/**
|
||||
* Display a prevention message when enabling or disabling a plugin can be in conflict with WP Rocket
|
||||
*
|
||||
* @since 1.3.0
|
||||
*/
|
||||
function rocket_deactivate_plugin() {
|
||||
if ( ! isset( $_GET['plugin'], $_GET['_wpnonce'] ) ) {
|
||||
return;
|
||||
}
|
||||
if ( ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'deactivate_plugin' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
deactivate_plugins( sanitize_text_field( wp_unslash( $_GET['plugin'] ) ) );
|
||||
|
||||
wp_safe_redirect( wp_get_referer() );
|
||||
die();
|
||||
}
|
||||
add_action( 'admin_post_deactivate_plugin', 'rocket_deactivate_plugin' );
|
||||
|
||||
/**
|
||||
* This function will force the direct download of the plugin's options, compressed.
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
function rocket_do_options_export() {
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'rocket_export' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
list( $filename, $options ) = rocket_export_options();
|
||||
nocache_headers();
|
||||
@header( 'Content-Type: application/json' );
|
||||
@header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
|
||||
@header( 'Content-Transfer-Encoding: binary' );
|
||||
@header( 'Content-Length: ' . strlen( $options ) );
|
||||
@header( 'Connection: close' );
|
||||
echo $options; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
exit();
|
||||
}
|
||||
add_action( 'admin_post_rocket_export', 'rocket_do_options_export' );
|
||||
|
||||
if ( ! defined( 'DOING_AJAX' ) && ! defined( 'DOING_AUTOSAVE' ) ) {
|
||||
add_action( 'admin_init', 'rocket_init_cache_dir' );
|
||||
add_action( 'admin_init', 'rocket_maybe_generate_advanced_cache_file' );
|
||||
add_action( 'admin_init', 'rocket_maybe_generate_config_files' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate the advanced-cache.php file if an issue is detected.
|
||||
*
|
||||
* @since 2.6
|
||||
*/
|
||||
function rocket_maybe_generate_advanced_cache_file() {
|
||||
if ( ! defined( 'WP_ROCKET_ADVANCED_CACHE' ) || ( defined( 'WP_ROCKET_ADVANCED_CACHE_PROBLEM' ) && WP_ROCKET_ADVANCED_CACHE_PROBLEM ) ) {
|
||||
rocket_generate_advanced_cache_file();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate config file if an issue is detected.
|
||||
*
|
||||
* @since 2.6.5
|
||||
*/
|
||||
function rocket_maybe_generate_config_files() {
|
||||
$home = get_rocket_parse_url( rocket_get_home_url() );
|
||||
|
||||
$path = ( ! empty( $home['path'] ) ) ? str_replace( '/', '.', untrailingslashit( $home['path'] ) ) : '';
|
||||
|
||||
if ( ! file_exists( WP_ROCKET_CONFIG_PATH . strtolower( $home['host'] ) . $path . '.php' ) ) {
|
||||
rocket_generate_config_file();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all data to send to the analytics system
|
||||
*
|
||||
* @since 3.0 Send CDN zones, sitemaps paths, and count the number of CDN URLs used
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return mixed An array of data, or false if WP Rocket options is not an array
|
||||
*/
|
||||
function rocket_analytics_data() {
|
||||
global $wp_version, $is_nginx, $is_apache, $is_iis7, $is_IIS;
|
||||
|
||||
if ( ! is_array( get_option( WP_ROCKET_SLUG ) ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$untracked_wp_rocket_options = [
|
||||
'license' => 1,
|
||||
'consumer_email' => 1,
|
||||
'consumer_key' => 1,
|
||||
'secret_key' => 1,
|
||||
'secret_cache_key' => 1,
|
||||
'minify_css_key' => 1,
|
||||
'minify_js_key' => 1,
|
||||
'cloudflare_email' => 1,
|
||||
'cloudflare_api_key' => 1,
|
||||
'cloudflare_zone_id' => 1,
|
||||
'cloudflare_old_settings' => 1,
|
||||
'submit_optimize' => 1,
|
||||
'analytics_enabled' => 1,
|
||||
];
|
||||
|
||||
$theme = wp_get_theme();
|
||||
$data = array_diff_key( get_option( WP_ROCKET_SLUG ), $untracked_wp_rocket_options );
|
||||
$locale = explode( '_', get_locale() );
|
||||
$data['web_server'] = 'Unknown';
|
||||
|
||||
if ( $is_nginx ) {
|
||||
$data['web_server'] = 'NGINX';
|
||||
} elseif ( $is_apache ) {
|
||||
$data['web_server'] = 'Apache';
|
||||
} elseif ( $is_iis7 ) {
|
||||
$data['web_server'] = 'IIS 7';
|
||||
} elseif ( $is_IIS ) {
|
||||
$data['web_server'] = 'IIS';
|
||||
}
|
||||
|
||||
$data['php_version'] = preg_replace( '@^(\d\.\d+).*@', '\1', phpversion() );
|
||||
$data['wordpress_version'] = preg_replace( '@^(\d\.\d+).*@', '\1', $wp_version );
|
||||
$data['current_theme'] = $theme->get( 'Name' );
|
||||
$data['active_plugins'] = rocket_get_active_plugins();
|
||||
$data['locale'] = $locale[0];
|
||||
$data['multisite'] = is_multisite();
|
||||
|
||||
if ( ! empty( $data['cdn_cnames'] ) && is_array( $data['cdn_cnames'] ) ) {
|
||||
$data['cdn_cnames'] = count( $data['cdn_cnames'] );
|
||||
} else {
|
||||
$data['cdn_cnames'] = 0;
|
||||
}
|
||||
|
||||
$customer_data = get_transient( 'wp_rocket_customer_data' );
|
||||
$data['license_type'] = '';
|
||||
if ( false !== $customer_data ) {
|
||||
$data['license_type'] = rocket_get_license_type( $customer_data );
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if we should send the analytics data
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return bool True if we should send them, false otherwise
|
||||
*/
|
||||
function rocket_send_analytics_data() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the analytics opt-in notice selection and prevent further display
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*/
|
||||
function rocket_analytics_optin() {
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'analytics_optin' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
wp_safe_redirect( wp_get_referer() );
|
||||
die();
|
||||
}
|
||||
|
||||
if ( isset( $_GET['value'] ) && 'yes' === $_GET['value'] ) {
|
||||
update_rocket_option( 'analytics_enabled', 1 );
|
||||
set_transient( 'rocket_analytics_optin', 1 );
|
||||
}
|
||||
|
||||
update_option( 'rocket_analytics_notice_displayed', 1 );
|
||||
|
||||
wp_safe_redirect( wp_get_referer() );
|
||||
die();
|
||||
}
|
||||
add_action( 'admin_post_rocket_analytics_optin', 'rocket_analytics_optin' );
|
||||
|
||||
/**
|
||||
* Handle WP Rocket settings import.
|
||||
*
|
||||
* @since 3.10 disable async_css if both async_css and remove_unused_css are enabled
|
||||
* @since 3.0 Hooked on admin_post now
|
||||
* @since 2.10.7
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function rocket_handle_settings_import() {
|
||||
check_ajax_referer( 'rocket_import_settings', 'rocket_import_settings_nonce' );
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
rocket_settings_import_redirect( __( 'Settings import failed: you do not have the permissions to do this.', 'rocket' ), 'error' );
|
||||
}
|
||||
|
||||
if ( ! isset( $_FILES['import'] ) || ( isset( $_FILES['import']['size'] ) && 0 === $_FILES['import']['size'] ) ) {
|
||||
rocket_settings_import_redirect( __( 'Settings import failed: no file uploaded.', 'rocket' ), 'error' );
|
||||
}
|
||||
|
||||
if ( isset( $_FILES['import']['name'] ) && ! preg_match( '/wp-rocket-settings(?:-.*)?-20\d{2}-\d{2}-\d{2}-[a-f0-9]{13}\.(?:txt|json)/', sanitize_file_name( $_FILES['import']['name'] ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
|
||||
rocket_settings_import_redirect( __( 'Settings import failed: incorrect filename.', 'rocket' ), 'error' );
|
||||
}
|
||||
|
||||
add_filter( 'mime_types', 'rocket_allow_json_mime_type' );
|
||||
add_filter( 'wp_check_filetype_and_ext', 'rocket_check_json_filetype', 10, 4 );
|
||||
|
||||
$mimes = get_allowed_mime_types();
|
||||
$mimes = rocket_allow_json_mime_type( $mimes );
|
||||
$file_data = wp_check_filetype_and_ext( $_FILES['import']['tmp_name'], sanitize_file_name( $_FILES['import']['name'] ), $mimes ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotValidated
|
||||
|
||||
if ( 'text/plain' !== $file_data['type'] && 'application/json' !== $file_data['type'] ) {
|
||||
rocket_settings_import_redirect( __( 'Settings import failed: incorrect filetype.', 'rocket' ), 'error' );
|
||||
}
|
||||
|
||||
$_post_action = isset( $_POST['action'] ) ? wp_unslash( sanitize_key( $_POST['action'] ) ) : '';
|
||||
$_POST['action'] = 'wp_handle_sideload';
|
||||
$overrides = [];
|
||||
$overrides['mimes'] = $mimes;
|
||||
$file = wp_handle_sideload( $_FILES['import'], $overrides ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
|
||||
|
||||
if ( isset( $file['error'] ) ) {
|
||||
rocket_settings_import_redirect( __( 'Settings import failed: ', 'rocket' ) . $file['error'], 'error' );
|
||||
}
|
||||
|
||||
$_POST['action'] = $_post_action;
|
||||
$settings = rocket_direct_filesystem()->get_contents( $file['file'] );
|
||||
remove_filter( 'mime_types', 'rocket_allow_json_mime_type' );
|
||||
remove_filter( 'wp_check_filetype_and_ext', 'rocket_check_json_filetype', 10 );
|
||||
|
||||
if ( 'text/plain' === $file_data['type'] ) {
|
||||
$gz = 'gz' . strrev( 'etalfni' );
|
||||
$settings = $gz( $settings );
|
||||
$settings = maybe_unserialize( $settings );
|
||||
} elseif ( 'application/json' === $file_data['type'] ) {
|
||||
$settings = json_decode( $settings, true );
|
||||
|
||||
if ( null === $settings ) {
|
||||
rocket_settings_import_redirect( __( 'Settings import failed: unexpected file content.', 'rocket' ), 'error' );
|
||||
}
|
||||
}
|
||||
|
||||
rocket_put_content( $file['file'], '' );
|
||||
rocket_direct_filesystem()->delete( $file['file'] );
|
||||
|
||||
if ( is_array( $settings ) ) {
|
||||
$options_api = new WP_Rocket\Admin\Options( 'wp_rocket_' );
|
||||
$current_options = $options_api->get( 'settings', [] );
|
||||
|
||||
$settings['consumer_key'] = $current_options['consumer_key'];
|
||||
$settings['consumer_email'] = $current_options['consumer_email'];
|
||||
$settings['secret_key'] = $current_options['secret_key'];
|
||||
$settings['secret_cache_key'] = $current_options['secret_cache_key'];
|
||||
$settings['minify_css_key'] = $current_options['minify_css_key'];
|
||||
$settings['minify_js_key'] = $current_options['minify_js_key'];
|
||||
$settings['version'] = $current_options['version'];
|
||||
if (
|
||||
isset( $settings['async_css'] ) && $settings['async_css'] &&
|
||||
isset( $settings['remove_unused_css'] ) && $settings['remove_unused_css']
|
||||
) {
|
||||
$settings['async_css'] = 0;
|
||||
}
|
||||
if ( ! empty( $settings['cache_webp'] ) && apply_filters( 'rocket_disable_webp_cache', false ) ) {
|
||||
$settings['cache_webp'] = 0;
|
||||
}
|
||||
|
||||
$options_api->set( 'settings', $settings );
|
||||
|
||||
rocket_settings_import_redirect( __( 'Settings imported and saved.', 'rocket' ), 'updated' );
|
||||
}
|
||||
}
|
||||
add_action( 'admin_post_rocket_import_settings', 'rocket_handle_settings_import' );
|
||||
@@ -0,0 +1,224 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* When our settings are saved: purge, flush, preload!
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* When the settins menu is hidden, redirect on the main settings page to avoid the same thing
|
||||
* (Only when a form is sent from our options page )
|
||||
*
|
||||
* @since 2.1
|
||||
*
|
||||
* @param array $oldvalue An array of previous values for the settings.
|
||||
* @param array $value An array of submitted values for the settings.
|
||||
*/
|
||||
function rocket_after_save_options( $oldvalue, $value ) {
|
||||
if ( ! is_array( $oldvalue ) || ! is_array( $value ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Missing
|
||||
if ( ( array_key_exists( 'minify_js', $oldvalue ) && array_key_exists( 'minify_js', $value ) && $oldvalue['minify_js'] !== $value['minify_js'] )
|
||||
||
|
||||
( array_key_exists( 'exclude_js', $oldvalue ) && array_key_exists( 'exclude_js', $value ) && $oldvalue['exclude_js'] !== $value['exclude_js'] )
|
||||
||
|
||||
( array_key_exists( 'cdn', $oldvalue ) && array_key_exists( 'cdn', $value ) && $oldvalue['cdn'] !== $value['cdn'] )
|
||||
||
|
||||
( array_key_exists( 'cdn_cnames', $oldvalue ) && array_key_exists( 'cdn_cnames', $value ) && $oldvalue['cdn_cnames'] !== $value['cdn_cnames'] )
|
||||
) {
|
||||
rocket_clean_minify( 'js' );
|
||||
}
|
||||
|
||||
// Regenerate advanced-cache.php file.
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Missing
|
||||
if ( ! empty( $_POST ) && ( ( isset( $oldvalue['do_caching_mobile_files'] ) && ! isset( $value['do_caching_mobile_files'] ) ) || ( ! isset( $oldvalue['do_caching_mobile_files'] ) && isset( $value['do_caching_mobile_files'] ) ) || ( isset( $oldvalue['do_caching_mobile_files'], $value['do_caching_mobile_files'] ) ) && $oldvalue['do_caching_mobile_files'] !== $value['do_caching_mobile_files'] ) ) {
|
||||
rocket_generate_advanced_cache_file();
|
||||
}
|
||||
|
||||
// Update .htaccess file rules.
|
||||
flush_rocket_htaccess( ! rocket_valid_key() );
|
||||
|
||||
// Update config file.
|
||||
rocket_generate_config_file();
|
||||
|
||||
if ( isset( $oldvalue['analytics_enabled'], $value['analytics_enabled'] ) && $oldvalue['analytics_enabled'] !== $value['analytics_enabled'] && 1 === (int) $value['analytics_enabled'] ) {
|
||||
set_transient( 'rocket_analytics_optin', 1 );
|
||||
}
|
||||
// If it's different, clean the domain.
|
||||
if ( rocket_create_options_hash( $value ) !== rocket_create_options_hash( $oldvalue ) ) {
|
||||
// Purge all cache files.
|
||||
rocket_clean_domain();
|
||||
|
||||
/**
|
||||
* Fires after WP Rocket options that require a cache purge have changed
|
||||
*
|
||||
* @since 3.11
|
||||
*
|
||||
* @param array $value An array of submitted values for the settings.
|
||||
*/
|
||||
do_action( 'rocket_options_changed', $value );
|
||||
}
|
||||
}
|
||||
add_action( 'update_option_' . rocket_get_constant( 'WP_ROCKET_SLUG' ), 'rocket_after_save_options', 10, 2 );
|
||||
|
||||
/**
|
||||
* Perform actions when settings are saved.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param array $newvalue An array of submitted options values.
|
||||
* @param array $oldvalue An array of previous options values.
|
||||
* @return array Updated submitted options values.
|
||||
*/
|
||||
function rocket_pre_main_option( $newvalue, $oldvalue ) {
|
||||
$rocket_settings_errors = [];
|
||||
|
||||
// Make sure that fields that allow users to enter patterns are well formatted.
|
||||
$is_form_submit = isset( $_POST['option_page'] ) ? sanitize_key( $_POST['option_page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing
|
||||
$is_form_submit = WP_ROCKET_PLUGIN_SLUG === $is_form_submit;
|
||||
$errors = [];
|
||||
$pattern_labels = [
|
||||
'exclude_css' => __( 'Excluded CSS Files', 'rocket' ),
|
||||
'exclude_inline_js' => __( 'Excluded Inline JavaScript', 'rocket' ),
|
||||
'exclude_js' => __( 'Excluded JavaScript Files', 'rocket' ),
|
||||
'exclude_defer_js' => __( 'Defer JavaScript Files', 'rocket' ),
|
||||
'delay_js_exclusions' => __( 'Excluded Delay JavaScript Files', 'rocket' ),
|
||||
'cache_reject_uri' => __( 'Never Cache URL(s)', 'rocket' ),
|
||||
'cache_reject_ua' => __( 'Never Cache User Agent(s)', 'rocket' ),
|
||||
'cache_purge_pages' => __( 'Always Purge URL(s)', 'rocket' ),
|
||||
'cdn_reject_files' => __( 'Exclude files from CDN', 'rocket' ),
|
||||
];
|
||||
|
||||
foreach ( $pattern_labels as $pattern_field => $label ) {
|
||||
if ( empty( $newvalue[ $pattern_field ] ) ) {
|
||||
// The field is empty.
|
||||
continue;
|
||||
}
|
||||
|
||||
// Sanitize.
|
||||
$newvalue[ $pattern_field ] = rocket_sanitize_textarea_field( $pattern_field, $newvalue[ $pattern_field ] );
|
||||
|
||||
// Validate.
|
||||
$newvalue[ $pattern_field ] = array_filter(
|
||||
$newvalue[ $pattern_field ],
|
||||
function ( $excluded ) use ( $pattern_field, $label, $is_form_submit, &$errors ) {
|
||||
if ( false === @preg_match( '#' . str_replace( '#', '\#', $excluded ) . '#', 'dummy-sample' ) && $is_form_submit ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
|
||||
/* translators: 1 and 2 can be anything. */
|
||||
$errors[ $pattern_field ][] = sprintf( __( '%1$s: <em>%2$s</em>', 'rocket' ), $label, esc_html( $excluded ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if ( $errors ) {
|
||||
$error_message = _n( 'The following pattern is invalid and has been removed:', 'The following patterns are invalid and have been removed:', array_sum( array_map( 'count', $errors ) ), 'rocket' );
|
||||
|
||||
$error_message .= '</strong></p>'; // close out default opening tags from WP's settings_errors().
|
||||
|
||||
foreach ( $errors as $error ) {
|
||||
$error_message .= '<ul><li>' . implode( '</li><li>', $error ) . '</li></ul>';
|
||||
}
|
||||
|
||||
$error_message .= '<p><strong>'; // Re-open tags that WP's settings_errors() will close at end of notice box.
|
||||
|
||||
$container = apply_filters( 'rocket_container', [] );
|
||||
$invalid_exclusions_beacon = $container->get( 'beacon' )->get_suggest( 'invalid_exclusions' );
|
||||
$error_message .= sprintf(
|
||||
'<a href="%1$s" data-beacon-article="%2$s" rel="noopener noreferrer" target="_blank">%3$s</a>',
|
||||
$invalid_exclusions_beacon['url'],
|
||||
$invalid_exclusions_beacon['id'],
|
||||
__( 'More info', 'rocket' )
|
||||
);
|
||||
|
||||
$errors = [];
|
||||
|
||||
$rocket_settings_errors[] = [
|
||||
'setting' => 'general',
|
||||
'code' => 'invalid_patterns',
|
||||
'message' => __( 'WP Rocket: ', 'rocket' ) . $error_message,
|
||||
'type' => 'error',
|
||||
];
|
||||
}
|
||||
|
||||
// Clear WP Rocket database optimize cron if the setting has been modified.
|
||||
if ( ( isset( $newvalue['schedule_automatic_cleanup'], $oldvalue['schedule_automatic_cleanup'] ) && $newvalue['schedule_automatic_cleanup'] !== $oldvalue['schedule_automatic_cleanup'] ) || ( ( isset( $newvalue['automatic_cleanup_frequency'], $oldvalue['automatic_cleanup_frequency'] ) && $newvalue['automatic_cleanup_frequency'] !== $oldvalue['automatic_cleanup_frequency'] ) ) ) {
|
||||
if ( wp_next_scheduled( 'rocket_database_optimization_time_event' ) ) {
|
||||
wp_clear_scheduled_hook( 'rocket_database_optimization_time_event' );
|
||||
}
|
||||
}
|
||||
|
||||
// Regenerate the minify key if JS files have been modified.
|
||||
// phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
|
||||
if ( ( isset( $newvalue['minify_js'], $oldvalue['minify_js'] ) && $newvalue['minify_js'] != $oldvalue['minify_js'] )
|
||||
|| ( isset( $newvalue['exclude_js'], $oldvalue['exclude_js'] ) && $newvalue['exclude_js'] !== $oldvalue['exclude_js'] )
|
||||
|| ( isset( $oldvalue['cdn'] ) && ! isset( $newvalue['cdn'] ) || ! isset( $oldvalue['cdn'] ) && isset( $newvalue['cdn'] ) )
|
||||
) {
|
||||
$newvalue['minify_js_key'] = create_rocket_uniqid();
|
||||
}
|
||||
|
||||
// Checked the SSL option if the whole website is on SSL.
|
||||
if ( rocket_is_ssl_website() ) {
|
||||
$newvalue['cache_ssl'] = 1;
|
||||
}
|
||||
|
||||
if ( 'local' === wp_get_environment_type() ) {
|
||||
$newvalue['optimize_css_delivery'] = 0;
|
||||
$newvalue['remove_unused_css'] = 0;
|
||||
$newvalue['async_css'] = 0;
|
||||
}
|
||||
|
||||
if ( ! rocket_get_constant( 'WP_ROCKET_ADVANCED_CACHE' ) ) {
|
||||
rocket_generate_advanced_cache_file();
|
||||
}
|
||||
|
||||
$keys = get_transient( WP_ROCKET_SLUG );
|
||||
|
||||
if ( $keys ) {
|
||||
delete_transient( WP_ROCKET_SLUG );
|
||||
$newvalue = array_merge( $newvalue, $keys );
|
||||
}
|
||||
|
||||
if ( ! $rocket_settings_errors ) {
|
||||
return $newvalue;
|
||||
}
|
||||
|
||||
$transient_errors = get_transient( 'settings_errors' );
|
||||
if ( ! $transient_errors || ! is_array( $transient_errors ) ) {
|
||||
$transient_errors = [];
|
||||
}
|
||||
|
||||
$settings_errors = array_merge( $transient_errors, $rocket_settings_errors );
|
||||
|
||||
foreach ( $settings_errors as $error ) {
|
||||
add_settings_error( $error['setting'], $error['code'], $error['message'], $error['type'] );
|
||||
}
|
||||
|
||||
return $newvalue;
|
||||
}
|
||||
|
||||
add_filter( 'pre_update_option_' . rocket_get_constant( 'WP_ROCKET_SLUG' ), 'rocket_pre_main_option', 10, 2 );
|
||||
|
||||
/**
|
||||
* Auto-activate the SSL cache if the website URL is updated with https protocol
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param array $old_value An array of previous options values.
|
||||
* @param array $value An array of submitted options values.
|
||||
*/
|
||||
function rocket_update_ssl_option_after_save_home_url( $old_value, $value ) {
|
||||
if ( $old_value === $value || get_rocket_option( 'cache_ssl' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$scheme = rocket_extract_url_component( $value, PHP_URL_SCHEME );
|
||||
|
||||
update_rocket_option( 'cache_ssl', 'https' === $scheme ? 1 : 0 );
|
||||
}
|
||||
add_action( 'update_option_home', 'rocket_update_ssl_option_after_save_home_url', 10, 2 );
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Add the CSS and JS files for WP Rocket options page
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
function rocket_add_admin_css_js() {
|
||||
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
|
||||
|
||||
wp_enqueue_style( 'wpr-admin', WP_ROCKET_ASSETS_CSS_URL . 'wpr-admin' . $suffix . '.css', null, WP_ROCKET_VERSION );
|
||||
wp_enqueue_script( 'micromodal', WP_ROCKET_ASSETS_JS_URL . 'micromodal.min.js', null, '0.4.10', true );
|
||||
wp_enqueue_script( 'wpr-admin', WP_ROCKET_ASSETS_JS_URL . 'wpr-admin' . $suffix . '.js', [ 'micromodal' ], WP_ROCKET_VERSION, true );
|
||||
|
||||
wp_localize_script(
|
||||
'wpr-admin',
|
||||
'rocket_ajax_data',
|
||||
/**
|
||||
* Filters the data passed to the localize script function for WP Rocket admin JS
|
||||
*
|
||||
* @since 3.7.4
|
||||
*
|
||||
* @param array $data Localize script data.
|
||||
*/
|
||||
apply_filters(
|
||||
'rocket_localize_admin_script',
|
||||
[
|
||||
'nonce' => wp_create_nonce( 'rocket-ajax' ),
|
||||
'origin_url' => untrailingslashit( rocket_get_constant( 'WP_ROCKET_WEB_MAIN' ) ),
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_rtl() ) {
|
||||
wp_enqueue_style( 'wpr-admin-rtl', WP_ROCKET_ASSETS_CSS_URL . 'wpr-admin-rtl' . $suffix . '.css', null, WP_ROCKET_VERSION );
|
||||
}
|
||||
}
|
||||
add_action( 'admin_print_styles-settings_page_' . WP_ROCKET_PLUGIN_SLUG, 'rocket_add_admin_css_js' );
|
||||
|
||||
/**
|
||||
* Add the CSS and JS files needed by WP Rocket everywhere on admin pages
|
||||
*
|
||||
* @since 2.1
|
||||
*/
|
||||
function rocket_add_admin_css_js_everywhere() {
|
||||
wp_enqueue_script( 'wpr-admin-common', WP_ROCKET_ASSETS_JS_URL . 'wpr-admin-common.js', [ 'jquery' ], WP_ROCKET_VERSION, true );
|
||||
wp_enqueue_style( 'wpr-admin-common', WP_ROCKET_ASSETS_CSS_URL . 'wpr-admin-common.css', [], WP_ROCKET_VERSION );
|
||||
}
|
||||
add_action( 'admin_enqueue_scripts', 'rocket_add_admin_css_js_everywhere', 11 );
|
||||
|
||||
/**
|
||||
* Adds mixpanel JS code in header when analytics data should be sent
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*/
|
||||
function rocket_add_mixpanel_code() {
|
||||
if ( rocket_send_analytics_data() ) {
|
||||
?>
|
||||
<!-- start Mixpanel --><script type="text/javascript">(function(e,a){if(!a.__SV){var b=window;try{var c,l,i,j=b.location,g=j.hash;c=function(a,b){return(l=a.match(RegExp(b+"=([^&]*)")))?l[1]:null};g&&c(g,"state")&&(i=JSON.parse(decodeURIComponent(c(g,"state"))),"mpeditor"===i.action&&(b.sessionStorage.setItem("_mpcehash",g),history.replaceState(i.desiredHash||"",e.title,j.pathname+j.search)))}catch(m){}var k,h;window.mixpanel=a;a._i=[];a.init=function(b,c,f){function e(b,a){var c=a.split(".");2==c.length&&(b=b[c[0]],a=c[1]);b[a]=function(){b.push([a].concat(Array.prototype.slice.call(arguments,
|
||||
0)))}}var d=a;"undefined"!==typeof f?d=a[f]=[]:f="mixpanel";d.people=d.people||[];d.toString=function(b){var a="mixpanel";"mixpanel"!==f&&(a+="."+f);b||(a+=" (stub)");return a};d.people.toString=function(){return d.toString(1)+".people (stub)"};k="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config reset people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");
|
||||
for(h=0;h<k.length;h++)e(d,k[h]);a._i.push([b,c,f])};a.__SV=1.2;b=e.createElement("script");b.type="text/javascript";b.async=!0;b.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";c=e.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)}})(document,window.mixpanel||[]);
|
||||
mixpanel.init("a36067b00a263cce0299cfd960e26ecf", {
|
||||
'ip':false,
|
||||
'property_blacklist': ['$initial_referrer', '$current_url', '$initial_referring_domain', '$referrer', '$referring_domain']
|
||||
} );
|
||||
mixpanel.track( 'WP Rocket', <?php echo wp_json_encode( rocket_analytics_data() ); ?> );
|
||||
mixpanel.track( 'Settings Sidebar Display', localStorage.getItem('wpr-show-sidebar') );
|
||||
</script><!-- end Mixpanel -->
|
||||
<?php
|
||||
}
|
||||
}
|
||||
add_action( 'admin_print_scripts', 'rocket_add_mixpanel_code' );
|
||||
|
||||
/**
|
||||
* Add CSS & JS files for the Imagify installation call to action
|
||||
*
|
||||
* @since 2.7
|
||||
*/
|
||||
function rocket_enqueue_modal_plugin() {
|
||||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
|
||||
|
||||
if ( defined( 'IMAGIFY_VERSION' ) || in_array( 'rocket_imagify_notice', (array) $boxes, true ) || 1 === get_option( 'wp_rocket_dismiss_imagify_notice' ) || ! current_user_can( 'manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
wp_enqueue_style( 'plugin-install' );
|
||||
|
||||
wp_enqueue_script( 'plugin-install' );
|
||||
wp_enqueue_script( 'updates' );
|
||||
add_thickbox();
|
||||
}
|
||||
add_action( 'admin_print_styles-media-new.php', 'rocket_enqueue_modal_plugin' );
|
||||
add_action( 'admin_print_styles-upload.php', 'rocket_enqueue_modal_plugin' );
|
||||
add_action( 'admin_print_styles-settings_page_' . WP_ROCKET_PLUGIN_SLUG, 'rocket_enqueue_modal_plugin' );
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Add a link "Purge cache" in the post submit area
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
function rocket_post_submitbox_start() {
|
||||
|
||||
if ( ! rocket_can_display_options() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( current_user_can( 'rocket_purge_posts' ) ) {
|
||||
global $post;
|
||||
|
||||
$cpts = get_post_types(
|
||||
[
|
||||
'public' => true,
|
||||
],
|
||||
'objects'
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the post type on submitbox.
|
||||
*
|
||||
* @since 3.12.1
|
||||
*
|
||||
* @param array $cpts Post Types.
|
||||
*/
|
||||
$cpts = apply_filters( 'rocket_submitbox_options_post_types', $cpts );
|
||||
|
||||
if ( isset( $cpts[ $post->post_type ] ) ) {
|
||||
$url = wp_nonce_url( admin_url( 'admin-post.php?action=purge_cache&type=post-' . $post->ID ), 'purge_cache_post-' . $post->ID );
|
||||
printf( '<div id="purge-action"><a class="button-secondary" href="%s">%s</a></div>', esc_url( $url ), esc_html__( 'Clear cache', 'rocket' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
add_action( 'post_submitbox_start', 'rocket_post_submitbox_start' );
|
||||
@@ -0,0 +1,796 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* This warnings are displayed when the plugin can not be deactivated correctly
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
function rocket_bad_deactivations() {
|
||||
global $current_user;
|
||||
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( 'plugins' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$msgs = get_transient( $current_user->ID . '_donotdeactivaterocket' );
|
||||
if ( current_user_can( 'rocket_manage_options' ) && $msgs ) {
|
||||
|
||||
delete_transient( $current_user->ID . '_donotdeactivaterocket' );
|
||||
$errors = [];
|
||||
|
||||
foreach ( $msgs as $msg ) {
|
||||
switch ( $msg ) {
|
||||
case 'wpconfig':
|
||||
$errors['wpconfig'] = '<p>' . sprintf(
|
||||
// translators: %1$s WP Rocket plugin name; %2$s = file name.
|
||||
__(
|
||||
'<strong>%1$s</strong> has not been deactivated due to missing writing permissions.<br>
|
||||
Make <strong>%2$s</strong> writeable and retry deactivation, or force deactivation now:',
|
||||
'rocket'
|
||||
),
|
||||
WP_ROCKET_PLUGIN_NAME,
|
||||
'wp-config.php'
|
||||
) . '</p>';
|
||||
break;
|
||||
|
||||
case 'htaccess':
|
||||
$errors['htaccess'] = '<p>' . sprintf(
|
||||
// translators: %1$s WP Rocket plugin name; %2$s = file name.
|
||||
__(
|
||||
'<strong>%1$s</strong> has not been deactivated due to missing writing permissions.<br>
|
||||
Make <strong>%2$s</strong> writeable and retry deactivation, or force deactivation now:',
|
||||
'rocket'
|
||||
),
|
||||
WP_ROCKET_PLUGIN_NAME,
|
||||
'.htaccess'
|
||||
) . '</p>';
|
||||
break;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the output messages for each bad deactivation attempt.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param array $errors Contains the error messages to be filtered
|
||||
* @param string $msg Contains the error type (wpconfig or htaccess)
|
||||
*/
|
||||
$errors = apply_filters( 'rocket_bad_deactivations', $errors, $msg );
|
||||
|
||||
}
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'dismissible' => '',
|
||||
'message' => implode( '', $errors ),
|
||||
'action' => 'force_deactivation',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_bad_deactivations' );
|
||||
|
||||
/**
|
||||
* This warning is displayed to inform the user that a plugin de/activation can be followed by a cache clear
|
||||
*
|
||||
* @since 1.3.0
|
||||
*/
|
||||
function rocket_warning_plugin_modification() {
|
||||
if ( current_user_can( 'rocket_manage_options' ) && rocket_valid_key() ) {
|
||||
|
||||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
|
||||
|
||||
if ( in_array( __FUNCTION__, (array) $boxes, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'warning',
|
||||
'dismissible' => '',
|
||||
// translators: %s is WP Rocket plugin name.
|
||||
'message' => sprintf( __( '<strong>%s</strong>: One or more plugins have been enabled or disabled, clear the cache if they affect the front end of your site.', 'rocket' ), WP_ROCKET_PLUGIN_NAME ),
|
||||
'action' => 'clear_cache',
|
||||
'dismiss_button' => __FUNCTION__,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_warning_plugin_modification' );
|
||||
|
||||
/**
|
||||
* This warning is displayed when some plugins may conflict with WP Rocket
|
||||
*
|
||||
* @since 1.3.0
|
||||
*/
|
||||
function rocket_plugins_to_deactivate() {
|
||||
$plugins = [];
|
||||
$plugins_explanations = [];
|
||||
|
||||
/**
|
||||
* Filter the recommended plugins to deactivate to prevent conflicts
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param array $plugins List of recommended plugins to deactivate.
|
||||
*/
|
||||
$plugins = apply_filters( 'rocket_plugins_to_deactivate', $plugins );
|
||||
|
||||
/**
|
||||
* Filter the recommended plugins to deactivate explanations
|
||||
*
|
||||
* @since 3.10.5
|
||||
*
|
||||
* @param array $plugins List of recommended plugins to deactivate explanations.
|
||||
*/
|
||||
$plugins_explanations = apply_filters( 'rocket_plugins_to_deactivate_explanations', $plugins_explanations );
|
||||
|
||||
$plugins = array_filter( $plugins, 'is_plugin_active' );
|
||||
|
||||
if ( current_user_can( 'rocket_manage_options' )
|
||||
&& count( $plugins )
|
||||
&& rocket_valid_key()
|
||||
) {
|
||||
|
||||
// translators: %s is WP Rocket plugin name.
|
||||
$warning = '<p>' . sprintf( __( '<strong>%s</strong>: The following plugins are not compatible with this plugin and may cause unexpected results:', 'rocket' ), WP_ROCKET_PLUGIN_NAME ) . '</p>';
|
||||
|
||||
$warning .= '<ul class="rocket-plugins-error">';
|
||||
|
||||
foreach ( $plugins as $k => $plugin ) {
|
||||
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin );
|
||||
$warning .= '<li><b>' . $plugin_data['Name'] . '</b>' . ( isset( $plugins_explanations[ $k ] ) ? ' - ' . $plugins_explanations[ $k ] : '' ) . '</span> <a href="' . wp_nonce_url( admin_url( 'admin-post.php?action=deactivate_plugin&plugin=' . rawurlencode( $plugin ) ), 'deactivate_plugin' ) . '" class="button-secondary alignright">' . __( 'Deactivate', 'rocket' ) . '</a></li>';
|
||||
}
|
||||
|
||||
$warning .= '</ul>';
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'dismissible' => '',
|
||||
'message' => $warning,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_plugins_to_deactivate' );
|
||||
|
||||
/**
|
||||
* Displays a warning if Rocket Footer JS plugin is active
|
||||
*
|
||||
* @since 3.2.3
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function rocket_warning_footer_js_plugin() {
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 'settings_page_wprocket' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! is_plugin_active( 'rocket-footer-js/rocket-footer-js.php' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'warning',
|
||||
'message' => __( 'WP Rocket Footer JS is not an official add-on. It prevents some options in WP Rocket from working correctly. Please deactivate it if you have problems.', 'rocket' ),
|
||||
'dismiss_button' => true,
|
||||
]
|
||||
);
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_warning_footer_js_plugin' );
|
||||
|
||||
/**
|
||||
* Display a warning if Endurance Cache is not disabled
|
||||
*
|
||||
* @since 3.3.7
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function rocket_warning_endurance_cache() {
|
||||
$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;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'Endurance_Page_Cache' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 0 === (int) get_option( 'endurance_cache_level' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'message' => sprintf(
|
||||
// translators: %1$s = opening link tag, %2$s = closing link tag.
|
||||
__( 'Endurance Cache is currently enabled, which will conflict with WP Rocket Cache. Please set the Endurance Cache cache level to Off (Level 0) on the %1$sSettings > General%2$s page to prevent any issues.', 'rocket' ),
|
||||
'<a href="' . admin_url( 'options-general.php#epc_settings' ) . '">',
|
||||
'</a>'
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_warning_endurance_cache' );
|
||||
|
||||
/**
|
||||
* This warning is displayed when there is no permalink structure in the configuration.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
function rocket_warning_using_permalinks() {
|
||||
if ( current_user_can( 'rocket_manage_options' )
|
||||
&& ! $GLOBALS['wp_rewrite']->using_permalinks()
|
||||
&& rocket_valid_key()
|
||||
) {
|
||||
$message = sprintf(
|
||||
/* translators: %1$s WP Rocket plugin name; %2$s = opening link; %3$s = closing link */
|
||||
__( '%1$s: A custom permalink structure is required for the plugin to work properly. %2$sGo to permalinks settings%3$s', 'rocket' ),
|
||||
'<strong>' . WP_ROCKET_PLUGIN_NAME . '</strong>',
|
||||
'<a href="' . admin_url( 'options-permalink.php' ) . '">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'dismissible' => '',
|
||||
'message' => $message,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_warning_using_permalinks' );
|
||||
|
||||
/**
|
||||
* This warning is displayed when the .htaccess file doesn't exist or isn't writeable
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
function rocket_warning_htaccess_permissions() {
|
||||
global $is_apache;
|
||||
$htaccess_file = get_home_path() . '.htaccess';
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' )
|
||||
|| ( rocket_direct_filesystem()->is_writable( $htaccess_file ) )
|
||||
|| ! $is_apache
|
||||
// This filter is documented in inc/functions/htaccess.php.
|
||||
|| apply_filters( 'rocket_disable_htaccess', false )
|
||||
|| ! rocket_valid_key() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( rocket_check_htaccess_rules() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
|
||||
|
||||
if ( in_array( __FUNCTION__, (array) $boxes, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$message = sprintf(
|
||||
// translators: %s = plugin name.
|
||||
__( '%s could not modify the .htaccess file due to missing writing permissions.', 'rocket' ),
|
||||
'<strong>' . WP_ROCKET_PLUGIN_NAME . '</strong>'
|
||||
);
|
||||
|
||||
$message .= '<br>' . sprintf(
|
||||
/* translators: This is a doc title! %1$s = opening link; %2$s = closing link */
|
||||
__( 'Troubleshoot: %1$sHow to make system files writeable%2$s', 'rocket' ),
|
||||
/* translators: Documentation exists in EN, DE, FR, ES, IT; use loaclised URL if applicable */
|
||||
'<a href="' . __( 'https://docs.wp-rocket.me/article/626-how-to-make-system-files-htaccess-wp-config-writeable/?utm_source=wp_plugin&utm_medium=wp_rocket', 'rocket' ) . '" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
add_filter( 'rocket_htaccess_mod_rewrite', '__return_false', 42 );
|
||||
|
||||
$message .= '<p>' . __( 'Don’t worry, WP Rocket’s page caching and settings will still function correctly.', 'rocket' ) . '<br>' . __( 'For optimal performance, adding the following lines into your .htaccess is recommended (not required):', 'rocket' ) . '<br><textarea readonly="readonly" id="rocket_htaccess_rules" name="rocket_htaccess_rules" class="large-text readonly" rows="6">' . esc_textarea( get_rocket_htaccess_marker() ) . '</textarea></p>';
|
||||
|
||||
remove_filter( 'rocket_htaccess_mod_rewrite', '__return_false', 42 );
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'warning',
|
||||
'dismissible' => '',
|
||||
'message' => $message,
|
||||
'dismiss_button' => __FUNCTION__,
|
||||
]
|
||||
);
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_warning_htaccess_permissions' );
|
||||
|
||||
/**
|
||||
* This warning is displayed when the config dir isn't writeable
|
||||
*
|
||||
* @since 2.0.2
|
||||
*/
|
||||
function rocket_warning_config_dir_permissions() {
|
||||
if ( current_user_can( 'rocket_manage_options' )
|
||||
&& ( ! rocket_direct_filesystem()->is_writable( WP_ROCKET_CONFIG_PATH ) )
|
||||
&& rocket_valid_key() ) {
|
||||
|
||||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
|
||||
|
||||
if ( in_array( __FUNCTION__, (array) $boxes, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$message = rocket_notice_writing_permissions( trim( str_replace( ABSPATH, '', WP_ROCKET_CONFIG_PATH ), '/' ) );
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'dismissible' => '',
|
||||
'message' => $message,
|
||||
]
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_warning_config_dir_permissions' );
|
||||
|
||||
/**
|
||||
* This warning is displayed when the cache dir isn't writeable
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
function rocket_warning_cache_dir_permissions() {
|
||||
if ( current_user_can( 'rocket_manage_options' )
|
||||
&& ( ! rocket_direct_filesystem()->is_writable( WP_ROCKET_CACHE_PATH ) )
|
||||
&& rocket_valid_key() ) {
|
||||
|
||||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
|
||||
|
||||
if ( in_array( __FUNCTION__, (array) $boxes, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$message = rocket_notice_writing_permissions( trim( str_replace( ABSPATH, '', WP_ROCKET_CACHE_PATH ), '/' ) );
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'dismissible' => '',
|
||||
'message' => $message,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_warning_cache_dir_permissions' );
|
||||
|
||||
/**
|
||||
* This warning is displayed when the minify cache dir isn't writeable
|
||||
*
|
||||
* @since 2.1
|
||||
*/
|
||||
function rocket_warning_minify_cache_dir_permissions() {
|
||||
if ( current_user_can( 'rocket_manage_options' )
|
||||
&& ( ! rocket_direct_filesystem()->is_writable( WP_ROCKET_MINIFY_CACHE_PATH ) )
|
||||
&& ( get_rocket_option( 'minify_css', false ) || get_rocket_option( 'minify_js', false ) )
|
||||
&& rocket_valid_key() ) {
|
||||
|
||||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
|
||||
|
||||
if ( in_array( __FUNCTION__, (array) $boxes, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$message = rocket_notice_writing_permissions( trim( str_replace( ABSPATH, '', WP_ROCKET_MINIFY_CACHE_PATH ), '/' ) );
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'dismissible' => '',
|
||||
'message' => $message,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_warning_minify_cache_dir_permissions' );
|
||||
|
||||
/**
|
||||
* This warning is displayed when the busting cache dir isn't writeable
|
||||
*
|
||||
* @since 2.9
|
||||
*/
|
||||
function rocket_warning_busting_cache_dir_permissions() {
|
||||
if ( current_user_can( 'rocket_manage_options' )
|
||||
&& ( ! rocket_direct_filesystem()->is_writable( WP_ROCKET_CACHE_BUSTING_PATH ) )
|
||||
&& rocket_valid_key() ) {
|
||||
|
||||
$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );
|
||||
|
||||
if ( in_array( __FUNCTION__, (array) $boxes, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$message = rocket_notice_writing_permissions( trim( str_replace( ABSPATH, '', WP_ROCKET_CACHE_BUSTING_PATH ), '/' ) );
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'dismissible' => '',
|
||||
'message' => $message,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_warning_busting_cache_dir_permissions' );
|
||||
|
||||
/**
|
||||
* Confirming notice when the site has been added
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
function rocket_thank_you_license() {
|
||||
if ( '1' === get_rocket_option( 'license' ) ) {
|
||||
$options = get_option( WP_ROCKET_SLUG );
|
||||
$options['license'] = time();
|
||||
$options['ignore'] = true;
|
||||
update_option( WP_ROCKET_SLUG, $options );
|
||||
|
||||
$message = sprintf(
|
||||
/* translators: %1$s = plugin name, %2$s + %3$s = opening links, %4$s = closing link */
|
||||
__( '%1$s is good to go! %2$sTest your load time%4$s, or visit your %3$ssettings%4$s.', 'rocket' ),
|
||||
'<strong>' . WP_ROCKET_PLUGIN_NAME . '</strong>',
|
||||
'<a href="https://wp-rocket.me/blog/how-to-test-wordpress-site-performance-measure-speed-results/?utm_source=wp_plugin&utm_medium=wp_rocket" target="_blank">',
|
||||
'<a href="' . admin_url( 'options-general.php?page=' . WP_ROCKET_PLUGIN_SLUG ) . '">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
rocket_notice_html( [ 'message' => $message ] );
|
||||
}
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_thank_you_license' );
|
||||
|
||||
/**
|
||||
* Displays a notice for analytics opt-in
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*/
|
||||
function rocket_analytics_optin_notice() {
|
||||
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 'settings_page_wprocket' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 1 === (int) get_option( 'rocket_analytics_notice_displayed' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( get_rocket_option( 'analytics_enabled' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$analytics_notice = sprintf(
|
||||
// Opening <p> provided by rocket_notice_html().
|
||||
'<strong>%1$s</strong><br>%2$s</p>',
|
||||
__( 'Would you allow WP Rocket to collect non-sensitive diagnostic data from this website?', 'rocket' ),
|
||||
__( 'This would help us to improve WP Rocket for you in the future.', 'rocket' )
|
||||
);
|
||||
|
||||
$analytics_notice .= sprintf(
|
||||
'<p><button class="hide-if-no-js button-rocket-reveal rocket-preview-analytics-data">%s</button></p>',
|
||||
/* translators: button text, click will expand data collection preview */
|
||||
__( 'What info will we collect?', 'rocket' )
|
||||
);
|
||||
|
||||
$analytics_notice .= sprintf(
|
||||
'<div class="rocket-analytics-data-container"><p class="description">%1$s</p>%2$s</div>',
|
||||
__( 'Below is a detailed view of all data WP Rocket will collect if granted permission. WP Rocket will never transmit any domain names or email addresses (except for license validation), IP addresses, or third-party API keys.', 'rocket' ),
|
||||
rocket_data_collection_preview_table()
|
||||
);
|
||||
|
||||
$analytics_notice .= sprintf(
|
||||
'<p><a href="%1$s" class="button button-primary">%2$s</a> <a href="%3$s" class="button button-secondary">%4$s</a>',
|
||||
// Closing </p> provided by rocket_notice_html().
|
||||
wp_nonce_url( admin_url( 'admin-post.php?action=rocket_analytics_optin&value=yes' ), 'analytics_optin' ),
|
||||
/* translators: button text for data collection opt-in */
|
||||
__( 'Yes, allow', 'rocket' ),
|
||||
wp_nonce_url( admin_url( 'admin-post.php?action=rocket_analytics_optin&value=no' ), 'analytics_optin' ),
|
||||
/* translators: button text for data collection opt-in */
|
||||
__( 'No, thanks', 'rocket' )
|
||||
);
|
||||
|
||||
// Status should be as neutral as possible; nothing has happened yet.
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'info',
|
||||
'message' => $analytics_notice,
|
||||
]
|
||||
);
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_analytics_optin_notice' );
|
||||
|
||||
/**
|
||||
* Displays a notice after analytics opt-in
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*/
|
||||
function rocket_analytics_optin_thankyou_notice() {
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 'settings_page_wprocket' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$analytics_optin = get_transient( 'rocket_analytics_optin' );
|
||||
|
||||
if ( ! $analytics_optin ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$thankyou_message = sprintf(
|
||||
// Opening <p> provided by rocket_notice_html().
|
||||
'<strong>%s</strong></p>',
|
||||
__( 'Thank you!', 'rocket' )
|
||||
);
|
||||
|
||||
$thankyou_message .= sprintf(
|
||||
'<p>%1$s</p><div>%2$s</div>',
|
||||
__( 'WP Rocket now collects these metrics from your website:', 'rocket' ),
|
||||
rocket_data_collection_preview_table()
|
||||
);
|
||||
|
||||
// Closing </p> provided by rocket_notice_html().
|
||||
$thankyou_message .= '<p>';
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'message' => $thankyou_message,
|
||||
]
|
||||
);
|
||||
|
||||
delete_transient( 'rocket_analytics_optin' );
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_analytics_optin_thankyou_notice' );
|
||||
|
||||
/**
|
||||
* Displays a notice after clearing the cache
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*/
|
||||
function rocket_clear_cache_notice() {
|
||||
$cleared_cache = get_transient( 'rocket_clear_cache' );
|
||||
|
||||
if ( ! $cleared_cache ) {
|
||||
return;
|
||||
}
|
||||
|
||||
delete_transient( 'rocket_clear_cache' );
|
||||
|
||||
$notice = '';
|
||||
|
||||
switch ( $cleared_cache ) {
|
||||
case 'all':
|
||||
if ( current_user_can( 'rocket_purge_cache' ) ) {
|
||||
// translators: %s = plugin name.
|
||||
$notice = sprintf( __( '%s: Cache cleared.', 'rocket' ), '<strong>' . WP_ROCKET_PLUGIN_NAME . '</strong>' );
|
||||
$notice .= '<em> (' . date_i18n( get_option( 'date_format' ) ) . ' @ ' . date_i18n( get_option( 'time_format' ) ) . ') </em>';
|
||||
}
|
||||
break;
|
||||
case 'post':
|
||||
if ( current_user_can( 'rocket_purge_posts' ) ) {
|
||||
// translators: %s = plugin name.
|
||||
$notice = sprintf( __( '%s: Post cache cleared.', 'rocket' ), '<strong>' . WP_ROCKET_PLUGIN_NAME . '</strong>' );
|
||||
$notice .= '<em> (' . date_i18n( get_option( 'date_format' ) ) . ' @ ' . date_i18n( get_option( 'time_format' ) ) . ') </em>';
|
||||
}
|
||||
break;
|
||||
case 'term':
|
||||
if ( current_user_can( 'rocket_purge_terms' ) ) {
|
||||
// translators: %s = plugin name.
|
||||
$notice = sprintf( __( '%s: Term cache cleared.', 'rocket' ), '<strong>' . WP_ROCKET_PLUGIN_NAME . '</strong>' );
|
||||
$notice .= '<em> (' . date_i18n( get_option( 'date_format' ) ) . ' @ ' . date_i18n( get_option( 'time_format' ) ) . ') </em>';
|
||||
}
|
||||
break;
|
||||
case 'user':
|
||||
if ( current_user_can( 'rocket_purge_users' ) ) {
|
||||
// translators: %s = plugin name).
|
||||
$notice = sprintf( __( '%s: User cache cleared.', 'rocket' ), '<strong>' . WP_ROCKET_PLUGIN_NAME . '</strong>' );
|
||||
$notice .= '<em> (' . date_i18n( get_option( 'date_format' ) ) . ' @ ' . date_i18n( get_option( 'time_format' ) ) . ') </em>';
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if ( empty( $notice ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'message' => $notice,
|
||||
]
|
||||
);
|
||||
}
|
||||
add_action( 'admin_notices', 'rocket_clear_cache_notice' );
|
||||
|
||||
/**
|
||||
* Outputs notice HTML
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $args An array of arguments used to determine the notice output.
|
||||
* @return void
|
||||
*/
|
||||
function rocket_notice_html( $args ) {
|
||||
$defaults = [
|
||||
'status' => 'success',
|
||||
'dismissible' => 'is-dismissible',
|
||||
'message' => '',
|
||||
'action' => '',
|
||||
'dismiss_button' => false,
|
||||
'dismiss_button_message' => __( 'Dismiss this notice', 'rocket' ),
|
||||
'readonly_content' => '',
|
||||
'id' => '',
|
||||
];
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
|
||||
switch ( $args['action'] ) {
|
||||
case 'clear_cache':
|
||||
$args['action'] = '<a class="wp-core-ui button" href="' . wp_nonce_url( admin_url( 'admin-post.php?action=purge_cache&type=all' ), 'purge_cache_all' ) . '">' . __( 'Clear cache', 'rocket' ) . '</a>';
|
||||
break;
|
||||
case 'clear_used_css':
|
||||
$params = [
|
||||
'action' => 'rocket_clear_usedcss',
|
||||
];
|
||||
|
||||
if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
|
||||
$referer_url = filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
|
||||
$params['_wp_http_referer'] = rawurlencode( $referer_url );
|
||||
}
|
||||
$args['action'] = '<a class="wp-core-ui button" href="' . add_query_arg( $params, wp_nonce_url( admin_url( 'admin-post.php' ), $params['action'] ) ) . '">' . __( 'Clear Used CSS', 'rocket' ) . '</a>';
|
||||
break;
|
||||
case 'stop_preload':
|
||||
$args['action'] = '<a class="wp-core-ui button" href="' . wp_nonce_url( admin_url( 'admin-post.php?action=rocket_stop_preload&type=all' ), 'rocket_stop_preload' ) . '">' . __( 'Stop Preload', 'rocket' ) . '</a>';
|
||||
break;
|
||||
case 'switch_to_rucss':
|
||||
$params = [
|
||||
'action' => 'switch_to_rucss',
|
||||
];
|
||||
$args['action'] = '<a class="wp-core-ui button" href="' . add_query_arg( $params, wp_nonce_url( admin_url( 'admin-post.php' ), 'rucss_switch' ) ) . '">' . __( 'Turn on Remove Unused CSS', 'rocket' ) . '</a>';
|
||||
break;
|
||||
case 'enable_separate_mobile_cache':
|
||||
$params = [
|
||||
'action' => 'rocket_enable_separate_mobile_cache',
|
||||
];
|
||||
$args['action'] = '<a class="wp-core-ui button" href="' . add_query_arg( $params, wp_nonce_url( admin_url( 'admin-post.php' ), 'rocket_enable_separate_mobile_cache' ) ) . '">' . __( 'Enable “Separate Cache Files for Mobile Devices” now', 'rocket' ) . '</a>';
|
||||
break;
|
||||
case 'force_deactivation':
|
||||
/**
|
||||
* Allow a "force deactivation" link to be printed, use at your own risks
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param bool $permit_force_deactivation true will print the link.
|
||||
*/
|
||||
$permit_force_deactivation = apply_filters( 'rocket_permit_force_deactivation', true );
|
||||
|
||||
// We add a link to permit "force deactivation", use at your own risks.
|
||||
if ( $permit_force_deactivation ) {
|
||||
global $status, $page, $s;
|
||||
$plugin_file = 'wp-rocket/wp-rocket.php';
|
||||
$rocket_nonce = wp_create_nonce( 'force_deactivation' );
|
||||
|
||||
$args['action'] = '<a href="' . wp_nonce_url( 'plugins.php?action=deactivate&rocket_nonce=' . $rocket_nonce . '&plugin=' . $plugin_file . '&plugin_status=' . $status . '&paged=' . $page . '&s=' . $s, 'deactivate-plugin_' . $plugin_file ) . '">' . __( 'Force deactivation ', 'rocket' ) . '</a>';
|
||||
}
|
||||
break;
|
||||
}
|
||||
/**
|
||||
* Notice arguments.
|
||||
*
|
||||
* @param array $args arguments from the notice.
|
||||
* @return array
|
||||
*/
|
||||
$filtered_args = apply_filters( 'rocket_notice_args', $args );
|
||||
|
||||
if ( is_array( $filtered_args ) ) {
|
||||
$args = wp_parse_args( $filtered_args, $defaults );
|
||||
}
|
||||
|
||||
$notice_id = '';
|
||||
|
||||
if ( ! empty( $args['id'] ) ) {
|
||||
$notice_id = ' id="' . esc_attr( $args['id'] ) . '"';
|
||||
}
|
||||
|
||||
?>
|
||||
<div class="notice notice-<?php echo esc_attr( $args['status'] ); ?> <?php echo esc_attr( $args['dismissible'] ); ?>"<?php echo $notice_id; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
|
||||
<?php
|
||||
$tag = 0 !== strpos( $args['message'], '<p' ) && 0 !== strpos( $args['message'], '<ul' );
|
||||
|
||||
echo ( $tag ? '<p>' : '' ) . $args['message'] . ( $tag ? '</p>' : '' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
|
||||
?>
|
||||
<?php if ( ! empty( $args['readonly_content'] ) ) : ?>
|
||||
<p><?php esc_html_e( 'The following code should have been written to this file:', 'rocket' ); ?>
|
||||
<br><textarea readonly="readonly" id="rules" name="rules" class="large-text readonly" rows="6"><?php echo esc_textarea( $args['readonly_content'] ); ?></textarea>
|
||||
</p>
|
||||
<?php
|
||||
endif;
|
||||
if ( $args['action'] || $args['dismiss_button'] ) :
|
||||
?>
|
||||
<p>
|
||||
<?php echo $args['action']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
|
||||
<?php if ( $args['dismiss_button'] ) : ?>
|
||||
<a class="rocket-dismiss <?php echo $args['dismiss_button_class'] ?? ''; ?>" href="<?php echo wp_nonce_url( admin_url( 'admin-post.php?action=rocket_ignore&box=' . $args['dismiss_button'] ), 'rocket_ignore_' . $args['dismiss_button'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>"><?php echo esc_html( $args['dismiss_button_message'] ); ?></a>
|
||||
<?php endif; ?>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs formatted notice about issues with writing permissions
|
||||
*
|
||||
* @since 2.11
|
||||
* @author Caspar Hübinger
|
||||
*
|
||||
* @param string $file File or folder name.
|
||||
* @return string Message HTML
|
||||
*/
|
||||
function rocket_notice_writing_permissions( $file ) {
|
||||
|
||||
$message = sprintf(
|
||||
// translators: %s = plugin name.
|
||||
__( '%s cannot configure itself due to missing writing permissions.', 'rocket' ),
|
||||
'<strong>' . WP_ROCKET_PLUGIN_NAME . '</strong>'
|
||||
);
|
||||
|
||||
$message .= '<br>' . sprintf(
|
||||
/* translators: %s = file/folder name */
|
||||
__( 'Affected file/folder: %s', 'rocket' ),
|
||||
'<code>' . $file . '</code>'
|
||||
);
|
||||
|
||||
$message .= '<br>' . sprintf(
|
||||
/* translators: This is a doc title! %1$s = opening link; %2$s = closing link */
|
||||
__( 'Troubleshoot: %1$sHow to make system files writeable%2$s', 'rocket' ),
|
||||
/* translators: Documentation exists in EN, DE, FR, ES, IT; use loaclised URL if applicable */
|
||||
'<a href="' . __( 'https://docs.wp-rocket.me/article/626-how-to-make-system-files-htaccess-wp-config-writeable/?utm_source=wp_plugin&utm_medium=wp_rocket', 'rocket' ) . '" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
<?php
|
||||
use WP_Rocket\Logger\Logger;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Tell WP what to do when admin is loaded aka upgrader
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
function rocket_upgrader() {
|
||||
// Grab some infos.
|
||||
$actual_version = (string) get_rocket_option( 'version' );
|
||||
// You can hook the upgrader to trigger any action when WP Rocket is upgraded.
|
||||
// first install.
|
||||
if ( ! $actual_version ) {
|
||||
do_action( 'wp_rocket_first_install' );
|
||||
}
|
||||
// already installed but got updated.
|
||||
elseif ( WP_ROCKET_VERSION !== $actual_version ) {
|
||||
do_action( 'wp_rocket_upgrade', WP_ROCKET_VERSION, $actual_version );
|
||||
}
|
||||
|
||||
// If any upgrade has been done, we flush and update version number.
|
||||
if ( did_action( 'wp_rocket_first_install' ) || did_action( 'wp_rocket_upgrade' ) ) {
|
||||
flush_rocket_htaccess();
|
||||
|
||||
$options = get_option( WP_ROCKET_SLUG ); // do not use get_rocket_option() here.
|
||||
$options['version'] = WP_ROCKET_VERSION;
|
||||
|
||||
$keys = rocket_check_key();
|
||||
if ( is_array( $keys ) ) {
|
||||
$options = array_merge( $keys, $options );
|
||||
}
|
||||
|
||||
update_option( WP_ROCKET_SLUG, $options );
|
||||
}
|
||||
|
||||
$page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
if (
|
||||
'wprocket' === $page
|
||||
&&
|
||||
did_action( 'wp_rocket_upgrade' )
|
||||
) {
|
||||
wp_safe_redirect( esc_url_raw( admin_url( 'options-general.php?page=wprocket' ) ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
if ( ! rocket_valid_key() && current_user_can( 'rocket_manage_options' ) && 'wprocket' === $page ) {
|
||||
add_action( 'admin_notices', 'rocket_need_api_key' );
|
||||
}
|
||||
}
|
||||
add_action( 'admin_init', 'rocket_upgrader' );
|
||||
|
||||
/**
|
||||
* Keeps this function up to date at each version
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
function rocket_first_install() {
|
||||
// Generate an random key for cache dir of user.
|
||||
$secret_cache_key = create_rocket_uniqid();
|
||||
|
||||
// Generate an random key for minify md5 filename.
|
||||
$minify_css_key = create_rocket_uniqid();
|
||||
$minify_js_key = create_rocket_uniqid();
|
||||
|
||||
// Create Option.
|
||||
add_option(
|
||||
rocket_get_constant( 'WP_ROCKET_SLUG' ),
|
||||
/**
|
||||
* Filters the default rocket options array
|
||||
*
|
||||
* @since 2.8
|
||||
*
|
||||
* @param array Array of default rocket options
|
||||
*/
|
||||
apply_filters(
|
||||
'rocket_first_install_options',
|
||||
[
|
||||
'secret_cache_key' => $secret_cache_key,
|
||||
'cache_mobile' => 1,
|
||||
'do_caching_mobile_files' => 0,
|
||||
'cache_webp' => 0,
|
||||
'cache_logged_user' => 0,
|
||||
'cache_ssl' => 1,
|
||||
'emoji' => 1,
|
||||
'cache_reject_uri' => [],
|
||||
'cache_reject_cookies' => [],
|
||||
'cache_reject_ua' => [],
|
||||
'cache_query_strings' => [],
|
||||
'cache_purge_pages' => [],
|
||||
'purge_cron_interval' => 10,
|
||||
'purge_cron_unit' => 'HOUR_IN_SECONDS',
|
||||
'exclude_css' => [],
|
||||
'exclude_js' => [],
|
||||
'exclude_inline_js' => [],
|
||||
'defer_all_js' => 0,
|
||||
'async_css' => 0,
|
||||
'critical_css' => '',
|
||||
'lazyload' => 0,
|
||||
'lazyload_iframes' => 0,
|
||||
'lazyload_youtube' => 0,
|
||||
'minify_css' => 0,
|
||||
'minify_css_key' => $minify_css_key,
|
||||
'minify_js' => 0,
|
||||
'minify_js_key' => $minify_js_key,
|
||||
'minify_concatenate_js' => 0,
|
||||
'minify_google_fonts' => 1,
|
||||
'manual_preload' => 1,
|
||||
'dns_prefetch' => 0,
|
||||
'preload_fonts' => [],
|
||||
'database_revisions' => 0,
|
||||
'database_auto_drafts' => 0,
|
||||
'database_trashed_posts' => 0,
|
||||
'database_spam_comments' => 0,
|
||||
'database_trashed_comments' => 0,
|
||||
'database_all_transients' => 0,
|
||||
'database_optimize_tables' => 0,
|
||||
'schedule_automatic_cleanup' => 0,
|
||||
'automatic_cleanup_frequency' => 'daily',
|
||||
'cdn' => 0,
|
||||
'cdn_cnames' => [],
|
||||
'cdn_zone' => [],
|
||||
'cdn_reject_files' => [],
|
||||
'do_cloudflare' => 0,
|
||||
'cloudflare_email' => '',
|
||||
'cloudflare_api_key' => '',
|
||||
'cloudflare_zone_id' => '',
|
||||
'cloudflare_devmode' => 0,
|
||||
'cloudflare_protocol_rewrite' => 0,
|
||||
'cloudflare_auto_settings' => 0,
|
||||
'cloudflare_old_settings' => '',
|
||||
'control_heartbeat' => 1,
|
||||
'heartbeat_site_behavior' => 'reduce_periodicity',
|
||||
'heartbeat_admin_behavior' => 'reduce_periodicity',
|
||||
'heartbeat_editor_behavior' => 'reduce_periodicity',
|
||||
'varnish_auto_purge' => 0,
|
||||
'analytics_enabled' => 0,
|
||||
'sucury_waf_cache_sync' => 0,
|
||||
'sucury_waf_api_key' => '',
|
||||
]
|
||||
)
|
||||
);
|
||||
rocket_dismiss_box( 'rocket_warning_plugin_modification' );
|
||||
}
|
||||
add_action( 'wp_rocket_first_install', 'rocket_first_install' );
|
||||
|
||||
/**
|
||||
* What to do when Rocket is updated, depending on versions
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $wp_rocket_version Latest WP Rocket version.
|
||||
* @param string $actual_version Installed WP Rocket version.
|
||||
*/
|
||||
function rocket_new_upgrade( $wp_rocket_version, $actual_version ) {
|
||||
if ( version_compare( $actual_version, '2.4.1', '<' ) ) {
|
||||
delete_transient( 'rocket_ask_for_update' );
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '2.8', '<' ) ) {
|
||||
$options = get_option( WP_ROCKET_SLUG );
|
||||
$options['manual_preload'] = 1;
|
||||
$options['automatic_preload'] = 1;
|
||||
$options['sitemap_preload_url_crawl'] = '500000';
|
||||
|
||||
update_option( WP_ROCKET_SLUG, $options );
|
||||
}
|
||||
|
||||
// Deactivate CloudFlare completely if PHP Version is lower than 5.4.
|
||||
if ( version_compare( $actual_version, '2.8.16', '<' ) ) {
|
||||
$options = get_option( WP_ROCKET_SLUG );
|
||||
$options['do_cloudflare'] = 0;
|
||||
$options['cloudflare_email'] = '';
|
||||
$options['cloudflare_api_key'] = '';
|
||||
$options['cloudflare_devmode'] = 0;
|
||||
$options['cloudflare_protocol_rewrite'] = 0;
|
||||
$options['cloudflare_auto_settings'] = 0;
|
||||
$options['cloudflare_old_settings'] = '';
|
||||
|
||||
update_option( WP_ROCKET_SLUG, $options );
|
||||
}
|
||||
|
||||
// Disable minification options if they're active in Autoptimize.
|
||||
if ( version_compare( $actual_version, '2.9.5', '<' ) ) {
|
||||
if ( is_plugin_active( 'autoptimize/autoptimize.php' ) ) {
|
||||
if ( 'on' === get_option( 'autoptimize_css' ) ) {
|
||||
update_rocket_option( 'minify_css', 0 );
|
||||
}
|
||||
|
||||
if ( 'on' === get_option( 'autoptimize_js' ) ) {
|
||||
update_rocket_option( 'minify_js', 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete old transients.
|
||||
if ( version_compare( $actual_version, '2.9.7', '<' ) ) {
|
||||
delete_transient( 'rocket_check_licence_30' );
|
||||
delete_transient( 'rocket_check_licence_1' );
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '2.11', '<' ) ) {
|
||||
rocket_clean_minify();
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.2', '<' ) ) {
|
||||
// Default Heartbeat settings.
|
||||
$options = get_option( WP_ROCKET_SLUG, [] );
|
||||
$options['heartbeat_site_behavior'] = 'reduce_periodicity';
|
||||
$options['heartbeat_admin_behavior'] = 'reduce_periodicity';
|
||||
$options['heartbeat_editor_behavior'] = 'reduce_periodicity';
|
||||
|
||||
if ( ! empty( $options['automatic_preload'] ) || ! empty( $options['sitemap_preload'] ) ) {
|
||||
$options['manual_preload'] = 1;
|
||||
}
|
||||
|
||||
update_option( WP_ROCKET_SLUG, $options );
|
||||
rocket_generate_config_file();
|
||||
|
||||
// Create a .htaccess file in the log folder.
|
||||
$handler = Logger::get_stream_handler();
|
||||
|
||||
if ( method_exists( $handler, 'create_htaccess_file' ) ) {
|
||||
try {
|
||||
$success = $handler->create_htaccess_file();
|
||||
} catch ( \Exception $e ) {
|
||||
$success = false;
|
||||
}
|
||||
|
||||
if ( ! $success ) {
|
||||
Logger::delete_log_file();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.2.0.1', '<' ) ) {
|
||||
wp_safe_remote_get( esc_url( home_url() ) );
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.12.6', '<' ) ) {
|
||||
do_action( 'rocket_preload_unlock_all_urls' );
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.3.6', '<' ) ) {
|
||||
delete_site_transient( 'update_wprocket' );
|
||||
delete_site_transient( 'update_wprocket_response' );
|
||||
|
||||
if ( get_rocket_option( 'do_cloudflare' ) && get_rocket_option( 'cloudflare_auto_settings' ) ) {
|
||||
if ( function_exists( 'set_rocket_cloudflare_browser_cache_ttl' ) ) {
|
||||
set_rocket_cloudflare_browser_cache_ttl( '31536000' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( rocket_is_ssl_website() ) {
|
||||
if ( 1 !== (int) get_rocket_option( 'cache_ssl' ) ) {
|
||||
update_rocket_option( 'cache_ssl', 1 );
|
||||
rocket_generate_config_file();
|
||||
}
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.4', '<' ) ) {
|
||||
wp_clear_scheduled_hook( 'rocket_purge_time_event' );
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.6', '<' ) ) {
|
||||
rocket_clean_cache_busting();
|
||||
rocket_clean_domain();
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.7', '<' ) ) {
|
||||
rocket_clean_minify( 'css' );
|
||||
rocket_generate_advanced_cache_file();
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.8.1', '<' ) ) {
|
||||
$options = get_option( rocket_get_constant( 'WP_ROCKET_SLUG' ) );
|
||||
unset( $options['dequeue_jquery_migrate'] );
|
||||
update_option( rocket_get_constant( 'WP_ROCKET_SLUG' ), $options );
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.9', '<' ) ) {
|
||||
$busting_path = rocket_get_constant( 'WP_ROCKET_CACHE_BUSTING_PATH' );
|
||||
|
||||
rocket_rrmdir( $busting_path . 'facebook-tracking' );
|
||||
rocket_rrmdir( $busting_path . 'google-tracking' );
|
||||
wp_clear_scheduled_hook( 'rocket_facebook_tracking_cache_update' );
|
||||
wp_clear_scheduled_hook( 'rocket_google_tracking_cache_update' );
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.10', '<' ) ) {
|
||||
$options = get_option( rocket_get_constant( 'WP_ROCKET_SLUG' ) );
|
||||
if (
|
||||
isset( $options['async_css'] ) && $options['async_css'] &&
|
||||
isset( $options['remove_unused_css'] ) && $options['remove_unused_css']
|
||||
) {
|
||||
$options['async_css'] = 0;
|
||||
$cache_path = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH' );
|
||||
rocket_rrmdir( $cache_path . 'used-css' );
|
||||
update_option( rocket_get_constant( 'WP_ROCKET_SLUG' ), $options );
|
||||
}
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.11.1', '<' ) ) {
|
||||
rocket_generate_config_file();
|
||||
}
|
||||
|
||||
if ( version_compare( $actual_version, '3.12.4', '<' ) ) {
|
||||
delete_transient( 'wp_rocket_pricing' );
|
||||
}
|
||||
}
|
||||
add_action( 'wp_rocket_upgrade', 'rocket_new_upgrade', 10, 2 );
|
||||
Reference in New Issue
Block a user