Initial commit xmarticaims

This commit is contained in:
2026-11-02 10:26:32 -06:00
commit e74236f5c1
9764 changed files with 2880647 additions and 0 deletions
@@ -0,0 +1,32 @@
<?php
namespace WP_Rocket\Engine\Admin\API;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'admin_api_subscriber',
];
/**
* Registers the option array in the container
*
* @since 3.3
*
* @return void
*/
public function register() {
$this->getContainer()->add( 'admin_api_subscriber', Subscriber::class );
}
}
@@ -0,0 +1,82 @@
<?php
namespace WP_Rocket\Engine\Admin\API;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Subscriber implements Subscriber_Interface {
const ROUTE_NAMESPACE = 'wp-rocket/v1';
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return string[]
*/
public static function get_subscribed_events() {
return [
'rest_api_init' => 'register_route',
'admin_enqueue_scripts' => [ 'enqueue_url', 999 ],
];
}
/**
* Enqueue the URL for option exporting.
*
* @return void
*/
public function enqueue_url() {
wp_localize_script(
'wpr-admin-common',
'rocket_option_export',
[
'rest_url_option_export' => wp_nonce_url( admin_url( 'admin-post.php?action=rocket_export' ), 'rocket_export' ),
]
);
}
/**
* Register REST route.
*
* @return void
*/
public function register_route() {
register_rest_route(
self::ROUTE_NAMESPACE,
'options/export',
[
'methods' => 'GET',
'callback' => [ $this, 'export_options' ],
'permission_callback' => [ $this, 'has_permissions' ],
]
);
}
/**
* Export options.
*
* @return void
*/
public function export_options() {
list( $filename, $options ) = rocket_export_options();
nocache_headers();
@header( 'Content-Type: application/json' ); //phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); //phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@header( 'Content-Transfer-Encoding: binary' ); //phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@header( 'Content-Length: ' . strlen( $options ) ); //phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@header( 'Connection: close' ); //phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
echo $options; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit();
}
/**
* Has permission to use the API route.
*
* @return bool
*/
public function has_permissions() {
return current_user_can( 'rocket_manage_options' );
}
}
@@ -0,0 +1,39 @@
<?php
namespace WP_Rocket\Engine\Admin;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\ThirdParty\ReturnTypesTrait;
class ActionSchedulerSubscriber implements Subscriber_Interface {
use ReturnTypesTrait;
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'action_scheduler_check_pastdue_actions' => 'return_false',
'action_scheduler_extra_action_counts' => 'hide_pastdue_status_filter',
];
}
/**
* Hide past-due from status filter in Action Scheduler tools page.
*
* @param array $extra_actions Array with format action_count_identifier => action count.
*
* @return array
*/
public function hide_pastdue_status_filter( array $extra_actions ) {
if ( ! isset( $extra_actions['past-due'] ) ) {
return $extra_actions;
}
unset( $extra_actions['past-due'] );
return $extra_actions;
}
}
@@ -0,0 +1,800 @@
<?php
namespace WP_Rocket\Engine\Admin\Beacon;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Support\Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* Helpscout Beacon integration
*
* @since 3.2
*/
class Beacon extends Abstract_Render implements Subscriber_Interface {
/**
* Options_Data instance
*
* @since 3.2
*
* @var Options_Data $options
*/
private $options;
/**
* Current user locale
*
* @since 3.2
*
* @var string $locale
*/
private $locale;
/**
* Support data instance
*
* @var Data
*/
private $support_data;
/**
* Constructor
*
* @since 3.2
*
* @param Options_Data $options Options instance.
* @param string $template_path Absolute path to the views/settings.
* @param Data $support_data Support data instance.
*/
public function __construct( Options_Data $options, $template_path, Data $support_data ) {
parent::__construct( $template_path );
$this->options = $options;
$this->support_data = $support_data;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.2
*
* @return array
*/
public static function get_subscribed_events() {
return [
'admin_print_footer_scripts-settings_page_wprocket' => 'insert_script',
];
}
/**
* Configures and returns beacon javascript
*
* @since 3.2
*
* @return void
*/
public function insert_script() {
if (
rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' )
||
! current_user_can( 'rocket_manage_options' )
) {
return;
}
switch ( $this->get_user_locale() ) {
case 'fr':
$form_id = '9db9417a-5e2f-41dd-8857-1421d5112aea';
break;
default:
$form_id = '44cc73fb-7636-4206-b115-c7b33823551b';
break;
}
$data = [
'form_id' => $form_id,
'identify' => wp_json_encode( $this->identify_data() ),
'session' => wp_json_encode( $this->support_data->get_support_data() ),
'prefill' => wp_json_encode( $this->prefill_data() ),
'config' => wp_json_encode( $this->config_data() ),
];
echo $this->generate( 'beacon', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Sets the locale property with the current user locale if not set yet
*
* @since 3.5
*
* @return string
*/
private function get_user_locale() {
if ( ! isset( $this->locale ) ) {
$this->locale = current( array_slice( explode( '_', get_user_locale() ), 0, 1 ) );
}
/**
* Filters the locale ID for Beacon
*
* @since 3.6
*
* @param string $locale The locale ID.
*/
return apply_filters( 'rocket_beacon_locale', $this->locale );
}
/**
* Returns Identify data to pass to Beacon
*
* @since 3.0
*
* @return array
*/
private function identify_data() {
$identify_data = [
'email' => $this->options->get( 'consumer_email' ),
'Website' => home_url(),
];
$customer_data = get_transient( 'wp_rocket_customer_data' );
if ( false !== $customer_data && isset( $customer_data->status ) ) {
$identify_data['status'] = $customer_data->status;
}
return $identify_data;
}
/**
* Returns prefill data to pass to Beacon
*
* @since 3.6
*
* @return array
*/
private function prefill_data() {
$prefill_data = [
'fields' => [
[
'id' => 21728,
'value' => 108003, // default to nulled.
],
],
];
$customer_data = get_transient( 'wp_rocket_customer_data' );
if ( false === $customer_data || ! isset( $customer_data->licence_account ) ) {
return $prefill_data;
}
$licenses = [
'Single' => 108000,
'Plus' => 108001,
'Infinite' => 108002,
'Unavailable' => 108003,
];
if ( isset( $licenses[ $customer_data->licence_account ] ) ) {
$prefill_data['fields'][0]['value'] = $licenses[ $customer_data->licence_account ];
}
return $prefill_data;
}
/**
* Returns config data to pass to Beacon
*
* @since 3.8.5
*
* @return array
*/
private function config_data(): array {
return [
'display' => [
'position' => is_rtl() ? 'left' : 'right',
],
];
}
/**
* Returns the IDs for the HelpScout docs for the corresponding section and language.
*
* @since 3.0
*
* @param string $doc_id Section identifier.
*
* @return string|array
*/
public function get_suggest( $doc_id ) {
$suggest = [
'faq' => [
'en' => [
[
'id' => '5569b671e4b027e1978e3c51',
'url' => 'https://docs.wp-rocket.me/article/99-pages-are-not-cached-or-css-and-js-minification-are-not-working/?utm_source=wp_plugin&utm_medium=wp_rocket',
'title' => 'Pages Are Not Cached or CSS and JS Minification Are Not Working',
],
[
'id' => '556778c8e4b01a224b426fad',
'url' => 'https://docs.wp-rocket.me/article/85-google-page-speed-grade-does-not-improve/?utm_source=wp_plugin&utm_medium=wp_rocket',
'title' => 'Google PageSpeed Grade does not Improve',
],
[
'id' => '556ef48ce4b01a224b428691',
'url' => 'https://docs.wp-rocket.me/article/106-my-site-is-broken/?utm_source=wp_plugin&utm_medium=wp_rocket',
'title' => 'My Site Is Broken',
],
[
'id' => '6001a83b2e764327f87bf189',
'url' => 'https://docs.wp-rocket.me/article/1407-eliminate-render-blocking-resources/?utm_source=wp_plugin&utm_medium=wp_rocket',
'title' => 'Eliminate Render Blocking Resources',
],
[
'id' => '54e6f7e5e4b034c37ea9095f',
'url' => 'https://docs.wp-rocket.me/article/46-how-to-check-if-wp-rocket-is-caching-your-pages/?utm_source=wp_plugin&utm_medium=wp_rocket',
'title' => 'How to check if WP Rocket is caching your pages',
],
],
'fr' => [
[
'id' => '5697d2dc9033603f7da31041',
'url' => 'https://fr.docs.wp-rocket.me/article/264-les-pages-ne-sont-pas-mises-en-cache-ou-la-minification-css-et-js-ne-fonctionne-pas/?utm_source=wp_plugin&utm_medium=wp_rocket',
'title' => 'Les pages ne sont pas mises en cache, ou la minification CSS et JS ne fonctionne pas',
],
[
'id' => '569564dfc69791436155e0b0',
'url' => 'https://fr.docs.wp-rocket.me/article/218-la-note-google-page-speed-ne-sameliore-pas/?utm_source=wp_plugin&utm_medium=wp_rocket',
'title' => "La note Google Page Speed ne s'améliore pas",
],
[
'id' => '5697d03bc69791436155ed69',
'url' => 'https://fr.docs.wp-rocket.me/article/263-site-casse/?utm_source=wp_plugin&utm_medium=wp_rocket',
'title' => 'Mon site est cassé',
],
[
'id' => '601d4b83ac2f834ec5385ca5',
'url' => 'https://fr.docs.wp-rocket.me/article/1440-eliminez-les-ressources-qui-bloquent-le-rendu/?utm_source=wp_plugin&utm_medium=wp_rocket',
'title' => 'Éliminez les ressources qui bloquent le rendu',
],
[
'id' => '568fe9ebc69791436155cd32',
'url' => 'https://fr.docs.wp-rocket.me/article/180-verifier-cache/?utm_source=wp_plugin&utm_medium=wp_rocket',
'title' => 'Comment vérifier si WP Rocket met bien en cache vos pages',
],
],
],
'user_cache_section' => [
'en' => '56b55ba49033600da1c0b687,587920b5c697915403a0e1f4,560c66b0c697917e72165a6d',
'fr' => '56cb9ba990336008e9e9e3d9,5879230cc697915403a0e211,569410999033603f7da2fa94',
],
'user_cache' => [
'en' => [
'id' => '56b55ba49033600da1c0b687',
'url' => 'https://docs.wp-rocket.me/article/313-user-cache/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '56cb9ba990336008e9e9e3d9',
'url' => 'https://fr.docs.wp-rocket.me/article/333-cache-utilisateurs-connectes/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'mobile_cache_section' => [
'en' => '577a5f1f903360258a10e52a,5678aa76c697914361558e92,5745b9a6c697917290ddc715',
'fr' => '589b17a02c7d3a784630b249,5a6b32830428632faf6233dc,58a480e5dd8c8e56bfa7b85c',
],
'mobile_cache' => [
'en' => [
'id' => '577a5f1f903360258a10e52a',
'url' => 'https://docs.wp-rocket.me/article/708-mobile-caching/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '589b17a02c7d3a784630b249',
'url' => 'https://fr.docs.wp-rocket.me/article/934-mise-en-cache-pour-mobile/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'cache_ssl' => [
'en' => [
'id' => '56c24fd3903360436857f1ed',
'url' => 'https://docs.wp-rocket.me/article/314-using-ssl-with-wp-rocket/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '56cb9d24c6979102ccfc801c',
'url' => 'https://fr.docs.wp-rocket.me/article/335-utiliser-ssl-wp-rocket/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'cache_lifespan' => [
'en' => [
'id' => '555c7e9ee4b027e1978e17a5',
'url' => 'https://docs.wp-rocket.me/article/78-how-often-is-the-cache-updated/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '568f7df49033603f7da2ec72',
'url' => 'https://fr.docs.wp-rocket.me/article/171-intervalle-cache/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'cache_lifespan_section' => [
'en' => '555c7e9ee4b027e1978e17a5,5922fd0e0428634b4a33552c',
'fr' => '568f7df49033603f7da2ec72,598080e1042863033a1b890e',
],
'nonce' => [
'en' => [
'id' => '5922fd0e0428634b4a33552c',
'url' => 'https://docs.wp-rocket.me/article/975-nonces-and-cache-lifespan/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '598080e1042863033a1b890e',
'url' => 'https://fr.docs.wp-rocket.me/article/1015-nonces-delai-nettoyage-cache/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'css_section' => [
'en' => '556ef48ce4b01a224b428691,6001a83b2e764327f87bf189,5569b671e4b027e1978e3c51,5d5214d10428631e94f94ae6',
'fr' => '5697d2dc9033603f7da31041,5d5abcce0428634552d85c1c,5697d03bc69791436155ed69,601d4b83ac2f834ec5385ca5',
],
'js_section' => [
'en' => '54b9509de4b07997ea3f27c7,59236dfb0428634b4a3358f9,5f359695042863444aa04e26,556ef48ce4b01a224b428691,6001a83b2e764327f87bf189',
'fr' => '56967eebc69791436155e649,593fe9882c7d3a0747cddb77,5f523c46c9e77c0016384ba0,5697d03bc69791436155ed69,601d4b83ac2f834ec5385ca5',
],
'file_optimization' => [
'en' => [
'id' => '6001a83b2e764327f87bf189',
'url' => 'https://docs.wp-rocket.me/article/1407-eliminate-render-blocking-resources/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '601d4b83ac2f834ec5385ca5',
'url' => 'https://fr.docs.wp-rocket.me/article/1440-eliminez-les-ressources-qui-bloquent-le-rendu/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'combine' => [
'en' => [
'id' => '596eaf7d2c7d3a73488b3661',
'url' => 'https://docs.wp-rocket.me/article/1009-configuration-for-http-2/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '59a418ad042863033a1c572e',
'url' => 'https://fr.docs.wp-rocket.me/article/1018-configuration-http-2/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'remove_unused_css' => [
'en' => [
'id' => '6076083ff8c0ef2d98df1f97',
'url' => 'https://docs.wp-rocket.me/article/1529-remove-unused-css?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '60d499a705ff892e6bc2a89e',
'url' => 'https://fr.docs.wp-rocket.me/article/1577-supprimer-les-ressources-css-inutilisees?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'exclude_inline_js' => [
'en' => [
'id' => '5b4879100428630abc0c0713',
'url' => 'https://docs.wp-rocket.me/article/1104-excluding-inline-js-from-combine/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5b4dd9290428631d7a89023c',
'url' => 'https://fr.docs.wp-rocket.me/article/1109-exclure-les-js-inline-de-la-combinaison?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'exclude_js' => [
'en' => [
'id' => '54b9509de4b07997ea3f27c7',
'url' => 'https://docs.wp-rocket.me/article/39-excluding-external-js-from-concatenation/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '56967eebc69791436155e649',
'url' => 'https://fr.docs.wp-rocket.me/article/243-exclure-js-externe-minification?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'exclude_css' => [
'en' => [
'id' => '5bf339b12c7d3a31944e2111',
'url' => 'https://docs.wp-rocket.me/article/1131-resolving-issues-with-css-minify-combine?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5bf3bece04286304a71c6d35',
'url' => 'https://fr.docs.wp-rocket.me/article/1132-resoudre-problemes-minification-combinaison-css?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'defer_js' => [
'en' => [
'id' => '5d52138d2c7d3a68825e8faa',
'url' => 'https://docs.wp-rocket.me/article/1265-load-javascript-deferred/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5d5ac08b2c7d3a7920be3649',
'url' => 'https://fr.​docs.​wp-rocket.​me/article/1270-chargement-differe-des-fichiers-js/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'delay_js' => [
'en' => [
'id' => '5f359695042863444aa04e26',
'url' => 'https://docs.wp-rocket.me/article/1349-delay-javascript-execution/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '60e5b05605ff892e6bc2e86c',
'url' => 'https://fr.docs.wp-rocket.me/article/1626-reporter-l-execution-du-javascript?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'delay_js_exclusions' => [
'en' => [
'id' => '',
'url' => 'https://docs.wp-rocket.me/article/1560-delay-javascript-execution-compatibility-exclusions/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'async' => [
'en' => [
'id' => '5d52144c0428631e94f94ae2',
'url' => 'https://docs.wp-rocket.me/article/1266-optimize-css-delivery/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5d5abada0428634552d85bff',
'url' => 'https://fr.​docs.​wp-rocket.​me/article/1268-optimiser-le-chargement-du-css/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'lazyload' => [
'en' => [
'id' => '5c884cf80428633d2cf38314',
'url' => 'https://docs.wp-rocket.me/article/1141-using-lazyload-in-wp-rocket/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5c98ff532c7d3a1544614cf4',
'url' => 'https://fr.docs.wp-rocket.me/article/1146-utiliser-lazyload-images-wp-rocket/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'webp' => [
'fr' => [
'id' => '5d7b495e04286364bc8f12ef',
'url' => 'https://fr.docs.wp-rocket.me/article/1286-compatibilite-webp?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'en' => [
'id' => '5d72919704286364bc8ed49d',
'url' => 'https://docs.wp-rocket.me/article/1282-webp?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'lazyload_section' => [
'en' => '5c884cf80428633d2cf38314,54b85754e4b0512429883a86,5418c792e4b0e7b8127bed99,569ec4a69033603f7da32c93,5419e246e4b099def9b5561e',
'fr' => '56967a859033603f7da30858,56967952c69791436155e60a,56cb9c9d90336008e9e9e3dc,569676ea9033603f7da3083d',
],
'sitemap_preload' => [
'en' => '541780fde4b005ed2d11784c,5a71c8ab2c7d3a4a4198a9b3,55b282ede4b0b0593824f852',
'fr' => '5693d582c69791436155d645',
],
'preload_bot' => [
'en' => '541780fde4b005ed2d11784c,55b282ede4b0b0593824f852,559113eae4b027e1978eba11',
'fr' => '5693d582c69791436155d645,569433d1c69791436155d99c',
],
'preload_exclusions' => [
'en' => [
'id' => '6349682bde258f5018eb456d',
'url' => 'https://docs.wp-rocket.me/article/1721-exclude-urls-from-being-preloaded?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '63496d5b8a552811521e52d2',
'url' => 'https://fr.docs.wp-rocket.me/article/1722-exclure-url-du-prechargement?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'bot' => [
'en' => [
'id' => '541780fde4b005ed2d11784c',
'url' => 'https://docs.wp-rocket.me/article/8-how-the-cache-is-preloaded/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5693d582c69791436155d645',
'url' => 'https://fr.docs.wp-rocket.me/article/188-comment-est-pre-charge-le-cache/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'dns_prefetch' => [
'en' => [
'id' => '5e055a602c7d3a7e9ae5881c',
'url' => 'https://docs.wp-rocket.me/article/1302-prefetch-dns-requests/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5e1891892c7d3a7e9ae60983',
'url' => 'https://fr.docs.wp-rocket.me/article/1303-prechargement-requetes-dns/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'fonts_preload' => [
'en' => [
'id' => '5eab7729042863474d19f647',
'url' => 'https://docs.wp-rocket.me/article/1317-preload-fonts/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5eb3add02c7d3a5ea54aa66d',
'url' => 'https://fr.docs.wp-rocket.me/article/1319-precharger-polices/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'preload_links' => [
'en' => [
'id' => '5f35939b042863444aa04df9',
'url' => 'https://docs.wp-rocket.me/article/1348-preload-links/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5f58527cc9e77c001603746c',
'url' => 'https://fr.docs.wp-rocket.me/article/1358-precharger-les-liens/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'ecommerce' => [
'en' => [
'id' => '548f492de4b034fd4862493e',
'url' => 'https://docs.wp-rocket.me/article/27-using-wp-rocket-on-your-ecommerce-site/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '569431189033603f7da2fc13',
'url' => 'https://fr.docs.wp-rocket.me/article/198-ecommerce?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'cache_query_strings' => [
'en' => [
'id' => '590a83610428634b4a32d52c',
'url' => 'https://docs.wp-rocket.me/article/971-caching-query-strings/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '597a04fd042863033a1b6da4',
'url' => 'https://fr.docs.wp-rocket.me/article/1014-cache-query-strings/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'exclude_cache' => [
'en' => [
'id' => '5519ab03e4b061031402119f',
'url' => 'https://docs.wp-rocket.me/article/54-exclude-pages-from-the-cache/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '56941c0cc69791436155d8ab',
'url' => 'https://fr.docs.wp-rocket.me/article/196-exclure-pages-cache/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'exclude_cookie' => [
'en' => [
'id' => '5fe5462df24ccf588e3fe804',
'url' => 'https://docs.wp-rocket.me/article/1382-never-cache-cookies/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'exclude_user_agent' => [
'en' => [
'id' => '5ff728d3551e0c2853f3a245',
'url' => 'https://docs.wp-rocket.me/article/1389-never-cache-user-agents/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'always_purge' => [
'en' => [
'id' => '5ff72b4dfd168b77735328b7',
'url' => 'https://docs.wp-rocket.me/article/1391-always-purge-url-s/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'db_optimization' => [
'en' => [
'id' => '60259156b3ebfb109b58182d',
'url' => 'https://docs.wp-rocket.me/article/1443-database-optimizations-are-not-working/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '6040c5b90a2dae5b58fb5d29',
'url' => 'https://fr.docs.wp-rocket.me/article/1486-les-optimisations-de-la-base-de-donnees-ne-fonctionne-pas/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'cdn_section' => [
'en' => '5e4c84bd04286364bc958833,54c7fa3de4b0512429885b5c,54a6d578e4b047ebb774a687,56b2b4459033603f7da37acf,566f749f9033603f7da28459,5434667fe4b0310ce5ee867a',
'fr' => '5f351e42042863444aa04652,5696830b9033603f7da308ac,569685749033603f7da308c0,57a4961190336059d4edc9d8,5697d5f8c69791436155ed8e,569684d29033603f7da308b9',
],
'cdn' => [
'en' => [
'id' => '54c7fa3de4b0512429885b5c',
'url' => 'https://docs.wp-rocket.me/article/42-using-wp-rocket-with-a-cdn/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5696830b9033603f7da308ac',
'url' => 'https://fr.docs.wp-rocket.me/article/246-utiliser-wp-rocket-avec-un-cdn/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'rocketcdn' => [
'en' => [
'id' => '5e4c84bd04286364bc958833',
'url' => 'https://docs.wp-rocket.me/article/1307-rocketcdn/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5f351e42042863444aa04652',
'url' => 'https://fr.docs.wp-rocket.me/article/1343-comment-utiliser-rocketcdn/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'rocketcdn_error' => [
'en' => [
'id' => '60ddc72d9e87cb3d01249270',
'url' => 'https://docs.wp-rocket.me/article/1608-error-notices-during-the-rocketcdn-subscription-process/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '60df1cb200fd0d7c253fc044',
'url' => 'https://fr.docs.wp-rocket.me/article/1620-messages-derreur-pendant-le-processus-dabonnement/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'exclude_cdn' => [
'en' => [
'id' => '5434667fe4b0310ce5ee867a',
'url' => 'https://docs.wp-rocket.me/article/24-resolving-issues-with-cdn-and-fonts-icons/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '569684d29033603f7da308b9',
'url' => 'https://fr.docs.wp-rocket.me/article/248-resoudre-des-problemes-avec-cdn-et-les-polices-icones/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'cloudflare_credentials' => [
'en' => [
'id' => '54205619e4b0e7b8127bf849',
'url' => 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5696837e9033603f7da308ae',
'url' => 'https://fr.docs.wp-rocket.me/article/247-utiliser-wp-rocket-avec-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'cloudflare_settings' => [
'en' => [
'id' => '54205619e4b0e7b8127bf849',
'url' => 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5696837e9033603f7da308ae',
'url' => 'https://fr.docs.wp-rocket.me/article/247-utiliser-wp-rocket-avec-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'cloudflare_credentials_api' => [
'en' => [
'id' => '54205619e4b0e7b8127bf849',
'url' => 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on',
],
'fr' => [
'id' => '5696837e9033603f7da308ae',
'url' => 'https://fr.docs.wp-rocket.me/article/247-utiliser-wp-rocket-avec-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on',
],
],
'cloudflare_apo' => [
'en' => [
'id' => '602593e90a2dae5b58faee1e',
'url' => 'https://docs.wp-rocket.me/article/1444-using-cloudflare-apo-with-wp-rocket?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '6486cb4147772865db893c7c',
'url' => 'https://fr.docs.wp-rocket.me/article/1757-utiliser-cloudflare-apo-avec-wp-rocket?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'sucuri_credentials' => [
'en' => [
'id' => '5bce07be2c7d3a04dd5bf94d',
'url' => 'https://docs.wp-rocket.me/article/1120-sucuri-add-on/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5bcf39c72c7d3a4db66085b9',
'url' => 'https://fr.docs.wp-rocket.me/article/1122-sucuri-add-on/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'varnish' => [
'en' => [
'id' => '56f48132c6979115a34095bd',
'url' => 'https://docs.wp-rocket.me/article/493-using-varnish-with-wp-rocket/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '56fd2f789033601d6683e574',
'url' => 'https://fr.docs.wp-rocket.me/article/512-varnish-wp-rocket-2-7/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'heartbeat_settings' => [
'en' => [
'id' => '5bcdfecd042863158cc7b672',
'url' => 'https://docs.wp-rocket.me/article/1119-control-wordpress-heartbeat-api/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5bcf4378042863215a46bc00',
'url' => 'https://fr.docs.wp-rocket.me/article/1124-controler-api-wordpress-heartbeat/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'google_fonts' => [
'en' => [
'id' => '5e8687c22c7d3a7e9aea4c4a',
'url' => 'https://docs.wp-rocket.me/article/1312-optimize-google-fonts',
],
'fr' => [
'id' => '5e970f512c7d3a7e9aeaf9fb',
'url' => 'https://fr.docs.wp-rocket.me/article/1314-optimiser-les-google-fonts/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'dynamic_lists' => [
'en' => [
'id' => '63234712b0f178684ee3b04a',
'url' => 'https://docs.wp-rocket.me/article/1716-dynamic-exclusions-and-inclusions/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '6323604341e1a47267b8d0e3',
'url' => 'https://fr.docs.wp-rocket.me/article/1717-inclusions-et-exclusions-dynamiques/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'image_dimensions' => [
'en' => [
'id' => '5fc70216de1bfa158fb54737',
'url' => 'https://docs.wp-rocket.me/article/1366-add-missing-image-dimensions/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '5fd20dcab6c6251cd1c35079',
'url' => 'https://fr.docs.wp-rocket.me/article/1369-ajouter-les-dimensions-dimage-manquantes/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'exclude_defer_js' => [
'en' => [
'id' => '59236dfb0428634b4a3358f9',
'url' => 'https://docs.wp-rocket.me/article/976-exclude-files-from-defer-js/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'exclude_lazyload' => [
'fr' => [
'id' => '56967952c69791436155e60a',
'url' => 'https://fr.docs.wp-rocket.me/article/235-desactivez-le-lazyload-sur-des-images-specifiques?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'en' => [
'id' => '5418c792e4b0e7b8127bed99',
'url' => 'https://docs.wp-rocket.me/article/15-disabling-lazy-load-on-specific-images/?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'invalid_exclusions' => [
'en' => [
'id' => '619e90a3d3efbe495c3b26b8',
'url' => 'https://docs.wp-rocket.me/article/1657-invalid-patterns-of-exclusions',
],
'fr' => [
'id' => '61b21c1297682b790dad345a',
'url' => 'https://fr.docs.wp-rocket.me/article/1659-motifs-exclusion-non-valables',
],
],
'async_opti' => [
'en' => [
'id' => '622a725a2ce7ed0fb0914056',
'url' => 'https://docs.wp-rocket.me/article/1688-asynchronous-optimizations?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '6231fc24c1688a6d26a75ee1',
'url' => 'https://fr.docs.wp-rocket.me/article/1689-optimisations-asynchrones?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'offline' => [
'en' => [
'id' => '60623465c44f5d025f4491de',
'url' => 'https://docs.wp-rocket.me/article/1514-private-intranet-offline?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '6065cb184466ce6ddc5f05fb',
'url' => 'https://fr.docs.wp-rocket.me/article/1521-utiliser-wp-rocket-sur-un-intranet-prive-ou-hors-ligne?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'fallback_css' => [
'en' => [
'id' => '5ec5c4072c7d3a5ea54b7de7',
'url' => 'https://docs.wp-rocket.me/article/1321-critical-css-issues-fouc#use-fallback-critical-css',
],
'fr' => [
'id' => '5edf8a5504286306f804e1dc',
'url' => 'https://fr.docs.wp-rocket.me/article/1327-problemes-critical-css-fouc#critical-path-css-de-secours',
],
],
'domain_change' => [
'en' => [
'id' => '577578b1903360258a10d8ba',
'url' => 'https://docs.wp-rocket.me/article/705-changing-domains-migrating-sites-with-wp-rocket?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '57868414c697912dee72a98a',
'url' => 'https://fr.docs.wp-rocket.me/article/837-changer-de-domaine-migrer-un-site-avec-wp-rocket?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'rucss_firewall_ips' => [
'en' => [
'id' => '6076083ff8c0ef2d98df1f97',
'url' => 'https://docs.wp-rocket.me/article/1529-remove-unused-css?utm_source=wp_plugin&utm_medium=wp_rocket#basic-requirements',
],
'fr' => [
'id' => '60d499a705ff892e6bc2a89e',
'url' => 'https://fr.docs.wp-rocket.me/article/1577-supprimer-les-ressources-css-inutilisees?utm_source=wp_plugin&utm_medium=wp_rocket#basic-requirements',
],
],
];
return isset( $suggest[ $doc_id ][ $this->get_user_locale() ] )
? $suggest[ $doc_id ][ $this->get_user_locale() ]
: $suggest[ $doc_id ]['en'];
}
}
@@ -0,0 +1,38 @@
<?php
namespace WP_Rocket\Engine\Admin\Beacon;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service Provider for Beacon
*
* @since 3.3
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'beacon',
];
/**
* Registers items with the container
*
* @return void
*/
public function register() {
$this->getContainer()->share( 'beacon', Beacon::class )
->addArgument( $this->getContainer()->get( 'options' ) )
->addArgument( $this->getContainer()->get( 'template_path' ) . '/settings' )
->addArgument( $this->getContainer()->get( 'support_data' ) )
->addTag( 'admin_subscriber' );
}
}
@@ -0,0 +1,107 @@
<?php
namespace WP_Rocket\Engine\Admin\Database;
/**
* Handles the database optimization process.
*/
class Optimization {
/**
* Background process instance
*
* @var OptimizationProcess $process Background Process instance.
*/
protected $process;
/**
* Array of option name/label pairs.
*
* @var array
*/
private $options;
/**
* Class constructor.
*
* @param OptimizationProcess $process Background process instance.
*/
public function __construct( OptimizationProcess $process ) {
$this->process = $process;
$this->options = [
'database_revisions' => __( 'Revisions', 'rocket' ),
'database_auto_drafts' => __( 'Auto Drafts', 'rocket' ),
'database_trashed_posts' => __( 'Trashed Posts', 'rocket' ),
'database_spam_comments' => __( 'Spam Comments', 'rocket' ),
'database_trashed_comments' => __( 'Trashed Comments', 'rocket' ),
'database_all_transients' => __( 'Transients', 'rocket' ),
'database_optimize_tables' => __( 'Tables', 'rocket' ),
];
}
/**
* Get Database options
*
* @since 3.0.4
*
* @return array
*/
public function get_options() {
return $this->options;
}
/**
* Performs the database optimization
*
* @since 2.11
*
* @param array $options WP Rocket Database options.
*/
public function process_handler( $options ) {
if ( method_exists( $this->process, 'cancel_process' ) ) {
$this->process->cancel_process();
}
array_map( [ $this->process, 'push_to_queue' ], $options );
$this->process->save()->dispatch();
}
/**
* Count the number of items concerned by the database cleanup
*
* @since 2.8
*
* @param string $type Item type to count.
* @return int Number of items for this type
*/
public function count_cleanup_items( $type ) {
global $wpdb;
$count = 0;
switch ( $type ) {
case 'database_revisions':
$count = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'revision'" );
break;
case 'database_auto_drafts':
$count = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_status = 'auto-draft'" );
break;
case 'database_trashed_posts':
$count = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_status = 'trash'" );
break;
case 'database_spam_comments':
$count = $wpdb->get_var( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'" );
break;
case 'database_trashed_comments':
$count = $wpdb->get_var( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE (comment_approved = 'trash' OR comment_approved = 'post-trashed')" );
break;
case 'database_all_transients':
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(option_id) FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_site_transient_' ) . '%' ) );
break;
case 'database_optimize_tables':
$count = $wpdb->get_var( "SELECT COUNT(table_name) FROM information_schema.tables WHERE table_schema = '" . DB_NAME . "' and Engine <> 'InnoDB' and data_free > 0" );
break;
}
return $count;
}
}
@@ -0,0 +1,151 @@
<?php
namespace WP_Rocket\Engine\Admin\Database;
use WP_Rocket_WP_Background_Process;
/**
* Extends the background process class for the database optimization background process.
*
* @see WP_Rocket_WP_Background_Process
*/
class OptimizationProcess extends WP_Rocket_WP_Background_Process {
/**
* Prefix
*
* @var string
*/
protected $prefix = 'rocket';
/**
* Specific action identifier for sitemap preload.
*
* @var string Action identifier
*/
protected $action = 'database_optimization';
/**
* Count the number of optimized items.
*
* @var array $count An array of indexed number of optimized items.
*/
protected $count = [];
/**
* Dispatch
*
* @return array|WP_Error
*/
public function dispatch() {
set_transient( 'rocket_database_optimization_process', 'running', HOUR_IN_SECONDS );
// Perform remote post.
return parent::dispatch();
}
/**
* Perform the optimization corresponding to $item
*
* @param mixed $item Queue item to iterate over.
*
* @return bool false
*/
protected function task( $item ) {
global $wpdb;
switch ( $item ) {
case 'database_revisions':
$query = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'revision'" );
if ( $query ) {
$number = 0;
foreach ( $query as $id ) {
$number += wp_delete_post_revision( intval( $id ) ) instanceof \WP_Post ? 1 : 0;
}
$this->count[ $item ] = $number;
}
break;
case 'database_auto_drafts':
$query = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft'" );
if ( $query ) {
$number = 0;
foreach ( $query as $id ) {
$number += wp_delete_post( intval( $id ), true ) instanceof \WP_Post ? 1 : 0;
}
$this->count[ $item ] = $number;
}
break;
case 'database_trashed_posts':
$query = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'trash'" );
if ( $query ) {
$number = 0;
foreach ( $query as $id ) {
$number += wp_delete_post( $id, true ) instanceof \WP_Post ? 1 : 0;
}
$this->count[ $item ] = $number;
}
break;
case 'database_spam_comments':
$query = $wpdb->get_col( "SELECT comment_ID FROM $wpdb->comments WHERE comment_approved = 'spam'" );
if ( $query ) {
$number = 0;
foreach ( $query as $id ) {
$number += (int) wp_delete_comment( intval( $id ), true );
}
$this->count[ $item ] = $number;
}
break;
case 'database_trashed_comments':
$query = $wpdb->get_col( "SELECT comment_ID FROM $wpdb->comments WHERE (comment_approved = 'trash' OR comment_approved = 'post-trashed')" );
if ( $query ) {
$number = 0;
foreach ( $query as $id ) {
$number += (int) wp_delete_comment( intval( $id ), true );
}
$this->count[ $item ] = $number;
}
break;
case 'database_all_transients':
$query = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_site_transient_' ) . '%' ) );
if ( $query ) {
$number = 0;
foreach ( $query as $transient ) {
if ( strpos( $transient, '_site_transient_' ) !== false ) {
$number += (int) delete_site_transient( str_replace( '_site_transient_', '', $transient ) );
} else {
$number += (int) delete_transient( str_replace( '_transient_', '', $transient ) );
}
}
$this->count[ $item ] = $number;
}
break;
case 'database_optimize_tables':
$query = $wpdb->get_results( "SELECT table_name, data_free FROM information_schema.tables WHERE table_schema = '" . DB_NAME . "' and Engine <> 'InnoDB' and data_free > 0" );
if ( $query ) {
$number = 0;
foreach ( $query as $table ) {
$number += (int) $wpdb->query( "OPTIMIZE TABLE $table->table_name" );
}
$this->count[ $item ] = $number;
}
break;
}
return false;
}
/**
* Complete
*/
protected function complete() {
delete_transient( 'rocket_database_optimization_process' );
set_transient( 'rocket_database_optimization_process_complete', $this->count );
parent::complete();
}
}
@@ -0,0 +1,44 @@
<?php
namespace WP_Rocket\Engine\Admin\Database;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service Provider for database optimization
*
* @since 3.3
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'db_optimization_process',
'db_optimization',
'db_optimization_subscriber',
];
/**
* Registers the option array in the container
*
* @since 3.3
*
* @return void
*/
public function register() {
$this->getContainer()->add( 'db_optimization_process', OptimizationProcess::class );
$this->getContainer()->add( 'db_optimization', Optimization::class )
->addArgument( $this->getContainer()->get( 'db_optimization_process' ) );
$this->getContainer()->share( 'db_optimization_subscriber', Subscriber::class )
->addArgument( $this->getContainer()->get( 'db_optimization' ) )
->addArgument( $this->getContainer()->get( 'options' ) )
->addTag( 'common_subscriber' );
}
}
@@ -0,0 +1,247 @@
<?php
namespace WP_Rocket\Engine\Admin\Database;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Admin\Options_Data;
/**
* Subscriber for the database optimization
*/
class Subscriber implements Subscriber_Interface {
/**
* Optimization process instance.
*
* @since 3.4
*
* @var Optimization
*/
private $optimize;
/**
* WP Rocket Options instance.
*
* @since 3.4
*
* @var Options_Data
*/
private $options;
/**
* Constructor
*
* @param Optimization $optimize Optimize instance.
* @param Options_Data $options WP Rocket options.
*/
public function __construct( Optimization $optimize, Options_Data $options ) {
$this->optimize = $optimize;
$this->options = $options;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.3
*
* @return array
*/
public static function get_subscribed_events() {
return [
'cron_schedules' => 'add_cron_schedule',
'init' => 'database_optimization_scheduled',
'rocket_database_optimization_time_event' => 'cron_optimize',
'pre_update_option_' . WP_ROCKET_SLUG => 'save_optimize',
'admin_notices' => [
[ 'notice_process_running' ],
[ 'notice_process_complete' ],
],
];
}
/**
* Add a new interval for the cron job.
* This adds a weekly/monthly interval for database optimization.
*
* @since 3.4
*
* @param array $schedules An array of intervals used by cron jobs.
* @return array Updated array of intervals.
*/
public function add_cron_schedule( $schedules ) {
if ( ! $this->options->get( 'schedule_automatic_cleanup', false ) ) {
return $schedules;
}
switch ( $this->options->get( 'automatic_cleanup_frequency', 'weekly' ) ) {
case 'weekly':
$schedules['weekly'] = [
'interval' => 604800,
'display' => __( 'weekly', 'rocket' ),
];
break;
case 'monthly':
$schedules['monthly'] = [
'interval' => 2592000,
'display' => __( 'monthly', 'rocket' ),
];
break;
}
return $schedules;
}
/**
* Plans database optimization cron
* If the task is not programmed, it is automatically triggered
*
* @since 2.8
*
* @see process_handler()
*/
public function database_optimization_scheduled() {
if ( ! $this->options->get( 'schedule_automatic_cleanup', false ) ) {
return;
}
if ( ! wp_next_scheduled( 'rocket_database_optimization_time_event' ) ) {
wp_schedule_event( time(), $this->options->get( 'automatic_cleanup_frequency', 'weekly' ), 'rocket_database_optimization_time_event' );
}
}
/**
* Database Optimization cron callback
*
* @since 3.0.4
*/
public function cron_optimize() {
$items = array_filter( array_keys( $this->optimize->get_options() ), [ $this->options, 'get' ] );
if ( empty( $items ) ) {
return;
}
$this->optimize->process_handler( $items );
}
/**
* Launches the database optimization when the settings are saved with optimize button
*
* @since 2.8
*
* @see process_handler()
*
* @param array $value The new, unserialized option value.
* @return array
*/
public function save_optimize( $value ) {
if ( empty( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
return $value;
}
if ( empty( $value ) || ! isset( $value['submit_optimize'] ) ) {
return $value;
}
unset( $value['submit_optimize'] );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return $value;
}
$items = [];
$db_options = $this->optimize->get_options();
foreach ( $value as $key => $option_value ) {
if ( isset( $db_options[ $key ] ) && 1 === $option_value ) {
$items[] = $key;
}
}
if ( empty( $items ) ) {
return $value;
}
$this->optimize->process_handler( $items );
return $value;
}
/**
* This notice is displayed after launching the database optimization process
*
* @since 2.11
*/
public function notice_process_running() {
$screen = get_current_screen();
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
if ( 'settings_page_wprocket' !== $screen->id ) {
return;
}
$notice = get_transient( 'rocket_database_optimization_process' );
if ( ! $notice ) {
return;
}
\rocket_notice_html(
[
'status' => 'info',
'message' => esc_html__( 'Database optimization process is running', 'rocket' ),
]
);
}
/**
* This notice is displayed when the database optimization process is complete
*
* @since 2.11
*/
public function notice_process_complete() {
$screen = get_current_screen();
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
if ( 'settings_page_wprocket' !== $screen->id ) {
return;
}
$optimized = get_transient( 'rocket_database_optimization_process_complete' );
if ( false === $optimized ) {
return;
}
$db_options = $this->optimize->get_options();
delete_transient( 'rocket_database_optimization_process_complete' );
$message = esc_html__( 'Database optimization process is complete. Everything was already optimized!', 'rocket' );
if ( ! empty( $optimized ) ) {
$message = esc_html__( 'Database optimization process is complete. List of optimized items below:', 'rocket' );
}
if ( ! empty( $optimized ) ) {
$message .= '<ul>';
foreach ( $optimized as $key => $number ) {
$message .= '<li>' .
/* translators: %1$d = number of items optimized, %2$s = type of optimization */
sprintf( esc_html__( '%1$d %2$s optimized.', 'rocket' ), $number, $db_options[ $key ] )
. '</li>';
}
$message .= '</ul>';
}
\rocket_notice_html(
[
'message' => $message,
]
);
}
}
@@ -0,0 +1,197 @@
<?php
namespace WP_Rocket\Engine\Admin\Deactivation;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Admin\Options;
use WP_Rocket\Admin\Options_Data;
class DeactivationIntent extends Abstract_Render {
/**
* Options instance.
*
* @var Options
*/
private $options_api;
/**
* Options_Data instance.
*
* @since 3.0
*
* @var Options_Data
*/
private $options;
/**
* Constructor
*
* @since 3.0
*
* @param string $template Template path.
* @param Options $options_api Options instance.
* @param Options_Data $options Options_Data instance.
*/
public function __construct( $template, Options $options_api, Options_Data $options ) {
parent::__construct( $template );
$this->options_api = $options_api;
$this->options = $options;
}
/**
* Checks if the deactivation modal is snoozed
*
* @since 3.11.1
*
* @return bool
*/
private function is_snoozed(): bool {
if ( 1 === (int) get_option( 'wp_rocket_hide_deactivation_form', 0 ) ) {
return true;
}
if ( false !== get_transient( 'rocket_hide_deactivation_form' ) ) {
return true;
}
return false;
}
/**
* Inserts the deactivation intent form on plugins page
*
* @since 3.0
*
* @return void
*/
public function insert_deactivation_intent_form() {
if ( $this->is_snoozed() ) {
return;
}
$data = [
'form_action' => admin_url( 'admin-post.php?action=rocket_deactivation' ),
];
echo $this->generate( 'form', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Deactivate the plugin and set the snooze value
*
* @since 3.11.1
*
* @param int $snooze Snooze value.
*
* @return void
*/
public function deactivate_and_snooze( $snooze ) {
$this->set_snooze( $snooze );
deactivate_plugins( 'wp-rocket/wp-rocket.php' );
}
/**
* Sets the snooze value
*
* @since 3.11.1
*
* @param int $snooze Snooze value.
*
* @return void
*/
private function set_snooze( int $snooze ) {
if ( 0 === $snooze ) {
add_option( 'wp_rocket_hide_deactivation_form', 1 );
return;
}
set_transient( 'rocket_hide_deactivation_form', 1, $snooze * DAY_IN_SECONDS );
}
/**
* Activates WP Rocket safe mode by deactivating possibly layout breaking options
*
* @since 3.0
*
* @return void
*/
public function activate_safe_mode() {
/**
* Filters the array of options to reset when activating safe mode
*
* @since 3.7
*
* @param array $options Array of options to reset.
*/
$reset_options = apply_filters(
'rocket_safe_mode_reset_options',
[
'async_css' => 0,
'lazyload' => 0,
'lazyload_iframes' => 0,
'lazyload_youtube' => 0,
'minify_css' => 0,
'minify_js' => 0,
'minify_concatenate_js' => 0,
'defer_all_js' => 0,
'delay_js' => 0,
'remove_unused_css' => 0,
'minify_google_fonts' => 0,
'cdn' => 0,
]
);
$this->options->set_values( $reset_options );
$this->options_api->set( 'settings', $this->options->get_options() );
}
/**
* Add modal assets on the plugins page
*
* @since 3.11.1
*
* @param string $hook The current admin page.
*
* @return void
*/
public function add_modal_assets( $hook ) {
if ( 'plugins.php' !== $hook ) {
return;
}
if ( $this->is_snoozed() ) {
return;
}
wp_enqueue_style( 'wpr-modal', rocket_get_constant( 'WP_ROCKET_ASSETS_CSS_URL' ) . 'wpr-modal.css', null, rocket_get_constant( 'WP_ROCKET_VERSION' ) );
wp_enqueue_script( 'micromodal', rocket_get_constant( 'WP_ROCKET_ASSETS_JS_URL' ) . 'micromodal.min.js', null, '0.4.10', true );
wp_add_inline_script(
'micromodal',
'window.addEventListener("DOMContentLoaded", (event) => {
document.getElementById("deactivate-wp-rocket").addEventListener("click", (event) => {event.preventDefault();});MicroModal.init();
});'
);
}
/**
* Add data attribute to WP Rocket deactivation link for the modal
*
* @since 3.11.1
*
* @param string[] $actions An array of plugin action links.
*
* @return array
*/
public function add_data_attribute( $actions ) {
if ( ! isset( $actions['deactivate'] ) ) {
return $actions;
}
$deactivate_link = str_replace( '<a', '<a data-micromodal-trigger="wpr-deactivation-modal"', $actions['deactivate'] );
$actions['deactivate'] = $deactivate_link;
return $actions;
}
}
@@ -0,0 +1,107 @@
<?php
namespace WP_Rocket\Engine\Admin\Deactivation;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Subscriber implements Subscriber_Interface {
/**
* DeactivationIntent instance
*
* @var DeactivationIntent
*/
private $deactivation;
/**
* Constructor
*
* @param DeactivationIntent $deactivation DeactivationIntent instance.
*/
public function __construct( DeactivationIntent $deactivation ) {
$this->deactivation = $deactivation;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'admin_footer-plugins.php' => 'insert_deactivation_intent_form',
'admin_enqueue_scripts' => 'add_modal_assets',
'admin_post_rocket_deactivation' => 'safe_mode_or_deactivate',
'plugin_action_links_wp-rocket/wp-rocket.php' => 'add_data_attribute',
];
}
/**
* Inserts the deactivation intent form on plugins page
*
* @since 3.11.1
*
* @return void
*/
public function insert_deactivation_intent_form() {
$this->deactivation->insert_deactivation_intent_form();
}
/**
* Add modal assets on the plugins page
*
* @since 3.11.1
*
* @param string $hook The current admin page.
*
* @return void
*/
public function add_modal_assets( $hook ) {
$this->deactivation->add_modal_assets( $hook );
}
/**
* Apply safe mode or deactivate the plugin
*
* @since 3.11.1
*
* @return void
*/
public function safe_mode_or_deactivate() {
check_admin_referer( 'rocket_deactivation', '_wpnonce' );
$referer = wp_get_referer();
if ( ! current_user_can( 'manage_options' ) ) {
wp_safe_redirect( $referer );
exit;
}
$mode = isset( $_POST['mode'] ) ? sanitize_key( $_POST['mode'] ) : '';
if ( 'safe_mode' === $mode ) {
$this->deactivation->activate_safe_mode();
} elseif ( 'deactivate' === $mode ) {
$snooze = isset( $_POST['snooze'] ) ? absint( $_POST['snooze'] ) : 0;
$this->deactivation->deactivate_and_snooze( $snooze );
$referer = add_query_arg( 'deactivate', '1', $referer );
}
wp_safe_redirect( $referer );
exit;
}
/**
* Add data attribute to WP Rocket deactivation link for the modal
*
* @since 3.11.1
*
* @param string[] $actions An array of plugin action links.
*
* @return array
*/
public function add_data_attribute( $actions ): array {
return $this->deactivation->add_data_attribute( $actions );
}
}
@@ -0,0 +1,35 @@
<?php
namespace WP_Rocket\Engine\Admin\DomainChange;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Common\Ajax\AjaxHandler;
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'domain_change_subscriber',
'ajax_handler',
];
/**
* Registers items with the container
*
* @return void
*/
public function register() {
$this->getContainer()->add( 'ajax_handler', AjaxHandler::class );
$this->getContainer()->add( 'domain_change_subscriber', Subscriber::class )
->addArgument( $this->getContainer()->get( 'ajax_handler' ) )
->addArgument( $this->getContainer()->get( 'beacon' ) );
}
}
@@ -0,0 +1,210 @@
<?php
namespace WP_Rocket\Engine\Admin\DomainChange;
use WP_Rocket\Engine\Admin\Beacon\Beacon;
use WP_Rocket\Engine\Common\Ajax\AjaxHandler;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Subscriber implements Subscriber_Interface {
/**
* Handle basic ajax operations.
*
* @var AjaxHandler
*/
protected $ajax_handler;
/**
* Beacon instance
*
* @var Beacon
*/
protected $beacon;
/**
* Name of the option saving the last base URL.
*
* @var string
*/
const LAST_BASE_URL_OPTION = 'wp_rocket_last_base_url';
/**
* Instantiate the class.
*
* @param AjaxHandler $ajax_handler Handle basic ajax operations.
* @param Beacon $beacon Beacon instance.
*/
public function __construct( AjaxHandler $ajax_handler, Beacon $beacon ) {
$this->ajax_handler = $ajax_handler;
$this->beacon = $beacon;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return string[]
*/
public static function get_subscribed_events() {
return [
'admin_init' => 'maybe_launch_domain_changed',
'admin_notices' => 'maybe_display_domain_change_notice',
'rocket_domain_changed' => 'maybe_clean_cache_domain_change',
'rocket_notice_args' => 'add_regenerate_configuration_action',
'admin_post_rocket_regenerate_configuration' => 'regenerate_configuration',
];
}
/**
* Maybe launch the domain changed event.
*
* @return void
*/
public function maybe_launch_domain_changed() {
if ( wp_doing_ajax() ) {
return;
}
$base_url = trailingslashit( get_option( 'home' ) );
$base_url_encoded = base64_encode( $base_url ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$last_base_url_encoded = get_option( self::LAST_BASE_URL_OPTION );
if ( ! $last_base_url_encoded ) {
update_option( self::LAST_BASE_URL_OPTION, $base_url_encoded, true );
return;
}
if ( $base_url_encoded === $last_base_url_encoded ) {
return;
}
update_option( self::LAST_BASE_URL_OPTION, $base_url_encoded, true );
$last_base_url = base64_decode( $last_base_url_encoded ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
set_transient( 'rocket_domain_changed', $last_base_url_encoded, 2 * rocket_get_constant( 'WEEK_IN_SECONDS', 604800 ) );
/**
* Fires when the domain of the website has been changed.
*
* @param string $current_url current URL from the website.
* @param string $old_url old URL from the website.
*/
do_action( 'rocket_detected_domain_changed', $base_url, $last_base_url );
}
/**
* Maybe clean cache on domain change.
*
* @return void
*/
public function maybe_clean_cache_domain_change() {
$options = get_option( rocket_get_constant( 'WP_ROCKET_SLUG' ) );
if ( ! $options ) {
return;
}
/**
* Fires after WP Rocket options that require a cache purge have changed
*
* @param array $value An array of submitted values for the settings.
*/
do_action( 'rocket_domain_options_changed', $options );
}
/**
* Maybe display a notice when domain change.
*
* @return void
*/
public function maybe_display_domain_change_notice() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
$notice = get_transient( 'rocket_domain_changed' );
if ( ! $notice || is_multisite() ) {
return;
}
$beacon = $this->beacon->get_suggest( 'domain_change' );
$args = [
'status' => 'warning',
'dismissible' => '',
'dismiss_button' => false,
'message' => sprintf(
// translators: %1$s = <strong>, %2$s = </strong>, %3$s = <a>, %4$s = </a>.
__( '%1$sWP Rocket:%2$s We detected that the website domain has changed. The configuration files must be regenerated for the page cache and all other optimizations to work as intended. %3$sLearn More%4$s', 'rocket' ),
'<strong>',
'</strong>',
'<a href="' . esc_url( $beacon['url'] ) . '" data-beacon-article="' . esc_attr( $beacon['id'] ) . '" target="_blank" rel="noopener noreferrer">',
'</a>'
),
'action' => 'regenerate_configuration',
];
rocket_notice_html( $args );
}
/**
* Add mapping on notice.
*
* @param array $args Arguments from the notice.
*
* @return array
*/
public function add_regenerate_configuration_action( $args ) {
if ( ! key_exists( 'action', $args ) || 'regenerate_configuration' !== $args['action'] ) {
return $args;
}
$params = [
'action' => 'rocket_regenerate_configuration',
];
$args['action'] = '<a class="wp-core-ui button" href="' . add_query_arg( $params, wp_nonce_url( admin_url( 'admin-post.php' ), 'rocket_regenerate_configuration' ) ) . '">' . __( 'Regenerate WP Rocket configuration files now', 'rocket' ) . '</a>';
return $args;
}
/**
* Regenerate configurations.
*
* @return void
*/
public function regenerate_configuration() {
if ( ! $this->ajax_handler->validate_referer(
'rocket_regenerate_configuration',
'rocket_manage_options'
) ) {
return;
}
$last_base_url_encoded = get_transient( 'rocket_domain_changed' );
if ( ! $last_base_url_encoded ) {
return;
}
$last_base_url = base64_decode( $last_base_url_encoded ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$base_url = trailingslashit( get_option( 'home' ) );
/**
* Fires when the domain of the website has been changed and user clicked on notice.
*
* @param string $current_url current URL from the website.
* @param string $old_url old URL from the website.
*/
do_action( 'rocket_domain_changed', $base_url, $last_base_url );
delete_transient( 'rocket_domain_changed' );
$this->ajax_handler->redirect();
}
}
@@ -0,0 +1,205 @@
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Admin\Metaboxes;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
class PostEditOptionsSubscriber extends Abstract_Render implements Subscriber_Interface {
/**
* Options instance
*
* @var Options_data
*/
private $options;
/**
* Constructor
*
* @param Options_Data $options Options instance.
* @param string $template_path Path to the views.
*/
public function __construct( Options_Data $options, $template_path ) {
parent::__construct( $template_path );
$this->options = $options;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'add_meta_boxes' => 'options_metabox',
'save_post' => 'save_metabox_options',
];
}
/**
* Add options metabox on post edit page
*
* @return void
*/
public function options_metabox() {
if ( ! rocket_can_display_options() ) {
return;
}
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
$cpts = get_post_types(
[
'public' => true,
],
'objects'
);
unset( $cpts['attachment'] );
/**
* Filters the post types to add the options metabox to
*
* @param array $cpts Array of post types.
*/
$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 ), [ $this, 'display_metabox' ], $cpt, 'side', 'core' );
}
}
/**
* Displays checkboxes to de/activate some options
*
* @return void
*/
public function display_metabox() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
global $post, $pagenow;
$excluded_url = false;
if ( 'post-new.php' !== $pagenow ) {
$path = rocket_clean_exclude_file( get_permalink( $post->ID ) );
if ( in_array( $path, $this->options->get( 'cache_reject_uri', [] ), true ) ) {
$excluded_url = true;
}
}
$original_fields = [];
/**
* WP Rocket Metabox fields on post edit page.
*
* @param string[] $original_fields Metaboxes fields.
*/
$fields = apply_filters( 'rocket_meta_boxes_fields', $original_fields );
if ( ! is_array( $fields ) ) {
$fields = $original_fields;
}
$fields_attributes = [];
foreach ( $fields as $field => $label ) {
$disabled = disabled( ! $this->options->get( $field ), true, false );
$fields_attributes[ $field ]['id'] = $field;
$fields_attributes[ $field ]['label'] = $label;
// translators: %s is the name of the option.
$fields_attributes[ $field ]['title'] = $disabled ? ' title="' . esc_attr( sprintf( __( 'Activate first the %s option.', 'rocket' ), $label ) ) . '"' : '';
$fields_attributes[ $field ]['class'] = $disabled ? ' class="rkt-disabled"' : '';
$fields_attributes[ $field ]['checked'] = ! $disabled ? checked( ! get_post_meta( $post->ID, '_rocket_exclude_' . $field, true ), true, false ) : '';
$fields_attributes[ $field ]['disabled'] = $disabled;
}
echo $this->generate( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'post_edit_options',
[
'excluded_url' => $excluded_url, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'fields' => $fields_attributes, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
]
);
}
/**
* Updates the options from the metabox.
*
* @return void
*/
public function save_metabox_options() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
if ( ! isset( $_POST['post_ID'], $_POST['rocket_post_exclude_hidden'] ) ) {
return;
}
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 = $this->options->get( '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 );
}
} elseif ( 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();
}
}
$original_fields = [];
/**
* Metaboxes fields.
*
* @param string[] $original_fields Metaboxes fields.
*/
$fields = apply_filters( 'rocket_meta_boxes_fields', $original_fields );
if ( ! is_array( $fields ) ) {
$fields = $original_fields;
}
$fields = array_keys( $fields );
foreach ( $fields as $field ) {
if ( ! isset( $_POST['rocket_post_exclude_hidden'][ $field ] ) ) {
continue;
}
if ( isset( $_POST['rocket_post_exclude'][ $field ] ) ) {
delete_post_meta( (int) $_POST['post_ID'], '_rocket_exclude_' . $field );
continue;
}
if ( $this->options->get( $field ) ) {
update_post_meta( (int) $_POST['post_ID'], '_rocket_exclude_' . $field, true );
}
}
}
}
@@ -0,0 +1,57 @@
<?php
namespace WP_Rocket\Engine\Admin;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Admin\Deactivation\{DeactivationIntent, Subscriber};
use WP_Rocket\Engine\Admin\Metaboxes\PostEditOptionsSubscriber;
use WP_Rocket\ThirdParty\Plugins\Optimization\Hummingbird;
/**
* Service Provider for admin subscribers.
*
* @since 3.3
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'deactivation_intent',
'deactivation_intent_subscriber',
'hummingbird_subscriber',
'actionscheduler_admin_subscriber',
'post_edit_options_subscriber',
];
/**
* Registers items with the container
*
* @return void
*/
public function register() {
$options = $this->getContainer()->get( 'options' );
$this->getContainer()->add( 'deactivation_intent', DeactivationIntent::class )
->addArgument( $this->getContainer()->get( 'template_path' ) . '/deactivation-intent' )
->addArgument( $this->getContainer()->get( 'options_api' ) )
->addArgument( $options );
$this->getContainer()->share( 'deactivation_intent_subscriber', Subscriber::class )
->addArgument( $this->getContainer()->get( 'deactivation_intent' ) )
->addTag( 'admin_subscriber' );
$this->getContainer()->share( 'hummingbird_subscriber', Hummingbird::class )
->addArgument( $options )
->addTag( 'admin_subscriber' );
$this->getContainer()->share( 'actionscheduler_admin_subscriber', ActionSchedulerSubscriber::class );
$this->getContainer()->share( 'post_edit_options_subscriber', PostEditOptionsSubscriber::class )
->addArgument( $options )
->addArgument( $this->getContainer()->get( 'template_path' ) . '/metaboxes' )
->addTag( 'admin_subscriber' );
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,517 @@
<?php
namespace WP_Rocket\Engine\Admin\Settings;
use stdClass;
use WP_Rocket\Abstract_Render;
defined( 'ABSPATH' ) || exit;
/**
* Handle rendering of HTML content for the settings page.
*
* @since 3.5.5 Moves into the new architecture.
* @since 3.0
*/
class Render extends Abstract_render {
/**
* Settings array.
*
* @since 3.0
*
* @var array
*/
private $settings = [];
/**
* Hidden settings array.
*
* @since 3.0
*
* @var array
*/
private $hidden_settings;
/**
* Sets the settings value.
*
* @since 3.0
*
* @param array $settings Array of settings.
* @return void
*/
public function set_settings( $settings ) {
$this->settings = (array) $settings;
}
/**
* Sets the hidden settings value.
*
* @since 3.0
*
* @param array $hidden_settings Array of hidden settings.
*/
public function set_hidden_settings( $hidden_settings ) {
$this->hidden_settings = $hidden_settings;
}
/**
* Renders the page sections navigation.
*
* @since 3.0
*/
public function render_navigation() {
/**
* Filters WP Rocket settings page navigation items.
*
* @since 3.0
*
* @param array $navigation {
* Items to populate the navigation.
*
* @type string $id Page section identifier.
* @type string $title Menu item title.
* @type string $menu_description Menu item description.
* @type string $class Menu item classes
* }
*/
$navigation = (array) apply_filters( 'rocket_settings_menu_navigation', $this->settings );
$default = [
'id' => '',
'title' => '',
'menu_description' => '',
'class' => '',
];
$navigation = array_map(
function ( array $item ) use ( $default ) {
$item = wp_parse_args( $item, $default );
if ( ! empty( $item['class'] ) ) {
$item['class'] = implode( ' ', array_map( 'sanitize_html_class', $item['class'] ) );
}
unset( $item['sections'] );
return $item;
},
$navigation
);
echo $this->generate( 'navigation', $navigation ); // phpcs:ignore WordPress.Security.EscapeOutput -- Dynamic content is properly escaped in the view.
}
/**
* Render the page sections.
*
* @since 3.0
*/
public function render_form_sections() {
if ( ! isset( $this->settings ) ) {
return;
}
foreach ( $this->settings as $id => $args ) {
$default = [
'title' => '',
'menu_description' => '',
'class' => '',
];
$args = wp_parse_args( $args, $default );
$id = str_replace( '_', '-', $id );
echo $this->generate( 'page-sections/' . $id, $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
}
/**
* Render the Imagify page section.
*
* @since 3.2
*/
public function render_imagify_section() {
echo $this->generate( 'page-sections/imagify' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Render the Tutorials page section.
*
* @since 3.4
*/
public function render_tutorials_section() {
echo $this->generate( 'page-sections/tutorials' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Render the tools page section.
*
* @since 3.0
*/
public function render_tools_section() {
echo $this->generate( 'page-sections/tools' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Renders the settings sections for a page section.
*
* @since 3.0
*
* @param string $page Page section identifier.
* @return void
*/
public function render_settings_sections( $page ) {
if ( ! isset( $this->settings[ $page ]['sections'] ) ) {
return;
}
foreach ( $this->settings[ $page ]['sections'] as $args ) {
$default = [
'type' => 'fields_container',
'title' => '',
'description' => '',
'class' => '',
'help' => '',
'helper' => '',
'page' => '',
];
$args = wp_parse_args( $args, $default );
if ( ! empty( $args['class'] ) ) {
$args['class'] = implode( ' ', array_map( 'sanitize_html_class', $args['class'] ) );
}
call_user_func_array( [ $this, $args['type'] ], [ $args ] );
}
}
/**
* Renders the settings fields for a setting section and page.
*
* @since 3.0
*
* @param string $page Page section identifier.
* @param string $section Settings section identifier.
* @return void
*/
public function render_settings_fields( $page, $section ) {
if ( ! isset( $this->settings[ $page ]['sections'][ $section ]['fields'] ) ) {
return;
}
$this->render_fields( $this->settings[ $page ]['sections'][ $section ]['fields'] );
}
/**
* Renders hidden fields in the form.
*
* @since 3.0
*/
public function render_hidden_fields() {
foreach ( $this->hidden_settings as $setting ) {
call_user_func_array( [ $this, 'hidden' ], [ $setting ] );
}
}
/**
* Displays the fields container section template.
*
* @since 3.0
* @author Remy Perona
*
* @param array $args Array of arguments to populate the template.
*/
public function fields_container( $args ) {
echo $this->generate( 'sections/fields-container', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the no container section template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function nocontainer( $args ) {
echo $this->generate( 'sections/nocontainer', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the add-ons container section template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function addons_container( $args ) {
echo $this->generate( 'sections/addons-container', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the text field template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function text( $args ) {
echo $this->generate( 'fields/text', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the checkbox field template.
*
* @since 3.0
* @author Remy Perona
*
* @param array $args Array of arguments to populate the template.
* @return void
*/
public function checkbox( $args ) {
echo $this->generate( 'fields/checkbox', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the textarea field template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function textarea( $args ) {
if ( is_array( $args['value'] ) ) {
$args['value'] = implode( "\n", $args['value'] );
}
$args['value'] = empty( $args['value'] ) ? '' : $args['value'];
echo $this->generate( 'fields/textarea', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the sliding checkbox field template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function sliding_checkbox( $args ) {
echo $this->generate( 'fields/sliding-checkbox', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the number input field template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function number( $args ) {
echo $this->generate( 'fields/number', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the multiselect field template.
*
* @param array $args Array of arguments to populate the template.
*/
public function categorized_multiselect( $args ) {
$args['items'] = empty( $args['items'] ) ? new stdClass() : $args['items'];
$args['selected'] = get_rocket_option( sanitize_key( $args['id'] ), [] );
echo $this->generate( 'fields/categorized_multiselect', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the select field template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function select( $args ) {
echo $this->generate( 'fields/select', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the clear cache lifespan block template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function cache_lifespan( $args ) {
echo $this->generate( 'fields/cache-lifespan', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the hidden field template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function hidden( $args ) {
echo $this->generate( 'fields/hidden', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the CDN CNAMES template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function cnames( $args ) {
echo $this->generate( 'fields/cnames', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the RocketCDN template.
*
* @since 3.5
*
* @param array $args Array of arguments to populate the template.
*/
public function rocket_cdn( $args ) {
echo $this->generate( 'fields/rocket-cdn', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the one-click add-on field template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function one_click_addon( $args ) {
echo $this->generate( 'fields/one-click-addon', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the Rocket add-on field template.
*
* @since 3.0
*
* @param array $args Array of arguments to populate the template.
*/
public function rocket_addon( $args ) {
echo $this->generate( 'fields/rocket-addon', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the import form template.
*
* @since 3.0
*/
public function render_import_form() {
$args = [];
/**
* Filter the maximum allowed upload size for import files.
*
* @since (WordPress) 2.3.0
*
* @see wp_max_upload_size()
*
* @param int $max_upload_size Allowed upload size. Default 1 MB.
*/
$args['bytes'] = apply_filters( 'import_upload_size_limit', wp_max_upload_size() ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
$args['size'] = size_format( $args['bytes'] );
$args['upload_dir'] = wp_upload_dir();
$args['action'] = 'rocket_import_settings';
$args['submit_text'] = __( 'Upload file and import settings', 'rocket' );
echo $this->generate( 'fields/import-form', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays a partial template.
*
* @since 3.0
*
* @param string $part Partial template name.
*/
public function render_part( $part ) {
echo $this->generate( 'partials/' . $part ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Displays the radio_buttons field template.
*
* @since 3.10
*
* @param array $args Array of arguments to populate the template.
*/
public function radio_buttons( $args ) {
echo $this->generate( 'fields/radio-buttons', $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
}
/**
* Renders the fields.
*
* @since 3.10
*
* @param array $fields fields to render.
*
* @return void
*/
public function render_fields( $fields ) {
foreach ( $fields as $id => $args ) {
$default = [
'type' => 'text',
'label' => '',
'description' => '',
'class' => '',
'container_class' => '',
'default' => '',
'helper' => '',
'placeholder' => '',
'parent' => '',
'section' => '',
'page' => '',
'sanitize_callback' => 'sanitize_text_field',
'input_attr' => '',
'warning' => [],
];
$args = wp_parse_args( $args, $default );
if ( empty( $args['id'] ) ) {
$args['id'] = $id;
}
if ( ! empty( $args['input_attr'] ) ) {
$input_attr = '';
foreach ( $args['input_attr'] as $key => $value ) {
if ( 'disabled' === $key ) {
if ( 1 === $value ) {
$input_attr .= ' disabled';
}
continue;
}
$input_attr .= ' ' . sanitize_key( $key ) . '="' . esc_attr( $value ) . '"';
}
$args['input_attr'] = $input_attr;
}
if ( ! empty( $args['parent'] ) ) {
$args['parent'] = ' data-parent="' . esc_attr( $args['parent'] ) . '"';
}
if ( ! empty( $args['class'] ) ) {
$args['class'] = implode( ' ', array_map( 'sanitize_html_class', $args['class'] ) );
}
if ( ! empty( $args['container_class'] ) ) {
$args['container_class'] = implode( ' ', array_map( 'sanitize_html_class', $args['container_class'] ) );
}
call_user_func_array( [ $this, $args['type'] ], [ $args ] );
}
}
}
@@ -0,0 +1,52 @@
<?php
namespace WP_Rocket\Engine\Admin\Settings;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
/**
* Service provider for the WP Rocket settings.
*
* @since 3.5.5 Moves into the new architecture.
* @since 3.3
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'settings',
'settings_render',
'settings_page',
'settings_page_subscriber',
];
/**
* Registers items with the container
*
* @return void
*/
public function register() {
$this->getContainer()->add( 'settings', Settings::class )
->addArgument( $this->getContainer()->get( 'options' ) );
$this->getContainer()->add( 'settings_render', Render::class )
->addArgument( $this->getContainer()->get( 'template_path' ) . '/settings' );
$this->getContainer()->add( 'settings_page', Page::class )
->addArgument( $this->getContainer()->get( 'settings_page_config' ) )
->addArgument( $this->getContainer()->get( 'settings' ) )
->addArgument( $this->getContainer()->get( 'settings_render' ) )
->addArgument( $this->getContainer()->get( 'beacon' ) )
->addArgument( $this->getContainer()->get( 'db_optimization' ) )
->addArgument( $this->getContainer()->get( 'user_client' ) )
->addArgument( $this->getContainer()->get( 'delay_js_sitelist' ) );
$this->getContainer()->share( 'settings_page_subscriber', Subscriber::class )
->addArgument( $this->getContainer()->get( 'settings_page' ) )
->addTag( 'admin_subscriber' );
}
}
@@ -0,0 +1,701 @@
<?php
namespace WP_Rocket\Engine\Admin\Settings;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Addon\Sucuri\Subscriber as SucuriSubscriber;
/**
* Settings class.
*
* @since 3.5.5 Moves into the new architecture.
*/
class Settings {
/**
* Options_Data instance.
*
* @since 3.0
*
* @var Options_Data
*/
private $options;
/**
* Array of settings to build the settings page.
*
* @since 3.0
*
* @var array
*/
private $settings;
/**
* Hidden settings on the settings page.
*
* @since 3.0
*
* @var array
*/
private $hidden_settings;
/**
* Font formats allowed to be preloaded.
*
* @since 3.6
* @see $this->sanitize_font()
*
* @var array
*/
private $font_formats = [
'otf',
'ttf',
'svg',
'woff',
'woff2',
];
/**
* Array of valid hosts.
*
* @since 3.6
* @see $this->get_hosts()
*
* @var array
*/
private $hosts;
/**
* Constructor
*
* @since 3.0
*
* @param Options_Data $options Options_Data instance.
*/
public function __construct( Options_Data $options ) {
$this->options = $options;
}
/**
* Adds a page section to the settings.
*
* A page section is a top-level block containing settings sections.
*
* @since 3.0
*
* @param string $id Page section identifier.
* @param array $args {
* Data to build the section.
*
* @type string $title Section title.
* @type string $menu_description Description displayed in the navigation.
* }
* @return void
*/
public function add_page_section( $id, $args ) {
$args['id'] = $id;
$this->settings[ $id ] = $args;
}
/**
* Adds settings sections to the settings.
*
* A setting section is a block containing settings fields.
*
* @since 3.0
*
* @param array $settings_sections {
* Data to build the section.
*
* @type string $title Section title.
* @type string $type Type of section.
* @type string $page Page section identifier it belongs to.
* @type string $description Section description.
* @type string $help Helper IDs for beacon.
* }
* @return void
*/
public function add_settings_sections( $settings_sections ) {
foreach ( $settings_sections as $id => $args ) {
$args['id'] = $id;
$this->settings[ $args['page'] ]['sections'][ $id ] = $args;
}
}
/**
* Adds settings fields to the settings.
*
* @since 3.0
*
* @param array $settings_fields {
* Data to build the section.
*
* @type string $id Identifier.
* @type string $title Field title.
* }
* @return void
*/
public function add_settings_fields( $settings_fields ) {
foreach ( $settings_fields as $id => $args ) {
$args['id'] = $id;
$args['value'] = $this->options->get( $id, $args['default'] );
$page = $args['page'];
$section = $args['section'];
unset( $args['page'], $args['section'] );
/**
* Filters the field before add to the settings
*
* @since 3.10
*
* @param array $input Array of sanitized values after being submitted by the form.
*/
$args = apply_filters( 'rocket_before_add_field_to_settings', $args );
$this->settings[ $page ]['sections'][ $section ]['fields'][ $id ] = $args;
}
}
/**
* Adds hidden settings fields to the settings.
*
* @since 3.0
*
* @param array $hidden_settings_fields {
* Data to build the section.
*
* @type string $id Identifier.
* }
* @return void
*/
public function add_hidden_settings_fields( $hidden_settings_fields ) {
foreach ( $hidden_settings_fields as $id ) {
$value = $this->options->get( $id );
$this->hidden_settings[] = [
'id' => $id,
'value' => $value,
];
}
}
/**
* Returns the plugin settings
*
* @since 3.0
*
* @return array
*/
public function get_settings() {
return $this->settings;
}
/**
* Returns the plugin hidden settings
*
* @since 3.0
*
* @return array
*/
public function get_hidden_settings() {
return $this->hidden_settings;
}
/**
* Sanitizes the submitted values.
*
* @since 3.0
*
* @param array $input Array of values submitted by the form.
* @return array
*/
public function sanitize_callback( $input ) {
global $wp_settings_errors;
$input['cache_logged_user'] = ! empty( $input['cache_logged_user'] ) ? 1 : 0;
$input['cache_ssl'] = ! empty( $input['cache_ssl'] ) ? 1 : 0;
$input['cache_mobile'] = ! empty( $input['cache_mobile'] ) ? 1 : 0;
$input['do_caching_mobile_files'] = ! empty( $input['do_caching_mobile_files'] ) ? 1 : 0;
$input['minify_google_fonts'] = ! empty( $input['minify_google_fonts'] ) ? 1 : 0;
// Option : Minification CSS & JS.
$input['minify_css'] = ! empty( $input['minify_css'] ) ? 1 : 0;
$input['minify_js'] = ! empty( $input['minify_js'] ) ? 1 : 0;
$input['minify_concatenate_js'] = ! empty( $input['minify_concatenate_js'] ) ? 1 : 0;
$input['defer_all_js'] = ! empty( $input['defer_all_js'] ) ? 1 : 0;
$input['exclude_defer_js'] = ! empty( $input['exclude_defer_js'] ) ? rocket_sanitize_textarea_field( 'exclude_defer_js', $input['exclude_defer_js'] ) : [];
$input['emoji'] = ! empty( $input['emoji'] ) ? 1 : 0;
$input['lazyload'] = ! empty( $input['lazyload'] ) ? 1 : 0;
$input['lazyload_iframes'] = ! empty( $input['lazyload_iframes'] ) ? 1 : 0;
$input['lazyload_youtube'] = ! empty( $input['lazyload_youtube'] ) ? 1 : 0;
// If iframes lazyload is not checked, uncheck youtube thumbnail option too.
if ( 0 === $input['lazyload_iframes'] ) {
$input['lazyload_youtube'] = 0;
}
// Option : Purge interval.
$input['purge_cron_interval'] = isset( $input['purge_cron_interval'] ) ? (int) $input['purge_cron_interval'] : $this->options->get( 'purge_cron_interval' );
$allowed_cron_units = [
'HOUR_IN_SECONDS' => 1,
'DAY_IN_SECONDS' => 1,
];
$input['purge_cron_unit'] = isset( $input['purge_cron_unit'], $allowed_cron_units[ $input['purge_cron_unit'] ] ) ? $input['purge_cron_unit'] : $this->options->get( 'purge_cron_unit' );
// Option : Prefetch DNS requests.
$input['dns_prefetch'] = $this->sanitize_dns_prefetch( $input );
// Option : Empty the cache of the following pages when updating a post.
if ( ! empty( $input['cache_purge_pages'] ) ) {
$input['cache_purge_pages'] = rocket_sanitize_textarea_field( 'cache_purge_pages', $input['cache_purge_pages'] );
} else {
$input['cache_purge_pages'] = [];
}
// Option : Never cache the following pages.
if ( ! empty( $input['cache_reject_uri'] ) ) {
$input['cache_reject_uri'] = rocket_sanitize_textarea_field( 'cache_reject_uri', $input['cache_reject_uri'] );
$input['cache_reject_uri'] = $this->check_global_exclusion( $input['cache_reject_uri'] );
} else {
$input['cache_reject_uri'] = [];
}
// Option : Don't cache pages that use the following cookies.
if ( ! empty( $input['cache_reject_cookies'] ) ) {
$input['cache_reject_cookies'] = rocket_sanitize_textarea_field( 'cache_reject_cookies', $input['cache_reject_cookies'] );
} else {
$input['cache_reject_cookies'] = [];
}
// Option : Cache pages that use the following query strings (GET parameters).
if ( ! empty( $input['cache_query_strings'] ) ) {
$input['cache_query_strings'] = rocket_sanitize_textarea_field( 'cache_query_strings', $input['cache_query_strings'] );
} else {
$input['cache_query_strings'] = [];
}
// Option : Never send cache pages for these user agents.
if ( ! empty( $input['cache_reject_ua'] ) ) {
$input['cache_reject_ua'] = rocket_sanitize_textarea_field( 'cache_reject_ua', $input['cache_reject_ua'] );
} else {
$input['cache_reject_ua'] = [];
}
// Option : CSS files to exclude from the minification.
if ( ! empty( $input['exclude_css'] ) ) {
$input['exclude_css'] = rocket_sanitize_textarea_field( 'exclude_css', $input['exclude_css'] );
} else {
$input['exclude_css'] = [];
}
// Option : JS files to exclude from the minification.
if ( ! empty( $input['exclude_js'] ) ) {
$input['exclude_js'] = rocket_sanitize_textarea_field( 'exclude_js', $input['exclude_js'] );
} else {
$input['exclude_js'] = [];
}
// Option: inline JS patterns to exclude from combine JS.
if ( ! empty( $input['exclude_inline_js'] ) ) {
$input['exclude_inline_js'] = rocket_sanitize_textarea_field( 'exclude_inline_js', $input['exclude_inline_js'] );
} else {
$input['exclude_inline_js'] = [];
}
// Option: Async CSS.
$input['async_css'] = ! empty( $input['async_css'] ) ? 1 : 0;
// Option: Critical CSS.
$input['critical_css'] = ! empty( $input['critical_css'] ) ? wp_strip_all_tags( str_replace( [ '<style>', '</style>' ], '', $input['critical_css'] ), [ "\'", '\"' ] ) : '';
// Database options.
$input['database_revisions'] = ! empty( $input['database_revisions'] ) ? 1 : 0;
$input['database_auto_drafts'] = ! empty( $input['database_auto_drafts'] ) ? 1 : 0;
$input['database_trashed_posts'] = ! empty( $input['database_trashed_posts'] ) ? 1 : 0;
$input['database_spam_comments'] = ! empty( $input['database_spam_comments'] ) ? 1 : 0;
$input['database_trashed_comments'] = ! empty( $input['database_trashed_comments'] ) ? 1 : 0;
$input['database_all_transients'] = ! empty( $input['database_all_transients'] ) ? 1 : 0;
$input['database_optimize_tables'] = ! empty( $input['database_optimize_tables'] ) ? 1 : 0;
$input['schedule_automatic_cleanup'] = ! empty( $input['schedule_automatic_cleanup'] ) ? 1 : 0;
$cleanup_frequencies = [
'daily' => 1,
'weekly' => 1,
'monthly' => 1,
];
$input['automatic_cleanup_frequency'] = isset( $input['automatic_cleanup_frequency'], $cleanup_frequencies[ $input['automatic_cleanup_frequency'] ] ) ? $input['automatic_cleanup_frequency'] : $this->options->get( 'automatic_cleanup_frequency' );
if ( 1 !== $input['schedule_automatic_cleanup'] && ( 'daily' !== $input['automatic_cleanup_frequency'] || 'weekly' !== $input['automatic_cleanup_frequency'] || 'monthly' !== $input['automatic_cleanup_frequency'] ) ) {
$input['automatic_cleanup_frequency'] = $this->options->get( 'automatic_cleanup_frequency' );
}
// Options: Activate bot preload.
$input['manual_preload'] = ! empty( $input['manual_preload'] ) ? 1 : 0;
// Option : fonts to preload.
$input['preload_fonts'] = ! empty( $input['preload_fonts'] ) ? $this->sanitize_fonts( $input['preload_fonts'] ) : [];
// Options: Sucuri cache. And yeah, there's a typo, but now it's too late to fix ^^'.
$input['sucury_waf_cache_sync'] = ! empty( $input['sucury_waf_cache_sync'] ) ? 1 : 0;
if ( defined( 'WP_ROCKET_SUCURI_API_KEY' ) ) {
$input['sucury_waf_api_key'] = WP_ROCKET_SUCURI_API_KEY;
} else {
$input['sucury_waf_api_key'] = isset( $input['sucury_waf_api_key'] ) ? sanitize_text_field( $input['sucury_waf_api_key'] ) : '';
}
$input['sucury_waf_api_key'] = trim( $input['sucury_waf_api_key'] );
if ( ! SucuriSubscriber::is_api_key_valid( $input['sucury_waf_api_key'] ) ) {
$input['sucury_waf_api_key'] = '';
if ( $input['sucury_waf_cache_sync'] && empty( $input['ignore'] ) ) {
add_settings_error( 'general', 'sucuri_waf_api_key_invalid', __( 'Sucuri Add-on: The API key for the Sucuri firewall must be in format <code>{32 characters}/{32 characters}</code>.', 'rocket' ), 'error' );
}
}
// Options : Heartbeat.
$choices = [
'' => 1,
'reduce_periodicity' => 1,
'disable' => 1,
];
$input['control_heartbeat'] = ! empty( $input['control_heartbeat'] ) ? 1 : 0;
$input['heartbeat_site_behavior'] = isset( $input['heartbeat_site_behavior'], $choices[ $input['heartbeat_site_behavior'] ] ) ? $input['heartbeat_site_behavior'] : '';
$input['heartbeat_admin_behavior'] = isset( $input['heartbeat_admin_behavior'], $choices[ $input['heartbeat_admin_behavior'] ] ) ? $input['heartbeat_admin_behavior'] : '';
$input['heartbeat_editor_behavior'] = isset( $input['heartbeat_editor_behavior'], $choices[ $input['heartbeat_editor_behavior'] ] ) ? $input['heartbeat_editor_behavior'] : '';
// Option : CDN.
$input['cdn'] = ! empty( $input['cdn'] ) ? 1 : 0;
// Option : CDN Cnames.
if ( isset( $input['cdn_cnames'] ) ) {
$input['cdn_cnames'] = $this->sanitize_cdn_cnames( $input['cdn_cnames'] );
} else {
$input['cdn_cnames'] = [];
}
if ( ! $input['cdn_cnames'] ) {
$input['cdn_zone'] = [];
} else {
$total_cdn_cnames = max( array_keys( $input['cdn_cnames'] ) );
for ( $i = 0; $i <= $total_cdn_cnames; $i++ ) {
if ( ! isset( $input['cdn_cnames'][ $i ] ) ) {
unset( $input['cdn_zone'][ $i ] );
} else {
$allowed_cdn_zones = [
'all' => 1,
'images' => 1,
'css_and_js' => 1,
'css' => 1,
'js' => 1,
];
$input['cdn_zone'][ $i ] = isset( $allowed_cdn_zones[ $input['cdn_zone'][ $i ] ] ) ? $input['cdn_zone'][ $i ] : 'all';
}
}
$input['cdn_cnames'] = array_values( $input['cdn_cnames'] );
$input['cdn_cnames'] = array_map( 'untrailingslashit', $input['cdn_cnames'] );
ksort( $input['cdn_zone'] );
$input['cdn_zone'] = array_values( $input['cdn_zone'] );
}
// Option : Files to exclude from the CDN process.
if ( ! empty( $input['cdn_reject_files'] ) ) {
$input['cdn_reject_files'] = rocket_sanitize_textarea_field( 'cdn_reject_files', $input['cdn_reject_files'] );
} else {
$input['cdn_reject_files'] = [];
}
$input['varnish_auto_purge'] = ! empty( $input['varnish_auto_purge'] ) ? 1 : 0;
if ( ! rocket_valid_key() ) {
$checked = rocket_check_key();
}
if ( isset( $checked ) && is_array( $checked ) ) {
$input['consumer_key'] = $checked['consumer_key'];
$input['consumer_email'] = $checked['consumer_email'];
$input['secret_key'] = $checked['secret_key'];
}
if ( ! empty( $input['secret_key'] ) && empty( $input['ignore'] ) && rocket_valid_key() ) {
// Add a "Settings saved." admin notice only if not already added.
$notices = array_merge( (array) $wp_settings_errors, (array) get_transient( 'settings_errors' ) );
$notices = array_filter(
$notices,
function ( $error ) {
if ( ! $error || ! is_array( $error ) ) {
return false;
}
if ( ! isset( $error['setting'], $error['code'], $error['type'] ) ) {
return false;
}
return 'general' === $error['setting'] && 'settings_updated' === $error['code'] && 'updated' === $error['type'];
}
);
if ( ! $notices ) {
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'rocket' ), 'updated' );
}
}
unset( $input['ignore'] );
/**
* Filters the sanitized input before returning the array
*
* @param array $input Array of sanitized values after being submitted by the form.
* @param Settings $settings Current class instance.
*/
return apply_filters( 'rocket_input_sanitize', $input, $this );
}
/**
* Sanitizes the returned value of a checkbox
*
* @since 3.0
*
* @param array $array Options array.
* @param string $key Array key to check.
* @return int
*/
public function sanitize_checkbox( $array, $key ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
return isset( $array[ $key ] ) && ! empty( $array[ $key ] ) ? 1 : 0;
}
/**
* Sanitizes the DNS Prefetch sub-option value
*
* @since 3.5.1
*
* @param array $input Array of values for the WP Rocket settings option.
* @return array Sanitized array for the DNS Prefetch sub-option
*/
private function sanitize_dns_prefetch( array $input ) {
if ( empty( $input['dns_prefetch'] ) ) {
return [];
}
$value = $input['dns_prefetch'];
if ( ! is_array( $value ) ) {
$value = explode( "\n", $value );
}
$urls = [];
foreach ( $value as $url ) {
$url = trim( $url );
if ( empty( $url ) ) {
continue;
}
$url = preg_replace( '/^(?:https?)?:?\/{3,}/i', 'http://', $url );
$url = esc_url_raw( $url );
if ( empty( $url ) ) {
continue;
}
$urls[] = $url;
}
if ( empty( $urls ) ) {
return [];
}
return array_unique(
array_map(
function ( $url ) {
return '//' . wp_parse_url( $url, PHP_URL_HOST );
},
$urls
)
);
}
/**
* Sanitize a list of font file paths.
*
* @since 3.6
*
* @param array|string $files List of filepaths to sanitize. Can be an array of strings or a string listing paths separated by "\n".
* @return array Sanitized filepaths.
*/
private function sanitize_fonts( $files ) {
if ( ! is_array( $files ) ) {
$files = explode( "\n", trim( $files ) );
}
$files = array_map( [ $this, 'sanitize_font' ], $files );
return array_unique( array_filter( $files ) );
}
/**
* Sanitize an entry for the preload fonts option.
*
* @since 3.6
*
* @param string $file URL or path to a font file.
* @return string|bool
*/
private function sanitize_font( $file ) {
if ( ! is_string( $file ) ) {
return false;
}
$file = trim( $file );
if ( empty( $file ) ) {
return false;
}
$parsed_url = wp_parse_url( $file );
$hosts = $this->get_hosts();
if ( ! empty( $parsed_url['host'] ) ) {
$match = false;
foreach ( $hosts as $host ) {
if ( false !== strpos( $file, $host ) ) {
$match = true;
break;
}
}
if ( ! $match ) {
return false;
}
}
$file = str_replace( [ 'http:', 'https:' ], '', $file );
$file = str_replace( $hosts, '', $file );
$file = '/' . ltrim( $file, '/' );
$ext = strtolower( pathinfo( $parsed_url['path'], PATHINFO_EXTENSION ) );
if ( ! in_array( $ext, $this->font_formats, true ) ) {
return false;
}
return $file;
}
/**
* Gets an array of valid hosts.
*
* @since 3.6
*
* @return array
*/
private function get_hosts() {
if ( isset( $this->hosts ) ) {
return $this->hosts;
}
$urls = (array) $this->options->get( 'cdn_cnames', [] );
$urls[] = home_url();
$urls = array_map( 'rocket_add_url_protocol', $urls );
foreach ( $urls as $url ) {
$parsed_url = get_rocket_parse_url( $url );
if ( empty( $parsed_url['host'] ) ) {
continue;
}
$parsed_url['path'] = ( '/' === $parsed_url['path'] ) ? '' : $parsed_url['path'];
$this->hosts[] = "//{$parsed_url['host']}{$parsed_url['path']}";
}
if ( empty( $this->hosts ) ) {
$this->hosts = [];
}
return $this->hosts;
}
/**
* Sets radio buttons sub fields value from wp options.
*
* @since 3.10
*
* @param array $sub_fields Array of fields to display..
* @return array
*/
public function set_radio_buttons_sub_fields_value( $sub_fields ) {
foreach ( $sub_fields as $id => &$args ) {
$args['id'] = $id;
$args['value'] = $this->options->get( $id, $args['default'] );
$args = apply_filters( 'rocket_before_render_option_extra_field', $args );
}
return $sub_fields;
}
/**
* Checks if the global exclusion pattern is used in the given field
*
* @since 3.10.3
*
* @param array $field A field array value.
*
* @return array
*/
private function check_global_exclusion( $field ) {
if ( ! in_array( '/(.*)', $field, true ) ) {
return $field;
}
add_settings_error( 'general', 'reject_uri_global_exclusion', __( 'Sorry! Adding /(.*) in Advanced Rules > Never Cache URL(s) was not saved because it disables caching and optimizations for every page on your site.', 'rocket' ) );
return array_diff_key( $field, array_flip( array_keys( $field, '/(.*)', true ) ) );
}
/**
* Sanitizes the CDN cnames values
*
* @param array $cnames Array of user submitted values for the cnames.
*
* @return array
*/
private function sanitize_cdn_cnames( array $cnames ) {
$cnames = array_map(
function ( $cname ) {
$cname = trim( $cname );
if ( empty( $cname ) ) {
return false;
}
$cname_parts = get_rocket_parse_url( rocket_add_url_protocol( $cname ) );
if ( false === filter_var( $cname_parts['host'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME ) ) {
return false;
}
return $cname_parts['scheme'] . '://' . $cname_parts['host'] . $cname_parts['path'];
},
$cnames
);
return array_unique( array_filter( $cnames ) );
}
}
@@ -0,0 +1,234 @@
<?php
namespace WP_Rocket\Engine\Admin\Settings;
use Imagify_Partner;
use WP_Rocket\Event_Management\Subscriber_Interface;
/**
* WP Rocket settings page subscriber.
*
* @since 3.5.5 Moves into the new architecture.
* @since 3.3
*/
class Subscriber implements Subscriber_Interface {
/**
* Page instance
*
* @var Page
*/
private $page;
/**
* Creates an instance of the object.
*
* @param Page $page Page instance.
*/
public function __construct( Page $page ) {
$this->page = $page;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.3
*
* @return array
*/
public static function get_subscribed_events() {
return [
'admin_menu' => 'add_admin_page',
'admin_init' => 'configure',
'wp_ajax_rocket_refresh_customer_data' => 'refresh_customer_data',
'wp_ajax_rocket_toggle_option' => 'toggle_option',
'rocket_settings_menu_navigation' => [
[ 'add_menu_tools_page' ],
[ 'add_imagify_page', 9 ],
[ 'add_tutorials_page', 11 ],
],
'admin_enqueue_scripts' => [
[ 'enqueue_rocket_scripts' ],
[ 'enqueue_url' ],
],
'script_loader_tag' => [ 'async_wistia_script', 10, 2 ],
'rocket_after_settings_radio_options' => [ 'display_radio_options_sub_fields', 11 ],
];
}
/**
* Enqueue the URL for option exporting.
*
* @return void
*/
public function enqueue_url() {
wp_localize_script(
'wpr-admin-common',
'rocket_option_export',
[
'rest_url_option_export' => rest_url( 'wp-rocket/v1/options/export/' ),
]
);
}
/**
* Enqueues WP Rocket scripts on the settings page
*
* @since 3.6
*
* @param string $hook The current admin page.
*
* @return void
*/
public function enqueue_rocket_scripts( $hook ) {
$this->page->enqueue_rocket_scripts( $hook );
}
/**
* Adds the async attribute to the Wistia script
*
* @param string $tag The <script> tag for the enqueued script.
* @param string $handle The script's registered handle.
*
* @return string
*/
public function async_wistia_script( $tag, $handle ) {
return $this->page->async_wistia_script( $tag, $handle );
}
/**
* Adds plugin page to the Settings menu.
*
* @since 3.0
*/
public function add_admin_page() {
add_options_page(
$this->page->get_title(),
/**
* Filters the menu title to display in the Settings sub-menu
*
* @since 3.7.4
*
* @param string $menu_title The text to be used for the menu.
*/
apply_filters( 'rocket_menu_title', $this->page->get_title() ),
$this->page->get_capability(),
$this->page->get_slug(),
[ $this->page, 'render_page' ]
);
}
/**
* Registers the settings, page sections, fields sections and fields.
*
* @since 3.0
*/
public function configure() {
$this->page->configure();
}
/**
* Gets customer data to refresh it on the dashboard with AJAX.
*
* @since 3.0
*
* @return string
*/
public function refresh_customer_data() {
check_ajax_referer( 'rocket-ajax' );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_die();
}
delete_transient( 'wp_rocket_customer_data' );
return wp_send_json_success( $this->page->customer_data() );
}
/**
* Toggle sliding checkboxes option value.
*
* @since 3.0
*/
public function toggle_option() {
$this->page->toggle_option();
}
/**
* Add Tools section to navigation.
*
* @since 3.0
*
* @param array $navigation Array of menu items.
* @return array
*/
public function add_menu_tools_page( $navigation ) {
$navigation['tools'] = [
'id' => 'tools',
'title' => __( 'Tools', 'rocket' ),
'menu_description' => __( 'Import, Export, Rollback', 'rocket' ),
];
return $navigation;
}
/**
* Add Imagify section to navigation.
*
* @since 3.2
*
* @param array $navigation Array of menu items.
* @return array
*/
public function add_imagify_page( $navigation ) {
if (
rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' )
||
Imagify_Partner::has_imagify_api_key()
) {
return $navigation;
}
$navigation['imagify'] = [
'id' => 'imagify',
'title' => __( 'Image Optimization', 'rocket' ),
'menu_description' => __( 'Compress your images', 'rocket' ),
];
return $navigation;
}
/**
* Add Tutorials section to navigation.
*
* @since 3.4
*
* @param array $navigation Array of menu items.
* @return array
*/
public function add_tutorials_page( $navigation ) {
$navigation['tutorials'] = [
'id' => 'tutorials',
'title' => __( 'Tutorials', 'rocket' ),
'menu_description' => __( 'Getting started and how to videos', 'rocket' ),
];
return $navigation;
}
/**
* Displays the radio option sub fields
*
* @since 3.10
*
* @param array $option_data array of option_id and sub_fields of the option.
*
* @return void
*/
public function display_radio_options_sub_fields( $option_data ) {
if ( empty( $option_data['sub_fields'] ) ) {
return;
}
$this->page->display_radio_options_sub_fields( $option_data['sub_fields'] );
}
}