* @return void */ public function __construct() { add_action( self::HOOK, array( $this, 'check_media_existence' ) ); add_action( self::HOOK, array( $this, 'refresh_local_rendering_files' ) ); // Connect to the API $this->api = new \Affilizz\API(); // Schedule events if ( ! wp_next_scheduled( self::HOOK ) ) { wp_schedule_event( time(), 'hourly', self::HOOK ); } } /** * 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. * The warning lasts until a check finds them again, or until the configuration or the API key changes. * @author Affilizz * @return void */ public function check_media_existence() { if ( ! $this->api ) { return; } // Temporary error variables $missing_media = false; $missing_channel = false; $api_key = get_option( 'affilizz_api_key', false ); $organization = get_option( 'affilizz_organization', false ); $media = get_option( 'affilizz_media', false ); $channel = get_option( 'affilizz_channel', false ); // Nothing to check until the installation wizard is completed if ( empty( $api_key ) || empty( $organization ) || empty( $media ) || empty( $channel ) ) { return; } // Check if the media exists, unless Affilizz refused the key or failed to answer: then check again next time $affilizz_medias = $this->api->get_media( $api_key, $organization ); if ( ! is_array( $affilizz_medias ) ) { return; } // The lists are keyed by ID, their values are the names if ( ! array_key_exists( $media, $affilizz_medias ) ) { $missing_media = true; } if ( ! $missing_media ) { $affilizz_channels = $this->api->get_channels( $organization, $media ); if ( ! is_array( $affilizz_channels ) ) { return; } if ( ! array_key_exists( $channel, $affilizz_channels ) ) { $missing_channel = true; } } // Store the error status, and forget it once found again (the warning about a missing media covers its channel) if ( $missing_channel ) { set_transient( 'affilizz_missing_channel', 1, 8 * HOUR_IN_SECONDS ); } else { delete_transient( 'affilizz_missing_channel' ); } if ( $missing_media ) { set_transient( 'affilizz_missing_media', 1, 8 * HOUR_IN_SECONDS ); } else { delete_transient( 'affilizz_missing_media' ); } } /** * Forgets the media and channel found missing, when the configuration or the API key changes: the next check looks again. * @author Affilizz * @since 1.19.1 * @return void */ public static function forget_missing_media() { delete_transient( 'affilizz_missing_media' ); delete_transient( 'affilizz_missing_channel' ); } }