PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta1
Elementor Website Builder – more than just a page builder v4.3.0-beta1
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.3.0-beta1, at core/files/css/post.php

392 lines 9.1 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 update_post_meta( $this->post_id, static::META_KEY, $meta );
131 }
132
133 /**
134 * Delete meta.
135 *
136 * Delete the file meta data.
137 *
138 * @since 2.1.0
139 * @access protected
140 */
141 protected function delete_meta() {
142 delete_post_meta( $this->post_id, static::META_KEY );
143 }
144
145 /**
146 * Get post data.
147 *
148 * Retrieve raw post data from the database.
149 *
150 * @since 1.9.0
151 * @access protected
152 *
153 * @return array Post data.
154 */
155 protected function get_data() {
156 $document = Plugin::$instance->documents->get( $this->post_id );
157 return $document ? $document->get_elements_data() : [];
158 }
159
160 /**
161 * Render CSS.
162 *
163 * Parse the CSS for all the elements.
164 *
165 * @since 1.2.0
166 * @access protected
167 */
168 protected function render_css() {
169 $data = $this->get_data();
170
171 if ( ! empty( $data ) ) {
172 foreach ( $data as $element_data ) {
173 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
174
175 if ( ! $element ) {
176 continue;
177 }
178
179 $this->render_styles( $element );
180 }
181 }
182 }
183
184 /**
185 * Enqueue CSS.
186 *
187 * Enqueue the post CSS file in Elementor.
188 *
189 * This method ensures that the post was actually built with elementor before
190 * enqueueing the post CSS file.
191 *
192 * @since 1.2.2
193 * @access public
194 */
195 public function enqueue() {
196 $document = Plugin::$instance->documents->get( $this->post_id );
197
198 if ( ! $document || ! $document->is_built_with_elementor() ) {
199 return;
200 }
201
202 parent::enqueue();
203 }
204
205 /**
206 * Add controls-stack style rules.
207 *
208 * Parse the CSS for all the elements inside any given controls stack.
209 *
210 * This method recursively renders the CSS for all the child elements in the stack.
211 *
212 * @since 1.6.0
213 * @access public
214 *
215 * @param Controls_Stack $controls_stack The controls stack.
216 * @param array $controls Controls array.
217 * @param array $values Values array.
218 * @param array $placeholders Placeholders.
219 * @param array $replacements Replacements.
220 * @param array $all_controls All controls.
221 */
222 public function add_controls_stack_style_rules( Controls_Stack $controls_stack, array $controls, array $values, array $placeholders, array $replacements, ?array $all_controls = null ) {
223 parent::add_controls_stack_style_rules( $controls_stack, $controls, $values, $placeholders, $replacements, $all_controls );
224
225 if ( $controls_stack instanceof Element_Base ) {
226 foreach ( $controls_stack->get_children() as $child_element ) {
227 $this->render_styles( $child_element );
228 }
229 }
230 }
231
232 /**
233 * Get enqueue dependencies.
234 *
235 * Retrieve the name of the stylesheet used by `wp_enqueue_style()`.
236 *
237 * @since 1.2.0
238 * @access protected
239 *
240 * @return array Name of the stylesheet.
241 */
242 protected function get_enqueue_dependencies() {
243 return [ 'elementor-frontend' ];
244 }
245
246 /**
247 * Get inline dependency.
248 *
249 * Retrieve the name of the stylesheet used by `wp_add_inline_style()`.
250 *
251 * @since 1.2.0
252 * @access protected
253 *
254 * @return string Name of the stylesheet.
255 */
256 protected function get_inline_dependency() {
257 return 'elementor-frontend';
258 }
259
260 /**
261 * Get file handle ID.
262 *
263 * Retrieve the handle ID for the post CSS file.
264 *
265 * @since 1.2.0
266 * @access protected
267 *
268 * @return string CSS file handle ID.
269 */
270 protected function get_file_handle_id() {
271 return 'elementor-post-' . $this->post_id;
272 }
273
274 /**
275 * Render styles.
276 *
277 * Parse the CSS for any given element.
278 *
279 * @since 1.2.0
280 * @access protected
281 *
282 * @param Element_Base $element The element.
283 */
284 protected function render_styles( Element_Base $element ) {
285 if ( Widget_Content_Render_Mode::is( Widget_Content_Render_Mode::MARKDOWN ) ) {
286 return;
287 }
288
289 /**
290 * Before element parse CSS.
291 *
292 * Fires before the CSS of the element is parsed.
293 *
294 * @since 1.2.0
295 *
296 * @param Post $this The post CSS file.
297 * @param Element_Base $element The element.
298 */
299 do_action( 'elementor/element/before_parse_css', $this, $element );
300
301 $this->render_element_global_styles( $element );
302 $this->render_element_styles( $element );
303
304 /**
305 * After element parse CSS.
306 *
307 * Fires after the CSS of the element is parsed.
308 *
309 * @since 1.2.0
310 *
311 * @param Post $this The post CSS file.
312 * @param Element_Base $element The element.
313 */
314 do_action( 'elementor/element/parse_css', $this, $element );
315 }
316
317 private function render_element_styles( Element_Base $element ) {
318 $this->add_controls_stack_style_rules(
319 $element,
320 $this->get_style_controls( $element, null, $element->get_parsed_dynamic_settings() ),
321 $element->get_settings(),
322 [ '{{ID}}', '{{WRAPPER}}' ],
323 [ $element->get_id(), $this->get_element_unique_selector( $element ) ]
324 );
325 }
326
327 private function render_element_global_styles( Element_Base $element ) {
328 if ( $this instanceof Dynamic_CSS ) {
329 return;
330 }
331
332 /** @var Manager $module */
333 $kits_manager = Plugin::$instance->kits_manager;
334 $custom_colors_enabled = $kits_manager->is_custom_colors_enabled();
335 $custom_typography_enabled = $kits_manager->is_custom_typography_enabled();
336
337 $controls = $element->get_controls();
338 $global_controls = [];
339 $global_values['__globals__'] = [];
340
341 foreach ( $controls as $control ) {
342 $this->build_global_controls_and_values(
343 $control,
344 $controls,
345 $global_controls,
346 $global_values,
347 $custom_colors_enabled,
348 $custom_typography_enabled
349 );
350 }
351
352 foreach ( $global_controls as $control ) {
353 $this->add_control_rules(
354 $control,
355 $controls,
356 function( $control ) {},
357 [ '{{WRAPPER}}' ],
358 [ '.elementor-widget-' . $element->get_name() ],
359 $global_values
360 );
361 }
362 }
363
364 private function build_global_controls_and_values( $control, $controls, &$global_controls, &$global_values, $custom_colors_enabled, $custom_typography_enabled ) {
365 $is_color_control = 'color' === $control['type'];
366 $is_typography_control = isset( $control['groupType'] ) && 'typography' === $control['groupType'];
367
368 // If it is a color/typography control and default colors/typography are disabled,
369 // don't add the default CSS.
370 if ( ( $is_color_control && ! $custom_colors_enabled ) || ( $is_typography_control && ! $custom_typography_enabled ) ) {
371 return;
372 }
373
374 $global_control = $control;
375
376 // Handle group controls that don't have a default global property.
377 if ( ! empty( $control['groupType'] ) ) {
378 $global_control = $controls[ $control['groupPrefix'] . $control['groupType'] ];
379 }
380
381 // If the control has a default global defined, add it to the globals array
382 // that is used in add_control_rules.
383 if ( ! empty( $control['global']['default'] ) ) {
384 $global_values['__globals__'][ $control['name'] ] = $global_control['global']['default'];
385 }
386
387 if ( ! empty( $global_control['global']['default'] ) ) {
388 $global_controls[] = $control;
389 }
390 }
391 }
392