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

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