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