/** * Get all the remote extensions * * @access public * @return array $remote_extensions All the remote extensions */ function msa_get_remote_extensions() { $remote_extensions = array(); $response = vip_safe_wp_remote_get('https://99robots.com/msa-extensions.json'); if (is_array($response)) { $remote_extensions = json_decode($response['body'], true); } return $remote_extensions; }
public function get($url, $headers = array()) { $this->response = vip_safe_wp_remote_get($url, '', 3, 1, 20, compact('headers')); if (is_wp_error($this->response)) { $this->last_error = $this->response->get_error_message(); return false; } $this->response_code = wp_remote_retrieve_response_code($this->response); return wp_remote_retrieve_body($this->response); }
/** * Checks for failed services * * Checks for failed Brightcove services. * * @since 1.1.0 * * @return mixed an array of failed services or false if no services have failed */ protected function _check_for_failed() { if (function_exists('vip_safe_wp_remote_get')) { $status_response = vip_safe_wp_remote_get($this->status_endpoint); } else { $status_response = wp_remote_get($this->status_endpoint); } $statuses = array(); if (!defined('WPCOM_IS_VIP_ENV') || !WPCOM_IS_VIP_ENV) { $failed_services = get_site_transient('brightcove_failed_services'); } else { // while this makes sense to be a site transient, due to the nature of WordPress.com VIP ensure to keep it as regular transient on this environment. $failed_services = get_transient('brightcove_failed_services'); } if (false === $failed_services) { $failed_services = array(); $timeout = 300; // Used for transient. 5 min if a problem, 60 if all is well. if (!is_wp_error($status_response)) { $body = json_decode($status_response['body'], true); if (isset($body['result']) && isset($body['result']['status']) && is_array($body['result']['status'])) { $statuses = $body['result']['status']; } } foreach ($statuses as $status) { // Process statuses for each service. if (isset($status['containers']) && is_array($status['containers'])) { foreach ($status['containers'] as $container) { // Process each services' locations. if ('Operational' !== $container['status']) { // Are there other valid statuses? if (isset($failed_services[$status['name']])) { $failed_services[$status['name']][] = $container['name']; } else { $failed_services[$status['name']] = array($container['name']); } } } } } if (empty($failed_services)) { $timeout = 3600; } if (!defined('WPCOM_IS_VIP_ENV') || !WPCOM_IS_VIP_ENV) { set_site_transient('brightcove_failed_services', $failed_services, $timeout); } else { set_transient('brightcove_failed_services', $failed_services, $timeout); } } if (empty($failed_services)) { return false; } return $failed_services; }
/** * Retrieve records * * @param array $args * * @return array */ public function get_records($args) { $headers = array('Authorization' => 'Basic ' . base64_encode($this->api_access_id . ':' . $this->api_access_key)); $params = array('q' => '_source=spp-test-stream'); $url = $this->api_endpoint . '?' . build_query($params); if (function_exists('vip_safe_wp_remote_get')) { $response = \vip_safe_wp_remote_get($url, array('headers' => $headers)); } else { $response = \wp_remote_get($url, array('headers' => $headers)); } if (is_wp_error($response)) { return array(); } $result = json_decode($response['body']); return $result; }
public function request($url, $args = array()) { if (file_exists(LFAPPS__PLUGIN_PATH . '/../vip-init.php')) { if (isset($args['data'])) { $args['body'] = $args['data']; unset($args['data']); } return vip_safe_wp_remote_get($url, $args); } else { $http = new WP_Http(); if (isset($args['data'])) { $args['body'] = $args['data']; unset($args['data']); } return $http->request($url, $args); } }
public function call($call, $args = array()) { $url = $this->call_url($call, $args); $response = null; switch ($this->_library) { case 'wpvip': $response = vip_safe_wp_remote_get($url); break; case 'wp': default: $response = wp_remote_get($url); break; } if (is_wp_error($response)) { return 'Error: call to JW Player API failed'; } $response = wp_remote_retrieve_body($response); $decoded_response = json_decode($response, $assoc = true); return $decoded_response; }
public function request($url, $args = array()) { if (file_exists(LFAPPS__PLUGIN_PATH . '/../vip-init.php')) { if (isset($args['data'])) { $args['body'] = $args['data']; unset($args['data']); } return vip_safe_wp_remote_get($url, $args); } else { $http = new WP_Http(); if (isset($args['data'])) { $args['body'] = $args['data']; unset($args['data']); } $result = $http->request($url, $args); if ($result instanceof WP_Error) { return array('response' => array('code' => '500')); } else { return $result; } } }
function lp_update_shortlink() { check_ajax_referer('lp_update_shortlink'); $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : null; $update_id = isset($_REQUEST['update_id']) ? intval($_REQUEST['update_id']) : null; $cache_key = 'lp_shortlink_aaaa' . LP_PLUGIN_VERSION . '_' . $update_id; $shortlink = LivePress_WP_Utils::get_from_post($post_id, $cache_key, true); $status_code = 200; if (!$shortlink) { global $post; $post = get_post($post_id); $post_parent_url = get_permalink($post); if (0 === preg_match('/\\/\\?/', $post_parent_url)) { $post_parent_url .= '?'; } else { $post_parent_url .= '&'; } $canonical_url = $post_parent_url . 'lpup=' . $update_id . '#livepress-update-' . $update_id; //$bitly_api = '7ea952a9826d091fbda8a4ca220ba634efe61e31'; // TODO: Allow to set up from web page $bitly_api = 'a68d0da03159457bff5f6b287d6cdecb88b108dd'; // datacompboy $get_shortlink = 'https://api-ssl.bitly.com/v3/shorten?access_token=' . $bitly_api . '&domain=bit.ly&longUrl=' . urlencode($canonical_url); $url = $get_shortlink; if (function_exists('vip_safe_wp_remote_get')) { $res = vip_safe_wp_remote_get($url, '', 5, 10, 20, array('reject_unsafe_urls' => false)); } else { $res = wp_remote_get($url, array('reject_unsafe_urls' => false)); } $response = json_decode($res['body'], true); $status_code = $response['status_code']; if (is_wp_error($res) || 200 !== $status_code) { $shortlink = $canonical_url; } else { if (!$res || !isset($response['data']) || !isset($response['data']['url'])) { $shortlink = $canonical_url; $status_code = 500; } else { $shortlink = $response['data']['url']; LivePress_WP_Utils::save_on_post($post_id, $cache_key, $shortlink); $options = get_option(LivePress_Administration::$options_name); $livepress_com = new LivePress_Communication($options['api_key']); $livepress_com->send_to_livepress_broadcast($post_id, array('shortlink' => array($update_id => $shortlink))); } } } wp_send_json_success(array('shortlink' => $shortlink, 'code' => $status_code)); }
/** * Add file contents to the MIME request. * * @param string $filepath * @param string $name * @return string * @access public */ public function add_content_from_file($filepath, $name = null) { // Get the contents of the file. $contents = ''; // Try wp_remote_get first. if (defined('WPCOM_IS_VIP_ENV') && WPCOM_IS_VIP_ENV) { $request = vip_safe_wp_remote_get($filepath); } else { $request = wp_remote_get($filepath); } if (is_wp_error($request)) { // Try file_get_contents instead. This could be a local path. $contents = file_get_contents($filepath); } else { $contents = wp_remote_retrieve_body($request); } // Attempt to get the size $size = strlen($contents); // If this fails for some reason, try alternate methods if (empty($size)) { if (filter_var($filepath, FILTER_VALIDATE_URL)) { $headers = get_headers($filepath); foreach ($headers as $header) { if (preg_match('/Content-Length: ([0-9]+)/i', $header, $matches)) { $size = intval($matches[1]); } } } else { // This will be the final catch for local files $size = filesize($filepath); } } // If the name wasn't specified, build it from the filename $filename = \Apple_News::get_filename($filepath); if (empty($name)) { $name = sanitize_key($filename); } return $this->build_attachment($name, $filename, $contents, $this->get_mime_type_for($filepath), $size); }
private function cached_get($url, $args) { global $bc_accounts; /** * Filter the length of time to cache proxied remote calls to the Brightcove API. * * @param int $cache_time_in_seconds The cache time to use, in seconds. Default 180. */ $cache_time_in_seconds = apply_filters('brightcove_proxy_cache_time_in_seconds', 180); $account_id = $bc_accounts->get_account_id(); $max_key_length = 45; // transients support a max key of 45 $transient_key = substr('_brightcove_req_' . $account_id . BC_Utility::get_hash_for_object($url), 0, $max_key_length); $request = BC_Utility::get_cache_item($transient_key); if (false === $request) { if (function_exists('vip_safe_wp_remote_get')) { $request = vip_safe_wp_remote_get($url, '', 3, 1, 20, $args); } else { $request = wp_remote_get($url, $args); } $successful_response_codes = array(200, 201, 202, 204); if (in_array(wp_remote_retrieve_response_code($request), $successful_response_codes)) { BC_Utility::set_cache_item($transient_key, '', $request, $cache_time_in_seconds); } } return $request; }
/** * Check if the remote file exists for this node. * * @param DomNode $node * @return boolean * @static * @access protected */ protected static function remote_file_exists($node) { $html = $node->ownerDocument->saveXML($node); preg_match('/src="([^"]*?)"/im', $html, $matches); $path = $matches[1]; // Is it a URL? Check the headers in case of 404 if (false !== filter_var($path, FILTER_VALIDATE_URL)) { if (defined('WPCOM_IS_VIP_ENV') && WPCOM_IS_VIP_ENV) { $result = vip_safe_wp_remote_get($path); } else { $result = wp_safe_remote_get($path); } if (is_wp_error($result) || empty($result['response']['code']) || 404 == $result['response']['code']) { return false; } else { return true; } } // This could be a local file path. // Check that, except on WordPress VIP where this is not possible. if (!defined('WPCOM_IS_VIP_ENV') || !WPCOM_IS_VIP_ENV) { return file_exists($path); } // Nothing was found or no further validation is possible. return false; }
/** * Check if subdomains or domains are accessible * * @since 1.8 * * @param array $options new set of options to test */ protected function check_domains($options) { $options = array_merge($this->options, $options); $model = new PLL_Model($options); $links_model = $model->get_links_model(); foreach ($this->model->get_languages_list() as $lang) { $url = add_query_arg('deactivate-polylang', 1, $links_model->home_url($lang)); // Don't redefine vip_safe_wp_remote_get() as it has not the same signature as wp_remote_get() $response = function_exists('vip_safe_wp_remote_get') ? vip_safe_wp_remote_get(esc_url_raw($url)) : wp_remote_get(esc_url_raw($url)); $response_code = wp_remote_retrieve_response_code($response); if (200 != $response_code) { add_settings_error('general', 'pll_invalid_domain', esc_html(sprintf(__('Polylang was unable to access the URL %s. Please check that the URL is valid.', 'polylang'), $url))); } } }
/** * Do a get to the livepress service. * * @param string $action Livepress service action url. * @param string $get_vars Variables to get. * * @return WP_Error or HTTP API result array. */ private function do_get_to_livepress($action, $get_vars = array()) { $url = $this->livepress_config->livepress_service_host() . $action; $url .= '?'; $url .= $this->add_vars_to_URL($get_vars); $url .= 'address='; $url .= urlencode($this->address); $url .= '&'; $url .= 'api_key='; $url .= urlencode($this->api_key); if (function_exists('vip_safe_wp_remote_get')) { return vip_safe_wp_remote_get($url, '', 5, 10, 20, array('reject_unsafe_urls' => false)); } else { return wp_remote_get($url, array('reject_unsafe_urls' => false)); } }
/** * Formulate the response based on the request $params. * * @param array $params { * @type string $nonce * @type string $host * @type string $path * @type string $query * @type string $if_none_match * @type string $if_modified_since * } * @return array $r { * @type array $response { * @type int $code * @type string $message * } * @type array $headers * @type string $body * } * * @throws Exception */ function send_proxy_response(array $params) { $params = array_merge(array_fill_keys(array('nonce', 'host', 'path', 'query', 'if_none_match', 'if_modified_since'), null), $params); if (!$this->is_proxy_enabled()) { throw new Exception('proxy_not_enabled', 401); } if (!wp_verify_nonce($params['nonce'], self::MODULE_SLUG)) { throw new Exception('bad_nonce', 403); } // Construct the proxy URL for the resource $url = 'http://' . $params['host'] . $params['path']; if ($params['query']) { $url .= '?' . $params['query']; } $transient_key = sprintf('proxied_' . md5($url)); if (strlen($transient_key) > 40) { throw new Exception('transient_key_too_large', 500); } $r = get_transient($transient_key); if (empty($r)) { // @todo We eliminate transient expiration and send if-modified-since/if-none-match to server $timeout = $this->config('request_timeout'); if (function_exists('vip_safe_wp_remote_get')) { $fallback_value = ''; $threshold = 3; $r = vip_safe_wp_remote_get($url, $fallback_value, $threshold, $timeout); } else { $args = compact('timeout'); // @codingStandardsIgnoreStart $r = wp_remote_get($url, $args); // @codingStandardsIgnoreEnd } if (is_wp_error($r)) { $r = array('response' => array('code' => 400, 'message' => $r->get_error_code()), 'headers' => array('content-type' => 'text/plain'), 'body' => $r->get_error_message()); } if (!isset($r['headers']['content-length'])) { $r['headers']['content-length'] = 0; } $r['headers']['content-length'] = max($r['headers']['content-length'], strlen(wp_remote_retrieve_body($r))); if ($r['headers']['content-length'] > $this->config('max_content_length')) { $r = array('response' => array('code' => 502, 'message' => 'Response Too Large'), 'headers' => array('content-type' => 'text/plain'), 'body' => sprintf(__('Response body (content-length: %1$d) too big for HTTPS Resource Proxy (max_content_length: %2$d).', 'customize-widgets-plus'), $r['headers']['content-length'], $this->config('max_content_length'))); } if (!empty($r['headers']['expires'])) { $cache_ttl = strtotime($r['headers']['expires']) - time(); } else { if (!empty($r['headers']['cache-control']) && preg_match('/max-age=(\\d+)/', $r['headers']['cache-control'], $matches)) { $cache_ttl = intval($matches[1]); } else { $cache_ttl = -1; } } $cache_ttl = max($cache_ttl, $this->config('min_cache_ttl')); $r['headers']['expires'] = str_replace('+0000', 'GMT', gmdate('r', time() + $cache_ttl)); // @todo in addition to the checks for whether the user is logged-in and if in customizer, should we do a check to prevent too many resources from being cached? set_transient($transient_key, $r, $cache_ttl); } $is_not_modified = false; $response_code = wp_remote_retrieve_response_code($r); $response_message = wp_remote_retrieve_response_message($r); if (200 === $response_code) { $is_etag_not_modified = !empty($params['if_none_match']) && isset($r['headers']['etag']) && false !== strpos($params['if_none_match'], $r['headers']['etag']); $is_last_modified_not_modified = !empty($params['if_modified_since']) && isset($r['headers']['last-modified']) && strtotime($r['headers']['last-modified']) <= strtotime($params['if_modified_since']); $is_not_modified = $is_etag_not_modified || $is_last_modified_not_modified; if ($is_not_modified) { $response_code = 304; $response_message = 'Not Modified'; } } else { unset($r['headers']['last-modified']); unset($r['headers']['etag']); } $body = ''; $forwarded_response_headers = array('content-type', 'last-modified', 'etag', 'expires'); $headers = wp_array_slice_assoc($r['headers'], $forwarded_response_headers); if (!$is_not_modified) { // @todo Content-Encoding deflate/gzip if requested $headers['content-length'] = strlen(wp_remote_retrieve_body($r)); $body = wp_remote_retrieve_body($r); } return array('response' => array('code' => $response_code, 'message' => $response_message), 'headers' => $headers, 'body' => $body); }
/** * Pushes the saved post to the relevant portal blogs * * Assembles the post data required for submitting a new post in the portal sites, grabs a list * of portal sites to push to and then runs through each, submitting the post data as a new post. * * @param int $orig_post_id ID of the saved post. * @param object $orig_post WP_Post object for the saved post. * * @return void */ protected function push_post_data_to_blogs($orig_post_id, $orig_post) { global $current_blog; if ($this->recursing) { return; } $this->recursing = true; // Prepare the post data. $orig_post_data = $this->prepare_post_data($orig_post_id); // Prepare the metadata. $orig_meta_data = $this->prepare_meta_data($orig_post_id, $current_blog); // Prepare terms. $orig_terms = $this->prepare_terms($orig_post_id, $orig_post); // Prepare featured image. $featured_image = $this->prepare_featured_image($orig_post_id); // Get the array of sites to sync to. $sync_destinations = $this->aggregator->get_portals($current_blog->blog_id); // Loop through all destinations to perform the sync. foreach ($sync_destinations as $sync_destination) { // Check this destination is (probably) okay to push to. if (!$this->check_destination($sync_destination)) { continue; } // Get the relevant sync job, if there is one. $this->job = new Aggregator_Job($sync_destination, $current_blog->blog_id); if (!$this->job->job_id) { continue; // There is no job for this destination. } // Check if we should be pushing this post, don't if not. if (!$this->allowed_post_type($orig_post->post_type, $this->job->get_post_types())) { return; } // Take the list of associated taxonomy terms and remove any taxonomies not allowed. $destination_terms = $this->allowed_taxonomies($orig_terms); // Take the list of associated taxonomy terms and remove any terms not allowed. $destination_terms = $this->allowed_terms($destination_terms); if (is_wp_error($destination_terms)) { continue; // See allowed_terms(). } // Okay, fine, switch sites and do the synchronisation dance. // @codingStandardsIgnoreStart switch_to_blog($sync_destination); // @codingStandardsIgnoreEnd // Make we use the right author. $orig_post_data['author'] = $this->prepare_author_id($orig_post_data['author'], $sync_destination); // Acquire ID and update post (or insert post and acquire ID). $portal_target_post_id = $this->get_portal_blog_post_id($orig_post_id, $current_blog->blog_id); if (false !== $portal_target_post_id) { $target_post_id = $orig_post_data['ID']; wp_update_post($orig_post_data); } else { $target_post_id = wp_insert_post($orig_post_data); } // Push the meta data. $this->push_meta_data($target_post_id, $orig_meta_data); // Push the featured image. if ($featured_image) { $this->push_featured_image($target_post_id, $featured_image); } // Push taxonomies and terms. $this->push_taxonomy_terms($target_post_id, $destination_terms); $portal_site_url = get_home_url($sync_destination); // Filter url of portal_site_url. $portal_site_url = apply_filters('aggregator_remote_get_url', $portal_site_url); // Args for wp_remote_get function. $args = array('blocking' => false); // If WP_CRON_LOCK_TIMEOUT is set and a number, set the curl timeout to a higher value. if (defined(WP_CRON_LOCK_TIMEOUT) && is_numeric(WP_CRON_LOCK_TIMEOUT)) { // Add 1 to time, give it a little extra time. $timeout = intval(WP_CRON_LOCK_TIMEOUT) + 1; $args['timeout'] = $timeout; } // Filter args for wp_remote_get. $args = apply_filters('aggregator_remote_get_args', $args, $portal_site_url); // Ping the cron on the portal site to trigger term import now. if (function_exists('vip_safe_wp_remote_get')) { vip_safe_wp_remote_get($portal_site_url . '/wp-cron.php', $args); } else { // @codingStandardsIgnoreStart wp_remote_get($portal_site_url . '/wp-cron.php', $args); // @codingStandardsIgnoreEnd } // Finally, set the post status. $new_post_data = get_post($target_post_id); /** * Filter the status of a syncing post. * * Allows for the status of a synced post to be overridden. Before * this filter runs, the status will be 'pending'. The value of * `$status` is the status of the original post being synced, which * will be passed to `wp_update_post()`. * * @var string $status The final status of the post. */ $new_post_data->post_status = apply_filters('aggregator_post_status', $orig_post_data['post_status']); wp_update_post($new_post_data); // Get boolean on if post is updated or brand new. $updating = false !== $portal_target_post_id; $new_post_id = $updating ? $portal_target_post_id : $new_post_data->ID; /** * Do an action after the post has been successfully created on the destination site. * * @param int $new_post_id Fresh post ID * @param int $orig_post_id Original post ID * @param int $sync_destination Destination site ID * @param int $current_blog Original site ID * @param bool $updating Whether post is updating (true) or a new post */ do_action('aggregator_after_push_post_data', $new_post_id, $orig_post_id, $sync_destination, $current_blog->blog_id, $updating); // Switch back to source blog. restore_current_blog(); } $this->recursing = false; }
/** * AJAX handler that looks up an article based on PMID and parses the data for a reference. * Echos a JSON encoded array * * @return void */ function anno_reference_import_doi() { check_ajax_referer('anno_import_doi', '_ajax_nonce-import-doi'); if (!isset($_POST['doi'])) { anno_reference_error_response(); } else { $doi = $_POST['doi']; } $lookup_response = array('message' => 'error', 'text' => _x('An error has occurred, please try again later', 'pmid lookup error message', 'anno')); // DOIs cannot contain any control characters. As defined here: http://www.doi.org/handbook_2000/appendix_1.html $doi = trim($doi); if (preg_match('/[\\x00-\\x1F\\x7F]/', $doi)) { anno_reference_error_response(_x('Invalid DOI', 'pmid lookup error message', 'anno')); } // Generate the URL for lookup $crossref_login = cfct_get_option('crossref_login'); $crossref_pass = cfct_get_option('crossref_pass'); // Empty login, or empty password and login is not an email. if (empty($crossref_login) || empty($crossref_pass) && !anno_is_valid_email($crossref_login)) { anno_reference_error_response(_x('Invalid CrossRef Login', 'pmid lookup error message', 'anno')); } else { if (empty($croossref_pass)) { $url = 'http://www.crossref.org/openurl/?pid=' . $crossref_login . '&id=doi:' . $doi . '&noredirect=true'; } else { $url = 'http://www.crossref.org/openurl/?pid=' . $crossref_login . ':' . $crossref_pass . '&id=doi:' . $doi . '&noredirect=true'; } } // Use wp.com functions if available for lookup. if (function_exists('vip_safe_wp_remote_get')) { $response = vip_safe_wp_remote_get($url); } else { $response = wp_remote_get($url); } if (is_wp_error($response) || isset($response['response']['code']) && $response['response']['code'] != 200 || !isset($response['body'])) { anno_reference_error_response(); } else { include_once CFCT_PATH . 'functions/phpquery/phpquery.php'; phpQuery::newDocumentXML($response['body']); phpQuery::getDocument(); $html = pq('html'); // If we find an HTML tag, echo error. if ($html->length > 0) { // We should only hit an HTML page for malformed URLs or invalid logins // @TODO error for invalid login. anno_reference_error_response(_x('Invalid DOI', 'pmid lookup error message', 'anno')); } $query_status = pq('query')->attr('status'); // Error if unresolved if ($query_status == 'unresolved') { $lookup_response = anno_reference_error_response(pq('msg')->text()); } else { if ($query_status == 'resolved') { $text = ''; // There should only be a single 'first' author. $prime_author = pq('contributor[sequence="first"][contributor_role="author"]'); $author_text = anno_reference_doi_process_author($prime_author); if (!empty($author_text)) { $author_arr[] = $author_text; } $additional_authors = pq('contributor[sequence="additional"][contributor_role="author"]'); foreach ($additional_authors as $additional_author) { $additional_author = pq($additional_author); $author_text = anno_reference_doi_process_author($additional_author); if (!empty($author_text)) { $author_arr[] = $author_text; } } $text .= implode(', ', $author_arr) . '. '; // Title $title = pq('article_title')->text(); if (!empty($title)) { // Titles do not have periods $text .= $title . '. '; } // Source $source = pq('journal_title')->text(); if (!empty($source)) { $text .= $source . '. '; } // Date, Volume, Issue, Page $date_meta = ''; $date = pq('year')->text(); $volume = pq('volume')->text(); $issue = pq('issue')->text(); $first_page = pq('first_page')->text(); $last_page = pq('last_page')->text(); if (!empty($date)) { $date_meta .= $date; } if (!empty($volume) || !empty($issue) || !empty($page)) { $date_meta .= ';'; if (!empty($volume)) { $date_meta .= $volume; } if (!empty($issue)) { $date_meta .= '(' . $issue . ')'; } if (!empty($first_page)) { $date_meta .= ':' . $first_page; } if (!empty($last_page)) { $date_meta .= '-' . $last_page; } } if (!empty($date_meta)) { $text .= $date_meta . '. '; } $text .= _x('DOI:', 'Reference text for doi lookup', 'anno') . $doi . '.'; $lookup_response = array('message' => 'success', 'text' => esc_textarea($text)); } else { anno_reference_error_response(); } } } echo json_encode($lookup_response); die; }
public function call($call, $args = array()) { $url = $this->call_url($call, $args); $response = null; switch ($this->_library) { case 'wpvip': $response = vip_safe_wp_remote_get($url); break; case 'wp': default: $response = wp_remote_get($url); break; } if (is_wp_error($response)) { $response = 'Error: call to JW Player API failed'; } else { $response = wp_remote_retrieve_body($response); } $unserialized_response = unserialize($response); return $unserialized_response; }
/** * Text Search API with '/content/search'. * * @param string $access_token Access token to access API * @param string $search_string Search query * @return object Search results object */ private function run_text_search($access_token, $search_string) { $cache_key = 'ndn_query_' . $search_string; $response = wp_cache_get($cache_key); // If there is no response in cache if ($response == false) { $headers = array('Authorization' => sprintf('Bearer %s', $access_token)); $query_data = array('text' => $search_string); $query_string = http_build_query($query_data); $wp_get_url = sprintf(self::NDN_SEARCH_API . '/content/search/v1/text?%s', $query_string); $wp_get_args = array('headers' => $headers); $response = vip_safe_wp_remote_get($wp_get_url, '', 10, 3, 20, $wp_get_args); if (is_array($response)) { if (array_key_exists('response', $response)) { $info = $response['response']; if ($info['code'] == '401') { // Token is stale. Need user to re-authorize the API return false; } // If API gives back error message } else { if (array_key_exists('errors', $response)) { return false; } } } else { if (is_wp_error($response)) { return false; } } } return $response; }
/** * Validate the user's API key both with the LivePress webservice * and the plugin update service. * * @return string */ public static function api_key_validate() { self::die_if_not_allowed(); check_ajax_referer('livepress_api_validate_nonce'); $api_key = esc_html(stripslashes($_GET['api_key'])); $domains = array(); // Add domain mapping primary domain if (function_exists('domain_mapping_siteurl')) { $domain_mapping_siteurl = domain_mapping_siteurl(); $domains['alias[]'] = $domain_mapping_siteurl; } $home_url = get_home_url(); // Mapped domain on VIP $domains['alias[]'] = $home_url; // Validate with the LivePress webservice $livepress_communication = new LivePress_Communication($api_key); $status = $livepress_communication->validate_on_livepress($domains); $options = get_option(self::$options_name); $options['api_key'] = $api_key; $options['error_api_key'] = 1 != $status && 2 != $status; if ($status == 1) { // We pass validation, update blog parameters from LP side $blog = $livepress_communication->get_blog(); $options['blog_shortname'] = $blog->shortname; $options['post_from_twitter_username'] = $blog->twitter_username; } update_option(self::$options_name, $options); if (false == $options['error_api_key']) { // Validate with plugin update service $api_params = array('edd_action' => 'activate_license', 'license' => $api_key, 'item_name' => urlencode(LP_ITEM_NAME)); if (function_exists('vip_safe_wp_remote_get')) { $response = vip_safe_wp_remote_get(add_query_arg($api_params, LP_STORE_URL), '', 5, 10, 20, array('reject_unsafe_urls' => false)); } else { $response = wp_remote_get(add_query_arg($api_params, LP_STORE_URL), array('reject_unsafe_urls' => false)); } if (is_wp_error($response)) { die('Ouch'); } $license_data = json_decode(wp_remote_retrieve_body($response)); update_option('livepress_license_status', $license_data->license); } if (2 == $status || 1 == $status || 0 == $status) { header('Content-Type: application/json'); die(json_encode($options)); } else { die('Ouch'); } }