PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.2
Elementor Website Builder – more than just a page builder v4.2.2
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 / files / css / post.php

post.php in Elementor Website Builder – more than just a page builder 4.2.2, at core/files/css/post.php

387 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\Files\CSS;
3
4 use Elementor\Controls_Stack;
5 use Elementor\Core\DynamicTags\Dynamic_CSS;
6 use Elementor\Core\Kits\Manager;
7 use Elementor\Element_Base;
8 use Elementor\Plugin;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 /**
15 * Elementor post CSS file.
16 *
17 * Elementor CSS file handler class is responsible for generating the single
18 * post CSS file.
19 *
20 * @since 1.2.0
21 */
22 class Post extends Base {
23
24 /**
25 * Elementor post CSS file prefix.
26 */
27 const FILE_PREFIX = 'post-';
28
29 const META_KEY = '_elementor_css';
30
31 /**
32 * Post ID.
33 *
34 * Holds the current post ID.
35 *
36 * @var int
37 */
38 private $post_id;
39
40 protected function is_global_parsing_supported() {
41 return true;
42 }
43
44 /**
45 * Post CSS file constructor.
46 *
47 * Initializing the CSS file of the post. Set the post ID and initiate the stylesheet.
48 *
49 * @since 1.2.0
50 * @access public
51 *
52 * @param int $post_id Post ID.
53 */
54 public function __construct( $post_id ) {
55 $this->post_id = $post_id;
56
57 parent::__construct( static::FILE_PREFIX . $post_id . '.css' );
58 }
59
60 /**
61 * Get CSS file name.
62 *
63 * Retrieve the CSS file name.
64 *
65 * @since 1.6.0
66 * @access public
67 *
68 * @return string CSS file name.
69 */
70 public function get_name() {
71 return 'post';
72 }
73
74 /**
75 * Get post ID.
76 *
77 * Retrieve the ID of current post.
78 *
79 * @since 1.2.0
80 * @access public
81 *
82 * @return int Post ID.
83 */
84 public function get_post_id() {
85 return $this->post_id;
86 }
87
88 /**
89 * Get unique element selector.
90 *
91 * Retrieve the unique selector for any given element.
92 *
93 * @since 1.2.0
94 * @access public
95 *
96 * @param Element_Base $element The element.
97 *
98 * @return string Unique element selector.
99 */
100 public function get_element_unique_selector( Element_Base $element ) {
101 return '.elementor-' . $this->post_id . ' .elementor-element' . $element->get_unique_selector();
102 }
103
104 /**
105 * Load meta data.
106 *
107 * Retrieve the post CSS file meta data.
108 *
109 * @since 1.2.0
110 * @access protected
111 *
112 * @return array Post CSS file meta data.
113 */
114 protected function load_meta() {
115 return get_post_meta( $this->post_id, static::META_KEY, true );
116 }
117
118 /**
119 * Update meta data.
120 *
121 * Update the global CSS file meta data.
122 *
123 * @since 1.2.0
124 * @access protected
125 *
126 * @param array $meta New meta data.
127 */
128 protected function update_meta( $meta ) {
129 update_post_meta( $this->post_id, static::META_KEY, $meta );
130 }
131
132 /**
133 * Delete meta.
134 *
135 * Delete the file meta data.
136 *
137 * @since 2.1.0
138 * @access protected
139 */
140 protected function delete_meta() {
141 delete_post_meta( $this->post_id, static::META_KEY );
142 }
143
144 /**
145 * Get post data.
146 *
147 * Retrieve raw post data from the database.
148 *
149 * @since 1.9.0
150 * @access protected
151 *
152 * @return array Post data.
153 */
154 protected function get_data() {
155 $document = Plugin::$instance->documents->get( $this->post_id );
156 return $document ? $document->get_elements_data() : [];
157 }
158
159 /**
160 * Render CSS.
161 *
162 * Parse the CSS for all the elements.
163 *
164 * @since 1.2.0
165 * @access protected
166 */
167 protected function render_css() {
168 $data = $this->get_data();
169
170 if ( ! empty( $data ) ) {
171 foreach ( $data as $element_data ) {
172 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
173
174 if ( ! $element ) {
175 continue;
176 }
177
178 $this->render_styles( $element );
179 }
180 }
181 }
182
183 /**
184 * Enqueue CSS.
185 *
186 * Enqueue the post CSS file in Elementor.
187 *
188 * This method ensures that the post was actually built with elementor before
189 * enqueueing the post CSS file.
190 *
191 * @since 1.2.2
192 * @access public
193 */
194 public function enqueue() {
195 $document = Plugin::$instance->documents->get( $this->post_id );
196
197 if ( ! $document || ! $document->is_built_with_elementor() ) {
198 return;
199 }
200
201 parent::enqueue();
202 }
203
204 /**
205 * Add controls-stack style rules.
206 *
207 * Parse the CSS for all the elements inside any given controls stack.
208 *
209 * This method recursively renders the CSS for all the child elements in the stack.
210 *
211 * @since 1.6.0
212 * @access public
213 *
214 * @param Controls_Stack $controls_stack The controls stack.
215 * @param array $controls Controls array.
216 * @param array $values Values array.
217 * @param array $placeholders Placeholders.
218 * @param array $replacements Replacements.
219 * @param array $all_controls All controls.
220 */
221 public function add_controls_stack_style_rules( Controls_Stack $controls_stack, array $controls, array $values, array $placeholders, array $replacements, ?array $all_controls = null ) {
222 parent::add_controls_stack_style_rules( $controls_stack, $controls, $values, $placeholders, $replacements, $all_controls );
223
224 if ( $controls_stack instanceof Element_Base ) {
225 foreach ( $controls_stack->get_children() as $child_element ) {
226 $this->render_styles( $child_element );
227 }
228 }
229 }
230
231 /**
232 * Get enqueue dependencies.
233 *
234 * Retrieve the name of the stylesheet used by `wp_enqueue_style()`.
235 *
236 * @since 1.2.0
237 * @access protected
238 *
239 * @return array Name of the stylesheet.
240 */
241 protected function get_enqueue_dependencies() {
242 return [ 'elementor-frontend' ];
243 }
244
245 /**
246 * Get inline dependency.
247 *
248 * Retrieve the name of the stylesheet used by `wp_add_inline_style()`.
249 *
250 * @since 1.2.0
251 * @access protected
252 *
253 * @return string Name of the stylesheet.
254 */
255 protected function get_inline_dependency() {
256 return 'elementor-frontend';
257 }
258
259 /**
260 * Get file handle ID.
261 *
262 * Retrieve the handle ID for the post CSS file.
263 *
264 * @since 1.2.0
265 * @access protected
266 *
267 * @return string CSS file handle ID.
268 */
269 protected function get_file_handle_id() {
270 return 'elementor-post-' . $this->post_id;
271 }
272
273 /**
274 * Render styles.
275 *
276 * Parse the CSS for any given element.
277 *
278 * @since 1.2.0
279 * @access protected
280 *
281 * @param Element_Base $element The element.
282 */
283 protected function render_styles( Element_Base $element ) {
284 /**
285 * Before element parse CSS.
286 *
287 * Fires before the CSS of the element is parsed.
288 *
289 * @since 1.2.0
290 *
291 * @param Post $this The post CSS file.
292 * @param Element_Base $element The element.
293 */
294 do_action( 'elementor/element/before_parse_css', $this, $element );
295
296 $this->render_element_global_styles( $element );
297 $this->render_element_styles( $element );
298
299 /**
300 * After element parse CSS.
301 *
302 * Fires after the CSS of the element is parsed.
303 *
304 * @since 1.2.0
305 *
306 * @param Post $this The post CSS file.
307 * @param Element_Base $element The element.
308 */
309 do_action( 'elementor/element/parse_css', $this, $element );
310 }
311
312 private function render_element_styles( Element_Base $element ) {
313 $this->add_controls_stack_style_rules(
314 $element,
315 $this->get_style_controls( $element, null, $element->get_parsed_dynamic_settings() ),
316 $element->get_settings(),
317 [ '{{ID}}', '{{WRAPPER}}' ],
318 [ $element->get_id(), $this->get_element_unique_selector( $element ) ]
319 );
320 }
321
322 private function render_element_global_styles( Element_Base $element ) {
323 if ( $this instanceof Dynamic_CSS ) {
324 return;
325 }
326
327 /** @var Manager $module */
328 $kits_manager = Plugin::$instance->kits_manager;
329 $custom_colors_enabled = $kits_manager->is_custom_colors_enabled();
330 $custom_typography_enabled = $kits_manager->is_custom_typography_enabled();
331
332 $controls = $element->get_controls();
333 $global_controls = [];
334 $global_values['__globals__'] = [];
335
336 foreach ( $controls as $control ) {
337 $this->build_global_controls_and_values(
338 $control,
339 $controls,
340 $global_controls,
341 $global_values,
342 $custom_colors_enabled,
343 $custom_typography_enabled
344 );
345 }
346
347 foreach ( $global_controls as $control ) {
348 $this->add_control_rules(
349 $control,
350 $controls,
351 function( $control ) {},
352 [ '{{WRAPPER}}' ],
353 [ '.elementor-widget-' . $element->get_name() ],
354 $global_values
355 );
356 }
357 }
358
359 private function build_global_controls_and_values( $control, $controls, &$global_controls, &$global_values, $custom_colors_enabled, $custom_typography_enabled ) {
360 $is_color_control = 'color' === $control['type'];
361 $is_typography_control = isset( $control['groupType'] ) && 'typography' === $control['groupType'];
362
363 // If it is a color/typography control and default colors/typography are disabled,
364 // don't add the default CSS.
365 if ( ( $is_color_control && ! $custom_colors_enabled ) || ( $is_typography_control && ! $custom_typography_enabled ) ) {
366 return;
367 }
368
369 $global_control = $control;
370
371 // Handle group controls that don't have a default global property.
372 if ( ! empty( $control['groupType'] ) ) {
373 $global_control = $controls[ $control['groupPrefix'] . $control['groupType'] ];
374 }
375
376 // If the control has a default global defined, add it to the globals array
377 // that is used in add_control_rules.
378 if ( ! empty( $control['global']['default'] ) ) {
379 $global_values['__globals__'][ $control['name'] ] = $global_control['global']['default'];
380 }
381
382 if ( ! empty( $global_control['global']['default'] ) ) {
383 $global_controls[] = $control;
384 }
385 }
386 }
387