PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / default-styles / default-styles-repository.php

default-styles-repository.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/default-styles/default-styles-repository.php

123 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\DefaultStyles;
4
5 use Elementor\Core\Kits\Concerns\Has_Kit_Dependency;
6 use Elementor\Core\Kits\Documents\Kit;
7 use Elementor\Modules\DefaultStyles\Utils\Default_Style_Data_Normalizer;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 class Default_Styles_Repository {
14 use Has_Kit_Dependency;
15
16 private ?array $cache = null;
17
18 public function __construct( ?Kit $kit = null ) {
19 if ( null !== $kit ) {
20 $this->set_kit( $kit );
21 }
22 }
23
24 public static function make( ?Kit $kit = null ): self {
25 return new self( $kit );
26 }
27
28 public function all( bool $force = false ): array {
29 if ( ! $force && null !== $this->cache ) {
30 return $this->cache;
31 }
32
33 $items = [];
34 $tag_post_ids = Default_Styles_Tag_Post_IDs::make( $this->get_kit() )->get_all();
35
36 foreach ( $tag_post_ids as $tag => $post_id ) {
37 $post = Default_Style_Post::from_post_id( $post_id );
38
39 if ( $post ) {
40 $items[ $tag ] = $post->to_array();
41 }
42 }
43
44 $this->cache = $items;
45
46 return $items;
47 }
48
49 public function get( string $tag ): ?array {
50 $post = Default_Style_Post::find_by_tag( $tag, $this->get_kit() );
51
52 return $post ? $post->to_array() : null;
53 }
54
55 public function each_item( callable $cb, bool $skip_migration = false ): void {
56 $tag_post_ids = Default_Styles_Tag_Post_IDs::make( $this->get_kit() )->get_all();
57
58 foreach ( $tag_post_ids as $post_id ) {
59 $post = Default_Style_Post::from_post_id( $post_id );
60
61 if ( $post ) {
62 $cb( $post->to_array( $skip_migration ) );
63 }
64 }
65 }
66
67 public function put( string $tag, array $data ): bool {
68 if ( ! self::is_allowed_tag( $tag ) ) {
69 return false;
70 }
71
72 $post = Default_Style_Post::find_by_tag( $tag, $this->get_kit() );
73 $normalized = Default_Style_Data_Normalizer::normalize_style_fields( $data );
74
75 if ( $post ) {
76 if ( false === $post->update_data( $normalized ) ) {
77 return false;
78 }
79
80 clean_post_cache( $post->get_post_id() );
81 } else {
82 $created = Default_Style_Post::create( $tag, $normalized, $this->get_kit() );
83
84 if ( ! $created ) {
85 return false;
86 }
87
88 clean_post_cache( $created->get_post_id() );
89 }
90
91 $this->cache = null;
92
93 do_action( 'elementor/default_styles/update', [ 'tag' => $tag ] );
94
95 return true;
96 }
97
98 public function delete( string $tag ): void {
99 $post = Default_Style_Post::find_by_tag( $tag, $this->get_kit() );
100
101 if ( ! $post ) {
102 return;
103 }
104
105 Default_Styles_Tag_Post_IDs::make( $this->get_kit() )->remove_tag( $tag );
106 $post->delete();
107
108 $this->cache = null;
109
110 do_action(
111 'elementor/default_styles/update',
112 [
113 'tag' => $tag,
114 'deleted' => true,
115 ]
116 );
117 }
118
119 public static function is_allowed_tag( string $tag ): bool {
120 return in_array( $tag, Default_Styles_Allowed_Tags::TAGS, true );
121 }
122 }
123