PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/elementor/core/settings/base/manager.php

https://gitlab.com/ebrjose/comcebu
PHP | 339 lines | 104 code | 38 blank | 197 comment | 6 complexity | bc125451fe1682ab5e1885b329c42aba MD5 | raw file
  1. <?php
  2. namespace Elementor\Core\Settings\Base;
  3. use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
  4. use Elementor\Plugin;
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit; // Exit if accessed directly.
  7. }
  8. /**
  9. * Elementor settings base manager.
  10. *
  11. * Elementor settings base manager handler class is responsible for registering
  12. * and managing Elementor settings base managers.
  13. *
  14. * @since 1.6.0
  15. * @abstract
  16. */
  17. abstract class Manager {
  18. /**
  19. * Models cache.
  20. *
  21. * Holds all the models.
  22. *
  23. * @since 1.6.0
  24. * @access private
  25. *
  26. * @var Model[]
  27. */
  28. private $models_cache = [];
  29. /**
  30. * Settings base manager constructor.
  31. *
  32. * Initializing Elementor settings base manager.
  33. *
  34. * @since 1.6.0
  35. * @access public
  36. */
  37. public function __construct() {
  38. add_action( 'elementor/editor/init', [ $this, 'on_elementor_editor_init' ] );
  39. add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
  40. }
  41. /**
  42. * Register ajax actions.
  43. *
  44. * Add new actions to handle data after an ajax requests returned.
  45. *
  46. * Fired by `elementor/ajax/register_actions` action.
  47. *
  48. * @since 2.0.0
  49. * @access public
  50. *
  51. * @param Ajax $ajax_manager
  52. */
  53. public function register_ajax_actions( $ajax_manager ) {
  54. $name = $this->get_name();
  55. $ajax_manager->register_ajax_action( "save_{$name}_settings", [ $this, 'ajax_save_settings' ] );
  56. }
  57. /**
  58. * Get model for config.
  59. *
  60. * Retrieve the model for settings configuration.
  61. *
  62. * @since 1.6.0
  63. * @access public
  64. * @abstract
  65. *
  66. * @return Model The model object.
  67. */
  68. abstract public function get_model_for_config();
  69. /**
  70. * Get manager name.
  71. *
  72. * Retrieve settings manager name.
  73. *
  74. * @since 1.6.0
  75. * @access public
  76. * @abstract
  77. */
  78. abstract public function get_name();
  79. /**
  80. * Get model.
  81. *
  82. * Retrieve the model for any given model ID.
  83. *
  84. * @since 1.6.0
  85. * @access public
  86. *
  87. * @param int $id Optional. Model ID. Default is `0`.
  88. *
  89. * @return Model The model.
  90. */
  91. final public function get_model( $id = 0 ) {
  92. if ( ! isset( $this->models_cache[ $id ] ) ) {
  93. $this->create_model( $id );
  94. }
  95. return $this->models_cache[ $id ];
  96. }
  97. /**
  98. * Ajax request to save settings.
  99. *
  100. * Save settings using an ajax request.
  101. *
  102. * @since 1.6.0
  103. * @access public
  104. *
  105. * @param array $request Ajax request.
  106. *
  107. * @return array Ajax response data.
  108. */
  109. final public function ajax_save_settings( $request ) {
  110. $data = $request['data'];
  111. $id = 0;
  112. if ( ! empty( $request['id'] ) ) {
  113. $id = $request['id'];
  114. }
  115. $this->ajax_before_save_settings( $data, $id );
  116. $this->save_settings( $data, $id );
  117. $settings_name = $this->get_name();
  118. $success_response_data = [];
  119. /**
  120. * Settings success response data.
  121. *
  122. * Filters the success response data when saving settings using ajax.
  123. *
  124. * The dynamic portion of the hook name, `$settings_name`, refers to the settings name.
  125. *
  126. * @since 2.0.0
  127. *
  128. * @param array $success_response_data Success response data.
  129. * @param int $id Settings ID.
  130. * @param array $data Settings data.
  131. */
  132. $success_response_data = apply_filters( "elementor/settings/{$settings_name}/success_response_data", $success_response_data, $id, $data );
  133. return $success_response_data;
  134. }
  135. /**
  136. * Save settings.
  137. *
  138. * Save settings to the database.
  139. *
  140. * @since 1.6.0
  141. * @access public
  142. *
  143. * @param array $settings Settings.
  144. * @param int $id Optional. Post ID. Default is `0`.
  145. */
  146. public function save_settings( array $settings, $id = 0 ) {
  147. $special_settings = $this->get_special_settings_names();
  148. $settings_to_save = $settings;
  149. foreach ( $special_settings as $special_setting ) {
  150. if ( isset( $settings_to_save[ $special_setting ] ) ) {
  151. unset( $settings_to_save[ $special_setting ] );
  152. }
  153. }
  154. $this->save_settings_to_db( $settings_to_save, $id );
  155. // Clear cache after save.
  156. if ( isset( $this->models_cache[ $id ] ) ) {
  157. unset( $this->models_cache[ $id ] );
  158. }
  159. }
  160. /**
  161. * On Elementor init.
  162. *
  163. * Add editor template for the settings
  164. *
  165. * Fired by `elementor/init` action.
  166. *
  167. * @since 2.3.0
  168. * @access public
  169. */
  170. public function on_elementor_editor_init() {
  171. Plugin::$instance->common->add_template( $this->get_editor_template(), 'text' );
  172. }
  173. /**
  174. * Get saved settings.
  175. *
  176. * Retrieve the saved settings from the database.
  177. *
  178. * @since 1.6.0
  179. * @access protected
  180. * @abstract
  181. *
  182. * @param int $id Post ID.
  183. */
  184. abstract protected function get_saved_settings( $id );
  185. /**
  186. * Save settings to DB.
  187. *
  188. * Save settings to the database.
  189. *
  190. * @since 1.6.0
  191. * @access protected
  192. * @abstract
  193. *
  194. * @param array $settings Settings.
  195. * @param int $id Post ID.
  196. */
  197. abstract protected function save_settings_to_db( array $settings, $id );
  198. /**
  199. * Get special settings names.
  200. *
  201. * Retrieve the names of the special settings that are not saved as regular
  202. * settings. Those settings have a separate saving process.
  203. *
  204. * @since 1.6.0
  205. * @access protected
  206. *
  207. * @return array Special settings names.
  208. */
  209. protected function get_special_settings_names() {
  210. return [];
  211. }
  212. /**
  213. * Ajax before saving settings.
  214. *
  215. * Validate the data before saving it and updating the data in the database.
  216. *
  217. * @since 1.6.0
  218. * @access public
  219. *
  220. * @param array $data Post data.
  221. * @param int $id Post ID.
  222. */
  223. public function ajax_before_save_settings( array $data, $id ) {}
  224. /**
  225. * Print the setting template content in the editor.
  226. *
  227. * Used to generate the control HTML in the editor using Underscore JS
  228. * template. The variables for the class are available using `data` JS
  229. * object.
  230. *
  231. * @since 1.6.0
  232. * @access protected
  233. *
  234. * @param string $name Settings panel name.
  235. */
  236. protected function print_editor_template_content( $name ) {
  237. ?>
  238. <#
  239. const tabs = elementor.config.settings.<?php
  240. // PHPCS - the variable $name does not contain a user input value.
  241. echo $name; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  242. ?>.tabs;
  243. if ( Object.values( tabs ).length > 1 ) { #>
  244. <div class="elementor-panel-navigation">
  245. <# _.each( tabs, function( tabTitle, tabSlug ) {
  246. $e.bc.ensureTab( 'panel/<?php
  247. // PHPCS - the variable $name does not contain a user input value.
  248. echo $name; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  249. ?>-settings', tabSlug ); #>
  250. <div class="elementor-component-tab elementor-panel-navigation-tab elementor-tab-control-{{ tabSlug }}" data-tab="{{ tabSlug }}">
  251. <a href="#">{{{ tabTitle }}}</a>
  252. </div>
  253. <# } ); #>
  254. </div>
  255. <# } #>
  256. <div id="elementor-panel-<?php echo esc_attr( $name ); ?>-settings-controls"></div>
  257. <?php
  258. }
  259. /**
  260. * Create model.
  261. *
  262. * Create a new model object for any given model ID and store the object in
  263. * models cache property for later use.
  264. *
  265. * @since 1.6.0
  266. * @access private
  267. *
  268. * @param int $id Model ID.
  269. */
  270. private function create_model( $id ) {
  271. $class_parts = explode( '\\', get_called_class() );
  272. array_splice( $class_parts, count( $class_parts ) - 1, 1, 'Model' );
  273. $class_name = implode( '\\', $class_parts );
  274. $this->models_cache[ $id ] = new $class_name( [
  275. 'id' => $id,
  276. 'settings' => $this->get_saved_settings( $id ),
  277. ] );
  278. }
  279. /**
  280. * Get editor template.
  281. *
  282. * Retrieve the final HTML for the editor.
  283. *
  284. * @since 1.6.0
  285. * @access private
  286. *
  287. * @return string Settings editor template.
  288. */
  289. private function get_editor_template() {
  290. $name = $this->get_name();
  291. ob_start();
  292. ?>
  293. <script type="text/template" id="tmpl-elementor-panel-<?php echo esc_attr( $name ); ?>-settings">
  294. <?php $this->print_editor_template_content( $name ); ?>
  295. </script>
  296. <?php
  297. return ob_get_clean();
  298. }
  299. }