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