PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/elementor/includes/template-library/sources/base.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 368 lines | 96 code | 41 blank | 231 comment | 14 complexity | 419098245432cf60a699efb1d407340b MD5 | raw file
  1. <?php
  2. namespace Elementor\TemplateLibrary;
  3. use Elementor\Controls_Stack;
  4. use Elementor\Plugin;
  5. use Elementor\Utils;
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit; // Exit if accessed directly.
  8. }
  9. /**
  10. * Elementor template library source base.
  11. *
  12. * Elementor template library source base handler class is responsible for
  13. * initializing all the methods controlling the source of Elementor templates.
  14. *
  15. * @since 1.0.0
  16. * @abstract
  17. */
  18. abstract class Source_Base {
  19. /**
  20. * User meta.
  21. *
  22. * Holds the current user meta data.
  23. *
  24. * @access private
  25. *
  26. * @var array
  27. */
  28. private $user_meta;
  29. /**
  30. * Get template ID.
  31. *
  32. * Retrieve the template ID.
  33. *
  34. * @since 1.0.0
  35. * @access public
  36. * @abstract
  37. */
  38. abstract public function get_id();
  39. /**
  40. * Get template title.
  41. *
  42. * Retrieve the template title.
  43. *
  44. * @since 1.0.0
  45. * @access public
  46. * @abstract
  47. */
  48. abstract public function get_title();
  49. /**
  50. * Register template data.
  51. *
  52. * Used to register custom template data like a post type, a taxonomy or any
  53. * other data.
  54. *
  55. * @since 1.0.0
  56. * @access public
  57. * @abstract
  58. */
  59. abstract public function register_data();
  60. /**
  61. * Get templates.
  62. *
  63. * Retrieve templates from the template library.
  64. *
  65. * @since 1.0.0
  66. * @access public
  67. * @abstract
  68. *
  69. * @param array $args Optional. Filter templates list based on a set of
  70. * arguments. Default is an empty array.
  71. */
  72. abstract public function get_items( $args = [] );
  73. /**
  74. * Get template.
  75. *
  76. * Retrieve a single template from the template library.
  77. *
  78. * @since 1.0.0
  79. * @access public
  80. * @abstract
  81. *
  82. * @param int $template_id The template ID.
  83. */
  84. abstract public function get_item( $template_id );
  85. /**
  86. * Get template data.
  87. *
  88. * Retrieve a single template data from the template library.
  89. *
  90. * @since 1.5.0
  91. * @access public
  92. * @abstract
  93. *
  94. * @param array $args Custom template arguments.
  95. */
  96. abstract public function get_data( array $args );
  97. /**
  98. * Delete template.
  99. *
  100. * Delete template from the database.
  101. *
  102. * @since 1.0.0
  103. * @access public
  104. * @abstract
  105. *
  106. * @param int $template_id The template ID.
  107. */
  108. abstract public function delete_template( $template_id );
  109. /**
  110. * Save template.
  111. *
  112. * Save new or update existing template on the database.
  113. *
  114. * @since 1.0.0
  115. * @access public
  116. * @abstract
  117. *
  118. * @param array $template_data The template data.
  119. */
  120. abstract public function save_item( $template_data );
  121. /**
  122. * Update template.
  123. *
  124. * Update template on the database.
  125. *
  126. * @since 1.0.0
  127. * @access public
  128. * @abstract
  129. *
  130. * @param array $new_data New template data.
  131. */
  132. abstract public function update_item( $new_data );
  133. /**
  134. * Export template.
  135. *
  136. * Export template to a file.
  137. *
  138. * @since 1.0.0
  139. * @access public
  140. * @abstract
  141. *
  142. * @param int $template_id The template ID.
  143. */
  144. abstract public function export_template( $template_id );
  145. /**
  146. * Template library source base constructor.
  147. *
  148. * Initializing the template library source base by registering custom
  149. * template data.
  150. *
  151. * @since 1.0.0
  152. * @access public
  153. */
  154. public function __construct() {
  155. $this->register_data();
  156. }
  157. /**
  158. * Mark template as favorite.
  159. *
  160. * Update user meta containing his favorite templates. For a given template
  161. * ID, add the template to the favorite templates or remove it from the
  162. * favorites, based on the `favorite` parameter.
  163. *
  164. * @since 1.9.0
  165. * @access public
  166. *
  167. * @param int $template_id The template ID.
  168. * @param bool $favorite Optional. Whether the template is marked as
  169. * favorite, or not. Default is true.
  170. *
  171. * @return int|bool User meta ID if the key didn't exist, true on successful
  172. * update, false on failure.
  173. */
  174. public function mark_as_favorite( $template_id, $favorite = true ) {
  175. $favorites_templates = $this->get_user_meta( 'favorites' );
  176. if ( ! $favorites_templates ) {
  177. $favorites_templates = [];
  178. }
  179. if ( $favorite ) {
  180. $favorites_templates[ $template_id ] = $favorite;
  181. } elseif ( isset( $favorites_templates[ $template_id ] ) ) {
  182. unset( $favorites_templates[ $template_id ] );
  183. }
  184. return $this->update_user_meta( 'favorites', $favorites_templates );
  185. }
  186. /**
  187. * Get current user meta.
  188. *
  189. * Retrieve Elementor meta data for the current user.
  190. *
  191. * @since 1.9.0
  192. * @access public
  193. *
  194. * @param string $item Optional. User meta key. Default is null.
  195. *
  196. * @return null|array An array of user meta data, or null otherwise.
  197. */
  198. public function get_user_meta( $item = null ) {
  199. if ( null === $this->user_meta ) {
  200. $this->user_meta = get_user_meta( get_current_user_id(), $this->get_user_meta_prefix(), true );
  201. }
  202. if ( ! $this->user_meta ) {
  203. $this->user_meta = [];
  204. }
  205. if ( $item ) {
  206. if ( isset( $this->user_meta[ $item ] ) ) {
  207. return $this->user_meta[ $item ];
  208. }
  209. return null;
  210. }
  211. return $this->user_meta;
  212. }
  213. /**
  214. * Update current user meta.
  215. *
  216. * Update user meta data based on meta key an value.
  217. *
  218. * @since 1.9.0
  219. * @access public
  220. *
  221. * @param string $key Optional. User meta key.
  222. * @param mixed $value Optional. User meta value.
  223. *
  224. * @return int|bool User meta ID if the key didn't exist, true on successful
  225. * update, false on failure.
  226. */
  227. public function update_user_meta( $key, $value ) {
  228. $meta = $this->get_user_meta();
  229. $meta[ $key ] = $value;
  230. $this->user_meta = $meta;
  231. return update_user_meta( get_current_user_id(), $this->get_user_meta_prefix(), $meta );
  232. }
  233. /**
  234. * Replace elements IDs.
  235. *
  236. * For any given Elementor content/data, replace the IDs with new randomly
  237. * generated IDs.
  238. *
  239. * @since 1.0.0
  240. * @access protected
  241. *
  242. * @param array $content Any type of Elementor data.
  243. *
  244. * @return mixed Iterated data.
  245. */
  246. protected function replace_elements_ids( $content ) {
  247. return Plugin::$instance->db->iterate_data( $content, function( $element ) {
  248. $element['id'] = Utils::generate_random_string();
  249. return $element;
  250. } );
  251. }
  252. /**
  253. * Get Elementor library user meta prefix.
  254. *
  255. * Retrieve user meta prefix used to save Elementor data.
  256. *
  257. * @since 1.9.0
  258. * @access protected
  259. *
  260. * @return string User meta prefix.
  261. */
  262. protected function get_user_meta_prefix() {
  263. return 'elementor_library_' . $this->get_id();
  264. }
  265. /**
  266. * Process content for export/import.
  267. *
  268. * Process the content and all the inner elements, and prepare all the
  269. * elements data for export/import.
  270. *
  271. * @since 1.5.0
  272. * @access protected
  273. *
  274. * @param array $content A set of elements.
  275. * @param string $method Accepts either `on_export` to export data or
  276. * `on_import` to import data.
  277. *
  278. * @return mixed Processed content data.
  279. */
  280. protected function process_export_import_content( $content, $method ) {
  281. return Plugin::$instance->db->iterate_data(
  282. $content, function( $element_data ) use ( $method ) {
  283. $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
  284. // If the widget/element isn't exist, like a plugin that creates a widget but deactivated
  285. if ( ! $element ) {
  286. return null;
  287. }
  288. return $this->process_element_export_import_content( $element, $method );
  289. }
  290. );
  291. }
  292. /**
  293. * Process single element content for export/import.
  294. *
  295. * Process any given element and prepare the element data for export/import.
  296. *
  297. * @since 1.5.0
  298. * @access protected
  299. *
  300. * @param Controls_Stack $element
  301. * @param string $method
  302. *
  303. * @return array Processed element data.
  304. */
  305. protected function process_element_export_import_content( Controls_Stack $element, $method ) {
  306. $element_data = $element->get_data();
  307. if ( method_exists( $element, $method ) ) {
  308. // TODO: Use the internal element data without parameters.
  309. $element_data = $element->{$method}( $element_data );
  310. }
  311. foreach ( $element->get_controls() as $control ) {
  312. $control_class = Plugin::$instance->controls_manager->get_control( $control['type'] );
  313. // If the control isn't exist, like a plugin that creates the control but deactivated.
  314. if ( ! $control_class ) {
  315. return $element_data;
  316. }
  317. if ( method_exists( $control_class, $method ) ) {
  318. $element_data['settings'][ $control['name'] ] = $control_class->{$method}( $element->get_settings( $control['name'] ), $control );
  319. }
  320. // On Export, check if the control has an argument 'export' => false.
  321. if ( 'on_export' === $method && isset( $control['export'] ) && false === $control['export'] ) {
  322. unset( $element_data['settings'][ $control['name'] ] );
  323. }
  324. }
  325. return $element_data;
  326. }
  327. }