* @return void */ public function __construct() { add_action( 'affilizz_hourly_cron', array( $this, 'check_media_existence' ) ); add_action( 'affilizz_hourly_cron', array( $this, 'refresh_local_rendering_files' ) ); add_action( 'admin_notices', array( $this, 'display_missing_media_notice' ) ); // Connect to the API $this->api = new \Affilizz\API(); // Schedule events if ( ! wp_next_scheduled( 'affilizz_hourly_cron' ) ) { wp_schedule_event( time(), 'hourly', 'affilizz_hourly_cron', array( false ) ); } } /** * Refreshes the local copy of the rendering script once it is older than its cache time * (see \Affilizz\Util\Assets) : the page renders only create a missing copy. * @author Affilizz * @since 1.18.0 * @return void */ public function refresh_local_rendering_files() { ( \Affilizz\Util\Assets::get_instance() )->store_local_rendering_files_copy(); } /** * Checks if a media / channel exists, adds a warning if deleted on Affilizz side. * @author Affilizz * @return void */ public function check_media_existence() { if ( ! $this->api ) { return; } // Temporary error variables $missing_media = false; $missing_channel = false; $organization = get_option( 'affilizz_organization', false ); $media = get_option( 'affilizz_media', false ); $channel = get_option( 'affilizz_channel', false ); // Check if the media exists $affilizz_medias = $this->api->get_media( get_option( 'affilizz_api_key' ), $organization ); if ( ! in_array( $media, $affilizz_medias ) ) { $missing_media = true; } if ( ! $missing_media ) { $affilizz_channels = $this->api->get_channels( $organization, $media ); if ( ! in_array( $channel, $affilizz_channels ) ) { $missing_channel = true; } } // Store the error status if ( $missing_channel ) { set_transient( 'affilizz_missing_channel', 1, 8 * HOUR_IN_SECONDS ); } if ( $missing_media ) { set_transient( 'affilizz_missing_media', 1, 8 * HOUR_IN_SECONDS ); } } }