' ) || str_contains( $selector, '+' ) || str_contains( $selector, '~' ) ) { return __( 'Complex selectors with combinators or spaces are not supported.', 'v4studio' ); } if ( str_starts_with( $selector, '#' ) ) { return __( 'ID selectors are not supported.', 'v4studio' ); } if ( ! str_starts_with( $selector, '.' ) ) { return __( 'Only simple class selectors are supported.', 'v4studio' ); } return __( 'Invalid class name.', 'v4studio' ); } public function detect_variable_type( string $value ): string { $value = trim( $value ); if ( preg_match( '/^(#(?:[0-9a-fA-F]{3,4}){1,2}|rgba?\([^)]+\)|hsla?\([^)]+\))$/', $value ) || $this->is_css_color_keyword( $value ) ) { return 'color'; } if ( $this->is_font_family_value( $value ) ) { return 'font'; } if ( preg_match( '/^-?\d*\.?\d+(px|rem|em|%|vw|vh|ch|vmin|vmax|fr)$/i', $value ) || 'auto' === strtolower( $value ) ) { return 'size'; } if ( preg_match( '/^(clamp|calc|min|max|fit-content)\(.+\)$/i', $value ) && preg_match( '/(-?\d*\.?\d+(px|rem|em|%|vw|vh|ch|vmin|vmax|fr)|auto)/i', $value ) ) { return 'size'; } if ( preg_match( '/^var\(--(?:text|space|radius|size|width|height)[a-zA-Z0-9_-]*\)$/i', $value ) ) { return 'size'; } if ( is_numeric( $value ) ) { return 'number'; } return 'text'; } private function is_css_color_keyword( string $value ): bool { $value = strtolower( trim( $value ) ); if ( '' === $value ) { return false; } $keywords = [ 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray', 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred', 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'transparent', 'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen', 'currentcolor', ]; return in_array( $value, $keywords, true ); } private function is_font_family_value( string $value ): bool { $value = trim( $value ); if ( '' === $value || str_contains( $value, '(' ) || str_contains( $value, ')' ) ) { return false; } if ( preg_match( '/(^|,)\s*["\']?[-a-zA-Z0-9 ]+["\']?\s*(,|$)/', $value ) && preg_match( '/(serif|sans-serif|monospace|cursive|fantasy|system-ui|ui-)/i', $value ) ) { return true; } $family = $this->font_family_name_from_value( $value ); if ( '' === $family || ! preg_match( '/^[\p{L}\p{N}][\p{L}\p{N} ._-]*$/u', $family ) ) { return false; } return in_array( $this->normalize_font_family_key( $family ), $this->get_known_font_family_keys(), true ); } private function normalize_font_family_key( string $font ): string { $font = strtolower( trim( $font ) ); $font = preg_replace( '/\s+/', ' ', $font ) ?? $font; return $font; } private function get_known_font_family_keys(): array { if ( null !== $this->known_font_families ) { return $this->known_font_families; } $fonts = [ 'Inter', 'Poppins', 'Roboto', 'Open Sans', 'Lato', 'Montserrat', 'Oswald', 'Raleway', 'Merriweather', 'Playfair Display', 'Nunito Sans', 'Source Sans 3', 'Work Sans', 'DM Sans', 'Manrope', 'Abel', 'Akaya Telivigala', ]; if ( class_exists( '\Elementor\Fonts' ) && method_exists( '\Elementor\Fonts', 'get_fonts' ) ) { $elementor_fonts = \Elementor\Fonts::get_fonts(); if ( is_array( $elementor_fonts ) ) { $fonts = array_merge( $fonts, array_keys( $elementor_fonts ) ); } } if ( function_exists( 'get_post_types' ) && function_exists( 'get_posts' ) ) { $post_types = []; foreach ( get_post_types( [], 'names' ) as $post_type ) { if ( str_contains( (string) $post_type, 'elementor' ) && str_contains( (string) $post_type, 'font' ) ) { $post_types[] = $post_type; } } if ( $post_types ) { $posts = get_posts( [ 'post_type' => $post_types, 'post_status' => [ 'publish', 'draft', 'private' ], 'posts_per_page' => 200, 'orderby' => 'title', 'order' => 'ASC', 'fields' => 'ids', ] ); foreach ( $posts as $post_id ) { $title = get_the_title( $post_id ); if ( is_string( $title ) && '' !== trim( $title ) ) { $fonts[] = $title; } } } } $this->known_font_families = array_values( array_unique( array_filter( array_map( fn( string $font ): string => $this->normalize_font_family_key( $font ), array_map( 'strval', $fonts ) ) ) ) ); return $this->known_font_families; } }