|
<?php |
|
/** |
|
* Creates a CoAuthor and returns the user_nicename. |
|
* Returns false if the author couldn't be created. |
|
* |
|
* @param $author_name string Author Name. |
|
* |
|
* @return false|string |
|
*/ |
|
function create_and_get_author( $author_name ) { |
|
global $coauthors_plus; |
|
|
|
// Build the author name and slug. |
|
$author_slug = sanitize_title( $author_name ); |
|
$author_tax_slug = 'cap-' . $author_slug; |
|
|
|
// Create the post name to be used for the guest author |
|
$post_name = $coauthors_plus->guest_authors->get_post_meta_key( $author_slug ); |
|
|
|
// Create the user as a new guest |
|
$new_post = [ |
|
'post_title' => $author_name, |
|
'post_name' => $post_name, |
|
'post_type' => $coauthors_plus->guest_authors->post_type, |
|
'post_status' => 'publish', |
|
]; |
|
$user_post_id = wp_insert_post( $new_post, true ); |
|
|
|
if ( is_wp_error( $user_post_id ) ) { |
|
return false; |
|
} else { |
|
// Add or update the necessary meta values |
|
update_post_meta( $user_post_id, $coauthors_plus->guest_authors->get_post_meta_key( 'display_name' ), $author_name ); |
|
update_post_meta( $user_post_id, $coauthors_plus->guest_authors->get_post_meta_key( 'user_login' ), $author_slug ); |
|
|
|
// Ensure there's an 'author' term for this user/guest author |
|
if ( ! wpcom_vip_term_exists( $author_slug, $coauthors_plus->coauthor_taxonomy ) ) { |
|
$author_term = wp_insert_term( |
|
$author_slug, |
|
$coauthors_plus->coauthor_taxonomy, |
|
[ |
|
'slug' => $author_tax_slug, |
|
] |
|
); |
|
if ( ! is_wp_error( $author_term ) ) { |
|
$author = $coauthors_plus->get_coauthor_by( 'id', $user_post_id ); |
|
return $author->user_nicename; |
|
} else { |
|
return false; |
|
} |
|
} |
|
} |
|
return false; |
|
} |