post.php (6881B)
1 <?php 2 namespace Elementor\Core\Files\CSS; 3 4 use Elementor\Base_Data_Control; 5 use Elementor\Control_Repeater; 6 use Elementor\Controls_Stack; 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 $element_settings = $element->get_settings(); 297 298 $this->add_controls_stack_style_rules( $element, $this->get_style_controls( $element, null, $element->get_parsed_dynamic_settings() ), $element_settings, [ '{{ID}}', '{{WRAPPER}}' ], [ $element->get_id(), $this->get_element_unique_selector( $element ) ] ); 299 300 /** 301 * After element parse CSS. 302 * 303 * Fires after the CSS of the element is parsed. 304 * 305 * @since 1.2.0 306 * 307 * @param Post $this The post CSS file. 308 * @param Element_Base $element The element. 309 */ 310 do_action( 'elementor/element/parse_css', $this, $element ); 311 } 312 }