| 1 | <?php |
| 2 | /** |
| 3 | * BuddyPress XProfile Classes. |
| 4 | * |
| 5 | * @package BuddyPress |
| 6 | * @subpackage XProfileClasses |
| 7 | * @since 8.0.0 |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | // Exit if accessed directly. |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Tos field. |
| 16 | */ |
| 17 | class BP_XProfile_Field_Type_Checkbox_Acceptance extends BP_XProfile_Field_Type { |
| 18 | |
| 19 | /** |
| 20 | * Constructor for the textarea field type. |
| 21 | * |
| 22 | * @since 8.0.0 |
| 23 | */ |
| 24 | public function __construct() { |
| 25 | |
| 26 | parent::__construct(); |
| 27 | |
| 28 | $this->name = _x( 'Checkbox Acceptance', 'xprofile field type', 'buddypress' ); |
| 29 | $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' ); |
| 30 | |
| 31 | $this->supports_options = false; |
| 32 | $this->do_settings_section = true; |
| 33 | |
| 34 | $this->set_format( '/^.+$/', 'replace' ); |
| 35 | |
| 36 | /** |
| 37 | * Fires inside __construct() method for bp_xprofile_field_type_checkbox_acceptance class. |
| 38 | * |
| 39 | * @since 8.0.0 |
| 40 | * |
| 41 | * @param bp_xprofile_field_type_checkbox_acceptance $this Current instance of |
| 42 | * the field type Checkbox Acceptance. |
| 43 | */ |
| 44 | do_action( 'bp_xprofile_field_type_checkbox_acceptance', $this ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Output the edit field HTML for this field type. |
| 50 | * |
| 51 | * Must be used inside the {@link bp_profile_fields()} template loop. |
| 52 | * |
| 53 | * @since 8.0.0 |
| 54 | * |
| 55 | * @param array $raw_properties Optional key/value array of |
| 56 | * {@link http://dev.w3.org/html5/markup/textarea.html permitted attributes} |
| 57 | * that you want to add. |
| 58 | */ |
| 59 | public function edit_field_html( array $raw_properties = array() ) { |
| 60 | |
| 61 | $user_id = bp_displayed_user_id(); |
| 62 | |
| 63 | if ( isset( $raw_properties['user_id'] ) ) { |
| 64 | $user_id = (int) $raw_properties['user_id']; |
| 65 | unset( $raw_properties['user_id'] ); |
| 66 | } |
| 67 | |
| 68 | // HTML5 required attribute. |
| 69 | if ( bp_get_the_profile_field_is_required() ) { |
| 70 | $raw_properties['required'] = 'required'; |
| 71 | $required = true; |
| 72 | } else { |
| 73 | $required = false; |
| 74 | } |
| 75 | ?> |
| 76 | <legend id="<?php bp_the_profile_field_input_name(); ?>-1"> |
| 77 | <?php bp_the_profile_field_name(); ?> |
| 78 | <?php bp_the_profile_field_required_label(); ?> |
| 79 | </legend> |
| 80 | |
| 81 | <?php do_action( bp_get_the_profile_field_errors_action() ); ?> |
| 82 | |
| 83 | <?php bp_the_profile_field_options( "user_id={$user_id}&required={$required}" ); ?> |
| 84 | |
| 85 | <?php if ( bp_get_the_profile_field_description() ) : ?> |
| 86 | <p class="description" |
| 87 | id="<?php bp_the_profile_field_input_name(); ?>-3"><?php bp_the_profile_field_description(); ?></p> |
| 88 | <?php endif; ?> |
| 89 | |
| 90 | <?php |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Field html for Admin-> User->Profile Fields screen. |
| 95 | * |
| 96 | * @param array $raw_properties properties. |
| 97 | * |
| 98 | * @since 8.0.0 |
| 99 | */ |
| 100 | public function admin_field_html( array $raw_properties = array() ) { |
| 101 | global $field; |
| 102 | |
| 103 | $text = wp_kses_data( self::get_content( $field ) ); |
| 104 | |
| 105 | $html = $this->get_edit_field_html_elements( array_merge( |
| 106 | array( 'type' => 'checkbox' ), |
| 107 | $raw_properties |
| 108 | ) ); |
| 109 | ?> |
| 110 | <label for="<?php bp_the_profile_field_input_name(); ?>"> |
| 111 | <input <?php echo $html; ?>> |
| 112 | <?php echo $text; ?> |
| 113 | </label> |
| 114 | <?php |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Admin new field screen. |
| 119 | * |
| 120 | * @param BP_XProfile_Field $current_field profile field object. |
| 121 | * @param string $control_type type. |
| 122 | * |
| 123 | * @since 8.0.0 |
| 124 | */ |
| 125 | public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) { |
| 126 | |
| 127 | $type = array_search( get_class( $this ), bp_xprofile_get_field_types() ); |
| 128 | |
| 129 | if ( false === $type ) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | $class = $current_field->type != $type ? 'display: none;' : ''; |
| 134 | $text = self::get_content( $current_field ); |
| 135 | |
| 136 | ?> |
| 137 | <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" |
| 138 | style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> |
| 139 | <h3><?php esc_html_e( 'Use this field to write a text that should be displayed beside the checkbox:', 'buddypress' ); ?></h3> |
| 140 | <div class="inside"> |
| 141 | <p> |
| 142 | <?php |
| 143 | $settings = array( |
| 144 | 'media_buttons' => false, |
| 145 | 'wpautop' => false, |
| 146 | 'drag_drop_upload' => false, |
| 147 | 'textarea_name' => 'bp_xprofile_tos_content', |
| 148 | 'textarea_rows' => 4, |
| 149 | 'quicktags' => false, |
| 150 | 'tinymce' => array( |
| 151 | 'toolbar1' => 'bold, italic, underline, link', |
| 152 | 'toolbar2' => '', |
| 153 | 'toolbar3' => '' |
| 154 | ) |
| 155 | ); |
| 156 | wp_editor( stripslashes($text), 'bp_xprofile_tos_content', $settings ); |
| 157 | ?> |
| 158 | </p> |
| 159 | </div> |
| 160 | </div> |
| 161 | <?php |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Save settings from the field edit screen in the Dashboard. |
| 166 | * |
| 167 | * @param int $field_id ID of the field. |
| 168 | * @param array $settings Array of settings. |
| 169 | * @return bool True on success. |
| 170 | * |
| 171 | * @since 8.0.0 |
| 172 | */ |
| 173 | public function admin_save_settings( $field_id, $settings ) { |
| 174 | |
| 175 | if ( isset( $_POST['bp_xprofile_tos_content'] ) ) { |
| 176 | bp_xprofile_update_meta( $field_id, 'field', 'bp_xprofile_tos_content', wp_kses_post($_POST['bp_xprofile_tos_content']) ); |
| 177 | } |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Profile edit/register options html. |
| 184 | * |
| 185 | * @param array $args args. |
| 186 | * |
| 187 | * @since 8.0.0 |
| 188 | */ |
| 189 | public function edit_field_options_html( array $args = array() ) { |
| 190 | global $field; |
| 191 | |
| 192 | $checkbox_acceptance = maybe_unserialize( \BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ) ); |
| 193 | |
| 194 | if ( ! empty( $_POST[ 'field_' . $this->field_obj->id ] ) ) { |
| 195 | $new_checkbox_acceptance = $_POST[ 'field_' . $this->field_obj->id ]; |
| 196 | $checkbox_acceptance = ( $checkbox_acceptance != $new_checkbox_acceptance ) ? $new_checkbox_acceptance : $checkbox_acceptance; |
| 197 | } |
| 198 | |
| 199 | $checkbox_acceptance = absint( $checkbox_acceptance ); |
| 200 | |
| 201 | $atts = array( |
| 202 | 'type' => 'checkbox', |
| 203 | 'name' => bp_get_the_profile_field_input_name(), |
| 204 | 'id' => bp_get_the_profile_field_input_name(), |
| 205 | 'value' => 1, |
| 206 | 'class' => 'bp-xprofile-tos-checkbox', |
| 207 | ); |
| 208 | |
| 209 | if ( $checkbox_acceptance == 1 ) { |
| 210 | $atts['checked'] = "checked"; |
| 211 | } |
| 212 | |
| 213 | $html = '<div class="bp-xprofile-checkbox-acceptance-field"><input ' . $this->get_edit_field_html_elements( $atts ) . ' />'; |
| 214 | // we should most probably avoid kses on output. |
| 215 | $html .= wp_kses_post( str_replace( array('<p>','</p>'), array('', ''), self::get_content( $field ) ) ); |
| 216 | |
| 217 | $html .= "</div>"; |
| 218 | echo apply_filters( 'bp_get_the_profile_field_checkbox_acceptance', $html, $args['type'], $this->field_obj->id, $checkbox_acceptance ); |
| 219 | ?> |
| 220 | <?php |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Check if field is valid? |
| 225 | * |
| 226 | * @param string|int $values value. |
| 227 | * |
| 228 | * @since 8.0.0 |
| 229 | * |
| 230 | * @return bool |
| 231 | */ |
| 232 | public function is_valid( $values ) { |
| 233 | |
| 234 | if ( empty( $values ) || 1 == $values ) { |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Modify the appearance of value. |
| 243 | * |
| 244 | * @param string $field_value Original value of field. |
| 245 | * @param int $field_id field id. |
| 246 | * |
| 247 | * @return string Value formatted |
| 248 | * |
| 249 | * @since 8.0.0 |
| 250 | */ |
| 251 | public static function display_filter( $field_value, $field_id = 0 ) { |
| 252 | return empty( $field_value ) ? __( 'No', 'buddypress' ) : __( 'Yes', 'buddypress' ); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get the terms content. |
| 257 | * |
| 258 | * @param BP_XProfile_Field $field field object. |
| 259 | * |
| 260 | * @return string |
| 261 | * |
| 262 | * @since 8.0.0 |
| 263 | */ |
| 264 | private static function get_content( $field = null ) { |
| 265 | |
| 266 | if ( ! $field ) { |
| 267 | $field_id = bp_get_the_profile_field_id(); |
| 268 | } else { |
| 269 | $field_id = $field->id; |
| 270 | } |
| 271 | |
| 272 | if ( ! $field_id ) { |
| 273 | return ''; |
| 274 | } |
| 275 | |
| 276 | return bp_xprofile_get_meta( $field_id, 'field', 'bp_xprofile_tos_content', true ); |
| 277 | } |
| 278 | } |