PluginProbe
Elementor Website Builder – more than just a page builder / 3.0.13
Elementor Website Builder – more than just a page builder v3.0.13
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / kits / manager.php

manager.php in Elementor Website Builder – more than just a page builder 3.0.13, at core/kits/manager.php

286 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
49 * Init kit controls.
50 *
51 * A temp solution in order to avoid init kit group control from within another group control.
52 *
53 * 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
54 * and because the group control is a singleton, its args are changed to the last kit group control.
55 */
56 public function init_kit_controls() {
57 $this->get_active_kit_for_frontend()->get_settings();
58 }
59
60 public function get_current_settings( $setting = null ) {
61 $kit = $this->get_active_kit_for_frontend();
62
63 if ( ! $kit ) {
64 return '';
65 }
66
67 return $kit->get_settings( $setting );
68 }
69
70 private function create_default() {
71 $kit = Plugin::$instance->documents->create( 'kit', [
72 'post_type' => Source_Local::CPT,
73 'post_title' => __( 'Default Kit', 'elementor' ),
74 'post_status' => 'publish',
75 ] );
76
77 return $kit->get_id();
78 }
79
80 /**
81 * @param Documents_Manager $documents_manager
82 */
83 public function register_document( $documents_manager ) {
84 $documents_manager->register_document_type( 'kit', Kit::get_class_full_name() );
85 }
86
87 public function localize_settings( $settings ) {
88 $kit = $this->get_active_kit();
89 $kit_controls = $kit->get_controls();
90 $design_system_controls = [
91 'colors' => $kit_controls['system_colors']['fields'],
92 'typography' => $kit_controls['system_typography']['fields'],
93 ];
94
95 $settings = array_replace_recursive( $settings, [
96 'kit_id' => $kit->get_main_id(),
97 'kit_config' => [
98 'typography_prefix' => Global_Typography::TYPOGRAPHY_GROUP_PREFIX,
99 'design_system_controls' => $design_system_controls,
100 ],
101 'user' => [
102 'can_edit_kit' => $kit->is_editable_by_current_user(),
103 ],
104 'i18n' => [
105 'close' => __( 'Close', 'elementor' ),
106 'back' => __( 'Back', 'elementor' ),
107 'site_identity' => __( 'Site Identity', 'elementor' ),
108 'lightbox' => __( 'Lightbox', 'elementor' ),
109 'layout' => __( 'Layout', 'elementor' ),
110 'theme_style' => __( 'Theme Style', 'elementor' ),
111 'add_color' => __( 'Add Color', 'elementor' ),
112 'add_style' => __( 'Add Style', 'elementor' ),
113 'new_item' => __( 'New Item', 'elementor' ),
114 'global_color' => __( 'Global Color', 'elementor' ),
115 'global_fonts' => __( 'Global Fonts', 'elementor' ),
116 'global_colors' => __( 'Global Colors', 'elementor' ),
117 'invalid' => __( 'Invalid', 'elementor' ),
118 'color_cannot_be_deleted' => __( 'System Color can\'t be deleted', 'elementor' ),
119 'font_cannot_be_deleted' => __( 'System Font can\'t be deleted', 'elementor' ),
120 'design_system' => __( 'Design System', 'elementor' ),
121 'buttons' => __( 'Buttons', 'elementor' ),
122 'images' => __( 'Images', 'elementor' ),
123 'form_fields' => __( 'Form Fields', 'elementor' ),
124 'background' => __( 'Background', 'elementor' ),
125 'custom_css' => __( 'Custom CSS', 'elementor' ),
126 'additional_settings' => __( 'Additional Settings', 'elementor' ),
127 'kit_changes_updated' => __( 'Your changes have been updated.', 'elementor' ),
128 'back_to_editor' => __( 'Back to Editor', 'elementor' ),
129 ],
130 ] );
131
132 return $settings;
133 }
134
135 public function preview_enqueue_styles() {
136 $kit = $this->get_kit_for_frontend();
137
138 if ( $kit ) {
139 // On preview, the global style is not enqueued.
140 $this->frontend_before_enqueue_styles();
141
142 Plugin::$instance->frontend->print_fonts_links();
143 }
144 }
145
146 public function frontend_before_enqueue_styles() {
147 $kit = $this->get_kit_for_frontend();
148
149 if ( $kit ) {
150 if ( $kit->is_autosave() ) {
151 $css_file = Post_Preview::create( $kit->get_id() );
152 } else {
153 $css_file = Post_CSS::create( $kit->get_id() );
154 }
155
156 $css_file->enqueue();
157 }
158 }
159
160 public function render_panel_html() {
161 require __DIR__ . '/views/panel.php';
162 }
163
164 public function get_kit_for_frontend() {
165 $kit = false;
166 $active_kit = $this->get_active_kit();
167 $is_kit_preview = is_preview() && isset( $_GET['preview_id'] ) && $active_kit->get_main_id() === (int) $_GET['preview_id'];
168
169 if ( $is_kit_preview ) {
170 $kit = Plugin::$instance->documents->get_doc_or_auto_save( $active_kit->get_main_id(), get_current_user_id() );
171 } elseif ( 'publish' === $active_kit->get_main_post()->post_status ) {
172 $kit = $active_kit;
173 }
174
175 return $kit;
176 }
177
178 public function update_kit_settings_based_on_option( $key, $value ) {
179 /** @var Kit $active_kit */
180 $active_kit = $this->get_active_kit();
181
182 if ( $active_kit->is_saving() ) {
183 return;
184 }
185
186 $active_kit->update_settings( [ $key => $value ] );
187 }
188
189 /**
190 * Map Scheme To Global
191 *
192 * Convert a given scheme value to its corresponding default global value
193 *
194 * @param string $type 'color'/'typography'
195 * @param $value
196 * @return mixed
197 */
198 private function map_scheme_to_global( $type, $value ) {
199 $schemes_to_globals_map = [
200 'color' => [
201 '1' => Global_Colors::COLOR_PRIMARY,
202 '2' => Global_Colors::COLOR_SECONDARY,
203 '3' => Global_Colors::COLOR_TEXT,
204 '4' => Global_Colors::COLOR_ACCENT,
205 ],
206 'typography' => [
207 '1' => Global_Typography::TYPOGRAPHY_PRIMARY,
208 '2' => Global_Typography::TYPOGRAPHY_SECONDARY,
209 '3' => Global_Typography::TYPOGRAPHY_TEXT,
210 '4' => Global_Typography::TYPOGRAPHY_ACCENT,
211 ],
212 ];
213
214 return $schemes_to_globals_map[ $type ][ $value ];
215 }
216
217 /**
218 * Convert Scheme to Default Global
219 *
220 * If a control has a scheme property, convert it to a default Global.
221 *
222 * @param $scheme - Control scheme property
223 * @return array - Control/group control args
224 * @since 3.0.0
225 * @access public
226 */
227 public function convert_scheme_to_global( $scheme ) {
228 if ( isset( $scheme['type'] ) && isset( $scheme['value'] ) ) {
229 //_deprecated_argument( $args['scheme'], '3.0.0', 'Schemes are now deprecated - use $args[\'global\'] instead.' );
230 return $this->map_scheme_to_global( $scheme['type'], $scheme['value'] );
231 }
232
233 // Typography control 'scheme' properties usually only include the string with the typography value ('1'-'4').
234 return $this->map_scheme_to_global( 'typography', $scheme );
235 }
236
237 public function register_controls() {
238 $controls_manager = Plugin::$instance->controls_manager;
239
240 $controls_manager->register_control( Repeater::CONTROL_TYPE, new Repeater() );
241 }
242
243 public function is_custom_colors_enabled() {
244 return ! get_option( 'elementor_disable_color_schemes' );
245 }
246
247 public function is_custom_typography_enabled() {
248 return ! get_option( 'elementor_disable_typography_schemes' );
249 }
250
251 /**
252 * Add kit wrapper body class.
253 *
254 * It should be added even for non Elementor pages,
255 * in order to support embedded templates.
256 */
257 private function add_body_class() {
258 $kit = $this->get_kit_for_frontend();
259
260 if ( $kit ) {
261 Plugin::$instance->frontend->add_body_class( 'elementor-kit-' . $kit->get_main_id() );
262 }
263 }
264
265 public function __construct() {
266 add_action( 'elementor/documents/register', [ $this, 'register_document' ] );
267 add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] );
268 add_filter( 'elementor/editor/footer', [ $this, 'render_panel_html' ] );
269 add_action( 'elementor/frontend/after_enqueue_styles', [ $this, 'frontend_before_enqueue_styles' ], 0 );
270 add_action( 'elementor/preview/enqueue_styles', [ $this, 'preview_enqueue_styles' ], 0 );
271 add_action( 'elementor/controls/controls_registered', [ $this, 'register_controls' ] );
272
273 add_action( 'update_option_blogname', function ( $old_value, $value ) {
274 $this->update_kit_settings_based_on_option( 'site_name', $value );
275 }, 10, 2 );
276
277 add_action( 'update_option_blogdescription', function ( $old_value, $value ) {
278 $this->update_kit_settings_based_on_option( 'site_description', $value );
279 }, 10, 2 );
280
281 add_action( 'wp_head', function() {
282 $this->add_body_class();
283 } );
284 }
285 }
286