has_credentials() ) { // Saving credentials clears the throttle, so the refresh happens then. return; } if ( false === wp_next_scheduled( self::REFRESH_ACTION ) ) { wp_schedule_single_event( time(), self::REFRESH_ACTION ); } } /** * Refreshing every list fetched from Kordiam. * * @return int How many lists were updated. */ public function refresh(): int { $updated = 0; if ( ( new StatusRegistry() )->refresh() ) { ++$updated; } if ( ( new CategoryRegistry() )->refresh() ) { ++$updated; } if ( null !== ( new FormatRegistry( new ApiClient() ) )->refresh() ) { ++$updated; } $custom_fields = new CustomFieldRegistry(); foreach ( CustomFieldRegistry::scopes() as $scope ) { if ( $custom_fields->refresh( $scope ) ) { ++$updated; } } return $updated; } /** * Checking whether any list is still empty. * * @return bool Whether something is missing. */ public static function anything_missing(): bool { if ( ! StatusRegistry::is_populated() || ! CategoryRegistry::is_populated() ) { return true; } if ( array() === FormatRegistry::cached_formats() ) { return true; } foreach ( CustomFieldRegistry::scopes() as $scope ) { if ( ! CustomFieldRegistry::is_populated( $scope ) ) { return true; } } return false; } /** * Allowing the next admin page load to ask for a refresh again. * * Called when the Kordiam credentials change. */ public static function forget_throttle(): void { delete_transient( self::THROTTLE_KEY ); } /** * Scheduling the recurring refresh on activation. */ public static function schedule(): void { if ( false === wp_next_scheduled( self::REFRESH_ACTION ) ) { wp_schedule_event( time() + MINUTE_IN_SECONDS, self::SCHEDULE, self::REFRESH_ACTION ); } } /** * Removing the recurring refresh on deactivation. */ public static function unschedule(): void { wp_clear_scheduled_hook( self::REFRESH_ACTION ); self::forget_throttle(); } }