| 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 |
|
| 25 |
$kit_document = Plugin::$instance->documents->get( $id ); |
| 26 |
|
| 27 |
if ( ! $kit_document || ! $kit_document instanceof Kit || 'trash' === $kit_document->get_main_post()->post_status ) { |
| 28 |
$id = $this->create_default(); |
| 29 |
update_option( self::OPTION_ACTIVE, $id ); |
| 30 |
} |
| 31 |
|
| 32 |
return $id; |
| 33 |
} |
| 34 |
|
| 35 |
public function get_active_kit() { |
| 36 |
$id = $this->get_active_id(); |
| 37 |
|
| 38 |
return Plugin::$instance->documents->get( $id ); |
| 39 |
} |
| 40 |
|
| 41 |
public function get_active_kit_for_frontend() { |
| 42 |
$id = $this->get_active_id(); |
| 43 |
|
| 44 |
return Plugin::$instance->documents->get_doc_for_frontend( $id ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Checks if specific post is a kit. |
| 49 |
* |
| 50 |
* @param $post_id |
| 51 |
* |
| 52 |
* @return bool |
| 53 |
*/ |
| 54 |
public function is_kit( $post_id ) { |
| 55 |
$document = Plugin::$instance->documents->get( $post_id ); |
| 56 |
|
| 57 |
return $document && $document instanceof Kit && ! $document->is_revision(); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Init kit controls. |
| 63 |
* |
| 64 |
* A temp solution in order to avoid init kit group control from within another group control. |
| 65 |
* |
| 66 |
* 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 |
| 67 |
* and because the group control is a singleton, its args are changed to the last kit group control. |
| 68 |
*/ |
| 69 |
public function init_kit_controls() { |
| 70 |
$this->get_active_kit_for_frontend()->get_settings(); |
| 71 |
} |
| 72 |
|
| 73 |
public function get_current_settings( $setting = null ) { |
| 74 |
$kit = $this->get_active_kit_for_frontend(); |
| 75 |
|
| 76 |
if ( ! $kit ) { |
| 77 |
return ''; |
| 78 |
} |
| 79 |
|
| 80 |
return $kit->get_settings( $setting ); |
| 81 |
} |
| 82 |
|
| 83 |
private function create_default() { |
| 84 |
$kit = Plugin::$instance->documents->create( 'kit', [ |
| 85 |
'post_type' => Source_Local::CPT, |
| 86 |
'post_title' => __( 'Default Kit', 'elementor' ), |
| 87 |
'post_status' => 'publish', |
| 88 |
] ); |
| 89 |
|
| 90 |
return $kit->get_id(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @param Documents_Manager $documents_manager |
| 95 |
*/ |
| 96 |
public function register_document( $documents_manager ) { |
| 97 |
$documents_manager->register_document_type( 'kit', Kit::get_class_full_name() ); |
| 98 |
} |
| 99 |
|
| 100 |
public function localize_settings( $settings ) { |
| 101 |
$kit = $this->get_active_kit(); |
| 102 |
$kit_controls = $kit->get_controls(); |
| 103 |
$design_system_controls = [ |
| 104 |
'colors' => $kit_controls['system_colors']['fields'], |
| 105 |
'typography' => $kit_controls['system_typography']['fields'], |
| 106 |
]; |
| 107 |
|
| 108 |
$settings = array_replace_recursive( $settings, [ |
| 109 |
'kit_id' => $kit->get_main_id(), |
| 110 |
'kit_config' => [ |
| 111 |
'typography_prefix' => Global_Typography::TYPOGRAPHY_GROUP_PREFIX, |
| 112 |
'design_system_controls' => $design_system_controls, |
| 113 |
], |
| 114 |
'user' => [ |
| 115 |
'can_edit_kit' => $kit->is_editable_by_current_user(), |
| 116 |
], |
| 117 |
] ); |
| 118 |
|
| 119 |
return $settings; |
| 120 |
} |
| 121 |
|
| 122 |
public function preview_enqueue_styles() { |
| 123 |
$kit = $this->get_kit_for_frontend(); |
| 124 |
|
| 125 |
if ( $kit ) { |
| 126 |
// On preview, the global style is not enqueued. |
| 127 |
$this->frontend_before_enqueue_styles(); |
| 128 |
|
| 129 |
Plugin::$instance->frontend->print_fonts_links(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
public function frontend_before_enqueue_styles() { |
| 134 |
$kit = $this->get_kit_for_frontend(); |
| 135 |
|
| 136 |
if ( $kit ) { |
| 137 |
if ( $kit->is_autosave() ) { |
| 138 |
$css_file = Post_Preview::create( $kit->get_id() ); |
| 139 |
} else { |
| 140 |
$css_file = Post_CSS::create( $kit->get_id() ); |
| 141 |
} |
| 142 |
|
| 143 |
$css_file->enqueue(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public function render_panel_html() { |
| 148 |
require __DIR__ . '/views/panel.php'; |
| 149 |
} |
| 150 |
|
| 151 |
public function get_kit_for_frontend() { |
| 152 |
$kit = false; |
| 153 |
$active_kit = $this->get_active_kit(); |
| 154 |
$is_kit_preview = is_preview() && isset( $_GET['preview_id'] ) && $active_kit->get_main_id() === (int) $_GET['preview_id']; |
| 155 |
|
| 156 |
if ( $is_kit_preview ) { |
| 157 |
$kit = Plugin::$instance->documents->get_doc_or_auto_save( $active_kit->get_main_id(), get_current_user_id() ); |
| 158 |
} elseif ( 'publish' === $active_kit->get_main_post()->post_status ) { |
| 159 |
$kit = $active_kit; |
| 160 |
} |
| 161 |
|
| 162 |
return $kit; |
| 163 |
} |
| 164 |
|
| 165 |
public function update_kit_settings_based_on_option( $key, $value ) { |
| 166 |
/** @var Kit $active_kit */ |
| 167 |
$active_kit = $this->get_active_kit(); |
| 168 |
|
| 169 |
if ( $active_kit->is_saving() ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
$active_kit->update_settings( [ $key => $value ] ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Map Scheme To Global |
| 178 |
* |
| 179 |
* Convert a given scheme value to its corresponding default global value |
| 180 |
* |
| 181 |
* @param string $type 'color'/'typography' |
| 182 |
* @param $value |
| 183 |
* @return mixed |
| 184 |
*/ |
| 185 |
private function map_scheme_to_global( $type, $value ) { |
| 186 |
$schemes_to_globals_map = [ |
| 187 |
'color' => [ |
| 188 |
'1' => Global_Colors::COLOR_PRIMARY, |
| 189 |
'2' => Global_Colors::COLOR_SECONDARY, |
| 190 |
'3' => Global_Colors::COLOR_TEXT, |
| 191 |
'4' => Global_Colors::COLOR_ACCENT, |
| 192 |
], |
| 193 |
'typography' => [ |
| 194 |
'1' => Global_Typography::TYPOGRAPHY_PRIMARY, |
| 195 |
'2' => Global_Typography::TYPOGRAPHY_SECONDARY, |
| 196 |
'3' => Global_Typography::TYPOGRAPHY_TEXT, |
| 197 |
'4' => Global_Typography::TYPOGRAPHY_ACCENT, |
| 198 |
], |
| 199 |
]; |
| 200 |
|
| 201 |
return $schemes_to_globals_map[ $type ][ $value ]; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Convert Scheme to Default Global |
| 206 |
* |
| 207 |
* If a control has a scheme property, convert it to a default Global. |
| 208 |
* |
| 209 |
* @param $scheme - Control scheme property |
| 210 |
* @return array - Control/group control args |
| 211 |
* @since 3.0.0 |
| 212 |
* @access public |
| 213 |
*/ |
| 214 |
public function convert_scheme_to_global( $scheme ) { |
| 215 |
if ( isset( $scheme['type'] ) && isset( $scheme['value'] ) ) { |
| 216 |
//_deprecated_argument( $args['scheme'], '3.0.0', 'Schemes are now deprecated - use $args[\'global\'] instead.' ); |
| 217 |
return $this->map_scheme_to_global( $scheme['type'], $scheme['value'] ); |
| 218 |
} |
| 219 |
|
| 220 |
// Typography control 'scheme' properties usually only include the string with the typography value ('1'-'4'). |
| 221 |
return $this->map_scheme_to_global( 'typography', $scheme ); |
| 222 |
} |
| 223 |
|
| 224 |
public function register_controls() { |
| 225 |
$controls_manager = Plugin::$instance->controls_manager; |
| 226 |
|
| 227 |
$controls_manager->register_control( Repeater::CONTROL_TYPE, new Repeater() ); |
| 228 |
} |
| 229 |
|
| 230 |
public function is_custom_colors_enabled() { |
| 231 |
return ! get_option( 'elementor_disable_color_schemes' ); |
| 232 |
} |
| 233 |
|
| 234 |
public function is_custom_typography_enabled() { |
| 235 |
return ! get_option( 'elementor_disable_typography_schemes' ); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Add kit wrapper body class. |
| 240 |
* |
| 241 |
* It should be added even for non Elementor pages, |
| 242 |
* in order to support embedded templates. |
| 243 |
*/ |
| 244 |
private function add_body_class() { |
| 245 |
$kit = $this->get_kit_for_frontend(); |
| 246 |
|
| 247 |
if ( $kit ) { |
| 248 |
Plugin::$instance->frontend->add_body_class( 'elementor-kit-' . $kit->get_main_id() ); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Send a confirm message before move a kit to trash, or if delete permanently not for trash. |
| 254 |
* |
| 255 |
* @param $post_id |
| 256 |
* @param false $is_permanently_delete |
| 257 |
*/ |
| 258 |
private function before_delete_kit( $post_id, $is_permanently_delete = false ) { |
| 259 |
$document = Plugin::$instance->documents->get( $post_id ); |
| 260 |
|
| 261 |
if ( |
| 262 |
! $document || |
| 263 |
! $this->is_kit( $post_id ) || |
| 264 |
isset( $_GET['force_delete_kit'] ) || // phpcs:ignore -- nonce validation is not require here. |
| 265 |
( $is_permanently_delete && $document->is_trash() ) |
| 266 |
) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
ob_start(); |
| 271 |
require __DIR__ . '/views/trash-kit-confirmation.php'; |
| 272 |
|
| 273 |
$confirmation_content = ob_get_clean(); |
| 274 |
|
| 275 |
wp_die( |
| 276 |
new \WP_Error( 'cant_delete_kit', $confirmation_content ) |
| 277 |
); |
| 278 |
} |
| 279 |
|
| 280 |
public function __construct() { |
| 281 |
add_action( 'elementor/documents/register', [ $this, 'register_document' ] ); |
| 282 |
add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] ); |
| 283 |
add_filter( 'elementor/editor/footer', [ $this, 'render_panel_html' ] ); |
| 284 |
add_action( 'elementor/frontend/after_enqueue_styles', [ $this, 'frontend_before_enqueue_styles' ], 0 ); |
| 285 |
add_action( 'elementor/preview/enqueue_styles', [ $this, 'preview_enqueue_styles' ], 0 ); |
| 286 |
add_action( 'elementor/controls/controls_registered', [ $this, 'register_controls' ] ); |
| 287 |
|
| 288 |
add_action( 'wp_trash_post', function ( $post_id ) { |
| 289 |
$this->before_delete_kit( $post_id ); |
| 290 |
} ); |
| 291 |
|
| 292 |
add_action( 'before_delete_post', function ( $post_id ) { |
| 293 |
$this->before_delete_kit( $post_id, true ); |
| 294 |
} ); |
| 295 |
|
| 296 |
add_action( 'update_option_blogname', function ( $old_value, $value ) { |
| 297 |
$this->update_kit_settings_based_on_option( 'site_name', $value ); |
| 298 |
}, 10, 2 ); |
| 299 |
|
| 300 |
add_action( 'update_option_blogdescription', function ( $old_value, $value ) { |
| 301 |
$this->update_kit_settings_based_on_option( 'site_description', $value ); |
| 302 |
}, 10, 2 ); |
| 303 |
|
| 304 |
add_action( 'wp_head', function() { |
| 305 |
$this->add_body_class(); |
| 306 |
} ); |
| 307 |
} |
| 308 |
} |
| 309 |
|