/** * Example WDS_WP_REST_API_Connect usage */ function wp_json_api_connect_example_test() { // Consumer credentials $consumer = array('consumer_key' => 'YOUR CONSUMER KEY', 'consumer_secret' => 'YOUR CONSUMER SECRET', 'json_url' => 'REST API URL OF SITE'); $api = new WDS_WP_REST_API_Connect($consumer); $auth_url = $api->get_authorization_url(); // Only returns URL if not yet authenticated if ($auth_url) { echo '<div id="message" class="updated">'; echo '<p><a href="' . esc_url($auth_url) . '" class="button">Authorize Connection</a></p>'; echo '</div>'; // Do not proceed return; } $post_id_to_view = 1; $response = $api->auth_get_request('posts/' . $post_id_to_view); if (is_wp_error($response)) { echo '<div id="message" class="error">'; echo wpautop($response->get_error_message()); echo '</div>'; } else { echo '<div id="message" class="updated">'; echo '<p><strong>' . $response['title'] . ' retrieved!</strong></p>'; echo '<xmp>auth_get_request $response: ' . print_r($response, true) . '</xmp>'; echo '</div>'; } $post_id_to_update = 1; $updated_data = array('title' => 'Hello REST API World!'); $response = $api->auth_post_request('posts/' . $post_id_to_update, $updated_data); if (is_wp_error($response)) { echo '<div id="message" class="error">'; echo wpautop($response->get_error_message()); echo '</div>'; } else { echo '<div id="message" class="updated">'; echo '<p><strong>Post updated!</strong></p>'; echo '<xmp>auth_post_request $response: ' . print_r($response, true) . '</xmp>'; echo '</div>'; } }
/** * Return (and initiate) API object. * * @return WDS_Network_Connect_API_Connect */ public function api() { if (!empty($this->api->key)) { // Has already been initated return $this->api; } $all = $this->get('all'); $all = is_array($all) ? array_filter($all) : false; // Make sure we have the bare minimums saved for making a connection. if (empty($all) || !$this->get('url') || !$this->get('consumer_key') || !$this->get('consumer_secret')) { return $this->api; } $args['consumer_key'] = $this->get('consumer_key'); $args['consumer_secret'] = $this->get('consumer_secret'); $args['json_url'] = $this->get('api_url'); if ($this->get('header_key') && $this->get('header_token')) { $args['headers'] = array($this->get('header_key') => $this->get('header_token')); } // Initate the API. return $this->api->init($args); }
/** * Handles deleting the stored data for a connection * * @since 0.1.0 * * @return bool Result of delete_option or delete_site_option */ public function delete_entire_option() { $deleted = parent::delete_entire_option(); if ($this->is_network) { $deleted = delete_site_option($this->option_key); } return $deleted; }