Skip to content

Commit

Permalink
Merge pull request #94 from buddypress/component-command
Browse files Browse the repository at this point in the history
wp bp component command improvements
  • Loading branch information
renatonascalves authored Mar 21, 2021
2 parents 5d48ecc + d32cc46 commit bdd26c6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 41 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
* Upgraded the wp-cli/wp-cli-tests package to latest
* Upgraded the wp-cli/wp-cli package to latest
* Added a new behat.yml file for the test setup
* wp bp component:
* properly listing active components.
* status string updated from uppercase to lowercase.
* we are checking the component status correctly.
* we improved the behat tests.
* we are decidubg the component description.
* we are escaping the component title.

## 2.0.0

Expand Down
34 changes: 32 additions & 2 deletions features/component.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,51 @@ Feature: Manage BuddyPress Components
When I run `wp bp component list --type=required`
Then STDOUT should be a table containing rows:
| number | id | status | title | description |
| 1 | core | Active | BuddyPress Core | It&#8216;s what makes <del>time travel</del> BuddyPress possible! |
| 2 | members | Active | Community Members | Everything in a BuddyPress community revolves around its members. |
| 1 | core | active | BuddyPress Core | Its what makes <del>time travel</del> BuddyPress possible! |
| 2 | members | active | Community Members | Everything in a BuddyPress community revolves around its members. |

When I run `wp bp component list --fields=id --type=required`
Then STDOUT should be a table containing rows:
| id |
| core |
| members |

When I run `wp bp component list --fields=id --type=optional`
Then STDOUT should be a table containing rows:
| id |
| xprofile |
| settings |
| friends |
| messages |
| activity |
| notifications |
| groups |
| blogs |

When I run `wp bp component list --fields=id --status=active`
Then STDOUT should be a table containing rows:
| id |
| core |

When I try `wp bp component list --type=retired`
Then STDERR should be:
"""
Error: There is no component available.
"""
And the return code should be 1

When I run `wp bp component activate groups`
Then STDOUT should contain:
"""
Success: The Groups component has been activated.
"""

When I run `wp bp component list --fields=id --status=active`
Then STDOUT should be a table containing rows:
| id |
| groups |
| core |

When I run `wp bp component list --fields=id`
Then STDOUT should be a table containing rows:
| id |
Expand Down
70 changes: 31 additions & 39 deletions src/components.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,15 @@ public function list_( $args, $assoc_args ) { // phpcs:ignore PSR2.Methods.Metho
$type = $assoc_args['type'];

// Get components.
$components = bp_core_get_components( $type );
$components = (array) bp_core_get_components( $type );

// Active components.
$active_components = apply_filters( 'bp_active_components', bp_get_option( 'bp-active-components' ) );
$active_components = apply_filters( 'bp_active_components', bp_get_option( 'bp-active-components', array() ) );

// Core component is always active.
if ( 'optional' !== $type ) {
if ( 'optional' !== $type && isset( $components['core'] ) ) {
if ( ! isset( $active_components['core'] ) ) {
$active_components = [
'core' => $components['core'],
];
} else {
$active_components['core'] = $components['core'];
$active_components = array_merge( $active_components, [ 'core' => $components['core'] ] );
}
}

Expand All @@ -209,48 +205,42 @@ public function list_( $args, $assoc_args ) { // phpcs:ignore PSR2.Methods.Metho
switch ( $assoc_args['status'] ) {
case 'all':
$index = 0;
foreach ( $components as $name => $labels ) {
foreach ( $components as $component_key => $component ) {
$index++;
$current_components[] = array(
'number' => $index,
'id' => $name,
'status' => $this->verify_component_status( $name ),
'title' => $labels['title'],
'description' => $labels['description'],
'id' => $component_key,
'status' => $this->verify_component_status( $component_key ),
'title' => esc_html( $component['title'] ),
'description' => html_entity_decode( $component['description'] ),
);
}
break;

case 'active':
$index = 0;
foreach ( array_keys( $active_components ) as $component ) {
foreach ( array_keys( $active_components ) as $component_key ) {
$index++;

$info = $components[ $component ];

$current_components[] = array(
'number' => $index,
'id' => $component,
'status' => 'Active',
'title' => $info['title'],
'description' => $info['description'],
'id' => $component_key,
'status' => $this->verify_component_status( $component_key ),
'title' => esc_html( $components[ $component_key ]['title'] ),
'description' => html_entity_decode( $components[ $component_key ]['description'] ),
);
}
break;

case 'inactive':
$index = 0;
foreach ( $inactive_components as $component ) {
foreach ( $inactive_components as $component_key ) {
$index++;

$info = $components[ $component ];

$current_components[] = array(
'number' => $index,
'id' => $component,
'status' => 'Inactive',
'title' => $info['title'],
'description' => $info['description'],
'id' => $component_key,
'status' => $this->verify_component_status( $component_key ),
'title' => esc_html( $components[ $component_key ]['title'] ),
'description' => html_entity_decode( $components[ $component_key ]['description'] ),
);
}
break;
Expand All @@ -267,30 +257,32 @@ public function list_( $args, $assoc_args ) { // phpcs:ignore PSR2.Methods.Metho
/**
* Does the component exist?
*
* @param string $component Component.
* @param string $component_key Component key.
* @return bool
*/
protected function component_exists( $component ) {
$keys = array_keys( bp_core_get_components() );

return in_array( $component, $keys, true );
protected function component_exists( $component_key ) {
return in_array(
$component_key,
array_keys( bp_core_get_components() ),
true
);
}

/**
* Verify Component Status.
*
* @since 1.7.0
*
* @param string $id Component id.
* @param string $component_key Component key.
* @return string
*/
protected function verify_component_status( $id ) {
$active = 'Active';
protected function verify_component_status( $component_key ) {
$active = 'active';

if ( 'core' === $id ) {
if ( 'core' === $component_key ) {
return $active;
}

return bp_is_active( $id ) ? $active : 'Inactive';
return bp_is_active( $component_key ) ? $active : 'inactive';
}
}

0 comments on commit bdd26c6

Please sign in to comment.