PluginProbe
Redirection / 4.7.2
Redirection v4.7.2
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 4.7.2, at redirection-cli.php

253 lines 6.1 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 * Get or set a Redirection setting
26 *
27 * ## OPTIONS
28 *
29 * <name>
30 * : The setting name to get or set
31 *
32 * [--set=<value>]
33 * : The value to set
34 *
35 * ## EXAMPLES
36 *
37 * wp redirection setting name <value>
38 */
39 public function setting( $args, $extra ) {
40 $name = $args[0];
41 $set = isset( $extra['set'] ) ? $extra['set'] : false;
42
43 $options = red_get_options();
44
45 if ( ! isset( $options[ $name ] ) ) {
46 WP_CLI::error( 'Unsupported setting: ' . $name );
47 return;
48 }
49
50 $value = $options[ $name ];
51
52 if ( $set ) {
53 $decoded = json_decode( $set, true );
54 if ( ! $decoded ) {
55 $decoded = $set;
56 }
57
58 $options = [];
59 $options[ $name ] = $decoded;
60
61 $options = red_set_options( $options );
62 $value = $options[ $name ];
63 }
64
65 WP_CLI::success( is_array( $value ) ? wp_json_encode( $value ) : $value );
66 }
67
68 /**
69 * Import redirections from a JSON, CSV, or .htaccess file
70 *
71 * ## OPTIONS
72 *
73 * <file>
74 * : The name of the file to import.
75 *
76 * [--group=<groupid>]
77 * : The group ID to import into. Defaults to the first available group. JSON
78 * contains it's own group
79 *
80 * [--format=<importformat>]
81 * : The import format - csv, apache, or json. Defaults to json
82 *
83 * ## EXAMPLES
84 *
85 * wp redirection import .htaccess --format=apache
86 */
87 public function import( $args, $extra ) {
88 $format = isset( $extra['format'] ) ? $extra['format'] : 'json';
89 $group = $this->get_group( isset( $extra['group'] ) ? intval( $extra['group'], 10 ) : 0 );
90
91 if ( ! $group ) {
92 WP_CLI::error( 'Invalid group' );
93 return;
94 }
95
96 $importer = Red_FileIO::create( $format );
97
98 if ( ! $importer ) {
99 WP_CLI::error( 'Invalid import format - csv, json, or apache supported' );
100 return;
101 }
102
103 if ( $format === 'csv' ) {
104 $file = fopen( $args[0], 'r' );
105
106 if ( $file ) {
107 $count = $importer->load( $group, $args[0], '' );
108 WP_CLI::success( 'Imported ' . $count . ' as ' . $format );
109 } else {
110 WP_CLI::error( 'Invalid import file' );
111 }
112 } else {
113 $data = @file_get_contents( $args[0] );
114
115 if ( $data ) {
116 $count = $importer->load( $group, $args[0], $data );
117 WP_CLI::success( 'Imported ' . $count . ' redirects as ' . $format );
118 } else {
119 WP_CLI::error( 'Invalid import file' );
120 }
121 }
122 }
123
124 /**
125 * Export redirections to a CSV, JSON, .htaccess, or rewrite.rules file
126 *
127 * ## OPTIONS
128 *
129 * <module>
130 * : The module to export - wordpress, apache, nginx, or all
131 *
132 * <filename>
133 * : The file to export to, or - for stdout
134 *
135 * [--format=<exportformat>]
136 * : The export format. One of json, csv, apache, or nginx. Defaults to json
137 *
138 * ## EXAMPLES
139 *
140 * wp redirection export wordpress --format=apache
141 */
142 public function export( $args, $extra ) {
143 $format = isset( $extra['format'] ) ? $extra['format'] : 'json';
144 $exporter = Red_FileIO::create( $format );
145
146 if ( ! $exporter ) {
147 WP_CLI::error( 'Invalid export format - json, csv, apache, or nginx supported' );
148 return;
149 }
150
151 $file = fopen( $args[1] === '-' ? 'php://stdout' : $args[1], 'w' );
152 if ( $file ) {
153 $export = Red_FileIO::export( $args[0], $format );
154
155 if ( $export === false ) {
156 // phpcs:ignore
157 WP_CLI::error( 'Invalid module - must be wordpress, apache, nginx, or all' );
158 return;
159 }
160
161 fwrite( $file, $export['data'] );
162 fclose( $file );
163
164 WP_CLI::success( 'Exported ' . $export['total'] . ' to ' . $format );
165 } else {
166 WP_CLI::error( 'Invalid output file' );
167 }
168 }
169
170 /**
171 * Perform Redirection database actions
172 *
173 * ## OPTIONS
174 *
175 * <action>
176 * : The database action to perform: install, remove, upgrade
177 *
178 * ## EXAMPLES
179 *
180 * wp redirection database install
181 */
182 public function database( $args, $extra ) {
183 $action = false;
184
185 if ( count( $args ) === 0 || ! in_array( $args[0], array( 'install', 'remove', 'upgrade' ), true ) ) {
186 WP_CLI::error( 'Invalid database action - please use install, remove, or upgrade' );
187 return;
188 }
189
190 if ( $args[0] === 'install' ) {
191 Red_Database::apply_to_sites( function() {
192 $latest = Red_Database::get_latest_database();
193 $latest->install();
194
195 WP_CLI::success( 'Site ' . get_current_blog_id() . ' database is installed' );
196 } );
197
198 WP_CLI::success( 'Database install finished' );
199 } elseif ( $args[0] === 'upgrade' ) {
200 Red_Database::apply_to_sites( function() {
201 $database = new Red_Database();
202 $status = new Red_Database_Status();
203
204 if ( ! $status->needs_updating() ) {
205 WP_CLI::success( 'Site ' . get_current_blog_id() . ' database is already the latest version' );
206 return;
207 }
208
209 $loop = 0;
210
211 while ( $loop < 50 ) {
212 $result = $database->apply_upgrade( $status );
213 $info = $status->get_json();
214
215 if ( ! $info['inProgress'] ) {
216 break;
217 }
218
219 if ( $info['status'] === 'error' ) {
220 WP_CLI::error( 'Site ' . get_current_blog_id() . ' database failed to upgrade: ' . $info['reason'] );
221 return;
222 }
223
224 $loop++;
225 }
226
227 WP_CLI::success( 'Site ' . get_current_blog_id() . ' database upgraded' );
228 } );
229
230 WP_CLI::success( 'Database upgrade finished' );
231 } elseif ( $args[0] === 'remove' ) {
232 Red_Database::apply_to_sites( function() {
233 $latest = Red_Database::get_latest_database();
234 $latest->remove();
235 } );
236
237 WP_CLI::success( 'Database removed' );
238 }
239 }
240 }
241
242 if ( defined( 'WP_CLI' ) && WP_CLI ) {
243 WP_CLI::add_command( 'redirection import', [ 'Redirection_Cli', 'import' ] );
244 WP_CLI::add_command( 'redirection export', [ 'Redirection_Cli', 'export' ] );
245 WP_CLI::add_command( 'redirection database', [ 'Redirection_Cli', 'database' ] );
246 WP_CLI::add_command( 'redirection setting', [ 'Redirection_Cli', 'setting' ] );
247
248 add_action( Red_Flusher::DELETE_HOOK, function() {
249 $flusher = new Red_Flusher();
250 $flusher->flush();
251 } );
252 }
253