| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Utils; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Bulk_Operations_Result { |
| 10 |
|
| 11 |
private array $rows = []; |
| 12 |
|
| 13 |
public function add_success( int $index, string $action, array $extra = [] ): void { |
| 14 |
$this->rows[ $index ] = array_merge( |
| 15 |
[ |
| 16 |
'index' => $index, |
| 17 |
'action' => $action, |
| 18 |
'status' => 'ok', |
| 19 |
], |
| 20 |
$extra |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
public function add_error( int $index, string $action, string $code, string $message ): void { |
| 25 |
$this->rows[ $index ] = [ |
| 26 |
'index' => $index, |
| 27 |
'action' => $action, |
| 28 |
'status' => 'error', |
| 29 |
'code' => $code, |
| 30 |
'message' => $message, |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
public function has( int $index ): bool { |
| 35 |
return isset( $this->rows[ $index ] ); |
| 36 |
} |
| 37 |
|
| 38 |
public function to_array(): array { |
| 39 |
ksort( $this->rows ); |
| 40 |
|
| 41 |
$results = array_values( $this->rows ); |
| 42 |
$ok_count = 0; |
| 43 |
$error_count = 0; |
| 44 |
|
| 45 |
foreach ( $results as $result ) { |
| 46 |
if ( 'ok' === ( $result['status'] ?? '' ) ) { |
| 47 |
++$ok_count; |
| 48 |
} else { |
| 49 |
++$error_count; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
$status = 'ok'; |
| 54 |
if ( $ok_count > 0 && $error_count > 0 ) { |
| 55 |
$status = 'partial_error'; |
| 56 |
} elseif ( 0 === $ok_count && $error_count > 0 ) { |
| 57 |
$status = 'error'; |
| 58 |
} |
| 59 |
|
| 60 |
return [ |
| 61 |
'status' => $status, |
| 62 |
'results' => $results, |
| 63 |
]; |
| 64 |
} |
| 65 |
} |
| 66 |
|