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

200 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 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 Red_Database::apply_to_sites( function() {
151 $database = new Red_Database();
152 $status = new Red_Database_Status();
153
154 if ( ! $status->needs_updating() ) {
155 WP_CLI::success( 'Database is already the latest version' );
156 return;
157 }
158
159 $loop = 0;
160
161 while ( $loop < 50 ) {
162 $result = $database->apply_upgrade( $status );
163 $info = $status->get_json();
164
165 if ( ! $info['inProgress'] ) {
166 break;
167 }
168
169 if ( $info['status'] === 'error' ) {
170 WP_CLI::error( 'Database failed to upgrade: ' . $info['reason'] );
171 return;
172 }
173
174 $loop++;
175 }
176 } );
177
178 WP_CLI::success( 'Database upgraded' );
179 } elseif ( $args[0] === 'remove' ) {
180 Red_Database::apply_to_sites( function() {
181 $latest = Red_Database::get_latest_database();
182 $latest->remove();
183 } );
184
185 WP_CLI::success( 'Database removed' );
186 }
187 }
188 }
189
190 if ( defined( 'WP_CLI' ) && WP_CLI ) {
191 WP_CLI::add_command( 'redirection import', array( 'Redirection_Cli', 'import' ) );
192 WP_CLI::add_command( 'redirection export', array( 'Redirection_Cli', 'export' ) );
193 WP_CLI::add_command( 'redirection database', array( 'Redirection_Cli', 'database' ) );
194
195 add_action( Red_Flusher::DELETE_HOOK, function() {
196 $flusher = new Red_Flusher();
197 $flusher->flush();
198 } );
199 }
200