-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstu-search-filter-bricks.php
160 lines (131 loc) · 4.93 KB
/
stu-search-filter-bricks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/*
* Plugin Name: Search & Filter - Bricks Extension
* Description: Makes it easier to use Search & Filter with Bricks
* Author: Curtis Stewart
* Version: 1.2
* License: GPL2
*/
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', array( 'Stu_Search_Filter_Bricks', 'get_instance' ) );
Class Stu_Search_Filter_Bricks {
protected static $instance = null;
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct() {
add_action( 'wp_enqueue_scripts', function() {
// Enqueue your files on the canvas & frontend, not the builder panel. Otherwise custom CSS might affect builder)
if ( ! bricks_is_builder_main() ) {
wp_enqueue_script( 'stu-search-filter-bricks', plugin_dir_url( __FILE__ ) . 'assets/js/stu-search-filter-bricks.js', array('jquery'), filemtime(plugin_dir_path( __FILE__ ) . 'assets/js/stu-search-filter-bricks.js'));
}
} );
// List of elements to add controls to
$elements = [
'container',
'div',
'block',
'accordion',
'slider',
'posts',
'woocommerce-products'
];
// Loop through elements and add controls
foreach($elements as $element) {
add_filter( 'bricks/elements/' . $element . '/controls', function( $controls ) {
return self::get_bricks_controls($controls);
} );
}
// Add Search & Filter note to Pagination Element
add_filter( 'bricks/elements/pagination/controls', function( $controls ) {
$new_controls = [ 'searchAndFilterFormNote' => [
'tab' => 'content',
'content' => esc_html__( 'Search & Filter Note: If you want to use this with Search & Filter, you must put it inside the same container that you put your query loop.' , 'bricks' ),
'type' => 'info',
]];
// Combining this way ensures the note show up first in Bricks
return $new_controls + $controls;
} );
// Inject Search & Filter Id into query loop
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
if (isset( $settings['searchAndFilterFormId'] )) {
$query_vars['search_filter_id'] = $settings['searchAndFilterFormId'];
}
return $query_vars;
}, 10, 3 );
// Setup Search & Filter Form Element
add_action( 'init', function() {
$file = '/elements/search-filter-form.php';
$element_name = 'stu-search-filter-form';
$class_name = 'Stu_Element_Search_Filter_Form';
\Bricks\Elements::register_element( __DIR__ . $file, $element_name, $class_name);
}, 11 );
}
// This function does the brunt of the work of setting up
// the controls for each supported Bricks Element
private static function get_bricks_controls($controls) {
if (!bricks_is_builder_main()) return $controls;
$ordered_controls = [];
// Loop over existing controls so we can inject our controls exactly where we want them
foreach($controls as $k => $v) {
$ordered_controls[$k] = $v;
// Place after the query control with the
// exception of the Product element which we
// place after the out of stock control
if ($k === 'query' ||
$k === 'hideOutOfStock') {
$ordered_controls['searchAndFilterFormId'] = [
'tab' => 'content',
'label' => esc_html__( 'Link with a Search and Filter Form', 'bricks' ),
'type' => 'select',
'options' => self::get_search_form_options(),
'placeholder' => esc_html__( 'None', 'bricks' ),
];
$ordered_controls['searchAndFilterFormNote'] = [
'tab' => 'content',
'content' => esc_html__( 'Search & Filter Note: If you have Search & Filter Pro and want ajax to work, you must put your query loop inside a container and then put the container ID in the Search Form options.' , 'bricks' ),
'type' => 'info',
];
}
}
// Only make the hasLoop control required if it exists
if ( array_key_exists('hasLoop', $controls) ) {
$ordered_controls['searchAndFilterFormId']['required'] = [
['hasLoop', '!=', ''],
];
$ordered_controls['searchAndFilterFormNote']['required'] = [
['hasLoop', '!=', ''],
];
}
// Only add the controls to the query group if
// we're in the product element
if ( array_key_exists('hideOutOfStock',$controls) ) {
$ordered_controls['searchAndFilterFormId']['group'] = 'query';
$ordered_controls['searchAndFilterFormNote']['group'] = 'query';
};
return $ordered_controls;
}
// Method to get an array of Search & Filter Forms
public static function get_search_form_options() {
$args = [
'post_type' => 'search-filter-widget',
'post_status' => 'publish',
'posts_per_page' => -1
];
$custom_posts = new WP_Query($args);
$search_form_options = [];
while ($custom_posts->have_posts()) {
$custom_posts->the_post();
$search_form_options[get_the_ID()] = html_entity_decode(get_the_title());
}
wp_reset_postdata();
return $search_form_options;
}
}