PluginProbe ʕ •ᴥ•ʔ
Search Regex / trunk
Search Regex vtrunk
3.4.4 3.4.3 trunk 1.4.12 1.4.13 1.4.14 1.4.15 1.4.16 2.0 2.0.1 2.1 2.2 2.2.1 2.3 2.3.1 2.3.2 2.3.3 2.4 2.4.1 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1 3.1.1 3.1.2 3.2 3.3 3.3.0 3.3.1 3.4 3.4.1 3.4.2
search-regex / includes / action / class-action.php
search-regex / includes / action Last commit date
type 1 month ago class-action.php 3 days ago class-dynamic-column.php 2 months ago
class-action.php
164 lines
1 <?php
2
3 namespace SearchRegex\Action;
4
5 use SearchRegex\Action\Type;
6 use SearchRegex\Schema;
7 use SearchRegex\Source;
8 use SearchRegex\Search;
9
10 /**
11 * Perform an action on a result
12 */
13 abstract class Action {
14 /**
15 * Schema
16 */
17 protected Schema\Source $schema;
18
19 /**
20 * Should this action save data to the database?
21 */
22 protected bool $save = false;
23
24 /**
25 * Constructor
26 *
27 * @param array<string, mixed>|array<int, array<string, mixed>>|string $options Options.
28 * @param Schema\Schema $schema Schema.
29 */
30 public function __construct( $options, Schema\Schema $schema ) {
31 // Always the first one
32 $this->schema = $schema->get_sources()[0];
33 }
34
35 /**
36 * Create an action
37 *
38 * @param string $action_type Type of action.
39 * @param array<string, mixed>|string $action_options Array of action options.
40 * @param Schema\Schema $schema Schema for all sources.
41 * @return Action
42 */
43 public static function create( $action_type, $action_options, Schema\Schema $schema ) {
44 if ( $action_type === 'modify' && is_array( $action_options ) ) {
45 return new Type\Modify( $action_options, $schema );
46 } elseif ( $action_type === 'replace' ) {
47 return new Type\Global_Replace( $action_options, $schema );
48 } elseif ( $action_type === 'delete' ) {
49 return new Type\Delete( $action_options, $schema );
50 } elseif ( $action_type === 'export' ) {
51 return new Type\Export( $action_options, $schema );
52 } elseif ( $action_type === 'action' ) {
53 return new Type\Run( $action_options, $schema );
54 }
55
56 return new Type\Nothing();
57 }
58
59 /**
60 * Convert action to JSON
61 *
62 * @return array<string, mixed>
63 */
64 abstract public function to_json();
65
66 /**
67 * Perform the action
68 *
69 * @param int $row_id Row ID.
70 * @param array<string, mixed> $row Data for row.
71 * @param Source\Source $source Source.
72 * @param array<Search\Column> $columns Contexts.
73 * @return array<Search\Column>|\WP_Error
74 */
75 public function perform( $row_id, array $row, Source\Source $source, array $columns ) {
76 return $columns;
77 }
78
79 /**
80 * Get the results from the action.
81 *
82 * @param array<string, mixed> $results Results.
83 * @return array<string, mixed>
84 */
85 public function get_results( array $results ) {
86 $json = [];
87
88 foreach ( $results['results'] as $result ) {
89 // A result can be `false` when a row no longer matches the search - for example
90 // after a single-row replace has removed the last occurrence of the search phrase.
91 if ( $result !== false ) {
92 $json[] = $result->to_json();
93 }
94 }
95
96 $results['results'] = $json;
97
98 return $results;
99 }
100
101 /**
102 * Change the save mode
103 *
104 * @param bool $mode Save.
105 * @return void
106 */
107 public function set_save_mode( $mode ) {
108 $this->save = $mode;
109 }
110
111 /**
112 * Get any columns we need to view for this action.
113 *
114 * @return list<string>
115 */
116 public function get_view_columns() {
117 return [];
118 }
119
120 /**
121 * Should this action save to the database?
122 *
123 * @return bool
124 */
125 public function should_save() {
126 return $this->save;
127 }
128
129 /**
130 * Debug helper function. Logs an action.
131 *
132 * @param string $title Log title.
133 * @return void
134 */
135 protected function log_action( $title ) {
136 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
137 // @phpstan-ignore disallowed.function
138 error_log( $title ); // phpcs:ignore
139 }
140 }
141
142 /**
143 * Get the action options
144 *
145 * @param string|array<string, mixed> $options Options.
146 * @return array<string, mixed>
147 */
148 public static function get_options( $options ) {
149 if ( ! is_array( $options ) ) {
150 return [];
151 }
152
153 if ( isset( $options['action'] ) && $options['action'] === 'replace' ) {
154 return [
155 'search' => $options['searchPhrase'],
156 'replacement' => $options['replacement'],
157 'flags' => $options['searchFlags'],
158 ];
159 }
160
161 return $options['actionOption'] ?? [];
162 }
163 }
164