diff --git a/src/Flex.php b/src/Flex.php index 1c77719..1ae104f 100644 --- a/src/Flex.php +++ b/src/Flex.php @@ -2,6 +2,7 @@ namespace Motiontactic; +use Illuminate\Support\Facades\File; use StoutLogic\AcfBuilder\FieldsBuilder; class Flex @@ -44,9 +45,38 @@ public function build_flexible_components() } public function build() + { + if ( function_exists( 'storage_path' ) && defined( 'FLEX_CACHE' ) && FLEX_CACHE ) $this->cache(); + if ( function_exists( 'storage_path' ) && $this->exists( 'flex.php' ) ) { + $build = include $this->path( 'flex.php' ); + } else { + $this->flex = apply_filters( 'mt_flexible_component_before_build', $this->flex ); + $build = $this->flex->build(); + } + \acf_add_local_field_group( $build ); + } + + private function path( $filename = null ) + { + $directory = storage_path( 'framework/cache' ); + if ( !$filename ) return $directory; + return $directory . '/' . $filename; + } + + private function exists( $filename = null ): bool + { + return file_exists( $this->path( $filename ) ); + } + + private function cache() { $this->flex = apply_filters( 'mt_flexible_component_before_build', $this->flex ); - \acf_add_local_field_group( $this->flex->build() ); + $build = $this->flex->build(); + File::ensureDirectoryExists( $this->path() ); + return file_put_contents( + $this->path( 'flex.php' ), + '