Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionalities #237

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
WP Advanced Search
==================

[0.0.1]

##Initial commit

[0.0.2]

##Add
- Added function _the_form_shortcodes()_ to make the form work properly within shortcodes.

###Modified files
* wpas.php

## Add
- Implemented functionality to show the number of results for each search via AJAX. (Needs an element with id wpas-no-of-results).

###Modified files
- lib.php
- js/scripts.js

[0.0.3]

## Add
- Added span to display the number of results for each input.

###Modified files
- src/InputMarkup.php

[0.0.4]

##Add
- Added function to display post count on each taxonomy input.
- Added parameter show_results to display the number of results within each input label

###Modified files
- src/Input.php
- src/InputMarkup.php
- src/Field.php
- lib.php
- wpas.php
- js/scripts.js

[0.0.5]

##Add
- Added filter display, working for taxonomies, search and meta_key fields. Also added functionality to delete each filter and run an updated AJAX request.

##Edit
- Edited display post results within each taxonomy input to include meta fields if exist.
- Edited display post results within each taxonomy input to include additional tax_queries if defined on the search form.

###Modified files
- lib.php
- wpas.php -> Added get_args() function so we could retrieve the args set on the form and include tax_queries if they exist
- js/scripts.js


[0.0.6]

##Add
- Added functionality to make meta queries on several meta keys.
- Added option to include a custom img to close the filters boxes. If none is defined, a svg formatted cross will appear.
- Added option to hide taxonomy elements if count is 0

##Fix
- Fixed some checkboxes unchecking when closing filters. (If there were two or more checkboxes with the same value, they would all uncheck).

###Modified files
- js/scripts.js
- src/Field.php
- src/MetaQuery.php
- src/Form.php
- src/AjaxConfig.php
- src/Input.php
54 changes: 51 additions & 3 deletions js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var __WPAS = {
FORM_ID: "",
KEY_PREFIX: "wpasInstance_",
HASH: "",
NUMBER: "#wpas-no-of-results",
INPUT_COUNT: '#wpas-input-count-',
FILTERS: '#wpas-input-filters',
STORAGE_KEY: function() {
return this.KEY_PREFIX + this.FORM_ID;
}
Expand Down Expand Up @@ -49,6 +52,9 @@ jQuery(document).ready(function($) {
});
});




