PluginProbe
Elementor Website Builder – more than just a page builder / 3.10.1
Elementor Website Builder – more than just a page builder v3.10.1
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 4.0.7 All 451 releases
elementor / app / modules / kit-library / data / repository.php

repository.php in Elementor Website Builder – more than just a page builder 3.10.1, at app/modules/kit-library/data/repository.php

320 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\App\Modules\KitLibrary\Data;
3
4 use Elementor\Core\Utils\Collection;
5 use Elementor\Data\V2\Base\Exceptions\Error_404;
6 use Elementor\Data\V2\Base\Exceptions\WP_Error_Exception;
7 use Elementor\Modules\Library\User_Favorites;
8 use Elementor\App\Modules\KitLibrary\Connect\Kit_Library;
9 use Elementor\Plugin;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 class Repository {
16 /**
17 * There is no label for subscription plan with access_level=0 + it should not
18 * be translated.
19 */
20 const SUBSCRIPTION_PLAN_FREE_TAG = 'Free';
21
22 const TAXONOMIES_KEYS = [ 'tags', 'categories', 'main_category', 'third_category', 'features', 'types' ];
23
24 const KITS_CACHE_KEY = 'elementor_remote_kits';
25 const KITS_TAXONOMIES_CACHE_KEY = 'elementor_remote_kits_taxonomies';
26
27 const KITS_CACHE_TTL_HOURS = 12;
28 const KITS_TAXONOMIES_CACHE_TTL_HOURS = 12;
29
30 /**
31 * @var Kit_Library
32 */
33 protected $api;
34
35 /**
36 * @var User_Favorites
37 */
38 protected $user_favorites;
39
40 /**
41 * @var Collection
42 */
43 protected $subscription_plans;
44
45 /**
46 * Get all kits.
47 *
48 * @param false $force_api_request
49 *
50 * @return Collection
51 */
52 public function get_all( $force_api_request = false ) {
53 return $this->get_kits_data( $force_api_request )
54 ->map( function ( $kit ) {
55 return $this->transform_kit_api_response( $kit );
56 } );
57 }
58
59 /**
60 * Get specific kit.
61 *
62 * @param $id
63 * @param array $options
64 *
65 * @return array|null
66 */
67 public function find( $id, $options = [] ) {
68 $options = wp_parse_args( $options, [
69 'manifest_included' => true,
70 ] );
71
72 $item = $this->get_kits_data()
73 ->find( function ( $kit ) use ( $id ) {
74 return $kit->_id === $id;
75 } );
76
77 if ( ! $item ) {
78 return null;
79 }
80
81 $manifest = null;
82
83 if ( $options['manifest_included'] ) {
84 $manifest = $this->api->get_manifest( $id );
85
86 if ( is_wp_error( $manifest ) ) {
87 throw new WP_Error_Exception( $manifest );
88 }
89 }
90
91 return $this->transform_kit_api_response( $item, $manifest );
92 }
93
94 /**
95 * @param false $force_api_request
96 *
97 * @return Collection
98 */
99 public function get_taxonomies( $force_api_request = false ) {
100 return $this->get_taxonomies_data( $force_api_request )
101 ->only( static::TAXONOMIES_KEYS )
102 ->reduce( function ( Collection $carry, $taxonomies, $type ) {
103 return $carry->merge( array_map( function ( $taxonomy ) use ( $type ) {
104 return [
105 'text' => $taxonomy->name,
106 'type' => $type,
107 ];
108 }, $taxonomies ) );
109 }, new Collection( [] ) )
110 ->merge(
111 $this->subscription_plans->map( function ( $label ) {
112 return [
113 'text' => $label ? $label : self::SUBSCRIPTION_PLAN_FREE_TAG,
114 'type' => 'subscription_plans',
115 ];
116 } )
117 )
118 ->unique( [ 'text', 'type' ] );
119 }
120
121 /**
122 * @param $id
123 *
124 * @return array
125 */
126 public function get_download_link( $id ) {
127 $response = $this->api->download_link( $id );
128
129 if ( is_wp_error( $response ) ) {
130 throw new WP_Error_Exception( $response );
131 }
132
133 return [ 'download_link' => $response->download_link ];
134 }
135
136 /**
137 * @param $id
138 *
139 * @return array
140 * @throws \Exception
141 */
142 public function add_to_favorites( $id ) {
143 $kit = $this->find( $id, [ 'manifest_included' => false ] );
144
145 if ( ! $kit ) {
146 throw new Error_404( esc_html__( 'Kit not found', 'elementor' ), 'kit_not_found' );
147 }
148
149 $this->user_favorites->add( 'elementor', 'kits', $kit['id'] );
150
151 $kit['is_favorite'] = true;
152
153 return $kit;
154 }
155
156 /**
157 * @param $id
158 *
159 * @return array
160 * @throws \Exception
161 */
162 public function remove_from_favorites( $id ) {
163 $kit = $this->find( $id, [ 'manifest_included' => false ] );
164
165 if ( ! $kit ) {
166 throw new Error_404( esc_html__( 'Kit not found', 'elementor' ), 'kit_not_found' );
167 }
168
169 $this->user_favorites->remove( 'elementor', 'kits', $kit['id'] );
170
171 $kit['is_favorite'] = false;
172
173 return $kit;
174 }
175
176 /**
177 * @param bool $force_api_request
178 *
179 * @return Collection
180 */
181 private function get_kits_data( $force_api_request = false ) {
182 $data = get_transient( static::KITS_CACHE_KEY );
183
184 $experiments_manager = Plugin::$instance->experiments;
185 $kits_editor_layout_type = $experiments_manager->is_feature_active( 'container' ) ? 'container_flexbox' : '';
186
187 if ( ! $data || $force_api_request ) {
188 $args = [
189 'body' => [
190 'editor_layout_type' => $kits_editor_layout_type,
191 ],
192 ];
193
194 $data = $this->api->get_all( $args );
195
196 if ( is_wp_error( $data ) ) {
197 throw new WP_Error_Exception( $data );
198 }
199
200 set_transient( static::KITS_CACHE_KEY, $data, static::KITS_CACHE_TTL_HOURS * HOUR_IN_SECONDS );
201 }
202
203 return new Collection( $data );
204 }
205
206 /**
207 * @param bool $force_api_request
208 *
209 * @return Collection
210 */
211 private function get_taxonomies_data( $force_api_request = false ) {
212 $data = get_transient( static::KITS_TAXONOMIES_CACHE_KEY );
213
214 if ( ! $data || $force_api_request ) {
215 $data = $this->api->get_taxonomies();
216
217 if ( is_wp_error( $data ) ) {
218 throw new WP_Error_Exception( $data );
219 }
220
221 set_transient( static::KITS_TAXONOMIES_CACHE_KEY, $data, static::KITS_TAXONOMIES_CACHE_TTL_HOURS * HOUR_IN_SECONDS );
222 }
223
224 return new Collection( (array) $data );
225 }
226
227 /**
228 * @param $kit
229 * @param null $manifest
230 *
231 * @return array
232 */
233 private function transform_kit_api_response( $kit, $manifest = null ) {
234 $subscription_plan_tag = $this->subscription_plans->get( $kit->access_level );
235
236 $taxonomies = ( new Collection( ( (array) $kit )['taxonomies'] ) )
237 ->filter( function ( $taxonomy ) {
238 return in_array( $taxonomy->type, self::TAXONOMIES_KEYS );
239 } )
240 ->flatten()
241 ->pluck( 'name' )
242 ->push( $subscription_plan_tag ? $subscription_plan_tag : self::SUBSCRIPTION_PLAN_FREE_TAG );
243
244 return array_merge(
245 [
246 'id' => $kit->_id,
247 'title' => $kit->title,
248 'thumbnail_url' => $kit->thumbnail,
249 'access_level' => $kit->access_level,
250 'keywords' => $kit->keywords,
251 'taxonomies' => $taxonomies->values(),
252 'is_favorite' => $this->user_favorites->exists( 'elementor', 'kits', $kit->_id ),
253 // TODO: Remove all the isset when the API stable.
254 'trend_index' => isset( $kit->trend_index ) ? $kit->trend_index : 0,
255 'featured_index' => isset( $kit->featured_index ) ? $kit->featured_index : 0,
256 'popularity_index' => isset( $kit->popularity_index ) ? $kit->popularity_index : 0,
257 'created_at' => isset( $kit->created_at ) ? $kit->created_at : null,
258 'updated_at' => isset( $kit->updated_at ) ? $kit->updated_at : null,
259 //
260 ],
261 $manifest ? $this->transform_manifest_api_response( $manifest ) : []
262 );
263 }
264
265 /**
266 * @param $manifest
267 *
268 * @return array
269 */
270 private function transform_manifest_api_response( $manifest ) {
271 $manifest_content = ( new Collection( (array) $manifest->content ) )
272 ->reduce( function ( $carry, $content, $type ) {
273 $mapped_documents = array_map( function ( $document ) use ( $type ) {
274 // TODO: Fix it!
275 // Hack to override a bug when a document with type of 'wp-page' is declared as 'wp-post'.
276 if ( 'page' === $type ) {
277 $document->doc_type = 'wp-page';
278 }
279
280 return $document;
281 }, (array) $content );
282
283 return $carry + $mapped_documents;
284 }, [] );
285
286 $content = ( new Collection( (array) $manifest->templates ) )
287 ->union( $manifest_content )
288 ->map( function ( $manifest_item, $key ) {
289 return [
290 'id' => isset( $manifest_item->id ) ? $manifest_item->id : $key,
291 'title' => $manifest_item->title,
292 'doc_type' => $manifest_item->doc_type,
293 'thumbnail_url' => $manifest_item->thumbnail,
294 'preview_url' => isset( $manifest_item->url ) ? $manifest_item->url : null,
295 ];
296 } );
297
298 return [
299 'description' => $manifest->description,
300 'preview_url' => isset( $manifest->site ) ? $manifest->site : '',
301 'documents' => $content->values(),
302 ];
303 }
304
305 /**
306 * @param Kit_Library $kit_library
307 * @param User_Favorites $user_favorites
308 * @param Collection $subscription_plans
309 */
310 public function __construct( Kit_Library $kit_library, User_Favorites $user_favorites, Collection $subscription_plans ) {
311 $this->api = $kit_library;
312 $this->user_favorites = $user_favorites;
313 $this->subscription_plans = $subscription_plans;
314 }
315
316 public static function clear_cache() {
317 delete_transient( static::KITS_CACHE_KEY );
318 }
319 }
320