class-delete.php
6 months ago
class-export.php
1 week ago
class-global-replace.php
6 months ago
class-modify.php
6 months ago
class-nothing.php
6 months ago
class-run.php
1 week ago
class-run.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Action\Type; |
| 4 | |
| 5 | use SearchRegex\Action\Action; |
| 6 | use SearchRegex\Schema; |
| 7 | use SearchRegex\Source; |
| 8 | use SearchRegex\Search; |
| 9 | |
| 10 | class Run extends Action { |
| 11 | /** |
| 12 | * Hook name |
| 13 | * |
| 14 | * @var non-empty-string|false |
| 15 | */ |
| 16 | private $hook = false; |
| 17 | |
| 18 | /** |
| 19 | * Constructor |
| 20 | * |
| 21 | * @param array<string, mixed>|string $options Options. |
| 22 | * @param Schema\Schema $schema Schema. |
| 23 | */ |
| 24 | public function __construct( $options, Schema\Schema $schema ) { |
| 25 | if ( is_array( $options ) && isset( $options['hook'] ) && is_string( $options['hook'] ) ) { |
| 26 | $hook = preg_replace( '/[^A-Za-z0-9_-]/', '', $options['hook'] ); |
| 27 | |
| 28 | if ( $hook !== null && $hook !== '' && has_action( $hook ) ) { |
| 29 | $this->hook = $hook; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | parent::__construct( $options, $schema ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @return array<string, mixed> |
| 38 | */ |
| 39 | public function to_json() { |
| 40 | return [ |
| 41 | 'action' => 'action', |
| 42 | 'actionOption' => [ |
| 43 | 'hook' => $this->hook, |
| 44 | ], |
| 45 | ]; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @return bool |
| 50 | */ |
| 51 | public function should_save() { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param int $row_id |
| 57 | * @param array<string, mixed> $row |
| 58 | * @param Source\Source $source |
| 59 | * @param array<Search\Column> $columns |
| 60 | * @return array<Search\Column> |
| 61 | */ |
| 62 | public function perform( $row_id, array $row, Source\Source $source, array $columns ) { |
| 63 | if ( $this->hook === false || ! $this->save ) { |
| 64 | return $columns; |
| 65 | } |
| 66 | |
| 67 | do_action( $this->hook, $row, $row_id, $source, $columns ); |
| 68 | |
| 69 | return $columns; |
| 70 | } |
| 71 | } |
| 72 |