PluginProbe
Elementor Website Builder – more than just a page builder / 3.2.5
Elementor Website Builder – more than just a page builder v3.2.5
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.2.5, at core/kits/manager.php

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