I am wondering about interest for developing a plugin that would produce a “bricks template selector” gutenberg block. Now that each template has an associated thumbnail in a recent Bricks release, this could query the available template, and return their thumbnail, title, and shortcode. Selecting one would simply insert the shortcode. Bonus point if we make the block show the shortcod as well as the thumbnail from Bricks.
Anyone thought about this, seen a different approach, or seen a solution already? I know Gutenbricks offers a better, more robust solution in the long term, but short term, thought this could be a helpful tool.
Here is a baseline query for anyone interested:
<?php
function get_template_thumbnail_url($template_id) {
$upload_dir = wp_get_upload_dir(); // Get WordPress upload directory
$screenshot_dir = $upload_dir['basedir'] . '/bricks/template-screenshots/'; // Path to the screenshots folder
// Construct the pattern to match files for this template ID
$pattern = 'template-screenshot-' . $template_id . '-*.webp';
// Scan the directory for matching files
$files = glob($screenshot_dir . $pattern);
// If no files are found, return a placeholder image
if (empty($files)) {
return 'https://via.placeholder.com/150'; // Replace with your preferred placeholder image
}
// Sort the files by modification time to get the most recent one
usort($files, function($a, $b) {
return filemtime($b) - filemtime($a); // Newest first
});
// Get the most recent file URL
$most_recent_file = basename($files[0]);
return $upload_dir['baseurl'] . '/bricks/template-screenshots/' . $most_recent_file;
}
function get_bricks_templates() {
// Arguments for querying the 'bricks_template' post type
$args = array(
'post_type' => 'bricks_template',
'posts_per_page' => -1, // Get all templates
'post_status' => 'publish',
);
// Query to get the templates
$templates_query = new WP_Query($args);
$templates = [];
if ($templates_query->have_posts()) {
while ($templates_query->have_posts()) {
$templates_query->the_post();
// Retrieve the post ID, title, and shortcode (meta)
$post_id = get_the_ID();
$title = get_the_title($post_id);
// Construct the thumbnail URL by querying the folder for the most recent screenshot
$thumbnail = get_template_thumbnail_url($post_id);
$shortcode = '[bricks_template id="' . $post_id . '"]'; // Shortcode for template
// Store template details in an array
$templates[] = array(
'id' => $post_id,
'title' => $title,
'thumbnail' => $thumbnail,
'shortcode' => $shortcode,
);
}
wp_reset_postdata(); // Reset post data after query
}
return $templates;
}
get_bricks_templates();