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

329 lines 8.3 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 /**
195 * Filters arguments for the request to the Kits API.
196 *
197 * @since 3.11.0
198 *
199 * @param array[] $args Array of http arguments.
200 */
201 $args = apply_filters( 'elementor/kit-library/get-kits-data/args', $args );
202
203 $data = $this->api->get_all( $args );
204
205 if ( is_wp_error( $data ) ) {
206 throw new WP_Error_Exception( $data );
207 }
208
209 set_transient( static::KITS_CACHE_KEY, $data, static::KITS_CACHE_TTL_HOURS * HOUR_IN_SECONDS );
210 }
211
212 return new Collection( $data );
213 }
214
215 /**
216 * @param bool $force_api_request
217 *
218 * @return Collection
219 */
220 private function get_taxonomies_data( $force_api_request = false ) {
221 $data = get_transient( static::KITS_TAXONOMIES_CACHE_KEY );
222
223 if ( ! $data || $force_api_request ) {
224 $data = $this->api->get_taxonomies();
225
226 if ( is_wp_error( $data ) ) {
227 throw new WP_Error_Exception( $data );
228 }
229
230 set_transient( static::KITS_TAXONOMIES_CACHE_KEY, $data, static::KITS_TAXONOMIES_CACHE_TTL_HOURS * HOUR_IN_SECONDS );
231 }
232
233 return new Collection( (array) $data );
234 }
235
236 /**
237 * @param $kit
238 * @param null $manifest
239 *
240 * @return array
241 */
242 private function transform_kit_api_response( $kit, $manifest = null ) {
243 $subscription_plan_tag = $this->subscription_plans->get( $kit->access_level );
244
245 $taxonomies = ( new Collection( ( (array) $kit )['taxonomies'] ) )
246 ->filter( function ( $taxonomy ) {
247 return in_array( $taxonomy->type, self::TAXONOMIES_KEYS );
248 } )
249 ->flatten()
250 ->pluck( 'name' )
251 ->push( $subscription_plan_tag ? $subscription_plan_tag : self::SUBSCRIPTION_PLAN_FREE_TAG );
252
253 return array_merge(
254 [
255 'id' => $kit->_id,
256 'title' => $kit->title,
257 'thumbnail_url' => $kit->thumbnail,
258 'access_level' => $kit->access_level,
259 'keywords' => $kit->keywords,
260 'taxonomies' => $taxonomies->values(),
261 'is_favorite' => $this->user_favorites->exists( 'elementor', 'kits', $kit->_id ),
262 // TODO: Remove all the isset when the API stable.
263 'trend_index' => isset( $kit->trend_index ) ? $kit->trend_index : 0,
264 'featured_index' => isset( $kit->featured_index ) ? $kit->featured_index : 0,
265 'popularity_index' => isset( $kit->popularity_index ) ? $kit->popularity_index : 0,
266 'created_at' => isset( $kit->created_at ) ? $kit->created_at : null,
267 'updated_at' => isset( $kit->updated_at ) ? $kit->updated_at : null,
268 //
269 ],
270 $manifest ? $this->transform_manifest_api_response( $manifest ) : []
271 );
272 }
273
274 /**
275 * @param $manifest
276 *
277 * @return array
278 */
279 private function transform_manifest_api_response( $manifest ) {
280 $manifest_content = ( new Collection( (array) $manifest->content ) )
281 ->reduce( function ( $carry, $content, $type ) {
282 $mapped_documents = array_map( function ( $document ) use ( $type ) {
283 // TODO: Fix it!
284 // Hack to override a bug when a document with type of 'wp-page' is declared as 'wp-post'.
285 if ( 'page' === $type ) {
286 $document->doc_type = 'wp-page';
287 }
288
289 return $document;
290 }, (array) $content );
291
292 return $carry + $mapped_documents;
293 }, [] );
294
295 $content = ( new Collection( (array) $manifest->templates ) )
296 ->union( $manifest_content )
297 ->map( function ( $manifest_item, $key ) {
298 return [
299 'id' => isset( $manifest_item->id ) ? $manifest_item->id : $key,
300 'title' => $manifest_item->title,
301 'doc_type' => $manifest_item->doc_type,
302 'thumbnail_url' => $manifest_item->thumbnail,
303 'preview_url' => isset( $manifest_item->url ) ? $manifest_item->url : null,
304 ];
305 } );
306
307 return [
308 'description' => $manifest->description,
309 'preview_url' => isset( $manifest->site ) ? $manifest->site : '',
310 'documents' => $content->values(),
311 ];
312 }
313
314 /**
315 * @param Kit_Library $kit_library
316 * @param User_Favorites $user_favorites
317 * @param Collection $subscription_plans
318 */
319 public function __construct( Kit_Library $kit_library, User_Favorites $user_favorites, Collection $subscription_plans ) {
320 $this->api = $kit_library;
321 $this->user_favorites = $user_favorites;
322 $this->subscription_plans = $subscription_plans;
323 }
324
325 public static function clear_cache() {
326 delete_transient( static::KITS_CACHE_KEY );
327 }
328 }
329