Skip to content

Instantly share code, notes, and snippets.

@scribu
Created June 3, 2012 21:42
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scribu/2865132 to your computer and use it in GitHub Desktop.
Save scribu/2865132 to your computer and use it in GitHub Desktop.
APP_View
/**
* Helper class for controlling all aspects of a view.
*
* Supported methods (automatically hooked):
* - init() - for registering post types, taxonomies, rewrite rules etc.
* - parse_query() - for correcting query flags
* - pre_get_posts() - for altering the query, without affecting the query flags
* - posts_search(), posts_clauses(), posts_request() - for direct SQL manipulation
* - the_posts() - for various other manipulations
* - template_redirect() - for enqueuing scripts etc.
* - template_include( $path ) - for loading a different template file
* - title_parts( $parts ) - for changing the title
* - breadcrumbs( $trail ) - for changing the breadcrumbs
* - notices() - for displaying notices
*/
abstract class APP_View {
/**
* Test if this class should handle the current view.
*
* Use is_*() conditional tags and get_query_var()
*
* @return bool
*/
abstract function condition();
function __construct() {
// 'init' hook (always ran)
if ( method_exists( $this, 'init' ) )
add_action( 'init', array( $this, 'init' ) );
// $wp_query hooks
$actions = array( 'parse_query', 'pre_get_posts' );
$filters = array( 'posts_search', 'posts_clauses', 'posts_request', 'the_posts' );
foreach ( $actions as $method ) {
if ( method_exists( $this, $method ) )
add_action( $method, array( $this, '_action' ) );
}
foreach ( $filters as $method ) {
if ( method_exists( $this, $method ) )
add_filter( $method, array( $this, '_filter' ), 10, 2 );
}
// other hooks
add_action( 'template_redirect', array( $this, '_template_redirect' ) );
}
final function _action( $wp_query ) {
if ( $wp_query->is_main_query() && $this->condition() ) {
$method = current_filter();
// debug( get_class( $this ) . '->' . $method . '()' );
$this->$method( $wp_query );
}
}
final function _filter( $value, $wp_query ) {
if ( $wp_query->is_main_query() && $this->condition() ) {
$method = current_filter();
// debug( get_class( $this ) . '->' . $method . '()' );
$value = $this->$method( $value, $wp_query );
}
return $value;
}
final function _template_redirect() {
if ( !$this->condition() )
return;
if ( method_exists( $this, 'template_redirect' ) )
$this->template_redirect();
$filters = array(
'template_include' => 'template_include',
'appthemes_title_parts' => 'title_parts',
'appthemes_notices' => 'notices',
'breadcrumb_trail_items' => 'breadcrumbs',
);
foreach ( $filters as $filter => $method ) {
if ( method_exists( $this, $method ) )
add_filter( $filter, array( $this, $method ) );
}
}
}
@scribu
Copy link
Author

scribu commented Jun 5, 2012

Yes, and what I'm saying is that you should change them directly in the class; and also change the APP_ prefix while you're at it. :)

@rilwis
Copy link

rilwis commented Jun 5, 2012

Oh, got it. I'm too greedy when I want a class-for-all ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment