PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.1
Elementor Website Builder – more than just a page builder v3.32.1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / global-classes / atomic-global-styles.php

atomic-global-styles.php in Elementor Website Builder – more than just a page builder 3.32.1, at modules/global-classes/atomic-global-styles.php

92 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\GlobalClasses;
4
5 use Elementor\Plugin;
6 use Elementor\Modules\AtomicWidgets\Styles\Atomic_Styles_Manager;
7 use Elementor\Modules\AtomicWidgets\Cache_Validity;
8
9 class Atomic_Global_Styles {
10 const STYLES_KEY = 'global';
11
12 public function register_hooks() {
13 add_action(
14 'elementor/atomic-widgets/styles/register',
15 fn( Atomic_Styles_Manager $styles_manager, array $post_ids ) => $this->register_styles( $styles_manager ),
16 20,
17 2
18 );
19
20 add_action( 'elementor/global_classes/update', fn( string $context ) => $this->invalidate_cache( $context ), 10, 1 );
21
22 add_action(
23 'deleted_post',
24 fn( $post_id ) => $this->on_post_delete( $post_id )
25 );
26
27 add_action(
28 'elementor/core/files/clear_cache',
29 fn() => $this->invalidate_cache(),
30 );
31
32 add_filter('elementor/atomic-widgets/settings/transformers/classes',
33 fn( $value ) => $this->transform_classes_names( $value )
34 );
35 }
36
37 private function register_styles( Atomic_Styles_Manager $styles_manager ) {
38 $context = is_preview() ? Global_Classes_Repository::CONTEXT_PREVIEW : Global_Classes_Repository::CONTEXT_FRONTEND;
39
40 $get_styles = function () use ( $context ) {
41 return Global_Classes_Repository::make()->context( $context )->all()->get_items()->map( function( $item ) {
42 $item['id'] = $item['label'];
43 return $item;
44 })->all();
45 };
46
47 $styles_manager->register(
48 self::STYLES_KEY . '-' . $context,
49 $get_styles,
50 [ self::STYLES_KEY, $context ]
51 );
52 }
53
54 private function on_post_delete( $post_id ) {
55 if ( ! Plugin::$instance->kits_manager->is_kit( $post_id ) ) {
56 return;
57 }
58
59 $this->invalidate_cache();
60 }
61
62 private function invalidate_cache( ?string $context = null ) {
63 $cache_validity = new Cache_Validity();
64
65 if ( empty( $context ) || Global_Classes_Repository::CONTEXT_FRONTEND === $context ) {
66 $cache_validity->invalidate( [ self::STYLES_KEY ] );
67
68 return;
69 }
70
71 $cache_validity->invalidate( [ self::STYLES_KEY, $context ] );
72 }
73
74 private function transform_classes_names( $ids ) {
75 $context = is_preview() ? Global_Classes_Repository::CONTEXT_PREVIEW : Global_Classes_Repository::CONTEXT_FRONTEND;
76
77 $classes = Global_Classes_Repository::make()
78 ->context( $context )
79 ->all()
80 ->get_items();
81
82 return array_map(
83 function( $id ) use ( $classes ) {
84 $class = $classes->get( $id );
85
86 return $class ? $class['label'] : $id;
87 },
88 $ids
89 );
90 }
91 }
92