Jetpack_Protect_Module::protect_call PHP Method

protect_call() public method

Calls over to the api using wp_remote_post
public protect_call ( string $action = 'check_ip', array $request = [] ) : array
$action string 'check_ip', 'check_key', or 'failed_attempt'
$request array Any custom data to post to the api
return array
    function protect_call($action = 'check_ip', $request = array())
    {
        global $wp_version, $wpdb, $current_user;
        $api_key = $this->maybe_get_protect_key();
        $user_agent = "WordPress/{$wp_version} | Jetpack/" . constant('JETPACK__VERSION');
        $request['action'] = $action;
        $request['ip'] = jetpack_protect_get_ip();
        $request['host'] = $this->get_local_host();
        $request['headers'] = json_encode($this->get_headers());
        $request['jetpack_version'] = constant('JETPACK__VERSION');
        $request['wordpress_version'] = strval($wp_version);
        $request['api_key'] = $api_key;
        $request['multisite'] = "0";
        if (is_multisite()) {
            $request['multisite'] = get_blog_count();
        }
        /**
         * Filter controls maximum timeout in waiting for reponse from Protect servers.
         *
         * @module protect
         *
         * @since 4.0.4
         *
         * @param int $timeout Max time (in seconds) to wait for a response.
         */
        $timeout = apply_filters('jetpack_protect_connect_timeout', 30);
        $args = array('body' => $request, 'user-agent' => $user_agent, 'httpversion' => '1.0', 'timeout' => absint($timeout));
        $response_json = wp_remote_post($this->get_api_host(), $args);
        $this->last_response_raw = $response_json;
        $headers = $this->get_headers();
        $header_hash = md5(json_encode($headers));
        $transient_name = 'jpp_li_' . $header_hash;
        $this->delete_transient($transient_name);
        if (is_array($response_json)) {
            $response = json_decode($response_json['body'], true);
        }
        if (isset($response['blocked_attempts']) && $response['blocked_attempts']) {
            update_site_option('jetpack_protect_blocked_attempts', $response['blocked_attempts']);
        }
        if (isset($response['status']) && !isset($response['error'])) {
            $response['expire'] = time() + $response['seconds_remaining'];
            $this->set_transient($transient_name, $response, $response['seconds_remaining']);
            $this->delete_transient('brute_use_math');
        } else {
            // Fallback to Math Captcha if no response from API host
            $this->set_transient('brute_use_math', 1, 600);
            $response['status'] = 'ok';
            $response['math'] = true;
        }
        if (isset($response['error'])) {
            update_site_option('jetpack_protect_error', $response['error']);
        } else {
            delete_site_option('jetpack_protect_error');
        }
        return $response;
    }

Usage Example

Esempio n. 1
0
 /**
  * Sends a "check_key" API call once a day.  This call allows us to track IP-related
  * headers for this server via the Protect API, in order to better identify the source
  * IP for login attempts
  */
 public function maybe_update_headers($force = false)
 {
     $updated_recently = $this->get_transient('jpp_headers_updated_recently');
     if (!$force) {
         if (isset($_GET['protect_update_headers'])) {
             $force = true;
         }
     }
     // check that current user is admin so we prevent a lower level user from adding
     // a trusted header, allowing them to brute force an admin account
     if ($updated_recently && !$force || !current_user_can('update_plugins')) {
         return;
     }
     $response = Jetpack_Protect_Module::protect_call('check_key');
     $this->set_transient('jpp_headers_updated_recently', 1, DAY_IN_SECONDS);
     if (isset($response['msg']) && $response['msg']) {
         update_site_option('trusted_ip_header', json_decode($response['msg']));
     }
 }
All Usage Examples Of Jetpack_Protect_Module::protect_call