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