Skip to content

Instantly share code, notes, and snippets.

@ericnicolaas
Created May 26, 2022 02:18
Show Gist options
  • Save ericnicolaas/0392efa46f8456a9ddc5bb004d39070f to your computer and use it in GitHub Desktop.
Save ericnicolaas/0392efa46f8456a9ddc5bb004d39070f to your computer and use it in GitHub Desktop.
Add donor comments for donations where the comments were not added correctly
<?php
function ed_add_missing_comment( $donation_id ) {
$comment = get_post_meta( $donation_id, 'donor_comment', true );
if ( empty( $comment ) ) {
return;
}
$donation = charitable_get_donation($donation_id );
$donor = $donation->get_donor();
$campaigns = $donation->get_campaign_donations();
$comments = array();
if ( isset( $donor->is_anonymous ) && $donor->is_anonymous ) {
$comment_author = '';
} else {
$comment_author = trim( sprintf( '%s %s', $donor->__get('first_name'), $donor->__get('last_name') ) );
}
/**
* Generally there will only be one campaign per donation, but the data
* structure does support a single donation being for multiple campaigns.
*/
foreach ( $campaigns as $campaign ) {
/**
* Filter the comment data that will be passed to wp_insert_comment.
*
* @see wp_insert_comment
*
* @since 1.0.0
*
* @param array $data The comment data.
* @param Charitable_Donation_Processor $processor Donation Processor object.
*/
$comment_data = array(
'comment_post_ID' => $campaign->campaign_id,
'comment_author' => $comment_author,
'comment_author_email' => $donor->__get('email'),
'comment_author_IP' => '',// Set to empty string
'comment_author_url' => '', // Set to empty string
'comment_date' => get_post_field( 'post_date', $donation_id, 'db' ),
'comment_date_gmt' => get_post_field( 'post_date_gmt', $donation_id, 'db' ),
'comment_content' => $comment,
'comment_type' => 'charitable_comment',
'comment_parent' => 0, // Seems to be neccesary to prevent it using my last comment as the parent.
'comment_meta' => array(
'donation_id' => $donation_id,
'is_anonymous' => get_post_meta( $donation_id, 'anonymous_donation', true ) ? 1 : 0 ),
'user_id' => 0, // Seems to be necesary to prevent it using my admin user id. Could be wrong sometimes but not a lot.
);
$comment_id = wp_new_comment( $comment_data, true );
if ( $comment_id && ! is_wp_error( $comment_id ) ) {
add_post_meta( $donation_id, '_donor_comment', $comment_id );
$comments[] = $comment_id;
}
}//end foreach
return $comments;
}
// This should be an array of donation IDs where a comment was made but it's missing.
$missing_comments = array(
123,
234
);
foreach ( $missing_comments as $donation_id ) {
ed_add_missing_comment( $donation_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment