public
Created

APP_View_Generic

  • Download Gist
class-app-view-generic.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?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;
}
}
generic-view-example.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?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;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.