示例#1
0
 public function request(array $request)
 {
     if (is_wp_error($youtube = $this->get_connection())) {
         return $youtube;
     }
     $params = $request['params'];
     switch ($params['tab']) {
         case 'by_user':
             $request = array('channel' => sanitize_text_field($params['channel']), 'type' => 'video', 'page_token' => sanitize_text_field($params['page_token']));
             //if ( isset( $params['page_token'] ) && '' !== $params['page_token'] )
             //$request['page_token'] = sanitize_text_field( $params['page_token'] );
             // Make the request to the YouTube API
             $search_response = $youtube->get_videos_from_channel($request);
             break;
         default:
         case 'all':
             $request = array('q' => sanitize_text_field($params['q']), 'maxResults' => self::DEFAULT_MAX_RESULTS);
             if (isset($params['page_token']) && '' !== $params['page_token']) {
                 $request['page_token'] = sanitize_text_field($params['page_token']);
             }
             if (isset($params['type'])) {
                 $request['type'] = sanitize_text_field($params['type']);
             }
             // Make the request to the YouTube API
             $search_response = $youtube->get_videos($request);
             break;
     }
     // Create the response for the API
     $response = new MEXP_Response();
     if (!isset($search_response['items'])) {
         return false;
     }
     foreach ($search_response['items'] as $index => $search_item) {
         $item = new MEXP_Response_Item();
         if ($request['type'] == 'video' && isset($request['q'])) {
             // For videos searched by query
             $item->set_url(esc_url(sprintf("http://www.youtube.com/watch?v=%s", $search_item['id']['videoId'])));
         } elseif ($request['type'] == 'playlist' && isset($request['q'])) {
             // For playlists searched by query
             $item->set_url(esc_url(sprintf("http://www.youtube.com/playlist?list=%s", $search_item['id']['playlistId'])));
         } else {
             // For videos searched by channel name
             $item->set_url(esc_url(sprintf("http://www.youtube.com/watch?v=%s", $search_item['snippet']['resourceId']['videoId'])));
         }
         $item->add_meta('user', $search_item['snippet']['channelTitle']);
         $item->set_id((int) $params['startIndex'] + (int) $index);
         $item->set_content($search_item['snippet']['title']);
         $item->set_thumbnail($search_item['snippet']['thumbnails']['medium']['url']);
         $item->set_date(strtotime($search_item['snippet']['publishedAt']));
         $item->set_date_format('g:i A - j M y');
         $response->add_item($item);
     }
     if (isset($search_response['nextPageToken'])) {
         $response->add_meta('page_token', $search_response['nextPageToken']);
     }
     return $response;
 }
示例#2
0
 public function response($r)
 {
     if (!isset($r->statuses) or empty($r->statuses)) {
         return false;
     }
     $response = new MEXP_Response();
     if (isset($r->search_metadata->next_results)) {
         $response->add_meta('max_id', self::get_max_id($r->search_metadata->next_results));
     }
     if (isset($this->response_meta)) {
         $response->add_meta($this->response_meta);
     }
     foreach ($r->statuses as $status) {
         $item = new MEXP_Response_Item();
         $item->set_id($status->id_str);
         $item->set_url(self::status_url($status));
         $item->set_content(self::status_content($status));
         $item->set_thumbnail(is_ssl() ? $status->user->profile_image_url_https : $status->user->profile_image_url);
         $item->set_date(strtotime($status->created_at));
         $item->set_date_format('g:i A - j M y');
         $item->add_meta('user', array('name' => $status->user->name, 'screen_name' => $status->user->screen_name));
         $response->add_item($item);
     }
     return $response;
 }
示例#3
0
 public function response($r)
 {
     if (empty($r['data'])) {
         return false;
     }
     $response = new MEXP_Response();
     foreach ($r['data']->data as $result) {
         $item = new MEXP_Response_Item();
         $item->set_id($result->id);
         $item->set_url($result->link);
         // Not all results have a caption
         if (is_object($result->caption)) {
             $item->set_content($result->caption->text);
         }
         $item->set_thumbnail(set_url_scheme($result->images->thumbnail->url));
         $item->set_date($result->created_time);
         $item->set_date_format('g:i A - j M y');
         $item->add_meta('user', array('username' => $result->user->username));
         $response->add_item($item);
     }
     // Pagination details
     if (!empty($r['data']->pagination)) {
         if (isset($r['data']->pagination->next_max_id)) {
             $response->add_meta('max_id', $r['data']->pagination->next_max_id);
         }
         if (isset($r['data']->pagination->next_min_id)) {
             $response->add_meta('min_id', $r['data']->pagination->next_min_id);
         }
     }
     return $response;
 }
示例#4
0
 /**
  * Calls the Google PHP API during AJAX request time.
  *
  * @param array $r Arguments.
  */
 public function response($r)
 {
     // create Google Drive service
     $service = new Google_Service_Drive($this->client);
     // Show all Google Drive files
     if (isset($r['nomore'])) {
         return;
     }
     $files = $this->list_files($service, $r);
     $response = new MEXP_Response();
     // Pagination details
     if (isset($files['pageToken'])) {
         $response->add_meta('max_id', $files['pageToken']);
         unset($files['pageToken']);
     }
     if (empty($files)) {
         return;
     }
     //ray_log( 'files: ' . print_r( $files, true ) );
     foreach ($files as $file) {
         $item = new MEXP_Response_Item();
         $item->set_id($file->getId());
         $item->set_url($file->getSelfLink());
         $item->set_content($file->getTitle());
         $item->set_date(strtotime($file->getModifiedDate()));
         $item->set_date_format('g:i A - j M y');
         // other variables
         $nothumb = false;
         $thumb = str_replace('s220', 'w200-h150-p-k-nu', $file->getThumbnailLink());
         $icon = $file->iconLink;
         // no thumb? use file icon
         if (empty($thumb)) {
             $nothumb = true;
             // generic icons need a different URL format
             if (false !== strpos($file->iconLink, 'generic')) {
                 $icon = 'https://drive-thirdparty.googleusercontent.com/16/type/application/' . substr($file->getMimeType(), 0, strpos($file->getMimeType(), '/'));
                 $thumb = 'https://drive-thirdparty.googleusercontent.com/128/type/application/' . substr($file->getMimeType(), 0, strpos($file->getMimeType(), '/'));
             } else {
                 $thumb = str_replace('https://ssl.gstatic.com/docs/doclist/images/icon_', '', $file->iconLink);
                 $thumb = str_replace('_list.png', '', $thumb);
                 $thumb = substr($thumb, strpos($thumb, '_') + 1);
                 $thumb = "https://ssl.gstatic.com/docs/doclist/images/mediatype/icon_1_{$thumb}_x128.png";
             }
             // some thumbs require access token to render
             // @see http://stackoverflow.com/a/14865218
         } elseif (false !== strpos($thumb, '/feeds/')) {
             $token = json_decode($this->client->getAccessToken());
             if (isset($token->access_token)) {
                 $thumb .= '&access_token=' . $token->access_token;
             }
         }
         $item->set_thumbnail($thumb);
         //$owners = $file->getOwnerNames();
         // truncated file type
         if (false !== strpos($file->getMimeType(), 'google-apps')) {
             $type = substr(strrchr($file->getMimeType(), '.'), 1);
         } else {
             $type = substr($file->getMimeType(), 0, strpos($file->getMimeType(), '/'));
         }
         // set up file meta
         $file_meta = array('icon' => $icon, 'type' => $type, 'dateCreated' => date($item->date_format, strtotime($file->getCreatedDate())), 'nothumb' => $nothumb);
         // add marker to tell that this is a gdoc
         $item->add_meta('gdoc', 1);
         // this is the old way of passing data... aka. meta-stuffing
         $item->add_meta('file', $file_meta);
         $response->add_item($item);
     }
     return $response;
 }