Last active
May 2, 2017 10:07
-
-
Save robertuniqid/5a737ac0279023b5286dd37bd309f70e to your computer and use it in GitHub Desktop.
Easily attach filters in WordPress, then remove them.
This file contains hidden or 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 | |
// This is an PHP Trait http://php.net/manual/ro/language.oop5.traits.php | |
trait FilterProxy { | |
protected $_filter_proxy_storage = []; | |
public function _filter_proxy_setup( $information ) { | |
if( empty( $information ) ) | |
return; | |
if( !empty( $this->_filter_proxy_storage ) ) | |
$this->_filter_proxy_remove(); | |
$this->_filter_proxy_storage = $information; | |
foreach( $this->_filter_proxy_storage as $filter_name => $filter_function ) | |
add_filter( $filter_name, [ $this, '_filter_proxy_callback' ], 20 ); | |
} | |
public function _filter_proxy_remove() { | |
if( empty( $this->_filter_proxy_storage ) ) | |
return; | |
foreach( $this->_filter_proxy_storage as $filter_name => $filter_function ) | |
remove_filter( $filter_name, [ $this, '_filter_proxy_callback' ], 20 ); | |
$this->_filter_proxy_storage = []; | |
} | |
public function _filter_proxy_callback( $current_value ) { | |
$current_filter = current_filter(); | |
if( !isset( $this->_filter_proxy_storage[$current_filter] ) ) | |
return $current_value; | |
if( !is_callable( $this->_filter_proxy_storage[$current_filter] ) ) | |
return $this->_filter_proxy_storage[$current_filter]; | |
return call_user_func_array( $this->_filter_proxy_storage[$current_filter], func_get_args() ); | |
} | |
} |
This file contains hidden or 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 Course { | |
use FilterProxy; | |
public function __construct() { | |
add_shortcode( 'WPEP_index', [ $this, 'primary' ] ); | |
} | |
public function primary($attributes, $content = null) { | |
if( !is_user_logged_in() && in_array( 'index-page-shortcode', wpep_get_auth_status_page_restrictions() ) ) | |
return WPEP_Controller::instance()->shortCode->profile->authentication( $attributes ); | |
WPEP_Controller::instance()->templateStyle->match_request_layout_options( $attributes ); | |
$post_id = isset($attributes['post_id']) ? intval( $attributes['post_id'] ) : false; | |
$include_header = isset($attributes['include_header']) ? intval( $attributes['include_header'] ) : false; | |
$include_footer = isset($attributes['include_footer']) ? intval( $attributes['include_footer'] ) : false; | |
$this->_filter_proxy_setup( $this->_primary_filters( $attributes ) ); | |
$response = WPEP_Controller::instance()->template->get_template( | |
'primary.php', [ | |
'post_id' => $post_id, | |
'include_header' => $include_header, | |
'include_footer' => $include_footer | |
] | |
); | |
$this->_filter_proxy_remove(); | |
return $response; | |
} | |
private function _primary_filters( $attributes ) { | |
$filter_proxy_list = []; | |
$category_ids = isset($attributes['categories']) ? $attributes['categories'] : ''; | |
$categories_per_row = isset($attributes['categories_per_row']) ? intval( $attributes['categories_per_row'] ) : ''; | |
$course_progress_bars = isset($attributes['course_progress_bars']) ? intval( $attributes['course_progress_bars'] ) : ''; | |
$courses_per_row = isset($attributes['courses_per_row']) ? intval( $attributes['courses_per_row'] ) : ''; | |
$videos_per_row = isset($attributes['videos_per_row']) ? intval( $attributes['videos_per_row'] ) : ''; | |
$ebooks_per_row = isset($attributes['ebooks_per_row']) ? intval( $attributes['ebooks_per_row'] ) : ''; | |
$offers_per_row = isset($attributes['offers_per_row']) ? intval( $attributes['offers_per_row'] ) : ''; | |
$category_navigation = isset($attributes['category_navigation']) ? intval( $attributes['category_navigation'] ) : ''; | |
$display_header = isset($attributes['display_header']) ? $attributes['display_header'] : null; | |
$course_list_ids = isset($attributes['courses']) ? $attributes['courses'] : ''; | |
if( $category_ids !== '' ) | |
$filter_proxy_list['wpep_primary_content_category_list_args'] = function( $args ) use ( $category_ids ) { | |
$args['include'] = $category_ids; | |
return $args; | |
}; | |
if( $categories_per_row != '' ) | |
$filter_proxy_list['wpep_template_category_list_items_per_row'] = function ($current_per_row) use ($categories_per_row) { | |
return ($categories_per_row == 0 ? $current_per_row : $categories_per_row); | |
}; | |
if( $course_progress_bars != '' ) | |
$filter_proxy_list['wpep_primary_content_grid_courses_progress_bars'] = function( $current_per_row ) use ( $course_progress_bars ) { | |
return $course_progress_bars; | |
}; | |
if( $courses_per_row !== '' ) | |
$filter_proxy_list['wpep_primary_content_grid_courses_per_row'] = function( $current_per_row ) use ( $courses_per_row ) { | |
return $courses_per_row; | |
}; | |
if( $course_list_ids !== '' ) | |
$filter_proxy_list['wpep_primary_content_course_list_args'] = function( $args ) use ( $course_list_ids ) { | |
$args['include'] = $course_list_ids; | |
return $args; | |
}; | |
if( $videos_per_row !== '' ) | |
$filter_proxy_list['wpep_primary_content_grid_videos_per_row'] = function( $current_per_row ) use ( $videos_per_row ) { | |
return $videos_per_row; | |
}; | |
if( $ebooks_per_row !== '' ) | |
$filter_proxy_list['wpep_primary_content_grid_ebooks_per_row'] = function( $current_per_row ) use ( $ebooks_per_row ) { | |
return $ebooks_per_row; | |
}; | |
if( $offers_per_row !== '' ) | |
$filter_proxy_list['wpep_primary_content_grid_offers_per_row'] = function( $current_per_row ) use ( $offers_per_row ) { | |
return $offers_per_row; | |
}; | |
if( $category_navigation !== '' ) | |
$filter_proxy_list['wpep_primary_content_category_navigation_enabled'] = function( $current_category_navigation ) use ( $category_navigation ) { | |
return $category_navigation; | |
}; | |
if( $display_header !== null ) | |
$filter_proxy_list['wpep_primary_content_display_header'] = function( $internal_display_header ) use ( $display_header ) { | |
return $display_header; | |
}; | |
return $filter_proxy_list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment