PluginProbe
Code Snippets / 3.10.1
Code Snippets v3.10.1
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / REST_API / Import / Plugins / Plugin_Importer.php

Plugin_Importer.php in Code Snippets 3.10.1, at php/REST_API/Import/Plugins/Plugin_Importer.php

217 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\REST_API\Import\Plugins;
4
5 use Code_Snippets\Model\Snippet;
6 use WP_REST_Request;
7 use WP_REST_Response;
8 use function Code_Snippets\save_snippet;
9
10 /**
11 * Base class for representing a plugin importer.
12 */
13 abstract class Plugin_Importer {
14
15 /**
16 * REST API arguments for the import endpoint.
17 *
18 * @var array
19 */
20 public const REST_ARGS = [
21 'ids' => [
22 'type' => 'array',
23 'required' => false,
24 ],
25 'network' => [
26 'type' => 'boolean',
27 'required' => false,
28 ],
29 'auto_add_tags' => [
30 'type' => 'boolean',
31 'required' => false,
32 ],
33 'tag_value' => [
34 'type' => 'string',
35 'required' => false,
36 ],
37 ];
38
39 /**
40 * Mapping of source plugin fields to Snippet object fields.
41 *
42 * @var array
43 */
44 protected const FIELD_MAPPINGS = [];
45
46 /**
47 * Get the name of the importer, used in REST API routes.
48 *
49 * @return string
50 */
51 abstract public function get_name(): string;
52
53 /**
54 * Get the human-readable title of the importer.
55 *
56 * @return string
57 */
58 abstract public function get_title(): string;
59
60 /**
61 * Retrieve snippet data from the source plugin's database table.
62 *
63 * @param array $ids_to_import Optional array of snippet IDs to import.
64 *
65 * @return array
66 */
67 abstract public function get_data( array $ids_to_import = [] ): array;
68
69 /**
70 * Create a Snippet object from the source plugin's snippet data.
71 *
72 * @param array $snippet_data Source plugin's snippet data.
73 * @param bool $multisite Whether to create a multisite snippet.
74 *
75 * @return Snippet|null
76 */
77 /**
78 * Create a new snippet from the provided snippet data.
79 *
80 * @param array $snippet_data Snippet data.
81 * @param bool $multisite Whether to create a network-wide snippet.
82 *
83 * @return Snippet|null
84 */
85 public function create_snippet( array $snippet_data, bool $multisite ): ?Snippet {
86 $snippet = new Snippet();
87 $snippet->network = $multisite;
88
89 foreach ( static::FIELD_MAPPINGS as $source_field => $target_field ) {
90 if ( ! isset( $snippet_data[ $source_field ] ) ) {
91 continue;
92 }
93
94 $value = $this->transform_field_value(
95 $target_field,
96 $snippet_data[ $source_field ],
97 $snippet_data
98 );
99
100 $scope_not_supported = 'scope' === $target_field && null === $value;
101 if ( $scope_not_supported ) {
102 return null;
103 }
104
105 $snippet->set_field( $target_field, $value );
106 }
107
108 return $snippet;
109 }
110
111 /**
112 * Transform field value based on the target field.
113 *
114 * @param string $target_field Target field name.
115 * @param mixed $value Original value.
116 * @param array $snippet_data Snippet data.
117 *
118 * @return mixed|null
119 */
120 abstract protected function transform_field_value( string $target_field, $value, array $snippet_data );
121
122 /**
123 * Check if the source plugin is active.
124 *
125 * @return bool
126 */
127 abstract public static function is_active(): bool;
128
129 /**
130 * Transform raw snippet data into Snippet objects.
131 *
132 * @param array $data Raw snippet data.
133 * @param bool $multisite Whether to create multisite snippets.
134 * @param bool $auto_add_tags Whether to automatically add tags.
135 * @param string $tag_value The tag value to add if auto_add_tags is true.
136 *
137 * @return array
138 */
139 public function transform( array $data, bool $multisite, bool $auto_add_tags = false, string $tag_value = '' ): array {
140 $snippets = [];
141
142 foreach ( $data as $snippet_item ) {
143 if ( ! is_array( $snippet_item ) ) {
144 continue;
145 }
146
147 $snippet = $this->create_snippet( $snippet_item, $multisite );
148
149 if ( $snippet ) {
150 if ( $auto_add_tags && ! empty( $tag_value ) ) {
151 if ( ! empty( $snippet->tags ) ) {
152 $snippet->add_tag( $tag_value );
153 } else {
154 $snippet->tags = [ $tag_value ];
155 }
156 }
157
158 $snippets[] = $snippet;
159 }
160 }
161
162 return $snippets;
163 }
164
165 /**
166 * Import snippets via REST API.
167 *
168 * @param WP_REST_Request $request The REST request.
169 *
170 * @return WP_REST_Response
171 */
172 public function import( WP_REST_Request $request ): WP_REST_Response {
173 $ids_to_import = $request->get_param( 'ids' ) ?? [];
174 $multisite = $request->get_param( 'network' ) ?? false;
175 $auto_add_tags = $request->get_param( 'auto_add_tags' ) ?? false;
176 $tag_value = $request->get_param( 'tag_value' ) ?? '';
177
178 $data = $this->get_data( $ids_to_import );
179 $snippets = $this->transform( $data, $multisite, $auto_add_tags, $tag_value );
180 $imported = $this->save_snippets( $snippets );
181
182 return rest_ensure_response( [ 'imported' => $imported ] );
183 }
184
185 /**
186 * Retrieve all snippet data via REST API.
187 *
188 * @return array
189 */
190 public function get_items(): array {
191 return $this->get_data();
192 }
193
194 /**
195 * Save snippets to the database.
196 *
197 * @param array $snippets Array of Snippet objects.
198 *
199 * @return array
200 */
201 protected function save_snippets( array $snippets ): array {
202 $imported = [];
203
204 foreach ( $snippets as $snippet ) {
205 $saved_snippet = save_snippet( $snippet );
206
207 $snippet_id = $saved_snippet->id;
208
209 if ( $snippet_id ) {
210 $imported[] = $snippet_id;
211 }
212 }
213
214 return $imported;
215 }
216 }
217