PluginProbe
Redirection / 5.3.4
Redirection v5.3.4
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.3.4, at redirection-cli.php

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