class-background-media-library-scanner.php
6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace Smush\Core\Media_Library;
use Smush\Core\Controller;
use Smush\Core\Helper;
use Smush\Core\Media\Media_Item_Query;
use Smush\Core\Stats\Global_Stats;
use WP_Smush;
class Background_Media_Library_Scanner extends Controller {
const OPTIMIZE_ON_COMPLETED_OPTION_KEY = 'wp_smush_run_optimize_on_scan_completed';
/**
* @var Media_Library_Scanner
*/
private $scanner;
/**
* @var Media_Library_Scan_Background_Process
*/
private $background_process;
private $logger;
/**
* @var bool
*/
private $optimize_on_scan_completed;
/**
* @var Global_Stats
*/
private $global_stats;
/**
* Static instance
*
* @var self
*/
private static $instance;
public static function get_instance() {
if ( empty( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
$this->scanner = new Media_Library_Scanner();
$this->logger = Helper::logger();
$this->global_stats = Global_Stats::get();
$identifier = $this->make_identifier();
$this->background_process = new Media_Library_Scan_Background_Process( $identifier, $this->scanner );
$this->background_process->set_logger( Helper::logger() );
$this->register_action( 'wp_ajax_wp_smush_start_background_scan', array( $this, 'start_background_scan' ) );
$this->register_action( 'wp_ajax_wp_smush_cancel_background_scan', array( $this, 'cancel_background_scan' ) );
$this->register_action( 'wp_ajax_wp_smush_get_background_scan_status', array( $this, 'send_status' ) );
$this->register_action( "{$identifier}_completed", array( $this, 'background_process_completed' ) );
$this->register_action( "{$identifier}_dead", array( $this, 'background_process_dead' ) );
add_filter( 'wp_smush_script_data', array( $this, 'localize_media_library_scan_script_data' ) );
}
public function start_background_scan() {
check_ajax_referer( 'wp_smush_media_library_scanner' );
if ( ! Helper::is_user_allowed() ) {
wp_send_json_error();
}
$in_processing = $this->background_process->get_status()->is_in_processing();
if ( $in_processing ) {
// Already in progress
wp_send_json_error( array( 'message' => __( 'Background scan is already in processing.', 'wp-smushit' ) ) );
}
if ( ! Helper::loopback_supported() ) {
$this->logger->error( 'Loopback check failed. Not starting a new background process.' );
wp_send_json_error( array(
'message' => sprintf(
esc_html__( 'Your site seems to have an issue with loopback requests. Please try again and if the problem persists find out more %s.', 'wp-smushit' ),
sprintf( '<a target="_blank" href="https://wpmudev.com/docs/wpmu-dev-plugins/smush/#background-processing">%s</a>', esc_html__( 'here', 'wp-smushit' ) )
),
) );
} else {
$this->logger->notice( 'Loopback check successful.' );
}
$this->set_optimize_on_scan_completed( ! empty( $_REQUEST['optimize_on_scan_completed'] ) );
if ( $this->background_process->get_status()->is_dead() ) {
$this->scanner->reduce_slice_size_option();
}
$this->scanner->before_scan_library();
$slice_size = $this->scanner->get_slice_size();
$query = new Media_Item_Query();
$slice_count = $query->get_slice_count( $slice_size );
$tasks = range( 1, $slice_count );
$this->background_process->start( $tasks );
wp_send_json_success( $this->get_scan_status() );
}
public function cancel_background_scan() {
check_ajax_referer( 'wp_smush_media_library_scanner' );
if ( ! Helper::is_user_allowed() ) {
wp_send_json_error();
}
$this->background_process->cancel();
$this->set_optimize_on_scan_completed( false );
wp_send_json_success( $this->get_scan_status() );
}
public function send_status() {
check_ajax_referer( 'wp_smush_media_library_scanner' );
if ( ! Helper::is_user_allowed() ) {
wp_send_json_error();
}
wp_send_json_success( $this->get_scan_status() );
}
public function background_process_completed() {
$this->scanner->after_scan_library();
if ( $this->enabled_optimize_on_scan_completed() ) {
$bg_optimization = WP_Smush::get_instance()->core()->mod->bg_optimization;
$bg_optimization->start_bulk_smush_direct();
}
}
public function background_process_dead() {
$this->global_stats->mark_as_outdated();
}
private function make_identifier() {
$identifier = 'wp_smush_background_scan_process';
if ( is_multisite() ) {
$post_fix = "_" . get_current_blog_id();
$identifier .= $post_fix;
}
return $identifier;
}
public function localize_media_library_scan_script_data( $script_data ) {
$scan_script_data = $this->background_process->get_status()->to_array();
$scan_script_data['nonce'] = wp_create_nonce( 'wp_smush_media_library_scanner' );
$script_data['media_library_scan'] = $scan_script_data;
return $script_data;
}
private function set_optimize_on_scan_completed( $status ) {
$this->optimize_on_scan_completed = $status;
if ( $this->optimize_on_scan_completed ) {
update_option( self::OPTIMIZE_ON_COMPLETED_OPTION_KEY, 1, false );
} else {
delete_option( self::OPTIMIZE_ON_COMPLETED_OPTION_KEY );
}
}
private function enabled_optimize_on_scan_completed() {
if ( null === $this->optimize_on_scan_completed ) {
$this->optimize_on_scan_completed = get_option( self::OPTIMIZE_ON_COMPLETED_OPTION_KEY );
}
return ! empty( $this->optimize_on_scan_completed );
}
private function get_scan_status() {
$is_completed = $this->background_process->get_status()->is_completed();
$is_cancelled = $this->background_process->get_status()->is_cancelled();
$status = $this->background_process->get_status()->to_array();
$status['optimize_on_scan_completed'] = $this->enabled_optimize_on_scan_completed();
// Add global stats on completed/cancelled.
if ( $is_completed || $is_cancelled ) {
$status['global_stats'] = WP_Smush::get_instance()->admin()->get_global_stats_with_bulk_smush_content_and_notice();
}
if ( $is_completed ) {
$bg_optimization = WP_Smush::get_instance()->core()->mod->bg_optimization;
$status['enabled_background_process'] = $bg_optimization->should_use_background();
}
return $status;
}
public function get_background_process() {
return $this->background_process;
}
}