/
bpbd.php
298 lines (227 loc) · 9.17 KB
/
bpbd.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
class BPBD {
var $get_params = array();
var $filterable_fields = array();
/**
* PHP 4 constructor
*
* @package BP Better Directories
* @since 1.0
*/
function bpbd() {
$this->__construct();
}
/**
* PHP 5 constructor
*
* @package BP Better Directories
* @since 1.0
*/
function __construct() {
define( 'BPBD_INSTALL_DIR', trailingslashit( dirname(__FILE__) ) );
define( 'BPBD_INSTALL_URL', plugins_url() . '/bp-better-directories/' );
if ( version_compare( BP_VERSION, '1.3', '<' ) ) {
require( BPBD_INSTALL_DIR . 'includes/1.5-abstraction.php' );
}
$this->setup_get_params();
add_action( 'init', array( $this, 'setup' ) );
add_action( 'wp_ajax_members_filter', array( $this, 'filter_ajax_requests' ), 1 );
add_action( 'wp_print_styles', array( $this, 'enqueue_styles' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
function setup() {
global $bp;
// Temporary backpat for 1.2
if ( function_exists( 'bp_is_current_component' ) ) {
$is_members = bp_is_current_component( 'members' );
} else {
$is_members = $bp->members->slug == bp_current_component();
}
if ( $is_members && !bp_is_single_item() ) {
// Add the sql filters
add_filter( 'bp_core_get_paged_users_sql', array( $this, 'users_sql_filter' ), 10, 2 );
add_filter( 'bp_core_get_total_users_sql', array( $this, 'users_sql_filter' ), 10, 2 );
// Add the filter UI
add_action( 'bp_before_directory_members', array( $this, 'filter_ui' ) );
}
}
function setup_get_params() {
$filterable_fields = get_blog_option( BP_ROOT_BLOG, 'bpdb_fields' );
// Set so it can be used object-wide
$this->filterable_fields = $filterable_fields;
if ( is_array( $filterable_fields ) ) {
$filterable_keys = array_keys( $filterable_fields );
}
$potential_fields = isset( $_GET ) ? $_GET : array();
$cookie_fields = isset( $_COOKIE['bpbd-filters'] ) ? (array)json_decode( stripslashes( $_COOKIE['bpbd-filters'] ) ) : null;
if ( isset( $_COOKIE['bpbd-filters'] ) )
$potential_fields = array_merge( $potential_fields, $cookie_fields );
foreach ( (array)$filterable_keys as $filterable_key ) {
if ( !empty( $potential_fields[$filterable_key] ) ) {
// Get the field id for keying the array
$field_id = $filterable_fields[$filterable_key]['id'];
// Put the field data into the get_params property array
$this->get_params[$field_id] = $filterable_fields[$filterable_key];
// Add the filtered value from $_GET
if ( is_array( $potential_fields[$filterable_key] ) ) {
$values = array();
foreach( $potential_fields[$filterable_key] as $key => $value ) {
$values[$key] = urldecode( $value );
}
} else {
$values = urldecode( $potential_fields[$filterable_key] );
}
$this->get_params[$field_id]['value'] = $values;
}
}
// Todo: sort order?
}
function users_sql_filter( $s, $sql ) {
global $bp, $wpdb;
$bpbd_select = array();
$bpbd_from = array();
$bpbd_where = array();
$counter = 1;
// Build the additional queries
foreach( $this->get_params as $field_id => $field ) {
$table_shortname = 'bpbd' . $counter;
// Since we're already doing the join, let's bring the extra content into
// the template. This'll be unset in the total_users filter
$bpbd_select[] = ", {$table_shortname}.value as {$field['slug']}";
$bpbd_from[] = "INNER JOIN {$bp->profile->table_name_data} {$table_shortname} ON ({$table_shortname}.user_id = u.ID)";
if ( 'textbox' == $field['type'] || 'multiselectbox' == $field['type'] || 'checkbox' == $field['type'] ) {
// Multiselect and checkbox values may be stored as arrays, so we
// have to do multiple LIKEs. Hack alert
$clauses = array();
foreach( (array)$field['value'] as $val ) {
$clauses[] = $table_shortname . '.value LIKE "%%' . $val . '%%"';
}
$where = implode( ' OR ', $clauses );
} else {
// Everything else is an exact match
$op = '=';
$value = $wpdb->prepare( "%s", $field['value'] );
$where = $table_shortname . '.value' . $op . ' ' . $value;
}
$bpbd_where[] = $wpdb->prepare( "AND {$table_shortname}.field_id = %d AND ({$where})", $field['id'] );
$counter++;
}
if ( !empty( $bpbd_from ) && !empty( $bpbd_where ) ) {
// The total_sql query won't have this index
if ( isset( $sql['select_main'] ) )
$sql['select_main'] .= ' ' . implode( ' ', $bpbd_select );
$sql['from'] .= ' ' . implode( ' ', $bpbd_from );
$sql['where'] .= ' ' . implode( ' ', $bpbd_where );
$s = join( ' ', (array)$sql );
}
return $s;
}
function filter_ajax_requests() {
header("Cache-Control: no-cache, must-revalidate");
add_filter( 'bp_core_get_paged_users_sql', array( $this, 'users_sql_filter' ), 10, 2 );
add_filter( 'bp_core_get_total_users_sql', array( $this, 'users_sql_filter' ), 10, 2 );
}
function filter_ui() {
if ( empty( $this->filterable_fields ) ) {
// Nothing to see here.
return;
}
?>
<form id="bpbd-filter-form" method="get" action="<?php bp_root_domain() ?>/<?php bp_members_root_slug() ?>">
<div id="bpbd-filters">
<h4><?php _e( 'Narrow Results', 'bpbd' ) ?> <span id="bpbd-clear-all"><a href="#">Clear All</a></span></h4>
<ul>
<?php foreach ( $this->filterable_fields as $slug => $field ) : ?>
<li id="bpbd-filter-crit-<?php echo esc_attr( $field['slug'] ) ?>" class="bpbd-filter-crit bpbd-filter-crit-type-<?php echo esc_attr( $field['type'] ) ?>">
<?php $this->render_field( $field ) ?>
</li>
<?php endforeach ?>
</ul>
<input type="submit" class="button" value="<?php _e( 'Submit', 'bpdb' ) ?>" />
</div>
</form>
<?php
}
function render_field( $field ) {
?>
<label for="<?php echo esc_attr( $field['slug'] ) ?>"><?php echo esc_html( $field['name'] ) ?> <span class="bpbd-clear-this"><a href="#">Clear</a></span></label>
<?php
$field_data = new BP_XProfile_Field( $field['id'] );
$options = $field_data->get_children();
// Get the current value for this item, if any, out of the $_GET params
$value = isset( $this->get_params[$field['id']] ) ? $this->get_params[$field['id']]['value'] : false;
// Display the field based on type
switch ( $field['type'] ) {
case 'radio' :
?>
<ul>
<?php foreach ( $options as $option ) : ?>
<li>
<input type="radio" name="<?php echo esc_attr( $field['slug'] ) ?>" value="<?php echo urlencode( $option->name ) ?>" <?php checked( $value, $option->name, true ) ?>/> <?php echo esc_html( $option->name ) ?>
</li>
<?php endforeach ?>
</ul>
<?php
break;
case 'selectbox' :
?>
<select name="<?php echo esc_attr( $field['slug'] ) ?>">
<option value="">--------</option>
<?php foreach( $options as $option ) : ?>
<option <?php selected( $value, $option->name, true ) ?>><?php echo $option->name ?></option>
<?php endforeach ?>
</select>
<?php
break;
case 'multiselectbox' :
?>
<select name="<?php echo esc_attr( $field['slug'] ) ?>[]" multiple="multiple">
<?php foreach( $options as $option ) : ?>
<option <?php if ( is_array( $value ) && in_array( $option->name, $value ) ) : ?>selected="selected"<?php endif ?>/><?php echo $option->name ?></option>
<?php endforeach ?>
</select>
<?php
break;
case 'checkbox' :
?>
<ul>
<?php foreach ( (array)$options as $option ) : ?>
<li>
<input type="checkbox" name="<?php echo esc_attr( $field['slug'] ) ?>[]" value="<?php echo urlencode( $option->name ) ?>" <?php if ( is_array( $value ) && in_array( $option->name, $value ) ) : ?>checked="checked"<?php endif ?>/> <?php echo esc_html( $option->name ) ?>
</li>
<?php endforeach ?>
</ul>
<?php
break;
case 'textbox' :
default :
?>
<input id="bpbd-filter-<?php echo esc_attr( $field['slug'] ) ?>" type="text" name="<?php echo esc_attr( $field['slug'] ) ?>" value=""/>
<ul class="bpbd-search-terms">
<?php if ( is_array( $value ) && !empty( $value ) ) : ?>
<?php foreach ( (array)$value as $sterm ) : ?>
<?php if ( !trim( $sterm ) ) continue; ?>
<li id="bpbd-value-<?php echo sanitize_title( $sterm ) ?>"><span class="bpbd-remove"><a href="#">x</a></span> <?php echo esc_html( $sterm ) ?></li>
<?php endforeach ?>
<?php endif ?>
</ul>
<?php /* Comma-separated string */ ?>
<input class="bpbd-hidden-value" id="bpbd-filter-value-<?php echo esc_attr( $field['slug'] ) ?>" type="hidden" name="bpbd-filter-value-<?php echo esc_attr( $field['slug'] ) ?>" value="<?php echo esc_attr( implode( ',', (array)$value ) ) ?>" />
<?php
break;
}
}
function enqueue_styles() {
if ( bp_is_directory() && bp_is_members_component() ) {
wp_enqueue_style( 'jquery-loadmask-css', BPBD_INSTALL_URL . '/includes/lib/jquery.loadmask/jquery.loadmask.css' );
wp_enqueue_style( 'bpbd-css', BPBD_INSTALL_URL . '/includes/css/style.css' );
}
}
function enqueue_scripts() {
if ( bp_is_directory() && bp_is_members_component() ) {
wp_enqueue_script( 'jquery-loadmask', BPBD_INSTALL_URL . '/includes/lib/jquery.loadmask/jquery.loadmask.min.js', array( 'jquery' ) );
wp_enqueue_script( 'bpbd-js', BPBD_INSTALL_URL . '/includes/js/bpbd.js', array( 'jquery', 'dtheme-ajax-js', 'jquery-loadmask' ) );
}
}
}
?>