15 if ( ! class_exists(
'\GV\Gamajo_Template_Loader' ) ) {
35 protected $filter_prefix =
'your_plugin';
46 protected $theme_template_directory =
'plugin-templates';
59 protected $plugin_directory =
'YOUR_PLUGIN_DIR';
72 protected $plugin_template_directory =
'templates';
83 private $template_data_var_names = array(
'data' );
90 private $file_paths = array();
97 public function __destruct() {
98 $this->unset_template_data();
111 public function get_template_part( $slug,
$name = null, $load =
true ) {
113 do_action(
'get_template_part_' . $slug, $slug,
$name );
114 do_action( $this->filter_prefix .
'_get_template_part_' . $slug, $slug,
$name );
120 return $this->locate_template(
$templates, $load,
false );
138 public function set_template_data( $data, $var_name =
'data' ) {
141 $wp_query->query_vars[ $var_name ] = (object) $data;
144 if (
'data' !== $var_name ) {
145 $this->template_data_var_names[] = $var_name;
160 public function unset_template_data() {
164 $custom_var_names = array_unique( $this->template_data_var_names );
167 foreach ( $custom_var_names as $var ) {
168 if ( isset( $wp_query->query_vars[ $var ] ) ) {
169 unset( $wp_query->query_vars[ $var ] );
185 protected function get_template_file_names( $slug,
$name ) {
187 if ( isset(
$name ) ) {
204 return apply_filters( $this->filter_prefix .
'_get_template_part',
$templates, $slug,
$name );
222 public function locate_template( $template_names, $load =
false, $require_once =
true ) {
223 static $template_path_cache = array();
226 $cache_key = md5( json_encode( $template_names ) );
229 if ( isset( $template_path_cache[ $this->filter_prefix ][ $cache_key ] ) ) {
230 $located = $template_path_cache[ $this->filter_prefix ][ $cache_key ];
236 $template_names = array_filter( (array) $template_names );
237 $template_paths = $this->get_template_paths();
240 foreach ( $template_names as $template_name ) {
242 $template_name = ltrim( $template_name,
'/' );
245 foreach ( $template_paths as $template_path ) {
246 if ( file_exists( $template_path . $template_name ) ) {
247 $located = $template_path . $template_name;
249 $template_path_cache[ $this->filter_prefix ][ $cache_key ] = $located;
257 $template_path_cache[ $this->filter_prefix ][ $cache_key ] =
false;
261 if ( $load && $located ) {
262 load_template( $located, $require_once );
279 protected function get_template_paths() {
281 if ( ! empty( $this->file_paths ) ) {
282 return $this->file_paths;
285 $theme_directory = trailingslashit( $this->theme_template_directory );
288 10 => trailingslashit( get_template_directory() ) . $theme_directory,
289 100 => $this->get_templates_dir(),
293 if ( get_stylesheet_directory() !== get_template_directory() ) {
294 $file_paths[1] = trailingslashit( get_stylesheet_directory() ) . $theme_directory;
304 $file_paths = apply_filters( $this->filter_prefix .
'_template_paths', $file_paths );
307 ksort( $file_paths, SORT_NUMERIC );
309 $file_paths = array_map(
'trailingslashit', $file_paths );
311 $this->file_paths = $file_paths;
313 return $this->file_paths;
325 protected function get_templates_dir() {
326 return trailingslashit( $this->plugin_directory ) . $this->plugin_template_directory;
Template Loader for Plugins.