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);
 }
 /**
  * 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');
     }
 }
 /**
  * Return a set of the most recent videos for the specified account.
  *
  * @param string $account_id
  * @param int $count
  *
  * @global BC_Accounts $bc_accounts
  *
  * @return array
  */
 protected function fetch_videos($account_id, $count = 10)
 {
     global $bc_accounts;
     $transient_key = substr('_brightcove_req_heartbeat_' . $account_id, 0, 45);
     $results = BC_Utility::get_cache_item($transient_key);
     $results = is_array($results) ? $results : array();
     if (empty($results)) {
         // Set up the account from which we're fetching data
         $account = $bc_accounts->set_current_account_by_id($account_id);
         if (false === $account) {
             // Account was invalid, fail
             // Restore our global, default account
             $bc_accounts->restore_default_account();
             return array();
         }
         // Get a list of videos
         $results = $this->cms_api->video_list($count, 0, '', 'updated_at');
         // Get a list of available custom fields
         $fields = $this->cms_api->video_fields();
         // Loop through results to remap items
         foreach ($results as &$result) {
             // Map the custom_fields array to a collection of objects with description, display name, id, etc
             $result['custom'] = $fields['custom_fields'];
             foreach ($result['custom_fields'] as $id => $value) {
                 // Extract the change tracking item explicitly
                 if ($id == '_change_history') {
                     $result['history'] = $value;
                     continue;
                 }
                 foreach ($result['custom'] as &$field) {
                     if ($field['id'] === $id) {
                         $field['value'] = $value;
                         break;
                     }
                 }
             }
             // Massage the text tracks
             $result['captions'] = array();
             foreach ($result['text_tracks'] as $caption) {
                 $result['captions'][] = array('source' => $caption['src'], 'language' => $caption['srclang'], 'label' => $caption['label']);
             }
         }
         $bc_accounts->restore_default_account();
         if (!empty($results)) {
             BC_Utility::set_cache_item($transient_key, 'video_list', $results, 600);
             // High cache time due to high expense of fetching the data.
         }
     }
     return $results;
 }
 /**
  * Check permission level for an account.
  *
  * @return array List of permission issues.
  */
 protected function check_permissions_level()
 {
     $permission_issues = array();
     $video_id = false;
     // Start enumerating permissions that we'll need to ensure the account is good.
     $cms_api = new BC_CMS_API();
     // Create a video
     $video_creation = $cms_api->video_add(__('Brightcove WordPress plugin test video', 'brightcove'));
     if (!$video_creation || is_wp_error($video_creation)) {
         $permission_issues[] = esc_html__('create videos', 'brightcove');
     } else {
         $video_id = $video_creation['id'];
         // Update a video
         $renamed_title = __('Brightcove WordPress plugin test video renamed', 'brightcove');
         $video_renamed = $cms_api->video_update($video_id, array('name' => $renamed_title));
         if (!$video_renamed || $renamed_title !== $video_renamed['name']) {
             $permission_issues[] = esc_html__('modify videos', 'brightcove');
         }
     }
     $playlist = $cms_api->playlist_add(__('Brightcove WordPress plugin test playlist', 'brightcove'));
     if (!$playlist || !is_array($playlist) || !isset($playlist['id'])) {
         $permission_issues[] = esc_html__('create playlists', 'brightcove');
     } else {
         // For use through other Playlist test API calls.
         $playlist_id = $playlist['id'];
         $update_data = array('video_ids' => array($video_id), 'type' => 'EXPLICIT');
         $updated_playlist = $cms_api->playlist_update($playlist_id, $update_data);
         if (!$updated_playlist || !is_array($updated_playlist) || !isset($updated_playlist['id'])) {
             $permission_issues[] = esc_html__('modify playlists', 'brightcove');
         }
         // Delete a playlist
         if (!$cms_api->playlist_delete($playlist_id)) {
             $permission_issues[] = esc_html__('delete playlists', 'brightcove');
         }
     }
     // Delete a video
     if (!$cms_api->video_delete($video_id)) {
         $permission_issues[] = esc_html__('delete videos', 'brightcove');
     }
     $player_api = new BC_Player_Management_API($this);
     // Fetch all players
     $players = $player_api->player_list();
     if (is_wp_error($players) || !is_array($players['items'])) {
         $permission_issues[] = esc_html__('fetch players', 'brightcove');
     }
     return $permission_issues;
 }
 public static function preload_params()
 {
     global $bc_accounts;
     $tags = new BC_Tags();
     $params = array();
     // Fetch all preload vids/playlists as appropriate.
     $uri = $_SERVER['REQUEST_URI'];
     $type = 'videos';
     if (BC_Utility::current_user_can_brightcove()) {
         $cms_api = new BC_CMS_API();
         $admin_media_api = new BC_Admin_Media_API();
         if (false !== strpos($uri, BC_Admin_Menu::get_playlists_page_uri_component())) {
             $type = 'playlists';
             $params['playlists'] = $cms_api->playlist_list();
         }
     } else {
         return false;
     }
     $params['dates'] = array($type => BC_Utility::get_video_playlist_dates_for_display($type));
     $params['nonce'] = wp_create_nonce('_bc_ajax_search_nonce');
     $params['tags'] = $tags->get_tags();
     $params['plupload'] = array('runtimes' => 'html5,silverlight,flash,html4', 'browse_button' => 'brightcove-select-files-button', 'container' => 'drop-target', 'drop_element' => 'drop-target', 'multiple_queues' => true, 'max_file_size' => wp_max_upload_size() . 'b', 'url' => admin_url('admin-ajax.php?action=bc_media_upload'), 'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'), 'silverlight_xap_url' => includes_url('js/plupload/plupload.silverlight.xap'), 'filters' => array(array('title' => esc_html__('Allowed Files'), 'extensions' => '*')), 'multipart' => true, 'urlstream_upload' => true, 'multi_selection' => true, 'multipart_params' => array('action' => 'bc_media_upload'));
     $params['messages'] = array('confirmDelete' => esc_html__('Deleting this video will prevent it from showing in any existing posts. Are you sure you want to delete?', 'brightcove'), 'ongoingSync' => esc_html__('We are currently performing a sync of your new Brightcove source, you may not see all videos and playlists until that is complete.', 'brightcove'), 'successUpload' => esc_html__('Successfully uploaded file with name %%s%%.', 'brightcove'), 'unableToUpload' => esc_html__('We were unable to upload the file with name %%s%% Please try reuploading it again.', 'brightcove'));
     // Fetch all account hash/name combos.
     $params['accounts'] = $bc_accounts->get_sanitized_all_accounts();
     // Fetch all supported mime types.
     $params['mimeTypes'] = BC_Utility::get_all_brightcove_mimetypes();
     $defaultAccount = $bc_accounts->get_account_details_for_user();
     $params['defaultAccount'] = $defaultAccount['hash'];
     $params['defaultAccountId'] = $defaultAccount['account_id'];
     return $params;
 }
 /**
  * Remove a subscription listener for a specific account.
  * 
  * @global BC_Accounts $bc_accounts
  * 
  * @param string $account_hash
  */
 public static function remove_subscription($account_hash)
 {
     global $bc_accounts;
     // Set up the account to which we're pushing data
     $account = $bc_accounts->set_current_account($account_hash);
     if (false === $account) {
         // Account was invalid, fail
         // Restore our global, default account
         $bc_accounts->restore_default_account();
         return;
     }
     // Get the subscription ID so we can delete it
     $subscription_id = get_option('bc_sub_' . $account_hash);
     if (false !== $subscription_id) {
         // We're in a static method, so instantiate the API we need
         $cms_api = new BC_CMS_API();
         // Unsubscribe from the thing
         $cms_api->remove_subscription($subscription_id);
     }
     // Restore our global, default account
     $bc_accounts->restore_default_account();
 }