| 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 |
* Build the private nonce action for a plugin using this SDK. |
| 17 |
* |
| 18 |
* The public 'wp_rest' action must not be used on its own: WordPress hands |
| 19 |
* the same 'wp_rest' nonce to every logged out visitor, and plugins print |
| 20 |
* it on public pages, so it proves nothing about who is calling. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
* |
| 24 |
* @param string $identifier |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public static function get_nonce_action( $identifier ) { |
| 29 |
return 'ens_' . $identifier . '_flow'; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* will verify nonce |
| 34 |
* |
| 35 |
* @return array |
| 36 |
*/ |
| 37 |
public static function ens_verify_nonce( $nonce, $identifier ) { |
| 38 |
if ( ! empty( $nonce ) && wp_verify_nonce( $nonce, self::get_nonce_action( $identifier ) ) ) { |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
// Backward compatibility: plugins bundling an older SDK still send a |
| 43 |
// 'wp_rest' nonce. This is only a CSRF check; authorization is enforced |
| 44 |
// by the capability check in each route's permission_callback. |
| 45 |
if ( ! empty( $nonce ) && wp_verify_nonce( $nonce, 'wp_rest' ) ) { |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
$response = [ |
| 50 |
'success' => 0, |
| 51 |
'status_code' => 403, |
| 52 |
'message' => __( 'Nonce not verified', self::get_config_data( $identifier,'text_domain' ) ), |
| 53 |
'data' => [], |
| 54 |
]; |
| 55 |
return new WP_HTTP_Response( $response, 403 ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* will sanitize recursive |
| 60 |
* |
| 61 |
* @return array |
| 62 |
*/ |
| 63 |
public static function ens_sanitize_recursive( $input ) { |
| 64 |
if ( is_array( $input ) ) { |
| 65 |
|
| 66 |
foreach ( $input as $key => $value ) { |
| 67 |
$input[$key] = ens_sanitize_recursive( $value ); |
| 68 |
} |
| 69 |
|
| 70 |
} elseif ( is_object( $input ) ) { |
| 71 |
|
| 72 |
foreach ( $input as $key => $value ) { |
| 73 |
$input->$key = ens_sanitize_recursive( $value ); |
| 74 |
} |
| 75 |
|
| 76 |
} else { |
| 77 |
if ( filter_var( $input, FILTER_VALIDATE_URL ) !== false ) { |
| 78 |
$input = sanitize_url( $input ); |
| 79 |
} elseif ( is_email( $input ) ) { |
| 80 |
$input = sanitize_email( $input ); |
| 81 |
} elseif ( is_int( $input ) ) { |
| 82 |
$input = intval( $input ); |
| 83 |
} elseif ( is_float( $input ) ) { |
| 84 |
$input = floatval( $input ); |
| 85 |
} elseif ( is_string( $input ) ) { |
| 86 |
if ( strpos( $input, '<svg' ) === false ) { |
| 87 |
$input = wp_kses_post( $input ); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $input; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* will return post status |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public static function ens_get_post_status() { |
| 101 |
return ['publish', 'draft', 'trash']; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get config data value with fallback |
| 106 |
* |
| 107 |
* @param string $prefix |
| 108 |
* @param string $value_of |
| 109 |
* @return mixed|null |
| 110 |
*/ |
| 111 |
public static function get_config_data($prefix, $value_of) { |
| 112 |
$key = $prefix . '_ens_config'; |
| 113 |
|
| 114 |
$data = get_option( $key ); |
| 115 |
|
| 116 |
if (isset($data[$value_of])) { |
| 117 |
return $data[$value_of]; |
| 118 |
} |
| 119 |
|
| 120 |
return null; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Build a plugin-scoped hook name. |
| 125 |
* |
| 126 |
* Prepends the consumer plugin's hook_prefix (or general_prefix as |
| 127 |
* fallback) to the given suffix so hooks fired from the SDK do not |
| 128 |
* collide when the package is bundled in multiple plugins. |
| 129 |
* |
| 130 |
* @param string $identifier The consumer plugin identifier (general_prefix). |
| 131 |
* @param string $hook_suffix The unscoped hook name (e.g. 'email_body'). |
| 132 |
* |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public static function get_hook_name( $identifier, $hook_suffix ) { |
| 136 |
$prefix = self::get_config_data( $identifier, 'hook_prefix' ); |
| 137 |
|
| 138 |
if ( empty( $prefix ) ) { |
| 139 |
$prefix = $identifier; |
| 140 |
} |
| 141 |
|
| 142 |
return $prefix . '_' . $hook_suffix; |
| 143 |
} |
| 144 |
} |