/**
  * Retrieve all players and create/update when necessary
  *
  * @return bool
  */
 public function sync_players($retry = false)
 {
     $force_sync = false;
     if (defined('BRIGHTCOVE_FORCE_SYNC') && BRIGHTCOVE_FORCE_SYNC) {
         $force_sync = true;
     }
     $players = $this->players_api->player_list();
     if (!is_array($players)) {
         if (!$retry) {
             return $this->sync_players(true);
         } else {
             return false;
             // Something happened we retried, we failed.
         }
     }
     $players = $this->sort_api_response($players);
     if ($force_sync || BC_Utility::hash_changed('players', $players, $this->cms_api->account_id)) {
         $player_ids_to_keep = array();
         // for deleting outdated players
         /* process all players */
         foreach ($players as $player) {
             $this->add_or_update_wp_player($player);
             $player_ids_to_keep[] = BC_Utility::sanitize_subscription_id($player['id']);
         }
         BC_Utility::remove_deleted_players($player_ids_to_keep);
         BC_Utility::store_hash('players', $players, $this->cms_api->account_id);
     }
     return true;
 }
 /**
  * Sync playlists with Brightcove
  *
  * Retrieve all playlists and create/update when necessary.
  *
  * @since 1.0.0
  *
  * @param bool $retry whether this is a 2nd attempt or not
  *
  * @return bool True on success or false
  */
 public function sync_playlists($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_playlists')) {
         return false;
     }
     $accounts = $bc_accounts->get_sanitized_all_accounts();
     $completed_accounts = array();
     foreach ($accounts as $account => $account_data) {
         // We may have multiple accounts for an account_id, prevent syncing that account more than once.
         if (!in_array($account_data['account_id'], $completed_accounts)) {
             $completed_accounts[] = $account_data['account_id'];
             $bc_accounts->set_current_account($account);
             $playlists = $this->cms_api->playlist_list();
             if (!is_array($playlists)) {
                 if (!$retry) {
                     return $this->sync_playlists(true);
                 } else {
                     return false;
                     // Something happened we retried, we failed
                 }
             }
             $playlists = $this->sort_api_response($playlists);
             if ($force_sync || BC_Utility::hash_changed('playlists', $playlists, $this->cms_api->account_id)) {
                 $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);
             }
         }
         $bc_accounts->restore_default_account();
     }
     set_transient('brightcove_sync_playlists', true, 30);
     return true;
 }
 /**
  * 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;
 }