@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Autoptimize options handler. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -21,8 +21,8 @@ discard block |
||
21 | 21 | * Ensure that is_plugin_active_for_network function is declared. |
22 | 22 | */ |
23 | 23 | public static function maybe_include_plugin_functions() { |
24 | - if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
|
25 | - include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
24 | + if (!function_exists('is_plugin_active_for_network')) { |
|
25 | + include_once ABSPATH.'wp-admin/includes/plugin.php'; |
|
26 | 26 | } |
27 | 27 | } |
28 | 28 | |
@@ -33,19 +33,19 @@ discard block |
||
33 | 33 | * @param mixed $default Optional. Default value to return if the option does not exist. |
34 | 34 | * @return mixed Value set for the option. |
35 | 35 | */ |
36 | - public static function get_option( $option, $default = false ) { |
|
36 | + public static function get_option($option, $default = false) { |
|
37 | 37 | // This is always a network setting, it is on by default to ensure settings are available at site level unless explicitly turned off. |
38 | - if ( 'autoptimize_enable_site_config' === $option ) { |
|
39 | - return get_network_option( get_main_network_id(), $option, 'on' ); |
|
38 | + if ('autoptimize_enable_site_config' === $option) { |
|
39 | + return get_network_option(get_main_network_id(), $option, 'on'); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | // If the plugin is network activated and our per site setting is not on, use the network configuration. |
43 | - $configuration_per_site = get_network_option( get_main_network_id(), 'autoptimize_enable_site_config', 'on' ); |
|
44 | - if ( self::is_ao_active_for_network() && ( 'on' !== $configuration_per_site || is_network_admin() ) ) { |
|
45 | - return get_network_option( get_main_network_id(), $option, $default ); |
|
43 | + $configuration_per_site = get_network_option(get_main_network_id(), 'autoptimize_enable_site_config', 'on'); |
|
44 | + if (self::is_ao_active_for_network() && ('on' !== $configuration_per_site || is_network_admin())) { |
|
45 | + return get_network_option(get_main_network_id(), $option, $default); |
|
46 | 46 | } |
47 | 47 | |
48 | - return get_option( $option, $default ); |
|
48 | + return get_option($option, $default); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | /** |
@@ -59,11 +59,11 @@ discard block |
||
59 | 59 | * the default value is 'yes'. Default null. |
60 | 60 | * @return bool False if value was not updated and true if value was updated. |
61 | 61 | */ |
62 | - public static function update_option( $option, $value, $autoload = null ) { |
|
63 | - if ( self::is_ao_active_for_network() && is_network_admin() ) { |
|
64 | - return update_network_option( get_main_network_id(), $option, $value ); |
|
65 | - } elseif ( 'autoptimize_enable_site_config' !== $option ) { |
|
66 | - return update_option( $option, $value, $autoload ); |
|
62 | + public static function update_option($option, $value, $autoload = null) { |
|
63 | + if (self::is_ao_active_for_network() && is_network_admin()) { |
|
64 | + return update_network_option(get_main_network_id(), $option, $value); |
|
65 | + } elseif ('autoptimize_enable_site_config' !== $option) { |
|
66 | + return update_option($option, $value, $autoload); |
|
67 | 67 | } |
68 | 68 | } |
69 | 69 | |
@@ -72,8 +72,8 @@ discard block |
||
72 | 72 | * in that case, take care of multisite case. |
73 | 73 | */ |
74 | 74 | public static function check_multisite_on_saving_options() { |
75 | - if ( self::is_ao_active_for_network() ) { |
|
76 | - add_filter( 'pre_update_option', 'autoptimizeOptionWrapper::update_autoptimize_option_on_network', 10, 3 ); |
|
75 | + if (self::is_ao_active_for_network()) { |
|
76 | + add_filter('pre_update_option', 'autoptimizeOptionWrapper::update_autoptimize_option_on_network', 10, 3); |
|
77 | 77 | } |
78 | 78 | } |
79 | 79 | |
@@ -84,16 +84,16 @@ discard block |
||
84 | 84 | * @param string $option Option name. |
85 | 85 | * @param string $old_value Old value. |
86 | 86 | */ |
87 | - public static function update_autoptimize_option_on_network( $value, $option, $old_value ) { |
|
88 | - if ( strpos( $option, 'autoptimize_' ) === 0 && self::is_options_from_network_admin() ) { |
|
89 | - if ( self::is_ao_active_for_network() ) { |
|
90 | - update_network_option( get_main_network_id(), $option, $value ); |
|
87 | + public static function update_autoptimize_option_on_network($value, $option, $old_value) { |
|
88 | + if (strpos($option, 'autoptimize_') === 0 && self::is_options_from_network_admin()) { |
|
89 | + if (self::is_ao_active_for_network()) { |
|
90 | + update_network_option(get_main_network_id(), $option, $value); |
|
91 | 91 | // Return old value, to stop update_option logic. |
92 | 92 | return $old_value; |
93 | 93 | } |
94 | - if ( apply_filters( 'autoptimize_filter_optionwrapper_wp_cache_delete', false ) ) { |
|
94 | + if (apply_filters('autoptimize_filter_optionwrapper_wp_cache_delete', false)) { |
|
95 | 95 | // in some (rare) cases options seem to get stuck in WP's Object cache, this should clear it there. |
96 | - wp_cache_delete( $option ); |
|
96 | + wp_cache_delete($option); |
|
97 | 97 | } |
98 | 98 | } |
99 | 99 | return $value; |
@@ -107,8 +107,8 @@ discard block |
||
107 | 107 | public static function is_options_from_network_admin() { |
108 | 108 | static $_really_is_network_admin = null; |
109 | 109 | |
110 | - if ( null === $_really_is_network_admin ) { |
|
111 | - if ( array_key_exists( '_wp_http_referer', $_POST ) && strpos( network_admin_url( 'settings.php' ), strtok( $_POST['_wp_http_referer'], '?' ) ) !== false ) { |
|
110 | + if (null === $_really_is_network_admin) { |
|
111 | + if (array_key_exists('_wp_http_referer', $_POST) && strpos(network_admin_url('settings.php'), strtok($_POST['_wp_http_referer'], '?')) !== false) { |
|
112 | 112 | $_really_is_network_admin = true; |
113 | 113 | } else { |
114 | 114 | $_really_is_network_admin = false; |
@@ -123,9 +123,9 @@ discard block |
||
123 | 123 | */ |
124 | 124 | public static function is_ao_active_for_network() { |
125 | 125 | static $_is_ao_active_for_network = null; |
126 | - if ( null === $_is_ao_active_for_network || defined( 'TEST_MULTISITE_FORCE_AO_ON_NETWORK' ) ) { |
|
126 | + if (null === $_is_ao_active_for_network || defined('TEST_MULTISITE_FORCE_AO_ON_NETWORK')) { |
|
127 | 127 | self::maybe_include_plugin_functions(); |
128 | - if ( is_plugin_active_for_network( 'autoptimize/autoptimize.php' ) || is_plugin_active_for_network( 'autoptimize-beta/autoptimize.php' ) || defined( 'TEST_MULTISITE_FORCE_AO_ON_NETWORK' ) ) { |
|
128 | + if (is_plugin_active_for_network('autoptimize/autoptimize.php') || is_plugin_active_for_network('autoptimize-beta/autoptimize.php') || defined('TEST_MULTISITE_FORCE_AO_ON_NETWORK')) { |
|
129 | 129 | $_is_ao_active_for_network = true; |
130 | 130 | } else { |
131 | 131 | $_is_ao_active_for_network = false; |
@@ -335,6 +335,9 @@ discard block |
||
335 | 335 | } |
336 | 336 | } |
337 | 337 | |
338 | + /** |
|
339 | + * @param boolean $render |
|
340 | + */ |
|
338 | 341 | public static function ao_ccss_key_status( $render ) { |
339 | 342 | // Provide key status |
340 | 343 | // Get key and key status. |
@@ -539,6 +542,9 @@ discard block |
||
539 | 542 | return true; |
540 | 543 | } |
541 | 544 | |
545 | + /** |
|
546 | + * @param integer $lvl |
|
547 | + */ |
|
542 | 548 | public static function ao_ccss_log( $msg, $lvl ) { |
543 | 549 | // Commom logging facility |
544 | 550 | // Attach debug option. |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * gets called by AO core, checks the rules and if a matching rule is found returns the associated CCSS. |
5 | 5 | */ |
6 | 6 | |
7 | -if ( ! defined( 'ABSPATH' ) ) { |
|
7 | +if (!defined('ABSPATH')) { |
|
8 | 8 | exit; |
9 | 9 | } |
10 | 10 | |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | { |
14 | 14 | // fetch all options at once and populate them individually explicitely as globals. |
15 | 15 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
16 | - foreach ( $all_options as $_option => $_value ) { |
|
16 | + foreach ($all_options as $_option => $_value) { |
|
17 | 17 | global ${$_option}; |
18 | 18 | ${$_option} = $_value; |
19 | 19 | } |
@@ -28,28 +28,28 @@ discard block |
||
28 | 28 | global $ao_ccss_unloadccss; |
29 | 29 | |
30 | 30 | // add all filters to do CCSS if key present. |
31 | - if ( $ao_css_defer && isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) ) { |
|
31 | + if ($ao_css_defer && isset($ao_ccss_key) && !empty($ao_ccss_key)) { |
|
32 | 32 | // Set AO behavior: disable minification to avoid double minifying and caching. |
33 | - add_filter( 'autoptimize_filter_css_critcss_minify', '__return_false' ); |
|
34 | - add_filter( 'autoptimize_filter_css_defer_inline', array( $this, 'ao_ccss_frontend' ), 10, 1 ); |
|
33 | + add_filter('autoptimize_filter_css_critcss_minify', '__return_false'); |
|
34 | + add_filter('autoptimize_filter_css_defer_inline', array($this, 'ao_ccss_frontend'), 10, 1); |
|
35 | 35 | |
36 | 36 | // Add the action to enqueue jobs for CriticalCSS cron. |
37 | - add_action( 'autoptimize_action_css_hash', array( 'autoptimizeCriticalCSSEnqueue', 'ao_ccss_enqueue' ), 10, 1 ); |
|
37 | + add_action('autoptimize_action_css_hash', array('autoptimizeCriticalCSSEnqueue', 'ao_ccss_enqueue'), 10, 1); |
|
38 | 38 | |
39 | 39 | // conditionally add the filter to defer jquery and others. |
40 | - if ( $ao_ccss_deferjquery ) { |
|
41 | - add_filter( 'autoptimize_html_after_minify', array( $this, 'ao_ccss_defer_jquery' ), 11, 1 ); |
|
40 | + if ($ao_ccss_deferjquery) { |
|
41 | + add_filter('autoptimize_html_after_minify', array($this, 'ao_ccss_defer_jquery'), 11, 1); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | // conditionally add filter to unload the CCSS. |
45 | - if ( $ao_ccss_unloadccss ) { |
|
46 | - add_filter( 'autoptimize_html_after_minify', array( $this, 'ao_ccss_unloadccss' ), 12, 1 ); |
|
45 | + if ($ao_ccss_unloadccss) { |
|
46 | + add_filter('autoptimize_html_after_minify', array($this, 'ao_ccss_unloadccss'), 12, 1); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | // Order paths by length, as longest ones have greater priority in the rules. |
50 | - if ( ! empty( $ao_ccss_rules['paths'] ) ) { |
|
51 | - $keys = array_map( 'strlen', array_keys( $ao_ccss_rules['paths'] ) ); |
|
52 | - array_multisort( $keys, SORT_DESC, $ao_ccss_rules['paths'] ); |
|
50 | + if (!empty($ao_ccss_rules['paths'])) { |
|
51 | + $keys = array_map('strlen', array_keys($ao_ccss_rules['paths'])); |
|
52 | + array_multisort($keys, SORT_DESC, $ao_ccss_rules['paths']); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | // Add an array with default WordPress's conditional tags |
@@ -58,14 +58,14 @@ discard block |
||
58 | 58 | $ao_ccss_types = $this->get_ao_ccss_core_types(); |
59 | 59 | |
60 | 60 | // Extend conditional tags on plugin initalization. |
61 | - add_action( apply_filters( 'autoptimize_filter_ccss_extend_types_hook', 'init' ), array( $this, 'ao_ccss_extend_types' ) ); |
|
61 | + add_action(apply_filters('autoptimize_filter_ccss_extend_types_hook', 'init'), array($this, 'ao_ccss_extend_types')); |
|
62 | 62 | |
63 | 63 | // When autoptimize cache is cleared, also clear transient cache for page templates. |
64 | - add_action( 'autoptimize_action_cachepurged', array( 'autoptimizeCriticalCSSCore', 'ao_ccss_clear_page_tpl_cache' ), 10, 0 ); |
|
64 | + add_action('autoptimize_action_cachepurged', array('autoptimizeCriticalCSSCore', 'ao_ccss_clear_page_tpl_cache'), 10, 0); |
|
65 | 65 | } |
66 | 66 | } |
67 | 67 | |
68 | - public function ao_ccss_frontend( $inlined ) { |
|
68 | + public function ao_ccss_frontend($inlined) { |
|
69 | 69 | // Apply CriticalCSS to frontend pages |
70 | 70 | // Attach types and settings arrays. |
71 | 71 | global $ao_ccss_types; |
@@ -77,20 +77,20 @@ discard block |
||
77 | 77 | $no_ccss = ''; |
78 | 78 | |
79 | 79 | // Only if keystatus is OK and option to add CCSS for logged on users is on or user is not logged in. |
80 | - if ( ( $ao_ccss_keyst && 2 == $ao_ccss_keyst ) && ( $ao_ccss_loggedin || ! is_user_logged_in() ) ) { |
|
80 | + if (($ao_ccss_keyst && 2 == $ao_ccss_keyst) && ($ao_ccss_loggedin || !is_user_logged_in())) { |
|
81 | 81 | // Check for a valid CriticalCSS based on path to return its contents. |
82 | - $req_path = strtok( $_SERVER['REQUEST_URI'], '?' ); |
|
83 | - if ( ! empty( $ao_ccss_rules['paths'] ) ) { |
|
84 | - foreach ( $ao_ccss_rules['paths'] as $path => $rule ) { |
|
82 | + $req_path = strtok($_SERVER['REQUEST_URI'], '?'); |
|
83 | + if (!empty($ao_ccss_rules['paths'])) { |
|
84 | + foreach ($ao_ccss_rules['paths'] as $path => $rule) { |
|
85 | 85 | // explicit match OR partial match if MANUAL rule. |
86 | - if ( $req_path == $path || urldecode( $req_path ) == $path || ( false == $rule['hash'] && false != $rule['file'] && strpos( $req_path, str_replace( site_url(), '', $path ) ) !== false ) ) { |
|
87 | - if ( file_exists( AO_CCSS_DIR . $rule['file'] ) ) { |
|
88 | - $_ccss_contents = file_get_contents( AO_CCSS_DIR . $rule['file'] ); |
|
89 | - if ( 'none' != $_ccss_contents ) { |
|
90 | - if ( $ao_ccss_debug ) { |
|
91 | - $_ccss_contents = '/* PATH: ' . $path . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
86 | + if ($req_path == $path || urldecode($req_path) == $path || (false == $rule['hash'] && false != $rule['file'] && strpos($req_path, str_replace(site_url(), '', $path)) !== false)) { |
|
87 | + if (file_exists(AO_CCSS_DIR.$rule['file'])) { |
|
88 | + $_ccss_contents = file_get_contents(AO_CCSS_DIR.$rule['file']); |
|
89 | + if ('none' != $_ccss_contents) { |
|
90 | + if ($ao_ccss_debug) { |
|
91 | + $_ccss_contents = '/* PATH: '.$path.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
92 | 92 | } |
93 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
93 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
94 | 94 | } else { |
95 | 95 | $no_ccss = 'none'; |
96 | 96 | } |
@@ -100,55 +100,55 @@ discard block |
||
100 | 100 | } |
101 | 101 | |
102 | 102 | // Check for a valid CriticalCSS based on conditional tags to return its contents. |
103 | - if ( ! empty( $ao_ccss_rules['types'] ) && 'none' !== $no_ccss ) { |
|
103 | + if (!empty($ao_ccss_rules['types']) && 'none' !== $no_ccss) { |
|
104 | 104 | // order types-rules by the order of the original $ao_ccss_types array so as not to depend on the order in which rules were added. |
105 | - $ao_ccss_rules['types'] = array_replace( array_intersect_key( array_flip( $ao_ccss_types ), $ao_ccss_rules['types'] ), $ao_ccss_rules['types'] ); |
|
105 | + $ao_ccss_rules['types'] = array_replace(array_intersect_key(array_flip($ao_ccss_types), $ao_ccss_rules['types']), $ao_ccss_rules['types']); |
|
106 | 106 | $is_front_page = is_front_page(); |
107 | 107 | |
108 | - foreach ( $ao_ccss_rules['types'] as $type => $rule ) { |
|
109 | - if ( in_array( $type, $ao_ccss_types ) && file_exists( AO_CCSS_DIR . $rule['file'] ) ) { |
|
110 | - $_ccss_contents = file_get_contents( AO_CCSS_DIR . $rule['file'] ); |
|
111 | - if ( $is_front_page && 'is_front_page' == $type ) { |
|
112 | - if ( 'none' != $_ccss_contents ) { |
|
113 | - if ( $ao_ccss_debug ) { |
|
114 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
108 | + foreach ($ao_ccss_rules['types'] as $type => $rule) { |
|
109 | + if (in_array($type, $ao_ccss_types) && file_exists(AO_CCSS_DIR.$rule['file'])) { |
|
110 | + $_ccss_contents = file_get_contents(AO_CCSS_DIR.$rule['file']); |
|
111 | + if ($is_front_page && 'is_front_page' == $type) { |
|
112 | + if ('none' != $_ccss_contents) { |
|
113 | + if ($ao_ccss_debug) { |
|
114 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
115 | 115 | } |
116 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
116 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
117 | 117 | } else { |
118 | 118 | $no_ccss = 'none'; |
119 | 119 | } |
120 | - } elseif ( strpos( $type, 'custom_post_' ) === 0 && ! $is_front_page ) { |
|
121 | - if ( get_post_type( get_the_ID() ) === substr( $type, 12 ) ) { |
|
122 | - if ( 'none' != $_ccss_contents ) { |
|
123 | - if ( $ao_ccss_debug ) { |
|
124 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
120 | + } elseif (strpos($type, 'custom_post_') === 0 && !$is_front_page) { |
|
121 | + if (get_post_type(get_the_ID()) === substr($type, 12)) { |
|
122 | + if ('none' != $_ccss_contents) { |
|
123 | + if ($ao_ccss_debug) { |
|
124 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
125 | 125 | } |
126 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
126 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
127 | 127 | } else { |
128 | 128 | $no_ccss = 'none'; |
129 | 129 | } |
130 | 130 | } |
131 | - } elseif ( 0 === strpos( $type, 'template_' ) && ! $is_front_page ) { |
|
132 | - if ( is_page_template( substr( $type, 9 ) ) ) { |
|
133 | - if ( 'none' != $_ccss_contents ) { |
|
134 | - if ( $ao_ccss_debug ) { |
|
135 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
131 | + } elseif (0 === strpos($type, 'template_') && !$is_front_page) { |
|
132 | + if (is_page_template(substr($type, 9))) { |
|
133 | + if ('none' != $_ccss_contents) { |
|
134 | + if ($ao_ccss_debug) { |
|
135 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
136 | 136 | } |
137 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
137 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
138 | 138 | } else { |
139 | 139 | $no_ccss = 'none'; |
140 | 140 | } |
141 | 141 | } |
142 | - } elseif ( ! $is_front_page ) { |
|
142 | + } elseif (!$is_front_page) { |
|
143 | 143 | // all "normal" conditional tags, core + woo + buddypress + edd + bbpress |
144 | 144 | // but we have to remove the prefix for the non-core ones for them to function. |
145 | - $type = str_replace( array( 'woo_', 'bp_', 'bbp_', 'edd_' ), '', $type ); |
|
146 | - if ( function_exists( $type ) && call_user_func( $type ) ) { |
|
147 | - if ( 'none' != $_ccss_contents ) { |
|
148 | - if ( $ao_ccss_debug ) { |
|
149 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
145 | + $type = str_replace(array('woo_', 'bp_', 'bbp_', 'edd_'), '', $type); |
|
146 | + if (function_exists($type) && call_user_func($type)) { |
|
147 | + if ('none' != $_ccss_contents) { |
|
148 | + if ($ao_ccss_debug) { |
|
149 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
150 | 150 | } |
151 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
151 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
152 | 152 | } else { |
153 | 153 | $no_ccss = 'none'; |
154 | 154 | } |
@@ -161,45 +161,45 @@ discard block |
||
161 | 161 | |
162 | 162 | // Finally, inline the default CriticalCSS if any or else the entire CSS for the page |
163 | 163 | // This also applies to logged in users if the option to add CCSS for logged in users has been disabled. |
164 | - if ( ! empty( $inlined ) && 'none' !== $no_ccss ) { |
|
165 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $inlined . $ao_ccss_additional ); |
|
164 | + if (!empty($inlined) && 'none' !== $no_ccss) { |
|
165 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $inlined.$ao_ccss_additional); |
|
166 | 166 | } else { |
167 | - add_filter( 'autoptimize_filter_css_inline', '__return_true' ); |
|
167 | + add_filter('autoptimize_filter_css_inline', '__return_true'); |
|
168 | 168 | return; |
169 | 169 | } |
170 | 170 | } |
171 | 171 | |
172 | - public function ao_ccss_defer_jquery( $in ) { |
|
172 | + public function ao_ccss_defer_jquery($in) { |
|
173 | 173 | global $ao_ccss_loggedin; |
174 | 174 | // defer all linked and inline JS. |
175 | - if ( ( ! is_user_logged_in() || $ao_ccss_loggedin ) && preg_match_all( '#<script.*>(.*)</script>#Usmi', $in, $matches, PREG_SET_ORDER ) ) { |
|
176 | - foreach ( $matches as $match ) { |
|
177 | - if ( str_replace( apply_filters( 'autoptimize_filter_ccss_core_defer_exclude', array( 'data-noptimize="1"', 'data-cfasync="false"', 'data-pagespeed-no-defer' ) ), '', $match[0] ) !== $match[0] ) { |
|
175 | + if ((!is_user_logged_in() || $ao_ccss_loggedin) && preg_match_all('#<script.*>(.*)</script>#Usmi', $in, $matches, PREG_SET_ORDER)) { |
|
176 | + foreach ($matches as $match) { |
|
177 | + if (str_replace(apply_filters('autoptimize_filter_ccss_core_defer_exclude', array('data-noptimize="1"', 'data-cfasync="false"', 'data-pagespeed-no-defer')), '', $match[0]) !== $match[0]) { |
|
178 | 178 | // do not touch JS with noptimize/ cfasync/ pagespeed-no-defer flags. |
179 | 179 | continue; |
180 | - } elseif ( '' !== $match[1] && ( ! preg_match( '/<script.* type\s?=.*>/', $match[0] ) || preg_match( '/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $match[0] ) ) ) { |
|
180 | + } elseif ('' !== $match[1] && (!preg_match('/<script.* type\s?=.*>/', $match[0]) || preg_match('/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $match[0]))) { |
|
181 | 181 | // base64-encode and defer all inline JS. |
182 | - $base64_js = '<script defer src="data:text/javascript;base64,' . base64_encode( $match[1] ) . '"></script>'; |
|
183 | - $in = str_replace( $match[0], $base64_js, $in ); |
|
184 | - } elseif ( str_replace( array( ' defer', ' async' ), '', $match[0] ) === $match[0] ) { |
|
182 | + $base64_js = '<script defer src="data:text/javascript;base64,'.base64_encode($match[1]).'"></script>'; |
|
183 | + $in = str_replace($match[0], $base64_js, $in); |
|
184 | + } elseif (str_replace(array(' defer', ' async'), '', $match[0]) === $match[0]) { |
|
185 | 185 | // and defer linked JS unless already deferred or asynced. |
186 | - $new_match = str_replace( '<script ', '<script defer ', $match[0] ); |
|
187 | - $in = str_replace( $match[0], $new_match, $in ); |
|
186 | + $new_match = str_replace('<script ', '<script defer ', $match[0]); |
|
187 | + $in = str_replace($match[0], $new_match, $in); |
|
188 | 188 | } |
189 | 189 | } |
190 | 190 | } |
191 | 191 | return $in; |
192 | 192 | } |
193 | 193 | |
194 | - public function ao_ccss_unloadccss( $html_in ) { |
|
194 | + public function ao_ccss_unloadccss($html_in) { |
|
195 | 195 | // set media attrib of inline CCSS to none at onLoad to avoid it impacting full CSS (rarely needed). |
196 | - $_unloadccss_js = apply_filters( 'autoptimize_filter_ccss_core_unloadccss_js', '<script>window.addEventListener("load", function(event) {document.getElementById("aoatfcss").media="none";})</script>' ); |
|
196 | + $_unloadccss_js = apply_filters('autoptimize_filter_ccss_core_unloadccss_js', '<script>window.addEventListener("load", function(event) {document.getElementById("aoatfcss").media="none";})</script>'); |
|
197 | 197 | |
198 | - if ( false !== strpos( $html_in, $_unloadccss_js . '</body>' ) ) { |
|
198 | + if (false !== strpos($html_in, $_unloadccss_js.'</body>')) { |
|
199 | 199 | return $html_in; |
200 | 200 | } |
201 | 201 | |
202 | - return str_replace( '</body>', $_unloadccss_js . '</body>', $html_in ); |
|
202 | + return str_replace('</body>', $_unloadccss_js.'</body>', $html_in); |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | public function ao_ccss_extend_types() { |
@@ -208,9 +208,9 @@ discard block |
||
208 | 208 | global $ao_ccss_types; |
209 | 209 | |
210 | 210 | // in some cases $ao_ccss_types is empty and/or not an array, this should work around that problem. |
211 | - if ( empty( $ao_ccss_types ) || ! is_array( $ao_ccss_types ) ) { |
|
211 | + if (empty($ao_ccss_types) || !is_array($ao_ccss_types)) { |
|
212 | 212 | $ao_ccss_types = get_ao_ccss_core_types(); |
213 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Empty types array in extend, refetching array with core conditionals.', 3 ); |
|
213 | + autoptimizeCriticalCSSCore::ao_ccss_log('Empty types array in extend, refetching array with core conditionals.', 3); |
|
214 | 214 | } |
215 | 215 | |
216 | 216 | // Custom Post Types. |
@@ -222,23 +222,23 @@ discard block |
||
222 | 222 | 'names', |
223 | 223 | 'and' |
224 | 224 | ); |
225 | - foreach ( $cpts as $cpt ) { |
|
226 | - array_unshift( $ao_ccss_types, 'custom_post_' . $cpt ); |
|
225 | + foreach ($cpts as $cpt) { |
|
226 | + array_unshift($ao_ccss_types, 'custom_post_'.$cpt); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | // Templates. |
230 | 230 | // Transient cache to avoid frequent disk reads. |
231 | - $templates = get_transient( 'autoptimize_ccss_page_templates' ); |
|
232 | - if ( ! $templates ) { |
|
231 | + $templates = get_transient('autoptimize_ccss_page_templates'); |
|
232 | + if (!$templates) { |
|
233 | 233 | $templates = wp_get_theme()->get_page_templates(); |
234 | - set_transient( 'autoptimize_ccss_page_templates', $templates, HOUR_IN_SECONDS ); |
|
234 | + set_transient('autoptimize_ccss_page_templates', $templates, HOUR_IN_SECONDS); |
|
235 | 235 | } |
236 | - foreach ( $templates as $tplfile => $tplname ) { |
|
237 | - array_unshift( $ao_ccss_types, 'template_' . $tplfile ); |
|
236 | + foreach ($templates as $tplfile => $tplname) { |
|
237 | + array_unshift($ao_ccss_types, 'template_'.$tplfile); |
|
238 | 238 | } |
239 | 239 | |
240 | 240 | // bbPress tags. |
241 | - if ( function_exists( 'is_bbpress' ) ) { |
|
241 | + if (function_exists('is_bbpress')) { |
|
242 | 242 | $ao_ccss_types = array_merge( |
243 | 243 | array( |
244 | 244 | 'bbp_is_bbpress', |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | } |
271 | 271 | |
272 | 272 | // BuddyPress tags. |
273 | - if ( function_exists( 'is_buddypress' ) ) { |
|
273 | + if (function_exists('is_buddypress')) { |
|
274 | 274 | $ao_ccss_types = array_merge( |
275 | 275 | array( |
276 | 276 | 'bp_is_activation_page', |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | } |
313 | 313 | |
314 | 314 | // Easy Digital Downloads (EDD) tags. |
315 | - if ( function_exists( 'edd_is_checkout' ) ) { |
|
315 | + if (function_exists('edd_is_checkout')) { |
|
316 | 316 | $ao_ccss_types = array_merge( |
317 | 317 | array( |
318 | 318 | 'edd_is_checkout', |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | } |
325 | 325 | |
326 | 326 | // WooCommerce tags. |
327 | - if ( class_exists( 'WooCommerce' ) ) { |
|
327 | + if (class_exists('WooCommerce')) { |
|
328 | 328 | $ao_ccss_types = array_merge( |
329 | 329 | array( |
330 | 330 | 'woo_is_account_page', |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | |
344 | 344 | public function get_ao_ccss_core_types() { |
345 | 345 | global $ao_ccss_types; |
346 | - if ( empty( $ao_ccss_types ) || ! is_array( $ao_ccss_types ) ) { |
|
346 | + if (empty($ao_ccss_types) || !is_array($ao_ccss_types)) { |
|
347 | 347 | return array( |
348 | 348 | 'is_404', |
349 | 349 | 'is_archive', |
@@ -364,7 +364,7 @@ discard block |
||
364 | 364 | } |
365 | 365 | } |
366 | 366 | |
367 | - public static function ao_ccss_key_status( $render ) { |
|
367 | + public static function ao_ccss_key_status($render) { |
|
368 | 368 | // Provide key status |
369 | 369 | // Get key and key status. |
370 | 370 | global $ao_ccss_key; |
@@ -377,55 +377,55 @@ discard block |
||
377 | 377 | $key_return = array(); |
378 | 378 | $status = false; |
379 | 379 | |
380 | - if ( $key && 2 == $key_status ) { |
|
380 | + if ($key && 2 == $key_status) { |
|
381 | 381 | // Key exists and its status is valid. |
382 | 382 | // Set valid key status. |
383 | 383 | $status = 'valid'; |
384 | - $status_msg = __( 'Valid' ); |
|
384 | + $status_msg = __('Valid'); |
|
385 | 385 | $color = '#46b450'; // Green. |
386 | 386 | $message = null; |
387 | - } elseif ( $key && 1 == $key_status ) { |
|
387 | + } elseif ($key && 1 == $key_status) { |
|
388 | 388 | // Key exists but its validation has failed. |
389 | 389 | // Set invalid key status. |
390 | 390 | $status = 'invalid'; |
391 | - $status_msg = __( 'Invalid' ); |
|
391 | + $status_msg = __('Invalid'); |
|
392 | 392 | $color = '#dc3232'; // Red. |
393 | - $message = __( 'Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize' ); |
|
394 | - } elseif ( $key && ! $key_status ) { |
|
393 | + $message = __('Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize'); |
|
394 | + } elseif ($key && !$key_status) { |
|
395 | 395 | // Key exists but it has no valid status yet |
396 | 396 | // Perform key validation. |
397 | - $key_check = $self->ao_ccss_key_validation( $key ); |
|
397 | + $key_check = $self->ao_ccss_key_validation($key); |
|
398 | 398 | |
399 | 399 | // Key is valid, set valid status. |
400 | - if ( $key_check ) { |
|
400 | + if ($key_check) { |
|
401 | 401 | $status = 'valid'; |
402 | - $status_msg = __( 'Valid' ); |
|
402 | + $status_msg = __('Valid'); |
|
403 | 403 | $color = '#46b450'; // Green. |
404 | 404 | $message = null; |
405 | 405 | } else { |
406 | 406 | // Key is invalid, set invalid status. |
407 | 407 | $status = 'invalid'; |
408 | - $status_msg = __( 'Invalid' ); |
|
408 | + $status_msg = __('Invalid'); |
|
409 | 409 | $color = '#dc3232'; // Red. |
410 | - if ( get_option( 'autoptimize_ccss_keyst' ) == 1 ) { |
|
411 | - $message = __( 'Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize' ); |
|
410 | + if (get_option('autoptimize_ccss_keyst') == 1) { |
|
411 | + $message = __('Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize'); |
|
412 | 412 | } else { |
413 | - $message = __( 'Something went wrong when checking your API key, make sure you server can communicate with https://criticalcss.com and/ or try again later.', 'autoptimize' ); |
|
413 | + $message = __('Something went wrong when checking your API key, make sure you server can communicate with https://criticalcss.com and/ or try again later.', 'autoptimize'); |
|
414 | 414 | } |
415 | 415 | } |
416 | 416 | } else { |
417 | 417 | // No key nor status |
418 | 418 | // Set no key status. |
419 | 419 | $status = 'nokey'; |
420 | - $status_msg = __( 'None' ); |
|
420 | + $status_msg = __('None'); |
|
421 | 421 | $color = '#ffb900'; // Yellow. |
422 | - $message = __( 'Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> API key to start.', 'autoptimize' ); |
|
422 | + $message = __('Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> API key to start.', 'autoptimize'); |
|
423 | 423 | } |
424 | 424 | |
425 | 425 | // Fill returned values. |
426 | 426 | $key_return['status'] = $status; |
427 | 427 | // Provide rendering information if required. |
428 | - if ( $render ) { |
|
428 | + if ($render) { |
|
429 | 429 | $key_return['stmsg'] = $status_msg; |
430 | 430 | $key_return['color'] = $color; |
431 | 431 | $key_return['msg'] = $message; |
@@ -435,7 +435,7 @@ discard block |
||
435 | 435 | return $key_return; |
436 | 436 | } |
437 | 437 | |
438 | - public function ao_ccss_key_validation( $key ) { |
|
438 | + public function ao_ccss_key_validation($key) { |
|
439 | 439 | global $ao_ccss_noptimize; |
440 | 440 | |
441 | 441 | // POST a dummy job to criticalcss.com to check for key validation |
@@ -443,26 +443,26 @@ discard block |
||
443 | 443 | $src_url = get_home_url(); |
444 | 444 | |
445 | 445 | // Avoid AO optimizations if required by config or avoid lazyload if lazyload is active in AO. |
446 | - if ( ! empty( $ao_ccss_noptimize ) ) { |
|
446 | + if (!empty($ao_ccss_noptimize)) { |
|
447 | 447 | $src_url .= '?ao_noptirocket=1'; |
448 | - } elseif ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) { |
|
448 | + } elseif (class_exists('autoptimizeImages', false) && autoptimizeImages::should_lazyload_wrapper()) { |
|
449 | 449 | $src_url .= '?ao_nolazy=1'; |
450 | 450 | } |
451 | 451 | |
452 | - $src_url = apply_filters( 'autoptimize_filter_ccss_cron_srcurl', $src_url ); |
|
452 | + $src_url = apply_filters('autoptimize_filter_ccss_cron_srcurl', $src_url); |
|
453 | 453 | |
454 | 454 | // Prepare the request. |
455 | - $url = esc_url_raw( AO_CCSS_API . 'generate' ); |
|
455 | + $url = esc_url_raw(AO_CCSS_API.'generate'); |
|
456 | 456 | $args = array( |
457 | 457 | 'headers' => array( |
458 | - 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER, |
|
458 | + 'User-Agent' => 'Autoptimize v'.AO_CCSS_VER, |
|
459 | 459 | 'Content-type' => 'application/json; charset=utf-8', |
460 | - 'Authorization' => 'JWT ' . $key, |
|
460 | + 'Authorization' => 'JWT '.$key, |
|
461 | 461 | 'Connection' => 'close', |
462 | 462 | ), |
463 | 463 | // Body must be JSON. |
464 | 464 | 'body' => json_encode( |
465 | - apply_filters( 'autoptimize_ccss_cron_api_generate_body', |
|
465 | + apply_filters('autoptimize_ccss_cron_api_generate_body', |
|
466 | 466 | array( |
467 | 467 | 'url' => $src_url, |
468 | 468 | 'aff' => 1, |
@@ -473,23 +473,23 @@ discard block |
||
473 | 473 | ); |
474 | 474 | |
475 | 475 | // Dispatch the request and store its response code. |
476 | - $req = wp_safe_remote_post( $url, $args ); |
|
477 | - $code = wp_remote_retrieve_response_code( $req ); |
|
478 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
476 | + $req = wp_safe_remote_post($url, $args); |
|
477 | + $code = wp_remote_retrieve_response_code($req); |
|
478 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
479 | 479 | |
480 | - if ( 200 == $code ) { |
|
480 | + if (200 == $code) { |
|
481 | 481 | // Response is OK. |
482 | 482 | // Set key status as valid and log key check. |
483 | - update_option( 'autoptimize_ccss_keyst', 2 ); |
|
484 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 ); |
|
483 | + update_option('autoptimize_ccss_keyst', 2); |
|
484 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is valid, updating key status', 3); |
|
485 | 485 | |
486 | 486 | // extract job-id from $body and put it in the queue as a P job |
487 | 487 | // but only if no jobs and no rules! |
488 | 488 | global $ao_ccss_queue; |
489 | 489 | global $ao_ccss_rules; |
490 | 490 | |
491 | - if ( 0 == count( $ao_ccss_queue ) && 0 == count( $ao_ccss_rules['types'] ) && 0 == count( $ao_ccss_rules['paths'] ) ) { |
|
492 | - if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] ) { |
|
491 | + if (0 == count($ao_ccss_queue) && 0 == count($ao_ccss_rules['types']) && 0 == count($ao_ccss_rules['paths'])) { |
|
492 | + if ('JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status']) { |
|
493 | 493 | $jprops['ljid'] = 'firstrun'; |
494 | 494 | $jprops['rtarget'] = 'types|is_front_page'; |
495 | 495 | $jprops['ptype'] = 'is_front_page'; |
@@ -500,30 +500,30 @@ discard block |
||
500 | 500 | $jprops['jqstat'] = $body['job']['status']; |
501 | 501 | $jprops['jrstat'] = null; |
502 | 502 | $jprops['jvstat'] = null; |
503 | - $jprops['jctime'] = microtime( true ); |
|
503 | + $jprops['jctime'] = microtime(true); |
|
504 | 504 | $jprops['jftime'] = null; |
505 | 505 | $ao_ccss_queue['/'] = $jprops; |
506 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
507 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
508 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Created P job for is_front_page based on API key check response.', 3 ); |
|
506 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
507 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
508 | + autoptimizeCriticalCSSCore::ao_ccss_log('Created P job for is_front_page based on API key check response.', 3); |
|
509 | 509 | } |
510 | 510 | } |
511 | 511 | return true; |
512 | - } elseif ( 401 == $code ) { |
|
512 | + } elseif (401 == $code) { |
|
513 | 513 | // Response is unauthorized |
514 | 514 | // Set key status as invalid and log key check. |
515 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
516 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
515 | + update_option('autoptimize_ccss_keyst', 1); |
|
516 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
517 | 517 | return false; |
518 | 518 | } else { |
519 | 519 | // Response unkown |
520 | 520 | // Log key check attempt. |
521 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: could not check API key status, this is a service error, body follows if any...', 2 ); |
|
522 | - if ( ! empty( $body ) ) { |
|
523 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
521 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: could not check API key status, this is a service error, body follows if any...', 2); |
|
522 | + if (!empty($body)) { |
|
523 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
524 | 524 | } |
525 | - if ( is_wp_error( $req ) ) { |
|
526 | - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 ); |
|
525 | + if (is_wp_error($req)) { |
|
526 | + autoptimizeCriticalCSSCore::ao_ccss_log($req->get_error_message(), 2); |
|
527 | 527 | } |
528 | 528 | return false; |
529 | 529 | } |
@@ -538,14 +538,14 @@ discard block |
||
538 | 538 | $viewport = array(); |
539 | 539 | |
540 | 540 | // Viewport Width. |
541 | - if ( ! empty( $ao_ccss_viewport['w'] ) ) { |
|
541 | + if (!empty($ao_ccss_viewport['w'])) { |
|
542 | 542 | $viewport['w'] = $ao_ccss_viewport['w']; |
543 | 543 | } else { |
544 | 544 | $viewport['w'] = ''; |
545 | 545 | } |
546 | 546 | |
547 | 547 | // Viewport Height. |
548 | - if ( ! empty( $ao_ccss_viewport['h'] ) ) { |
|
548 | + if (!empty($ao_ccss_viewport['h'])) { |
|
549 | 549 | $viewport['h'] = $ao_ccss_viewport['h']; |
550 | 550 | } else { |
551 | 551 | $viewport['h'] = ''; |
@@ -554,23 +554,23 @@ discard block |
||
554 | 554 | return $viewport; |
555 | 555 | } |
556 | 556 | |
557 | - public static function ao_ccss_check_contents( $ccss ) { |
|
557 | + public static function ao_ccss_check_contents($ccss) { |
|
558 | 558 | // Perform basic exploit avoidance and CSS validation. |
559 | - if ( ! empty( $ccss ) ) { |
|
559 | + if (!empty($ccss)) { |
|
560 | 560 | // Try to avoid code injection. |
561 | - $blocklist = array( '#!/', 'function(', '<script', '<?php' ); |
|
562 | - foreach ( $blocklist as $blocklisted ) { |
|
563 | - if ( strpos( $ccss, $blocklisted ) !== false ) { |
|
564 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received contained blocklisted content.', 2 ); |
|
561 | + $blocklist = array('#!/', 'function(', '<script', '<?php'); |
|
562 | + foreach ($blocklist as $blocklisted) { |
|
563 | + if (strpos($ccss, $blocklisted) !== false) { |
|
564 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received contained blocklisted content.', 2); |
|
565 | 565 | return false; |
566 | 566 | } |
567 | 567 | } |
568 | 568 | |
569 | 569 | // Check for most basics CSS structures. |
570 | - $needlist = array( '{', '}', ':' ); |
|
571 | - foreach ( $needlist as $needed ) { |
|
572 | - if ( false === strpos( $ccss, $needed ) && 'none' !== $ccss ) { |
|
573 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received did not seem to contain real CSS.', 2 ); |
|
570 | + $needlist = array('{', '}', ':'); |
|
571 | + foreach ($needlist as $needed) { |
|
572 | + if (false === strpos($ccss, $needed) && 'none' !== $ccss) { |
|
573 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received did not seem to contain real CSS.', 2); |
|
574 | 574 | return false; |
575 | 575 | } |
576 | 576 | } |
@@ -580,7 +580,7 @@ discard block |
||
580 | 580 | return true; |
581 | 581 | } |
582 | 582 | |
583 | - public static function ao_ccss_log( $msg, $lvl ) { |
|
583 | + public static function ao_ccss_log($msg, $lvl) { |
|
584 | 584 | // Commom logging facility |
585 | 585 | // Attach debug option. |
586 | 586 | global $ao_ccss_debug; |
@@ -591,7 +591,7 @@ discard block |
||
591 | 591 | // 3: DD (for debug) |
592 | 592 | // Default: UU (for unkown). |
593 | 593 | $level = false; |
594 | - switch ( $lvl ) { |
|
594 | + switch ($lvl) { |
|
595 | 595 | case 1: |
596 | 596 | $level = 'II'; |
597 | 597 | break; |
@@ -600,7 +600,7 @@ discard block |
||
600 | 600 | break; |
601 | 601 | case 3: |
602 | 602 | // Output debug messages only if debug mode is enabled. |
603 | - if ( $ao_ccss_debug ) { |
|
603 | + if ($ao_ccss_debug) { |
|
604 | 604 | $level = 'DD'; |
605 | 605 | } |
606 | 606 | break; |
@@ -609,19 +609,19 @@ discard block |
||
609 | 609 | } |
610 | 610 | |
611 | 611 | // Prepare and write a log message if there's a valid level. |
612 | - if ( $level ) { |
|
612 | + if ($level) { |
|
613 | 613 | |
614 | 614 | // Prepare message. |
615 | - $message = date( 'c' ) . ' - [' . $level . '] ' . htmlentities( $msg ) . '<br>'; |
|
615 | + $message = date('c').' - ['.$level.'] '.htmlentities($msg).'<br>'; |
|
616 | 616 | |
617 | 617 | // Write message to log file. |
618 | - error_log( $message, 3, AO_CCSS_LOG ); |
|
618 | + error_log($message, 3, AO_CCSS_LOG); |
|
619 | 619 | } |
620 | 620 | } |
621 | 621 | |
622 | 622 | public static function ao_ccss_clear_page_tpl_cache() { |
623 | 623 | // Clears transient cache for page templates. |
624 | - delete_transient( 'autoptimize_ccss_page_templates' ); |
|
624 | + delete_transient('autoptimize_ccss_page_templates'); |
|
625 | 625 | } |
626 | 626 | |
627 | 627 | } |
@@ -423,6 +423,9 @@ discard block |
||
423 | 423 | } |
424 | 424 | } |
425 | 425 | |
426 | + /** |
|
427 | + * @param boolean|string $debug |
|
428 | + */ |
|
426 | 429 | public function ao_ccss_api_generate( $path, $debug, $dcode ) { |
427 | 430 | // POST jobs to criticalcss.com and return responses |
428 | 431 | // Get key and key status. |
@@ -559,6 +562,9 @@ discard block |
||
559 | 562 | } |
560 | 563 | } |
561 | 564 | |
565 | + /** |
|
566 | + * @param boolean $debug |
|
567 | + */ |
|
562 | 568 | public function ao_ccss_api_results( $jobid, $debug, $dcode ) { |
563 | 569 | // GET jobs from criticalcss.com and return responses |
564 | 570 | // Get key. |
@@ -625,6 +631,9 @@ discard block |
||
625 | 631 | } |
626 | 632 | } |
627 | 633 | |
634 | + /** |
|
635 | + * @param boolean $review |
|
636 | + */ |
|
628 | 637 | public function ao_ccss_save_file( $ccss, $target, $review ) { |
629 | 638 | // Save critical CSS into the filesystem and return its filename |
630 | 639 | // Prepare review mark. |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * processes the queue, submitting jobs to criticalcss.com and retrieving generated CSS from criticalcss.com and saving rules. |
5 | 5 | */ |
6 | 6 | |
7 | -if ( ! defined( 'ABSPATH' ) ) { |
|
7 | +if (!defined('ABSPATH')) { |
|
8 | 8 | exit; |
9 | 9 | } |
10 | 10 | |
@@ -13,23 +13,23 @@ discard block |
||
13 | 13 | { |
14 | 14 | // fetch all options at once and populate them individually explicitely as globals. |
15 | 15 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
16 | - foreach ( $all_options as $_option => $_value ) { |
|
16 | + foreach ($all_options as $_option => $_value) { |
|
17 | 17 | global ${$_option}; |
18 | 18 | ${$_option} = $_value; |
19 | 19 | } |
20 | 20 | |
21 | 21 | // Add queue control to a registered event. |
22 | - add_action( 'ao_ccss_queue', array( $this, 'ao_ccss_queue_control' ) ); |
|
22 | + add_action('ao_ccss_queue', array($this, 'ao_ccss_queue_control')); |
|
23 | 23 | // Add cleaning job to a registered event. |
24 | - add_action( 'ao_ccss_maintenance', array( $this, 'ao_ccss_cleaning' ) ); |
|
24 | + add_action('ao_ccss_maintenance', array($this, 'ao_ccss_cleaning')); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | public function ao_ccss_queue_control() { |
28 | 28 | // The queue execution backend. |
29 | 29 | global $ao_ccss_key; |
30 | - if ( ! isset( $ao_ccss_key ) || empty( $ao_ccss_key ) ) { |
|
30 | + if (!isset($ao_ccss_key) || empty($ao_ccss_key)) { |
|
31 | 31 | // no key set, not processing the queue! |
32 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'No key set, so not processing queue.', 3 ); |
|
32 | + autoptimizeCriticalCSSCore::ao_ccss_log('No key set, so not processing queue.', 3); |
|
33 | 33 | return; |
34 | 34 | } |
35 | 35 | |
@@ -50,35 +50,35 @@ discard block |
||
50 | 50 | * When properly set, queue will always finish a job with the declared settings above regardless of the real API responses. |
51 | 51 | */ |
52 | 52 | $queue_debug = false; |
53 | - if ( file_exists( AO_CCSS_DEBUG ) ) { |
|
54 | - $qdobj_raw = file_get_contents( AO_CCSS_DEBUG ); |
|
55 | - $qdobj = json_decode( $qdobj_raw, true ); |
|
56 | - if ( $qdobj ) { |
|
57 | - if ( 1 === $qdobj['enable'] ) { |
|
53 | + if (file_exists(AO_CCSS_DEBUG)) { |
|
54 | + $qdobj_raw = file_get_contents(AO_CCSS_DEBUG); |
|
55 | + $qdobj = json_decode($qdobj_raw, true); |
|
56 | + if ($qdobj) { |
|
57 | + if (1 === $qdobj['enable']) { |
|
58 | 58 | $queue_debug = true; |
59 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue operating in debug mode with the following settings: <' . $qdobj_raw . '>', 3 ); |
|
59 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue operating in debug mode with the following settings: <'.$qdobj_raw.'>', 3); |
|
60 | 60 | } |
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | 64 | // Set some default values for $qdobj to avoid function call warnings. |
65 | - if ( ! $queue_debug ) { |
|
65 | + if (!$queue_debug) { |
|
66 | 66 | $qdobj['htcode'] = false; |
67 | 67 | } |
68 | 68 | |
69 | 69 | // Check if queue is already running. |
70 | 70 | $queue_lock = false; |
71 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
71 | + if (file_exists(AO_CCSS_LOCK)) { |
|
72 | 72 | $queue_lock = true; |
73 | 73 | } |
74 | 74 | |
75 | 75 | // Proceed with the queue if it's not already running. |
76 | - if ( ! $queue_lock ) { |
|
76 | + if (!$queue_lock) { |
|
77 | 77 | |
78 | 78 | // Log queue start and create the lock file. |
79 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control started', 3 ); |
|
80 | - if ( touch( AO_CCSS_LOCK ) ) { |
|
81 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control locked', 3 ); |
|
79 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control started', 3); |
|
80 | + if (touch(AO_CCSS_LOCK)) { |
|
81 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control locked', 3); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | // Attach required variables. |
@@ -88,223 +88,223 @@ discard block |
||
88 | 88 | // Initialize job counters. |
89 | 89 | $jc = 1; |
90 | 90 | $jr = 1; |
91 | - $jt = count( $ao_ccss_queue ); |
|
91 | + $jt = count($ao_ccss_queue); |
|
92 | 92 | |
93 | 93 | // Sort queue by ascending job status (e.g. ERROR, JOB_ONGOING, JOB_QUEUED, NEW...). |
94 | - array_multisort( array_column( $ao_ccss_queue, 'jqstat' ), $ao_ccss_queue ); // @codingStandardsIgnoreLine |
|
94 | + array_multisort(array_column($ao_ccss_queue, 'jqstat'), $ao_ccss_queue); // @codingStandardsIgnoreLine |
|
95 | 95 | |
96 | 96 | // Iterates over the entire queue. |
97 | - foreach ( $ao_ccss_queue as $path => $jprops ) { |
|
97 | + foreach ($ao_ccss_queue as $path => $jprops) { |
|
98 | 98 | // Prepare flags and target rule. |
99 | 99 | $update = false; |
100 | 100 | $deljob = false; |
101 | 101 | $rule_update = false; |
102 | 102 | $oldccssfile = false; |
103 | - $trule = explode( '|', $jprops['rtarget'] ); |
|
103 | + $trule = explode('|', $jprops['rtarget']); |
|
104 | 104 | |
105 | 105 | // Log job count. |
106 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Processing job ' . $jc . ' of ' . $jt . ' with id <' . $jprops['ljid'] . '> and status <' . $jprops['jqstat'] . '>', 3 ); |
|
106 | + autoptimizeCriticalCSSCore::ao_ccss_log('Processing job '.$jc.' of '.$jt.' with id <'.$jprops['ljid'].'> and status <'.$jprops['jqstat'].'>', 3); |
|
107 | 107 | |
108 | 108 | // Process NEW jobs. |
109 | - if ( 'NEW' == $jprops['jqstat'] ) { |
|
109 | + if ('NEW' == $jprops['jqstat']) { |
|
110 | 110 | |
111 | 111 | // Log the new job. |
112 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found NEW job with local ID <' . $jprops['ljid'] . '>, starting its queue processing', 3 ); |
|
112 | + autoptimizeCriticalCSSCore::ao_ccss_log('Found NEW job with local ID <'.$jprops['ljid'].'>, starting its queue processing', 3); |
|
113 | 113 | |
114 | 114 | // Compare job and rule hashes (if any). |
115 | - $hash = $this->ao_ccss_diff_hashes( $jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget'] ); |
|
115 | + $hash = $this->ao_ccss_diff_hashes($jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget']); |
|
116 | 116 | |
117 | 117 | // If job hash is new or different of a previous one. |
118 | - if ( $hash ) { |
|
118 | + if ($hash) { |
|
119 | 119 | // Set job hash. |
120 | 120 | $jprops['hash'] = $hash; |
121 | 121 | |
122 | 122 | // If this is not the first job, wait 10 seconds before process next job due criticalcss.com API limits. |
123 | - if ( $jr > 1 ) { |
|
124 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Waiting ' . AO_CCSS_SLEEP . ' seconds due to criticalcss.com API limits', 3 ); |
|
125 | - sleep( AO_CCSS_SLEEP ); |
|
123 | + if ($jr > 1) { |
|
124 | + autoptimizeCriticalCSSCore::ao_ccss_log('Waiting '.AO_CCSS_SLEEP.' seconds due to criticalcss.com API limits', 3); |
|
125 | + sleep(AO_CCSS_SLEEP); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | // Dispatch the job generate request and increment request count. |
129 | - $apireq = $this->ao_ccss_api_generate( $path, $queue_debug, $qdobj['htcode'] ); |
|
129 | + $apireq = $this->ao_ccss_api_generate($path, $queue_debug, $qdobj['htcode']); |
|
130 | 130 | $jr++; |
131 | 131 | |
132 | 132 | // NOTE: All the following conditions maps to the ones in admin_settings_queue.js.php. |
133 | - if ( 'JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status'] ) { |
|
133 | + if ('JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status']) { |
|
134 | 134 | // SUCCESS: request has a valid result. |
135 | 135 | // Update job properties. |
136 | 136 | $jprops['jid'] = $apireq['job']['id']; |
137 | 137 | $jprops['jqstat'] = $apireq['job']['status']; |
138 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request successful, remote id <' . $jprops['jid'] . '>, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
139 | - } elseif ( 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
138 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> generate request successful, remote id <'.$jprops['jid'].'>, status now is <'.$jprops['jqstat'].'>', 3); |
|
139 | + } elseif ('STATUS_JOB_BAD' == $apireq['job']['status']) { |
|
140 | 140 | // ERROR: concurrent requests |
141 | 141 | // Update job properties. |
142 | 142 | $jprops['jid'] = $apireq['job']['id']; |
143 | 143 | $jprops['jqstat'] = $apireq['job']['status']; |
144 | - if ( $apireq['job']['error'] ) { |
|
144 | + if ($apireq['job']['error']) { |
|
145 | 145 | $jprops['jrstat'] = $apireq['job']['error']; |
146 | 146 | } else { |
147 | 147 | $jprops['jrstat'] = 'Baby did a bad bad thing'; |
148 | 148 | } |
149 | 149 | $jprops['jvstat'] = 'NONE'; |
150 | - $jprops['jftime'] = microtime( true ); |
|
151 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Concurrent requests when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
|
152 | - } elseif ( 'INVALID_JWT_TOKEN' == $apireq['errorCode'] ) { |
|
150 | + $jprops['jftime'] = microtime(true); |
|
151 | + autoptimizeCriticalCSSCore::ao_ccss_log('Concurrent requests when processing job id <'.$jprops['ljid'].'>, job status is now <'.$jprops['jqstat'].'>', 3); |
|
152 | + } elseif ('INVALID_JWT_TOKEN' == $apireq['errorCode']) { |
|
153 | 153 | // ERROR: key validation |
154 | 154 | // Update job properties. |
155 | 155 | $jprops['jqstat'] = $apireq['errorCode']; |
156 | 156 | $jprops['jrstat'] = $apireq['error']; |
157 | 157 | $jprops['jvstat'] = 'NONE'; |
158 | - $jprops['jftime'] = microtime( true ); |
|
159 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'API key validation error when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
|
160 | - } elseif ( empty( $apireq ) ) { |
|
158 | + $jprops['jftime'] = microtime(true); |
|
159 | + autoptimizeCriticalCSSCore::ao_ccss_log('API key validation error when processing job id <'.$jprops['ljid'].'>, job status is now <'.$jprops['jqstat'].'>', 3); |
|
160 | + } elseif (empty($apireq)) { |
|
161 | 161 | // ERROR: no response |
162 | 162 | // Update job properties. |
163 | 163 | $jprops['jqstat'] = 'NO_RESPONSE'; |
164 | 164 | $jprops['jrstat'] = 'NONE'; |
165 | 165 | $jprops['jvstat'] = 'NONE'; |
166 | - $jprops['jftime'] = microtime( true ); |
|
167 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
166 | + $jprops['jftime'] = microtime(true); |
|
167 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> request has no response, status now is <'.$jprops['jqstat'].'>', 3); |
|
168 | 168 | } else { |
169 | 169 | // UNKNOWN: unhandled generate exception |
170 | 170 | // Update job properties. |
171 | 171 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
172 | 172 | $jprops['jrstat'] = 'NONE'; |
173 | 173 | $jprops['jvstat'] = 'NONE'; |
174 | - $jprops['jftime'] = microtime( true ); |
|
175 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 ); |
|
176 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
174 | + $jprops['jftime'] = microtime(true); |
|
175 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> generate request has an UNKNOWN condition, status now is <'.$jprops['jqstat'].'>, check log messages above for more information', 2); |
|
176 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
177 | 177 | } |
178 | 178 | } else { |
179 | 179 | // SUCCESS: Job hash is equal to a previous one, so it's done |
180 | 180 | // Update job status and finish time. |
181 | 181 | $jprops['jqstat'] = 'JOB_DONE'; |
182 | - $jprops['jftime'] = microtime( true ); |
|
183 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> requires no further processing, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
182 | + $jprops['jftime'] = microtime(true); |
|
183 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> requires no further processing, status now is <'.$jprops['jqstat'].'>', 3); |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | // Set queue update flag. |
187 | 187 | $update = true; |
188 | 188 | |
189 | - } elseif ( 'JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat'] ) { |
|
189 | + } elseif ('JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat']) { |
|
190 | 190 | // Process QUEUED and ONGOING jobs |
191 | 191 | // Log the pending job. |
192 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found PENDING job with local ID <' . $jprops['ljid'] . '>, continuing its queue processing', 3 ); |
|
192 | + autoptimizeCriticalCSSCore::ao_ccss_log('Found PENDING job with local ID <'.$jprops['ljid'].'>, continuing its queue processing', 3); |
|
193 | 193 | |
194 | 194 | // If this is not the first job, wait before process next job due criticalcss.com API limits. |
195 | - if ( $jr > 1 ) { |
|
196 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Waiting ' . AO_CCSS_SLEEP . ' seconds due to criticalcss.com API limits', 3 ); |
|
197 | - sleep( AO_CCSS_SLEEP ); |
|
195 | + if ($jr > 1) { |
|
196 | + autoptimizeCriticalCSSCore::ao_ccss_log('Waiting '.AO_CCSS_SLEEP.' seconds due to criticalcss.com API limits', 3); |
|
197 | + sleep(AO_CCSS_SLEEP); |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | // Dispatch the job result request and increment request count. |
201 | - $apireq = $this->ao_ccss_api_results( $jprops['jid'], $queue_debug, $qdobj['htcode'] ); |
|
201 | + $apireq = $this->ao_ccss_api_results($jprops['jid'], $queue_debug, $qdobj['htcode']); |
|
202 | 202 | $jr++; |
203 | 203 | |
204 | 204 | // NOTE: All the following condigitons maps to the ones in admin_settings_queue.js.php |
205 | 205 | // Replace API response values if queue debugging is enabled and some value is set. |
206 | - if ( $queue_debug ) { |
|
207 | - if ( $qdobj['status'] ) { |
|
206 | + if ($queue_debug) { |
|
207 | + if ($qdobj['status']) { |
|
208 | 208 | $apireq['status'] = $qdobj['status']; |
209 | 209 | } |
210 | - if ( $qdobj['resultStatus'] ) { |
|
210 | + if ($qdobj['resultStatus']) { |
|
211 | 211 | $apireq['resultStatus'] = $qdobj['resultStatus']; |
212 | 212 | } |
213 | - if ( $qdobj['validationStatus'] ) { |
|
213 | + if ($qdobj['validationStatus']) { |
|
214 | 214 | $apireq['validationStatus'] = $qdobj['validationStatus']; |
215 | 215 | } |
216 | 216 | } |
217 | 217 | |
218 | - if ( 'JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status'] ) { |
|
218 | + if ('JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status']) { |
|
219 | 219 | // SUCCESS: request has a valid result |
220 | 220 | // Process a PENDING job |
221 | 221 | // Update job properties. |
222 | 222 | $jprops['jqstat'] = $apireq['status']; |
223 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '> unchanged', 3 ); |
|
224 | - } elseif ( 'JOB_DONE' == $apireq['status'] ) { |
|
223 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].'> unchanged', 3); |
|
224 | + } elseif ('JOB_DONE' == $apireq['status']) { |
|
225 | 225 | // Process a DONE job |
226 | 226 | // New resultStatus from ccss.com "HTML_404", consider as "GOOD" for now. |
227 | - if ( 'HTML_404' == $apireq['resultStatus'] ) { |
|
227 | + if ('HTML_404' == $apireq['resultStatus']) { |
|
228 | 228 | $apireq['resultStatus'] = 'GOOD'; |
229 | 229 | } |
230 | 230 | |
231 | - if ( 'GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus'] ) { |
|
231 | + if ('GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus']) { |
|
232 | 232 | // SUCCESS: GOOD job with GOOD validation |
233 | 233 | // Update job properties. |
234 | - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, false ); |
|
234 | + $jprops['file'] = $this->ao_ccss_save_file($apireq['css'], $trule, false); |
|
235 | 235 | $jprops['jqstat'] = $apireq['status']; |
236 | 236 | $jprops['jrstat'] = $apireq['resultStatus']; |
237 | 237 | $jprops['jvstat'] = $apireq['validationStatus']; |
238 | - $jprops['jftime'] = microtime( true ); |
|
238 | + $jprops['jftime'] = microtime(true); |
|
239 | 239 | $rule_update = true; |
240 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '>, file saved <' . $jprops['file'] . '>', 3 ); |
|
241 | - } elseif ( 'GOOD' == $apireq['resultStatus'] && ( 'WARN' == $apireq['validationStatus'] || 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'] ) ) { |
|
240 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].'>, file saved <'.$jprops['file'].'>', 3); |
|
241 | + } elseif ('GOOD' == $apireq['resultStatus'] && ('WARN' == $apireq['validationStatus'] || 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'])) { |
|
242 | 242 | // SUCCESS: GOOD job with WARN or BAD validation |
243 | 243 | // Update job properties. |
244 | - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, true ); |
|
244 | + $jprops['file'] = $this->ao_ccss_save_file($apireq['css'], $trule, true); |
|
245 | 245 | $jprops['jqstat'] = $apireq['status']; |
246 | 246 | $jprops['jrstat'] = $apireq['resultStatus']; |
247 | 247 | $jprops['jvstat'] = $apireq['validationStatus']; |
248 | - $jprops['jftime'] = microtime( true ); |
|
248 | + $jprops['jftime'] = microtime(true); |
|
249 | 249 | $rule_update = true; |
250 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . ', file saved <' . $jprops['file'] . '> but requires REVIEW', 3 ); |
|
251 | - } elseif ( 'GOOD' != $apireq['resultStatus'] && ( 'GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'] ) ) { |
|
250 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].', file saved <'.$jprops['file'].'> but requires REVIEW', 3); |
|
251 | + } elseif ('GOOD' != $apireq['resultStatus'] && ('GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'])) { |
|
252 | 252 | // ERROR: no GOOD, WARN or BAD results |
253 | 253 | // Update job properties. |
254 | 254 | $jprops['jqstat'] = $apireq['status']; |
255 | 255 | $jprops['jrstat'] = $apireq['resultStatus']; |
256 | 256 | $jprops['jvstat'] = $apireq['validationStatus']; |
257 | - $jprops['jftime'] = microtime( true ); |
|
258 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
257 | + $jprops['jftime'] = microtime(true); |
|
258 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
259 | 259 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
260 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
260 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
261 | 261 | } else { |
262 | 262 | // UNKNOWN: unhandled JOB_DONE exception |
263 | 263 | // Update job properties. |
264 | 264 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
265 | 265 | $jprops['jrstat'] = $apireq['resultStatus']; |
266 | 266 | $jprops['jvstat'] = $apireq['validationStatus']; |
267 | - $jprops['jftime'] = microtime( true ); |
|
268 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job is UNKNOWN, status now is <' . $jprops['jqstat'] . '>', 2 ); |
|
267 | + $jprops['jftime'] = microtime(true); |
|
268 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job is UNKNOWN, status now is <'.$jprops['jqstat'].'>', 2); |
|
269 | 269 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
270 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
270 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
271 | 271 | } |
272 | - } elseif ( 'JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
272 | + } elseif ('JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status']) { |
|
273 | 273 | // ERROR: failed job |
274 | 274 | // Update job properties. |
275 | 275 | $jprops['jqstat'] = $apireq['job']['status']; |
276 | - if ( $apireq['job']['error'] ) { |
|
276 | + if ($apireq['job']['error']) { |
|
277 | 277 | $jprops['jrstat'] = $apireq['job']['error']; |
278 | 278 | } else { |
279 | 279 | $jprops['jrstat'] = 'Baby did a bad bad thing'; |
280 | 280 | } |
281 | 281 | $jprops['jvstat'] = 'NONE'; |
282 | - $jprops['jftime'] = microtime( true ); |
|
283 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
284 | - } elseif ( 'This css no longer exists. Please re-generate it.' == $apireq['error'] ) { |
|
282 | + $jprops['jftime'] = microtime(true); |
|
283 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
284 | + } elseif ('This css no longer exists. Please re-generate it.' == $apireq['error']) { |
|
285 | 285 | // ERROR: CSS doesn't exist |
286 | 286 | // Update job properties. |
287 | 287 | $jprops['jqstat'] = 'NO_CSS'; |
288 | 288 | $jprops['jrstat'] = $apireq['error']; |
289 | 289 | $jprops['jvstat'] = 'NONE'; |
290 | - $jprops['jftime'] = microtime( true ); |
|
291 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
292 | - } elseif ( empty( $apireq ) ) { |
|
290 | + $jprops['jftime'] = microtime(true); |
|
291 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
292 | + } elseif (empty($apireq)) { |
|
293 | 293 | // ERROR: no response |
294 | 294 | // Update job properties. |
295 | 295 | $jprops['jqstat'] = 'NO_RESPONSE'; |
296 | 296 | $jprops['jrstat'] = 'NONE'; |
297 | 297 | $jprops['jvstat'] = 'NONE'; |
298 | - $jprops['jftime'] = microtime( true ); |
|
299 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
298 | + $jprops['jftime'] = microtime(true); |
|
299 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> request has no response, status now is <'.$jprops['jqstat'].'>', 3); |
|
300 | 300 | } else { |
301 | 301 | // UNKNOWN: unhandled results exception |
302 | 302 | // Update job properties. |
303 | 303 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
304 | 304 | $jprops['jrstat'] = 'NONE'; |
305 | 305 | $jprops['jvstat'] = 'NONE'; |
306 | - $jprops['jftime'] = microtime( true ); |
|
307 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 ); |
|
306 | + $jprops['jftime'] = microtime(true); |
|
307 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request has an UNKNOWN condition, status now is <'.$jprops['jqstat'].'>, check log messages above for more information', 2); |
|
308 | 308 | } |
309 | 309 | |
310 | 310 | // Set queue update flag. |
@@ -312,40 +312,40 @@ discard block |
||
312 | 312 | } |
313 | 313 | |
314 | 314 | // Mark DONE jobs for removal. |
315 | - if ( 'JOB_DONE' == $jprops['jqstat'] ) { |
|
315 | + if ('JOB_DONE' == $jprops['jqstat']) { |
|
316 | 316 | $update = true; |
317 | 317 | $deljob = true; |
318 | 318 | } |
319 | 319 | |
320 | 320 | // Persist updated queue object. |
321 | - if ( $update ) { |
|
322 | - if ( ! $deljob ) { |
|
321 | + if ($update) { |
|
322 | + if (!$deljob) { |
|
323 | 323 | // Update properties of a NEW or PENDING job... |
324 | - $ao_ccss_queue[ $path ] = $jprops; |
|
324 | + $ao_ccss_queue[$path] = $jprops; |
|
325 | 325 | } else { |
326 | 326 | // ...or remove the DONE job. |
327 | - unset( $ao_ccss_queue[ $path ] ); |
|
328 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> is DONE and was removed from the queue', 3 ); |
|
327 | + unset($ao_ccss_queue[$path]); |
|
328 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> is DONE and was removed from the queue', 3); |
|
329 | 329 | } |
330 | 330 | |
331 | 331 | // Update queue object. |
332 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
333 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
334 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue updated by job id <' . $jprops['ljid'] . '>', 3 ); |
|
332 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
333 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
334 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue updated by job id <'.$jprops['ljid'].'>', 3); |
|
335 | 335 | |
336 | 336 | // Update target rule. |
337 | - if ( $rule_update ) { |
|
338 | - $this->ao_ccss_rule_update( $jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash'] ); |
|
339 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> updated the target rule <' . $jprops['rtarget'] . '>', 3 ); |
|
337 | + if ($rule_update) { |
|
338 | + $this->ao_ccss_rule_update($jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash']); |
|
339 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> updated the target rule <'.$jprops['rtarget'].'>', 3); |
|
340 | 340 | } |
341 | 341 | } else { |
342 | 342 | // Or log no queue action. |
343 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Nothing to do on this job', 3 ); |
|
343 | + autoptimizeCriticalCSSCore::ao_ccss_log('Nothing to do on this job', 3); |
|
344 | 344 | } |
345 | 345 | |
346 | 346 | // Break the loop if request limit is set and was reached. |
347 | - if ( $ao_ccss_rlimit && $ao_ccss_rlimit == $jr ) { |
|
348 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'The limit of ' . $ao_ccss_rlimit . ' request(s) to criticalcss.com was reached, queue control must finish now', 3 ); |
|
347 | + if ($ao_ccss_rlimit && $ao_ccss_rlimit == $jr) { |
|
348 | + autoptimizeCriticalCSSCore::ao_ccss_log('The limit of '.$ao_ccss_rlimit.' request(s) to criticalcss.com was reached, queue control must finish now', 3); |
|
349 | 349 | break; |
350 | 350 | } |
351 | 351 | |
@@ -354,46 +354,46 @@ discard block |
||
354 | 354 | } |
355 | 355 | |
356 | 356 | // Remove the lock file and log the queue end. |
357 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
358 | - unlink( AO_CCSS_LOCK ); |
|
359 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control unlocked', 3 ); |
|
357 | + if (file_exists(AO_CCSS_LOCK)) { |
|
358 | + unlink(AO_CCSS_LOCK); |
|
359 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control unlocked', 3); |
|
360 | 360 | } |
361 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control finished', 3 ); |
|
361 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control finished', 3); |
|
362 | 362 | |
363 | 363 | // Log that queue is locked. |
364 | 364 | } else { |
365 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue is already running, skipping the attempt to run it again', 3 ); |
|
365 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue is already running, skipping the attempt to run it again', 3); |
|
366 | 366 | } |
367 | 367 | } |
368 | 368 | |
369 | - public function ao_ccss_diff_hashes( $ljid, $hash, $hashes, $rule ) { |
|
369 | + public function ao_ccss_diff_hashes($ljid, $hash, $hashes, $rule) { |
|
370 | 370 | // Compare job hashes |
371 | 371 | // STEP 1: update job hashes. |
372 | - if ( 1 == count( $hashes ) ) { |
|
372 | + if (1 == count($hashes)) { |
|
373 | 373 | // Job with a single hash |
374 | 374 | // Set job hash. |
375 | 375 | $hash = $hashes[0]; |
376 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with SINGLE hash <' . $hash . '>', 3 ); |
|
376 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> updated with SINGLE hash <'.$hash.'>', 3); |
|
377 | 377 | } else { |
378 | 378 | // Job with multiple hashes |
379 | 379 | // Loop through hashes to concatenate them. |
380 | 380 | $nhash = ''; |
381 | - foreach ( $hashes as $shash ) { |
|
381 | + foreach ($hashes as $shash) { |
|
382 | 382 | $nhash .= $shash; |
383 | 383 | } |
384 | 384 | |
385 | 385 | // Set job hash. |
386 | - $hash = md5( $nhash ); |
|
387 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with a COMPOSITE hash <' . $hash . '>', 3 ); |
|
386 | + $hash = md5($nhash); |
|
387 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> updated with a COMPOSITE hash <'.$hash.'>', 3); |
|
388 | 388 | } |
389 | 389 | |
390 | 390 | // STEP 2: compare job to existing jobs to prevent double submission for same type+hash. |
391 | 391 | global $ao_ccss_queue; |
392 | 392 | |
393 | - foreach ( $ao_ccss_queue as $queue_item ) { |
|
394 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Comparing <' . $rule . $hash . '> with <' . $queue_item['rtarget'] . $queue_item['hash'] . '>', 3 ); |
|
395 | - if ( $queue_item['hash'] == $hash && $queue_item['rtarget'] == $rule && in_array( $queue_item['jqstat'], array( 'JOB_QUEUED', 'JOB_ONGOING', 'JOB_DONE' ) ) ) { |
|
396 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> matches the already pending job <' . $queue_item['ljid'] . '>', 3 ); |
|
393 | + foreach ($ao_ccss_queue as $queue_item) { |
|
394 | + autoptimizeCriticalCSSCore::ao_ccss_log('Comparing <'.$rule.$hash.'> with <'.$queue_item['rtarget'].$queue_item['hash'].'>', 3); |
|
395 | + if ($queue_item['hash'] == $hash && $queue_item['rtarget'] == $rule && in_array($queue_item['jqstat'], array('JOB_QUEUED', 'JOB_ONGOING', 'JOB_DONE'))) { |
|
396 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> matches the already pending job <'.$queue_item['ljid'].'>', 3); |
|
397 | 397 | return false; |
398 | 398 | } |
399 | 399 | } |
@@ -403,32 +403,32 @@ discard block |
||
403 | 403 | global $ao_ccss_rules; |
404 | 404 | |
405 | 405 | // Prepare rule variables. |
406 | - $trule = explode( '|', $rule ); |
|
407 | - $srule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ]; |
|
406 | + $trule = explode('|', $rule); |
|
407 | + $srule = $ao_ccss_rules[$trule[0]][$trule[1]]; |
|
408 | 408 | |
409 | 409 | // Check if a MANUAL rule exist and return false. |
410 | - if ( ! empty( $srule ) && ( 0 == $srule['hash'] && 0 != $srule['file'] ) ) { |
|
411 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> matches the MANUAL rule <' . $trule[0] . '|' . $trule[1] . '>', 3 ); |
|
410 | + if (!empty($srule) && (0 == $srule['hash'] && 0 != $srule['file'])) { |
|
411 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> matches the MANUAL rule <'.$trule[0].'|'.$trule[1].'>', 3); |
|
412 | 412 | return false; |
413 | - } elseif ( ! empty( $srule ) ) { |
|
413 | + } elseif (!empty($srule)) { |
|
414 | 414 | // Check if an AUTO rule exist. |
415 | - if ( $hash === $srule['hash'] && is_file( AO_CCSS_DIR . $srule['file'] ) && 0 != filesize( AO_CCSS_DIR . $srule['file'] ) ) { |
|
415 | + if ($hash === $srule['hash'] && is_file(AO_CCSS_DIR.$srule['file']) && 0 != filesize(AO_CCSS_DIR.$srule['file'])) { |
|
416 | 416 | // Check if job hash matches rule, if the CCSS file exists said file is not empty and return FALSE is so. |
417 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> MATCH the one in rule <' . $trule[0] . '|' . $trule[1] . '>', 3 ); |
|
417 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> MATCH the one in rule <'.$trule[0].'|'.$trule[1].'>', 3); |
|
418 | 418 | return false; |
419 | 419 | } else { |
420 | 420 | // Or return the new hash if they differ. |
421 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> DOES NOT MATCH the one in rule <' . $trule[0] . '|' . $trule[1] . '> or rule\'s CCSS file was invalid.', 3 ); |
|
421 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> DOES NOT MATCH the one in rule <'.$trule[0].'|'.$trule[1].'> or rule\'s CCSS file was invalid.', 3); |
|
422 | 422 | return $hash; |
423 | 423 | } |
424 | 424 | } else { |
425 | 425 | // Or just return the hash if no rule exist yet. |
426 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> has no rule yet', 3 ); |
|
426 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> has no rule yet', 3); |
|
427 | 427 | return $hash; |
428 | 428 | } |
429 | 429 | } |
430 | 430 | |
431 | - public function ao_ccss_api_generate( $path, $debug, $dcode ) { |
|
431 | + public function ao_ccss_api_generate($path, $debug, $dcode) { |
|
432 | 432 | // POST jobs to criticalcss.com and return responses |
433 | 433 | // Get key and key status. |
434 | 434 | global $ao_ccss_key; |
@@ -440,35 +440,35 @@ discard block |
||
440 | 440 | global $ao_ccss_noptimize; |
441 | 441 | |
442 | 442 | $site_host = get_site_url(); |
443 | - $site_path = parse_url( $site_host, PHP_URL_PATH ); |
|
443 | + $site_path = parse_url($site_host, PHP_URL_PATH); |
|
444 | 444 | |
445 | - if ( ! empty( $site_path ) ) { |
|
446 | - $site_host = str_replace( $site_path, '', $site_host ); |
|
445 | + if (!empty($site_path)) { |
|
446 | + $site_host = str_replace($site_path, '', $site_host); |
|
447 | 447 | } |
448 | 448 | |
449 | 449 | // Logic to bind to one domain to avoid site clones of sites would |
450 | 450 | // automatically begin spawning requests to criticalcss.com which has |
451 | 451 | // a per domain cost. |
452 | 452 | global $ao_ccss_domain; |
453 | - if ( empty( $ao_ccss_domain ) ) { |
|
453 | + if (empty($ao_ccss_domain)) { |
|
454 | 454 | // first request being done, update option to allow future requests are only allowed if from same domain. |
455 | - update_option( 'autoptimize_ccss_domain', str_rot13( $site_host ) ); |
|
456 | - } elseif ( trim( $ao_ccss_domain, '\'"' ) !== 'none' && parse_url( $site_host, PHP_URL_HOST ) !== parse_url( $ao_ccss_domain, PHP_URL_HOST ) && apply_filters( 'autoptimize_filter_ccss_bind_domain', true ) ) { |
|
455 | + update_option('autoptimize_ccss_domain', str_rot13($site_host)); |
|
456 | + } elseif (trim($ao_ccss_domain, '\'"') !== 'none' && parse_url($site_host, PHP_URL_HOST) !== parse_url($ao_ccss_domain, PHP_URL_HOST) && apply_filters('autoptimize_filter_ccss_bind_domain', true)) { |
|
457 | 457 | // not the same domain, log as error and return without posting to criticalcss.com. |
458 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Request for domain ' . $site_host . ' does not match bound domain ' . $ao_ccss_domain . ' so not proceeding.', 2 ); |
|
458 | + autoptimizeCriticalCSSCore::ao_ccss_log('Request for domain '.$site_host.' does not match bound domain '.$ao_ccss_domain.' so not proceeding.', 2); |
|
459 | 459 | return false; |
460 | 460 | } |
461 | 461 | |
462 | - $src_url = $site_host . $path; |
|
462 | + $src_url = $site_host.$path; |
|
463 | 463 | |
464 | 464 | // Avoid AO optimizations if required by config or avoid lazyload if lazyload is active in AO. |
465 | - if ( ! empty( $ao_ccss_noptimize ) ) { |
|
465 | + if (!empty($ao_ccss_noptimize)) { |
|
466 | 466 | $src_url .= '?ao_noptirocket=1'; |
467 | - } elseif ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) { |
|
467 | + } elseif (class_exists('autoptimizeImages', false) && autoptimizeImages::should_lazyload_wrapper()) { |
|
468 | 468 | $src_url .= '?ao_nolazy=1'; |
469 | 469 | } |
470 | 470 | |
471 | - $src_url = apply_filters( 'autoptimize_filter_ccss_cron_srcurl', $src_url ); |
|
471 | + $src_url = apply_filters('autoptimize_filter_ccss_cron_srcurl', $src_url); |
|
472 | 472 | |
473 | 473 | // Initialize request body. |
474 | 474 | $body = array(); |
@@ -478,88 +478,88 @@ discard block |
||
478 | 478 | |
479 | 479 | // Prepare and add viewport size to the body if available. |
480 | 480 | $viewport = autoptimizeCriticalCSSCore::ao_ccss_viewport(); |
481 | - if ( ! empty( $viewport['w'] ) && ! empty( $viewport['h'] ) ) { |
|
481 | + if (!empty($viewport['w']) && !empty($viewport['h'])) { |
|
482 | 482 | $body['width'] = $viewport['w']; |
483 | 483 | $body['height'] = $viewport['h']; |
484 | 484 | } |
485 | 485 | |
486 | 486 | // Prepare and add forceInclude to the body if available. |
487 | 487 | global $ao_ccss_finclude; |
488 | - $finclude = $this->ao_ccss_finclude( $ao_ccss_finclude ); |
|
489 | - if ( ! empty( $finclude ) ) { |
|
488 | + $finclude = $this->ao_ccss_finclude($ao_ccss_finclude); |
|
489 | + if (!empty($finclude)) { |
|
490 | 490 | $body['forceInclude'] = $finclude; |
491 | 491 | } |
492 | 492 | |
493 | 493 | // Add filter to allow the body array to be altered (e.g. to add customPageHeaders). |
494 | - $body = apply_filters( 'autoptimize_ccss_cron_api_generate_body', $body ); |
|
494 | + $body = apply_filters('autoptimize_ccss_cron_api_generate_body', $body); |
|
495 | 495 | |
496 | 496 | // Body must be json and log it. |
497 | - $body = json_encode( $body ); |
|
498 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request body is ' . $body, 3 ); |
|
497 | + $body = json_encode($body); |
|
498 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request body is '.$body, 3); |
|
499 | 499 | |
500 | 500 | // Prepare the request. |
501 | - $url = esc_url_raw( AO_CCSS_API . 'generate?aover=' . AO_CCSS_VER ); |
|
501 | + $url = esc_url_raw(AO_CCSS_API.'generate?aover='.AO_CCSS_VER); |
|
502 | 502 | $args = array( |
503 | 503 | 'headers' => array( |
504 | - 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER, |
|
504 | + 'User-Agent' => 'Autoptimize v'.AO_CCSS_VER, |
|
505 | 505 | 'Content-type' => 'application/json; charset=utf-8', |
506 | - 'Authorization' => 'JWT ' . $key, |
|
506 | + 'Authorization' => 'JWT '.$key, |
|
507 | 507 | 'Connection' => 'close', |
508 | 508 | ), |
509 | 509 | 'body' => $body, |
510 | 510 | ); |
511 | 511 | |
512 | 512 | // Dispatch the request and store its response code. |
513 | - $req = wp_safe_remote_post( $url, $args ); |
|
514 | - $code = wp_remote_retrieve_response_code( $req ); |
|
515 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
513 | + $req = wp_safe_remote_post($url, $args); |
|
514 | + $code = wp_remote_retrieve_response_code($req); |
|
515 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
516 | 516 | |
517 | - if ( $debug && $dcode ) { |
|
517 | + if ($debug && $dcode) { |
|
518 | 518 | // If queue debug is active, change response code. |
519 | 519 | $code = $dcode; |
520 | 520 | } |
521 | 521 | |
522 | - if ( 200 == $code ) { |
|
522 | + if (200 == $code) { |
|
523 | 523 | // Response code is OK. |
524 | 524 | // Workaround criticalcss.com non-RESTful reponses. |
525 | - if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status'] ) { |
|
525 | + if ('JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status']) { |
|
526 | 526 | // Log successful and return encoded request body. |
527 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied successfully', 3 ); |
|
527 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied successfully', 3); |
|
528 | 528 | |
529 | 529 | // This code also means the key is valid, so cache key status for 24h if not already cached. |
530 | - if ( ( ! $key_status || 2 != $key_status ) && $key ) { |
|
531 | - update_option( 'autoptimize_ccss_keyst', 2 ); |
|
532 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 ); |
|
530 | + if ((!$key_status || 2 != $key_status) && $key) { |
|
531 | + update_option('autoptimize_ccss_keyst', 2); |
|
532 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is valid, updating key status', 3); |
|
533 | 533 | } |
534 | 534 | |
535 | 535 | // Return the request body. |
536 | 536 | return $body; |
537 | 537 | } else { |
538 | 538 | // Log successful requests with invalid reponses. |
539 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with code <' . $code . '> and an UNKNOWN error condition, body follows...', 2 ); |
|
540 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
539 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied with code <'.$code.'> and an UNKNOWN error condition, body follows...', 2); |
|
540 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
541 | 541 | return $body; |
542 | 542 | } |
543 | 543 | } else { |
544 | 544 | // Response code is anything else. |
545 | 545 | // Log failed request with a valid response code and return body. |
546 | - if ( $code ) { |
|
547 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with error code <' . $code . '>, body follows...', 2 ); |
|
548 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
546 | + if ($code) { |
|
547 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied with error code <'.$code.'>, body follows...', 2); |
|
548 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
549 | 549 | |
550 | - if ( 401 == $code ) { |
|
550 | + if (401 == $code) { |
|
551 | 551 | // If request is unauthorized, also clear key status. |
552 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
553 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
552 | + update_option('autoptimize_ccss_keyst', 1); |
|
553 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
554 | 554 | } |
555 | 555 | |
556 | 556 | // Return the request body. |
557 | 557 | return $body; |
558 | 558 | } else { |
559 | 559 | // Log failed request with no response and return false. |
560 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> has no response, this could be a service timeout', 2 ); |
|
561 | - if ( is_wp_error( $req ) ) { |
|
562 | - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 ); |
|
560 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> has no response, this could be a service timeout', 2); |
|
561 | + if (is_wp_error($req)) { |
|
562 | + autoptimizeCriticalCSSCore::ao_ccss_log($req->get_error_message(), 2); |
|
563 | 563 | } |
564 | 564 | |
565 | 565 | return false; |
@@ -567,76 +567,76 @@ discard block |
||
567 | 567 | } |
568 | 568 | } |
569 | 569 | |
570 | - public function ao_ccss_api_results( $jobid, $debug, $dcode ) { |
|
570 | + public function ao_ccss_api_results($jobid, $debug, $dcode) { |
|
571 | 571 | // GET jobs from criticalcss.com and return responses |
572 | 572 | // Get key. |
573 | 573 | global $ao_ccss_key; |
574 | 574 | $key = $ao_ccss_key; |
575 | 575 | |
576 | 576 | // Prepare the request. |
577 | - $url = AO_CCSS_API . 'results?resultId=' . $jobid; |
|
577 | + $url = AO_CCSS_API.'results?resultId='.$jobid; |
|
578 | 578 | $args = array( |
579 | 579 | 'headers' => array( |
580 | - 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v' . AO_CCSS_VER, |
|
581 | - 'Authorization' => 'JWT ' . $key, |
|
580 | + 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v'.AO_CCSS_VER, |
|
581 | + 'Authorization' => 'JWT '.$key, |
|
582 | 582 | 'Connection' => 'close', |
583 | 583 | ), |
584 | 584 | ); |
585 | 585 | |
586 | 586 | // Dispatch the request and store its response code. |
587 | - $req = wp_safe_remote_get( $url, $args ); |
|
588 | - $code = wp_remote_retrieve_response_code( $req ); |
|
589 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
587 | + $req = wp_safe_remote_get($url, $args); |
|
588 | + $code = wp_remote_retrieve_response_code($req); |
|
589 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
590 | 590 | |
591 | - if ( $debug && $dcode ) { |
|
591 | + if ($debug && $dcode) { |
|
592 | 592 | // If queue debug is active, change response code. |
593 | 593 | $code = $dcode; |
594 | 594 | } |
595 | 595 | |
596 | - if ( 200 == $code ) { |
|
596 | + if (200 == $code) { |
|
597 | 597 | // Response code is OK. |
598 | - if ( is_array( $body ) && ( array_key_exists( 'status', $body ) || array_key_exists( 'job', $body ) ) && ( 'JOB_QUEUED' == $body['status'] || 'JOB_ONGOING' == $body['status'] || 'JOB_DONE' == $body['status'] || 'JOB_FAILED' == $body['status'] || 'JOB_UNKNOWN' == $body['status'] || 'STATUS_JOB_BAD' == $body['job']['status'] ) ) { |
|
598 | + if (is_array($body) && (array_key_exists('status', $body) || array_key_exists('job', $body)) && ('JOB_QUEUED' == $body['status'] || 'JOB_ONGOING' == $body['status'] || 'JOB_DONE' == $body['status'] || 'JOB_FAILED' == $body['status'] || 'JOB_UNKNOWN' == $body['status'] || 'STATUS_JOB_BAD' == $body['job']['status'])) { |
|
599 | 599 | // Workaround criticalcss.com non-RESTful reponses |
600 | 600 | // Log successful and return encoded request body. |
601 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully', 3 ); |
|
601 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied successfully', 3); |
|
602 | 602 | return $body; |
603 | - } elseif ( is_array( $body ) && ( array_key_exists( 'error', $body ) && 'This css no longer exists. Please re-generate it.' == $body['error'] ) ) { |
|
603 | + } elseif (is_array($body) && (array_key_exists('error', $body) && 'This css no longer exists. Please re-generate it.' == $body['error'])) { |
|
604 | 604 | // Handle no CSS reply |
605 | 605 | // Log no CSS error and return encoded request body. |
606 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully but the CSS for it does not exist anymore', 3 ); |
|
606 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied successfully but the CSS for it does not exist anymore', 3); |
|
607 | 607 | return $body; |
608 | 608 | } else { |
609 | 609 | // Log failed request and return false. |
610 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with code <' . $code . '> and an UNKNOWN error condition, body follows...', 2 ); |
|
611 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
610 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied with code <'.$code.'> and an UNKNOWN error condition, body follows...', 2); |
|
611 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
612 | 612 | return false; |
613 | 613 | } |
614 | 614 | } else { |
615 | 615 | // Response code is anything else |
616 | 616 | // Log failed request with a valid response code and return body. |
617 | - if ( $code ) { |
|
618 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with error code <' . $code . '>, body follows...', 2 ); |
|
619 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
620 | - if ( 401 == $code ) { |
|
617 | + if ($code) { |
|
618 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied with error code <'.$code.'>, body follows...', 2); |
|
619 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
620 | + if (401 == $code) { |
|
621 | 621 | // If request is unauthorized, also clear key status. |
622 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
623 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
622 | + update_option('autoptimize_ccss_keyst', 1); |
|
623 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
624 | 624 | } |
625 | 625 | |
626 | 626 | // Return the request body. |
627 | 627 | return $body; |
628 | 628 | } else { |
629 | 629 | // Log failed request with no response and return false. |
630 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> has no response, this could be a service timeout', 2 ); |
|
630 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> has no response, this could be a service timeout', 2); |
|
631 | 631 | return false; |
632 | 632 | } |
633 | 633 | } |
634 | 634 | } |
635 | 635 | |
636 | - public function ao_ccss_save_file( $ccss, $target, $review ) { |
|
636 | + public function ao_ccss_save_file($ccss, $target, $review) { |
|
637 | 637 | // Save critical CSS into the filesystem and return its filename |
638 | 638 | // Prepare review mark. |
639 | - if ( $review ) { |
|
639 | + if ($review) { |
|
640 | 640 | $rmark = '_R'; |
641 | 641 | } else { |
642 | 642 | $rmark = ''; |
@@ -646,21 +646,21 @@ discard block |
||
646 | 646 | $filename = false; |
647 | 647 | $content = $ccss; |
648 | 648 | |
649 | - if ( autoptimizeCriticalCSSCore::ao_ccss_check_contents( $content ) ) { |
|
649 | + if (autoptimizeCriticalCSSCore::ao_ccss_check_contents($content)) { |
|
650 | 650 | // Sanitize content, set filename and try to save file. |
651 | - $file = AO_CCSS_DIR . 'ccss_' . md5( $ccss . $target[1] ) . $rmark . '.css'; |
|
652 | - $status = file_put_contents( $file, $content, LOCK_EX ); |
|
653 | - $filename = pathinfo( $file, PATHINFO_BASENAME ); |
|
654 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS file for the rule <' . $target[0] . '|' . $target[1] . '> was saved as <' . $filename . '>, size in bytes is <' . $status . '>', 3 ); |
|
651 | + $file = AO_CCSS_DIR.'ccss_'.md5($ccss.$target[1]).$rmark.'.css'; |
|
652 | + $status = file_put_contents($file, $content, LOCK_EX); |
|
653 | + $filename = pathinfo($file, PATHINFO_BASENAME); |
|
654 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS file for the rule <'.$target[0].'|'.$target[1].'> was saved as <'.$filename.'>, size in bytes is <'.$status.'>', 3); |
|
655 | 655 | |
656 | - if ( ! $status ) { |
|
656 | + if (!$status) { |
|
657 | 657 | // If file has not been saved, reset filename. |
658 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS file <' . $filename . '> could not be not saved', 2 ); |
|
658 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS file <'.$filename.'> could not be not saved', 2); |
|
659 | 659 | $filename = false; |
660 | 660 | return $filename; |
661 | 661 | } |
662 | 662 | } else { |
663 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received did not pass content check', 2 ); |
|
663 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received did not pass content check', 2); |
|
664 | 664 | return $filename; |
665 | 665 | } |
666 | 666 | |
@@ -670,15 +670,15 @@ discard block |
||
670 | 670 | global $ao_ccss_rules; |
671 | 671 | |
672 | 672 | // Prepare rule variables. |
673 | - $srule = $ao_ccss_rules[ $target[0] ][ $target[1] ]; |
|
673 | + $srule = $ao_ccss_rules[$target[0]][$target[1]]; |
|
674 | 674 | $oldfile = $srule['file']; |
675 | 675 | |
676 | - if ( $oldfile && $oldfile !== $filename ) { |
|
677 | - $delfile = AO_CCSS_DIR . $oldfile; |
|
678 | - if ( file_exists( $delfile ) ) { |
|
679 | - $unlinkst = unlink( $delfile ); |
|
680 | - if ( $unlinkst ) { |
|
681 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'A previous critical CSS file <' . $oldfile . '> was removed for the rule <' . $target[0] . '|' . $target[1] . '>', 3 ); |
|
676 | + if ($oldfile && $oldfile !== $filename) { |
|
677 | + $delfile = AO_CCSS_DIR.$oldfile; |
|
678 | + if (file_exists($delfile)) { |
|
679 | + $unlinkst = unlink($delfile); |
|
680 | + if ($unlinkst) { |
|
681 | + autoptimizeCriticalCSSCore::ao_ccss_log('A previous critical CSS file <'.$oldfile.'> was removed for the rule <'.$target[0].'|'.$target[1].'>', 3); |
|
682 | 682 | } |
683 | 683 | } |
684 | 684 | } |
@@ -687,29 +687,29 @@ discard block |
||
687 | 687 | return $filename; |
688 | 688 | } |
689 | 689 | |
690 | - public function ao_ccss_rule_update( $ljid, $srule, $file, $hash ) { |
|
690 | + public function ao_ccss_rule_update($ljid, $srule, $file, $hash) { |
|
691 | 691 | // Update or create a rule |
692 | 692 | // Attach required arrays. |
693 | 693 | global $ao_ccss_rules; |
694 | 694 | |
695 | 695 | // Prepare rule variables. |
696 | - $trule = explode( '|', $srule ); |
|
697 | - $rule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ]; |
|
696 | + $trule = explode('|', $srule); |
|
697 | + $rule = $ao_ccss_rules[$trule[0]][$trule[1]]; |
|
698 | 698 | $action = false; |
699 | 699 | $rtype = ''; |
700 | 700 | |
701 | - if ( 0 === $rule['hash'] && 0 !== $rule['file'] ) { |
|
701 | + if (0 === $rule['hash'] && 0 !== $rule['file']) { |
|
702 | 702 | // manual rule, don't ever overwrite. |
703 | 703 | $action = 'NOT UPDATED'; |
704 | 704 | $rtype = 'MANUAL'; |
705 | - } elseif ( 0 === $rule['hash'] && 0 === $rule['file'] ) { |
|
705 | + } elseif (0 === $rule['hash'] && 0 === $rule['file']) { |
|
706 | 706 | // If this is an user created AUTO rule with no hash and file yet, update its hash and filename |
707 | 707 | // Set rule hash, file and action flag. |
708 | 708 | $rule['hash'] = $hash; |
709 | 709 | $rule['file'] = $file; |
710 | 710 | $action = 'UPDATED'; |
711 | 711 | $rtype = 'AUTO'; |
712 | - } elseif ( 0 !== $rule['hash'] && ctype_alnum( $rule['hash'] ) ) { |
|
712 | + } elseif (0 !== $rule['hash'] && ctype_alnum($rule['hash'])) { |
|
713 | 713 | // If this is an genuine AUTO rule, update its hash and filename |
714 | 714 | // Set rule hash, file and action flag. |
715 | 715 | $rule['hash'] = $hash; |
@@ -719,7 +719,7 @@ discard block |
||
719 | 719 | } else { |
720 | 720 | // If rule doesn't exist, create an AUTO rule |
721 | 721 | // AUTO rules were only for types, but will now also work for paths. |
722 | - if ( 'types' == $trule[0] || 'paths' == $trule[0] ) { |
|
722 | + if ('types' == $trule[0] || 'paths' == $trule[0]) { |
|
723 | 723 | // Set rule hash and file and action flag. |
724 | 724 | $rule['hash'] = $hash; |
725 | 725 | $rule['file'] = $file; |
@@ -727,47 +727,47 @@ discard block |
||
727 | 727 | $rtype = 'AUTO'; |
728 | 728 | } else { |
729 | 729 | // Log that no rule was created. |
730 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Exception, no AUTO rule created', 3 ); |
|
730 | + autoptimizeCriticalCSSCore::ao_ccss_log('Exception, no AUTO rule created', 3); |
|
731 | 731 | } |
732 | 732 | } |
733 | 733 | |
734 | - if ( $action ) { |
|
734 | + if ($action) { |
|
735 | 735 | // If a rule creation/update is required, persist updated rules object. |
736 | - $ao_ccss_rules[ $trule[0] ][ $trule[1] ] = $rule; |
|
737 | - $ao_ccss_rules_raw = json_encode( $ao_ccss_rules ); |
|
738 | - update_option( 'autoptimize_ccss_rules', $ao_ccss_rules_raw ); |
|
739 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Target rule <' . $srule . '> of type <' . $rtype . '> was ' . $action . ' for job id <' . $ljid . '>', 3 ); |
|
736 | + $ao_ccss_rules[$trule[0]][$trule[1]] = $rule; |
|
737 | + $ao_ccss_rules_raw = json_encode($ao_ccss_rules); |
|
738 | + update_option('autoptimize_ccss_rules', $ao_ccss_rules_raw); |
|
739 | + autoptimizeCriticalCSSCore::ao_ccss_log('Target rule <'.$srule.'> of type <'.$rtype.'> was '.$action.' for job id <'.$ljid.'>', 3); |
|
740 | 740 | } else { |
741 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'No rule action required', 3 ); |
|
741 | + autoptimizeCriticalCSSCore::ao_ccss_log('No rule action required', 3); |
|
742 | 742 | } |
743 | 743 | } |
744 | 744 | |
745 | - function ao_ccss_finclude( $finclude_raw ) { |
|
745 | + function ao_ccss_finclude($finclude_raw) { |
|
746 | 746 | // Prepare forceInclude object. |
747 | - if ( ! empty( $finclude_raw ) ) { |
|
747 | + if (!empty($finclude_raw)) { |
|
748 | 748 | // If there are any content |
749 | 749 | // Convert raw string into arra and initialize the returning object. |
750 | - $fincludes = explode( ',', $finclude_raw ); |
|
750 | + $fincludes = explode(',', $finclude_raw); |
|
751 | 751 | $finclude = array(); |
752 | 752 | |
753 | 753 | // Interacts over every rule. |
754 | 754 | $i = 0; |
755 | - foreach ( $fincludes as $include ) { |
|
755 | + foreach ($fincludes as $include) { |
|
756 | 756 | // Trim leading and trailing whitespaces. |
757 | - $include = trim( $include ); |
|
757 | + $include = trim($include); |
|
758 | 758 | |
759 | - if ( substr( $include, 0, 2 ) === '//' ) { |
|
759 | + if (substr($include, 0, 2) === '//') { |
|
760 | 760 | // Regex rule |
761 | 761 | // Format value as required. |
762 | - $include = str_replace( '//', '/', $include ); |
|
763 | - $include = $include . '/i'; |
|
762 | + $include = str_replace('//', '/', $include); |
|
763 | + $include = $include.'/i'; |
|
764 | 764 | |
765 | 765 | // Store regex object. |
766 | - $finclude[ $i ]['type'] = 'RegExp'; |
|
767 | - $finclude[ $i ]['value'] = $include; |
|
766 | + $finclude[$i]['type'] = 'RegExp'; |
|
767 | + $finclude[$i]['value'] = $include; |
|
768 | 768 | } else { |
769 | 769 | // Simple value rule. |
770 | - $finclude[ $i ]['value'] = $include; |
|
770 | + $finclude[$i]['value'] = $include; |
|
771 | 771 | } |
772 | 772 | |
773 | 773 | $i++; |
@@ -784,54 +784,54 @@ discard block |
||
784 | 784 | public function ao_ccss_cleaning() { |
785 | 785 | // Perform plugin maintenance |
786 | 786 | // Truncate log file >= 1MB . |
787 | - if ( file_exists( AO_CCSS_LOG ) ) { |
|
788 | - if ( filesize( AO_CCSS_LOG ) >= 1048576 ) { |
|
789 | - $logfile = fopen( AO_CCSS_LOG, 'w' ); |
|
790 | - fclose( $logfile ); |
|
787 | + if (file_exists(AO_CCSS_LOG)) { |
|
788 | + if (filesize(AO_CCSS_LOG) >= 1048576) { |
|
789 | + $logfile = fopen(AO_CCSS_LOG, 'w'); |
|
790 | + fclose($logfile); |
|
791 | 791 | } |
792 | 792 | } |
793 | 793 | |
794 | 794 | // Remove lock file. |
795 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
796 | - unlink( AO_CCSS_LOCK ); |
|
795 | + if (file_exists(AO_CCSS_LOCK)) { |
|
796 | + unlink(AO_CCSS_LOCK); |
|
797 | 797 | } |
798 | 798 | |
799 | 799 | // Make sure queue processing is scheduled, recreate if not. |
800 | - if ( ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
801 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
800 | + if (!wp_next_scheduled('ao_ccss_queue')) { |
|
801 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
802 | 802 | } |
803 | 803 | |
804 | 804 | // Queue cleaning. |
805 | 805 | global $ao_ccss_queue; |
806 | 806 | $queue_purge_threshold = 100; |
807 | - $queue_purge_age = 24 * 60 * 60; |
|
808 | - $queue_length = count( $ao_ccss_queue ); |
|
809 | - $timestamp_yesterday = microtime( true ) - $queue_purge_age; |
|
807 | + $queue_purge_age = 24*60*60; |
|
808 | + $queue_length = count($ao_ccss_queue); |
|
809 | + $timestamp_yesterday = microtime(true) - $queue_purge_age; |
|
810 | 810 | $remove_old_new = false; |
811 | 811 | $queue_altered = false; |
812 | 812 | |
813 | - if ( $queue_length > $queue_purge_threshold ) { |
|
813 | + if ($queue_length > $queue_purge_threshold) { |
|
814 | 814 | $remove_old_new = true; |
815 | 815 | } |
816 | 816 | |
817 | - foreach ( $ao_ccss_queue as $path => $job ) { |
|
818 | - if ( ( $remove_old_new && 'NEW' == $job['jqstat'] && $job['jctime'] < $timestamp_yesterday ) || in_array( $job['jqstat'], array( 'JOB_FAILED', 'STATUS_JOB_BAD', 'NO_CSS', 'NO_RESPONSE' ) ) ) { |
|
819 | - unset( $ao_ccss_queue[ $path ] ); |
|
817 | + foreach ($ao_ccss_queue as $path => $job) { |
|
818 | + if (($remove_old_new && 'NEW' == $job['jqstat'] && $job['jctime'] < $timestamp_yesterday) || in_array($job['jqstat'], array('JOB_FAILED', 'STATUS_JOB_BAD', 'NO_CSS', 'NO_RESPONSE'))) { |
|
819 | + unset($ao_ccss_queue[$path]); |
|
820 | 820 | $queue_altered = true; |
821 | 821 | } |
822 | 822 | } |
823 | 823 | |
824 | 824 | // save queue to options! |
825 | - if ( $queue_altered ) { |
|
826 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
827 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
828 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue cleaning done.', 3 ); |
|
825 | + if ($queue_altered) { |
|
826 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
827 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
828 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue cleaning done.', 3); |
|
829 | 829 | } |
830 | 830 | |
831 | 831 | // re-check key if invalid. |
832 | 832 | global $ao_ccss_keyst; |
833 | - if ( 1 == $ao_ccss_keyst ) { |
|
834 | - $this->ao_ccss_api_generate( '', '', '' ); |
|
833 | + if (1 == $ao_ccss_keyst) { |
|
834 | + $this->ao_ccss_api_generate('', '', ''); |
|
835 | 835 | } |
836 | 836 | } |
837 | 837 | } |
@@ -221,6 +221,11 @@ |
||
221 | 221 | return $page_type; |
222 | 222 | } |
223 | 223 | |
224 | + /** |
|
225 | + * @param string $path |
|
226 | + * @param false|string $target |
|
227 | + * @param boolean $create |
|
228 | + */ |
|
224 | 229 | public function ao_ccss_define_job( $path, $target, $type, $hash, $file, $jid, $jrstat, $jvstat, $create ) { |
225 | 230 | // Define a job entry to be created or updated |
226 | 231 | // Define commom job properties. |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Critical CSS job enqueue logic. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -12,27 +12,27 @@ discard block |
||
12 | 12 | { |
13 | 13 | // fetch all options at once and populate them individually explicitely as globals. |
14 | 14 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
15 | - foreach ( $all_options as $_option => $_value ) { |
|
15 | + foreach ($all_options as $_option => $_value) { |
|
16 | 16 | global ${$_option}; |
17 | 17 | ${$_option} = $_value; |
18 | 18 | } |
19 | 19 | } |
20 | 20 | |
21 | - public static function ao_ccss_enqueue( $hash ) { |
|
21 | + public static function ao_ccss_enqueue($hash) { |
|
22 | 22 | $self = new self(); |
23 | 23 | // Get key status. |
24 | - $key = autoptimizeCriticalCSSCore::ao_ccss_key_status( false ); |
|
24 | + $key = autoptimizeCriticalCSSCore::ao_ccss_key_status(false); |
|
25 | 25 | |
26 | 26 | // Queue is available to anyone... |
27 | 27 | $enqueue = true; |
28 | 28 | |
29 | 29 | // ... which are not the ones below. |
30 | - if ( is_user_logged_in() || is_feed() || is_404() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || $self->ao_ccss_ua() || 'nokey' == $key['status'] || 'invalid' == $key['status'] || false === apply_filters( 'autoptimize_filter_ccss_enqueue_should_enqueue', true ) ) { |
|
30 | + if (is_user_logged_in() || is_feed() || is_404() || (defined('DOING_AJAX') && DOING_AJAX) || $self->ao_ccss_ua() || 'nokey' == $key['status'] || 'invalid' == $key['status'] || false === apply_filters('autoptimize_filter_ccss_enqueue_should_enqueue', true)) { |
|
31 | 31 | $enqueue = false; |
32 | - autoptimizeCriticalCSSCore::ao_ccss_log( "Job queuing is not available for WordPress's logged in users, feeds, error pages, ajax calls, to criticalcss.com itself or when a valid API key is not found", 3 ); |
|
32 | + autoptimizeCriticalCSSCore::ao_ccss_log("Job queuing is not available for WordPress's logged in users, feeds, error pages, ajax calls, to criticalcss.com itself or when a valid API key is not found", 3); |
|
33 | 33 | } |
34 | 34 | |
35 | - if ( $enqueue ) { |
|
35 | + if ($enqueue) { |
|
36 | 36 | // Continue if queue is available |
37 | 37 | // Attach required arrays/ vars. |
38 | 38 | global $ao_ccss_rules; |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | global $ao_ccss_forcepath; |
42 | 42 | |
43 | 43 | // Get request path and page type, and initialize the queue update flag. |
44 | - $req_path = strtok( $_SERVER['REQUEST_URI'], '?' ); |
|
44 | + $req_path = strtok($_SERVER['REQUEST_URI'], '?'); |
|
45 | 45 | $req_type = $self->ao_ccss_get_type(); |
46 | 46 | $job_qualify = false; |
47 | 47 | $target_rule = false; |
@@ -49,21 +49,21 @@ discard block |
||
49 | 49 | $queue_update = false; |
50 | 50 | |
51 | 51 | // Match for paths in rules. |
52 | - foreach ( $ao_ccss_rules['paths'] as $path => $props ) { |
|
52 | + foreach ($ao_ccss_rules['paths'] as $path => $props) { |
|
53 | 53 | |
54 | 54 | // Prepare rule target and log. |
55 | - $target_rule = 'paths|' . $path; |
|
56 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Qualifying path <' . $req_path . '> for job submission by rule <' . $target_rule . '>', 3 ); |
|
55 | + $target_rule = 'paths|'.$path; |
|
56 | + autoptimizeCriticalCSSCore::ao_ccss_log('Qualifying path <'.$req_path.'> for job submission by rule <'.$target_rule.'>', 3); |
|
57 | 57 | |
58 | 58 | // Path match |
59 | 59 | // -> exact match needed for AUTO rules |
60 | 60 | // -> partial match OK for MANUAL rules (which have empty hash and a file with CCSS). |
61 | - if ( $path === $req_path || ( false == $props['hash'] && false != $props['file'] && preg_match( '|' . $path . '|', $req_path ) ) ) { |
|
61 | + if ($path === $req_path || (false == $props['hash'] && false != $props['file'] && preg_match('|'.$path.'|', $req_path))) { |
|
62 | 62 | |
63 | 63 | // There's a path match in the rule, so job QUALIFIES with a path rule match. |
64 | 64 | $job_qualify = true; |
65 | 65 | $rule_properties = $props; |
66 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Path <' . $req_path . '> QUALIFIED for job submission by rule <' . $target_rule . '>', 3 ); |
|
66 | + autoptimizeCriticalCSSCore::ao_ccss_log('Path <'.$req_path.'> QUALIFIED for job submission by rule <'.$target_rule.'>', 3); |
|
67 | 67 | |
68 | 68 | // Stop processing other path rules. |
69 | 69 | break; |
@@ -71,19 +71,19 @@ discard block |
||
71 | 71 | } |
72 | 72 | |
73 | 73 | // Match for types in rules if no path rule matches and if we're not enforcing paths. |
74 | - if ( ! $job_qualify && ( ! $ao_ccss_forcepath || ! in_array( $req_type, apply_filters( 'autoptimize_filter_ccss_coreenqueue_forcepathfortype', array( 'is_page' ) ) ) || ! apply_filters( 'autoptimize_filter_ccss_coreenqueue_ignorealltypes', false ) ) ) { |
|
75 | - foreach ( $ao_ccss_rules['types'] as $type => $props ) { |
|
74 | + if (!$job_qualify && (!$ao_ccss_forcepath || !in_array($req_type, apply_filters('autoptimize_filter_ccss_coreenqueue_forcepathfortype', array('is_page'))) || !apply_filters('autoptimize_filter_ccss_coreenqueue_ignorealltypes', false))) { |
|
75 | + foreach ($ao_ccss_rules['types'] as $type => $props) { |
|
76 | 76 | |
77 | 77 | // Prepare rule target and log. |
78 | - $target_rule = 'types|' . $type; |
|
79 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Qualifying page type <' . $req_type . '> on path <' . $req_path . '> for job submission by rule <' . $target_rule . '>', 3 ); |
|
78 | + $target_rule = 'types|'.$type; |
|
79 | + autoptimizeCriticalCSSCore::ao_ccss_log('Qualifying page type <'.$req_type.'> on path <'.$req_path.'> for job submission by rule <'.$target_rule.'>', 3); |
|
80 | 80 | |
81 | - if ( $req_type == $type ) { |
|
81 | + if ($req_type == $type) { |
|
82 | 82 | // Type match. |
83 | 83 | // There's a type match in the rule, so job QUALIFIES with a type rule match. |
84 | 84 | $job_qualify = true; |
85 | 85 | $rule_properties = $props; |
86 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Page type <' . $req_type . '> on path <' . $req_path . '> QUALIFIED for job submission by rule <' . $target_rule . '>', 3 ); |
|
86 | + autoptimizeCriticalCSSCore::ao_ccss_log('Page type <'.$req_type.'> on path <'.$req_path.'> QUALIFIED for job submission by rule <'.$target_rule.'>', 3); |
|
87 | 87 | |
88 | 88 | // Stop processing other type rules. |
89 | 89 | break; |
@@ -91,11 +91,11 @@ discard block |
||
91 | 91 | } |
92 | 92 | } |
93 | 93 | |
94 | - if ( $job_qualify && ( ( false == $rule_properties['hash'] && false != $rule_properties['file'] ) || strpos( $req_type, 'template_' ) !== false ) ) { |
|
94 | + if ($job_qualify && ((false == $rule_properties['hash'] && false != $rule_properties['file']) || strpos($req_type, 'template_') !== false)) { |
|
95 | 95 | // If job qualifies but rule hash is false and file isn't false (MANUAL rule) or if template, job does not qualify despite what previous evaluations says. |
96 | 96 | $job_qualify = false; |
97 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission DISQUALIFIED by MANUAL rule <' . $target_rule . '> with hash <' . $rule_properties['hash'] . '> and file <' . $rule_properties['file'] . '>', 3 ); |
|
98 | - } elseif ( ! $job_qualify && empty( $rule_properties ) ) { |
|
97 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job submission DISQUALIFIED by MANUAL rule <'.$target_rule.'> with hash <'.$rule_properties['hash'].'> and file <'.$rule_properties['file'].'>', 3); |
|
98 | + } elseif (!$job_qualify && empty($rule_properties)) { |
|
99 | 99 | // But if job does not qualify and rule properties are set, job qualifies as there is no matching rule for it yet |
100 | 100 | // Fill-in the new target rule. |
101 | 101 | $job_qualify = true; |
@@ -103,28 +103,28 @@ discard block |
||
103 | 103 | // Should we switch to path-base AUTO-rules? Conditions: |
104 | 104 | // 1. forcepath option has to be enabled (off by default) |
105 | 105 | // 2. request type should be (by default, but filterable) one of is_page (removed for now: woo_is_product or woo_is_product_category). |
106 | - if ( ( $ao_ccss_forcepath && in_array( $req_type, apply_filters( 'autoptimize_filter_ccss_coreenqueue_forcepathfortype', array( 'is_page' ) ) ) ) || apply_filters( 'autoptimize_filter_ccss_coreenqueue_ignorealltypes', false ) ) { |
|
107 | - if ( '/' !== $req_path ) { |
|
108 | - $target_rule = 'paths|' . $req_path; |
|
106 | + if (($ao_ccss_forcepath && in_array($req_type, apply_filters('autoptimize_filter_ccss_coreenqueue_forcepathfortype', array('is_page')))) || apply_filters('autoptimize_filter_ccss_coreenqueue_ignorealltypes', false)) { |
|
107 | + if ('/' !== $req_path) { |
|
108 | + $target_rule = 'paths|'.$req_path; |
|
109 | 109 | } else { |
110 | 110 | // Exception; we don't want a path-based rule for "/" as that messes things up, hard-switch this to a type-based is_front_page rule. |
111 | - $target_rule = 'types|' . 'is_front_page'; |
|
111 | + $target_rule = 'types|'.'is_front_page'; |
|
112 | 112 | } |
113 | 113 | } else { |
114 | - $target_rule = 'types|' . $req_type; |
|
114 | + $target_rule = 'types|'.$req_type; |
|
115 | 115 | } |
116 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission QUALIFIED by MISSING rule for page type <' . $req_type . '> on path <' . $req_path . '>, new rule target is <' . $target_rule . '>', 3 ); |
|
116 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job submission QUALIFIED by MISSING rule for page type <'.$req_type.'> on path <'.$req_path.'>, new rule target is <'.$target_rule.'>', 3); |
|
117 | 117 | } else { |
118 | 118 | // Or just log a job qualified by a matching rule. |
119 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission QUALIFIED by AUTO rule <' . $target_rule . '> with hash <' . $rule_properties['hash'] . '> and file <' . $rule_properties['file'] . '>', 3 ); |
|
119 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job submission QUALIFIED by AUTO rule <'.$target_rule.'> with hash <'.$rule_properties['hash'].'> and file <'.$rule_properties['file'].'>', 3); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | // Submit job. |
123 | - if ( $job_qualify ) { |
|
124 | - if ( ! array_key_exists( $req_path, $ao_ccss_queue ) ) { |
|
123 | + if ($job_qualify) { |
|
124 | + if (!array_key_exists($req_path, $ao_ccss_queue)) { |
|
125 | 125 | // This is a NEW job |
126 | 126 | // Merge job into the queue. |
127 | - $ao_ccss_queue[ $req_path ] = $self->ao_ccss_define_job( |
|
127 | + $ao_ccss_queue[$req_path] = $self->ao_ccss_define_job( |
|
128 | 128 | $req_path, |
129 | 129 | $target_rule, |
130 | 130 | $req_type, |
@@ -140,30 +140,30 @@ discard block |
||
140 | 140 | } else { |
141 | 141 | // This is an existing job |
142 | 142 | // The job is still NEW, most likely this is extra CSS file for the same page that needs a hash. |
143 | - if ( 'NEW' == $ao_ccss_queue[ $req_path ]['jqstat'] ) { |
|
143 | + if ('NEW' == $ao_ccss_queue[$req_path]['jqstat']) { |
|
144 | 144 | // Add hash if it's not already in the job. |
145 | - if ( ! in_array( $hash, $ao_ccss_queue[ $req_path ]['hashes'] ) ) { |
|
145 | + if (!in_array($hash, $ao_ccss_queue[$req_path]['hashes'])) { |
|
146 | 146 | // Push new hash to its array and update flag. |
147 | - $queue_update = array_push( $ao_ccss_queue[ $req_path ]['hashes'], $hash ); |
|
147 | + $queue_update = array_push($ao_ccss_queue[$req_path]['hashes'], $hash); |
|
148 | 148 | |
149 | 149 | // Log job update. |
150 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Hashes UPDATED on local job id <' . $ao_ccss_queue[ $req_path ]['ljid'] . '>, job status NEW, target rule <' . $ao_ccss_queue[ $req_path ]['rtarget'] . '>, hash added: ' . $hash, 3 ); |
|
150 | + autoptimizeCriticalCSSCore::ao_ccss_log('Hashes UPDATED on local job id <'.$ao_ccss_queue[$req_path]['ljid'].'>, job status NEW, target rule <'.$ao_ccss_queue[$req_path]['rtarget'].'>, hash added: '.$hash, 3); |
|
151 | 151 | |
152 | 152 | // Return from here as the hash array is already updated. |
153 | 153 | return true; |
154 | 154 | } |
155 | - } elseif ( 'NEW' != $ao_ccss_queue[ $req_path ]['jqstat'] && 'JOB_QUEUED' != $ao_ccss_queue[ $req_path ]['jqstat'] && 'JOB_ONGOING' != $ao_ccss_queue[ $req_path ]['jqstat'] ) { |
|
155 | + } elseif ('NEW' != $ao_ccss_queue[$req_path]['jqstat'] && 'JOB_QUEUED' != $ao_ccss_queue[$req_path]['jqstat'] && 'JOB_ONGOING' != $ao_ccss_queue[$req_path]['jqstat']) { |
|
156 | 156 | // Allow requeuing jobs that are not NEW, JOB_QUEUED or JOB_ONGOING |
157 | 157 | // Merge new job keeping some previous job values. |
158 | - $ao_ccss_queue[ $req_path ] = $self->ao_ccss_define_job( |
|
158 | + $ao_ccss_queue[$req_path] = $self->ao_ccss_define_job( |
|
159 | 159 | $req_path, |
160 | 160 | $target_rule, |
161 | 161 | $req_type, |
162 | 162 | $hash, |
163 | - $ao_ccss_queue[ $req_path ]['file'], |
|
164 | - $ao_ccss_queue[ $req_path ]['jid'], |
|
165 | - $ao_ccss_queue[ $req_path ]['jrstat'], |
|
166 | - $ao_ccss_queue[ $req_path ]['jvstat'], |
|
163 | + $ao_ccss_queue[$req_path]['file'], |
|
164 | + $ao_ccss_queue[$req_path]['jid'], |
|
165 | + $ao_ccss_queue[$req_path]['jrstat'], |
|
166 | + $ao_ccss_queue[$req_path]['jvstat'], |
|
167 | 167 | false |
168 | 168 | ); |
169 | 169 | // Set update flag. |
@@ -171,14 +171,14 @@ discard block |
||
171 | 171 | } |
172 | 172 | } |
173 | 173 | |
174 | - if ( $queue_update ) { |
|
174 | + if ($queue_update) { |
|
175 | 175 | // Persist the job to the queue and return. |
176 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
177 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
176 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
177 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
178 | 178 | return true; |
179 | 179 | } else { |
180 | 180 | // Or just return false if no job was added. |
181 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'A job for path <' . $req_path . '> already exist with NEW or PENDING status, skipping job creation', 3 ); |
|
181 | + autoptimizeCriticalCSSCore::ao_ccss_log('A job for path <'.$req_path.'> already exist with NEW or PENDING status, skipping job creation', 3); |
|
182 | 182 | return false; |
183 | 183 | } |
184 | 184 | } |
@@ -195,29 +195,29 @@ discard block |
||
195 | 195 | $page_type = false; |
196 | 196 | |
197 | 197 | // Iterates over the array to match a type. |
198 | - foreach ( $ao_ccss_types as $type ) { |
|
199 | - if ( is_404() ) { |
|
198 | + foreach ($ao_ccss_types as $type) { |
|
199 | + if (is_404()) { |
|
200 | 200 | $page_type = 'is_404'; |
201 | 201 | break; |
202 | - } elseif ( strpos( $type, 'custom_post_' ) !== false && ( ! $ao_ccss_forcepath || ! is_page() ) ) { |
|
202 | + } elseif (strpos($type, 'custom_post_') !== false && (!$ao_ccss_forcepath || !is_page())) { |
|
203 | 203 | // Match custom post types and not page or page not forced to path-based. |
204 | - if ( get_post_type( get_the_ID() ) === substr( $type, 12 ) ) { |
|
204 | + if (get_post_type(get_the_ID()) === substr($type, 12)) { |
|
205 | 205 | $page_type = $type; |
206 | 206 | break; |
207 | 207 | } |
208 | - } elseif ( strpos( $type, 'template_' ) !== false && ( ! $ao_ccss_forcepath || ! is_page() ) ) { |
|
208 | + } elseif (strpos($type, 'template_') !== false && (!$ao_ccss_forcepath || !is_page())) { |
|
209 | 209 | // Match templates if not page or if page is not forced to path-based. |
210 | - if ( is_page_template( substr( $type, 9 ) ) ) { |
|
210 | + if (is_page_template(substr($type, 9))) { |
|
211 | 211 | $page_type = $type; |
212 | 212 | break; |
213 | 213 | } |
214 | 214 | } else { |
215 | 215 | // Match all other existing types |
216 | 216 | // but remove prefix to be able to check if the function exists & returns true. |
217 | - $_type = str_replace( array( 'woo_', 'bp_', 'bbp_', 'edd_' ), '', $type ); |
|
218 | - if ( function_exists( $_type ) && call_user_func( $_type ) ) { |
|
217 | + $_type = str_replace(array('woo_', 'bp_', 'bbp_', 'edd_'), '', $type); |
|
218 | + if (function_exists($_type) && call_user_func($_type)) { |
|
219 | 219 | // Make sure we only return is_front_page (and is_home) for one page, not for the "paged frontpage" (/page/2 ..). |
220 | - if ( ( 'is_front_page' !== $_type && 'is_home' !== $_type ) || ! is_paged() ) { |
|
220 | + if (('is_front_page' !== $_type && 'is_home' !== $_type) || !is_paged()) { |
|
221 | 221 | $page_type = $type; |
222 | 222 | break; |
223 | 223 | } |
@@ -229,44 +229,44 @@ discard block |
||
229 | 229 | return $page_type; |
230 | 230 | } |
231 | 231 | |
232 | - public function ao_ccss_define_job( $path, $target, $type, $hash, $file, $jid, $jrstat, $jvstat, $create ) { |
|
232 | + public function ao_ccss_define_job($path, $target, $type, $hash, $file, $jid, $jrstat, $jvstat, $create) { |
|
233 | 233 | // Define a job entry to be created or updated |
234 | 234 | // Define commom job properties. |
235 | 235 | $path = array(); |
236 | 236 | $path['ljid'] = $this->ao_ccss_job_id(); |
237 | 237 | $path['rtarget'] = $target; |
238 | 238 | $path['ptype'] = $type; |
239 | - $path['hashes'] = array( $hash ); |
|
239 | + $path['hashes'] = array($hash); |
|
240 | 240 | $path['hash'] = $hash; |
241 | 241 | $path['file'] = $file; |
242 | 242 | $path['jid'] = $jid; |
243 | 243 | $path['jqstat'] = 'NEW'; |
244 | 244 | $path['jrstat'] = $jrstat; |
245 | 245 | $path['jvstat'] = $jvstat; |
246 | - $path['jctime'] = microtime( true ); |
|
246 | + $path['jctime'] = microtime(true); |
|
247 | 247 | $path['jftime'] = null; |
248 | 248 | |
249 | 249 | // Set operation requested. |
250 | - if ( $create ) { |
|
250 | + if ($create) { |
|
251 | 251 | $operation = 'CREATED'; |
252 | 252 | } else { |
253 | 253 | $operation = 'UPDATED'; |
254 | 254 | } |
255 | 255 | |
256 | 256 | // Log job creation. |
257 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job ' . $operation . ' with local job id <' . $path['ljid'] . '> for target rule <' . $target . '>', 3 ); |
|
257 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job '.$operation.' with local job id <'.$path['ljid'].'> for target rule <'.$target.'>', 3); |
|
258 | 258 | |
259 | 259 | return $path; |
260 | 260 | } |
261 | 261 | |
262 | - public function ao_ccss_job_id( $length = 6 ) { |
|
262 | + public function ao_ccss_job_id($length = 6) { |
|
263 | 263 | // Generate random strings for the local job ID |
264 | 264 | // Based on https://stackoverflow.com/a/4356295 . |
265 | 265 | $characters = '0123456789abcdefghijklmnopqrstuvwxyz'; |
266 | - $characters_length = strlen( $characters ); |
|
266 | + $characters_length = strlen($characters); |
|
267 | 267 | $random_string = 'j-'; |
268 | - for ( $i = 0; $i < $length; $i++ ) { |
|
269 | - $random_string .= $characters[ rand( 0, $characters_length - 1 ) ]; |
|
268 | + for ($i = 0; $i < $length; $i++) { |
|
269 | + $random_string .= $characters[rand(0, $characters_length - 1)]; |
|
270 | 270 | } |
271 | 271 | return $random_string; |
272 | 272 | } |
@@ -274,17 +274,17 @@ discard block |
||
274 | 274 | public function ao_ccss_ua() { |
275 | 275 | // Check for criticalcss.com user agent. |
276 | 276 | $agent = ''; |
277 | - if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) { |
|
277 | + if (isset($_SERVER['HTTP_USER_AGENT'])) { |
|
278 | 278 | $agent = $_SERVER['HTTP_USER_AGENT']; |
279 | 279 | } |
280 | 280 | |
281 | 281 | // Check for UA and return TRUE when criticalcss.com is the detected UA, false when not. |
282 | - $rtn = strpos( $agent, AO_CCSS_URL ); |
|
283 | - if ( 0 === $rtn ) { |
|
282 | + $rtn = strpos($agent, AO_CCSS_URL); |
|
283 | + if (0 === $rtn) { |
|
284 | 284 | $rtn = true; |
285 | 285 | } else { |
286 | 286 | $rtn = false; |
287 | 287 | } |
288 | - return ( $rtn ); |
|
288 | + return ($rtn); |
|
289 | 289 | } |
290 | 290 | } |
@@ -676,6 +676,9 @@ discard block |
||
676 | 676 | return true; |
677 | 677 | } |
678 | 678 | |
679 | + /** |
|
680 | + * @param string $tag |
|
681 | + */ |
|
679 | 682 | private function movetolast( $tag ) |
680 | 683 | { |
681 | 684 | if ( empty( $tag ) ) { |
@@ -733,7 +736,7 @@ discard block |
||
733 | 736 | * @param string $filepath Filepath. |
734 | 737 | * @param bool $cache_miss Optional. Force a cache miss. Default false. |
735 | 738 | * |
736 | - * @return bool|string Url pointing to the minified js file or false. |
|
739 | + * @return false|string Url pointing to the minified js file or false. |
|
737 | 740 | */ |
738 | 741 | public function minify_single( $filepath, $cache_miss = false ) |
739 | 742 | { |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Class for JS optimization. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -198,128 +198,128 @@ discard block |
||
198 | 198 | * |
199 | 199 | * @param array $options all options. |
200 | 200 | */ |
201 | - public function read( $options ) |
|
201 | + public function read($options) |
|
202 | 202 | { |
203 | - $noptimize_js = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content ); |
|
204 | - if ( $noptimize_js ) { |
|
203 | + $noptimize_js = apply_filters('autoptimize_filter_js_noptimize', false, $this->content); |
|
204 | + if ($noptimize_js) { |
|
205 | 205 | return false; |
206 | 206 | } |
207 | 207 | |
208 | 208 | // only optimize known good JS? |
209 | - $allowlist_js = apply_filters( 'autoptimize_filter_js_allowlist', '', $this->content ); |
|
210 | - $allowlist_js = apply_filters( 'autoptimize_filter_js_whitelist', $allowlist_js, $this->content ); // fixme: to be removed in next version. |
|
211 | - if ( ! empty( $allowlist_js ) ) { |
|
212 | - $this->allowlist = array_filter( array_map( 'trim', explode( ',', $allowlist_js ) ) ); |
|
209 | + $allowlist_js = apply_filters('autoptimize_filter_js_allowlist', '', $this->content); |
|
210 | + $allowlist_js = apply_filters('autoptimize_filter_js_whitelist', $allowlist_js, $this->content); // fixme: to be removed in next version. |
|
211 | + if (!empty($allowlist_js)) { |
|
212 | + $this->allowlist = array_filter(array_map('trim', explode(',', $allowlist_js))); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | // is there JS we should simply remove? |
216 | - $removable_js = apply_filters( 'autoptimize_filter_js_removables', '', $this->content ); |
|
217 | - if ( ! empty( $removable_js ) ) { |
|
218 | - $this->jsremovables = array_filter( array_map( 'trim', explode( ',', $removable_js ) ) ); |
|
216 | + $removable_js = apply_filters('autoptimize_filter_js_removables', '', $this->content); |
|
217 | + if (!empty($removable_js)) { |
|
218 | + $this->jsremovables = array_filter(array_map('trim', explode(',', $removable_js))); |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | // only header? |
222 | - if ( apply_filters( 'autoptimize_filter_js_justhead', $options['justhead'] ) ) { |
|
223 | - $content = explode( '</head>', $this->content, 2 ); |
|
224 | - $this->content = $content[0] . '</head>'; |
|
222 | + if (apply_filters('autoptimize_filter_js_justhead', $options['justhead'])) { |
|
223 | + $content = explode('</head>', $this->content, 2); |
|
224 | + $this->content = $content[0].'</head>'; |
|
225 | 225 | $this->restofcontent = $content[1]; |
226 | 226 | } |
227 | 227 | |
228 | 228 | // Determine whether we're doing JS-files aggregation or not. |
229 | - if ( ! $options['aggregate'] ) { |
|
229 | + if (!$options['aggregate']) { |
|
230 | 230 | $this->aggregate = false; |
231 | 231 | } |
232 | 232 | // Returning true for "dontaggregate" turns off aggregation. |
233 | - if ( $this->aggregate && apply_filters( 'autoptimize_filter_js_dontaggregate', false ) ) { |
|
233 | + if ($this->aggregate && apply_filters('autoptimize_filter_js_dontaggregate', false)) { |
|
234 | 234 | $this->aggregate = false; |
235 | 235 | } |
236 | 236 | |
237 | 237 | // include inline? |
238 | - if ( apply_filters( 'autoptimize_js_include_inline', $options['include_inline'] ) ) { |
|
238 | + if (apply_filters('autoptimize_js_include_inline', $options['include_inline'])) { |
|
239 | 239 | $this->include_inline = true; |
240 | 240 | } |
241 | 241 | |
242 | 242 | // filter to "late inject minified JS", default to true for now (it is faster). |
243 | - $this->inject_min_late = apply_filters( 'autoptimize_filter_js_inject_min_late', true ); |
|
243 | + $this->inject_min_late = apply_filters('autoptimize_filter_js_inject_min_late', true); |
|
244 | 244 | |
245 | 245 | // filters to override hardcoded do(nt)move(last) array contents (array in, array out!). |
246 | - $this->dontmove = apply_filters( 'autoptimize_filter_js_dontmove', $this->dontmove ); |
|
247 | - $this->domovelast = apply_filters( 'autoptimize_filter_js_movelast', $this->domovelast ); |
|
248 | - $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove ); |
|
246 | + $this->dontmove = apply_filters('autoptimize_filter_js_dontmove', $this->dontmove); |
|
247 | + $this->domovelast = apply_filters('autoptimize_filter_js_movelast', $this->domovelast); |
|
248 | + $this->domove = apply_filters('autoptimize_filter_js_domove', $this->domove); |
|
249 | 249 | |
250 | 250 | // Determine whether excluded files should be minified if not yet so. |
251 | - if ( ! $options['minify_excluded'] && $options['aggregate'] ) { |
|
251 | + if (!$options['minify_excluded'] && $options['aggregate']) { |
|
252 | 252 | $this->minify_excluded = false; |
253 | 253 | } |
254 | - $this->minify_excluded = apply_filters( 'autoptimize_filter_js_minify_excluded', $this->minify_excluded, '' ); |
|
254 | + $this->minify_excluded = apply_filters('autoptimize_filter_js_minify_excluded', $this->minify_excluded, ''); |
|
255 | 255 | |
256 | 256 | // get extra exclusions settings or filter. |
257 | 257 | $exclude_js = $options['js_exclude']; |
258 | - $exclude_js = apply_filters( 'autoptimize_filter_js_exclude', $exclude_js, $this->content ); |
|
259 | - |
|
260 | - if ( '' !== $exclude_js ) { |
|
261 | - if ( is_array( $exclude_js ) ) { |
|
262 | - $remove_keys = array_keys( $exclude_js, 'remove' ); |
|
263 | - if ( false !== $remove_keys ) { |
|
264 | - foreach ( $remove_keys as $remove_key ) { |
|
265 | - unset( $exclude_js[ $remove_key ] ); |
|
258 | + $exclude_js = apply_filters('autoptimize_filter_js_exclude', $exclude_js, $this->content); |
|
259 | + |
|
260 | + if ('' !== $exclude_js) { |
|
261 | + if (is_array($exclude_js)) { |
|
262 | + $remove_keys = array_keys($exclude_js, 'remove'); |
|
263 | + if (false !== $remove_keys) { |
|
264 | + foreach ($remove_keys as $remove_key) { |
|
265 | + unset($exclude_js[$remove_key]); |
|
266 | 266 | $this->jsremovables[] = $remove_key; |
267 | 267 | } |
268 | 268 | } |
269 | - $excl_js_arr = array_keys( $exclude_js ); |
|
269 | + $excl_js_arr = array_keys($exclude_js); |
|
270 | 270 | } else { |
271 | - $excl_js_arr = array_filter( array_map( 'trim', explode( ',', $exclude_js ) ) ); |
|
271 | + $excl_js_arr = array_filter(array_map('trim', explode(',', $exclude_js))); |
|
272 | 272 | } |
273 | - $this->dontmove = array_merge( $excl_js_arr, $this->dontmove ); |
|
273 | + $this->dontmove = array_merge($excl_js_arr, $this->dontmove); |
|
274 | 274 | } |
275 | 275 | |
276 | 276 | // Should we add try-catch? |
277 | - if ( $options['trycatch'] ) { |
|
277 | + if ($options['trycatch']) { |
|
278 | 278 | $this->trycatch = true; |
279 | 279 | } |
280 | 280 | |
281 | 281 | // force js in head? |
282 | - if ( $options['forcehead'] ) { |
|
282 | + if ($options['forcehead']) { |
|
283 | 283 | $this->forcehead = true; |
284 | 284 | } else { |
285 | 285 | $this->forcehead = false; |
286 | 286 | } |
287 | 287 | |
288 | - $this->forcehead = apply_filters( 'autoptimize_filter_js_forcehead', $this->forcehead ); |
|
288 | + $this->forcehead = apply_filters('autoptimize_filter_js_forcehead', $this->forcehead); |
|
289 | 289 | |
290 | 290 | // get cdn url. |
291 | 291 | $this->cdn_url = $options['cdn_url']; |
292 | 292 | |
293 | 293 | // noptimize me. |
294 | - $this->content = $this->hide_noptimize( $this->content ); |
|
294 | + $this->content = $this->hide_noptimize($this->content); |
|
295 | 295 | |
296 | 296 | // Save IE hacks. |
297 | - $this->content = $this->hide_iehacks( $this->content ); |
|
297 | + $this->content = $this->hide_iehacks($this->content); |
|
298 | 298 | |
299 | 299 | // comments. |
300 | - $this->content = $this->hide_comments( $this->content ); |
|
300 | + $this->content = $this->hide_comments($this->content); |
|
301 | 301 | |
302 | 302 | // Get script files. |
303 | - if ( preg_match_all( '#<script.*</script>#Usmi', $this->content, $matches ) ) { |
|
304 | - foreach ( $matches[0] as $tag ) { |
|
303 | + if (preg_match_all('#<script.*</script>#Usmi', $this->content, $matches)) { |
|
304 | + foreach ($matches[0] as $tag) { |
|
305 | 305 | // only consider script aggregation for types allowlisted in should_aggregate-function. |
306 | - $should_aggregate = $this->should_aggregate( $tag ); |
|
307 | - if ( ! $should_aggregate ) { |
|
306 | + $should_aggregate = $this->should_aggregate($tag); |
|
307 | + if (!$should_aggregate) { |
|
308 | 308 | $tag = ''; |
309 | 309 | continue; |
310 | 310 | } |
311 | 311 | |
312 | - if ( preg_match( '#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source ) ) { |
|
312 | + if (preg_match('#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source)) { |
|
313 | 313 | // non-inline script. |
314 | - if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
315 | - $this->content = str_replace( $tag, '', $this->content ); |
|
314 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
315 | + $this->content = str_replace($tag, '', $this->content); |
|
316 | 316 | continue; |
317 | 317 | } |
318 | 318 | |
319 | 319 | $orig_tag = null; |
320 | - $url = current( explode( '?', $source[2], 2 ) ); |
|
321 | - $path = $this->getpath( $url ); |
|
322 | - if ( false !== $path && preg_match( '#\.js$#', $path ) && $this->ismergeable( $tag ) ) { |
|
320 | + $url = current(explode('?', $source[2], 2)); |
|
321 | + $path = $this->getpath($url); |
|
322 | + if (false !== $path && preg_match('#\.js$#', $path) && $this->ismergeable($tag)) { |
|
323 | 323 | // ok to optimize, add to array. |
324 | 324 | $this->scripts[] = $path; |
325 | 325 | } else { |
@@ -327,11 +327,11 @@ discard block |
||
327 | 327 | $new_tag = $tag; |
328 | 328 | |
329 | 329 | // non-mergeable script (excluded or dynamic or external). |
330 | - if ( is_array( $exclude_js ) ) { |
|
330 | + if (is_array($exclude_js)) { |
|
331 | 331 | // should we add flags? |
332 | - foreach ( $exclude_js as $excl_tag => $excl_flags ) { |
|
333 | - if ( false !== strpos( $orig_tag, $excl_tag ) && in_array( $excl_flags, array( 'async', 'defer' ) ) ) { |
|
334 | - $new_tag = str_replace( '<script ', '<script ' . $excl_flags . ' ', $new_tag ); |
|
332 | + foreach ($exclude_js as $excl_tag => $excl_flags) { |
|
333 | + if (false !== strpos($orig_tag, $excl_tag) && in_array($excl_flags, array('async', 'defer'))) { |
|
334 | + $new_tag = str_replace('<script ', '<script '.$excl_flags.' ', $new_tag); |
|
335 | 335 | } |
336 | 336 | } |
337 | 337 | } |
@@ -339,14 +339,14 @@ discard block |
||
339 | 339 | // Should we minify the non-aggregated script? |
340 | 340 | // -> if aggregate is on and exclude minify is on |
341 | 341 | // -> if aggregate is off and the file is not in dontmove. |
342 | - if ( $path && $this->minify_excluded ) { |
|
343 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
|
344 | - if ( ( false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path ) || ( true === $this->aggregate && ( false === $consider_minified_array || str_replace( $consider_minified_array, '', $path ) === $path ) ) ) { |
|
345 | - $minified_url = $this->minify_single( $path ); |
|
346 | - if ( ! empty( $minified_url ) ) { |
|
342 | + if ($path && $this->minify_excluded) { |
|
343 | + $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified', false); |
|
344 | + if ((false === $this->aggregate && str_replace($this->dontmove, '', $path) === $path) || (true === $this->aggregate && (false === $consider_minified_array || str_replace($consider_minified_array, '', $path) === $path))) { |
|
345 | + $minified_url = $this->minify_single($path); |
|
346 | + if (!empty($minified_url)) { |
|
347 | 347 | // Replace original URL with minified URL from cache. |
348 | - $new_tag = str_replace( $url, $minified_url, $new_tag ); |
|
349 | - } elseif ( apply_filters( 'autoptimize_filter_ccsjs_remove_empty_minified_url', false ) ) { |
|
348 | + $new_tag = str_replace($url, $minified_url, $new_tag); |
|
349 | + } elseif (apply_filters('autoptimize_filter_ccsjs_remove_empty_minified_url', false)) { |
|
350 | 350 | // Remove the original script tag, because cache content is empty but only if filter |
351 | 351 | // is trued because $minified_url is also false if original JS is minified already. |
352 | 352 | $new_tag = ''; |
@@ -354,17 +354,17 @@ discard block |
||
354 | 354 | } |
355 | 355 | } |
356 | 356 | |
357 | - if ( $this->ismovable( $new_tag ) ) { |
|
357 | + if ($this->ismovable($new_tag)) { |
|
358 | 358 | // can be moved, flags and all. |
359 | - if ( $this->movetolast( $new_tag ) ) { |
|
359 | + if ($this->movetolast($new_tag)) { |
|
360 | 360 | $this->move['last'][] = $new_tag; |
361 | 361 | } else { |
362 | 362 | $this->move['first'][] = $new_tag; |
363 | 363 | } |
364 | 364 | } else { |
365 | 365 | // cannot be moved, so if flag was added re-inject altered tag immediately. |
366 | - if ( ( '' !== $new_tag && $orig_tag !== $new_tag ) || ( '' === $new_tag && apply_filters( 'autoptimize_filter_js_remove_empty_files', false ) ) ) { |
|
367 | - $this->content = str_replace( $orig_tag, $new_tag, $this->content ); |
|
366 | + if (('' !== $new_tag && $orig_tag !== $new_tag) || ('' === $new_tag && apply_filters('autoptimize_filter_js_remove_empty_files', false))) { |
|
367 | + $this->content = str_replace($orig_tag, $new_tag, $this->content); |
|
368 | 368 | $orig_tag = ''; |
369 | 369 | } |
370 | 370 | // and forget about the $tag (not to be touched any more). |
@@ -373,23 +373,23 @@ discard block |
||
373 | 373 | } |
374 | 374 | } else { |
375 | 375 | // Inline script. |
376 | - if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
377 | - $this->content = str_replace( $tag, '', $this->content ); |
|
376 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
377 | + $this->content = str_replace($tag, '', $this->content); |
|
378 | 378 | continue; |
379 | 379 | } |
380 | 380 | |
381 | 381 | // unhide comments, as javascript may be wrapped in comment-tags for old times' sake. |
382 | - $tag = $this->restore_comments( $tag ); |
|
383 | - if ( $this->ismergeable( $tag ) && $this->include_inline ) { |
|
384 | - preg_match( '#<script.*>(.*)</script>#Usmi', $tag, $code ); |
|
385 | - $code = preg_replace( '#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1] ); |
|
386 | - $code = preg_replace( '/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code ); |
|
387 | - $this->scripts[] = 'INLINE;' . $code; |
|
382 | + $tag = $this->restore_comments($tag); |
|
383 | + if ($this->ismergeable($tag) && $this->include_inline) { |
|
384 | + preg_match('#<script.*>(.*)</script>#Usmi', $tag, $code); |
|
385 | + $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1]); |
|
386 | + $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code); |
|
387 | + $this->scripts[] = 'INLINE;'.$code; |
|
388 | 388 | } else { |
389 | 389 | // Can we move this? |
390 | - $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag ); |
|
391 | - if ( $this->ismovable( $tag ) || '' !== $autoptimize_js_moveable ) { |
|
392 | - if ( $this->movetolast( $tag ) || 'last' === $autoptimize_js_moveable ) { |
|
390 | + $autoptimize_js_moveable = apply_filters('autoptimize_js_moveable', '', $tag); |
|
391 | + if ($this->ismovable($tag) || '' !== $autoptimize_js_moveable) { |
|
392 | + if ($this->movetolast($tag) || 'last' === $autoptimize_js_moveable) { |
|
393 | 393 | $this->move['last'][] = $tag; |
394 | 394 | } else { |
395 | 395 | $this->move['first'][] = $tag; |
@@ -400,11 +400,11 @@ discard block |
||
400 | 400 | } |
401 | 401 | } |
402 | 402 | // Re-hide comments to be able to do the removal based on tag from $this->content. |
403 | - $tag = $this->hide_comments( $tag ); |
|
403 | + $tag = $this->hide_comments($tag); |
|
404 | 404 | } |
405 | 405 | |
406 | 406 | // Remove the original script tag. |
407 | - $this->content = str_replace( $tag, '', $this->content ); |
|
407 | + $this->content = str_replace($tag, '', $this->content); |
|
408 | 408 | } |
409 | 409 | |
410 | 410 | return true; |
@@ -429,30 +429,30 @@ discard block |
||
429 | 429 | * @param string $tag Script node & child(ren). |
430 | 430 | * @return bool |
431 | 431 | */ |
432 | - public function should_aggregate( $tag ) |
|
432 | + public function should_aggregate($tag) |
|
433 | 433 | { |
434 | - if ( empty( $tag ) ) { |
|
434 | + if (empty($tag)) { |
|
435 | 435 | return false; |
436 | 436 | } |
437 | 437 | |
438 | 438 | // We're only interested in the type attribute of the <script> tag itself, not any possible |
439 | 439 | // inline code that might just contain the 'type=' string... |
440 | 440 | $tag_parts = array(); |
441 | - preg_match( '#<(script[^>]*)>#i', $tag, $tag_parts ); |
|
441 | + preg_match('#<(script[^>]*)>#i', $tag, $tag_parts); |
|
442 | 442 | $tag_without_contents = null; |
443 | - if ( ! empty( $tag_parts[1] ) ) { |
|
443 | + if (!empty($tag_parts[1])) { |
|
444 | 444 | $tag_without_contents = $tag_parts[1]; |
445 | 445 | } |
446 | 446 | |
447 | - $has_type = ( strpos( $tag_without_contents, 'type' ) !== false ); |
|
447 | + $has_type = (strpos($tag_without_contents, 'type') !== false); |
|
448 | 448 | |
449 | 449 | $type_valid = false; |
450 | - if ( $has_type ) { |
|
451 | - $type_valid = (bool) preg_match( '/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents ); |
|
450 | + if ($has_type) { |
|
451 | + $type_valid = (bool) preg_match('/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents); |
|
452 | 452 | } |
453 | 453 | |
454 | 454 | $should_aggregate = false; |
455 | - if ( ! $has_type || $type_valid ) { |
|
455 | + if (!$has_type || $type_valid) { |
|
456 | 456 | $should_aggregate = true; |
457 | 457 | } |
458 | 458 | |
@@ -464,75 +464,75 @@ discard block |
||
464 | 464 | */ |
465 | 465 | public function minify() |
466 | 466 | { |
467 | - foreach ( $this->scripts as $script ) { |
|
468 | - if ( empty( $script ) ) { |
|
467 | + foreach ($this->scripts as $script) { |
|
468 | + if (empty($script)) { |
|
469 | 469 | continue; |
470 | 470 | } |
471 | 471 | |
472 | 472 | // TODO/FIXME: some duplicate code here, can be reduced/simplified. |
473 | - if ( preg_match( '#^INLINE;#', $script ) ) { |
|
473 | + if (preg_match('#^INLINE;#', $script)) { |
|
474 | 474 | // Inline script. |
475 | - $script = preg_replace( '#^INLINE;#', '', $script ); |
|
476 | - $script = rtrim( $script, ";\n\t\r" ) . ';'; |
|
475 | + $script = preg_replace('#^INLINE;#', '', $script); |
|
476 | + $script = rtrim($script, ";\n\t\r").';'; |
|
477 | 477 | // Add try-catch? |
478 | - if ( $this->trycatch ) { |
|
479 | - $script = 'try{' . $script . '}catch(e){}'; |
|
478 | + if ($this->trycatch) { |
|
479 | + $script = 'try{'.$script.'}catch(e){}'; |
|
480 | 480 | } |
481 | - $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, '' ); |
|
482 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscript ) ) { |
|
481 | + $tmpscript = apply_filters('autoptimize_js_individual_script', $script, ''); |
|
482 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscript)) { |
|
483 | 483 | $script = $tmpscript; |
484 | 484 | $this->alreadyminified = true; |
485 | 485 | } |
486 | - $this->jscode .= "\n" . $script; |
|
486 | + $this->jscode .= "\n".$script; |
|
487 | 487 | } else { |
488 | 488 | // External script. |
489 | - if ( false !== $script && file_exists( $script ) && is_readable( $script ) ) { |
|
490 | - $scriptsrc = file_get_contents( $script ); |
|
491 | - $scriptsrc = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc ); |
|
492 | - $scriptsrc = rtrim( $scriptsrc, ";\n\t\r" ) . ';'; |
|
489 | + if (false !== $script && file_exists($script) && is_readable($script)) { |
|
490 | + $scriptsrc = file_get_contents($script); |
|
491 | + $scriptsrc = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc); |
|
492 | + $scriptsrc = rtrim($scriptsrc, ";\n\t\r").';'; |
|
493 | 493 | // Add try-catch? |
494 | - if ( $this->trycatch ) { |
|
495 | - $scriptsrc = 'try{' . $scriptsrc . '}catch(e){}'; |
|
494 | + if ($this->trycatch) { |
|
495 | + $scriptsrc = 'try{'.$scriptsrc.'}catch(e){}'; |
|
496 | 496 | } |
497 | - $tmpscriptsrc = apply_filters( 'autoptimize_js_individual_script', $scriptsrc, $script ); |
|
498 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscriptsrc ) ) { |
|
497 | + $tmpscriptsrc = apply_filters('autoptimize_js_individual_script', $scriptsrc, $script); |
|
498 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc)) { |
|
499 | 499 | $scriptsrc = $tmpscriptsrc; |
500 | 500 | $this->alreadyminified = true; |
501 | - } elseif ( $this->can_inject_late( $script ) ) { |
|
502 | - $scriptsrc = self::build_injectlater_marker( $script, md5( $scriptsrc ) ); |
|
501 | + } elseif ($this->can_inject_late($script)) { |
|
502 | + $scriptsrc = self::build_injectlater_marker($script, md5($scriptsrc)); |
|
503 | 503 | } |
504 | - $this->jscode .= "\n" . $scriptsrc; |
|
504 | + $this->jscode .= "\n".$scriptsrc; |
|
505 | 505 | } |
506 | 506 | } |
507 | 507 | } |
508 | 508 | |
509 | 509 | // Check for already-minified code. |
510 | - $this->md5hash = md5( $this->jscode ); |
|
511 | - $ccheck = new autoptimizeCache( $this->md5hash, 'js' ); |
|
512 | - if ( $ccheck->check() ) { |
|
510 | + $this->md5hash = md5($this->jscode); |
|
511 | + $ccheck = new autoptimizeCache($this->md5hash, 'js'); |
|
512 | + if ($ccheck->check()) { |
|
513 | 513 | $this->jscode = $ccheck->retrieve(); |
514 | 514 | return true; |
515 | 515 | } |
516 | - unset( $ccheck ); |
|
516 | + unset($ccheck); |
|
517 | 517 | |
518 | 518 | // $this->jscode has all the uncompressed code now. |
519 | - if ( true !== $this->alreadyminified ) { |
|
520 | - if ( apply_filters( 'autoptimize_js_do_minify', true ) ) { |
|
521 | - $tmp_jscode = trim( JSMin::minify( $this->jscode ) ); |
|
522 | - if ( ! empty( $tmp_jscode ) ) { |
|
519 | + if (true !== $this->alreadyminified) { |
|
520 | + if (apply_filters('autoptimize_js_do_minify', true)) { |
|
521 | + $tmp_jscode = trim(JSMin::minify($this->jscode)); |
|
522 | + if (!empty($tmp_jscode)) { |
|
523 | 523 | $this->jscode = $tmp_jscode; |
524 | - unset( $tmp_jscode ); |
|
524 | + unset($tmp_jscode); |
|
525 | 525 | } |
526 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
527 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
526 | + $this->jscode = $this->inject_minified($this->jscode); |
|
527 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
528 | 528 | return true; |
529 | 529 | } else { |
530 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
530 | + $this->jscode = $this->inject_minified($this->jscode); |
|
531 | 531 | return false; |
532 | 532 | } |
533 | 533 | } |
534 | 534 | |
535 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
535 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
536 | 536 | return true; |
537 | 537 | } |
538 | 538 | |
@@ -541,13 +541,13 @@ discard block |
||
541 | 541 | */ |
542 | 542 | public function cache() |
543 | 543 | { |
544 | - $cache = new autoptimizeCache( $this->md5hash, 'js' ); |
|
545 | - if ( ! $cache->check() ) { |
|
544 | + $cache = new autoptimizeCache($this->md5hash, 'js'); |
|
545 | + if (!$cache->check()) { |
|
546 | 546 | // Cache our code. |
547 | - $cache->cache( $this->jscode, 'text/javascript' ); |
|
547 | + $cache->cache($this->jscode, 'text/javascript'); |
|
548 | 548 | } |
549 | - $this->url = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
|
550 | - $this->url = $this->url_replace_cdn( $this->url ); |
|
549 | + $this->url = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
|
550 | + $this->url = $this->url_replace_cdn($this->url); |
|
551 | 551 | } |
552 | 552 | |
553 | 553 | /** |
@@ -556,47 +556,47 @@ discard block |
||
556 | 556 | public function getcontent() |
557 | 557 | { |
558 | 558 | // Restore the full content. |
559 | - if ( ! empty( $this->restofcontent ) ) { |
|
559 | + if (!empty($this->restofcontent)) { |
|
560 | 560 | $this->content .= $this->restofcontent; |
561 | 561 | $this->restofcontent = ''; |
562 | 562 | } |
563 | 563 | |
564 | 564 | // Add the scripts taking forcehead/ deferred (default) into account. |
565 | - if ( $this->forcehead ) { |
|
566 | - $replace_tag = array( '</head>', 'before' ); |
|
565 | + if ($this->forcehead) { |
|
566 | + $replace_tag = array('</head>', 'before'); |
|
567 | 567 | $defer = ''; |
568 | 568 | } else { |
569 | - $replace_tag = array( '</body>', 'before' ); |
|
569 | + $replace_tag = array('</body>', 'before'); |
|
570 | 570 | $defer = 'defer '; |
571 | 571 | } |
572 | 572 | |
573 | - $defer = apply_filters( 'autoptimize_filter_js_defer', $defer ); |
|
573 | + $defer = apply_filters('autoptimize_filter_js_defer', $defer); |
|
574 | 574 | $type_js = ''; |
575 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
575 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
576 | 576 | $type_js = 'type="text/javascript" '; |
577 | 577 | } |
578 | 578 | |
579 | - $bodyreplacementpayload = '<script ' . $type_js . $defer . 'src="' . $this->url . '"></script>'; |
|
580 | - $bodyreplacementpayload = apply_filters( 'autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload ); |
|
579 | + $bodyreplacementpayload = '<script '.$type_js.$defer.'src="'.$this->url.'"></script>'; |
|
580 | + $bodyreplacementpayload = apply_filters('autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload); |
|
581 | 581 | |
582 | - $bodyreplacement = implode( '', $this->move['first'] ); |
|
582 | + $bodyreplacement = implode('', $this->move['first']); |
|
583 | 583 | $bodyreplacement .= $bodyreplacementpayload; |
584 | - $bodyreplacement .= implode( '', $this->move['last'] ); |
|
584 | + $bodyreplacement .= implode('', $this->move['last']); |
|
585 | 585 | |
586 | - $replace_tag = apply_filters( 'autoptimize_filter_js_replacetag', $replace_tag ); |
|
586 | + $replace_tag = apply_filters('autoptimize_filter_js_replacetag', $replace_tag); |
|
587 | 587 | |
588 | - if ( strlen( $this->jscode ) > 0 ) { |
|
589 | - $this->inject_in_html( $bodyreplacement, $replace_tag ); |
|
588 | + if (strlen($this->jscode) > 0) { |
|
589 | + $this->inject_in_html($bodyreplacement, $replace_tag); |
|
590 | 590 | } |
591 | 591 | |
592 | 592 | // Restore comments. |
593 | - $this->content = $this->restore_comments( $this->content ); |
|
593 | + $this->content = $this->restore_comments($this->content); |
|
594 | 594 | |
595 | 595 | // Restore IE hacks. |
596 | - $this->content = $this->restore_iehacks( $this->content ); |
|
596 | + $this->content = $this->restore_iehacks($this->content); |
|
597 | 597 | |
598 | 598 | // Restore noptimize. |
599 | - $this->content = $this->restore_noptimize( $this->content ); |
|
599 | + $this->content = $this->restore_noptimize($this->content); |
|
600 | 600 | |
601 | 601 | // Return the modified HTML. |
602 | 602 | return $this->content; |
@@ -607,34 +607,34 @@ discard block |
||
607 | 607 | * |
608 | 608 | * @param string $tag JS tag. |
609 | 609 | */ |
610 | - private function ismergeable( $tag ) |
|
610 | + private function ismergeable($tag) |
|
611 | 611 | { |
612 | - if ( empty( $tag ) || ! $this->aggregate ) { |
|
612 | + if (empty($tag) || !$this->aggregate) { |
|
613 | 613 | return false; |
614 | 614 | } |
615 | 615 | |
616 | - if ( ! empty( $this->allowlist ) ) { |
|
617 | - foreach ( $this->allowlist as $match ) { |
|
618 | - if ( false !== strpos( $tag, $match ) ) { |
|
616 | + if (!empty($this->allowlist)) { |
|
617 | + foreach ($this->allowlist as $match) { |
|
618 | + if (false !== strpos($tag, $match)) { |
|
619 | 619 | return true; |
620 | 620 | } |
621 | 621 | } |
622 | 622 | // No match with allowlist. |
623 | 623 | return false; |
624 | 624 | } else { |
625 | - foreach ( $this->domove as $match ) { |
|
626 | - if ( false !== strpos( $tag, $match ) ) { |
|
625 | + foreach ($this->domove as $match) { |
|
626 | + if (false !== strpos($tag, $match)) { |
|
627 | 627 | // Matched something. |
628 | 628 | return false; |
629 | 629 | } |
630 | 630 | } |
631 | 631 | |
632 | - if ( $this->movetolast( $tag ) ) { |
|
632 | + if ($this->movetolast($tag)) { |
|
633 | 633 | return false; |
634 | 634 | } |
635 | 635 | |
636 | - foreach ( $this->dontmove as $match ) { |
|
637 | - if ( false !== strpos( $tag, $match ) ) { |
|
636 | + foreach ($this->dontmove as $match) { |
|
637 | + if (false !== strpos($tag, $match)) { |
|
638 | 638 | // Matched something. |
639 | 639 | return false; |
640 | 640 | } |
@@ -650,25 +650,25 @@ discard block |
||
650 | 650 | * |
651 | 651 | * @param string $tag tag to check for blocklist (exclusions). |
652 | 652 | */ |
653 | - private function ismovable( $tag ) |
|
653 | + private function ismovable($tag) |
|
654 | 654 | { |
655 | - if ( empty( $tag ) || true !== $this->include_inline || apply_filters( 'autoptimize_filter_js_unmovable', true ) ) { |
|
655 | + if (empty($tag) || true !== $this->include_inline || apply_filters('autoptimize_filter_js_unmovable', true)) { |
|
656 | 656 | return false; |
657 | 657 | } |
658 | 658 | |
659 | - foreach ( $this->domove as $match ) { |
|
660 | - if ( false !== strpos( $tag, $match ) ) { |
|
659 | + foreach ($this->domove as $match) { |
|
660 | + if (false !== strpos($tag, $match)) { |
|
661 | 661 | // Matched something. |
662 | 662 | return true; |
663 | 663 | } |
664 | 664 | } |
665 | 665 | |
666 | - if ( $this->movetolast( $tag ) ) { |
|
666 | + if ($this->movetolast($tag)) { |
|
667 | 667 | return true; |
668 | 668 | } |
669 | 669 | |
670 | - foreach ( $this->dontmove as $match ) { |
|
671 | - if ( false !== strpos( $tag, $match ) ) { |
|
670 | + foreach ($this->dontmove as $match) { |
|
671 | + if (false !== strpos($tag, $match)) { |
|
672 | 672 | // Matched something. |
673 | 673 | return false; |
674 | 674 | } |
@@ -678,14 +678,14 @@ discard block |
||
678 | 678 | return true; |
679 | 679 | } |
680 | 680 | |
681 | - private function movetolast( $tag ) |
|
681 | + private function movetolast($tag) |
|
682 | 682 | { |
683 | - if ( empty( $tag ) ) { |
|
683 | + if (empty($tag)) { |
|
684 | 684 | return false; |
685 | 685 | } |
686 | 686 | |
687 | - foreach ( $this->domovelast as $match ) { |
|
688 | - if ( false !== strpos( $tag, $match ) ) { |
|
687 | + foreach ($this->domovelast as $match) { |
|
688 | + if (false !== strpos($tag, $match)) { |
|
689 | 689 | // Matched, return true. |
690 | 690 | return true; |
691 | 691 | } |
@@ -705,12 +705,12 @@ discard block |
||
705 | 705 | * @param string $js_path Path to JS file. |
706 | 706 | * @return bool |
707 | 707 | */ |
708 | - private function can_inject_late( $js_path ) { |
|
709 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
|
710 | - if ( true !== $this->inject_min_late ) { |
|
708 | + private function can_inject_late($js_path) { |
|
709 | + $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified', false); |
|
710 | + if (true !== $this->inject_min_late) { |
|
711 | 711 | // late-inject turned off. |
712 | 712 | return false; |
713 | - } elseif ( ( false === strpos( $js_path, 'min.js' ) ) && ( false === strpos( $js_path, 'wp-includes/js/jquery/jquery.js' ) ) && ( str_replace( $consider_minified_array, '', $js_path ) === $js_path ) ) { |
|
713 | + } elseif ((false === strpos($js_path, 'min.js')) && (false === strpos($js_path, 'wp-includes/js/jquery/jquery.js')) && (str_replace($consider_minified_array, '', $js_path) === $js_path)) { |
|
714 | 714 | // file not minified based on filename & filter. |
715 | 715 | return false; |
716 | 716 | } else { |
@@ -737,35 +737,35 @@ discard block |
||
737 | 737 | * |
738 | 738 | * @return bool|string Url pointing to the minified js file or false. |
739 | 739 | */ |
740 | - public function minify_single( $filepath, $cache_miss = false ) |
|
740 | + public function minify_single($filepath, $cache_miss = false) |
|
741 | 741 | { |
742 | - $contents = $this->prepare_minify_single( $filepath ); |
|
742 | + $contents = $this->prepare_minify_single($filepath); |
|
743 | 743 | |
744 | - if ( empty( $contents ) ) { |
|
744 | + if (empty($contents)) { |
|
745 | 745 | return false; |
746 | 746 | } |
747 | 747 | |
748 | 748 | // Check cache. |
749 | - $hash = 'single_' . md5( $contents ); |
|
750 | - $cache = new autoptimizeCache( $hash, 'js' ); |
|
749 | + $hash = 'single_'.md5($contents); |
|
750 | + $cache = new autoptimizeCache($hash, 'js'); |
|
751 | 751 | |
752 | 752 | // If not in cache already, minify... |
753 | - if ( ! $cache->check() || $cache_miss ) { |
|
754 | - $contents = trim( JSMin::minify( $contents ) ); |
|
753 | + if (!$cache->check() || $cache_miss) { |
|
754 | + $contents = trim(JSMin::minify($contents)); |
|
755 | 755 | |
756 | 756 | // Check if minified cache content is empty. |
757 | - if ( empty( $contents ) ) { |
|
757 | + if (empty($contents)) { |
|
758 | 758 | return false; |
759 | 759 | } |
760 | 760 | |
761 | 761 | // Filter contents of excluded minified CSS. |
762 | - $contents = apply_filters( 'autoptimize_filter_js_single_after_minify', $contents ); |
|
762 | + $contents = apply_filters('autoptimize_filter_js_single_after_minify', $contents); |
|
763 | 763 | |
764 | 764 | // Store in cache. |
765 | - $cache->cache( $contents, 'text/javascript' ); |
|
765 | + $cache->cache($contents, 'text/javascript'); |
|
766 | 766 | } |
767 | 767 | |
768 | - $url = $this->build_minify_single_url( $cache ); |
|
768 | + $url = $this->build_minify_single_url($cache); |
|
769 | 769 | |
770 | 770 | return $url; |
771 | 771 | } |
@@ -439,7 +439,7 @@ discard block |
||
439 | 439 | * be inlined into a data: URI. Defaults to 4096, passed through |
440 | 440 | * `autoptimize_filter_css_datauri_maxsize` filter. |
441 | 441 | * |
442 | - * @return mixed |
|
442 | + * @return integer|null |
|
443 | 443 | */ |
444 | 444 | private function get_datauri_maxsize() |
445 | 445 | { |
@@ -460,6 +460,9 @@ discard block |
||
460 | 460 | return $max_size; |
461 | 461 | } |
462 | 462 | |
463 | + /** |
|
464 | + * @param false|string $url |
|
465 | + */ |
|
463 | 466 | private function check_datauri_exclude_list( $url ) |
464 | 467 | { |
465 | 468 | static $exclude_list = null; |
@@ -485,6 +488,9 @@ discard block |
||
485 | 488 | return $matched; |
486 | 489 | } |
487 | 490 | |
491 | + /** |
|
492 | + * @param false|string $path |
|
493 | + */ |
|
488 | 494 | private function build_or_get_datauri_image( $path ) |
489 | 495 | { |
490 | 496 | /** |
@@ -914,6 +920,11 @@ discard block |
||
914 | 920 | return true; |
915 | 921 | } |
916 | 922 | |
923 | + /** |
|
924 | + * @param string $code |
|
925 | + * |
|
926 | + * @return string |
|
927 | + */ |
|
917 | 928 | public function run_minifier_on( $code ) |
918 | 929 | { |
919 | 930 | if ( ! $this->alreadyminified ) { |
@@ -1135,6 +1146,9 @@ discard block |
||
1135 | 1146 | return $code; |
1136 | 1147 | } |
1137 | 1148 | |
1149 | + /** |
|
1150 | + * @param string $tag |
|
1151 | + */ |
|
1138 | 1152 | private function ismovable( $tag ) |
1139 | 1153 | { |
1140 | 1154 | if ( ! $this->aggregate ) { |
@@ -1164,6 +1178,9 @@ discard block |
||
1164 | 1178 | } |
1165 | 1179 | } |
1166 | 1180 | |
1181 | + /** |
|
1182 | + * @param string $css |
|
1183 | + */ |
|
1167 | 1184 | private function can_inject_late( $css_path, $css ) |
1168 | 1185 | { |
1169 | 1186 | $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false, $css_path ); |
@@ -1195,7 +1212,7 @@ discard block |
||
1195 | 1212 | * @param string $filepath Filepath. |
1196 | 1213 | * @param bool $cache_miss Optional. Force a cache miss. Default false. |
1197 | 1214 | * |
1198 | - * @return bool|string Url pointing to the minified css file or false. |
|
1215 | + * @return false|string Url pointing to the minified css file or false. |
|
1199 | 1216 | */ |
1200 | 1217 | public function minify_single( $filepath, $cache_miss = false ) |
1201 | 1218 | { |
@@ -1256,6 +1273,9 @@ discard block |
||
1256 | 1273 | $this->options = $options; |
1257 | 1274 | } |
1258 | 1275 | |
1276 | + /** |
|
1277 | + * @param string $name |
|
1278 | + */ |
|
1259 | 1279 | public function setOption( $name, $value ) |
1260 | 1280 | { |
1261 | 1281 | $this->options[ $name ] = $value; |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Class for CSS optimization. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -161,54 +161,54 @@ discard block |
||
161 | 161 | * |
162 | 162 | * @param array $options all options. |
163 | 163 | */ |
164 | - public function read( $options ) |
|
164 | + public function read($options) |
|
165 | 165 | { |
166 | - $noptimize_css = apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content ); |
|
167 | - if ( $noptimize_css ) { |
|
166 | + $noptimize_css = apply_filters('autoptimize_filter_css_noptimize', false, $this->content); |
|
167 | + if ($noptimize_css) { |
|
168 | 168 | return false; |
169 | 169 | } |
170 | 170 | |
171 | - $allowlist_css = apply_filters( 'autoptimize_filter_css_allowlist', '', $this->content ); |
|
172 | - $allowlist_css = apply_filters( 'autoptimize_filter_css_whitelist', $allowlist_css, $this->content ); // fixme: to be removed in next version. |
|
173 | - if ( ! empty( $allowlist_css ) ) { |
|
174 | - $this->allowlist = array_filter( array_map( 'trim', explode( ',', $allowlist_css ) ) ); |
|
171 | + $allowlist_css = apply_filters('autoptimize_filter_css_allowlist', '', $this->content); |
|
172 | + $allowlist_css = apply_filters('autoptimize_filter_css_whitelist', $allowlist_css, $this->content); // fixme: to be removed in next version. |
|
173 | + if (!empty($allowlist_css)) { |
|
174 | + $this->allowlist = array_filter(array_map('trim', explode(',', $allowlist_css))); |
|
175 | 175 | } |
176 | 176 | |
177 | - $removable_css = apply_filters( 'autoptimize_filter_css_removables', '' ); |
|
178 | - if ( ! empty( $removable_css ) ) { |
|
179 | - $this->cssremovables = array_filter( array_map( 'trim', explode( ',', $removable_css ) ) ); |
|
177 | + $removable_css = apply_filters('autoptimize_filter_css_removables', ''); |
|
178 | + if (!empty($removable_css)) { |
|
179 | + $this->cssremovables = array_filter(array_map('trim', explode(',', $removable_css))); |
|
180 | 180 | } |
181 | 181 | |
182 | - $this->cssinlinesize = apply_filters( 'autoptimize_filter_css_inlinesize', 256 ); |
|
182 | + $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize', 256); |
|
183 | 183 | |
184 | 184 | // filter to "late inject minified CSS", default to true for now (it is faster). |
185 | - $this->inject_min_late = apply_filters( 'autoptimize_filter_css_inject_min_late', true ); |
|
185 | + $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late', true); |
|
186 | 186 | |
187 | 187 | // Remove everything that's not the header. |
188 | - if ( apply_filters( 'autoptimize_filter_css_justhead', $options['justhead'] ) ) { |
|
189 | - $content = explode( '</head>', $this->content, 2 ); |
|
190 | - $this->content = $content[0] . '</head>'; |
|
188 | + if (apply_filters('autoptimize_filter_css_justhead', $options['justhead'])) { |
|
189 | + $content = explode('</head>', $this->content, 2); |
|
190 | + $this->content = $content[0].'</head>'; |
|
191 | 191 | $this->restofcontent = $content[1]; |
192 | 192 | } |
193 | 193 | |
194 | 194 | // Determine whether we're doing CSS-files aggregation or not. |
195 | - if ( isset( $options['aggregate'] ) && ! $options['aggregate'] ) { |
|
195 | + if (isset($options['aggregate']) && !$options['aggregate']) { |
|
196 | 196 | $this->aggregate = false; |
197 | 197 | } |
198 | 198 | // Returning true for "dontaggregate" turns off aggregation. |
199 | - if ( $this->aggregate && apply_filters( 'autoptimize_filter_css_dontaggregate', false ) ) { |
|
199 | + if ($this->aggregate && apply_filters('autoptimize_filter_css_dontaggregate', false)) { |
|
200 | 200 | $this->aggregate = false; |
201 | 201 | } |
202 | 202 | |
203 | 203 | // include inline? |
204 | - if ( apply_filters( 'autoptimize_css_include_inline', $options['include_inline'] ) ) { |
|
204 | + if (apply_filters('autoptimize_css_include_inline', $options['include_inline'])) { |
|
205 | 205 | $this->include_inline = true; |
206 | 206 | } |
207 | 207 | |
208 | 208 | // List of CSS strings which are excluded from autoptimization. |
209 | - $exclude_css = apply_filters( 'autoptimize_filter_css_exclude', $options['css_exclude'], $this->content ); |
|
210 | - if ( '' !== $exclude_css ) { |
|
211 | - $this->dontmove = array_filter( array_map( 'trim', explode( ',', $exclude_css ) ) ); |
|
209 | + $exclude_css = apply_filters('autoptimize_filter_css_exclude', $options['css_exclude'], $this->content); |
|
210 | + if ('' !== $exclude_css) { |
|
211 | + $this->dontmove = array_filter(array_map('trim', explode(',', $exclude_css))); |
|
212 | 212 | } else { |
213 | 213 | $this->dontmove = array(); |
214 | 214 | } |
@@ -219,16 +219,16 @@ discard block |
||
219 | 219 | // Should we defer css? |
220 | 220 | // value: true / false. |
221 | 221 | $this->defer = $options['defer']; |
222 | - $this->defer = apply_filters( 'autoptimize_filter_css_defer', $this->defer, $this->content ); |
|
222 | + $this->defer = apply_filters('autoptimize_filter_css_defer', $this->defer, $this->content); |
|
223 | 223 | |
224 | 224 | // Should we inline while deferring? |
225 | 225 | // value: inlined CSS. |
226 | - $this->defer_inline = apply_filters( 'autoptimize_filter_css_defer_inline', $options['defer_inline'], $this->content ); |
|
226 | + $this->defer_inline = apply_filters('autoptimize_filter_css_defer_inline', $options['defer_inline'], $this->content); |
|
227 | 227 | |
228 | 228 | // Should we inline? |
229 | 229 | // value: true / false. |
230 | 230 | $this->inline = $options['inline']; |
231 | - $this->inline = apply_filters( 'autoptimize_filter_css_inline', $this->inline, $this->content ); |
|
231 | + $this->inline = apply_filters('autoptimize_filter_css_inline', $this->inline, $this->content); |
|
232 | 232 | |
233 | 233 | // Store cdn url. |
234 | 234 | $this->cdn_url = $options['cdn_url']; |
@@ -237,16 +237,16 @@ discard block |
||
237 | 237 | $this->datauris = $options['datauris']; |
238 | 238 | |
239 | 239 | // Determine whether excluded files should be minified if not yet so. |
240 | - if ( ! $options['minify_excluded'] && $options['aggregate'] ) { |
|
240 | + if (!$options['minify_excluded'] && $options['aggregate']) { |
|
241 | 241 | $this->minify_excluded = false; |
242 | 242 | } |
243 | - $this->minify_excluded = apply_filters( 'autoptimize_filter_css_minify_excluded', $this->minify_excluded, '' ); |
|
243 | + $this->minify_excluded = apply_filters('autoptimize_filter_css_minify_excluded', $this->minify_excluded, ''); |
|
244 | 244 | |
245 | 245 | // should we force all media-attributes to all? |
246 | - $this->media_force_all = apply_filters( 'autoptimize_filter_css_tagmedia_forceall', false ); |
|
246 | + $this->media_force_all = apply_filters('autoptimize_filter_css_tagmedia_forceall', false); |
|
247 | 247 | |
248 | 248 | // noptimize me. |
249 | - $this->content = $this->hide_noptimize( $this->content ); |
|
249 | + $this->content = $this->hide_noptimize($this->content); |
|
250 | 250 | |
251 | 251 | // Exclude (no)script, as those may contain CSS which should be left as is. |
252 | 252 | $this->content = $this->replace_contents_with_marker_if_exists( |
@@ -257,25 +257,25 @@ discard block |
||
257 | 257 | ); |
258 | 258 | |
259 | 259 | // Save IE hacks. |
260 | - $this->content = $this->hide_iehacks( $this->content ); |
|
260 | + $this->content = $this->hide_iehacks($this->content); |
|
261 | 261 | |
262 | 262 | // Hide HTML comments. |
263 | - $this->content = $this->hide_comments( $this->content ); |
|
263 | + $this->content = $this->hide_comments($this->content); |
|
264 | 264 | |
265 | 265 | // Get <style> and <link>. |
266 | - if ( preg_match_all( '#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches ) ) { |
|
266 | + if (preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches)) { |
|
267 | 267 | |
268 | - foreach ( $matches[0] as $tag ) { |
|
269 | - if ( $this->isremovable( $tag, $this->cssremovables ) ) { |
|
270 | - $this->content = str_replace( $tag, '', $this->content ); |
|
271 | - } elseif ( $this->ismovable( $tag ) ) { |
|
268 | + foreach ($matches[0] as $tag) { |
|
269 | + if ($this->isremovable($tag, $this->cssremovables)) { |
|
270 | + $this->content = str_replace($tag, '', $this->content); |
|
271 | + } elseif ($this->ismovable($tag)) { |
|
272 | 272 | // Get the media. |
273 | - if ( false !== strpos( $tag, 'media=' ) ) { |
|
274 | - preg_match( '#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias ); |
|
275 | - $medias = explode( ',', $medias[1] ); |
|
273 | + if (false !== strpos($tag, 'media=')) { |
|
274 | + preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias); |
|
275 | + $medias = explode(',', $medias[1]); |
|
276 | 276 | $media = array(); |
277 | - foreach ( $medias as $elem ) { |
|
278 | - if ( empty( $elem ) ) { |
|
277 | + foreach ($medias as $elem) { |
|
278 | + if (empty($elem)) { |
|
279 | 279 | $elem = 'all'; |
280 | 280 | } |
281 | 281 | |
@@ -283,68 +283,68 @@ discard block |
||
283 | 283 | } |
284 | 284 | } else { |
285 | 285 | // No media specified - applies to all. |
286 | - $media = array( 'all' ); |
|
286 | + $media = array('all'); |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | // forcing media attribute to all to merge all in one file. |
290 | - if ( $this->media_force_all ) { |
|
291 | - $media = array( 'all' ); |
|
290 | + if ($this->media_force_all) { |
|
291 | + $media = array('all'); |
|
292 | 292 | } |
293 | 293 | |
294 | - $media = apply_filters( 'autoptimize_filter_css_tagmedia', $media, $tag ); |
|
294 | + $media = apply_filters('autoptimize_filter_css_tagmedia', $media, $tag); |
|
295 | 295 | |
296 | - if ( preg_match( '#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source ) ) { |
|
296 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
297 | 297 | // <link>. |
298 | - $url = current( explode( '?', $source[2], 2 ) ); |
|
299 | - $path = $this->getpath( $url ); |
|
298 | + $url = current(explode('?', $source[2], 2)); |
|
299 | + $path = $this->getpath($url); |
|
300 | 300 | |
301 | - if ( false !== $path && preg_match( '#\.css$#', $path ) ) { |
|
301 | + if (false !== $path && preg_match('#\.css$#', $path)) { |
|
302 | 302 | // Good link. |
303 | - $this->css[] = array( $media, $path ); |
|
303 | + $this->css[] = array($media, $path); |
|
304 | 304 | } else { |
305 | 305 | // Link is dynamic (.php etc). |
306 | - $new_tag = $this->optionally_defer_excluded( $tag, 'none' ); |
|
307 | - if ( '' !== $new_tag && $new_tag !== $tag ) { |
|
308 | - $this->content = str_replace( $tag, $new_tag, $this->content ); |
|
306 | + $new_tag = $this->optionally_defer_excluded($tag, 'none'); |
|
307 | + if ('' !== $new_tag && $new_tag !== $tag) { |
|
308 | + $this->content = str_replace($tag, $new_tag, $this->content); |
|
309 | 309 | } |
310 | 310 | $tag = ''; |
311 | 311 | } |
312 | 312 | } else { |
313 | 313 | // Inline css in style tags can be wrapped in comment tags, so restore comments. |
314 | - $tag = $this->restore_comments( $tag ); |
|
315 | - preg_match( '#<style.*>(.*)</style>#Usmi', $tag, $code ); |
|
314 | + $tag = $this->restore_comments($tag); |
|
315 | + preg_match('#<style.*>(.*)</style>#Usmi', $tag, $code); |
|
316 | 316 | |
317 | 317 | // And re-hide them to be able to to the removal based on tag. |
318 | - $tag = $this->hide_comments( $tag ); |
|
318 | + $tag = $this->hide_comments($tag); |
|
319 | 319 | |
320 | - if ( $this->include_inline ) { |
|
321 | - $code = preg_replace( '#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1] ); |
|
322 | - $this->css[] = array( $media, 'INLINE;' . $code ); |
|
320 | + if ($this->include_inline) { |
|
321 | + $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1]); |
|
322 | + $this->css[] = array($media, 'INLINE;'.$code); |
|
323 | 323 | } else { |
324 | 324 | $tag = ''; |
325 | 325 | } |
326 | 326 | } |
327 | 327 | |
328 | 328 | // Remove the original style tag. |
329 | - $this->content = str_replace( $tag, '', $this->content ); |
|
329 | + $this->content = str_replace($tag, '', $this->content); |
|
330 | 330 | } else { |
331 | - if ( preg_match( '#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source ) ) { |
|
332 | - $exploded_url = explode( '?', $source[2], 2 ); |
|
331 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
332 | + $exploded_url = explode('?', $source[2], 2); |
|
333 | 333 | $url = $exploded_url[0]; |
334 | - $path = $this->getpath( $url ); |
|
334 | + $path = $this->getpath($url); |
|
335 | 335 | $new_tag = $tag; |
336 | 336 | |
337 | 337 | // Excluded CSS, minify that file: |
338 | 338 | // -> if aggregate is on and exclude minify is on |
339 | 339 | // -> if aggregate is off and the file is not in dontmove. |
340 | - if ( $path && $this->minify_excluded ) { |
|
341 | - $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false ); |
|
342 | - if ( ( false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path ) || ( true === $this->aggregate && ( false === $consider_minified_array || str_replace( $consider_minified_array, '', $path ) === $path ) ) ) { |
|
343 | - $minified_url = $this->minify_single( $path ); |
|
344 | - if ( ! empty( $minified_url ) ) { |
|
340 | + if ($path && $this->minify_excluded) { |
|
341 | + $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false); |
|
342 | + if ((false === $this->aggregate && str_replace($this->dontmove, '', $path) === $path) || (true === $this->aggregate && (false === $consider_minified_array || str_replace($consider_minified_array, '', $path) === $path))) { |
|
343 | + $minified_url = $this->minify_single($path); |
|
344 | + if (!empty($minified_url)) { |
|
345 | 345 | // Replace orig URL with cached minified URL. |
346 | - $new_tag = str_replace( $url, $minified_url, $tag ); |
|
347 | - } elseif ( apply_filters( 'autoptimize_filter_ccsjs_remove_empty_minified_url', false ) ) { |
|
346 | + $new_tag = str_replace($url, $minified_url, $tag); |
|
347 | + } elseif (apply_filters('autoptimize_filter_ccsjs_remove_empty_minified_url', false)) { |
|
348 | 348 | // Remove the original style tag, because cache content is empty but only if |
349 | 349 | // filter is true-ed because $minified_url is also false if file is minified already. |
350 | 350 | $new_tag = ''; |
@@ -352,14 +352,14 @@ discard block |
||
352 | 352 | } |
353 | 353 | } |
354 | 354 | |
355 | - if ( '' !== $new_tag ) { |
|
355 | + if ('' !== $new_tag) { |
|
356 | 356 | // Optionally defer (preload) non-aggregated CSS. |
357 | - $new_tag = $this->optionally_defer_excluded( $new_tag, $url ); |
|
357 | + $new_tag = $this->optionally_defer_excluded($new_tag, $url); |
|
358 | 358 | } |
359 | 359 | |
360 | 360 | // And replace! |
361 | - if ( ( '' !== $new_tag && $new_tag !== $tag ) || ( '' === $new_tag && apply_filters( 'autoptimize_filter_css_remove_empty_files', false ) ) ) { |
|
362 | - $this->content = str_replace( $tag, $new_tag, $this->content ); |
|
361 | + if (('' !== $new_tag && $new_tag !== $tag) || ('' === $new_tag && apply_filters('autoptimize_filter_css_remove_empty_files', false))) { |
|
362 | + $this->content = str_replace($tag, $new_tag, $this->content); |
|
363 | 363 | } |
364 | 364 | } |
365 | 365 | } |
@@ -380,30 +380,30 @@ discard block |
||
380 | 380 | * |
381 | 381 | * @return string $new_tag |
382 | 382 | */ |
383 | - private function optionally_defer_excluded( $tag, $url = '' ) |
|
383 | + private function optionally_defer_excluded($tag, $url = '') |
|
384 | 384 | { |
385 | 385 | // Defer single CSS if "inline & defer" is ON and there is inline CSS. |
386 | - if ( ! empty( $tag ) && false === strpos( $tag, ' onload=' ) && $this->defer && ! empty( $this->defer_inline ) && apply_filters( 'autoptimize_filter_css_defer_excluded', true, $tag ) ) { |
|
386 | + if (!empty($tag) && false === strpos($tag, ' onload=') && $this->defer && !empty($this->defer_inline) && apply_filters('autoptimize_filter_css_defer_excluded', true, $tag)) { |
|
387 | 387 | // get media attribute and based on that create onload JS attribute value. |
388 | - if ( false === strpos( $tag, 'media=' ) ) { |
|
389 | - $tag = str_replace( '<link', "<link media='all'", $tag ); |
|
388 | + if (false === strpos($tag, 'media=')) { |
|
389 | + $tag = str_replace('<link', "<link media='all'", $tag); |
|
390 | 390 | } |
391 | 391 | |
392 | - preg_match( '#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $_medias ); |
|
392 | + preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $_medias); |
|
393 | 393 | $_media = $_medias[1]; |
394 | - $_preload_onload = autoptimizeConfig::get_ao_css_preload_onload( $_media ); |
|
394 | + $_preload_onload = autoptimizeConfig::get_ao_css_preload_onload($_media); |
|
395 | 395 | |
396 | - if ( 'print' !== $_media ) { |
|
396 | + if ('print' !== $_media) { |
|
397 | 397 | // If not media=print, adapt original <link> element for CSS to be preloaded and add <noscript>-version for fallback. |
398 | - $new_tag = '<noscript>' . autoptimizeUtils::remove_id_from_node( $tag ) . '</noscript>' . str_replace( |
|
398 | + $new_tag = '<noscript>'.autoptimizeUtils::remove_id_from_node($tag).'</noscript>'.str_replace( |
|
399 | 399 | $_medias[0], |
400 | - "media='print' onload=\"" . $_preload_onload . '"', |
|
400 | + "media='print' onload=\"".$_preload_onload.'"', |
|
401 | 401 | $tag |
402 | 402 | ); |
403 | 403 | |
404 | 404 | // Optionally (but default false) preload the (excluded) CSS-file. |
405 | - if ( apply_filters( 'autoptimize_fitler_css_preload_and_print', false ) && 'none' !== $url ) { |
|
406 | - $new_tag = '<link rel="preload" as="stylesheet" href="' . $url . '"/>' . $new_tag; |
|
405 | + if (apply_filters('autoptimize_fitler_css_preload_and_print', false) && 'none' !== $url) { |
|
406 | + $new_tag = '<link rel="preload" as="stylesheet" href="'.$url.'"/>'.$new_tag; |
|
407 | 407 | } |
408 | 408 | } else { |
409 | 409 | $new_tag = $tag; |
@@ -423,22 +423,22 @@ discard block |
||
423 | 423 | * @param string $path image path. |
424 | 424 | * @return boolean |
425 | 425 | */ |
426 | - private function is_datauri_candidate( $path ) |
|
426 | + private function is_datauri_candidate($path) |
|
427 | 427 | { |
428 | 428 | // Call only once since it's called from a loop. |
429 | 429 | static $max_size = null; |
430 | - if ( null === $max_size ) { |
|
430 | + if (null === $max_size) { |
|
431 | 431 | $max_size = $this->get_datauri_maxsize(); |
432 | 432 | } |
433 | 433 | |
434 | - if ( $path && preg_match( '#\.(jpe?g|png|gif|webp|bmp)$#i', $path ) && |
|
435 | - file_exists( $path ) && is_readable( $path ) && filesize( $path ) <= $max_size ) { |
|
434 | + if ($path && preg_match('#\.(jpe?g|png|gif|webp|bmp)$#i', $path) && |
|
435 | + file_exists($path) && is_readable($path) && filesize($path) <= $max_size) { |
|
436 | 436 | |
437 | 437 | // Seems we have a candidate. |
438 | 438 | $is_candidate = true; |
439 | 439 | } else { |
440 | 440 | // Filter allows overriding default decision (which checks for local file existence). |
441 | - $is_candidate = apply_filters( 'autoptimize_filter_css_is_datauri_candidate', false, $path ); |
|
441 | + $is_candidate = apply_filters('autoptimize_filter_css_is_datauri_candidate', false, $path); |
|
442 | 442 | } |
443 | 443 | |
444 | 444 | return $is_candidate; |
@@ -463,29 +463,29 @@ discard block |
||
463 | 463 | * of thing you're probably better of building assets completely |
464 | 464 | * outside of WordPress anyway. |
465 | 465 | */ |
466 | - if ( null === $max_size ) { |
|
467 | - $max_size = (int) apply_filters( 'autoptimize_filter_css_datauri_maxsize', 4096 ); |
|
466 | + if (null === $max_size) { |
|
467 | + $max_size = (int) apply_filters('autoptimize_filter_css_datauri_maxsize', 4096); |
|
468 | 468 | } |
469 | 469 | |
470 | 470 | return $max_size; |
471 | 471 | } |
472 | 472 | |
473 | - private function check_datauri_exclude_list( $url ) |
|
473 | + private function check_datauri_exclude_list($url) |
|
474 | 474 | { |
475 | 475 | static $exclude_list = null; |
476 | 476 | $no_datauris = array(); |
477 | 477 | |
478 | 478 | // Again, skip doing certain stuff repeatedly when loop-called. |
479 | - if ( null === $exclude_list ) { |
|
480 | - $exclude_list = apply_filters( 'autoptimize_filter_css_datauri_exclude', '' ); |
|
481 | - $no_datauris = array_filter( array_map( 'trim', explode( ',', $exclude_list ) ) ); |
|
479 | + if (null === $exclude_list) { |
|
480 | + $exclude_list = apply_filters('autoptimize_filter_css_datauri_exclude', ''); |
|
481 | + $no_datauris = array_filter(array_map('trim', explode(',', $exclude_list))); |
|
482 | 482 | } |
483 | 483 | |
484 | 484 | $matched = false; |
485 | 485 | |
486 | - if ( ! empty( $exclude_list ) ) { |
|
487 | - foreach ( $no_datauris as $no_datauri ) { |
|
488 | - if ( false !== strpos( $url, $no_datauri ) ) { |
|
486 | + if (!empty($exclude_list)) { |
|
487 | + foreach ($no_datauris as $no_datauri) { |
|
488 | + if (false !== strpos($url, $no_datauri)) { |
|
489 | 489 | $matched = true; |
490 | 490 | break; |
491 | 491 | } |
@@ -495,7 +495,7 @@ discard block |
||
495 | 495 | return $matched; |
496 | 496 | } |
497 | 497 | |
498 | - private function build_or_get_datauri_image( $path ) |
|
498 | + private function build_or_get_datauri_image($path) |
|
499 | 499 | { |
500 | 500 | /** |
501 | 501 | * TODO/FIXME: document the required return array format, or better yet, |
@@ -504,27 +504,27 @@ discard block |
||
504 | 504 | */ |
505 | 505 | |
506 | 506 | // Allows short-circuiting datauri generation for an image. |
507 | - $result = apply_filters( 'autoptimize_filter_css_datauri_image', array(), $path ); |
|
508 | - if ( ! empty( $result ) ) { |
|
509 | - if ( is_array( $result ) && isset( $result['full'] ) && isset( $result['base64data'] ) ) { |
|
507 | + $result = apply_filters('autoptimize_filter_css_datauri_image', array(), $path); |
|
508 | + if (!empty($result)) { |
|
509 | + if (is_array($result) && isset($result['full']) && isset($result['base64data'])) { |
|
510 | 510 | return $result; |
511 | 511 | } |
512 | 512 | } |
513 | 513 | |
514 | - $hash = md5( $path ); |
|
515 | - $check = new autoptimizeCache( $hash, 'img' ); |
|
516 | - if ( $check->check() ) { |
|
514 | + $hash = md5($path); |
|
515 | + $check = new autoptimizeCache($hash, 'img'); |
|
516 | + if ($check->check()) { |
|
517 | 517 | // we have the base64 image in cache. |
518 | 518 | $head_and_data = $check->retrieve(); |
519 | - $_base64data = explode( ';base64,', $head_and_data ); |
|
519 | + $_base64data = explode(';base64,', $head_and_data); |
|
520 | 520 | $base64data = $_base64data[1]; |
521 | - unset( $_base64data ); |
|
521 | + unset($_base64data); |
|
522 | 522 | } else { |
523 | 523 | // It's an image and we don't have it in cache, get the type by extension. |
524 | - $exploded_path = explode( '.', $path ); |
|
525 | - $type = end( $exploded_path ); |
|
524 | + $exploded_path = explode('.', $path); |
|
525 | + $type = end($exploded_path); |
|
526 | 526 | |
527 | - switch ( $type ) { |
|
527 | + switch ($type) { |
|
528 | 528 | case 'jpg': |
529 | 529 | case 'jpeg': |
530 | 530 | $dataurihead = 'data:image/jpeg;base64,'; |
@@ -546,13 +546,13 @@ discard block |
||
546 | 546 | } |
547 | 547 | |
548 | 548 | // Encode the data. |
549 | - $base64data = base64_encode( file_get_contents( $path ) ); |
|
550 | - $head_and_data = $dataurihead . $base64data; |
|
549 | + $base64data = base64_encode(file_get_contents($path)); |
|
550 | + $head_and_data = $dataurihead.$base64data; |
|
551 | 551 | |
552 | 552 | // Save in cache. |
553 | - $check->cache( $head_and_data, 'text/plain' ); |
|
553 | + $check->cache($head_and_data, 'text/plain'); |
|
554 | 554 | } |
555 | - unset( $check ); |
|
555 | + unset($check); |
|
556 | 556 | |
557 | 557 | return array( |
558 | 558 | 'full' => $head_and_data, |
@@ -569,13 +569,13 @@ discard block |
||
569 | 569 | * |
570 | 570 | * @return string |
571 | 571 | */ |
572 | - protected static function replace_longest_matches_first( $string, $replacements = array() ) |
|
572 | + protected static function replace_longest_matches_first($string, $replacements = array()) |
|
573 | 573 | { |
574 | - if ( ! empty( $replacements ) ) { |
|
574 | + if (!empty($replacements)) { |
|
575 | 575 | // Sort the replacements array by key length in desc order (so that the longest strings are replaced first). |
576 | - $keys = array_map( 'strlen', array_keys( $replacements ) ); |
|
577 | - array_multisort( $keys, SORT_DESC, $replacements ); |
|
578 | - $string = str_replace( array_keys( $replacements ), array_values( $replacements ), $string ); |
|
576 | + $keys = array_map('strlen', array_keys($replacements)); |
|
577 | + array_multisort($keys, SORT_DESC, $replacements); |
|
578 | + $string = str_replace(array_keys($replacements), array_values($replacements), $string); |
|
579 | 579 | } |
580 | 580 | |
581 | 581 | return $string; |
@@ -591,15 +591,15 @@ discard block |
||
591 | 591 | * |
592 | 592 | * @return string |
593 | 593 | */ |
594 | - public function replace_urls( $code = '' ) |
|
594 | + public function replace_urls($code = '') |
|
595 | 595 | { |
596 | 596 | $replacements = array(); |
597 | 597 | |
598 | - preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches ); |
|
599 | - if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) { |
|
600 | - foreach ( $url_src_matches[1] as $count => $original_url ) { |
|
598 | + preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches); |
|
599 | + if (is_array($url_src_matches) && !empty($url_src_matches)) { |
|
600 | + foreach ($url_src_matches[1] as $count => $original_url) { |
|
601 | 601 | // Removes quotes and other cruft. |
602 | - $url = trim( $original_url, " \t\n\r\0\x0B\"'" ); |
|
602 | + $url = trim($original_url, " \t\n\r\0\x0B\"'"); |
|
603 | 603 | |
604 | 604 | /** |
605 | 605 | * TODO/FIXME: Add a way for other code / callable to be called here |
@@ -617,17 +617,17 @@ discard block |
||
617 | 617 | */ |
618 | 618 | |
619 | 619 | // Do CDN replacement if needed. |
620 | - if ( ! empty( $this->cdn_url ) ) { |
|
621 | - $replacement_url = $this->url_replace_cdn( $url ); |
|
620 | + if (!empty($this->cdn_url)) { |
|
621 | + $replacement_url = $this->url_replace_cdn($url); |
|
622 | 622 | // Prepare replacements array. |
623 | - $replacements[ $url_src_matches[1][ $count ] ] = str_replace( |
|
624 | - $original_url, $replacement_url, $url_src_matches[1][ $count ] |
|
623 | + $replacements[$url_src_matches[1][$count]] = str_replace( |
|
624 | + $original_url, $replacement_url, $url_src_matches[1][$count] |
|
625 | 625 | ); |
626 | 626 | } |
627 | 627 | } |
628 | 628 | } |
629 | 629 | |
630 | - $code = self::replace_longest_matches_first( $code, $replacements ); |
|
630 | + $code = self::replace_longest_matches_first($code, $replacements); |
|
631 | 631 | |
632 | 632 | return $code; |
633 | 633 | } |
@@ -640,26 +640,26 @@ discard block |
||
640 | 640 | * @param string $code HTML being processed to hide fonts. |
641 | 641 | * @return string |
642 | 642 | */ |
643 | - public function hide_fontface_and_maybe_cdn( $code ) |
|
643 | + public function hide_fontface_and_maybe_cdn($code) |
|
644 | 644 | { |
645 | 645 | // Proceed only if @font-face declarations exist within $code. |
646 | - preg_match_all( self::FONT_FACE_REGEX, $code, $fontfaces ); |
|
647 | - if ( isset( $fontfaces[0] ) ) { |
|
646 | + preg_match_all(self::FONT_FACE_REGEX, $code, $fontfaces); |
|
647 | + if (isset($fontfaces[0])) { |
|
648 | 648 | // Check if we need to cdn fonts or not. |
649 | - $do_font_cdn = apply_filters( 'autoptimize_filter_css_fonts_cdn', false ); |
|
649 | + $do_font_cdn = apply_filters('autoptimize_filter_css_fonts_cdn', false); |
|
650 | 650 | |
651 | - foreach ( $fontfaces[0] as $full_match ) { |
|
651 | + foreach ($fontfaces[0] as $full_match) { |
|
652 | 652 | // Keep original match so we can search/replace it. |
653 | 653 | $match_search = $full_match; |
654 | 654 | |
655 | 655 | // Do font cdn if needed. |
656 | - if ( $do_font_cdn ) { |
|
657 | - $full_match = $this->replace_urls( $full_match ); |
|
656 | + if ($do_font_cdn) { |
|
657 | + $full_match = $this->replace_urls($full_match); |
|
658 | 658 | } |
659 | 659 | |
660 | 660 | // Replace declaration with its base64 encoded string. |
661 | - $replacement = self::build_marker( 'FONTFACE', $full_match ); |
|
662 | - $code = str_replace( $match_search, $replacement, $code ); |
|
661 | + $replacement = self::build_marker('FONTFACE', $full_match); |
|
662 | + $code = str_replace($match_search, $replacement, $code); |
|
663 | 663 | } |
664 | 664 | } |
665 | 665 | |
@@ -673,9 +673,9 @@ discard block |
||
673 | 673 | * @param string $code HTML being processed to unhide fonts. |
674 | 674 | * @return string |
675 | 675 | */ |
676 | - public function restore_fontface( $code ) |
|
676 | + public function restore_fontface($code) |
|
677 | 677 | { |
678 | - return $this->restore_marked_content( 'FONTFACE', $code ); |
|
678 | + return $this->restore_marked_content('FONTFACE', $code); |
|
679 | 679 | } |
680 | 680 | |
681 | 681 | /** |
@@ -684,10 +684,10 @@ discard block |
||
684 | 684 | * @param string $code HTML being processed rewrite assets. |
685 | 685 | * @return string |
686 | 686 | */ |
687 | - public function rewrite_assets( $code ) |
|
687 | + public function rewrite_assets($code) |
|
688 | 688 | { |
689 | 689 | // Handle @font-face rules by hiding and processing them separately. |
690 | - $code = $this->hide_fontface_and_maybe_cdn( $code ); |
|
690 | + $code = $this->hide_fontface_and_maybe_cdn($code); |
|
691 | 691 | |
692 | 692 | /** |
693 | 693 | * TODO/FIXME: |
@@ -704,35 +704,35 @@ discard block |
||
704 | 704 | $imgreplace = array(); |
705 | 705 | |
706 | 706 | // Matches and captures anything specified within the literal `url()` and excludes those containing data: URIs. |
707 | - preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches ); |
|
708 | - if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) { |
|
709 | - foreach ( $url_src_matches[1] as $count => $original_url ) { |
|
707 | + preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches); |
|
708 | + if (is_array($url_src_matches) && !empty($url_src_matches)) { |
|
709 | + foreach ($url_src_matches[1] as $count => $original_url) { |
|
710 | 710 | // Removes quotes and other cruft. |
711 | - $url = trim( $original_url, " \t\n\r\0\x0B\"'" ); |
|
711 | + $url = trim($original_url, " \t\n\r\0\x0B\"'"); |
|
712 | 712 | |
713 | 713 | // If datauri inlining is turned on, do it. |
714 | 714 | $inlined = false; |
715 | - if ( $this->datauris ) { |
|
715 | + if ($this->datauris) { |
|
716 | 716 | $iurl = $url; |
717 | - if ( false !== strpos( $iurl, '?' ) ) { |
|
718 | - $iurl = strtok( $iurl, '?' ); |
|
717 | + if (false !== strpos($iurl, '?')) { |
|
718 | + $iurl = strtok($iurl, '?'); |
|
719 | 719 | } |
720 | 720 | |
721 | - $ipath = $this->getpath( $iurl ); |
|
721 | + $ipath = $this->getpath($iurl); |
|
722 | 722 | |
723 | - $excluded = $this->check_datauri_exclude_list( $ipath ); |
|
724 | - if ( ! $excluded ) { |
|
725 | - $is_datauri_candidate = $this->is_datauri_candidate( $ipath ); |
|
726 | - if ( $is_datauri_candidate ) { |
|
727 | - $datauri = $this->build_or_get_datauri_image( $ipath ); |
|
723 | + $excluded = $this->check_datauri_exclude_list($ipath); |
|
724 | + if (!$excluded) { |
|
725 | + $is_datauri_candidate = $this->is_datauri_candidate($ipath); |
|
726 | + if ($is_datauri_candidate) { |
|
727 | + $datauri = $this->build_or_get_datauri_image($ipath); |
|
728 | 728 | $base64data = $datauri['base64data']; |
729 | 729 | // Add it to the list for replacement. |
730 | - $imgreplace[ $url_src_matches[1][ $count ] ] = str_replace( |
|
730 | + $imgreplace[$url_src_matches[1][$count]] = str_replace( |
|
731 | 731 | $original_url, |
732 | 732 | $datauri['full'], |
733 | - $url_src_matches[1][ $count ] |
|
733 | + $url_src_matches[1][$count] |
|
734 | 734 | ); |
735 | - $inlined = true; |
|
735 | + $inlined = true; |
|
736 | 736 | } |
737 | 737 | } |
738 | 738 | } |
@@ -743,20 +743,20 @@ discard block |
||
743 | 743 | * inlining isn't turned on, or if a resource is skipped from |
744 | 744 | * being inlined for whatever reason above. |
745 | 745 | */ |
746 | - if ( ! $inlined && ( ! empty( $this->cdn_url ) || has_filter( 'autoptimize_filter_base_replace_cdn' ) ) ) { |
|
746 | + if (!$inlined && (!empty($this->cdn_url) || has_filter('autoptimize_filter_base_replace_cdn'))) { |
|
747 | 747 | // Just do the "simple" CDN replacement. |
748 | - $replacement_url = $this->url_replace_cdn( $url ); |
|
749 | - $imgreplace[ $url_src_matches[1][ $count ] ] = str_replace( |
|
750 | - $original_url, $replacement_url, $url_src_matches[1][ $count ] |
|
748 | + $replacement_url = $this->url_replace_cdn($url); |
|
749 | + $imgreplace[$url_src_matches[1][$count]] = str_replace( |
|
750 | + $original_url, $replacement_url, $url_src_matches[1][$count] |
|
751 | 751 | ); |
752 | 752 | } |
753 | 753 | } |
754 | 754 | } |
755 | 755 | |
756 | - $code = self::replace_longest_matches_first( $code, $imgreplace ); |
|
756 | + $code = self::replace_longest_matches_first($code, $imgreplace); |
|
757 | 757 | |
758 | 758 | // Replace back font-face markers with actual font-face declarations. |
759 | - $code = $this->restore_fontface( $code ); |
|
759 | + $code = $this->restore_fontface($code); |
|
760 | 760 | |
761 | 761 | return $code; |
762 | 762 | } |
@@ -766,29 +766,29 @@ discard block |
||
766 | 766 | */ |
767 | 767 | public function minify() |
768 | 768 | { |
769 | - foreach ( $this->css as $group ) { |
|
770 | - list( $media, $css ) = $group; |
|
771 | - if ( preg_match( '#^INLINE;#', $css ) ) { |
|
769 | + foreach ($this->css as $group) { |
|
770 | + list($media, $css) = $group; |
|
771 | + if (preg_match('#^INLINE;#', $css)) { |
|
772 | 772 | // <style>. |
773 | - $css = preg_replace( '#^INLINE;#', '', $css ); |
|
774 | - $css = self::fixurls( ABSPATH . 'index.php', $css ); // ABSPATH already contains a trailing slash. |
|
775 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, '' ); |
|
776 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
773 | + $css = preg_replace('#^INLINE;#', '', $css); |
|
774 | + $css = self::fixurls(ABSPATH.'index.php', $css); // ABSPATH already contains a trailing slash. |
|
775 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, ''); |
|
776 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
777 | 777 | $css = $tmpstyle; |
778 | 778 | $this->alreadyminified = true; |
779 | 779 | } |
780 | 780 | } else { |
781 | 781 | // <link> |
782 | - if ( false !== $css && file_exists( $css ) && is_readable( $css ) ) { |
|
782 | + if (false !== $css && file_exists($css) && is_readable($css)) { |
|
783 | 783 | $css_path = $css; |
784 | - $css = self::fixurls( $css_path, file_get_contents( $css_path ) ); |
|
785 | - $css = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $css ); |
|
786 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $css_path ); |
|
787 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
784 | + $css = self::fixurls($css_path, file_get_contents($css_path)); |
|
785 | + $css = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $css); |
|
786 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, $css_path); |
|
787 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
788 | 788 | $css = $tmpstyle; |
789 | 789 | $this->alreadyminified = true; |
790 | - } elseif ( $this->can_inject_late( $css_path, $css ) ) { |
|
791 | - $css = self::build_injectlater_marker( $css_path, md5( $css ) ); |
|
790 | + } elseif ($this->can_inject_late($css_path, $css)) { |
|
791 | + $css = self::build_injectlater_marker($css_path, md5($css)); |
|
792 | 792 | } |
793 | 793 | } else { |
794 | 794 | // Couldn't read CSS. Maybe getpath isn't working? |
@@ -796,12 +796,12 @@ discard block |
||
796 | 796 | } |
797 | 797 | } |
798 | 798 | |
799 | - foreach ( $media as $elem ) { |
|
800 | - if ( ! empty( $css ) ) { |
|
801 | - if ( ! isset( $this->csscode[ $elem ] ) ) { |
|
802 | - $this->csscode[ $elem ] = ''; |
|
799 | + foreach ($media as $elem) { |
|
800 | + if (!empty($css)) { |
|
801 | + if (!isset($this->csscode[$elem])) { |
|
802 | + $this->csscode[$elem] = ''; |
|
803 | 803 | } |
804 | - $this->csscode[ $elem ] .= "\n/*FILESTART*/" . $css; |
|
804 | + $this->csscode[$elem] .= "\n/*FILESTART*/".$css; |
|
805 | 805 | } |
806 | 806 | } |
807 | 807 | } |
@@ -809,133 +809,133 @@ discard block |
||
809 | 809 | // Check for duplicate code. |
810 | 810 | $md5list = array(); |
811 | 811 | $tmpcss = $this->csscode; |
812 | - foreach ( $tmpcss as $media => $code ) { |
|
813 | - $md5sum = md5( $code ); |
|
812 | + foreach ($tmpcss as $media => $code) { |
|
813 | + $md5sum = md5($code); |
|
814 | 814 | $medianame = $media; |
815 | - foreach ( $md5list as $med => $sum ) { |
|
815 | + foreach ($md5list as $med => $sum) { |
|
816 | 816 | // If same code. |
817 | - if ( $sum === $md5sum ) { |
|
817 | + if ($sum === $md5sum) { |
|
818 | 818 | // Add the merged code. |
819 | - $medianame = $med . ', ' . $media; |
|
820 | - $this->csscode[ $medianame ] = $code; |
|
821 | - $md5list[ $medianame ] = $md5list[ $med ]; |
|
822 | - unset( $this->csscode[ $med ], $this->csscode[ $media ], $md5list[ $med ] ); |
|
819 | + $medianame = $med.', '.$media; |
|
820 | + $this->csscode[$medianame] = $code; |
|
821 | + $md5list[$medianame] = $md5list[$med]; |
|
822 | + unset($this->csscode[$med], $this->csscode[$media], $md5list[$med]); |
|
823 | 823 | } |
824 | 824 | } |
825 | - $md5list[ $medianame ] = $md5sum; |
|
825 | + $md5list[$medianame] = $md5sum; |
|
826 | 826 | } |
827 | - unset( $tmpcss ); |
|
827 | + unset($tmpcss); |
|
828 | 828 | |
829 | 829 | // Manage @imports, while is for recursive import management. |
830 | - foreach ( $this->csscode as &$thiscss ) { |
|
830 | + foreach ($this->csscode as &$thiscss) { |
|
831 | 831 | // Flag to trigger import reconstitution and var to hold external imports. |
832 | 832 | $fiximports = false; |
833 | 833 | $external_imports = ''; |
834 | 834 | |
835 | 835 | // remove comments to avoid importing commented-out imports. |
836 | - $thiscss_nocomments = preg_replace( '#/\*.*\*/#Us', '', $thiscss ); |
|
837 | - while ( preg_match_all( '#@import +(?:url)?(?:(?:\((["\']?)(?:[^"\')]+)\1\)|(["\'])(?:[^"\']+)\2)(?:[^,;"\']+(?:,[^,;"\']+)*)?)(?:;)#mi', $thiscss_nocomments, $matches ) ) { |
|
838 | - foreach ( $matches[0] as $import ) { |
|
839 | - if ( $this->isremovable( $import, $this->cssremovables ) ) { |
|
840 | - $thiscss = str_replace( $import, '', $thiscss ); |
|
836 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
837 | + while (preg_match_all('#@import +(?:url)?(?:(?:\((["\']?)(?:[^"\')]+)\1\)|(["\'])(?:[^"\']+)\2)(?:[^,;"\']+(?:,[^,;"\']+)*)?)(?:;)#mi', $thiscss_nocomments, $matches)) { |
|
838 | + foreach ($matches[0] as $import) { |
|
839 | + if ($this->isremovable($import, $this->cssremovables)) { |
|
840 | + $thiscss = str_replace($import, '', $thiscss); |
|
841 | 841 | $import_ok = true; |
842 | 842 | } else { |
843 | - $url = trim( preg_replace( '#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim( $import ) ), " \t\n\r\0\x0B\"'" ); |
|
844 | - $path = $this->getpath( $url ); |
|
843 | + $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim($import)), " \t\n\r\0\x0B\"'"); |
|
844 | + $path = $this->getpath($url); |
|
845 | 845 | $import_ok = false; |
846 | - if ( file_exists( $path ) && is_readable( $path ) ) { |
|
847 | - $code = addcslashes( self::fixurls( $path, file_get_contents( $path ) ), '\\' ); |
|
848 | - $code = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $code ); |
|
849 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, '' ); |
|
850 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
846 | + if (file_exists($path) && is_readable($path)) { |
|
847 | + $code = addcslashes(self::fixurls($path, file_get_contents($path)), '\\'); |
|
848 | + $code = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $code); |
|
849 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $code, ''); |
|
850 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
851 | 851 | $code = $tmpstyle; |
852 | 852 | $this->alreadyminified = true; |
853 | - } elseif ( $this->can_inject_late( $path, $code ) ) { |
|
854 | - $code = self::build_injectlater_marker( $path, md5( $code ) ); |
|
853 | + } elseif ($this->can_inject_late($path, $code)) { |
|
854 | + $code = self::build_injectlater_marker($path, md5($code)); |
|
855 | 855 | } |
856 | 856 | |
857 | - if ( ! empty( $code ) ) { |
|
858 | - $tmp_thiscss = preg_replace( '#(/\*FILESTART\*/.*)' . preg_quote( $import, '#' ) . '#Us', '/*FILESTART2*/' . $code . '$1', $thiscss ); |
|
859 | - if ( ! empty( $tmp_thiscss ) ) { |
|
857 | + if (!empty($code)) { |
|
858 | + $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import, '#').'#Us', '/*FILESTART2*/'.$code.'$1', $thiscss); |
|
859 | + if (!empty($tmp_thiscss)) { |
|
860 | 860 | $thiscss = $tmp_thiscss; |
861 | 861 | $import_ok = true; |
862 | - unset( $tmp_thiscss ); |
|
862 | + unset($tmp_thiscss); |
|
863 | 863 | } |
864 | 864 | } |
865 | - unset( $code ); |
|
865 | + unset($code); |
|
866 | 866 | } |
867 | 867 | } |
868 | - if ( ! $import_ok ) { |
|
868 | + if (!$import_ok) { |
|
869 | 869 | // External imports and general fall-back. |
870 | 870 | $external_imports .= $import; |
871 | 871 | |
872 | - $thiscss = str_replace( $import, '', $thiscss ); |
|
872 | + $thiscss = str_replace($import, '', $thiscss); |
|
873 | 873 | $fiximports = true; |
874 | 874 | } |
875 | 875 | } |
876 | - $thiscss = preg_replace( '#/\*FILESTART\*/#', '', $thiscss ); |
|
877 | - $thiscss = preg_replace( '#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss ); |
|
876 | + $thiscss = preg_replace('#/\*FILESTART\*/#', '', $thiscss); |
|
877 | + $thiscss = preg_replace('#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss); |
|
878 | 878 | |
879 | 879 | // and update $thiscss_nocomments before going into next iteration in while loop. |
880 | - $thiscss_nocomments = preg_replace( '#/\*.*\*/#Us', '', $thiscss ); |
|
880 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
881 | 881 | } |
882 | - unset( $thiscss_nocomments ); |
|
882 | + unset($thiscss_nocomments); |
|
883 | 883 | |
884 | 884 | // Add external imports to top of aggregated CSS. |
885 | - if ( $fiximports ) { |
|
886 | - $thiscss = $external_imports . $thiscss; |
|
885 | + if ($fiximports) { |
|
886 | + $thiscss = $external_imports.$thiscss; |
|
887 | 887 | } |
888 | 888 | } |
889 | - unset( $thiscss ); |
|
889 | + unset($thiscss); |
|
890 | 890 | |
891 | 891 | // $this->csscode has all the uncompressed code now. |
892 | - foreach ( $this->csscode as &$code ) { |
|
892 | + foreach ($this->csscode as &$code) { |
|
893 | 893 | // Check for already-minified code. |
894 | - $hash = md5( $code ); |
|
895 | - do_action( 'autoptimize_action_css_hash', $hash ); |
|
896 | - $ccheck = new autoptimizeCache( $hash, 'css' ); |
|
897 | - if ( $ccheck->check() ) { |
|
894 | + $hash = md5($code); |
|
895 | + do_action('autoptimize_action_css_hash', $hash); |
|
896 | + $ccheck = new autoptimizeCache($hash, 'css'); |
|
897 | + if ($ccheck->check()) { |
|
898 | 898 | $code = $ccheck->retrieve(); |
899 | - $this->hashmap[ md5( $code ) ] = $hash; |
|
899 | + $this->hashmap[md5($code)] = $hash; |
|
900 | 900 | continue; |
901 | 901 | } |
902 | - unset( $ccheck ); |
|
902 | + unset($ccheck); |
|
903 | 903 | |
904 | 904 | // Rewrite and/or inline referenced assets. |
905 | - $code = $this->rewrite_assets( $code ); |
|
905 | + $code = $this->rewrite_assets($code); |
|
906 | 906 | |
907 | 907 | // Minify. |
908 | - $code = $this->run_minifier_on( $code ); |
|
908 | + $code = $this->run_minifier_on($code); |
|
909 | 909 | |
910 | 910 | // Bring back INJECTLATER stuff. |
911 | - $code = $this->inject_minified( $code ); |
|
911 | + $code = $this->inject_minified($code); |
|
912 | 912 | |
913 | 913 | // Filter results. |
914 | - $tmp_code = apply_filters( 'autoptimize_css_after_minify', $code ); |
|
915 | - if ( ! empty( $tmp_code ) ) { |
|
914 | + $tmp_code = apply_filters('autoptimize_css_after_minify', $code); |
|
915 | + if (!empty($tmp_code)) { |
|
916 | 916 | $code = $tmp_code; |
917 | - unset( $tmp_code ); |
|
917 | + unset($tmp_code); |
|
918 | 918 | } |
919 | 919 | |
920 | - $this->hashmap[ md5( $code ) ] = $hash; |
|
920 | + $this->hashmap[md5($code)] = $hash; |
|
921 | 921 | } |
922 | 922 | |
923 | - unset( $code ); |
|
923 | + unset($code); |
|
924 | 924 | return true; |
925 | 925 | } |
926 | 926 | |
927 | - public function run_minifier_on( $code ) |
|
927 | + public function run_minifier_on($code) |
|
928 | 928 | { |
929 | - if ( ! $this->alreadyminified ) { |
|
930 | - $do_minify = apply_filters( 'autoptimize_css_do_minify', true ); |
|
929 | + if (!$this->alreadyminified) { |
|
930 | + $do_minify = apply_filters('autoptimize_css_do_minify', true); |
|
931 | 931 | |
932 | - if ( $do_minify ) { |
|
932 | + if ($do_minify) { |
|
933 | 933 | $cssmin = new autoptimizeCSSmin(); |
934 | - $tmp_code = trim( $cssmin->run( $code ) ); |
|
934 | + $tmp_code = trim($cssmin->run($code)); |
|
935 | 935 | |
936 | - if ( ! empty( $tmp_code ) ) { |
|
936 | + if (!empty($tmp_code)) { |
|
937 | 937 | $code = $tmp_code; |
938 | - unset( $tmp_code ); |
|
938 | + unset($tmp_code); |
|
939 | 939 | } |
940 | 940 | } |
941 | 941 | } |
@@ -949,18 +949,18 @@ discard block |
||
949 | 949 | public function cache() |
950 | 950 | { |
951 | 951 | // CSS cache. |
952 | - foreach ( $this->csscode as $media => $code ) { |
|
953 | - if ( empty( $code ) ) { |
|
952 | + foreach ($this->csscode as $media => $code) { |
|
953 | + if (empty($code)) { |
|
954 | 954 | continue; |
955 | 955 | } |
956 | 956 | |
957 | - $md5 = $this->hashmap[ md5( $code ) ]; |
|
958 | - $cache = new autoptimizeCache( $md5, 'css' ); |
|
959 | - if ( ! $cache->check() ) { |
|
957 | + $md5 = $this->hashmap[md5($code)]; |
|
958 | + $cache = new autoptimizeCache($md5, 'css'); |
|
959 | + if (!$cache->check()) { |
|
960 | 960 | // Cache our code. |
961 | - $cache->cache( $code, 'text/css' ); |
|
961 | + $cache->cache($code, 'text/css'); |
|
962 | 962 | } |
963 | - $this->url[ $media ] = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
|
963 | + $this->url[$media] = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
|
964 | 964 | } |
965 | 965 | } |
966 | 966 | |
@@ -970,95 +970,95 @@ discard block |
||
970 | 970 | public function getcontent() |
971 | 971 | { |
972 | 972 | // Restore the full content (only applies when "autoptimize_filter_css_justhead" filter is true). |
973 | - if ( ! empty( $this->restofcontent ) ) { |
|
973 | + if (!empty($this->restofcontent)) { |
|
974 | 974 | $this->content .= $this->restofcontent; |
975 | 975 | $this->restofcontent = ''; |
976 | 976 | } |
977 | 977 | |
978 | 978 | // type is not added by default. |
979 | 979 | $type_css = ''; |
980 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
980 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
981 | 981 | $type_css = 'type="text/css" '; |
982 | 982 | } |
983 | 983 | |
984 | 984 | // Inject the new stylesheets. |
985 | - $replace_tag = array( '<title', 'before' ); |
|
986 | - $replace_tag = apply_filters( 'autoptimize_filter_css_replacetag', $replace_tag, $this->content ); |
|
985 | + $replace_tag = array('<title', 'before'); |
|
986 | + $replace_tag = apply_filters('autoptimize_filter_css_replacetag', $replace_tag, $this->content); |
|
987 | 987 | |
988 | - if ( $this->inline ) { |
|
989 | - foreach ( $this->csscode as $media => $code ) { |
|
990 | - $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', '<style ' . $type_css . 'media="' . $media . '">' . $code . '</style>' ), $replace_tag ); |
|
988 | + if ($this->inline) { |
|
989 | + foreach ($this->csscode as $media => $code) { |
|
990 | + $this->inject_in_html(apply_filters('autoptimize_filter_css_bodyreplacementpayload', '<style '.$type_css.'media="'.$media.'">'.$code.'</style>'), $replace_tag); |
|
991 | 991 | } |
992 | 992 | } else { |
993 | - if ( $this->defer ) { |
|
993 | + if ($this->defer) { |
|
994 | 994 | $preload_css_block = ''; |
995 | 995 | $inlined_ccss_block = ''; |
996 | 996 | $noscript_css_block = '<noscript id="aonoscrcss">'; |
997 | 997 | |
998 | 998 | $defer_inline_code = $this->defer_inline; |
999 | - if ( ! empty( $defer_inline_code ) ) { |
|
1000 | - if ( apply_filters( 'autoptimize_filter_css_critcss_minify', true ) ) { |
|
1001 | - $icss_hash = md5( $defer_inline_code ); |
|
1002 | - $icss_cache = new autoptimizeCache( $icss_hash, 'css' ); |
|
1003 | - if ( $icss_cache->check() ) { |
|
999 | + if (!empty($defer_inline_code)) { |
|
1000 | + if (apply_filters('autoptimize_filter_css_critcss_minify', true)) { |
|
1001 | + $icss_hash = md5($defer_inline_code); |
|
1002 | + $icss_cache = new autoptimizeCache($icss_hash, 'css'); |
|
1003 | + if ($icss_cache->check()) { |
|
1004 | 1004 | // we have the optimized inline CSS in cache. |
1005 | 1005 | $defer_inline_code = $icss_cache->retrieve(); |
1006 | 1006 | } else { |
1007 | 1007 | $cssmin = new autoptimizeCSSmin(); |
1008 | - $tmp_code = trim( $cssmin->run( $defer_inline_code ) ); |
|
1008 | + $tmp_code = trim($cssmin->run($defer_inline_code)); |
|
1009 | 1009 | |
1010 | - if ( ! empty( $tmp_code ) ) { |
|
1010 | + if (!empty($tmp_code)) { |
|
1011 | 1011 | $defer_inline_code = $tmp_code; |
1012 | - $icss_cache->cache( $defer_inline_code, 'text/css' ); |
|
1013 | - unset( $tmp_code ); |
|
1012 | + $icss_cache->cache($defer_inline_code, 'text/css'); |
|
1013 | + unset($tmp_code); |
|
1014 | 1014 | } |
1015 | 1015 | } |
1016 | 1016 | } |
1017 | 1017 | // inlined critical css set here, but injected when full CSS is injected |
1018 | 1018 | // to avoid CSS containing SVG with <title tag receiving the full CSS link. |
1019 | - $inlined_ccss_block = '<style ' . $type_css . 'id="aoatfcss" media="all">' . $defer_inline_code . '</style>'; |
|
1019 | + $inlined_ccss_block = '<style '.$type_css.'id="aoatfcss" media="all">'.$defer_inline_code.'</style>'; |
|
1020 | 1020 | } |
1021 | 1021 | } |
1022 | 1022 | |
1023 | - foreach ( $this->url as $media => $url ) { |
|
1024 | - $url = $this->url_replace_cdn( $url ); |
|
1023 | + foreach ($this->url as $media => $url) { |
|
1024 | + $url = $this->url_replace_cdn($url); |
|
1025 | 1025 | |
1026 | 1026 | // Add the stylesheet either deferred (import at bottom) or normal links in head. |
1027 | - if ( $this->defer && 'print' !== $media ) { |
|
1028 | - $preload_onload = autoptimizeConfig::get_ao_css_preload_onload( $media ); |
|
1027 | + if ($this->defer && 'print' !== $media) { |
|
1028 | + $preload_onload = autoptimizeConfig::get_ao_css_preload_onload($media); |
|
1029 | 1029 | |
1030 | - $preload_css_block .= '<link rel="stylesheet" media="print" href="' . $url . '" onload="' . $preload_onload . '" />'; |
|
1031 | - if ( apply_filters( 'autoptimize_fitler_css_preload_and_print', false ) ) { |
|
1032 | - $preload_css_block = '<link rel="preload" as="stylesheet" href="' . $url . '"/>' . $preload_css_block; |
|
1030 | + $preload_css_block .= '<link rel="stylesheet" media="print" href="'.$url.'" onload="'.$preload_onload.'" />'; |
|
1031 | + if (apply_filters('autoptimize_fitler_css_preload_and_print', false)) { |
|
1032 | + $preload_css_block = '<link rel="preload" as="stylesheet" href="'.$url.'"/>'.$preload_css_block; |
|
1033 | 1033 | } |
1034 | - $noscript_css_block .= '<link ' . $type_css . 'media="' . $media . '" href="' . $url . '" rel="stylesheet" />'; |
|
1034 | + $noscript_css_block .= '<link '.$type_css.'media="'.$media.'" href="'.$url.'" rel="stylesheet" />'; |
|
1035 | 1035 | } else { |
1036 | - if ( strlen( $this->csscode[ $media ] ) > $this->cssinlinesize ) { |
|
1037 | - $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', '<link ' . $type_css . 'media="' . $media . '" href="' . $url . '" rel="stylesheet" />' ), $replace_tag ); |
|
1038 | - } elseif ( strlen( $this->csscode[ $media ] ) > 0 ) { |
|
1039 | - $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', '<style ' . $type_css . 'media="' . $media . '">' . $this->csscode[ $media ] . '</style>' ), $replace_tag ); |
|
1036 | + if (strlen($this->csscode[$media]) > $this->cssinlinesize) { |
|
1037 | + $this->inject_in_html(apply_filters('autoptimize_filter_css_bodyreplacementpayload', '<link '.$type_css.'media="'.$media.'" href="'.$url.'" rel="stylesheet" />'), $replace_tag); |
|
1038 | + } elseif (strlen($this->csscode[$media]) > 0) { |
|
1039 | + $this->inject_in_html(apply_filters('autoptimize_filter_css_bodyreplacementpayload', '<style '.$type_css.'media="'.$media.'">'.$this->csscode[$media].'</style>'), $replace_tag); |
|
1040 | 1040 | } |
1041 | 1041 | } |
1042 | 1042 | } |
1043 | 1043 | |
1044 | - if ( $this->defer ) { |
|
1044 | + if ($this->defer) { |
|
1045 | 1045 | $noscript_css_block .= '</noscript>'; |
1046 | 1046 | // Inject inline critical CSS, the preloaded full CSS and the noscript-CSS. |
1047 | - $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', $inlined_ccss_block . $preload_css_block . $noscript_css_block ), $replace_tag ); |
|
1047 | + $this->inject_in_html(apply_filters('autoptimize_filter_css_bodyreplacementpayload', $inlined_ccss_block.$preload_css_block.$noscript_css_block), $replace_tag); |
|
1048 | 1048 | } |
1049 | 1049 | } |
1050 | 1050 | |
1051 | 1051 | // restore comments. |
1052 | - $this->content = $this->restore_comments( $this->content ); |
|
1052 | + $this->content = $this->restore_comments($this->content); |
|
1053 | 1053 | |
1054 | 1054 | // restore IE hacks. |
1055 | - $this->content = $this->restore_iehacks( $this->content ); |
|
1055 | + $this->content = $this->restore_iehacks($this->content); |
|
1056 | 1056 | |
1057 | 1057 | // restore (no)script. |
1058 | - $this->content = $this->restore_marked_content( 'SCRIPT', $this->content ); |
|
1058 | + $this->content = $this->restore_marked_content('SCRIPT', $this->content); |
|
1059 | 1059 | |
1060 | 1060 | // Restore noptimize. |
1061 | - $this->content = $this->restore_noptimize( $this->content ); |
|
1061 | + $this->content = $this->restore_noptimize($this->content); |
|
1062 | 1062 | |
1063 | 1063 | // Return the modified stylesheet. |
1064 | 1064 | return $this->content; |
@@ -1070,44 +1070,44 @@ discard block |
||
1070 | 1070 | * @param string $file filename of optimized CSS-file. |
1071 | 1071 | * @param string $code CSS-code in which to fix URL's. |
1072 | 1072 | */ |
1073 | - static function fixurls( $file, $code ) |
|
1073 | + static function fixurls($file, $code) |
|
1074 | 1074 | { |
1075 | 1075 | // Switch all imports to the url() syntax. |
1076 | - $code = preg_replace( '#@import ("|\')(.+?)\.css.*?("|\')#', '@import url("${2}.css")', $code ); |
|
1076 | + $code = preg_replace('#@import ("|\')(.+?)\.css.*?("|\')#', '@import url("${2}.css")', $code); |
|
1077 | 1077 | |
1078 | - if ( preg_match_all( self::ASSETS_REGEX, $code, $matches ) ) { |
|
1079 | - $file = str_replace( WP_ROOT_DIR, '/', $file ); |
|
1078 | + if (preg_match_all(self::ASSETS_REGEX, $code, $matches)) { |
|
1079 | + $file = str_replace(WP_ROOT_DIR, '/', $file); |
|
1080 | 1080 | /** |
1081 | 1081 | * Rollback as per https://github.com/futtta/autoptimize/issues/94 |
1082 | 1082 | * $file = str_replace( AUTOPTIMIZE_WP_CONTENT_NAME, '', $file ); |
1083 | 1083 | */ |
1084 | - $dir = dirname( $file ); // Like /themes/expound/css. |
|
1084 | + $dir = dirname($file); // Like /themes/expound/css. |
|
1085 | 1085 | |
1086 | 1086 | /** |
1087 | 1087 | * $dir should not contain backslashes, since it's used to replace |
1088 | 1088 | * urls, but it can contain them when running on Windows because |
1089 | 1089 | * fixurls() is sometimes called with `ABSPATH . 'index.php'` |
1090 | 1090 | */ |
1091 | - $dir = str_replace( '\\', '/', $dir ); |
|
1092 | - unset( $file ); // not used below at all. |
|
1091 | + $dir = str_replace('\\', '/', $dir); |
|
1092 | + unset($file); // not used below at all. |
|
1093 | 1093 | |
1094 | 1094 | $replace = array(); |
1095 | - foreach ( $matches[1] as $k => $url ) { |
|
1095 | + foreach ($matches[1] as $k => $url) { |
|
1096 | 1096 | // Remove quotes. |
1097 | - $url = trim( $url, " \t\n\r\0\x0B\"'" ); |
|
1098 | - $no_q_url = trim( $url, "\"'" ); |
|
1099 | - if ( $url !== $no_q_url ) { |
|
1097 | + $url = trim($url, " \t\n\r\0\x0B\"'"); |
|
1098 | + $no_q_url = trim($url, "\"'"); |
|
1099 | + if ($url !== $no_q_url) { |
|
1100 | 1100 | $removed_quotes = true; |
1101 | 1101 | } else { |
1102 | 1102 | $removed_quotes = false; |
1103 | 1103 | } |
1104 | 1104 | |
1105 | - if ( '' === $no_q_url ) { |
|
1105 | + if ('' === $no_q_url) { |
|
1106 | 1106 | continue; |
1107 | 1107 | } |
1108 | 1108 | |
1109 | 1109 | $url = $no_q_url; |
1110 | - if ( '/' === $url[0] || preg_match( '#^(https?://|ftp://|data:)#i', $url ) ) { |
|
1110 | + if ('/' === $url[0] || preg_match('#^(https?://|ftp://|data:)#i', $url)) { |
|
1111 | 1111 | // URL is protocol-relative, host-relative or something we don't touch. |
1112 | 1112 | continue; |
1113 | 1113 | } else { // Relative URL. |
@@ -1116,49 +1116,49 @@ discard block |
||
1116 | 1116 | * rollback as per https://github.com/futtta/autoptimize/issues/94 |
1117 | 1117 | * $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_CONTENT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) ); |
1118 | 1118 | */ |
1119 | - $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_ROOT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) ); |
|
1120 | - $newurl = apply_filters( 'autoptimize_filter_css_fixurl_newurl', $newurl ); |
|
1119 | + $newurl = preg_replace('/https?:/', '', str_replace(' ', '%20', AUTOPTIMIZE_WP_ROOT_URL.str_replace('//', '/', $dir.'/'.$url))); |
|
1120 | + $newurl = apply_filters('autoptimize_filter_css_fixurl_newurl', $newurl); |
|
1121 | 1121 | |
1122 | 1122 | /** |
1123 | 1123 | * Hash the url + whatever was behind potentially for replacement |
1124 | 1124 | * We must do this, or different css classes referencing the same bg image (but |
1125 | 1125 | * different parts of it, say, in sprites and such) loose their stuff... |
1126 | 1126 | */ |
1127 | - $hash = md5( $url . $matches[2][ $k ] ); |
|
1128 | - $code = str_replace( $matches[0][ $k ], $hash, $code ); |
|
1127 | + $hash = md5($url.$matches[2][$k]); |
|
1128 | + $code = str_replace($matches[0][$k], $hash, $code); |
|
1129 | 1129 | |
1130 | - if ( $removed_quotes ) { |
|
1131 | - $replace[ $hash ] = "url('" . $newurl . "')" . $matches[2][ $k ]; |
|
1130 | + if ($removed_quotes) { |
|
1131 | + $replace[$hash] = "url('".$newurl."')".$matches[2][$k]; |
|
1132 | 1132 | } else { |
1133 | - $replace[ $hash ] = 'url(' . $newurl . ')' . $matches[2][ $k ]; |
|
1133 | + $replace[$hash] = 'url('.$newurl.')'.$matches[2][$k]; |
|
1134 | 1134 | } |
1135 | 1135 | } |
1136 | 1136 | } |
1137 | 1137 | |
1138 | - $code = self::replace_longest_matches_first( $code, $replace ); |
|
1138 | + $code = self::replace_longest_matches_first($code, $replace); |
|
1139 | 1139 | } |
1140 | 1140 | |
1141 | 1141 | return $code; |
1142 | 1142 | } |
1143 | 1143 | |
1144 | - private function ismovable( $tag ) |
|
1144 | + private function ismovable($tag) |
|
1145 | 1145 | { |
1146 | - if ( ! $this->aggregate ) { |
|
1146 | + if (!$this->aggregate) { |
|
1147 | 1147 | return false; |
1148 | 1148 | } |
1149 | 1149 | |
1150 | - if ( ! empty( $this->allowlist ) ) { |
|
1151 | - foreach ( $this->allowlist as $match ) { |
|
1152 | - if ( false !== strpos( $tag, $match ) ) { |
|
1150 | + if (!empty($this->allowlist)) { |
|
1151 | + foreach ($this->allowlist as $match) { |
|
1152 | + if (false !== strpos($tag, $match)) { |
|
1153 | 1153 | return true; |
1154 | 1154 | } |
1155 | 1155 | } |
1156 | 1156 | // no match with allowlist. |
1157 | 1157 | return false; |
1158 | 1158 | } else { |
1159 | - if ( is_array( $this->dontmove ) && ! empty( $this->dontmove ) ) { |
|
1160 | - foreach ( $this->dontmove as $match ) { |
|
1161 | - if ( false !== strpos( $tag, $match ) ) { |
|
1159 | + if (is_array($this->dontmove) && !empty($this->dontmove)) { |
|
1160 | + foreach ($this->dontmove as $match) { |
|
1161 | + if (false !== strpos($tag, $match)) { |
|
1162 | 1162 | // Matched something. |
1163 | 1163 | return false; |
1164 | 1164 | } |
@@ -1170,22 +1170,22 @@ discard block |
||
1170 | 1170 | } |
1171 | 1171 | } |
1172 | 1172 | |
1173 | - private function can_inject_late( $css_path, $css ) |
|
1173 | + private function can_inject_late($css_path, $css) |
|
1174 | 1174 | { |
1175 | - $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false, $css_path ); |
|
1176 | - if ( true !== $this->inject_min_late ) { |
|
1175 | + $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false, $css_path); |
|
1176 | + if (true !== $this->inject_min_late) { |
|
1177 | 1177 | // late-inject turned off. |
1178 | 1178 | return false; |
1179 | - } elseif ( ( false === strpos( $css_path, 'min.css' ) ) && ( str_replace( $consider_minified_array, '', $css_path ) === $css_path ) ) { |
|
1179 | + } elseif ((false === strpos($css_path, 'min.css')) && (str_replace($consider_minified_array, '', $css_path) === $css_path)) { |
|
1180 | 1180 | // file not minified based on filename & filter. |
1181 | 1181 | return false; |
1182 | - } elseif ( false !== strpos( $css, '@import' ) ) { |
|
1182 | + } elseif (false !== strpos($css, '@import')) { |
|
1183 | 1183 | // can't late-inject files with imports as those need to be aggregated. |
1184 | 1184 | return false; |
1185 | - } elseif ( ( false !== strpos( $css, '@font-face' ) ) && ( apply_filters( 'autoptimize_filter_css_fonts_cdn', false ) === true ) && ( ! empty( $this->cdn_url ) ) ) { |
|
1185 | + } elseif ((false !== strpos($css, '@font-face')) && (apply_filters('autoptimize_filter_css_fonts_cdn', false) === true) && (!empty($this->cdn_url))) { |
|
1186 | 1186 | // don't late-inject CSS with font-src's if fonts are set to be CDN'ed. |
1187 | 1187 | return false; |
1188 | - } elseif ( ( ( true == $this->datauris ) || ( ! empty( $this->cdn_url ) ) ) && preg_match( '#background[^;}]*url\(#Ui', $css ) ) { |
|
1188 | + } elseif (((true == $this->datauris) || (!empty($this->cdn_url))) && preg_match('#background[^;}]*url\(#Ui', $css)) { |
|
1189 | 1189 | // don't late-inject CSS with images if CDN is set OR if image inlining is on. |
1190 | 1190 | return false; |
1191 | 1191 | } else { |
@@ -1203,44 +1203,44 @@ discard block |
||
1203 | 1203 | * |
1204 | 1204 | * @return bool|string Url pointing to the minified css file or false. |
1205 | 1205 | */ |
1206 | - public function minify_single( $filepath, $cache_miss = false ) |
|
1206 | + public function minify_single($filepath, $cache_miss = false) |
|
1207 | 1207 | { |
1208 | - $contents = $this->prepare_minify_single( $filepath ); |
|
1208 | + $contents = $this->prepare_minify_single($filepath); |
|
1209 | 1209 | |
1210 | - if ( empty( $contents ) ) { |
|
1210 | + if (empty($contents)) { |
|
1211 | 1211 | return false; |
1212 | 1212 | } |
1213 | 1213 | |
1214 | 1214 | // Check cache. |
1215 | - $hash = 'single_' . md5( $contents ); |
|
1216 | - $cache = new autoptimizeCache( $hash, 'css' ); |
|
1217 | - do_action( 'autoptimize_action_css_hash', $hash ); |
|
1215 | + $hash = 'single_'.md5($contents); |
|
1216 | + $cache = new autoptimizeCache($hash, 'css'); |
|
1217 | + do_action('autoptimize_action_css_hash', $hash); |
|
1218 | 1218 | |
1219 | 1219 | // If not in cache already, minify... |
1220 | - if ( ! $cache->check() || $cache_miss ) { |
|
1220 | + if (!$cache->check() || $cache_miss) { |
|
1221 | 1221 | // Fixurls... |
1222 | - $contents = self::fixurls( $filepath, $contents ); |
|
1222 | + $contents = self::fixurls($filepath, $contents); |
|
1223 | 1223 | // CDN-replace any referenced assets if needed... |
1224 | - $contents = $this->hide_fontface_and_maybe_cdn( $contents ); |
|
1225 | - $contents = $this->replace_urls( $contents ); |
|
1226 | - $contents = $this->restore_fontface( $contents ); |
|
1224 | + $contents = $this->hide_fontface_and_maybe_cdn($contents); |
|
1225 | + $contents = $this->replace_urls($contents); |
|
1226 | + $contents = $this->restore_fontface($contents); |
|
1227 | 1227 | // Now minify... |
1228 | 1228 | $cssmin = new autoptimizeCSSmin(); |
1229 | - $contents = trim( $cssmin->run( $contents ) ); |
|
1229 | + $contents = trim($cssmin->run($contents)); |
|
1230 | 1230 | |
1231 | 1231 | // Check if minified cache content is empty. |
1232 | - if ( empty( $contents ) ) { |
|
1232 | + if (empty($contents)) { |
|
1233 | 1233 | return false; |
1234 | 1234 | } |
1235 | 1235 | |
1236 | 1236 | // Filter contents of excluded minified CSS. |
1237 | - $contents = apply_filters( 'autoptimize_filter_css_single_after_minify', $contents ); |
|
1237 | + $contents = apply_filters('autoptimize_filter_css_single_after_minify', $contents); |
|
1238 | 1238 | |
1239 | 1239 | // Store in cache. |
1240 | - $cache->cache( $contents, 'text/css' ); |
|
1240 | + $cache->cache($contents, 'text/css'); |
|
1241 | 1241 | } |
1242 | 1242 | |
1243 | - $url = $this->build_minify_single_url( $cache ); |
|
1243 | + $url = $this->build_minify_single_url($cache); |
|
1244 | 1244 | |
1245 | 1245 | return $url; |
1246 | 1246 | } |
@@ -1260,19 +1260,19 @@ discard block |
||
1260 | 1260 | return $this->options; |
1261 | 1261 | } |
1262 | 1262 | |
1263 | - public function replaceOptions( $options ) |
|
1263 | + public function replaceOptions($options) |
|
1264 | 1264 | { |
1265 | 1265 | $this->options = $options; |
1266 | 1266 | } |
1267 | 1267 | |
1268 | - public function setOption( $name, $value ) |
|
1268 | + public function setOption($name, $value) |
|
1269 | 1269 | { |
1270 | - $this->options[ $name ] = $value; |
|
1270 | + $this->options[$name] = $value; |
|
1271 | 1271 | $this->$name = $value; |
1272 | 1272 | } |
1273 | 1273 | |
1274 | - public function getOption( $name ) |
|
1274 | + public function getOption($name) |
|
1275 | 1275 | { |
1276 | - return $this->options[ $name ]; |
|
1276 | + return $this->options[$name]; |
|
1277 | 1277 | } |
1278 | 1278 | } |
@@ -9,15 +9,15 @@ discard block |
||
9 | 9 | console.log('Exporting...'); |
10 | 10 | var data = { |
11 | 11 | 'action': 'ao_ccss_export', |
12 | - 'ao_ccss_export_nonce': '<?php echo wp_create_nonce( 'ao_ccss_export_nonce' ); ?>', |
|
12 | + 'ao_ccss_export_nonce': '<?php echo wp_create_nonce('ao_ccss_export_nonce'); ?>', |
|
13 | 13 | }; |
14 | 14 | |
15 | 15 | jQuery.post(ajaxurl, data, function(response) { |
16 | 16 | response_array=JSON.parse(response); |
17 | 17 | if (response_array['code'] == 200) { |
18 | 18 | <?php |
19 | - if ( is_multisite() ) { |
|
20 | - $blog_id = '/' . get_current_blog_id() . '/'; |
|
19 | + if (is_multisite()) { |
|
20 | + $blog_id = '/'.get_current_blog_id().'/'; |
|
21 | 21 | } else { |
22 | 22 | $blog_id = '/'; |
23 | 23 | } |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | autoOpen: true, |
33 | 33 | height: 210, |
34 | 34 | width: 700, |
35 | - title: "<?php _e( 'Export settings result', 'autoptimize' ); ?>", |
|
35 | + title: "<?php _e('Export settings result', 'autoptimize'); ?>", |
|
36 | 36 | modal: true, |
37 | 37 | buttons: { |
38 | 38 | OK: function() { |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | var settings_file = file[0].files[0]; |
51 | 51 | fd.append('file', settings_file); |
52 | 52 | fd.append('action', 'ao_ccss_import'); |
53 | - fd.append('ao_ccss_import_nonce', '<?php echo wp_create_nonce( 'ao_ccss_import_nonce' ); ?>'); |
|
53 | + fd.append('ao_ccss_import_nonce', '<?php echo wp_create_nonce('ao_ccss_import_nonce'); ?>'); |
|
54 | 54 | |
55 | 55 | jQuery.ajax({ |
56 | 56 | url: ajaxurl, |
@@ -13,12 +13,12 @@ discard block |
||
13 | 13 | ?> |
14 | 14 | <ul id="rules-panel"> |
15 | 15 | <li class="itemDetail"> |
16 | - <h2 class="itemTitle"><?php _e( 'Rules', 'autoptimize' ); ?></h2> |
|
16 | + <h2 class="itemTitle"><?php _e('Rules', 'autoptimize'); ?></h2> |
|
17 | 17 | |
18 | 18 | <!-- BEGIN Rule dialogs --> |
19 | 19 | <!-- Unsaved dialog --> |
20 | 20 | <div id="unSavedWarning" class="hidden updated settings-error notice notice-warning is-dismissible"> |
21 | - <p><?php _e( "<strong>Rules or Queue changed!</strong> Don't forget to save your changes!", 'autoptimize' ); ?></p> |
|
21 | + <p><?php _e("<strong>Rules or Queue changed!</strong> Don't forget to save your changes!", 'autoptimize'); ?></p> |
|
22 | 22 | </div> |
23 | 23 | |
24 | 24 | <!-- Create/edit rule dialog --> |
@@ -26,97 +26,97 @@ discard block |
||
26 | 26 | <table class="form-table rules"> |
27 | 27 | <tr id="critcss_addedit_type_wrapper"> |
28 | 28 | <th scope="row"> |
29 | - <?php _e( 'Rule Type', 'autoptimize' ); ?> |
|
29 | + <?php _e('Rule Type', 'autoptimize'); ?> |
|
30 | 30 | </th> |
31 | 31 | <td> |
32 | 32 | <select id="critcss_addedit_type" style="width:100%;"> |
33 | - <option value="paths"><?php _e( 'Path', 'autoptimize' ); ?></option> |
|
34 | - <option value="types"><?php _e( 'Conditional Tag', 'autoptimize' ); ?></option> |
|
33 | + <option value="paths"><?php _e('Path', 'autoptimize'); ?></option> |
|
34 | + <option value="types"><?php _e('Conditional Tag', 'autoptimize'); ?></option> |
|
35 | 35 | </select> |
36 | 36 | </td> |
37 | 37 | </tr> |
38 | 38 | <tr id="critcss_addedit_path_wrapper"> |
39 | 39 | <th scope="row"> |
40 | - <?php _e( 'String in Path', 'autoptimize' ); ?> |
|
40 | + <?php _e('String in Path', 'autoptimize'); ?> |
|
41 | 41 | </th> |
42 | 42 | <td> |
43 | - <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=""> |
|
43 | + <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=""> |
|
44 | 44 | </td> |
45 | 45 | </tr> |
46 | 46 | <tr id="critcss_addedit_pagetype_wrapper"> |
47 | 47 | <th scope="row"> |
48 | - <?php _e( 'Conditional Tag, Custom Post Type or Page Template', 'autoptimize' ); ?> |
|
48 | + <?php _e('Conditional Tag, Custom Post Type or Page Template', 'autoptimize'); ?> |
|
49 | 49 | </th> |
50 | 50 | <td> |
51 | 51 | <select id="critcss_addedit_pagetype" style="width:100%;"> |
52 | - <option value="" disabled selected><?php _e( 'Select from the list below...', 'autoptimize' ); ?></option> |
|
53 | - <optgroup label="<?php _e( 'Standard Conditional Tags', 'autoptimize' ); ?>"> |
|
52 | + <option value="" disabled selected><?php _e('Select from the list below...', 'autoptimize'); ?></option> |
|
53 | + <optgroup label="<?php _e('Standard Conditional Tags', 'autoptimize'); ?>"> |
|
54 | 54 | <?php |
55 | 55 | // Render grouped simple conditional tags. |
56 | - foreach ( $ao_ccss_types as $ctag ) { |
|
57 | - $optgrp = substr( $ctag, 0, 3 ); |
|
58 | - if ( substr( $ctag, 0, 3 ) === 'is_' ) { |
|
59 | - echo '<option value="' . $ctag . '">' . $ctag . '</option>'; |
|
56 | + foreach ($ao_ccss_types as $ctag) { |
|
57 | + $optgrp = substr($ctag, 0, 3); |
|
58 | + if (substr($ctag, 0, 3) === 'is_') { |
|
59 | + echo '<option value="'.$ctag.'">'.$ctag.'</option>'; |
|
60 | 60 | } |
61 | - $prevgrp = substr( $ctag, 0, 3 ); |
|
61 | + $prevgrp = substr($ctag, 0, 3); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | // Render grouped custom post types, templates and specific conditional tags. |
65 | - foreach ( $ao_ccss_types as $type ) { |
|
66 | - $optgrp = substr( $type, 0, 3 ); |
|
65 | + foreach ($ao_ccss_types as $type) { |
|
66 | + $optgrp = substr($type, 0, 3); |
|
67 | 67 | |
68 | 68 | // Option groups labels. |
69 | - if ( $optgrp !== $prevgrp && 'is_' !== $optgrp ) { |
|
69 | + if ($optgrp !== $prevgrp && 'is_' !== $optgrp) { |
|
70 | 70 | ?> |
71 | 71 | </optgroup> |
72 | 72 | <?php |
73 | - if ( substr( $type, 0, 12 ) === 'custom_post_' ) { |
|
73 | + if (substr($type, 0, 12) === 'custom_post_') { |
|
74 | 74 | ?> |
75 | - <optgroup label="<?php _e( 'Custom Post Types', 'autoptimize' ); ?>"> |
|
75 | + <optgroup label="<?php _e('Custom Post Types', 'autoptimize'); ?>"> |
|
76 | 76 | <?php |
77 | - } elseif ( substr( $type, 0, 9 ) === 'template_' ) { |
|
77 | + } elseif (substr($type, 0, 9) === 'template_') { |
|
78 | 78 | ?> |
79 | - <optgroup label="<?php _e( 'Page Templates', 'autoptimize' ); ?>"> |
|
79 | + <optgroup label="<?php _e('Page Templates', 'autoptimize'); ?>"> |
|
80 | 80 | <?php |
81 | - } elseif ( substr( $type, 0, 4 ) === 'bbp_' ) { |
|
81 | + } elseif (substr($type, 0, 4) === 'bbp_') { |
|
82 | 82 | ?> |
83 | - <optgroup label="<?php _e( 'BBPress Conditional Tags', 'autoptimize' ); ?>"> |
|
83 | + <optgroup label="<?php _e('BBPress Conditional Tags', 'autoptimize'); ?>"> |
|
84 | 84 | <?php |
85 | - } elseif ( substr( $type, 0, 3 ) === 'bp_' ) { |
|
85 | + } elseif (substr($type, 0, 3) === 'bp_') { |
|
86 | 86 | ?> |
87 | - <optgroup label="<?php _e( 'BuddyPress Conditional Tags', 'autoptimize' ); ?>"> |
|
87 | + <optgroup label="<?php _e('BuddyPress Conditional Tags', 'autoptimize'); ?>"> |
|
88 | 88 | <?php |
89 | - } elseif ( substr( $type, 0, 4 ) === 'edd_' ) { |
|
89 | + } elseif (substr($type, 0, 4) === 'edd_') { |
|
90 | 90 | ?> |
91 | - <optgroup label="<?php _e( 'Easy Digital Downloads Conditional Tags', 'autoptimize' ); ?>"> |
|
91 | + <optgroup label="<?php _e('Easy Digital Downloads Conditional Tags', 'autoptimize'); ?>"> |
|
92 | 92 | <?php |
93 | - } elseif ( substr( $type, 0, 4 ) === 'woo_' ) { |
|
93 | + } elseif (substr($type, 0, 4) === 'woo_') { |
|
94 | 94 | ?> |
95 | - <optgroup label="<?php _e( 'WooCommerce Conditional Tags', 'autoptimize' ); ?>"> |
|
95 | + <optgroup label="<?php _e('WooCommerce Conditional Tags', 'autoptimize'); ?>"> |
|
96 | 96 | <?php |
97 | 97 | } |
98 | 98 | } |
99 | 99 | |
100 | 100 | // Options. |
101 | - if ( 'is_' !== $optgrp ) { |
|
101 | + if ('is_' !== $optgrp) { |
|
102 | 102 | // Remove prefix from custom post types, templates and some specific conditional tags. |
103 | - if ( substr( $type, 0, 12 ) === 'custom_post_' ) { |
|
104 | - $_type = str_replace( 'custom_post_', '', $type ); |
|
105 | - } elseif ( substr( $type, 0, 9 ) === 'template_' ) { |
|
106 | - $_type = str_replace( 'template_', '', $type ); |
|
107 | - } elseif ( 'bbp_is_bbpress' == $type ) { |
|
108 | - $_type = str_replace( 'bbp_', '', $type ); |
|
109 | - } elseif ( 'bp_is_buddypress' == $type ) { |
|
110 | - $_type = str_replace( 'bp_', '', $type ); |
|
111 | - } elseif ( substr( $type, 0, 4 ) === 'woo_' ) { |
|
112 | - $_type = str_replace( 'woo_', '', $type ); |
|
113 | - } elseif ( substr( $type, 0, 4 ) === 'edd_' ) { |
|
114 | - $_type = str_replace( 'edd_', '', $type ); |
|
103 | + if (substr($type, 0, 12) === 'custom_post_') { |
|
104 | + $_type = str_replace('custom_post_', '', $type); |
|
105 | + } elseif (substr($type, 0, 9) === 'template_') { |
|
106 | + $_type = str_replace('template_', '', $type); |
|
107 | + } elseif ('bbp_is_bbpress' == $type) { |
|
108 | + $_type = str_replace('bbp_', '', $type); |
|
109 | + } elseif ('bp_is_buddypress' == $type) { |
|
110 | + $_type = str_replace('bp_', '', $type); |
|
111 | + } elseif (substr($type, 0, 4) === 'woo_') { |
|
112 | + $_type = str_replace('woo_', '', $type); |
|
113 | + } elseif (substr($type, 0, 4) === 'edd_') { |
|
114 | + $_type = str_replace('edd_', '', $type); |
|
115 | 115 | } else { |
116 | 116 | $_type = $type; |
117 | 117 | } |
118 | 118 | |
119 | - echo '<option value="' . $type . '">' . $_type . '</option>'; |
|
119 | + echo '<option value="'.$type.'">'.$_type.'</option>'; |
|
120 | 120 | $prevgrp = $optgrp; |
121 | 121 | } |
122 | 122 | } |
@@ -127,10 +127,10 @@ discard block |
||
127 | 127 | </tr> |
128 | 128 | <tr> |
129 | 129 | <th scope="row"> |
130 | - <?php _e( 'Custom Critical CSS', 'autoptimize' ); ?> |
|
130 | + <?php _e('Custom Critical CSS', 'autoptimize'); ?> |
|
131 | 131 | </th> |
132 | 132 | <td> |
133 | - <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> |
|
133 | + <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> |
|
134 | 134 | <input type="hidden" id="critcss_addedit_file"> |
135 | 135 | <input type="hidden" id="critcss_addedit_id"> |
136 | 136 | </td> |
@@ -139,23 +139,23 @@ discard block |
||
139 | 139 | </div> |
140 | 140 | |
141 | 141 | <!-- Remove dialog --> |
142 | - <div id="confirm-rm" title="<?php _e( 'Delete Rule', 'autoptimize' ); ?>" class="hidden"> |
|
143 | - <p><?php _e( 'This Critical CSS rule will be deleted immediately and cannot be recovered.<br /><br /><strong>Are you sure?</strong>', 'autoptimize' ); ?></p> |
|
142 | + <div id="confirm-rm" title="<?php _e('Delete Rule', 'autoptimize'); ?>" class="hidden"> |
|
143 | + <p><?php _e('This Critical CSS rule will be deleted immediately and cannot be recovered.<br /><br /><strong>Are you sure?</strong>', 'autoptimize'); ?></p> |
|
144 | 144 | </div> |
145 | 145 | |
146 | 146 | <!-- Remove All dialog --> |
147 | - <div id="confirm-rm-all" title="<?php _e( 'Delete all Rules and Jobs', 'autoptimize' ); ?>" class="hidden"> |
|
148 | - <p><?php _e( 'All Critical CSS rules will be deleted immediately and cannot be recovered.<br /><br /><strong>Are you sure?</strong>', 'autoptimize' ); ?></p> |
|
147 | + <div id="confirm-rm-all" title="<?php _e('Delete all Rules and Jobs', 'autoptimize'); ?>" class="hidden"> |
|
148 | + <p><?php _e('All Critical CSS rules will be deleted immediately and cannot be recovered.<br /><br /><strong>Are you sure?</strong>', 'autoptimize'); ?></p> |
|
149 | 149 | </div> |
150 | 150 | |
151 | 151 | <!-- Add/edit default critical CSS dialog --> |
152 | 152 | <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> |
|
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> |
|
154 | 154 | </div> |
155 | 155 | |
156 | 156 | <!-- Add/edit additional critical CSS dialog --> |
157 | 157 | <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> |
|
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> |
|
159 | 159 | </div> |
160 | 160 | |
161 | 161 | <!-- Wrapper for in screen notices --> |
@@ -165,38 +165,38 @@ discard block |
||
165 | 165 | <!-- BEGIN Rules UI --> |
166 | 166 | <div class="howto"> |
167 | 167 | <div class="title-wrap"> |
168 | - <h4 class="title"><?php _e( 'How To Use Autoptimize CriticalCSS Power-Up Rules', 'autoptimize' ); ?></h4> |
|
169 | - <p class="subtitle"><?php _e( 'Click the side arrow to toggle instructions', 'autoptimize' ); ?></p> |
|
168 | + <h4 class="title"><?php _e('How To Use Autoptimize CriticalCSS Power-Up Rules', 'autoptimize'); ?></h4> |
|
169 | + <p class="subtitle"><?php _e('Click the side arrow to toggle instructions', 'autoptimize'); ?></p> |
|
170 | 170 | </div> |
171 | 171 | <button type="button" class="toggle-btn"> |
172 | 172 | <span class="toggle-indicator dashicons dashicons-arrow-up dashicons-arrow-down"></span> |
173 | 173 | </button> |
174 | 174 | <div class="howto-wrap hidden"> |
175 | - <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> |
|
175 | + <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 | 176 | <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( '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 | - <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 | - <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 | - <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 | - <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> |
|
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('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 | + <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 | + <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 | + <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 | + <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 | 185 | </ol> |
186 | 186 | </div> |
187 | 187 | </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> |
|
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> |
|
190 | 190 | <table class="rules-list" cellspacing="0"><tbody id="rules-list"></tbody></table> |
191 | - <input class="hidden" type="text" id="critCssOrigin" name="autoptimize_ccss_rules" value='<?php echo ( json_encode( $ao_ccss_rules, JSON_FORCE_OBJECT ) ); ?>'> |
|
191 | + <input class="hidden" type="text" id="critCssOrigin" name="autoptimize_ccss_rules" value='<?php echo (json_encode($ao_ccss_rules, JSON_FORCE_OBJECT)); ?>'> |
|
192 | 192 | <div class="submit rules-btn"> |
193 | 193 | <div class="alignleft"> |
194 | - <span id="addCritCssButton" class="button-secondary"><?php _e( 'Add New Rule', 'autoptimize' ); ?></span> |
|
195 | - <span id="editDefaultButton" class="button-secondary"><?php _e( 'Edit Default Rule CSS', 'autoptimize' ); ?></span> |
|
196 | - <span id="editAdditionalButton" class="button-secondary"><?php _e( 'Add CSS To All Rules', 'autoptimize' ); ?></span> |
|
194 | + <span id="addCritCssButton" class="button-secondary"><?php _e('Add New Rule', 'autoptimize'); ?></span> |
|
195 | + <span id="editDefaultButton" class="button-secondary"><?php _e('Edit Default Rule CSS', 'autoptimize'); ?></span> |
|
196 | + <span id="editAdditionalButton" class="button-secondary"><?php _e('Add CSS To All Rules', 'autoptimize'); ?></span> |
|
197 | 197 | </div> |
198 | 198 | <div class="alignright"> |
199 | - <span id="removeAllRules" class="button-secondary" style="color:red;"><?php _e( 'Remove all rules', 'autoptimize' ); ?></span> |
|
199 | + <span id="removeAllRules" class="button-secondary" style="color:red;"><?php _e('Remove all rules', 'autoptimize'); ?></span> |
|
200 | 200 | </div> |
201 | 201 | </div> |
202 | 202 | <!-- END Rules UI --> |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | $ao_options = $wpdb->get_results(' |
11 | 11 | SELECT option_name AS name, |
12 | 12 | option_value AS value |
13 | - FROM ' . $wpdb->options . ' |
|
13 | + FROM ' . $wpdb->options.' |
|
14 | 14 | WHERE option_name LIKE "autoptimize_%%" |
15 | 15 | ORDER BY name |
16 | 16 | ', ARRAY_A); |
@@ -19,27 +19,27 @@ discard block |
||
19 | 19 | $ao_trans = $wpdb->get_results(' |
20 | 20 | SELECT option_name AS name, |
21 | 21 | option_value AS value |
22 | - FROM ' . $wpdb->options . ' |
|
22 | + FROM ' . $wpdb->options.' |
|
23 | 23 | WHERE option_name LIKE "_transient_autoptimize_%%" |
24 | 24 | OR option_name LIKE "_transient_timeout_autoptimize_%%" |
25 | 25 | ', ARRAY_A); |
26 | 26 | |
27 | 27 | // Render debug panel if there's something to show. |
28 | -if ( $ao_options || $ao_trans ) { |
|
28 | +if ($ao_options || $ao_trans) { |
|
29 | 29 | ?> |
30 | 30 | <!-- BEGIN: Settings Debug --> |
31 | 31 | <ul> |
32 | 32 | <li class="itemDetail"> |
33 | - <h2 class="itemTitle"><?php _e( 'Debug Information', 'autoptimize' ); ?></h2> |
|
33 | + <h2 class="itemTitle"><?php _e('Debug Information', 'autoptimize'); ?></h2> |
|
34 | 34 | |
35 | 35 | <?php |
36 | 36 | // Render options. |
37 | - if ( $ao_options ) { |
|
37 | + if ($ao_options) { |
|
38 | 38 | ?> |
39 | - <h4><?php _e( 'Options', 'autoptimize' ); ?>:</h4> |
|
39 | + <h4><?php _e('Options', 'autoptimize'); ?>:</h4> |
|
40 | 40 | <table class="form-table debug"> |
41 | 41 | <?php |
42 | - foreach ( $ao_options as $option ) { |
|
42 | + foreach ($ao_options as $option) { |
|
43 | 43 | ?> |
44 | 44 | <tr> |
45 | 45 | <th scope="row"> |
@@ -47,10 +47,10 @@ discard block |
||
47 | 47 | </th> |
48 | 48 | <td> |
49 | 49 | <?php |
50 | - if ( 'autoptimize_ccss_queue' == $option['name'] || 'autoptimize_ccss_rules' == $option['name'] ) { |
|
51 | - $value = print_r( json_decode( $option['value'], true ), true ); |
|
52 | - if ( $value ) { |
|
53 | - echo "Raw JSON:\n<pre>" . $option['value'] . "</pre>\n\nDecoded JSON:\n<pre>" . $value . '</pre>'; |
|
50 | + if ('autoptimize_ccss_queue' == $option['name'] || 'autoptimize_ccss_rules' == $option['name']) { |
|
51 | + $value = print_r(json_decode($option['value'], true), true); |
|
52 | + if ($value) { |
|
53 | + echo "Raw JSON:\n<pre>".$option['value']."</pre>\n\nDecoded JSON:\n<pre>".$value.'</pre>'; |
|
54 | 54 | } else { |
55 | 55 | echo 'Empty'; |
56 | 56 | } |
@@ -69,11 +69,11 @@ discard block |
||
69 | 69 | } |
70 | 70 | // Render WP-Cron intervals and scheduled events. |
71 | 71 | ?> |
72 | - <h4><?php _e( 'WP-Cron Intervals', 'autoptimize' ); ?>:</h4> |
|
73 | - <pre><?php print_r( wp_get_schedules() ); ?></pre> |
|
72 | + <h4><?php _e('WP-Cron Intervals', 'autoptimize'); ?>:</h4> |
|
73 | + <pre><?php print_r(wp_get_schedules()); ?></pre> |
|
74 | 74 | <hr /> |
75 | - <h4><?php _e( 'WP-Cron Scheduled Events', 'autoptimize' ); ?>:</h4> |
|
76 | - <pre><?php print_r( _get_cron_array() ); ?></pre> |
|
75 | + <h4><?php _e('WP-Cron Scheduled Events', 'autoptimize'); ?>:</h4> |
|
76 | + <pre><?php print_r(_get_cron_array()); ?></pre> |
|
77 | 77 | </li> |
78 | 78 | </ul> |
79 | 79 | <!-- END: Settings Debug --> |