page-template.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @author ThemePunch <info@themepunch.com>
  4. * @link http://www.themepunch.com/
  5. * @copyright 2016 ThemePunch
  6. */
  7. if( !defined( 'ABSPATH') ) exit();
  8. class RevSliderPageTemplate {
  9. /**
  10. * A reference to an instance of this class.
  11. */
  12. private static $instance;
  13. /**
  14. * The array of templates that this plugin tracks.
  15. */
  16. protected $templates;
  17. /**
  18. * Returns an instance of this class.
  19. */
  20. public static function get_instance() {
  21. if( null == self::$instance ) {
  22. self::$instance = new RevSliderPageTemplate();
  23. }
  24. return self::$instance;
  25. }
  26. /**
  27. * Initializes the plugin by setting filters and administration functions.
  28. */
  29. private function __construct() {
  30. $this->templates = array();
  31. // Add a filter to the attributes metabox to inject template into the cache.
  32. add_filter(
  33. 'page_attributes_dropdown_pages_args',
  34. array( $this, 'register_project_templates' )
  35. );
  36. // Add a filter to the save post to inject out template into the page cache
  37. add_filter(
  38. 'wp_insert_post_data',
  39. array( $this, 'register_project_templates' )
  40. );
  41. // Add a filter to the template include to determine if the page has our
  42. // template assigned and return it's path
  43. add_filter(
  44. 'template_include',
  45. array( $this, 'view_project_template')
  46. );
  47. // Add your templates to this array.
  48. $this->templates = array(
  49. '../public/views/revslider-page-template.php' => 'Slider Revolution Blank Template',
  50. );
  51. // Fix for WP 4.7
  52. add_filter( 'theme_page_templates', array($this, 'register_project_templates_new' ) );
  53. }
  54. // Adds our template to the new post templates setting (WP >= 4.7)
  55. public function register_project_templates_new( $post_templates ) {
  56. $post_templates = array_merge( $post_templates, $this->templates );
  57. return $post_templates;
  58. }
  59. /**
  60. * Adds our template to the pages cache in order to trick WordPress
  61. * into thinking the template file exists where it doens't really exist.
  62. *
  63. */
  64. public function register_project_templates( $atts ) {
  65. // Create the key used for the themes cache
  66. $cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
  67. // Retrieve the cache list.
  68. // If it doesn't exist, or it's empty prepare an array
  69. $templates = wp_get_theme()->get_page_templates();
  70. if ( empty( $templates ) ) {
  71. $templates = array();
  72. }
  73. // New cache, therefore remove the old one
  74. wp_cache_delete( $cache_key , 'themes');
  75. // Now add our template to the list of templates by merging our templates
  76. // with the existing templates array from the cache.
  77. $templates = array_merge( $templates, $this->templates );
  78. // Add the modified cache to allow WordPress to pick it up for listing
  79. // available templates
  80. wp_cache_add( $cache_key, $templates, 'themes', 1800 );
  81. return $atts;
  82. }
  83. /**
  84. * Checks if the template is assigned to the page
  85. */
  86. public function view_project_template( $template ) {
  87. global $post;
  88. if(!isset($post->ID)) return $template;
  89. if (!isset($this->templates[get_post_meta(
  90. $post->ID, '_wp_page_template', true
  91. )] ) ) {
  92. return $template;
  93. }
  94. $file = plugin_dir_path(__FILE__). get_post_meta(
  95. $post->ID, '_wp_page_template', true
  96. );
  97. // Just to be safe, we check if the file exist first
  98. if( file_exists( $file ) ) {
  99. return $file;
  100. }
  101. else { echo $file; }
  102. return $template;
  103. }
  104. }
  105. ?>