PluginProbe
Autoptimize / 3.1.7
Autoptimize v3.1.7
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
autoptimize / classes / autoptimizeMetabox.php

autoptimizeMetabox.php in Autoptimize 3.1.7, at classes/autoptimizeMetabox.php

352 lines 14.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles meta box to disable optimizations.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 class autoptimizeMetabox
11 {
12 public function __construct()
13 {
14 $this->run();
15 }
16
17 public function run()
18 {
19 add_action( 'add_meta_boxes', array( $this, 'ao_metabox_add_box' ) );
20 add_action( 'save_post', array( $this, 'ao_metabox_save' ) );
21 add_action( 'wp_ajax_ao_metabox_ccss_addjob', array( $this, 'ao_metabox_generateccss_callback' ) );
22 }
23
24 public function ao_metabox_add_box()
25 {
26 $screens = array(
27 'post',
28 'page',
29 // add extra types e.g. product or ... ?
30 );
31
32 $screens = apply_filters( 'autoptimize_filter_metabox_screens', $screens );
33
34 foreach ( $screens as $screen ) {
35 add_meta_box(
36 'ao_metabox',
37 __( 'Autoptimize this page', 'autoptimize' ),
38 array( $this, 'ao_metabox_content' ),
39 $screen,
40 'side'
41 );
42 }
43 }
44
45 /**
46 * Prints the box content.
47 *
48 * @param WP_Post $post The object for the current post/page.
49 */
50 function ao_metabox_content( $post )
51 {
52 // phpcs:disable Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace
53
54 wp_nonce_field( 'ao_metabox', 'ao_metabox_nonce' );
55
56 $ao_opt_value = $this->check_ao_opt_sanity( get_post_meta( $post->ID, 'ao_post_optimize', true ) );
57
58 $_ao_meta_sub_opacity = '';
59 if ( 'on' !== $ao_opt_value['ao_post_optimize'] ) {
60 $_ao_meta_sub_opacity = 'opacity:.33;';
61 }
62 ?>
63 <p >
64 <input type="checkbox" id="autoptimize_post_optimize" class="ao_meta_main" name="ao_post_optimize" <?php echo 'on' !== $ao_opt_value['ao_post_optimize'] ? '' : 'checked="checked" '; ?> />
65 <label for="autoptimize_post_optimize">
66 <?php _e( 'Optimize this page?', 'autoptimize' ); ?>
67 </label>
68 </p>
69 <?php
70 $_ao_meta_js_style = '';
71 if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_js', false ) ) {
72 $_ao_meta_js_style = 'display:none;';
73 }
74 echo '<p class="ao_meta_sub" style="' . $_ao_meta_sub_opacity . $_ao_meta_js_style . '">';
75 ?>
76 <input type="checkbox" id="autoptimize_post_optimize_js" name="ao_post_js_optimize" <?php echo 'on' !== $ao_opt_value['ao_post_js_optimize'] ? '' : 'checked="checked" '; ?> />
77 <label for="autoptimize_post_optimize_js">
78 <?php _e( 'Optimize JS?', 'autoptimize' ); ?>
79 </label>
80 </p>
81 <?php
82 $_ao_meta_css_style = '';
83 if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css', false ) ) {
84 $_ao_meta_css_style = 'display:none;';
85 }
86 echo '<p class="ao_meta_sub" style="' . $_ao_meta_sub_opacity . $_ao_meta_css_style . '">';
87 ?>
88 <input type="checkbox" id="autoptimize_post_optimize_css" name="ao_post_css_optimize" <?php echo 'on' !== $ao_opt_value['ao_post_css_optimize'] ? '' : 'checked="checked" '; ?> />
89 <label for="autoptimize_post_optimize_css">
90 <?php _e( 'Optimize CSS?', 'autoptimize' ); ?>
91 </label>
92 </p>
93 <?php
94 $_ao_meta_ccss_style = '';
95 if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer', false ) || 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css', false ) ) {
96 $_ao_meta_ccss_style = 'display:none;';
97 }
98 if ( 'on' !== $ao_opt_value['ao_post_css_optimize'] ) {
99 $_ao_meta_ccss_style .= 'opacity:.33;';
100 }
101 echo '<p class="ao_meta_sub ao_meta_sub_css" style="' . $_ao_meta_sub_opacity . $_ao_meta_ccss_style . '">';
102 ?>
103 <input type="checkbox" id="autoptimize_post_ccss" name="ao_post_ccss" <?php echo 'on' !== $ao_opt_value['ao_post_ccss'] ? '' : 'checked="checked" '; ?> />
104 <label for="autoptimize_post_ccss">
105 <?php _e( 'Inline critical CSS?', 'autoptimize' ); ?>
106 </label>
107 </p>
108 <?php
109 $_ao_meta_lazyload_style = '';
110 if ( false === autoptimizeImages::should_lazyload_wrapper( true ) ) {
111 $_ao_meta_lazyload_style = 'display:none;';
112 }
113 echo '<p class="ao_meta_sub" style="' . $_ao_meta_sub_opacity . $_ao_meta_lazyload_style . '">';
114 ?>
115 <input type="checkbox" id="autoptimize_post_lazyload" name="ao_post_lazyload" <?php echo 'on' !== $ao_opt_value['ao_post_lazyload'] ? '' : 'checked="checked" '; ?> />
116 <label for="autoptimize_post_lazyload">
117 <?php _e( 'Lazyload images?', 'autoptimize' ); ?>
118 </label>
119 </p>
120 <?php
121 $_ao_meta_preload_style = '';
122 if ( false === autoptimizeImages::should_lazyload_wrapper() && false === autoptimizeImages::imgopt_active() ) {
123 // img preload requires imgopt and/ or lazyload to be active.
124 $_ao_meta_preload_style = 'opacity:.33;';
125 }
126 ?>
127 <p class="ao_meta_sub ao_meta_preload" style="<?php echo $_ao_meta_sub_opacity . $_ao_meta_preload_style; ?>">
128 <label for="autoptimize_post_preload">
129 <?php _e( 'LCP Image to preload', 'autoptimize' ); ?>
130 </label>
131 <?php
132 if ( is_array( $ao_opt_value ) && array_key_exists( 'ao_post_preload', $ao_opt_value ) ) {
133 $_preload_img = esc_attr( $ao_opt_value['ao_post_preload'] );
134 } else {
135 $_preload_img = '';
136 }
137 ?>
138 <input type="text" id="autoptimize_post_preload" name="ao_post_preload" value="<?php echo $_preload_img; ?>">
139 </p>
140 <?php
141 echo apply_filters( 'autoptimize_filter_metabox_extra_ui', '');
142 ?>
143 <p>&nbsp;</p>
144 <p>
145 <?php
146 // Get path + check if button should be enabled or disabled.
147 $_generate_disabled = true;
148 $_slug = false;
149 $_type = 'is_single';
150
151 // harvest post ID from URL, get permalink from that and extract path from that.
152 if ( array_key_exists( 'post', $_GET ) ) {
153 $_slug = str_replace( AUTOPTIMIZE_WP_SITE_URL, '', get_permalink( $_GET['post'] ) );
154 }
155
156 // override the default 'is_single' if post.
157 global $post;
158 if ( 'page' === $post->post_type ) {
159 $_type = 'is_page';
160 }
161
162 // if CSS opt and inline & defer are on and if we have a slug, the button can be active.
163 if ( false !== $_slug && 'on' === autoptimizeOptionWrapper::get_option( 'autoptimize_css', false ) && 'on' === autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer', false ) && ! empty( apply_filters( 'autoptimize_filter_ccss_key', autoptimizeOptionWrapper::get_option( 'autoptimize_ccss_key', false ) ) ) && '2' === autoptimizeOptionWrapper::get_option( 'autoptimize_ccss_keyst', false ) ) {
164 $_generate_disabled = false;
165 }
166 ?>
167 <button class="button ao_meta_sub ao_meta_sub_css" id="generateccss" style="<?php echo $_ao_meta_sub_opacity . $_ao_meta_ccss_style; ?>" <?php if ( true === $_generate_disabled ) { echo 'disabled'; } ?>><?php _e( 'Generate Critical CSS', 'autoptimize' ); ?></button>
168 </p>
169 <script>
170 jQuery(document).ready(function() {
171 jQuery( "#autoptimize_post_optimize" ).change(function() {
172 if (this.checked) {
173 jQuery(".ao_meta_sub:visible").fadeTo("fast",1);
174 } else {
175 jQuery(".ao_meta_sub:visible").fadeTo("fast",.33);
176 }
177 });
178 jQuery( "#autoptimize_post_optimize_css" ).change(function() {
179 if (this.checked) {
180 jQuery(".ao_meta_sub_css:visible").fadeTo("fast",1);
181 } else {
182 jQuery(".ao_meta_sub_css:visible").fadeTo("fast",.33);
183 }
184 });
185 jQuery( "#autoptimize_post_ccss" ).change(function() {
186 if (this.checked) {
187 jQuery("#generateccss:visible").fadeTo("fast",1);
188 } else {
189 jQuery("#generateccss:visible").fadeTo("fast",.33);
190 }
191 });
192 <?php
193 if ( true === autoptimizeImages::should_lazyload_wrapper() && false === autoptimizeImages::imgopt_active() ) {
194 ?>
195 jQuery( "#autoptimize_post_lazyload" ).change(function() {
196 if (this.checked) {
197 jQuery(".ao_meta_preload:visible").fadeTo("fast",1);
198 } else {
199 jQuery(".ao_meta_preload:visible").fadeTo("fast",.33);
200 }
201 });
202 <?php
203 }
204 ?>
205 jQuery("#generateccss").click(function(e){
206 e.preventDefault();
207 // disable button to avoid it being clicked several times.
208 jQuery("#generateccss").prop('disabled', true);
209 var data = {
210 'action': 'ao_metabox_ccss_addjob',
211 'path' : '<?php echo $_slug; ?>',
212 'type' : '<?php echo $_type; ?>',
213 'ao_ccss_addjob_nonce': '<?php echo wp_create_nonce( 'ao_ccss_addjob_nonce' ); ?>',
214 };
215
216 jQuery.post(ajaxurl, data, function(response) {
217 response_array=JSON.parse(response);
218 if (response_array['code'] == 200) {
219 setCritCSSbutton("<?php _e( 'Added to CCSS job queue.', 'autoptimize' ); ?>", "green");
220 } else {
221 setCritCSSbutton("<?php _e( 'Could not add to CCSS job queue.', 'autoptimize' ); ?>", "orange");
222 }
223 }).fail(function() {
224 setCritCSSbutton("<?php _e( 'Sorry, something went wrong.', 'autoptimize' ); ?>", "orange");
225 });
226 });
227 });
228
229 function setCritCSSbutton( message, color) {
230 jQuery("#generateccss").html(message);
231 jQuery("#generateccss").prop("style","border-color:" + color + "!important; color:" + color + "!important");
232 }
233 </script>
234 <?php
235 }
236
237 /**
238 * When the post is saved, saves our custom data.
239 *
240 * @param int $post_id The ID of the post being saved.
241 */
242 public function ao_metabox_save( $post_id )
243 {
244 // If this is an autosave, our form has not been submitted, so we don't want to do anything.
245 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
246 return $post_id;
247 }
248
249 // check if from our own data.
250 if ( ! isset( $_POST['ao_metabox_nonce'] ) ) {
251 return $post_id;
252 }
253
254 // Check if our nonce is set and verify if valid.
255 $nonce = $_POST['ao_metabox_nonce'];
256 if ( ! wp_verify_nonce( $nonce, 'ao_metabox' ) ) {
257 return $post_id;
258 }
259
260 // Check the user's permissions.
261 if ( 'page' === $_POST['post_type'] ) {
262 if ( ! current_user_can( 'edit_page', $post_id ) ) {
263 return $post_id;
264 }
265 } else {
266 if ( ! current_user_can( 'edit_post', $post_id ) ) {
267 return $post_id;
268 }
269 }
270
271 // OK, we can have a look at the actual data now.
272 // Sanitize user input.
273 foreach ( apply_filters( 'autoptimize_filter_meta_valid_optims', array( 'ao_post_optimize', 'ao_post_js_optimize', 'ao_post_css_optimize', 'ao_post_ccss', 'ao_post_lazyload', 'ao_post_preload' ) ) as $opti_type ) {
274 if ( in_array( $opti_type, apply_filters( 'autoptimize_filter_meta_optim_nonbool', array( 'ao_post_preload' ) ) ) ) {
275 if ( isset( $_POST[ $opti_type ] ) ) {
276 $ao_meta_result[ $opti_type ] = $_POST[ $opti_type ];
277 } else {
278 $ao_meta_result[ $opti_type ] = false;
279 }
280 } else if ( ! isset( $_POST[ $opti_type ] ) ) {
281 $ao_meta_result[ $opti_type ] = '';
282 } else if ( 'on' === $_POST[ $opti_type ] ) {
283 $ao_meta_result[ $opti_type ] = 'on';
284 }
285 }
286
287 // Update the meta field in the database.
288 update_post_meta( $post_id, 'ao_post_optimize', $ao_meta_result );
289 }
290
291 public function ao_metabox_generateccss_callback()
292 {
293 check_ajax_referer( 'ao_ccss_addjob_nonce', 'ao_ccss_addjob_nonce' );
294
295 if ( current_user_can( 'manage_options' ) && array_key_exists( 'path', $_POST ) && ! empty( $_POST['path'] ) ) {
296 if ( array_key_exists( 'type', $_POST ) && 'is_page' === $_POST['type'] ) {
297 $type = 'is_page';
298 } else {
299 $type = 'is_single';
300 }
301
302 $path = wp_strip_all_tags( $_POST['path'] );
303 $criticalcss = autoptimize()->criticalcss();
304 $_result = $criticalcss->enqueue( '', $path, $type );
305
306 if ( $_result ) {
307 $response['code'] = '200';
308 $response['string'] = $path . ' added to job queue.';
309 } else {
310 $response['code'] = '404';
311 $response['string'] = 'could not add ' . $path . ' to job queue.';
312 }
313 } else {
314 $response['code'] = '500';
315 $response['string'] = 'nok';
316 }
317
318 // Dispatch respose.
319 echo json_encode( $response );
320
321 // Close ajax request.
322 wp_die();
323 }
324
325 public function get_metabox_default_values()
326 {
327 $ao_metabox_defaults = array(
328 'ao_post_optimize' => 'on',
329 'ao_post_js_optimize' => 'on',
330 'ao_post_css_optimize' => 'on',
331 'ao_post_ccss' => 'on',
332 'ao_post_lazyload' => 'on',
333 'ao_post_preload' => '',
334 );
335 return $ao_metabox_defaults;
336 }
337
338 public function check_ao_opt_sanity( $ao_opt_val ) {
339 if ( empty( $ao_opt_val ) ) {
340 $ao_opt_val = $this->get_metabox_default_values();
341 } else {
342 foreach ( array( 'ao_post_optimize', 'ao_post_js_optimize', 'ao_post_css_optimize', 'ao_post_ccss', 'ao_post_lazyload' ) as $key ) {
343 if ( ! array_key_exists( $key, $ao_opt_val ) ) {
344 $ao_opt_val[ $key ] = 'off';
345 }
346 }
347 }
348
349 return $ao_opt_val;
350 }
351 }
352