Initial commit xmarticaims
This commit is contained in:
@@ -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,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user