PluginProbe
Autoptimize / 3.0.3
Autoptimize v3.0.3
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.0.3, at classes/autoptimizeMetabox.php

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