This repository was archived by the owner on Nov 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbp-xprofile-field-for-member-types.php
More file actions
614 lines (502 loc) · 17.4 KB
/
Copy pathbp-xprofile-field-for-member-types.php
File metadata and controls
614 lines (502 loc) · 17.4 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<?php
/**
* The BP XProfile Field For Member Types Plugin
*
* Requires BuddyPress 2.2
*
* @package BP XProfile Field For Member Types
* @subpackage Main
*/
/**
* Plugin Name: BP XProfile Field For Member Types
* Description: Manage member type specific XProfile fields in BuddyPress
* Plugin URI: https://github.com/lmoffereins/bp-xprofile-field-for-member-types
* Version: 1.1.2
* Author: Laurens Offereins
* Author URI: https://github.com/lmoffereins
* Network: true
* Text Domain: bp-xprofile-field-for-member-types
* Domain Path: /languages/
* GitHub Plugin URI: lmoffereins/bp-xprofile-field-for-member-types
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'BP_XProfile_Field_For_Member_Types' ) ) :
/**
* Main Plugin Class
*
* @since 1.0.0
*/
final class BP_XProfile_Field_For_Member_Types {
/**
* Setup and return the singleton pattern
*
* @since 1.0.0
*
* @uses BP_XProfile_Field_For_Member_Types::setup_actions()
* @return BP_XProfile_Field_For_Member_Types
*/
public static function instance() {
// Store the instance locally
static $instance = null;
if ( null === $instance ) {
$instance = new BP_XProfile_Field_For_Member_Types;
$instance->setup_globals();
$instance->setup_actions();
}
// Always return the instance
return $instance;
}
/**
* Setup plugin structure and hooks
*
* @since 1.0.0
*/
private function __construct() { /* Do nothing here */ }
/**
* Setup default class globals
*
* @since 1.0.0
*/
private function setup_globals() {
/** Version **************************************************/
$this->version = '1.1.2';
/** Plugin ***************************************************/
$this->file = __FILE__;
$this->basename = plugin_basename( $this->file );
$this->plugin_dir = plugin_dir_path( $this->file );
$this->plugin_url = plugin_dir_url( $this->file );
// Languages
$this->lang_dir = trailingslashit( $this->plugin_dir . 'languages' );
/** Misc *****************************************************/
$this->domain = 'bp-xprofile-field-for-member-types';
}
/**
* Setup default plugin actions and filters
*
* @since 1.0.0
*
* @uses bp_is_active() To check whether xprofile component is active
*/
private function setup_actions() {
// Require BP 2.2 and the XProfile component
if ( version_compare( buddypress()->version, '2.2', '<' ) || ! bp_is_active( 'xprofile' ) )
return;
// Bail when using BP 2.4+, which already contains this plugin's logic
if ( is_callable( array( 'BP_XProfile_Field', 'get_member_types' ) ) )
return;
// Plugin
add_action( 'init', array( $this, 'load_textdomain' ) );
add_action( 'admin_init', array( $this, 'check_for_update' ) );
// Main Logic
add_filter( 'bp_xprofile_get_hidden_fields_for_user', array( $this, 'filter_hidden_fields' ), 10, 3 );
// Metabox
add_action( 'xprofile_field_after_submitbox', array( $this, 'field_display_member_type_metabox' ) );
add_action( 'xprofile_field_after_save', array( $this, 'field_save_member_type_metabox' ) );
// Admin: Profile Fields
add_action( 'xprofile_admin_field_name_legend', array( $this, 'admin_field_legend' ) );
// Fire plugin loaded hook
do_action( 'bp_xprofile_field_for_member_types_loaded' );
}
/** Plugin ****************************************************************/
/**
* Load the translation file for current language
*
* Note that custom translation files inside the Plugin folder will
* be removed on Plugin updates. If you're creating custom translation
* files, please use the global language folder.
*
* @since 1.0.0
*
* @uses apply_filters() Calls 'plugin_locale' with {@link get_locale()} value
* @uses load_textdomain() To load the textdomain
* @uses load_plugin_textdomain() To load the plugin textdomain
*/
public function load_textdomain() {
// Traditional WordPress plugin locale filter
$locale = apply_filters( 'plugin_locale', get_locale(), $this->domain );
$mofile = sprintf( '%1$s-%2$s.mo', $this->domain, $locale );
// Setup paths to current locale file
$mofile_local = $this->lang_dir . $mofile;
$mofile_global = WP_LANG_DIR . '/bp-xprofile-field-for-member-types/' . $mofile;
// Look in global /wp-content/languages/bp-xprofile-field-for-member-types folder first
load_textdomain( $this->domain, $mofile_global );
// Look in global /wp-content/languages/plugins/ and local plugin languages folder
load_plugin_textdomain( $this->domain, false, 'bp-xprofile-field-for-member-types/languages' );
}
/**
* Check if the plugin needs to run the update logic
*
* @since 1.1.0
*
* @uses get_site_option()
* @uses BP_XProfile_Field_For_Member_Types::version_updater()
*/
public function check_for_update() {
// Get current version in DB
$version = get_site_option( '_bp_xprofile_field_for_member_types', false );
// Run updater when we're updating
if ( ! $version || version_compare( $version, $this->version, '<' ) ) {
$this->version_updater( $version );
}
}
/**
* Run logic when updating the plugin
*
* @since 1.1.0
*
* @uses WPDB::update()
* @uses update_site_option()
*/
public function version_updater( $version = null ) {
global $wpdb;
$bp = buddypress();
// Pre-1.1.0
if ( false === $version ) {
// Update: use an underscore in the plugin's field meta keys
$wpdb->update(
$bp->profile->table_name_meta,
array( 'meta_key' => 'member_type' ),
array( 'meta_key' => 'member-type', 'object_type' => 'field' ),
array( '%s' ),
array( '%s', '%s' )
);
// Update: use 'null' instead of 'none'
$wpdb->update(
$bp->profile->table_name_meta,
array( 'meta_value' => 'null' ),
array( 'meta_value' => 'none', 'object_type' => 'field' ),
array( '%s' ),
array( '%s', '%s' )
);
}
// Update current version in DB
update_site_option( '_bp_xprofile_field_for_member_types', $this->version );
}
/** Main Logic ************************************************************/
/**
* Return the field ids that are not visible for the displayed and current user
*
* The displayed user must have a member type of the field in order to show the field.
* If the check fails, the field is added to the hidden fields collection.
*
* @since 1.0.0
*
* @param array $hidden_fields Hidden field ids
* @param int $displayed_user_id Displayed user ID
* @param int $current_user_id Loggedin user ID
* @return array Hidden field ids
*/
public function filter_hidden_fields( $hidden_fields, $displayed_user_id, $current_user_id ) {
global $wpdb, $bp;
// Hidden = All - Visible for displayed user AND current user
$all_fields = array_map( 'intval', (array) $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields}" ) );
foreach ( $all_fields as $k => $field_id ) {
// Is displayed user not a member? Remove field
if ( ! $this->has_user_field_member_type( $field_id, $displayed_user_id ) ) {
$hidden_fields[] = $field_id;
}
}
// Sanitize return value
$hidden_fields = array_unique( $hidden_fields );
return $hidden_fields;
}
/**
* Return whether the user has one of the field's member types
*
* @since 1.0.0
*
* @uses bp_displayed_user_id()
* @uses BP_XProfile_Field_For_Member_Types::get_xprofile_member_types()
* @uses bp_get_member_type()
*
* @param int|object $field_group_id Field ID or
* @param int $user_id Optional. User ID. Defaults to the displayed user.
* @return bool User has field's member type
*/
public function has_user_field_member_type( $field_id, $user_id = 0 ) {
// Get field ID
if ( is_object( $field_id ) ) {
$field_id = $field_id->id;
}
// The primary field is for all, so bail
if ( 1 === (int) $field_id )
return true;
// Default to displayed user
if ( ! is_numeric( $user_id ) ) {
$user_id = bp_displayed_user_id();
}
// Get the field's member types
if ( $member_types = $this->get_xprofile_member_types( $field_id, 'field' ) ) {
// Default to 'null' when the user has no member type(s)
if ( ! $u_member_types = bp_get_member_type( $user_id, false ) ) {
$u_member_types = array( 'null' );
}
// Validate user by the field's member types
$validate = array_intersect( $member_types, $u_member_types );
// Return whether we have any matches
return ! empty( $validate );
// No member types were assigned, so user validates
} else {
return true;
}
}
/** CRUD ******************************************************************/
/**
* Return a field's or group's assigned member types meta value
*
* @since 1.0.0
*
* @uses bp_xprofile_get_meta()
* @uses bp_get_member_types()
*
* @param int $object_id Field or group ID
* @param string $meta_type Type of meta, either 'field' or 'group'
* @return array Field or group member type names
*/
public function get_xprofile_member_types( $object_id, $meta_type ) {
// Get all meta instances of 'member_type' meta
$types = bp_xprofile_get_meta( $object_id, $meta_type, 'member_type', false );
// If `$types` is not an array, it probably means it is a new field (id=0)
if ( ! is_array( $types ) ) {
$types = array();
}
// If '_none' is found in the array, it overrides all other types
if ( ! in_array( '_none', $types ) ) {
$registered_types = array_values( bp_get_member_types() );
foreach ( $types as $type ) {
if ( 'null' === $type || in_array( $type, $registered_types ) ) {
$types[] = $type;
}
}
// If no member types have been saved, interpret as *all* member types
if ( empty( $types ) ) {
$types = $registered_types;
// + the 'null' type, ie users without a type
$types[] = 'null';
}
} else {
$types = array();
}
return $types;
}
/**
* Update a field's or group's member types meta value
*
* @since 1.0.0
*
* @uses bp_get_member_type_object()
* @uses bp_xprofile_delete_meta()
* @uses bp_xprofile_add_meta()
* @uses bp_get_member_types()
*
* @param int $object_id Field or group ID
* @param string $meta_type Type of meta, either 'field' or 'group'
* @param array $selected_types Selected member type names
* @return bool Update success or failure
*/
public function update_xprofile_member_types( $object_id, $meta_type, $selected_types ) {
// Unset invalid types
$types = array();
foreach ( $selected_types as $type ) {
if ( 'null' === $type || bp_get_member_type_object( $type ) ) {
$types[] = $type;
}
}
// Delete all existing types before adding new ones
bp_xprofile_delete_meta( $object_id, $meta_type, 'member_type' );
/*
* We interpret an empty array as disassociating the field from all types. This is
* represented internally with the '_none' flag.
*/
if ( empty( $types ) ) {
return bp_xprofile_add_meta( $object_id, $meta_type, 'member_type', '_none' );
}
/*
* Unrestricted fields are represented in the database as having no 'member_type'.
* We detect whether a field is being set to unrestricted by checking whether the
* list of types passed to the method is the same as the list of registered types,
* plus the 'null' pseudo-type.
*/
$_rtypes = bp_get_member_types();
$rtypes = array_values( $_rtypes );
$rtypes[] = 'null';
sort( $types );
sort( $rtypes );
// Only save if this is a restricted field.
if ( $types !== $rtypes ) {
// Save new types.
foreach ( $types as $type ) {
bp_xprofile_add_meta( $object_id, $meta_type, 'member_type', $type );
}
}
return true;
}
/** Metabox ***************************************************************/
/**
* Output the metabox for field assigned member types
*
* Since BP 2.1.0.
*
* @since 1.0.0
*
* @param BP_XProfile_Field $field Current XProfile field
*/
public function field_display_member_type_metabox( $field ) {
// The primary field is for all, so bail
if ( 1 === (int) $field->id )
return;
// Bail when no member types are registered
if ( ! $member_types = bp_get_member_types( array(), 'objects' ) )
return;
// Get the field's member types
$field_member_types = ! empty( $field->id ) ? $this->get_xprofile_member_types( $field->id, 'field' ) : array();
?>
<div id="for_member_types" class="postbox">
<h3><?php _e( 'Member Types', 'bp-xprofile-field-for-member-types' ); ?></h3>
<div class="inside">
<p><?php _e( 'This field should be available to:', 'bp-xprofile-field-for-member-types' ); ?></p>
<ul>
<?php foreach ( $member_types as $member_type ) : ?>
<li>
<label>
<input name="member-types[]" type="checkbox" class="member-type-selector" value="<?php echo $member_type->name; ?>" <?php checked( in_array( $member_type->name, $field_member_types ) ); ?>/>
<?php echo $member_type->labels['singular_name']; ?>
</label>
</li>
<?php endforeach; ?>
<li>
<label>
<input name="member-types[]" type="checkbox" class="member-type-selector" value="null" <?php checked( in_array( 'null', $field_member_types ) ); ?>/>
<?php _e( 'Users with no member type', 'bp-xprofile-field-for-member-types' ); ?>
</label>
</li>
</ul>
<p class="description member-type-none-notice<?php if ( ! empty( $field_member_types ) ) : ?> hidden<?php endif; ?>"><?php _e( 'Unavailable to all members.', 'buddypress' ) ?></p>
<style>
.member-type-none-notice {
color: #f00 !important;
}
</style>
<script>
/**
* @summary Toggles "no member type" notice.
*
* @since 1.1.2
* @see BuddyPress (2.4.0)
*/
function toggle_no_member_type_notice() {
var $member_type_checkboxes = jQuery( 'input.member-type-selector' );
// No checkboxes? Nothing to do.
if ( ! $member_type_checkboxes.length )
return;
var has_checked = false;
$member_type_checkboxes.each( function() {
if ( jQuery( this ).is( ':checked' ) ) {
has_checked = true;
return false;
}
});
if ( has_checked ) {
jQuery( 'p.member-type-none-notice' ).addClass( 'hidden' );
} else {
jQuery( 'p.member-type-none-notice' ).removeClass( 'hidden' );
}
}
jQuery( document ).ready( function( $ ) {
// Set up the notice that shows when no member types are selected for a field.
toggle_no_member_type_notice();
$( 'input.member-type-selector' ).on( 'change', function() {
toggle_no_member_type_notice();
});
});
</script>
</div>
<?php wp_nonce_field( 'member-types', '_wpnonce_for_member_types' ); ?>
</div>
<?php
}
/**
* Save the metabox for field assigned member types
*
* @since 1.0.0
*
* @uses BP_Xprofile_For_Member_Types::update_xprofile_member_types()
*
* @param BP_XProfile_Field $field Saved XProfile field
*/
public function field_save_member_type_metabox( $field ) {
// Bail when nonce does not verify
if ( ! isset( $_REQUEST['_wpnonce_for_member_types'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce_for_member_types'], 'member-types' ) )
return;
/**
* The created field's id is unknown at this point. The following is a buggy fix
* while we're waiting on a fix for. Watch #BP6545 closely.
*/
if ( empty( $field->id ) ) {
global $wpdb;
$field->id = $wpdb->insert_id;
}
// Get posted values
$member_types = isset( $_REQUEST['member-types'] ) ? (array) $_REQUEST['member-types'] : array();
// Update changes
$this->update_xprofile_member_types( $field->id, 'field', $member_types );
}
/** Admin: Profile Fields *************************************************/
/**
* Display the selected member types per field on the Profile Fields screen
*
* @since 1.0.0
*
* @uses BP_XProfile_Field_For_Member_Types::get_xprofile_member_types()
* @uses bp_get_meber_types()
* @param BP_XProfile_Field $field Field object
*/
public function admin_field_legend( $field ) {
// The primary field is for all, so bail
if ( 1 === (int) $field->id )
return;
// Get selected member types
$member_types = $this->get_xprofile_member_types( $field->id, 'field' );
// Types selected
if ( ! empty( $member_types ) ) {
// Get member type objects
$types = bp_get_member_types( array(), 'objects' );
// Add 'null' type
$types['null'] = array(
'labels' => array(
'singular_name' => __( 'Users with no member type', 'bp-xprofile-field-for-member-types' )
)
);
sort( $member_types );
ksort( $types );
// If the field applies to all type, show no mesage
if ( $member_types === array_keys( $types ) )
return;
// Get selected type labels
$types = array_intersect_key( $types, array_flip( $member_types ) );
$types = wp_list_pluck( $types, 'labels' );
$types = wp_list_pluck( $types, 'singular_name' );
// Construct legend
$legend = sprintf( __( 'Member types: %s', 'bp-xprofile-field-for-member-types' ), implode( ', ', $types ) );
// No type selected
} else {
$legend = __( 'Unavailable to all members', 'bp-xprofile-field-for-member-types' );
}
// Output legend <span>
echo '<span class="member-types">(' . $legend . ')</span>';
}
}
/**
* Initiate plugin class and return singleton
*
* @since 1.0.0
*
* @return BP_XProfile_Field_For_Member_Types
*/
function bp_xprofile_field_for_member_types() {
return BP_XProfile_Field_For_Member_Types::instance();
}
// Fire it up!
add_action( 'bp_loaded', 'bp_xprofile_field_for_member_types' );
endif; // class_exists