class-auxels-admin-assets.php
5 months ago
class-auxels-archive-menu-links.php
5 months ago
class-auxels-envato-elements.php
5 months ago
class-auxels-import-parser.php
3 years ago
class-auxels-import.php
3 years ago
class-auxels-search-post-type.php
5 months ago
class-auxels-wc-attribute-nav-menu.php
5 months ago
class-auxin-admin-dashboard.php
5 months ago
class-auxin-demo-importer.php
2 days ago
class-auxin-dependency-sorting.php
3 years ago
class-auxin-import.php
5 months ago
class-auxin-install.php
5 months ago
class-auxin-master-nav-menu-admin.php
5 months ago
class-auxin-page-template.php
5 months ago
class-auxin-permalink.php
5 months ago
class-auxin-plugin-requirements.php
3 years ago
class-auxin-post-type-base.php
5 months ago
class-auxin-svg-support-allowedattributes.php
7 years ago
class-auxin-svg-support-allowedtags.php
7 years ago
class-auxin-svg-support.php
1 year ago
class-auxin-walker-nav-menu-back.php
5 months ago
class-auxin-welcome-sections.php
1 year ago
class-auxin-welcome.php
1 month ago
class-auxin-whitelabel.php
3 years ago
class-auxin-widget-indie.php
5 months ago
class-auxin-widget-shortcode-map.php
5 months ago
class-auxin-widget.php
5 months ago
class-auxin-page-template.php
214 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Custom Page Templates for Plugins |
| 4 | * |
| 5 | * |
| 6 | * @package Auxin |
| 7 | * @license LICENSE.txt |
| 8 | * @author averta |
| 9 | * @link http://phlox.pro/ |
| 10 | * @copyright (c) 2010-2026 averta |
| 11 | */ |
| 12 | class Auxin_Page_Template { |
| 13 | |
| 14 | /** |
| 15 | * The array of templates that this plugin tracks. |
| 16 | * |
| 17 | * @var array |
| 18 | */ |
| 19 | private $templates = array(); |
| 20 | |
| 21 | /** |
| 22 | * The array of template directories that are required to be tracked. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | private $template_directories = array(); |
| 27 | |
| 28 | /** |
| 29 | * Instance of this class. |
| 30 | * |
| 31 | * @var object |
| 32 | */ |
| 33 | protected static $instance = null; |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Return an instance of this class. |
| 38 | * |
| 39 | * @return object A single instance of this class. |
| 40 | */ |
| 41 | public static function get_instance() { |
| 42 | |
| 43 | // If the single instance hasn't been set, set it now. |
| 44 | if ( null == self::$instance ) { |
| 45 | self::$instance = new self; |
| 46 | } |
| 47 | |
| 48 | return self::$instance; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Initializes the plugin by setting filters and administration functions. |
| 53 | */ |
| 54 | private function __construct() { |
| 55 | |
| 56 | // Add a filter to the save post to inject out template into the page cache |
| 57 | add_filter( 'wp_insert_post_data', array( $this, 'register_templates' ) ); |
| 58 | |
| 59 | // Add a filter to the template include to determine if the page has our |
| 60 | add_filter( 'template_include', array( $this, 'include_template') ); |
| 61 | |
| 62 | // Add a filter to the attributes metabox to inject template into the dropdown. |
| 63 | add_filter( "theme_page_templates", array( $this, 'add_page_templates' ), 10, 3 ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Add the auxin page templates to the theme templates. |
| 68 | */ |
| 69 | public function add_page_templates( $page_templates, $wp_theme, $post ) { |
| 70 | // Make sure the templates ($this->templates) are collected. |
| 71 | if( ! $this->get_templates() ){ |
| 72 | return $page_templates; |
| 73 | } |
| 74 | |
| 75 | return $page_templates + $this->templates; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Collect and retrieve the page templates |
| 80 | * |
| 81 | * @return array |
| 82 | */ |
| 83 | private function get_templates(){ |
| 84 | |
| 85 | // Return the templates if they were retrieved before. |
| 86 | if( ! empty( $this->templates ) ){ |
| 87 | return $this->templates; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * $templates An array containing the list of template file names and labels |
| 92 | * |
| 93 | * @var array |
| 94 | * @example array( |
| 95 | * 'custom-template-name1.php' => __( 'Custom Template Name1', 'plugin-domain' ), |
| 96 | * 'custom-template-name2.php' => __( 'Custom Template Name2', 'plugin-domain' ) |
| 97 | * ); |
| 98 | */ |
| 99 | $this->templates = apply_filters( 'auxin/core_elements/page_templates/file_names', $this->templates ); |
| 100 | |
| 101 | return $this->templates; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Collect and retrieve the page template directories. |
| 106 | * |
| 107 | * @return array |
| 108 | */ |
| 109 | private function get_template_directories(){ |
| 110 | |
| 111 | // Return the template directories if they were retrieved before. |
| 112 | if( ! empty( $this->template_directories ) ){ |
| 113 | return $this->template_directories; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * An array containing the list of template directories. |
| 118 | * |
| 119 | * @var array |
| 120 | * @example array( |
| 121 | * 'path/to/custom/page/template1', |
| 122 | * 'path/to/custom/page/template2' |
| 123 | * ); |
| 124 | */ |
| 125 | $this->template_directories = apply_filters( 'auxin/core_elements/page_templates/directories', $this->template_directories ); |
| 126 | |
| 127 | return $this->template_directories; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Adds custom template to the pages cache in order to trick WordPress |
| 133 | * into thinking the template file exists where it doens't really exist. |
| 134 | */ |
| 135 | public function register_templates( $atts ) { |
| 136 | |
| 137 | // Create the key used for the themes cache |
| 138 | $cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() ); |
| 139 | |
| 140 | // If cache list doesn't exist, or it's empty prepare an array |
| 141 | $templates = wp_get_theme()->get_page_templates(); |
| 142 | if ( empty( $templates ) ) { |
| 143 | $templates = array(); |
| 144 | } |
| 145 | |
| 146 | // Make sure the templates ($this->templates) are collected. |
| 147 | $this->get_templates(); |
| 148 | |
| 149 | // Quit if no custom template is defined. |
| 150 | if( empty( $this->templates ) || ! is_array( $this->templates ) ){ |
| 151 | return $templates; |
| 152 | } |
| 153 | |
| 154 | // New cache, therefore remove the old one |
| 155 | wp_cache_delete( $cache_key , 'themes'); |
| 156 | |
| 157 | // Now add our template to the list of templates by merging our templates |
| 158 | $templates = array_merge( $templates, $this->templates ); |
| 159 | |
| 160 | // Add the modified cache to allow WordPress to pick it up for listing |
| 161 | wp_cache_add( $cache_key, $templates, 'themes', 1800 ); |
| 162 | |
| 163 | return $atts; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Checks if the template is assigned to the page |
| 168 | */ |
| 169 | public function include_template( $template ) { |
| 170 | |
| 171 | // Return the search template if we're searching (instead of the template for the first result) |
| 172 | if ( is_search() ) { |
| 173 | return $template; |
| 174 | } |
| 175 | |
| 176 | global $post; |
| 177 | if ( ! $post ) { |
| 178 | return $template; |
| 179 | } |
| 180 | |
| 181 | // Make sure the templates ($this->templates) are collected. |
| 182 | $this->get_templates(); |
| 183 | |
| 184 | // Get the saved template name id |
| 185 | $template_file_id = get_post_meta( $post->ID, '_wp_page_template', true ); |
| 186 | |
| 187 | // Return default template if we don't have a custom one defined |
| 188 | if ( ! isset( $this->templates[ $template_file_id ] ) ) { |
| 189 | return $template; |
| 190 | } |
| 191 | |
| 192 | // Make sure the template directories ($this->template_directories) are collected. |
| 193 | $this->get_template_directories(); |
| 194 | |
| 195 | // Quit if no custom template directory is defined. |
| 196 | if( empty( $this->template_directories ) || ! is_array( $this->template_directories ) ){ |
| 197 | return $template; |
| 198 | } |
| 199 | |
| 200 | foreach ( $this->template_directories as $directory ) { |
| 201 | $template_file = trailingslashit( $directory ) . $template_file_id; |
| 202 | |
| 203 | // Just to be safe, we check if the file exist first |
| 204 | if ( file_exists( $template_file ) ) { |
| 205 | return $template_file; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // Return template |
| 210 | return $template; |
| 211 | } |
| 212 | |
| 213 | } |
| 214 |