VaultPress::contact_service PHP Method

contact_service() public method

public contact_service ( $action, $args = [] )
    function contact_service($action, $args = array())
    {
        if ('test' != $action && 'register' != $action && !$this->check_connection()) {
            return false;
        }
        global $current_user;
        if (!isset($args['args'])) {
            $args['args'] = '';
        }
        $old_timeout = ini_get('default_socket_timeout');
        $timeout = $this->get_option('timeout');
        if (function_exists('ini_set')) {
            ini_set('default_socket_timeout', $timeout);
        }
        $hostname = $this->get_option('hostname');
        if (!class_exists('VaultPress_IXR_SSL_Client')) {
            require_once dirname(__FILE__) . '/class.vaultpress-ixr-ssl-client.php';
        }
        $client = new VaultPress_IXR_SSL_Client($hostname, '/xmlrpc.php', 80, $timeout);
        if ('vaultpress.com' == $hostname) {
            $client->ssl();
        }
        // Begin audit trail breadcrumbs
        if (isset($current_user) && is_object($current_user) && isset($current_user->ID)) {
            $args['cause_user_id'] = intval($current_user->ID);
            $args['cause_user_login'] = (string) $current_user->user_login;
        } else {
            $args['cause_user_id'] = -1;
            $args['cause_user_login'] = '';
        }
        $args['cause_ip'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
        $args['cause_uri'] = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;
        $args['cause_method'] = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null;
        // End audit trail breadcrumbs
        $args['version'] = $this->plugin_version;
        $args['locale'] = get_locale();
        $args['site_url'] = $this->site_url();
        $salt = md5(time() . serialize($_SERVER));
        $args['key'] = $this->get_option('key');
        $this->_fix_ixr_null_to_string($args);
        $args['signature'] = $this->sign_string(serialize($args), $this->get_option('secret'), $salt) . ":{$salt}";
        $client->query('vaultpress.' . $action, new IXR_Base64(serialize($args)));
        $rval = $client->message ? $client->getResponse() : '';
        if (function_exists('ini_set')) {
            ini_set('default_socket_timeout', $old_timeout);
        }
        // we got an error from the servers
        if (is_array($rval) && isset($rval['faultCode'])) {
            $this->update_option('connection', time());
            $this->update_option('connection_error_code', $rval['faultCode']);
            $this->update_option('connection_error_message', $rval['faultString']);
        }
        return $rval;
    }

Usage Example

 /**
  * Get VaultPress site data including, among other things, the date of the last backup if it was completed.
  *
  * @since 4.3.0
  *
  * @return mixed|WP_Error VaultPress site data. Otherwise, a WP_Error instance with the corresponding error.
  */
 public function get_vaultpress_data()
 {
     if (!class_exists('VaultPress')) {
         return new WP_Error('not_active', esc_html__('The requested Jetpack module is not active.', 'jetpack'), array('status' => 404));
     }
     $vaultpress = new VaultPress();
     if (!$vaultpress->is_registered()) {
         return rest_ensure_response(array('code' => 'not_registered', 'message' => esc_html__('You need to register for VaultPress.', 'jetpack')));
     }
     $data = json_decode(base64_decode($vaultpress->contact_service('plugin_data')));
     if (is_wp_error($data)) {
         return $data;
     } else {
         if (!$data->backups->last_backup) {
             return rest_ensure_response(array('code' => 'success', 'message' => esc_html__('VaultPress is active and will back up your site soon.', 'jetpack'), 'data' => $data));
         } else {
             return rest_ensure_response(array('code' => 'success', 'message' => esc_html(sprintf(__('Your site was successfully backed-up %s ago.', 'jetpack'), human_time_diff($data->backups->last_backup, current_time('timestamp')))), 'data' => $data));
         }
     }
 }
VaultPress