schemes.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor scheme manager.
  8. *
  9. * Elementor scheme manager handler class is responsible for registering and
  10. * initializing all the supported schemes.
  11. *
  12. * @since 1.0.0
  13. */
  14. class Schemes_Manager {
  15. /**
  16. * Registered schemes.
  17. *
  18. * Holds the list of all the registered schemes.
  19. *
  20. * @access protected
  21. *
  22. * @var Scheme_Base[]
  23. */
  24. protected $_registered_schemes = [];
  25. /**
  26. * Enabled schemes.
  27. *
  28. * Holds the list of all the enabled schemes.
  29. *
  30. * @access private
  31. * @static
  32. *
  33. * @var array
  34. */
  35. private static $_enabled_schemes;
  36. /**
  37. * Schemes types.
  38. *
  39. * Holds the list of the schemes types. Default types are `color`,
  40. * `typography` and `color-picker`.
  41. *
  42. * @access private
  43. * @static
  44. *
  45. * @var array
  46. */
  47. private static $_schemes_types = [
  48. 'color' => 'Scheme_Color',
  49. 'typography' => 'Scheme_Typography',
  50. 'color-picker' => 'Scheme_Color_Picker',
  51. ];
  52. /**
  53. * Register new scheme.
  54. *
  55. * Add a new scheme to the schemes list. The method creates a new scheme
  56. * instance for any given scheme class and adds the scheme to the registered
  57. * schemes list.
  58. *
  59. * @since 1.0.0
  60. * @access public
  61. *
  62. * @param string $scheme_class Scheme class name.
  63. */
  64. public function register_scheme( $scheme_class ) {
  65. /** @var Scheme_Base $scheme_instance */
  66. $scheme_instance = new $scheme_class();
  67. $this->_registered_schemes[ $scheme_instance::get_type() ] = $scheme_instance;
  68. }
  69. /**
  70. * Unregister scheme.
  71. *
  72. * Removes a scheme from the list of registered schemes.
  73. *
  74. * @since 1.0.0
  75. * @access public
  76. *
  77. * @param string $id Scheme ID.
  78. *
  79. * @return bool True if the scheme was removed, False otherwise.
  80. */
  81. public function unregister_scheme( $id ) {
  82. if ( ! isset( $this->_registered_schemes[ $id ] ) ) {
  83. return false;
  84. }
  85. unset( $this->_registered_schemes[ $id ] );
  86. return true;
  87. }
  88. /**
  89. * Get registered schemes.
  90. *
  91. * Retrieve the registered schemes list from the current instance.
  92. *
  93. * @since 1.0.0
  94. * @access public
  95. *
  96. * @return Scheme_Base[] Registered schemes.
  97. */
  98. public function get_registered_schemes() {
  99. return $this->_registered_schemes;
  100. }
  101. /**
  102. * Get schemes data.
  103. *
  104. * Retrieve all the registered schemes with data for each scheme.
  105. *
  106. * @since 1.0.0
  107. * @access public
  108. *
  109. * @return array Registered schemes with each scheme data.
  110. */
  111. public function get_registered_schemes_data() {
  112. $data = [];
  113. foreach ( $this->get_registered_schemes() as $scheme ) {
  114. $data[ $scheme::get_type() ] = [
  115. 'title' => $scheme->get_title(),
  116. 'disabled_title' => $scheme->get_disabled_title(),
  117. 'items' => $scheme->get_scheme(),
  118. ];
  119. }
  120. return $data;
  121. }
  122. /**
  123. * Get default schemes.
  124. *
  125. * Retrieve all the registered schemes with default scheme for each scheme.
  126. *
  127. * @since 1.0.0
  128. * @access public
  129. *
  130. * @return array Registered schemes with with default scheme for each scheme.
  131. */
  132. public function get_schemes_defaults() {
  133. $data = [];
  134. foreach ( $this->get_registered_schemes() as $scheme ) {
  135. $data[ $scheme::get_type() ] = [
  136. 'title' => $scheme->get_title(),
  137. 'items' => $scheme->get_default_scheme(),
  138. ];
  139. }
  140. return $data;
  141. }
  142. /**
  143. * Get system schemes.
  144. *
  145. * Retrieve all the registered schemes with system schemes for each scheme.
  146. *
  147. * @since 1.0.0
  148. * @access public
  149. *
  150. * @return array Registered schemes with with system scheme for each scheme.
  151. */
  152. public function get_system_schemes() {
  153. $data = [];
  154. foreach ( $this->get_registered_schemes() as $scheme ) {
  155. $data[ $scheme::get_type() ] = $scheme->get_system_schemes();
  156. }
  157. return $data;
  158. }
  159. /**
  160. * Get scheme.
  161. *
  162. * Retrieve a single scheme from the list of all the registered schemes in
  163. * the current instance.
  164. *
  165. * @since 1.0.0
  166. * @access public
  167. *
  168. * @param string $id Scheme ID.
  169. *
  170. * @return false|Scheme_Base Scheme instance if scheme exist, False otherwise.
  171. */
  172. public function get_scheme( $id ) {
  173. $schemes = $this->get_registered_schemes();
  174. if ( ! isset( $schemes[ $id ] ) ) {
  175. return false;
  176. }
  177. return $schemes[ $id ];
  178. }
  179. /**
  180. * Get scheme value.
  181. *
  182. * Retrieve the scheme value from the list of all the registered schemes in
  183. * the current instance.
  184. *
  185. * @since 1.0.0
  186. * @access public
  187. *
  188. * @param string $scheme_type Scheme type.
  189. * @param string $scheme_value Scheme value.
  190. *
  191. * @return false|string Scheme value if scheme exist, False otherwise.
  192. */
  193. public function get_scheme_value( $scheme_type, $scheme_value ) {
  194. $scheme = $this->get_scheme( $scheme_type );
  195. if ( ! $scheme ) {
  196. return false;
  197. }
  198. return $scheme->get_scheme_value()[ $scheme_value ];
  199. }
  200. /**
  201. * Ajax apply scheme.
  202. *
  203. * Ajax handler for Elementor apply_scheme.
  204. *
  205. * Fired by `wp_ajax_elementor_apply_scheme` action.
  206. *
  207. * @since 1.0.0
  208. * @access public
  209. */
  210. public function ajax_apply_scheme() {
  211. Plugin::$instance->editor->verify_ajax_nonce();
  212. if ( ! isset( $_POST['scheme_name'] ) ) {
  213. wp_send_json_error();
  214. }
  215. $scheme_obj = $this->get_scheme( $_POST['scheme_name'] );
  216. if ( ! $scheme_obj ) {
  217. wp_send_json_error();
  218. }
  219. $posted = json_decode( stripslashes( $_POST['data'] ), true );
  220. $scheme_obj->save_scheme( $posted );
  221. wp_send_json_success();
  222. }
  223. /**
  224. * Print schemes templates.
  225. *
  226. * Used to generate the scheme templates on the editor using Underscore JS
  227. * template, for all the registered schemes.
  228. *
  229. * @since 1.0.0
  230. * @access public
  231. */
  232. public function print_schemes_templates() {
  233. foreach ( $this->get_registered_schemes() as $scheme ) {
  234. $scheme->print_template();
  235. }
  236. }
  237. /**
  238. * Get enabled schemes.
  239. *
  240. * Retrieve all enabled schemes from the list of the registered schemes in
  241. * the current instance.
  242. *
  243. * @since 1.0.0
  244. * @access public
  245. * @static
  246. *
  247. * @return array Enabled schemes.
  248. */
  249. public static function get_enabled_schemes() {
  250. if ( null === self::$_enabled_schemes ) {
  251. $enabled_schemes = [];
  252. foreach ( self::$_schemes_types as $schemes_type => $scheme_class ) {
  253. if ( 'yes' === get_option( 'elementor_disable_' . $schemes_type . '_schemes' ) ) {
  254. continue;
  255. }
  256. $enabled_schemes[] = $schemes_type;
  257. }
  258. /**
  259. * Enabled schemes.
  260. *
  261. * Filters the list of enabled schemes.
  262. *
  263. * @since 1.0.0
  264. *
  265. * @param array $enabled_schemes The list of enabled schemes.
  266. */
  267. $enabled_schemes = apply_filters( 'elementor/schemes/enabled_schemes', $enabled_schemes );
  268. self::$_enabled_schemes = $enabled_schemes;
  269. }
  270. return self::$_enabled_schemes;
  271. }
  272. /**
  273. * Register default schemes.
  274. *
  275. * Add a default schemes to the register schemes list.
  276. *
  277. * This method is used to set initial schemes when initializing the class.
  278. *
  279. * @since 1.7.12
  280. * @access private
  281. */
  282. private function register_default_schemes() {
  283. foreach ( self::$_schemes_types as $schemes_class ) {
  284. $this->register_scheme( __NAMESPACE__ . '\\' . $schemes_class );
  285. }
  286. }
  287. /**
  288. * Schemes manager constructor.
  289. *
  290. * Initializing Elementor schemes manager and register default schemes.
  291. *
  292. * @since 1.0.0
  293. * @access public
  294. */
  295. public function __construct() {
  296. $this->register_default_schemes();
  297. add_action( 'wp_ajax_elementor_apply_scheme', [ $this, 'ajax_apply_scheme' ] );
  298. }
  299. }