diff --git src/bp-xprofile/bp-xprofile-functions.php src/bp-xprofile/bp-xprofile-functions.php
index 7cd35fa84..0a29b13ae 100644
|
|
|
function xprofile_set_field_data( $field, $user_id, $value, $is_required = false
|
| 454 | 454 | |
| 455 | 455 | // For certain fields, only certain parameters are acceptable, so add them to the list of allowed values. |
| 456 | 456 | if ( $field_type_obj->supports_options ) { |
| 457 | | $field_type_obj->set_whitelist_values( wp_list_pluck( $field->get_children(), 'name' ) ); |
| | 457 | $field_type_obj->set_allowed_values( wp_list_pluck( $field->get_children(), 'name' ) ); |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | 460 | // Check the value is in an accepted format for this form field. |
diff --git src/bp-xprofile/classes/class-bp-xprofile-field-type.php src/bp-xprofile/classes/class-bp-xprofile-field-type.php
index b3aaa49d0..d509d3dc7 100644
|
|
|
abstract class BP_XProfile_Field_Type {
|
| 31 | 31 | * @since 2.0.0 |
| 32 | 32 | * @var array Field type allowed values. |
| 33 | 33 | */ |
| 34 | | protected $validation_whitelist = array(); |
| | 34 | protected $validation_allowed_values = array(); |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * Name for field type. |
| … |
… |
abstract class BP_XProfile_Field_Type {
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
| 156 | | * Add a value to this type's whitelist that profile data will be asserted against. |
| 157 | | * |
| 158 | | * You can call this method multiple times to set multiple formats. When validation is performed, |
| 159 | | * it's successful as long as the new value matches any one of the registered formats. |
| | 156 | * Add a value to this type's list of allowed values that profile data will be asserted against. |
| 160 | 157 | * |
| 161 | 158 | * @since 2.0.0 |
| | 159 | * @deprecated 7.0.0 Use set_allowed_values() instead. |
| 162 | 160 | * |
| 163 | 161 | * @param string|array $values Whitelisted values. |
| 164 | 162 | * @return BP_XProfile_Field_Type |
| 165 | 163 | */ |
| 166 | 164 | public function set_whitelist_values( $values ) { |
| | 165 | _deprecated_function( __METHOD__, '7.0.0', 'BP_XProfile_Field_Type::set_allowed_values()' ); |
| | 166 | $this->set_allowed_values( $values ); |
| | 167 | } |
| | 168 | |
| | 169 | /** |
| | 170 | * Add a value to this type's list of allowed values that profile data will be asserted against. |
| | 171 | * |
| | 172 | * You can call this method multiple times to set multiple formats. When validation is performed, |
| | 173 | * it's successful as long as the new value matches any one of the registered formats. |
| | 174 | * |
| | 175 | * @since 7.0.0 |
| | 176 | * |
| | 177 | * @param string|array $values Allowed values. |
| | 178 | * @return BP_XProfile_Field_Type |
| | 179 | */ |
| | 180 | public function set_allowed_values( $values ) { |
| 167 | 181 | foreach ( (array) $values as $value ) { |
| 168 | 182 | |
| 169 | 183 | /** |
| 170 | | * Filters values for field type's whitelist that profile data will be asserted against. |
| | 184 | * Filters values for field type's list of allowed values that profile data will be asserted against. |
| 171 | 185 | * |
| 172 | 186 | * @since 2.0.0 |
| | 187 | * @deprecated 7.0.0 Use 'bp_xprofile_field_type_set_allowed_values' instead. |
| | 188 | * |
| | 189 | * @param string $value Field value. |
| | 190 | * @param array $values Original array of values. |
| | 191 | * @param BP_XProfile_Field_Type $this Current instance of the BP_XProfile_Field_Type class. |
| | 192 | */ |
| | 193 | $this->validation_allowed_values[] = apply_filters_deprecated( 'bp_xprofile_field_type_set_whitelist_values', array( $value, $values, $this ), '7.0.0', 'bp_xprofile_field_type_set_allowed_values' ); |
| | 194 | |
| | 195 | /** |
| | 196 | * Filters values for field type's list of allowed values that profile data will be asserted against. |
| | 197 | * |
| | 198 | * @since 7.0.0 |
| 173 | 199 | * |
| 174 | 200 | * @param string $value Field value. |
| 175 | 201 | * @param array $values Original array of values. |
| 176 | 202 | * @param BP_XProfile_Field_Type $this Current instance of the BP_XProfile_Field_Type class. |
| 177 | 203 | */ |
| 178 | | $this->validation_whitelist[] = apply_filters( 'bp_xprofile_field_type_set_whitelist_values', $value, $values, $this ); |
| | 204 | $this->validation_allowed_values[] = apply_filters( 'bp_xprofile_field_type_set_allowed_values', $value, $values, $this ); |
| 179 | 205 | } |
| 180 | 206 | |
| 181 | 207 | return $this; |
| … |
… |
abstract class BP_XProfile_Field_Type {
|
| 214 | 240 | $validated = true; |
| 215 | 241 | } |
| 216 | 242 | |
| 217 | | // If there's a whitelist set, make sure that each value is a whitelisted value. |
| 218 | | if ( ( true === $validated ) && ! empty( $values ) && ! empty( $this->validation_whitelist ) ) { |
| | 243 | // If there's a list of allowed values, make sure that each value is on that list. |
| | 244 | if ( ( true === $validated ) && ! empty( $values ) && ! empty( $this->validation_allowed_values ) ) { |
| 219 | 245 | foreach ( (array) $values as $value ) { |
| 220 | | if ( ! in_array( $value, $this->validation_whitelist, true ) ) { |
| | 246 | if ( ! in_array( $value, $this->validation_allowed_values, true ) ) { |
| 221 | 247 | $validated = false; |
| 222 | 248 | break; |
| 223 | 249 | } |
diff --git tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php
index 6b07a1124..06de7ffda 100644
|
|
|
class BP_Tests_XProfile_Field_Type extends BP_UnitTestCase {
|
| 24 | 24 | $this->assertTrue( $field->is_valid( 123 ) ); |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | | public function test_textbox_validate_whitelisted_string() { |
| | 27 | public function test_textbox_validate_allowed_string() { |
| 28 | 28 | $field = bp_xprofile_create_field_type( 'textbox' ); |
| 29 | 29 | |
| 30 | 30 | $this->assertTrue( $field->is_valid( 'a string' ) ); |
| 31 | | $this->assertFalse( $field->set_whitelist_values( 'pizza' )->is_valid( 'pasta' ) ); |
| | 31 | $this->assertFalse( $field->set_allowed_values( 'pizza' )->is_valid( 'pasta' ) ); |
| 32 | 32 | $this->assertTrue( $field->is_valid( 'pizza' ) ); |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | | public function test_multiselectbox_validate_whitelisted_array() { |
| | 35 | public function test_multiselectbox_validate_allowed_array() { |
| 36 | 36 | $field = bp_xprofile_create_field_type( 'multiselectbox' ); |
| 37 | | $field->set_whitelist_values( array( 'cheese', 'pepporoni' ) ); |
| | 37 | $field->set_allowed_values( array( 'cheese', 'pepporoni' ) ); |
| 38 | 38 | |
| 39 | 39 | $this->assertTrue( $field->is_valid( array( 'cheese', 'pepporoni' ) ) ); |
| 40 | 40 | $this->assertTrue( $field->is_valid( array( 'cheese' ) ) ); |
| … |
… |
class BP_Tests_XProfile_Field_Type extends BP_UnitTestCase {
|
| 44 | 44 | |
| 45 | 45 | public function test_multiselectbox_validate_null_value() { |
| 46 | 46 | $field = bp_xprofile_create_field_type( 'multiselectbox' ); |
| 47 | | $field->set_whitelist_values( array( 'cheese', 'pepporoni' ) ); |
| | 47 | $field->set_allowed_values( array( 'cheese', 'pepporoni' ) ); |
| 48 | 48 | |
| 49 | 49 | $this->assertFalse( $field->is_valid( array( '' ) ) ); |
| 50 | 50 | $this->assertFalse( $field->is_valid( '' ) ); |
| … |
… |
class BP_Tests_XProfile_Field_Type extends BP_UnitTestCase {
|
| 98 | 98 | $this->assertFalse( $field->is_valid( '' ) ); |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | | public function test_number_validate_whitelisted_array() { |
| | 101 | public function test_number_validate_allowed_array() { |
| 102 | 102 | $field = bp_xprofile_create_field_type( 'number' ); |
| 103 | | $field->set_whitelist_values( array( 123, 456 ) ); |
| | 103 | $field->set_allowed_values( array( 123, 456 ) ); |
| 104 | 104 | |
| 105 | 105 | $this->assertTrue( $field->is_valid( array( 123 ) ) ); |
| 106 | 106 | $this->assertTrue( $field->is_valid( array( 456 ) ) ); |
| … |
… |
class BP_Tests_XProfile_Field_Type extends BP_UnitTestCase {
|
| 108 | 108 | $this->assertFalse( $field->is_valid( array( 789 ) ) ); |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | | public function test_radiobutton_validate_whitelisted_array() { |
| | 111 | public function test_radiobutton_validate_allowed_array() { |
| 112 | 112 | $field = bp_xprofile_create_field_type( 'radio' ); |
| 113 | | $field->set_whitelist_values( array( 'cheese', 'pepporoni' ) ); |
| | 113 | $field->set_allowed_values( array( 'cheese', 'pepporoni' ) ); |
| 114 | 114 | |
| 115 | 115 | $this->assertTrue( $field->is_valid( array( 'cheese', 'pepporoni' ) ) ); |
| 116 | 116 | $this->assertTrue( $field->is_valid( array( 'cheese' ) ) ); |
| … |
… |
class BP_Tests_XProfile_Field_Type extends BP_UnitTestCase {
|
| 119 | 119 | $this->assertFalse( $field->is_valid( '' ) ); |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | | public function test_radiobutton_do_not_validate_empty_items_in_whitelist() { |
| | 122 | public function test_radiobutton_do_not_validate_empty_items_in_allowed_list() { |
| 123 | 123 | $field = bp_xprofile_create_field_type( 'radio' ); |
| 124 | | $field->set_whitelist_values( array( '' ) ); |
| | 124 | $field->set_allowed_values( array( '' ) ); |
| 125 | 125 | |
| 126 | 126 | $this->assertFalse( $field->is_valid( array( '' ) ) ); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | | public function test_checkbox_validate_whitelisted_array() { |
| | 129 | public function test_checkbox_validate_allowed_array() { |
| 130 | 130 | $field = bp_xprofile_create_field_type( 'checkbox' ); |
| 131 | | $field->set_whitelist_values( array( 'cheese', 'pepporoni' ) ); |
| | 131 | $field->set_allowed_values( array( 'cheese', 'pepporoni' ) ); |
| 132 | 132 | |
| 133 | 133 | $this->assertTrue( $field->is_valid( array( 'cheese', 'pepporoni' ) ) ); |
| 134 | 134 | $this->assertTrue( $field->is_valid( array( 'cheese' ) ) ); |
| … |
… |
class BP_Tests_XProfile_Field_Type extends BP_UnitTestCase {
|
| 139 | 139 | |
| 140 | 140 | public function test_checkbox_validate_null_value() { |
| 141 | 141 | $field = bp_xprofile_create_field_type( 'checkbox' ); |
| 142 | | $field->set_whitelist_values( array( 'cheese', 'pepporoni' ) ); |
| | 142 | $field->set_allowed_values( array( 'cheese', 'pepporoni' ) ); |
| 143 | 143 | |
| 144 | 144 | $this->assertFalse( $field->is_valid( array( '' ) ) ); |
| 145 | 145 | $this->assertFalse( $field->is_valid( '' ) ); |