| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
|
| 7 |
/** |
| 8 |
* Manage BuddyPress Components. |
| 9 |
* |
| 10 |
* ## EXAMPLES |
| 11 |
* |
| 12 |
* # Activate a component. |
| 13 |
* $ wp bp component activate groups |
| 14 |
* Success: The Groups component has been activated. |
| 15 |
* |
| 16 |
* # Deactive a component. |
| 17 |
* $ wp bp component deactivate groups |
| 18 |
* Success: The Groups component has been deactivated. |
| 19 |
* |
| 20 |
* # List required components. |
| 21 |
* $ wp bp component list --type=required |
| 22 |
* +--------+---------+--------+------------------------+--------------------------------------------+ |
| 23 |
* | number | id | status | title | description | |
| 24 |
* +--------+---------+--------+------------------------------------------+--------------------------+ |
| 25 |
* | 1 | core | Active | BuddyPress Core | It's what makes <del>time travel</del> | |
| 26 |
* | | | | | BuddyPress possible! | |
| 27 |
* | 2 | members | Active | Community Members | Everything in a BuddyPress community | |
| 28 |
* | | | | | revolves around its members. | |
| 29 |
* +--------+---------+--------+------------------------------------------+--------------------------+ |
| 30 |
* |
| 31 |
* @since 1.6.0 |
| 32 |
*/ |
| 33 |
class Components extends BuddyPressCommand { |
| 34 |
|
| 35 |
/** |
| 36 |
* Object fields. |
| 37 |
* |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
protected $obj_fields = [ |
| 41 |
'number', |
| 42 |
'id', |
| 43 |
'status', |
| 44 |
'title', |
| 45 |
'description', |
| 46 |
]; |
| 47 |
|
| 48 |
/** |
| 49 |
* Activate a component. |
| 50 |
* |
| 51 |
* ## OPTIONS |
| 52 |
* |
| 53 |
* <component> |
| 54 |
* : Name of the component to activate. |
| 55 |
* |
| 56 |
* ## EXAMPLE |
| 57 |
* |
| 58 |
* # Activate a component. |
| 59 |
* $ wp bp component activate groups |
| 60 |
* Success: The Groups component has been activated. |
| 61 |
*/ |
| 62 |
public function activate( $args ) { |
| 63 |
$component = $args[0]; |
| 64 |
|
| 65 |
if ( ! $this->component_exists( $component ) ) { |
| 66 |
WP_CLI::error( sprintf( '%s is not a valid component.', ucfirst( $component ) ) ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( bp_is_active( $component ) ) { |
| 70 |
WP_CLI::error( sprintf( 'The %s component is already active.', ucfirst( $component ) ) ); |
| 71 |
} |
| 72 |
|
| 73 |
$active_components =& buddypress()->active_components; |
| 74 |
|
| 75 |
// Set for the rest of the page load. |
| 76 |
$active_components[ $component ] = 1; |
| 77 |
|
| 78 |
// Save in the db. |
| 79 |
bp_update_option( 'bp-active-components', $active_components ); |
| 80 |
|
| 81 |
// Ensure that dbDelta() is defined. |
| 82 |
if ( ! function_exists( 'dbDelta' ) ) { |
| 83 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 84 |
} |
| 85 |
|
| 86 |
// Run the setup, in case tables have to be created. |
| 87 |
require_once buddypress()->plugin_dir . 'bp-core/admin/bp-core-admin-schema.php'; |
| 88 |
bp_core_install( $active_components ); |
| 89 |
bp_core_add_page_mappings( $active_components ); |
| 90 |
|
| 91 |
WP_CLI::success( sprintf( 'The %s component has been activated.', ucfirst( $component ) ) ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Deactivate a component. |
| 96 |
* |
| 97 |
* ## OPTIONS |
| 98 |
* |
| 99 |
* <component> |
| 100 |
* : Name of the component to deactivate. |
| 101 |
* |
| 102 |
* ## EXAMPLE |
| 103 |
* |
| 104 |
* # Deactive a component. |
| 105 |
* $ wp bp component deactivate groups |
| 106 |
* Success: The Groups component has been deactivated. |
| 107 |
*/ |
| 108 |
public function deactivate( $args ) { |
| 109 |
$component = $args[0]; |
| 110 |
|
| 111 |
if ( ! $this->component_exists( $component ) ) { |
| 112 |
WP_CLI::error( sprintf( '%s is not a valid component.', ucfirst( $component ) ) ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! bp_is_active( $component ) ) { |
| 116 |
WP_CLI::error( sprintf( 'The %s component is not active.', ucfirst( $component ) ) ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( array_key_exists( $component, bp_core_get_components( 'required' ) ) ) { |
| 120 |
WP_CLI::error( 'You cannot deactivate a required component.' ); |
| 121 |
} |
| 122 |
|
| 123 |
$active_components =& buddypress()->active_components; |
| 124 |
|
| 125 |
// Set for the rest of the page load. |
| 126 |
unset( $active_components[ $component ] ); |
| 127 |
|
| 128 |
// Save in the db. |
| 129 |
bp_update_option( 'bp-active-components', $active_components ); |
| 130 |
|
| 131 |
WP_CLI::success( sprintf( 'The %s component has been deactivated.', ucfirst( $component ) ) ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get a list of components. |
| 136 |
* |
| 137 |
* ## OPTIONS |
| 138 |
* |
| 139 |
* [--type=<type>] |
| 140 |
* : Type of the component (all, optional, retired, required). |
| 141 |
* --- |
| 142 |
* default: all |
| 143 |
* options: |
| 144 |
* - all |
| 145 |
* - optional |
| 146 |
* - retired |
| 147 |
* - required |
| 148 |
* --- |
| 149 |
* |
| 150 |
* [--status=<status>] |
| 151 |
* : Status of the component (all, active, inactive). |
| 152 |
* --- |
| 153 |
* default: all |
| 154 |
* options: |
| 155 |
* - all |
| 156 |
* - active |
| 157 |
* - inactive |
| 158 |
* --- |
| 159 |
* |
| 160 |
* [--fields=<fields>] |
| 161 |
* : Fields to display (id, title, description). |
| 162 |
* |
| 163 |
* [--format=<format>] |
| 164 |
* : Render output in a particular format. |
| 165 |
* --- |
| 166 |
* default: table |
| 167 |
* options: |
| 168 |
* - table |
| 169 |
* - csv |
| 170 |
* - ids |
| 171 |
* - json |
| 172 |
* - count |
| 173 |
* - yaml |
| 174 |
* --- |
| 175 |
* |
| 176 |
* ## EXAMPLES |
| 177 |
* |
| 178 |
* # List components and get the count. |
| 179 |
* $ wp bp component list --format=count |
| 180 |
* 10 |
| 181 |
* |
| 182 |
* # List components and get the ids. |
| 183 |
* $ wp bp component list --format=ids |
| 184 |
* core members xprofile settings friends messages activity notifications groups |
| 185 |
* |
| 186 |
* # List components. |
| 187 |
* $ wp bp component list |
| 188 |
* +--------+---------------+--------+--------------------+---------------------------------------------------------------------------------+ |
| 189 |
* | number | id | status | title | description | |
| 190 |
* +--------+---------------+--------+--------------------+---------------------------------------------------------------------------------+ |
| 191 |
* | 1 | core | active | BuddyPress Core | It‘s what makes <del>time travel</del> BuddyPress possible! | |
| 192 |
* | 2 | members | active | Community Members | Everything in a BuddyPress community revolves around its members. | |
| 193 |
* | 3 | xprofile | active | Extended Profiles | Customize your community with fully editable profile fields that allow your use | |
| 194 |
* | | | | | rs to describe themselves. | |
| 195 |
* | 4 | settings | active | Account Settings | Allow your users to modify their account and notification settings directly fro | |
| 196 |
* | | | | | m within their profiles. | |
| 197 |
* | 5 | friends | active | Friend Connections | Let your users make connections so they can track the activity of others and fo | |
| 198 |
* | | | | | cus on the people they care about the most. | |
| 199 |
* | 6 | messages | active | Private Messaging | Allow your users to talk to each other directly and in private. Not just limite | |
| 200 |
* | | | | | d to one-on-one discussions, messages can be sent between any number of members | |
| 201 |
* | | | | | . | |
| 202 |
* | 7 | activity | active | Activity Streams | Global, personal, and group activity streams with threaded commenting, direct p | |
| 203 |
* | | | | | osting, favoriting, and @mentions, all with full RSS feed and email notificatio | |
| 204 |
* | | | | | n support. | |
| 205 |
* | 8 | notifications | active | Notifications | Notify members of relevant activity with a toolbar bubble and/or via email, and | |
| 206 |
* | | | | | allow them to customize their notification settings. | |
| 207 |
* | 9 | groups | active | User Groups | Groups allow your users to organize themselves into specific public, private or | |
| 208 |
* | | | | | hidden sections with separate activity streams and member listings. | |
| 209 |
* | 10 | blogs | active | Site Tracking | Record activity for new sites, posts, and comments across your network. | |
| 210 |
* +--------+---------------+--------+--------------------+---------------------------------------------------------------------------------+ |
| 211 |
* |
| 212 |
* @subcommand list |
| 213 |
*/ |
| 214 |
public function list_( $args, $assoc_args ) { |
| 215 |
$formatter = $this->get_formatter( $assoc_args ); |
| 216 |
|
| 217 |
// Get type. |
| 218 |
$type = $assoc_args['type']; |
| 219 |
|
| 220 |
// Get components. |
| 221 |
$components = (array) bp_core_get_components( $type ); |
| 222 |
|
| 223 |
// Active components. |
| 224 |
$active_components = apply_filters( 'bp_active_components', bp_get_option( 'bp-active-components', [] ) ); |
| 225 |
|
| 226 |
// Core component is always active. |
| 227 |
if ( 'optional' !== $type && isset( $components['core'] ) ) { |
| 228 |
if ( ! isset( $active_components['core'] ) ) { |
| 229 |
$active_components = array_merge( $active_components, [ 'core' => $components['core'] ] ); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
// Inactive components. |
| 234 |
$inactive_components = array_diff( array_keys( $components ), array_keys( $active_components ) ); |
| 235 |
|
| 236 |
$current_components = []; |
| 237 |
switch ( $assoc_args['status'] ) { |
| 238 |
case 'all': |
| 239 |
$index = 0; |
| 240 |
foreach ( $components as $component_key => $component ) { |
| 241 |
|
| 242 |
// Skip if the component is not available. |
| 243 |
if ( ! isset( $components[ $component_key ] ) ) { |
| 244 |
continue; |
| 245 |
} |
| 246 |
|
| 247 |
++$index; |
| 248 |
$current_components[] = [ |
| 249 |
'number' => $index, |
| 250 |
'id' => $component_key, |
| 251 |
'status' => $this->verify_component_status( $component_key ), |
| 252 |
'title' => esc_html( $component['title'] ), |
| 253 |
'description' => html_entity_decode( $component['description'] ), |
| 254 |
]; |
| 255 |
} |
| 256 |
break; |
| 257 |
|
| 258 |
case 'active': |
| 259 |
$index = 0; |
| 260 |
foreach ( array_keys( $active_components ) as $component_key ) { |
| 261 |
|
| 262 |
// Skip if the component is not available. |
| 263 |
if ( ! isset( $components[ $component_key ] ) ) { |
| 264 |
continue; |
| 265 |
} |
| 266 |
|
| 267 |
++$index; |
| 268 |
$current_components[] = [ |
| 269 |
'number' => $index, |
| 270 |
'id' => $component_key, |
| 271 |
'status' => $this->verify_component_status( $component_key ), |
| 272 |
'title' => esc_html( $components[ $component_key ]['title'] ), |
| 273 |
'description' => html_entity_decode( $components[ $component_key ]['description'] ), |
| 274 |
]; |
| 275 |
} |
| 276 |
break; |
| 277 |
|
| 278 |
case 'inactive': |
| 279 |
$index = 0; |
| 280 |
foreach ( $inactive_components as $component_key ) { |
| 281 |
|
| 282 |
// Skip if the component is not available. |
| 283 |
if ( ! isset( $components[ $component_key ] ) ) { |
| 284 |
continue; |
| 285 |
} |
| 286 |
|
| 287 |
++$index; |
| 288 |
$current_components[] = [ |
| 289 |
'number' => $index, |
| 290 |
'id' => $component_key, |
| 291 |
'status' => $this->verify_component_status( $component_key ), |
| 292 |
'title' => esc_html( $components[ $component_key ]['title'] ), |
| 293 |
'description' => html_entity_decode( $components[ $component_key ]['description'] ), |
| 294 |
]; |
| 295 |
} |
| 296 |
break; |
| 297 |
} |
| 298 |
|
| 299 |
// Bail early. |
| 300 |
if ( empty( $current_components ) ) { |
| 301 |
WP_CLI::error( 'There is no component available.' ); |
| 302 |
} |
| 303 |
|
| 304 |
$formatter->display_items( 'ids' === $formatter->format ? wp_list_pluck( $current_components, 'id' ) : $current_components ); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Does the component exist? |
| 309 |
* |
| 310 |
* @param string $component_key Component key. |
| 311 |
* @return bool |
| 312 |
*/ |
| 313 |
protected function component_exists( $component_key ) { |
| 314 |
return in_array( |
| 315 |
$component_key, |
| 316 |
array_keys( bp_core_get_components() ), |
| 317 |
true |
| 318 |
); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Verify Component Status. |
| 323 |
* |
| 324 |
* @since 1.7.0 |
| 325 |
* |
| 326 |
* @param string $component_key Component key. |
| 327 |
* @return string |
| 328 |
*/ |
| 329 |
protected function verify_component_status( $component_key ) { |
| 330 |
$active = 'active'; |
| 331 |
|
| 332 |
if ( 'core' === $component_key ) { |
| 333 |
return $active; |
| 334 |
} |
| 335 |
|
| 336 |
return bp_is_active( $component_key ) ? $active : 'inactive'; |
| 337 |
} |
| 338 |
} |
| 339 |
|