frontend.php (4942B)
1 <?php 2 3 namespace Elementor\Core\Responsive\Files; 4 5 use Elementor\Core\Breakpoints\Breakpoint; 6 use Elementor\Core\Files\Base; 7 use Elementor\Core\Responsive\Responsive; 8 use Elementor\Plugin; 9 10 if ( ! defined( 'ABSPATH' ) ) { 11 exit; // Exit if accessed directly 12 } 13 14 class Frontend extends Base { 15 16 const META_KEY = 'elementor-custom-breakpoints-files'; 17 18 private $template_file; 19 20 /** 21 * @since 2.1.0 22 * @access public 23 */ 24 public function __construct( $file_name, $template_file = null ) { 25 $this->template_file = $template_file; 26 27 parent::__construct( $file_name ); 28 } 29 30 /** 31 * @since 2.1.0 32 * @access public 33 */ 34 public function parse_content() { 35 $breakpoints = Plugin::$instance->breakpoints->get_active_breakpoints(); 36 37 $breakpoints_keys = array_keys( $breakpoints ); 38 39 $file_content = file_get_contents( $this->template_file ); 40 41 // The regex pattern parses placeholders located in the frontend _templates.scss file. 42 $file_content = preg_replace_callback( '/ELEMENTOR_SCREEN_([A-Z_]+)(?:_(MIN|MAX|NEXT))/', function ( $placeholder_data ) use ( $breakpoints_keys, $breakpoints ) { 43 // Handle BC for legacy template files and Elementor Pro builds. 44 $placeholder_data = $this->maybe_convert_placeholder_data( $placeholder_data ); 45 46 $breakpoint_index = array_search( strtolower( $placeholder_data[1] ), $breakpoints_keys, true ); 47 48 if ( 'DESKTOP' === $placeholder_data[1] ) { 49 if ( 'MIN' === $placeholder_data[2] ) { 50 $value = Plugin::$instance->breakpoints->get_desktop_min_point(); 51 } elseif ( isset( $breakpoints['widescreen'] ) ) { 52 // If the 'widescreen' breakpoint is active, the Desktop's max value is the Widescreen breakpoint - 1px. 53 $value = $breakpoints['widescreen']->get_value() - 1; 54 } else { 55 // If the 'widescreen' breakpoint is not active, the Desktop device should not have a max value. 56 $value = 99999; 57 } 58 } elseif ( false === $breakpoint_index ) { 59 // If the breakpoint in the placeholder is not active - use a -1 value for the media query, to make 60 // sure the setting is printed (to avoid a PHP error) but doesn't apply. 61 $value = -1; 62 } elseif ( 'WIDESCREEN' === $placeholder_data[1] ) { 63 $value = $breakpoints['widescreen']->get_value(); 64 } else { 65 $breakpoint_index = array_search( strtolower( $placeholder_data[1] ), $breakpoints_keys, true ); 66 67 $is_max_point = 'MAX' === $placeholder_data[2]; 68 69 // If the placeholder capture is `MOBILE_NEXT` or `TABLET_NEXT`, the original breakpoint value is used. 70 if ( ! $is_max_point && 'NEXT' !== $placeholder_data[2] ) { 71 $breakpoint_index--; 72 } 73 74 $value = $breakpoints[ $breakpoints_keys[ $breakpoint_index ] ]->get_value(); 75 76 if ( ! $is_max_point ) { 77 $value++; 78 } 79 } 80 81 return $value . 'px'; 82 }, $file_content ); 83 84 return $file_content; 85 } 86 87 /** 88 * Load meta. 89 * 90 * Retrieve the file meta data. 91 * 92 * @since 2.1.0 93 * @access protected 94 */ 95 protected function load_meta() { 96 $option = $this->load_meta_option(); 97 98 $file_meta_key = $this->get_file_meta_key(); 99 100 if ( empty( $option[ $file_meta_key ] ) ) { 101 return []; 102 } 103 104 return $option[ $file_meta_key ]; 105 } 106 107 /** 108 * Update meta. 109 * 110 * Update the file meta data. 111 * 112 * @since 2.1.0 113 * @access protected 114 * 115 * @param array $meta New meta data. 116 */ 117 protected function update_meta( $meta ) { 118 $option = $this->load_meta_option(); 119 120 $option[ $this->get_file_meta_key() ] = $meta; 121 122 update_option( static::META_KEY, $option ); 123 } 124 125 /** 126 * Delete meta. 127 * 128 * Delete the file meta data. 129 * 130 * @since 2.1.0 131 * @access protected 132 */ 133 protected function delete_meta() { 134 $option = $this->load_meta_option(); 135 136 $file_meta_key = $this->get_file_meta_key(); 137 138 if ( isset( $option[ $file_meta_key ] ) ) { 139 unset( $option[ $file_meta_key ] ); 140 } 141 142 if ( $option ) { 143 update_option( static::META_KEY, $option ); 144 } else { 145 delete_option( static::META_KEY ); 146 } 147 } 148 149 /** 150 * @since 2.1.0 151 * @access private 152 */ 153 private function get_file_meta_key() { 154 return pathinfo( $this->get_file_name(), PATHINFO_FILENAME ); 155 } 156 157 /** 158 * @since 2.1.0 159 * @access private 160 */ 161 private function load_meta_option() { 162 $option = get_option( static::META_KEY ); 163 164 if ( ! $option ) { 165 $option = []; 166 } 167 168 return $option; 169 } 170 171 /** 172 * Maybe Convert Placeholder Data 173 * 174 * Converts responsive placeholders in Elementor CSS template files from the legacy format into the new format. 175 * Used for backwards compatibility for old Pro versions that were built with an Elementor Core version <3.2.0. 176 * 177 * @since 3.2.3 178 * 179 * @param $placeholder_data 180 * @return mixed 181 */ 182 private function maybe_convert_placeholder_data( $placeholder_data ) { 183 switch ( $placeholder_data[1] ) { 184 case 'SM': 185 $placeholder_data[1] = 'MOBILE'; 186 break; 187 case 'MD': 188 $placeholder_data[1] = 'TABLET'; 189 break; 190 case 'LG': 191 $placeholder_data[1] = 'DESKTOP'; 192 } 193 194 return $placeholder_data; 195 } 196 }