PluginProbe
Media Cloud Sync / 1.3.0
Media Cloud Sync v1.3.0
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / config / utils.php

utils.php in Media Cloud Sync 1.3.0, at includes/config/utils.php

796 lines 25.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS;
4
5 defined('ABSPATH') || exit;
6
7 class Utils {
8 /**
9 * Check a variable is empty
10 * @since 1.0.0
11 * @param string|integer|array|float
12 * @return boolean
13 */
14 public static function is_empty($var){
15 if (is_array($var)) {
16 return empty($var);
17 } else {
18 return ($var === null || $var === false || $var === '');
19 }
20 }
21
22 /**
23 * Function To get Plugin Specific Wordpress Option
24 * @since 1.0.0
25 * @return array|boolean|string|integer|float|double
26 */
27 public static function get_option($key, $default = false, $meta_name = false, $expire = false){
28 $data = Cache::get_object_cache( $key, false, $meta_name, $expire );
29 return $data == false ? $default : $data;
30 }
31
32 /**
33 * Function To update Plugin Specific Wordpress Option
34 * @since 1.0.0
35 * @return boolean
36 */
37 public static function update_option($key, $options, $meta_name = false, $expire = false){
38 return Cache::set_object_cache( $key, $options, false, $meta_name, $expire );
39 }
40
41 /**
42 * Function To delete Plugin Specific Wordpress Option
43 * @since 1.0.0
44 * @return boolean
45 */
46 public static function delete_option($key, $meta_name = false){
47 return Cache::delete_object_cache( $key, false, $meta_name );
48 }
49
50 /**
51 * Function To get Plugin Specific Wordpress post meta
52 * @since 1.0.0
53 * @return array|boolean|string|integer|float|double
54 */
55 public static function get_meta($post_id, $key, $default = false, $meta_name = false, $expire = false){
56 $data = Cache::get_object_cache( $key, $post_id, $meta_name, $expire );
57 return $data == false ? $default : $data;
58 }
59
60 /**
61 * Get Post Meta Data By Query
62 * @since 1.0.0
63 * @return boolean
64 */
65 public static function get_post_meta($post_id, $key, $single=false, $db_query=false){
66 global $wpdb;
67 if(!(!empty($key) || $post_id)) return false;
68
69 if($db_query) {
70 $meta_data = $wpdb->get_row( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id=$post_id AND meta_key='$key'" );
71 if ($wpdb->last_error || null === $meta_data || !isset($meta_data)) {
72 return false;
73 }
74 return $meta_data->meta_value;
75 } else {
76 return get_post_meta( $post_id, $key, $single );
77 }
78 }
79
80 /**
81 * Get Option Data By Query
82 * @since 1.0.0
83 * @return boolean
84 */
85 public static function get_option_meta($key, $single=false, $db_query=false){
86 global $wpdb;
87 if(!(!empty($key))) return false;
88
89 if($db_query) {
90 $meta_data = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name='$key'" );
91 if ($wpdb->last_error || null === $meta_data || !isset($meta_data)) {
92 return false;
93 }
94 return $meta_data->option_value;
95 } else {
96 return get_option( $key, $single );
97 }
98 }
99
100
101 /**
102 * Function To update Plugin Specific Wordpress post meta
103 * @since 1.0.0
104 * @return boolean
105 */
106 public static function update_meta($post_id, $key, $options, $meta_name = false, $expire = false){
107 return Cache::set_object_cache( $key, $options, $post_id, $meta_name, $expire );
108 }
109
110 /**
111 * Function To delete Plugin Specific Wordpress post meta
112 * @since 1.0.0
113 * @return boolean
114 */
115 public static function delete_meta($post_id, $key, $meta_name = false){
116 return Cache::delete_object_cache( $key, $post_id, $meta_name );
117 }
118
119 /**
120 * Function To get Plugin Specific Wordpress user meta
121 * @since 1.0.0
122 * @return array|boolean|string|integer|float|double
123 */
124 public static function get_user_meta($post_id, $key, $default = false, $meta_name = false, $expire = false){
125 $data = Cache::get_object_cache( $key, $post_id, $meta_name, $expire, true );
126 return $data == false ? $default : $data;
127 }
128
129 /**
130 * Function To update Plugin Specific Wordpress user meta
131 * @since 1.0.0
132 * @return boolean
133 */
134 public static function update_user_meta($post_id, $key, $options, $meta_name = false, $expire = false){
135 return Cache::set_object_cache( $key, $options, $post_id, $meta_name, $expire, true );
136 }
137
138 /**
139 * Function To delete Plugin Specific Wordpress user meta
140 * @since 1.0.0
141 * @return boolean
142 */
143 public static function delete_user_meta($post_id, $key, $meta_name = false){
144 return Cache::delete_object_cache( $key, $post_id, $meta_name, true );
145 }
146
147 /**
148 * Function To get Current credentials
149 * @since 1.0.0
150 * @return array|boolean|string|integer|float|double
151 */
152 public static function get_credentials($option='', $default=false, $masked_config = false){
153 $current_setttings = self::get_option('credentials',[], Schema::getConstant('GLOBAL_SETTINGS_KEY'));
154 if(isset($current_setttings) && !empty($current_setttings)){
155 if(isset($option) && !empty($option)){
156 if(isset($current_setttings[$option])) {
157 if($masked_config && $option == 'config'){
158 $current_setttings[$option] = self::mask_config($current_setttings[$option]);
159 }
160 return $current_setttings[$option];
161 } else {
162 return $default;
163 }
164 } else {
165 if($masked_config && isset($current_setttings['config'])){
166 $current_setttings['config'] = self::mask_config($current_setttings['config']);
167 }
168 return $current_setttings;
169 }
170 } else {
171 return $default;
172 }
173 }
174
175 /**
176 * Mask Config
177 * @since 1.2.13
178 * @return array|boolean|string|integer|float|double
179 */
180 public static function mask_config($config){
181 foreach ($config as $key => $value) {
182 if (in_array($key, ['config_json', 'secret_key'])) {
183 $config[$key] = substr($value, 0, 4) . self::mask_string(substr($value, 4));
184 }
185 }
186 return $config;
187 }
188
189 /**
190 * Mask String
191 * @since 1.2.13
192 * @return array|boolean|string|integer|float|double
193 */
194 public static function mask_string($string){
195 return str_repeat('*', strlen($string));
196 }
197
198 /**
199 * Function To get Current settings
200 * @since 1.0.0
201 * @return array|boolean|string|integer|float|double
202 */
203 public static function get_settings($option='', $default=false){
204 $current_setttings = self::get_option('settings',[], Schema::getConstant('GLOBAL_SETTINGS_KEY'));
205 if(isset($current_setttings) && !empty($current_setttings)){
206 if(isset($option) && !empty($option)){
207 if(isset($current_setttings[$option])) {
208 return $current_setttings[$option];
209 } else {
210 return $default;
211 }
212 } else {
213 return $current_setttings;
214 }
215 } else {
216 return $default;
217 }
218 }
219
220 /**
221 * Function To get Current statuses
222 * @since 1.0.0
223 * @return array|boolean|string|integer|float|double
224 */
225 public static function get_status($option='', $default=false){
226 $current_setttings = self::get_option('status',[], Schema::getConstant('STATUS_KEY'));
227 if(isset($current_setttings) && !empty($current_setttings)){
228 if(isset($option) && !empty($option)){
229 if(isset($current_setttings[$option])) {
230 return $current_setttings[$option];
231 } else {
232 return $default;
233 }
234 } else {
235 return $current_setttings;
236 }
237 } else {
238 return $default;
239 }
240 }
241
242 /**
243 * Function To set statuses
244 * @since 1.0.0
245 *
246 */
247 public static function set_status($option='', $data=[]){
248 $current_setttings = self::get_option('status',[], Schema::getConstant('STATUS_KEY'));
249 if(isset($option) && !empty($option)){
250 $current_setttings[$option] = $data;
251 return self::update_option('status', $current_setttings, Schema::getConstant('STATUS_KEY'));
252 } else {
253 return false;
254 }
255 }
256
257 /**
258 * Function To get Current Service
259 * @since 1.0.0
260 * @return array|boolean|string|integer|float|double
261 */
262 public static function get_service(){
263 $current_service = self::get_credentials('service', '');
264 if(isset($current_service) && !empty($current_service)){
265 return $current_service;
266 } else {
267 return false;
268 }
269 }
270
271 /**
272 * Function To get Current config
273 * @since 1.0.0
274 * @return array|boolean|string|integer|float|double
275 */
276 public static function get_config($option='', $default=false){
277 $current_setttings = self::get_credentials('config',[]);
278 if(isset($current_setttings) && !empty($current_setttings)){
279 if(isset($option) && !empty($option)){
280 if(isset($current_setttings[$option])) {
281 return $current_setttings[$option];
282 } else {
283 return $default;
284 }
285 } else {
286 return $current_setttings;
287 }
288 } else {
289 return $default;
290 }
291 }
292
293 /**
294 * Function to check serving media environment is ok
295 * @since 1.0.0
296 * @return boolean
297 */
298 public static function is_ok_to_serve($attachment_id = false, $check_id = true){
299 return (
300 self::get_service() &&
301 self::get_settings('rewrite_url') &&
302 ( $check_id ? isset($attachment_id) && !empty($attachment_id) : true )
303 );
304 }
305
306 /**
307 * Function to check uploading media environment is ok
308 * @since 1.0.0
309 * @return boolean
310 */
311 public static function is_ok_to_upload($attachment_id = false){
312 return (
313 self::get_service() &&
314 self::get_settings('copy_to_bucket') &&
315 isset($attachment_id) && !empty($attachment_id)
316 );
317 }
318
319 /**
320 * Function To check service is enabled
321 * @since 1.0.0
322 * @return array|boolean|string|integer|float|double
323 */
324 public static function is_service_enabled(){
325 return !!self::get_service();
326 }
327
328 /**
329 * Check whether a file exist in a list of files
330 * @since 1.0.1
331 * @return boolean
332 */
333 public static function check_existing_file_names( $filename, $files ) {
334 $fname = pathinfo( $filename, PATHINFO_FILENAME );
335 $ext = pathinfo( $filename, PATHINFO_EXTENSION );
336
337 // Edge case, file names like `.ext`.
338 if ( empty( $fname ) ) {
339 return false;
340 }
341
342 if ( $ext ) {
343 $ext = ".$ext";
344 }
345
346 $regex = '/^' . preg_quote( $fname ) . '-(?:\d+x\d+|scaled|rotated)' . preg_quote( $ext ) . '$/i';
347
348 foreach ( $files as $file ) {
349 if (
350 preg_match( $regex, wp_basename($file) ) ||
351 $filename == $file
352 ) {
353 return true;
354 }
355 }
356
357 return false;
358 }
359
360
361 /**
362 * Get Image URL and path from URL
363 * Return Relative URL and Path of an attachment.
364 * @since 1.0.0
365 *
366 */
367 public static function get_attachment_source_path($file) {
368 if ( isset($file) && !empty($file) ) {
369 $uploads = wp_get_upload_dir();
370 $file_path = '';
371 $site_url = site_url('/');
372 $enable_base_path = self::get_settings('enable_base_path', true);
373 $server_base_path = self::get_settings('base_path', 'wp-content/uploads');
374 if ( $uploads && false === $uploads['error'] ) {
375
376 $uploadDir = substr( $uploads['baseurl'], strpos( $uploads['baseurl'], $site_url ) + strlen($site_url));
377
378 // Get URL and PATH
379 if ( 0 === strpos( $file, $uploads['basedir'] ) || 0 === strpos( $file, $uploads['baseurl'] ) ) { // If URL is full link
380 $file_path = str_replace( $uploads['basedir'], '', $file );
381 $file_path = str_replace( $uploads['baseurl'], '', $file_path ); // Replace if has URl
382 } else if (
383 0 === strpos( $file, str_replace('/','\\', $uploads['basedir'] )) ||
384 0 === strpos( $file, str_replace('\\','/', $uploads['baseurl'] ))
385 ) { // If URL is full link and the url is slash unified (Like: str_replace('/','\\', $dir))
386 $file_path = str_replace( str_replace('/','\\', $uploads['basedir'] ), '', $file );
387 $file_path = str_replace( str_replace('\\','/', $uploads['baseurl'] ), '', $file_path ); // Replace if has URl
388 $file_path = str_replace('\\','/', $file_path );
389 } else if ( false !== strpos( $file, $uploadDir ) ) { //If URL has sub Directory That matches end of base URL(eg: wp-content/uploads)
390 $fileDir = dirname( $file );
391 $start_pos = strpos( $fileDir, $uploadDir ) + strlen($uploadDir);
392 $subDir = substr( $fileDir, $start_pos, strlen($fileDir)); // Find Sub Directory
393 $file_name = wp_basename( $file );
394
395 $file_path = trailingslashit($subDir) . $file_name;
396 } else if($enable_base_path && $server_base_path && false !== strpos( $file, trailingslashit($server_base_path))) {
397 $fileDir = dirname( $file );
398 $start_pos = strpos( $fileDir, $server_base_path) + strlen($server_base_path);
399 $subDir = substr( $fileDir, $start_pos, strlen($fileDir)); // Find Sub Directory
400 $file_name = wp_basename( $file );
401
402 $file_path = trailingslashit($subDir) . $file_name;
403 } else if(filter_var($file, FILTER_VALIDATE_URL)) {
404 $parsed = parse_url($file);
405 $path = isset($parsed["path"]) ? $parsed["path"] : '';
406 $query = isset($parsed["query"]) ? '?'.$parsed["query"] : '';
407 $file_path = $path. $query;
408 } else {
409 $file_path = $file;
410 }
411
412 return apply_filters( 'wpmcs_get_relative_file_path_from_upload_directory', untrailingslashit(ltrim( $file_path, '/\\' )), $file );
413 }
414 }
415 return false;
416 }
417
418 /**
419 * Check extension is compatible
420 * @since 1.0.0
421 * @return boolean
422 */
423 public static function is_extension_available($path){
424 $settings = self::get_settings();
425 $path_parts = pathinfo($path);
426
427 if(!isset($path_parts['basename']) || !isset($path_parts['extension'])) return false;
428
429 $alowed = isset($settings['extensions_include']) ? $settings['extensions_include'] : [];
430 $not_allowed = isset($settings['extensions_exclude']) ? $settings['extensions_exclude'] : [];
431
432 if(
433 (in_array($path_parts['extension'], $not_allowed)) ||
434 (!empty($alowed) && !in_array($path_parts['extension'], $alowed))
435 ) {
436 return false;
437 }
438
439 $type_and_ext = wp_check_filetype_and_ext($path, $path_parts['basename']);
440 $ext = empty( $type_and_ext['ext'] ) ? '' : $type_and_ext['ext'];
441 $type = empty( $type_and_ext['type'] ) ? '' : $type_and_ext['type'];
442
443 if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
444 return false;
445 }
446
447 return true;
448 }
449
450 /**
451 * Generate prefix for object versioning
452 * @since 1.0.0
453 * @return string
454 */
455 public static function generate_object_versioning_prefix(){
456 $year_month = self::get_settings('year_month');
457 $date_format = $year_month ? 'dHis' : 'YmdHis';
458
459 // Use current time so that object version is unique
460 $time = current_time('timestamp');
461
462 $object_version = date($date_format, $time) . '/';
463 $object_version = apply_filters('wpmcs_object_version_prefix', $object_version);
464
465 return $object_version;
466 }
467
468 /**
469 * Generate Key for Objects
470 * @since 1.0.0
471 */
472 /**
473 * Generate Key for Objects
474 * @since 1.0.0
475 */
476 public static function generate_object_key($media_path, $prefix) {
477 $upload_path = '';
478 $enable_base_path = self::get_settings('enable_base_path', true);
479 $base_path = self::get_settings('base_path', 'wp-content/uploads');
480 $year_month = self::get_settings('year_month', true);
481 $media_path = ltrim( $media_path, '/' );
482 $file_name = wp_basename( $media_path );
483
484 if(!$enable_base_path) { // If base path is not enabled
485 $base_path = '';
486 }
487
488 $keep_original_folder_structure = apply_filters( 'wpmcs_keep_original_folder_structure', false );
489
490 if(isset($base_path) && !empty($base_path)) {
491 $upload_path.= preg_replace('~/+~', '/',
492 str_replace('\\', '/',
493 trim($base_path," \n\r\t\v\x00\/ ")
494 )
495 );
496 }
497
498 if($keep_original_folder_structure) {
499 $object_key = ltrim($upload_path . '/' . dirname( $media_path ) . '/' . $prefix . $file_name, '/');
500 } else {
501 if(isset($year_month) && $year_month) {
502 $year_month_prefix = self::get_year_month_from_file_path($media_path);
503 if($year_month_prefix) {
504 $upload_path.= '/'.$year_month_prefix;
505 } else {
506 $upload_path.= '/'.date("Y/m");
507 }
508 }
509
510 $object_key = ltrim($upload_path.'/'.$prefix.$file_name, '/');
511 }
512
513 return apply_filters( 'wpmcs_object_key', $object_key, $media_path, $prefix );
514 }
515
516
517 /**
518 * Check if a file path or URL follows the year/month structure and return the year/month as a string.
519 *
520 * @param string $path_or_url The relative path, absolute path, or URL to check.
521 * @return string|false The year/month string if valid, false otherwise.
522 */
523 public static function get_year_month_from_file_path( $path_or_url ) {
524 // Regex pattern to match paths and URLs containing 'YYYY/MM/' at any depth, allowing subdirectories afterward
525 $pattern = '#(?:^|/)(\d{4})/(0[1-9]|1[0-2])/[^/]+(?:/[^/]+)*$#';
526
527 // Check if the input matches the pattern
528 if ( preg_match( $pattern, $path_or_url, $matches ) ) {
529 // Return the year/month as a string in the format 'YYYY/MM'
530 return $matches[1] . '/' . $matches[2];
531 }
532
533 return false; // Invalid format
534 }
535
536
537 /**
538 * Maybe convert size to string
539 *
540 * @param int $attachment_id
541 * @param mixed $size
542 *
543 * @return null|string
544 */
545 public static function maybe_convert_size_to_string( $attachment_id, $size ) {
546 if ( is_array( $size ) ) {
547 $width = ( isset( $size[0] ) && $size[0] > 0 ) ? $size[0] : 1;
548 $height = ( isset( $size[1] ) && $size[1] > 0 ) ? $size[1] : 1;
549 $original_aspect_ratio = $width / $height;
550 $meta = wp_get_attachment_metadata( $attachment_id );
551
552 if ( ! isset( $meta['sizes'] ) || empty( $meta['sizes'] ) ) {
553 return false;
554 }
555
556 $sizes = $meta['sizes'];
557 uasort( $sizes, function ( $a, $b ) {
558 // Order by image area
559 return ( $a['width'] * $a['height'] ) - ( $b['width'] * $b['height'] );
560 } );
561
562 $near_matches = array();
563
564 foreach ( $sizes as $size => $value ) {
565 if ( $width > $value['width'] || $height > $value['height'] ) {
566 continue;
567 }
568 $aspect_ratio = $value['width'] / $value['height'];
569 if ( $aspect_ratio === $original_aspect_ratio ) {
570 return $size;
571 }
572 $near_matches[] = $size;
573 }
574 // Return nearest match
575 if ( ! empty( $near_matches ) ) {
576 return $near_matches[0];
577 }
578 }
579
580 return $size;
581 }
582
583 /**
584 * Reduce the given URL down to the simplest version of itself.
585 *
586 * Useful for matching against the full version of the URL in a full-text search
587 * or saving as a key for dictionary type lookup.
588 *
589 * @param string $url
590 *
591 * @return string
592 */
593 public static function reduce_url( $url ) {
594 $parts = static::parse_url( $url );
595 $host = isset( $parts['host'] ) ? $parts['host'] : '';
596 $port = isset( $parts['port'] ) ? ":{$parts['port']}" : '';
597 $path = isset( $parts['path'] ) ? $parts['path'] : '';
598
599 return '//' . $host . $port . $path;
600 }
601
602 /**
603 * Remove scheme from URL.
604 *
605 * @param string $url
606 * @return string
607 */
608 public static function remove_scheme( $url ) {
609 return preg_replace( '/^(?:http|https):/', '', $url );
610 }
611
612 /**
613 * Remove size from filename (image[-100x100].jpeg).
614 *
615 * @param string $url
616 * @param bool $remove_extension
617 *
618 * @return string
619 */
620 public static function remove_size_from_filename( $url, $remove_extension = false ) {
621 $url = preg_replace( '/^(\S+)-[0-9]{1,4}x[0-9]{1,4}(\.[a-zA-Z0-9\.]{2,})?/', '$1$2', $url );
622
623 $url = apply_filters( 'wpmcs_remove_size_from_filename', $url );
624
625 if ( $remove_extension ) {
626 $ext = pathinfo( $url, PATHINFO_EXTENSION );
627 $url = str_replace( ".$ext", '', $url );
628 }
629
630 return $url;
631 }
632
633 /**
634 * Is the string a URL?
635 *
636 * @param mixed $string
637 *
638 * @return bool
639 */
640 public static function is_url( $string ): bool {
641 if ( empty( $string ) || ! is_string( $string ) ) {
642 return false;
643 }
644
645 if ( preg_match( '@^(?:https?:)?//[a-zA-Z0-9\-]+@', $string ) ) {
646 return true;
647 }
648
649 return false;
650 }
651
652 /**
653 * Parses a URL into its components. Compatible with PHP < 5.4.7.
654 *
655 * @param string $url The URL to parse.
656 *
657 * @param int $component PHP_URL_ constant for URL component to return.
658 *
659 * @return mixed An array of the parsed components, mixed for a requested component, or false on error.
660 */
661 public static function parse_url( $url, $component = -1 ) {
662 $url = trim( $url );
663 $no_scheme = 0 === strpos( $url, '//' );
664
665 if ( $no_scheme ) {
666 $url = 'http:' . $url;
667 }
668
669 $parts = parse_url( $url, $component );
670
671 if ( 0 < $component ) {
672 return $parts;
673 }
674
675 if ( $no_scheme && is_array( $parts ) ) {
676 unset( $parts['scheme'] );
677 }
678
679 return $parts;
680 }
681
682 /**
683 * Is the given string a usable URL?
684 *
685 * We need URLs that include at least a domain and filename with extension
686 * for URL rewriting in either direction.
687 *
688 * @param mixed $url
689 *
690 * @return bool
691 */
692 public static function is_file_url( $url ): bool {
693 if ( ! static::is_url( $url ) ) {
694 return false;
695 }
696
697 $parts = static::parse_url( $url );
698
699 if (
700 empty( $parts['host'] ) ||
701 empty( $parts['path'] ) ||
702 ! pathinfo( $parts['path'], PATHINFO_EXTENSION )
703 ) {
704 return false;
705 }
706
707 return true;
708 }
709
710
711 /**
712 * Remove query strings of services.
713 *
714 * @param string $content
715 * @param string $base_url Optional base URL that must exist within URL for Amazon query strings to be removed.
716 *
717 * @return string
718 */
719 public static function remove_query_strings( $content, $base_url = '' ) {
720 $pattern = '\?[^\s"<\?]*(?:X-Amz-Algorithm|AWSAccessKeyId|Key-Pair-Id|GoogleAccessId)=[^\s"<\?]+';
721 $group = 0;
722
723 if ( ! is_string( $content ) ) {
724 return $content;
725 }
726
727 if ( ! empty( $base_url ) ) {
728 $pattern = preg_quote( $base_url, '/' ) . '[^\s"<\?]+(' . $pattern . ')';
729 $group = 1;
730 }
731 if ( ! preg_match_all( '/' . $pattern . '/', $content, $matches ) || ! isset( $matches[ $group ] ) ) {
732 // No query strings found, return
733 return $content;
734 }
735
736 $matches = array_unique( $matches[ $group ] );
737
738 foreach ( $matches as $match ) {
739 $content = str_replace( $match, '', $content );
740 }
741 return $content;
742 }
743
744 /**
745 * Maybe unserialize data, but not if an object.
746 *
747 * @param mixed $data
748 *
749 * @return mixed
750 */
751 public static function maybe_unserialize( $data ) {
752 if ( is_serialized( $data ) ) {
753 return @unserialize( $data, array( 'allowed_classes' => false ) ); // @phpcs:ignore
754 }
755
756 return $data;
757 }
758
759
760 /**
761 * Validate JSON
762 */
763 public static function is_json( $string ) {
764 json_decode( $string );
765 return ( json_last_error() == JSON_ERROR_NONE );
766 }
767
768 /**
769 * Is this an AJAX process?
770 *
771 * @return bool
772 */
773 public static function is_ajax() {
774 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
775 return true;
776 }
777
778 return false;
779 }
780
781 /**
782 * Helper function for filtering super globals. Easily testable.
783 *
784 * @param string $variable
785 * @param int $type
786 * @param int $filter
787 * @param mixed $options
788 *
789 * @return mixed
790 */
791 public static function filter_input( $variable, $type = INPUT_GET, $filter = FILTER_DEFAULT, $options = array() ) {
792 return filter_input( $type, $variable, $filter, $options );
793 }
794
795 }
796