false, 'required' => true, 'do_autolink' => false, 'allow_custom_visibility' => false, 'member_types' => false, ); /** * Constructor for the Checkbox Acceptance field type. * * @since 8.0.0 */ public function __construct() { parent::__construct(); $this->name = _x( 'Checkbox Acceptance', 'xprofile field type', 'buddypress' ); $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' ); $this->supports_options = false; $this->do_settings_section = true; $this->accepts_null_value = false; $this->set_format( '/^.+$/', 'replace' ); /** * Fires inside __construct() method for bp_xprofile_field_type_checkbox_acceptance class. * * @since 8.0.0 * * @param BP_XProfile_Field_Type_Checkbox_Acceptance $this Current instance of the Checkbox Acceptance field type. */ do_action( 'bp_xprofile_field_type_checkbox_acceptance', $this ); // Make sure it's not possible to edit an accepted Checkbox Acceptance field. add_filter( 'bp_xprofile_set_field_data_pre_validate', array( $this, 'enforce_field_value' ), 10, 2 ); } /** * Output the edit field HTML for this field type. * * Must be used inside the {@link bp_profile_fields()} template loop. * * @since 8.0.0 * * @param array $raw_properties Optional key/value array of * {@link http://dev.w3.org/html5/markup/textarea.html permitted attributes} * that you want to add. */ public function edit_field_html( array $raw_properties = array() ) { $user_id = bp_displayed_user_id(); $required = false; $default_r = array(); if ( isset( $raw_properties['user_id'] ) ) { $user_id = (int) $raw_properties['user_id']; unset( $raw_properties['user_id'] ); } if ( bp_get_the_profile_field_is_required() ) { $default_r['required'] = 'required'; // HTML5 required attribute. $required = true; } $r = bp_parse_args( $raw_properties, $default_r, 'checkbox_acceptance' ); ?>

User->Profile Fields screen. * * @since 8.0.0 * * @param array $raw_properties properties. */ public function admin_field_html( array $raw_properties = array() ) { $page_id = bp_xprofile_get_meta( bp_get_the_profile_field_id(), 'field', 'bp_xprofile_checkbox_acceptance_page', true ); $page = null; $default_r = array( 'type' => 'checkbox' ); if ( bp_get_the_profile_field_is_required() ) { $default_r['required'] = 'required'; // HTML5 required attribute. } $r = bp_parse_args( $raw_properties, $default_r, 'checkbox_acceptance' ); if ( $page_id ) { $page = get_post( $page_id ); } ?> type != $type ? 'display: none;' : ''; $page_id = bp_xprofile_get_meta( $current_field->id, 'field', 'bp_xprofile_checkbox_acceptance_page', true ); ?>

'bp_xprofile_checkbox_acceptance_page', 'echo' => false, 'show_option_none' => __( '— Select —', 'buddypress' ), 'selected' => $page_id ? $page_id : false, ) ); $page = null; if ( $page_id ) { $page = get_post( $page_id ); } ?>

field_obj->id; $params = bp_parse_args( $args, array( 'user_id' => bp_displayed_user_id(), ) ); $checkbox_acceptance = (int) maybe_unserialize( \BP_XProfile_ProfileData::get_value_byid( $field_id, $params['user_id'] ) ); if ( ! empty( $_POST[ 'field_' . $field_id ] ) ) { $new_checkbox_acceptance = (int) wp_unslash( $_POST[ 'field_' . $field_id ] ); if ( $checkbox_acceptance !== $new_checkbox_acceptance ) { $checkbox_acceptance = $new_checkbox_acceptance; } } $r = array( 'type' => 'checkbox', 'name' => bp_get_the_profile_field_input_name(), 'id' => bp_get_the_profile_field_input_name(), 'value' => 1, 'class' => 'checkbox-acceptance', ); if ( bp_get_the_profile_field_is_required() ) { $r['required'] = 'required'; // HTML5 required attribute. } if ( 1 === $checkbox_acceptance ) { $r['checked'] = 'checked'; $r['readonly'] = 'readonly'; $r['onclick'] = 'return false;'; } $page_id = bp_xprofile_get_meta( $field_id, 'field', 'bp_xprofile_checkbox_acceptance_page', true ); $page = null; $html = ''; if ( $page_id ) { $page = get_post( $page_id ); } if ( $page instanceof WP_Post ) { $html = sprintf( '
%2$s
', $this->get_edit_field_html_elements( $r ), sprintf( /* translators: %s: link to the page the user needs to accept the terms of. */ esc_html__( 'I agree to %s.', 'buddypress' ), '' . esc_html( get_the_title( $page ) ) . '' ) ); } /** * Filter here to edit the HTML output. * * @since 8.0.0 * * @param string $html The HTML output. * @param int $field_id The field ID. * @param array $r The edit field HTML elements data. * @param int $checkbox_acceptance The field value. */ echo apply_filters( 'bp_get_the_profile_field_checkbox_acceptance', $html, $field_id, $checkbox_acceptance ); } /** * Enforces the field value if it has been already accepted. * * As it's always possible to edit HTML source and remove the `readonly="readonly"` attribute * of the checkbox, we may need to enforce the field value. * * @since 8.0.0 * * @param mixed $value Value passed to xprofile_set_field_data(). * @param BP_XProfile_Field $field Field object. * @return mixed The field value. */ public function enforce_field_value( $value, BP_XProfile_Field $field ) { if ( 'checkbox_acceptance' === $field->type && 1 !== (int) $value && 1 === (int) $field->data->value ) { $value = 1; } return $value; } /** * Check if field is valid? * * @since 8.0.0 * * @param string|int $values value. * @return bool */ public function is_valid( $value ) { if ( empty( $value ) || 1 === (int) $value ) { return true; } return false; } /** * Modify the appearance of value. * * @since 8.0.0 * * @param string $field_value Original value of field. * @param int $field_id field id. * * @return string Value formatted */ public static function display_filter( $field_value, $field_id = 0 ) { $page_id = bp_xprofile_get_meta( $field_id, 'field', 'bp_xprofile_checkbox_acceptance_page', true ); $page = null; $html = esc_html__( 'No', 'buddypress' ); /* translators: %s: link to the page the user needs to accept the terms of. */ $acceptance_text = esc_html__( 'I did not agree to %s', 'buddypress' ); if ( $page_id ) { $page = get_post( $page_id ); } if ( ! empty( $field_value ) ) { $html = esc_html__( 'Yes', 'buddypress' ); /* translators: %s: link to the page the user needs to accept the terms of. */ $acceptance_text = esc_html__( 'I agreed to %s.', 'buddypress' ); } if ( $page instanceof WP_Post ) { $html = sprintf( $acceptance_text, '' . esc_html( get_the_title( $page ) ) . '' ); } return $html; } }