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

203 lines 4.9 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
71 if ( $data ) {
72 $count = $importer->load( $group, $args[0], $data );
73 WP_CLI::success( 'Imported ' . $count . ' redirects as ' . $format );
74 } else {
75 WP_CLI::error( 'Invalid import file' );
76 }
77 }
78 }
79
80 /**
81 * Export redirections to a CSV, JSON, .htaccess, or rewrite.rules file
82 *
83 * ## OPTIONS
84 *
85 * <module>
86 * : The module to export - wordpress, apache, nginx, or all
87 *
88 * <filename>
89 * : The file to export to, or - for stdout
90 *
91 * [--format=<exportformat>]
92 * : The export format. One of json, csv, apache, or nginx. Defaults to json
93 *
94 * ## EXAMPLES
95 *
96 * wp redirection export wordpress --format=apache
97 */
98 public function export( $args, $extra ) {
99 $format = isset( $extra['format'] ) ? $extra['format'] : 'json';
100 $exporter = Red_FileIO::create( $format );
101
102 if ( ! $exporter ) {
103 WP_CLI::error( 'Invalid export format - json, csv, htaccess, or nginx supported' );
104 return;
105 }
106
107 $file = fopen( $args[1] === '-' ? 'php://stdout' : $args[1], 'w' );
108 if ( $file ) {
109 $export = Red_FileIO::export( $args[0], $format );
110
111 if ( $export === false ) {
112 WP_CLI::error( 'Invalid module - must be wordpress, apache, nginx, or all' );
113 return;
114 }
115
116 fwrite( $file, $export['data'] );
117 fclose( $file );
118
119 WP_CLI::success( 'Exported ' . $export['total'] . ' to ' . $format );
120 } else {
121 WP_CLI::error( 'Invalid output file' );
122 }
123 }
124
125 /**
126 * Perform Redirection database actions
127 *
128 * ## OPTIONS
129 *
130 * <action>
131 * : The database action to perform: install, remove, upgrade
132 *
133 * ## EXAMPLES
134 *
135 * wp redirection database install
136 */
137 public function database( $args, $extra ) {
138 $action = false;
139
140 if ( count( $args ) === 0 || ! in_array( $args[0], array( 'install', 'remove', 'upgrade' ), true ) ) {
141 WP_CLI::error( 'Invalid database action - please use install, remove, or upgrade' );
142 return;
143 }
144
145 if ( $args[0] === 'install' ) {
146 Red_Database::apply_to_sites( function() {
147 $latest = Red_Database::get_latest_database();
148 $latest->install();
149 } );
150
151 WP_CLI::success( 'Database installed' );
152 } elseif ( $args[0] === 'upgrade' ) {
153 Red_Database::apply_to_sites( function() {
154 $database = new Red_Database();
155 $status = new Red_Database_Status();
156
157 if ( ! $status->needs_updating() ) {
158 WP_CLI::success( 'Database is already the latest version' );
159 return;
160 }
161
162 $loop = 0;
163
164 while ( $loop < 50 ) {
165 $result = $database->apply_upgrade( $status );
166 $info = $status->get_json();
167
168 if ( ! $info['inProgress'] ) {
169 break;
170 }
171
172 if ( $info['status'] === 'error' ) {
173 WP_CLI::error( 'Database failed to upgrade: ' . $info['reason'] );
174 return;
175 }
176
177 $loop++;
178 }
179 } );
180
181 WP_CLI::success( 'Database upgraded' );
182 } elseif ( $args[0] === 'remove' ) {
183 Red_Database::apply_to_sites( function() {
184 $latest = Red_Database::get_latest_database();
185 $latest->remove();
186 } );
187
188 WP_CLI::success( 'Database removed' );
189 }
190 }
191 }
192
193 if ( defined( 'WP_CLI' ) && WP_CLI ) {
194 WP_CLI::add_command( 'redirection import', array( 'Redirection_Cli', 'import' ) );
195 WP_CLI::add_command( 'redirection export', array( 'Redirection_Cli', 'export' ) );
196 WP_CLI::add_command( 'redirection database', array( 'Redirection_Cli', 'database' ) );
197
198 add_action( Red_Flusher::DELETE_HOOK, function() {
199 $flusher = new Red_Flusher();
200 $flusher->flush();
201 } );
202 }
203