| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\GlobalClasses; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Modules\AtomicWidgets\Styles\Atomic_Styles_Manager; |
| 7 |
|
| 8 |
class Atomic_Global_Styles { |
| 9 |
const STYLES_KEY = 'global'; |
| 10 |
|
| 11 |
private Global_Classes_Relations $relations; |
| 12 |
|
| 13 |
public function __construct( Global_Classes_Relations $relations ) { |
| 14 |
$this->relations = $relations; |
| 15 |
} |
| 16 |
|
| 17 |
public function register_hooks() { |
| 18 |
add_action( |
| 19 |
'elementor/atomic-widgets/styles/register', |
| 20 |
fn( Atomic_Styles_Manager $styles_manager, array $post_ids ) => $this->register_styles( $styles_manager, $post_ids ), |
| 21 |
20, |
| 22 |
2 |
| 23 |
); |
| 24 |
|
| 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 |
); |
| 31 |
|
| 32 |
add_action( |
| 33 |
'deleted_post', |
| 34 |
fn( $post_id ) => $this->on_kit_delete( $post_id ) |
| 35 |
); |
| 36 |
|
| 37 |
add_action( |
| 38 |
'elementor/core/files/clear_cache', |
| 39 |
fn() => $this->invalidate_all_cache(), |
| 40 |
); |
| 41 |
|
| 42 |
add_filter( |
| 43 |
'elementor/atomic-widgets/settings/transformers/classes', |
| 44 |
fn( $value ) => $this->transform_classes_names( $value ) |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
private function register_styles( Atomic_Styles_Manager $styles_manager, array $post_ids ) { |
| 49 |
$context = $this->get_context(); |
| 50 |
|
| 51 |
foreach ( $post_ids as $post_id ) { |
| 52 |
$get_styles = fn() => $this->get_document_global_styles( $post_id, $context ); |
| 53 |
|
| 54 |
$styles_manager->register( |
| 55 |
[ self::STYLES_KEY, $post_id, $context ], |
| 56 |
$get_styles |
| 57 |
); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 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 ) { |
| 102 |
if ( ! Plugin::$instance->kits_manager->is_kit( $post_id ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$this->invalidate_all_cache(); |
| 107 |
} |
| 108 |
|
| 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 ) { |
| 153 |
if ( empty( $context ) || Global_Classes_Repository::CONTEXT_FRONTEND === $context ) { |
| 154 |
do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY ] ); |
| 155 |
|
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY, $context ] ); |
| 160 |
} |
| 161 |
|
| 162 |
private function transform_classes_names( $ids ) { |
| 163 |
$context = $this->get_context(); |
| 164 |
|
| 165 |
$repository = Global_Classes_Repository::make(); |
| 166 |
|
| 167 |
if ( Global_Classes_Repository::CONTEXT_PREVIEW === $context ) { |
| 168 |
$repository->set_preview( true ); |
| 169 |
} |
| 170 |
|
| 171 |
$labels = $repository->all_labels(); |
| 172 |
|
| 173 |
return array_map( |
| 174 |
static function( $id ) use ( $labels ) { |
| 175 |
return $labels[ $id ] ?? $id; |
| 176 |
}, |
| 177 |
$ids |
| 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; |
| 185 |
} |
| 186 |
} |
| 187 |
|