Commit 7a21c874 authored by Michael Iseard's avatar Michael Iseard
Browse files

Refactor and simplify controllers and templates

parent ca41fc79
......@@ -2,55 +2,65 @@
namespace App\Controllers;
use App\wp_bootstrap4_navwalker;
use Sober\Controller\Controller;
use function App\asset_path;
use function App\get_logo;
class App extends Controller
{
public function siteName()
{
return get_bloginfo('name');
class App extends Controller {
public function body_class() {
return 'app-template';
}
/**
* @return array
*/
public function logo() {
return [
'style' => get_logo('white'),
'size' => '25px'
];
}
/**
* @return array
*/
public function navbar() {
return [
'navClass' => 'navbar-dark',
'btnClass' => 'btn-outline-light'
];
}
public static function title()
{
if (is_home()) {
if ($home = get_option('page_for_posts', true)) {
return get_the_title($home);
public static function title() {
if ( is_home() ) {
if ( $home = get_option( 'page_for_posts', true ) ) {
return get_the_title( $home );
}
return __('Latest Posts', 'kudos-website');
return __( 'Latest Posts', 'kudos-website' );
}
if (is_archive()) {
if ( is_archive() ) {
return get_the_archive_title();
}
if (is_search()) {
return sprintf(__('Search Results for %s', 'kudos-website'), get_search_query());
if ( is_search() ) {
return sprintf( __( 'Search Results for %s', 'kudos-website' ), get_search_query() );
}
if (is_404()) {
return __('Not Found', 'kudos-website');
if ( is_404() ) {
return __( 'Not Found', 'kudos-website' );
}
return get_the_title();
}
public static function logos($size)
{
switch($size) {
case 'small':
return asset_path('images/logo-light.svg');
case 'full':
return asset_path('images/logo-light-full.svg');
case 'white':
return asset_path('images/logo-white.svg');
}
return false;
public function siteName() {
return get_bloginfo( 'name' );
}
public function site_news()
{
if (is_front_page() || carbon_get_theme_option('site_news_all_pages')) {
if (carbon_get_theme_option('show_site_news')) {
return carbon_get_theme_option('site_news');
public function site_news() {
if ( is_front_page() || carbon_get_theme_option( 'site_news_all_pages' ) ) {
if ( carbon_get_theme_option( 'show_site_news' ) ) {
return carbon_get_theme_option( 'site_news' );
}
}
......@@ -60,10 +70,9 @@ class App extends Controller
/**
* @return array
*/
public function cta()
{
$cta['label'] = carbon_get_theme_option('cta_label');
$cta['link'] = carbon_get_theme_option('cta_link');
public function cta() {
$cta['label'] = carbon_get_theme_option( 'cta_label' );
$cta['link'] = carbon_get_theme_option( 'cta_link' );
return $cta;
}
......@@ -72,13 +81,11 @@ class App extends Controller
* Primary Nav Menu arguments
* @return array
*/
public function primarymenu()
{
$args = array(
'theme_location' => 'primary_navigation',
'menu_class' => 'navbar-nav',
'walker' => new \App\wp_bootstrap4_navwalker(),
);
return $args;
public function primary_menu() {
return [
'theme_location' => 'primary_navigation',
'menu_class' => 'navbar-nav',
'walker' => new wp_bootstrap4_navwalker(),
];
}
}
......@@ -3,11 +3,10 @@
namespace App\Controllers;
use Sober\Controller\Controller;
use WP_Post;
use function App\filter_handlebar;
use function App\get_logo;
class FrontPage extends Controller
{
class FrontPage extends Controller {
/**
* @var mixed
*/
......@@ -16,52 +15,62 @@ class FrontPage extends Controller
* @var mixed
*/
private $intro_content;
function __construct() {
$this->intro_image_id = carbon_get_theme_option( 'intro_image' );
$this->intro_content = carbon_get_theme_option( 'intro_text' );
}
/**
* @var array|WP_Post|null
* @return array
*/
private $intro_image;
public function logo() {
return [
'style' => get_logo('small'),
'size' => '40px'
];
}
function __construct()
{
$intro_image_id = carbon_get_theme_option('intro_image');
if ($intro_image_id) {
$this->intro_image_id = $intro_image_id;
$this->intro_image = get_post($this->intro_image_id);
}
$this->intro_content = carbon_get_theme_option('intro_text');
/**
* @return array
*/
public function navbar() {
return [
'navClass' => 'navbar-light',
'btnClass' => 'btn-outline-secondary'
];
}
public function intro_image($size = 'full')
{
public function intro_image( $size = 'full' ) {
$intro_image_id = $this->intro_image_id;
if ($intro_image_id) {
return wp_get_attachment_image_url($intro_image_id, $size);
if ( $intro_image_id ) {
return wp_get_attachment_image_url( $intro_image_id, $size );
}
return false;
}
public function intro_image_caption()
{
$intro_image = $this->intro_image;
if ($intro_image) {
public function intro_image_caption() {
$intro_image = get_post( $this->intro_image_id );
if ( $intro_image ) {
return $intro_image->post_excerpt;
}
return false;
}
public function intro_image_description()
{
$intro_image = $this->intro_image;
if ($intro_image) {
public function intro_image_description() {
$intro_image = get_post( $this->intro_image_id );
if ( $intro_image ) {
return $intro_image->post_content;
}
return false;
}
public function intro_content()
{
public function intro_content() {
$content = $this->intro_content;
return filter_handlebar($content);
return filter_handlebar( $content );
}
}
......@@ -5,79 +5,75 @@ namespace App\Controllers;
use Sober\Controller\Controller;
use WP_Query;
class TemplateFaq extends Controller
{
class TemplateFaq extends Controller {
/**
* TemplateFaq constructor.
*/
public function __construct()
{
set_query_var('template_name', 'faq');
}
/**
* @return mixed
*/
public function current_category()
{
return get_query_var('category');
public function __construct() {
set_query_var( 'template_name', 'faq' );
}
/**
* @param null $category
* @param null $keyword
*
* @return WP_Query
*/
public function questions($category = null, $keyword = null)
{
public function questions( $category = null, $keyword = null ) {
if (!$category) {
$category=$this->current_category();
if ( ! $category ) {
$category = $this->current_category();
}
// WP_Query arguments
$args = [
'post_type' => ['faq'],
'post_status' => ['publish'],
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => [ 'faq' ],
'post_status' => [ 'publish' ],
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
];
if ($category) {
if ( $category ) {
$args['category_name'] = $category;
}
if ($keyword) {
if ( $keyword ) {
$args['s'] = $keyword;
}
// The Query
return new WP_Query($args);
return new WP_Query( $args );
}
/**
* @return mixed
*/
public function current_category() {
return get_query_var( 'category' );
}
/**
* @return array
*/
public function categories()
{
public function categories() {
$args = [
'type' => 'faq',
'type' => 'faq',
'taxonomy' => 'category',
'orderby' => 'name',
'order' => 'ASC'
'orderby' => 'name',
'order' => 'ASC',
];
$categories = get_categories($args);
$categories = get_categories( $args );
// Add "All" option
array_unshift($categories, (object) [
'slug' => null,
'name' => __('All', 'kudos-website')
]);
array_unshift( $categories,
(object) [
'slug' => null,
'name' => __( 'All', 'kudos-website' ),
] );
return $categories;
}
......
......@@ -2,23 +2,32 @@
namespace App;
use WP_Customize_Manager;
/**
* Theme customizer
*/
add_action('customize_register', function (\WP_Customize_Manager $wp_customize) {
// Add postMessage support
$wp_customize->get_setting('blogname')->transport = 'postMessage';
$wp_customize->selective_refresh->add_partial('blogname', [
'selector' => '.brand',
'render_callback' => function () {
bloginfo('name');
}
]);
});
add_action( 'customize_register',
function ( WP_Customize_Manager $wp_customize ) {
// Add postMessage support
$wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
$wp_customize->selective_refresh->add_partial( 'blogname',
[
'selector' => '.brand',
'render_callback' => function () {
bloginfo( 'name' );
},
] );
} );
/**
* Customizer JS
*/
add_action('customize_preview_init', function () {
wp_enqueue_script('sage/customizer.js', asset_path('scripts/customizer.js'), ['customize-preview'], null, true);
});
add_action( 'customize_preview_init',
function () {
wp_enqueue_script( 'sage/customizer.js',
asset_path( 'scripts/customizer.js' ),
[ 'customize-preview' ],
null,
true );
} );
This diff is collapsed.
......@@ -5,109 +5,138 @@ 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_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 class if sidebar is active */
if (display_sidebar()) {
$classes[] = 'sidebar-primary';
}
/** 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);
/** 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);
});
return array_filter( $classes );
} );
/**
* Add "… Continued" to the excerpt
*/
add_filter('excerpt_more', function () {
return ' &hellip; <a href="' . get_permalink() . '">' . __('Continued', 'sage') . '</a>';
});
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', 'embed'
])->map(function ($type) {
add_filter("{$type}_template_hierarchy", __NAMESPACE__.'\\filter_templates');
});
collect( [
'index',
'404',
'archive',
'author',
'category',
'tag',
'taxonomy',
'date',
'home',
'frontpage',
'page',
'paged',
'search',
'single',
'singular',
'attachment',
'embed',
] )->map( function ( $type ) {
add_filter( "{$type}_template_hierarchy", __NAMESPACE__ . '\\filter_templates' );
} );
/**
* Render page using Blade
*/
add_filter('template_include', function ($template) {
collect(['get_header', 'wp_head'])->each(function ($tag) {
ob_start();
do_action($tag);
$output = ob_get_clean();
remove_all_actions($tag);
add_action($tag, function () use ($output) {
echo $output;
});
});
$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);
add_filter( 'template_include',
function ( $template ) {
collect( [ 'get_header', 'wp_head' ] )->each( function ( $tag ) {
ob_start();
do_action( $tag );
$output = ob_get_clean();
remove_all_actions( $tag );
add_action( $tag,
function () use ( $output ) {
echo $output;
} );
} );
$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 );
/**
* Render comments.blade.php
*/
add_filter('comments_template', function ($comments_template) {
$comments_template = str_replace(
[get_stylesheet_directory(), get_template_directory()],
'',
$comments_template
);
add_filter( 'comments_template',
function ( $comments_template ) {
$comments_template = str_replace(
[ get_stylesheet_directory(), get_template_directory() ],
'',
$comments_template
);
$data = collect(get_body_class())->reduce(function ($data, $class) use ($comments_template) {
return apply_filters("sage/template/{$class}/data", $data, $comments_template);
}, []);
$data = collect( get_body_class() )->reduce( function ( $data, $class ) use ( $comments_template ) {
return apply_filters( "sage/template/{$class}/data", $data, $comments_template );
},
[] );
$theme_template = locate_template(["views/{$comments_template}", $comments_template]);
$theme_template = locate_template( [ "views/{$comments_template}", $comments_template ] );
if ($theme_template) {
echo template($theme_template, $data);
return get_stylesheet_directory().'/index.php';
}
if ( $theme_template ) {
echo template( $theme_template, $data );
return $comments_template;
}, 100);
return get_stylesheet_directory() . '/index.php';
}
return $comments_template;
},
100 );
// Custom SMTP settings
add_action('phpmailer_init', function ($phpmailer) {
$useSMTP = carbon_get_theme_option('we_use_smtp');
if ($useSMTP) {
$phpmailer->isSMTP();
$phpmailer->Host = carbon_get_theme_option('we_smtp_host');
$phpmailer->SMTPAutoTLS = true;
$phpmailer->SMTPAuth = true;
$phpmailer->Port = carbon_get_theme_option('we_smtp_port');
$phpmailer->Username = carbon_get_theme_option('we_smtp_user');
$phpmailer->Password = carbon_get_theme_option('we_smtp_password');
// Additional settings
$phpmailer->SMTPSecure = carbon_get_theme_option('we_security_protocol');
$phpmailer->From = carbon_get_theme_option('we_from_email');
$phpmailer->FromName = carbon_get_theme_option('we_from_name');
}
});
add_action( 'phpmailer_init',
function ( $phpmailer ) {
$useSMTP = carbon_get_theme_option( 'we_use_smtp' );
if ( $useSMTP ) {
$phpmailer->isSMTP();
$phpmailer->Host = carbon_get_theme_option( 'we_smtp_host' );
$phpmailer->SMTPAutoTLS = true;
$phpmailer->SMTPAuth = true;
$phpmailer->Port = carbon_get_theme_option( 'we_smtp_port' );
$phpmailer->Username = carbon_get_theme_option( 'we_smtp_user' );
$phpmailer->Password = carbon_get_theme_option( 'we_smtp_password' );
// Additional settings
$phpmailer->SMTPSecure = carbon_get_theme_option( 'we_security_protocol' );
$phpmailer->From = carbon_get_theme_option( 'we_from_email' );
$phpmailer->FromName = carbon_get_theme_option( 'we_from_name' );
}
} );
/**
* Register custom query vars
......@@ -116,7 +145,9 @@ add_action('phpmailer_init', function ($phpmailer) {
*
* @link https://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
*/
add_filter('query_vars', function ($vars) {
$vars[] = 'category';
return $vars;
});
add_filter( 'query_vars',
function ( $vars ) {