get_cache_key( $options, $context ); /** * Filters whether source results are cached. * * @param bool $enabled Whether caching is enabled. */ if ( null === $key || ! apply_filters( 'layout_now_cache_enabled', true ) ) { return $source->get_items( $options, $context ); } $transient = 'layout_now_' . md5( $key . '|' . $this->version() . '|' . self::FORMAT ); $cached = get_transient( $transient ); if ( is_array( $cached ) ) { return array_map( array( Item::class, 'from_array' ), $cached ); } $items = $source->get_items( $options, $context ); set_transient( $transient, array_map( static function ( Item $item ) { return $item->to_array(); }, $items ), self::TTL ); return $items; } /** * Flushes when real content (not our own layouts or revisions) is saved. * * @param int $post_id Post ID. */ public function flush_for_post( $post_id ) { if ( wp_is_post_revision( $post_id ) || PostType::NAME === get_post_type( $post_id ) ) { return; } $this->flush(); } /** * Invalidates every cached result. */ public function flush() { update_option( self::VERSION_OPTION, (string) microtime( true ), true ); } /** * Current cache version. * * @return string */ private function version() { return (string) get_option( self::VERSION_OPTION, '1' ); } }