-
Notifications
You must be signed in to change notification settings - Fork 1
/
bp-pseudo-role-field-hooks.php
118 lines (79 loc) · 2.48 KB
/
bp-pseudo-role-field-hooks.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
<?php
/**
* Helps connecting with BuddyPress Profile Search
* // bridge code for BP Profile Search 4.6 or later
*/
class BD_Pseudo_Role_Field_BPS_Helper {
private static $instance = null ;
private function __construct() {
$this->setup();
}
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
private function setup() {
add_filter ( 'bps_field_validation', array( $this, 'allow_validation' ), 10, 2 );
add_filter ( 'bps_field_data_for_search_form', array( $this, 'add_field_data' ) );
add_filter ( 'bps_field_data_for_filters', array( $this, 'add_field_data' ) );
add_filter( 'bps_field_setup_data', array( $this, 'unset_search_callback' ) );
add_filter( 'bps_field_query', array( $this, 'field_query' ), 10, 4 );
add_filter( 'bp_after_has_profile_parse_args', array( $this, 'hide_role_fields' ) );
}
public function allow_validation ( $settings, $field ) {
list ($value, $description, $range) = $settings;
if ($field->type == 'role') {
$range = false;
}
return array ( $value, $description, $range );
}
public function add_field_data ( $field ) {
if ( $field->type == 'role' ) {
$field->display = 'selectbox';
$field->values = isset ($_REQUEST[$field->code])? (array)$_REQUEST[$field->code]: array ();
$field->options = bd_pseudo_role_field_get_available_roles();
}
return $field;
}
/**
*
* Do not let BP Profile Search hijack the query
*
* @param $f
*
* @return mixed
*/
public function unset_search_callback( $f ) {
if ( $f->type == 'role' ) {
$f->search = '';
}
return $f;
}
public function field_query( $results, $field, $key, $value ) {
if ( $field->type == 'role' ) {
$results = bd_pseudo_get_users_by_role( $value );
}
return $results;
}
public function hide_role_fields( $args ) {
global $pagenow;
$hide = false;
if( is_admin() && isset( $_GET['page'] ) && $_GET['page'] == 'bp-profile-edit' && $pagenow == 'users.php' ) {
$hide = true;
}
if( ! $hide && ! bp_is_user() && ! bp_is_register_page() ) {
return $args;
}
$excluded_fields = $args['exclude_fields'];
if( empty( $excluded_fields ) ) {
$excluded_fields = array();
}
$role_field_ids = bd_pseudo_get_role_fields();
$excluded_fields = array_merge( $excluded_fields, $role_field_ids );
$args['exclude_fields'] = $excluded_fields;
return $args;
}
}
BD_Pseudo_Role_Field_BPS_Helper::get_instance();