PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.6.7
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.6.7
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Builder / Managers / Cache.php

Cache.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.6.7, at includes/Builder/Managers/Cache.php

114 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Builder\Managers;
4
5 use Templately\Builder\Source;
6 use Templately\Builder\Types\ThemeTemplate;
7 use WP_Query;
8
9 class Cache {
10 const OPTION_NAME = 'templately_builder_conditions';
11
12 protected $conditions = [];
13
14 public function __construct() {
15 $this->refresh();
16 }
17
18 public function refresh(): Cache {
19 $this->conditions = get_option( self::OPTION_NAME, [] );
20
21 return $this;
22 }
23
24 /**
25 * @param $document
26 * @param array $conditions
27 *
28 * @return $this
29 */
30 public function update( $document, array $conditions ): Cache {
31 return $this->remove( $document->get_main_id() )->add( $document, $conditions );
32 }
33
34 /**
35 * @param ThemeTemplate $document
36 * @param array $conditions
37 *
38 * @return $this
39 */
40 public function add( ThemeTemplate $document, array $conditions ): Cache {
41 $location = $document->get_location();
42 if ( $location ) {
43 if ( ! isset( $this->conditions[ $location ] ) ) {
44 $this->conditions[ $location ] = [];
45 }
46 $this->conditions[ $location ][ $document->get_main_id() ] = $conditions;
47 }
48
49 return $this;
50 }
51
52 /**
53 * @param int $post_id
54 *
55 * @return $this
56 */
57 public function remove( int $post_id ): Cache {
58 $post_id = absint( $post_id );
59
60 foreach ( $this->conditions as $location => $templates ) {
61 foreach ( $templates as $id => $template ) {
62 if ( $post_id === $id ) {
63 unset( $this->conditions[ $location ][ $id ] );
64 }
65 }
66 }
67
68 return $this;
69 }
70
71 public function save(): bool {
72 return update_option( self::OPTION_NAME, $this->conditions );
73 }
74
75 public function clear(): Cache {
76 $this->conditions = [];
77
78 return $this;
79 }
80
81 public function get_by_location( $location ) {
82 if ( isset( $this->conditions[ $location ] ) ) {
83 return $this->conditions[ $location ];
84 }
85
86 return [];
87 }
88
89 public function regenerate(): Cache {
90 $this->clear();
91
92 $query_args = [
93 'posts_per_page' => - 1,
94 'post_type' => Source::CPT,
95 'fields' => 'ids',
96 'meta_key' => '_templately_conditions',
97 ];
98
99 $query = new WP_Query( $query_args );
100
101 foreach ( $query->posts as $post_id ) {
102 $document = templately()->theme_builder->get_template( $post_id );
103
104 if ( $document ) {
105 $conditions = $document->get_meta( '_templately_conditions' );
106 $this->add( $document, $conditions );
107 }
108 }
109
110 $this->save();
111
112 return $this;
113 }
114 }