Last active
March 26, 2017 22:49
-
-
Save atwellpub/2839686d87c1a3d5a970cb5d78f73219 to your computer and use it in GitHub Desktop.
This file extends the Search and Replace Plugins's tag filters into the WordPress Jupiter theme's 'View More Posts' button. This file is located in /wp-content/themes/jupiter/framework/functions/search-and-filter-plugin.php and is included by, as of 2/26/2017, the main Jupiter functions.php file around line 238.
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 Codeable_Modifications_Search_And_Filter_Support { | |
/** | |
* Initialize Class | |
*/ | |
public function __construct() { | |
//add_action('after_setup_theme' , array( __CLASS__ , 'remove_hooks' )); | |
self::remove_hooks(); | |
self::replace_hooks(); | |
} | |
/** | |
* Removes default Jupiter AJAX functions for loading more posts | |
*/ | |
public static function remove_hooks() { | |
global $wp_filter; | |
unset($wp_filter['wp_ajax_nopriv_mk_load_more'] ); | |
unset($wp_filter['wp_ajax_mk_load_more'] ); | |
} | |
/** | |
* Replaces Jupiter AJAX functions for loading more posts | |
*/ | |
public static function replace_hooks() { | |
add_action('wp_ajax_nopriv_mk_load_more', array( __CLASS__ , | |
'get_loop' | |
)); | |
add_action('wp_ajax_mk_load_more', array( __CLASS__ , | |
'get_loop' | |
)); | |
} | |
/** | |
* Replacement function for Jupiter's Mk_Load_More::get_loop | |
*/ | |
public function get_loop() { | |
$content = ''; | |
check_ajax_referer('mk-load-more', 'safe_load_more'); | |
if(method_exists('WPBMap', 'addAllMappedShortcodes')) { | |
WPBMap::addAllMappedShortcodes(); | |
} | |
$referrer = isset($_REQUEST['_wp_http_referer']) ? $_REQUEST['_wp_http_referer'] : false; | |
$query = isset($_REQUEST['query']) ? json_decode(base64_decode($_REQUEST['query']), true) : false; | |
$atts = isset($_REQUEST['atts']) ? json_decode(base64_decode($_REQUEST['atts']), true) : false; | |
$loop_iterator = isset($_REQUEST['loop_iterator']) ? $_REQUEST['loop_iterator'] : 0; | |
/* find search and filter conditions */ | |
$referrer_parts = explode('?' , $referrer); | |
$sf_query_part = (isset($referrer_parts[1])) ? $referrer_parts[1] : ''; | |
parse_str($sf_query_part , $sf_query ); | |
if (is_array($sf_query)) { | |
foreach($sf_query as $k => $q) { | |
$sf_query[str_replace('_sft_' , '' , $k )] = $q; | |
} | |
$query['tag'] = $sf_query['post_tag']; | |
} | |
if(isset($_REQUEST['term'])) { | |
$query['categories'] = !empty($_REQUEST['term']) ? $_REQUEST['term'] : false; | |
} | |
if(isset($_REQUEST['author'])) { | |
$query['author'] = !empty($_REQUEST['author']) ? $_REQUEST['author'] : false; | |
} | |
if(isset($_REQUEST['posts'])) { | |
$query['post__in'] = !empty($_REQUEST['posts']) ? explode(',', $_REQUEST['posts']) : false; | |
} | |
$query['post_status'] = 'publish'; | |
$query['paged'] = isset($_REQUEST['paged']) ? $_REQUEST['paged'] : false; | |
$offset = $query['offset']; | |
$loaded_posts = ! empty( $_REQUEST['loaded_posts'] ) ? $_REQUEST['loaded_posts'] : array(); | |
$query['post__not_in'] = $loaded_posts; | |
$query = mk_wp_query($query); | |
$r = $query['wp_query']; | |
$atts['i'] = $loop_iterator; | |
if ($query && $atts) { | |
if ($r->have_posts()): | |
while ($r->have_posts()): | |
$r->the_post(); | |
$loaded_posts[] = $r->post->ID; | |
$content .= mk_get_shortcode_view($atts['shortcode_name'], 'loop-styles/' . $atts['style'], true, $atts); | |
$atts['i']++; | |
endwhile; | |
endif; | |
} | |
// If there is no posts_in and loaded posts are not empty, return the actual found posts | |
if ( empty( $_REQUEST['posts'] ) && ! empty( $_REQUEST['loaded_posts'] ) ) { | |
$found_posts = $r->found_posts - $r->post_count - $offset; | |
} | |
echo json_encode(array( | |
'i' => $atts['i'], | |
'maxPages' => $r->max_num_pages, | |
'loaded_posts' => $loaded_posts, | |
'found_posts' => $found_posts, | |
'content' => $content | |
)); | |
wp_die(); | |
} | |
/** | |
* We ended up not having to use this | |
* | |
*/ | |
public static function mk_wp_query_replacement($atts , $sf_query) { | |
extract($atts); | |
$count = isset($count) ? $count : 10; | |
$query = array( | |
'post_type' => $post_type, | |
'posts_per_page' => (int)$count, | |
'suppress_filters' => 0, | |
); | |
/* adds post tags */ | |
if ( isset($sf_query['post_tag']) && $sf_query['post_tag'] ) { | |
$tags = explode('+' , $sf_query['post_tag'] ); | |
$query['tag'] = $sf_query['post_tag']; | |
} | |
if ($post_type == 'attachment') { | |
$query['post_mime_type'] = 'image'; | |
$query['post_status'] = 'inherit'; | |
} | |
if (isset($post_status) && !empty($post_status) && $post_type != 'attachment') { | |
$query['post_status'] = $post_status; | |
} | |
if (isset($cat) && !empty($cat) && $post_type == 'post') { | |
$query['cat'] = $cat; | |
} | |
if (isset($category_name) && !empty($category_name) && $post_type == 'post') { | |
$query['category_name'] = $category_name; | |
} | |
if (isset($categories) && !empty($categories) && $post_type != 'post') { | |
$query['tax_query'] = array( | |
array( | |
'taxonomy' => $post_type . '_category', | |
'field' => 'slug', | |
'terms' => explode(',', $categories) | |
) | |
); | |
} | |
if ( isset($taxonomy_name) && !empty($taxonomy_name) ) { | |
$query['tax_query'] = array( | |
array( | |
'taxonomy' => $taxonomy_name, | |
'field' => 'slug', | |
'terms' => $term_slug | |
) | |
); | |
} | |
// Adds exclude option for blog loops post format | |
if(!empty($exclude_post_format)) { | |
$query['meta_query'] = array( | |
array( | |
'key' => '_single_post_type', | |
'value' => explode(',',$exclude_post_format), | |
'compare' => 'NOT IN' | |
) , | |
); | |
} | |
if (isset($author) && !empty($author)) { | |
$query['author'] = $author; | |
} | |
if (isset($author_name) && !empty($author_name)) { | |
$query['author_name'] = $author_name; | |
} | |
if (isset($posts) && !empty($posts)) { | |
$query['post__in'] = explode(',', $posts); | |
} | |
if (isset($orderby) && !empty($orderby)) { | |
$query['orderby'] = $orderby; | |
} | |
if (isset($order) && !empty($order)) { | |
$query['order'] = $order; | |
} | |
if (isset($year) && !empty($year)) { | |
$query['year'] = $year; | |
} | |
if (isset($monthnum) && !empty($monthnum)) { | |
$query['monthnum'] = $monthnum; | |
} | |
if (isset($m) && !empty($m)) { | |
$query['m'] = $m; | |
} | |
if (isset($second) && !empty($second)) { | |
$query['second'] = $second; | |
} | |
if (isset($minute) && !empty($minute)) { | |
$query['minute'] = $minute; | |
} | |
if (isset($hour) && !empty($hour)) { | |
$query['hour'] = $hour; | |
} | |
if (isset($w) && !empty($w)) { | |
$query['w'] = $w; | |
} | |
if (isset($day) && !empty($day)) { | |
$query['day'] = $day; | |
} | |
if (isset($tag) && !empty($tag)) { | |
$query['tag'] = $tag; | |
} | |
if(isset($paged) && !empty($paged)) { | |
$query['paged'] = $paged; | |
} else { | |
$paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1); | |
$query['paged'] = $paged; | |
} | |
if ($paged == 1) { | |
if (isset($offset) && !empty($offset)) { | |
$query['offset'] = $offset; | |
} | |
} | |
else { | |
if (isset($offset) && !empty($offset)) { | |
if ( ! isset( $post__not_in ) && empty( $post__not_in ) ) { | |
$offset = $offset + (($paged - 1) * $count); | |
} | |
$query['offset'] = $offset; | |
} | |
} | |
// When specific posts are selected from the shortcode settings, | |
// It's not possible to set post__not_in | |
if ( empty( $posts ) ) { | |
if ( isset( $post__not_in ) && ! empty( $post__not_in ) ) { | |
// Set the paged to 1, otherwise we can't get all the availabe posts. | |
if ( isset( $paged ) && ! empty( $paged ) ) { | |
$query['paged'] = 1; | |
} | |
$query['post__not_in'] = $post__not_in; | |
} | |
} | |
return array( | |
'wp_query' => new WP_Query($query) , | |
'paged' => $paged | |
); | |
} | |
} | |
/** | |
* Load Class | |
*/ | |
add_action('init' , 'codeable_load_search_filter_class' , 11 ); | |
function codeable_load_search_filter_class() { | |
new Codeable_Modifications_Search_And_Filter_Support; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment