2015-10-15 22:52:37 +10:00
< ? php
2020-04-20 20:09:43 +02:00
/**
2019-02-19 23:52:21 +07:00
* Credits
*
2020-04-20 20:09:43 +02:00
* Plugin Name : WP Rocket
2019-02-19 23:52:21 +07:00
* Plugin URI : https :// wp - rocket . me
* Author : WP Media
* Author URI : http :// wp - media . me
* Licence : GPLv2 or later
*
* The code is used for the MainWP Rocket Extension
* Extension URL : https :// mainwp . com / extension / rocket /
2020-04-20 20:09:43 +02:00
*/
2019-02-19 23:52:21 +07:00
2020-05-05 20:13:38 +07:00
namespace MainWP\Child ;
2015-10-15 22:52:37 +10:00
class MainWP_Child_WP_Rocket {
2020-03-26 19:45:07 +00:00
public static $instance = null ;
2020-03-27 15:13:11 +00:00
public $is_plugin_installed = false ;
2015-10-15 22:52:37 +10:00
2020-05-06 20:22:11 +07:00
public static function instance () {
2020-03-26 14:05:04 +00:00
if ( null === self :: $instance ) {
self :: $instance = new MainWP_Child_WP_Rocket ();
2015-10-15 22:52:37 +10:00
}
2020-03-26 14:05:04 +00:00
return self :: $instance ;
2015-10-15 22:52:37 +10:00
}
public function __construct () {
2020-03-27 15:13:11 +00:00
if ( is_plugin_active ( 'wp-rocket/wp-rocket.php' ) ) {
$this -> is_plugin_installed = true ;
}
2015-10-15 22:52:37 +10:00
}
public function init () {
2020-03-27 15:13:11 +00:00
if ( ! $this -> is_plugin_installed ) {
return ;
}
2015-10-15 22:52:37 +10:00
2020-05-06 00:47:59 +07:00
add_filter ( 'mainwp-site-sync-others-data' , array ( $this , 'sync_others_data' ), 10 , 2 );
2018-12-19 17:01:08 +07:00
2020-04-20 20:09:43 +02:00
if ( 'hide' === get_option ( 'mainwp_wprocket_hide_plugin' ) ) {
2015-10-15 22:52:37 +10:00
add_filter ( 'all_plugins' , array ( $this , 'all_plugins' ) );
add_action ( 'admin_menu' , array ( $this , 'remove_menu' ) );
add_filter ( 'site_transient_update_plugins' , array ( & $this , 'remove_update_nag' ) );
2020-03-27 15:13:11 +00:00
add_filter ( 'mainwp_child_hide_update_notice' , array ( & $this , 'hide_update_notice' ) );
2015-10-15 22:52:37 +10:00
add_action ( 'wp_before_admin_bar_render' , array ( $this , 'wp_before_admin_bar_render' ), 99 );
add_action ( 'admin_init' , array ( $this , 'remove_notices' ) );
}
}
2018-12-19 17:01:08 +07:00
2020-04-20 20:09:43 +02:00
public function get_rocket_default_options () {
return array (
'cache_mobile' => 1 ,
'do_caching_mobile_files' => 0 ,
'cache_logged_user' => 0 ,
'cache_ssl' => 0 ,
'emoji' => 0 ,
'embeds' => 1 ,
'control_heartbeat' => 0 ,
'heartbeat_site_behavior' => 'reduce_periodicity' ,
'heartbeat_admin_behavior' => 'reduce_periodicity' ,
'heartbeat_editor_behavior' => 'reduce_periodicity' ,
'varnish_auto_purge' => 0 ,
'manual_preload' => 0 ,
'automatic_preload' => 0 ,
'sitemap_preload' => 0 ,
'sitemap_preload_url_crawl' => 500000 ,
'sitemaps' => array (),
'database_revisions' => 0 ,
'database_auto_drafts' => 0 ,
'database_trashed_posts' => 0 ,
'database_spam_comments' => 0 ,
'database_trashed_comments' => 0 ,
'database_expired_transients' => 0 ,
'database_all_transients' => 0 ,
'database_optimize_tables' => 0 ,
'schedule_automatic_cleanup' => 0 ,
'automatic_cleanup_frequency' => '' ,
'cache_reject_uri' => array (),
'cache_reject_cookies' => array (),
'cache_reject_ua' => array (),
'cache_query_strings' => array (),
'cache_purge_pages' => array (),
'purge_cron_interval' => 10 ,
'purge_cron_unit' => 'HOUR_IN_SECONDS' ,
'exclude_css' => array (),
'exclude_js' => array (),
'exclude_inline_js' => array (),
'async_css' => 0 ,
'defer_all_js' => 0 ,
'defer_all_js_safe' => 1 ,
'critical_css' => '' ,
'deferred_js_files' => array (),
'lazyload' => 0 ,
'lazyload_iframes' => 0 ,
'lazyload_youtube' => 0 ,
'minify_css' => 0 ,
'minify_concatenate_css' => 0 ,
'minify_css_legacy' => 0 ,
'minify_js' => 0 ,
'minify_js_in_footer' => array (),
'minify_concatenate_js' => 0 ,
'minify_js_combine_all' => 0 ,
'minify_google_fonts' => 0 ,
'minify_html' => 0 ,
'remove_query_strings' => 0 ,
'dns_prefetch' => 0 ,
'cdn' => 0 ,
'cdn_cnames' => array (),
'cdn_zone' => array (),
'cdn_reject_files' => array (),
'do_cloudflare' => 0 ,
'cloudflare_email' => '' ,
'cloudflare_api_key' => '' ,
'cloudflare_domain' => '' ,
'cloudflare_devmode' => 0 ,
'cloudflare_protocol_rewrite' => 0 ,
'cloudflare_auto_settings' => 0 ,
'cloudflare_old_settings' => 0 ,
'do_beta' => 0 ,
'analytics_enabled' => 1 ,
);
2019-08-26 23:29:16 +07:00
}
2020-05-06 00:47:59 +07:00
public function sync_others_data ( $information , $data = array () ) {
2020-03-27 15:13:11 +00:00
if ( isset ( $data [ 'syncWPRocketData' ] ) && ( 'yes' === $data [ 'syncWPRocketData' ] ) ) {
try {
$data = array ( 'rocket_boxes' => get_user_meta ( $GLOBALS [ 'current_user' ] -> ID , 'rocket_boxes' , true ) );
$information [ 'syncWPRocketData' ] = $data ;
} catch ( Exception $e ) {
2020-04-20 20:09:43 +02:00
// ok!
2020-03-27 15:13:11 +00:00
}
}
2018-06-26 19:52:53 +02:00
return $information ;
}
2015-10-15 22:52:37 +10:00
2020-04-20 20:09:43 +02:00
public function remove_notices () {
2015-10-15 22:52:37 +10:00
$remove_hooks [ 'admin_notices' ] = array (
'rocket_bad_deactivations' => 10 ,
'rocket_warning_plugin_modification' => 10 ,
'rocket_plugins_to_deactivate' => 10 ,
'rocket_warning_using_permalinks' => 10 ,
'rocket_warning_wp_config_permissions' => 10 ,
'rocket_warning_advanced_cache_permissions' => 10 ,
'rocket_warning_advanced_cache_not_ours' => 10 ,
'rocket_warning_htaccess_permissions' => 10 ,
'rocket_warning_config_dir_permissions' => 10 ,
'rocket_warning_cache_dir_permissions' => 10 ,
'rocket_warning_minify_cache_dir_permissions' => 10 ,
'rocket_thank_you_license' => 10 ,
'rocket_need_api_key' => 10 ,
);
foreach ( $remove_hooks as $hook_name => $hooks ) {
foreach ( $hooks as $method => $priority ) {
MainWP_Helper :: remove_filters_with_method_name ( $hook_name , $method , $priority );
}
}
}
public function wp_before_admin_bar_render () {
global $wp_admin_bar ;
$nodes = $wp_admin_bar -> get_nodes ();
if ( is_array ( $nodes ) ) {
foreach ( $nodes as $node ) {
2020-04-20 20:09:43 +02:00
$node -> id = 'wp-rocket' ;
if ( 'wp-rocket' === $node -> parent || $node -> id ) {
2015-10-15 22:52:37 +10:00
$wp_admin_bar -> remove_node ( $node -> id );
}
}
}
}
2020-04-20 20:09:43 +02:00
public function hide_update_notice ( $slugs ) {
2020-03-27 15:13:11 +00:00
$slugs [] = 'wp-rocket/wp-rocket.php' ;
return $slugs ;
}
2018-12-19 17:01:08 +07:00
2020-04-20 20:09:43 +02:00
public function remove_update_nag ( $value ) {
2015-10-15 22:52:37 +10:00
if ( isset ( $_POST [ 'mainwpsignature' ] ) ) {
return $value ;
}
2018-12-19 17:01:08 +07:00
2020-03-27 15:13:11 +00:00
if ( ! MainWP_Helper :: is_screen_with_update () ) {
return $value ;
}
2018-12-19 17:01:08 +07:00
2015-10-15 22:52:37 +10:00
if ( isset ( $value -> response [ 'wp-rocket/wp-rocket.php' ] ) ) {
unset ( $value -> response [ 'wp-rocket/wp-rocket.php' ] );
}
return $value ;
}
2020-05-06 20:22:11 +07:00
public function is_activated () {
2020-03-27 15:13:11 +00:00
if ( ! $this -> is_plugin_installed ) {
return false ;
}
2015-10-15 22:52:37 +10:00
return true ;
}
public function remove_menu () {
global $submenu ;
if ( isset ( $submenu [ 'options-general.php' ] ) ) {
foreach ( $submenu [ 'options-general.php' ] as $index => $item ) {
if ( 'wprocket' === $item [ 2 ] ) {
unset ( $submenu [ 'options-general.php' ][ $index ] );
break ;
}
}
}
$pos = stripos ( $_SERVER [ 'REQUEST_URI' ], 'options-general.php?page=wprocket' );
if ( false !== $pos ) {
2020-05-05 00:56:15 +07:00
wp_safe_redirect ( get_option ( 'siteurl' ) . '/wp-admin/index.php' );
2015-10-15 22:52:37 +10:00
exit ();
}
}
public function all_plugins ( $plugins ) {
foreach ( $plugins as $key => $value ) {
$plugin_slug = basename ( $key , '.php' );
if ( 'wp-rocket' === $plugin_slug ) {
unset ( $plugins [ $key ] );
}
}
return $plugins ;
}
public function action () {
2020-03-27 15:13:11 +00:00
if ( ! $this -> is_plugin_installed ) {
2019-08-26 23:29:16 +07:00
MainWP_Helper :: write ( array ( 'error' => __ ( 'Please install WP Rocket plugin on child website' , $this -> plugin_translate ) ) );
return ;
2015-10-15 22:52:37 +10:00
}
2019-08-26 23:29:16 +07:00
$information = array ();
2015-10-15 22:52:37 +10:00
if ( isset ( $_POST [ 'mwp_action' ] ) ) {
2020-03-27 15:13:11 +00:00
try {
switch ( $_POST [ 'mwp_action' ] ) {
case 'set_showhide' :
$information = $this -> set_showhide ();
break ;
case 'purge_cloudflare' :
$information = $this -> purge_cloudflare ();
break ;
case 'purge_all' :
$information = $this -> purge_cache_all ();
break ;
case 'preload_cache' :
$information = $this -> preload_cache ();
break ;
case 'generate_critical_css' :
$information = $this -> generate_critical_css ();
break ;
case 'save_settings' :
$information = $this -> save_settings ();
break ;
case 'load_existing_settings' :
$information = $this -> load_existing_settings ();
break ;
case 'optimize_database' :
$information = $this -> optimize_database ();
break ;
case 'get_optimize_info' :
$information = $this -> get_optimize_info ();
break ;
case 'purge_opcache' :
$information = $this -> do_admin_post_rocket_purge_opcache ();
break ;
}
} catch ( Exception $e ) {
$information = array ( 'error' => $e -> getMessage () );
}
2015-10-15 22:52:37 +10:00
}
MainWP_Helper :: write ( $information );
}
2020-04-20 20:09:43 +02:00
public function set_showhide () {
$hide = isset ( $_POST [ 'showhide' ] ) && ( 'hide' === $_POST [ 'showhide' ] ) ? 'hide' : '' ;
2015-10-15 22:52:37 +10:00
MainWP_Helper :: update_option ( 'mainwp_wprocket_hide_plugin' , $hide );
$information [ 'result' ] = 'SUCCESS' ;
return $information ;
}
2020-04-20 20:09:43 +02:00
public function do_admin_post_rocket_purge_opcache () {
2020-03-27 15:13:11 +00:00
if ( function_exists ( 'opcache_reset' ) ) {
2020-04-23 19:53:22 +02:00
opcache_reset ();
2020-03-27 15:13:11 +00:00
} else {
return array ( 'error' => 'The host do not support the function reset opcache.' );
}
return array ( 'result' => 'SUCCESS' );
}
2017-07-11 14:10:22 +02:00
2020-04-20 20:09:43 +02:00
public function purge_cloudflare () {
2015-10-15 22:52:37 +10:00
if ( function_exists ( 'rocket_purge_cloudflare' ) ) {
rocket_purge_cloudflare ();
return array ( 'result' => 'SUCCESS' );
} else {
return array ( 'error' => 'function_not_exist' );
}
}
2020-04-20 20:09:43 +02:00
public function purge_cache_all () {
2015-10-15 22:52:37 +10:00
if ( function_exists ( 'rocket_clean_domain' ) || function_exists ( 'rocket_clean_minify' ) || function_exists ( 'create_rocket_uniqid' ) ) {
2018-06-26 19:52:53 +02:00
set_transient ( 'rocket_clear_cache' , 'all' , HOUR_IN_SECONDS );
2015-10-15 22:52:37 +10:00
rocket_clean_domain ();
rocket_clean_minify ();
2020-03-27 14:11:21 +00:00
if ( function_exists ( 'rocket_clean_cache_busting' ) ) {
2018-06-26 19:52:53 +02:00
rocket_clean_cache_busting ();
}
2020-04-22 20:05:24 +02:00
if ( ! function_exists ( 'rocket_dismiss_boxes' ) && defined ( 'WP_ROCKET_ADMIN_PATH' ) ) {
2018-12-19 17:01:08 +07:00
require_once WP_ROCKET_ADMIN_PATH . 'admin.php' ;
}
2020-03-26 14:11:33 +00:00
include_once ABSPATH . '/wp-admin/includes/template.php' ;
2015-10-15 22:52:37 +10:00
$options = get_option ( WP_ROCKET_SLUG );
$options [ 'minify_css_key' ] = create_rocket_uniqid ();
$options [ 'minify_js_key' ] = create_rocket_uniqid ();
remove_all_filters ( 'update_option_' . WP_ROCKET_SLUG );
update_option ( WP_ROCKET_SLUG , $options );
2018-06-26 19:52:53 +02:00
rocket_dismiss_box ( 'rocket_warning_plugin_modification' );
2015-10-15 22:52:37 +10:00
return array ( 'result' => 'SUCCESS' );
} else {
return array ( 'error' => 'function_not_exist' );
}
}
2020-04-20 20:09:43 +02:00
public function preload_cache () {
2020-03-27 15:13:11 +00:00
MainWP_Helper :: check_functions ( array ( 'run_rocket_sitemap_preload' , 'run_rocket_bot' ) );
2020-04-22 20:05:24 +02:00
MainWP_Helper :: check_classes_exists ( 'WP_Rocket\Preload\Full_Process' );
2015-10-15 22:52:37 +10:00
2020-03-27 15:13:11 +00:00
$preload_process = new WP_Rocket\Preload\Full_Process ();
2020-04-22 20:05:24 +02:00
MainWP_Helper :: check_methods ( $preload_process , array ( 'is_process_running' ) );
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
if ( $preload_process -> is_process_running () ) {
return array ( 'result' => 'RUNNING' );
}
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
delete_transient ( 'rocket_preload_errors' );
run_rocket_bot ( 'cache-preload' , '' );
run_rocket_sitemap_preload ();
return array ( 'result' => 'SUCCESS' );
2019-08-26 23:29:16 +07:00
}
2020-04-20 20:09:43 +02:00
public function generate_critical_css () {
MainWP_Helper :: check_classes_exists (
array (
'WP_Rocket\Subscriber\Optimization\Critical_CSS_Subscriber' ,
'WP_Rocket\Optimization\CSS\Critical_CSS' ,
'WP_Rocket\Optimization\CSS\Critical_CSS_Generation' ,
'WP_Rocket\Admin\Options' ,
'WP_Rocket\Admin\Options_Data' ,
)
);
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
$critical_css = new WP_Rocket\Optimization\CSS\Critical_CSS ( new WP_Rocket\Optimization\CSS\Critical_CSS_Generation () );
$options_api = new WP_Rocket\Admin\Options ( 'wp_rocket_' );
$options = new WP_Rocket\Admin\Options_Data ( $options_api -> get ( 'settings' , array () ) );
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
$sitemap_preload = new WP_Rocket\Subscriber\Optimization\Critical_CSS_Subscriber ( $critical_css , $options );
2019-08-26 23:29:16 +07:00
2020-04-20 20:09:43 +02:00
MainWP_Helper :: check_properties ( $sitemap_preload , 'critical_css' );
MainWP_Helper :: check_methods ( $sitemap_preload -> critical_css , 'process_handler' );
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
$sitemap_preload -> critical_css -> process_handler ();
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
return array ( 'result' => 'SUCCESS' );
2015-10-15 22:52:37 +10:00
}
2020-04-20 20:09:43 +02:00
public function save_settings () {
$options = maybe_unserialize ( base64_decode ( $_POST [ 'settings' ] ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for benign reasons.
2015-10-15 22:52:37 +10:00
if ( ! is_array ( $options ) || empty ( $options ) ) {
return array ( 'error' => 'INVALID_OPTIONS' );
}
$old_values = get_option ( WP_ROCKET_SLUG );
$defaults_fields = $this -> get_rocket_default_options ();
foreach ( $old_values as $field => $value ) {
2020-04-20 20:09:43 +02:00
if ( ! isset ( $defaults_fields [ $field ] ) ) {
2015-10-15 22:52:37 +10:00
$options [ $field ] = $value ;
}
}
2019-08-26 23:29:16 +07:00
2015-10-15 22:52:37 +10:00
update_option ( WP_ROCKET_SLUG , $options );
2020-04-20 20:09:43 +02:00
if ( isset ( $_POST [ 'do_database_optimization' ] ) && ! empty ( $_POST [ 'do_database_optimization' ] ) ) {
2019-08-26 23:29:16 +07:00
$this -> optimize_database ();
}
2015-10-15 22:52:37 +10:00
return array ( 'result' => 'SUCCESS' );
}
2020-04-20 20:09:43 +02:00
public function optimize_database () {
MainWP_Helper :: check_classes_exists (
array (
'WP_Rocket\Admin\Database\Optimization' ,
'WP_Rocket\Admin\Database\Optimization_Process' ,
'WP_Rocket\Admin\Options' ,
'WP_Rocket\Admin\Options_Data' ,
)
);
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
$process = new WP_Rocket\Admin\Database\Optimization_Process ();
$optimization = new WP_Rocket\Admin\Database\Optimization ( $process );
2020-04-20 20:09:43 +02:00
2020-03-27 15:13:11 +00:00
MainWP_Helper :: check_methods ( $optimization , array ( 'process_handler' , 'get_options' ) );
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
$options_api = new WP_Rocket\Admin\Options ( 'wp_rocket_' );
$options = new WP_Rocket\Admin\Options_Data ( $options_api -> get ( 'settings' , array () ) );
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
$items = array_filter ( array_keys ( $optimization -> get_options () ), array ( $options , 'get' ) );
2019-08-26 23:29:16 +07:00
2020-03-26 17:03:00 +00:00
if ( ! empty ( $items ) ) {
2020-03-27 15:13:11 +00:00
$optimization -> process_handler ( $items );
2016-06-08 21:47:49 +02:00
}
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
$return [ 'result' ] = 'SUCCESS' ;
2016-06-08 21:47:49 +02:00
return $return ;
}
2020-04-20 20:09:43 +02:00
public function get_optimize_info () {
MainWP_Helper :: check_classes_exists (
array (
'WP_Rocket\Admin\Database\Optimization' ,
'WP_Rocket\Admin\Database\Optimization_Process' ,
)
);
2019-08-26 23:29:16 +07:00
2020-03-27 15:13:11 +00:00
$process = new WP_Rocket\Admin\Database\Optimization_Process ();
$optimization = new WP_Rocket\Admin\Database\Optimization ( $process );
2020-04-20 20:09:43 +02:00
MainWP_Helper :: check_methods ( $optimization , 'count_cleanup_items' );
2020-03-27 15:13:11 +00:00
$information [ 'optimize_info' ] = array (
'total_revisions' => $optimization -> count_cleanup_items ( 'database_revisions' ),
'total_auto_draft' => $optimization -> count_cleanup_items ( 'database_auto_drafts' ),
'total_trashed_posts' => $optimization -> count_cleanup_items ( 'database_trashed_posts' ),
'total_spam_comments' => $optimization -> count_cleanup_items ( 'database_spam_comments' ),
'total_trashed_comments' => $optimization -> count_cleanup_items ( 'database_trashed_comments' ),
'total_expired_transients' => $optimization -> count_cleanup_items ( 'database_expired_transients' ),
'total_all_transients' => $optimization -> count_cleanup_items ( 'database_all_transients' ),
'total_optimize_tables' => $optimization -> count_cleanup_items ( 'database_optimize_tables' ),
);
$information [ 'result' ] = 'SUCCESS' ;
2016-06-08 21:47:49 +02:00
return $information ;
}
2020-04-20 20:09:43 +02:00
public function load_existing_settings () {
2015-12-05 17:38:52 +01:00
$options = get_option ( WP_ROCKET_SLUG );
2020-03-26 15:29:54 +00:00
return array (
'result' => 'SUCCESS' ,
'options' => $options ,
);
2015-12-05 17:38:52 +01:00
}
2015-10-15 22:52:37 +10:00
}