true ) ) : null; return is_array( $data ) ? $data : array(); } public static function files( $kind ) { if ( method_exists( '\Rooster\Core\Setup', 'variation_files' ) ) { return (array) \Rooster\Core\Setup::variation_files( $kind ); } $out = array(); foreach ( glob( get_template_directory() . '/styles/' . $kind . '/*.json' ) ?: array() as $file ) { $out[ basename( $file, '.json' ) ] = $file; } return $out; } private static function luminance( $hex ) { if ( ! preg_match( '/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', (string) $hex, $m ) ) { return null; } $h = 3 === strlen( $m[1] ) ? $m[1][0] . $m[1][0] . $m[1][1] . $m[1][1] . $m[1][2] . $m[1][2] : $m[1]; $c = static function ( $pair ) { $v = hexdec( $pair ) / 255; return $v <= 0.03928 ? $v / 12.92 : ( ( $v + 0.055 ) / 1.055 ) ** 2.4; }; return 0.2126 * $c( substr( $h, 0, 2 ) ) + 0.7152 * $c( substr( $h, 2, 2 ) ) + 0.0722 * $c( substr( $h, 4, 2 ) ); } private static function contrast( $a, $b ) { $la = self::luminance( $a ); $lb = self::luminance( $b ); return null === $la || null === $lb ? 0.0 : ( max( $la, $lb ) + 0.05 ) / ( min( $la, $lb ) + 0.05 ); } private static function is_dark( array $palette ) { $l = self::luminance( (string) ( $palette['bg'] ?? '' ) ); return null !== $l && $l < 0.2; } public static function site() { $active = get_option( 'rooster_active_variations' ); $active = is_array( $active ) ? $active : array(); return array( 'palette' => sanitize_key( (string) ( $active['colors'] ?? 'default' ) ), 'fonts' => sanitize_key( (string) ( $active['typography'] ?? '' ) ), 'shape' => sanitize_key( (string) rooster_setting( 'design', 'shape', 'soft' ) ), ); } public static function dots( array $palette ) { return array_values( array_filter( array( $palette['bg'] ?? '', $palette['accent'] ?? '', $palette['accent-2'] ?? '' ) ) ); } public static function own_palette() { $palette = wp_get_global_settings( array( 'color', 'palette' ) ); return self::colours( (array) ( $palette['custom'] ?? array() ) ) + self::colours( (array) ( $palette['theme'] ?? array() ) ); } public static function palettes() { $out = array(); $colours = self::files( 'colors' ); $pairs = (array) wp_get_global_settings( array( 'custom', 'rooster', 'schemePairs' ) ); foreach ( $colours as $slug => $file ) { $data = self::read( $file ); $palette = self::colours( (array) ( $data['settings']['color']['palette'] ?? array() ) ); if ( ! isset( $palette['bg'] ) || self::is_dark( $palette ) ) { continue; } $pair = (string) ( $pairs[ $slug ] ?? '' ); $out[ sanitize_key( (string) $slug ) ] = array( 'title' => (string) ( $data['title'] ?? $slug ), 'dots' => self::dots( $palette ), 'night' => '' !== $pair && isset( $colours[ $pair ] ) ? (string) ( self::read( $colours[ $pair ] )['title'] ?? $pair ) : '', ); } return $out; } public static function typographies() { $out = array(); foreach ( self::files( 'typography' ) as $slug => $file ) { $out[ sanitize_key( (string) $slug ) ] = (string) ( self::read( $file )['title'] ?? $slug ); } return $out; } public static function presets() { $out = array(); foreach ( class_exists( '\Rooster\Core\Design' ) && defined( '\Rooster\Core\Design::PRESETS' ) ? (array) \Rooster\Core\Design::PRESETS : array() as $slug => $roles ) { if ( is_array( $roles ) ) { $out[ sanitize_key( (string) $slug ) ] = $roles; } } return $out; } public static function looks() { if ( ! function_exists( 'rooster_looks' ) ) { return array(); } $out = array(); foreach ( (array) rooster_looks() as $slug => $look ) { $slug = sanitize_key( (string) $slug ); if ( '' === $slug || ! is_array( $look ) ) { continue; } $out[ $slug ] = array( 'title' => '' !== trim( (string) ( $look['title'] ?? '' ) ) ? trim( (string) $look['title'] ) : $slug, 'skin' => sanitize_key( (string) ( $look['skin'] ?? '' ) ), ); } return $out; } public static function look() { return function_exists( 'rooster_look' ) ? sanitize_key( (string) rooster_look() ) : ''; } public static function served() { return array() !== self::looks(); } public static function skin_file( $skin ) { $skin = sanitize_key( (string) $skin ); $file = '' !== $skin ? get_theme_file_path( 'styles/skins/' . $skin . '.json' ) : ''; return '' !== $file && is_readable( $file ) ? $file : ''; } public static function skin( $skin ) { $file = self::skin_file( $skin ); if ( '' !== $file ) { return self::read( $file ); } if ( '' !== (string) $skin && class_exists( '\Rooster\Core\Skins' ) && method_exists( '\Rooster\Core\Skins', 'get' ) ) { return (array) \Rooster\Core\Skins::get( sanitize_key( (string) $skin ) ); } return array(); } public static function skin_dots( $skin ) { return self::dots( self::colours( (array) ( self::skin( $skin )['settings']['color']['palette'] ?? array() ) ) ); } public static function key() { if ( null !== self::$key ) { return self::$key; } $parts = array( self::GENERATOR, get_template(), wp_get_theme( get_template() )->get( 'Version' ), get_stylesheet(), wp_get_theme()->get( 'Version' ), wp_json_encode( self::presets() ) ); foreach ( array( 'colors', 'typography' ) as $kind ) { foreach ( self::files( $kind ) as $slug => $file ) { $parts[] = $slug . ':' . ( is_file( $file ) ? (int) filemtime( $file ) : 0 ); } } $parts[] = (int) @filemtime( get_template_directory() . '/theme.json' ); foreach ( self::looks() as $slug => $look ) { $file = self::skin_file( $look['skin'] ); $parts[] = 'look:' . $slug . ':' . $look['skin'] . ':' . ( '' !== $file ? (int) filemtime( $file ) : 0 ); } self::$key = substr( md5( implode( '|', $parts ) ), 0, 12 ); return self::$key; } public static function reset() { self::$key = null; } public static function styles_url() { $up = wp_upload_dir( null, false ); if ( ! empty( $up['error'] ) ) { return ''; } $name = self::key() . '.json'; $dir = trailingslashit( $up['basedir'] ) . self::DIR; $file = $dir . '/' . $name; if ( ! is_file( $file ) ) { if ( ! wp_mkdir_p( $dir ) ) { return ''; } foreach ( glob( $dir . '/*.json' ) ?: array() as $old ) { wp_delete_file( $old ); } if ( false === file_put_contents( $file, (string) wp_json_encode( self::build(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ) ) { return ''; } } return set_url_scheme( trailingslashit( $up['baseurl'] ) . self::DIR . '/' . $name ); } public static function build() { $out = array( 'v' => self::key() ) + array_fill_keys( self::AXES, array() ); $colours = self::files( 'colors' ); $pairs = (array) wp_get_global_settings( array( 'custom', 'rooster', 'schemePairs' ) ); $dark = self::colours( (array) ( self::read( $colours['dark'] ?? '' )['settings']['color']['palette'] ?? array() ) ); foreach ( $colours as $slug => $file ) { $data = self::read( $file ); $day = self::colours( (array) ( $data['settings']['color']['palette'] ?? array() ) ); if ( ! isset( $day['bg'] ) || self::is_dark( $day ) ) { continue; } $day_custom = self::custom( (array) ( $data['settings']['custom'] ?? array() ) ) . self::custom( (array) ( $data['rooster']['tokens'] ?? array() ), '--rooster' ); $pair = (string) ( $pairs[ $slug ] ?? '' ); $night_data = '' !== $pair && isset( $colours[ $pair ] ) ? self::read( $colours[ $pair ] ) : array(); $night = self::colours( (array) ( $night_data['settings']['color']['palette'] ?? array() ) ); $night_custom = self::custom( (array) ( $night_data['settings']['custom'] ?? array() ) ) . self::custom( (array) ( $night_data['rooster']['tokens'] ?? array() ), '--rooster' ); if ( ! isset( $night['bg'] ) ) { $night = $dark; $night_custom = ''; foreach ( array( 'accent', 'accent-2' ) as $accent ) { if ( isset( $day[ $accent ], $night['bg'] ) && self::contrast( $day[ $accent ], $night['bg'] ) >= 4.5 ) { $night[ $accent ] = $day[ $accent ]; } } if ( isset( $day['accent'], $night['accent'] ) && $night['accent'] === $day['accent'] ) { $night['accent-text'] = $day['accent']; if ( isset( $day['on-accent'] ) ) { $night['on-accent'] = $day['on-accent']; } } } $sel = ':root[data-aieo-palette="' . sanitize_key( (string) $slug ) . '"]'; $css = $sel . '{' . self::vars( $day ) . $day_custom . '}'; $css .= isset( $night['bg'] ) ? $sel . '[data-rooster-base="dark"],' . $sel . '[data-rooster-scheme="dark"]{' . self::vars( $night ) . $night_custom . '}' : ''; $css .= $sel . '[data-rooster-scheme="light"]{' . self::vars( $day ) . $day_custom . '}'; $out['palette'][ sanitize_key( (string) $slug ) ] = $css; } foreach ( self::files( 'typography' ) as $slug => $file ) { $css = self::fonts_css( sanitize_key( (string) $slug ), self::read( $file ), $file ); if ( '' !== $css ) { $out['fonts'][ sanitize_key( (string) $slug ) ] = $css; } } foreach ( self::presets() as $slug => $roles ) { $vars = ''; foreach ( self::ROLES as $role ) { $step = (string) ( $roles[ $role ] ?? '' ); $vars .= preg_match( '/^[a-z0-9]+$/', $step ) ? '--wp--custom--rooster--shape--' . $role . ':' . ( 'none' === $step ? '0' : 'var(--wp--custom--rooster--radius--' . $step . ')' ) . ';' : ''; } $out['shape'][ $slug ] = ':root[data-aieo-shape="' . $slug . '"]{' . $vars . '}'; } $looks = self::looks(); if ( array() !== $looks && function_exists( 'rooster_skin_css' ) ) { $out['skins'] = array(); $out['looks'] = array(); foreach ( $looks as $slug => $look ) { $css = '' !== $look['skin'] ? trim( (string) rooster_skin_css( $look['skin'] ) ) : ''; if ( '' === $css || ! self::one_rule( $css ) ) { continue; } $out['skins'][ $slug ] = $css; $data = self::skin( $look['skin'] ); $opens = (string) ( $data['rooster']['opens'] ?? '' ); $out['looks'][ $slug ] = array( 'opens' => in_array( $opens, array( 'light', 'dark' ), true ) ? $opens : '', 'faces' => self::faces( $data, self::skin_file( $look['skin'] ) ), ); } } return $out; } public static function one_rule( $css ) { $css = (string) $css; if ( false !== strpos( $css, '<' ) ) { return false; } $depth = 0; foreach ( str_split( (string) preg_replace( '/[^{}]/', '', $css ) ) as $brace ) { $depth += '{' === $brace ? 1 : -1; if ( $depth < 0 ) { return false; } } return 0 === $depth; } private static function vars( array $palette ) { $out = ''; foreach ( $palette as $slug => $color ) { $out .= '--wp--preset--color--' . $slug . ':' . $color . ';'; } return $out; } private static function custom( array $node, $path = '' ) { $out = ''; foreach ( $node as $key => $value ) { $name = $path . '--' . strtolower( (string) preg_replace( '/([a-z])([A-Z])/', '$1-$2', (string) $key ) ); if ( ! preg_match( '/^[a-z0-9-]+$/', ltrim( $name, '-' ) . 'x' ) ) { continue; } if ( is_array( $value ) ) { $out .= self::custom( $value, $name ); } elseif ( is_scalar( $value ) && '' !== (string) $value && ! preg_match( '/[;{}<>]/', (string) $value ) && 'schemePairs' !== $key ) { $out .= '--wp--custom' . $name . ':' . $value . ';'; } } return $out; } private static function stack( $value ) { $value = (string) $value; return preg_match( '/^[A-Za-z0-9 ,"\'.\-]+$/', $value ) ? $value : ''; } public static function faces( array $data, $file ) { $faces = ''; $child = get_stylesheet_directory() !== get_template_directory() && 0 === strpos( wp_normalize_path( (string) $file ), wp_normalize_path( trailingslashit( get_stylesheet_directory() ) ) ); $uri = $child ? get_stylesheet_directory_uri() : get_template_directory_uri(); foreach ( (array) ( $data['settings']['typography']['fontFamilies'] ?? array() ) as $family ) { if ( ! is_array( $family ) || '' === sanitize_key( (string) ( $family['slug'] ?? '' ) ) || '' === self::stack( $family['fontFamily'] ?? '' ) ) { continue; } foreach ( (array) ( $family['fontFace'] ?? array() ) as $face ) { $name = self::stack( $face['fontFamily'] ?? '' ); $src = array(); foreach ( (array) ( $face['src'] ?? array() ) as $one ) { $one = (string) $one; $url = 0 === strpos( $one, 'file:./' ) ? trailingslashit( $uri ) . substr( $one, 7 ) : ''; if ( '' !== $url && preg_match( '/\.(woff2?|ttf|otf)$/i', $url ) ) { $src[] = 'url(' . esc_url_raw( $url ) . ') format("' . ( preg_match( '/\.woff2$/i', $url ) ? 'woff2' : ( preg_match( '/\.woff$/i', $url ) ? 'woff' : 'truetype' ) ) . '")'; } } if ( '' === $name || array() === $src ) { continue; } $faces .= '@font-face{font-family:"' . trim( $name, '"\'' ) . '";font-style:' . ( 'italic' === ( $face['fontStyle'] ?? '' ) ? 'italic' : 'normal' ) . ';font-weight:' . ( preg_match( '/^[0-9 ]+$/', (string) ( $face['fontWeight'] ?? '' ) ) ? $face['fontWeight'] : '400' ) . ';font-display:swap;src:' . implode( ',', $src ) . ( preg_match( '/^[A-Fa-f0-9U+, \-?]+$/', (string) ( $face['unicodeRange'] ?? '' ) ) ? ';unicode-range:' . $face['unicodeRange'] : '' ) . '}'; } } foreach ( (array) ( $data['settings']['custom']['rooster']['fontFallback'] ?? array() ) as $name => $spec ) { $p = explode( '|', (string) $spec ); if ( 5 === count( $p ) && preg_match( '/^[A-Za-z0-9 \-]+$/', (string) $name . $p[0] ) && preg_match( '/^[0-9.]+%$/', $p[1] . $p[2] . $p[3] . $p[4] ) ) { $faces .= '@font-face{font-family:"' . $name . '";src:local("' . $p[0] . '");size-adjust:' . $p[1] . ';ascent-override:' . $p[2] . ';descent-override:' . $p[3] . ';line-gap-override:' . $p[4] . '}'; } } return $faces; } private static function fonts_css( $slug, array $data, $file ) { $faces = self::faces( $data, $file ); $vars = ''; foreach ( (array) ( $data['settings']['typography']['fontFamilies'] ?? array() ) as $family ) { $fslug = is_array( $family ) ? sanitize_key( (string) ( $family['slug'] ?? '' ) ) : ''; $value = is_array( $family ) ? self::stack( $family['fontFamily'] ?? '' ) : ''; if ( '' === $fslug || '' === $value ) { continue; } $vars .= '--wp--preset--font-family--' . $fslug . ':' . $value . ';'; if ( in_array( $fslug, array( 'body', 'heading' ), true ) ) { $vars .= '--wp--preset--font-family--rooster-' . $fslug . ':' . $value . ';'; } } if ( '' === $vars ) { return ''; } $sel = ':root[data-aieo-fonts="' . $slug . '"]'; $heading = (array) ( $data['styles']['elements']['heading']['typography'] ?? array() ); $rules = ''; foreach ( array( 'fontWeight' => 'font-weight', 'letterSpacing' => 'letter-spacing', 'fontStyle' => 'font-style' ) as $key => $prop ) { if ( isset( $heading[ $key ] ) && preg_match( '/^[A-Za-z0-9.\- ]+$/', (string) $heading[ $key ] ) ) { $rules .= $prop . ':' . $heading[ $key ] . ';'; } } return $faces . $sel . '{' . $vars . '}' . ( '' !== $rules ? $sel . ' :is(h1,h2,h3,h4,h5,h6){' . $rules . '}' : '' ); } public static function head() { if ( is_admin() || ! self::active() ) { return; } $url = self::styles_url(); if ( '' === $url ) { return; } wp_print_inline_script_tag( self::head_script( $url ), array( 'id' => 'aieo-style-head' ) ); } public static function head_script( $url ) { return '(function(w,d){var r=d.documentElement,p=' . wp_json_encode( self::AXES ) . ',K="' . self::STORE . '",V=' . wp_json_encode( self::key() ) . ',U=' . wp_json_encode( wp_make_link_relative( $url ), JSON_UNESCAPED_SLASHES ) . ',l=r.getAttribute("data-rooster-look")||"",s=0,o=[],L,a,k;' . 'w.location.hash.slice(1).split("&").forEach(function(x){if(!x.indexOf("style="))s=x.slice(6).split(".");else if(x)o.push(x)});' . 'function put(c){var e=d.getElementById(K+"-kept")||d.createElement("style");e.id=K+"-kept";e.textContent=c;d.head.appendChild(e)}' . 'function set(k){p.forEach(function(n){k[n]?r.setAttribute("data-aieo-"+n,k[n]):r.removeAttribute("data-aieo-"+n)})}' . 'function get(f){fetch(U).then(function(x){return x.json()}).then(f)}' . 'try{L=w.localStorage;a=JSON.parse(L.getItem(K)||"null");a=a&&"object"==typeof a?a:{};k=a[l];' . 'if(s){w.history.replaceState(w.history.state,"",w.location.pathname+w.location.search+(o.length?"#"+o.join("&"):""));' . 'get(function(j){var c="",e={v:V},y=0;p.forEach(function(n,i){var v=s[i]||"";v=/^[a-z0-9-]+$/.test(v)&&j[n]&&j[n][v]?v:"";e[n]=v;if(v){y=1;c+=j[n][v]}});e.css=c;y?a[l]=e:delete a[l];L.setItem(K,JSON.stringify(a));set(e);put(c)})}' . 'else if(k&&"object"==typeof k){set(k);if(k.v==V&&k.css)put(k.css);else get(function(j){var c="";p.forEach(function(n){c+=j[n]&&j[n][k[n]]||""});k.v=V;k.css=c;a[l]=k;L.setItem(K,JSON.stringify(a));put(c)})}}catch(e){}})(window,document);'; } public static function register() { if ( ! self::zero() || ( class_exists( 'WP_Block_Type_Registry' ) && WP_Block_Type_Registry::get_instance()->is_registered( self::BLOCK ) ) ) { return; } register_block_type( self::BLOCK, array( 'api_version' => 3, 'title' => __( 'Rooster One Style switcher', 'ai-eshop-optimizer' ), 'category' => 'theme', 'icon' => 'admin-appearance', 'description' => __( 'The site\'s other colour schemes, fonts and corners for visitors to try.', 'ai-eshop-optimizer' ), 'supports' => array( 'html' => false, 'align' => array( 'wide', 'full' ) ), 'render_callback' => array( __CLASS__, 'render_block' ), ) ); } public static function render_block( $attributes = array() ) { if ( ! self::zero() ) { return ''; } return (string) apply_filters( 'aieo_style_switcher_block_html', '', is_array( $attributes ) ? $attributes : array() ); } } }