balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

manager.php (7958B)


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