Skip to content

Commit

Permalink
Merge pull request #424 from ernilambar/232-recently-active-plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored May 2, 2024
2 parents 23fe646 + 6e09dcb commit 0981288
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
146 changes: 146 additions & 0 deletions features/plugin-list-recently-active.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
Feature: List recently active WordPress plugins

Scenario: Verify plugin installation, activation, deactivation and confirm listing recently active plugins list is correct
Given a WP install

When I run `wp plugin install site-secrets debug-bar p2-by-email --activate`
Then STDOUT should contain:
"""
Plugin 'site-secrets' activated.
"""
And STDOUT should contain:
"""
Plugin 'debug-bar' activated.
"""
And STDOUT should contain:
"""
Plugin 'p2-by-email' activated.
"""

When I run `wp plugin list --recently-active --field=name --format=json`
Then STDOUT should be:
"""
[]
"""

When I run `wp plugin activate akismet`
Then STDOUT should contain:
"""
Plugin 'akismet' activated.
"""

When I run `wp plugin deactivate site-secrets debug-bar`
Then STDOUT should contain:
"""
Plugin 'site-secrets' deactivated.
Plugin 'debug-bar' deactivated.
Success: Deactivated 2 of 2 plugins.
"""

When I run `wp plugin list --recently-active --field=name`
Then STDOUT should be a table containing rows:
| debug-bar |
| site-secrets |

Scenario: Use recently active plugin to activate plugins
Given a WP install

When I run `wp plugin install site-secrets debug-bar --activate`
Then STDOUT should contain:
"""
Plugin 'site-secrets' activated.
"""
And STDOUT should contain:
"""
Plugin 'debug-bar' activated.
"""

When I run `wp plugin deactivate site-secrets debug-bar`
Then STDOUT should be:
"""
Plugin 'site-secrets' deactivated.
Plugin 'debug-bar' deactivated.
Success: Deactivated 2 of 2 plugins.
"""

When I run `wp plugin activate $(wp plugin list --recently-active --field=name)`
Then STDOUT should contain:
"""
Plugin 'debug-bar' activated.
"""
And STDOUT should contain:
"""
Plugin 'site-secrets' activated.
"""

Scenario: For a MU site, verify plugin installation, activation, deactivation and confirm listing recently active plugins list is correct
Given a WP multisite install

When I run `wp plugin install site-secrets debug-bar p2-by-email --activate-network`
Then STDOUT should contain:
"""
Plugin 'site-secrets' network activated.
"""
And STDOUT should contain:
"""
Plugin 'debug-bar' network activated.
"""
And STDOUT should contain:
"""
Plugin 'p2-by-email' network activated.
"""

When I run `wp plugin activate akismet --network`
Then STDOUT should contain:
"""
Plugin 'akismet' network activated.
"""

When I run `wp plugin list --recently-active --field=name --format=json`
Then STDOUT should be:
"""
[]
"""
When I run `wp plugin deactivate site-secrets debug-bar --network`
Then STDOUT should be:
"""
Plugin 'site-secrets' network deactivated.
Plugin 'debug-bar' network deactivated.
Success: Network deactivated 2 of 2 plugins.
"""

When I run `wp plugin list --recently-active --field=name`
Then STDOUT should be a table containing rows:
| debug-bar |
| site-secrets |

Scenario: For a MU site, use recently active plugin to activate plugins
Given a WP multisite install

When I run `wp plugin install site-secrets debug-bar --activate-network`
Then STDOUT should contain:
"""
Plugin 'site-secrets' network activated.
"""
And STDOUT should contain:
"""
Plugin 'debug-bar' network activated.
"""

When I run `wp plugin deactivate site-secrets debug-bar --network`
Then STDOUT should be:
"""
Plugin 'site-secrets' network deactivated.
Plugin 'debug-bar' network deactivated.
Success: Network deactivated 2 of 2 plugins.
"""

When I run `wp plugin activate $(wp plugin list --recently-active --field=name) --network`
Then STDOUT should contain:
"""
Plugin 'site-secrets' network activated.
"""
And STDOUT should contain:
"""
Plugin 'debug-bar' network activated.
"""
26 changes: 26 additions & 0 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ protected function get_all_items() {
* $ wp plugin activate hello --network
* Plugin 'hello' network activated.
* Success: Network activated 1 of 1 plugins.
*
* # Activate plugins that were recently active.
* $ wp plugin activate $(wp plugin list --recently-active --field=name)
* Plugin 'bbpress' activated.
* Plugin 'buddypress' activated.
* Success: Activated 2 of 2 plugins.
*
* # Activate plugins that were recently active on a multisite.
* $ wp plugin activate $(wp plugin list --recently-active --field=name) --network
* Plugin 'bbpress' network activated.
* Plugin 'buddypress' network activated.
* Success: Activated 2 of 2 plugins.
*/
public function activate( $args, $assoc_args = array() ) {
$network_wide = Utils\get_flag_value( $assoc_args, 'network', false );
Expand Down Expand Up @@ -718,6 +730,12 @@ protected function get_item_list() {
$auto_updates = [];
}

$recently_active = is_network_admin() ? get_site_option( 'recently_activated' ) : get_option( 'recently_activated' );

if ( false === $recently_active ) {
$recently_active = [];
}

foreach ( $this->get_all_plugins() as $file => $details ) {
$all_update_info = $this->get_update_info();
$update_info = ( isset( $all_update_info->response[ $file ] ) && null !== $all_update_info->response[ $file ] ) ? (array) $all_update_info->response[ $file ] : null;
Expand Down Expand Up @@ -745,6 +763,7 @@ protected function get_item_list() {
'tested_up_to' => '',
'wporg_status' => $wporg_info['status'],
'wporg_last_updated' => $wporg_info['last_updated'],
'recently_active' => in_array( $file, array_keys( $recently_active ), true ),
];

if ( $this->check_headers['tested_up_to'] ) {
Expand Down Expand Up @@ -1306,6 +1325,9 @@ public function delete( $args, $assoc_args = array() ) {
* [--skip-update-check]
* : If set, the plugin update check will be skipped.
*
* [--recently-active]
* : If set, only recently active plugins will be shown and the status filter will be ignored.
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each plugin:
Expand Down Expand Up @@ -1361,6 +1383,10 @@ public function delete( $args, $assoc_args = array() ) {
* | local | | |
* +--------------------+--------------+--------------------+
*
* # List recently active plugins on the site.
* $ wp plugin list --recently-active --field=name --format=json
* ["akismet","bbpress","buddypress"]
*
* @subcommand list
*/
public function list_( $_, $assoc_args ) {
Expand Down
9 changes: 9 additions & 0 deletions src/WP_CLI/CommandWithUpgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,15 @@ protected function _list( $_, $assoc_args ) {

$all_items = $this->get_all_items();

if ( false !== (bool) Utils\get_flag_value( $assoc_args, 'recently-active', false ) ) {
$all_items = array_filter(
$all_items,
function ( $value ) {
return isset( $value['recently_active'] ) && true === $value['recently_active'];
}
);
}

if ( ! is_array( $all_items ) ) {
WP_CLI::error( "No {$this->item_type}s found." );
}
Expand Down

0 comments on commit 0981288

Please sign in to comment.