PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.4
Elementor Website Builder – more than just a page builder v3.32.4
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.32.4, at app/modules/kit-library/data/repository.php

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