| @@ -8,9 +8,9 @@ | ||
| 8 | 8 | if ( $group_id === 0 ) { |
| 9 | 9 | $groups = Red_Group::get_filtered( array() ); |
| 10 | 10 | |
| 11 | 11 | if ( count( $groups['items'] ) > 0 ) { |
| 12 | - return $groups['items'][ 0 ]['id']; | |
| 12 | + return $groups['items'][0]['id']; | |
| 13 | 13 | } |
| 14 | 14 | } else { |
| 15 | 15 | $groups = Red_Group::get( $group_id ); |
| 16 | 16 | if ( $groups ) { |
| @@ -21,8 +21,91 @@ | ||
| 21 | 21 | return false; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 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 | + /** | |
| 25 | 108 | * Import redirections from a JSON, CSV, or .htaccess file |
| 26 | 109 | * |
| 27 | 110 | * ## OPTIONS |
| 28 | 111 | * |
| @@ -33,13 +116,13 @@ | ||
| 33 | 116 | * : The group ID to import into. Defaults to the first available group. JSON |
| 34 | 117 | * contains it's own group |
| 35 | 118 | * |
| 36 | 119 | * [--format=<importformat>] |
| 37 | - * : The import format - csv, htaccess, or json. Defaults to json | |
| 120 | + * : The import format - csv, apache, or json. Defaults to json | |
| 38 | 121 | * |
| 39 | 122 | * ## EXAMPLES |
| 40 | 123 | * |
| 41 | - * wp redirection import .htaccess --format=htaccess | |
| 124 | + * wp redirection import .htaccess --format=apache | |
| 42 | 125 | */ |
| 43 | 126 | public function import( $args, $extra ) { |
| 44 | 127 | $format = isset( $extra['format'] ) ? $extra['format'] : 'json'; |
| 45 | 128 | $group = $this->get_group( isset( $extra['group'] ) ? intval( $extra['group'], 10 ) : 0 ); |
| @@ -51,9 +134,9 @@ | ||
| 51 | 134 | |
| 52 | 135 | $importer = Red_FileIO::create( $format ); |
| 53 | 136 | |
| 54 | 137 | if ( ! $importer ) { |
| 55 | - WP_CLI::error( 'Invalid import format - csv, json, or htaccess supported' ); | |
| 138 | + WP_CLI::error( 'Invalid import format - csv, json, or apache supported' ); | |
| 56 | 139 | return; |
| 57 | 140 | } |
| 58 | 141 | |
| 59 | 142 | if ( $format === 'csv' ) { |
| @@ -99,9 +182,9 @@ | ||
| 99 | 182 | $format = isset( $extra['format'] ) ? $extra['format'] : 'json'; |
| 100 | 183 | $exporter = Red_FileIO::create( $format ); |
| 101 | 184 | |
| 102 | 185 | if ( ! $exporter ) { |
| 103 | - WP_CLI::error( 'Invalid export format - json, csv, htaccess, or nginx supported' ); | |
| 186 | + WP_CLI::error( 'Invalid export format - json, csv, apache, or nginx supported' ); | |
| 104 | 187 | return; |
| 105 | 188 | } |
| 106 | 189 | |
| 107 | 190 | $file = fopen( $args[1] === '-' ? 'php://stdout' : $args[1], 'w' ); |
| @@ -108,8 +191,9 @@ | ||
| 108 | 191 | if ( $file ) { |
| 109 | 192 | $export = Red_FileIO::export( $args[0], $format ); |
| 110 | 193 | |
| 111 | 194 | if ( $export === false ) { |
| 195 | + // phpcs:ignore | |
| 112 | 196 | WP_CLI::error( 'Invalid module - must be wordpress, apache, nginx, or all' ); |
| 113 | 197 | return; |
| 114 | 198 | } |
| 115 | 199 | |
| @@ -129,8 +213,11 @@ | ||
| 129 | 213 | * |
| 130 | 214 | * <action> |
| 131 | 215 | * : The database action to perform: install, remove, upgrade |
| 132 | 216 | * |
| 217 | + * [--skip-errors] | |
| 218 | + * : Skip errors and keep on upgrading | |
| 219 | + * | |
| 133 | 220 | * ## EXAMPLES |
| 134 | 221 | * |
| 135 | 222 | * wp redirection database install |
| 136 | 223 | */ |
| @@ -135,8 +222,9 @@ | ||
| 135 | 222 | * wp redirection database install |
| 136 | 223 | */ |
| 137 | 224 | public function database( $args, $extra ) { |
| 138 | 225 | $action = false; |
| 226 | + $skip = isset( $extra['skip-errors'] ) ? true : false; | |
| 139 | 227 | |
| 140 | 228 | if ( count( $args ) === 0 || ! in_array( $args[0], array( 'install', 'remove', 'upgrade' ), true ) ) { |
| 141 | 229 | WP_CLI::error( 'Invalid database action - please use install, remove, or upgrade' ); |
| 142 | 230 | return; |
| @@ -151,8 +239,12 @@ | ||
| 151 | 239 | } ); |
| 152 | 240 | |
| 153 | 241 | WP_CLI::success( 'Database install finished' ); |
| 154 | 242 | } elseif ( $args[0] === 'upgrade' ) { |
| 243 | + global $wpdb; | |
| 244 | + | |
| 245 | + $wpdb->show_errors( false ); | |
| 246 | + | |
| 155 | 247 | Red_Database::apply_to_sites( function() { |
| 156 | 248 | $database = new Red_Database(); |
| 157 | 249 | $status = new Red_Database_Status(); |
| 158 | 250 | |
| @@ -170,11 +262,16 @@ | ||
| 170 | 262 | if ( ! $info['inProgress'] ) { |
| 171 | 263 | break; |
| 172 | 264 | } |
| 173 | 265 | |
| 174 | - if ( $info['status'] === 'error' ) { | |
| 175 | - WP_CLI::error( 'Site ' . get_current_blog_id() . ' database failed to upgrade: ' . $info['reason'] ); | |
| 176 | - return; | |
| 266 | + if ( $info['result'] === 'error' ) { | |
| 267 | + if ( $skip === false ) { | |
| 268 | + WP_CLI::error( 'Site ' . get_current_blog_id() . ' database failed to upgrade: ' . $info['reason'] . ' - ' . $info['debug'][0] ); | |
| 269 | + return; | |
| 270 | + } | |
| 271 | + | |
| 272 | + WP_CLI::warning( 'Site ' . get_current_blog_id() . ' database failed to upgrade: ' . $info['reason'] . ' - ' . $info['debug'][0] ); | |
| 273 | + $status->set_next_stage(); | |
| 177 | 274 | } |
| 178 | 275 | |
| 179 | 276 | $loop++; |
| 180 | 277 | } |
| @@ -194,11 +291,11 @@ | ||
| 194 | 291 | } |
| 195 | 292 | } |
| 196 | 293 | |
| 197 | 294 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 198 | - WP_CLI::add_command( 'redirection import', array( 'Redirection_Cli', 'import' ) ); | |
| 199 | - WP_CLI::add_command( 'redirection export', array( 'Redirection_Cli', 'export' ) ); | |
| 200 | - WP_CLI::add_command( 'redirection database', array( 'Redirection_Cli', 'database' ) ); | |
| 295 | + | |
| 296 | + // Register "redirection" as top-level command, and all public methods as sub-commands | |
| 297 | + WP_CLI::add_command( 'redirection', 'Redirection_Cli' ); | |
| 201 | 298 | |
| 202 | 299 | add_action( Red_Flusher::DELETE_HOOK, function() { |
| 203 | 300 | $flusher = new Red_Flusher(); |
| 204 | 301 | $flusher->flush(); |