Skip to content

Commit

Permalink
Update sage-acf-gutenberg-blocks.php
Browse files Browse the repository at this point in the history
  • Loading branch information
MWDelaney committed Feb 14, 2019
1 parent dc50d3b commit e4666a9
Showing 1 changed file with 82 additions and 66 deletions.
148 changes: 82 additions & 66 deletions sage-acf-gutenberg-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}

0 comments on commit e4666a9

Please sign in to comment.