Created
June 7, 2012 17:17
-
-
Save scribu/2890140 to your computer and use it in GitHub Desktop.
APP_View_Generic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Helper class for using the generic.php template from a plugin. | |
* | |
* Optional methods: | |
* - init() - for registering post types, taxonomies, rewrite rules etc. | |
* - before_load_template() - for processing forms, enqueueing scripts etc | |
*/ | |
abstract class APP_View_Generic { | |
/** | |
* Test if this controller should handle the current page. | |
* | |
* Use is_*() conditional tags and get_query_var() | |
* | |
* @return bool | |
*/ | |
abstract function condition(); | |
/** | |
* Use this to actually display something within generic.php | |
* | |
* Can be another call to locate_template() | |
*/ | |
abstract function load_template(); | |
function __construct() { | |
if ( method_exists( $this, 'init' ) ) | |
add_action( 'init', array( $this, 'init' ) ); | |
add_action( 'template_redirect', array( $this, '_template_redirect' ) ); | |
} | |
final function _template_redirect() { | |
if ( !$this->condition() ) | |
return; | |
if ( method_exists( $this, 'before_load_template' ) ) | |
$this->before_load_template(); | |
add_action( 'generic_content', array( $this, 'load_template' ) ); | |
locate_template( 'generic.php', true ); | |
die; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class My_Generic_View extends APP_View_Generic { | |
function init() { | |
global $wp; | |
$wp->add_query_var( 'my_plugin_query_var' ); | |
} | |
function condition() { | |
return get_query_var( 'my_plugin_query_var' ); | |
} | |
function load_template() { | |
echo '<p>In the generic.php template.</p>'; | |
} | |
} | |
new My_Generic_View; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Context: http://core.trac.wordpress.org/ticket/20509
Similar: https://gist.github.com/2865132