PluginProbe
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress / 4.0.1
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress v4.0.1
4.1.2 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.0.1, at admin/class-cron-job.php

251 lines 7.0 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
100 $data = get_post_meta( $id, 'quickwebp_data', true );
101 if ( ! empty( $data ) ) {
102 $quickwebp_image_optimizer->remove_related_files( $data );
103 }
104
105 if ( '1' == $mode_enabled ) {
106 update_post_meta( $id, 'quickwebp_already_optimized', '1' );
107 } elseif ( '2' == $mode_enabled ) {
108 update_post_meta( $id, 'quickwebp_already_optimized', '2' );
109 }
110
111 update_post_meta( $id, 'quickwebp_data', $new_sizes );
112 delete_post_meta( $id, 'quickwebp_has_error' );
113 } else {
114 update_post_meta( $id, 'quickwebp_has_error', '1' );
115 delete_post_meta( $id, 'quickwebp_already_optimized' );
116 delete_post_meta( $id, 'quickwebp_data' );
117 }
118
119 $current++;
120 update_option( 'quickwebp_bulk_optimize_current', $current );
121 }
122
123 $this->end_cron_job();
124 }
125
126 /**
127 * Start bulk optimization
128 */
129 public function start_bulk_optimization() {
130
131 if ( ! current_user_can( 'manage_options' ) ) {
132 wp_send_json_error( __( 'You are not allowed to manage plugin settings.', 'quickwebp' ), 403 );
133 }
134
135 // verify the nonce.
136 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
137 if( !wp_verify_nonce( $nonce, 'image_optimize_nonce' ) ) {
138 wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
139 }
140
141 $quickwebp_image_optimizer = new Quickwebp_Image_Optimizer( QUICKWEBP_TEXT_DOMAIN, QUICKWEBP_VERSION );
142 $settings = $quickwebp_image_optimizer->get_settings();
143
144 $mode_enabled = $settings['quickwebp_settings_conversion'];
145 if ( '0' === $mode_enabled ) {
146 wp_send_json_error( __( 'Choose an image format and save the settings.', 'quickwebp' ) );
147 }
148
149 $media_ids = $quickwebp_image_optimizer->get_unoptimized_media_ids();
150 if ( empty( $media_ids ) ) {
151 wp_send_json_error( __( 'No images to optimize.', 'quickwebp' ) );
152 }
153
154 $status = get_option( 'quickwebp_bulk_optimize_status', 'finish' );
155 if ( $status != 'finish' ) {
156 wp_send_json_error( __( 'Bulk optimization is already running.', 'quickwebp' ) );
157 }
158
159 if ( ! wp_next_scheduled( 'quickwebp_bulk_optimization_hook' ) ) {
160 wp_schedule_event( time(), 'bulk_optimization', 'quickwebp_bulk_optimization_hook' );
161
162 $total = count( $media_ids );
163 $current = 0;
164
165 update_option( 'quickwebp_bulk_optimize_total', $total );
166 update_option( 'quickwebp_bulk_optimize_current', $current );
167 update_option( 'quickwebp_bulk_optimize_status', 'running' );
168
169 $data = array(
170 'progress' => $current . '/' . $total,
171 'percent' => $total ? round( abs( ( $current / $total ) ) * 100 ) . '%' : '0%'
172 );
173 wp_send_json_success( $data );
174 }
175
176 wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
177 }
178
179 /**
180 * Stop bulk optimization
181 */
182 public function stop_bulk_optimization() {
183
184 if ( ! current_user_can( 'manage_options' ) ) {
185 wp_send_json_error( __( 'You are not allowed to manage plugin settings.', 'quickwebp' ), 403 );
186 }
187
188 // verify the nonce.
189 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
190 if( !wp_verify_nonce( $nonce, 'image_optimize_nonce' ) ) {
191 wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
192 }
193
194 if ( $this->clear_bulk_optimization() ) {
195 wp_send_json_success( __( 'Bulk optimization stopped.', 'quickwebp' ) );
196 }
197
198 wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
199 }
200
201 /**
202 * Check bulk optimization progress
203 */
204 public function check_bulk_optimization_progress() {
205
206 if ( ! current_user_can( 'manage_options' ) ) {
207 wp_send_json_error( __( 'You are not allowed to manage plugin settings.', 'quickwebp' ), 403 );
208 }
209
210 // verify the nonce.
211 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
212 if( !wp_verify_nonce( $nonce, 'image_optimize_nonce' ) ) {
213 wp_send_json_error( __( 'Refresh the page and try again.', 'quickwebp' ) );
214 }
215
216 $status = get_option( 'quickwebp_bulk_optimize_status', '' );
217 $total = (int)get_option( 'quickwebp_bulk_optimize_total', 0 );
218 $current = (int)get_option( 'quickwebp_bulk_optimize_current', 0 );
219 $is_running = $status == 'running' ? true : false;
220
221 $data = array(
222 'running' => $is_running,
223 'progress' => $current . '/' . $total,
224 'percent' => $total ? round( abs( ( $current / $total ) ) * 100 ) . '%' : '0%'
225 );
226 wp_send_json_success( $data );
227 }
228
229 /**
230 * Clear bulk optimization
231 */
232 private function clear_bulk_optimization() {
233 update_option( 'quickwebp_bulk_optimize_status', 'finish' );
234
235 if ( wp_next_scheduled( 'quickwebp_bulk_optimization_hook' ) ) {
236 wp_clear_scheduled_hook( 'quickwebp_bulk_optimization_hook' );
237 return true;
238 }
239
240 return false;
241 }
242
243 /**
244 * End cron job
245 */
246 private function end_cron_job() {
247 $this->clear_bulk_optimization();
248 exit;
249 }
250 }
251