| @@ -3,185 +3,89 @@ | ||
| 3 | 3 | namespace Elementor\Modules\GlobalClasses; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Plugin; |
| 6 | 6 | use Elementor\Modules\AtomicWidgets\Styles\Atomic_Styles_Manager; |
| 7 | +use Elementor\Modules\AtomicWidgets\Cache_Validity; | |
| 7 | 8 | |
| 8 | 9 | class Atomic_Global_Styles { |
| 9 | 10 | const STYLES_KEY = 'global'; |
| 10 | 11 | |
| 11 | - private Global_Classes_Relations $relations; | |
| 12 | - | |
| 13 | - public function __construct( Global_Classes_Relations $relations ) { | |
| 14 | - $this->relations = $relations; | |
| 15 | - } | |
| 16 | - | |
| 17 | 12 | public function register_hooks() { |
| 18 | 13 | add_action( |
| 19 | 14 | 'elementor/atomic-widgets/styles/register', |
| 20 | - fn( Atomic_Styles_Manager $styles_manager, array $post_ids ) => $this->register_styles( $styles_manager, $post_ids ), | |
| 15 | + fn( Atomic_Styles_Manager $styles_manager, array $post_ids ) => $this->register_styles( $styles_manager ), | |
| 21 | 16 | 20, |
| 22 | 17 | 2 |
| 23 | 18 | ); |
| 24 | 19 | |
| 25 | - add_action( | |
| 26 | - 'elementor/global_classes/update', | |
| 27 | - fn( string $context, array $changes ) => $this->invalidate_cache_for_updated_classes( $context, $changes ), | |
| 28 | - 10, | |
| 29 | - 2 | |
| 30 | - ); | |
| 20 | + add_action( 'elementor/global_classes/update', fn( string $context ) => $this->invalidate_cache( $context ), 10, 1 ); | |
| 31 | 21 | |
| 32 | 22 | add_action( |
| 33 | 23 | 'deleted_post', |
| 34 | - fn( $post_id ) => $this->on_kit_delete( $post_id ) | |
| 24 | + fn( $post_id ) => $this->on_post_delete( $post_id ) | |
| 35 | 25 | ); |
| 36 | 26 | |
| 37 | 27 | add_action( |
| 38 | 28 | 'elementor/core/files/clear_cache', |
| 39 | - fn() => $this->invalidate_all_cache(), | |
| 29 | + fn() => $this->invalidate_cache(), | |
| 40 | 30 | ); |
| 41 | 31 | |
| 42 | - add_filter( | |
| 43 | - 'elementor/atomic-widgets/settings/transformers/classes', | |
| 32 | + add_filter('elementor/atomic-widgets/settings/transformers/classes', | |
| 44 | 33 | fn( $value ) => $this->transform_classes_names( $value ) |
| 45 | 34 | ); |
| 46 | 35 | } |
| 47 | 36 | |
| 48 | - private function register_styles( Atomic_Styles_Manager $styles_manager, array $post_ids ) { | |
| 49 | - $context = $this->get_context(); | |
| 37 | + private function register_styles( Atomic_Styles_Manager $styles_manager ) { | |
| 38 | + $context = is_preview() ? Global_Classes_Repository::CONTEXT_PREVIEW : Global_Classes_Repository::CONTEXT_FRONTEND; | |
| 50 | 39 | |
| 51 | - foreach ( $post_ids as $post_id ) { | |
| 52 | - $get_styles = fn() => $this->get_document_global_styles( $post_id, $context ); | |
| 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 | + }; | |
| 53 | 46 | |
| 54 | - $styles_manager->register( | |
| 55 | - [ self::STYLES_KEY, $post_id, $context ], | |
| 56 | - $get_styles | |
| 57 | - ); | |
| 58 | - } | |
| 47 | + $styles_manager->register( | |
| 48 | + self::STYLES_KEY . '-' . $context, | |
| 49 | + $get_styles, | |
| 50 | + [ self::STYLES_KEY, $context ] | |
| 51 | + ); | |
| 59 | 52 | } |
| 60 | 53 | |
| 61 | - private function get_document_global_styles( int $post_id, string $context ): array { | |
| 62 | - $is_preview = Global_Classes_Repository::CONTEXT_PREVIEW === $context; | |
| 63 | - $class_ids = $this->relations->set_preview( $is_preview )->get_styles_by_post( $post_id ); | |
| 64 | - | |
| 65 | - if ( empty( $class_ids ) ) { | |
| 66 | - return []; | |
| 67 | - } | |
| 68 | - | |
| 69 | - $repository = Global_Classes_Repository::make(); | |
| 70 | - | |
| 71 | - if ( $is_preview ) { | |
| 72 | - $repository->set_preview( true ); | |
| 73 | - } | |
| 74 | - | |
| 75 | - $global_order = $repository->all_labels(); | |
| 76 | - $ordered_class_ids = array_values( array_intersect( array_keys( $global_order ), $class_ids ) ); | |
| 77 | - | |
| 78 | - if ( empty( $ordered_class_ids ) ) { | |
| 79 | - return []; | |
| 80 | - } | |
| 81 | - | |
| 82 | - $items = $repository->get_by_ids( $ordered_class_ids ); | |
| 83 | - $reversed_order = array_reverse( $ordered_class_ids ); | |
| 84 | - | |
| 85 | - $styles = []; | |
| 86 | - | |
| 87 | - foreach ( $reversed_order as $class_id ) { | |
| 88 | - $item = $items[ $class_id ] ?? null; | |
| 89 | - | |
| 90 | - if ( ! $item ) { | |
| 91 | - continue; | |
| 92 | - } | |
| 93 | - | |
| 94 | - $resolved_label = $global_order[ $class_id ] ?? $item['label']; | |
| 95 | - $item['id'] = $resolved_label; | |
| 96 | - $item['label'] = $resolved_label; | |
| 97 | - $styles[] = $item; | |
| 98 | - } | |
| 99 | - | |
| 100 | - return $styles; | |
| 101 | - } | |
| 102 | - | |
| 103 | - private function on_kit_delete( $post_id ) { | |
| 54 | + private function on_post_delete( $post_id ) { | |
| 104 | 55 | if ( ! Plugin::$instance->kits_manager->is_kit( $post_id ) ) { |
| 105 | 56 | return; |
| 106 | 57 | } |
| 107 | 58 | |
| 108 | - $this->invalidate_all_cache(); | |
| 59 | + $this->invalidate_cache(); | |
| 109 | 60 | } |
| 110 | 61 | |
| 111 | - private function invalidate_cache_for_updated_classes( string $context, array $changes ) { | |
| 112 | - if ( isset( $changes['order'] ) && $changes['order'] ) { | |
| 113 | - $this->invalidate_all_cache( $context ); | |
| 62 | + private function invalidate_cache( ?string $context = null ) { | |
| 63 | + $cache_validity = new Cache_Validity(); | |
| 114 | 64 | |
| 115 | - return; | |
| 116 | - } | |
| 117 | - | |
| 118 | - $affected = array_unique( array_merge( | |
| 119 | - $changes['added'] ?? [], | |
| 120 | - $changes['deleted'] ?? [], | |
| 121 | - $changes['modified'] ?? [] | |
| 122 | - ) ); | |
| 123 | - | |
| 124 | - if ( empty( $affected ) ) { | |
| 125 | - return; | |
| 126 | - } | |
| 127 | - | |
| 128 | - $document_ids = []; | |
| 129 | - $is_preview = Global_Classes_Repository::CONTEXT_PREVIEW === $context; | |
| 130 | - | |
| 131 | - foreach ( $affected as $class_id ) { | |
| 132 | - foreach ( $this->relations->set_preview( $is_preview )->get_posts_by_style( $class_id ) as $doc_id ) { | |
| 133 | - $document_ids[ $doc_id ] = true; | |
| 134 | - } | |
| 135 | - } | |
| 136 | - | |
| 137 | - if ( empty( $document_ids ) ) { | |
| 138 | - return; | |
| 139 | - } | |
| 140 | - | |
| 141 | - foreach ( array_keys( $document_ids ) as $post_id ) { | |
| 142 | - $this->invalidate_document_cache( $post_id, $context ); | |
| 143 | - } | |
| 144 | - } | |
| 145 | - | |
| 146 | - private function invalidate_document_cache( int $post_id, ?string $context = null ) { | |
| 147 | - if ( empty( $context ) ) { | |
| 148 | - do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY, $post_id ] ); | |
| 149 | - } else { | |
| 150 | - do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY, $post_id, $context ] ); | |
| 151 | - } | |
| 152 | - } | |
| 153 | - | |
| 154 | - private function invalidate_all_cache( ?string $context = null ) { | |
| 155 | 65 | if ( empty( $context ) || Global_Classes_Repository::CONTEXT_FRONTEND === $context ) { |
| 156 | - do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY ] ); | |
| 66 | + $cache_validity->invalidate( [ self::STYLES_KEY ] ); | |
| 157 | 67 | |
| 158 | 68 | return; |
| 159 | 69 | } |
| 160 | 70 | |
| 161 | - do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY, $context ] ); | |
| 71 | + $cache_validity->invalidate( [ self::STYLES_KEY, $context ] ); | |
| 162 | 72 | } |
| 163 | 73 | |
| 164 | 74 | private function transform_classes_names( $ids ) { |
| 75 | + $context = is_preview() ? Global_Classes_Repository::CONTEXT_PREVIEW : Global_Classes_Repository::CONTEXT_FRONTEND; | |
| 165 | 76 | |
| 166 | - $labels = Global_Classes_Repository::make() | |
| 167 | - ->set_preview( $this->is_preview() ) | |
| 168 | - ->all_labels(); | |
| 77 | + $classes = Global_Classes_Repository::make() | |
| 78 | + ->context( $context ) | |
| 79 | + ->all() | |
| 80 | + ->get_items(); | |
| 169 | 81 | |
| 170 | 82 | return array_map( |
| 171 | - static function( $id ) use ( $labels ) { | |
| 172 | - return $labels[ $id ] ?? $id; | |
| 83 | + function( $id ) use ( $classes ) { | |
| 84 | + $class = $classes->get( $id ); | |
| 85 | + | |
| 86 | + return $class ? $class['label'] : $id; | |
| 173 | 87 | }, |
| 174 | 88 | $ids |
| 175 | 89 | ); |
| 176 | - } | |
| 177 | - | |
| 178 | - private function get_context(): string { | |
| 179 | - return $this->is_preview() | |
| 180 | - ? Global_Classes_Repository::CONTEXT_PREVIEW | |
| 181 | - : Global_Classes_Repository::CONTEXT_FRONTEND; | |
| 182 | - } | |
| 183 | - | |
| 184 | - private function is_preview(): bool { | |
| 185 | - return Plugin::$instance->preview->is_editor_or_preview(); | |
| 186 | 90 | } |
| 187 | 91 | } |