1 | <?php |
---|
2 | /** |
---|
3 | * BuddyPress XProfile Filters. |
---|
4 | * |
---|
5 | * Apply WordPress defined filters. |
---|
6 | * |
---|
7 | * @package BuddyPress |
---|
8 | * @subpackage XProfileFilters |
---|
9 | * @since 1.0.0 |
---|
10 | */ |
---|
11 | |
---|
12 | // Exit if accessed directly. |
---|
13 | defined( 'ABSPATH' ) || exit; |
---|
14 | |
---|
15 | add_filter( 'bp_get_the_profile_group_name', 'wp_filter_kses', 1 ); |
---|
16 | add_filter( 'bp_get_the_profile_group_description', 'wp_filter_kses', 1 ); |
---|
17 | add_filter( 'bp_get_the_profile_field_name', 'wp_filter_kses', 1 ); |
---|
18 | add_filter( 'bp_get_the_profile_field_edit_value', 'xprofile_sanitize_data_value_before_display', 1, 3 ); |
---|
19 | add_filter( 'bp_get_the_profile_field_description', 'wp_filter_kses', 1 ); |
---|
20 | add_filter( 'bp_get_the_profile_field_value', 'xprofile_sanitize_data_value_before_display', 1, 3 ); |
---|
21 | |
---|
22 | add_filter( 'bp_get_the_profile_field_value', 'wptexturize' ); |
---|
23 | add_filter( 'bp_get_the_profile_field_value', 'convert_chars' ); |
---|
24 | add_filter( 'bp_get_the_profile_field_value', 'wpautop' ); |
---|
25 | add_filter( 'bp_get_the_profile_field_value', 'force_balance_tags' ); |
---|
26 | add_filter( 'bp_get_the_profile_field_value', 'make_clickable' ); |
---|
27 | add_filter( 'bp_get_the_profile_field_value', 'bp_xprofile_escape_field_data', 8, 3 ); |
---|
28 | add_filter( 'bp_get_the_profile_field_value', 'convert_smilies', 9 ); |
---|
29 | add_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_format_field_value', 1, 2 ); |
---|
30 | add_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_format_field_value_by_type', 8, 3 ); |
---|
31 | add_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9, 3 ); |
---|
32 | |
---|
33 | add_filter( 'bp_get_the_profile_field_edit_value', 'force_balance_tags' ); |
---|
34 | add_filter( 'bp_get_the_profile_field_edit_value', 'bp_xprofile_escape_field_data', 10, 3 ); |
---|
35 | |
---|
36 | add_filter( 'bp_get_the_profile_group_name', 'stripslashes' ); |
---|
37 | add_filter( 'bp_get_the_profile_group_description', 'stripslashes' ); |
---|
38 | add_filter( 'bp_get_the_profile_field_value', 'stripslashes' ); |
---|
39 | add_filter( 'bp_get_the_profile_field_edit_value', 'stripslashes' ); |
---|
40 | add_filter( 'bp_get_the_profile_field_name', 'stripslashes' ); |
---|
41 | add_filter( 'bp_get_the_profile_field_description', 'stripslashes' ); |
---|
42 | |
---|
43 | add_filter( 'xprofile_get_field_data', 'xprofile_sanitize_data_value_before_display_from_get_field_data', 1, 2 ); |
---|
44 | add_filter( 'xprofile_field_name_before_save', 'wp_filter_kses', 1 ); |
---|
45 | add_filter( 'xprofile_field_description_before_save', 'wp_filter_kses', 1 ); |
---|
46 | |
---|
47 | add_filter( 'xprofile_get_field_data', 'force_balance_tags' ); |
---|
48 | add_filter( 'xprofile_field_name_before_save', 'force_balance_tags' ); |
---|
49 | add_filter( 'xprofile_field_description_before_save', 'force_balance_tags' ); |
---|
50 | |
---|
51 | add_filter( 'xprofile_get_field_data', 'stripslashes' ); |
---|
52 | add_filter( 'xprofile_get_field_data', 'xprofile_filter_format_field_value_by_field_id', 5, 2 ); |
---|
53 | |
---|
54 | add_filter( 'bp_xprofile_set_field_data_pre_validate', 'xprofile_filter_pre_validate_value_by_field_type', 10, 3 ); |
---|
55 | add_filter( 'xprofile_data_value_before_save', 'xprofile_sanitize_data_value_before_save', 1, 4 ); |
---|
56 | add_filter( 'xprofile_filtered_data_value_before_save', 'trim', 2 ); |
---|
57 | |
---|
58 | // Save field groups. |
---|
59 | add_filter( 'xprofile_group_name_before_save', 'wp_filter_kses' ); |
---|
60 | add_filter( 'xprofile_group_description_before_save', 'wp_filter_kses' ); |
---|
61 | |
---|
62 | add_filter( 'xprofile_group_name_before_save', 'stripslashes' ); |
---|
63 | add_filter( 'xprofile_group_description_before_save', 'stripslashes' ); |
---|
64 | |
---|
65 | // Save fields. |
---|
66 | add_filter( 'xprofile_field_name_before_save', 'wp_filter_kses' ); |
---|
67 | add_filter( 'xprofile_field_type_before_save', 'wp_filter_kses' ); |
---|
68 | add_filter( 'xprofile_field_description_before_save', 'wp_filter_kses' ); |
---|
69 | add_filter( 'xprofile_field_order_by_before_save', 'wp_filter_kses' ); |
---|
70 | |
---|
71 | add_filter( 'xprofile_field_is_required_before_save', 'absint' ); |
---|
72 | add_filter( 'xprofile_field_field_order_before_save', 'absint' ); |
---|
73 | add_filter( 'xprofile_field_option_order_before_save', 'absint' ); |
---|
74 | add_filter( 'xprofile_field_can_delete_before_save', 'absint' ); |
---|
75 | |
---|
76 | // Save field options. |
---|
77 | add_filter( 'xprofile_field_options_before_save', 'bp_xprofile_sanitize_field_options' ); |
---|
78 | add_filter( 'xprofile_field_default_before_save', 'bp_xprofile_sanitize_field_default' ); |
---|
79 | |
---|
80 | // Personal data export. |
---|
81 | add_filter( 'wp_privacy_personal_data_exporters', 'bp_xprofile_register_personal_data_exporter' ); |
---|
82 | |
---|
83 | /** |
---|
84 | * Sanitize each field option name for saving to the database. |
---|
85 | * |
---|
86 | * @since 2.3.0 |
---|
87 | * |
---|
88 | * @param mixed $field_options Options to sanitize. |
---|
89 | * @return mixed |
---|
90 | */ |
---|
91 | function bp_xprofile_sanitize_field_options( $field_options = '' ) { |
---|
92 | if ( is_array( $field_options ) ) { |
---|
93 | return array_map( 'sanitize_text_field', $field_options ); |
---|
94 | } else { |
---|
95 | return sanitize_text_field( $field_options ); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Sanitize each field option default for saving to the database. |
---|
101 | * |
---|
102 | * @since 2.3.0 |
---|
103 | * |
---|
104 | * @param mixed $field_default Field defaults to sanitize. |
---|
105 | * @return array|int |
---|
106 | */ |
---|
107 | function bp_xprofile_sanitize_field_default( $field_default = '' ) { |
---|
108 | if ( is_array( $field_default ) ) { |
---|
109 | return array_map( 'intval', $field_default ); |
---|
110 | } else { |
---|
111 | return intval( $field_default ); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Run profile field values through kses with filterable allowed tags. |
---|
117 | * |
---|
118 | * @since 1.5.0 |
---|
119 | * @since 2.1.0 Added `$data_obj` parameter. |
---|
120 | * @since 5.0.0 Added `$field_id` parameter. |
---|
121 | * |
---|
122 | * @param string $content Content to filter. |
---|
123 | * @param BP_XProfile_ProfileData|null $data_obj Optional. The BP_XProfile_ProfileData object. |
---|
124 | * @param int|null $field_id Optional. The ID of the profile field. |
---|
125 | * @return string $content |
---|
126 | */ |
---|
127 | function xprofile_filter_kses( $content, $data_obj = null, $field_id = null ) { |
---|
128 | global $allowedtags; |
---|
129 | |
---|
130 | $xprofile_allowedtags = $allowedtags; |
---|
131 | $xprofile_allowedtags['a']['rel'] = array(); |
---|
132 | |
---|
133 | if ( null === $field_id && $data_obj instanceof BP_XProfile_ProfileData ) { |
---|
134 | $field_id = $data_obj->field_id; |
---|
135 | } |
---|
136 | |
---|
137 | // If the field supports rich text, we must allow tags that appear in wp_editor(). |
---|
138 | if ( $field_id && bp_xprofile_is_richtext_enabled_for_field( $field_id ) ) { |
---|
139 | $richtext_tags = array( |
---|
140 | 'img' => array( 'src' => 1, 'alt' => 1, 'width' => 1, 'height' => 1 ), |
---|
141 | 'ul' => array(), |
---|
142 | 'ol' => array(), |
---|
143 | 'li' => array(), |
---|
144 | 'span' => array( 'style' => 1 ), |
---|
145 | 'p' => array( 'style' => 1 ), |
---|
146 | ); |
---|
147 | |
---|
148 | $xprofile_allowedtags = array_merge( $allowedtags, $richtext_tags ); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Filters the allowed tags for use within xprofile_filter_kses(). |
---|
153 | * |
---|
154 | * @since 1.5.0 |
---|
155 | * @since 2.1.0 Added `$data_obj` parameter. |
---|
156 | * @since 5.0.0 Added `$field_id` parameter. |
---|
157 | * |
---|
158 | * @param array $xprofile_allowedtags Array of allowed tags for profile field values. |
---|
159 | * @param BP_XProfile_ProfileData|null $data_obj The BP_XProfile_ProfileData object. |
---|
160 | * @param int|null $field_id The ID of the profile field. |
---|
161 | */ |
---|
162 | $xprofile_allowedtags = apply_filters( 'xprofile_allowed_tags', $xprofile_allowedtags, $data_obj, $field_id ); |
---|
163 | return wp_kses( $content, $xprofile_allowedtags ); |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * Filters profile field values for whitelisted HTML. |
---|
168 | * |
---|
169 | * @since 5.0.0 |
---|
170 | * |
---|
171 | * @param string $value Field value. |
---|
172 | * @param string $type Field type. |
---|
173 | * @param int $field_id Field ID. |
---|
174 | */ |
---|
175 | function xprofile_sanitize_data_value_before_display( $value, $type, $field_id ) { |
---|
176 | return xprofile_filter_kses( $value, null, $field_id ); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * Filters profile field values for whitelisted HTML, when coming from xprofile_get_field_data(). |
---|
181 | * |
---|
182 | * @since 5.0.0 |
---|
183 | * |
---|
184 | * @param string $value Field value. |
---|
185 | * @param int $field_id Field ID. |
---|
186 | */ |
---|
187 | function xprofile_sanitize_data_value_before_display_from_get_field_data( $value, $field_id ) { |
---|
188 | return xprofile_filter_kses( $value, $field_id ); |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Safely runs profile field data through kses and force_balance_tags. |
---|
193 | * |
---|
194 | * @since 1.2.6 |
---|
195 | * |
---|
196 | * @param string $field_value Field value being sanitized. |
---|
197 | * @param int $field_id Field ID being sanitized. |
---|
198 | * @param bool $reserialize Whether to reserialize arrays before returning. Defaults to true. |
---|
199 | * @param object|null $data_obj The BP_XProfile_ProfileData object. |
---|
200 | * @return string |
---|
201 | */ |
---|
202 | function xprofile_sanitize_data_value_before_save( $field_value, $field_id = 0, $reserialize = true, $data_obj = null ) { |
---|
203 | |
---|
204 | // Return if empty. |
---|
205 | if ( empty( $field_value ) ) { |
---|
206 | return $field_value; |
---|
207 | } |
---|
208 | |
---|
209 | // Force reserialization if serialized (avoids mutation, retains integrity) |
---|
210 | if ( is_serialized( $field_value ) && ( false === $reserialize ) ) { |
---|
211 | $reserialize = true; |
---|
212 | } |
---|
213 | |
---|
214 | // Value might be a serialized array of options. |
---|
215 | $field_value = maybe_unserialize( $field_value ); |
---|
216 | |
---|
217 | // Sanitize single field value. |
---|
218 | if ( ! is_array( $field_value ) ) { |
---|
219 | $kses_field_value = xprofile_filter_kses( $field_value, $data_obj ); |
---|
220 | $filtered_field_value = wp_rel_nofollow( force_balance_tags( $kses_field_value ) ); |
---|
221 | |
---|
222 | /** |
---|
223 | * Filters the kses-filtered data before saving to database. |
---|
224 | * |
---|
225 | * @since 1.5.0 |
---|
226 | * |
---|
227 | * @param string $filtered_field_value The filtered value. |
---|
228 | * @param string $field_value The original value before filtering. |
---|
229 | * @param BP_XProfile_ProfileData $data_obj The BP_XProfile_ProfileData object. |
---|
230 | */ |
---|
231 | $filtered_field_value = apply_filters( 'xprofile_filtered_data_value_before_save', $filtered_field_value, $field_value, $data_obj ); |
---|
232 | |
---|
233 | // Sanitize multiple individual option values. |
---|
234 | } else { |
---|
235 | $filtered_values = array(); |
---|
236 | foreach ( (array) $field_value as $value ) { |
---|
237 | $kses_field_value = xprofile_filter_kses( $value, $data_obj ); |
---|
238 | $filtered_value = wp_rel_nofollow( force_balance_tags( $kses_field_value ) ); |
---|
239 | |
---|
240 | /** This filter is documented in bp-xprofile/bp-xprofile-filters.php */ |
---|
241 | $filtered_values[] = apply_filters( 'xprofile_filtered_data_value_before_save', $filtered_value, $value, $data_obj ); |
---|
242 | } |
---|
243 | |
---|
244 | if ( !empty( $reserialize ) ) { |
---|
245 | $filtered_field_value = serialize( $filtered_values ); |
---|
246 | } else { |
---|
247 | $filtered_field_value = $filtered_values; |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | return $filtered_field_value; |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * Runs stripslashes on XProfile fields. |
---|
256 | * |
---|
257 | * @since 1.0.0 |
---|
258 | * |
---|
259 | * @param string $field_value XProfile field_value to be filtered. |
---|
260 | * @param string $field_type XProfile field_type to be filtered. |
---|
261 | * @return false|string $field_value Filtered XProfile field_value. False on failure. |
---|
262 | */ |
---|
263 | function xprofile_filter_format_field_value( $field_value, $field_type = '' ) { |
---|
264 | |
---|
265 | // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731. |
---|
266 | if ( ! isset( $field_value ) || empty( $field_value ) && ( '0' !== $field_value ) ) { |
---|
267 | return false; |
---|
268 | } |
---|
269 | |
---|
270 | if ( 'datebox' !== $field_type ) { |
---|
271 | $field_value = str_replace( ']]>', ']]>', $field_value ); |
---|
272 | } |
---|
273 | |
---|
274 | return stripslashes( $field_value ); |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * Apply display_filter() filters as defined by BP_XProfile_Field_Type classes, when inside a bp_has_profile() loop. |
---|
279 | * |
---|
280 | * @since 2.1.0 |
---|
281 | * @since 2.4.0 Added `$field_id` parameter. |
---|
282 | * |
---|
283 | * @param mixed $field_value Field value. |
---|
284 | * @param string $field_type Field type. |
---|
285 | * @param string|int $field_id Optional. ID of the field. |
---|
286 | * @return mixed |
---|
287 | */ |
---|
288 | function xprofile_filter_format_field_value_by_type( $field_value, $field_type = '', $field_id = '' ) { |
---|
289 | foreach ( bp_xprofile_get_field_types() as $type => $class ) { |
---|
290 | if ( $type !== $field_type ) { |
---|
291 | continue; |
---|
292 | } |
---|
293 | |
---|
294 | if ( method_exists( $class, 'display_filter' ) ) { |
---|
295 | $field_value = call_user_func( array( $class, 'display_filter' ), $field_value, $field_id ); |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | return $field_value; |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * Apply display_filter() filters as defined by the BP_XProfile_Field_Type classes, when fetched |
---|
304 | * by xprofile_get_field_data(). |
---|
305 | * |
---|
306 | * @since 2.1.0 |
---|
307 | * |
---|
308 | * @param mixed $field_value Field value. |
---|
309 | * @param int $field_id Field type. |
---|
310 | * @return string |
---|
311 | */ |
---|
312 | function xprofile_filter_format_field_value_by_field_id( $field_value, $field_id ) { |
---|
313 | $field = xprofile_get_field( $field_id ); |
---|
314 | return xprofile_filter_format_field_value_by_type( $field_value, $field->type, $field_id ); |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * Apply pre_validate_filter() filters as defined by the BP_XProfile_Field_Type classes before validating. |
---|
319 | * |
---|
320 | * @since 2.1.0 |
---|
321 | * |
---|
322 | * @param mixed $value Value passed to the bp_xprofile_set_field_data_pre_validate filter. |
---|
323 | * @param BP_XProfile_Field $field Field object. |
---|
324 | * @param BP_XProfile_Field_Type $field_type_obj Field type object. |
---|
325 | * @return mixed |
---|
326 | */ |
---|
327 | function xprofile_filter_pre_validate_value_by_field_type( $value, $field, $field_type_obj ) { |
---|
328 | if ( method_exists( $field_type_obj, 'pre_validate_filter' ) ) { |
---|
329 | $value = call_user_func( array( $field_type_obj, 'pre_validate_filter' ), $value, $field->id ); |
---|
330 | } |
---|
331 | |
---|
332 | return $value; |
---|
333 | } |
---|
334 | |
---|
335 | /** |
---|
336 | * Escape field value for display. |
---|
337 | * |
---|
338 | * Most field values are simply run through esc_html(). Those that support rich text (by default, `textarea` only) |
---|
339 | * are sanitized using kses, which allows a whitelist of HTML tags. |
---|
340 | * |
---|
341 | * @since 2.4.0 |
---|
342 | * |
---|
343 | * @param string $value Field value. |
---|
344 | * @param string $field_type Field type. |
---|
345 | * @param int $field_id Field ID. |
---|
346 | * @return string |
---|
347 | */ |
---|
348 | function bp_xprofile_escape_field_data( $value, $field_type, $field_id ) { |
---|
349 | if ( bp_xprofile_is_richtext_enabled_for_field( $field_id ) ) { |
---|
350 | // The xprofile_filter_kses() expects a BP_XProfile_ProfileData object. |
---|
351 | $data_obj = null; |
---|
352 | if ( bp_is_user() ) { |
---|
353 | $data_obj = new BP_XProfile_ProfileData( $field_id, bp_displayed_user_id() ); |
---|
354 | } |
---|
355 | |
---|
356 | $value = xprofile_filter_kses( $value, $data_obj ); |
---|
357 | } else { |
---|
358 | $value = esc_html( $value ); |
---|
359 | } |
---|
360 | |
---|
361 | return $value; |
---|
362 | } |
---|
363 | |
---|
364 | /** |
---|
365 | * Filter an Extended Profile field value, and attempt to make clickable links |
---|
366 | * to members search results out of them. |
---|
367 | * |
---|
368 | * - Not run on datebox field types. |
---|
369 | * - Not run on values without commas with less than 5 words. |
---|
370 | * - URL's are made clickable. |
---|
371 | * |
---|
372 | * To disable globally: |
---|
373 | * remove_filter( 'bp_get_the_profile_field_value', 'xprofile_filter_link_profile_data', 9, 3 ); |
---|
374 | * |
---|
375 | * To disable for a single field, use the 'Autolink' settings in Dashboard > Users > Profile Fields. |
---|
376 | * |
---|
377 | * @since 1.1.0 |
---|
378 | * |
---|
379 | * @param string $field_value Profile field data value. |
---|
380 | * @param string $field_type Profile field type. |
---|
381 | * @return string|array |
---|
382 | */ |
---|
383 | function xprofile_filter_link_profile_data( $field_value, $field_type = 'textbox' ) { |
---|
384 | global $field; |
---|
385 | |
---|
386 | if ( ! $field->get_do_autolink() ) { |
---|
387 | return $field_value; |
---|
388 | } |
---|
389 | |
---|
390 | if ( 'datebox' === $field_type ) { |
---|
391 | return $field_value; |
---|
392 | } |
---|
393 | |
---|
394 | if ( strpos( $field_value, ',' ) === false && strpos( $field_value, ';' ) === false && ( count( explode( ' ', $field_value ) ) > 5 ) ) { |
---|
395 | return $field_value; |
---|
396 | } |
---|
397 | |
---|
398 | if ( strpos( $field_value, ',' ) !== false ) { |
---|
399 | // Comma-separated lists. |
---|
400 | $list_type = 'comma'; |
---|
401 | $values = explode( ',', $field_value ); |
---|
402 | } else { |
---|
403 | /* |
---|
404 | * Semicolon-separated lists. |
---|
405 | * |
---|
406 | * bp_xprofile_escape_field_data() runs before this function, which often runs esc_html(). |
---|
407 | * In turn, that encodes HTML entities in the string (";" becomes "'"). |
---|
408 | * |
---|
409 | * Before splitting on the ";" character, decode the HTML entities, and re-encode after. |
---|
410 | * This prevents input like "O'Hara" rendering as "O' Hara" (with each of those parts |
---|
411 | * having a separate HTML link). |
---|
412 | */ |
---|
413 | $list_type = 'semicolon'; |
---|
414 | $field_value = wp_specialchars_decode( $field_value, ENT_QUOTES ); |
---|
415 | $values = explode( ';', $field_value ); |
---|
416 | |
---|
417 | array_walk( $values, function( &$value, $key ) use ( $field_type, $field ) { |
---|
418 | $value = bp_xprofile_escape_field_data( $value, $field_type, $field->id ); |
---|
419 | } ); |
---|
420 | } |
---|
421 | |
---|
422 | if ( ! empty( $values ) ) { |
---|
423 | foreach ( (array) $values as $value ) { |
---|
424 | $value = trim( $value ); |
---|
425 | |
---|
426 | // If the value is a URL, skip it and just make it clickable. |
---|
427 | if ( preg_match( '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $value ) ) { |
---|
428 | $new_values[] = make_clickable( $value ); |
---|
429 | |
---|
430 | // Is not clickable. |
---|
431 | } else { |
---|
432 | |
---|
433 | // More than 5 spaces. |
---|
434 | if ( count( explode( ' ', $value ) ) > 5 ) { |
---|
435 | $new_values[] = $value; |
---|
436 | |
---|
437 | // Less than 5 spaces. |
---|
438 | } else { |
---|
439 | $query_arg = bp_core_get_component_search_query_arg( 'members' ); |
---|
440 | $search_url = add_query_arg( array( $query_arg => urlencode( $value ) ), bp_get_members_directory_permalink() ); |
---|
441 | $new_values[] = '<a href="' . esc_url( $search_url ) . '" rel="nofollow">' . $value . '</a>'; |
---|
442 | } |
---|
443 | } |
---|
444 | } |
---|
445 | |
---|
446 | if ( 'comma' === $list_type ) { |
---|
447 | $values = implode( ', ', $new_values ); |
---|
448 | } else { |
---|
449 | $values = implode( '; ', $new_values ); |
---|
450 | } |
---|
451 | } |
---|
452 | |
---|
453 | return $values; |
---|
454 | } |
---|
455 | |
---|
456 | /** |
---|
457 | * Ensures that BP data appears in comments array. |
---|
458 | * |
---|
459 | * This filter loops through the comments return by a normal WordPress request |
---|
460 | * and swaps out user data with BP xprofile data, where available. |
---|
461 | * |
---|
462 | * @since 1.2.0 |
---|
463 | * |
---|
464 | * @param array $comments Comments to filter in. |
---|
465 | * @param int $post_id Post ID the comments are for. |
---|
466 | * @return array $comments |
---|
467 | */ |
---|
468 | function xprofile_filter_comments( $comments, $post_id = 0 ) { |
---|
469 | |
---|
470 | // Locate comment authors with WP accounts. |
---|
471 | foreach( (array) $comments as $comment ) { |
---|
472 | if ( $comment->user_id ) { |
---|
473 | $user_ids[] = $comment->user_id; |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | // If none are found, just return the comments array. |
---|
478 | if ( empty( $user_ids ) ) { |
---|
479 | return $comments; |
---|
480 | } |
---|
481 | |
---|
482 | // Pull up the xprofile fullname of each commenter. |
---|
483 | if ( $fullnames = bp_core_get_user_displaynames( $user_ids ) ) { |
---|
484 | foreach( (array) $fullnames as $user_id => $user_fullname ) { |
---|
485 | $users[ $user_id ] = trim( stripslashes( $user_fullname ) ); |
---|
486 | } |
---|
487 | } |
---|
488 | |
---|
489 | // Loop through and match xprofile fullname with commenters. |
---|
490 | foreach( (array) $comments as $i => $comment ) { |
---|
491 | if ( ! empty( $comment->user_id ) ) { |
---|
492 | if ( ! empty( $users[ $comment->user_id ] ) ) { |
---|
493 | $comments[ $i ]->comment_author = $users[ $comment->user_id ]; |
---|
494 | } |
---|
495 | } |
---|
496 | } |
---|
497 | |
---|
498 | return $comments; |
---|
499 | } |
---|
500 | add_filter( 'comments_array', 'xprofile_filter_comments', 10, 2 ); |
---|
501 | |
---|
502 | /** |
---|
503 | * Filter BP_User_Query::populate_extras to override each queries users fullname. |
---|
504 | * |
---|
505 | * @since 1.7.0 |
---|
506 | * |
---|
507 | * @param BP_User_Query $user_query User query to filter. |
---|
508 | * @param string $user_ids_sql SQL statement to use. |
---|
509 | */ |
---|
510 | function bp_xprofile_filter_user_query_populate_extras( BP_User_Query $user_query, $user_ids_sql = '' ) { |
---|
511 | |
---|
512 | if ( ! bp_is_active( 'xprofile' ) ) { |
---|
513 | return; |
---|
514 | } |
---|
515 | |
---|
516 | $user_id_names = bp_core_get_user_displaynames( $user_query->user_ids ); |
---|
517 | |
---|
518 | // Loop through names and override each user's fullname. |
---|
519 | foreach ( $user_id_names as $user_id => $user_fullname ) { |
---|
520 | if ( isset( $user_query->results[ $user_id ] ) ) { |
---|
521 | $user_query->results[ $user_id ]->fullname = $user_fullname; |
---|
522 | } |
---|
523 | } |
---|
524 | } |
---|
525 | add_filter( 'bp_user_query_populate_extras', 'bp_xprofile_filter_user_query_populate_extras', 2, 2 ); |
---|
526 | |
---|
527 | /** |
---|
528 | * Parse 'xprofile_query' argument passed to BP_User_Query. |
---|
529 | * |
---|
530 | * @since 2.2.0 |
---|
531 | * |
---|
532 | * @param BP_User_Query $q User query object. |
---|
533 | */ |
---|
534 | function bp_xprofile_add_xprofile_query_to_user_query( BP_User_Query $q ) { |
---|
535 | |
---|
536 | // Bail if no `xprofile_query` clause. |
---|
537 | if ( empty( $q->query_vars['xprofile_query'] ) ) { |
---|
538 | return; |
---|
539 | } |
---|
540 | |
---|
541 | $xprofile_query = new BP_XProfile_Query( $q->query_vars['xprofile_query'] ); |
---|
542 | $sql = $xprofile_query->get_sql( 'u', $q->uid_name ); |
---|
543 | |
---|
544 | if ( ! empty( $sql['join'] ) ) { |
---|
545 | $q->uid_clauses['select'] .= $sql['join']; |
---|
546 | $q->uid_clauses['where'] .= $sql['where']; |
---|
547 | } |
---|
548 | } |
---|
549 | add_action( 'bp_pre_user_query', 'bp_xprofile_add_xprofile_query_to_user_query' ); |
---|
550 | |
---|
551 | /** |
---|
552 | * Filter meta queries to modify for the xprofile data schema. |
---|
553 | * |
---|
554 | * @since 2.0.0 |
---|
555 | * |
---|
556 | * @access private Do not use. |
---|
557 | * |
---|
558 | * @param string $q SQL query. |
---|
559 | * @return string |
---|
560 | */ |
---|
561 | function bp_xprofile_filter_meta_query( $q ) { |
---|
562 | global $wpdb; |
---|
563 | |
---|
564 | $raw_q = $q; |
---|
565 | |
---|
566 | /* |
---|
567 | * Replace quoted content with __QUOTE__ to avoid false positives. |
---|
568 | * This regular expression will match nested quotes. |
---|
569 | */ |
---|
570 | $quoted_regex = "/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'/s"; |
---|
571 | preg_match_all( $quoted_regex, $q, $quoted_matches ); |
---|
572 | $q = preg_replace( $quoted_regex, '__QUOTE__', $q ); |
---|
573 | |
---|
574 | // Get the first word of the command. |
---|
575 | preg_match( '/^(\S+)/', $q, $first_word_matches ); |
---|
576 | |
---|
577 | if ( empty( $first_word_matches[0] ) ) { |
---|
578 | return $raw_q; |
---|
579 | } |
---|
580 | |
---|
581 | // Get the field type. |
---|
582 | preg_match( '/xprofile_(group|field|data)_id/', $q, $matches ); |
---|
583 | |
---|
584 | if ( empty( $matches[0] ) || empty( $matches[1] ) ) { |
---|
585 | return $raw_q; |
---|
586 | } |
---|
587 | |
---|
588 | switch ( $first_word_matches[0] ) { |
---|
589 | |
---|
590 | /** |
---|
591 | * SELECT: |
---|
592 | * - replace 'xprofile_{fieldtype}_id' with 'object_id' |
---|
593 | * - ensure that 'object_id' is aliased to 'xprofile_{fieldtype}_id', |
---|
594 | * because update_meta_cache() needs the column name to parse |
---|
595 | * the query results |
---|
596 | * - append the 'object type' WHERE clause |
---|
597 | */ |
---|
598 | case 'SELECT' : |
---|
599 | $q = str_replace( |
---|
600 | array( |
---|
601 | $matches[0], |
---|
602 | 'SELECT object_id', |
---|
603 | 'WHERE ', |
---|
604 | ), |
---|
605 | array( |
---|
606 | 'object_id', |
---|
607 | 'SELECT object_id AS ' . $matches[0], |
---|
608 | $wpdb->prepare( 'WHERE object_type = %s AND ', $matches[1] ), |
---|
609 | ), |
---|
610 | $q |
---|
611 | ); |
---|
612 | break; |
---|
613 | |
---|
614 | /** |
---|
615 | * UPDATE and DELETE: |
---|
616 | * - replace 'xprofile_{fieldtype}_id' with 'object_id' |
---|
617 | * - append the 'object type' WHERE clause |
---|
618 | */ |
---|
619 | case 'UPDATE' : |
---|
620 | case 'DELETE' : |
---|
621 | $q = str_replace( |
---|
622 | array( |
---|
623 | $matches[0], |
---|
624 | 'WHERE ', |
---|
625 | ), |
---|
626 | array( |
---|
627 | 'object_id', |
---|
628 | $wpdb->prepare( 'WHERE object_type = %s AND ', $matches[1] ), |
---|
629 | ), |
---|
630 | $q |
---|
631 | ); |
---|
632 | break; |
---|
633 | |
---|
634 | /** |
---|
635 | * UPDATE and DELETE: |
---|
636 | * - replace 'xprofile_{fieldtype}_id' with 'object_id' |
---|
637 | * - ensure that the object_type field gets filled in |
---|
638 | */ |
---|
639 | case 'INSERT' : |
---|
640 | $q = str_replace( |
---|
641 | array( |
---|
642 | '`' . $matches[0] . '`', |
---|
643 | 'VALUES (', |
---|
644 | ), |
---|
645 | array( |
---|
646 | '`object_type`,`object_id`', |
---|
647 | $wpdb->prepare( "VALUES (%s,", $matches[1] ), |
---|
648 | ), |
---|
649 | $q |
---|
650 | ); |
---|
651 | break; |
---|
652 | } |
---|
653 | |
---|
654 | // Put quoted content back into the string. |
---|
655 | if ( ! empty( $quoted_matches[0] ) ) { |
---|
656 | for ( $i = 0; $i < count( $quoted_matches[0] ); $i++ ) { |
---|
657 | $quote_pos = strpos( $q, '__QUOTE__' ); |
---|
658 | $q = substr_replace( $q, $quoted_matches[0][ $i ], $quote_pos, 9 ); |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | return $q; |
---|
663 | } |
---|
664 | |
---|
665 | /** |
---|
666 | * Register XProfile personal data exporter. |
---|
667 | * |
---|
668 | * @since 4.0.0 |
---|
669 | * @since 5.0.0 adds an `exporter_bp_friendly_name` param to exporters. |
---|
670 | * |
---|
671 | * @param array $exporters An array of personal data exporters. |
---|
672 | * @return array An array of personal data exporters. |
---|
673 | */ |
---|
674 | function bp_xprofile_register_personal_data_exporter( $exporters ) { |
---|
675 | $exporters['buddypress-xprofile'] = array( |
---|
676 | 'exporter_friendly_name' => __( 'BuddyPress Extended Profile Data', 'buddypress' ), |
---|
677 | 'callback' => 'bp_xprofile_personal_data_exporter', |
---|
678 | 'exporter_bp_friendly_name' => _x( 'Extended Profile information', 'BuddyPress Extended Profile data exporter friendly name', 'buddypress' ), |
---|
679 | ); |
---|
680 | |
---|
681 | return $exporters; |
---|
682 | } |
---|