|null */ private static $cache = null; /** * Clear the cache when something shown in the card changes. * * @since 1.0.0 * @return void */ public static function register() { add_action( 'apwc_module_toggled', array( __CLASS__, 'flush' ) ); } /** * The status: cached, or checked now when asked, missing, or from another plugin version. * * @since 1.0.0 * @param bool $fresh Check now and update the cache. * @return array{facts: array, jobs: array, checked_at: int, version: string} */ public static function get( $fresh = false ) { if ( ! $fresh ) { if ( null !== self::$cache ) { return self::$cache; } $cached = get_transient( self::TRANSIENT ); if ( is_array( $cached ) && ( $cached['version'] ?? '' ) === APWC_VERSION && isset( $cached['facts'], $cached['jobs'] ) ) { self::$cache = $cached; return $cached; } } Health::flush(); self::$cache = array( 'facts' => Sidebar::system_info( Plugin::instance()->registry() ), 'jobs' => Health::check(), 'checked_at' => time(), 'version' => APWC_VERSION, ); set_transient( self::TRANSIENT, self::$cache, self::TTL ); return self::$cache; } /** * "Checked 5 minutes ago". * * @since 1.0.0 * @param array $status Result of get(). * @return string */ public static function checked_text( array $status ) { $at = (int) ( $status['checked_at'] ?? 0 ); if ( ! $at || time() - $at < MINUTE_IN_SECONDS ) { return __( 'Checked just now', 'admin-powerups-for-woocommerce' ); } /* translators: %s: time since, e.g. "5 minutes". */ return sprintf( __( 'Checked %s ago', 'admin-powerups-for-woocommerce' ), human_time_diff( $at ) ); } /** * Forget the cached status. * * @since 1.0.0 * @return void */ public static function flush() { self::$cache = null; delete_transient( self::TRANSIENT ); } }