[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress XProfile Classes. 4 * 5 * @package BuddyPress 6 * @subpackage XProfileClasses 7 * @since 8.0.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * Checkbox Acceptance xProfile field type. 15 * 16 * @since 8.0.0 17 */ 18 class BP_XProfile_Field_Type_Checkbox_Acceptance extends BP_XProfile_Field_Type { 19 20 /** 21 * Checkbox Acceptance field's visibility setting. 22 * 23 * Defaults to 'adminsonly'. This property enforces Field's default visibility. 24 * 25 * @since 8.0.0 26 * 27 * @return string The Checkbox Acceptance field's visibility setting. 28 */ 29 public $visibility = 'adminsonly'; 30 31 /** 32 * Supported features for the Checkbox Acceptance field type. 33 * 34 * @since 8.0.0 35 * @var bool[] The WordPress field supported features. 36 */ 37 public static $supported_features = array( 38 'switch_fieldtype' => false, 39 'required' => true, 40 'do_autolink' => false, 41 'allow_custom_visibility' => false, 42 'member_types' => false, 43 ); 44 45 /** 46 * Constructor for the Checkbox Acceptance field type. 47 * 48 * @since 8.0.0 49 */ 50 public function __construct() { 51 parent::__construct(); 52 53 $this->name = _x( 'Checkbox Acceptance', 'xprofile field type', 'buddypress' ); 54 $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' ); 55 56 $this->supports_options = false; 57 $this->do_settings_section = true; 58 $this->accepts_null_value = false; 59 60 $this->set_format( '/^.+$/', 'replace' ); 61 62 /** 63 * Fires inside __construct() method for bp_xprofile_field_type_checkbox_acceptance class. 64 * 65 * @since 8.0.0 66 * 67 * @param BP_XProfile_Field_Type_Checkbox_Acceptance $this Current instance of the Checkbox Acceptance field type. 68 */ 69 do_action( 'bp_xprofile_field_type_checkbox_acceptance', $this ); 70 71 // Make sure it's not possible to edit an accepted Checkbox Acceptance field. 72 add_filter( 'bp_xprofile_set_field_data_pre_validate', array( $this, 'enforce_field_value' ), 10, 2 ); 73 } 74 75 76 /** 77 * Output the edit field HTML for this field type. 78 * 79 * Must be used inside the {@link bp_profile_fields()} template loop. 80 * 81 * @since 8.0.0 82 * 83 * @param array $raw_properties Optional key/value array of 84 * {@link http://dev.w3.org/html5/markup/textarea.html permitted attributes} 85 * that you want to add. 86 */ 87 public function edit_field_html( array $raw_properties = array() ) { 88 $user_id = bp_displayed_user_id(); 89 $required = false; 90 $default_r = array(); 91 92 if ( isset( $raw_properties['user_id'] ) ) { 93 $user_id = (int) $raw_properties['user_id']; 94 unset( $raw_properties['user_id'] ); 95 } 96 97 if ( bp_get_the_profile_field_is_required() ) { 98 $default_r['required'] = 'required'; // HTML5 required attribute. 99 $required = true; 100 } 101 102 $r = bp_parse_args( 103 $raw_properties, 104 $default_r, 105 'checkbox_acceptance' 106 ); 107 ?> 108 <legend> 109 <?php bp_the_profile_field_name(); ?> 110 <?php bp_the_profile_field_required_label(); ?> 111 </legend> 112 113 <?php 114 /** This action is documented in bp-xprofile/bp-xprofile-classes */ 115 do_action( bp_get_the_profile_field_errors_action() ); 116 117 $r['user_id'] = $user_id; 118 bp_the_profile_field_options( $r ); 119 ?> 120 121 <?php if ( bp_get_the_profile_field_description() ) : ?> 122 <p class="description" tabindex="0"><?php bp_the_profile_field_description(); ?></p> 123 <?php endif; 124 } 125 126 /** 127 * Field html for Admin-> User->Profile Fields screen. 128 * 129 * @since 8.0.0 130 * 131 * @param array $raw_properties properties. 132 */ 133 public function admin_field_html( array $raw_properties = array() ) { 134 $page_id = bp_xprofile_get_meta( bp_get_the_profile_field_id(), 'field', 'bp_xprofile_checkbox_acceptance_page', true ); 135 $page = null; 136 $default_r = array( 'type' => 'checkbox' ); 137 138 if ( bp_get_the_profile_field_is_required() ) { 139 $default_r['required'] = 'required'; // HTML5 required attribute. 140 } 141 142 $r = bp_parse_args( 143 $raw_properties, 144 $default_r, 145 'checkbox_acceptance' 146 ); 147 148 if ( $page_id ) { 149 $page = get_post( $page_id ); 150 } 151 ?> 152 153 <?php if ( $page instanceof WP_Post ) : ?> 154 <label for="<?php bp_the_profile_field_input_name(); ?>"> 155 <input <?php echo $this->get_edit_field_html_elements( $r ); ?>> 156 <?php 157 printf( 158 /* translators: %s: link to the page the user needs to accept the terms of. */ 159 esc_html__( 'I agree to %s.', 'buddypress' ), 160 '<a href="' . esc_url( get_permalink( $page ) ) . '">' . esc_html( get_the_title( $page ) ) . '</a>' 161 ); 162 ?> 163 </label> 164 <?php endif; 165 } 166 167 /** 168 * Admin new field screen. 169 * 170 * @since 8.0.0 171 * 172 * @param BP_XProfile_Field $current_field Profile field object. 173 * @param string $control_type Control type. 174 */ 175 public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) { 176 $type = array_search( get_class( $this ), bp_xprofile_get_field_types() ); 177 178 if ( false === $type ) { 179 return; 180 } 181 182 $class = $current_field->type != $type ? 'display: none;' : ''; 183 $page_id = bp_xprofile_get_meta( $current_field->id, 'field', 'bp_xprofile_checkbox_acceptance_page', true ); 184 ?> 185 186 <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> 187 <h3><?php esc_html_e( 'Select the page the user needs to accept the terms of:', 'buddypress' ); ?></h3> 188 <div class="inside"> 189 <p> 190 <?php 191 echo wp_dropdown_pages( 192 array( 193 'name' => 'bp_xprofile_checkbox_acceptance_page', 194 'echo' => false, 195 'show_option_none' => __( '— Select —', 'buddypress' ), 196 'selected' => $page_id ? $page_id : false, 197 ) 198 ); 199 200 $page = null; 201 if ( $page_id ) { 202 $page = get_post( $page_id ); 203 } 204 ?> 205 206 <?php if ( $page instanceof WP_Post ) : ?> 207 208 <a href="<?php echo esc_url( get_permalink( $page ) ); ?>" class="button-secondary" target="_bp"> 209 <?php esc_html_e( 'View', 'buddypress' ); ?> <span class="dashicons dashicons-external" aria-hidden="true" style="vertical-align: text-bottom;"></span> 210 <span class="screen-reader-text"><?php esc_html_e( '(opens in a new tab)', 'buddypress' ); ?></span> 211 </a> 212 213 <?php endif; ?> 214 </p> 215 </div> 216 </div> 217 <?php 218 } 219 220 /** 221 * Save settings from the field edit screen in the Dashboard. 222 * 223 * @since 8.0.0 224 * 225 * @param int $field_id ID of the field. 226 * @param array $settings Array of settings. 227 * @return bool True on success. 228 */ 229 public function admin_save_settings( $field_id, $settings ) { 230 if ( isset( $_POST['bp_xprofile_checkbox_acceptance_page'] ) ) { 231 bp_xprofile_update_meta( $field_id, 'field', 'bp_xprofile_checkbox_acceptance_page', absint( wp_unslash( $_POST['bp_xprofile_checkbox_acceptance_page'] ) ) ); 232 } 233 234 return true; 235 } 236 237 /** 238 * Profile edit/register options html. 239 * 240 * @since 8.0.0 241 * 242 * @param array $args args. 243 */ 244 public function edit_field_options_html( array $args = array() ) { 245 $field_id = (int) $this->field_obj->id; 246 $params = bp_parse_args( 247 $args, 248 array( 249 'user_id' => bp_displayed_user_id(), 250 ) 251 ); 252 $checkbox_acceptance = (int) maybe_unserialize( \BP_XProfile_ProfileData::get_value_byid( $field_id, $params['user_id'] ) ); 253 254 if ( ! empty( $_POST[ 'field_' . $field_id ] ) ) { 255 $new_checkbox_acceptance = (int) wp_unslash( $_POST[ 'field_' . $field_id ] ); 256 257 if ( $checkbox_acceptance !== $new_checkbox_acceptance ) { 258 $checkbox_acceptance = $new_checkbox_acceptance; 259 } 260 } 261 262 $r = array( 263 'type' => 'checkbox', 264 'name' => bp_get_the_profile_field_input_name(), 265 'id' => bp_get_the_profile_field_input_name(), 266 'value' => 1, 267 'class' => 'checkbox-acceptance', 268 ); 269 270 if ( bp_get_the_profile_field_is_required() ) { 271 $r['required'] = 'required'; // HTML5 required attribute. 272 } 273 274 if ( 1 === $checkbox_acceptance ) { 275 $r['checked'] = 'checked'; 276 $r['readonly'] = 'readonly'; 277 $r['onclick'] = 'return false;'; 278 } 279 280 $page_id = bp_xprofile_get_meta( $field_id, 'field', 'bp_xprofile_checkbox_acceptance_page', true ); 281 $page = null; 282 $html = ''; 283 284 if ( $page_id ) { 285 $page = get_post( $page_id ); 286 } 287 288 if ( $page instanceof WP_Post ) { 289 $html = sprintf( 290 '<div class="bp-xprofile-checkbox-acceptance-field"><input %1$s />%2$s</div>', 291 $this->get_edit_field_html_elements( $r ), 292 sprintf( 293 /* translators: %s: link to the page the user needs to accept the terms of. */ 294 esc_html__( 'I agree to %s.', 'buddypress' ), 295 '<a href="' . esc_url( get_permalink( $page ) ) . '">' . esc_html( get_the_title( $page ) ) . '</a>' 296 ) 297 ); 298 } 299 300 /** 301 * Filter here to edit the HTML output. 302 * 303 * @since 8.0.0 304 * 305 * @param string $html The HTML output. 306 * @param int $field_id The field ID. 307 * @param array $r The edit field HTML elements data. 308 * @param int $checkbox_acceptance The field value. 309 */ 310 echo apply_filters( 'bp_get_the_profile_field_checkbox_acceptance', $html, $field_id, $checkbox_acceptance ); 311 } 312 313 /** 314 * Enforces the field value if it has been already accepted. 315 * 316 * As it's always possible to edit HTML source and remove the `readonly="readonly"` attribute 317 * of the checkbox, we may need to enforce the field value. 318 * 319 * @since 8.0.0 320 * 321 * @param mixed $value Value passed to xprofile_set_field_data(). 322 * @param BP_XProfile_Field $field Field object. 323 * @return mixed The field value. 324 */ 325 public function enforce_field_value( $value, BP_XProfile_Field $field ) { 326 if ( 'checkbox_acceptance' === $field->type && 1 !== (int) $value && 1 === (int) $field->data->value ) { 327 $value = 1; 328 } 329 330 return $value; 331 } 332 333 /** 334 * Check if field is valid? 335 * 336 * @since 8.0.0 337 * 338 * @param string|int $values value. 339 * @return bool 340 */ 341 public function is_valid( $value ) { 342 if ( empty( $value ) || 1 === (int) $value ) { 343 return true; 344 } 345 346 return false; 347 } 348 349 /** 350 * Modify the appearance of value. 351 * 352 * @since 8.0.0 353 * 354 * @param string $field_value Original value of field. 355 * @param int $field_id field id. 356 * 357 * @return string Value formatted 358 */ 359 public static function display_filter( $field_value, $field_id = 0 ) { 360 $page_id = bp_xprofile_get_meta( $field_id, 'field', 'bp_xprofile_checkbox_acceptance_page', true ); 361 $page = null; 362 $html = esc_html__( 'No', 'buddypress' ); 363 364 /* translators: %s: link to the page the user needs to accept the terms of. */ 365 $acceptance_text = esc_html__( 'I did not agree to %s', 'buddypress' ); 366 367 if ( $page_id ) { 368 $page = get_post( $page_id ); 369 } 370 371 if ( ! empty( $field_value ) ) { 372 $html = esc_html__( 'Yes', 'buddypress' ); 373 374 /* translators: %s: link to the page the user needs to accept the terms of. */ 375 $acceptance_text = esc_html__( 'I agreed to %s.', 'buddypress' ); 376 } 377 378 if ( $page instanceof WP_Post ) { 379 $html = sprintf( 380 $acceptance_text, 381 '<a href="' . esc_url( get_permalink( $page ) ) . '">' . esc_html( get_the_title( $page ) ) . '</a>' 382 ); 383 } 384 385 return $html; 386 } 387 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:54 2024 | Cross-referenced by PHPXref 0.7.1 |