1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspressoBatchRequest\JobHandlers; |
4
|
|
|
|
5
|
|
|
use EEM_Event; |
6
|
|
|
use EEM_Price; |
7
|
|
|
use EEM_Ticket; |
8
|
|
|
use EventEspresso\core\exceptions\InvalidClassException; |
9
|
|
|
use EventEspresso\core\services\orm\tree_traversal\ModelObjNode; |
10
|
|
|
use EventEspressoBatchRequest\Helpers\BatchRequestException; |
11
|
|
|
use EventEspressoBatchRequest\Helpers\JobParameters; |
12
|
|
|
use EventEspressoBatchRequest\Helpers\JobStepResponse; |
13
|
|
|
use EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandler; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class EventDeletion |
17
|
|
|
* |
18
|
|
|
* Given a list of event IDs, identified all the dependent model objects that would need to be deleted in order to not |
19
|
|
|
* leave any orphaned data. |
20
|
|
|
* |
21
|
|
|
* @package Event Espresso |
22
|
|
|
* @author Mike Nelson |
23
|
|
|
* @since $VID:$ |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
class PreviewEventDeletion extends JobHandler |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @param JobParameters $job_parameters |
33
|
|
|
* @throws BatchRequestException |
34
|
|
|
* @return JobStepResponse |
35
|
|
|
*/ |
36
|
|
|
public function create_job(JobParameters $job_parameters) |
37
|
|
|
{ |
38
|
|
|
// Set the "root" model objects we will want to delete (record their ID and model) |
39
|
|
|
$event_ids = $job_parameters->request_datum('EVT_IDs', array()); |
40
|
|
|
// Find all the root nodes to delete (this isn't just events, because there's other data, like related tickets, |
41
|
|
|
// prices, message templates, etc, whose model definition doesn't make them dependent on events. But, |
42
|
|
|
// we have no UI to access them independent of events, so they may as well get deleted too.) |
43
|
|
|
$model_objects_to_delete = []; |
44
|
|
|
foreach ($event_ids as $event_id) { |
|
|
|
|
45
|
|
|
$event = EEM_Event::instance()->get_one_by_ID($event_id); |
46
|
|
|
// Also, we want to delete their related, non-global, tickets, prices and message templates |
47
|
|
|
$related_non_global_tickets = EEM_Ticket::instance()->get_all_deleted_and_undeleted( |
48
|
|
|
[ |
49
|
|
|
[ |
50
|
|
|
'TKT_is_default' => false, |
51
|
|
|
'Datetime.EVT_ID' => $event_id |
52
|
|
|
] |
53
|
|
|
] |
54
|
|
|
); |
55
|
|
|
$related_non_global_prices = EEM_Price::instance()->get_all_deleted_and_undeleted( |
56
|
|
|
[ |
57
|
|
|
[ |
58
|
|
|
'PRC_is_default' => false, |
59
|
|
|
'Ticket.Datetime.EVT_ID' => $event_id |
60
|
|
|
] |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
$model_objects_to_delete = array_merge( |
64
|
|
|
$model_objects_to_delete, |
65
|
|
|
[$event], |
66
|
|
|
$related_non_global_tickets, |
67
|
|
|
$related_non_global_prices |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
$roots = []; |
71
|
|
|
foreach ($model_objects_to_delete as $model_object) { |
72
|
|
|
$roots[] = new ModelObjNode($model_object->ID(), $model_object->get_model()); |
73
|
|
|
} |
74
|
|
|
$job_parameters->add_extra_data('roots', $roots); |
75
|
|
|
// Set an estimate of how long this will take (we're discovering as we go, so it seems impossible to give |
76
|
|
|
// an accurate count.) |
77
|
|
|
$estimated_work_per_model_obj = 100; |
78
|
|
|
$job_parameters->set_job_size(count($roots) * $estimated_work_per_model_obj); |
79
|
|
|
return new JobStepResponse( |
80
|
|
|
$job_parameters, |
81
|
|
|
esc_html__('Generating preview of data to be deleted...', 'event_espresso') |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Performs another step of the job |
87
|
|
|
* @param JobParameters $job_parameters |
88
|
|
|
* @param int $batch_size |
89
|
|
|
* @return JobStepResponse |
90
|
|
|
* @throws BatchRequestException |
91
|
|
|
*/ |
92
|
|
|
public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
93
|
|
|
{ |
94
|
|
|
// Serializing and unserializing is what really makes this drag on (eg on localhost, the ajax requests took |
95
|
|
|
// about 4 seconds when the batch size was 250, but 3 seconds when the batch size was 50. So like |
96
|
|
|
// 50% of the request is just serializing and unserializing.) So, make the batches much bigger. |
97
|
|
|
$batch_size *= 3; |
98
|
|
|
$units_processed = 0; |
99
|
|
|
foreach ($job_parameters->extra_datum('roots', array()) as $root_node) { |
|
|
|
|
100
|
|
|
if ($units_processed >= $batch_size) { |
101
|
|
|
break; |
102
|
|
|
} |
103
|
|
|
if (! $root_node instanceof ModelObjNode) { |
104
|
|
|
throw new InvalidClassException('ModelObjNode'); |
105
|
|
|
} |
106
|
|
|
if ($root_node->isComplete()) { |
107
|
|
|
continue; |
108
|
|
|
} |
109
|
|
|
$units_processed += $root_node->visit($batch_size - $units_processed); |
110
|
|
|
} |
111
|
|
|
$job_parameters->mark_processed($units_processed); |
112
|
|
|
// If the most-recently processed root node is complete, we must be all done because we're doing them |
113
|
|
|
// sequentially. |
114
|
|
|
if (isset($root_node) && $root_node instanceof ModelObjNode && $root_node->isComplete()) { |
115
|
|
|
$job_parameters->set_status(JobParameters::status_complete); |
116
|
|
|
// Show a full progress bar. |
117
|
|
|
$job_parameters->set_units_processed($job_parameters->job_size()); |
118
|
|
|
$deletion_job_code = $job_parameters->request_datum('deletion_job_code'); |
119
|
|
|
add_option('ee_deletion_' . $deletion_job_code, $job_parameters->extra_datum('roots'), null, 'no'); |
120
|
|
|
return new JobStepResponse( |
121
|
|
|
$job_parameters, |
122
|
|
|
esc_html__('Finished identifying items for deletion.', 'event_espresso'), |
123
|
|
|
[ |
124
|
|
|
'deletion_job_code' => $deletion_job_code |
125
|
|
|
] |
126
|
|
|
); |
127
|
|
|
} else { |
128
|
|
|
// Because the job size was a guess, it may have likely been provden wrong. We don't want to show more work |
129
|
|
|
// done than we originally said there would be. So adjust the estimate. |
130
|
|
|
if (($job_parameters->units_processed() / $job_parameters->job_size()) > .8) { |
131
|
|
|
$job_parameters->set_job_size($job_parameters->job_size() * 2); |
132
|
|
|
} |
133
|
|
|
return new JobStepResponse( |
134
|
|
|
$job_parameters, |
135
|
|
|
sprintf( |
136
|
|
|
esc_html__('Identified %d items for deletion.', 'event_espresso'), |
137
|
|
|
$units_processed |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Performs any clean-up logic when we know the job is completed |
145
|
|
|
* @param JobParameters $job_parameters |
146
|
|
|
* @return JobStepResponse |
147
|
|
|
* @throws BatchRequestException |
148
|
|
|
*/ |
149
|
|
|
public function cleanup_job(JobParameters $job_parameters) |
150
|
|
|
{ |
151
|
|
|
// Nothing much to do. We can't delete the option with the built tree because we may need it in a moment for the deletion |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
// End of file EventDeletion.php |
155
|
|
|
// Location: EventEspressoBatchRequest\JobHandlers/EventDeletion.php |
156
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.