Skip to content

Commit

Permalink
Merge pull request #36 from wojsmol/use-wp_cli_cs
Browse files Browse the repository at this point in the history
Implement CS checking based on the `WP_CLI_CS` ruleset
  • Loading branch information
schlessera authored Apr 20, 2019
2 parents 7cba321 + 6a626dc commit bd1543c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
.travis.yml
behat.yml
circle.yml
phpcs.xml.dist
phpunit.xml.dist
bin/
features/
utils/
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.DS_Store
.phpcs.xml
phpunit.xml
phpcs.xml
wp-cli.local.yml
node_modules/
vendor/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
58 changes: 58 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0"?>
<ruleset name="WP-CLI-super-admin">
<description>Custom ruleset for WP-CLI super-admin-command</description>

<!--
#############################################################################
COMMAND LINE ARGUMENTS
For help understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml
For help using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
#############################################################################
-->

<!-- What to scan. -->
<file>.</file>

<!-- Show progress. -->
<arg value="p"/>

<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>

<!-- Check up to 8 files simultaneously. -->
<arg name="parallel" value="8"/>

<!--
#############################################################################
USE THE WP_CLI_CS RULESET
#############################################################################
-->

<rule ref="WP_CLI_CS"/>

<!--
#############################################################################
PROJECT SPECIFIC CONFIGURATION FOR SNIFFS
#############################################################################
-->

<!-- For help understanding the `testVersion` configuration setting:
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.4-"/>

<!-- Verify that everything in the global namespace is either namespaced or prefixed.
See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array">
<element value="WP_CLI\SuperAdmin"/><!-- Namespaces. -->
<element value="wpcli_super_admin"/><!-- Global variables and such. -->
</property>
</properties>
</rule>

<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
<exclude-pattern>*/src/Super_Admin_Command\.php$</exclude-pattern>
</rule>
</ruleset>
57 changes: 33 additions & 24 deletions src/Super_Admin_Command.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
use WP_CLI\Fetchers\User as UserFetcher;

/**
* Lists, adds, or removes super admin users on a multisite installation.
Expand All @@ -23,11 +24,11 @@
class Super_Admin_Command extends WP_CLI_Command {

private $fields = array(
'user_login'
'user_login',
);

public function __construct() {
$this->fetcher = new \WP_CLI\Fetchers\User;
$this->fetcher = new UserFetcher();
}

/**
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -113,15 +114,15 @@ 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." );
} else {
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." );
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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.";
}
Expand All @@ -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' ) );
}
}
24 changes: 14 additions & 10 deletions super-admin-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.' );
}
},
)
);

0 comments on commit bd1543c

Please sign in to comment.