| 1 |
<?php |
| 2 |
/** |
| 3 |
* Breakpoints |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! class_exists( 'GhostKit_Breakpoints' ) ) { |
| 9 |
/** |
| 10 |
* GhostKit_Breakpoints class |
| 11 |
*/ |
| 12 |
// phpcs:ignore |
| 13 |
class GhostKit_Breakpoints { |
| 14 |
/** |
| 15 |
* Extra Small Default Breakpoint. |
| 16 |
* |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
protected static $default_xs = 576; |
| 20 |
|
| 21 |
/** |
| 22 |
* Mobile Default Breakpoint. |
| 23 |
* |
| 24 |
* @var int |
| 25 |
*/ |
| 26 |
protected static $default_sm = 768; |
| 27 |
|
| 28 |
/** |
| 29 |
* Tablet Breakpoint. |
| 30 |
* |
| 31 |
* @var int |
| 32 |
*/ |
| 33 |
protected static $default_md = 992; |
| 34 |
|
| 35 |
/** |
| 36 |
* Desktop Breakpoint. |
| 37 |
* |
| 38 |
* @var int |
| 39 |
*/ |
| 40 |
protected static $default_lg = 1200; |
| 41 |
|
| 42 |
/** |
| 43 |
* Database saved hash option Name. |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
protected $database_saved_hash_option_name = 'ghostkit_saved_breakpoints_hash'; |
| 48 |
|
| 49 |
/** |
| 50 |
* Plugin name. |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
protected $plugin_name = 'ghostkit'; |
| 55 |
|
| 56 |
/** |
| 57 |
* Plugin version. |
| 58 |
* |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
protected $plugin_version = GHOSTKIT_VERSION; |
| 62 |
|
| 63 |
/** |
| 64 |
* Scss Configurations. |
| 65 |
* |
| 66 |
* @var array |
| 67 |
*/ |
| 68 |
protected $scss_configs = array( |
| 69 |
'gutenberg/style.scss', |
| 70 |
'gutenberg/editor.scss', |
| 71 |
'gutenberg/blocks/*/styles/style.scss', |
| 72 |
); |
| 73 |
|
| 74 |
/** |
| 75 |
* Compile Scss Configurations. |
| 76 |
* |
| 77 |
* @var array |
| 78 |
*/ |
| 79 |
protected $compile_scss_configs; |
| 80 |
|
| 81 |
/** |
| 82 |
* Background breakpoints processing. |
| 83 |
* |
| 84 |
* @var object |
| 85 |
*/ |
| 86 |
protected $breakpoints_background_process; |
| 87 |
|
| 88 |
/** |
| 89 |
* GhostKit_Breakpoints constructor. |
| 90 |
*/ |
| 91 |
public function __construct() { |
| 92 |
if ( ! class_exists( 'GhostKit_Breakpoints_Background' ) ) { |
| 93 |
// breakpoints background. |
| 94 |
require_once __DIR__ . '/class-breakpoints-background.php'; |
| 95 |
} |
| 96 |
|
| 97 |
$this->compile_scss_configs = $this->get_compile_scss_configs(); |
| 98 |
$this->breakpoints_background_process = new GhostKit_Breakpoints_Background(); |
| 99 |
|
| 100 |
add_filter( 'style_loader_src', array( $this, 'change_style_src_to_compile' ), 10, 1 ); |
| 101 |
add_action( 'gkt_before_assets_register', array( $this, 'maybe_compile_scss_files' ) ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get plugin path. |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function get_plugin_path() { |
| 110 |
return ghostkit()->plugin_path; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get plugin url. |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
public function get_plugin_url() { |
| 119 |
return ghostkit()->plugin_url; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Get compile scss configurations. |
| 124 |
* |
| 125 |
* @return array |
| 126 |
*/ |
| 127 |
protected function get_compile_scss_configs() { |
| 128 |
$plugin_path = $this->get_plugin_path(); |
| 129 |
$upload_dir = wp_upload_dir(); |
| 130 |
$compile_scss_configs = array(); |
| 131 |
$scss_paths = apply_filters( 'gkt_scss_paths', $plugin_path ); |
| 132 |
$scss_configs = apply_filters( 'gkt_scss_configs', $this->scss_configs ); |
| 133 |
|
| 134 |
if ( ! is_array( $scss_paths ) ) { |
| 135 |
$scss_paths = array( $scss_paths ); |
| 136 |
} |
| 137 |
|
| 138 |
foreach ( $scss_configs as $scss_config ) { |
| 139 |
foreach ( $scss_paths as $path ) { |
| 140 |
$compile_scss_configs = $this->get_parsed_scss_files( $compile_scss_configs, $path, $upload_dir, $scss_config ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return $compile_scss_configs; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get parsed array of scss files |
| 149 |
* |
| 150 |
* @param array $compile_scss_configs - Array of config width scss files. |
| 151 |
* @param string $scss_path - Path to scss files. |
| 152 |
* @param string $upload_dir - Uploas WP dir. |
| 153 |
* @param string $scss_config - File search mask. |
| 154 |
* @return array |
| 155 |
*/ |
| 156 |
protected function get_parsed_scss_files( &$compile_scss_configs, $scss_path, $upload_dir, $scss_config ) { |
| 157 |
foreach ( glob( $scss_path . $scss_config ) as $template ) { |
| 158 |
$output_file = str_replace( $scss_path, $upload_dir['basedir'] . '/' . $this->plugin_name . '/', $template ); |
| 159 |
$output_file = str_replace( '.scss', '.min.css', $output_file ); |
| 160 |
|
| 161 |
$compile_scss_configs[] = array( |
| 162 |
'input_file' => $template, |
| 163 |
'output_file' => $output_file, |
| 164 |
); |
| 165 |
} |
| 166 |
return $compile_scss_configs; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Change style src to compile css files. |
| 171 |
* |
| 172 |
* @param string $src - Url to style. |
| 173 |
* @return string |
| 174 |
*/ |
| 175 |
public function change_style_src_to_compile( $src ) { |
| 176 |
$plugin_url = $this->get_plugin_url(); |
| 177 |
$breakpoints = self::get_breakpoints(); |
| 178 |
$breakpoints_hash = $this->get_breakpoints_hash( $breakpoints ); |
| 179 |
$default_breakpoints_hash = $this->get_default_breakpoints_hash(); |
| 180 |
|
| 181 |
if ( $breakpoints_hash !== $default_breakpoints_hash ) { |
| 182 |
$is_plugin_file = strstr( $src, $plugin_url ); |
| 183 |
|
| 184 |
if ( $is_plugin_file ) { |
| 185 |
$configs = $this->compile_scss_configs; |
| 186 |
$relative_uri = str_replace( $plugin_url, '', $is_plugin_file ); |
| 187 |
$relative_path = explode( '?ver=', $relative_uri )[0]; |
| 188 |
$upload_dir = wp_upload_dir(); |
| 189 |
$output_file = $upload_dir['basedir'] . '/' . $this->plugin_name . '/' . $relative_path; |
| 190 |
|
| 191 |
foreach ( $configs as $config ) { |
| 192 |
if ( |
| 193 |
$config['output_file'] === $output_file && |
| 194 |
file_exists( $output_file ) |
| 195 |
) { |
| 196 |
// add hash to compiled CSS version to prevent problems with cache. |
| 197 |
$uri_with_hash_version = str_replace( '?ver=', '?ver=' . $breakpoints_hash . '.', $relative_uri ); |
| 198 |
|
| 199 |
$src = $upload_dir['baseurl'] . '/' . $this->plugin_name . '/' . $uri_with_hash_version; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return $src; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get Breakpoints. |
| 210 |
*/ |
| 211 |
public static function get_breakpoints() { |
| 212 |
$xs = self::get_breakpoint_xs(); |
| 213 |
$xs = ( ! empty( $xs ) && $xs ) ? $xs : self::$default_xs; |
| 214 |
|
| 215 |
$sm = self::get_breakpoint_sm(); |
| 216 |
$sm = ( ! empty( $sm ) && $sm ) ? $sm : self::$default_sm; |
| 217 |
|
| 218 |
$md = self::get_breakpoint_md(); |
| 219 |
$md = ( ! empty( $md ) && $md ) ? $md : self::$default_md; |
| 220 |
|
| 221 |
$lg = self::get_breakpoint_lg(); |
| 222 |
$lg = ( ! empty( $lg ) && $lg ) ? $lg : self::$default_lg; |
| 223 |
|
| 224 |
return array( |
| 225 |
'xs' => $xs, |
| 226 |
'sm' => $sm, |
| 227 |
'md' => $md, |
| 228 |
'lg' => $lg, |
| 229 |
); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Get default breakpoints. |
| 234 |
* |
| 235 |
* @return array |
| 236 |
*/ |
| 237 |
public static function get_default_breakpoints() { |
| 238 |
return array( |
| 239 |
'xs' => self::get_default_breakpoint_xs(), |
| 240 |
'sm' => self::get_default_breakpoint_sm(), |
| 241 |
'md' => self::get_default_breakpoint_md(), |
| 242 |
'lg' => self::get_default_breakpoint_lg(), |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Get breakpoints Hash |
| 248 |
* |
| 249 |
* @param array $breakpoints - Breakpoints. |
| 250 |
* @return string |
| 251 |
*/ |
| 252 |
protected function get_breakpoints_hash( $breakpoints ) { |
| 253 |
return md5( |
| 254 |
wp_json_encode( |
| 255 |
array_merge( |
| 256 |
$breakpoints, |
| 257 |
array( |
| 258 |
$this->plugin_version, |
| 259 |
) |
| 260 |
) |
| 261 |
) |
| 262 |
); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Get default breakpoints Hash. |
| 267 |
* |
| 268 |
* @return string |
| 269 |
*/ |
| 270 |
protected function get_default_breakpoints_hash() { |
| 271 |
return $this->get_breakpoints_hash( |
| 272 |
array( |
| 273 |
'xs' => self::$default_xs, |
| 274 |
'sm' => self::$default_sm, |
| 275 |
'md' => self::$default_md, |
| 276 |
'lg' => self::$default_lg, |
| 277 |
) |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Styles may need to be compiled if breakpoints have been changed. |
| 283 |
* |
| 284 |
* @return void |
| 285 |
*/ |
| 286 |
public function maybe_compile_scss_files() { |
| 287 |
$breakpoints = self::get_breakpoints(); |
| 288 |
$breakpoints_hash = $this->get_breakpoints_hash( $breakpoints ); |
| 289 |
$default_breakpoints_hash = $this->get_default_breakpoints_hash(); |
| 290 |
$saved_breakpoints_hash = get_option( $this->database_saved_hash_option_name ); |
| 291 |
|
| 292 |
if ( |
| 293 |
$breakpoints_hash !== $saved_breakpoints_hash && |
| 294 |
$breakpoints_hash !== $default_breakpoints_hash |
| 295 |
) { |
| 296 |
$compile_scss_configs = $this->compile_scss_configs; |
| 297 |
|
| 298 |
// Replace modules in all SCSS files. |
| 299 |
if ( ! empty( $compile_scss_configs ) ) { |
| 300 |
$plugin_path = $this->get_plugin_path(); |
| 301 |
|
| 302 |
$scss_paths = apply_filters( 'gkt_scss_paths', $plugin_path ); |
| 303 |
|
| 304 |
if ( ! is_array( $scss_paths ) ) { |
| 305 |
$scss_paths = array( $scss_paths ); |
| 306 |
} |
| 307 |
|
| 308 |
foreach ( $scss_paths as $path ) { |
| 309 |
new GhostKit_Scss_Replace_Modules( $path ); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
$breakpoints = self::get_breakpoints(); |
| 314 |
|
| 315 |
$variables = array( |
| 316 |
'media-xs' => $breakpoints['xs'] . 'px', |
| 317 |
'media-sm' => $breakpoints['sm'] . 'px', |
| 318 |
'media-md' => $breakpoints['md'] . 'px', |
| 319 |
'media-lg' => $breakpoints['lg'] . 'px', |
| 320 |
); |
| 321 |
|
| 322 |
foreach ( $compile_scss_configs as $compile_scss_config ) { |
| 323 |
$scss_config_arguments = array_merge( |
| 324 |
$compile_scss_config, |
| 325 |
array( |
| 326 |
'variables' => $variables, |
| 327 |
) |
| 328 |
); |
| 329 |
|
| 330 |
if ( ! file_exists( $compile_scss_config['output_file'] ) ) { |
| 331 |
new GhostKit_Scss_Compiler( $scss_config_arguments ); |
| 332 |
} else { |
| 333 |
$this->breakpoints_background_process->push_to_queue( $scss_config_arguments ); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
$this->breakpoints_background_process->save()->dispatch(); |
| 338 |
|
| 339 |
update_option( $this->database_saved_hash_option_name, $breakpoints_hash ); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Get Default Extra Small Breakpoint. |
| 345 |
* |
| 346 |
* @return int |
| 347 |
*/ |
| 348 |
public static function get_default_breakpoint_xs() { |
| 349 |
return apply_filters( 'gkt_default_breakpoint_xs', self::$default_xs ); |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Get Extra Small Breakpoint. |
| 354 |
* |
| 355 |
* @return int |
| 356 |
*/ |
| 357 |
public static function get_breakpoint_xs() { |
| 358 |
return apply_filters( 'gkt_breakpoint_xs', self::get_default_breakpoint_xs() ); |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Get Default Mobile Breakpoint. |
| 363 |
* |
| 364 |
* @return int |
| 365 |
*/ |
| 366 |
public static function get_default_breakpoint_sm() { |
| 367 |
return apply_filters( 'gkt_default_breakpoint_sm', self::$default_sm ); |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Get Mobile Breakpoint. |
| 372 |
* |
| 373 |
* @return int |
| 374 |
*/ |
| 375 |
public static function get_breakpoint_sm() { |
| 376 |
return apply_filters( 'gkt_breakpoint_sm', self::get_default_breakpoint_sm() ); |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Get Default Tablet Breakpoint. |
| 381 |
* |
| 382 |
* @return int |
| 383 |
*/ |
| 384 |
public static function get_default_breakpoint_md() { |
| 385 |
return apply_filters( 'gkt_default_breakpoint_md', self::$default_md ); |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Get Tablet Breakpoint. |
| 390 |
* |
| 391 |
* @return int |
| 392 |
*/ |
| 393 |
public static function get_breakpoint_md() { |
| 394 |
return apply_filters( 'gkt_breakpoint_md', self::get_default_breakpoint_md() ); |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Get Default Desktop Breakpoint. |
| 399 |
* |
| 400 |
* @return int |
| 401 |
*/ |
| 402 |
public static function get_default_breakpoint_lg() { |
| 403 |
return apply_filters( 'gkt_default_breakpoint_lg', self::$default_lg ); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Get Desktop Breakpoint. |
| 408 |
* |
| 409 |
* @return int |
| 410 |
*/ |
| 411 |
public static function get_breakpoint_lg() { |
| 412 |
return apply_filters( 'gkt_breakpoint_lg', self::get_default_breakpoint_lg() ); |
| 413 |
} |
| 414 |
} |
| 415 |
new GhostKit_Breakpoints(); |
| 416 |
} |
| 417 |
|