PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.3
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 1 year ago BackgroundProcess.php 1 year ago BackgroundServiceProvider.php 1 year ago BulkActionService.php 1 year ago QueueService.php 1 year ago
BulkActionService.php
281 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 // delete any succeeded bulk actions.
58 $this->deleteCompletedBulkActions();
59 }
60
61 /**
62 * Get the active bulk actions.
63 *
64 * @return array
65 */
66 protected function getBulkActionsToCheck() {
67 $actions = [];
68 foreach ( $_COOKIE as $key => $value ) {
69 if ( 0 === strpos( $key, $this->cookie_prefix ) ) {
70 $actions[] = sanitize_text_field( $value );
71 }
72 }
73 return $actions;
74 }
75
76 /**
77 * Do we have processing bulk actions?
78 *
79 * @return boolean
80 */
81 protected function hasBulkActionsToCheck() {
82 return ! empty( $this->getBulkActionsToCheck() );
83 }
84
85 /**
86 * Check if the model has a pending action.
87 *
88 * @param \SureCart\Models\Model $model The model.
89 * @param string $name The action name.
90 *
91 * @return boolean
92 */
93 public function modelHasPendingAction( $model, $name = 'delete_products' ) {
94 $pending_record_ids = $this->getRecordIds( $name, 'pending' );
95 $processing_record_ids = $this->getRecordIds( $name, 'processing' );
96
97 if ( empty( $model->id ) ) {
98 return false;
99 }
100
101 return in_array( $model->id, $pending_record_ids ) || in_array( $model->id, $processing_record_ids );
102 }
103
104 /**
105 * Group the bulk actions data by action type and status.
106 *
107 * @return array
108 */
109 public function groupedBulkActions() {
110 $sorted_bulk_actions = [];
111 if ( ! empty( $this->bulk_actions ) ) {
112 foreach ( $this->bulk_actions as $bulk_action ) {
113 foreach ( $this->statuses as $status ) {
114 if ( ! isset( $sorted_bulk_actions[ $bulk_action->action_type ][ $status . '_record_ids' ] ) ) {
115 $sorted_bulk_actions[ $bulk_action->action_type ][ $status . '_record_ids' ] = array();
116 }
117 if ( ! isset( $sorted_bulk_actions[ $bulk_action->action_type ][ $status . '_bulk_actions' ] ) ) {
118 $sorted_bulk_actions[ $bulk_action->action_type ][ $status . '_bulk_actions' ] = array();
119 }
120 }
121 if ( ! is_wp_error( $bulk_action ) ) {
122 $sorted_bulk_actions[ $bulk_action->action_type ][ $bulk_action->status ][] = $bulk_action;
123 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.
124 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.
125 }
126 }
127 }
128 return $sorted_bulk_actions;
129 }
130
131 /**
132 * Get the completed bulk actions.
133 *
134 * @return array
135 */
136 public function getCompletedBulkActions() {
137 return array_filter(
138 $this->bulk_actions,
139 function ( $action ) {
140 return in_array( $action->status, [ 'pending', 'processing' ] );
141 }
142 );
143 }
144
145 /**
146 * Delete succeeded bulk actions from the cookie.
147 *
148 * @return void
149 */
150 public function deleteCompletedBulkActions() {
151 // get any bulk actions that are processing.
152 $processing = array_filter(
153 $this->bulk_actions,
154 function ( $action ) {
155 return in_array( $action->status, [ 'pending', 'processing' ] );
156 }
157 );
158
159 $completed = false;
160
161 // for each of our cookies, if the bulk action is not processing, delete the cookie.
162 foreach ( $_COOKIE as $key => $value ) {
163 if ( 0 === strpos( $key, $this->cookie_prefix ) ) {
164 if ( ! in_array( $value, array_column( $processing, 'id' ) ) ) {
165 $completed = true;
166 setcookie(
167 $key,
168 '',
169 time() - DAY_IN_SECONDS,
170 COOKIEPATH,
171 COOKIE_DOMAIN,
172 is_ssl(),
173 true
174 );
175 }
176 }
177 }
178
179 if ( ! $completed ) {
180 return;
181 }
182
183 // make sure to clear cache since these are now deleted.
184 \SureCart::account()->clearCache();
185 }
186
187 /**
188 * Show the bulk action admin notice.
189 *
190 * @param string $action_type The action type.
191 *
192 * @return void
193 */
194 public function showBulkActionAdminNotice( $action_type = null ) {
195 if ( empty( $action_type ) ) {
196 return;
197 }
198
199 $actions = $this->groupedBulkActions();
200
201 $status_parts = [];
202 foreach ( $this->statuses as $status ) {
203 $count = count( $actions[ $action_type ][ $status . '_record_ids' ] ?? [] );
204 if ( $count > 0 ) {
205 // translators: %1$d is Count of specific deletions, %2$s is bulk deletion progress status.
206 $status_parts[] = sprintf( esc_html__( '%1$d %2$s', 'surecart' ), $count, $status );
207 }
208 }
209
210 if ( ! empty( $status_parts ) ) {
211 $status_summary = esc_html__( 'Bulk Action Summary:', 'surecart' ) . ' ' . implode( ', ', $status_parts ) . '.';
212 echo wp_kses_post(
213 \SureCart::notices()->render(
214 [
215 'type' => 'info',
216 'title' => esc_html__( 'SureCart bulk action progress status.', 'surecart' ),
217 'text' => '<p>' . $status_summary . '</p>',
218 ]
219 )
220 );
221 }
222 }
223
224 /**
225 * Create a bulk action.
226 *
227 * This creates a bulk action and stores the id in
228 * a cookie so it can be checked later.
229 *
230 * @param string $action_type The action type.
231 * @param array $record_ids The record ids.
232 *
233 * @return void
234 */
235 public function createBulkAction( $action_type, $record_ids ) {
236 $bulk_action = BulkAction::create(
237 [
238 'action_type' => $action_type,
239 'record_ids' => $record_ids,
240 ]
241 );
242
243 if ( is_wp_error( $bulk_action ) ) {
244 return $bulk_action;
245 }
246
247 setcookie(
248 $this->cookie_prefix . '_' . $bulk_action->id,
249 $bulk_action->id,
250 time() + DAY_IN_SECONDS,
251 COOKIEPATH,
252 COOKIE_DOMAIN,
253 is_ssl(),
254 true
255 );
256
257 return $bulk_action;
258 }
259
260 /**
261 * Get the record ids for a specific action type and status.
262 *
263 * @param string $action_type The action type.
264 * @param string $status The status.
265 *
266 * @return array
267 */
268 public function getRecordIds( $action_type, $status ) {
269 $actions = $this->groupedBulkActions();
270
271 if ( empty( $action_type ) || empty( $actions[ $action_type ] ) ) {
272 return [];
273 }
274
275 $action_data = $actions[ $action_type ] ?? [];
276 $record_ids = $action_data[ $status . '_record_ids' ] ?? [];
277
278 return $record_ids;
279 }
280 }
281