PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.5.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.5.0
4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / Background / BulkActionService.php
surecart / app / src / Background Last commit date
AsyncRequest.php 1 year ago AsyncWebhookService.php 9 months ago BackgroundProcess.php 7 months ago BackgroundServiceProvider.php 1 year ago BulkActionService.php 6 months ago Job.php 1 year ago QueueService.php 2 months ago
BulkActionService.php
287 lines
1 <?php
2
3 namespace SureCart\Background;
4
5 use SureCart\Models\BulkAction;
6
7 /**
8 * Handled the business logic for Bulk Actions.
9 */
10 class BulkActionService {
11 /**
12 * The cookie prefix.
13 *
14 * @var string
15 */
16 public $cookie_prefix = 'sc_bulk_action_';
17
18 /**
19 * The bulk actions data.
20 *
21 * @var array
22 */
23 public $bulk_actions_data = [];
24
25 /**
26 * The bulk actions.
27 *
28 * @var array
29 */
30 public $bulk_actions = [];
31
32 /**
33 * The bulk action statuses.
34 *
35 * @var array
36 */
37 public $statuses = array( 'succeeded', 'processing', 'pending', 'invalid', 'completed' );
38
39 /**
40 * Bootstrap any actions.
41 *
42 * @return void
43 */
44 public function bootstrap() {
45 // we will only do this in the admin or if there are bulk actions to check.
46 if ( ! $this->hasBulkActionsToCheck() || ! is_admin() ) {
47 return;
48 }
49
50 // store the bulk actions in the object.
51 $this->bulk_actions = BulkAction::where(
52 [
53 'ids' => $this->getBulkActionsToCheck(),
54 ]
55 )->get();
56
57 // If we got an error, bail early.
58 if ( is_wp_error( $this->bulk_actions ) ) {
59 $this->bulk_actions = [];
60 return;
61 }
62
63 // delete any succeeded bulk actions.
64 $this->deleteCompletedBulkActions();
65 }
66
67 /**
68 * Get the active bulk actions.
69 *
70 * @return array
71 */
72 protected function getBulkActionsToCheck() {
73 $actions = [];
74 foreach ( $_COOKIE as $key => $value ) {
75 if ( 0 === strpos( $key, $this->cookie_prefix ) ) {
76 $actions[] = sanitize_text_field( $value );
77 }
78 }
79 return $actions;
80 }
81
82 /**
83 * Do we have processing bulk actions?
84 *
85 * @return boolean
86 */
87 protected function hasBulkActionsToCheck() {
88 return ! empty( $this->getBulkActionsToCheck() );
89 }
90
91 /**
92 * Check if the model has a pending action.
93 *
94 * @param \SureCart\Models\Model $model The model.
95 * @param string $name The action name.
96 *
97 * @return boolean
98 */
99 public function modelHasPendingAction( $model, $name = 'delete_products' ) {
100 $pending_record_ids = $this->getRecordIds( $name, 'pending' );
101 $processing_record_ids = $this->getRecordIds( $name, 'processing' );
102
103 if ( empty( $model->id ) ) {
104 return false;
105 }
106
107 return in_array( $model->id, $pending_record_ids ) || in_array( $model->id, $processing_record_ids );
108 }
109
110 /**
111 * Group the bulk actions data by action type and status.
112 *
113 * @return array
114 */
115 public function groupedBulkActions() {
116 $sorted_bulk_actions = [];
117 if ( ! empty( $this->bulk_actions ) ) {
118 foreach ( $this->bulk_actions as $bulk_action ) {
119 foreach ( $this->statuses as $status ) {
120 if ( ! isset( $sorted_bulk_actions[ $bulk_action->action_type ][ $status . '_record_ids' ] ) ) {
121 $sorted_bulk_actions[ $bulk_action->action_type ][ $status . '_record_ids' ] = array();
122 }
123 if ( ! isset( $sorted_bulk_actions[ $bulk_action->action_type ][ $status . '_bulk_actions' ] ) ) {
124 $sorted_bulk_actions[ $bulk_action->action_type ][ $status . '_bulk_actions' ] = array();
125 }
126 }
127 if ( ! is_wp_error( $bulk_action ) ) {
128 $sorted_bulk_actions[ $bulk_action->action_type ][ $bulk_action->status ][] = $bulk_action;
129 array_push( $sorted_bulk_actions[ $bulk_action->action_type ][ $bulk_action->status . '_bulk_actions' ], $bulk_action->id ); // Saves the bulks actions ids for each status.
130 array_push( $sorted_bulk_actions[ $bulk_action->action_type ][ $bulk_action->status . '_record_ids' ], ...$bulk_action->record_ids ); // Saves the record ids for each status.
131 }
132 }
133 }
134 return $sorted_bulk_actions;
135 }
136
137 /**
138 * Get the completed bulk actions.
139 *
140 * @return array
141 */
142 public function getCompletedBulkActions() {
143 return array_filter(
144 $this->bulk_actions,
145 function ( $action ) {
146 return in_array( $action->status, [ 'pending', 'processing' ] );
147 }
148 );
149 }
150
151 /**
152 * Delete succeeded bulk actions from the cookie.
153 *
154 * @return void
155 */
156 public function deleteCompletedBulkActions() {
157 // get any bulk actions that are processing.
158 $processing = array_filter(
159 $this->bulk_actions,
160 function ( $action ) {
161 return in_array( $action->status, [ 'pending', 'processing' ] );
162 }
163 );
164
165 $completed = false;
166
167 // for each of our cookies, if the bulk action is not processing, delete the cookie.
168 foreach ( $_COOKIE as $key => $value ) {
169 if ( 0 === strpos( $key, $this->cookie_prefix ) ) {
170 if ( ! in_array( $value, array_column( $processing, 'id' ) ) ) {
171 $completed = true;
172 setcookie(
173 $key,
174 '',
175 time() - DAY_IN_SECONDS,
176 COOKIEPATH,
177 COOKIE_DOMAIN,
178 is_ssl(),
179 true
180 );
181 }
182 }
183 }
184
185 if ( ! $completed ) {
186 return;
187 }
188
189 // make sure to clear cache since these are now deleted.
190 \SureCart::account()->clearCache();
191 }
192
193 /**
194 * Show the bulk action admin notice.
195 *
196 * @param string $action_type The action type.
197 *
198 * @return void
199 */
200 public function showBulkActionAdminNotice( $action_type = null ) {
201 if ( empty( $action_type ) ) {
202 return;
203 }
204
205 $actions = $this->groupedBulkActions();
206
207 $status_parts = [];
208 foreach ( $this->statuses as $status ) {
209 $count = count( $actions[ $action_type ][ $status . '_record_ids' ] ?? [] );
210 if ( $count > 0 ) {
211 // translators: %1$d is Count of specific deletions, %2$s is bulk deletion progress status.
212 $status_parts[] = sprintf( esc_html__( '%1$d %2$s', 'surecart' ), $count, $status );
213 }
214 }
215
216 if ( ! empty( $status_parts ) ) {
217 $status_summary = esc_html__( 'Bulk Action Summary:', 'surecart' ) . ' ' . implode( ', ', $status_parts ) . '.';
218 echo wp_kses_post(
219 \SureCart::notices()->render(
220 [
221 'type' => 'info',
222 'title' => esc_html__( 'SureCart bulk action progress status.', 'surecart' ),
223 'text' => '<p>' . $status_summary . '</p>',
224 ]
225 )
226 );
227 }
228 }
229
230 /**
231 * Create a bulk action.
232 *
233 * This creates a bulk action and stores the id in
234 * a cookie so it can be checked later.
235 *
236 * @param string $action_type The action type.
237 * @param array $record_ids The record ids.
238 *
239 * @return void
240 */
241 public function createBulkAction( $action_type, $record_ids ) {
242 $bulk_action = BulkAction::create(
243 [
244 'action_type' => $action_type,
245 'record_ids' => $record_ids,
246 ]
247 );
248
249 if ( is_wp_error( $bulk_action ) ) {
250 return $bulk_action;
251 }
252
253 setcookie(
254 $this->cookie_prefix . '_' . $bulk_action->id,
255 $bulk_action->id,
256 time() + DAY_IN_SECONDS,
257 COOKIEPATH,
258 COOKIE_DOMAIN,
259 is_ssl(),
260 true
261 );
262
263 return $bulk_action;
264 }
265
266 /**
267 * Get the record ids for a specific action type and status.
268 *
269 * @param string $action_type The action type.
270 * @param string $status The status.
271 *
272 * @return array
273 */
274 public function getRecordIds( $action_type, $status ) {
275 $actions = $this->groupedBulkActions();
276
277 if ( empty( $action_type ) || empty( $actions[ $action_type ] ) ) {
278 return [];
279 }
280
281 $action_data = $actions[ $action_type ] ?? [];
282 $record_ids = $action_data[ $status . '_record_ids' ] ?? [];
283
284 return $record_ids;
285 }
286 }
287