| 1 |
<?php |
| 2 |
namespace Elementor\Core\Breakpoints; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Module; |
| 5 |
use Elementor\Core\Kits\Documents\Tabs\Settings_Layout; |
| 6 |
use Elementor\Core\Responsive\Files\Frontend; |
| 7 |
use Elementor\Plugin; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Manager extends Module { |
| 14 |
|
| 15 |
const BREAKPOINT_SETTING_PREFIX = 'viewport_'; |
| 16 |
const BREAKPOINT_KEY_MOBILE = 'mobile'; |
| 17 |
const BREAKPOINT_KEY_MOBILE_EXTRA = 'mobile_extra'; |
| 18 |
const BREAKPOINT_KEY_TABLET = 'tablet'; |
| 19 |
const BREAKPOINT_KEY_TABLET_EXTRA = 'tablet_extra'; |
| 20 |
const BREAKPOINT_KEY_LAPTOP = 'laptop'; |
| 21 |
const BREAKPOINT_KEY_WIDESCREEN = 'widescreen'; |
| 22 |
|
| 23 |
private $breakpoints; |
| 24 |
private $active_breakpoints; |
| 25 |
|
| 26 |
public function get_name() { |
| 27 |
return 'breakpoints'; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get Breakpoints |
| 32 |
* |
| 33 |
* Retrieve the array containing instances of all breakpoints existing in the system, or a single breakpoint if a |
| 34 |
* name is passed. |
| 35 |
* |
| 36 |
* @since 3.2.0 |
| 37 |
* |
| 38 |
* @param $breakpoint_name |
| 39 |
* @return Breakpoint[]|Breakpoint |
| 40 |
*/ |
| 41 |
public function get_breakpoints( $breakpoint_name = null ) { |
| 42 |
if ( ! $this->breakpoints ) { |
| 43 |
$this->init_breakpoints(); |
| 44 |
} |
| 45 |
return self::get_items( $this->breakpoints, $breakpoint_name ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get Active Breakpoints |
| 50 |
* |
| 51 |
* Retrieve the array of --enabled-- breakpoints, or a single breakpoint if a name is passed. |
| 52 |
* |
| 53 |
* @since 3.2.0 |
| 54 |
* |
| 55 |
* @param string|null $breakpoint_name |
| 56 |
* @return Breakpoint[]|Breakpoint |
| 57 |
*/ |
| 58 |
public function get_active_breakpoints( $breakpoint_name = null ) { |
| 59 |
if ( ! $this->active_breakpoints ) { |
| 60 |
$this->init_active_breakpoints(); |
| 61 |
} |
| 62 |
|
| 63 |
return self::get_items( $this->active_breakpoints, $breakpoint_name ); |
| 64 |
} |
| 65 |
|
| 66 |
/** Has Custom Breakpoints |
| 67 |
* |
| 68 |
* Checks whether there are currently custom breakpoints saved in the database. |
| 69 |
* Returns true if there are breakpoint values saved in the active kit. |
| 70 |
* |
| 71 |
* @since 3.2.0 |
| 72 |
* |
| 73 |
* @return boolean |
| 74 |
*/ |
| 75 |
public function has_custom_breakpoints() { |
| 76 |
$breakpoints = $this->get_breakpoints(); |
| 77 |
|
| 78 |
foreach ( $breakpoints as $breakpoint ) { |
| 79 |
/** @var Breakpoint $breakpoint */ |
| 80 |
if ( $breakpoint->is_custom() ) { |
| 81 |
return true; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get Device Min Breakpoint |
| 90 |
* |
| 91 |
* For a given device, return the minimum possible breakpoint. Except for the cases of mobile and widescreen |
| 92 |
* devices, A device's min breakpoint is determined by the previous device's max breakpoint + 1px. |
| 93 |
* |
| 94 |
* @param string $device_name |
| 95 |
* @return int the min breakpoint of the passed device |
| 96 |
*@since 3.2.0 |
| 97 |
*/ |
| 98 |
public function get_device_min_breakpoint( $device_name ) { |
| 99 |
if ( 'desktop' === $device_name ) { |
| 100 |
return $this->get_desktop_min_point(); |
| 101 |
} |
| 102 |
|
| 103 |
$active_breakpoints = $this->get_active_breakpoints(); |
| 104 |
$current_device_breakpoint = $active_breakpoints[ $device_name ]; |
| 105 |
|
| 106 |
// Since this method is called multiple times, usage of class variables is to memory and processing time. |
| 107 |
// Get only the keys for active breakpoints. |
| 108 |
$breakpoint_keys = array_keys( $active_breakpoints ); |
| 109 |
|
| 110 |
if ( $breakpoint_keys[0] === $device_name ) { |
| 111 |
// For the lowest breakpoint, the min point is always 320. |
| 112 |
$min_breakpoint = 320; |
| 113 |
} elseif ( 'min' === $current_device_breakpoint->get_direction() ) { |
| 114 |
// 'min-width' breakpoints only have a minimum point. The breakpoint value itself the device min point. |
| 115 |
$min_breakpoint = $current_device_breakpoint->get_value(); |
| 116 |
} else { |
| 117 |
// This block handles all other devices. |
| 118 |
$device_name_index = array_search( $device_name, $breakpoint_keys, true ); |
| 119 |
|
| 120 |
$previous_index = $device_name_index - 1; |
| 121 |
$previous_breakpoint_key = $breakpoint_keys[ $previous_index ]; |
| 122 |
/** @var Breakpoint $previous_breakpoint */ |
| 123 |
$previous_breakpoint = $active_breakpoints[ $previous_breakpoint_key ]; |
| 124 |
|
| 125 |
$min_breakpoint = $previous_breakpoint->get_value() + 1; |
| 126 |
} |
| 127 |
|
| 128 |
return $min_breakpoint; |
| 129 |
} |
| 130 |
|
| 131 |
public function get_desktop_min_point() { |
| 132 |
$active_breakpoints = $this->get_active_breakpoints(); |
| 133 |
$desktop_previous_device = $this->get_desktop_previous_device_key(); |
| 134 |
|
| 135 |
return $active_breakpoints[ $desktop_previous_device ]->get_value() + 1; |
| 136 |
} |
| 137 |
|
| 138 |
public function refresh() { |
| 139 |
$this->init_breakpoints(); |
| 140 |
$this->init_active_breakpoints(); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get Default Config |
| 145 |
* |
| 146 |
* Retrieve the default breakpoints config array. The 'selector' property is used for CSS generation (the |
| 147 |
* Stylesheet::add_device() method). |
| 148 |
* |
| 149 |
* @return array |
| 150 |
*/ |
| 151 |
public static function get_default_config() { |
| 152 |
return [ |
| 153 |
self::BREAKPOINT_KEY_MOBILE => [ |
| 154 |
'label' => __( 'Mobile', 'elementor' ), |
| 155 |
'default_value' => 767, |
| 156 |
'direction' => 'max', |
| 157 |
], |
| 158 |
self::BREAKPOINT_KEY_MOBILE_EXTRA => [ |
| 159 |
'label' => __( 'Mobile Extra', 'elementor' ), |
| 160 |
'default_value' => 880, |
| 161 |
'direction' => 'max', |
| 162 |
], |
| 163 |
self::BREAKPOINT_KEY_TABLET => [ |
| 164 |
'label' => __( 'Tablet', 'elementor' ), |
| 165 |
'default_value' => 1024, |
| 166 |
'direction' => 'max', |
| 167 |
], |
| 168 |
self::BREAKPOINT_KEY_TABLET_EXTRA => [ |
| 169 |
'label' => __( 'Tablet Extra', 'elementor' ), |
| 170 |
'default_value' => 1365, |
| 171 |
'direction' => 'max', |
| 172 |
], |
| 173 |
self::BREAKPOINT_KEY_LAPTOP => [ |
| 174 |
'label' => __( 'Laptop', 'elementor' ), |
| 175 |
'default_value' => 1620, |
| 176 |
'direction' => 'max', |
| 177 |
], |
| 178 |
self::BREAKPOINT_KEY_WIDESCREEN => [ |
| 179 |
'label' => __( 'Widescreen', 'elementor' ), |
| 180 |
'default_value' => 2400, |
| 181 |
'direction' => 'min', |
| 182 |
], |
| 183 |
]; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Get Stylesheet Templates Path |
| 188 |
* |
| 189 |
* @since 3.2.0 |
| 190 |
* @access public |
| 191 |
* @static |
| 192 |
*/ |
| 193 |
public static function get_stylesheet_templates_path() { |
| 194 |
return ELEMENTOR_ASSETS_PATH . 'css/templates/'; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Compile Stylesheet Templates |
| 199 |
* |
| 200 |
* @since 3.2.0 |
| 201 |
* @access public |
| 202 |
* @static |
| 203 |
*/ |
| 204 |
public static function compile_stylesheet_templates() { |
| 205 |
foreach ( self::get_stylesheet_templates() as $file_name => $template_path ) { |
| 206 |
$file = new Frontend( $file_name, $template_path ); |
| 207 |
|
| 208 |
$file->update(); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Init Breakpoints |
| 214 |
* |
| 215 |
* Creates the breakpoints array, containing instances of each breakpoint. Returns an array of ALL breakpoints, |
| 216 |
* both enabled and disabled. |
| 217 |
* |
| 218 |
* @since 3.2.0 |
| 219 |
*/ |
| 220 |
private function init_breakpoints() { |
| 221 |
$breakpoints = []; |
| 222 |
$kit = Plugin::$instance->kits_manager->get_active_kit_for_frontend(); |
| 223 |
$active_breakpoint_keys = $kit->get_settings( Settings_Layout::ACTIVE_BREAKPOINTS_CONTROL_ID ); |
| 224 |
$default_config = self::get_default_config(); |
| 225 |
$prefix = self::BREAKPOINT_SETTING_PREFIX; |
| 226 |
|
| 227 |
foreach ( $default_config as $breakpoint_name => $breakpoint_config ) { |
| 228 |
$args = [ 'name' => $breakpoint_name ] + $breakpoint_config; |
| 229 |
|
| 230 |
// Make sure the two default breakpoints (mobile, tablet) are always enabled. |
| 231 |
if ( self::BREAKPOINT_KEY_MOBILE === $breakpoint_name || self::BREAKPOINT_KEY_TABLET === $breakpoint_name ) { |
| 232 |
// Make sure the default Mobile and Tablet breakpoints are always enabled. |
| 233 |
$args['is_enabled'] = true; |
| 234 |
} else { |
| 235 |
// If the breakpoint is in the active breakpoints array, make sure it's instantiated as enabled. |
| 236 |
$args['is_enabled'] = in_array( $prefix . $breakpoint_name, $active_breakpoint_keys, true ); |
| 237 |
} |
| 238 |
|
| 239 |
$breakpoints[ $breakpoint_name ] = new Breakpoint( $args ); |
| 240 |
} |
| 241 |
|
| 242 |
$this->breakpoints = $breakpoints; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Init Active Breakpoints |
| 247 |
* |
| 248 |
* Create/Refresh the array of --enabled-- breakpoints. |
| 249 |
* |
| 250 |
* @since 3.2.0 |
| 251 |
*/ |
| 252 |
private function init_active_breakpoints() { |
| 253 |
$this->active_breakpoints = array_filter( $this->get_breakpoints(), function( $breakpoint ) { |
| 254 |
/** @var Breakpoint $breakpoint */ |
| 255 |
return $breakpoint->is_enabled(); |
| 256 |
} ); |
| 257 |
} |
| 258 |
|
| 259 |
private function get_desktop_previous_device_key() { |
| 260 |
$config_array_keys = array_keys( $this->get_active_breakpoints() ); |
| 261 |
$num_of_devices = count( $config_array_keys ); |
| 262 |
|
| 263 |
// If the widescreen breakpoint is active, the device that's previous to desktop is the last one before |
| 264 |
// widescreen. |
| 265 |
if ( self::BREAKPOINT_KEY_WIDESCREEN === $config_array_keys[ $num_of_devices - 1 ] ) { |
| 266 |
$desktop_previous_device = $config_array_keys[ $num_of_devices - 2 ]; |
| 267 |
} else { |
| 268 |
// If the widescreen breakpoint isn't active, we just take the last device returned by the config. |
| 269 |
$desktop_previous_device = $config_array_keys[ $num_of_devices - 1 ]; |
| 270 |
} |
| 271 |
|
| 272 |
return $desktop_previous_device; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Get Stylesheet Templates |
| 277 |
* |
| 278 |
* @since 3.2.0 |
| 279 |
* @access private |
| 280 |
* @static |
| 281 |
*/ |
| 282 |
private static function get_stylesheet_templates() { |
| 283 |
$templates_paths = glob( self::get_stylesheet_templates_path() . '*.css' ); |
| 284 |
|
| 285 |
$templates = []; |
| 286 |
|
| 287 |
foreach ( $templates_paths as $template_path ) { |
| 288 |
$file_name = 'custom-' . basename( $template_path ); |
| 289 |
|
| 290 |
$templates[ $file_name ] = $template_path; |
| 291 |
} |
| 292 |
|
| 293 |
$deprecated_hook = 'elementor/core/responsive/get_stylesheet_templates'; |
| 294 |
$replacement_hook = 'elementor/core/breakpoints/get_stylesheet_template'; |
| 295 |
|
| 296 |
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_hook( $deprecated_hook, '3.2.0', $replacement_hook ); |
| 297 |
|
| 298 |
// TODO: REMOVE THIS DEPRECATED HOOK IN ELEMENTOR v3.10.0/v4.0.0 |
| 299 |
$templates = apply_filters( $deprecated_hook, $templates ); |
| 300 |
|
| 301 |
return apply_filters( $replacement_hook, $templates ); |
| 302 |
} |
| 303 |
} |
| 304 |
|