| @@ -7,11 +7,16 @@ | ||
| 7 | 7 | * Main function to render the rules panel. |
| 8 | 8 | */ |
| 9 | 9 | function ao_ccss_render_rules() { |
| 10 | 10 | // Attach required arrays. |
| 11 | - global $ao_ccss_rules; | |
| 12 | - global $ao_ccss_types; | |
| 13 | -?> | |
| 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 | + ?> | |
| 14 | 19 | <ul id="rules-panel"> |
| 15 | 20 | <li class="itemDetail"> |
| 16 | 21 | <h2 class="itemTitle"><?php _e( 'Rules', 'autoptimize' ); ?></h2> |
| 17 | 22 | |
| @@ -103,11 +108,11 @@ | ||
| 103 | 108 | if ( substr( $type, 0, 12 ) === 'custom_post_' ) { |
| 104 | 109 | $_type = str_replace( 'custom_post_', '', $type ); |
| 105 | 110 | } elseif ( substr( $type, 0, 9 ) === 'template_' ) { |
| 106 | 111 | $_type = str_replace( 'template_', '', $type ); |
| 107 | - } elseif ( 'bbp_is_bbpress' == $type ) { | |
| 112 | + } elseif ( 'bbp_is_bbpress' === $type ) { | |
| 108 | 113 | $_type = str_replace( 'bbp_', '', $type ); |
| 109 | - } elseif ( 'bp_is_buddypress' == $type ) { | |
| 114 | + } elseif ( 'bp_is_buddypress' === $type ) { | |
| 110 | 115 | $_type = str_replace( 'bp_', '', $type ); |
| 111 | 116 | } elseif ( substr( $type, 0, 4 ) === 'woo_' ) { |
| 112 | 117 | $_type = str_replace( 'woo_', '', $type ); |
| 113 | 118 | } elseif ( substr( $type, 0, 4 ) === 'edd_' ) { |
| @@ -149,24 +154,20 @@ | ||
| 149 | 154 | </div> |
| 150 | 155 | |
| 151 | 156 | <!-- Add/edit default critical CSS dialog --> |
| 152 | 157 | <div id="default_critcss_wrapper" class="hidden"> |
| 153 | - <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> | |
| 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> | |
| 154 | 159 | </div> |
| 155 | 160 | |
| 156 | 161 | <!-- Add/edit additional critical CSS dialog --> |
| 157 | 162 | <div id="additional_critcss_wrapper" class="hidden"> |
| 158 | - <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> | |
| 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> | |
| 159 | 164 | </div> |
| 160 | 165 | |
| 161 | - <!-- Wrapper for in screen notices --> | |
| 162 | - <div id="rules-notices"></div> | |
| 163 | - <!-- END Rule add/edit dialogs --> | |
| 164 | - | |
| 165 | 166 | <!-- BEGIN Rules UI --> |
| 166 | 167 | <div class="howto"> |
| 167 | 168 | <div class="title-wrap"> |
| 168 | - <h4 class="title"><?php _e( 'How To Use Autoptimize CriticalCSS Power-Up Rules', 'autoptimize' ); ?></h4> | |
| 169 | + <h4 class="title"><?php _e( 'How To Use Autoptimize CriticalCSS Rules', 'autoptimize' ); ?></h4> | |
| 169 | 170 | <p class="subtitle"><?php _e( 'Click the side arrow to toggle instructions', 'autoptimize' ); ?></p> |
| 170 | 171 | </div> |
| 171 | 172 | <button type="button" class="toggle-btn"> |
| 172 | 173 | <span class="toggle-indicator dashicons dashicons-arrow-up dashicons-arrow-down"></span> |
| @@ -173,23 +174,24 @@ | ||
| 173 | 174 | </button> |
| 174 | 175 | <div class="howto-wrap hidden"> |
| 175 | 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> |
| 176 | 177 | <ol> |
| 177 | - <li><?php _e( 'When a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> API key is in place, Autoptimize CriticalCSS Power-Up starts to operate <strong>automatically</strong>.', 'autoptimize' ); ?></li> | |
| 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> | |
| 178 | 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> |
| 179 | 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> |
| 180 | 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> |
| 181 | 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> |
| 182 | - <li><?php _e( 'You can also create an <span class="badge auto">AUTO</span> rule for a path by leaving its critical CSS content empty. The critical CSS for that path will be automatically fetched from <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> for you and updated whenever it changes.', 'autoptimize' ); ?></li> | |
| 183 | - <li><?php _e( "If you see an <span class='badge auto'>AUTO</span> rule with a <span class='badge review'>R</span> besides it (R is after REVIEW), it means that the fetched critical CSS for that rule is not 100% garanteed to work according to <a href='https://criticalcss.com/?aff=1' target='_blank'>criticalcss.com</a> analysis. It's advised that you edit and review that rule to make any required adjustments.", 'autoptimize' ); ?></li> | |
| 184 | 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> |
| 185 | 184 | </ol> |
| 186 | 185 | </div> |
| 187 | 186 | </div> |
| 188 | - <textarea id="autoptimize_css_defer_inline" name="autoptimize_css_defer_inline" rows="19" cols="10" style="width:100%;"><?php echo get_option( 'autoptimize_css_defer_inline', '' ); ?></textarea> | |
| 189 | - <textarea id="autoptimize_ccss_additional" name="autoptimize_ccss_additional" rows="19" cols="10" style="width:100%;"><?php echo get_option( 'autoptimize_ccss_additional', '' ); ?></textarea> | |
| 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> | |
| 190 | 189 | <table class="rules-list" cellspacing="0"><tbody id="rules-list"></tbody></table> |
| 191 | 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 --> | |
| 192 | 194 | <div class="submit rules-btn"> |
| 193 | 195 | <div class="alignleft"> |
| 194 | 196 | <span id="addCritCssButton" class="button-secondary"><?php _e( 'Add New Rule', 'autoptimize' ); ?></span> |
| 195 | 197 | <span id="editDefaultButton" class="button-secondary"><?php _e( 'Edit Default Rule CSS', 'autoptimize' ); ?></span> |
| @@ -201,7 +203,33 @@ | ||
| 201 | 203 | </div> |
| 202 | 204 | <!-- END Rules UI --> |
| 203 | 205 | </li> |
| 204 | 206 | </ul> |
| 205 | -<?php | |
| 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 | + return $rules; | |
| 206 | 234 | } |
| 207 | 235 | ?> |