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

253 lines 8.0 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 $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 'global_color' => __( 'Global Color', 'elementor' ),
117 'global_fonts' => __( 'Global Fonts', 'elementor' ),
118 'global_colors' => __( 'Global Colors', 'elementor' ),
119 'invalid' => __( 'Invalid', 'elementor' ),
120 'color_cannot_be_deleted' => __( 'System Color can\'t be deleted', 'elementor' ),
121 'font_cannot_be_deleted' => __( 'System Font can\'t be deleted', 'elementor' ),
122 'design_system' => __( 'Design System', 'elementor' ),
123 'buttons' => __( 'Buttons', 'elementor' ),
124 'images' => __( 'Images', 'elementor' ),
125 'form_fields' => __( 'Form Fields', 'elementor' ),
126 'background' => __( 'Background', 'elementor' ),
127 'custom_css' => __( 'Custom CSS', 'elementor' ),
128 'additional_settings' => __( 'Additional Settings', 'elementor' ),
129 'kit_changes_updated' => __( 'Your changes have been updated.', 'elementor' ),
130 'back_to_editor' => __( 'Back to Editor', 'elementor' ),
131 ],
132 ] );
133
134 return $settings;
135 }
136
137 public function preview_enqueue_styles() {
138 $kit = $this->get_kit_for_frontend();
139
140 if ( $kit ) {
141 // On preview, the global style is not enqueued.
142 $this->frontend_before_enqueue_styles();
143
144 Plugin::$instance->frontend->print_fonts_links();
145 }
146 }
147
148 public function frontend_before_enqueue_styles() {
149 $kit = $this->get_kit_for_frontend();
150
151 if ( $kit ) {
152 if ( $kit->is_autosave() ) {
153 $css_file = Post_Preview::create( $kit->get_id() );
154 } else {
155 $css_file = Post_CSS::create( $kit->get_id() );
156 }
157
158 $css_file->enqueue();
159
160 Plugin::$instance->frontend->add_body_class( 'elementor-kit-' . $kit->get_main_id() );
161 }
162 }
163
164 public function render_panel_html() {
165 require __DIR__ . '/views/panel.php';
166 }
167
168 public function get_kit_for_frontend() {
169 $kit = false;
170 $active_kit = $this->get_active_kit();
171 $is_kit_preview = is_preview() && isset( $_GET['preview_id'] ) && $active_kit->get_main_id() === (int) $_GET['preview_id'];
172
173 if ( $is_kit_preview ) {
174 $kit = Plugin::$instance->documents->get_doc_or_auto_save( $active_kit->get_main_id(), get_current_user_id() );
175 } elseif ( 'publish' === $active_kit->get_main_post()->post_status ) {
176 $kit = $active_kit;
177 }
178
179 return $kit;
180 }
181
182 /**
183 * Map Scheme To Global
184 *
185 * Convert a given scheme value to its corresponding default global value
186 *
187 * @param string $type 'color'/'typography'
188 * @param $value
189 * @return mixed
190 */
191 private function map_scheme_to_global( $type, $value ) {
192 $schemes_to_globals_map = [
193 'color' => [
194 '1' => Global_Colors::COLOR_PRIMARY,
195 '2' => Global_Colors::COLOR_SECONDARY,
196 '3' => Global_Colors::COLOR_TEXT,
197 '4' => Global_Colors::COLOR_ACCENT,
198 ],
199 'typography' => [
200 '1' => Global_Typography::TYPOGRAPHY_PRIMARY,
201 '2' => Global_Typography::TYPOGRAPHY_SECONDARY,
202 '3' => Global_Typography::TYPOGRAPHY_TEXT,
203 '4' => Global_Typography::TYPOGRAPHY_ACCENT,
204 ],
205 ];
206
207 return $schemes_to_globals_map[ $type ][ $value ];
208 }
209
210 /**
211 * Convert Scheme to Default Global
212 *
213 * If a control has a scheme property, convert it to a default Global.
214 *
215 * @param $scheme - Control scheme property
216 * @return array - Control/group control args
217 * @since 3.0.0
218 * @access public
219 */
220 public function convert_scheme_to_global( $scheme ) {
221 if ( isset( $scheme['type'] ) && isset( $scheme['value'] ) ) {
222 //_deprecated_argument( $args['scheme'], '3.0.0', 'Schemes are now deprecated - use $args[\'global\'] instead.' );
223 return $this->map_scheme_to_global( $scheme['type'], $scheme['value'] );
224 }
225
226 // Typography control 'scheme' properties usually only include the string with the typography value ('1'-'4').
227 return $this->map_scheme_to_global( 'typography', $scheme );
228 }
229
230 public function register_controls() {
231 $controls_manager = Plugin::$instance->controls_manager;
232
233 $controls_manager->register_control( Repeater::CONTROL_TYPE, new Repeater() );
234 }
235
236 public function is_custom_colors_enabled() {
237 return ! get_option( 'elementor_disable_color_schemes' );
238 }
239
240 public function is_custom_typography_enabled() {
241 return ! get_option( 'elementor_disable_typography_schemes' );
242 }
243
244 public function __construct() {
245 add_action( 'elementor/documents/register', [ $this, 'register_document' ] );
246 add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] );
247 add_filter( 'elementor/editor/footer', [ $this, 'render_panel_html' ] );
248 add_action( 'elementor/frontend/after_enqueue_styles', [ $this, 'frontend_before_enqueue_styles' ], 0 );
249 add_action( 'elementor/preview/enqueue_styles', [ $this, 'preview_enqueue_styles' ], 0 );
250 add_action( 'elementor/controls/controls_registered', [ $this, 'register_controls' ] );
251 }
252 }
253