PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 2.25.0
Block Animations, Motion & Scroll Effects – Ghost Kit v2.25.0
3.7.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 2.25.0, at classes/class-breakpoints.php

416 lines 12.9 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 $this->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 = '2.25.0';
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 foreach ( $scss_configs as $scss_config ) {
153 if ( is_array( $scss_paths ) ) {
154 foreach ( $scss_paths as $path ) {
155 $compile_scss_configs = $this->get_parsed_scss_files( $compile_scss_configs, $path, $upload_dir, $scss_config );
156 }
157 } else {
158 $compile_scss_configs = $this->get_parsed_scss_files( $compile_scss_configs, $scss_paths, $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 = str_replace( '?ver=' . $this->plugin_version, '', $relative_uri );
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 $src = $upload_dir['baseurl'] . '/' . $this->plugin_name . '/' . $relative_uri;
215 }
216 }
217 }
218 }
219
220 return $src;
221 }
222
223 /**
224 * Get Breakpoints.
225 */
226 public static function get_breakpoints() {
227 $xs = self::get_breakpoint_xs();
228 $xs = ( ! empty( $xs ) && $xs ) ? $xs : self::$default_xs;
229
230 $sm = self::get_breakpoint_sm();
231 $sm = ( ! empty( $sm ) && $sm ) ? $sm : self::$default_sm;
232
233 $md = self::get_breakpoint_md();
234 $md = ( ! empty( $md ) && $md ) ? $md : self::$default_md;
235
236 $lg = self::get_breakpoint_lg();
237 $lg = ( ! empty( $lg ) && $lg ) ? $lg : self::$default_lg;
238
239 return array(
240 'xs' => $xs,
241 'sm' => $sm,
242 'md' => $md,
243 'lg' => $lg,
244 );
245 }
246
247 /**
248 * Get default breakpoints.
249 *
250 * @return array
251 */
252 public static function get_default_breakpoints() {
253 return array(
254 'xs' => self::get_default_breakpoint_xs(),
255 'sm' => self::get_default_breakpoint_sm(),
256 'md' => self::get_default_breakpoint_md(),
257 'lg' => self::get_default_breakpoint_lg(),
258 );
259 }
260
261 /**
262 * Get breakpoints Hash
263 *
264 * @param array $breakpoints - Breakpoints.
265 * @return string
266 */
267 protected function get_breakpoints_hash( $breakpoints ) {
268 return md5(
269 wp_json_encode(
270 array_merge(
271 $breakpoints,
272 array(
273 $this->plugin_version,
274 )
275 )
276 )
277 );
278 }
279
280 /**
281 * Get default breakpoints Hash.
282 *
283 * @return string
284 */
285 protected function get_default_breakpoints_hash() {
286 return $this->get_breakpoints_hash(
287 array(
288 'xs' => self::$default_xs,
289 'sm' => self::$default_sm,
290 'md' => self::$default_md,
291 'lg' => self::$default_lg,
292 )
293 );
294 }
295
296 /**
297 * Styles may need to be compiled if breakpoints have been changed.
298 *
299 * @return void
300 */
301 public function maybe_compile_scss_files() {
302 $breakpoints = self::get_breakpoints();
303 $breakpoints_hash = $this->get_breakpoints_hash( $breakpoints );
304 $default_breakpoints_hash = $this->get_default_breakpoints_hash();
305 $saved_breakpoints_hash = get_option( $this->database_saved_hash_option_name );
306
307 if (
308 $breakpoints_hash !== $saved_breakpoints_hash &&
309 $breakpoints_hash !== $default_breakpoints_hash
310 ) {
311 $compile_scss_configs = $this->compile_scss_configs;
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 if ( ! file_exists( $compile_scss_config['output_file'] ) ) {
330 new GhostKit_Scss_Compiler( $scss_config_arguments );
331 } else {
332 $this->breakpoints_background_process->push_to_queue( $scss_config_arguments );
333 }
334 }
335
336 $this->breakpoints_background_process->save()->dispatch();
337
338 update_option( $this->database_saved_hash_option_name, $breakpoints_hash );
339 }
340 }
341
342 /**
343 * Get Default Extra Small Breakpoint.
344 *
345 * @return int
346 */
347 public static function get_default_breakpoint_xs() {
348 return apply_filters( 'gkt_default_breakpoint_xs', self::$default_xs );
349 }
350
351 /**
352 * Get Extra Small Breakpoint.
353 *
354 * @return int
355 */
356 public static function get_breakpoint_xs() {
357 return apply_filters( 'gkt_breakpoint_xs', self::get_default_breakpoint_xs() );
358 }
359
360 /**
361 * Get Default Mobile Breakpoint.
362 *
363 * @return int
364 */
365 public static function get_default_breakpoint_sm() {
366 return apply_filters( 'gkt_default_breakpoint_sm', self::$default_sm );
367 }
368
369 /**
370 * Get Mobile Breakpoint.
371 *
372 * @return int
373 */
374 public static function get_breakpoint_sm() {
375 return apply_filters( 'gkt_breakpoint_sm', self::get_default_breakpoint_sm() );
376 }
377
378 /**
379 * Get Default Tablet Breakpoint.
380 *
381 * @return int
382 */
383 public static function get_default_breakpoint_md() {
384 return apply_filters( 'gkt_default_breakpoint_md', self::$default_md );
385 }
386
387 /**
388 * Get Tablet Breakpoint.
389 *
390 * @return int
391 */
392 public static function get_breakpoint_md() {
393 return apply_filters( 'gkt_breakpoint_md', self::get_default_breakpoint_md() );
394 }
395
396 /**
397 * Get Default Desktop Breakpoint.
398 *
399 * @return int
400 */
401 public static function get_default_breakpoint_lg() {
402 return apply_filters( 'gkt_default_breakpoint_lg', self::$default_lg );
403 }
404
405 /**
406 * Get Desktop Breakpoint.
407 *
408 * @return int
409 */
410 public static function get_breakpoint_lg() {
411 return apply_filters( 'gkt_breakpoint_lg', self::get_default_breakpoint_lg() );
412 }
413 }
414 new GhostKit_Breakpoints();
415 }
416