PluginProbe
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress / 4.1.1
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress v4.1.1
4.1.1 4.1.0 4.0.1 4.0.0 3.3.1 3.3.0 trunk 1.0.0 2.0.0 2.1.0 3.0.0 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8
quickwebp / admin / class-cron-job.php

class-cron-job.php in QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress 4.1.1, at admin/class-cron-job.php

252 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The admin-specific functionality of the plugin.
4 *
5 * @link http://webdeclic.com
6 * @since 1.0.0
7 *
8 * @package Quickwebp
9 * @subpackage Quickwebp/admin
10 */
11 class Quickwebp_Cron_Job {
12
13 /**
14 * The ID of this plugin.
15 *
16 * @since 1.0.0
17 * @access private
18 * @var string $plugin_name The ID of this plugin.
19 */
20 private $plugin_name;
21
22 /**
23 * The version of this plugin.
24 *
25 * @since 1.0.0
26 * @access private
27 * @var string $version The current version of this plugin.
28 */
29 private $version;
30
31 /**
32 * Initialize the class and set its properties.
33 *
34 * @since 1.0.0
35 * @param string $plugin_name The name of this plugin.
36 * @param string $version The version of this plugin.
37 */
38 public function __construct( $plugin_name, $version ) {
39 $this->plugin_name = $plugin_name;
40 $this->version = $version;
41 }
42
43 /**
44 * Register cron jobs
45 */
46 public function crons_registrations( $schedules ) {
47
48 $schedules['bulk_optimization'] = array(
49 'interval' => MINUTE_IN_SECONDS,
50 'display' => __( 'Every minute', 'quickwebp' )
51 );
52
53 return $schedules;
54 }
55
56 /**
57 * excute bulk optimization
58 */
59 public function excute_bulk_optimization() {
60 $time_start = microtime(true);
61
62 $quickwebp_image_optimizer = new Quickwebp_Image_Optimizer( QUICKWEBP_TEXT_DOMAIN, QUICKWEBP_VERSION );
63 $settings = $quickwebp_image_optimizer->get_settings();
64
65 $mode_enabled = $settings['quickwebp_settings_conversion'];
66 if ( '0' === $mode_enabled ) {
67 $this->end_cron_job();
68 }
69
70 $media_ids = $quickwebp_image_optimizer->get_unoptimized_media_ids();
71 if ( empty( $media_ids ) ) {
72 $this->end_cron_job();
73 }
74
75 $status = get_option( 'quickwebp_bulk_optimize_status', 'finish' );
76 if ( $status != 'running' ) {
77 $this->end_cron_job();
78 }
79
80 $current = (int)get_option( 'quickwebp_bulk_optimize_current', 0 );
81
82 foreach ( $media_ids as $id ) {
83 if ( $time_start + 55 < microtime(true) ) {
84 exit;
85 }
86
87 $sizes = $quickwebp_image_optimizer->get_media_files( $id );
88 $new_sizes = array();
89
90 foreach ( $sizes as $key => $size ) {
91 $result = $quickwebp_image_optimizer->optimize_local_file( $size );
92
93 if ( $result ) {
94 $new_sizes[$key] = $result;
95 }
96 }
97
98 if ( ! empty( $new_sizes ) ) {
99 $quickwebp_image_optimizer->record_attachment_optimization_stats( $new_sizes );
100
101 $data = get_post_meta( $id, 'quickwebp_data', true );
102 if ( ! empty( $data ) ) {
103 $quickwebp_image_optimizer->remove_related_files( $data );
104 }
105
106 if ( '1' == $mode_enabled ) {
107 update_post_meta( $id, 'quickwebp_already_optimized', '1' );
108 } elseif ( '2' == $mode_enabled ) {
109 update_post_meta( $id, 'quickwebp_already_optimized', '2' );
110 }
111
112 update_post_meta( $id, 'quickwebp_data', $new_sizes );
113 delete_post_meta( $id, 'quickwebp_has_error' );
114 } else {
115 update_post_meta( $id, 'quickwebp_has_error', '1' );
116 delete_post_meta( $id, 'quickwebp_already_optimized' );
117 delete_post_meta( $id, 'quickwebp_data' );
118 }
119
120 $current++;
121 update_option( 'quickwebp_bulk_optimize_current', $current );
122 }
123
124 $this->end_cron_job();
125 }
126
127 /**
128 * Start bulk optimization
129 */
130 public function start_bulk_optimization() {
131
132 if ( ! current_user_can( 'manage_options' ) ) {
133 wp_send_json_error( __( 'You are not allowed to manage plugin settings.', 'quickwebp' ), 403 );
134 }
135
136 // verify the nonce.
137 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
138 if( !wp_verify_nonce( $nonce, 'image_optimize_nonce' ) ) {
139 wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
140 }
141
142 $quickwebp_image_optimizer = new Quickwebp_Image_Optimizer( QUICKWEBP_TEXT_DOMAIN, QUICKWEBP_VERSION );
143 $settings = $quickwebp_image_optimizer->get_settings();
144
145 $mode_enabled = $settings['quickwebp_settings_conversion'];
146 if ( '0' === $mode_enabled ) {
147 wp_send_json_error( __( 'Choose an image format and save the settings.', 'quickwebp' ) );
148 }
149
150 $media_ids = $quickwebp_image_optimizer->get_unoptimized_media_ids();
151 if ( empty( $media_ids ) ) {
152 wp_send_json_error( __( 'No images to optimize.', 'quickwebp' ) );
153 }
154
155 $status = get_option( 'quickwebp_bulk_optimize_status', 'finish' );
156 if ( $status != 'finish' ) {
157 wp_send_json_error( __( 'Bulk optimization is already running.', 'quickwebp' ) );
158 }
159
160 if ( ! wp_next_scheduled( 'quickwebp_bulk_optimization_hook' ) ) {
161 wp_schedule_event( time(), 'bulk_optimization', 'quickwebp_bulk_optimization_hook' );
162
163 $total = count( $media_ids );
164 $current = 0;
165
166 update_option( 'quickwebp_bulk_optimize_total', $total );
167 update_option( 'quickwebp_bulk_optimize_current', $current );
168 update_option( 'quickwebp_bulk_optimize_status', 'running' );
169
170 $data = array(
171 'progress' => $current . '/' . $total,
172 'percent' => $total ? round( abs( ( $current / $total ) ) * 100 ) . '%' : '0%'
173 );
174 wp_send_json_success( $data );
175 }
176
177 wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
178 }
179
180 /**
181 * Stop bulk optimization
182 */
183 public function stop_bulk_optimization() {
184
185 if ( ! current_user_can( 'manage_options' ) ) {
186 wp_send_json_error( __( 'You are not allowed to manage plugin settings.', 'quickwebp' ), 403 );
187 }
188
189 // verify the nonce.
190 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
191 if( !wp_verify_nonce( $nonce, 'image_optimize_nonce' ) ) {
192 wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
193 }
194
195 if ( $this->clear_bulk_optimization() ) {
196 wp_send_json_success( __( 'Bulk optimization stopped.', 'quickwebp' ) );
197 }
198
199 wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
200 }
201
202 /**
203 * Check bulk optimization progress
204 */
205 public function check_bulk_optimization_progress() {
206
207 if ( ! current_user_can( 'manage_options' ) ) {
208 wp_send_json_error( __( 'You are not allowed to manage plugin settings.', 'quickwebp' ), 403 );
209 }
210
211 // verify the nonce.
212 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
213 if( !wp_verify_nonce( $nonce, 'image_optimize_nonce' ) ) {
214 wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
215 }
216
217 $status = get_option( 'quickwebp_bulk_optimize_status', '' );
218 $total = (int)get_option( 'quickwebp_bulk_optimize_total', 0 );
219 $current = (int)get_option( 'quickwebp_bulk_optimize_current', 0 );
220 $is_running = $status == 'running' ? true : false;
221
222 $data = array(
223 'running' => $is_running,
224 'progress' => $current . '/' . $total,
225 'percent' => $total ? round( abs( ( $current / $total ) ) * 100 ) . '%' : '0%'
226 );
227 wp_send_json_success( $data );
228 }
229
230 /**
231 * Clear bulk optimization
232 */
233 private function clear_bulk_optimization() {
234 update_option( 'quickwebp_bulk_optimize_status', 'finish' );
235
236 if ( wp_next_scheduled( 'quickwebp_bulk_optimization_hook' ) ) {
237 wp_clear_scheduled_hook( 'quickwebp_bulk_optimization_hook' );
238 return true;
239 }
240
241 return false;
242 }
243
244 /**
245 * End cron job
246 */
247 private function end_cron_job() {
248 $this->clear_bulk_optimization();
249 exit;
250 }
251 }
252