コード例 #1
0
ファイル: Instagram.php プロジェクト: bpea/amtrak-careers
 public static function get_data($slides, $options, $type)
 {
     if ($type === 'instagram') {
         if (isset($options['rs_instagram'])) {
             self::$access_token = get_option('new_royalslider_instagram_oauth_token');
             if (self::$access_token && isset(self::$access_token->access_token)) {
                 self::$access_token = self::$access_token->access_token;
             } else {
                 return "Instagram access token is missing, go to RoyalSlider global settings and connect your Instagram account.";
             }
             $options = $options['rs_instagram'];
             if (!isset($options['usernameortag']) || !trim($options['usernameortag'])) {
                 return __('Enter Instagram username or tag.', 'new_royalslider');
             }
             $max_images = (int) $options['limit'];
             if (!($max_images > 0)) {
                 return __('Incorrect value in "Limit" option, set it to integer.', 'new_royalslider');
             }
             if ($max_images > 300) {
                 $max_images = 300;
             }
             $data = array('access_token' => self::$access_token, 'count' => $max_images);
             $is_tag = strpos($options['usernameortag'], '#') > -1 ? true : false;
             if ($is_tag) {
                 $tag = str_replace('#', '', $options['usernameortag']);
                 $url = 'https://api.instagram.com/v1/tags/' . $tag . '/media/recent?' . http_build_query($data);
             } else {
                 $user_id = self::getInstagramUserID($options['usernameortag']);
                 if (!$user_id) {
                     return 'Username ' . $options['usernameortag'] . ' not found';
                 }
                 $url = 'https://api.instagram.com/v1/users/' . $user_id . '/media/recent?' . http_build_query($data);
             }
             $url = apply_filters('new_rs_instagram_api_url', $url, $options);
             $images = array();
             $gotAllResults = false;
             $numPages = 1;
             $imageCount = 0;
             while (!$gotAllResults) {
                 $response = wp_remote_get($url, array('timeout' => 30, 'redirection' => 5, 'sslverify' => false));
                 if (is_wp_error($response)) {
                     $gotAllResults = true;
                     if (count($images) > 0) {
                         break;
                     } else {
                         return print_r($response, true) . ' Request URL: ' . $url;
                     }
                 }
                 $response = $response['body'];
                 $response = json_decode($response, ARRAY_A);
                 if (isset($response['pagination']) && isset($response['pagination']['next_url'])) {
                     $url = $response['pagination']['next_url'];
                 } else {
                     $gotAllResults = true;
                 }
                 if (isset($response['data'])) {
                     $response_items = $response['data'];
                     foreach ($response_items as $key => $item) {
                         $imageCount++;
                         if ($imageCount > $max_images) {
                             $gotAllResults = true;
                             break;
                         }
                         $image =& $images[$imageCount];
                         $image['image'] = $item['images']['standard_resolution']['url'];
                         $image['thumbnail'] = $item['images']['thumbnail']['url'];
                         //$image['title'] = isset($item['caption']) ? $item['caption']['text'] : '';
                         $image['title'] = isset($item['caption']) ? htmlspecialchars($item['caption']['text']) : '';
                         $image['title'] = mb_convert_encoding($image['title'], 'HTML-ENTITIES', 'utf-8');
                         $image['original_obj'] = $item;
                     }
                 }
                 if ($imageCount > 300 || $numPages > 20) {
                     $gotAllResults = true;
                     break;
                 }
                 $numPages++;
             }
             if (count($images) > 0) {
                 return $images;
             } else {
                 return sprintf(__('No images found. %s', 'new_royalslider'), isset($response['error']) ? $response['error'] : '');
             }
             return $images;
         }
     }
     return $slides;
 }