| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Classes\Http; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Plugin; |
| 7 |
|
| 8 |
// If this file is called directly, abort. |
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
class Http_Tools { |
| 14 |
|
| 15 |
protected static $query; |
| 16 |
|
| 17 |
public static function get_site_host() { |
| 18 |
return str_ireplace( 'www.', '', wp_parse_url( home_url(), PHP_URL_HOST ) ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Get current user IP Address. |
| 23 |
* |
| 24 |
* Taken from: |
| 25 |
* https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-geolocation.php#L80-L91 |
| 26 |
* (WC_Geolocation::get_ip_address) |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public static function get_ip_address(): string { |
| 31 |
if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) { |
| 32 |
return sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) ); |
| 33 |
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 34 |
// Proxy servers can send through this header like this: X-Forwarded-For: client1, proxy1, proxy2 |
| 35 |
// Make sure we always only send through the first IP in the list which should always be the client IP. |
| 36 |
return (string) rest_is_ip_address( trim( current( preg_split( '/,/', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) ) ) ); |
| 37 |
} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
| 38 |
return sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); |
| 39 |
} |
| 40 |
|
| 41 |
return ''; |
| 42 |
} |
| 43 |
|
| 44 |
public static function get_server_ip_address(): string { |
| 45 |
if ( empty( $_SERVER['SERVER_ADDR'] ) && empty( $_SERVER['LOCAL_ADDR'] ) ) { |
| 46 |
return ''; |
| 47 |
} |
| 48 |
|
| 49 |
return sanitize_text_field( |
| 50 |
wp_unslash( ! empty( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'] ) |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
public static function get_user_agent(): string { |
| 55 |
return sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ); |
| 56 |
} |
| 57 |
|
| 58 |
public static function replace_path_args( string $path, array $path_args ): string { |
| 59 |
$patterns = array(); |
| 60 |
|
| 61 |
foreach ( $path_args as $key => $value ) { |
| 62 |
$patterns[ "#\(\?P<$key\>\S+\)#" ] = function () use ( $value ) { |
| 63 |
return (string) $value; |
| 64 |
}; |
| 65 |
} |
| 66 |
|
| 67 |
return implode( |
| 68 |
'/', |
| 69 |
preg_replace_callback_array( |
| 70 |
$patterns, |
| 71 |
explode( '/', $path ) |
| 72 |
) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns form action url |
| 78 |
* |
| 79 |
* @param array $args |
| 80 |
* |
| 81 |
* @return string [type] [description] |
| 82 |
*/ |
| 83 |
public static function get_form_action_url( array $args = array() ): string { |
| 84 |
global $wp; |
| 85 |
|
| 86 |
$args = array_merge( |
| 87 |
array( |
| 88 |
Plugin::instance()->form_handler->hook_key => Plugin::instance()->form_handler->hook_val, |
| 89 |
'method' => 'reload', |
| 90 |
), |
| 91 |
$args |
| 92 |
); |
| 93 |
|
| 94 |
$action = add_query_arg( |
| 95 |
$args, |
| 96 |
trailingslashit( |
| 97 |
home_url( $wp->request ) |
| 98 |
) |
| 99 |
); |
| 100 |
|
| 101 |
return apply_filters( 'jet-form-builder/form-action-url', $action ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns form refer url |
| 106 |
* |
| 107 |
* @return [type] [description] |
| 108 |
*/ |
| 109 |
public static function get_form_refer_url(): string { |
| 110 |
|
| 111 |
global $wp; |
| 112 |
|
| 113 |
$refer = user_trailingslashit( home_url( $wp->request ) ); |
| 114 |
|
| 115 |
if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { |
| 116 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 117 |
$refer = trailingslashit( $refer ) . '?' . $_SERVER['QUERY_STRING']; |
| 118 |
} |
| 119 |
|
| 120 |
return apply_filters( 'jet-form-builder/form-refer-url', $refer ); |
| 121 |
} |
| 122 |
|
| 123 |
public static function get_query(): array { |
| 124 |
if ( ! is_null( self::$query ) ) { |
| 125 |
return self::$query; |
| 126 |
} |
| 127 |
|
| 128 |
if ( ! jet_fb_handler()->is_process() ) { |
| 129 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 130 |
self::$query = wp_unslash( $_GET ); |
| 131 |
|
| 132 |
return self::$query; |
| 133 |
} |
| 134 |
|
| 135 |
$query = array(); |
| 136 |
|
| 137 |
wp_parse_str( |
| 138 |
wp_parse_url( jet_fb_handler()->refer, PHP_URL_QUERY ), |
| 139 |
$query |
| 140 |
); |
| 141 |
|
| 142 |
self::$query = wp_unslash( $query ); |
| 143 |
|
| 144 |
return self::$query; |
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|