| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\GlobalClasses; |
| 4 |
|
| 5 |
use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 6 |
use Elementor\Modules\AtomicWidgets\Styles\Styles_Renderer; |
| 7 |
use Elementor\Plugin; |
| 8 |
|
| 9 |
class Global_Classes_CSS { |
| 10 |
public function register_hooks() { |
| 11 |
add_action( 'elementor/css-file/post/parse', fn( Post_CSS $post ) => $this->inject_global_classes( $post ), 20 ); |
| 12 |
|
| 13 |
add_action( 'elementor/global_classes/create', fn() => $this->clear_kit_css_cache() ); |
| 14 |
add_action( 'elementor/global_classes/update', fn() => $this->clear_kit_css_cache() ); |
| 15 |
add_action( 'elementor/global_classes/delete', fn() => $this->clear_kit_css_cache() ); |
| 16 |
add_action( 'elementor/global_classes/arrange', fn() => $this->clear_kit_css_cache() ); |
| 17 |
} |
| 18 |
|
| 19 |
private function inject_global_classes( Post_CSS $post ) { |
| 20 |
if ( ! Plugin::$instance->kits_manager->is_kit( $post->get_post_id() ) ) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
$global_classes = Global_Classes_Repository::make()->all(); |
| 25 |
|
| 26 |
if ( $global_classes->get_items()->is_empty() ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
$sorted_items = $global_classes |
| 31 |
->get_order() |
| 32 |
->map( |
| 33 |
fn( $id ) => $global_classes->get_items()->get( $id ) |
| 34 |
); |
| 35 |
|
| 36 |
$css = Styles_Renderer::make( |
| 37 |
Plugin::$instance->breakpoints->get_breakpoints_config() |
| 38 |
)->on_prop_transform( function( $key, $value ) use ( &$post ) { |
| 39 |
if ( 'font-family' !== $key ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$post->add_font( $value ); |
| 44 |
} )->render( $sorted_items->all() ); |
| 45 |
|
| 46 |
$post->get_stylesheet()->add_raw_css( $css ); |
| 47 |
} |
| 48 |
|
| 49 |
private function clear_kit_css_cache() { |
| 50 |
$kit_id = Plugin::$instance->kits_manager->get_active_id(); |
| 51 |
|
| 52 |
Post_CSS::create( $kit_id )->delete(); |
| 53 |
} |
| 54 |
} |
| 55 |
|