Last active
May 24, 2024 11:54
-
-
Save Crocoblock/51502b3bbd835aef3b3453411ec84a10 to your computer and use it in GitHub Desktop.
JetEngine. Components. Replace CSS vars from component properties with raw color values of these properties in oEmbed URLs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'jet-engine/component/before-content', function( $component ) { | |
add_filter( 'oembed_result', 'jet_components_fix_oembed_vars', 99 ); | |
} ); | |
add_action( 'jet-engine/component/after-content', function() { | |
remove_filter( 'oembed_result', 'jet_components_fix_oembed_vars', 99 ); | |
} ); | |
function jet_components_fix_oembed_vars( $html ) { | |
$html = preg_replace_callback( '/var\(--jet-component-(.*?)\)/', function( $matches ) { | |
$value = jet_engine()->listings->components->state->get( $matches[1] ); | |
if ( ! $value ) { | |
$globals = jet_engine()->listings->components->state->get( '__globals__' ); | |
$value = isset( $globals ) ? $globals[ $matches[1] ] : false; | |
if ( $value && false !== strpos( $value, 'globals/colors?id=' ) ) { | |
$system_colors = \Elementor\Plugin::$instance->kits_manager->get_current_settings( 'system_colors' ); | |
$custom_colors = \Elementor\Plugin::$instance->kits_manager->get_current_settings( 'custom_colors' ); | |
if ( $custom_colors ) { | |
$system_colors = array_merge( $system_colors, $custom_colors ); | |
} | |
$id = str_replace( 'globals/colors?id=', '', $value ); | |
$value = false; | |
foreach ( $system_colors as $color ) { | |
if ( $color['_id'] === $id ) { | |
$value = $color['color']; | |
break; | |
} | |
} | |
} | |
} | |
if ( $value ) { | |
return str_replace( '#', '', $value ); | |
} | |
return $matches[0]; | |
}, $html ); | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment