| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Styles; |
| 4 |
|
| 5 |
use Elementor\Core\Base\Document; |
| 6 |
use Elementor\Modules\AtomicWidgets\Cache_Validity; |
| 7 |
use Elementor\Modules\AtomicWidgets\Utils; |
| 8 |
use Elementor\Modules\GlobalClasses\Utils\Atomic_Elements_Utils; |
| 9 |
use Elementor\Plugin; |
| 10 |
|
| 11 |
class Atomic_Widget_Styles { |
| 12 |
const STYLES_KEY = 'local'; |
| 13 |
|
| 14 |
public function register_hooks() { |
| 15 |
add_action( 'elementor/atomic-widgets/styles/register', function( Atomic_Styles_Manager $styles_manager, array $post_ids ) { |
| 16 |
$this->register_styles( $styles_manager, $post_ids ); |
| 17 |
}, 30, 2 ); |
| 18 |
|
| 19 |
add_action( 'elementor/document/after_save', fn( Document $document ) => $this->invalidate_cache( |
| 20 |
[ $document->get_main_post()->ID ] |
| 21 |
), 20, 2 ); |
| 22 |
|
| 23 |
add_action( |
| 24 |
'elementor/core/files/clear_cache', |
| 25 |
fn() => $this->invalidate_cache(), |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
private function register_styles( Atomic_Styles_Manager $styles_manager, array $post_ids ) { |
| 30 |
foreach ( $post_ids as $post_id ) { |
| 31 |
$get_styles = fn() => $this->parse_post_styles( $post_id ); |
| 32 |
|
| 33 |
$style_key = $this->get_style_key( $post_id ); |
| 34 |
|
| 35 |
$styles_manager->register( $style_key, $get_styles, [ self::STYLES_KEY, $post_id ] ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
private function parse_post_styles( $post_id ) { |
| 40 |
$document = Plugin::$instance->documents->get_doc_for_frontend( $post_id ); |
| 41 |
|
| 42 |
if ( ! $document ) { |
| 43 |
return []; |
| 44 |
} |
| 45 |
|
| 46 |
$elements_data = $document->get_elements_data(); |
| 47 |
|
| 48 |
if ( empty( $elements_data ) ) { |
| 49 |
return []; |
| 50 |
} |
| 51 |
|
| 52 |
$post_styles = []; |
| 53 |
|
| 54 |
Plugin::$instance->db->iterate_data( $elements_data, function( $element_data ) use ( &$post_styles ) { |
| 55 |
$post_styles = array_merge( $post_styles, $this->parse_element_style( $element_data ) ); |
| 56 |
}); |
| 57 |
|
| 58 |
return $post_styles; |
| 59 |
} |
| 60 |
|
| 61 |
private function parse_element_style( array $element_data ) { |
| 62 |
$element_type = Atomic_Elements_Utils::get_element_type( $element_data ); |
| 63 |
$element_instance = Atomic_Elements_Utils::get_element_instance( $element_type ); |
| 64 |
|
| 65 |
if ( ! Utils::is_atomic( $element_instance ) ) { |
| 66 |
return []; |
| 67 |
} |
| 68 |
|
| 69 |
return $element_data['styles'] ?? []; |
| 70 |
} |
| 71 |
|
| 72 |
private function invalidate_cache( ?array $post_ids = null ) { |
| 73 |
$cache_validity = new Cache_Validity(); |
| 74 |
|
| 75 |
if ( empty( $post_ids ) ) { |
| 76 |
$cache_validity->invalidate( [ self::STYLES_KEY ] ); |
| 77 |
|
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
foreach ( $post_ids as $post_id ) { |
| 82 |
$cache_validity->invalidate( [ self::STYLES_KEY, $post_id ] ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private function get_style_key( $post_id ) { |
| 87 |
return self::STYLES_KEY . '-' . $post_id; |
| 88 |
} |
| 89 |
} |
| 90 |
|