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-fallback.php

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

380 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Fallback for old Breakpoints used in Ghost Kit PRO v1.6.2 to prevent PHP error.
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 * GhostKit_Breakpoints constructor.
83 */
84 public function __construct() {
85 $this->compile_scss_configs = $this->get_compile_scss_configs();
86
87 add_filter( 'style_loader_src', array( $this, 'change_style_src_to_compile' ), 10, 1 );
88 add_action( 'gkt_before_assets_register', array( $this, 'maybe_compile_scss_files' ) );
89 }
90
91 /**
92 * Get plugin path.
93 *
94 * @return string
95 */
96 public function get_plugin_path() {
97 return ghostkit()->plugin_path;
98 }
99
100 /**
101 * Get plugin url.
102 *
103 * @return string
104 */
105 public function get_plugin_url() {
106 return ghostkit()->plugin_url;
107 }
108
109 /**
110 * Get compile scss configurations.
111 *
112 * @return array
113 */
114 protected function get_compile_scss_configs() {
115 $plugin_path = $this->get_plugin_path();
116 $upload_dir = wp_upload_dir();
117 $compile_scss_configs = array();
118 $scss_paths = apply_filters( 'gkt_scss_paths', $plugin_path );
119 $scss_configs = apply_filters( 'gkt_scss_configs', $this->scss_configs );
120
121 foreach ( $scss_configs as $scss_config ) {
122 if ( is_array( $scss_paths ) ) {
123 foreach ( $scss_paths as $path ) {
124 $compile_scss_configs = $this->get_parsed_scss_files( $compile_scss_configs, $path, $upload_dir, $scss_config );
125 }
126 } else {
127 $compile_scss_configs = $this->get_parsed_scss_files( $compile_scss_configs, $scss_paths, $upload_dir, $scss_config );
128 }
129 }
130
131 return $compile_scss_configs;
132 }
133
134 /**
135 * Get parsed array of scss files
136 *
137 * @param array $compile_scss_configs - Array of config width scss files.
138 * @param string $scss_path - Path to scss files.
139 * @param string $upload_dir - Uploas WP dir.
140 * @param string $scss_config - File search mask.
141 * @return array
142 */
143 protected function get_parsed_scss_files( &$compile_scss_configs, $scss_path, $upload_dir, $scss_config ) {
144 foreach ( glob( $scss_path . $scss_config ) as $template ) {
145 $output_file = str_replace( $scss_path, $upload_dir['basedir'] . '/' . $this->plugin_name . '/', $template );
146 $output_file = str_replace( '.scss', '.min.css', $output_file );
147
148 $compile_scss_configs[] = array(
149 'input_file' => $template,
150 'output_file' => $output_file,
151 );
152 }
153 return $compile_scss_configs;
154 }
155
156 /**
157 * Change style src to compile css files.
158 *
159 * @param string $src - Url to style.
160 * @return string
161 */
162 public function change_style_src_to_compile( $src ) {
163 $plugin_url = $this->get_plugin_url();
164 $breakpoints = self::get_breakpoints();
165 $breakpoints_hash = self::get_breakpoints_hash( $breakpoints );
166 $default_breakpoints_hash = self::get_default_breakpoints_hash();
167
168 if ( $breakpoints_hash !== $default_breakpoints_hash ) {
169 $is_plugin_file = strstr( $src, $plugin_url );
170
171 if ( $is_plugin_file ) {
172 $configs = $this->compile_scss_configs;
173 $relative_uri = str_replace( $plugin_url, '', $is_plugin_file );
174 $relative_path = str_replace( '?ver=' . $this->plugin_version, '', $relative_uri );
175 $upload_dir = wp_upload_dir();
176 $output_file = $upload_dir['basedir'] . '/' . $this->plugin_name . '/' . $relative_path;
177
178 foreach ( $configs as $config ) {
179 if (
180 $config['output_file'] === $output_file &&
181 file_exists( $output_file )
182 ) {
183 $src = $upload_dir['baseurl'] . '/' . $this->plugin_name . '/' . $relative_uri;
184 }
185 }
186 }
187 }
188
189 return $src;
190 }
191
192 /**
193 * Get Breakpoints.
194 */
195 public static function get_breakpoints() {
196 $xs = self::get_breakpoint_xs();
197 $xs = ( ! empty( $xs ) && $xs ) ? $xs : self::$default_xs;
198
199 $sm = self::get_breakpoint_sm();
200 $sm = ( ! empty( $sm ) && $sm ) ? $sm : self::$default_sm;
201
202 $md = self::get_breakpoint_md();
203 $md = ( ! empty( $md ) && $md ) ? $md : self::$default_md;
204
205 $lg = self::get_breakpoint_lg();
206 $lg = ( ! empty( $lg ) && $lg ) ? $lg : self::$default_lg;
207
208 return array(
209 'xs' => $xs,
210 'sm' => $sm,
211 'md' => $md,
212 'lg' => $lg,
213 );
214 }
215
216 /**
217 * Get default breakpoints.
218 *
219 * @return array
220 */
221 public static function get_default_breakpoints() {
222 return array(
223 'xs' => self::get_default_breakpoint_xs(),
224 'sm' => self::get_default_breakpoint_sm(),
225 'md' => self::get_default_breakpoint_md(),
226 'lg' => self::get_default_breakpoint_lg(),
227 );
228 }
229
230 /**
231 * Get breakpoints Hash
232 *
233 * @param array $breakpoints - Breakpoints.
234 * @return string
235 */
236 protected static function get_breakpoints_hash( $breakpoints ) {
237 return md5(
238 wp_json_encode(
239 array_merge(
240 $breakpoints,
241 array(
242 GHOSTKIT_VERSION,
243 )
244 )
245 )
246 );
247 }
248
249 /**
250 * Get default breakpoints Hash.
251 *
252 * @return string
253 */
254 protected static function get_default_breakpoints_hash() {
255 return self::get_breakpoints_hash(
256 array(
257 'xs' => self::$default_xs,
258 'sm' => self::$default_sm,
259 'md' => self::$default_md,
260 'lg' => self::$default_lg,
261 )
262 );
263 }
264
265 /**
266 * Styles may need to be compiled if breakpoints have been changed.
267 *
268 * @return void
269 */
270 public function maybe_compile_scss_files() {
271 $breakpoints = self::get_breakpoints();
272 $breakpoints_hash = self::get_breakpoints_hash( $breakpoints );
273 $default_breakpoints_hash = self::get_default_breakpoints_hash();
274 $saved_breakpoints_hash = get_option( $this->database_saved_hash_option_name );
275
276 if (
277 $breakpoints_hash !== $saved_breakpoints_hash &&
278 $breakpoints_hash !== $default_breakpoints_hash
279 ) {
280 $compile_scss_configs = $this->compile_scss_configs;
281
282 $breakpoints = self::get_breakpoints();
283
284 $variables = array(
285 'media-xs' => $breakpoints['xs'] . 'px',
286 'media-sm' => $breakpoints['sm'] . 'px',
287 'media-md' => $breakpoints['md'] . 'px',
288 'media-lg' => $breakpoints['lg'] . 'px',
289 );
290
291 foreach ( $compile_scss_configs as $compile_scss_config ) {
292 new GhostKit_Scss_Compiler(
293 array_merge(
294 $compile_scss_config,
295 array(
296 'variables' => $variables,
297 )
298 )
299 );
300 }
301
302 update_option( $this->database_saved_hash_option_name, $breakpoints_hash );
303 }
304 }
305
306 /**
307 * Get Default Extra Small Breakpoint.
308 *
309 * @return int
310 */
311 public static function get_default_breakpoint_xs() {
312 return apply_filters( 'gkt_default_breakpoint_xs', self::$default_xs );
313 }
314
315 /**
316 * Get Extra Small Breakpoint.
317 *
318 * @return int
319 */
320 public static function get_breakpoint_xs() {
321 return apply_filters( 'gkt_breakpoint_xs', self::get_default_breakpoint_xs() );
322 }
323
324 /**
325 * Get Default Mobile Breakpoint.
326 *
327 * @return int
328 */
329 public static function get_default_breakpoint_sm() {
330 return apply_filters( 'gkt_default_breakpoint_sm', self::$default_sm );
331 }
332
333 /**
334 * Get Mobile Breakpoint.
335 *
336 * @return int
337 */
338 public static function get_breakpoint_sm() {
339 return apply_filters( 'gkt_breakpoint_sm', self::get_default_breakpoint_sm() );
340 }
341
342 /**
343 * Get Default Tablet Breakpoint.
344 *
345 * @return int
346 */
347 public static function get_default_breakpoint_md() {
348 return apply_filters( 'gkt_default_breakpoint_md', self::$default_md );
349 }
350
351 /**
352 * Get Tablet Breakpoint.
353 *
354 * @return int
355 */
356 public static function get_breakpoint_md() {
357 return apply_filters( 'gkt_breakpoint_md', self::get_default_breakpoint_md() );
358 }
359
360 /**
361 * Get Default Desktop Breakpoint.
362 *
363 * @return int
364 */
365 public static function get_default_breakpoint_lg() {
366 return apply_filters( 'gkt_default_breakpoint_lg', self::$default_lg );
367 }
368
369 /**
370 * Get Desktop Breakpoint.
371 *
372 * @return int
373 */
374 public static function get_breakpoint_lg() {
375 return apply_filters( 'gkt_breakpoint_lg', self::get_default_breakpoint_lg() );
376 }
377 }
378 new GhostKit_Breakpoints();
379 }
380