diff --git a/php/classes/controllers/class-ads-controller.php b/php/classes/controllers/class-ads-controller.php index 61b0a0e5..29f55e43 100644 --- a/php/classes/controllers/class-ads-controller.php +++ b/php/classes/controllers/class-ads-controller.php @@ -113,7 +113,7 @@ protected function is_ads_enabled_in_castos() { $series_id = $this->get_current_feed_series_id(); - if ( empty( $podcasts['data']['podcast_list'] ) ) { + if ( ! is_array( $podcasts ) || empty( $podcasts['data']['podcast_list'] ) ) { return false; } diff --git a/php/classes/controllers/class-cron-controller.php b/php/classes/controllers/class-cron-controller.php index afeadb00..61fae3b2 100644 --- a/php/classes/controllers/class-cron-controller.php +++ b/php/classes/controllers/class-cron-controller.php @@ -35,6 +35,12 @@ class Cron_Controller { * */ protected $upgrade_handler; + const SYNC_SCHEDULE_META = 'podmotor_schedule_upload'; + + const ATTEMPTS_META = 'castos_sync_attempts'; + + const MAX_ATTEMPTS = 3; + /** * @param Castos_Handler $castos_handler * @param Episode_Repository $episodes_respository @@ -42,9 +48,9 @@ class Cron_Controller { */ public function __construct( $castos_handler, $episodes_respository, $upgrade_handler ) { - $this->castos_handler = $castos_handler; + $this->castos_handler = $castos_handler; $this->episodes_respository = $episodes_respository; - $this->upgrade_handler = $upgrade_handler; + $this->upgrade_handler = $upgrade_handler; add_action( 'admin_init', array( $this, 'schedule_events' ) ); add_filter( 'cron_schedules', array( $this, 'add_cron_intervals' ) ); @@ -55,7 +61,7 @@ public function __construct( $castos_handler, $episodes_respository, $upgrade_ha /** * @return void */ - protected function run_actions(){ + protected function run_actions() { add_action( 'ssp_cron_hook', array( $this, 'upload_scheduled_episodes' ) ); $this->upgrade_handler->run_upgrade_actions(); } @@ -80,7 +86,7 @@ public function schedule_events() { if ( ! wp_next_scheduled( 'ssp_cron_hook' ) ) { wp_schedule_event( time(), 'hourly', 'ssp_cron_hook' ); } - if( ! wp_next_scheduled('ssp_check_ads') ){ + if ( ! wp_next_scheduled( 'ssp_check_ads' ) ) { wp_schedule_event( time(), 'daily', 'ssp_check_ads' ); } } @@ -90,19 +96,36 @@ public function schedule_events() { */ public function upload_scheduled_episodes() { $uploaded = 0; - $logger = new Log_Helper(); + $logger = new Log_Helper(); + foreach ( $this->episodes_respository->get_scheduled_episodes() as $episode ) { $response = $this->castos_handler->upload_episode_to_castos( $episode ); if ( 'success' === $response['status'] ) { - delete_post_meta( $episode->ID, 'podmotor_schedule_upload' ); + $this->unschedule_episode( $episode->ID ); $this->episodes_respository->update_episode_sync_status( $episode->ID, Sync_Status::SYNC_STATUS_SYNCED ); $this->episodes_respository->delete_episode_sync_error( $episode->ID ); - $uploaded++; + $uploaded ++; + } else { + $attempts = get_post_meta( $episode->ID, self::ATTEMPTS_META, true ); + if ( $attempts < self::MAX_ATTEMPTS ) { + update_post_meta( $episode->ID, self::ATTEMPTS_META, ++ $attempts ); + } else { + $this->unschedule_episode( $episode->ID ); + } } - if ( 404 == $response['code'] ) { - delete_post_meta( $episode->ID, 'podmotor_schedule_upload' ); + if ( isset( $response['code'] ) && 404 == $response['code'] ) { + $castos_episode_id = get_post_meta( $episode->ID, 'podmotor_episode_id', true ); + + // Episode does not exists anymore, remove connection + if ( $castos_episode_id ) { + delete_post_meta( $episode->ID, 'podmotor_episode_id' ); + delete_post_meta( $episode->ID, 'podmotor_file_id' ); + } + + $this->unschedule_episode( $episode->ID ); + $logger->log( sprintf( 'Cron: could not upload episode %d', $episode->ID ) ); } } @@ -113,4 +136,8 @@ public function upload_scheduled_episodes() { return $uploaded; } + + protected function unschedule_episode( $episode_id ) { + delete_post_meta( $episode_id, self::SYNC_SCHEDULE_META ); + } } diff --git a/php/classes/controllers/class-podcast-post-types-controller.php b/php/classes/controllers/class-podcast-post-types-controller.php index 8cc111db..ad15b8ff 100644 --- a/php/classes/controllers/class-podcast-post-types-controller.php +++ b/php/classes/controllers/class-podcast-post-types-controller.php @@ -719,7 +719,8 @@ public function sync_episode( $id, $post ) { // Schedule uploading with a cronjob.1 // If it's 404, something wrong with the file ID. We don't try to reupload it since result will be the same. if ( 404 != $response['code'] ) { - update_post_meta( $id, 'podmotor_schedule_upload', true ); + update_post_meta( $id, Cron_Controller::SYNC_SCHEDULE_META, true ); + update_post_meta( $id, Cron_Controller::ATTEMPTS_META, 1 ); } $this->admin_notices_handler->add_predefined_flash_notice( Admin_Notifications_Handler::NOTICE_API_EPISODE_ERROR diff --git a/php/classes/handlers/class-castos-handler.php b/php/classes/handlers/class-castos-handler.php index 926e80eb..23ca3ea6 100644 --- a/php/classes/handlers/class-castos-handler.php +++ b/php/classes/handlers/class-castos-handler.php @@ -307,12 +307,6 @@ public function upload_episode_to_castos( $post ) { $app_response = wp_remote_post( $api_url, $options ); - if ( isset( $app_response['response']['code'] ) ) { - $this->update_response( 'code', intval( $app_response['response']['code'] ) ); - } - - $this->logger->log( 'Upload Podcast app_response', $app_response ); - if ( is_wp_error( $app_response ) ) { $this->logger->log( 'An unknown error occurred sending podcast data to castos: ' . $app_response->get_error_message() ); $this->update_response( 'message', 'An unknown error occurred: ' . $app_response->get_error_message() ); @@ -320,6 +314,12 @@ public function upload_episode_to_castos( $post ) { return $this->response; } + if ( isset( $app_response['response']['code'] ) ) { + $this->update_response( 'code', intval( $app_response['response']['code'] ) ); + } + + $this->logger->log( 'Upload Podcast app_response', $app_response ); + $response_object = json_decode( wp_remote_retrieve_body( $app_response ) ); $this->logger->log( 'Upload Podcast Response', $response_object );