PluginProbe
BuddyPress / trunk
BuddyPress vtrunk
11.6.2 12.7.2 14.5.2 11.6.0 12.7.0 2.5.0-rc1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.0-beta1 2.6.0-rc1 2.6.1 2.6.1.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.0-beta1 2.7.0-rc1 2.7.0-rc2 2.7.1 2.7.2 All 274 releases
buddypress / cli / src / xprofile-group.php

xprofile-group.php in BuddyPress trunk, at cli/src/xprofile-group.php

215 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Buddypress\CLI\Command;
4
5 use WP_CLI;
6
7 /**
8 * Manage XProfile Groups.
9 *
10 * @since 1.5.0
11 */
12 class XProfile_Group extends BuddyPressCommand {
13
14 /**
15 * XProfile object fields.
16 *
17 * @var array
18 */
19 protected $obj_fields = [
20 'id',
21 'name',
22 'description',
23 'group_order',
24 'can_delete',
25 ];
26
27 /**
28 * Object ID key.
29 *
30 * @var string
31 */
32 protected $obj_id_key = 'id';
33
34 /**
35 * Create an XProfile group.
36 *
37 * ## OPTIONS
38 *
39 * --name=<name>
40 * : The name for this field group.
41 *
42 * [--description=<description>]
43 * : The description for this field group.
44 *
45 * [--can-delete=<can-delete>]
46 * : Whether the group can be deleted.
47 * ---
48 * default: 1
49 * ---
50 *
51 * [--silent]
52 * : Whether to silent the XProfile group creation.
53 *
54 * [--porcelain]
55 * : Output just the new group id.
56 *
57 * ## EXAMPLES
58 *
59 * # Create XProfile field group.
60 * $ wp bp xprofile group create --name="Group Name" --description="Xprofile Group Description"
61 * Success: Created XProfile field group "Group Name" (ID 123).
62 *
63 * # Create XProfile field group that can't be deleted.
64 * $ wp bp xprofile group add --name="Another Group" --can-delete=false
65 * Success: Created XProfile field group "Another Group" (ID 21212).
66 *
67 * @alias add
68 */
69 public function create( $args, $assoc_args ) {
70 $r = wp_parse_args(
71 $assoc_args,
72 [
73 'name' => '',
74 'description' => '',
75 ]
76 );
77
78 $xprofile_group_id = xprofile_insert_field_group( $r );
79
80 // Silent it before it errors.
81 if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) {
82 return;
83 }
84
85 if ( ! $xprofile_group_id ) {
86 WP_CLI::error( 'Could not create field group.' );
87 }
88
89 if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
90 WP_CLI::log( $xprofile_group_id );
91 } else {
92 $group = new \BP_XProfile_Group( $xprofile_group_id );
93
94 WP_CLI::success(
95 sprintf(
96 'Created XProfile field group "%s" (ID %d).',
97 $group->name,
98 $group->id
99 )
100 );
101 }
102 }
103
104 /**
105 * Fetch specific XProfile field group.
106 *
107 * ## OPTIONS
108 *
109 * <field-group-id>
110 * : Identifier for the field group.
111 *
112 * [--fields=<fields>]
113 * : Limit the output to specific fields.
114 *
115 * [--format=<format>]
116 * : Render output in a particular format.
117 * ---
118 * default: table
119 * options:
120 * - table
121 * - json
122 * - csv
123 * - yaml
124 * ---
125 *
126 * ## EXAMPLES
127 *
128 * # Get a specific field group.
129 * $ wp bp xprofile group get 500
130 * +-------------+---------------+
131 * | Field | Value |
132 * +-------------+---------------+
133 * | id | 2 |
134 * | name | Group |
135 * | description | |
136 * | can_delete | 1 |
137 * | group_order | 0 |
138 * | fields | null |
139 * +-------------+---------------+
140 *
141 * # Get a specific field group in JSON format.
142 * $ wp bp xprofile group see 56 --format=json
143 * {"id":2,"name":"Group","description":"","can_delete":1,"group_order":0,"fields":null}
144 *
145 * @alias see
146 */
147 public function get( $args, $assoc_args ) {
148 $field_group_id = $args[0];
149
150 if ( ! is_numeric( $field_group_id ) ) {
151 WP_CLI::error( 'Please provide a numeric field group ID.' );
152 }
153
154 $object = xprofile_get_field_group( $field_group_id );
155
156 if ( empty( $object->id ) && ! is_object( $object ) ) {
157 WP_CLI::error( 'No XProfile field group found.' );
158 }
159
160 $object_arr = get_object_vars( $object );
161
162 if ( empty( $assoc_args['fields'] ) ) {
163 $assoc_args['fields'] = array_keys( $object_arr );
164 }
165
166 $this->get_formatter( $assoc_args )->display_item( $object_arr );
167 }
168
169 /**
170 * Delete specific XProfile field group(s).
171 *
172 * ## OPTIONS
173 *
174 * <field-group-id>...
175 * : ID or IDs of field groups to delete.
176 *
177 * [--yes]
178 * : Answer yes to the confirmation message.
179 *
180 * ## EXAMPLES
181 *
182 * # Delete a specific field group.
183 * $ wp bp xprofile group delete 500 --yes
184 * Success: Field group deleted 500.
185 *
186 * $ wp bp xprofile group delete 55654 54564 --yes
187 * Success: Field group deleted 55654.
188 * Success: Field group deleted 54564.
189 *
190 * @alias remove
191 * @alias trash
192 */
193 public function delete( $args, $assoc_args ) {
194 $field_groups_ids = wp_parse_id_list( $args );
195
196 if ( count( $field_groups_ids ) > 1 ) {
197 WP_CLI::confirm( 'Are you sure you want to delete these field groups?', $assoc_args );
198 } else {
199 WP_CLI::confirm( 'Are you sure you want to delete this field group?', $assoc_args );
200 }
201
202 parent::_delete(
203 $field_groups_ids,
204 $assoc_args,
205 function ( $field_group_id ) {
206 if ( xprofile_delete_field_group( $field_group_id ) ) {
207 return [ 'success', sprintf( 'Field group deleted %d.', $field_group_id ) ];
208 }
209
210 return [ 'error', sprintf( 'Could not delete the field group %d.', $field_group_id ) ];
211 }
212 );
213 }
214 }
215