__( 'Completa correctamente todos los campos.', '360-connect-hub' ) ), 400 ); } if ( strlen( $message ) < 3 ) { wp_send_json_error( array( 'message' => __( 'El mensaje es demasiado corto.', '360-connect-hub' ) ), 400 ); } $country = $this->detect_country(); $inserted = Net360_360CH_Leads::insert( compact( 'name', 'email', 'agent', 'message', 'page', 'country' ) ); if ( false === $inserted ) { wp_send_json_error( array( 'message' => __( 'No se pudo guardar el lead.', '360-connect-hub' ) ), 500 ); } wp_send_json_success(); } private function detect_country() { if ( ! get_option( 'n360wp_country_detection', 1 ) ) return ''; if ( ! empty( $_SERVER['HTTP_CF_IPCOUNTRY'] ) ) return sanitize_text_field( wp_unslash( $_SERVER['HTTP_CF_IPCOUNTRY'] ) ); $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; if ( ! $ip || ! filter_var( $ip, FILTER_VALIDATE_IP ) || $this->is_private_ip( $ip ) ) return ''; $url = 'https://free.freeipapi.com/api/json/' . rawurlencode( $ip ); $response = wp_remote_get( $url, array( 'timeout' => 3, 'redirection' => 0, 'headers' => array( 'Accept' => 'application/json', 'User-Agent' => '360-Chat/' . NET360_360CH_VERSION . '; ' . home_url( '/' ), ), ) ); if ( is_wp_error( $response ) ) return ''; $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); $country = isset( $data['countryCode'] ) ? strtoupper( sanitize_text_field( $data['countryCode'] ) ) : ''; return preg_match( '/^[A-Z]{2}$/', $country ) ? $country : ''; } private function is_private_ip( $ip ) { return ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ); } }