| 1 |
<?php |
| 2 |
namespace Ens\Utils; |
| 3 |
|
| 4 |
class Helpers { |
| 5 |
|
| 6 |
/** |
| 7 |
* will return strings after translation |
| 8 |
* |
| 9 |
* @return array |
| 10 |
*/ |
| 11 |
public static function ens_get_translatable_strings( $string, $text_domain ) { |
| 12 |
return $string; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* will verify nonce |
| 17 |
* |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
public static function ens_verify_nonce( $nonce, $identifier ) { |
| 21 |
$is_local = isset( $_SERVER['REMOTE_ADDR'] ) && in_array( $_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'] ); |
| 22 |
|
| 23 |
if ( ( isset( $nonce ) && wp_verify_nonce( $nonce, 'wp_rest' ) ) || $is_local ) { |
| 24 |
return true; |
| 25 |
} |
| 26 |
|
| 27 |
$response = [ |
| 28 |
'success' => 0, |
| 29 |
'status_code' => 403, |
| 30 |
'message' => __( 'Nonce not verified', self::get_config_data( $identifier,'text_domain' ) ), |
| 31 |
'data' => [], |
| 32 |
]; |
| 33 |
return new WP_HTTP_Response( $response, 403 ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* will sanitize recursive |
| 38 |
* |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
public static function ens_sanitize_recursive( $input ) { |
| 42 |
if ( is_array( $input ) ) { |
| 43 |
|
| 44 |
foreach ( $input as $key => $value ) { |
| 45 |
$input[$key] = ens_sanitize_recursive( $value ); |
| 46 |
} |
| 47 |
|
| 48 |
} elseif ( is_object( $input ) ) { |
| 49 |
|
| 50 |
foreach ( $input as $key => $value ) { |
| 51 |
$input->$key = ens_sanitize_recursive( $value ); |
| 52 |
} |
| 53 |
|
| 54 |
} else { |
| 55 |
if ( filter_var( $input, FILTER_VALIDATE_URL ) !== false ) { |
| 56 |
$input = sanitize_url( $input ); |
| 57 |
} elseif ( is_email( $input ) ) { |
| 58 |
$input = sanitize_email( $input ); |
| 59 |
} elseif ( is_int( $input ) ) { |
| 60 |
$input = intval( $input ); |
| 61 |
} elseif ( is_float( $input ) ) { |
| 62 |
$input = floatval( $input ); |
| 63 |
} elseif ( is_string( $input ) ) { |
| 64 |
if ( strpos( $input, '<svg' ) === false ) { |
| 65 |
$input = wp_kses_post( $input ); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return $input; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* will return post status |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public static function ens_get_post_status() { |
| 79 |
return ['publish', 'draft', 'trash']; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get config data value with fallback |
| 84 |
* |
| 85 |
* @param string $prefix |
| 86 |
* @param string $value_of |
| 87 |
* @return mixed|null |
| 88 |
*/ |
| 89 |
public static function get_config_data($prefix, $value_of) { |
| 90 |
$key = $prefix . '_ens_config'; |
| 91 |
|
| 92 |
$data = get_option( $key ); |
| 93 |
|
| 94 |
if (isset($data[$value_of])) { |
| 95 |
return $data[$value_of]; |
| 96 |
} |
| 97 |
|
| 98 |
return null; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Build a plugin-scoped hook name. |
| 103 |
* |
| 104 |
* Prepends the consumer plugin's hook_prefix (or general_prefix as |
| 105 |
* fallback) to the given suffix so hooks fired from the SDK do not |
| 106 |
* collide when the package is bundled in multiple plugins. |
| 107 |
* |
| 108 |
* @param string $identifier The consumer plugin identifier (general_prefix). |
| 109 |
* @param string $hook_suffix The unscoped hook name (e.g. 'email_body'). |
| 110 |
* |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public static function get_hook_name( $identifier, $hook_suffix ) { |
| 114 |
$prefix = self::get_config_data( $identifier, 'hook_prefix' ); |
| 115 |
|
| 116 |
if ( empty( $prefix ) ) { |
| 117 |
$prefix = $identifier; |
| 118 |
} |
| 119 |
|
| 120 |
return $prefix . '_' . $hook_suffix; |
| 121 |
} |
| 122 |
} |