Changeset 12868
- Timestamp:
- 03/21/2021 02:17:21 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/admin/js/admin.js
r12833 r12868 96 96 } 97 97 98 // Show/hides metaboxes according to selected field type supports. 99 jQuery( '#field-type-visibiliy-metabox, #field-type-required-metabox, #field-type-autolink-metabox, #field-type-member-types' ).show(); 100 if ( -1 !== XProfileAdmin.hide_required_metabox.indexOf( forWhat ) ) { 101 jQuery( '#field-type-required-metabox' ).hide(); 102 } 103 104 if ( -1 !== XProfileAdmin.hide_allow_custom_visibility_metabox.indexOf( forWhat ) ) { 105 jQuery( '#field-type-visibiliy-metabox' ).hide(); 106 } 107 108 if ( -1 !== XProfileAdmin.hide_do_autolink_metabox.indexOf( forWhat ) ) { 109 jQuery( '#field-type-autolink-metabox' ).hide(); 110 } 111 112 if ( -1 !== XProfileAdmin.hide_member_types_metabox.indexOf( forWhat ) ) { 113 jQuery( '#field-type-member-types' ).hide(); 114 } 115 98 116 jQuery( document ).trigger( 'bp-xprofile-show-options', forWhat ); 99 117 } … … 174 192 titleprompt.on( 'click', function(){ 175 193 jQuery(this).addClass('screen-reader-text'); 176 title. focus();194 title.trigger( 'focus' ); 177 195 }); 178 196 … … 185 203 }).on( 'keydown', function(e){ 186 204 titleprompt.addClass('screen-reader-text'); 187 jQuery(this). unbind(e);205 jQuery(this).off( e ); 188 206 }); 189 207 } 190 208 191 jQuery( document ).ready(function() {209 jQuery( function() { 192 210 193 211 // Set focus in Field Title, if we're on the right page. -
trunk/src/bp-xprofile/bp-xprofile-admin.php
r12768 r12868 494 494 $field->name = $_POST['title']; 495 495 496 /* 497 * By default a Textbox field is created. To run field type's feature 498 * checks we need to set it to what it really is early. 499 */ 500 if ( is_null( $field_id ) ) { 501 $field_type = bp_xprofile_create_field_type( $field->type ); 502 503 // If it's a placeholder, then the field type is not registered. 504 if ( ! $field_type instanceof BP_XProfile_Field_Type_Placeholder ) { 505 $field->type_obj = $field_type; 506 } 507 } 508 509 if ( ! $field->field_type_supports( 'required' ) ) { 510 $field->is_required = "0"; 511 } 512 496 513 if ( ! empty( $_POST['description'] ) ) { 497 514 $field->description = $_POST['description']; … … 538 555 // Validate default visibility. 539 556 if ( ! empty( $_POST['default-visibility'] ) && in_array( $_POST['default-visibility'], wp_list_pluck( bp_xprofile_get_visibility_levels(), 'id' ) ) ) { 540 bp_xprofile_update_field_meta( $field_id, 'default_visibility', $_POST['default-visibility'] ); 557 $default_visibility = $_POST['default-visibility']; 558 559 if ( ! $field->field_type_supports( 'allow_custom_visibility' ) ) { 560 $default_visibility = 'public'; 561 } 562 563 bp_xprofile_update_field_meta( $field_id, 'default_visibility', $default_visibility ); 541 564 } 542 565 543 566 // Validate custom visibility. 544 567 if ( ! empty( $_POST['allow-custom-visibility'] ) && in_array( $_POST['allow-custom-visibility'], array( 'allowed', 'disabled' ) ) ) { 545 bp_xprofile_update_field_meta( $field_id, 'allow_custom_visibility', $_POST['allow-custom-visibility'] ); 568 $allow_custom_visibility = $_POST['allow-custom-visibility']; 569 570 if ( ! $field->field_type_supports( 'allow_custom_visibility' ) ) { 571 $allow_custom_visibility = 'disabled'; 572 } 573 574 bp_xprofile_update_field_meta( $field_id, 'allow_custom_visibility', $allow_custom_visibility ); 546 575 } 547 576 … … 553 582 } 554 583 584 $do_autolink = ''; 585 if ( $field->field_type_supports( 'do_autolink' ) && isset( $_POST['do_autolink'] ) && $_POST['do_autolink'] ) { 586 $do_autolink = wp_unslash( $_POST['do_autolink'] ); 587 } 588 555 589 // Save autolink settings. 556 if ( isset( $_POST['do_autolink'] ) && 'on' === wp_unslash( $_POST['do_autolink'] )) {590 if ( 'on' === $do_autolink ) { 557 591 bp_xprofile_update_field_meta( $field_id, 'do_autolink', 'on' ); 558 592 } else { -
trunk/src/bp-xprofile/bp-xprofile-cssjs.php
r12829 r12868 52 52 // "please enter options for this field" section. 53 53 $strings = array( 54 'do_settings_section_field_types' => array(), 55 'do_autolink' => '', 56 'text' => array( 54 'do_settings_section_field_types' => array(), 55 'do_autolink' => '', 56 'hide_do_autolink_metabox' => array(), 57 'hide_allow_custom_visibility_metabox' => array(), 58 'hide_required_metabox' => array(), 59 'hide_member_types_metabox' => array(), 60 'text' => array( 57 61 'defaultValue' => __( 'Default Value', 'buddypress' ), 58 62 'deleteLabel' => __( 'Delete', 'buddypress' ), … … 64 68 if ( $field->do_settings_section() ) { 65 69 $strings['do_settings_section_field_types'][] = $field_type; 70 } 71 72 if ( isset( $field::$supported_features ) && is_array( $field::$supported_features ) ) { 73 foreach ( $field::$supported_features as $feature => $support ) { 74 if ( isset( $strings['hide_' . $feature . '_metabox'] ) && ! $support ) { 75 $strings['hide_' . $feature . '_metabox'][] = $field_type; 76 } 77 } 66 78 } 67 79 } -
trunk/src/bp-xprofile/bp-xprofile-template.php
r12768 r12868 15 15 * 16 16 * @since 1.0.0 17 * @since 2.4.0 Introduced `$member_type` argument. 18 * @since 8.0.0 Introduced `$hide_field_types` argument. 17 19 * 18 20 * @global object $profile_template … … 32 34 * @type bool $fetch_visibility_level Defaults to true when an admin is viewing a profile, or when a user is 33 35 * viewing her own profile, or during registration. Otherwise false. 34 * @type int|bool $exclude_groups Default: false. 35 * @type int|bool $exclude_fields Default: false 36 * @type int[]|bool $exclude_groups Default: false. 37 * @type int[]|bool $exclude_fields Default: false. 38 * @type string[] $hide_field_types Default: empty array. 36 39 * @type bool $update_meta_cache Default: true. 37 40 * } … … 65 68 'exclude_groups' => false, // Comma-separated list of profile field group IDs to exclude. 66 69 'exclude_fields' => false, // Comma-separated list of profile field IDs to exclude. 70 'hide_field_types' => array(), // List of field types to hide from profile fields loop. 67 71 'update_meta_cache' => true, 68 72 ), 'has_profile' ); -
trunk/src/bp-xprofile/classes/class-bp-xprofile-data-template.php
r12426 r12868 106 106 * @since 1.5.0 107 107 * @since 2.4.0 Introduced `$member_type` argument. 108 * @since 8.0.0 Introduced `$hide_field_types` argument. 108 109 * 109 110 * @param array|string $args { … … 121 122 * @type int|bool $fetch_visibility_level Fetch visibility levels. 122 123 * @type int|bool $update_meta_cache Should metadata cache be updated. 124 * @type string[] $hide_field_types List of field types to hide form loop. Default: empty array. 123 125 * } 124 126 */ … … 157 159 'exclude_groups' => false, 158 160 'exclude_fields' => false, 161 'hide_field_types' => array(), 159 162 'update_meta_cache' => true 160 163 ) ); -
trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php
r12725 r12868 183 183 $this->type_obj->field_obj = $this; 184 184 } 185 186 /** 187 * Fires when the xProfile field object has been constructed. 188 * 189 * @since 8.0.0 190 * 191 * @param BP_XProfile_Field $this The xProfile field object. 192 */ 193 do_action( 'bp_xprofile_field', $this ); 185 194 } 186 195 … … 807 816 public function get_default_visibility() { 808 817 if ( ! isset( $this->default_visibility ) ) { 809 $this->default_visibility = bp_xprofile_get_meta( $this->id, 'field', 'default_visibility' ); 810 811 if ( ! $this->default_visibility ) { 812 $this->default_visibility = 'public'; 818 $this->default_visibility = 'public'; 819 $this->visibility = ''; 820 821 if ( isset( $this->type_obj->visibility ) && $this->type_obj->visibility ) { 822 $this->visibility = $this->type_obj->visibility; 823 } 824 825 if ( $this->field_type_supports( 'allow_custom_visibility' ) ) { 826 $this->visibility = bp_xprofile_get_meta( $this->id, 'field', 'default_visibility' ); 827 } 828 829 if ( $this->visibility ) { 830 $this->default_visibility = $this->visibility; 813 831 } 814 832 } … … 1319 1337 1320 1338 /** 1339 * Gets field type supports. 1340 * 1341 * @since 8.0.0 1342 * 1343 * @return bool[] Supported features. 1344 */ 1345 public function get_field_type_supports() { 1346 $supports = array( 1347 'switch_fieldtype' => true, 1348 'allow_required' => true, 1349 'allow_autolink' => true, 1350 'allow_custom_visibility' => true, 1351 'member_types' => true, 1352 ); 1353 1354 if ( isset( $this->type_obj ) && $this->type_obj ) { 1355 $field_type = $this->type_obj; 1356 1357 if ( isset( $field_type::$supported_features ) ) { 1358 $supports = array_merge( $supports, $field_type::$supported_features ); 1359 } 1360 } 1361 1362 return $supports; 1363 } 1364 1365 /** 1366 * Checks whether the field type supports the requested feature. 1367 * 1368 * @since 8.0.0 1369 * 1370 * @param string $support The name of the feature. 1371 * @return boolean True if the field type supports the feature. False otherwise. 1372 */ 1373 public function field_type_supports( $support = '' ) { 1374 $retval = true; 1375 $features = $this->get_field_type_supports(); 1376 1377 if ( isset( $features[ $support ] ) ) { 1378 $retval = $features[ $support ]; 1379 } 1380 1381 return $retval; 1382 } 1383 1384 /** 1321 1385 * Private method used to display the submit metabox. 1322 1386 * … … 1443 1507 1444 1508 // The primary field is for all, so bail. 1445 if ( 1 === (int) $this->id ) {1509 if ( 1 === (int) $this->id || ! $this->field_type_supports( 'member_types' ) ) { 1446 1510 return; 1447 1511 } … … 1456 1520 ?> 1457 1521 1458 <div id=" member-types-div" class="postbox">1522 <div id="field-type-member-types" class="postbox"> 1459 1523 <h2><?php _e( 'Member Types', 'buddypress' ); ?></h2> 1460 1524 <div class="inside"> … … 1497 1561 private function visibility_metabox() { 1498 1562 1499 // Default field cannot have custom visibility.1500 if ( true === $this->is_default_field() ) {1563 // Default field and field types not supporting the feature cannot have custom visibility. 1564 if ( true === $this->is_default_field() || ! $this->field_type_supports( 'allow_custom_visibility' ) ) { 1501 1565 return; 1502 1566 } ?> 1503 1567 1504 <div class="postbox" >1568 <div class="postbox" id="field-type-visibiliy-metabox"> 1505 1569 <h2><label for="default-visibility"><?php esc_html_e( 'Visibility', 'buddypress' ); ?></label></h2> 1506 1570 <div class="inside"> … … 1546 1610 private function required_metabox() { 1547 1611 1548 // Default field is alwaysrequired.1549 if ( true === $this->is_default_field() ) {1612 // Default field and field types not supporting the feature cannot be required. 1613 if ( true === $this->is_default_field() || ! $this->field_type_supports( 'required' ) ) { 1550 1614 return; 1551 1615 } ?> 1552 1616 1553 <div class="postbox" >1617 <div class="postbox" id="field-type-required-metabox"> 1554 1618 <h2><label for="required"><?php esc_html_e( 'Requirement', 'buddypress' ); ?></label></h2> 1555 1619 <div class="inside"> … … 1572 1636 */ 1573 1637 private function autolink_metabox() { 1574 ?> 1575 1576 <div class="postbox"> 1638 1639 // Field types not supporting the feature cannot use autolink. 1640 if ( ! $this->field_type_supports( 'do_autolink' ) ) { 1641 return; 1642 } ?> 1643 1644 <div class="postbox" id="field-type-autolink-metabox"> 1577 1645 <h2><?php esc_html_e( 'Autolink', 'buddypress' ); ?></h2> 1578 1646 <div class="inside"> … … 1607 1675 if ( true === $this->is_default_field() ) { 1608 1676 return; 1609 } ?> 1677 } 1678 ?> 1610 1679 1611 1680 <div class="postbox"> 1612 1681 <h2><label for="fieldtype"><?php esc_html_e( 'Type', 'buddypress'); ?></label></h2> 1613 1682 <div class="inside" aria-live="polite" aria-atomic="true" aria-relevant="all"> 1614 <select name="fieldtype" id="fieldtype" onchange="show_options(this.value)"> 1615 1616 <?php bp_xprofile_admin_form_field_types( $this->type ); ?> 1617 1618 </select> 1683 <?php if ( ! $this->field_type_supports( 'switch_fieldtype' ) ) : ?> 1684 <input type="text" disabled="true" value="<?php echo esc_attr( $this->type_obj->name ); ?>"> 1685 <input type="hidden" name="fieldtype" id="fieldtype" value="<?php echo esc_attr( $this->type ); ?>"> 1686 1687 <?php else : ?> 1688 <select name="fieldtype" id="fieldtype" onchange="show_options(this.value)"> 1689 1690 <?php bp_xprofile_admin_form_field_types( $this->type ); ?> 1691 1692 </select> 1693 <?php endif; ?> 1619 1694 1620 1695 <?php … … 1643 1718 wp_nonce_field( 'bp_xprofile_admin_field', 'bp_xprofile_admin_field' ); 1644 1719 1645 // Field 1 is the fullname field, which cannot have custom visibility. 1646 if ( false === $this->is_default_field() ) { 1720 // Init default field hidden inputs. 1721 $default_field_hidden_inputs = array(); 1722 $hidden_fields = array( 1723 'required' => array( 1724 'name' => 'required', 1725 'id' => 'required', 1726 'value' => '0', 1727 ), 1728 'default_visibility' => array( 1729 'name' => 'default-visibility', 1730 'id' => 'default-visibility', 1731 'value' => $this->get_default_visibility(), 1732 ), 1733 'allow_custom_visibility' => array( 1734 'name' => 'allow-custom-visibility', 1735 'id' => 'allow-custom-visibility', 1736 'value' => 'disabled', 1737 ), 1738 'do_autolink' => array( 1739 'name' => 'do_autolink', 1740 'id' => 'do-autolink', 1741 'value' => '', 1742 ), 1743 ); 1744 1745 // Field 1 is the fullname field, which is required. 1746 if ( true === $this->is_default_field() ) { 1747 $default_field_required = $hidden_fields['required']; 1748 $default_field_required['value'] = '1'; 1749 1750 $default_field_hidden_inputs = array( 1751 $default_field_required, 1752 array( 1753 'name' => 'fieldtype', 1754 'id' => 'fieldtype', 1755 'value' => 'textbox', 1756 ), 1757 ); 1758 } 1759 1760 $supports = $this->get_field_type_supports(); 1761 if ( $supports ) { 1762 foreach ( $supports as $feature => $support ) { 1763 if ( true === $support || in_array( $feature, array( 'switch_fieldtype', 'member_types' ), true ) ) { 1764 continue; 1765 } 1766 1767 $default_field_hidden_inputs[] = $hidden_fields[ $feature ]; 1768 1769 if ( 'allow_custom_visibility' === $feature ) { 1770 $default_field_hidden_inputs[] = $hidden_fields['default_visibility']; 1771 } 1772 } 1773 } 1774 1775 if ( ! $default_field_hidden_inputs ) { 1647 1776 return; 1648 } ?> 1649 1650 <input type="hidden" name="required" id="required" value="1" /> 1651 <input type="hidden" name="fieldtype" id="fieldtype" value="textbox" /> 1652 1653 <?php 1777 } 1778 1779 foreach ( $default_field_hidden_inputs as $default_field_hidden_input ) { 1780 printf( 1781 '<input type="hidden" name="%1$s" id="%2$s" value="%3$s"/>', 1782 esc_attr( $default_field_hidden_input['name'] ), 1783 esc_attr( $default_field_hidden_input['id'] ), 1784 esc_attr( $default_field_hidden_input['value'] ) 1785 ); 1786 } 1654 1787 } 1655 1788 -
trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php
r12725 r12868 238 238 * 239 239 * @since 1.2.0 240 * @since 2.4.0 Introduced `$member_type` argument. 241 * @since 8.0.0 Introduced `$hide_field_types` argument. 240 242 * 241 243 * @global object $wpdb WordPress DB access object. … … 257 259 * @type bool $fetch_field_data Whether to fetch data for each field. Requires a $user_id. 258 260 * Default: false. 259 * @type array $exclude_groups Comma-separated list or array of group IDs to exclude. 260 * @type array $exclude_fields Comma-separated list or array of field IDs to exclude. 261 * @type int[]|bool $exclude_groups Comma-separated list or array of group IDs to exclude. 262 * @type int[]|bool $exclude_fields Comma-separated list or array of field IDs to exclude. 263 * @type string[] $hide_field_types List of field types to hide form loop. Default: empty array. 261 264 * @type bool $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields, 262 265 * and data. Default: true. … … 279 282 'exclude_groups' => false, 280 283 'exclude_fields' => false, 284 'hide_field_types' => array(), 281 285 'update_meta_cache' => true, 282 286 ) ); … … 339 343 $fields = array(); 340 344 foreach ( $field_ids as $field_id ) { 341 $fields[] = xprofile_get_field( $field_id, null, false ); 345 $_field = xprofile_get_field( $field_id, null, false ); 346 347 if ( in_array( $_field->type, $r['hide_field_types'], true ) ) { 348 continue; 349 } 350 351 $fields[] = $_field; 342 352 } 343 353 … … 347 357 // Maybe fetch field data. 348 358 if ( ! empty( $r['fetch_field_data'] ) ) { 359 $field_type_objects = wp_list_pluck( $fields, 'type_obj', 'id' ); 349 360 350 361 // Get field data for user ID. 351 362 if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) { 352 $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids );363 $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids, $field_type_objects ); 353 364 } 354 365 -
trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php
r12697 r12868 1 1 <?php 2 // Include the xProfile Test Type 3 include_once BP_TESTS_DIR . 'assets/bptest-xprofile-field-type.php'; 4 2 5 /** 3 6 * @group xprofile … … 5 8 */ 6 9 class BP_Tests_XProfile_Field_Type extends BP_UnitTestCase { 10 11 public function setUp() { 12 parent::setUp(); 13 14 add_filter( 'bp_xprofile_get_field_types', array( $this, 'get_field_types' ) ); 15 } 16 17 public function tearDown() { 18 parent::tearDown(); 19 20 remove_filter( 'bp_xprofile_get_field_types', array( $this, 'get_field_types' ) ); 21 } 22 7 23 public function test_unregistered_field_type_returns_textbox() { 8 24 $field = bp_xprofile_create_field_type( 'fakeyfield' ); … … 184 200 $this->assertTrue( $field->is_valid( '(212) 664-7665' ) ); 185 201 } 202 203 /** 204 * @ticket BP7162 205 */ 206 public function test_xprofile_field_type_test_supports() { 207 $group_id = self::factory()->xprofile_group->create(); 208 $field_id = self::factory()->xprofile_field->create( 209 array( 210 'field_group_id' => $group_id, 211 'type' => 'test-field-type', 212 'name' => 'Test Supports', 213 ) 214 ); 215 216 $field = xprofile_get_field( $field_id, null, false ); 217 218 $this->assertTrue( $field->field_type_supports( 'switch_fieldtype' ) ); 219 $this->assertFalse( $field->field_type_supports( 'do_autolink' ) ); 220 $this->assertFalse( $field->field_type_supports( 'allow_custom_visibility' ) ); 221 $this->assertTrue( $field->field_type_supports( 'required' ) ); 222 $this->assertTrue( $field->field_type_supports( 'member_types' ) ); 223 $this->assertEquals( 'adminsonly', $field->get_default_visibility() ); 224 } 225 226 public function get_field_types( $types ) { 227 $types['test-field-type'] = 'BPTest_XProfile_Field_Type'; 228 return $types; 229 } 186 230 }
Note: See TracChangeset
for help on using the changeset viewer.