/** * In the event video object data is stale in WordPress, or a video has never been generated, * create/update WP data store with Brightcove data. * * @param $video * @param bool $add_only True denotes that we know the object is not in our library and we are adding it first time to the library. This is to improve the initial sync. * * @return bool|WP_Error */ public function add_or_update_wp_video($video, $add_only = false) { $hash = BC_Utility::get_hash_for_object($video); $video_id = $video['id']; if (!$add_only) { $stored_hash = $this->get_video_hash_by_id($video_id); // No change to existing playlist if ($hash === $stored_hash) { return true; } } $post_excerpt = !is_null($video['description']) ? $video['description'] : ''; $post_content = !is_null($video['long_description']) ? $video['long_description'] : $post_excerpt; $post_title = !is_null($video['name']) ? $video['name'] : ''; $post_date = new DateTime($video['created_at']); $post_date = $post_date->format('Y-m-d g:i:s'); $utc_timezone = new DateTimeZone('GMT'); $gmt = new DateTime($video['created_at'], $utc_timezone); $gmt = $gmt->format('Y-m-d g:i:s'); $video_post_args = array('post_type' => $this->video_cpt, 'post_title' => $post_title, 'post_content' => $post_content, 'post_excerpt' => $post_excerpt, 'post_date' => $post_date, 'post_date_gmt' => $gmt, 'post_status' => 'publish'); if (!$add_only) { $existing_post = $this->get_video_by_id($video_id); if ($existing_post) { $video_post_args['ID'] = $existing_post->ID; $post_id = wp_update_post($video_post_args); } else { $post_id = wp_insert_post($video_post_args); } } else { $post_id = wp_insert_post($video_post_args); } if (!$post_id) { $error_message = esc_html__('The video has not been created in WordPress', 'brightcove'); BC_Logging::log(sprintf('BC WORDPRESS ERROR: %s'), $error_message); return new WP_Error('post-not-created', $error_message); } BC_Logging::log(sprintf(esc_html__('BC WORDPRESS: Video with ID #%d has been created', 'brightcove'), $post_id)); if (!empty($video['tags'])) { wp_set_post_terms($post_id, $video['tags'], 'brightcove_tags', false); } update_post_meta($post_id, '_brightcove_hash', $hash); update_post_meta($post_id, '_brightcove_video_id', BC_Utility::sanitize_and_generate_meta_video_id($video['id'])); update_post_meta($post_id, '_brightcove_transcoded', $video['complete']); update_post_meta($post_id, '_brightcove_account_id', $video['account_id']); update_post_meta($post_id, '_brightcove_video_object', $video); $meta = array(); $meta_keys = apply_filters('brightcove_meta_keys', array('images', 'state', 'cue_points', 'custom_fields', 'duration', 'economics')); foreach ($meta_keys as $key) { if (!empty($video[$key])) { $meta[$key] = $video[$key]; } } update_post_meta($post_id, '_brightcove_metadata', $meta); return true; }
/** * @param string $type playlists|video|players * @param array $data sorted playlists|videos|players associative array * * @return bool if stored hash matches calculated hash. */ public static function hash_changed($type, $data, $account_id) { $key = "_brightcove_hash_{$type}_{$account_id}"; $data_hash = BC_Utility::get_hash_for_object($data); $existing_hash = get_option($key); return $existing_hash !== $data_hash; }
/** * Sends API requests to remote server * * Sends the request to the remote server using the appropriate method and * logs any errors in the event of failures. * * @param string $url the endpoint to connect to * @param string $method the http method to use * @param array $data array of further data to send to the server * @param boolean $force_new_token whether or not to force obtaining a new oAuth token * * * @return mixed the return data from the call of false if a failure occurred */ protected function send_request($url, $method = 'GET', $data = array(), $force_new_token = false) { $method = strtoupper(sanitize_text_field($method)); $allowed_methods = array('DELETE', 'GET', 'PATCH', 'POST', 'PUT', 'JSON_DELETE', 'JSON_POST'); //only allow methods required by the brightcove APIs if (!in_array($method, $allowed_methods)) { return false; } $url = esc_url_raw($url); $transient_key = false; if ($method === "GET") { $hash = substr(BC_Utility::get_hash_for_object(array("url" => $url, "data" => $data)), 0, 20); $transient_key = "_bc_request_{$hash}"; $cached_request = BC_Utility::get_cache_item($transient_key); if (false !== $cached_request) { return $cached_request; } } $auth_header = $this->get_authorization_header($force_new_token); if (is_wp_error($auth_header)) { return $auth_header; } add_filter('http_request_timeout', array($this, 'increase_http_timeout')); $headers = array('Authorization' => $auth_header); // All JSON_ methods are used to indicate that application/json is the content type if (false !== strpos($method, 'JSON')) { $headers['Content-type'] = 'application/json'; $method = str_replace('JSON_', '', $method); } else { $headers['Content-type'] = 'application/x-www-form-urlencoded'; } $args = array('headers' => $headers); switch ($method) { case 'GET': $request = $this->cached_get($url, $args); break; case 'POST': $args['body'] = wp_json_encode($data); $request = wp_remote_post($url, $args); break; default: $args['method'] = $method; $args['body'] = wp_json_encode($data); if (!empty($data)) { $args['body'] = json_encode($data); } $request = wp_remote_request($url, $args); break; } if (401 === wp_remote_retrieve_response_code($request)) { if ("Unauthorized" === $request['response']['message']) { // Token may have expired, so before we give up, let's retry // the request with a fresh OAuth token. if (!$force_new_token) { return $this->send_request($url, $method, $data, true); } else { $this->errors[] = array('url' => $url, 'error' => new WP_Error('unauthorized-oauth', __('API says permission denied, check your client ID and client secret', 'brightcove'))); return false; } } } //log errors for further processing or return the body if (is_wp_error($request)) { $this->errors[] = array('url' => $url, 'error' => $request->get_error_message()); BC_Logging::log(sprintf('WP_ERROR: %s', $request->get_error_message())); return false; } $body = json_decode(wp_remote_retrieve_body($request), true); $successful_response_codes = array(200, 201, 202, 204); if (!in_array(wp_remote_retrieve_response_code($request), $successful_response_codes)) { $message = esc_html__('An unspecified error has occurred.', 'brightcove'); if (isset($body[0]) && isset($body[0]['error_code'])) { $message = $body[0]['error_code']; } elseif (isset($body['message'])) { $message = $body['message']; } $this->errors[] = array('url' => $url, 'error' => new WP_Error($request['response']['message'], $message)); BC_Logging::log(sprintf('BC API ERROR: %s', $message)); return false; } if ('204' == wp_remote_retrieve_response_code($request)) { return true; } if ($transient_key && $body && (!defined('WP_DEBUG') || false === WP_DEBUG)) { // Store body for 60s to prevent hammering the BC APIs. BC_Utility::set_cache_item($transient_key, 'api-request', $body, 60); } return $body; }
/** * In the event playlist object data is stale in WordPress, or a playlist has never been generated, * create/update WP data store with Brightcove data. * * @param $playlist * * @return bool|WP_Error */ public function add_or_update_wp_playlist($playlist) { $hash = BC_Utility::get_hash_for_object($playlist); $playlist_id = $playlist['id']; $stored_hash = $this->get_playlist_hash_by_id($playlist_id); // No change to existing playlist if ($hash === $stored_hash) { return true; } $post_excerpt = !is_null($playlist['description']) ? $playlist['description'] : ''; $post_content = $post_excerpt; $post_title = !is_null($playlist['name']) ? $playlist['name'] : ''; $post_date = new DateTime($playlist['created_at']); $post_date = $post_date->format('Y-m-d g:i:s'); $utc_timezone = new DateTimeZone('GMT'); $gmt = new DateTime($playlist['created_at'], $utc_timezone); $gmt = $gmt->format('Y-m-d g:i:s'); $playlist_post_args = array('post_type' => 'brightcove-playlist', 'post_title' => $post_title, 'post_name' => $post_title . uniqid(), 'post_content' => $post_content, 'post_excerpt' => $post_excerpt, 'post_date' => $post_date, 'post_date_gmt' => $gmt, 'post_status' => 'publish'); $existing_post = $this->get_playlist_by_id($playlist_id); if ($existing_post) { $playlist_post_args['ID'] = $existing_post->ID; $post_id = wp_update_post($playlist_post_args, true); } else { $post_id = wp_insert_post($playlist_post_args, true); } if (is_wp_error($post_id)) { $error_message = $post_id->get_error_message(); BC_Logging::log(sprintf('BC WORDPRESS ERROR: %s'), $error_message); return new WP_Error('post-not-created', $error_message); } $this->playlist_ids[] = $post_id; update_post_meta($post_id, '_brightcove_hash', $hash); update_post_meta($post_id, '_brightcove_playlist_id', BC_Utility::sanitize_and_generate_meta_video_id($playlist_id)); update_post_meta($post_id, '_brightcove_account_id', BC_Utility::sanitize_id($playlist['account_id'])); update_post_meta($post_id, '_brightcove_playlist_object', $playlist); $video_ids = BC_Utility::sanitize_payload_item($playlist['video_ids']); foreach ($video_ids as $video_id) { update_post_meta($post_id, '_brightcove_video_id', $video_id); } $meta = array(); $meta_keys = apply_filters('brightcove_meta_keys', array('favorite', 'limit', 'search')); foreach ($meta_keys as $key) { if (!empty($playlist[$key])) { $meta[$key] = $playlist[$key]; } } update_post_meta($post_id, '_brightcove_metadata', $meta); return true; }
public function fetch_all($type, $posts_per_page = 100, $page = 1, $query_string = '', $sort_order = 'updated_at') { global $bc_accounts; $request_identifier = "{$type}-{$posts_per_page}-{$query_string}-{$sort_order}"; $transient_key = substr('_brightcove_req_all_' . BC_Utility::get_hash_for_object($request_identifier), 0, 45); $results = get_transient($transient_key); $results = is_array($results) ? $results : array(); $initial_page = 1; $accounts = $bc_accounts->get_sanitized_all_accounts(); $account_ids = array(); foreach ($accounts as $account) { $account_ids[] = $account['account_id']; } $account_ids = array_unique($account_ids); while (count($results) <= $page * $posts_per_page) { if (0 === count($account_ids)) { // No more vids to fetch break; } foreach ($account_ids as $key => $account_id) { $bc_accounts->set_current_account_by_id($account_id); $account_results = $this->cms_api->video_list($posts_per_page, $posts_per_page * ($initial_page - 1), $query_string, 'updated_at'); // Not enough account results returned, so we assume there are no more results to fetch. if (count($account_results) < $posts_per_page) { unset($account_ids[$key]); } if (is_array($account_results) && count($account_results) > 0) { $results = array_merge($results, $account_results); } else { unset($account_ids[$key]); } } $initial_page++; } set_transient($transient_key, $results, 600); // High cache time due to high expense of fetching the data. $results = array_slice($results, $posts_per_page * ($page - 1), $posts_per_page); $bc_accounts->restore_default_account(); return $results; }
public function get_player_hash_by_id($player_id) { $player = $this->get_player_by_id($player_id); if (!$player) { return false; } else { return BC_Utility::get_hash_for_object($player); } }