| 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 |
public function register_hooks() { |
| 12 |
add_action( |
| 13 |
'elementor/atomic-widgets/styles/register', |
| 14 |
fn( Atomic_Styles_Manager $styles_manager ) => $this->register_styles( $styles_manager ), |
| 15 |
20, |
| 16 |
2 |
| 17 |
); |
| 18 |
|
| 19 |
add_action( 'elementor/global_classes/update', fn( string $context ) => $this->invalidate_cache( $context ), 10, 1 ); |
| 20 |
|
| 21 |
add_action( |
| 22 |
'deleted_post', |
| 23 |
fn( $post_id ) => $this->on_post_delete( $post_id ) |
| 24 |
); |
| 25 |
|
| 26 |
add_action( |
| 27 |
'elementor/core/files/clear_cache', |
| 28 |
fn() => $this->invalidate_cache(), |
| 29 |
); |
| 30 |
|
| 31 |
add_filter('elementor/atomic-widgets/settings/transformers/classes', |
| 32 |
fn( $value ) => $this->transform_classes_names( $value ) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
private function register_styles( Atomic_Styles_Manager $styles_manager ) { |
| 37 |
$context = is_preview() ? Global_Classes_Repository::CONTEXT_PREVIEW : Global_Classes_Repository::CONTEXT_FRONTEND; |
| 38 |
|
| 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 |
}; |
| 45 |
|
| 46 |
$styles_manager->register( |
| 47 |
[ self::STYLES_KEY, $context ], |
| 48 |
$get_styles, |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
private function on_post_delete( $post_id ) { |
| 53 |
if ( ! Plugin::$instance->kits_manager->is_kit( $post_id ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$this->invalidate_cache(); |
| 58 |
} |
| 59 |
|
| 60 |
private function invalidate_cache( ?string $context = null ) { |
| 61 |
if ( empty( $context ) || Global_Classes_Repository::CONTEXT_FRONTEND === $context ) { |
| 62 |
do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY ] ); |
| 63 |
|
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY, $context ] ); |
| 68 |
} |
| 69 |
|
| 70 |
private function transform_classes_names( $ids ) { |
| 71 |
$context = is_preview() ? Global_Classes_Repository::CONTEXT_PREVIEW : Global_Classes_Repository::CONTEXT_FRONTEND; |
| 72 |
|
| 73 |
$classes = Global_Classes_Repository::make() |
| 74 |
->context( $context ) |
| 75 |
->all() |
| 76 |
->get_items(); |
| 77 |
|
| 78 |
return array_map( |
| 79 |
function( $id ) use ( $classes ) { |
| 80 |
$class = $classes->get( $id ); |
| 81 |
|
| 82 |
return $class ? $class['label'] : $id; |
| 83 |
}, |
| 84 |
$ids |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
|