registered[$handle]) ? $styles->registered[$handle] : null; if (!$style || !is_string($style->src) || self::layer_twin($style->src) === $style->src || (string) $css === '') { return $css; } if (!$above && !$by_markup) { return self::in_layer((string) $css); } list($in, $out) = $by_markup ? self::split_markup((string) $css) : self::split_properties((string) $css, $above); return self::in_layer($in) . $out; } public static function in_layer($css) { if ('' === (string) $css) { return ''; } list($layered, $copies) = self::split_css((string) $css, static function ($selector, $body) { $a = array(); $b = array(); foreach (explode(';', $body) as $declaration) { $declaration = trim($declaration); if ($declaration === '' || strpos($declaration, ':') === false) { continue; } if (preg_match('/!\s*important\s*$/i', $declaration)) { $b[] = $declaration; } else { $a[] = $declaration; } } return array($a ? $selector . '{' . implode(';', $a) . ';}' : '', $b ? $selector . '{' . implode(';', $b) . ';}' : ''); }); if ('' === $copies) { return '@layer rooster.one{' . $css . '}'; } return ('' !== $layered ? '@layer rooster.one{' . $layered . '}' : '') . $copies; } const OWN = '/^(aieo-|rc-|om-|rs-|rooster-one|wp-block-aieo-)/'; const VENDOR = '/^(dgwt|fibo|gform|gfield|ginput|gf_|gchoice|select2|fc-|h5p|swiper|flatpickr|pswp|ui-|mfp|slick|tolstoy|wpml|otgs|grecaptcha|stripe|klarna|paypal|iti|leaflet|mapboxgl|elementor|e-)/'; const WOO = '/^(wc-|woocommerce|wp-block-woocommerce|variations|single_add_to_cart_button|cart_item|shop_table|onsale|added_to_cart|quantity|qty|reset_variations|checkout|shipping|payment)/'; const CORE = '/^(wp-block-|wp-container|wp-element|is-layout-|alignfull|alignwide|wp-site-blocks|screen-reader-text|admin-bar|wp-|has-|is-style-|entry-content)/'; public static function selector_is_ours($selector) { $compounds = array(); $part = ''; $depth = 0; $len = strlen($selector); for ($k = 0; $k < $len; $k++) { $c = $selector[$k]; if ($c === '(' || $c === '[') { $depth++; } elseif ($c === ')' || $c === ']') { $depth--; } if ($depth === 0 && ($c === '>' || $c === '+' || $c === '~' || ctype_space($c))) { if (trim($part) !== '') { $compounds[] = trim($part); } $part = ''; continue; } $part .= $c; } if (trim($part) !== '') { $compounds[] = trim($part); } foreach (array_reverse($compounds) as $compound) { for ($guard = 0; $guard < 8 && preg_match('/:(not|has)\(/', $compound); $guard++) { $compound = (string) preg_replace('/:(not|has)\((?:[^()]|\([^()]*\))*\)/', '', $compound); } preg_match_all('/[.#]([A-Za-z_][\w-]*)/', $compound, $classes); preg_match_all('/\[data-([\w-]+)/', $compound, $data); $tokens = array_map('strtolower', array_merge($classes[1], $data[1])); if (!$tokens) { continue; } $kind = array('own' => false, 'theirs' => false, 'vendor' => false, 'woo' => false, 'core' => false, 'theme' => false); foreach ($tokens as $token) { if (preg_match(self::OWN, $token)) { $kind['own'] = true; continue; } $kind['vendor'] = $kind['vendor'] || (bool) preg_match(self::VENDOR, $token); $kind['woo'] = $kind['woo'] || (bool) preg_match(self::WOO, $token); $kind['core'] = $kind['core'] || (bool) preg_match(self::CORE, $token); $kind['theme'] = $kind['theme'] || 0 === strpos($token, 'rooster-'); } $kind['theirs'] = $kind['vendor'] || $kind['woo'] || $kind['core'] || $kind['theme']; if ($kind['own']) { return !$kind['theirs']; } if ($kind['vendor']) { return false; } if ($kind['woo']) { return true; } if ($kind['core'] || $kind['theme']) { return false; } } return false; } public static function split_markup($css) { return self::split_css($css, static function ($selector, $body) { $ours = array(); $others = array(); foreach (self::selector_list($selector) as $one) { if (self::selector_is_ours($one)) { $ours[] = $one; } else { $others[] = $one; } } return array($ours ? implode(',', $ours) . '{' . $body . '}' : '', $others ? implode(',', $others) . '{' . $body . '}' : ''); }); } private static function selector_list($list) { $out = array(); $part = ''; $depth = 0; $quote = ''; $len = strlen($list); for ($k = 0; $k < $len; $k++) { $c = $list[$k]; if ($quote !== '') { $part .= $c; if ($c === $quote) { $quote = ''; } continue; } if ($c === '"' || $c === "'") { $quote = $c; } elseif ($c === '(' || $c === '[') { $depth++; } elseif ($c === ')' || $c === ']') { $depth--; } elseif ($c === ',' && $depth === 0) { if (trim($part) !== '') { $out[] = trim($part); } $part = ''; continue; } $part .= $c; } if (trim($part) !== '') { $out[] = trim($part); } return $out; } public static function split_properties($css, array $props) { return self::split_css($css, static function ($selector, $body) use ($props) { return self::split_rule($selector, $body, $props); }); } private static function split_css($css, $rule) { $in = ''; $out = ''; $n = strlen($css); $i = 0; while ($i < $n) { $open = strpos($css, '{', $i); if ($open === false) { break; } $prelude = trim(substr($css, $i, $open - $i)); if (strpos($prelude, '@') === 0) { $j = $open + 1; $a = ''; $b = ''; while ($j < $n) { while ($j < $n && ctype_space($css[$j])) { $j++; } if ($j >= $n || $css[$j] === '}') { $j++; break; } $o = strpos($css, '{', $j); $c = $o === false ? false : strpos($css, '}', $o); if ($c === false) { $j = $n; break; } list($x, $y) = $rule(trim(substr($css, $j, $o - $j)), substr($css, $o + 1, $c - $o - 1)); $a .= $x; $b .= $y; $j = $c + 1; } $in .= '' !== $a ? $prelude . '{' . $a . '}' : ''; $out .= '' !== $b ? $prelude . '{' . $b . '}' : ''; $i = $j; continue; } $c = strpos($css, '}', $open); if ($c === false) { break; } list($x, $y) = $rule($prelude, substr($css, $open + 1, $c - $open - 1)); $in .= $x; $out .= $y; $i = $c + 1; } return array($in, $out); } private static function split_rule($selector, $body, array $props) { $a = array(); $b = array(); foreach (explode(';', $body) as $declaration) { $declaration = trim($declaration); if ($declaration === '' || strpos($declaration, ':') === false) { continue; } if (in_array(trim(substr($declaration, 0, strpos($declaration, ':'))), $props, true)) { $b[] = $declaration; } else { $a[] = $declaration; } } return array($a ? $selector . '{' . implode(';', $a) . ';}' : '', $b ? $selector . '{' . implode(';', $b) . ';}' : ''); } private static function plugin_file($url) { $path_part = (string) wp_parse_url($url, PHP_URL_PATH); $pos = strpos($path_part, '/plugins/'); return $pos === false ? '' : WP_PLUGIN_DIR . substr($path_part, $pos + strlen('/plugins')); } public static function min_style_paths() { $styles = wp_styles(); foreach ((array) $styles->queue as $handle) { $style = isset($styles->registered[$handle]) ? $styles->registered[$handle] : null; if (!$style || !is_string($style->src) || $style->src === '') { continue; } if (!$styles->get_data($handle, 'path')) { continue; } $min = self::layer_twin($style->src); if (self::$min_on) { $min = self::rewrite($min); } if ($min === $style->src) { continue; } $abs = self::plugin_file($min); if ($abs === '' || !is_file($abs)) { continue; } $styles->add_data($handle, 'path', wp_normalize_path($abs)); } } public static function module_src($abs, $url) { if (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) { return $url; } $min_abs = preg_replace('/\.js$/', '.min.js', (string) $abs); if ($min_abs === $abs || !is_file($min_abs) || !is_file($abs) || filemtime($min_abs) < filemtime($abs)) { return $url; } return preg_replace('/\.js(?=[?#]|$)/', '.min.js', (string) $url, 1); } private static $deferred_modules = array(); public static function defer_module($id, $url, $ver = '') { if (defined('AIEO_ENGINE_DIR')) { wp_enqueue_script_module((string) $id); return; } if (empty(self::$deferred_modules)) { add_action('wp_print_footer_scripts', array(__CLASS__, 'print_deferred_modules'), 30); } self::$deferred_modules[(string) $id] = '' !== (string) $ver ? add_query_arg('ver', (string) $ver, (string) $url) : (string) $url; } public static function print_deferred_modules() { if (empty(self::$deferred_modules)) { return; } $urls = array_values(array_map('esc_url_raw', self::$deferred_modules)); self::$deferred_modules = array(); $js = '(function(){var m=' . wp_json_encode($urls) . ',d=false;function go(){if(d){return;}d=true;m.forEach(function(u){try{import(u).catch(function(){});}catch(e){}});}' . "['pointerdown','keydown','touchstart','wheel','scroll'].forEach(function(e){addEventListener(e,go,{once:true,passive:true});});" . "addEventListener('rooster:human',go,{once:true});})();"; echo '' . "\n"; } public static function rewrite($src) { if (!is_string($src) || $src === '') { return $src; } if (strpos($src, '/ai-eshop-optimizer/') === false && strpos($src, '/ai-eshop-optimizer-runtime/') === false && strpos($src, '/ai-eshop-optimizer-pro/') === false) { return $src; } if (preg_match('#\.min\.(?:css|js)(?:[?\#]|$)#', $src)) { return $src; } if (!preg_match('#\.(css|js)(?:([?\#]).*)?$#', $src)) { return $src; } $min = preg_replace('#\.(css|js)(?=[?\#]|$)#', '.min.$1', $src, 1); $path_part = (string) wp_parse_url($min, PHP_URL_PATH); $marker = '/plugins/'; $pos = strpos($path_part, $marker); if ($pos === false) { return $src; } $abs = WP_PLUGIN_DIR . substr($path_part, $pos + strlen('/plugins')); if (!file_exists($abs)) { return $src; } $src_path = preg_replace('#\.min\.(css|js)$#', '.$1', $abs); if ($src_path !== $abs && file_exists($src_path) && filemtime($src_path) > filemtime($abs)) { return $src; } return $min; } } }