1 | <?php |
||
25 | class Gamajo_Template_Loader { |
||
26 | /** |
||
27 | * Prefix for filter names. |
||
28 | * |
||
29 | * @since 1.0.0 |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $filter_prefix = 'your_plugin'; |
||
34 | |||
35 | /** |
||
36 | * Directory name where custom templates for this plugin should be found in the theme. |
||
37 | * |
||
38 | * For example: 'your-plugin-templates'. |
||
39 | * |
||
40 | * @since 1.0.0 |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $theme_template_directory = 'plugin-templates'; |
||
45 | |||
46 | /** |
||
47 | * Reference to the root directory path of this plugin. |
||
48 | * |
||
49 | * Can either be a defined constant, or a relative reference from where the subclass lives. |
||
50 | * |
||
51 | * e.g. YOUR_PLUGIN_TEMPLATE or plugin_dir_path( dirname( __FILE__ ) ); etc. |
||
52 | * |
||
53 | * @since 1.0.0 |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $plugin_directory = 'YOUR_PLUGIN_DIR'; |
||
58 | |||
59 | /** |
||
60 | * Directory name where templates are found in this plugin. |
||
61 | * |
||
62 | * Can either be a defined constant, or a relative reference from where the subclass lives. |
||
63 | * |
||
64 | * e.g. 'templates' or 'includes/templates', etc. |
||
65 | * |
||
66 | * @since 1.1.0 |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $plugin_template_directory = 'templates'; |
||
71 | |||
72 | /** |
||
73 | * Clean up template data. |
||
74 | * |
||
75 | * @since 1.2.0 |
||
76 | */ |
||
77 | public function __destruct() { |
||
80 | |||
81 | /** |
||
82 | * Retrieve a template part. |
||
83 | * |
||
84 | * @since 1.0.0 |
||
85 | * |
||
86 | * @param string $slug Template slug. |
||
87 | * @param string $name Optional. Template variation name. Default null. |
||
88 | * @param bool $load Optional. Whether to load template. Default true. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function get_template_part( $slug, $name = null, $load = true ) { |
||
102 | |||
103 | /** |
||
104 | * Make custom data available to template. |
||
105 | * |
||
106 | * Data is available to the template as properties under the `$data` variable. |
||
107 | * i.e. A value provided here under `$data['foo']` is available as `$data->foo`. |
||
108 | * |
||
109 | * When an input key has a hyphen, you can use `$data->{foo-bar}` in the template. |
||
110 | * |
||
111 | * @since 1.2.0 |
||
112 | * |
||
113 | * @param array $data Custom data for the template. |
||
114 | * @param string $var_name Optional. Variable under which the custom data is available in the template. |
||
115 | * Default is 'data'. |
||
116 | */ |
||
117 | public function set_template_data( array $data, $var_name = 'data' ) { |
||
122 | |||
123 | /** |
||
124 | * Remove access to custom data in template. |
||
125 | * |
||
126 | * Good to use once the final template part has been requested. |
||
127 | * |
||
128 | * @since 1.2.0 |
||
129 | */ |
||
130 | public function unset_template_data() { |
||
137 | |||
138 | /** |
||
139 | * Given a slug and optional name, create the file names of templates. |
||
140 | * |
||
141 | * @since 1.0.0 |
||
142 | * |
||
143 | * @param string $slug Template slug. |
||
144 | * @param string $name Template variation name. |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | protected function get_template_file_names( $slug, $name ) { |
||
169 | |||
170 | /** |
||
171 | * Retrieve the name of the highest priority template file that exists. |
||
172 | * |
||
173 | * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which |
||
174 | * inherit from a parent theme can just overload one file. If the template is |
||
175 | * not found in either of those, it looks in the theme-compat folder last. |
||
176 | * |
||
177 | * @since 1.0.0 |
||
178 | * |
||
179 | * @param string|array $template_names Template file(s) to search for, in order. |
||
180 | * @param bool $load If true the template file will be loaded if it is found. |
||
181 | * @param bool $require_once Whether to require_once or require. Default true. |
||
182 | * Has no effect if $load is false. |
||
183 | * |
||
184 | * @return string The template filename if one is located. |
||
185 | */ |
||
186 | public function locate_template( $template_names, $load = false, $require_once = true ) { |
||
214 | |||
215 | /** |
||
216 | * Return a list of paths to check for template locations. |
||
217 | * |
||
218 | * Default is to check in a child theme (if relevant) before a parent theme, so that themes which inherit from a |
||
219 | * parent theme can just overload one file. If the template is not found in either of those, it looks in the |
||
220 | * theme-compat folder last. |
||
221 | * |
||
222 | * @since 1.0.0 |
||
223 | * |
||
224 | * @return mixed|void |
||
225 | */ |
||
226 | protected function get_template_paths() { |
||
253 | |||
254 | /** |
||
255 | * Return the path to the templates directory in this plugin. |
||
256 | * |
||
257 | * May be overridden in subclass. |
||
258 | * |
||
259 | * @since 1.0.0 |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | protected function get_templates_dir() { |
||
266 | } |
||
267 | } |
||
268 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state