| 1 |
<?php |
| 2 |
namespace Elementor\Core\Kits; |
| 3 |
|
| 4 |
use Elementor\Core\Kits\Controls\Repeater; |
| 5 |
use Elementor\Core\Kits\Documents\Tabs\Global_Colors; |
| 6 |
use Elementor\Core\Kits\Documents\Tabs\Global_Typography; |
| 7 |
use Elementor\Plugin; |
| 8 |
use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 9 |
use Elementor\Core\Files\CSS\Post_Preview as Post_Preview; |
| 10 |
use Elementor\Core\Documents_Manager; |
| 11 |
use Elementor\Core\Kits\Documents\Kit; |
| 12 |
use Elementor\TemplateLibrary\Source_Local; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
class Manager { |
| 19 |
|
| 20 |
const OPTION_ACTIVE = 'elementor_active_kit'; |
| 21 |
|
| 22 |
public function get_active_id() { |
| 23 |
$id = get_option( self::OPTION_ACTIVE ); |
| 24 |
$kit_post = null; |
| 25 |
|
| 26 |
if ( $id ) { |
| 27 |
$kit_post = get_post( $id ); |
| 28 |
} |
| 29 |
|
| 30 |
if ( ! $id || ! $kit_post || 'trash' === $kit_post->post_status ) { |
| 31 |
$id = $this->create_default(); |
| 32 |
update_option( self::OPTION_ACTIVE, $id ); |
| 33 |
} |
| 34 |
return $id; |
| 35 |
} |
| 36 |
|
| 37 |
public function get_active_kit() { |
| 38 |
$id = $this->get_active_id(); |
| 39 |
|
| 40 |
return Plugin::$instance->documents->get( $id ); |
| 41 |
} |
| 42 |
|
| 43 |
public function get_active_kit_for_frontend() { |
| 44 |
$id = $this->get_active_id(); |
| 45 |
|
| 46 |
return Plugin::$instance->documents->get_doc_for_frontend( $id ); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* Init kit controls. |
| 52 |
* |
| 53 |
* A temp solution in order to avoid init kit group control from within another group control. |
| 54 |
* |
| 55 |
* After moving the `default_font` to the kit, the Typography group control cause initialize the kit controls at: https://github.com/elementor/elementor/blob/e6e1db9eddef7e3c1a5b2ba0c2338e2af2a3bfe3/includes/controls/groups/typography.php#L91 |
| 56 |
* and because the group control is a singleton, its args are changed to the last kit group control. |
| 57 |
*/ |
| 58 |
public function init_kit_controls() { |
| 59 |
$this->get_active_kit_for_frontend()->get_settings(); |
| 60 |
} |
| 61 |
|
| 62 |
public function get_current_settings( $setting = null ) { |
| 63 |
$kit = $this->get_active_kit_for_frontend(); |
| 64 |
|
| 65 |
if ( ! $kit ) { |
| 66 |
return ''; |
| 67 |
} |
| 68 |
|
| 69 |
return $kit->get_settings( $setting ); |
| 70 |
} |
| 71 |
|
| 72 |
private function create_default() { |
| 73 |
$kit = Plugin::$instance->documents->create( 'kit', [ |
| 74 |
'post_type' => Source_Local::CPT, |
| 75 |
'post_title' => __( 'Default Kit', 'elementor' ), |
| 76 |
'post_status' => 'publish', |
| 77 |
] ); |
| 78 |
|
| 79 |
return $kit->get_id(); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param Documents_Manager $documents_manager |
| 84 |
*/ |
| 85 |
public function register_document( $documents_manager ) { |
| 86 |
$documents_manager->register_document_type( 'kit', Kit::get_class_full_name() ); |
| 87 |
} |
| 88 |
|
| 89 |
public function localize_settings( $settings ) { |
| 90 |
$kit = $this->get_active_kit(); |
| 91 |
$kit_controls = $kit->get_controls(); |
| 92 |
$design_system_controls = [ |
| 93 |
'colors' => $kit_controls['system_colors']['fields'], |
| 94 |
'typography' => $kit_controls['system_typography']['fields'], |
| 95 |
]; |
| 96 |
|
| 97 |
$settings = array_replace_recursive( $settings, [ |
| 98 |
'kit_id' => $kit->get_main_id(), |
| 99 |
'kit_config' => [ |
| 100 |
'typography_prefix' => Global_Typography::TYPOGRAPHY_GROUP_PREFIX, |
| 101 |
'design_system_controls' => $design_system_controls, |
| 102 |
], |
| 103 |
'user' => [ |
| 104 |
'can_edit_kit' => $kit->is_editable_by_current_user(), |
| 105 |
], |
| 106 |
'i18n' => [ |
| 107 |
'close' => __( 'Close', 'elementor' ), |
| 108 |
'back' => __( 'Back', 'elementor' ), |
| 109 |
'site_identity' => __( 'Site Identity', 'elementor' ), |
| 110 |
'lightbox' => __( 'Lightbox', 'elementor' ), |
| 111 |
'layout' => __( 'Layout', 'elementor' ), |
| 112 |
'theme_style' => __( 'Theme Style', 'elementor' ), |
| 113 |
'add_color' => __( 'Add Color', 'elementor' ), |
| 114 |
'add_style' => __( 'Add Style', 'elementor' ), |
| 115 |
'new_item' => __( 'New Item', 'elementor' ), |
| 116 |
'new_global' => __( 'New Global', 'elementor' ), |
| 117 |
'global_color' => __( 'Global Color', 'elementor' ), |
| 118 |
'global_fonts' => __( 'Global Fonts', 'elementor' ), |
| 119 |
'global_colors' => __( 'Global Colors', 'elementor' ), |
| 120 |
'invalid' => __( 'Invalid', 'elementor' ), |
| 121 |
'color_cannot_be_deleted' => __( 'System Colors can\'t be deleted', 'elementor' ), |
| 122 |
'font_cannot_be_deleted' => __( 'System Font can\'t be deleted', 'elementor' ), |
| 123 |
'design_system' => __( 'Design System', 'elementor' ), |
| 124 |
'buttons' => __( 'Buttons', 'elementor' ), |
| 125 |
'images' => __( 'Images', 'elementor' ), |
| 126 |
'form_fields' => __( 'Form Fields', 'elementor' ), |
| 127 |
'background' => __( 'Background', 'elementor' ), |
| 128 |
'custom_css' => __( 'Custom CSS', 'elementor' ), |
| 129 |
'additional_settings' => __( 'Additional Settings', 'elementor' ), |
| 130 |
'kit_changes_updated' => __( 'Your changes have been updated.', 'elementor' ), |
| 131 |
'back_to_editor' => __( 'Back to Editor', 'elementor' ), |
| 132 |
], |
| 133 |
] ); |
| 134 |
|
| 135 |
return $settings; |
| 136 |
} |
| 137 |
|
| 138 |
public function preview_enqueue_styles() { |
| 139 |
$kit = $this->get_kit_for_frontend(); |
| 140 |
|
| 141 |
if ( $kit ) { |
| 142 |
// On preview, the global style is not enqueued. |
| 143 |
$this->frontend_before_enqueue_styles(); |
| 144 |
|
| 145 |
Plugin::$instance->frontend->print_fonts_links(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
public function frontend_before_enqueue_styles() { |
| 150 |
$kit = $this->get_kit_for_frontend(); |
| 151 |
|
| 152 |
if ( $kit ) { |
| 153 |
if ( $kit->is_autosave() ) { |
| 154 |
$css_file = Post_Preview::create( $kit->get_id() ); |
| 155 |
} else { |
| 156 |
$css_file = Post_CSS::create( $kit->get_id() ); |
| 157 |
} |
| 158 |
|
| 159 |
$css_file->enqueue(); |
| 160 |
|
| 161 |
Plugin::$instance->frontend->add_body_class( 'elementor-kit-' . $kit->get_main_id() ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
public function render_panel_html() { |
| 166 |
require __DIR__ . '/views/panel.php'; |
| 167 |
} |
| 168 |
|
| 169 |
public function get_kit_for_frontend() { |
| 170 |
$kit = false; |
| 171 |
$active_kit = $this->get_active_kit(); |
| 172 |
$is_kit_preview = is_preview() && isset( $_GET['preview_id'] ) && $active_kit->get_main_id() === (int) $_GET['preview_id']; |
| 173 |
|
| 174 |
if ( $is_kit_preview ) { |
| 175 |
$kit = Plugin::$instance->documents->get_doc_or_auto_save( $active_kit->get_main_id(), get_current_user_id() ); |
| 176 |
} elseif ( 'publish' === $active_kit->get_main_post()->post_status ) { |
| 177 |
$kit = $active_kit; |
| 178 |
} |
| 179 |
|
| 180 |
return $kit; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Map Scheme To Global |
| 185 |
* |
| 186 |
* Convert a given scheme value to its corresponding default global value |
| 187 |
* |
| 188 |
* @param string $type 'color'/'typography' |
| 189 |
* @param $value |
| 190 |
* @return mixed |
| 191 |
*/ |
| 192 |
private function map_scheme_to_global( $type, $value ) { |
| 193 |
$schemes_to_globals_map = [ |
| 194 |
'color' => [ |
| 195 |
'1' => Global_Colors::COLOR_PRIMARY, |
| 196 |
'2' => Global_Colors::COLOR_SECONDARY, |
| 197 |
'3' => Global_Colors::COLOR_TEXT, |
| 198 |
'4' => Global_Colors::COLOR_ACCENT, |
| 199 |
], |
| 200 |
'typography' => [ |
| 201 |
'1' => Global_Typography::TYPOGRAPHY_PRIMARY, |
| 202 |
'2' => Global_Typography::TYPOGRAPHY_SECONDARY, |
| 203 |
'3' => Global_Typography::TYPOGRAPHY_TEXT, |
| 204 |
'4' => Global_Typography::TYPOGRAPHY_ACCENT, |
| 205 |
], |
| 206 |
]; |
| 207 |
|
| 208 |
return $schemes_to_globals_map[ $type ][ $value ]; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Convert Scheme to Default Global |
| 213 |
* |
| 214 |
* If a control has a scheme property, convert it to a default Global. |
| 215 |
* |
| 216 |
* @param $scheme - Control scheme property |
| 217 |
* @return array - Control/group control args |
| 218 |
* @since 3.0.0 |
| 219 |
* @access public |
| 220 |
*/ |
| 221 |
public function convert_scheme_to_global( $scheme ) { |
| 222 |
if ( isset( $scheme['type'] ) && isset( $scheme['value'] ) ) { |
| 223 |
//_deprecated_argument( $args['scheme'], '3.0.0', 'Schemes are now deprecated - use $args[\'global\'] instead.' ); |
| 224 |
return $this->map_scheme_to_global( $scheme['type'], $scheme['value'] ); |
| 225 |
} |
| 226 |
|
| 227 |
// Typography control 'scheme' properties usually only include the string with the typography value ('1'-'4'). |
| 228 |
return $this->map_scheme_to_global( 'typography', $scheme ); |
| 229 |
} |
| 230 |
|
| 231 |
public function register_controls() { |
| 232 |
$controls_manager = Plugin::$instance->controls_manager; |
| 233 |
|
| 234 |
$controls_manager->register_control( Repeater::CONTROL_TYPE, new Repeater() ); |
| 235 |
} |
| 236 |
|
| 237 |
public function is_custom_colors_enabled() { |
| 238 |
return ! get_option( 'elementor_disable_color_schemes' ); |
| 239 |
} |
| 240 |
|
| 241 |
public function is_custom_typography_enabled() { |
| 242 |
return ! get_option( 'elementor_disable_typography_schemes' ); |
| 243 |
} |
| 244 |
|
| 245 |
public function __construct() { |
| 246 |
add_action( 'elementor/documents/register', [ $this, 'register_document' ] ); |
| 247 |
add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] ); |
| 248 |
add_filter( 'elementor/editor/footer', [ $this, 'render_panel_html' ] ); |
| 249 |
add_action( 'elementor/frontend/after_enqueue_styles', [ $this, 'frontend_before_enqueue_styles' ], 0 ); |
| 250 |
add_action( 'elementor/preview/enqueue_styles', [ $this, 'preview_enqueue_styles' ], 0 ); |
| 251 |
add_action( 'elementor/controls/controls_registered', [ $this, 'register_controls' ] ); |
| 252 |
} |
| 253 |
} |
| 254 |
|