PageRenderTime 24ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/elementor/core/app/modules/kit-library/data/repository.php

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