Converted indentation into spaces
This commit is contained in:
parent
8868724ac1
commit
9052f6d94b
|
@ -1,7 +1,7 @@
|
|||
(function (){
|
||||
|
||||
var queryParams = null;
|
||||
var container = document.querySelector('.js-container-async');
|
||||
var container = document.querySelector('.js-container-async');
|
||||
if (container) {
|
||||
var filterTogglers = container.getElementsByClassName('js-toggle-filters');
|
||||
var content = container.querySelector('.ajax-posts__posts');
|
||||
|
@ -62,21 +62,21 @@
|
|||
*
|
||||
* @param NodeElement filter Clicked filter
|
||||
*/
|
||||
function handleFilterEvent(filter) {
|
||||
function handleFilterEvent(filter) {
|
||||
|
||||
if (!filter.classList.contains('is-active')) {
|
||||
filter.classList.add('is-active');
|
||||
updateQueryParams({
|
||||
page: 1,
|
||||
filter: filter.dataset.filter,
|
||||
term: filter.dataset.term,
|
||||
});
|
||||
} else {
|
||||
filter.classList.remove('is-active');
|
||||
removeQueryParam(filter.dataset.filter, filter.dataset.term);
|
||||
}
|
||||
getAJAXPosts({reset: true});
|
||||
}
|
||||
if (!filter.classList.contains('is-active')) {
|
||||
filter.classList.add('is-active');
|
||||
updateQueryParams({
|
||||
page: 1,
|
||||
filter: filter.dataset.filter,
|
||||
term: filter.dataset.term,
|
||||
});
|
||||
} else {
|
||||
filter.classList.remove('is-active');
|
||||
removeQueryParam(filter.dataset.filter, filter.dataset.term);
|
||||
}
|
||||
getAJAXPosts({reset: true});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next page of posts
|
||||
|
@ -119,32 +119,32 @@
|
|||
*
|
||||
* @param Array params params that will be changed
|
||||
*/
|
||||
function updateQueryParams(params) {
|
||||
queryParams.page = params.page;
|
||||
function updateQueryParams(params) {
|
||||
queryParams.page = params.page;
|
||||
|
||||
// If we're also updating the taxonomy
|
||||
if (params.filter) {
|
||||
if (queryParams.tax.hasOwnProperty(params.filter)) {
|
||||
queryParams.tax[params.filter].push(params.term);
|
||||
} else {
|
||||
queryParams.tax[params.filter] = [params.term];
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we're also updating the taxonomy
|
||||
if (params.filter) {
|
||||
if (queryParams.tax.hasOwnProperty(params.filter)) {
|
||||
queryParams.tax[params.filter].push(params.term);
|
||||
} else {
|
||||
queryParams.tax[params.filter] = [params.term];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a term from the set of query params
|
||||
*
|
||||
* @param string tax taxonomy of the term to remove
|
||||
* @param {tring term term to remove
|
||||
*/
|
||||
function removeQueryParam(tax, term) {
|
||||
if (queryParams.tax.hasOwnProperty(tax)) {
|
||||
if (queryParams.tax[tax].indexOf(term) > -1) {
|
||||
queryParams.tax[tax].splice( queryParams.tax[tax].indexOf(term) , 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Remove a term from the set of query params
|
||||
*
|
||||
* @param string tax taxonomy of the term to remove
|
||||
* @param {tring term term to remove
|
||||
*/
|
||||
function removeQueryParam(tax, term) {
|
||||
if (queryParams.tax.hasOwnProperty(tax)) {
|
||||
if (queryParams.tax[tax].indexOf(term) > -1) {
|
||||
queryParams.tax[tax].splice( queryParams.tax[tax].indexOf(term) , 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the Error Reponse div
|
||||
|
@ -161,24 +161,24 @@
|
|||
status.style.display = 'none';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get new posts via Ajax
|
||||
*
|
||||
* Retrieve a new set of posts based on the created query
|
||||
*
|
||||
* @return string server side generated HTML
|
||||
*/
|
||||
function getAJAXPosts(args) {
|
||||
/**
|
||||
* Get new posts via Ajax
|
||||
*
|
||||
* Retrieve a new set of posts based on the created query
|
||||
*
|
||||
* @return string server side generated HTML
|
||||
*/
|
||||
function getAJAXPosts(args) {
|
||||
|
||||
// Set status to querying
|
||||
container.classList.add('is-waiting');
|
||||
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', filterPosts.ajaxUrl, true);
|
||||
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
request.timeout = 4000; // time in milliseconds
|
||||
|
||||
request.onload = function() {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('POST', filterPosts.ajaxUrl, true);
|
||||
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
request.timeout = 4000; // time in milliseconds
|
||||
|
||||
request.onload = function() {
|
||||
|
||||
console.log(this.response);
|
||||
|
||||
|
@ -188,14 +188,14 @@
|
|||
content.removeChild(loadMoreButton.parentNode);
|
||||
}
|
||||
var response = JSON.parse(this.response);
|
||||
if (this.status === 200) {
|
||||
if (this.status === 200) {
|
||||
// If we have a succesfull query
|
||||
if (response.success) {
|
||||
// Hide error status button
|
||||
hideResponseMessage();
|
||||
// If we have to remove the show more button
|
||||
if (args.reset) {
|
||||
content.innerHTML = response.data.content;
|
||||
content.innerHTML = response.data.content;
|
||||
} else {
|
||||
content.innerHTML += response.data.content;
|
||||
}
|
||||
|
@ -203,41 +203,41 @@
|
|||
status.innerHTML = response.data;
|
||||
showResponseMessage();
|
||||
}
|
||||
} else {
|
||||
status.innerHTML = filterPosts.serverErrorMessage;
|
||||
} else {
|
||||
status.innerHTML = filterPosts.serverErrorMessage;
|
||||
showResponseMessage();
|
||||
}
|
||||
}
|
||||
// Resolve status
|
||||
container.classList.remove('is-waiting');
|
||||
};
|
||||
};
|
||||
|
||||
request.ontimeout = function() {
|
||||
status.innerHTML = filterPosts.timeoutMessage;
|
||||
request.ontimeout = function() {
|
||||
status.innerHTML = filterPosts.timeoutMessage;
|
||||
showResponseMessage();
|
||||
container.classList.remove('is-waiting');
|
||||
}
|
||||
}
|
||||
|
||||
request.send(objectToQueryString({
|
||||
action: 'process_filter_change',
|
||||
nonce: filterPosts.nonce,
|
||||
params: queryParams,
|
||||
}));
|
||||
}
|
||||
request.send(objectToQueryString({
|
||||
action: 'process_filter_change',
|
||||
nonce: filterPosts.nonce,
|
||||
params: queryParams,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for event delegation
|
||||
*
|
||||
* To add event listeners on dynamic content, you can add a listener
|
||||
* on thewrapping container, find the dom-node that triggered
|
||||
* the event and check if that node mach our
|
||||
*
|
||||
* @param NodeElement el wrapping element for the dynamic content
|
||||
* @param string eventName type of event, e.g. click, mouseenter, etc
|
||||
* @param string selector selector criteria of the element where the action should be on
|
||||
* @param Function fn callback funciton
|
||||
* @return Function The callback
|
||||
*/
|
||||
function on(el, eventName, selector, fn) {
|
||||
/**
|
||||
* Helper function for event delegation
|
||||
*
|
||||
* To add event listeners on dynamic content, you can add a listener
|
||||
* on thewrapping container, find the dom-node that triggered
|
||||
* the event and check if that node mach our
|
||||
*
|
||||
* @param NodeElement el wrapping element for the dynamic content
|
||||
* @param string eventName type of event, e.g. click, mouseenter, etc
|
||||
* @param string selector selector criteria of the element where the action should be on
|
||||
* @param Function fn callback funciton
|
||||
* @return Function The callback
|
||||
*/
|
||||
function on(el, eventName, selector, fn) {
|
||||
var element = el;
|
||||
|
||||
element.addEventListener(eventName, function(event) {
|
||||
|
@ -258,23 +258,23 @@
|
|||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an deep object to a url parameter list
|
||||
*
|
||||
* Boiled down from jQuery
|
||||
*
|
||||
* WordPress Ajax post request doesn't accept JSON, only form-urlencoded!
|
||||
* Took me a while to get...
|
||||
/**
|
||||
* Convert an deep object to a url parameter list
|
||||
*
|
||||
* Boiled down from jQuery
|
||||
*
|
||||
* WordPress Ajax post request doesn't accept JSON, only form-urlencoded!
|
||||
* Took me a while to get...
|
||||
*
|
||||
* Although seems not to be totally true:
|
||||
* http://wordpress.stackexchange.com/questions/177554/allowing-admin-ajax-php-to-receive-application-json-instead-of-x-www-form-url
|
||||
* Although seems not to be totally true:
|
||||
* http://wordpress.stackexchange.com/questions/177554/allowing-admin-ajax-php-to-receive-application-json-instead-of-x-www-form-url
|
||||
*
|
||||
* But in this case we just convert the params object to a url encoded string, like our friend and foe jQuery does.
|
||||
*
|
||||
*/
|
||||
function objectToQueryString(a) {
|
||||
*
|
||||
*/
|
||||
function objectToQueryString(a) {
|
||||
var prefix, s, add, name, r20, output;
|
||||
s = [];
|
||||
r20 = /%20/g;
|
||||
|
@ -323,6 +323,6 @@
|
|||
}
|
||||
}
|
||||
|
||||
init();
|
||||
init();
|
||||
|
||||
}());
|
|
@ -26,290 +26,290 @@
|
|||
*/
|
||||
class Ajax_Filter_Posts {
|
||||
|
||||
/**
|
||||
* The unique identifier of this plugin.
|
||||
*
|
||||
* @var string $plugin_name The string used to uniquely identify this plugin.
|
||||
*/
|
||||
protected $plugin_name;
|
||||
/**
|
||||
* The unique identifier of this plugin.
|
||||
*
|
||||
* @var string $plugin_name The string used to uniquely identify this plugin.
|
||||
*/
|
||||
protected $plugin_name;
|
||||
|
||||
/**
|
||||
* The current version of the plugin.
|
||||
*
|
||||
* @var String $version The current version of the plugin.
|
||||
*/
|
||||
protected $version;
|
||||
/**
|
||||
* The current version of the plugin.
|
||||
*
|
||||
* @var String $version The current version of the plugin.
|
||||
*/
|
||||
protected $version;
|
||||
|
||||
|
||||
/**
|
||||
* Define the core functionality of the plugin.
|
||||
*
|
||||
* Set the plugin name and the plugin version that can be used throughout the plugin.
|
||||
* Load the dependencies, define the locale, and set the hooks.
|
||||
*
|
||||
*/
|
||||
public function __construct() {
|
||||
/**
|
||||
* Define the core functionality of the plugin.
|
||||
*
|
||||
* Set the plugin name and the plugin version that can be used throughout the plugin.
|
||||
* Load the dependencies, define the locale, and set the hooks.
|
||||
*
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
$this->plugin_name = 'ajax-filter-posts';
|
||||
$this->version = '0.2.1';
|
||||
$this->plugin_name = 'ajax-filter-posts';
|
||||
$this->version = '0.2.1';
|
||||
|
||||
add_action( 'plugins_loaded', [$this, 'load_textdomain'] );
|
||||
add_action( 'wp_enqueue_scripts', [$this,'add_scripts'] );
|
||||
add_action('wp_ajax_process_filter_change', [$this, 'process_filter_change']);
|
||||
add_action('wp_ajax_nopriv_process_filter_change', [$this, 'process_filter_change']);
|
||||
add_shortcode( 'ajax_filter_posts', [$this, 'create_shortcode']);
|
||||
}
|
||||
add_action( 'plugins_loaded', [$this, 'load_textdomain'] );
|
||||
add_action( 'wp_enqueue_scripts', [$this,'add_scripts'] );
|
||||
add_action('wp_ajax_process_filter_change', [$this, 'process_filter_change']);
|
||||
add_action('wp_ajax_nopriv_process_filter_change', [$this, 'process_filter_change']);
|
||||
add_shortcode( 'ajax_filter_posts', [$this, 'create_shortcode']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the plugins language domain
|
||||
*/
|
||||
public function load_textdomain() {
|
||||
/**
|
||||
* Set the plugins language domain
|
||||
*/
|
||||
public function load_textdomain() {
|
||||
load_muplugin_textdomain( 'ajax-filter-posts', basename( dirname( __FILE__ )) . '/languages' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the required assets for this plugin.
|
||||
*
|
||||
*/
|
||||
public function add_scripts() {
|
||||
/**
|
||||
* Load the required assets for this plugin.
|
||||
*
|
||||
*/
|
||||
public function add_scripts() {
|
||||
|
||||
$script_variables = [
|
||||
$script_variables = [
|
||||
'nonce' => wp_create_nonce( 'filter-posts-nonce' ),
|
||||
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
|
||||
'timeoutMessage' => __('It took to long the get the posts. Please reload the page and try again.', 'ajax-filter-posts'),
|
||||
'serverErrorMessage' => __('Got no response. Please reload the page and try again.', 'ajax-filter-posts'),
|
||||
];
|
||||
|
||||
// IF WPML is installed add language variable to set variable later during the query
|
||||
// WPML can't figure out which language to query, when posts are loaded via AJAX.
|
||||
if (ICL_LANGUAGE_CODE) {
|
||||
$script_variables['language'] = ICL_LANGUAGE_CODE;
|
||||
}
|
||||
// IF WPML is installed add language variable to set variable later during the query
|
||||
// WPML can't figure out which language to query, when posts are loaded via AJAX.
|
||||
if (ICL_LANGUAGE_CODE) {
|
||||
$script_variables['language'] = ICL_LANGUAGE_CODE;
|
||||
}
|
||||
|
||||
wp_enqueue_script( 'ajax-filter', plugins_url('/assets/js/ajax-filter-posts.js', __FILE__), [], '', true );
|
||||
wp_enqueue_style( 'ajax-filter', plugins_url('/assets/css/ajax-filter-posts.css', __FILE__), []);
|
||||
wp_localize_script( 'ajax-filter', 'filterPosts', $script_variables);
|
||||
}
|
||||
wp_enqueue_script( 'ajax-filter', plugins_url('/assets/js/ajax-filter-posts.js', __FILE__), [], '', true );
|
||||
wp_enqueue_style( 'ajax-filter', plugins_url('/assets/css/ajax-filter-posts.css', __FILE__), []);
|
||||
wp_localize_script( 'ajax-filter', 'filterPosts', $script_variables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create shortcode
|
||||
*
|
||||
* @param Array $atts Array of given attributes
|
||||
* @return String HTML initial rendered by shortcode
|
||||
*/
|
||||
public function create_shortcode($atts) {
|
||||
/**
|
||||
* Create shortcode
|
||||
*
|
||||
* @param Array $atts Array of given attributes
|
||||
* @return String HTML initial rendered by shortcode
|
||||
*/
|
||||
public function create_shortcode($atts) {
|
||||
|
||||
$attributes = shortcode_atts( array(
|
||||
'post_type'=> 'post',
|
||||
$attributes = shortcode_atts( array(
|
||||
'post_type'=> 'post',
|
||||
'tax' => ['post_tag'],
|
||||
'posts_per_page' => 12, // How many posts per page,
|
||||
), $atts, $this->plugin_name );
|
||||
|
||||
$filterlists = $this->get_filterlist($attributes['tax']);
|
||||
$filterlists = $this->get_filterlist($attributes['tax']);
|
||||
|
||||
$query = new WP_Query([
|
||||
'post_type' => $attributes['post_type'],
|
||||
'posts_per_page' => $attributes['posts_per_page'],
|
||||
'post_type' => $attributes['post_type'],
|
||||
'posts_per_page' => $attributes['posts_per_page'],
|
||||
]);
|
||||
|
||||
$plural_post_name = strtolower(get_post_type_object($query->query['post_type'])->labels->name);
|
||||
$plural_post_name = strtolower(get_post_type_object($query->query['post_type'])->labels->name);
|
||||
|
||||
ob_start();
|
||||
include( $this->get_local_template('base.php') );
|
||||
ob_start();
|
||||
include( $this->get_local_template('base.php') );
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of filters and terms, based on the taxonomies set in the shortcode
|
||||
*
|
||||
* @param String $taxonomies Comma seperated list of taxonomies
|
||||
* @return Array List of taxonomies with terms
|
||||
*/
|
||||
protected function get_filterlist($taxonomies) {
|
||||
$filterlists = explode(',', $taxonomies);
|
||||
$filterlists = array_map('trim', $filterlists);
|
||||
$filterlists = array_filter($filterlists, 'taxonomy_exists');
|
||||
$filterlists = $this->get_termlist($filterlists);
|
||||
return $filterlists;
|
||||
}
|
||||
/**
|
||||
* Get a list of filters and terms, based on the taxonomies set in the shortcode
|
||||
*
|
||||
* @param String $taxonomies Comma seperated list of taxonomies
|
||||
* @return Array List of taxonomies with terms
|
||||
*/
|
||||
protected function get_filterlist($taxonomies) {
|
||||
$filterlists = explode(',', $taxonomies);
|
||||
$filterlists = array_map('trim', $filterlists);
|
||||
$filterlists = array_filter($filterlists, 'taxonomy_exists');
|
||||
$filterlists = $this->get_termlist($filterlists);
|
||||
return $filterlists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of filters and terms
|
||||
*
|
||||
* @param string $taxonomies A single taxonomy
|
||||
* @return Array Taxonomy name and list of terms associated with the taxonomy
|
||||
*/
|
||||
protected function get_termlist($taxonomies) {
|
||||
$list = [];
|
||||
/**
|
||||
* Get a list of filters and terms
|
||||
*
|
||||
* @param string $taxonomies A single taxonomy
|
||||
* @return Array Taxonomy name and list of terms associated with the taxonomy
|
||||
*/
|
||||
protected function get_termlist($taxonomies) {
|
||||
$list = [];
|
||||
|
||||
foreach ($taxonomies as $taxonomy) {
|
||||
$terms = get_terms($taxonomy);
|
||||
if (!empty($terms)) {
|
||||
$list[] = [
|
||||
'name' => get_taxonomy($taxonomy)->labels->singular_name,
|
||||
'filters' => $terms,
|
||||
];
|
||||
}
|
||||
}
|
||||
foreach ($taxonomies as $taxonomy) {
|
||||
$terms = get_terms($taxonomy);
|
||||
if (!empty($terms)) {
|
||||
$list[] = [
|
||||
'name' => get_taxonomy($taxonomy)->labels->singular_name,
|
||||
'filters' => $terms,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send new posts query via AJAX after filters are changed in the frontend
|
||||
*
|
||||
* @return String HTML string with parsed posts or an error message
|
||||
*/
|
||||
public function process_filter_change() {
|
||||
/**
|
||||
* Send new posts query via AJAX after filters are changed in the frontend
|
||||
*
|
||||
* @return String HTML string with parsed posts or an error message
|
||||
*/
|
||||
public function process_filter_change() {
|
||||
|
||||
check_ajax_referer( 'filter-posts-nonce', 'nonce' );
|
||||
|
||||
$post_type = sanitize_text_field($_POST['params']['postType']);
|
||||
$tax = $this->get_tax_query_vars($_POST['params']['tax']);
|
||||
$page = intval($_POST['params']['page']);
|
||||
$quantity = intval($_POST['params']['quantity']);
|
||||
$language = sanitize_text_field($_POST['params']['language']);
|
||||
check_ajax_referer( 'filter-posts-nonce', 'nonce' );
|
||||
|
||||
$post_type = sanitize_text_field($_POST['params']['postType']);
|
||||
$tax = $this->get_tax_query_vars($_POST['params']['tax']);
|
||||
$page = intval($_POST['params']['page']);
|
||||
$quantity = intval($_POST['params']['quantity']);
|
||||
$language = sanitize_text_field($_POST['params']['language']);
|
||||
|
||||
$args = [
|
||||
'paged' => $page,
|
||||
'post_type' => $post_type,
|
||||
'posts_per_page' => $quantity,
|
||||
'tax_query' => $tax
|
||||
];
|
||||
|
||||
$response = $this->get_filter_posts($args, $language);
|
||||
|
||||
if ($response) {
|
||||
wp_send_json_success($response);
|
||||
} else {
|
||||
wp_send_json_error(__('Oops, something went wrong. Please reload the page and try again.', 'ajax-filter-posts'));
|
||||
}
|
||||
die();
|
||||
}
|
||||
$args = [
|
||||
'paged' => $page,
|
||||
'post_type' => $post_type,
|
||||
'posts_per_page' => $quantity,
|
||||
'tax_query' => $tax
|
||||
];
|
||||
|
||||
$response = $this->get_filter_posts($args, $language);
|
||||
|
||||
if ($response) {
|
||||
wp_send_json_success($response);
|
||||
} else {
|
||||
wp_send_json_error(__('Oops, something went wrong. Please reload the page and try again.', 'ajax-filter-posts'));
|
||||
}
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the queried page number to a real page number
|
||||
*
|
||||
* @param Object $query WP Query
|
||||
* @return Integer Current page
|
||||
*/
|
||||
private function get_page_number($query){
|
||||
$query_page = $query->get( 'paged' );
|
||||
return $query_page == 0 ? 1 : $query_page;
|
||||
}
|
||||
/**
|
||||
* Converts the queried page number to a real page number
|
||||
*
|
||||
* @param Object $query WP Query
|
||||
* @return Integer Current page
|
||||
*/
|
||||
private function get_page_number($query){
|
||||
$query_page = $query->get( 'paged' );
|
||||
return $query_page == 0 ? 1 : $query_page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the queried page is the last page of the query
|
||||
*
|
||||
* @param Object $query WP Query
|
||||
* @return Boolean true if is last page
|
||||
*/
|
||||
private function is_last_page($query) {
|
||||
return $this->get_page_number($query) >= $query->max_num_pages;
|
||||
}
|
||||
/**
|
||||
* Check if the queried page is the last page of the query
|
||||
*
|
||||
* @param Object $query WP Query
|
||||
* @return Boolean true if is last page
|
||||
*/
|
||||
private function is_last_page($query) {
|
||||
return $this->get_page_number($query) >= $query->max_num_pages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query paramaters based on set filters
|
||||
*
|
||||
* @param array $taxonomies list of taxanomies with terms
|
||||
* @return array taxonomies prepared for the WordPress Query
|
||||
* @param array $taxonomies list of taxanomies with terms
|
||||
* @return array taxonomies prepared for the WordPress Query
|
||||
*/
|
||||
protected function get_tax_query_vars($taxonomies) {
|
||||
$tax_query = [];
|
||||
protected function get_tax_query_vars($taxonomies) {
|
||||
$tax_query = [];
|
||||
|
||||
foreach ($taxonomies as $taxonomy => $terms) {
|
||||
$taxonomy = sanitize_text_field($taxonomy);
|
||||
if (taxonomy_exists($taxonomy)) {
|
||||
$valid_terms = $this->get_valid_terms($terms, $taxonomy);
|
||||
if ($valid_terms) {
|
||||
$term_query = [
|
||||
'taxonomy' => $taxonomy,
|
||||
'field' => 'slug',
|
||||
'terms' => $valid_terms,
|
||||
];
|
||||
foreach ($taxonomies as $taxonomy => $terms) {
|
||||
$taxonomy = sanitize_text_field($taxonomy);
|
||||
if (taxonomy_exists($taxonomy)) {
|
||||
$valid_terms = $this->get_valid_terms($terms, $taxonomy);
|
||||
if ($valid_terms) {
|
||||
$term_query = [
|
||||
'taxonomy' => $taxonomy,
|
||||
'field' => 'slug',
|
||||
'terms' => $valid_terms,
|
||||
];
|
||||
|
||||
$tax_query[] = $term_query;
|
||||
$tax_query[] = $term_query;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( count($tax_query) > 1 ) {
|
||||
$tax_query[] = ['relation' => 'OR'];
|
||||
}
|
||||
return $tax_query;
|
||||
}
|
||||
if( count($tax_query) > 1 ) {
|
||||
$tax_query[] = ['relation' => 'OR'];
|
||||
}
|
||||
return $tax_query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check of the given thers are valid terms
|
||||
*
|
||||
* @param array $terms List of terms set by the filters
|
||||
* @param string $tax Taxomy associated with the terms
|
||||
* @return array List of valid terms
|
||||
*/
|
||||
protected function get_valid_terms($terms, $tax) {
|
||||
$valid_terms = [];
|
||||
|
||||
foreach ($terms as $term) {
|
||||
$term = sanitize_text_field($term);
|
||||
if (term_exists($term,$tax)) {
|
||||
$valid_terms[] = $term;
|
||||
}
|
||||
}
|
||||
return $valid_terms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up a filters query and parse the template
|
||||
*
|
||||
* @param array $args Arguments for the WordPress Query
|
||||
* @return string HTMl to be sent via Ajax
|
||||
*/
|
||||
public function get_filter_posts($args, $language) {
|
||||
if (!empty($language)) {
|
||||
global $sitepress;
|
||||
$sitepress->switch_lang( $language );
|
||||
}
|
||||
/**
|
||||
* Check of the given thers are valid terms
|
||||
*
|
||||
* @param array $terms List of terms set by the filters
|
||||
* @param string $tax Taxomy associated with the terms
|
||||
* @return array List of valid terms
|
||||
*/
|
||||
protected function get_valid_terms($terms, $tax) {
|
||||
$valid_terms = [];
|
||||
|
||||
foreach ($terms as $term) {
|
||||
$term = sanitize_text_field($term);
|
||||
if (term_exists($term,$tax)) {
|
||||
$valid_terms[] = $term;
|
||||
}
|
||||
}
|
||||
return $valid_terms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up a filters query and parse the template
|
||||
*
|
||||
* @param array $args Arguments for the WordPress Query
|
||||
* @return string HTMl to be sent via Ajax
|
||||
*/
|
||||
public function get_filter_posts($args, $language) {
|
||||
if (!empty($language)) {
|
||||
global $sitepress;
|
||||
$sitepress->switch_lang( $language );
|
||||
}
|
||||
|
||||
$query = new WP_Query($args);
|
||||
$response = [];
|
||||
|
||||
ob_start();
|
||||
include( $this->get_local_template('partials/loop.php'));
|
||||
$response['content'] = ob_get_clean();
|
||||
$response['found'] = $query->found_posts;
|
||||
return $response;
|
||||
}
|
||||
$query = new WP_Query($args);
|
||||
$response = [];
|
||||
|
||||
ob_start();
|
||||
include( $this->get_local_template('partials/loop.php'));
|
||||
$response['content'] = ob_get_clean();
|
||||
$response['found'] = $query->found_posts;
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate template.
|
||||
*
|
||||
* Locate the called template.
|
||||
* Search Order:
|
||||
* 1. /themes/theme/ajax-posts-filters/$template_name
|
||||
* 2. /plugins/ajax-filter-posts/templates/$template_name.
|
||||
*
|
||||
* @since 0.3.0
|
||||
*
|
||||
* @param string $template_name Template to load.
|
||||
* @return string Path to the template file.
|
||||
*/
|
||||
public function get_local_template($template_name) {
|
||||
/**
|
||||
* Locate template.
|
||||
*
|
||||
* Locate the called template.
|
||||
* Search Order:
|
||||
* 1. /themes/theme/ajax-posts-filters/$template_name
|
||||
* 2. /plugins/ajax-filter-posts/templates/$template_name.
|
||||
*
|
||||
* @since 0.3.0
|
||||
*
|
||||
* @param string $template_name Template to load.
|
||||
* @return string Path to the template file.
|
||||
*/
|
||||
public function get_local_template($template_name) {
|
||||
|
||||
if (empty($template_name)) return false;
|
||||
if (empty($template_name)) return false;
|
||||
|
||||
$template = locate_template('ajax-filter-posts/' . $template_name);
|
||||
$template = locate_template('ajax-filter-posts/' . $template_name);
|
||||
|
||||
// If template not in theme, get plugins template file.
|
||||
if ( !$template ) {
|
||||
$template = plugin_dir_path( __FILE__ ) . 'templates/' . $template_name;
|
||||
}
|
||||
// If template not in theme, get plugins template file.
|
||||
if ( !$template ) {
|
||||
$template = plugin_dir_path( __FILE__ ) . 'templates/' . $template_name;
|
||||
}
|
||||
|
||||
if ( !file_exists( $template ) ) {
|
||||
_doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $template ), '4.6.0' );
|
||||
return;
|
||||
}
|
||||
if ( !file_exists( $template ) ) {
|
||||
_doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $template ), '4.6.0' );
|
||||
return;
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
<?php foreach ($filterlists as $filterlist) : ?>
|
||||
<h3><?= $filterlist['name'] ?></h3>
|
||||
<ul>
|
||||
<?php foreach ($filterlist['filters'] as $filter) : ?>
|
||||
<li>
|
||||
<a href="<?= get_term_link( $filter, $filter->taxonomy ); ?>" class="ajax-posts__filter" data-filter="<?= $filter->taxonomy; ?>" data-term="<?= $filter->slug; ?>">
|
||||
<?= $filter->name; ?>
|
||||
</a>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
<?php foreach ($filterlist['filters'] as $filter) : ?>
|
||||
<li>
|
||||
<a href="<?= get_term_link( $filter, $filter->taxonomy ); ?>" class="ajax-posts__filter" data-filter="<?= $filter->taxonomy; ?>" data-term="<?= $filter->slug; ?>">
|
||||
<?= $filter->name; ?>
|
||||
</a>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php endforeach; ?>
|
|
@ -1,26 +1,26 @@
|
|||
<?php if ( $query->have_posts() ) : ?>
|
||||
<?php while ( $query->have_posts() ): $query->the_post();?>
|
||||
<div class="ajax-posts__post">
|
||||
<article <?php post_class(); ?>>
|
||||
<a href="<?= get_the_permalink(); ?>">
|
||||
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'medium' ); }?>
|
||||
<h3><?php the_title(); ?></h3>
|
||||
</a>
|
||||
</article>
|
||||
</div>
|
||||
<?php endwhile; ?>
|
||||
<?php if (!$this->is_last_page($query)) : ?>
|
||||
<div class="ajax-posts__load-more">
|
||||
<button class="js-load-more" data-page="<?= $this->get_page_number($query) + 1; ?>">
|
||||
<?php _e('Load more', 'ajax-filter-posts') ?>
|
||||
</button>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php while ( $query->have_posts() ): $query->the_post();?>
|
||||
<div class="ajax-posts__post">
|
||||
<article <?php post_class(); ?>>
|
||||
<a href="<?= get_the_permalink(); ?>">
|
||||
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'medium' ); }?>
|
||||
<h3><?php the_title(); ?></h3>
|
||||
</a>
|
||||
</article>
|
||||
</div>
|
||||
<?php endwhile; ?>
|
||||
<?php if (!$this->is_last_page($query)) : ?>
|
||||
<div class="ajax-posts__load-more">
|
||||
<button class="js-load-more" data-page="<?= $this->get_page_number($query) + 1; ?>">
|
||||
<?php _e('Load more', 'ajax-filter-posts') ?>
|
||||
</button>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php else: ?>
|
||||
<div class="ajax-posts-message ajax-posts-message--empty">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="31.95mm" height="41.85mm" viewBox="0 0 90.57 118.62"><defs><style>.a{fill:transparent;}.b,.c,.d,.e,.f,.g{fill:none;stroke:#aaa;stroke-miterlimit:10;}.b,.c,.d,.e,.f{stroke-width:1.45px;}.c{stroke-dasharray:5.41 5.41;}.d{stroke-dasharray:5.86 5.86;}.e{stroke-dasharray:6.41 6.41;}.f{stroke-dasharray:5.75 5.75;}.g{stroke-width:1.48px;stroke-dasharray:5.91;}</style></defs><polygon class="a" points="87.74 117.89 0.73 117.89 0.73 0.73 64.26 0.73 87.74 25.74 87.74 117.89"/><polyline class="b" points="87.74 114.98 87.74 117.89 84.84 117.89"/><line class="c" x1="79.42" y1="117.89" x2="6.34" y2="117.89"/><polyline class="b" points="3.63 117.89 0.73 117.89 0.73 114.98"/><line class="d" x1="0.73" y1="109.12" x2="0.73" y2="6.56"/><polyline class="b" points="0.73 3.63 0.73 0.73 3.63 0.73"/><line class="e" x1="10.05" y1="0.73" x2="58.14" y2="0.73"/><polyline class="b" points="61.35 0.73 64.26 0.73 66.25 2.85"/><polyline class="f" points="70.18 7.03 87.74 25.74 87.74 112.11"/><polyline class="g" points="63.89 0.73 63.89 26.18 87.74 26.18"/>
|
||||
</svg>
|
||||
<h4><?php printf( __('Oh, we couldn\'t find any %s', 'ajax-filter-posts'), $plural_post_name); ?></h4>
|
||||
<p><?php printf( __('Try different filters or <a %s> reset them all.</a>', 'ajax-filter-posts'), 'href="#" class="js-reset-filters"'); ?></p>
|
||||
</div>
|
||||
<div class="ajax-posts-message ajax-posts-message--empty">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="31.95mm" height="41.85mm" viewBox="0 0 90.57 118.62"><defs><style>.a{fill:transparent;}.b,.c,.d,.e,.f,.g{fill:none;stroke:#aaa;stroke-miterlimit:10;}.b,.c,.d,.e,.f{stroke-width:1.45px;}.c{stroke-dasharray:5.41 5.41;}.d{stroke-dasharray:5.86 5.86;}.e{stroke-dasharray:6.41 6.41;}.f{stroke-dasharray:5.75 5.75;}.g{stroke-width:1.48px;stroke-dasharray:5.91;}</style></defs><polygon class="a" points="87.74 117.89 0.73 117.89 0.73 0.73 64.26 0.73 87.74 25.74 87.74 117.89"/><polyline class="b" points="87.74 114.98 87.74 117.89 84.84 117.89"/><line class="c" x1="79.42" y1="117.89" x2="6.34" y2="117.89"/><polyline class="b" points="3.63 117.89 0.73 117.89 0.73 114.98"/><line class="d" x1="0.73" y1="109.12" x2="0.73" y2="6.56"/><polyline class="b" points="0.73 3.63 0.73 0.73 3.63 0.73"/><line class="e" x1="10.05" y1="0.73" x2="58.14" y2="0.73"/><polyline class="b" points="61.35 0.73 64.26 0.73 66.25 2.85"/><polyline class="f" points="70.18 7.03 87.74 25.74 87.74 112.11"/><polyline class="g" points="63.89 0.73 63.89 26.18 87.74 26.18"/>
|
||||
</svg>
|
||||
<h4><?php printf( __('Oh, we couldn\'t find any %s', 'ajax-filter-posts'), $plural_post_name); ?></h4>
|
||||
<p><?php printf( __('Try different filters or <a %s> reset them all.</a>', 'ajax-filter-posts'), 'href="#" class="js-reset-filters"'); ?></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
|
Loading…
Reference in New Issue