Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Specify user by ID in assign-user-to-coauthor #698

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
20 changes: 14 additions & 6 deletions php/class-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,30 +210,38 @@ public function assign_coauthors( $args, $assoc_args ) {
* @since 3.0
*
* @subcommand assign-user-to-coauthor
* @synopsis --user_login=<user-login> --coauthor=<coauthor>
* @synopsis --user_login=<user-login> --user_id=<user-id> --coauthor=<coauthor>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not at all sure how to specify that one of user_login or user_id is required. Anyone else?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In git's man pages, parentheses and pipes are used to indicate that a subcommand accepts one and only one of a selection of flags (see the last line):

SYNOPSIS
       git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>]
               [<upstream> [<branch>]]
       git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>]
               --root [<branch>]
       git rebase (--continue | --skip | --abort | --quit | --edit-todo | --show-current-patch)

So this line could be rewritten as:

	 * @synopsis ( --user_login=<user-login> | --user_id=<user-id> ) --coauthor=<coauthor>

*/
public function assign_user_to_coauthor( $args, $assoc_args ) {
global $coauthors_plus, $wpdb;

$defaults = array(
'user_login' => '',
'user_login' => NULL,
'user_id' => NULL,
'coauthor' => '',
);
$assoc_args = wp_parse_args( $assoc_args, $defaults );

$user = get_user_by( 'login', $assoc_args['user_login'] );
$user_id = intval($assoc_args['user_id']);
if ( $assoc_args['user_login'] ) {
$user = get_user_by( 'login', $assoc_args['user_login'] );
if ($user) {
$user_id = $user->ID;
}
}

$coauthor = $coauthors_plus->get_coauthor_by( 'login', $assoc_args['coauthor'] );

if ( ! $user ) {
WP_CLI::error( __( 'Please specify a valid user_login', 'co-authors-plus' ) );
if ( ! $user_id ) {
Floppy marked this conversation as resolved.
Show resolved Hide resolved
WP_CLI::error( __( 'Please specify a valid user_login or user_id', 'co-authors-plus' ) );
}

if ( ! $coauthor ) {
WP_CLI::error( __( 'Please specify a valid co-author login', 'co-authors-plus' ) );
}

$post_types = implode( "','", $coauthors_plus->supported_post_types );
$posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author=%d AND post_type IN ('$post_types')", $user->ID ) );
$posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author=%d AND post_type IN ('$post_types')", $user_id ) );
$affected = 0;
foreach ( $posts as $post_id ) {
$coauthors = cap_get_coauthor_terms_for_post( $post_id );
Expand Down