PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.2
Elementor Website Builder – more than just a page builder v4.3.2
4.3.2 4.3.1 4.3.0 4.3.0-beta3 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 All 455 releases
elementor / core / files / css / post.php

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

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