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

127 lines 3.1 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 if ( defined( 'WP_CLI' ) && WP_CLI ) {
124 WP_CLI::add_command( 'redirection import', array( 'Redirection_Cli', 'import' ) );
125 WP_CLI::add_command( 'redirection export', array( 'Redirection_Cli', 'export' ) );
126 }
127