Advertisement
bcworkz

PageTemplater

Sep 15th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.43 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: PageTemplater
  4. Description: Allows page templates in plugin/templates/ folder to appear and be used on page edit screen. Modified by bcworkz to work on quick edit form.
  5. Author: Harri Bell-Thomas
  6. Version: 0.50 beta
  7. License: GPL
  8. */
  9.  
  10. class PageTemplater {
  11.  
  12.         /**
  13.          * A Unique Identifier
  14.          */
  15.          protected $plugin_slug;
  16.  
  17.         /**
  18.          * A reference to an instance of this class.
  19.          */
  20.         private static $instance;
  21.  
  22.         /**
  23.          * The array of templates that this plugin tracks.
  24.          */
  25.         protected $templates;
  26.  
  27.         /**
  28.          * Returns an instance of this class.
  29.          */
  30.         public static function get_instance() {
  31.  
  32.                 if( null == self::$instance ) {
  33.                         self::$instance = new PageTemplater();
  34.                 }
  35.  
  36.                 return self::$instance;
  37.  
  38.         }
  39.  
  40.         /**
  41.          * Initializes the plugin by setting filters and administration functions.
  42.          */
  43.         private function __construct() {
  44.  
  45.                 $this->templates = array();
  46.  
  47.                 // Add a filter to the attributes metabox to inject template into the cache.
  48.                 // --Does nothing for page attributes, this filter fires just before the cache is called
  49.                 //   by page_template_dropdown()
  50.                 add_filter(
  51.                     //'page_attributes_dropdown_pages_args',
  52.                     // 'wp_dropdown_pages' fires for both quick edit and edit pages, the attributes
  53.                     //      filter only fires for edit pages -- bcworkz
  54.                     'wp_dropdown_pages',
  55.                      array( $this, 'register_project_templates' )
  56.                 );
  57.  
  58.                 // Add a filter to the save post to inject out template into the page cache
  59.                 add_filter(
  60.                     'wp_insert_post_data',
  61.                     array( $this, 'register_project_templates' )
  62.                 );
  63.  
  64.                 // Add a filter to the template include to determine if the page has our
  65.                 // template assigned and return it's path
  66.                 // --Loads template when page dependent on it is displayed
  67.                 add_filter(
  68.                     'template_include',
  69.                     array( $this, 'view_project_template')
  70.                 );
  71.  
  72.                 // Add your templates to this array.
  73.                 $this->templates = array(
  74.             'mytemplate-test.php'     => 'Injected Template',
  75.         );
  76. }
  77.  
  78.         /**
  79.          * Adds our template to the pages cache in order to trick WordPress
  80.          * into thinking the template file exists where it doens't really exist.
  81.          *
  82.          */
  83.  
  84.         public function register_project_templates( $atts ) {
  85.  
  86.                 // Create the key used for the themes cache
  87.                 $cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
  88.  
  89.                 // Retrieve the cache list.
  90.                 // If it doesn't exist, or it's empty prepare an array
  91.                 $templates = wp_get_theme()->get_page_templates();
  92.                 if ( empty( $templates ) ) {
  93.                         $templates = array();
  94.                 }
  95.  
  96.                 // New cache, therefore remove the old one
  97.                 wp_cache_delete( $cache_key , 'themes');
  98.  
  99.                 // Now add our template to the list of templates by merging our templates
  100.                 // with the existing templates array from the cache.
  101.                 $templates = array_merge( $templates, $this->templates );
  102.  
  103.                 // Add the modified cache to allow WordPress to pick it up for listing
  104.                 // available templates
  105.                 wp_cache_add( $cache_key, $templates, 'themes', 1800 );
  106.  
  107.                 return $atts;
  108.  
  109.         }
  110.  
  111.         /**
  112.          * Checks if the template is assigned to the page
  113.          */
  114.         public function view_project_template( $template ) {
  115.  
  116.                 global $post;
  117.  
  118.                 if (!isset($this->templates[get_post_meta(
  119.                     $post->ID, '_wp_page_template', true
  120.                 )] ) ) {
  121.  
  122.                         return $template;
  123.  
  124.                 }
  125.  
  126.                 $file = plugin_dir_path(__FILE__). 'templates/' .get_post_meta(
  127.                     $post->ID, '_wp_page_template', true
  128.                 );
  129.  
  130.                 // Just to be safe, we check if the file exist first
  131.                 if( file_exists( $file ) ) {
  132.                         return $file;
  133.                 }
  134.                 else { echo $file.' not found!'; }
  135.  
  136.                 return $template;
  137.  
  138.         }
  139.  
  140. }
  141.  
  142. add_action( 'plugins_loaded', array( 'PageTemplater', 'get_instance' ) );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement