thoughtis / cata-co-authors-plus

Common functions, configuration and compatibility fixes for Co-Authors Plus when used in Cata child themes. Not a fork or replacement for CAP.
GNU General Public License v3.0
0 stars 0 forks source link

Get the queried author when viewing the author profile #5

Closed douglas-johnson closed 3 years ago

douglas-johnson commented 3 years ago

CAP hooks into some of the core WordPress functions for displaying author information. But, if you have anything custom and you want to get the author object, its a little tough to obtain.

This also contains a test for whether or not a coauthor is a guest author, which determines if they have any of the custom data that is only appended to Guest Author profiles.

We handled this similarly on Creepy Catalog and Shop Catalog

/**
 * CoAuthor is Guest Author
 * Is this coauthor an object where the type value is 'guest-author'?
 * 
 * @param  object $coauthor Object to test.
 * @return bool Whether or not the object passed the test.
 */
function creepycatalog_coauthor_is_guest_author( $coauthor ) : bool {
    return 'guest-author' === creepycatalog_safe_get_object_property( 'type', $coauthor );
}

/**
 * Get Global Guest Author
 * 
 * @global $coauthors_plus
 * @return mixed {object|WP_User|false}
 */
function creepycatalog_get_global_guest_author() {

    global $coauthors_plus;

    $author_name = sanitize_user( get_query_var( 'author_name' ) );

    return $coauthors_plus->get_coauthor_by( 'user_nicename', $author_name );
}

/**
 * Has Global Guest Author
 * 
 * @return bool Whether author_name query variable is associated with a Guest Author.
 */
function creepycatalog_has_global_guest_author() : bool {
    return creepycatalog_coauthor_is_guest_author(
        creepycatalog_get_global_guest_author()
    );
}