Add namespace

This commit is contained in:
Ben Word
2015-01-09 22:08:57 -06:00
parent 9e1922b06f
commit 587fbf6c35
13 changed files with 93 additions and 50 deletions

View File

@@ -1,3 +1,10 @@
<?php
namespace Roots\Sage;
use Roots\Sage\Config;
use Roots\Sage\Wrapper;
?>
<?php get_template_part('templates/head'); ?> <?php get_template_part('templates/head'); ?>
<body <?php body_class(); ?>> <body <?php body_class(); ?>>
@@ -15,11 +22,11 @@
<div class="wrap container" role="document"> <div class="wrap container" role="document">
<div class="content row"> <div class="content row">
<main class="main" role="main"> <main class="main" role="main">
<?php include sage_template_path(); ?> <?php include Wrapper\template_path(); ?>
</main><!-- /.main --> </main><!-- /.main -->
<?php if (sage_display_sidebar()) : ?> <?php if (Config\display_sidebar()) : ?>
<aside class="sidebar" role="complementary"> <aside class="sidebar" role="complementary">
<?php include sage_sidebar_path(); ?> <?php include Wrapper\sidebar_path(); ?>
</aside><!-- /.sidebar --> </aside><!-- /.sidebar -->
<?php endif; ?> <?php endif; ?>
</div><!-- /.content --> </div><!-- /.content -->

View File

