| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
|
| 7 |
/** |
| 8 |
* Manage XProfile Data. |
| 9 |
* |
| 10 |
* @since 1.5.0 |
| 11 |
*/ |
| 12 |
class XProfile_Data extends BuddyPressCommand { |
| 13 |
|
| 14 |
/** |
| 15 |
* XProfile object fields. |
| 16 |
* |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
protected $obj_fields = [ |
| 20 |
'id', |
| 21 |
'field_id', |
| 22 |
'user_id', |
| 23 |
'value', |
| 24 |
'last_updated', |
| 25 |
]; |
| 26 |
|
| 27 |
/** |
| 28 |
* Set profile data for a user. |
| 29 |
* |
| 30 |
* ## OPTIONS |
| 31 |
* |
| 32 |
* --user-id=<user> |
| 33 |
* : Identifier for the user. Accepts either a user_login or a numeric ID. |
| 34 |
* |
| 35 |
* --field-id=<field> |
| 36 |
* : Identifier for the field. Accepts either the name of the field or a numeric ID. |
| 37 |
* |
| 38 |
* --value=<value> |
| 39 |
* : Value to set. |
| 40 |
* |
| 41 |
* [--silent] |
| 42 |
* : Whether to silent the success message. |
| 43 |
* |
| 44 |
* ## EXAMPLE |
| 45 |
* |
| 46 |
* # Set profile data for a user. |
| 47 |
* $ wp bp xprofile data set --user-id=45 --field-id=120 --value=test |
| 48 |
* Success: Updated XProfile field "Field Name" (ID 120) with value "test" for user user_login (ID 45). |
| 49 |
* |
| 50 |
* @alias set |
| 51 |
* @alias add |
| 52 |
* @alias update |
| 53 |
*/ |
| 54 |
public function create( $args, $assoc_args ) { |
| 55 |
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] ); |
| 56 |
$field_id = $this->get_field_id( $assoc_args['field-id'] ); |
| 57 |
$field = new \BP_XProfile_Field( $field_id ); |
| 58 |
|
| 59 |
if ( empty( $field->name ) ) { |
| 60 |
WP_CLI::error( 'XProfile field not found.' ); |
| 61 |
} |
| 62 |
|
| 63 |
$value = $assoc_args['value']; |
| 64 |
|
| 65 |
if ( 'checkbox' === $field->type ) { |
| 66 |
$value = explode( ',', $assoc_args['value'] ); |
| 67 |
} |
| 68 |
|
| 69 |
$updated = xprofile_set_field_data( $field->id, $user->ID, $value ); |
| 70 |
|
| 71 |
// Silent it before it errors. |
| 72 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! $updated ) { |
| 77 |
WP_CLI::error( 'Could not set profile data.' ); |
| 78 |
} |
| 79 |
|
| 80 |
WP_CLI::success( |
| 81 |
sprintf( |
| 82 |
'Updated XProfile field "%s" (ID %d) with value "%s" for user %s (ID %d).', |
| 83 |
$field->name, |
| 84 |
$field->id, |
| 85 |
$assoc_args['value'], |
| 86 |
$user->user_nicename, |
| 87 |
$user->ID |
| 88 |
) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get profile data for a user. |
| 94 |
* |
| 95 |
* ## OPTIONS |
| 96 |
* |
| 97 |
* --user-id=<user> |
| 98 |
* : Identifier for the user. Accepts either a user_login or a numeric ID. |
| 99 |
* |
| 100 |
* [--field-id=<field>] |
| 101 |
* : Identifier for the field. Accepts either the name of the field or a numeric ID. |
| 102 |
* |
| 103 |
* [--format=<format>] |
| 104 |
* : Render output in a particular format. |
| 105 |
* --- |
| 106 |
* default: table |
| 107 |
* options: |
| 108 |
* - table |
| 109 |
* - json |
| 110 |
* - csv |
| 111 |
* - yaml |
| 112 |
* --- |
| 113 |
* |
| 114 |
* [--multi-format=<value>] |
| 115 |
* : The format for the array data. |
| 116 |
* --- |
| 117 |
* default: array |
| 118 |
* options: |
| 119 |
* - array |
| 120 |
* - comma |
| 121 |
* --- |
| 122 |
* |
| 123 |
* ## EXAMPLES |
| 124 |
* |
| 125 |
* # Get profile data for a user. |
| 126 |
* $ wp bp xprofile data get --user-id=45 --field-id=120 |
| 127 |
* |
| 128 |
* # Get profile data for a user, formatting the data. |
| 129 |
* $ wp bp xprofile data see --user-id=user_test --field-id=Hometown --multi-format=comma |
| 130 |
* |
| 131 |
* @alias see |
| 132 |
*/ |
| 133 |
public function get( $args, $assoc_args ) { |
| 134 |
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] ); |
| 135 |
|
| 136 |
if ( isset( $assoc_args['field-id'] ) ) { |
| 137 |
$data = xprofile_get_field_data( $assoc_args['field-id'], $user->ID, $assoc_args['multi-format'] ); |
| 138 |
WP_CLI::print_value( $data, $assoc_args ); |
| 139 |
} else { |
| 140 |
$data = \BP_XProfile_ProfileData::get_all_for_user( $user->ID ); |
| 141 |
$formatted_data = []; |
| 142 |
|
| 143 |
foreach ( $data as $field_name => $field_data ) { |
| 144 |
// Omit WP core fields. |
| 145 |
if ( ! is_array( $field_data ) ) { |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
$formatted_data[] = [ |
| 150 |
'field_id' => $field_data['field_id'], |
| 151 |
'field_name' => $field_name, |
| 152 |
'value' => wp_json_encode( maybe_unserialize( $field_data['field_data'] ) ), |
| 153 |
]; |
| 154 |
} |
| 155 |
|
| 156 |
$format_args = $assoc_args; |
| 157 |
$format_args['fields'] = [ |
| 158 |
'field_id', |
| 159 |
'field_name', |
| 160 |
'value', |
| 161 |
]; |
| 162 |
|
| 163 |
$this->get_formatter( $format_args )->display_items( $formatted_data ); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Delete XProfile data for a user. |
| 169 |
* |
| 170 |
* ## OPTIONS |
| 171 |
* |
| 172 |
* --user-id=<user> |
| 173 |
* : Identifier for the user. Accepts either a user_login or a numeric ID. |
| 174 |
* |
| 175 |
* [--field-id=<field>] |
| 176 |
* : Identifier for the field. Accepts either the name of the field or a numeric ID. |
| 177 |
* |
| 178 |
* [--delete-all] |
| 179 |
* : Delete all data for the user. |
| 180 |
* |
| 181 |
* [--yes] |
| 182 |
* : Answer yes to the confirmation message. |
| 183 |
* |
| 184 |
* ## EXAMPLES |
| 185 |
* |
| 186 |
* # Delete a specific XProfile field data. |
| 187 |
* $ wp bp xprofile data delete --user-id=45 --field-id=120 --yes |
| 188 |
* Success: XProfile data removed. |
| 189 |
* |
| 190 |
* # Delete all XProfile data for a user. |
| 191 |
* $ wp bp xprofile data remove --user-id=user_test --delete-all --yes |
| 192 |
* Success: XProfile data removed. |
| 193 |
* |
| 194 |
* @alias remove |
| 195 |
* @alias trash |
| 196 |
*/ |
| 197 |
public function delete( $args, $assoc_args ) { |
| 198 |
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] ); |
| 199 |
|
| 200 |
if ( ! isset( $assoc_args['field-id'] ) && ! isset( $assoc_args['delete-all'] ) ) { |
| 201 |
WP_CLI::error( 'Either --field-id or --delete-all must be provided.' ); |
| 202 |
} |
| 203 |
|
| 204 |
if ( isset( $assoc_args['delete-all'] ) ) { |
| 205 |
WP_CLI::confirm( sprintf( 'Are you sure you want to delete all XProfile data for the user %s (#%d)?', $user->user_login, $user->ID ), $assoc_args ); |
| 206 |
|
| 207 |
xprofile_remove_data( $user->ID ); |
| 208 |
WP_CLI::success( 'XProfile data removed.' ); |
| 209 |
} else { |
| 210 |
WP_CLI::confirm( 'Are you sure you want to delete that?', $assoc_args ); |
| 211 |
|
| 212 |
if ( xprofile_delete_field_data( $assoc_args['field-id'], $user->ID ) ) { |
| 213 |
WP_CLI::success( 'XProfile data removed.' ); |
| 214 |
} else { |
| 215 |
WP_CLI::error( 'Could not delete XProfile data.' ); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|