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