-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp-admin-only-profile-fields.php
403 lines (307 loc) · 10.1 KB
/
bp-admin-only-profile-fields.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
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
<?php
/*
Plugin Name: BuddyPress Admin Global Profile Fields
Description: Introduce global BuddyPress profiles fields, allowing only admin users to edit and and allow all users to view them.
Version: 1.0
Author: Garrett Hyder
Author URI: http://nightbook.ca
License: GPL2
Copyright 2015 Garrett Hyder (email : nightbook.g.hyder@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* BuddyPress Admin Global Profile Fields
*
* @package BuddyPress Admin Global Profile Fields
* @since 1.0
*/
class BP_Admin_Global_Profile_Fields {
/**
* Instance of this class.
*
* @since 1.0
*/
private static $instance = null;
/**
* Initialize the plugin.
*
* @since 1.0
*/
private function __construct() {
// Setup plugin constants
self::setup_constants();
// Load plugin text domain
self::load_plugin_textdomain();
// Actions
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'xprofile_field_before_contentbox', array( $this, 'bp_xprofile_global_field_value' ) );
add_action( 'xprofile_fields_saved_field', array( $this, 'bp_xprofile_save_global_field_value' ) );
add_action( 'bp_profile_field_item', array( $this, 'display_global_profile_fields' ) );
// Filters
add_filter( 'bp_xprofile_get_visibility_levels', array( $this, 'global_visibility_level' ) );
add_filter( 'bp_xprofile_get_hidden_field_types_for_user', array( $this, 'append_global_visibility_level' ), 10, 3 );
add_filter( 'bp_profile_get_visibility_radio_buttons', array( $this, 'filter_global_visibility_from_radio_buttons' ), 10, 3 );
add_filter( 'bp_after_has_profile_parse_args', array( $this, 'show_global_fields' ) );
}
/**
* Return an instance of this class.
*
* @since 1.0
*
* @return BP_Admin_Global_Profile_Fields
*/
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;
}
/**
* Setup plugin constants.
*
* @since 1.0
*/
private function setup_constants() {
if ( ! defined( 'BPAGPF_VERSION' ) ) {
define( 'BPAGPF_VERSION', '1.0' );
}
if ( ! defined( 'BPAGPF_PLUGIN_URL' ) ) {
define( 'BPAGPF_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'BPAGPF_PLUGIN_DIR' ) ) {
define( 'BPAGPF_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
}
/**
* Load the plugin text domain.
*
* @since 1.0
*/
private function load_plugin_textdomain() {
load_plugin_textdomain( 'bp_admin_global_profile_fields', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Enqueue admin scripts.
*
* @since 1.0
*/
public function enqueue_scripts() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$jssrc = plugins_url( 'js/script' . $min . '.js', __FILE__ );
$csssrc = plugins_url( 'css/style' . $min . '.css', __FILE__ );
$version = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? time() : BPAGPF_VERSION;
wp_register_script( 'bp_admin_global_profile_fields', $jssrc, array( 'jquery' ), $version, true );
wp_register_style( 'bp_admin_global_profile_fields', $csssrc, false, $version );
if ( ! empty( $_GET['page'] ) && false !== strpos( $_GET['page'], 'bp-profile-setup' ) ) {
wp_enqueue_script( 'bp_admin_global_profile_fields' );
wp_enqueue_style( 'bp_admin_global_profile_fields' );
}
}
/**
* Output the metabox for setting the field value on admin global fields.
*
* @since 1.0
*
* @param object $field
*
* @return void If default field
*/
private function value_metabox( $field ) {
// Default field cannot change type.
if ( ! empty( $field->id ) && 1 === (int) $field->id ) {
return;
} ?>
<div class="postbox" style="display:none;">
<h3><label for="fieldvalue"><?php esc_html_e( 'Value', 'bp_admin_global_profile_fields'); ?></label></h3>
<div class="inside">
<?php $field->type_obj->admin_field_html( array( 'name' => 'fieldvalue', 'id' => 'fieldvalue', 'value' => bp_xprofile_get_meta( $field->id, 'field', 'global_value' ) ) ); ?>
<small id="type-change-notice" style="display:none;">Note: The Field Type has changed, save the field and re-edit to render the Value field appropriately.</small>
</div>
</div>
<?php
}
/**
* Add the global fields value input to the xprofile edit screen.
*
* @since 1.0
*
* @param object $field
*/
public function bp_xprofile_global_field_value( $field ) {
// Output the field value metabox.
$this->value_metabox( $field );
}
/**
* Save the global field value.
*
* @since 1.0
*
* @param object $field
*/
public function bp_xprofile_save_global_field_value( $field ) {
if ( ! empty( $_POST['saveField'] ) ) {
if ( BP_XProfile_Field::admin_validate() ) {
$field_id = $field->id;
if ( empty( $field_id ) ) {
$field_id = BP_XProfile_Field::get_id_from_name( $field->name );
}
$this->__update_xprofile_meta( $field_id, 'field', 'global_value', $_POST['fieldvalue'] );
}
}
}
/**
* Add our global visibility level.
*
* @since 1.0
*
* @param array $levels
*
* @return array
*/
public function global_visibility_level( $levels ) {
$levels['global'] = array(
'id' => 'global',
'label' => __( 'Global', 'bp_admin_global_profile_fields' )
);
return $levels;
}
/**
* Append 'global' to the visibility levels for all users.
*
* @since 1.0
*
* @param array $hidden_levels
* @param int $displayed_user_id
* @param int $current_user_id
*
* @return array
*/
public function append_global_visibility_level( $hidden_levels, $displayed_user_id, $current_user_id ) {
if ( bp_is_user_profile_edit() ) {
// Editing profile
$hidden_levels[] = 'global';
}
return $hidden_levels;
}
/**
* Filter 'global' visibility levels from radio buttons.
*
* @since 1.0
*
* @param string $retval
* @param array $request
* @param array $args
*
* @return array
*/
public function filter_global_visibility_from_radio_buttons( $retval, $r, $args ) {
// Empty return value, filled in below if a valid field ID is found
$retval = '';
// Only do-the-do if there's a valid field ID
if ( ! empty( $r['field_id'] ) ) :
// Start the output buffer
ob_start();
// Output anything before
echo $r['before']; ?>
<?php if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?>
<?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?>
<?php if ( $level['id'] != 'global' ) : ?>
<?php printf( $r['before_radio'], esc_attr( $level['id'] ) ); ?>
<label for="<?php echo esc_attr( 'see-field_' . $r['field_id'] . '_' . $level['id'] ); ?>">
<input type="radio" id="<?php echo esc_attr( 'see-field_' . $r['field_id'] . '_' . $level['id'] ); ?>" name="<?php echo esc_attr( 'field_' . $r['field_id'] . '_visibility' ); ?>" value="<?php echo esc_attr( $level['id'] ); ?>" <?php checked( $level['id'], bp_get_the_profile_field_visibility_level() ); ?> />
<span class="field-visibility-text"><?php echo esc_html( $level['label'] ); ?></span>
</label>
<?php echo $r['after_radio']; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif;
// Output anything after
echo $r['after'];
// Get the output buffer and empty it
$retval = ob_get_clean();
endif;
return $retval;
}
/**
* Display the global fields on the users public profile.
*
* @since 1.0
*
*/
public function display_global_profile_fields() {
global $profile_template;
$this->write_log($profile_template);
if ($profile_template->field->visibility_level == 'global') {
$this->write_log($profile_template->field->visibility_level);
$global_field_value = bp_xprofile_get_meta( $profile_template->field->id, 'field', 'global_value' );
if ( ! empty( $global_field_value ) || ( '0' === $global_field_value ) ) {
$value = maybe_unserialize( $global_field_value );
} else {
$value = false;
}
if ( ! empty( $value ) || ( '0' === $value ) ) { ?>
<tr<?php bp_field_css_class(); ?>>
<td class="label"><?php bp_the_profile_field_name(); ?></td>
<td class="data"><?php echo $value; ?></td>
</tr>
<?php }
}
}
/**
* Update has_profile args to not filter the empty fields
*
* @since 1.0
*
* @param array $r
*
* @return array
*/
public function show_global_fields( $r ) {
$r['hide_empty_fields'] = false;
return $r;
}
/**
* Helper function for handling xProfile Meta
*
* @since 1.0
*
* @param int $object_id
* @param string $object_type
* @param string $meta_key
* @param string $meta_value
*
*/
private function __update_xprofile_meta( $object_id, $object_type, $meta_key, $meta_value = '' ) {
if ( empty( $meta_value ) || ! $meta_value ) {
bp_xprofile_delete_meta( $object_id, $object_type, $meta_key );
} elseif ( ! bp_xprofile_get_meta( $object_id, $object_type, $meta_key ) ) {
bp_xprofile_add_meta( $object_id, $object_type, $meta_key, $meta_value );
} else {
bp_xprofile_update_meta( $object_id, $object_type, $meta_key, $meta_value );
}
}
private function write_log ( $log ) {
if ( true === WP_DEBUG ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
}
$bp_admin_global_profile_fields = BP_Admin_Global_Profile_Fields::get_instance();