base.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor scheme base.
  8. *
  9. * An abstract class implementing the scheme interface, responsible for
  10. * creating new schemes.
  11. *
  12. * @since 1.0.0
  13. * @abstract
  14. */
  15. abstract class Scheme_Base implements Scheme_Interface {
  16. /**
  17. * DB option name for the time when the scheme was last updated.
  18. */
  19. const LAST_UPDATED_META = '_elementor_scheme_last_updated';
  20. /**
  21. * System schemes.
  22. *
  23. * Holds the list of all the system schemes.
  24. *
  25. * @since 1.0.0
  26. * @access private
  27. *
  28. * @var array System schemes.
  29. */
  30. private $_system_schemes;
  31. /**
  32. * Init system schemes.
  33. *
  34. * Initialize the system schemes.
  35. *
  36. * @since 1.0.0
  37. * @access protected
  38. * @abstract
  39. */
  40. abstract protected function _init_system_schemes();
  41. /**
  42. * Get description.
  43. *
  44. * Retrieve the scheme description.
  45. *
  46. * @since 1.0.0
  47. * @access public
  48. * @static
  49. *
  50. * @return string Scheme description.
  51. */
  52. public static function get_description() {
  53. return '';
  54. }
  55. /**
  56. * Get system schemes.
  57. *
  58. * Retrieve the system schemes.
  59. *
  60. * @since 1.0.0
  61. * @access public
  62. *
  63. * @return string System schemes.
  64. */
  65. final public function get_system_schemes() {
  66. if ( null === $this->_system_schemes ) {
  67. $this->_system_schemes = $this->_init_system_schemes();
  68. }
  69. return $this->_system_schemes;
  70. }
  71. /**
  72. * Get scheme value.
  73. *
  74. * Retrieve the scheme value.
  75. *
  76. * @since 1.0.0
  77. * @access public
  78. *
  79. * @return string Scheme value.
  80. */
  81. public function get_scheme_value() {
  82. $scheme_value = get_option( 'elementor_scheme_' . static::get_type() );
  83. if ( ! $scheme_value ) {
  84. $scheme_value = $this->get_default_scheme();
  85. update_option( 'elementor_scheme_' . static::get_type(), $scheme_value );
  86. }
  87. return $scheme_value;
  88. }
  89. /**
  90. * Save scheme.
  91. *
  92. * Update Elementor scheme in the database, and update the last updated
  93. * scheme time.
  94. *
  95. * @since 1.0.0
  96. * @access public
  97. *
  98. * @param array $posted
  99. */
  100. public function save_scheme( array $posted ) {
  101. $scheme_value = $this->get_scheme_value();
  102. update_option( 'elementor_scheme_' . static::get_type(), array_replace( $scheme_value, array_intersect_key( $posted, $scheme_value ) ) );
  103. update_option( self::LAST_UPDATED_META, time() );
  104. }
  105. /**
  106. * Get scheme.
  107. *
  108. * Retrieve the scheme.
  109. *
  110. * @since 1.0.0
  111. * @access public
  112. *
  113. * @return array The scheme.
  114. */
  115. public function get_scheme() {
  116. $scheme = [];
  117. $titles = $this->get_scheme_titles();
  118. foreach ( $this->get_scheme_value() as $scheme_key => $scheme_value ) {
  119. $scheme[ $scheme_key ] = [
  120. 'title' => isset( $titles[ $scheme_key ] ) ? $titles[ $scheme_key ] : '',
  121. 'value' => $scheme_value,
  122. ];
  123. }
  124. return $scheme;
  125. }
  126. /**
  127. * Print scheme template.
  128. *
  129. * Used to generate the scheme template on the editor using Underscore JS
  130. * template.
  131. *
  132. * @since 1.0.0
  133. * @access public
  134. */
  135. final public function print_template() {
  136. ?>
  137. <script type="text/template" id="tmpl-elementor-panel-schemes-<?php echo static::get_type(); ?>">
  138. <div class="elementor-panel-scheme-buttons">
  139. <div class="elementor-panel-scheme-button-wrapper elementor-panel-scheme-reset">
  140. <button class="elementor-button">
  141. <i class="fa fa-undo" aria-hidden="true"></i>
  142. <?php echo __( 'Reset', 'elementor' ); ?>
  143. </button>
  144. </div>
  145. <div class="elementor-panel-scheme-button-wrapper elementor-panel-scheme-discard">
  146. <button class="elementor-button">
  147. <i class="fa fa-times" aria-hidden="true"></i>
  148. <?php echo __( 'Discard', 'elementor' ); ?>
  149. </button>
  150. </div>
  151. <div class="elementor-panel-scheme-button-wrapper elementor-panel-scheme-save">
  152. <button class="elementor-button elementor-button-success" disabled><?php echo __( 'Apply', 'elementor' ); ?></button>
  153. </div>
  154. </div>
  155. <?php $this->print_template_content(); ?>
  156. </script>
  157. <?php
  158. }
  159. }