From d32cc46ac45518ff6bbbee569b4831a2ec000d16 Mon Sep 17 00:00:00 2001 From: Renato Alves Date: Sun, 21 Mar 2021 12:28:19 -0300 Subject: [PATCH] Component: improvements to the wp bp component command --- CHANGELOG.md | 7 ++++ features/component.feature | 34 ++++++++++++++++-- src/components.php | 70 +++++++++++++++++--------------------- 3 files changed, 70 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f06b7cf7..a5dd8c24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/features/component.feature b/features/component.feature index c6ca0462..0d47a8ed 100644 --- a/features/component.feature +++ b/features/component.feature @@ -24,8 +24,8 @@ 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‘s what makes time travel BuddyPress possible! | - | 2 | members | Active | Community Members | Everything in a BuddyPress community revolves around its members. | + | 1 | core | active | BuddyPress Core | It‘s what makes time travel 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: @@ -33,12 +33,42 @@ Feature: Manage BuddyPress Components | 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 | diff --git a/src/components.php b/src/components.php index 92a86a0f..07c13fa0 100644 --- a/src/components.php +++ b/src/components.php @@ -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'] ] ); } } @@ -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; @@ -267,13 +257,15 @@ 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 + ); } /** @@ -281,16 +273,16 @@ protected function component_exists( $component ) { * * @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'; } }