diff --git a/.distignore b/.distignore index b964b40c7..95b52fb02 100644 --- a/.distignore +++ b/.distignore @@ -6,6 +6,8 @@ .travis.yml behat.yml circle.yml +phpcs.xml.dist +phpunit.xml.dist bin/ features/ utils/ diff --git a/.gitignore b/.gitignore index ff4941991..54612060b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ .DS_Store +.phpcs.xml +phpunit.xml +phpcs.xml wp-cli.local.yml node_modules/ vendor/ diff --git a/composer.json b/composer.json index e85de13a3..540f2d5ae 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ }, "require-dev": { "wp-cli/entity-command": "^1.3 || ^2", - "wp-cli/wp-cli-tests": "^2.0.7" + "wp-cli/wp-cli-tests": "^2.1" }, "config": { "process-timeout": 7200, diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 000000000..3ae3aff14 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,58 @@ + + + Custom ruleset for WP-CLI super-admin-command + + + + + . + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */src/Super_Admin_Command\.php$ + + diff --git a/src/Super_Admin_Command.php b/src/Super_Admin_Command.php index 47d3c48a0..e52e49074 100644 --- a/src/Super_Admin_Command.php +++ b/src/Super_Admin_Command.php @@ -1,4 +1,5 @@ fetcher = new \WP_CLI\Fetchers\User; + $this->fetcher = new UserFetcher(); } /** @@ -57,18 +58,17 @@ public function __construct() { * * @subcommand list */ - public function _list( $_, $assoc_args ) { + public function list_subcommand( $_, $assoc_args ) { $super_admins = self::get_admins(); if ( 'list' === $assoc_args['format'] ) { foreach ( $super_admins as $user_login ) { WP_CLI::line( $user_login ); } - } - else { + } else { $output_users = array(); foreach ( $super_admins as $user_login ) { - $output_user = new stdClass; + $output_user = new stdClass(); $output_user->user_login = $user_login; @@ -94,17 +94,18 @@ public function _list( $_, $assoc_args ) { */ public function add( $args, $_ ) { - $successes = $errors = 0; - $users = $this->fetcher->get_many( $args ); - if ( count( $users ) != count( $args ) ) { + $successes = 0; + $errors = 0; + $users = $this->fetcher->get_many( $args ); + if ( count( $users ) !== count( $args ) ) { $errors = count( $args ) - count( $users ); } - $user_logins = wp_list_pluck( $users, 'user_login' ); - $super_admins = self::get_admins(); + $user_logins = wp_list_pluck( $users, 'user_login' ); + $super_admins = self::get_admins(); $num_super_admins = count( $super_admins ); foreach ( $user_logins as $user_login ) { - if ( in_array( $user_login, $super_admins ) ) { + if ( in_array( $user_login, $super_admins, true ) ) { WP_CLI::warning( "User '{$user_login}' already has super-admin capabilities." ); continue; } @@ -113,7 +114,7 @@ public function add( $args, $_ ) { $successes++; } - if ( $num_super_admins === count( $super_admins ) ) { + if ( count( $super_admins ) === $num_super_admins ) { if ( $errors ) { $user_count = count( $args ); WP_CLI::error( "Couldn't grant super-admin capabilities to {$errors} of {$user_count} users." ); @@ -121,7 +122,7 @@ public function add( $args, $_ ) { WP_CLI::success( 'Super admins remain unchanged.' ); } } else { - if ( update_site_option( 'site_admins' , $super_admins ) ) { + if ( update_site_option( 'site_admins', $super_admins ) ) { if ( $errors ) { $user_count = count( $args ); WP_CLI::error( "Only granted super-admin capabilities to {$successes} of {$user_count} users." ); @@ -154,18 +155,26 @@ public function remove( $args, $_ ) { WP_CLI::error( 'No super admins to revoke super-admin privileges from.' ); } - $users = $this->fetcher->get_many( $args ); - $user_logins = $users ? array_values( array_unique( wp_list_pluck( $users, 'user_login' ) ) ) : array(); + $users = $this->fetcher->get_many( $args ); + $user_logins = $users ? array_values( array_unique( wp_list_pluck( $users, 'user_login' ) ) ) : array(); $user_logins_count = count( $user_logins ); if ( $user_logins_count < count( $args ) ) { $flipped_user_logins = array_flip( $user_logins ); // Fetcher has already warned so don't bother here, but continue with any args that are possible login names to cater for invalid users in the site options meta. - $user_logins = array_merge( $user_logins, array_unique( array_filter( $args, function ( $v ) use ( $flipped_user_logins ) { - // Exclude numeric and email-like logins (login names can be email-like but ignore this given the circumstances). - return ! isset( $flipped_user_logins[ $v ] ) && ! is_numeric( $v ) && ! is_email( $v ); - } ) ) ); + $user_logins = array_merge( + $user_logins, + array_unique( + array_filter( + $args, + function ( $v ) use ( $flipped_user_logins ) { + // Exclude numeric and email-like logins (login names can be email-like but ignore this given the circumstances). + return ! isset( $flipped_user_logins[ $v ] ) && ! is_numeric( $v ) && ! is_email( $v ); + } + ) + ) + ); $user_logins_count = count( $user_logins ); } if ( ! $user_logins ) { @@ -174,15 +183,15 @@ public function remove( $args, $_ ) { $update_super_admins = array_diff( $super_admins, $user_logins ); if ( $update_super_admins === $super_admins ) { - WP_CLI::error( $user_logins_count > 1 ? 'None of the given users is a super admin.' : 'The given user is not a super admin.' ); + WP_CLI::error( $user_logins_count > 1 ? 'None of the given users is a super admin.' : 'The given user is not a super admin.' ); } - update_site_option( 'site_admins' , $update_super_admins ); + update_site_option( 'site_admins', $update_super_admins ); $successes = count( $super_admins ) - count( $update_super_admins ); if ( $successes === $user_logins_count ) { $message = $user_logins_count > 1 ? 'users' : 'user'; - $msg = "Revoked super-admin capabilities from {$user_logins_count} {$message}."; + $msg = "Revoked super-admin capabilities from {$user_logins_count} {$message}."; } else { $msg = "Revoked super-admin capabilities from {$successes} of {$user_logins_count} users."; } @@ -194,6 +203,6 @@ public function remove( $args, $_ ) { private static function get_admins() { // We don't use get_super_admins() because we don't want to mess with the global - return (array) get_site_option( 'site_admins', array('admin') ); + return (array) get_site_option( 'site_admins', array( 'admin' ) ); } } diff --git a/super-admin-command.php b/super-admin-command.php index afced9cbb..c80bd228e 100644 --- a/super-admin-command.php +++ b/super-admin-command.php @@ -4,15 +4,19 @@ return; } -$autoload = dirname( __FILE__ ) . '/vendor/autoload.php'; -if ( file_exists( $autoload ) ) { - require_once $autoload; +$wpcli_super_admin_autoloader = dirname( __FILE__ ) . '/vendor/autoload.php'; +if ( file_exists( $wpcli_super_admin_autoloader ) ) { + require_once $wpcli_super_admin_autoloader; } -WP_CLI::add_command( 'super-admin', 'Super_Admin_Command', array( - 'before_invoke' => function () { - if ( !is_multisite() ) { - WP_CLI::error( 'This is not a multisite installation.' ); - } - } -) ); +WP_CLI::add_command( + 'super-admin', + 'Super_Admin_Command', + array( + 'before_invoke' => function () { + if ( ! is_multisite() ) { + WP_CLI::error( 'This is not a multisite installation.' ); + } + }, + ) +);