/** * Request Instagram access token. * * @since 1.2 * * @return void */ public function instagram_request_access_token() { // Instagram return URL has 'code' parameter if (!isset($_GET['code']) && empty($_GET['code'])) { return; } $settings = get_option('clean_social_feeds_settings'); $response = Clean_Social_Feeds::remoteUrl('post', 'https://api.instagram.com/oauth/access_token', array('body' => array('client_id' => $settings['instagram_client_id'], 'client_secret' => $settings['instagram_client_secret'], 'grant_type' => 'authorization_code', 'redirect_uri' => admin_url() . 'options-general.php?page=clean_social_feeds', 'code' => $_GET['code']))); if (is_wp_error($response)) { add_settings_error('Instagram Access Token', 'instagram-access-token', __('Error requesting Instagram access token. Check your settings and try again.', 'clean-social-feeds')); return; } $response = json_decode($response, true); $settings['instagram_access_token'] = $response['access_token']; update_option('clean_social_feeds_settings', $settings); add_settings_error('Instagram Access Token', 'instagram-access-token', __('Instagram access token requested successfully.', 'clean-social-feeds'), 'updated'); }
/** * Get Instagram User posts. * * @since 1.2 * * @param $array $args { * Arguments for loading the posts. * * @param bool cache If we shoud cache the results. * @param int cache_time Time to cache the results. * @param int limit Number of posts to get. * @param string user_id Instagram user ID to get posts from. Defaults to 'self'. * } * @return WP_Error|Array Error message or array of posts. */ function getInstagramUserPosts($args = null) { // Set sensible defaults $args = wp_parse_args($args, array('cache' => true, 'cache_time' => 60 * 60, 'limit' => 10, 'user_id' => 'self')); // Check transient for existing results $transient = $this->_maybeGetTransient($args['cache'], 'instagram_' . $args['user_id']); if (false !== $transient) { return json_decode($transient, true); } if (empty($this->instagram_client_id) || empty($this->instagram_client_secret) || empty($this->instagram_access_token) || empty($args['user_id'])) { return new WP_Error('clean-social-feeds', 'Missing Instagram App information, check your settings.'); } $response = Clean_Social_Feeds::remoteUrl('get', 'https://api.instagram.com/v1/users/' . $args['user_id'] . '/media/recent/?access_token=' . $this->instagram_access_token . '&count=' . $args['limit']); if (is_wp_error($response)) { return $response; } // Set transient $this->_maybeSetTransient($args['cache'], 'instagram_' . $args['user_id'], $args['cache_time'], $response); $this->instagram_posts = json_decode($response, true); return $this->instagram_posts; }