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 |