File: /var/www/html/insiders/wp-load/wp-content/themes/breadly/lib/gutenmate-fallback.php
<?php
if (defined('GTM_VERSION')) {
return;
}
add_action('init', 'gtmt_fallback_init', 11);
function gtmt_fallback_init() {
add_filter('get_block_templates', 'gtmt_strip_gutenmate_templates', 10, 3);
add_filter('get_block_template', 'gtmt_strip_gutenmate_template', 10, 3);
add_filter('body_class', 'gtmt_fallback_body_class', 10, 1);
// Add fallback style to editor
add_editor_style('fallback.css');
gtmt_add_image_size_fallback();
}
/**
* Additional body classes for css styling
*/
function gtmt_fallback_body_class($classes) {
$comment_args = array(
'post_id' => get_queried_object_id(),
'count' => true,
'status' => 'approve',
);
if (is_singular() && (comments_open() || have_comments() || get_comments($comment_args))) {
$classes = array_merge($classes, ['gtm-has-comments-section']);
}
return $classes;
}
/**
* Add fallback assets for FSE
*/
add_action('admin_enqueue_scripts', 'gtmt_fallback_fse_assets');
function gtmt_fallback_fse_assets() {
$should_enqueue = true;
if (is_admin()) {
$should_enqueue = false;
$screen = get_current_screen();
if (isset($screen->id) && in_array($screen->id, ['site-editor', 'appearance_page_gutenberg-edit-site'])) {
$should_enqueue = true;
} else if (isset($screen->base) && $screen->base == 'post') {
$should_enqueue = true;
}
}
if ($should_enqueue) {
// Workaround since FSE can't handle dynamic inline style
// https://github.com/WordPress/gutenberg/issues/18571
wp_enqueue_style('gtmt-fallback-inline-style', get_theme_file_uri('/assets/css/dummy-with-preview-support.css'), [], GTMT_VERSION);
/**
* Perform action
* Use `wp_add_inline_style` to add an inline style such as Google Font css into the `gtmt-fallback-inline-style` handler
*/
do_action('gtmt-fallback-inline-style');
}
}
/**
* Strip all templates from the list and use fallback templates instead
*/
function gtmt_strip_gutenmate_templates($query_result, $query, $template_type) {
if (!empty($query_result)) {
$query_result = array_filter($query_result, function ($block_template) use ($template_type) {
if (gtmt_is_fallbackable_template($block_template)) {
$fallback_content = gtmt_get_fallback_template($block_template->slug, $template_type);
if (!empty($fallback_content)) {
// Keep this template but replace the content with content of fallback template
$block_template->content = $fallback_content;
} else {
// Strip this template
return null;
}
}
return $block_template;
});
}
return $query_result;
}
/**
* Fallback a single template, Used in REST
*/
function gtmt_strip_gutenmate_template($block_template, $id, $template_type) {
if (gtmt_is_fallbackable_template($block_template)) {
$block_template->content = gtmt_get_fallback_template($block_template->slug, $template_type) ?: $block_template->content;
}
return $block_template;
}
/**
* Determine the certain template is able to replace with fallback template
*/
function gtmt_is_fallbackable_template($template) {
return ($template->source == 'theme' && is_null($template->origin));
}
/**
* Get fallback template content, Null if not available
*/
function gtmt_get_fallback_template($slug, $template_type = 'wp_template') {
if ('wp_template_part' === $template_type) {
$fallback_template = get_theme_file_path("fallback-parts/{$slug}.html");
} else { // wp_template
$fallback_template = get_theme_file_path("fallback-templates/{$slug}.html");
}
$wp_filesystem = gtmt_get_wp_filesystem();
// Return content or null if not exists
if (file_exists($fallback_template)) {
return $wp_filesystem->get_contents($fallback_template);
}
}
/**
* Fallback for image size registration when Gutenmate plugin does not activated
*/
function gtmt_add_image_size_fallback() {
if (!defined('GTM_VERSION')) {
$image_sizes = get_theme_support('gutenmate-image-sizes', []);
if (!empty($image_sizes[0])) {
foreach ($image_sizes[0] as $name => $dimension) {
add_image_size($name, $dimension['width'] ?? 0, $dimension['height'] ?? 0, $dimension['crop'] ?? false);
}
}
}
}