/**
* AJAX Functionality
*/
Expand Down Expand Up @@ -151,18 +157,59 @@ jQuery(document).ready(function($) {
function sendRequest(data, page) {
ajaxLoader.hideButton();
ajaxLoader.showImage();
jQuery.ajax({
$.ajax({
type: 'POST',
url: WPAS_Ajax.ajaxurl,
data: {
action: 'wpas_ajax_load',
page: page,
form_data: data
},

success: function(data, textStatus, XMLHttpRequest) {
response = JSON.parse(data);
setTimeout(function() {
appendHTML(__WPAS.INNER, response.results);
updateHTML(__WPAS.NUMBER, response.count); //Update the element showing the total number of results.
$.each(response.values, function(id, inputs){
$.each(inputs, function(name, value){
updateHTML(__WPAS.INPUT_COUNT+id+'-'+name, ' (' + value + ')');
if(inputs.hide){
if(value == 0){
$('input[value="'+name+'"][name="'+id+'[]"]').parent().hide();
}
else{
$('input[value="'+name+'"][name="'+id+'[]"]').parent().show();
}
}
else{
if(value == 0){
$('input[value="'+name+'"][name="'+id+'[]"]').parent().addClass('disabled');
$('input[value="'+name+'"][name="'+id+'[]"]').prop('disabled', true);
}
else{
$('input[value="'+name+'"][name="'+id+'[]"]').parent().removeClass('disabled');
$('input[value="'+name+'"][name="'+id+'[]"]').prop('disabled', false);
}
}
});
});

updateHTML(__WPAS.FILTERS, '');
$.each(response.selected, function(id, inputs){
$.each(inputs.selected, function(number, value){
appendHTML(__WPAS.FILTERS, '<div class="filters"><div style="display:inline;" id="'+inputs.id+'_filter_'+number+'">'+inputs.label+': </div>'+$(__WPAS.FORM).data('ajax-close-img-html')+'</div>');
$('#'+inputs.id+'_filter_'+number).siblings().addClass('filters-close').attr('data-value', value.slug ? value.slug : value).attr('data-id', inputs.id); //Add the necessary attributes to the custom filters close img.
$('.filters-close').on('click', function(){
$(this).parent().hide();
$('input[value="'+$(this).attr('data-value')+'"][name="'+$(this).attr('data-id')+'[]"]').prop('checked', false); //Filter to prevent several checkboxes with same value to be unchecked.
$('input[name="'+inputs.id+'"]').val('');
$('form.wpas-autosubmit :input').parents('form').submit();
});
appendHTML('#'+inputs.id+'_filter_'+number, (value.name ? value.name : value) + ' ');

});
});
ajaxLoader.hideImage();
updateHTML(__WPAS.DEBUG_CONTAINER,response.debug);
CURRENT_PAGE = response.current_page;
Expand All @@ -175,16 +222,17 @@ jQuery(document).ready(function($) {
} else {
ajaxLoader.showButton();
}

window.location.hash = __WPAS.HASH;
storeInstance();
unlockForm();


}, T);

},
error: function(MLHttpRequest, textStatus, errorThrown){
console.log(errorThrown);
console.log(MLHttpRequest);
}
});
}
Expand Down
114 changes: 109 additions & 5 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,132 @@ function wpas_build_ajax_response(array $post) {
$request['paged'] = $page;

$wpas_id = $request['wpas_id'];

$wpas = new WP_Advanced_Search($wpas_id, $request);
$q = $wpas->query();
$template = $wpas->get_ajax()->resultsTemplate();

$response = array();
$response['query'] = $q;
$response["count"] = $q->found_posts; //Access the query object on order to retrieve the total number of found posts.
$response["results"] = wpas_load_template_part($template, $q);
$response["current_page"] = $q->query_vars['paged'];
$response["max_page"] = $q->max_num_pages;

$response["max_page"] = $q->max_num_pages;
$response["values"] = wpas_get_tax_count($wpas->inputs, $q, $wpas->get_args());
$response["selected"] = wpas_get_selected($wpas->inputs);

if ($response["results"] === false) {
$wpas->set_error("AJAX results template '".$template."' not found in theme root.");
}

$response["debug"] = "";
if ($wpas->debug_enabled()) $response["debug"] = "<pre>". $wpas->create_debug_output() . "</pre>";

return json_encode($response);
}

function wpas_get_selected($inputs){
$response = array();
foreach ($inputs as $input){
$selected = $input->getSelected();

//Apply some filters
if(empty($selected)) continue;
if($input->getFieldType() != 'taxonomy' && $input->getFieldType() != 'search' && $input->getFieldType() != 'meta_key') continue;
if($selected[0] == '') continue; //If text inputs are empty, continue. Otherwise, they will appear on filters even if they are empty.
//

if(isset($input->taxonomy)){
$label = get_taxonomy($input->taxonomy);
$terms = get_terms(array('taxonomy' => $input->taxonomy),array('slug' => $selected));
}
$response[] = array(
'selected' => isset($terms) ? $terms : $selected,
'type' => $input->getFieldType(),
'id' => $input->getId(),
'label' => isset($label) ? $label->label : $input->getLabel(),
);
}

return $response;
}

function wpas_get_tax_count($inputs, $q, $query_args){

$response = array();
$values = array();
$all_values = array();
$tax = array();

foreach($inputs as $id => $input){
if(isset($input->taxonomy)){
$values[$input->taxonomy] = array(
'selected' => $input->getSelected(),
'format' => $input->term_format,
'operator' => $input->operator,
);
$all_values[$input->getId()] = $input->getValues();
$tax[] = array('tax' => $input->taxonomy, 'format' => $input->term_format);

$response[$input->getId()]['hide'] = $input->hideEmpty(); //Set hide_empty option so AJAX knows whether to hide it.
}
}

$args = array();

$args['meta_query'] = $q->meta_query->queries; //If a meta_query is set, set it to each subQuery.
$i = 0;
foreach($values as $taxonomy => $elements){
$terms = array();

$args['tax_query'][$i] = array(
'taxonomy' => $taxonomy,
'operator' => $elements['operator'],
'field' => $elements['format'],
);
foreach($elements['selected'] as $selection){
$terms[] = $selection;
}

if(!empty($terms)){
$args['tax_query'][$i]['terms'] = $terms;
}

if(!isset($args['tax_query'][$i]['terms'])) unset($args['tax_query'][$i]);

$i++;
}

//If tax_query was set on the form, include it here.
if(!empty($query_args['wp_query']['tax_query'])) $args['tax_query'][] = $query_args['wp_query']['tax_query'];

$i = 0;
foreach($all_values as $id => $value){
foreach($value as $slug => $label){
if(isset($args['tax_query'][$i])){
array_push($args['tax_query'][$i]['terms'], $slug);
$query = new WP_Query($args);
$response[$id][$slug] = $query->found_posts;
array_pop($args['tax_query'][$i]['terms']);
}
else{
$args['tax_query'][] = array(
'taxonomy' => $tax[$i]['tax'],
'field' => $tax[$i]['format'],
'terms' => $slug,
);
$query = new WP_Query($args);
$response[$id][$slug] = $query->found_posts;
array_pop($args['tax_query']);
}
}
$i++;
}

return $response;

}
/**
* Loads and returns a template part
*
Expand All @@ -58,7 +165,6 @@ function wpas_build_ajax_response(array $post) {
*/
function wpas_load_template_part($template, $query_object) {
global $wp_query;

$template_suffix = '/'.ltrim($template,'/');
$template = get_stylesheet_directory().$template_suffix;
if (!file_exists($template)) {
Expand All @@ -67,12 +173,10 @@ function wpas_load_template_part($template, $query_object) {
}
$temp = $wp_query;
$wp_query = $query_object;

ob_start();
load_template($template);
$var = ob_get_contents();
ob_end_clean();

$wp_query = $temp;
return $var;
}
Expand Down
17 changes: 14 additions & 3 deletions src/AjaxConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class AjaxConfig extends StdObject
private $show_default_results;
private $results_template;
private $url_hash;
private $close_img_html;
protected $args;

static protected $rules = array(
Expand All @@ -17,15 +18,17 @@ class AjaxConfig extends StdObject
'button_text' => 'string',
'show_default_results' => 'bool',
'results_template' => 'string',
'url_hash' => 'string'
'url_hash' => 'string',
'close_img_html' => 'string',
);

static protected $defaults = array(
'enabled' => false,
'button_text' => 'LOAD MORE RESULTS',
'show_default_results' => true,
'url_hash' => 'results'
);
'url_hash' => 'results',
);
//'close_img' =>

function __construct($args = array())
{
Expand All @@ -51,6 +54,10 @@ private function processArgs(array $args) {
$args['results_template'] = $dir . '/templates/template-ajax-results.php';
}

if(empty($args['close_img_html'])){
$args['close_img_html'] = "<svg style='display:inline; cursor:pointer;' class='filters-close' width='12' height='12' viewBox='0 0 32 32'><path class='path1' d='M31.708 25.708c-0-0-0-0-0-0l-9.708-9.708 9.708-9.708c0-0 0-0 0-0 0.105-0.105 0.18-0.227 0.229-0.357 0.133-0.356 0.057-0.771-0.229-1.057l-4.586-4.586c-0.286-0.286-0.702-0.361-1.057-0.229-0.13 0.048-0.252 0.124-0.357 0.228 0 0-0 0-0 0l-9.708 9.708-9.708-9.708c-0-0-0-0-0-0-0.105-0.104-0.227-0.18-0.357-0.228-0.356-0.133-0.771-0.057-1.057 0.229l-4.586 4.586c-0.286 0.286-0.361 0.702-0.229 1.057 0.049 0.13 0.124 0.252 0.229 0.357 0 0 0 0 0 0l9.708 9.708-9.708 9.708c-0 0-0 0-0 0-0.104 0.105-0.18 0.227-0.229 0.357-0.133 0.355-0.057 0.771 0.229 1.057l4.586 4.586c0.286 0.286 0.702 0.361 1.057 0.229 0.13-0.049 0.252-0.124 0.357-0.229 0-0 0-0 0-0l9.708-9.708 9.708 9.708c0 0 0 0 0 0 0.105 0.105 0.227 0.18 0.357 0.229 0.356 0.133 0.771 0.057 1.057-0.229l4.586-4.586c0.286-0.286 0.362-0.702 0.229-1.057-0.049-0.13-0.124-0.252-0.229-0.357z'></path></svg>";
}

return $args;
}

Expand Down Expand Up @@ -83,4 +90,8 @@ public function urlHash() {
return $this->url_hash;
}

public function closeImg(){
return $this->close_img_html;
}

}
Loading