-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
142 lines (117 loc) · 3.46 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
class Hand_Built {
private static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self;
self::$instance->setup_constants();
self::$instance->setup_actions();
self::$instance->setup_filters();
self::$instance->require_files();
self::$instance->setup_controllers();
}
return self::$instance;
}
private function setup_constants() {
}
private function setup_actions() {
add_action( 'init', function(){
if ( ! is_admin() ) {
show_admin_bar( false );
}
});
add_action( 'after_setup_theme', array( $this, 'action_after_setup_theme' ) );
}
private function setup_filters() {
add_filter( 'pre_option_blogname', function() {
return 'Hand Built';
});
add_filter( 'pre_option_blogdescription', function() {
return 'Technology leadership you can trust';
});
/**
* Ensure plugin assets loaded from within the theme are served at a corrected URL
*/
add_filter( 'plugins_url', function( $url = '', $path = '', $plugin = '' ){
if ( empty( $plugin ) ) {
return $url;
}
if ( 0 === strpos( $plugin, get_stylesheet_directory() ) ) {
$url_override = str_replace( get_stylesheet_directory(), get_stylesheet_directory_uri(), dirname( $plugin ) );
$url = trailingslashit( $url_override ) . $path;
}
return $url;
}, 9, 3 );
}
private function require_files() {
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/lib/msm-sitemap/msm-sitemap.php';
spl_autoload_register( function( $class ) {
$class = ltrim( $class, '\\' );
if ( 0 !== stripos( $class, 'Hand_Built\\' ) ) {
return;
}
$parts = explode( '\\', $class );
array_shift( $parts ); // Don't need "Hand_Built"
$last = array_pop( $parts ); // File should be 'class-[...].php'
$last = 'class-' . $last . '.php';
$parts[] = $last;
$file = dirname( __FILE__ ) . '/inc/' . str_replace( '_', '-', strtolower( implode( $parts, '/' ) ) );
if ( file_exists( $file ) ) {
require $file;
}
// Might be a trait
$file = str_replace( '/class-', '/trait-', $file );
if ( file_exists( $file ) ) {
require $file;
}
});
}
private function setup_controllers() {
$controllers = array(
'\Hand_Built\Assets',
'\Hand_Built\Content_Model',
'\Hand_Built\Query',
);
foreach( $controllers as $controller ) {
$controller::get_instance();
}
}
public function action_after_setup_theme() {
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'post-formats', array( 'aside', 'status', 'quote' ) );
add_theme_support( 'html5' );
add_image_size( 'twitter-card', 560, 294, true );
add_image_size( 'facebook-open-graph', 1200, 630, true );
}
/**
* Get a rendered template part
*
* @param string $template
* @param array $vars
* @return string
*/
public static function get_template_part( $template, $vars = array() ) {
$full_path = get_template_directory() . '/parts/' . $template . '.php';
if ( ! file_exists( $full_path ) ) {
return '';
}
ob_start();
// @codingStandardsIgnoreStart
if ( ! empty( $vars ) ) {
extract( $vars );
}
// @codingStandardsIgnoreEnd
include $full_path;
return ob_get_clean();
}
/**
* Get the current request URI
*/
public static function get_request_uri() {
// Accommodate subdirectory installs
return str_replace( parse_url( home_url(), PHP_URL_PATH ), '', $_SERVER['REQUEST_URI'] );
}
}
Hand_Built::get_instance();