/** * Renders the iFrame player from Brightcove based on passed parameters * * @param $video_id * @param $account_id * @param $player_id * @param int $width * @param int $height * * @return string */ public static function player($video_id, $account_id, $player_id, $width = 500, $height = 250) { // Sanitize and Verify $account_id = BC_Utility::sanitize_id($account_id); $player_id = 'default' == $player_id ? 'default' : BC_Utility::sanitize_id($player_id); $video_id = BC_Utility::sanitize_id($video_id); $height = (int) $height; $width = (int) $width; $html = '<div style="width: ' . $width . 'px; height: ' . $height . 'px">'; $html .= '<style> .video-js { height: 100%; width: 100%; } .vjs-big-play-button { display: none; } </style>'; $html .= '<!-- Start of Brightcove Player -->'; $html .= sprintf('<video data-account="%s" data-player="%s" data-embed="default" data-video-id="%s" class="video-js" controls></video>', $account_id, $player_id, $video_id); $html .= sprintf('<script src="//players.brightcove.net/%s/%s_default/index.min.js"></script>', $account_id, $player_id); $html .= '<!-- End of Brightcove Player -->'; $html .= '</div>'; return $html; }
/** * Updates Metadata to the Brightcove API * * @param array $sanitized_post_data This should be sanitized POST data. * * @return bool|WP_Error */ public function update_bc_playlist($sanitized_post_data) { global $bc_accounts; if (!wp_verify_nonce($_POST['nonce'], '_bc_ajax_search_nonce')) { return false; } $playlist_id = BC_Utility::sanitize_id($sanitized_post_data['playlist_id']); $update_data = array('type' => 'EXPLICIT'); if (array_key_exists('name', $sanitized_post_data) && '' !== $sanitized_post_data['name']) { $update_data['name'] = utf8_uri_encode(sanitize_text_field($sanitized_post_data['name'])); } if (array_key_exists('playlist_videos', $sanitized_post_data) && !empty($sanitized_post_data['playlist_videos'])) { $update_data['video_ids'] = BC_Utility::sanitize_payload_item($sanitized_post_data['playlist_videos']); } $bc_accounts->set_current_account($sanitized_post_data['account']); $request = $this->cms_api->playlist_update($playlist_id, $update_data); $bc_accounts->restore_default_account(); if (is_wp_error($request) || false === $request) { return false; } if (is_array($request) && isset($request['id'])) { return true; } return true; }
public static function update_video_meta() { if (!wp_verify_nonce($_POST['nonce'], '_bc_ajax_search_nonce')) { return false; } if (!array_key_exists('update-metadata', $_POST)) { return false; } $video_id = BC_Utility::sanitize_id($_POST['video-id']); $api = new BC_CMS_API(); $video = $api->video_get($video_id); $updated_data = array(); foreach ($_POST as $key => $postdata) { echo $key; $updated_data = BC_Utility::sanitize_payload_item($postdata); } if (array_key_exists('video-related-url', $_POST)) { $video_related_url = esc_url_raw($_POST['video-related-url']); if (strlen($video_related_url)) { $updated_data['link'] = array_merge($video['link'], array('url' => $video_related_url)); } } if (array_key_exists('video-related-text', $_POST)) { $updated_data['link'] = array_merge($video['link'], array('text' => sanitize_text_field($_POST['video-related-text']))); } if (array_key_exists('video-tags', $_POST)) { $tags = explode(',', $_POST['video-tags']); $tags = array_filter($tags, 'trim'); $tags = array_filter($tags, 'sanitize_text_field'); $updated_data['tags'] = array_merge($video['tags'], $tags); } $api->video_update($video_id, $updated_data); }
/** * Updates Metadata to the Brightcove API * * @param array $sanitized_post_data . This should be sanitized POST data * * @return bool|WP_Error */ public function update_bc_video($sanitized_post_data) { global $bc_accounts; $video_id = BC_Utility::sanitize_id($sanitized_post_data['video_id']); if (array_key_exists('name', $sanitized_post_data) && '' !== $sanitized_post_data['name']) { $update_data['name'] = utf8_uri_encode(sanitize_text_field($sanitized_post_data['name'])); } if (array_key_exists('description', $sanitized_post_data) && !empty($sanitized_post_data['description'])) { $update_data['description'] = BC_Utility::sanitize_payload_item($sanitized_post_data['description']); } if (array_key_exists('long_description', $sanitized_post_data) && !empty($sanitized_post_data['long_description'])) { $update_data['long_description'] = BC_Utility::sanitize_payload_item($sanitized_post_data['long_description']); } if (array_key_exists('tags', $sanitized_post_data) && !empty($sanitized_post_data['tags'])) { // Convert tags string to an array. $update_data['tags'] = array_map('trim', explode(',', $sanitized_post_data['tags'])); } $bc_accounts->set_current_account($sanitized_post_data['account']); $request = $this->cms_api->video_update($video_id, $update_data); $bc_accounts->restore_default_account(); /** * If we had any tags in the update, add them to the tags collection if we don't already track them. */ if (array_key_exists('tags', $update_data) && is_array($update_data['tags']) && count($update_data['tags'])) { $existing_tags = $this->tags->get_tags(); $new_tags = array_diff($update_data['tags'], $existing_tags); if (count($new_tags)) { $this->tags->add_tags($new_tags); } } if (is_wp_error($request) || $request === false) { return false; } return true; }
/** * Provides the handler for saving/updating source data * * @return bool/WP_Error */ public function save_account() { global $bc_accounts; if (!isset($_POST['brightcove-check_oauth'])) { return false; } if (!current_user_can('brightcove_manipulate_accounts')) { $error_message = esc_html__('You do not have permission to manage this account.', 'brightcove'); BC_Logging::log(sprintf('ACCOUNT: %s', $error_message)); $this->notices[] = array('message' => $error_message, 'type' => 'error'); return new WP_Error('brightcove-account-manage-permissions', $error_message); } if (!wp_verify_nonce($_POST['brightcove-check_oauth'], '_brightcove_check_oauth_for_source')) { return false; } // Only go through the oAuth credential validation when we're adding a new account or editing the account's credentials (not default players etc) if ('create' === $_POST['source-action']) { $required_keys = array('brightcove-check_oauth', 'source-account-id', 'source-client-id', 'source-client-secret', 'source-name'); foreach ($required_keys as $key) { if (!array_key_exists($key, $_POST)) { return false; } } $account_id = BC_Utility::sanitize_id($_POST['source-account-id']); $client_id = sanitize_text_field($_POST['source-client-id']); $client_secret = BC_Utility::get_sanitized_client_secret($_POST['source-client-secret']); $account_name = sanitize_text_field(stripslashes_deep($_POST['source-name'])); $set_default = isset($_POST['source-default-account']) && 'on' === $_POST['source-default-account'] ? 'default' : ''; $hash = BC_Utility::get_hash_for_account(array('account_id' => $account_id, 'client_id' => $client_id, 'client_secret' => $client_secret)); $account = $bc_accounts->get_account_by_hash($hash); if ($account) { // Account already exists $error_message = esc_html__('The Brightcove credentials provided already exist.', 'brightcove'); BC_Logging::log(sprintf('BC ACCOUNTS: %s', $error_message)); $this->notices[] = array('message' => $error_message, 'type' => 'error'); return new WP_Error('bc-account-exists-error', $error_message); } if (!$bc_accounts->add_account($account_id, $client_id, $client_secret, $account_name, $set_default, false)) { $error_message = esc_html__('We could not authenticate your credentials with Brightcove', 'brightcove'); BC_Logging::log(sprintf('BC OAUTH ERROR: %s', $error_message)); $this->notices[] = array('message' => $error_message, 'type' => 'error'); return new WP_Error('bc-oauth-error', $error_message); } BC_Utility::clear_cached_api_requests('all'); $bc_accounts->set_current_account_by_id($account_id); $players = new BC_Players(); $players->sync_players(); } if ('update' === $_POST['source-action']) { if (isset($_POST['source-default-account']) && 'on' === $_POST['source-default-account']) { update_option('_brightcove_default_account', sanitize_text_field($_POST['hash'])); } } // Deleting transient to allow syncing from the new account, otherwise we won't be able to sync it until this transient expires. delete_transient('brightcove_sync_videos'); $this->notices[] = array('message' => sprintf('%s <a href="%s">%s</a>.', esc_html__('Congratulations! Your credentials have been authenticated. Return to', 'brightcove'), admin_url('admin.php?page=brightcove-sources '), esc_html__('Settings', 'brightcove')), 'type' => 'updated'); return true; }
public function set_sync_type($account_id, $type, $hours = 0) { $option_key_sync_type = '_brightcove_sync_type_' . BC_Utility::sanitize_id($account_id); $sync_val = 'full' === $type ? 'full' : (int) $hours; if (is_numeric($sync_val) && ($sync_val < 0 || $sync_val > 200)) { return false; } return update_option($option_key_sync_type, $sync_val); }
public static function bc_playlist($atts) { global $allowedtags, $bc_accounts; $defaults = array('playlist_id' => '', 'account_id' => '', 'height' => 250, 'width' => 500); $atts = shortcode_atts($defaults, $atts, 'bc_playlist'); if (false === $atts['playlist_id']) { return false; } $players = get_option('_bc_player_playlist_ids_' . $atts['account_id']); if (!$players || !is_array($players)) { return ''; } $player_key = apply_filters('brightcove_player_key', array_shift($players)); $html = sprintf('<iframe style="width: ' . intval($atts['width']) . 'px; height: ' . intval($atts['height']) . 'px;" src="//players.brightcove.net/%1$s/%2$s_default/index.html?playlistId=%3$s" height="%4$s" width="%5$s" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>', BC_Utility::sanitize_id($atts['account_id']), esc_attr($player_key), BC_Utility::sanitize_id($atts['playlist_id']), esc_attr($atts['height']), esc_attr($atts['width'])); return $html; }
/** * Function for processing a callback notification from Brightcove * * Valid callback URI: /wp-admin/admin-post.php?bc_auth=4455f75b * Valid callback JSON: * {"timestamp":1427307045995,"account_id":"4089003419001","event":"video-change","video":"4133902975001","version":0} **/ public function video_notification() { if (!isset($_GET['bc_auth'])) { return; } $auth = $_GET['bc_auth']; $json = file_get_contents('php://input'); $decoded = json_decode($json, true); if (!is_array($decoded)) { return; } if (!isset($decoded['account_id']) || !isset($decoded['video'])) { return; } $account_id = BC_Utility::sanitize_id($decoded['account_id']); $valid_auth = BC_Utility::get_auth_key_for_id($account_id); if ($valid_auth !== $auth) { // Someone was spoofing callbacks? return; } $video_id = BC_Utility::sanitize_id($decoded['video']); if (!$video_id) { wp_send_json_error('missing video id'); // Some sort of error occurred with the callback and we have no video_id. } global $bc_accounts; if (!$bc_accounts->set_current_account_by_id($account_id)) { wp_send_json_error('bad account id'); // Bad account id in callback } $cms_api = new BC_CMS_API(); $video_details = $cms_api->video_get($video_id); if (false === $video_details) { wp_send_json_error('video does not exist'); } $videos = new BC_Videos(); $video_update = $videos->add_or_update_wp_video($video_details); $bc_accounts->restore_default_account(); $this->trigger_background_fetch(); if ($video_update) { wp_send_json_success('video successfully updated'); } else { wp_send_json_error('unable to update video'); } }
public function update_profile() { global $bc_accounts; if (!isset($_POST['_bc_profile_nonce'])) { return false; } if (!wp_verify_nonce($_POST['_bc_profile_nonce'], 'bc_profile_nonce')) { return false; } $hash = BC_Utility::sanitize_payload_item($_POST['bc-user-default-source']); $user_id = BC_Utility::sanitize_id($_POST['user_id']); $accounts = $bc_accounts->get_sanitized_all_accounts(); if (!isset($accounts[$hash])) { BC_Utility::admin_notice_messages(array(array('message' => esc_html__('The specified Source does not exist.', 'brightcove'), 'type' => 'error'))); return false; } BC_Utility::update_user_meta($user_id, '_brightcove_default_account_' . get_current_blog_id(), $hash); return true; }
public function get_option_key_for($account_id) { return '_notifications_subscribed_' . BC_Utility::sanitize_id($account_id); }
/** * Render Player * * Renders the player from Brightcove based on passed parameters * * @since 1.0 * * @param string $type "playlist" or "video". * @param string $id The brightcove player or video ID. * @param string $account_id The Brightcove account ID. * @param string $player_id The brightcove player ID. * @param int $width The Width to display. * @param int $height The height to display. * * @return string The HTML code for the player */ public static function player($type, $id, $account_id, $player_id = 'default', $width = 0, $height = 0) { // Sanitize and Verify. $account_id = BC_Utility::sanitize_id($account_id); $player_id = 'default' == $player_id ? 'default' : BC_Utility::sanitize_id($player_id); $id = BC_Utility::sanitize_id($id); $height = (int) $height; $width = (int) $width; $type = 'playlist' === $type ? 'playlist' : 'video'; if ('playlist' === $type && 'default' === $player_id) { $player_api = new BC_Player_Management_API(); $players = $player_api->player_list_playlist_enabled(); if (is_wp_error($players) || !is_array($players) || $players['item_count'] < 1) { return '<div class="brightcove-player-warning">' . __('A specified Source does not have a playlist capable player <a href="https://studio.brightcove.com/products/videocloud/players/">configured</a>. Make sure there is at least one player with "Display Playlist" enabled.', 'brightcove') . '</div>'; } $player_id = esc_attr($players['items'][0]['id']); } $html = '<!-- Start of Brightcove Player -->'; if (0 === $width && 0 === $height) { $html .= '<div style="display: block; position: relative; max-width: 100%;"><div style="padding-top: 56.25%;">'; } $html .= sprintf('<iframe src="//players.brightcove.net/%s/%s_default/index.html?%sId=%s" allowfullscreen="" webkitallowfullscreen="" mozallowfullscreen="" style="width: %s; height: %s;%s"></iframe>', $account_id, $player_id, $type, $id, 0 === $width ? '100%' : $width . 'px', 0 === $height ? '100%' : $height . 'px', 0 === $width && 0 === $height ? 'position: absolute; top: 0px; bottom: 0px; right: 0px; left: 0px;' : ''); if (0 === $width && 0 === $height) { $html .= '</div></div>'; } $html .= '<!-- End of Brightcove Player -->'; return $html; }
public static function remove_all_media_objects_for_account_id($account_id) { // Delete account players $player_ids = get_option('_bc_player_ids_' . BC_Utility::sanitize_id($account_id), array()); delete_option('_bc_player_playlist_ids_' . BC_Utility::sanitize_id($account_id)); delete_option('_bc_player_ids_' . BC_Utility::sanitize_id($account_id)); foreach ($player_ids as $player_id) { delete_option('_bc_player_' . BC_Utility::sanitize_player_id($player_id) . '_' . BC_Utility::sanitize_id($account_id)); } delete_option('_bc_player_default_' . BC_Utility::sanitize_id($account_id)); wp_reset_postdata(); }
/** * 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 brightcove_media_query() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], '_bc_ajax_search_nonce')) { wp_send_json_error(); } $video_ids = false; // Used for the playlist edit. if (isset($_POST['videoIds'])) { if ('none' === $_POST['videoIds']) { wp_send_json_success(array()); // Existing playlist with no video IDs. } $video_ids = array(); // To handle the playlist of one video ID which is sent as a string instead of Array of 1 element. if (is_string($_POST['videoIds'])) { $video_ids[] = BC_Utility::sanitize_id($_POST['videoIds']); } else { foreach ($_POST['videoIds'] as $video_id) { $video_ids[] = BC_Utility::sanitize_id($video_id); } } } $account_id = isset($_POST['account']) ? sanitize_text_field($_POST['account']) : 'all'; if ('all' !== $account_id) { $account_id = BC_Utility::sanitize_id($_POST['account']); } $query = isset($_POST['search']) && '' !== $_POST['search'] ? sanitize_text_field($_POST['search']) : false; $tags = isset($_POST['tags']) && '' !== $_POST['tags'] ? array_map('absint', (array) $_POST['tags']) : false; $tag_name = isset($_POST['tagName']) && '' !== $_POST['tagName'] ? sanitize_text_field($_POST['tagName']) : false; $dates = isset($_POST['dates']) && 'all' !== $_POST['dates'] ? BC_Utility::sanitize_date($_POST['dates']) : false; $posts_per_page = isset($_POST['posts_per_page']) ? absint($_POST['posts_per_page']) : apply_filters('brightcove_max_posts_per_page', 100); $page = isset($_POST['page_number']) ? absint($_POST['page_number']) : 1; $type = isset($_POST['type']) ? sanitize_key($_POST['type']) : false; if (!$type || !in_array($type, array('videos', 'playlists'))) { wp_send_json_error(esc_html__('Invalid Search Type', 'brightcove')); exit; // Type can only be videos or playlists. } global $bc_accounts; if ('videos' === $type) { $query_terms = array(); $query_string = ''; if ($tag_name) { $query_terms[] = "tags:{$tag_name}"; $query_string .= "tags:{$tag_name}"; } if ($dates) { $query_terms[] = "updated_at:{$dates}-01..{$dates}-31"; $query_string .= "updated_at:{$dates}-01..{$dates}-31"; } if ($query) { array_unshift($query_terms, $query); } if ($video_ids) { // We send the video_ids sorted since we have them returned sorted by ID. // This way we get cache hits when playlist order, but not content have changed. $video_ids_sorted = $video_ids; sort($video_ids_sorted); $query_terms[] = "id:" . implode("+id:", $video_ids_sorted); } $query_string = implode("+", $query_terms); /** * For playlists, we specify the order in the query string as follows: * https://cms.api.brightcove.com/v1/accounts/<account_id>/videos?q=id:<video_id1>...+id:<video_idn> * * However it comes back to us sorted by video ID (smallest to largest, so afterwards we resort the dataset * by playlist sort order. */ $bc_accounts->set_current_account_by_id($account_id); $results = $this->cms_api->video_list($posts_per_page, $posts_per_page * ($page - 1), $query_string, 'updated_at'); /** * Since we use the video_list to fetch the videos for a playlist, it returns them to us * ordered by video_id, so we use the order of video_ids (the playlist order) to sort them * as per the playlist. */ if ($video_ids) { $ordered_results = array(); foreach ($video_ids as $video_id) { foreach ($results as $video_result) { if ($video_id === $video_result['id']) { $ordered_results[] = $video_result; break; } } } // $ordered_results is now in the same order as $video_ids $results = $ordered_results; } } else { $bc_accounts->set_current_account_by_id($account_id); $results = $this->cms_api->playlist_list(); } $bc_accounts->restore_default_account(); wp_send_json_success($results); }
/** * Handle an uploaded caption file and associate it with the specified video * * @global BC_Accounts $bc_accounts * * @param string $account_hash * @param int $video_id * @param array $raw_captions */ public function ajax_caption_upload($account_hash, $video_id, $raw_captions) { global $bc_accounts; // Set up the account to which we're pushing data $account = $bc_accounts->set_current_account($account_hash); if (false === $account) { $bc_accounts->restore_default_account(); return; } // Sanitize our passed data $video_id = BC_Utility::sanitize_id($video_id); $captions = array(); foreach ($raw_captions as $caption) { // Validate required fields if (!isset($caption['source']) || !isset($caption['language'])) { continue; } $url = esc_url($caption['source']); $lang = sanitize_text_field($caption['language']); if (empty($url) || empty($lang)) { continue; // Attachment has no URL, fail } $label = isset($caption['label']) ? sanitize_text_field($caption['label']) : ''; $source = parse_url($caption['source']); if (0 === strpos($source['host'], 'brightcove')) { // If the hostname starts with "brightcove," assume this media has already been ingested continue; } $captions[] = new BC_Text_Track($url, $lang, 'captions', $label); } if (empty($captions)) { return; // After sanitization, we have no valid captions } // Push the captions to Brightcove $this->cms_api->text_track_upload($video_id, $captions); // Restore our global, default account $bc_accounts->restore_default_account(); }