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