Files
sage/app/filters.php
Ben e898cf1f4d Extract logic from search form template
This removes all logic from the search form Blade template, and extracts
it to the "Controller" level via Sage's filter system. It also adds a
global 'app' filter to hook into, so that the search form data will be
served on every page.
2018-08-06 13:06:34 -07:00

95 lines
2.8 KiB
PHP

<?php
namespace App;
/**
* Add <body> classes
*/
add_filter('body_class', function (array $classes) {
/** Add page slug if it doesn't exist */
if (is_single() || is_page() && !is_front_page()) {
if (!in_array(basename(get_permalink()), $classes)) {
$classes[] = basename(get_permalink());
}
}
/** Add a global class to everything.
* We want it to come first, so stuff its filter does can be overridden.
*/
array_unshift($classes, 'app');
/** Add class if sidebar is active */
if (display_sidebar()) {
$classes[] = 'sidebar-primary';
}
/** Clean up class names for custom templates */
$classes = array_map(function ($class) {
return preg_replace(['/-blade(-php)?$/', '/^page-template-views/'], '', $class);
}, $classes);
return array_filter($classes);
});
/**
* Add "… Continued" to the excerpt
*/
add_filter('excerpt_more', function () {
return ' &hellip; <a href="' . get_permalink() . '">' . __('Continued', 'sage') . '</a>';
});
/**
* Template Hierarchy should search for .blade.php files
*/
collect([
'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date', 'home',
'frontpage', 'page', 'paged', 'search', 'single', 'singular', 'attachment'
])->map(function ($type) {
add_filter("{$type}_template_hierarchy", __NAMESPACE__.'\\filter_templates');
});
/**
* Render page using Blade
*/
add_filter('template_include', function ($template) {
$data = collect(get_body_class())->reduce(function ($data, $class) use ($template) {
return apply_filters("sage/template/{$class}/data", $data, $template);
}, []);
if ($template) {
echo template($template, $data);
return get_stylesheet_directory().'/index.php';
}
return $template;
}, PHP_INT_MAX);
/**
* Tell WordPress how to find the compiled path of comments.blade.php
*/
add_filter('comments_template', function ($comments_template) {
$comments_template = str_replace(
[get_stylesheet_directory(), get_template_directory()],
'',
$comments_template
);
return template_path(locate_template(["views/{$comments_template}", $comments_template]) ?: $comments_template);
}, 100);
/**
* Render WordPress searchform using Blade
*/
add_filter('get_search_form', function () {
return template('partials.searchform');
});
/**
* Collect data for searchform.
*/
add_filter('sage/template/app/data', function ($data) {
$data['sf_action'] = esc_url(home_url('/'));
$data['sf_screen_reader_text'] = _x('Search for:', 'label', 'sage');
$data['sf_placeholder'] = esc_attr_x('Search &hellip;', 'placeholder', 'sage');
$data['sf_current_query'] = get_search_query();
$data['sf_submit_text'] = esc_attr_x('Search', 'submit button', 'sage');
return $data;
});