PluginProbe ʕ •ᴥ•ʔ
Search Regex / 3.4.3
Search Regex v3.4.3
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 6 days ago class-action.php 6 months ago class-dynamic-column.php 1 month ago
class-action.php
160 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 $json[] = $result->to_json();
90 }
91
92 $results['results'] = $json;
93
94 return $results;
95 }
96
97 /**
98 * Change the save mode
99 *
100 * @param bool $mode Save.
101 * @return void
102 */
103 public function set_save_mode( $mode ) {
104 $this->save = $mode;
105 }
106
107 /**
108 * Get any columns we need to view for this action.
109 *
110 * @return list<string>
111 */
112 public function get_view_columns() {
113 return [];
114 }
115
116 /**
117 * Should this action save to the database?
118 *
119 * @return bool
120 */
121 public function should_save() {
122 return $this->save;
123 }
124
125 /**
126 * Debug helper function. Logs an action.
127 *
128 * @param string $title Log title.
129 * @return void
130 */
131 protected function log_action( $title ) {
132 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
133 // @phpstan-ignore disallowed.function
134 error_log( $title ); // phpcs:ignore
135 }
136 }
137
138 /**
139 * Get the action options
140 *
141 * @param string|array<string, mixed> $options Options.
142 * @return array<string, mixed>
143 */
144 public static function get_options( $options ) {
145 if ( ! is_array( $options ) ) {
146 return [];
147 }
148
149 if ( isset( $options['action'] ) && $options['action'] === 'replace' ) {
150 return [
151 'search' => $options['searchPhrase'],
152 'replacement' => $options['replacement'],
153 'flags' => $options['searchFlags'],
154 ];
155 }
156
157 return $options['actionOption'] ?? [];
158 }
159 }
160