Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sync comments with missing email addresses
<?php
function rv_name_search($name) {
// set up SQL query
global $wpdb;
// run SQL query
$name_query = $wpdb->get_results ("
SELECT *
FROM $wpdb->comments
WHERE comment_author = '".$name."'
"
);
$count = count($name_query);
// bail if no counts
if ( $count = 0 )
return false;
// check for values and filter through
foreach ($name_query as $names) :
// create new array of just email addresses
$emails[] = $names->comment_author_email;
endforeach;
// parse down those missing and get a single address
$new_email = current( array_filter( $emails ) );
return $new_email;
}
// get array of comments without an author
function rv_comment_missing($pid) {
$args = array(
'status' => 'approve',
'post_id' => $pid,
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
$missing_ids = false;
if (!$comments )
return $missing_ids;
foreach ( $comments as $comment ) :
$cm_id = $comment->comment_ID;
$cm_name = $comment->comment_author;
$cm_email = $comment->comment_author_email;
if (empty($cm_email)) :
$missing_ids[] = $cm_id;
endif;
endforeach;
return $missing_ids;
}
function rv_comment_sync($pid) {
$comments = rv_comment_missing($pid);
if (!$comments )
return;
foreach ( $comments as $comment ) :
$detail = get_comment($comment);
$cm_id = $detail->comment_ID;
$cm_name = $detail->comment_author;
$cm_email = $detail->comment_author_email;
if( !empty( $cm_email ) )
return;
$new_email = rv_name_search($cm_name);
// run update function
$commentarr = get_comment($cm_id, ARRAY_A);
$commentarr['comment_author_email'] = $new_email;
wp_update_comment($commentarr);
endforeach;
}
function rv_comment_process() {
$args = array(
'fields' => 'ids',
'post_type' => 'post',
'numberposts' => -1,
'no_found_rows' => true,
);
$pids = get_posts($args);
foreach ($pids as $pid):
rv_comment_sync($pid);
endforeach;
}
// now....LETS DO THIS
rv_comment_process();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment