Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions features/check-network-required-plugins.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Feature: Check required network plugins

Scenario: Verify check description
Given an empty directory
And a config.yml file:
"""
network-required-plugins:
check: Network_Required_Plugins
options:
plugins:
- akismet
"""

When I try `wp doctor list --fields=name,description --config=config.yml`
Then STDOUT should be a table containing rows:
| name | description |
| network-required-plugins | Errors when required plugins are not network-activated. |

Scenario: Required plugin is not network-activated
Given a WP multisite installation
And a config.yml file:
"""
network-required-plugins:
check: Network_Required_Plugins
options:
plugins:
- akismet
"""

When I try `wp doctor check network-required-plugins --config=config.yml`
Then STDOUT should be a table containing rows:
| name | status | message |
| network-required-plugins | error | Required network plugin check failed. Not network-activated: akismet (inactive). |
And STDERR should contain:
"""
Error: 1 check reports 'error'.
"""
And the return code should be 1

Scenario: Required plugin is network-activated
Given a WP multisite installation
And a config.yml file:
"""
network-required-plugins:
check: Network_Required_Plugins
options:
plugins:
- akismet
"""
And I run `wp plugin activate akismet --network`

When I run `wp doctor check network-required-plugins --config=config.yml`
Then STDOUT should be a table containing rows:
| name | status | message |
| network-required-plugins | success | All required plugins are network-activated. |
46 changes: 46 additions & 0 deletions features/check-network-site-count.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Feature: Check multisite network site count

Scenario: Verify check description
Given an empty directory
And a config.yml file:
"""
network-site-count:
check: Network_Site_Count
"""

When I try `wp doctor list --fields=name,description --config=config.yml`
Then STDOUT should be a table containing rows:
| name | description |
| network-site-count | Warns when multisite network site count is outside the range 1-500. |

Scenario: Site count is within expected range
Given a WP multisite installation
And a config.yml file:
"""
network-site-count:
check: Network_Site_Count
options:
minimum: 1
maximum: 10
"""

When I run `wp doctor check network-site-count --config=config.yml`
Then STDOUT should be a table containing rows:
| name | status | message |
| network-site-count | success | Network has 1 sites; expected between 1 and 10. |

Scenario: Site count is outside expected range
Given a WP multisite installation
And a config.yml file:
"""
network-site-count:
check: Network_Site_Count
options:
minimum: 2
maximum: 10
"""

When I run `wp doctor check network-site-count --config=config.yml`
Then STDOUT should be a table containing rows:
| name | status | message |
| network-site-count | warning | Network has 1 sites; expected between 2 and 10. |
100 changes: 100 additions & 0 deletions features/check-network-site-option-value.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
Feature: Check the value of a network option

Scenario: Verify check description
Given an empty directory
And a config.yml file:
"""
network-registration:
check: Network_Site_Option_Value
options:
option: registration
value: none
"""

When I try `wp doctor list --fields=name,description --config=config.yml`
Then STDOUT should be a table containing rows:
| name | description |
| network-registration | Confirms the expected value of the network option 'registration'. |

Scenario: Check the value of a network option
Given a WP multisite installation
And a config.yml file:
"""
network-registration:
check: Network_Site_Option_Value
options:
option: registration
value: all
"""
And I run `wp eval 'update_site_option( "registration", "none" );'`

When I try `wp doctor check network-registration --config=config.yml`
Then STDOUT should be a table containing rows:
| name | status | message |
| network-registration | error | Network option 'registration' is 'none' but expected to be 'all'. |
And STDERR should contain:
"""
Error: 1 check reports 'error'.
"""
And the return code should be 1

When I run `wp eval 'update_site_option( "registration", "all" );'`
Then STDOUT should be empty

When I run `wp doctor check network-registration --config=config.yml`
Then STDOUT should be a table containing rows:
| name | status | message |
| network-registration | success | Network option 'registration' is 'all' as expected. |

Scenario: Check value_is_not for a network option
Given a WP multisite installation
And a config.yml file:
"""
network-registration:
check: Network_Site_Option_Value
options:
option: registration
value_is_not: none
"""
And I run `wp eval 'update_site_option( "registration", "none" );'`

When I try `wp doctor check network-registration --config=config.yml`
Then STDOUT should be a table containing rows:
| name | status | message |
| network-registration | error | Network option 'registration' is 'none' and expected not to be. |
And STDERR should contain:
"""
Error: 1 check reports 'error'.
"""
And the return code should be 1

When I run `wp eval 'update_site_option( "registration", "all" );'`
Then STDOUT should be empty

When I run `wp doctor check network-registration --config=config.yml`
Then STDOUT should be a table containing rows:
| name | status | message |
| network-registration | success | Network option 'registration' is not 'none' as expected. |

Scenario: Gracefully render array values in error messages
Given a WP multisite installation
And a config.yml file:
"""
network-registration:
check: Network_Site_Option_Value
options:
option: registration
value: none
"""
And I run `wp eval 'update_site_option( "registration", array( "users" => "all" ) );'`

