Skip to content

Commit d2413ef

Browse files
Merge pull request #632 from contactashish13/issue-627
Basic filtering/ordering system
2 parents 90d5246 + 965b830 commit d2413ef

File tree

6 files changed

+234
-57
lines changed

6 files changed

+234
-57
lines changed

classes/Visualizer/Module/Admin.php

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,64 @@ function setScreenOptions( $status, $option, $value ) {
742742
}
743743
}
744744

745+
/**
746+
* Adds the display filters to be used in the meta_query while displaying the charts.
747+
*/
748+
private function getDisplayFilters( &$query_args ) {
749+
$query = array();
750+
751+
// add chart type filter to the query arguments
752+
$type = filter_input( INPUT_GET, 'type' );
753+
if ( $type && in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) {
754+
$query[] = array(
755+
'key' => Visualizer_Plugin::CF_CHART_TYPE,
756+
'value' => $type,
757+
'compare' => '=',
758+
);
759+
}
760+
761+
// add chart library filter to the query arguments
762+
$library = filter_input( INPUT_GET, 'library' );
763+
if ( $library ) {
764+
$query[] = array(
765+
'key' => Visualizer_Plugin::CF_CHART_LIBRARY,
766+
'value' => $library,
767+
'compare' => '=',
768+
);
769+
}
770+
771+
// add date filter to the query arguments
772+
$date = filter_input( INPUT_GET, 'date' );
773+
$possible = array_keys( Visualizer_Plugin::getSupportedDateFilter() );
774+
if ( $date && in_array( $date, $possible, true ) ) {
775+
$query_args['date_query'] = array(
776+
'after' => "$date -1 day",
777+
'inclusive' => true,
778+
);
779+
}
780+
781+
// add source filter to the query arguments
782+
$source = filter_input( INPUT_GET, 'source' );
783+
if ( $source ) {
784+
$source = ucwords( $source );
785+
$source = "Visualizer_Source_{$source}";
786+
$query[] = array(
787+
'key' => Visualizer_Plugin::CF_SOURCE,
788+
'value' => $source,
789+
'compare' => '=',
790+
);
791+
}
792+
793+
$query_args['meta_query'] = $query;
794+
795+
$orderby = filter_input( INPUT_GET, 'orderby' );
796+
$order = filter_input( INPUT_GET, 'order' );
797+
if ( $orderby ) {
798+
$query_args['order_by'] = $orderby;
799+
}
800+
$query_args['order'] = empty( $order ) ? 'desc' : $order;
801+
}
802+
745803
/**
746804
* Get the instance of WP_Query that fetches the charts as per the given criteria.
747805
*/
@@ -784,19 +842,9 @@ private function getQuery() {
784842
'posts_per_page' => $per_page,
785843
'paged' => $page,
786844
);
787-
// add chart type filter to the query arguments
788-
$filter = filter_input( INPUT_GET, 'type' );
789-
if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) {
790-
$query_args['meta_query'] = array(
791-
array(
792-
'key' => Visualizer_Plugin::CF_CHART_TYPE,
793-
'value' => $filter,
794-
'compare' => '=',
795-
),
796-
);
797-
} else {
798-
$filter = 'all';
799-
}
845+
846+
$this->getDisplayFilters( $query_args );
847+
800848
// Added by Ash/Upwork
801849
$filterByMeta = filter_input( INPUT_GET, 's', FILTER_SANITIZE_STRING );
802850
if ( $filterByMeta ) {

classes/Visualizer/Plugin.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ class Visualizer_Plugin {
123123
*/
124124
private $_modules = array();
125125

126+
/**
127+
* Returns the date queries supported in the library date filter.
128+
*/
129+
public static function getSupportedDateFilter() {
130+
return apply_filters( 'visualizer_filter_by_date', array( '' => __( 'All dates', 'visualizer' ), 'yesterday' => __( 'Yesterday', 'visualizer' ), 'last week' => __( 'Last Week', 'visualizer' ), 'last month' => __( 'Last Month', 'visualizer' ), 'last year' => __( 'Last Year', 'visualizer' ) ) );
131+
}
132+
126133
/**
127134
* Private constructor.
128135
*

classes/Visualizer/Render/Library.php

Lines changed: 141 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,146 @@ private function _renderMessages() {
6767
}
6868
}
6969

70+
/**
71+
* Displays the search form.
72+
*/
73+
private function getDisplayForm() {
74+
echo '<div class="visualizer-library-form">
75+
<form action="' . admin_url( 'admin.php' ) . '">
76+
<input type="hidden" name="page" value="' . Visualizer_Plugin::NAME . '"/>
77+
<select class="viz-filter" name="type">
78+
';
79+
80+
echo '<option value="" selected>' . __( 'All types', 'visualizer' ) . '</option>';
81+
82+
$type = isset( $_GET['type'] ) ? $_GET['type'] : '';
83+
$enabled = array();
84+
$disabled = array();
85+
foreach ( $this->types as $id => $array ) {
86+
if ( ! is_array( $array ) ) {
87+
// support for old pro
88+
$array = array( 'enabled' => true, 'name' => $array );
89+
}
90+
if ( ! $array['enabled'] ) {
91+
$disabled[ $id ] = $array['name'];
92+
continue;
93+
}
94+
$enabled[ $id ] = $array['name'];
95+
}
96+
97+
asort( $enabled );
98+
asort( $disabled );
99+
100+
foreach ( $enabled as $id => $name ) {
101+
echo '<option value="' . esc_attr( $id ) . '" ' . selected( $type, $id ) . '>' . $name . '</option>';
102+
}
103+
104+
if ( $disabled ) {
105+
echo '<optgroup label="' . __( 'Not available', 'visualizer' ) . '">';
106+
foreach ( $disabled as $id => $name ) {
107+
echo '<option value="' . esc_attr( $id ) . '" ' . selected( $type, $id ) . ' disabled>' . $name . '</option>';
108+
}
109+
echo '</optgroup>';
110+
}
111+
112+
echo '
113+
</select>
114+
<select class="viz-filter" name="library">
115+
';
116+
117+
$libraries = array( '', 'ChartJS', 'DataTable', 'GoogleCharts' );
118+
$library = isset( $_GET['library'] ) ? $_GET['library'] : '';
119+
foreach ( $libraries as $lib ) {
120+
echo '<option value="' . esc_attr( $lib ) . '" ' . selected( $library, $lib ) . '>' . ( $lib === '' ? __( 'All libraries', 'visualizer' ) : $lib ) . '</option>';
121+
}
122+
123+
echo '
124+
</select>
125+
<select class="viz-filter" name="date">
126+
';
127+
128+
$dates = Visualizer_Plugin::getSupportedDateFilter();
129+
$date = isset( $_GET['date'] ) ? $_GET['date'] : '';
130+
foreach ( Visualizer_Plugin::getSupportedDateFilter() as $dt => $label ) {
131+
echo '<option value="' . esc_attr( $dt ) . '" ' . selected( $date, $dt ) . '>' . $label . '</option>';
132+
}
133+
134+
echo '
135+
</select>
136+
<select class="viz-filter" name="source">
137+
';
138+
139+
$disabled = array();
140+
$sources = array( 'json' => __( 'JSON', 'visualizer' ), 'csv' => __( 'Local CSV', 'visualizer' ), 'csv_remote' => __( 'Remote CSV', 'visualizer' ), 'query' => __( 'Database', 'visualizer' ), 'query_wp' => __( 'WordPress', 'visualizer' ) );
141+
if ( ! Visualizer_Module::is_pro() ) {
142+
$disabled['query'] = $sources['query'];
143+
unset( $sources['query'] );
144+
}
145+
if ( ! apply_filters( 'visualizer_is_business', false ) ) {
146+
$disabled['query_wp'] = $sources['query_wp'];
147+
unset( $sources['query_wp'] );
148+
}
149+
$sources = array_filter( $sources );
150+
uasort(
151+
$sources, function( $a, $b ) {
152+
if ( $a === $b ) {
153+
return 0;
154+
}
155+
return ( $a < $b ) ? -1 : 1;
156+
}
157+
);
158+
159+
$source = isset( $_GET['source'] ) ? $_GET['source'] : '';
160+
echo '<option value="">' . __( 'All sources', 'visualizer' ) . '</option>';
161+
foreach ( $sources as $field => $label ) {
162+
echo '<option value="' . esc_attr( $field ) . '" ' . selected( $source, $field ) . '>' . $label . '</option>';
163+
}
164+
165+
if ( $disabled ) {
166+
echo '<optgroup label="' . __( 'Not available', 'visualizer' ) . '">';
167+
foreach ( $disabled as $id => $name ) {
168+
echo '<option value="' . esc_attr( $id ) . '" ' . selected( $type, $id ) . ' disabled>' . $name . '</option>';
169+
}
170+
echo '</optgroup>';
171+
}
172+
173+
$name = isset( $_GET['s'] ) ? $_GET['s'] : '';
174+
echo '
175+
</select>
176+
<input class="viz-filter" type="text" name="s" placeholder="' . __( 'Enter title', 'visualizer' ) . '" value="' . esc_attr( $name ) . '">
177+
';
178+
179+
echo '
180+
<span class="viz-filter">|</span>
181+
<select class="viz-filter" name="orderby">
182+
';
183+
184+
$order_by_fields = apply_filters( 'visualizer_filter_order_by', array( 'date' => __( 'Date', 'visualizer' ), 's' => __( 'Title', 'visualizer' ) ) );
185+
$order_by = isset( $_GET['orderby'] ) ? $_GET['orderby'] : '';
186+
echo '<option value="">' . __( 'Order By', 'visualizer' ) . '</option>';
187+
foreach ( $order_by_fields as $field => $label ) {
188+
echo '<option value="' . esc_attr( $field ) . '" ' . selected( $order_by, $field ) . '>' . $label . '</option>';
189+
}
190+
191+
echo '
192+
</select>
193+
<select class="viz-filter" name="order">
194+
';
195+
196+
$order_type = array( 'desc' => __( 'Descending', 'visualizer' ), 'asc' => __( 'Ascending', 'visualizer' ) );
197+
$order = isset( $_GET['order'] ) ? $_GET['order'] : 'desc';
198+
foreach ( $order_type as $field => $label ) {
199+
echo '<option value="' . esc_attr( $field ) . '" ' . selected( $order, $field ) . '>' . $label . '</option>';
200+
}
201+
202+
echo '
203+
</select>
204+
<input type="submit" class="viz-filter button button-secondary" value="' . __( 'Apply Filters', 'visualizer' ) . '">
205+
<input type="button" id="viz-lib-reset" class="viz-filter button button-secondary" value="' . __( 'Clear Filters', 'visualizer' ) . '">
206+
</form>
207+
</div>';
208+
}
209+
70210
/**
71211
* Renders library content.
72212
*
@@ -83,48 +223,7 @@ private function _renderLibrary() {
83223
// Added by Ash/Upwork
84224
echo $this->custom_css;
85225
echo '<div id="visualizer-types" class="visualizer-clearfix">';
86-
echo '<ul class="subsubsub">';
87-
// All tab.
88-
echo '<li class="visualizer-list-item all"><a class="' . ( ! isset( $_GET['type'] ) || empty( $_GET['type'] ) ? 'current' : '' ) . '" href="', esc_url( add_query_arg( array( 'vpage' => false, 'type' => false, 'vaction' => false, 's' => false ) ) ), '">' . __( 'All', 'visualizer' ) . '</a> | </li>';
89-
foreach ( $this->types as $type => $array ) {
90-
if ( ! is_array( $array ) ) {
91-
// support for old pro
92-
$array = array( 'enabled' => true, 'name' => $array );
93-
}
94-
$label = $array['name'];
95-
$link = '<a class=" " href="' . esc_url(
96-
add_query_arg(
97-
array(
98-
'type' => $type,
99-
'vpage' => false,
100-
'vaction' => false,
101-
's' => false,
102-
)
103-
)
104-
) . '">';
105-
if ( ! $array['enabled'] ) {
106-
$link = "<a class=' visualizer-pro-only' href='" . Visualizer_Plugin::PRO_TEASER_URL . "' target='_blank'>";
107-
}
108-
echo '<li class="visualizer-list-item ' . esc_attr( $this->type ) . '">';
109-
if ( $type === $this->type ) {
110-
echo '<a class="current" href="', esc_url( add_query_arg( 'vpage', false ) ), '">';
111-
echo $label;
112-
echo '</a>';
113-
} else {
114-
echo $link;
115-
echo $label;
116-
echo '</a>';
117-
}
118-
echo ' | </li>';
119-
}
120-
echo '</ul>';
121-
122-
echo '<form action="" method="get"><p id="visualizer-search" class="search-box">
123-
<input type="search" placeholder="' . __( 'Enter title', 'visualizer' ) . '" name="s" value="' . $filterBy . '">
124-
<input type="hidden" name="page" value="visualizer">
125-
<button type="submit" id="search-submit" title="' . __( 'Search', 'visualizer' ) . '"><i class="dashicons dashicons-search"></i></button>
126-
</p> </form>';
127-
226+
$this->getDisplayForm();
128227
echo '</div>';
129228
echo '<div id="visualizer-content-wrapper">';
130229
if ( ! empty( $this->charts ) ) {

css/library.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ input:checked + .visualizer-slider:before {
288288
text-align: center;
289289
}
290290

291+
div.visualizer-library-form .viz-filter {
292+
vertical-align: middle;
293+
}
294+
291295
button.media-modal-close {
292296
display: none !important;
293297
}

cypress/integration/free-lifecycle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ describe('Test Free - lifecycle', function() {
1313
cy.visit(Cypress.env('urls').library );
1414

1515
// chart types
16-
cy.get('li.visualizer-list-item').should( "have.length", parseInt( Cypress.env('chart_types').free ) + parseInt( Cypress.env('chart_types').pro ) + 1 );
16+
//cy.get('li.visualizer-list-item').should( "have.length", parseInt( Cypress.env('chart_types').free ) + parseInt( Cypress.env('chart_types').pro ) + 1 );
1717

1818
// pro chart types
19-
cy.get('li.visualizer-list-item a.visualizer-pro-only').should( "have.length", parseInt( Cypress.env('chart_types').pro ) );
19+
//cy.get('li.visualizer-list-item a.visualizer-pro-only').should( "have.length", parseInt( Cypress.env('chart_types').pro ) );
2020

2121
// upsell
2222
cy.get('.visualizer-sidebar-box').should( "have.length", 1 );

js/library.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@
4747
$(document).ready(function () {
4848
$('.visualizer-chart, .visualizer-library-pagination').fadeIn(500);
4949

50+
// clears the filters in the library form and submits.
51+
$('#viz-lib-reset').on('click', function(e){
52+
e.preventDefault();
53+
$(this).parent('form')[0].reset();
54+
$(this).parent('form').find('.viz-filter').each(function(index, el){
55+
var tag = $(el).prop('tagName').toLowerCase() + (typeof $(el).attr('type') !== 'undefined' ? $(el).attr('type').toLowerCase() : '');
56+
switch(tag){
57+
case 'select':
58+
$(el).prop('selectedIndex', 0);
59+
break;
60+
case 'inputtext':
61+
$(el).val('');
62+
break;
63+
}
64+
65+
});
66+
$(this).parent('form').submit();
67+
});
68+
5069
$('.visualizer-chart-shortcode').click(function (e) {
5170
var range, selection;
5271

0 commit comments

Comments
 (0)