collect_globals( $settings ); $instance->replace_global_settings( $elements_data ); return $elements_data; } /** * Collect colors and fonts from kit settings. */ private function collect_globals( array $settings ): void { $keys = [ 'system_colors', 'custom_colors', 'system_typography', 'custom_typography' ]; foreach ( $keys as $key ) { if ( empty( $settings[ $key ] ) ) { continue; } // `strpos()`, not `str_contains()` (PHP 8.0+, core-polyfilled only from WP 5.9): // the plugin advertises WordPress 5.0 / PHP 7.2. if ( strpos( $key, 'colors' ) !== false ) { $this->colors = array_merge( $this->colors, $settings[ $key ] ); } else { $this->fonts = array_merge( $this->fonts, $settings[ $key ] ); } } } /** * Walk elements tree and replace __globals__ references with actual values. */ private function replace_global_settings( array &$elements_data ): void { foreach ( $elements_data as &$element ) { if ( ! empty( $element['settings'] ) && ! empty( $element['settings']['__globals__'] ) ) { foreach ( $element['settings']['__globals__'] as $key => $setting ) { if ( $this->is_color_setting( $setting ) ) { $this->replace_color( $element['settings'], $key ); } if ( $this->is_typography_setting( $setting ) ) { $this->replace_fonts( $element['settings'], $key ); } } } if ( ! empty( $element['elements'] ) ) { $this->replace_global_settings( $element['elements'] ); } } } private function replace_color( array &$settings, string $key ): void { $color = $this->get_color( str_replace( 'globals/colors?id=', '', trim( $settings['__globals__'][ $key ] ) ) ); if ( ! empty( $color ) ) { $settings[ $key ] = $color; unset( $settings['__globals__'][ $key ] ); } } private function replace_fonts( array &$settings, string $key ): void { $font = $this->get_font( str_replace( 'globals/typography?id=', '', trim( $settings['__globals__'][ $key ] ) ) ); if ( ! empty( $font ) ) { unset( $font['_id'], $font['title'] ); unset( $settings['__globals__'][ $key ] ); $settings = array_merge( $settings, $font ); } } private function get_color( string $id ): ?string { foreach ( $this->colors as $color ) { if ( isset( $color['_id'] ) && $color['_id'] === $id ) { return $color['color'] ?? null; } } return null; } private function get_font( string $id ): ?array { foreach ( $this->fonts as $font ) { if ( isset( $font['_id'] ) && $font['_id'] === $id ) { return $font; } } return null; } private function is_color_setting( string $setting ): bool { return strpos( $setting, 'globals/colors' ) !== false; } private function is_typography_setting( string $setting ): bool { return strpos( $setting, 'globals/typography' ) !== false; } }