| @@ -1,14 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\App\Modules\KitLibrary\Data; |
| 3 | 3 | |
| 4 | -use Elementor\Core\Common\Modules\Connect\Module as ConnectModule; | |
| 5 | 4 | use Elementor\Core\Utils\Collection; |
| 6 | 5 | use Elementor\Data\V2\Base\Exceptions\Error_404; |
| 7 | 6 | use Elementor\Data\V2\Base\Exceptions\WP_Error_Exception; |
| 8 | 7 | use Elementor\Modules\Library\User_Favorites; |
| 9 | 8 | use Elementor\App\Modules\KitLibrary\Connect\Kit_Library; |
| 10 | -use Elementor\Plugin; | |
| 11 | 9 | |
| 12 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | 11 | exit; // Exit if accessed directly. |
| 14 | 12 | } |
| @@ -19,9 +17,9 @@ | ||
| 19 | 17 | * be translated. |
| 20 | 18 | */ |
| 21 | 19 | const SUBSCRIPTION_PLAN_FREE_TAG = 'Free'; |
| 22 | 20 | |
| 23 | - const TAXONOMIES_KEYS = [ 'tags', 'categories', 'main_category', 'third_category', 'features', 'types' ]; | |
| 21 | + const TAXONOMIES_KEYS = [ 'tags', 'categories', 'features', 'types' ]; | |
| 24 | 22 | |
| 25 | 23 | const KITS_CACHE_KEY = 'elementor_remote_kits'; |
| 26 | 24 | const KITS_TAXONOMIES_CACHE_KEY = 'elementor_remote_kits_taxonomies'; |
| 27 | 25 | |
| @@ -63,10 +61,8 @@ | ||
| 63 | 61 | * @param $id |
| 64 | 62 | * @param array $options |
| 65 | 63 | * |
| 66 | 64 | * @return array|null |
| 67 | - * | |
| 68 | - * @throws WP_Error_Exception If kit is not found. | |
| 69 | 65 | */ |
| 70 | 66 | public function find( $id, $options = [] ) { |
| 71 | 67 | $options = wp_parse_args( $options, [ |
| 72 | 68 | 'manifest_included' => true, |
| @@ -86,9 +82,9 @@ | ||
| 86 | 82 | if ( $options['manifest_included'] ) { |
| 87 | 83 | $manifest = $this->api->get_manifest( $id ); |
| 88 | 84 | |
| 89 | 85 | if ( is_wp_error( $manifest ) ) { |
| 90 | - throw new WP_Error_Exception( esc_html( $manifest ) ); | |
| 86 | + throw new WP_Error_Exception( $manifest ); | |
| 91 | 87 | } |
| 92 | 88 | } |
| 93 | 89 | |
| 94 | 90 | return $this->transform_kit_api_response( $item, $manifest ); |
| @@ -124,16 +120,14 @@ | ||
| 124 | 120 | /** |
| 125 | 121 | * @param $id |
| 126 | 122 | * |
| 127 | 123 | * @return array |
| 128 | - * | |
| 129 | - * @throws WP_Error_Exception If download link retrieval fails or API errors occur. | |
| 130 | 124 | */ |
| 131 | 125 | public function get_download_link( $id ) { |
| 132 | 126 | $response = $this->api->download_link( $id ); |
| 133 | 127 | |
| 134 | 128 | if ( is_wp_error( $response ) ) { |
| 135 | - throw new WP_Error_Exception( esc_html( $response ) ); | |
| 129 | + throw new WP_Error_Exception( $response ); | |
| 136 | 130 | } |
| 137 | 131 | |
| 138 | 132 | return [ 'download_link' => $response->download_link ]; |
| 139 | 133 | } |
| @@ -141,10 +135,9 @@ | ||
| 141 | 135 | /** |
| 142 | 136 | * @param $id |
| 143 | 137 | * |
| 144 | 138 | * @return array |
| 145 | - * | |
| 146 | - * @throws Error_404 If kit is not found. | |
| 139 | + * @throws \Exception | |
| 147 | 140 | */ |
| 148 | 141 | public function add_to_favorites( $id ) { |
| 149 | 142 | $kit = $this->find( $id, [ 'manifest_included' => false ] ); |
| 150 | 143 | |
| @@ -162,10 +155,9 @@ | ||
| 162 | 155 | /** |
| 163 | 156 | * @param $id |
| 164 | 157 | * |
| 165 | 158 | * @return array |
| 166 | - * | |
| 167 | - * @throws Error_404 If kit is not found. | |
| 159 | + * @throws \Exception | |
| 168 | 160 | */ |
| 169 | 161 | public function remove_from_favorites( $id ) { |
| 170 | 162 | $kit = $this->find( $id, [ 'manifest_included' => false ] ); |
| 171 | 163 | |
| @@ -183,37 +175,17 @@ | ||
| 183 | 175 | /** |
| 184 | 176 | * @param bool $force_api_request |
| 185 | 177 | * |
| 186 | 178 | * @return Collection |
| 187 | - * | |
| 188 | - * @throws WP_Error_Exception If kits data retrieval fails. | |
| 189 | 179 | */ |
| 190 | 180 | private function get_kits_data( $force_api_request = false ) { |
| 191 | 181 | $data = get_transient( static::KITS_CACHE_KEY ); |
| 192 | 182 | |
| 193 | - $experiments_manager = Plugin::$instance->experiments; | |
| 194 | - $kits_editor_layout_type = $experiments_manager->is_feature_active( 'container' ) ? 'container_flexbox' : ''; | |
| 195 | - | |
| 196 | 183 | if ( ! $data || $force_api_request ) { |
| 197 | - $args = [ | |
| 198 | - 'body' => [ | |
| 199 | - 'editor_layout_type' => $kits_editor_layout_type, | |
| 200 | - ], | |
| 201 | - ]; | |
| 184 | + $data = $this->api->get_all(); | |
| 202 | 185 | |
| 203 | - /** | |
| 204 | - * Filters arguments for the request to the Kits API. | |
| 205 | - * | |
| 206 | - * @since 3.11.0 | |
| 207 | - * | |
| 208 | - * @param array[] $args Array of http arguments. | |
| 209 | - */ | |
| 210 | - $args = apply_filters( 'elementor/kit-library/get-kits-data/args', $args ); | |
| 211 | - | |
| 212 | - $data = $this->api->get_all( $args ); | |
| 213 | - | |
| 214 | 186 | if ( is_wp_error( $data ) ) { |
| 215 | - throw new WP_Error_Exception( esc_html( $data ) ); | |
| 187 | + throw new WP_Error_Exception( $data ); | |
| 216 | 188 | } |
| 217 | 189 | |
| 218 | 190 | set_transient( static::KITS_CACHE_KEY, $data, static::KITS_CACHE_TTL_HOURS * HOUR_IN_SECONDS ); |
| 219 | 191 | } |
| @@ -224,10 +196,8 @@ | ||
| 224 | 196 | /** |
| 225 | 197 | * @param bool $force_api_request |
| 226 | 198 | * |
| 227 | 199 | * @return Collection |
| 228 | - * | |
| 229 | - * @throws WP_Error_Exception If taxonomies data retrieval fails. | |
| 230 | 200 | */ |
| 231 | 201 | private function get_taxonomies_data( $force_api_request = false ) { |
| 232 | 202 | $data = get_transient( static::KITS_TAXONOMIES_CACHE_KEY ); |
| 233 | 203 | |
| @@ -234,9 +204,9 @@ | ||
| 234 | 204 | if ( ! $data || $force_api_request ) { |
| 235 | 205 | $data = $this->api->get_taxonomies(); |
| 236 | 206 | |
| 237 | 207 | if ( is_wp_error( $data ) ) { |
| 238 | - throw new WP_Error_Exception( esc_html( $data ) ); | |
| 208 | + throw new WP_Error_Exception( $data ); | |
| 239 | 209 | } |
| 240 | 210 | |
| 241 | 211 | set_transient( static::KITS_TAXONOMIES_CACHE_KEY, $data, static::KITS_TAXONOMIES_CACHE_TTL_HOURS * HOUR_IN_SECONDS ); |
| 242 | 212 | } |
| @@ -250,23 +220,12 @@ | ||
| 250 | 220 | * |
| 251 | 221 | * @return array |
| 252 | 222 | */ |
| 253 | 223 | private function transform_kit_api_response( $kit, $manifest = null ) { |
| 254 | - // BC: Support legacy APIs that don't have access tiers. | |
| 255 | - if ( isset( $kit->access_tier ) ) { | |
| 256 | - $access_tier = $kit->access_tier; | |
| 257 | - } else { | |
| 258 | - $access_tier = 0 === $kit->access_level | |
| 259 | - ? ConnectModule::ACCESS_TIER_FREE | |
| 260 | - : ConnectModule::ACCESS_TIER_ESSENTIAL; | |
| 261 | - } | |
| 224 | + $subscription_plan_tag = $this->subscription_plans->get( $kit->access_level ); | |
| 262 | 225 | |
| 263 | - $subscription_plan_tag = $this->subscription_plans->get( $access_tier ); | |
| 264 | - | |
| 265 | - $taxonomies = ( new Collection( ( (array) $kit )['taxonomies'] ) ) | |
| 266 | - ->filter( function ( $taxonomy ) { | |
| 267 | - return in_array( $taxonomy->type, self::TAXONOMIES_KEYS ); | |
| 268 | - } ) | |
| 226 | + $taxonomies = ( new Collection( (array) $kit ) ) | |
| 227 | + ->only( static::TAXONOMIES_KEYS ) | |
| 269 | 228 | ->flatten() |
| 270 | 229 | ->pluck( 'name' ) |
| 271 | 230 | ->push( $subscription_plan_tag ? $subscription_plan_tag : self::SUBSCRIPTION_PLAN_FREE_TAG ); |
| 272 | 231 | |
| @@ -275,9 +234,8 @@ | ||
| 275 | 234 | 'id' => $kit->_id, |
| 276 | 235 | 'title' => $kit->title, |
| 277 | 236 | 'thumbnail_url' => $kit->thumbnail, |
| 278 | 237 | 'access_level' => $kit->access_level, |
| 279 | - 'access_tier' => $access_tier, | |
| 280 | 238 | 'keywords' => $kit->keywords, |
| 281 | 239 | 'taxonomies' => $taxonomies->values(), |
| 282 | 240 | 'is_favorite' => $this->user_favorites->exists( 'elementor', 'kits', $kit->_id ), |
| 283 | 241 | // TODO: Remove all the isset when the API stable. |
| @@ -285,8 +243,9 @@ | ||
| 285 | 243 | 'featured_index' => isset( $kit->featured_index ) ? $kit->featured_index : 0, |
| 286 | 244 | 'popularity_index' => isset( $kit->popularity_index ) ? $kit->popularity_index : 0, |
| 287 | 245 | 'created_at' => isset( $kit->created_at ) ? $kit->created_at : null, |
| 288 | 246 | 'updated_at' => isset( $kit->updated_at ) ? $kit->updated_at : null, |
| 247 | + // | |
| 289 | 248 | ], |
| 290 | 249 | $manifest ? $this->transform_manifest_api_response( $manifest ) : [] |
| 291 | 250 | ); |
| 292 | 251 | } |
| @@ -339,10 +298,6 @@ | ||
| 339 | 298 | public function __construct( Kit_Library $kit_library, User_Favorites $user_favorites, Collection $subscription_plans ) { |
| 340 | 299 | $this->api = $kit_library; |
| 341 | 300 | $this->user_favorites = $user_favorites; |
| 342 | 301 | $this->subscription_plans = $subscription_plans; |
| 343 | - } | |
| 344 | - | |
| 345 | - public static function clear_cache() { | |
| 346 | - delete_transient( static::KITS_CACHE_KEY ); | |
| 347 | 302 | } |
| 348 | 303 | } |