-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat): add PronamicIdealCleanupCommand
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
htdocs/wp-content/themes/hollandskroon/src/Commands/PronamicIdealCleanupCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
htdocs/wp-content/themes/hollandskroon/src/Providers/CommandServiceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |