コード例 #1
0
 /**
  * Initial playlist sync
  *
  * Retrieve all playlists and create/update when necessary.
  *
  * @since 1.0.0
  *
  * @param bool   $is_cli     whether the call is coming via WP_CLI
  *
  * @return bool True on success or false
  */
 public function handle_initial_sync($is_cli = false)
 {
     if (true === $is_cli) {
         WP_CLI::line(esc_html__('Starting Playlist Sync', 'brightcove'));
     }
     global $bc_accounts;
     $playlists = $this->cms_api->playlist_list();
     if (!is_array($playlists)) {
         return false;
     }
     if (true === $is_cli) {
         WP_CLI::line(esc_html__(sprintf('There are %d playlists to sync for this account. Please be patient.', sizeof($playlists)), 'brightcove'));
     }
     $playlists = $this->sort_api_response($playlists);
     $playlist_ids_to_keep = array();
     // for deleting outdated playlists
     $playlist_dates = array();
     /* process all playlists */
     foreach ($playlists as $playlist) {
         $this->add_or_update_wp_playlist($playlist);
         $playlist_ids_to_keep[] = BC_Utility::sanitize_and_generate_meta_video_id($playlist['id']);
         $yyyy_mm = substr(preg_replace('/[^0-9-]/', '', $playlist['created_at']), 0, 7);
         // Get YYYY-MM from created string
         $playlist_dates[$yyyy_mm] = $yyyy_mm;
     }
     ksort($playlist_dates);
     $playlist_dates = array_keys($playlist_dates);
     // Only interested in the dates
     BC_Utility::set_video_playlist_dates('playlists', $playlist_dates, $bc_accounts->get_account_id());
     BC_Utility::store_hash('playlists', $playlists, $this->cms_api->account_id);
     if (true === $is_cli) {
         WP_CLI::line(esc_html__('Playlist Sync Complete', 'brightcove'));
     }
     return true;
 }
コード例 #2
0
 /**
  * Sync videos with Brightcove
  *
  * Retrieve all videos and create/update when necessary.
  *
  * @since 1.0.0
  *
  * @todo  Enable queuing to avoid large syncs which may tax resources
  *
  * @param bool $retry whether this is a 2nd attempt or not
  *
  * @return bool True on success or false
  */
 public function sync_videos($retry = false)
 {
     global $bc_accounts;
     $force_sync = false;
     if (defined('BRIGHTCOVE_FORCE_SYNC') && BRIGHTCOVE_FORCE_SYNC) {
         $force_sync = true;
     }
     if (!$force_sync && get_transient('brightcove_sync_videos')) {
         return false;
     }
     $accounts = $bc_accounts->get_sanitized_all_accounts();
     $completed_accounts = array();
     if (empty($accounts)) {
         return false;
     }
     foreach ($accounts as $account => $account_data) {
         $account_id = $account_data['account_id'];
         // We may have multiple accounts for an account_id, prevent syncing that account more than once.
         if (!in_array($account_id, $completed_accounts)) {
             // Have we sync'd this account yet?
             $sync_complete = $bc_accounts->is_initial_sync_complete($account_id);
             if (!$sync_complete) {
                 $this->handle_initial_sync($account_id);
             } else {
                 $sync_type = $bc_accounts->get_sync_type($account_id);
                 $hours_sync = is_numeric($sync_type) ? $sync_type : 24;
                 $completed_accounts[] = $account_id;
                 $bc_accounts->set_current_account($account);
                 $videos_per_page = apply_filters('brightcove_videos_per_page', 100);
                 // Brightcove as of April 7th 2015 can only fetch 100 videos at a time
                 $hours_sync = apply_filters('brightcove_hours_to_sync', $hours_sync);
                 if ('full' === $sync_type) {
                     $video_count = $this->cms_api->video_count();
                 } else {
                     $video_count = $this->cms_api->video_count("?q=updated_at:-{$hours_sync}hours..");
                 }
                 if (is_wp_error($video_count)) {
                     return false;
                 }
                 $videos = array();
                 if (1 <= $video_count) {
                     for ($offset = 0; $offset < $video_count; $offset += $videos_per_page) {
                         $offset = min($offset, $video_count);
                         if ('full' === $sync_type) {
                             $list = $this->cms_api->video_list($videos_per_page, $offset, "", "-updated_at");
                         } else {
                             $list = $this->cms_api->video_list($videos_per_page, $offset, "updated_at:-{$hours_sync}hours..");
                         }
                         if (is_array($list)) {
                             $videos = array_merge($videos, $list);
                         } else {
                             BC_Logging::log(sprintf('Could not retrieve videos for account %s, offset %d. Sync may be incomplete', $account_id, $offset));
                         }
                     }
                     if (!is_array($videos)) {
                         if (!$retry) {
                             return $this->sync_videos(true);
                         } else {
                             return false;
                             // Something happened we retried, we failed.
                         }
                     }
                 }
                 $videos = $this->sort_api_response($videos);
                 if ($force_sync || BC_Utility::hash_changed('videos', $videos, $bc_accounts->get_account_id())) {
                     if ('full' === $sync_type) {
                         $video_ids_to_keep = array();
                         // for deleting outdated playlists
                     }
                     $video_dates = array();
                     /* process all videos */
                     foreach ($videos as $video) {
                         $this->add_or_update_wp_video($video);
                         if ('full' === $sync_type) {
                             $video_ids_to_keep[] = BC_Utility::sanitize_and_generate_meta_video_id($video['id']);
                         }
                         $yyyy_mm = substr(preg_replace('/[^0-9-]/', '', $video['created_at']), 0, 7);
                         // Get YYYY-MM from created string
                         $video_dates[$yyyy_mm] = $yyyy_mm;
                     }
                     ksort($video_dates);
                     $video_dates = array_keys($video_dates);
                     // Only interested in the dates
                     BC_Utility::set_video_playlist_dates('videos', $video_dates, $bc_accounts->get_account_id());
                     BC_Utility::store_hash('videos', $videos, $bc_accounts->get_account_id());
                 }
             }
             $bc_accounts->restore_default_account();
         }
     }
     set_transient('brightcove_sync_videos', true, 30);
     return true;
 }