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

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