| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
|
| 7 |
/** |
| 8 |
* Manage XProfile Fields. |
| 9 |
* |
| 10 |
* @since 1.5.0 |
| 11 |
*/ |
| 12 |
class XProfile_Field extends BuddyPressCommand { |
| 13 |
|
| 14 |
/** |
| 15 |
* XProfile object fields. |
| 16 |
* |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
protected $obj_fields = [ |
| 20 |
'id', |
| 21 |
'name', |
| 22 |
'description', |
| 23 |
'type', |
| 24 |
'group_id', |
| 25 |
'is_required', |
| 26 |
]; |
| 27 |
|
| 28 |
/** |
| 29 |
* Get a list of XProfile fields. |
| 30 |
* |
| 31 |
* ## OPTIONS |
| 32 |
* |
| 33 |
* [--<field>=<value>] |
| 34 |
* : One or more parameters to pass. See bp_xprofile_get_groups() |
| 35 |
* |
| 36 |
* [--format=<format>] |
| 37 |
* : Render output in a particular format. |
| 38 |
* --- |
| 39 |
* default: table |
| 40 |
* options: |
| 41 |
* - table |
| 42 |
* - csv |
| 43 |
* - ids |
| 44 |
* - json |
| 45 |
* - count |
| 46 |
* - yaml |
| 47 |
* --- |
| 48 |
* |
| 49 |
* * ## AVAILABLE FIELDS |
| 50 |
* |
| 51 |
* These fields will be displayed by default for each field: |
| 52 |
* |
| 53 |
* * id |
| 54 |
* * name |
| 55 |
* * description |
| 56 |
* * type |
| 57 |
* * group_id |
| 58 |
* * is_required |
| 59 |
* |
| 60 |
* ## EXAMPLE |
| 61 |
* |
| 62 |
* # List XProfile fields. |
| 63 |
* $ wp bp xprofile field list |
| 64 |
* +----+------+-------------+---------+----------+-------------+ |
| 65 |
* | id | name | description | type | group_id | is_required | |
| 66 |
* +----+------+-------------+---------+----------+-------------+ |
| 67 |
* | 1 | Name | | textbox | 1 | 1 | |
| 68 |
* +----+------+-------------+---------+----------+-------------+ |
| 69 |
* |
| 70 |
* @subcommand list |
| 71 |
*/ |
| 72 |
public function list_( $args, $assoc_args ) { |
| 73 |
$args = array_merge( |
| 74 |
$assoc_args, |
| 75 |
[ |
| 76 |
'fields' => 'id,name', |
| 77 |
'fetch_fields' => true, |
| 78 |
] |
| 79 |
); |
| 80 |
|
| 81 |
$fields = []; |
| 82 |
$groups = bp_xprofile_get_groups( $args ); |
| 83 |
|
| 84 |
// Reformat so that field_group_id is a property of fields. |
| 85 |
foreach ( $groups as $group ) { |
| 86 |
foreach ( $group->fields as $field ) { |
| 87 |
$fields[ $field->id ] = $field; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
$formatter = $this->get_formatter( $assoc_args ); |
| 92 |
$formatter->display_items( 'ids' === $formatter->format ? wp_list_pluck( $fields, 'id' ) : $fields ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Create a XProfile field. |
| 97 |
* |
| 98 |
* ## OPTIONS |
| 99 |
* |
| 100 |
* --field-group-id=<field-group-id> |
| 101 |
* : ID of the field group where the new field will be created. |
| 102 |
* |
| 103 |
* --name=<name> |
| 104 |
* : Name of the new field. |
| 105 |
* |
| 106 |
* [--type=<type>] |
| 107 |
* : Field type. |
| 108 |
* --- |
| 109 |
* default: textbox |
| 110 |
* --- |
| 111 |
* |
| 112 |
* [--silent] |
| 113 |
* : Whether to silent the XProfile field creation. |
| 114 |
* |
| 115 |
* [--porcelain] |
| 116 |
* : Output just the new field id. |
| 117 |
* |
| 118 |
* ## EXAMPLES |
| 119 |
* |
| 120 |
* # Create a XProfile field. |
| 121 |
* $ wp bp xprofile field create --type=checkbox --field-group-id=508 --name="Field Name" |
| 122 |
* Success: Created XProfile field "Field Name" (ID 24564). |
| 123 |
* |
| 124 |
* # Create a XProfile field. |
| 125 |
* $ wp bp xprofile field add --field-group-id=165 --name="Another Field" |
| 126 |
* Success: Created XProfile field "Another Field" (ID 5465). |
| 127 |
* |
| 128 |
* @alias add |
| 129 |
*/ |
| 130 |
public function create( $args, $assoc_args ) { |
| 131 |
// Check this is a non-empty, valid field type. |
| 132 |
if ( ! in_array( $assoc_args['type'], (array) buddypress()->profile->field_types, true ) ) { |
| 133 |
WP_CLI::error( 'Not a valid field type.' ); |
| 134 |
} |
| 135 |
|
| 136 |
$xprofile_field_id = xprofile_insert_field( |
| 137 |
[ |
| 138 |
'type' => $assoc_args['type'], |
| 139 |
'name' => $assoc_args['name'], |
| 140 |
'field_group_id' => $assoc_args['field-group-id'], |
| 141 |
] |
| 142 |
); |
| 143 |
|
| 144 |
// Silent it before it errors. |
| 145 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if ( ! $xprofile_field_id ) { |
| 150 |
WP_CLI::error( 'Could not create XProfile field.' ); |
| 151 |
} |
| 152 |
|
| 153 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { |
| 154 |
WP_CLI::log( $xprofile_field_id ); |
| 155 |
} else { |
| 156 |
$field = new \BP_XProfile_Field( $xprofile_field_id ); |
| 157 |
|
| 158 |
WP_CLI::success( |
| 159 |
sprintf( |
| 160 |
'Created XProfile field "%s" (ID %d).', |
| 161 |
$field->name, |
| 162 |
$field->id |
| 163 |
) |
| 164 |
); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get an XProfile field. |
| 170 |
* |
| 171 |
* ## OPTIONS |
| 172 |
* |
| 173 |
* <field-id> |
| 174 |
* : Identifier for the field. Accepts either the name of the field or a numeric ID. |
| 175 |
* |
| 176 |
* [--fields=<fields>] |
| 177 |
* : Limit the output to specific fields. |
| 178 |
* |
| 179 |
* [--format=<format>] |
| 180 |
* : Render output in a particular format. |
| 181 |
* --- |
| 182 |
* default: table |
| 183 |
* options: |
| 184 |
* - table |
| 185 |
* - json |
| 186 |
* - csv |
| 187 |
* - yaml |
| 188 |
* --- |
| 189 |
* |
| 190 |
* ## EXAMPLES |
| 191 |
* |
| 192 |
* # Get a xprofile field. |
| 193 |
* $ wp bp xprofile field get 500 |
| 194 |
* |
| 195 |
* # Get a xprofile field in JSON format. |
| 196 |
* $ wp bp xprofile field see 56 --format=json |
| 197 |
* |
| 198 |
* @alias see |
| 199 |
*/ |
| 200 |
public function get( $args, $assoc_args ) { |
| 201 |
$field_id = $this->get_field_id( $args[0] ); |
| 202 |
$object = xprofile_get_field( $field_id ); |
| 203 |
|
| 204 |
if ( empty( $object->id ) && ! is_object( $object ) ) { |
| 205 |
WP_CLI::error( 'No XProfile field found.' ); |
| 206 |
} |
| 207 |
|
| 208 |
$object_arr = get_object_vars( $object ); |
| 209 |
|
| 210 |
if ( empty( $assoc_args['fields'] ) ) { |
| 211 |
$assoc_args['fields'] = array_keys( $object_arr ); |
| 212 |
} |
| 213 |
|
| 214 |
$this->get_formatter( $assoc_args )->display_item( $object_arr ); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Delete an XProfile field. |
| 219 |
* |
| 220 |
* ## OPTIONS |
| 221 |
* |
| 222 |
* <field-id>... |
| 223 |
* : ID or IDs for the field. Accepts either the name of the field or a numeric ID. |
| 224 |
* |
| 225 |
* [--delete-data] |
| 226 |
* : Delete user data for the field as well. |
| 227 |
* |
| 228 |
* [--yes] |
| 229 |
* : Answer yes to the confirmation message. |
| 230 |
* |
| 231 |
* ## EXAMPLES |
| 232 |
* |
| 233 |
* # Delete a field. |
| 234 |
* $ wp bp xprofile field delete 500 --yes |
| 235 |
* Success: Deleted XProfile field "Field Name" (ID 500). |
| 236 |
* |
| 237 |
* # Delete a field and its data. |
| 238 |
* $ wp bp xprofile field remove 458 --delete-data --yes |
| 239 |
* Success: Deleted XProfile field "Another Field Name" (ID 458). |
| 240 |
* |
| 241 |
* @alias remove |
| 242 |
* @alias trash |
| 243 |
*/ |
| 244 |
public function delete( $args, $assoc_args ) { |
| 245 |
$delete_data = (bool) WP_CLI\Utils\get_flag_value( $assoc_args, 'delete-data' ); |
| 246 |
$field_ids = wp_parse_id_list( $args ); |
| 247 |
|
| 248 |
if ( count( $field_ids ) > 1 ) { |
| 249 |
WP_CLI::confirm( 'Are you sure you want to delete these fields?', $assoc_args ); |
| 250 |
} else { |
| 251 |
WP_CLI::confirm( 'Are you sure you want to delete this field?', $assoc_args ); |
| 252 |
} |
| 253 |
|
| 254 |
parent::_delete( |
| 255 |
$field_ids, |
| 256 |
$assoc_args, |
| 257 |
function ( $field_id ) use ( $delete_data ) { |
| 258 |
$field = new \BP_XProfile_Field( $field_id ); |
| 259 |
$name = $field->name; |
| 260 |
$id = $field->id; |
| 261 |
|
| 262 |
if ( $field->delete( $delete_data ) ) { |
| 263 |
return [ 'success', sprintf( 'Deleted XProfile field "%s" (ID %d).', $name, $id ) ]; |
| 264 |
} |
| 265 |
|
| 266 |
return [ 'error', sprintf( 'Failed deleting XProfile field "%s" (ID %d).', $name, $id ) ]; |
| 267 |
} |
| 268 |
); |
| 269 |
} |
| 270 |
} |
| 271 |
|