Skip to content

Instantly share code, notes, and snippets.

@Matt-Welland
Created September 5, 2024 08:35
Show Gist options
  • Save Matt-Welland/025173531b84eab845f034d4f021ebfa to your computer and use it in GitHub Desktop.
Save Matt-Welland/025173531b84eab845f034d4f021ebfa to your computer and use it in GitHub Desktop.
Batch Approve WooCommerce Reviews
// Hook into 'init' to start processing reviews
add_action('init', 'schedule_review_approval');
// Function to schedule the review approval process
function schedule_review_approval() {
if (current_user_can('administrator') && !wp_next_scheduled('approve_reviews_batch')) {
// Schedule an event to run immediately, and repeat it every 30 seconds
wp_schedule_single_event(time(), 'approve_reviews_batch');
}
}
// Hook the function to handle each batch
add_action('approve_reviews_batch', 'approve_reviews_batch');
// Function to process and approve reviews in batches
function approve_reviews_batch() {
$batch_size = 50; // Number of reviews to process in each batch
$delay = 5; // Delay in seconds between batches
// Get the next batch of pending reviews
$args = array(
'status' => 'hold', // Only pending reviews
'number' => $batch_size, // Process 10 reviews at a time
'post_type' => 'product',
);
// Fetch pending reviews
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query($args);
// Approve each review in the batch
foreach ($comments as $comment) {
wp_set_comment_status($comment->comment_ID, 'approve');
}
// Check if there are more reviews to process
if (count($comments) === $batch_size) {
// If there are still more reviews, schedule the next batch
wp_schedule_single_event(time() + $delay, 'approve_reviews_batch');
} else {
// No more pending reviews, all done!
// Optionally, you can show a message or take another action here
echo "All reviews have been approved!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment