Skip to content

Commit

Permalink
(feat): add PronamicIdealCleanupCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
hnccox committed Jan 3, 2025
1 parent 5f91231 commit 089f020
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace HK\Commands;

use \WP_CLI;
use \WP_CLI\ExitException;

class PronamicIdealCleanupCommand
{
/**
* @throws \JsonException
* @throws ExitException
*/
public function __invoke($args, $assoc_args)
{
$paged = 1;
$postsPerPage = 100;

do {
// Get fulfilled payments older than 5 days in batches of 100
$payments = new \WP_Query([
'post_type' => 'pronamic_payment',
'post_status' => 'payment_completed',
'posts_per_page' => $postsPerPage,
'paged' => $paged,
'date_query' => [
'before' => '5 days ago',
],
]);

if (!$payments->have_posts()) {
WP_CLI::success('No fulfilled payments found to clean up.');
}

foreach ($payments->posts as $payment) {
if (!wp_delete_post($payment->ID, true)) {
WP_CLI::warning('Failed to delete post with ID: ' . $payment->ID);
}
}

$paged++;
} while ($payments->have_posts());

WP_CLI::success('Pronamic fulfilled payments cleaned up successfully.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace HK\Providers;

use HK\Commands\PronamicIdealCleanupCommand;

class CommandServiceProvider
{
/**
* @throws \Exception
*/
public function boot()
{
if (defined('WP_CLI') && WP_CLI) {
\WP_CLI::add_command('pronamic:cleanup', PronamicIdealCleanupCommand::class);
}
}
}

0 comments on commit 089f020

Please sign in to comment.