| 1 |
<?php |
| 2 |
/** |
| 3 |
* LoginPress Theme Template Class. |
| 4 |
* |
| 5 |
* LoginPress template functions. |
| 6 |
* |
| 7 |
* @package LoginPress |
| 8 |
* @since 1.1.3 |
| 9 |
*/ |
| 10 |
|
| 11 |
// Exit if accessed directly. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
if ( ! class_exists( 'LoginPress_Theme_Template' ) ) : |
| 17 |
|
| 18 |
/** |
| 19 |
* LoginPress Theme Template Class. |
| 20 |
* |
| 21 |
* Add LoginPress Templates to use in the theme. |
| 22 |
* |
| 23 |
* @since 1.1.3 |
| 24 |
*/ |
| 25 |
class LoginPress_Theme_Template { |
| 26 |
|
| 27 |
/** |
| 28 |
* A reference to an instance of this class. |
| 29 |
* |
| 30 |
* @var LoginPress_Theme_Template|null |
| 31 |
*/ |
| 32 |
private static $instance; |
| 33 |
|
| 34 |
/** |
| 35 |
* The array of templates that this plugin tracks. |
| 36 |
* |
| 37 |
* @var array<string, string> |
| 38 |
*/ |
| 39 |
protected $templates; |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns an instance of this class. |
| 43 |
* |
| 44 |
* @since 1.1.3 |
| 45 |
* @return LoginPress_Theme_Template |
| 46 |
*/ |
| 47 |
public static function get_instance() { |
| 48 |
if ( null === self::$instance ) { |
| 49 |
self::$instance = new LoginPress_Theme_Template(); |
| 50 |
} |
| 51 |
|
| 52 |
return self::$instance; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Initializes the plugin by setting filters and administration functions. |
| 57 |
* |
| 58 |
* @since 1.1.3 |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
private function __construct() { |
| 62 |
|
| 63 |
$this->templates = array(); |
| 64 |
|
| 65 |
// Add a filter to the attributes metabox to inject template into the cache. |
| 66 |
if ( version_compare( (string) get_bloginfo( 'version' ), '4.7', '<' ) ) { |
| 67 |
// 4.6 and older. |
| 68 |
add_filter( 'page_attributes_dropdown_pages_args', array( $this, 'register_project_templates' ) ); |
| 69 |
} else { |
| 70 |
// Add a filter to the wp 4.7 version attributes metabox. |
| 71 |
add_filter( 'theme_page_templates', array( $this, 'add_new_template' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
// Add a filter to the save post to inject out template into the page cache. |
| 75 |
add_filter( 'wp_insert_post_data', array( $this, 'register_project_templates' ) ); |
| 76 |
|
| 77 |
// Add a filter to the template include to determine if the page has our template assigned and return it's path. |
| 78 |
add_filter( 'template_include', array( $this, 'view_project_template' ) ); |
| 79 |
|
| 80 |
// Add templates. |
| 81 |
$this->templates = array( |
| 82 |
'template-loginpress.php' => 'LoginPress', |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Adds our template to the page dropdown for v4.7+. |
| 88 |
* |
| 89 |
* @param array<string, string> $posts_templates The templates array. |
| 90 |
* @return array<string, string> |
| 91 |
*/ |
| 92 |
public function add_new_template( $posts_templates ) { |
| 93 |
|
| 94 |
$posts_templates = array_merge( $posts_templates, $this->templates ); |
| 95 |
return $posts_templates; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Adds our template to the pages cache in order to trick WordPress. |
| 100 |
* into thinking the template file exists where it doesn't really exist. |
| 101 |
* |
| 102 |
* @param array<string, mixed> $atts The attributes array. |
| 103 |
* @return array<string, mixed> |
| 104 |
*/ |
| 105 |
public function register_project_templates( $atts ) { |
| 106 |
|
| 107 |
// Create the key used for the themes cache. |
| 108 |
$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() ); |
| 109 |
|
| 110 |
// Retrieve the cache list. |
| 111 |
// If it doesn't exist, or it's empty prepare an array. |
| 112 |
$templates = wp_get_theme()->get_page_templates(); |
| 113 |
if ( empty( $templates ) ) { |
| 114 |
$templates = array(); |
| 115 |
} |
| 116 |
|
| 117 |
// New cache, therefore remove the old one. |
| 118 |
wp_cache_delete( $cache_key, 'themes' ); |
| 119 |
|
| 120 |
// Now add our template to the list of templates by merging our templates |
| 121 |
// with the existing templates array from the cache. |
| 122 |
$templates = array_merge( $templates, $this->templates ); |
| 123 |
|
| 124 |
// Add the modified cache to allow WordPress to pick it up for listing |
| 125 |
// available templates. |
| 126 |
wp_cache_add( $cache_key, $templates, 'themes', 1800 ); |
| 127 |
|
| 128 |
return $atts; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Checks if the template is assigned to the page. |
| 133 |
* |
| 134 |
* @param string $template The template path. |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
public function view_project_template( $template ) { |
| 138 |
|
| 139 |
// Get global post. |
| 140 |
global $post; |
| 141 |
|
| 142 |
// Return template if post is empty. |
| 143 |
if ( ! $post ) { |
| 144 |
return $template; |
| 145 |
} |
| 146 |
|
| 147 |
// Return default template if we don't have a custom one defined. |
| 148 |
if ( ! isset( $this->templates[ get_post_meta( $post->ID, '_wp_page_template', true ) ] ) ) { |
| 149 |
return $template; |
| 150 |
} |
| 151 |
|
| 152 |
$file = plugin_dir_path( __FILE__ ) . get_post_meta( $post->ID, '_wp_page_template', true ); |
| 153 |
|
| 154 |
// Just to be safe, we check if the file exist first. |
| 155 |
if ( file_exists( $file ) ) { |
| 156 |
return $file; |
| 157 |
} else { |
| 158 |
echo esc_html( $file ); |
| 159 |
} |
| 160 |
|
| 161 |
// Return template. |
| 162 |
return $template; |
| 163 |
} |
| 164 |
} |
| 165 |
endif; |
| 166 |
add_action( |
| 167 |
'plugins_loaded', |
| 168 |
function () { |
| 169 |
LoginPress_Theme_Template::get_instance(); |
| 170 |
} |
| 171 |
); |
| 172 |
|