PluginProbe
Redirection / 5.6.0
Redirection v5.6.0
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / redirection-cli.php

redirection-cli.php in Redirection 5.6.0, at redirection-cli.php

339 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Implements example command.
5 */
6 class Redirection_Cli extends WP_CLI_Command {
7 /**
8 * Resolve a group ID, or return the first available group.
9 *
10 * @param int $group_id Group ID, or 0 to auto-select the first group.
11 * @return int|false Group ID or false when not available.
12 */
13 private function get_group( $group_id ) {
14 if ( $group_id === 0 ) {
15 $groups = Red_Group::get_filtered( array() );
16
17 if ( count( $groups['items'] ) > 0 ) {
18 return $groups['items'][0]['id'];
19 }
20 } else {
21 $groups = Red_Group::get( $group_id );
22 if ( $groups !== false ) {
23 return $group_id;
24 }
25 }
26
27 return false;
28 }
29
30 /**
31 * Import from another plugin to Redirection.
32 *
33 * Supports:
34 * - wp-simple-redirect
35 * - seo-redirection
36 * - safe-redirect-manager
37 * - wordpress-old-slugs
38 * - rank-math
39 * - quick-redirects
40 *
41 * ## OPTIONS
42 *
43 * <name>
44 * : The plugin name to import from (see above)
45 *
46 * [--group=<groupid>]
47 * : The group ID to import into. Defaults to the first available group.
48 *
49 * ## EXAMPLES
50 *
51 * wp redirection plugin quick-redirects
52 *
53 * @param list<string> $args Positional arguments.
54 * @param array<string, mixed> $extra Associative flags.
55 * @return void
56 */
57 public function plugin( $args, $extra ) {
58 include_once __DIR__ . '/models/importer.php';
59
60 $name = $args[0];
61 $group = $this->get_group( isset( $extra['group'] ) ? intval( $extra['group'], 10 ) : 0 );
62
63 $importer = Red_Plugin_Importer::get_importer( $name );
64 if ( $importer !== false && $group !== false ) {
65 $count = $importer->import_plugin( $group );
66 WP_CLI::success( sprintf( 'Imported %d redirects from plugin %s', $count, $name ) );
67 return;
68 }
69
70 WP_CLI::error( 'Invalid plugin name' );
71 }
72
73 /**
74 * Get or set a Redirection setting
75 *
76 * ## OPTIONS
77 *
78 * <name>
79 * : The setting name to get or set
80 *
81 * [--set=<value>]
82 * : The value to set (JSON)
83 *
84 * ## EXAMPLES
85 *
86 * wp redirection setting name <value>
87 *
88 * @param list<string> $args Positional arguments.
89 * @param array<string, mixed> $extra Associative flags.
90 * @return void
91 */
92 public function setting( $args, $extra ) {
93 $name = $args[0];
94 $set = isset( $extra['set'] ) ? $extra['set'] : false;
95
96 $options = Red_Options::get();
97
98 if ( ! isset( $options[ $name ] ) ) {
99 WP_CLI::error( 'Unsupported setting: ' . $name );
100 return;
101 }
102
103 $value = $options[ $name ];
104
105 if ( $set !== false ) {
106 $decoded = json_decode( $set, true );
107 if ( $decoded === null ) {
108 $decoded = $set;
109 }
110
111 $options = [];
112 $options[ $name ] = $decoded;
113
114 $options = red_set_options( $options );
115 $value = $options[ $name ];
116 }
117
118 $encoded = is_array( $value ) ? wp_json_encode( $value ) : (string) $value;
119 WP_CLI::success( is_string( $encoded ) ? $encoded : '' );
120 }
121
122 /**
123 * Import redirections from a JSON, CSV, or .htaccess file
124 *
125 * ## OPTIONS
126 *
127 * <file>
128 * : The name of the file to import.
129 *
130 * [--group=<groupid>]
131 * : The group ID to import into. Defaults to the first available group. JSON
132 * contains it's own group
133 *
134 * [--format=<importformat>]
135 * : The import format - csv, apache, or json. Defaults to json
136 *
137 * ## EXAMPLES
138 *
139 * wp redirection import .htaccess --format=apache
140 *
141 * @param list<string> $args Positional arguments.
142 * @param array<string, mixed> $extra Associative flags.
143 * @return void
144 */
145 public function import( $args, $extra ) {
146 $format = isset( $extra['format'] ) ? $extra['format'] : 'json';
147 $group = $this->get_group( isset( $extra['group'] ) ? intval( $extra['group'], 10 ) : 0 );
148
149 if ( $group === false ) {
150 WP_CLI::error( 'Invalid group' );
151 return;
152 }
153
154 $importer = Red_FileIO::create( $format );
155
156 if ( $importer === false ) {
157 WP_CLI::error( 'Invalid import format - csv, json, or apache supported' );
158 return;
159 }
160
161 if ( $format === 'csv' ) {
162 $file = fopen( $args[0], 'r' );
163
164 if ( $file !== false ) {
165 $count = $importer->load( $group, $args[0], '' );
166
167 WP_CLI::success( 'Imported ' . $count . ' as ' . $format );
168 } else {
169 WP_CLI::error( 'Invalid import file' );
170 }
171 } else {
172 $data = @file_get_contents( $args[0] );
173
174 if ( $data !== false ) {
175 $count = $importer->load( $group, $args[0], $data );
176 WP_CLI::success( 'Imported ' . $count . ' redirects as ' . $format );
177 } else {
178 WP_CLI::error( 'Invalid import file' );
179 }
180 }
181 }
182
183 /**
184 * Export redirections to a CSV, JSON, .htaccess, or rewrite.rules file
185 *
186 * ## OPTIONS
187 *
188 * <module>
189 * : The module to export - wordpress, apache, nginx, or all
190 *
191 * <filename>
192 * : The file to export to, or - for stdout
193 *
194 * [--format=<exportformat>]
195 * : The export format. One of json, csv, apache, or nginx. Defaults to json
196 *
197 * ## EXAMPLES
198 *
199 * wp redirection export wordpress --format=apache
200 *
201 * @param list<string> $args Positional arguments.
202 * @param array<string, mixed> $extra Associative flags.
203 * @return void
204 */
205 public function export( $args, $extra ) {
206 $format = isset( $extra['format'] ) ? $extra['format'] : 'json';
207 $exporter = Red_FileIO::create( $format );
208
209 if ( $exporter === false ) {
210 WP_CLI::error( 'Invalid export format - json, csv, apache, or nginx supported' );
211 return;
212 }
213
214 $file = fopen( $args[1] === '-' ? 'php://stdout' : $args[1], 'w' );
215 if ( $file !== false ) {
216 $export = Red_FileIO::export( $args[0], $format );
217
218 if ( $export === false ) {
219 // phpcs:ignore
220 WP_CLI::error( 'Invalid module - must be wordpress, apache, nginx, or all' );
221 return;
222 }
223
224 fwrite( $file, $export['data'] );
225 fclose( $file );
226
227 WP_CLI::success( 'Exported ' . $export['total'] . ' to ' . $format );
228 } else {
229 WP_CLI::error( 'Invalid output file' );
230 }
231 }
232
233 /**
234 * Perform Redirection database actions
235 *
236 * ## OPTIONS
237 *
238 * <action>
239 * : The database action to perform: install, remove, upgrade
240 *
241 * [--skip-errors]
242 * : Skip errors and keep on upgrading
243 *
244 * ## EXAMPLES
245 *
246 * wp redirection database install
247 *
248 * @param list<string> $args Positional arguments.
249 * @param array<string, mixed> $extra Associative flags.
250 * @return void
251 */
252 public function database( $args, $extra ) {
253 $skip = isset( $extra['skip-errors'] ) ? true : false;
254
255 if ( count( $args ) === 0 || ! in_array( $args[0], array( 'install', 'remove', 'upgrade' ), true ) ) {
256 WP_CLI::error( 'Invalid database action - please use install, remove, or upgrade' );
257 return;
258 }
259
260 if ( $args[0] === 'install' ) {
261 Red_Database::apply_to_sites(
262 function () {
263 $latest = Red_Database::get_latest_database();
264 $latest->install();
265
266 WP_CLI::success( 'Site ' . get_current_blog_id() . ' database is installed' );
267 }
268 );
269
270 WP_CLI::success( 'Database install finished' );
271 } elseif ( $args[0] === 'upgrade' ) {
272 global $wpdb;
273
274 $wpdb->show_errors( false );
275
276 Red_Database::apply_to_sites(
277 function () use ( $skip ) {
278 $database = new Red_Database();
279 $status = new Red_Database_Status();
280
281 if ( ! $status->needs_updating() ) {
282 WP_CLI::success( 'Site ' . get_current_blog_id() . ' database is already the latest version' );
283 return;
284 }
285
286 $loop = 0;
287
288 while ( $loop < 50 ) {
289 $database->apply_upgrade( $status );
290 $info = $status->get_json();
291
292 if ( ! $info['inProgress'] ) {
293 break;
294 }
295
296 if ( isset( $info['result'] ) && $info['result'] === 'error' && isset( $info['reason'] ) && isset( $info['debug'] ) ) {
297 if ( $skip === false ) {
298 WP_CLI::error( 'Site ' . get_current_blog_id() . ' database failed to upgrade: ' . $info['reason'] . ' - ' . $info['debug'][0] );
299 return;
300 }
301
302 WP_CLI::warning( 'Site ' . get_current_blog_id() . ' database failed to upgrade: ' . $info['reason'] . ' - ' . $info['debug'][0] );
303 $status->set_next_stage();
304 }
305
306 $loop++;
307 }
308
309 WP_CLI::success( 'Site ' . get_current_blog_id() . ' database upgraded' );
310 }
311 );
312
313 WP_CLI::success( 'Database upgrade finished' );
314 } elseif ( $args[0] === 'remove' ) {
315 Red_Database::apply_to_sites(
316 function () {
317 $latest = Red_Database::get_latest_database();
318 $latest->remove();
319 }
320 );
321
322 WP_CLI::success( 'Database removed' );
323 }
324 }
325 }
326
327 if ( defined( 'WP_CLI' ) && WP_CLI ) {
328 // Register "redirection" as top-level command, and all public methods as sub-commands
329 WP_CLI::add_command( 'redirection', 'Redirection_Cli' );
330
331 add_action(
332 Red_Flusher::DELETE_HOOK,
333 function () {
334 $flusher = new Red_Flusher();
335 $flusher->flush();
336 }
337 );
338 }
339