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

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