/**
  * Test registering, removing, and modifying a role.
  */
 public function test_setup_and_remove_author_roles()
 {
     // The default author roles should be active initially.
     $roles = get_author_roles();
     $this->assertEquals(3, count($roles));
     $this->assertEquals('Author', $roles['author']->name);
     // removing and adding roles.
     remove_author_role('photographer');
     $roles = get_author_roles();
     $this->assertEquals(2, count($roles));
 }
/**
 * Clear all coauthor data on a post.
 *
 * Used when updating coauthors, as otherwise there's no way of ordering them.
 *
 * @param int $post_id Post to remove coauthor postmeta from
 */
function remove_all_coauthor_meta($post_id)
{
    // Get the author_role terms as they would be stored in post meta: look up all the author roles
    // registered, and add the "cap-" prefix to generate the key used here.
    $roles_meta_keys = array_map(function ($term) {
        return 'cap-' . $term->slug;
    }, get_author_roles());
    foreach (get_post_meta($post_id) as $key => $values) {
        if (in_array($key, $roles_meta_keys)) {
            delete_post_meta($post_id, $key);
        }
    }
}
Esempio n. 3
0
/**
 * Get all coauthors on a post, and their roles.
 *
 * A lot of copypasta from get_coauthors() in Co-Authors Plus, with additional options.
 *
 * @param int $post_ID ID of post to check. Defaults to the current post.
 * @param arr $args    Query options. Available options are:
 *						author_role array of roles to query in
 * @return arr Array of authors
 */
