| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC; |
| 4 |
|
| 5 |
/** |
| 6 |
* Helper functions |
| 7 |
*/ |
| 8 |
class Helpers { |
| 9 |
/** |
| 10 |
* Helper function to search a value recursively in a multidimensional array. |
| 11 |
* |
| 12 |
* @param mixed $needle The value to search for. |
| 13 |
* @param array $haystack The array to search in. |
| 14 |
* |
| 15 |
* @return bool True if $needle is found in $haystack, false otherwise. |
| 16 |
*/ |
| 17 |
public static function is_value_in_multidimensional_array( $needle, $haystack ) { |
| 18 |
if ( in_array( $needle, $haystack, true ) ) { |
| 19 |
return true; |
| 20 |
} |
| 21 |
foreach ( $haystack as $element ) { |
| 22 |
if ( is_array( $element ) && self::is_value_in_multidimensional_array( $needle, $element ) ) { |
| 23 |
return true; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Securely unserialize a WordPress option |
| 32 |
* a copy of WP core’s maybe_unserialize() function with additional code to prevent object injection |
| 33 |
* See https://www.tenable.com/security/research/tra-2023-7 about the risks of object injections |
| 34 |
* |
| 35 |
* @param string $data value to unserialize. |
| 36 |
* |
| 37 |
* @return mixed Unserialized data can be any type. |
| 38 |
*/ |
| 39 |
public static function maybe_unserialize( $data ) { |
| 40 |
if ( is_serialized( $data ) ) { |
| 41 |
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize, WordPress.PHP.NoSilencedErrors.Discouraged -- unserialize() is the only way to unserialize WP options |
| 42 |
return @unserialize( trim( $data ), [ 'allowed_classes' => false ] ); |
| 43 |
} |
| 44 |
|
| 45 |
return $data; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Wrapper for wp_enqueue_script() to handle various script dependencies |
| 50 |
* |
| 51 |
* @param string $handle The script handle. |
| 52 |
* @param string $src The relative script source path. The base URL will be added dynamically. |
| 53 |
* @param array $deps An array of script handles this script depends on. |
| 54 |
* @param bool $in_footer Whether to enqueue the script in the footer. |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public static function enqueue_script( string $handle, string $src, array $deps = [], bool $in_footer = true ) { |
| 59 |
wp_enqueue_script( $handle, self::get_script_url( $src ), $deps, ISCVERSION, $in_footer ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Wrapper for wp_register_script() to handle various script dependencies |
| 64 |
* |
| 65 |
* @param string $handle The script handle. |
| 66 |
* @param string $src The relative script source path. The base URL will be added dynamically. |
| 67 |
* @param array $deps An array of script handles this script depends on. |
| 68 |
* @param bool $in_footer Whether to enqueue the script in the footer. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public static function register_script( string $handle, string $src, array $deps = [], bool $in_footer = true ) { |
| 73 |
wp_register_script( $handle, self::get_script_url( $src ), $deps, ISCVERSION, $in_footer ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get script URL |
| 78 |
* |
| 79 |
* @param string $src The relative script source path. |
| 80 |
* |
| 81 |
* @return string The full URL to the script, with the base URL prepended and minified if SCRIPT_DEBUG is off. |
| 82 |
*/ |
| 83 |
public static function get_script_url( string $src ): string { |
| 84 |
// load the minified script version if SCRIPT_DEBUG is off |
| 85 |
if ( ! SCRIPT_DEBUG && strpos( $src, '.min.js' ) === false ) { |
| 86 |
$src = str_replace( '.js', '.min.js', $src ); |
| 87 |
} |
| 88 |
|
| 89 |
// fix slashes |
| 90 |
return trailingslashit( ISCBASEURL ) . ltrim( $src, '/' ); |
| 91 |
} |
| 92 |
} |
| 93 |
|