| 1 |
<?php |
| 2 |
if (!defined('ABSPATH') && !defined('MCDATAPATH') && !defined('PHP_ERR_MONIT_PATH')) exit; |
| 3 |
|
| 4 |
if (!class_exists('WPRHelper')) : |
| 5 |
class WPRHelper { |
| 6 |
public static function safePregMatch($pattern, $subject, &$matches = null, $flags = 0, $offset = 0) { |
| 7 |
if (!is_string($pattern) || !is_string($subject)) { |
| 8 |
return false; |
| 9 |
} |
| 10 |
return preg_match($pattern, $subject, $matches, $flags, $offset); |
| 11 |
} |
| 12 |
|
| 13 |
# XNOTE - The below function assumes valid input |
| 14 |
# $array should be an array and $keys should be an array of string, or integer data |
| 15 |
public static function filterArray($array, $keys) { |
| 16 |
$filteredArray = array(); |
| 17 |
foreach ($keys as $key) { |
| 18 |
if (array_key_exists($key, $array)) { |
| 19 |
$filteredArray[$key] = $array[$key]; |
| 20 |
} |
| 21 |
} |
| 22 |
return $filteredArray; |
| 23 |
} |
| 24 |
|
| 25 |
# XNOTE - The below function assumes valid input |
| 26 |
# $array should be an array and $keys should be an array of string, or integer data |
| 27 |
public static function digArray($array, $keys) { |
| 28 |
if (empty($keys)) { |
| 29 |
return null; |
| 30 |
} |
| 31 |
$curr_array = $array; |
| 32 |
foreach ($keys as $key) { |
| 33 |
if (is_array($curr_array) && array_key_exists($key, $curr_array)) { |
| 34 |
$curr_array = $curr_array[$key]; |
| 35 |
} else { |
| 36 |
return null; |
| 37 |
} |
| 38 |
} |
| 39 |
return $curr_array; |
| 40 |
} |
| 41 |
|
| 42 |
public static function arrayKeyFirst($array) { |
| 43 |
if (!function_exists('array_key_first')) { |
| 44 |
foreach ($array as $key => $value) { |
| 45 |
return $key; |
| 46 |
} |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
return array_key_first($array); |
| 51 |
} |
| 52 |
|
| 53 |
public static function safePregReplace($replace_regex, $replace_string, $element) { |
| 54 |
if (!is_string($replace_regex) || !is_string($replace_string) || !is_string($element)) { |
| 55 |
return $element; |
| 56 |
} |
| 57 |
$updated_element = preg_replace($replace_regex, $replace_string, $element); |
| 58 |
if ($updated_element === null && preg_last_error() !== PREG_NO_ERROR) { |
| 59 |
return $element; |
| 60 |
} |
| 61 |
return $updated_element; |
| 62 |
} |
| 63 |
|
| 64 |
public static function safeStrReplace($search, $replace, $subject) { |
| 65 |
if (!is_string($search) || !is_string($replace) || !is_string($subject)) { |
| 66 |
return $subject; |
| 67 |
} |
| 68 |
$updated_subject = str_replace($search, $replace, $subject); |
| 69 |
if ($updated_subject === null) { |
| 70 |
return $subject; |
| 71 |
} |
| 72 |
return $updated_subject; |
| 73 |
} |
| 74 |
|
| 75 |
public static function preInitWPHook($hook_name, $function_name, $priority, $accepted_args) { |
| 76 |
global $wp_filter; |
| 77 |
|
| 78 |
// Check if $wp_filter is not initialized or not an array |
| 79 |
if (!isset($wp_filter) || !is_array($wp_filter)) { |
| 80 |
$wp_filter = array(); |
| 81 |
} |
| 82 |
|
| 83 |
// Check if the hook exists in $wp_filter |
| 84 |
if (!isset($wp_filter[$hook_name])) { |
| 85 |
$wp_filter[$hook_name] = array(); |
| 86 |
} |
| 87 |
|
| 88 |
// Check if the priority exists for the hook |
| 89 |
if (!isset($wp_filter[$hook_name][$priority])) { |
| 90 |
$wp_filter[$hook_name][$priority] = array(); |
| 91 |
} |
| 92 |
|
| 93 |
// Add the filter function information to the $wp_filter array |
| 94 |
$wp_filter[$hook_name][$priority][] = array( |
| 95 |
'function' => $function_name, |
| 96 |
'accepted_args' => $accepted_args, |
| 97 |
); |
| 98 |
} |
| 99 |
} |
| 100 |
endif; |