From e4666a94b22b85bfe6bfcb7264b604a5b50565f1 Mon Sep 17 00:00:00 2001 From: "Michael W. Delaney" Date: Thu, 14 Feb 2019 15:38:47 -0500 Subject: [PATCH] Update sage-acf-gutenberg-blocks.php --- sage-acf-gutenberg-blocks.php | 148 +++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 66 deletions(-) diff --git a/sage-acf-gutenberg-blocks.php b/sage-acf-gutenberg-blocks.php index 8619d98..df2b96f 100644 --- a/sage-acf-gutenberg-blocks.php +++ b/sage-acf-gutenberg-blocks.php @@ -2,6 +2,79 @@ namespace App; +// Check whether WordPress and ACF are available; bail if not. +if (! function_exists('acf_register_block')) { + return; +} +if (! function_exists('add_filter')) { + return; +} +if (! function_exists('add_action')) { + return; +} + +// Add the default blocks location, 'views/blocks', via filter +add_filter('sage-acf-gutenberg-blocks-templates', function () { + return array('views/blocks'); +}); + +/** + * Create blocks based on templates found in Sage's "views/blocks" directory + */ +add_action('acf/init', function () { + + // Global $sage_error so we can throw errors in the typical sage manner + global $sage_error; + + // Get an array of directories containing blocks + $directories = apply_filters('sage-acf-gutenberg-blocks-templates', []); + + // Check whether ACF exists before continuing + foreach ($directories as $dir) { + $template_directory = new \DirectoryIterator(\locate_template($dir)); + + foreach ($template_directory as $template) { + if (!$template->isDot() && !$template->isDir()) { + + // Strip the file extension to get the slug + $slug = removeBladeExtension($template->getFilename()); + + // Get header info from the found template file(s) + $file_path = locate_template($dir."/${slug}.blade.php"); + $file_headers = get_file_data($file_path, [ + 'title' => 'Title', + 'description' => 'Description', + 'category' => 'Category', + 'icon' => 'Icon', + 'keywords' => 'Keywords', + ]); + + if (empty($file_headers['title'])) { + $sage_error(__('This block needs a title: ' . $dir . '/' . $template->getFilename(), 'sage'), __('Block title missing', 'sage')); + } + + if (empty($file_headers['category'])) { + $sage_error(__('This block needs a category: ' . $dir . '/' . $template->getFilename(), 'sage'), __('Block category missing', 'sage')); + } + + // Set up block data for registration + $data = [ + 'name' => $slug, + 'title' => $file_headers['title'], + 'description' => $file_headers['description'], + 'category' => $file_headers['category'], + 'icon' => $file_headers['icon'], + 'keywords' => explode(' ', $file_headers['keywords']), + 'render_callback' => __NAMESPACE__.'\\sage_blocks_callback', + ]; + + // Register the block with ACF + \acf_register_block($data); + } + } + } +}); + /** * Callback to register blocks */ @@ -19,72 +92,15 @@ function sage_blocks_callback($block) echo \App\template("blocks/${slug}", ['block' => $block]); } - /** - * Create blocks based on templates found in Sage's "views/blocks" directory + * Function to strip the `.blade.php` from a blade filename */ -if (function_exists('add_action')) { - add_action('acf/init', function () { - - // Set the directory blocks are stored in - $template_directory = "views/blocks/"; - - // Set Sage9 friendly path at /theme-directory/resources/views/blocks - $path = get_stylesheet_directory() . '/' . $template_directory; - - // If the directory doesn't exist, create it. - if (!is_dir($path)) { - mkdir($path); - } - - if (function_exists('acf_register_block')) { - - // Global $sage_error so we can throw errors in the typical sage manner - global $sage_error; - - // Get all templates in 'views/blocks' - $dir = new \DirectoryIterator(\locate_template($template_directory)); - - // Loop through found templates and set up data - foreach ($dir as $fileinfo) { - if (!$fileinfo->isDot()) { - - // Strip the file extension to get the slug - $slug = str_replace('.blade.php', '', $fileinfo->getFilename()); - - // Get header info from the found template file(s) - $file_path = locate_template("views/blocks/${slug}.blade.php"); - $file_headers = get_file_data($file_path, [ - 'title' => 'Title', - 'description' => 'Description', - 'category' => 'Category', - 'icon' => 'Icon', - 'keywords' => 'Keywords', - ]); - - if (empty($file_headers['title'])) { - $sage_error(__('This block needs a title: ' . $template_directory . $fileinfo->getFilename(), 'sage'), __('Block title missing', 'sage')); - } - - if (empty($file_headers['category'])) { - $sage_error(__('This block needs a category: ' . $template_directory . $fileinfo->getFilename(), 'sage'), __('Block category missing', 'sage')); - } - - // Set up block data for registration - $data = [ - 'name' => $slug, - 'title' => $file_headers['title'], - 'description' => $file_headers['description'], - 'category' => $file_headers['category'], - 'icon' => $file_headers['icon'], - 'keywords' => explode(' ', $file_headers['keywords']), - 'render_callback' => __NAMESPACE__.'\\sage_blocks_callback', - ]; - - // Register the block with ACF - acf_register_block($data); - } - } - } - }); +function removeBladeExtension($filename) +{ + + // Remove the unwanted extensions + $return = substr($filename, 0, strrpos($filename, '.blade.php')); + + // Always return + return $return; }