| 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( $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( $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( $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( $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 |
], |
| 281 |
$manifest ? $this->transform_manifest_api_response( $manifest ) : [] |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* @param $manifest |
| 287 |
* |
| 288 |
* @return array |
| 289 |
*/ |
| 290 |
private function transform_manifest_api_response( $manifest ) { |
| 291 |
$manifest_content = ( new Collection( (array) $manifest->content ) ) |
| 292 |
->reduce( function ( $carry, $content, $type ) { |
| 293 |
$mapped_documents = array_map( function ( $document ) use ( $type ) { |
| 294 |
// TODO: Fix it! |
| 295 |
// Hack to override a bug when a document with type of 'wp-page' is declared as 'wp-post'. |
| 296 |
if ( 'page' === $type ) { |
| 297 |
$document->doc_type = 'wp-page'; |
| 298 |
} |
| 299 |
|
| 300 |
return $document; |
| 301 |
}, (array) $content ); |
| 302 |
|
| 303 |
return $carry + $mapped_documents; |
| 304 |
}, [] ); |
| 305 |
|
| 306 |
$content = ( new Collection( (array) $manifest->templates ) ) |
| 307 |
->union( $manifest_content ) |
| 308 |
->map( function ( $manifest_item, $key ) { |
| 309 |
return [ |
| 310 |
'id' => isset( $manifest_item->id ) ? $manifest_item->id : $key, |
| 311 |
'title' => $manifest_item->title, |
| 312 |
'doc_type' => $manifest_item->doc_type, |
| 313 |
'thumbnail_url' => $manifest_item->thumbnail, |
| 314 |
'preview_url' => isset( $manifest_item->url ) ? $manifest_item->url : null, |
| 315 |
]; |
| 316 |
} ); |
| 317 |
|
| 318 |
return [ |
| 319 |
'description' => $manifest->description, |
| 320 |
'preview_url' => isset( $manifest->site ) ? $manifest->site : '', |
| 321 |
'documents' => $content->values(), |
| 322 |
]; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* @param Kit_Library $kit_library |
| 327 |
* @param User_Favorites $user_favorites |
| 328 |
* @param Collection $subscription_plans |
| 329 |
*/ |
| 330 |
public function __construct( Kit_Library $kit_library, User_Favorites $user_favorites, Collection $subscription_plans ) { |
| 331 |
$this->api = $kit_library; |
| 332 |
$this->user_favorites = $user_favorites; |
| 333 |
$this->subscription_plans = $subscription_plans; |
| 334 |
} |
| 335 |
|
| 336 |
public static function clear_cache() { |
| 337 |
delete_transient( static::KITS_CACHE_KEY ); |
| 338 |
} |
| 339 |
} |
| 340 |
|