function get_coauthors($post_id = 0, $args = array())
{
    global $post, $post_ID, $coauthors_plus, $wpdb;
    // Merge default query args with parameters
    $args = wp_parse_args($args, array('author_role' => 'byline'));
    $coauthors = array();
    // Default to current post if $post_id is not set.
    $post_id = (int) $post_id;
    if (!$post_id && $post_ID) {
        $post_id = $post_ID;
    }
    if (!$post_id && $post) {
        $post_id = $post->ID;
    }
    if (!$post_id) {
        return;
    }
    // Querying for "bylines" (the default) should return the same results as in CAP
    if ($args['author_role'] === 'byline') {
        /*
         * This query is the same as the one from Co-Authors Plus, because any co-authors
         * added in byline roles should have an author term set.
         */
        $coauthor_terms = get_the_terms($post_id, $coauthors_plus->coauthor_taxonomy);
        if (is_array($coauthor_terms) && !empty($coauthor_terms)) {
            foreach ($coauthor_terms as $coauthor) {
                $coauthor_slug = preg_replace('#^cap\\-#', '', $coauthor->slug);
                $post_author = $coauthors_plus->get_coauthor_by('user_nicename', $coauthor_slug);
                // In case the user has been deleted while plugin was deactivated
                if (!empty($post_author)) {
                    $post_author->author_role = 'byline';
                    $coauthors[] = $post_author;
                }
            }
        } else {
            if (!$coauthors_plus->force_guest_authors) {
                if ($post && $post_id == $post->ID) {
                    $post_author = get_userdata($post->post_author);
                } else {
                    $post_author = get_userdata($wpdb->get_var($wpdb->prepare("SELECT post_author FROM {$wpdb->posts} WHERE ID = %d", $post_id)));
                }
                if (!empty($post_author)) {
                    $coauthors[] = $post_author;
                }
            }
        }
        // the empty else case is because if we force guest authors, we don't ever care what value wp_posts.post_author has.
    } else {
        /*
         * Any other queries need to be performed using postmeta fields.
         *
         * Passing the string 'any' or false to 'author_role' should get anyone, regardless of role.
         * Passing a string or array of strings should get all roles with those slugs.
         */
        if ($args['author_role'] === 'any' || !$args['author_role']) {
            $author_roles = wp_list_pluck(get_author_roles(), 'slug');
        } else {
            if (is_string($args['author_role'])) {
                $author_roles = array($args['author_role']);
            } else {
                $author_roles = $args['author_role'];
            }
        }
        // Get the terms as stored in post meta: look up all the author roles passed in by function
        // arguments (to whitelist arguments), and return the slug for each as would be used in post meta keys.
        $roles_meta_keys = array_map(function ($term) {
            return 'cap-' . $term;
        }, array_filter($author_roles, 'CoAuthorsPlusRoles\\get_author_role'));
        // Get all coauthors on this post, from post meta values
        $coauthor_nicenames = array();
        foreach (get_post_meta($post_id) as $key => $values) {
            if (in_array($key, $roles_meta_keys)) {
                foreach ($values as $author_name) {
                    $author = $coauthors_plus->get_coauthor_by('user_nicename', $author_name);
                    if ($role = get_author_role(substr($key, 4))) {
                        $author->author_role = $role->slug;
                    }
                    $coauthors[] = $author;
                }
            }
        }
        if ($args['author_role'] === 'any' || !$args['author_role'] || is_array($args['author_role']) && in_array('byline', $args['author_role'])) {
            // Now, get the author terms, in case of bylines or coauthors who were entered with CAP.
            $coauthor_terms = get_the_terms($post_id, $coauthors_plus->coauthor_taxonomy);
            $byline_coauthors = array();
            if (is_array($coauthor_terms) && !empty($coauthor_terms)) {
                foreach ($coauthor_terms as $coauthor) {
                    $coauthor_slug = preg_replace('#^cap\\-#', '', $coauthor->slug);
                    // Since some authors may have also been added through postmeta, skip them here
                    // so as not to include duplicate authors in results.
                    if (in_array($coauthor_slug, wp_list_pluck($coauthors, 'user_nicename'))) {
                        continue;
                    }
                    $post_author = $coauthors_plus->get_coauthor_by('user_nicename', $coauthor_slug);
                    /*
                     * The special case where `author_role` is empty should be handled separately.
                     * An empty role means that its treated as a "byline". We give it a name here for display purposes,
                     * but this name is never saved.
                     */
                    $post_author->author_role = esc_html__('byline', 'co-authors-plus-roles');
                    // In case the user has been deleted while plugin was deactivated
                    if (!empty($post_author)) {
                        $byline_coauthors[] = $post_author;
                    }
                }
            }
            $coauthors = array_merge($byline_coauthors, $coauthors);
        }
    }
    return $coauthors;
}
function coauthor_select_dialog()
{
    global $post_ID, $post;
    $post_id = $post_ID;
    if (!isset($post_id) && isset($post)) {
        $post_id = $post->ID;
    }
    ?>
	<div id="coauthor-select-backdrop" style="display: none"></div>
	<div id="coauthor-select-wrap" class="wp-core-ui" style="display: none">
		<form id="coauthor-select-contributor" tabindex="-1">
		<?php 
    wp_nonce_field('coauthor-select', '_coauthor_select_nonce', false);
    ?>
		<input type="hidden" id="coauthor-post-id" value="<?php 
    echo $post_id;
    ?>
" />
			<div id="coauthor-select-modal-title">
				<span id="coauthor-select-header"></span>
				<button type="button" id="coauthor-select-close">
					<span class="screen-reader-text"><?php 
    esc_html_e('Close', 'co-authors-plus-roles');
    ?>
</span>
				</button>
			</div>
			<div id="coauthor-select">
				<input type="hidden" id="coauthor-author-nicename" value="" />
				<div id="coauthor-search-panel">
					<div class="coauthor-search-wrapper">
						<label>
							<p class="howto"><?php 
    esc_html_e('Search by name or email address:', 'co-authors-plus-roles');
    ?>
</p>
							<input type="search" id="coauthor-search-field" class="coauthor-search-field" autocomplete="off" />
							<span class="spinner"></span>
						</label>
					</div>
					<div id="search-results" class="query-results" tabindex="0">
						<ul></ul>
						<div class="river-waiting">
							<span class="spinner"></span>
						</div>
					</div>
					<div id="most-recent-results" class="query-results" tabindex="0">
						<div class="query-notice" id="query-notice-message">
							<em class="query-notice-default"><?php 
    esc_html_e('No search term specified. Showing most used authors.', 'co-authors-plus-roles');
    ?>
</em>
							<em class="query-notice-hint screen-reader-text"><?php 
    esc_html_e('Search or use up and down arrow keys to select an author.', 'co-authors-plus-roles');
    ?>
</em>
						</div>
						<ul></ul>
						<div class="river-waiting">
							<span class="spinner"></span>
						</div>
					</div>
				</div>
			</div>
			<?php 
    $roles_available = apply_filters('coauthors_author_roles', get_author_roles(), $post_id);
    $options = array('' => __('Choose a role', 'co-authors-plus-roles'));
    foreach ($roles_available as $role) {
        $options[$role->slug] = $role->name;
    }
    $options = apply_filters('coauthors_role_options', $options, $post_id);
    if (!empty($options)) {
        ?>
			<div id="coauthor-options">
				<p class="howto"><?php 
        esc_html_e('Choose the role for this contributor:', 'co-authors-plus-roles');
        ?>
</p>
				<select id="coauthor-select-role" name="coauthor-select-role">
					<?php 
        foreach ($options as $value => $label) {
            echo '<option value="' . esc_attr($value) . '">' . esc_html($label) . '</option>';
        }
        ?>
				</select>
			</div>
			<?php 
    }
    ?>
			<div class="submitbox">
				<div id="coauthor-select-cancel">
					<a class="submitdelete deletion" href="#"><?php 
    esc_html_e('Cancel', 'co-authors-plus-roles');
    ?>
</a>
				</div>
				<div id="coauthor-select-update">
					<input type="submit" value="<?php 
    esc_attr_e('Add coauthor to post', 'co-authors-plus-roles');
    ?>
" class="button button-primary" id="coauthor-select-submit" name="coauthor-select-submit">
				</div>
			</div>
		</form>
		</div>
	<?php 
}