@@ -1,4 +1,7 @@
<?php <?php
namespace Roots\Sage\Assets;
/** /**
* Scripts and stylesheets * Scripts and stylesheets
* *
@@ -14,7 +17,7 @@
* - An ID has been defined in config.php * - An ID has been defined in config.php
* - You're not logged in as an administrator * - You're not logged in as an administrator
*/ */
function sage_asset_path($filename) { function asset_path($filename) {
if (WP_ENV === 'development') { if (WP_ENV === 'development') {
return get_template_directory_uri() . '/dist/' . $filename; return get_template_directory_uri() . '/dist/' . $filename;
} }
@@ -32,8 +35,8 @@ function sage_asset_path($filename) {
} }
} }
function sage_assets() { function assets() {
wp_enqueue_style('sage_css', sage_asset_path('styles/main.css'), false, null); wp_enqueue_style('sage_css', asset_path('styles/main.css'), false, null);
/** /**
* Grab Google CDN's latest jQuery with a protocol relative URL; fallback to local if offline * Grab Google CDN's latest jQuery with a protocol relative URL; fallback to local if offline
@@ -46,21 +49,21 @@ function sage_assets() {
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js', array(), null, true); wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js', array(), null, true);
add_filter('script_loader_src', 'sage_jquery_local_fallback', 10, 2); add_filter('script_loader_src', __NAMESPACE__ . '\\jquery_local_fallback', 10, 2);
} }
if (is_single() && comments_open() && get_option('thread_comments')) { if (is_single() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply'); wp_enqueue_script('comment-reply');
} }
wp_enqueue_script('modernizr', sage_asset_path('scripts/modernizr.js'), array(), null, true); wp_enqueue_script('modernizr', asset_path('scripts/modernizr.js'), array(), null, true);
wp_enqueue_script('jquery'); wp_enqueue_script('jquery');
wp_enqueue_script('sage_js', sage_asset_path('scripts/app.js'), array(), null, true); wp_enqueue_script('sage_js', asset_path('scripts/app.js'), array(), null, true);
} }
add_action('wp_enqueue_scripts', 'sage_assets', 100); add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\assets', 100);
// http://wordpress.stackexchange.com/a/12450 // http://wordpress.stackexchange.com/a/12450
function sage_jquery_local_fallback($src, $handle = null) { function jquery_local_fallback($src, $handle = null) {
static $add_jquery_fallback = false; static $add_jquery_fallback = false;
if ($add_jquery_fallback) { if ($add_jquery_fallback) {
@@ -74,14 +77,14 @@ function sage_jquery_local_fallback($src, $handle = null) {
return $src; return $src;
} }
add_action('wp_head', 'sage_jquery_local_fallback'); add_action('wp_head', __NAMESPACE__ . '\\jquery_local_fallback');
/** /**
* Google Analytics snippet from HTML5 Boilerplate * Google Analytics snippet from HTML5 Boilerplate
* *
* Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM * Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM
*/ */
function sage_google_analytics() { ?> function google_analytics() { ?>
<script> <script>
<?php if (WP_ENV === 'production') : ?> <?php if (WP_ENV === 'production') : ?>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]= (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
@@ -99,5 +102,5 @@ function sage_google_analytics() { ?>
<?php } <?php }
if (GOOGLE_ANALYTICS_ID && (WP_ENV !== 'production' || !current_user_can('manage_options'))) { if (GOOGLE_ANALYTICS_ID && (WP_ENV !== 'production' || !current_user_can('manage_options'))) {
add_action('wp_footer', 'sage_google_analytics', 20); add_action('wp_footer', __NAMESPACE__ . '\\google_analytics', 20);
} }

View File

@@ -1,4 +1,8 @@
<?php <?php
namespace Roots\Sage\Config;
use Roots\Sage\Sidebar;
/** /**
* Enable theme features * Enable theme features
*/ */
@@ -20,24 +24,24 @@ if (!defined('WP_ENV')) {
/** /**
* Add body class if sidebar is active * Add body class if sidebar is active
*/ */
function sage_sidebar_body_class($classes) { function sidebar_body_class($classes) {
if (sage_display_sidebar()) { if (display_sidebar()) {
$classes[] = 'sidebar-primary'; $classes[] = 'sidebar-primary';
} }
return $classes; return $classes;
} }
add_filter('body_class', 'sage_sidebar_body_class'); add_filter('body_class', __NAMESPACE__ . '\\sidebar_body_class');
/** /**
* Define which pages shouldn't have the sidebar * Define which pages shouldn't have the sidebar
* *
* See lib/sidebar.php for more details * See lib/sidebar.php for more details
*/ */
function sage_display_sidebar() { function display_sidebar() {
static $display; static $display;
if (!isset($display)) { if (!isset($display)) {
$sidebar_config = new Sage_Sidebar( $sidebar_config = new Sidebar\Sage_Sidebar(
/** /**
* Conditional tag checks (http://codex.wordpress.org/Conditional_Tags) * Conditional tag checks (http://codex.wordpress.org/Conditional_Tags)
* Any of these conditional tags that return true won't show the sidebar * Any of these conditional tags that return true won't show the sidebar

View File

@@ -1,8 +1,11 @@
<?php <?php
namespace Roots\Sage\Extras;
/** /**
* Clean up the_excerpt() * Clean up the_excerpt()
*/ */
function sage_excerpt_more() { function excerpt_more() {
return ' &hellip; <a href="' . get_permalink() . '">' . __('Continued', 'sage') . '</a>'; return ' &hellip; <a href="' . get_permalink() . '">' . __('Continued', 'sage') . '</a>';
} }
add_filter('excerpt_more', 'sage_excerpt_more'); add_filter('excerpt_more', __NAMESPACE__ . '\\excerpt_more');

View File

@@ -1,4 +1,7 @@
<?php <?php
namespace Roots\Sage\Gallery;
/** /**
* Clean up gallery_shortcode() * Clean up gallery_shortcode()
* *
@@ -7,7 +10,7 @@
* *
* @link http://getbootstrap.com/components/#thumbnails * @link http://getbootstrap.com/components/#thumbnails
*/ */
function sage_gallery($attr) { function gallery($attr) {
$post = get_post(); $post = get_post();
static $instance = 0; static $instance = 0;
@@ -115,15 +118,15 @@ function sage_gallery($attr) {
} }
if (current_theme_supports('bootstrap-gallery')) { if (current_theme_supports('bootstrap-gallery')) {
remove_shortcode('gallery'); remove_shortcode('gallery');
add_shortcode('gallery', 'sage_gallery'); add_shortcode('gallery', __NAMESPACE__ . '\\gallery');
add_filter('use_default_gallery_style', '__return_null'); add_filter('use_default_gallery_style', '__return_null');
} }
/** /**
* Add class="thumbnail img-thumbnail" to attachment items * Add class="thumbnail img-thumbnail" to attachment items
*/ */
function sage_attachment_link_class($html) { function attachment_link_class($html) {
$html = str_replace('<a', '<a class="thumbnail img-thumbnail"', $html); $html = str_replace('<a', '<a class="thumbnail img-thumbnail"', $html);
return $html; return $html;
} }
add_filter('wp_get_attachment_link', 'sage_attachment_link_class', 10, 1); add_filter('wp_get_attachment_link', __NAMESPACE__ . '\\attachment_link_class', 10, 1);

View File

@@ -1,8 +1,11 @@
<?php <?php
namespace Roots\Sage\Init;
/** /**
* Sage initial setup and constants * Sage initial setup and constants
*/ */
function sage_setup() { function setup() {
// Make theme available for translation // Make theme available for translation
// Community translations can be found at https://github.com/roots/sage-translations // Community translations can be found at https://github.com/roots/sage-translations
load_theme_textdomain('sage', get_template_directory() . '/lang'); load_theme_textdomain('sage', get_template_directory() . '/lang');
@@ -34,12 +37,12 @@ function sage_setup() {
// Tell the TinyMCE editor to use a custom stylesheet // Tell the TinyMCE editor to use a custom stylesheet
add_editor_style('/dist/css/editor-style.css'); add_editor_style('/dist/css/editor-style.css');
} }
add_action('after_setup_theme', 'sage_setup'); add_action('after_setup_theme', __NAMESPACE__ . '\\setup');
/** /**
* Register sidebars * Register sidebars
*/ */
function sage_widgets_init() { function widgets_init() {
register_sidebar(array( register_sidebar(array(
'name' => __('Primary', 'sage'), 'name' => __('Primary', 'sage'),
'id' => 'sidebar-primary', 'id' => 'sidebar-primary',
@@ -58,4 +61,4 @@ function sage_widgets_init() {
'after_title' => '</h3>', 'after_title' => '</h3>',
)); ));
} }
add_action('widgets_init', 'sage_widgets_init'); add_action('widgets_init', __NAMESPACE__ . '\\widgets_init');

View File

@@ -1,4 +1,7 @@
<?php <?php
namespace Roots\Sage\Nav;
/** /**
* Cleaner walker for wp_nav_menu() * Cleaner walker for wp_nav_menu()
* *
@@ -10,7 +13,7 @@
* <li class="menu-home"><a href="/">Home</a></li> * <li class="menu-home"><a href="/">Home</a></li>
* <li class="menu-sample-page"><a href="/sample-page/">Sample Page</a></li> * <li class="menu-sample-page"><a href="/sample-page/">Sample Page</a></li>
*/ */
class Sage_Nav_Walker extends Walker_Nav_Menu { class Sage_Nav_Walker extends \Walker_Nav_Menu {
function check_current($classes) { function check_current($classes) {
return preg_match('/(current[-_])|active|dropdown/', $classes); return preg_match('/(current[-_])|active|dropdown/', $classes);
} }
@@ -53,7 +56,7 @@ class Sage_Nav_Walker extends Walker_Nav_Menu {
* Remove the id="" on nav menu items * Remove the id="" on nav menu items
* Return 'menu-slug' for nav menu classes * Return 'menu-slug' for nav menu classes
*/ */
function sage_nav_menu_css_class($classes, $item) { function nav_menu_css_class($classes, $item) {
$slug = sanitize_title($item->title); $slug = sanitize_title($item->title);
$classes = preg_replace('/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', 'active', $classes); $classes = preg_replace('/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', 'active', $classes);
$classes = preg_replace('/^((menu|page)[-_\w+]+)+/', '', $classes); $classes = preg_replace('/^((menu|page)[-_\w+]+)+/', '', $classes);
@@ -62,9 +65,9 @@ function sage_nav_menu_css_class($classes, $item) {
$classes = array_unique($classes); $classes = array_unique($classes);
return array_filter($classes, 'is_element_empty'); return array_filter($classes, 'Roots\\Sage\\Utils\\is_element_empty');
} }
add_filter('nav_menu_css_class', 'sage_nav_menu_css_class', 10, 2); add_filter('nav_menu_css_class', __NAMESPACE__ . '\\nav_menu_css_class', 10, 2);
add_filter('nav_menu_item_id', '__return_null'); add_filter('nav_menu_item_id', '__return_null');
/** /**
@@ -73,19 +76,19 @@ add_filter('nav_menu_item_id', '__return_null');
* Remove the container * Remove the container
* Use Sage_Nav_Walker() by default * Use Sage_Nav_Walker() by default
*/ */
function sage_nav_menu_args($args = '') { function nav_menu_args($args = '') {
$sage_nav_menu_args = array(); $nav_menu_args = array();
$sage_nav_menu_args['container'] = false; $nav_menu_args['container'] = false;
if (!$args['items_wrap']) { if (!$args['items_wrap']) {
$sage_nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>'; $nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>';
} }
if (!$args['depth']) { if (!$args['depth']) {
$sage_nav_menu_args['depth'] = 2; $nav_menu_args['depth'] = 2;
} }
return array_merge($args, $sage_nav_menu_args); return array_merge($args, $nav_menu_args);
} }
add_filter('wp_nav_menu_args', 'sage_nav_menu_args'); add_filter('wp_nav_menu_args', __NAMESPACE__ . '\\nav_menu_args');

View File

@@ -1,4 +1,7 @@
<?php <?php
namespace Roots\Sage\Sidebar;
/** /**
* Determines whether or not to display the sidebar based on an array of conditional tags or page templates. * Determines whether or not to display the sidebar based on an array of conditional tags or page templates.
* *

View File

@@ -1,8 +1,11 @@
<?php <?php
namespace Roots\Sage\Titles;
/** /**
* Page titles * Page titles
*/ */
function sage_title() { function title() {
if (is_home()) { if (is_home()) {
if (get_option('page_for_posts', true)) { if (get_option('page_for_posts', true)) {
return get_the_title(get_option('page_for_posts', true)); return get_the_title(get_option('page_for_posts', true));

View File

@@ -1,4 +1,7 @@
<?php <?php
namespace Roots\Sage\Utils;
/** /**
* Utility functions * Utility functions
*/ */
@@ -8,17 +11,17 @@ function is_element_empty($element) {
} }
// Tell WordPress to use searchform.php from the templates/ directory // Tell WordPress to use searchform.php from the templates/ directory
function sage_get_search_form() { function get_search_form() {
$form = ''; $form = '';
locate_template('/templates/searchform.php', true, false); locate_template('/templates/searchform.php', true, false);
return $form; return $form;
} }
add_filter('get_search_form', 'sage_get_search_form'); add_filter('get_search_form', __NAMESPACE__ . '\\get_search_form');
/** /**
* Add page slug to body_class() classes if it doesn't exist * Add page slug to body_class() classes if it doesn't exist
*/ */
function sage_body_class($classes) { function body_class($classes) {
// Add post/page slug // Add post/page slug
if (is_single() || is_page() && !is_front_page()) { if (is_single() || is_page() && !is_front_page()) {
if (!in_array(basename(get_permalink()), $classes)) { if (!in_array(basename(get_permalink()), $classes)) {
@@ -27,4 +30,4 @@ function sage_body_class($classes) {
} }
return $classes; return $classes;
} }
add_filter('body_class', 'sage_body_class'); add_filter('body_class', __NAMESPACE__ . '\\body_class');

View File

@@ -1,15 +1,19 @@
<?php <?php
namespace Roots\Sage\Wrapper;
/** /**
* Theme wrapper * Theme wrapper
* *
* @link http://roots.io/getting-started/theme-wrapper/ * @link http://roots.io/getting-started/theme-wrapper/
* @link http://scribu.net/wordpress/theme-wrappers.html * @link http://scribu.net/wordpress/theme-wrappers.html
*/ */
function sage_template_path() {
function template_path() {
return Sage_Wrapping::$main_template; return Sage_Wrapping::$main_template;
} }
function sage_sidebar_path() { function sidebar_path() {
return new Sage_Wrapping('templates/sidebar.php'); return new Sage_Wrapping('templates/sidebar.php');
} }
@@ -57,4 +61,4 @@ class Sage_Wrapping {
return new Sage_Wrapping(); return new Sage_Wrapping();
} }
} }
add_filter('template_include', array('Sage_Wrapping', 'wrap'), 99); add_filter('template_include', array(__NAMESPACE__ . '\\Sage_Wrapping', 'wrap'), 99);

View File

@@ -1,3 +1,5 @@
<?php use Roots\Sage\Nav; ?>
<header class="banner navbar navbar-default navbar-static-top" role="banner"> <header class="banner navbar navbar-default navbar-static-top" role="banner">
<div class="container"> <div class="container">
<div class="navbar-header"> <div class="navbar-header">
@@ -13,7 +15,7 @@
<nav class="collapse navbar-collapse" role="navigation"> <nav class="collapse navbar-collapse" role="navigation">
<?php <?php
if (has_nav_menu('primary_navigation')) : if (has_nav_menu('primary_navigation')) :
wp_nav_menu(array('theme_location' => 'primary_navigation', 'walker' => new Sage_Nav_Walker(), 'menu_class' => 'nav navbar-nav')); wp_nav_menu(array('theme_location' => 'primary_navigation', 'walker' => new Nav\Sage_Nav_Walker(), 'menu_class' => 'nav navbar-nav'));
endif; endif;
?> ?>
</nav> </nav>

View File

@@ -1,5 +1,7 @@
<?php use Roots\Sage\Titles; ?>
<div class="page-header"> <div class="page-header">
<h1> <h1>
<?php echo sage_title(); ?> <?php echo Titles\title(); ?>
</h1> </h1>
</div> </div>