1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
<?php
namespace SIW\Batch;
class Delete_Workcamps extends Job {
const MAX_AGE_WORKCAMP = 6;
protected $action = 'delete_workcamps';
protected $name = 'verwijderen groepsprojecten';
protected $category = 'groepsprojecten';
protected function select_data() {
$limit = date( 'Y-m-d', time() - ( self::MAX_AGE_WORKCAMP * MONTH_IN_SECONDS ) );
$args = [
'return' => 'ids',
'limit' => -1,
'max_start_date' => $limit
];
$products = wc_get_products( $args );
if ( empty( $products ) ) {
return false;
}
return $products;
}
protected function task( $product_id ) {
$product = wc_get_product( $product_id );
if ( false == $product ) {
return false;
}
$project_images = get_posts([
'post_type' => 'attachment',
'post_status' => 'inherit',
'fields' => 'ids',
'meta_query' => [
[
'key' => 'plato_project_id',
'value' => $product->get_meta('project_id'),
'compare' => '='
],
],
]);
foreach ( $project_images as $project_image ) {
wp_delete_attachment( $project_image, true );
}
$variations = $product->get_children();
foreach ( $variations as $variation_id ) {
$variation = wc_get_product( $variation_id );
if ( false == $variation ) {
continue;
}
$variation->delete( true );
}
$product->delete( true );
$this->increment_processed_count();
return false;
}
}