Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/01/2015 04:18:13 AM (11 years ago)
Author:
tw2113
Message:

First pass of documentation cleanup for the XProfile Component.

See #6406.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php

    r10160 r10163  
    11<?php
    22/**
    3  * BuddyPress XProfile Classes
     3 * BuddyPress XProfile Classes.
    44 *
    55 * @package BuddyPress
    … …  
    1313
    1414        /**
     15         * Field ID.
     16         *
    1517         * @since 1.0.0
    1618         *
    17          * @var int ID of field
     19         * @var int ID of field.
    1820         */
    1921        public $id;
    2022
    2123        /**
     24         * Field group ID.
     25         *
    2226         * @since 1.0.0
    2327         *
    24          * @var int Field group ID for field
     28         * @var int Field group ID for field.
    2529         */
    2630        public $group_id;
    2731
    2832        /**
     33         * Field parent ID.
     34         *
    2935         * @since 1.0.0
    3036         *
    31          * @var int Parent ID of field
     37         * @var int Parent ID of field.
    3238         */
    3339        public $parent_id;
    3440
    3541        /**
     42         * Field type.
     43         *
    3644         * @since 1.0.0
    3745         *
    38          * @var string Field type
     46         * @var string Field type.
    3947         */
    4048        public $type;
    4149
    4250        /**
     51         * Field name.
     52         *
    4353         * @since 1.0.0
    4454         *
    45          * @var string Field name
     55         * @var string Field name.
    4656         */
    4757        public $name;
    4858
    4959        /**
     60         * Field description.
     61         *
    5062         * @since 1.0.0
    5163         *
    52          * @var string Field description
     64         * @var string Field description.
    5365         */
    5466        public $description;
    5567
    5668        /**
     69         * Required field?
     70         *
    5771         * @since 1.0.0
    5872         *
    … …  
    6276
    6377        /**
     78         * Deletable field?
     79         *
    6480         * @since 1.0.0
    6581         *
    … …  
    6985
    7086        /**
     87         * Field position.
     88         *
    7189         * @since 1.0.0
    7290         *
    73          * @var int Field position
     91         * @var int Field position.
    7492         */
    7593        public $field_order;
    7694
    7795        /**
     96         * Option order.
     97         *
    7898         * @since 1.0.0
    7999         *
    80          * @var int Option order
     100         * @var int Option order.
    81101         */
    82102        public $option_order;
    83103
    84104        /**
     105         * Order child fields.
     106         *
    85107         * @since 1.0.0
    86108         *
    87          * @var string Order child fields by
     109         * @var string Order child fields by.
    88110         */
    89111        public $order_by;
    90112
    91113        /**
     114         * Is this the default option?
     115         *
    92116         * @since 1.0.0
    93117         *
    … …  
    97121
    98122        /**
     123         * Field data visibility.
     124         *
    99125         * @since 1.9.0
    100126         *
    101          * @var string Default field data visibility
     127         * @var string Default field data visibility.
    102128         */
    103129        public $default_visibility = 'public';
    104130
    105131        /**
     132         * Is the visibility able to be modified?
     133         *
    106134         * @since 2.3.0
    107135         *
    108          * @var string Members are allowed/disallowed to modify data visibility
     136         * @var string Members are allowed/disallowed to modify data visibility.
    109137         */
    110138        public $allow_custom_visibility = 'allowed';
    111139
    112140        /**
     141         * Field type option.
     142         *
    113143         * @since 2.0.0
    114144         *
    115          * @var BP_XProfile_Field_Type Field type object used for validation
     145         * @var BP_XProfile_Field_Type Field type object used for validation.
    116146         */
    117147        public $type_obj = null;
    118148
    119149        /**
     150         * Field data for user ID.
     151         *
    120152         * @since 2.0.0
    121153         *
    122          * @var BP_XProfile_ProfileData Field data for user ID
     154         * @var BP_XProfile_ProfileData Field data for user ID.
    123155         */
    124156        public $data;
    … …  
    134166
    135167        /**
    136          * Initialize and/or populate profile field
     168         * Initialize and/or populate profile field.
    137169         *
    138170         * @since 1.1.0
    139171         *
    140          * @param int  $id
    141          * @param int  $user_id
    142          * @param bool $get_data
     172         * @param int|null $id Field ID.
     173         * @param int|null $user_id User ID.
     174         * @param bool     $get_data Get data.
    143175         */
    144176        public function __construct( $id = null, $user_id = null, $get_data = true ) {
    … …  
    147179                        $this->populate( $id, $user_id, $get_data );
    148180
    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.
    150182                } else {
    151183                        $this->type_obj            = bp_xprofile_create_field_type( 'textbox' );
    … …  
    155187
    156188        /**
    157          * Populate a profile field object
     189         * Populate a profile field object.
    158190         *
    159191         * @since 1.1.0
    … …  
    162194         * @global object $userdata
    163195         *
    164          * @param  int    $id
    165          * @param  int    $user_id
    166          * @param  bool   $get_data
     196         * @param int  $id Field ID.
     197         * @param int  $user_id User ID.
     198         * @param bool $get_data Get data.
    167199         */
    168200        public function populate( $id, $user_id = null, $get_data = true ) {
    … …  
    198230                        }
    199231
    200                         // Get metadata for field
     232                        // Get metadata for field.
    201233                        $default_visibility       = bp_xprofile_get_meta( $id, 'field', 'default_visibility'      );
    202234                        $allow_custom_visibility  = bp_xprofile_get_meta( $id, 'field', 'allow_custom_visibility' );
    203235
    204                         // Setup default visibility
     236                        // Setup default visibility.
    205237                        $this->default_visibility = ! empty( $default_visibility )
    206238                                ? $default_visibility
    207239                                : 'public';
    208240
    209                         // Allow members to customize visibilty
     241                        // Allow members to customize visibilty.
    210242                        $this->allow_custom_visibility = ( 'disabled' === $allow_custom_visibility )
    211243                                ? 'disabled'
    … …  
    215247
    216248        /**
    217          * Delete a profile field
     249         * Delete a profile field.
    218250         *
    219251         * @since 1.1.0
    220252         *
    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         *
    223257         * @return boolean
    224258         */
    … …  
    226260                global $wpdb;
    227261
    228                 // Prevent deletion if no ID is present
     262                // Prevent deletion if no ID is present.
    229263                // Prevent deletion by url when can_delete is false.
    230264                // Prevent deletion of option 1 since this invalidates fields with options.
    … …  
    240274                }
    241275
    242                 // delete the data in the DB for this field
     276                // Delete the data in the DB for this field.
    243277                if ( true === $delete_data ) {
    244278                        BP_XProfile_ProfileData::delete_for_field( $this->id );
    … …  
    249283
    250284        /**
    251          * Save a profile field
     285         * Save a profile field.
    252286         *
    253287         * @since 1.1.0
    … …  
    281315                 * @since 1.0.0
    282316                 *
    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.
    284318                 */
    285319                do_action_ref_array( 'xprofile_field_before_save', array( $this ) );
    … …  
    303337                        }
    304338
    305                         // Only do this if we are editing an existing field
     339                        // Only do this if we are editing an existing field.
    306340                        if ( ! $is_new_field ) {
    307341
    … …  
    379413                         * @since 1.0.0
    380414                         *
    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.
    382416                         */
    383417                        do_action_ref_array( 'xprofile_field_after_save', array( $this ) );
    … …  
    394428
    395429        /**
    396          * Get field data for a user ID
     430         * Get field data for a user ID.
    397431         *
    398432         * @since 1.2.0
    399433         *
    400          * @param  int $user_id
     434         * @param int $user_id ID of the user to get field data for.
     435         *
    401436         * @return object
    402437         */
    … …  
    406441
    407442        /**
    408          * Get all child fields for this field ID
     443         * Get all child fields for this field ID.
    409444         *
    410445         * @since 1.2.0
    … …  
    412447         * @global object $wpdb
    413448         *
    414          * @param  bool  $for_editing
     449         * @param bool $for_editing Whether or not the field is for editing.
     450         *
    415451         * @return array
    416452         */
    … …  
    418454                global $wpdb;
    419455
    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.
    421457                if ( empty( $for_editing ) && ( 'asc' === $this->order_by ) ) {
    422458                        $sort_sql = 'ORDER BY name ASC';
    … …  
    428464
    429465                // This eliminates a problem with getting all fields when there is no
    430                 // id for the object
     466                // id for the object.
    431467                if ( empty( $this->id ) ) {
    432468                        $parent_id = -1;
    … …  
    452488
    453489        /**
    454          * Delete all field children for this field
     490         * Delete all field children for this field.
    455491         *
    456492         * @since 1.2.0
    … …  
    534570         * @param bool  $append       Whether to append to existing member types. If false, all existing member type
    535571         *                            associations will be deleted before adding your `$member_types`. Default false.
     572         *
    536573         * @return array Member types for the current field, after being saved.
    537574         */
    … …  
    588625                 * @since 2.4.0
    589626                 *
    590                  * @param BP_XProfile_Field $field Field object.
     627                 * @param BP_XProfile_Field $this Field object.
    591628                 */
    592629                do_action( 'bp_xprofile_field_set_member_type', $this );
    … …  
    661698                global $wpdb;
    662699
    663                 // Bail if no field ID
     700                // Bail if no field ID.
    664701                if ( empty( $field_id ) ) {
    665702                        return false;
    … …  
    670707                $type = $wpdb->get_var( $sql );
    671708
    672                 // Return field type
     709                // Return field type.
    673710                if ( ! empty( $type ) ) {
    674711                        return $type;
    … …  
    679716
    680717        /**
    681          * Delete all fields in a field group
     718         * Delete all fields in a field group.
    682719         *
    683720         * @since 1.2.0
    … …  
    685722         * @global object $wpdb
    686723         *
    687          * @param  int    $group_id
     724         * @param int $group_id ID of the field group to delete fields from.
    688725         *
    689726         * @return boolean
    … …  
    692729                global $wpdb;
    693730
    694                 // Bail if no group ID
     731                // Bail if no group ID.
    695732                if ( empty( $group_id ) ) {
    696733                        return false;
    … …  
    701738                $deleted = $wpdb->get_var( $sql );
    702739
    703                 // Return true if fields were deleted
     740                // Return true if fields were deleted.
    704741                if ( false !== $deleted ) {
    705742                        return true;
    … …  
    710747
    711748        /**
    712          * Get field ID from field name
     749         * Get field ID from field name.
    713750         *
    714751         * @since 1.5.0
    715752         *
    716753         * @global object $wpdb
    717          * @param  string $field_name
     754         *
     755         * @param string $field_name Name of the field to query the ID for.
    718756         *
    719757         * @return boolean
    … …  
    734772
    735773        /**
    736          * Update field position and/or field group when relocating
     774         * Update field position and/or field group when relocating.
    737775         *
    738776         * @since 1.5.0
    … …  
    740778         * @global object $wpdb
    741779         *
    742          * @param  int $field_id
    743          * @param  int $position
    744          * @param  int $field_group_id
     780         * @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.
    745783         *
    746784         * @return boolean
    … …  
    749787                global $wpdb;
    750788
    751                 // Bail if invalid position or field group
     789                // Bail if invalid position or field group.
    752790                if ( ! is_numeric( $position ) || ! is_numeric( $field_group_id ) ) {
    753791                        return false;
    754792                }
    755793
    756                 // Get table name and field parent
     794                // Get table name and field parent.
    757795                $table_name = buddypress()->profile->table_name_fields;
    758796                $sql        = $wpdb->prepare( "UPDATE {$table_name} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id );
    759797                $parent     = $wpdb->query( $sql );
    760798
    761                 // Update $field_id with new $position and $field_group_id
     799                // Update $field_id with new $position and $field_group_id.
    762800                if ( ! empty( $parent ) && ! is_wp_error( $parent ) ) {
    763801
    764                         // Update any children of this $field_id
     802                        // Update any children of this $field_id.
    765803                        $sql = $wpdb->prepare( "UPDATE {$table_name} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id );
    766804                        $wpdb->query( $sql );
    … …  
    779817         * @param string|array $member_types Member type or array of member types. Use 'any' to return unrestricted
    780818         *                                   fields (those available for anyone, regardless of member type).
     819         *
    781820         * @return array Multi-dimensional array, with field IDs as top-level keys, and arrays of member types
    782821         *               associated with each field as values.
    … …  
    847886
    848887        /**
    849          * Validate form field data on sumbission
     888         * Validate form field data on sumbission.
    850889         *
    851890         * @since 2.2.0
    852891         *
    853          * @global type $message
     892         * @global $message
     893         *
    854894         * @return boolean
    855895         */
    … …  
    857897                global $message;
    858898
    859                 // Check field name
     899                // Check field name.
    860900                if ( ! isset( $_POST['title'] ) || ( '' === $_POST['title'] ) ) {
    861901                        $message = esc_html__( 'Profile fields must have a name.', 'buddypress' );
    … …  
    863903                }
    864904
    865                 // Check field requirement
     905                // Check field requirement.
    866906                if ( ! isset( $_POST['required'] ) ) {
    867907                        $message = esc_html__( 'Profile field requirement is missing.', 'buddypress' );
    … …  
    869909                }
    870910
    871                 // Check field type
     911                // Check field type.
    872912                if ( empty( $_POST['fieldtype'] ) ) {
    873913                        $message = esc_html__( 'Profile field type is missing.', 'buddypress' );
    … …  
    875915                }
    876916
    877                 // Check that field is of valid type
     917                // Check that field is of valid type.
    878918                if ( ! in_array( $_POST['fieldtype'], array_keys( bp_xprofile_get_field_types() ), true ) ) {
    879919                        $message = sprintf( esc_html__( 'The profile field type %s is not registered.', 'buddypress' ), '<code>' . esc_attr( $_POST['fieldtype'] ) . '</code>' );
    … …  
    881921                }
    882922
    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.
    884924                $field_type = bp_xprofile_create_field_type( $_POST['fieldtype'] );
    885925
    886                 // Field type requires options
     926                // Field type requires options.
    887927                if ( true === $field_type->supports_options ) {
    888928
    889                         // Build the field option key
     929                        // Build the field option key.
    890930                        $option_name = sanitize_key( $_POST['fieldtype'] ) . '_option';
    891931
    892                         // Check for missing or malformed options
     932                        // Check for missing or malformed options.
    893933                        if ( empty( $_POST[ $option_name ] ) || ! is_array( $_POST[ $option_name ] ) ) {
    894934                                $message = esc_html__( 'These field options are invalid.', 'buddypress' );
    … …  
    896936                        }
    897937
    898                         // Trim out empty field options
     938                        // Trim out empty field options.
    899939                        $field_values  = array_values( $_POST[ $option_name ] );
    900940                        $field_options = array_map( 'sanitize_text_field', $field_values );
    901941                        $field_count   = count( $field_options );
    902942
    903                         // Check for missing or malformed options
     943                        // Check for missing or malformed options.
    904944                        if ( 0 === $field_count ) {
    905945                                $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name );
    … …  
    907947                        }
    908948
    909                         // If only one option exists, it cannot be an empty string
     949                        // If only one option exists, it cannot be an empty string.
    910950                        if ( ( 1 === $field_count ) && ( '' === $field_options[0] ) ) {
    911951                                $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name );
    … …  
    929969
    930970        /**
    931          * Oupput the admin form for this field
     971         * Oupput the admin form for this field.
    932972         *
    933973         * @since 1.9.0
    934974         *
    935          * @param type $message
     975         * @param string $message Message to display.
    936976         */
    937977        public function render_admin_form( $message = '' ) {
    … …  
    9761016                                                        <?php
    9771017
    978                                                         // Output the name & description fields
     1018                                                        // Output the name & description fields.
    9791019                                                        $this->name_and_description(); ?>
    9801020
    … …  
    9851025                                                        <?php
    9861026
    987                                                         // Output the sumbit metabox
     1027                                                        // Output the sumbit metabox.
    9881028                                                        $this->submit_metabox( $button );
    9891029
    990                                                         // Output the required metabox
     1030                                                        // Output the required metabox.
    9911031                                                        $this->required_metabox();
    9921032
    … …  
    9941034                                                        $this->member_type_metabox();
    9951035
    996                                                         // Output the field visibility metaboxes
     1036                                                        // Output the field visibility metaboxes.
    9971037                                                        $this->visibility_metabox();
    9981038
    … …  
    10211061                                                        do_action( 'xprofile_field_before_contentbox', $this );
    10221062
    1023                                                         // Output the field attributes metabox
     1063                                                        // Output the field attributes metabox.
    10241064                                                        $this->type_metabox();
    10251065
    1026                                                         // Output hidden inputs for default field
     1066                                                        // Output hidden inputs for default field.
    10271067                                                        $this->default_field_hidden_inputs();
    10281068
    … …  
    10461086
    10471087        /**
    1048          * Private method used to display the submit metabox
     1088         * Private method used to display the submit metabox.
    10491089         *
    10501090         * @since 2.3.0
    10511091         *
    1052          * @param string $button_text
     1092         * @param string $button_text Text to put on button.
    10531093         */
    10541094        private function submit_metabox( $button_text = '' ) {
    … …  
    11151155
    11161156        /**
    1117          * Private method used to output field name and description fields
     1157         * Private method used to output field name and description fields.
    11181158         *
    11191159         * @since 2.3.0
    … …  
    11931233
    11941234        /**
    1195          * Private method used to output field visibility metaboxes
     1235         * Private method used to output field visibility metaboxes.
    11961236         *
    11971237         * @since 2.3.0
    11981238         *
    1199          * @return if default field id 1
     1239         * @return void If default field id 1.
    12001240         */
    12011241        private function visibility_metabox() {
    12021242
    1203                 // Default field cannot have custom visibility
     1243                // Default field cannot have custom visibility.
    12041244                if ( true === $this->is_default_field() ) {
    12051245                        return;
    … …  
    12421282
    12431283        /**
    1244          * Output the metabox for setting if field is required or not
     1284         * Output the metabox for setting if field is required or not.
    12451285         *
    12461286         * @since 2.3.0
    12471287         *
    1248          * @return if default field
     1288         * @return void If default field.
    12491289         */
    12501290        private function required_metabox() {
    12511291
    1252                 // Default field is always required
     1292                // Default field is always required.
    12531293                if ( true === $this->is_default_field() ) {
    12541294                        return;
    … …  
    12691309
    12701310        /**
    1271          * Output the metabox for setting what type of field this is
     1311         * Output the metabox for setting what type of field this is.
    12721312         *
    12731313         * @since 2.3.0
    12741314         *
    1275          * @return if default field
     1315         * @return void If default field.
    12761316         */
    12771317        private function type_metabox() {
    12781318
    1279                 // Default field cannot change type
     1319                // Default field cannot change type.
    12801320                if ( true === $this->is_default_field() ) {
    12811321                        return;
    … …  
    13051345
    13061346        /**
    1307          * Output hidden fields used by default field
     1347         * Output hidden fields used by default field.
    13081348         *
    13091349         * @since 2.3.0
    13101350         *
    1311          * @return if not default field
     1351         * @return void If not default field.
    13121352         */
    13131353        private function default_field_hidden_inputs() {
    13141354
    1315                 // Field 1 is the fullname field, which cannot have custom visibility
     1355                // Field 1 is the fullname field, which cannot have custom visibility.
    13161356                if ( false === $this->is_default_field() ) {
    13171357                        return;
    … …  
    13251365
    13261366        /**
    1327          * Return if a field ID is the default field
     1367         * Return if a field ID is the default field.
    13281368         *
    13291369         * @since 2.3.0
    13301370         *
    1331          * @param  int $field_id ID of field to check
     1371         * @param int $field_id ID of field to check.
     1372         *
    13321373         * @return bool
    13331374         */
    13341375        private function is_default_field( $field_id = 0 ) {
    13351376
    1336                 // Fallback to current field ID if none passed
     1377                // Fallback to current field ID if none passed.
    13371378                if ( empty( $field_id ) ) {
    13381379                        $field_id = $this->id;
    13391380                }
    13401381
    1341                 // Compare & return
     1382                // Compare & return.
    13421383                return (bool) ( 1 === (int) $field_id );
    13431384        }
Note: See TracChangeset for help on using the changeset viewer.