- Timestamp:
- 10/01/2015 04:18:13 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php
r10160 r10163 1 1 <?php 2 2 /** 3 * BuddyPress XProfile Classes 3 * BuddyPress XProfile Classes. 4 4 * 5 5 * @package BuddyPress … … 13 13 14 14 /** 15 * Field ID. 16 * 15 17 * @since 1.0.0 16 18 * 17 * @var int ID of field 19 * @var int ID of field. 18 20 */ 19 21 public $id; 20 22 21 23 /** 24 * Field group ID. 25 * 22 26 * @since 1.0.0 23 27 * 24 * @var int Field group ID for field 28 * @var int Field group ID for field. 25 29 */ 26 30 public $group_id; 27 31 28 32 /** 33 * Field parent ID. 34 * 29 35 * @since 1.0.0 30 36 * 31 * @var int Parent ID of field 37 * @var int Parent ID of field. 32 38 */ 33 39 public $parent_id; 34 40 35 41 /** 42 * Field type. 43 * 36 44 * @since 1.0.0 37 45 * 38 * @var string Field type 46 * @var string Field type. 39 47 */ 40 48 public $type; 41 49 42 50 /** 51 * Field name. 52 * 43 53 * @since 1.0.0 44 54 * 45 * @var string Field name 55 * @var string Field name. 46 56 */ 47 57 public $name; 48 58 49 59 /** 60 * Field description. 61 * 50 62 * @since 1.0.0 51 63 * 52 * @var string Field description 64 * @var string Field description. 53 65 */ 54 66 public $description; 55 67 56 68 /** 69 * Required field? 70 * 57 71 * @since 1.0.0 58 72 * … … 62 76 63 77 /** 78 * Deletable field? 79 * 64 80 * @since 1.0.0 65 81 * … … 69 85 70 86 /** 87 * Field position. 88 * 71 89 * @since 1.0.0 72 90 * 73 * @var int Field position 91 * @var int Field position. 74 92 */ 75 93 public $field_order; 76 94 77 95 /** 96 * Option order. 97 * 78 98 * @since 1.0.0 79 99 * 80 * @var int Option order 100 * @var int Option order. 81 101 */ 82 102 public $option_order; 83 103 84 104 /** 105 * Order child fields. 106 * 85 107 * @since 1.0.0 86 108 * 87 * @var string Order child fields by 109 * @var string Order child fields by. 88 110 */ 89 111 public $order_by; 90 112 91 113 /** 114 * Is this the default option? 115 * 92 116 * @since 1.0.0 93 117 * … … 97 121 98 122 /** 123 * Field data visibility. 124 * 99 125 * @since 1.9.0 100 126 * 101 * @var string Default field data visibility 127 * @var string Default field data visibility. 102 128 */ 103 129 public $default_visibility = 'public'; 104 130 105 131 /** 132 * Is the visibility able to be modified? 133 * 106 134 * @since 2.3.0 107 135 * 108 * @var string Members are allowed/disallowed to modify data visibility 136 * @var string Members are allowed/disallowed to modify data visibility. 109 137 */ 110 138 public $allow_custom_visibility = 'allowed'; 111 139 112 140 /** 141 * Field type option. 142 * 113 143 * @since 2.0.0 114 144 * 115 * @var BP_XProfile_Field_Type Field type object used for validation 145 * @var BP_XProfile_Field_Type Field type object used for validation. 116 146 */ 117 147 public $type_obj = null; 118 148 119 149 /** 150 * Field data for user ID. 151 * 120 152 * @since 2.0.0 121 153 * 122 * @var BP_XProfile_ProfileData Field data for user ID 154 * @var BP_XProfile_ProfileData Field data for user ID. 123 155 */ 124 156 public $data; … … 134 166 135 167 /** 136 * Initialize and/or populate profile field 168 * Initialize and/or populate profile field. 137 169 * 138 170 * @since 1.1.0 139 171 * 140 * @param int $id141 * @param int $user_id142 * @param bool $get_data172 * @param int|null $id Field ID. 173 * @param int|null $user_id User ID. 174 * @param bool $get_data Get data. 143 175 */ 144 176 public function __construct( $id = null, $user_id = null, $get_data = true ) { … … 147 179 $this->populate( $id, $user_id, $get_data ); 148 180 149 // Initialise the type obj to prevent fatals when creating new profile fields 181 // Initialise the type obj to prevent fatals when creating new profile fields. 150 182 } else { 151 183 $this->type_obj = bp_xprofile_create_field_type( 'textbox' ); … … 155 187 156 188 /** 157 * Populate a profile field object 189 * Populate a profile field object. 158 190 * 159 191 * @since 1.1.0 … … 162 194 * @global object $userdata 163 195 * 164 * @param int $id165 * @param int $user_id166 * @param bool $get_data196 * @param int $id Field ID. 197 * @param int $user_id User ID. 198 * @param bool $get_data Get data. 167 199 */ 168 200 public function populate( $id, $user_id = null, $get_data = true ) { … … 198 230 } 199 231 200 // Get metadata for field 232 // Get metadata for field. 201 233 $default_visibility = bp_xprofile_get_meta( $id, 'field', 'default_visibility' ); 202 234 $allow_custom_visibility = bp_xprofile_get_meta( $id, 'field', 'allow_custom_visibility' ); 203 235 204 // Setup default visibility 236 // Setup default visibility. 205 237 $this->default_visibility = ! empty( $default_visibility ) 206 238 ? $default_visibility 207 239 : 'public'; 208 240 209 // Allow members to customize visibilty 241 // Allow members to customize visibilty. 210 242 $this->allow_custom_visibility = ( 'disabled' === $allow_custom_visibility ) 211 243 ? 'disabled' … … 215 247 216 248 /** 217 * Delete a profile field 249 * Delete a profile field. 218 250 * 219 251 * @since 1.1.0 220 252 * 221 * @global object $wpdb 222 * @param boolean $delete_data 253 * @global object $wpdb 254 * 255 * @param boolean $delete_data Whether or not to delete data. 256 * 223 257 * @return boolean 224 258 */ … … 226 260 global $wpdb; 227 261 228 // Prevent deletion if no ID is present 262 // Prevent deletion if no ID is present. 229 263 // Prevent deletion by url when can_delete is false. 230 264 // Prevent deletion of option 1 since this invalidates fields with options. … … 240 274 } 241 275 242 // delete the data in the DB for this field276 // Delete the data in the DB for this field. 243 277 if ( true === $delete_data ) { 244 278 BP_XProfile_ProfileData::delete_for_field( $this->id ); … … 249 283 250 284 /** 251 * Save a profile field 285 * Save a profile field. 252 286 * 253 287 * @since 1.1.0 … … 281 315 * @since 1.0.0 282 316 * 283 * @param BP_XProfile_Field Current instance of the field being saved.317 * @param BP_XProfile_Field $this Current instance of the field being saved. 284 318 */ 285 319 do_action_ref_array( 'xprofile_field_before_save', array( $this ) ); … … 303 337 } 304 338 305 // Only do this if we are editing an existing field 339 // Only do this if we are editing an existing field. 306 340 if ( ! $is_new_field ) { 307 341 … … 379 413 * @since 1.0.0 380 414 * 381 * @param BP_XProfile_Field Current instance of the field being saved.415 * @param BP_XProfile_Field $this Current instance of the field being saved. 382 416 */ 383 417 do_action_ref_array( 'xprofile_field_after_save', array( $this ) ); … … 394 428 395 429 /** 396 * Get field data for a user ID 430 * Get field data for a user ID. 397 431 * 398 432 * @since 1.2.0 399 433 * 400 * @param int $user_id 434 * @param int $user_id ID of the user to get field data for. 435 * 401 436 * @return object 402 437 */ … … 406 441 407 442 /** 408 * Get all child fields for this field ID 443 * Get all child fields for this field ID. 409 444 * 410 445 * @since 1.2.0 … … 412 447 * @global object $wpdb 413 448 * 414 * @param bool $for_editing 449 * @param bool $for_editing Whether or not the field is for editing. 450 * 415 451 * @return array 416 452 */ … … 418 454 global $wpdb; 419 455 420 // This is done here so we don't have problems with sql injection 456 // This is done here so we don't have problems with sql injection. 421 457 if ( empty( $for_editing ) && ( 'asc' === $this->order_by ) ) { 422 458 $sort_sql = 'ORDER BY name ASC'; … … 428 464 429 465 // This eliminates a problem with getting all fields when there is no 430 // id for the object 466 // id for the object. 431 467 if ( empty( $this->id ) ) { 432 468 $parent_id = -1; … … 452 488 453 489 /** 454 * Delete all field children for this field 490 * Delete all field children for this field. 455 491 * 456 492 * @since 1.2.0 … … 534 570 * @param bool $append Whether to append to existing member types. If false, all existing member type 535 571 * associations will be deleted before adding your `$member_types`. Default false. 572 * 536 573 * @return array Member types for the current field, after being saved. 537 574 */ … … 588 625 * @since 2.4.0 589 626 * 590 * @param BP_XProfile_Field $ fieldField object.627 * @param BP_XProfile_Field $this Field object. 591 628 */ 592 629 do_action( 'bp_xprofile_field_set_member_type', $this ); … … 661 698 global $wpdb; 662 699 663 // Bail if no field ID 700 // Bail if no field ID. 664 701 if ( empty( $field_id ) ) { 665 702 return false; … … 670 707 $type = $wpdb->get_var( $sql ); 671 708 672 // Return field type 709 // Return field type. 673 710 if ( ! empty( $type ) ) { 674 711 return $type; … … 679 716 680 717 /** 681 * Delete all fields in a field group 718 * Delete all fields in a field group. 682 719 * 683 720 * @since 1.2.0 … … 685 722 * @global object $wpdb 686 723 * 687 * @param int $group_id724 * @param int $group_id ID of the field group to delete fields from. 688 725 * 689 726 * @return boolean … … 692 729 global $wpdb; 693 730 694 // Bail if no group ID 731 // Bail if no group ID. 695 732 if ( empty( $group_id ) ) { 696 733 return false; … … 701 738 $deleted = $wpdb->get_var( $sql ); 702 739 703 // Return true if fields were deleted 740 // Return true if fields were deleted. 704 741 if ( false !== $deleted ) { 705 742 return true; … … 710 747 711 748 /** 712 * Get field ID from field name 749 * Get field ID from field name. 713 750 * 714 751 * @since 1.5.0 715 752 * 716 753 * @global object $wpdb 717 * @param string $field_name 754 * 755 * @param string $field_name Name of the field to query the ID for. 718 756 * 719 757 * @return boolean … … 734 772 735 773 /** 736 * Update field position and/or field group when relocating 774 * Update field position and/or field group when relocating. 737 775 * 738 776 * @since 1.5.0 … … 740 778 * @global object $wpdb 741 779 * 742 * @param int $field_id743 * @param int $position744 * @param int $field_group_id780 * @param int $field_id ID of the field to update. 781 * @param int|null $position Field position to update. 782 * @param int|null $field_group_id ID of the field group. 745 783 * 746 784 * @return boolean … … 749 787 global $wpdb; 750 788 751 // Bail if invalid position or field group 789 // Bail if invalid position or field group. 752 790 if ( ! is_numeric( $position ) || ! is_numeric( $field_group_id ) ) { 753 791 return false; 754 792 } 755 793 756 // Get table name and field parent 794 // Get table name and field parent. 757 795 $table_name = buddypress()->profile->table_name_fields; 758 796 $sql = $wpdb->prepare( "UPDATE {$table_name} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id ); 759 797 $parent = $wpdb->query( $sql ); 760 798 761 // Update $field_id with new $position and $field_group_id 799 // Update $field_id with new $position and $field_group_id. 762 800 if ( ! empty( $parent ) && ! is_wp_error( $parent ) ) { 763 801 764 // Update any children of this $field_id 802 // Update any children of this $field_id. 765 803 $sql = $wpdb->prepare( "UPDATE {$table_name} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id ); 766 804 $wpdb->query( $sql ); … … 779 817 * @param string|array $member_types Member type or array of member types. Use 'any' to return unrestricted 780 818 * fields (those available for anyone, regardless of member type). 819 * 781 820 * @return array Multi-dimensional array, with field IDs as top-level keys, and arrays of member types 782 821 * associated with each field as values. … … 847 886 848 887 /** 849 * Validate form field data on sumbission 888 * Validate form field data on sumbission. 850 889 * 851 890 * @since 2.2.0 852 891 * 853 * @global type $message 892 * @global $message 893 * 854 894 * @return boolean 855 895 */ … … 857 897 global $message; 858 898 859 // Check field name 899 // Check field name. 860 900 if ( ! isset( $_POST['title'] ) || ( '' === $_POST['title'] ) ) { 861 901 $message = esc_html__( 'Profile fields must have a name.', 'buddypress' ); … … 863 903 } 864 904 865 // Check field requirement 905 // Check field requirement. 866 906 if ( ! isset( $_POST['required'] ) ) { 867 907 $message = esc_html__( 'Profile field requirement is missing.', 'buddypress' ); … … 869 909 } 870 910 871 // Check field type 911 // Check field type. 872 912 if ( empty( $_POST['fieldtype'] ) ) { 873 913 $message = esc_html__( 'Profile field type is missing.', 'buddypress' ); … … 875 915 } 876 916 877 // Check that field is of valid type 917 // Check that field is of valid type. 878 918 if ( ! in_array( $_POST['fieldtype'], array_keys( bp_xprofile_get_field_types() ), true ) ) { 879 919 $message = sprintf( esc_html__( 'The profile field type %s is not registered.', 'buddypress' ), '<code>' . esc_attr( $_POST['fieldtype'] ) . '</code>' ); … … 881 921 } 882 922 883 // Get field type so we can check for and lavidate any field options 923 // Get field type so we can check for and lavidate any field options. 884 924 $field_type = bp_xprofile_create_field_type( $_POST['fieldtype'] ); 885 925 886 // Field type requires options 926 // Field type requires options. 887 927 if ( true === $field_type->supports_options ) { 888 928 889 // Build the field option key 929 // Build the field option key. 890 930 $option_name = sanitize_key( $_POST['fieldtype'] ) . '_option'; 891 931 892 // Check for missing or malformed options 932 // Check for missing or malformed options. 893 933 if ( empty( $_POST[ $option_name ] ) || ! is_array( $_POST[ $option_name ] ) ) { 894 934 $message = esc_html__( 'These field options are invalid.', 'buddypress' ); … … 896 936 } 897 937 898 // Trim out empty field options 938 // Trim out empty field options. 899 939 $field_values = array_values( $_POST[ $option_name ] ); 900 940 $field_options = array_map( 'sanitize_text_field', $field_values ); 901 941 $field_count = count( $field_options ); 902 942 903 // Check for missing or malformed options 943 // Check for missing or malformed options. 904 944 if ( 0 === $field_count ) { 905 945 $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name ); … … 907 947 } 908 948 909 // If only one option exists, it cannot be an empty string 949 // If only one option exists, it cannot be an empty string. 910 950 if ( ( 1 === $field_count ) && ( '' === $field_options[0] ) ) { 911 951 $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name ); … … 929 969 930 970 /** 931 * Oupput the admin form for this field 971 * Oupput the admin form for this field. 932 972 * 933 973 * @since 1.9.0 934 974 * 935 * @param type $message975 * @param string $message Message to display. 936 976 */ 937 977 public function render_admin_form( $message = '' ) { … … 976 1016 <?php 977 1017 978 // Output the name & description fields 1018 // Output the name & description fields. 979 1019 $this->name_and_description(); ?> 980 1020 … … 985 1025 <?php 986 1026 987 // Output the sumbit metabox 1027 // Output the sumbit metabox. 988 1028 $this->submit_metabox( $button ); 989 1029 990 // Output the required metabox 1030 // Output the required metabox. 991 1031 $this->required_metabox(); 992 1032 … … 994 1034 $this->member_type_metabox(); 995 1035 996 // Output the field visibility metaboxes 1036 // Output the field visibility metaboxes. 997 1037 $this->visibility_metabox(); 998 1038 … … 1021 1061 do_action( 'xprofile_field_before_contentbox', $this ); 1022 1062 1023 // Output the field attributes metabox 1063 // Output the field attributes metabox. 1024 1064 $this->type_metabox(); 1025 1065 1026 // Output hidden inputs for default field 1066 // Output hidden inputs for default field. 1027 1067 $this->default_field_hidden_inputs(); 1028 1068 … … 1046 1086 1047 1087 /** 1048 * Private method used to display the submit metabox 1088 * Private method used to display the submit metabox. 1049 1089 * 1050 1090 * @since 2.3.0 1051 1091 * 1052 * @param string $button_text 1092 * @param string $button_text Text to put on button. 1053 1093 */ 1054 1094 private function submit_metabox( $button_text = '' ) { … … 1115 1155 1116 1156 /** 1117 * Private method used to output field name and description fields 1157 * Private method used to output field name and description fields. 1118 1158 * 1119 1159 * @since 2.3.0 … … 1193 1233 1194 1234 /** 1195 * Private method used to output field visibility metaboxes 1235 * Private method used to output field visibility metaboxes. 1196 1236 * 1197 1237 * @since 2.3.0 1198 1238 * 1199 * @return if default field id 11239 * @return void If default field id 1. 1200 1240 */ 1201 1241 private function visibility_metabox() { 1202 1242 1203 // Default field cannot have custom visibility 1243 // Default field cannot have custom visibility. 1204 1244 if ( true === $this->is_default_field() ) { 1205 1245 return; … … 1242 1282 1243 1283 /** 1244 * Output the metabox for setting if field is required or not 1284 * Output the metabox for setting if field is required or not. 1245 1285 * 1246 1286 * @since 2.3.0 1247 1287 * 1248 * @return if default field1288 * @return void If default field. 1249 1289 */ 1250 1290 private function required_metabox() { 1251 1291 1252 // Default field is always required 1292 // Default field is always required. 1253 1293 if ( true === $this->is_default_field() ) { 1254 1294 return; … … 1269 1309 1270 1310 /** 1271 * Output the metabox for setting what type of field this is 1311 * Output the metabox for setting what type of field this is. 1272 1312 * 1273 1313 * @since 2.3.0 1274 1314 * 1275 * @return if default field1315 * @return void If default field. 1276 1316 */ 1277 1317 private function type_metabox() { 1278 1318 1279 // Default field cannot change type 1319 // Default field cannot change type. 1280 1320 if ( true === $this->is_default_field() ) { 1281 1321 return; … … 1305 1345 1306 1346 /** 1307 * Output hidden fields used by default field 1347 * Output hidden fields used by default field. 1308 1348 * 1309 1349 * @since 2.3.0 1310 1350 * 1311 * @return if not default field1351 * @return void If not default field. 1312 1352 */ 1313 1353 private function default_field_hidden_inputs() { 1314 1354 1315 // Field 1 is the fullname field, which cannot have custom visibility 1355 // Field 1 is the fullname field, which cannot have custom visibility. 1316 1356 if ( false === $this->is_default_field() ) { 1317 1357 return; … … 1325 1365 1326 1366 /** 1327 * Return if a field ID is the default field 1367 * Return if a field ID is the default field. 1328 1368 * 1329 1369 * @since 2.3.0 1330 1370 * 1331 * @param int $field_id ID of field to check 1371 * @param int $field_id ID of field to check. 1372 * 1332 1373 * @return bool 1333 1374 */ 1334 1375 private function is_default_field( $field_id = 0 ) { 1335 1376 1336 // Fallback to current field ID if none passed 1377 // Fallback to current field ID if none passed. 1337 1378 if ( empty( $field_id ) ) { 1338 1379 $field_id = $this->id; 1339 1380 } 1340 1381 1341 // Compare & return 1382 // Compare & return. 1342 1383 return (bool) ( 1 === (int) $field_id ); 1343 1384 }
Note: See TracChangeset
for help on using the changeset viewer.