Skip to content

Commit

Permalink
SyndicatedPost / build in a sanity check to avoid an infinite loop wh…
Browse files Browse the repository at this point in the history
…en an unexpected WP_Error comes up for the user login name.
  • Loading branch information
radgeek committed Jan 23, 2022
1 parent b3667c2 commit 5afc20f
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions syndicatedpost.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2339,16 +2339,23 @@ function author_id ($unfamiliar_author = 'create') {

#-- loop. Keep trying to add the user until you get it
#-- right. Or until PHP crashes, I guess.
$insanity = 0;
do {
$id = wp_insert_user($userdata);
if (is_wp_error($id)) :
$codes = $id->get_error_code();
switch ($codes) :
case 'empty_user_login' :
case 'existing_user_login' :
case 'invalid_username' :
// Add a random disambiguator
$userdata['user_login'] .= substr(md5(uniqid(microtime())), 0, 6);
break;
case 'user_login_too_long' :
// Limit length to 53 characters; if we end up needing a random disambiguator,
// we should still have space to add it.
$userdata['user_login'] = mb_substr( $userdata['user_login'], 0, 53 );
break;
case 'user_nicename_too_long' :
// Add a limited 50 characters user_nicename based on user_login
$userdata['user_nicename'] = mb_substr( $userdata['user_login'], 0, 50 );
Expand All @@ -2363,17 +2370,34 @@ function author_id ($unfamiliar_author = 'create') {
// Reassemble
$userdata['user_email'] = $parts[0].'@'.$parts[1];
break;
default :
if ( $insanity > 10 ) :
// Try some settings that are unlikely to cause complaint...
$url = parse_url($hostUrl);

$userdata['user_login'] = substr(md5(uniqid(microtime())), 0, 6);
$userdata['user_nicename'] = $userdata['user_login'];
$userdata['user_email'] = 'noreply@' . $url['host'];
elseif ( $insanity > 50 ) :
// Stop doing the same thing and expecting a different result
break;
endif;
endswitch;
endif;
$insanity = $insanity + 1;
} while (is_wp_error($id));

// $id should now contain the numeric ID of a newly minted
// user account. Let's mark them as having been generated
// by FeedWordPress in the usermeta table, as per the
// suggestion of @boonebgorges, in case we need to process,
// winnow, filter, or merge syndicated author accounts, &c.
add_user_meta($id, 'feedwordpress_generated', 1);

if (!is_wp_error($id)) :
add_user_meta($id, 'feedwordpress_generated', 1);
else :
$id = null;
endif;

elseif (is_numeric($unfamiliar_author) and get_userdata((int) $unfamiliar_author)) :
$id = (int) $unfamiliar_author;
elseif ($unfamiliar_author === 'default') :
Expand Down

0 comments on commit 5afc20f

Please sign in to comment.