/** * Handles the AJAX request and returns an appropriate response. This should be used, for example, to perform an API request to the service provider and return the results. * * @param array $request The request parameters. * @return MEXP_Response|bool|WP_Error A MEXP_Response object should be returned on success, boolean false should be returned if there are no results to show, and a WP_Error should be returned if there is an error. */ public function request(array $request) { $response = new MEXP_Response(); // Ensure that 'page' is never 0. This breaks things. $request['page'] = $request['page'] < 1 ? 1 : $request['page']; // Build the request URL. $args = array_map('rawurlencode', apply_filters('resourcespace_request_args', array('search' => sanitize_text_field($request['params']['q']), 'key' => PJ_RESOURCE_SPACE_KEY, 'previewsize' => 'pre', 'original' => true, 'results_per_page' => PJ_RESOURCE_SPACE_RESULTS_PER_PAGE, 'page' => absint($request['page']), 'restypes' => 1))); $api_url = add_query_arg($args, sprintf('%s/plugins/api_search/', PJ_RESOURCE_SPACE_DOMAIN)); $request_args = array('headers' => array()); // Pass basic auth header if available. if (defined('PJ_RESOURCE_SPACE_AUTHL') && defined('PJ_RESOURCE_SPACE_AUTHP')) { $request_args['headers']['Authorization'] = 'Basic ' . base64_encode(PJ_RESOURCE_SPACE_AUTHL . ':' . PJ_RESOURCE_SPACE_AUTHP); } $api_response = wp_remote_get($api_url, $request_args); if (200 !== wp_remote_retrieve_response_code($api_response)) { return $api_response; } $response_data = json_decode(wp_remote_retrieve_body($api_response)); foreach ((array) $response_data->resources as $resource) { $dirty_data = array('title' => $resource->field8, 'date' => strtotime($resource->creation_date), 'id' => $resource->ref, 'thumbnail' => $resource->preview, 'url' => null); $dirty_data = apply_filters('resourcespace_parse_raw_image_data', $dirty_data, $resource); $clean_data = array(); foreach ($this->get_item_fields() as $field => $args) { $clean_data[$field] = ''; if (isset($dirty_data[$field])) { $clean_data[$field] = call_user_func($args['sanitize_callback'], $dirty_data[$field]); } elseif (!isset($dirty_data[$field]) && isset($args['default'])) { $clean_data[$field] = $args['default']; } } $item = new MEXP_Response_Item(); $item->set_content($clean_data['title']); $item->set_date($clean_data['date']); $item->set_date_format($clean_data['date_format']); $item->set_id($clean_data['id']); $item->set_url($clean_data['url']); $item->set_thumbnail($clean_data['thumbnail']); $response->add_item($item); } $response->add_meta('per_page', $response_data->pagination->per_page); $response->add_meta('page', $response_data->pagination->page); $response->add_meta('total_pages', $response_data->pagination->total_pages); $response->add_meta('total_resources', $response_data->pagination->total_resources); return $response; }
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; }
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; }
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; }
/** * 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; }