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 / critcss-inc / admin_settings_rules.php

admin_settings_rules.php in Autoptimize 3.1.7, at classes/critcss-inc/admin_settings_rules.php

239 lines 15.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Render the rules panel.
4 */
5
6 /**
7 * Main function to render the rules panel.
8 */
9 function ao_ccss_render_rules() {
10 // Attach required arrays.
11 $criticalcss = autoptimize()->criticalcss();
12 $ao_ccss_rules = sanitize_rules( $criticalcss->get_option( 'rules' ) );
13 $ao_ccss_types = $criticalcss->get_types();
14
15 if ( empty( $ao_ccss_types ) || ! is_array( $ao_ccss_types ) ) {
16 $ao_ccss_types = array( 'No conditionals, check CSS optimization settings.' );
17 }
18 ?>
19 <ul id="rules-panel">
20 <li class="itemDetail">
21 <h2 class="itemTitle"><?php _e( 'Rules', 'autoptimize' ); ?></h2>
22
23 <!-- BEGIN Rule dialogs -->
24 <!-- Unsaved dialog -->
25 <div id="unSavedWarning" class="hidden updated settings-error notice notice-warning is-dismissible">
26 <p><?php _e( "<strong>Rules or Queue changed!</strong> Don't forget to save your changes!", 'autoptimize' ); ?></p>
27 </div>
28
29 <!-- Create/edit rule dialog -->
30 <div id="addEditCritCss" class="hidden">
31 <table class="form-table rules">
32 <tr id="critcss_addedit_type_wrapper">
33 <th scope="row">
34 <?php _e( 'Rule Type', 'autoptimize' ); ?>
35 </th>
36 <td>
37 <select id="critcss_addedit_type" style="width:100%;">
38 <option value="paths"><?php _e( 'Path', 'autoptimize' ); ?></option>
39 <option value="types"><?php _e( 'Conditional Tag', 'autoptimize' ); ?></option>
40 </select>
41 </td>
42 </tr>
43 <tr id="critcss_addedit_path_wrapper">
44 <th scope="row">
45 <?php _e( 'String in Path', 'autoptimize' ); ?>
46 </th>
47 <td>
48 <input type="text" id="critcss_addedit_path" placeholder="<?php _e( "Enter a part of the URL that identifies the page(s) you're targetting.", 'autoptimize' ); ?>" style="width:100%;" value="">
49 </td>
50 </tr>
51 <tr id="critcss_addedit_pagetype_wrapper">
52 <th scope="row">
53 <?php _e( 'Conditional Tag, Custom Post Type or Page Template', 'autoptimize' ); ?>
54 </th>
55 <td>
56 <select id="critcss_addedit_pagetype" style="width:100%;">
57 <option value="" disabled selected><?php _e( 'Select from the list below...', 'autoptimize' ); ?></option>
58 <optgroup label="<?php _e( 'Standard Conditional Tags', 'autoptimize' ); ?>">
59 <?php
60 // Render grouped simple conditional tags.
61 foreach ( $ao_ccss_types as $ctag ) {
62 $optgrp = substr( $ctag, 0, 3 );
63 if ( substr( $ctag, 0, 3 ) === 'is_' ) {
64 echo '<option value="' . $ctag . '">' . $ctag . '</option>';
65 }
66 $prevgrp = substr( $ctag, 0, 3 );
67 }
68
69 // Render grouped custom post types, templates and specific conditional tags.
70 foreach ( $ao_ccss_types as $type ) {
71 $optgrp = substr( $type, 0, 3 );
72
73 // Option groups labels.
74 if ( $optgrp !== $prevgrp && 'is_' !== $optgrp ) {
75 ?>
76 </optgroup>
77 <?php
78 if ( substr( $type, 0, 12 ) === 'custom_post_' ) {
79 ?>
80 <optgroup label="<?php _e( 'Custom Post Types', 'autoptimize' ); ?>">
81 <?php
82 } elseif ( substr( $type, 0, 9 ) === 'template_' ) {
83 ?>
84 <optgroup label="<?php _e( 'Page Templates', 'autoptimize' ); ?>">
85 <?php
86 } elseif ( substr( $type, 0, 4 ) === 'bbp_' ) {
87 ?>
88 <optgroup label="<?php _e( 'BBPress Conditional Tags', 'autoptimize' ); ?>">
89 <?php
90 } elseif ( substr( $type, 0, 3 ) === 'bp_' ) {
91 ?>
92 <optgroup label="<?php _e( 'BuddyPress Conditional Tags', 'autoptimize' ); ?>">
93 <?php
94 } elseif ( substr( $type, 0, 4 ) === 'edd_' ) {
95 ?>
96 <optgroup label="<?php _e( 'Easy Digital Downloads Conditional Tags', 'autoptimize' ); ?>">
97 <?php
98 } elseif ( substr( $type, 0, 4 ) === 'woo_' ) {
99 ?>
100 <optgroup label="<?php _e( 'WooCommerce Conditional Tags', 'autoptimize' ); ?>">
101 <?php
102 }
103 }
104
105 // Options.
106 if ( 'is_' !== $optgrp ) {
107 // Remove prefix from custom post types, templates and some specific conditional tags.
108 if ( substr( $type, 0, 12 ) === 'custom_post_' ) {
109 $_type = str_replace( 'custom_post_', '', $type );
110 } elseif ( substr( $type, 0, 9 ) === 'template_' ) {
111 $_type = str_replace( 'template_', '', $type );
112 } elseif ( 'bbp_is_bbpress' === $type ) {
113 $_type = str_replace( 'bbp_', '', $type );
114 } elseif ( 'bp_is_buddypress' === $type ) {
115 $_type = str_replace( 'bp_', '', $type );
116 } elseif ( substr( $type, 0, 4 ) === 'woo_' ) {
117 $_type = str_replace( 'woo_', '', $type );
118 } elseif ( substr( $type, 0, 4 ) === 'edd_' ) {
119 $_type = str_replace( 'edd_', '', $type );
120 } else {
121 $_type = $type;
122 }
123
124 echo '<option value="' . $type . '">' . $_type . '</option>';
125 $prevgrp = $optgrp;
126 }
127 }
128 ?>
129 </optgroup>
130 </select>
131 </td>
132 </tr>
133 <tr>
134 <th scope="row">
135 <?php _e( 'Custom Critical CSS', 'autoptimize' ); ?>
136 </th>
137 <td>
138 <textarea id="critcss_addedit_css" rows="13" cols="10" style="width:100%;" placeholder="<?php _e( 'Paste your specific critical CSS here and hit submit to save.', 'autoptimize' ); ?>"></textarea>
139 <input type="hidden" id="critcss_addedit_file">
140 <input type="hidden" id="critcss_addedit_id">
141 </td>
142 </tr>
143 </table>
144 </div>
145
146 <!-- Remove dialog -->
147 <div id="confirm-rm" title="<?php _e( 'Delete Rule', 'autoptimize' ); ?>" class="hidden">
148 <p><?php _e( 'This Critical CSS rule will be deleted immediately and cannot be recovered.<br /><br /><strong>Are you sure?</strong>', 'autoptimize' ); ?></p>
149 </div>
150
151 <!-- Remove All dialog -->
152 <div id="confirm-rm-all" title="<?php _e( 'Delete all Rules and Jobs', 'autoptimize' ); ?>" class="hidden">
153 <p><?php _e( 'All Critical CSS rules will be deleted immediately and cannot be recovered.<br /><br /><strong>Are you sure?</strong>', 'autoptimize' ); ?></p>
154 </div>
155
156 <!-- Add/edit default critical CSS dialog -->
157 <div id="default_critcss_wrapper" class="hidden">
158 <textarea id="dummyDefault" rows="19" cols="10" style="width:100%;" placeholder="<?php _e( 'Paste your minified default critical CSS here and hit submit to save. This is the critical CSS to be used for every page not matching any rule.', 'autoptimize' ); ?>"></textarea>
159 </div>
160
161 <!-- Add/edit additional critical CSS dialog -->
162 <div id="additional_critcss_wrapper" class="hidden">
163 <textarea id="dummyAdditional" rows="19" cols="10" style="width:100%;" placeholder="<?php _e( 'Paste your minified additional critical CSS here and hit submit to save. This is the CSS to be added AT THE END of every critical CSS provided by a matching rule, or the default one.', 'autoptimize' ); ?>"></textarea>
164 </div>
165
166 <!-- BEGIN Rules UI -->
167 <div class="howto">
168 <div class="title-wrap">
169 <h4 class="title"><?php _e( 'How To Use Autoptimize CriticalCSS Rules', 'autoptimize' ); ?></h4>
170 <p class="subtitle"><?php _e( 'Click the side arrow to toggle instructions', 'autoptimize' ); ?></p>
171 </div>
172 <button type="button" class="toggle-btn">
173 <span class="toggle-indicator dashicons dashicons-arrow-up dashicons-arrow-down"></span>
174 </button>
175 <div class="howto-wrap hidden">
176 <p><?php _e( "TL;DR:<br />Critical CSS files from <span class='badge auto'>AUTO</span> <strong>rules are updated automatically</strong> while from <span class='badge manual'>MANUAL</span> <strong>rules are not.</strong>", 'autoptimize' ); ?></p>
177 <ol>
178 <li><?php _e( 'When a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> API key is in place, Autoptimize starts to operate <strong>automatically</strong>.', 'autoptimize' ); ?></li>
179 <li><?php _e( 'Upon a request to any of the frontend pages made by a <strong>not logged in user</strong>, it will <strong>asynchronously</strong> fetch and update the critical CSS from <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> for conditional tags you have on your site (e.g. is_page, is_single, is_archive etc.)', 'autoptimize' ); ?></li>
180 <li><?php _e( 'These requests also creates an <span class="badge auto">AUTO</span> rule for you. The critical CSS files from <span class="badge auto">AUTO</span> <strong>rules are updated automatically</strong> when a CSS file in your theme or frontend plugins changes.', 'autoptimize' ); ?></li>
181 <li><?php _e( 'If you want to make any fine tunning in the critical CSS file of an <span class="badge auto">AUTO</span> rule, click on "Edit" button of that rule, change what you need, submit and save it. The rule you\'ve just edited becomes a <span class="badge manual">MANUAL</span> rule then.', 'autoptimize' ); ?></li>
182 <li><?php _e( 'You can create <span class="badge manual">MANUAL</span> rules for specific page paths (URL). Longer, more specific paths have higher priority over shorter ones, which in turn have higher priority over <span class="badge auto">AUTO</span> rules. Also, critical CSS files from <span class="badge manual">MANUAL</span> <strong>rules are NEVER updated automatically.</strong>', 'autoptimize' ); ?></li>
183 <li><?php _e( 'At any time you can delete an <span class="badge auto">AUTO</span> or <span class="badge manual">MANUAL</span> rule by cliking on "Remove" button of the desired rule and saving your changes.', 'autoptimize' ); ?></li>
184 </ol>
185 </div>
186 </div>
187 <textarea id="autoptimize_css_defer_inline" name="autoptimize_css_defer_inline" rows="19" cols="10" style="width:100%;"><?php echo autoptimizeStyles::sanitize_css( get_option( 'autoptimize_css_defer_inline', '' ) ); ?></textarea>
188 <textarea id="autoptimize_ccss_additional" name="autoptimize_ccss_additional" rows="19" cols="10" style="width:100%;"><?php echo autoptimizeStyles::sanitize_css( get_option( 'autoptimize_ccss_additional', '' ) ); ?></textarea>
189 <table class="rules-list" cellspacing="0"><tbody id="rules-list"></tbody></table>
190 <input class="hidden" type="text" id="critCssOrigin" name="autoptimize_ccss_rules" value='<?php echo ( json_encode( $ao_ccss_rules, JSON_FORCE_OBJECT ) ); ?>'>
191 <!-- Wrapper for in screen notices -->
192 <div id="rules-notices"></div>
193 <!-- END Rule add/edit dialogs -->
194 <div class="submit rules-btn">
195 <div class="alignleft">
196 <span id="addCritCssButton" class="button-secondary"><?php _e( 'Add New Rule', 'autoptimize' ); ?></span>
197 <span id="editDefaultButton" class="button-secondary"><?php _e( 'Edit Default Rule CSS', 'autoptimize' ); ?></span>
198 <span id="editAdditionalButton" class="button-secondary"><?php _e( 'Add CSS To All Rules', 'autoptimize' ); ?></span>
199 </div>
200 <div class="alignright">
201 <span id="removeAllRules" class="button-secondary" style="color:red;"><?php _e( 'Remove all rules', 'autoptimize' ); ?></span>
202 </div>
203 </div>
204 <!-- END Rules UI -->
205 </li>
206 </ul>
207 <?php
208 }
209
210 /**
211 * Sanitize rules before rendering.
212 *
213 * @param array $rules Array with rules to be sanitized.
214 */
215 function sanitize_rules( $rules ) {
216 if ( apply_filters( 'autoptimize_filter_ccss_paths_clickable', true ) ) {
217 if ( array_key_exists( 'paths', $rules ) ) {
218 foreach ( $rules['paths'] as $key => $value ) {
219 $newkey = esc_url( $key );
220 if ( $newkey !== $key ) {
221 if ( 0 === strpos( $newkey, 'http://' ) && 0 !== strpos( $key, 'http://' ) ) {
222 // esc_url adds "http://" to any string that does not start with either a protocol or a
223 // slash, see https://developer.wordpress.org/reference/functions/esc_url/#more-information
224 // this removes that unneeded protocol again.
225 $newkey = substr_replace( $newkey, '', 0, 7 );
226 }
227 unset( $rules['paths'][ $key ] );
228 $rules['paths'][ $newkey ] = $value;
229 }
230 }
231 }
232 }
233
234 $rules = autoptimizeUtils::strip_tags_array( $rules );
235 return $rules;
236 }
237
238 ?>
239