| 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 = wp_parse_args( $args, array( 'user_id' => bp_displayed_user_id() ) ); |
| 247 | $checkbox_acceptance = (int) maybe_unserialize( \BP_XProfile_ProfileData::get_value_byid( $field_id, $params['user_id'] ) ); |
| 248 | |
| 249 | if ( ! empty( $_POST[ 'field_' . $field_id ] ) ) { |
| 250 | $new_checkbox_acceptance = (int) wp_unslash( $_POST[ 'field_' . $field_id ] ); |
| 251 | |
| 252 | if ( $checkbox_acceptance !== $new_checkbox_acceptance ) { |
| 253 | $checkbox_acceptance = $new_checkbox_acceptance; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | $r = array( |
| 258 | 'type' => 'checkbox', |
| 259 | 'name' => bp_get_the_profile_field_input_name(), |
| 260 | 'id' => bp_get_the_profile_field_input_name(), |
| 261 | 'value' => 1, |
| 262 | 'class' => 'checkbox-acceptance', |
| 263 | ); |
| 264 | |
| 265 | if ( bp_get_the_profile_field_is_required() ) { |
| 266 | $r['required'] = 'required'; // HTML5 required attribute. |
| 267 | } |
| 268 | |
| 269 | if ( 1 === $checkbox_acceptance ) { |
| 270 | $r['checked'] = 'checked'; |
| 271 | $r['readonly'] = 'readonly'; |
| 272 | $r['onclick'] = 'return false;'; |
| 273 | } |
| 274 | |
| 275 | $page_id = bp_xprofile_get_meta( $field_id, 'field', 'bp_xprofile_checkbox_acceptance_page', true ); |
| 276 | $page = null; |
| 277 | $html = ''; |
| 278 | |
| 279 | if ( $page_id ) { |
| 280 | $page = get_post( $page_id ); |
| 281 | } |
| 282 | |
| 283 | if ( $page instanceof WP_Post ) { |
| 284 | $html = sprintf( |
| 285 | '<div class="bp-xprofile-checkbox-acceptance-field"><input %1$s />%2$s</div>', |
| 286 | $this->get_edit_field_html_elements( $r ), |
| 287 | sprintf( |
| 288 | /* translators: %s: link to the page the user needs to accept the terms of. */ |
| 289 | esc_html__( 'I agree to %s.', 'buddypress' ), |
| 290 | '<a href="' . esc_url( get_permalink( $page ) ) . '">' . esc_html( get_the_title( $page ) ) . '</a>' |
| 291 | ) |
| 292 | ); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Filter here to edit the HTML output. |
| 297 | * |
| 298 | * @since 8.0.0 |
| 299 | * |
| 300 | * @param string $html The HTML output. |
| 301 | * @param int $field_id The field ID. |
| 302 | * @param array $r The edit field HTML elements data. |
| 303 | * @param int $checkbox_acceptance The field value. |
| 304 | */ |
| 305 | echo apply_filters( 'bp_get_the_profile_field_checkbox_acceptance', $html, $field_id, $checkbox_acceptance ); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Enforces the field value if it has been already accepted. |
| 310 | * |
| 311 | * As it's always possible to edit HTML source and remove the `readonly="readonly"` attribute |
| 312 | * of the checkbox, we may need to enforce the field value. |
| 313 | * |
| 314 | * @since 8.0.0 |
| 315 | * |
| 316 | * @param mixed $value Value passed to xprofile_set_field_data(). |
| 317 | * @param BP_XProfile_Field $field Field object. |
| 318 | * @return mixed The field value. |
| 319 | */ |
| 320 | public function enforce_field_value( $value, BP_XProfile_Field $field ) { |
| 321 | if ( 'checkbox_acceptance' === $field->type && 1 !== (int) $value && 1 === (int) $field->data->value ) { |
| 322 | $value = 1; |
| 323 | } |
| 324 | |
| 325 | return $value; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Check if field is valid? |
| 330 | * |
| 331 | * @since 8.0.0 |
| 332 | * |
| 333 | * @param string|int $values value. |
| 334 | * @return bool |
| 335 | */ |
| 336 | public function is_valid( $value ) { |
| 337 | if ( empty( $value ) || 1 === (int) $value ) { |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Modify the appearance of value. |
| 346 | * |
| 347 | * @since 8.0.0 |
| 348 | * |
| 349 | * @param string $field_value Original value of field. |
| 350 | * @param int $field_id field id. |
| 351 | * |
| 352 | * @return string Value formatted |
| 353 | */ |
| 354 | public static function display_filter( $field_value, $field_id = 0 ) { |
| 355 | $page_id = bp_xprofile_get_meta( $field_id, 'field', 'bp_xprofile_checkbox_acceptance_page', true ); |
| 356 | $page = null; |
| 357 | $html = esc_html__( 'No', 'buddypress' ); |
| 358 | |
| 359 | /* translators: %s: link to the page the user needs to accept the terms of. */ |
| 360 | $acceptance_text = esc_html__( 'I did not agree to %s', 'buddypress' ); |
| 361 | |
| 362 | if ( $page_id ) { |
| 363 | $page = get_post( $page_id ); |
| 364 | } |
| 365 | |
| 366 | if ( ! empty( $field_value ) ) { |
| 367 | $html = esc_html__( 'Yes', 'buddypress' ); |
| 368 | |
| 369 | /* translators: %s: link to the page the user needs to accept the terms of. */ |
| 370 | $acceptance_text = esc_html__( 'I agreed to %s.', 'buddypress' ); |
| 371 | } |
| 372 | |
| 373 | if ( $page instanceof WP_Post ) { |
| 374 | $html = sprintf( |
| 375 | $acceptance_text, |
| 376 | '<a href="' . esc_url( get_permalink( $page ) ) . '">' . esc_html( get_the_title( $page ) ) . '</a>' |
| 377 | ); |
| 378 | } |
| 379 | |
| 380 | return $html; |
| 381 | } |
| 382 | } |