Skip to:
Content

BuddyPress.org

Changeset 9667


Ignore:
Timestamp:
03/31/2015 11:21:54 PM (11 years ago)
Author:
johnjamesjacoby
Message:

XProfile: Improvements to BP_XProfile_Field:

  • PHPDoc all variables and methods
  • Simplify verbiage for visibility metaboxes
  • Remove redundant "Field" verbiage in metabox titles
  • Introduce methods for outputting each individual metabox
  • Move "Required" metabox to sidebar (for improved symmetry)
  • Audit method parameters and ensure types and default values match what is expected in the call-stack
  • Remove unused $message variables from class
  • Stylistic tweaks to remove bottom margin from primary name & description area

See #6318.

Location:
trunk/src/bp-xprofile
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/admin/css/admin-rtl.css

    r9647 r9667  
    272272        margin-bottom: 20px;
    273273}
     274#bp-xprofile-add-field #post-body-content {
     275        margin-bottom: 0;
     276}
  • trunk/src/bp-xprofile/admin/css/admin.css

    r9647 r9667  
    272272        margin-bottom: 20px;
    273273}
     274#bp-xprofile-add-field #post-body-content {
     275        margin-bottom: 0;
     276}
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php

    r9659 r9667  
    1111
    1212class BP_XProfile_Field {
     13
     14        /**
     15         * @since BuddyPress (1.0.0)
     16         *
     17         * @var int ID of field
     18         */
    1319        public $id;
     20
     21        /**
     22         * @since BuddyPress (1.0.0)
     23         *
     24         * @var int Field group ID for field
     25         */
    1426        public $group_id;
     27
     28        /**
     29         * @since BuddyPress (1.0.0)
     30         *
     31         * @var int Parent ID of field
     32         */
    1533        public $parent_id;
     34
     35        /**
     36         * @since BuddyPress (1.0.0)
     37         *
     38         * @var string Field type
     39         */
    1640        public $type;
     41
     42        /**
     43         * @since BuddyPress (1.0.0)
     44         *
     45         * @var string Field name
     46         */
    1747        public $name;
     48
     49        /**
     50         * @since BuddyPress (1.0.0)
     51         *
     52         * @var string Field description
     53         */
    1854        public $description;
     55
     56        /**
     57         * @since BuddyPress (1.0.0)
     58         *
     59         * @var bool Is field required to be filled out?
     60         */
    1961        public $is_required;
     62
     63        /**
     64         * @since BuddyPress (1.0.0)
     65         *
     66         * @var int Can field be deleted?
     67         */
    2068        public $can_delete = '1';
     69
     70        /**
     71         * @since BuddyPress (1.0.0)
     72         *
     73         * @var int Field position
     74         */
    2175        public $field_order;
     76
     77        /**
     78         * @since BuddyPress (1.0.0)
     79         *
     80         * @var int Option order
     81         */
    2282        public $option_order;
     83
     84        /**
     85         * @since BuddyPress (1.0.0)
     86         *
     87         * @var string Order child fields by
     88         */
    2389        public $order_by;
     90
     91        /**
     92         * @since BuddyPress (1.0.0)
     93         *
     94         * @var bool Is this the default option for this field?
     95         */
    2496        public $is_default_option;
     97
     98        /**
     99         * @since BuddyPress (1.9.0)
     100         *
     101         * @var string Default field data visibility
     102         */
    25103        public $default_visibility = 'public';
     104
     105        /**
     106         * @since BuddyPress (2.3.0)
     107         *
     108         * @var string Members are allowed/disallowed to modify data visibility
     109         */
    26110        public $allow_custom_visibility = 'allowed';
    27111
    28112        /**
    29113         * @since BuddyPress (2.0.0)
     114         *
    30115         * @var BP_XProfile_Field_Type Field type object used for validation
    31116         */
    32117        public $type_obj = null;
    33118
     119        /**
     120         * @since BuddyPress (2.0.0)
     121         *
     122         * @var BP_XProfile_ProfileData Field data for user ID
     123         */
    34124        public $data;
    35         public $message = null;
    36         public $message_type = 'err';
    37 
     125
     126        /**
     127         * Populate or initialize a profile field
     128         *
     129         * @since BuddyPress (1.1.0)
     130         *
     131         * @param int  $id
     132         * @param int  $user_id
     133         * @param bool $get_data
     134         */
    38135        public function __construct( $id = null, $user_id = null, $get_data = true ) {
    39                 if ( !empty( $id ) ) {
     136
     137                if ( ! empty( $id ) ) {
    40138                        $this->populate( $id, $user_id, $get_data );
    41139
     
    47145        }
    48146
    49         public function populate( $id, $user_id, $get_data ) {
     147        /**
     148         * Populate a profile field with
     149         *
     150         * @since BuddyPress (1.1.0)
     151         *
     152         * @global object $wpdb
     153         * @global object $userdata
     154         *
     155         * @param  int    $id
     156         * @param  int    $user_id
     157         * @param  bool   $get_data
     158         */
     159        public function populate( $id, $user_id = null, $get_data = true ) {
    50160                global $wpdb, $userdata;
    51161
     
    58168
    59169                if ( ! empty( $field ) ) {
    60                         $this->id               = $field->id;
     170                        $this->id                = $field->id;
    61171                        $this->group_id          = $field->group_id;
    62172                        $this->parent_id         = $field->parent_id;
     
    75185                        $this->type_obj->field_obj = $this;
    76186
    77                         if ( $get_data && $user_id ) {
    78                                 $this->data          = $this->get_field_data( $user_id );
     187                        if ( ! empty( $get_data ) && ! empty( $user_id ) ) {
     188                                $this->data = $this->get_field_data( $user_id );
    79189                        }
    80190
    81                         $this->default_visibility = bp_xprofile_get_meta( $id, 'field', 'default_visibility' );
    82 
    83                         if ( empty( $this->default_visibility ) ) {
    84                                 $this->default_visibility = 'public';
    85                         }
    86 
    87                         $this->allow_custom_visibility = 'disabled' == bp_xprofile_get_meta( $id, 'field', 'allow_custom_visibility' ) ? 'disabled' : 'allowed';
    88                 }
    89         }
    90 
     191                        // Get metadata for field
     192                        $default_visibility       = bp_xprofile_get_meta( $id, 'field', 'default_visibility'      );
     193                        $allow_custom_visibility  = bp_xprofile_get_meta( $id, 'field', 'allow_custom_visibility' );
     194
     195                        // Setup default visibility
     196                        $this->default_visibility = ! empty( $default_visibility )
     197                                ? $default_visibility
     198                                : 'public';
     199
     200                        // Allow members to customize visibilty
     201                        $this->allow_custom_visibility = ( 'disabled' === $allow_custom_visibility )
     202                                ? 'disabled'
     203                                : 'allowed';
     204                }
     205        }
     206
     207        /**
     208         * Delete a profile field
     209         *
     210         * @since BuddyPress (1.1.0)
     211         *
     212         * @global object  $wpdb
     213         * @param  boolean $delete_data
     214         * @return boolean
     215         */
    91216        public function delete( $delete_data = false ) {
    92217                global $wpdb;
     
    99224                }
    100225
    101                 $bp = buddypress();
    102 
    103                 if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE id = %d OR parent_id = %d", $this->id, $this->id ) ) ) {
     226                $bp  = buddypress();
     227                $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE id = %d OR parent_id = %d", $this->id, $this->id );
     228
     229                if ( ! $wpdb->query( $sql ) ) {
    104230                        return false;
    105231                }
     
    113239        }
    114240
     241        /**
     242         * Save a profile field
     243         *
     244         * @since BuddyPress (1.1.0)
     245         *
     246         * @global object $wpdb
     247         *
     248         * @return boolean
     249         */
    115250        public function save() {
    116251                global $wpdb;
     
    118253                $bp = buddypress();
    119254
    120                 $this->group_id    = apply_filters( 'xprofile_field_group_id_before_save',    $this->group_id,    $this->id );
    121                 $this->parent_id   = apply_filters( 'xprofile_field_parent_id_before_save',   $this->parent_id,   $this->id );
    122                 $this->type        = apply_filters( 'xprofile_field_type_before_save',        $this->type,        $this->id );
    123                 $this->name        = apply_filters( 'xprofile_field_name_before_save',        $this->name,        $this->id );
    124                 $this->description = apply_filters( 'xprofile_field_description_before_save', $this->description, $this->id );
    125                 $this->is_required = apply_filters( 'xprofile_field_is_required_before_save', $this->is_required, $this->id );
    126                 $this->order_by    = apply_filters( 'xprofile_field_order_by_before_save',    $this->order_by,    $this->id );
    127                 $this->field_order = apply_filters( 'xprofile_field_field_order_before_save', $this->field_order, $this->id );
     255                $this->group_id     = apply_filters( 'xprofile_field_group_id_before_save',     $this->group_id,     $this->id );
     256                $this->parent_id    = apply_filters( 'xprofile_field_parent_id_before_save',    $this->parent_id,    $this->id );
     257                $this->type         = apply_filters( 'xprofile_field_type_before_save',         $this->type,         $this->id );
     258                $this->name         = apply_filters( 'xprofile_field_name_before_save',         $this->name,         $this->id );
     259                $this->description  = apply_filters( 'xprofile_field_description_before_save',  $this->description, $this->id );
     260                $this->is_required  = apply_filters( 'xprofile_field_is_required_before_save',  $this->is_required, $this->id );
     261                $this->order_by     = apply_filters( 'xprofile_field_order_by_before_save',     $this->order_by,     $this->id );
     262                $this->field_order  = apply_filters( 'xprofile_field_field_order_before_save',  $this->field_order, $this->id );
    128263                $this->option_order = apply_filters( 'xprofile_field_option_order_before_save', $this->option_order, $this->id );
    129                 $this->can_delete  = apply_filters( 'xprofile_field_can_delete_before_save',  $this->can_delete,  $this->id );
    130                 $this->type_obj    = bp_xprofile_create_field_type( $this->type );
     264                $this->can_delete   = apply_filters( 'xprofile_field_can_delete_before_save',   $this->can_delete,   $this->id );
     265                $this->type_obj     = bp_xprofile_create_field_type( $this->type );
    131266
    132267                /**
     
    148283
    149284                /**
    150                  * Check for null so field options can be changed without changing any other part of the field.
    151                  * The described situation will return 0 here.
     285                 * Check for null so field options can be changed without changing any
     286                 * other part of the field. The described situation will return 0 here.
    152287                 */
    153288                if ( $wpdb->query( $sql ) !== null ) {
     
    184319
    185320                                // Allow plugins to filter the field's child options (i.e. the items in a selectbox).
    186                                 $post_option  = ! empty( $_POST["{$this->type}_option"] ) ? $_POST["{$this->type}_option"] : '';
     321                                $post_option  = ! empty( $_POST["{$this->type}_option"]           ) ? $_POST["{$this->type}_option"]          : '';
    187322                                $post_default = ! empty( $_POST["isDefault_{$this->type}_option"] ) ? $_POST["isDefault_{$this->type}_option"] : '';
    188323
     
    213348
    214349                                                if ( is_array( $defaults ) ) {
    215                                                         if ( isset( $defaults[$option_key] ) ) {
     350                                                        if ( isset( $defaults[ $option_key ] ) ) {
    216351                                                                $is_default = 1;
    217352                                                        }
     
    223358
    224359                                                if ( '' != $option_value ) {
    225                                                         if ( !$wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, option_order, is_default_option) VALUES (%d, %d, 'option', %s, '', 0, %d, %d)", $this->group_id, $parent_id, $option_value, $counter, $is_default ) ) ) {
     360                                                        $sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, option_order, is_default_option) VALUES (%d, %d, 'option', %s, '', 0, %d, %d)", $this->group_id, $parent_id, $option_value, $counter, $is_default );
     361                                                        if ( ! $wpdb->query( $sql ) ) {
    226362                                                                return false;
    227363                                                        }
     
    252388        }
    253389
    254         public function get_field_data( $user_id ) {
     390        /**
     391         * Get field data for a user ID
     392         *
     393         * @since BuddyPress (1.2.0)
     394         *
     395         * @param  int $user_id
     396         * @return object
     397         */
     398        public function get_field_data( $user_id = 0 ) {
    255399                return new BP_XProfile_ProfileData( $this->id, $user_id );
    256400        }
    257401
     402        /**
     403         * Get all child fields for this field ID
     404         *
     405         * @since BuddyPress (1.2.0)
     406         *
     407         * @global object $wpdb
     408         *
     409         * @param  bool  $for_editing
     410         * @return array
     411         */
    258412        public function get_children( $for_editing = false ) {
    259413                global $wpdb;
    260414
    261415                // This is done here so we don't have problems with sql injection
    262                 if ( 'asc' == $this->order_by && empty( $for_editing ) ) {
     416                if ( empty( $for_editing ) && ( 'asc' === $this->order_by ) ) {
    263417                        $sort_sql = 'ORDER BY name ASC';
    264                 } elseif ( 'desc' == $this->order_by && empty( $for_editing ) ) {
     418                } elseif ( empty( $for_editing ) && ( 'desc' === $this->order_by ) ) {
    265419                        $sort_sql = 'ORDER BY name DESC';
    266420                } else {
     
    268422                }
    269423
    270                 // This eliminates a problem with getting all fields when there is no id for the object
     424                // This eliminates a problem with getting all fields when there is no
     425                // id for the object
    271426                if ( empty( $this->id ) ) {
    272427                        $parent_id = -1;
     
    276431
    277432                $bp  = buddypress();
    278                 $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE parent_id = %d AND group_id = %d $sort_sql", $parent_id, $this->group_id );
     433                $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE parent_id = %d AND group_id = %d {$sort_sql}", $parent_id, $this->group_id );
    279434
    280435                $children = $wpdb->get_results( $sql );
     
    291446        }
    292447
     448        /**
     449         * Delete all field children for this field
     450         *
     451         * @since BuddyPress (1.2.0)
     452         *
     453         * @global object $wpdb
     454         */
    293455        public function delete_children() {
    294456                global $wpdb;
     
    302464        /** Static Methods ********************************************************/
    303465
    304         public static function get_type( $field_id ) {
     466        public static function get_type( $field_id = 0 ) {
    305467                global $wpdb;
    306468
    307                 if ( !empty( $field_id ) ) {
    308                         $bp  = buddypress();
    309                         $sql = $wpdb->prepare( "SELECT type FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id );
    310 
    311                         if ( !$field_type = $wpdb->get_var( $sql ) ) {
     469                // Bail if no field ID
     470                if ( empty( $field_id ) ) {
     471                        return false;
     472                }
     473
     474                $bp   = buddypress();
     475                $sql  = $wpdb->prepare( "SELECT type FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id );
     476                $type = $wpdb->get_var( $sql );
     477
     478                // Return field type
     479                if ( ! empty( $type ) ) {
     480                        return $type;
     481                }
     482
     483                return false;
     484        }
     485
     486        /**
     487         * Delete all fields in a field group
     488         *
     489         * @since BuddyPress (1.2.0)
     490         *
     491         * @global object $wpdb
     492         *
     493         * @param  int    $group_id
     494         *
     495         * @return boolean
     496         */
     497        public static function delete_for_group( $group_id = 0 ) {
     498                global $wpdb;
     499
     500                // Bail if no group ID
     501                if ( empty( $group_id ) ) {
     502                        return false;
     503                }
     504
     505                $bp      = buddypress();
     506                $sql     = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id );
     507                $deleted = $wpdb->get_var( $sql );
     508
     509                // Return true if fields were deleted
     510                if ( false !== $deleted ) {
     511                        return true;
     512                }
     513
     514                return false;
     515        }
     516
     517        /**
     518         * Get field ID from field name
     519         *
     520         * @since BuddyPress (1.5.0)
     521         *
     522         * @global object $wpdb
     523         * @param  string $field_name
     524         *
     525         * @return boolean
     526         */
     527        public static function get_id_from_name( $field_name = '' ) {
     528                global $wpdb;
     529
     530                $bp = buddypress();
     531
     532                if ( empty( $bp->profile->table_name_fields ) || empty( $field_name ) ) {
     533                        return false;
     534                }
     535
     536                $sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s AND parent_id = 0", $field_name );
     537
     538                return $wpdb->get_var( $sql );
     539        }
     540
     541        /**
     542         * Update field position and/or field group when relocating
     543         *
     544         * @since BuddyPress (1.5.0)
     545         *
     546         * @global object $wpdb
     547         *
     548         * @param  int $field_id
     549         * @param  int $position
     550         * @param  int $field_group_id
     551         *
     552         * @return boolean
     553         */
     554        public static function update_position( $field_id, $position = null, $field_group_id = null ) {
     555                global $wpdb;
     556
     557                // Bail if invalid position or field group
     558                if ( ! is_numeric( $position ) || ! is_numeric( $field_group_id ) ) {
     559                        return false;
     560                }
     561
     562                // Get table name and field parent
     563                $table_name = buddypress()->profile->table_name_fields;
     564                $sql        = $wpdb->prepare( "UPDATE {$table_name} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id );
     565                $parent     = $wpdb->query( $sql );
     566
     567                // Update $field_id with new $position and $field_group_id
     568                if ( ! empty( $parent ) && ! is_wp_error( $parent ) ) {
     569
     570                        // Update any children of this $field_id
     571                        $sql = $wpdb->prepare( "UPDATE {$table_name} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id );
     572                        $wpdb->query( $sql );
     573
     574                        return $parent;
     575                }
     576
     577                return false;
     578        }
     579
     580        /**
     581         * Validate form field data on sumbission
     582         *
     583         * @since BuddyPress (2.2.0)
     584         *
     585         * @global type $message
     586         * @return boolean
     587         */
     588        public static function admin_validate() {
     589                global $message;
     590
     591                // Validate Form
     592                if ( empty( $_POST['title'] ) || ! isset( $_POST['required'] ) || empty( $_POST['fieldtype'] ) ) {
     593                        $message = __( 'Please make sure you fill out all required fields.', 'buddypress' );
     594                        return false;
     595
     596                } elseif ( empty( $_POST['field_file'] ) ) {
     597                        $field_type  = bp_xprofile_create_field_type( $_POST['fieldtype'] );
     598                        $option_name = "{$_POST['fieldtype']}_option";
     599
     600                        if ( ! empty( $field_type->supports_options ) && isset( $_POST[ $option_name ] ) && empty( $_POST[ $option_name ][1] ) ) {
     601                                $message = __( 'This field type requires at least one option. Please add options below.', 'buddypress' );
    312602                                return false;
    313603                        }
    314 
    315                         return $field_type;
    316                 }
    317 
    318                 return false;
    319         }
    320 
    321         public static function delete_for_group( $group_id ) {
    322                 global $wpdb;
    323 
    324                 if ( !empty( $group_id ) ) {
    325                         $bp  = buddypress();
    326                         $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id );
    327 
    328                         if ( $wpdb->get_var( $sql ) === false ) {
    329                                 return false;
    330                         }
    331 
    332                         return true;
    333                 }
    334 
    335                 return false;
    336         }
    337 
    338         public static function get_id_from_name( $field_name ) {
    339                 global $wpdb;
    340 
    341                 $bp = buddypress();
    342 
    343                 if ( empty( $bp->profile->table_name_fields ) || !isset( $field_name ) ) {
    344                         return false;
    345                 }
    346 
    347                 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s AND parent_id = 0", $field_name ) );
    348         }
    349 
    350         public static function update_position( $field_id, $position, $field_group_id ) {
    351                 global $wpdb;
    352 
    353                 if ( !is_numeric( $position ) || !is_numeric( $field_group_id ) ) {
    354                         return false;
    355                 }
    356 
    357                 $bp = buddypress();
    358 
    359                 // Update $field_id with new $position and $field_group_id
    360                 if ( $parent = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_fields} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id ) ) ) {;
    361 
    362                         // Update any children of this $field_id
    363                         $children = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_fields} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id ) );
    364 
    365                         return $parent;
    366                 }
    367 
    368                 return false;
    369         }
    370 
    371         /**
    372          * This function populates the items for radio buttons checkboxes and drop down boxes
     604                }
     605
     606                return true;
     607        }
     608
     609        /**
     610         * This function populates the items for radio buttons checkboxes and drop
     611         * down boxes.
    373612         */
    374613        public function render_admin_form_children() {
     
    379618        }
    380619
     620        /**
     621         * Oupput the admin form for this field
     622         *
     623         * @since BuddyPress (1.9.0)
     624         *
     625         * @param type $message
     626         */
    381627        public function render_admin_form( $message = '' ) {
    382628                if ( empty( $this->id ) ) {
     
    417663                                        <div id="post-body" class="metabox-holder columns-<?php echo ( 1 == get_current_screen()->get_columns() ) ? '1' : '2'; ?>">
    418664                                                <div id="post-body-content">
    419                                                         <div id="titlediv">
    420                                                                 <div class="titlewrap">
    421                                                                         <label id="title-prompt-text" for="title"><?php echo esc_attr_x( 'Field Name', 'XProfile admin edit field', 'buddypress' ); ?></label>
    422                                                                         <input type="text" name="title" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" />
    423                                                                 </div>
    424                                                         </div>
    425                                                         <div class="postbox">
    426                                                                 <h3><?php _e( 'Field Description', 'buddypress' ); ?></h3>
    427                                                                 <div class="inside">
    428                                                                         <textarea name="description" id="description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea>
    429                                                                 </div>
    430                                                         </div>
     665
     666                                                        <?php
     667
     668                                                        // Output the name & description fields
     669                                                        $this->name_and_description(); ?>
     670
    431671                                                </div><!-- #post-body-content -->
    432672
     
    435675                                                        <?php
    436676
    437                                                         /**
    438                                                          * Fires before XProfile Field submit metabox.
    439                                                          *
    440                                                          * @since BuddyPress (2.1.0)
    441                                                          *
    442                                                          * @param BP_XProfile_Field $this Current XProfile field.
    443                                                          */
    444                                                         do_action( 'xprofile_field_before_submitbox', $this );
    445                                                         ?>
    446 
    447                                                         <div id="submitdiv" class="postbox">
    448                                                                 <h3><?php _e( 'Submit', 'buddypress' ); ?></h3>
    449                                                                 <div class="inside">
    450                                                                         <div id="submitcomment" class="submitbox">
    451                                                                                 <div id="major-publishing-actions">
    452 
    453                                                                                         <?php
    454 
    455                                                                                         /**
    456                                                                                          * Fires at the beginning of the XProfile Field publishing actions section.
    457                                                                                          *
    458                                                                                          * @since BuddyPress (2.1.0)
    459                                                                                          *
    460                                                                                          * @param BP_XProfile_Field $this Current XProfile field.
    461                                                                                          */
    462                                                                                         do_action( 'xprofile_field_submitbox_start', $this );
    463                                                                                         ?>
    464 
    465                                                                                         <input type="hidden" name="field_order" id="field_order" value="<?php echo esc_attr( $this->field_order ); ?>" />
    466                                                                                         <div id="publishing-action">
    467                                                                                                 <input type="submit" name="saveField" value="<?php echo esc_attr( $button ); ?>" class="button-primary" />
    468                                                                                         </div>
    469                                                                                         <div id="delete-action">
    470                                                                                                 <a href="users.php?page=bp-profile-setup" class="deletion"><?php _e( 'Cancel', 'buddypress' ); ?></a>
    471                                                                                         </div>
    472                                                                                         <?php wp_nonce_field( 'xprofile_delete_option' ); ?>
    473                                                                                         <div class="clear"></div>
    474                                                                                 </div>
    475                                                                         </div>
    476                                                                 </div>
    477                                                         </div>
    478 
    479                                                         <?php
    480 
    481                                                         /**
    482                                                          * Fires after XProfile Field submit metabox.
    483                                                          *
    484                                                          * @since BuddyPress (2.1.0)
    485                                                          *
    486                                                          * @param BP_XProfile_Field $this Current XProfile field.
    487                                                          */
    488                                                         do_action( 'xprofile_field_after_submitbox', $this );
    489                                                         ?>
    490 
    491                                                         <?php /* Field 1 is the fullname field, which cannot have custom visibility */ ?>
    492                                                         <?php if ( 1 != $this->id ) : ?>
    493 
    494                                                                 <div class="postbox">
    495                                                                         <h3><label for="default-visibility"><?php _e( 'Default Visibility', 'buddypress' ); ?></label></h3>
    496                                                                         <div class="inside">
    497                                                                                 <p><?php esc_html_e( 'Data in this field is visible to:', 'buddypress' ); ?></p>
    498                                                                                 <ul>
    499 
    500                                                                                         <?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?>
    501 
    502                                                                                                 <li>
    503                                                                                                         <input type="radio" id="default-visibility[<?php echo esc_attr( $level['id'] ) ?>]" name="default-visibility" value="<?php echo esc_attr( $level['id'] ) ?>" <?php checked( $this->default_visibility, $level['id'] ); ?> />
    504                                                                                                         <label for="default-visibility[<?php echo esc_attr( $level['id'] ) ?>]"><?php echo esc_html( $level['label'] ) ?></label>
    505                                                                                                 </li>
    506 
    507                                                                                         <?php endforeach ?>
    508 
    509                                                                                 </ul>
    510                                                                         </div>
    511                                                                 </div>
    512 
    513                                                                 <div class="postbox">
    514                                                                         <h3><label for="allow-custom-visibility"><?php _e( 'Visibility Override', 'buddypress' ); ?></label></h3>
    515                                                                         <div class="inside">
    516                                                                                 <ul>
    517                                                                                         <li>
    518                                                                                                 <input type="radio" id="allow-custom-visibility-allowed" name="allow-custom-visibility" value="allowed" <?php checked( $this->allow_custom_visibility, 'allowed' ); ?> />
    519                                                                                                 <label for="allow-custom-visibility-allowed"><?php _e( "Members can modify", 'buddypress' ); ?></label>
    520                                                                                         </li>
    521                                                                                         <li>
    522                                                                                                 <input type="radio" id="allow-custom-visibility-disabled" name="allow-custom-visibility" value="disabled" <?php checked( $this->allow_custom_visibility, 'disabled' ); ?> />
    523                                                                                                 <label for="allow-custom-visibility-disabled"><?php _e( 'Members cannot modify', 'buddypress' ); ?></label>
    524                                                                                         </li>
    525                                                                                 </ul>
    526                                                                         </div>
    527                                                                 </div>
    528 
    529                                                         <?php endif ?>
    530 
    531                                                         <?php
     677                                                        // Output the sumbit metabox
     678                                                        $this->submit_metabox( $button );
     679
     680                                                        // Output the required metabox
     681                                                        $this->required_metabox();
     682
     683                                                        // Output the field visibility metaboxes
     684                                                        $this->visibility_metabox();
    532685
    533686                                                        /**
     
    539692                                                         */
    540693                                                        do_action( 'xprofile_field_after_sidebarbox', $this ); ?>
     694
    541695                                                </div>
    542696
    543697                                                <div id="postbox-container-2" class="postbox-container">
    544698
    545                                                         <?php /* Field 1 is the fullname field, which cannot be altered */ ?>
    546                                                         <?php if ( 1 != $this->id ) : ?>
    547 
    548                                                                 <div class="postbox">
    549                                                                         <h3><label for="required"><?php _e( "Field Requirement", 'buddypress' ); ?></label></h3>
    550                                                                         <div class="inside">
    551                                                                                 <select name="required" id="required" style="width: 30%">
    552                                                                                         <option value="0"<?php selected( $this->is_required, '0' ); ?>><?php _e( 'Not Required', 'buddypress' ); ?></option>
    553                                                                                         <option value="1"<?php selected( $this->is_required, '1' ); ?>><?php _e( 'Required',     'buddypress' ); ?></option>
    554                                                                                 </select>
    555                                                                         </div>
    556                                                                 </div>
    557 
    558                                                                 <div class="postbox">
    559                                                                         <h3><label for="fieldtype"><?php _e( 'Field Type', 'buddypress'); ?></label></h3>
    560                                                                         <div class="inside">
    561                                                                                 <select name="fieldtype" id="fieldtype" onchange="show_options(this.value)" style="width: 30%">
    562                                                                                         <?php bp_xprofile_admin_form_field_types( $this->type ); ?>
    563                                                                                 </select>
    564 
    565                                                                                 <?php
    566                                                                                 // Deprecated filter, don't use. Go look at {@link BP_XProfile_Field_Type::admin_new_field_html()}.
    567                                                                                 do_action( 'xprofile_field_additional_options', $this );
    568 
    569                                                                                 $this->render_admin_form_children();
    570                                                                                 ?>
    571                                                                         </div>
    572                                                                 </div>
    573 
    574                                                         <?php else : ?>
    575 
    576                                                                 <input type="hidden" name="required"  id="required"  value="1"       />
    577                                                                 <input type="hidden" name="fieldtype" id="fieldtype" value="textbox" />
    578 
    579                                                         <?php endif; ?>
    580 
    581699                                                        <?php
     700
     701                                                        /**
     702                                                         * Fires before XProfile Field content metabox.
     703                                                         *
     704                                                         * @since BuddyPress (2.3.0)
     705                                                         *
     706                                                         * @param BP_XProfile_Field $this Current XProfile field.
     707                                                         */
     708                                                        do_action( 'xprofile_field_before_contentbox', $this );
     709
     710                                                        // Output the field attributes metabox
     711                                                        $this->type_metabox();
     712
     713                                                        // Output hidden inputs for default field
     714                                                        $this->default_field_hidden_inputs();
    582715
    583716                                                        /**
     
    589722                                                         */
    590723                                                        do_action( 'xprofile_field_after_contentbox', $this ); ?>
     724
    591725                                                </div>
    592726                                        </div><!-- #post-body -->
    593 
    594727                                </div><!-- #poststuff -->
    595 
    596728                        </form>
    597729                </div>
    598730
    599 <?php
    600         }
    601 
    602         public static function admin_validate() {
    603                 global $message;
    604 
    605                 // Validate Form
    606                 if ( empty( $_POST['title'] ) || ! isset( $_POST['required'] ) || empty( $_POST['fieldtype'] ) ) {
    607                         $message = __( 'Please make sure you fill out all required fields.', 'buddypress' );
    608                         return false;
    609 
    610                 } elseif ( empty( $_POST['field_file'] ) ) {
    611                         $field_type  = bp_xprofile_create_field_type( $_POST['fieldtype'] );
    612                         $option_name = "{$_POST['fieldtype']}_option";
    613 
    614                         if ( ! empty( $field_type->supports_options ) && isset( $_POST[$option_name] ) && empty( $_POST[$option_name][1] ) ) {
    615                                 $message = __( 'This field type requires at least one option. Please add options below.', 'buddypress' );
    616                                 return false;
    617                         }
    618                 }
    619 
    620                 return true;
     731        <?php
     732        }
     733
     734        /**
     735         * Private method used to display the submit metabox
     736         *
     737         * @since BuddyPress (2.3.0)
     738         *
     739         * @param string $button_text
     740         */
     741        private function submit_metabox( $button_text = '' ) {
     742
     743                /**
     744                 * Fires before XProfile Field submit metabox.
     745                 *
     746                 * @since BuddyPress (2.1.0)
     747                 *
     748                 * @param BP_XProfile_Field $this Current XProfile field.
     749                 */
     750                do_action( 'xprofile_field_before_submitbox', $this ); ?>
     751
     752                <div id="submitdiv" class="postbox">
     753                        <h3><?php esc_html_e( 'Submit', 'buddypress' ); ?></h3>
     754                        <div class="inside">
     755                                <div id="submitcomment" class="submitbox">
     756                                        <div id="major-publishing-actions">
     757
     758                                                <?php
     759
     760                                                /**
     761                                                 * Fires at the beginning of the XProfile Field publishing actions section.
     762                                                 *
     763                                                 * @since BuddyPress (2.1.0)
     764                                                 *
     765                                                 * @param BP_XProfile_Field $this Current XProfile field.
     766                                                 */
     767                                                do_action( 'xprofile_field_submitbox_start', $this ); ?>
     768
     769                                                <input type="hidden" name="field_order" id="field_order" value="<?php echo esc_attr( $this->field_order ); ?>" />
     770
     771                                                <?php if ( ! empty( $button_text ) ) : ?>
     772
     773                                                        <div id="publishing-action">
     774                                                                <input type="submit" name="saveField" value="<?php echo esc_attr( $button_text ); ?>" class="button-primary" />
     775                                                        </div>
     776
     777                                                <?php endif; ?>
     778
     779                                                <div id="delete-action">
     780                                                        <a href="users.php?page=bp-profile-setup" class="deletion"><?php esc_html_e( 'Cancel', 'buddypress' ); ?></a>
     781                                                </div>
     782
     783                                                <?php wp_nonce_field( 'xprofile_delete_option' ); ?>
     784
     785                                                <div class="clear"></div>
     786                                        </div>
     787                                </div>
     788                        </div>
     789                </div>
     790
     791                <?php
     792
     793                /**
     794                 * Fires after XProfile Field submit metabox.
     795                 *
     796                 * @since BuddyPress (2.1.0)
     797                 *
     798                 * @param BP_XProfile_Field $this Current XProfile field.
     799                 */
     800                do_action( 'xprofile_field_after_submitbox', $this );
     801        }
     802
     803        /**
     804         * Private method used to output field name and description fields
     805         *
     806         * @since BuddyPress (2.3.0)
     807         */
     808        private function name_and_description() {
     809        ?>
     810
     811                <div id="titlediv">
     812                        <div class="titlewrap">
     813                                <label id="title-prompt-text" for="title"><?php echo esc_html_x( 'Name', 'XProfile admin edit field', 'buddypress' ); ?></label>
     814                                <input type="text" name="title" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" />
     815                        </div>
     816                </div>
     817
     818                <div class="postbox">
     819                        <h3><?php echo esc_html_x( 'Description', 'XProfile admin edit field', 'buddypress' ); ?></h3>
     820                        <div class="inside">
     821                                <textarea name="description" id="description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea>
     822                        </div>
     823                </div>
     824
     825        <?php
     826        }
     827
     828        /**
     829         * Private method used to output field visibility metaboxes
     830         *
     831         * @since BuddyPress (2.3.0)
     832         *
     833         * @return if default field id 1
     834         */
     835        private function visibility_metabox() {
     836
     837                // Default field cannot have custom visibility
     838                if ( true === $this->is_default_field() ) {
     839                        return;
     840                } ?>
     841
     842                <div class="postbox">
     843                        <h3><label for="default-visibility"><?php esc_html_e( 'Visibility', 'buddypress' ); ?></label></h3>
     844                        <div class="inside">
     845                                <div>
     846                                        <select name="default-visibility" >
     847
     848                                                <?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?>
     849
     850                                                        <option value="<?php echo esc_attr( $level['id'] ); ?>" <?php selected( $this->default_visibility, $level['id'] ); ?>>
     851                                                                <?php echo esc_html( $level['label'] ); ?>
     852                                                        </option>
     853
     854                                                <?php endforeach ?>
     855
     856                                        </select>
     857                                </div>
     858
     859                                <div>
     860                                        <ul>
     861                                                <li>
     862                                                        <input type="radio" id="allow-custom-visibility-allowed" name="allow-custom-visibility" value="allowed" <?php checked( $this->allow_custom_visibility, 'allowed' ); ?> />
     863                                                        <label for="allow-custom-visibility-allowed"><?php esc_html_e( 'Allow members to override', 'buddypress' ); ?></label>
     864                                                </li>
     865                                                <li>
     866                                                        <input type="radio" id="allow-custom-visibility-disabled" name="allow-custom-visibility" value="disabled" <?php checked( $this->allow_custom_visibility, 'disabled' ); ?> />
     867                                                        <label for="allow-custom-visibility-disabled"><?php esc_html_e( 'Enforce field visibility', 'buddypress' ); ?></label>
     868                                                </li>
     869                                        </ul>
     870                                </div>
     871                        </div>
     872                </div>
     873
     874                <?php
     875        }
     876
     877        /**
     878         * Output the metabox for setting if field is required or not
     879         *
     880         * @since BuddyPress (2.3.0)
     881         *
     882         * @return if default field
     883         */
     884        private function required_metabox() {
     885
     886                // Default field is always required
     887                if ( true === $this->is_default_field() ) {
     888                        return;
     889                } ?>
     890
     891                <div class="postbox">
     892                        <h3><label for="required"><?php esc_html_e( 'Requirement', 'buddypress' ); ?></label></h3>
     893                        <div class="inside">
     894                                <select name="required" id="required">
     895                                        <option value="0"<?php selected( $this->is_required, '0' ); ?>><?php esc_html_e( 'Not Required', 'buddypress' ); ?></option>
     896                                        <option value="1"<?php selected( $this->is_required, '1' ); ?>><?php esc_html_e( 'Required',     'buddypress' ); ?></option>
     897                                </select>
     898                        </div>
     899                </div>
     900
     901        <?php
     902        }
     903
     904        /**
     905         * Output the metabox for setting what type of field this is
     906         *
     907         * @since BuddyPress (2.3.0)
     908         *
     909         * @return if default field
     910         */
     911        private function type_metabox() {
     912
     913                // Default field cannot change type
     914                if ( true === $this->is_default_field() ) {
     915                        return;
     916                } ?>
     917
     918                <div class="postbox">
     919                        <h3><label for="fieldtype"><?php esc_html_e( 'Type', 'buddypress'); ?></label></h3>
     920                        <div class="inside">
     921                                <select name="fieldtype" id="fieldtype" onchange="show_options(this.value)" style="width: 30%">
     922
     923                                        <?php bp_xprofile_admin_form_field_types( $this->type ); ?>
     924
     925                                </select>
     926
     927                                <?php
     928
     929                                // Deprecated filter, don't use. Go look at {@link BP_XProfile_Field_Type::admin_new_field_html()}.
     930                                do_action( 'xprofile_field_additional_options', $this );
     931
     932                                $this->render_admin_form_children(); ?>
     933
     934                        </div>
     935                </div>
     936
     937        <?php
     938        }
     939
     940        /**
     941         * Output hidden fields used by default field
     942         *
     943         * @since BuddyPress (2.3.0)
     944         *
     945         * @return if not default field
     946         */
     947        private function default_field_hidden_inputs() {
     948
     949                // Field 1 is the fullname field, which cannot have custom visibility
     950                if ( false === $this->is_default_field() ) {
     951                        return;
     952                } ?>
     953
     954                <input type="hidden" name="required"  id="required"  value="1"       />
     955                <input type="hidden" name="fieldtype" id="fieldtype" value="textbox" />
     956
     957                <?php
     958        }
     959
     960        /**
     961         * Return if a field ID is the default field
     962         *
     963         * @since BuddyPress (2.3.0)
     964         *
     965         * @param  int $field_id ID of field to check
     966         * @return bool
     967         */
     968        private function is_default_field( $field_id = 0 ) {
     969
     970                // Fallback to current field ID if none passed
     971                if ( empty( $field_id ) ) {
     972                        $field_id = $this->id;
     973                }
     974
     975                // Compare & return
     976                return (bool) ( 1 === (int) $field_id );
    621977        }
    622978}
Note: See TracChangeset for help on using the changeset viewer.