When I try `wp doctor check network-registration --config=config.yml`
Then STDOUT should contain:
"""
Network option 'registration' is '{"users":"all"}' but expected to be 'none'.
"""
And STDERR should contain:
"""
Error: 1 check reports 'error'.
"""
And the return code should be 1
108 changes: 108 additions & 0 deletions src/Check/Network_Required_Plugins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace WP_CLI\Doctor\Check;

use WP_CLI;
use WP_CLI\Doctor\Check;

/**
* Errors when required plugins are not network-activated.
*/
class Network_Required_Plugins extends Check {

/**
* List of required plugin slugs.
*
* @var array
*/
protected $plugins = array();

public function __construct( $options = array() ) {
parent::__construct( $options );

if ( is_string( $this->plugins ) ) {
$this->plugins = explode( ',', $this->plugins );
}
if ( ! is_array( $this->plugins ) ) {
WP_CLI::error( 'Invalid plugins option. Provide an array or comma-delimited string.' );
}
$this->plugins = array_values( array_filter( array_map( 'trim', $this->plugins ) ) );
if ( empty( $this->plugins ) ) {
WP_CLI::error( 'At least one plugin slug is required.' );
}
}

public function run() {
if ( ! is_multisite() ) {
$this->set_status( 'success' );
$this->set_message( 'WordPress is not a multisite installation; required network plugin check skipped.' );
return;
}

if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'is_plugin_active' ) || ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

$installed_plugins = get_plugins();

$missing = array();
$not_network_active = array();
foreach ( $this->plugins as $plugin_slug ) {
$plugin_file = $this->get_plugin_file( $plugin_slug, $installed_plugins );
if ( null === $plugin_file ) {
$missing[] = $plugin_slug;
continue;
}

if ( is_plugin_active_for_network( $plugin_file ) ) {
continue;
}

$status = is_plugin_active( $plugin_file ) ? 'active' : 'inactive';
$not_network_active[] = "{$plugin_slug} ({$status})";
}

if ( empty( $missing ) && empty( $not_network_active ) ) {
$this->set_status( 'success' );
$this->set_message( 'All required plugins are network-activated.' );
return;
}

$parts = array();
if ( ! empty( $missing ) ) {
$parts[] = 'Missing plugins: ' . implode( ', ', $missing ) . '.';
}
if ( ! empty( $not_network_active ) ) {
$parts[] = 'Not network-activated: ' . implode( ', ', $not_network_active ) . '.';
}
$this->set_status( 'error' );
$this->set_message( 'Required network plugin check failed. ' . implode( ' ', $parts ) );
}

/**
* Resolve a plugin slug or file to an installed plugin file path.
*
* @param string $plugin_slug Requested plugin slug/file.
* @param array $installed_plugins Installed plugins keyed by plugin file.
* @return string|null
*/
private function get_plugin_file( $plugin_slug, $installed_plugins ) {
if ( isset( $installed_plugins[ $plugin_slug ] ) ) {
return $plugin_slug;
}

foreach ( array_keys( $installed_plugins ) as $plugin_file ) {
$directory_slug = dirname( $plugin_file );
if ( '.' !== $directory_slug && $directory_slug === $plugin_slug ) {
return $plugin_file;
}

$file_slug = basename( $plugin_file, '.php' );
if ( $file_slug === $plugin_slug ) {
return $plugin_file;
}
}

return null;
}
}
55 changes: 55 additions & 0 deletions src/Check/Network_Site_Count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace WP_CLI\Doctor\Check;

use WP_CLI;
use WP_CLI\Doctor\Check;

/**
* Warns when multisite network site count is outside the range %minimum%-%maximum%.
*/
class Network_Site_Count extends Check {

/**
* Minimum number of sites expected in the network.
*
* @var integer
*/
protected $minimum = 1;

/**
* Maximum number of sites expected in the network.
*
* @var integer
*/
protected $maximum = 500;

public function __construct( $options = array() ) {
parent::__construct( $options );

$this->minimum = (int) $this->minimum;
$this->maximum = (int) $this->maximum;
if ( $this->minimum < 0 || $this->maximum < 0 || $this->maximum < $this->minimum ) {
WP_CLI::error( 'Invalid thresholds. Ensure 0 <= minimum <= maximum.' );
}
}

public function run() {
if ( ! is_multisite() ) {
$this->set_status( 'success' );
$this->set_message( 'WordPress is not a multisite installation; network site count check skipped.' );
return;
}

$count = (int) get_sites( array( 'count' => true ) );

if ( $count < $this->minimum || $count > $this->maximum ) {
$this->set_status( 'warning' );
$this->set_message( "Network has {$count} sites; expected between {$this->minimum} and {$this->maximum}." );
return;
}

$this->set_status( 'success' );
$this->set_message( "Network has {$count} sites; expected between {$this->minimum} and {$this->maximum}." );
}
}
Loading