-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-template-controller.php
189 lines (170 loc) · 6.09 KB
/
wp-template-controller.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/*
Plugin Name: Template Controller
Plugin URI: https://github.com/creativecoder/wp-template-controller
Description: Separate data generation from presentation in your WordPress templates
Version: 0.1.4
Author: Grant Kinney
Author URI: https://github.com/creativecoder
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: 'template-controller'
*/
if ( ! class_exists( 'Template_Controller' ) ) {
/**
* Parent class for passing data into template files
*
* All properties in this class are declared statically so that all data from child
* classes can be passed back to this parent class and called directly from here.
* (This takes advantage of a PHP quirk that that within child classes, `self` refers
* to the parent class)
*
* @package WordPress
* @subpackage Template_Controller
* @author Grant Kinney
* @license http://mit-license.org/ MIT
*/
class Template_Controller {
/**
* An array of instances used for singleton pattern that stores an instance for this
* parent class and separate instances for each child class
* @var array
*/
protected static $instances = array();
/**
* An array of body class for the current template
* @var array
*/
protected static $classes = array();
/**
* A key value store for data that is passed into templates
* @var array
*/
protected static $data = array();
protected static $data_replace = array();
/**
* Initialize class
* @return void
*/
public static function init() {
self::get_instance();
}
/**
* Singleton pattern: instantiate one and only one instance of this class, and one and
* only one instance of each child class that extends it
*
* @return object instance of class being instantiated
*/
final public static function get_instance() {
// Note, need PHP 5.3 or greater to use `get_called_class()`
$class = get_called_class();
if ( ! isset( self::$instances[$class] ) ) {
self::$instances[$class] = new $class;
}
return self::$instances[$class];
}
/**
* Hooks for setting up methods within class
*/
public function __construct() {
// Load template data before including template files
add_action( 'template_redirect', array( $this, 'load' ) );
}
/**
* Get a value from the template data by name
*
* @param string $name Name of data stored
* @return mixed Value of data, if it exists, otherwise false
*/
public function get( $name ) {
$data = false;
if ( isset(self::$data_replace[$name]) ) {
$data = self::$data_replace[$name];
} else if ( isset(self::$data[$name]) ) {
$data = self::$data[$name];
}
return $data;
}
/**
* Load template data
*
* Loops through each body class for the current template, checks for a method of
* the same name as the class, and calls that method if it exists
*
* @return void
*/
public function load() {
/**
* Global variable to store template data.
*
* Place the following at the top of your template file as an alternative to calling
* `tpl_data()` or `get_tpl_data()` functions for each individual data key
* `global $template_data;`
* `extract( $template_data, EXTR_SKIP );`
*
* @global array template_data
*/
global $template_data;
// Add `common` to class array so that it loads for every template
self::$classes = get_body_class( 'common' );
foreach( self::$classes as $class ) {
$class = str_replace( '-', '_', $class );
if ( method_exists( $this, $class ) ) {
call_user_func( array( $this, $class ) );
}
}
// Push template data out to global variable
$template_data = array_merge( self::$data, self::$data_replace );
}
/**
* Add data by name for use in templates
*
* @param string $name Name of data stored
* @param mixed $data Value of data
*/
protected function add( $name, $data ) {
self::$data[$name] = $data;
}
protected function replace( $name, $data ) {
self::$data_replace[$name] = $data;
}
} // end class
} // endif class_exists
/**
* Return a template data value from the specified name
*
* @param string $name Name of data key to retrieve
* @return mixed Data value for use in template
*/
function get_tpl_data( $name ) {
return Template_Controller::get_instance()->get($name);
}
/**
* Echo a template data value from the specified name
*
* @param string $name Name of data key to retrieve
* @return void
*/
function tpl_data( $name ) {
echo esc_html(Template_Controller::get_instance()->get($name));
}
add_action( 'admin_init', function () {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return; }
if ( ! class_exists('WP_GitHub_Updater') ) {
include_once( 'WordPress-GitHub-Plugin-Updater/updater.php' );
}
$config = array(
'slug' => plugin_basename(__FILE__), // this is the slug of your plugin
'proper_folder_name' => 'wp-template-controller', // this is the name of the folder your plugin lives in
'api_url' => 'https://api.github.com/repos/creativecoder/wp-template-controller', // the GitHub API url of your GitHub repo
'raw_url' => 'https://raw.github.com/creativecoder/wp-template-controller/master', // the GitHub raw url of your GitHub repo
'github_url' => 'https://github.com/creativecoder/wp-template-controller', // the GitHub url of your GitHub repo
'zip_url' => 'https://github.com/creativecoder/wp-template-controller/zipball/master', // the zip url of the GitHub repo
'sslverify' => true, // whether WP should check the validity of the SSL cert when getting an update, see https://github.com/jkudish/WordPress-GitHub-Plugin-Updater/issues/2 and https://github.com/jkudish/WordPress-GitHub-Plugin-Updater/issues/4 for details
'requires' => '3.0', // which version of WordPress does your plugin require?
'tested' => '4.4', // which version of WordPress is your plugin tested up to?
'readme' => 'README.md', // which file to use as the readme for the version number
'access_token' => '', // Access private repositories by authorizing under Appearance > GitHub Updates when this example plugin is installed
);
new WP_GitHub_Updater( $config );
});