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