PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.3.2
Block Animations, Motion & Scroll Effects – Ghost Kit v3.3.2
3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / classes / class-breakpoints.php

class-breakpoints.php in Block Animations, Motion & Scroll Effects – Ghost Kit 3.3.2, at classes/class-breakpoints.php

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