Skip to content

Instantly share code, notes, and snippets.

@chaance
Last active June 12, 2018 15:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chaance/b0875c7b47aa3e5cdeac3100f1859ac7 to your computer and use it in GitHub Desktop.
Save chaance/b0875c7b47aa3e5cdeac3100f1859ac7 to your computer and use it in GitHub Desktop.
Function to dynamically sync CPT team posts with WP users. The post type requires custom meta fields for linked_author (user ID) and email.
<?php
/**
* Helper funnction to remove name suffixes.
*
* @param string $name Name.
* @return array Suffix-free name parts.
*/
function xx_get_suffix_free_name_parts( $name ) {
// Format first and last names.
$parts = explode( ' ', (string) $name );
// Trim whitespace and punctuation.
$parts = array_map( function( $part ) {
return trim( $part, " \t\n\r\0\x0B,.;:'\"[]{}|\\/><()`~!@#$%^&*" );
}, $parts );
// Filter out suffixes and one-letter initials.
$parts = array_filter( $parts, function( $part ) {
$suffixes = [
'Jr',
'Sr',
'II',
'III',
'IV',
'V',
'VI',
'VII',
'VIII',
'IX',
'X',
];
return ! ( in_array( $part, $suffixes, true ) || empty( $part ) || strlen( $part ) === 1 );
} );
return $parts;
}
/**
* Create new users to match team members.
*
* @param integer $post_id Post ID.
*/
function xx_team_post_save_link_user( $post_id ) {
// If not a team post, let's bail.
if ( 'team' !== get_post_type( $post_id ) ) {
return;
}
// Format first and last names.
$name_parts = xx_get_suffix_free_name_parts( get_the_title( $post_id ) );
$first_name = $name_parts[0];
$last_name = $name_parts[ count( $name_parts ) - 1 ];
// Get user meta and data.
$author_id = get_post_meta( $post_id, 'linked_author', true );
$user_email = get_post_meta( $post_id, 'email', true );
// Create phony email if the email field isn't set.
if ( empty( $user_email ) ) {
$user_email = sanitize_text_field( $first_name ) . sanitize_text_field( $last_name ) . '@enablecomp.com';
}
// Check if Linked user is already set.
if ( $author_id ) {
$user_meta = get_user_meta( $author_id );
$user_data = get_userdata( $author_id );
// If field is set...
if ( username_exists( $user_data->user_login ) ) {
// Get the user.
// Compare fields and data.
if ( get_the_title( $post_id ) !== $user_data->display_name ) {
wp_update_user( [
'ID' => (int) $author_id,
'display_name' => sanitize_text_field( get_the_title( $post_id ) ),
] );
}
if ( is_email( $user_email ) ) {
if ( $user_email !== $user_data->user_email ) {
wp_update_user( [
'ID' => (int) $author_id,
'user_email' => sanitize_email( $user_email ),
] );
}
}
if ( $user_meta['first_name'] !== $first_name ) {
wp_update_user( [
'ID' => (int) $author_id,
'first_name' => sanitize_text_field( $first_name ),
] );
}
if ( $user_meta['last_name'] !== $last_name ) {
wp_update_user( [
'ID' => (int) $author_id,
'last_name' => sanitize_text_field( $last_name ),
] );
}
return;
}
} else { // Look for user with matching email address and link the user if they exist.
if ( is_email( $user_email ) ) {
foreach ( get_users() as $user ) {
if ( $user_email === $user->data->user_email ) {
update_post_meta( $post_id, 'linked_author', $user->ID );
break;
return;
}
}
}
}
// If not, let's create a user (author).
if ( is_email( $user_email ) ) {
$username = $first_name && $last_name ? strtolower( $first_name ) . '.' . strtolower( preg_replace( '/\s+/', '', $last_name ) ) : strtolower( get_the_title( $post_id ) );
// For reference, wp_insert_user sanitizes the user login before it is updated in the DB.
wp_insert_user( [
'user_login' => strtolower( $username ),
'user_pass' => wp_generate_password(),
'user_email' => sanitize_email( $user_email ),
'display_name' => sanitize_text_field( get_the_title( $post_id ) ),
'first_name' => sanitize_text_field( $first_name ),
'last_name' => sanitize_text_field( $last_name ),
'role' => 'author',
] );
// Link the post with the new author.
update_post_meta( $post_id, 'linked_author', get_user_by( 'email', sanitize_email( $user_email ) )->ID );
}
return;
}
add_action( 'save_post', 'xx_team_post_save_link_user' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment