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