-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-command.php
More file actions
152 lines (116 loc) · 3.31 KB
/
class-command.php
File metadata and controls
152 lines (116 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
// Only run through WP CLI.
if ( ! defined( 'WP_CLI' ) ) {
return;
}
class Revslider_Search_Replace extends WP_CLI_Command {
/**
* WP CLI Command to search replace the website URLs in the Revolution sliders
*
* ## OPTIONS
*
* <id>
* ID of the slider, also takes "all" as option where it will search accross all the sliders
*
* <source-url>
* Source URL
*
* <destination-url>
* destination URL
*
* [--network]
* Search Replace the strings in Revolution sliders throughout all the sites in multisite network
*
* ## EXAMPLES
*
* 1. wp rsr 2 <source-url> <destination-url>
* - This will search replace the strings in the slider with is "2"
* 2. wp rsr all <source-url> <destination-url>
* - This will search replace the strings on all the sliders on the site
* 3. wp rsr all <source-url> <destination-url> --network
* - This command will will search replace the strings on all sliders accross all the sites in multisite network.
*
*/
public $slider;
public function __invoke( $args, $assoc_args ) {
$network = false;
if ( ! class_exists( 'RevSliderSlider' ) ) {
WP_CLI::error( "Revolution slider is not active" );
return false;
}
$default = array(
0 => '',
1 => '',
2 => '',
);
if ( ! isset( $args[0] ) ) {
$args[0] = $default[0];
}
if ( ! isset( $args[1] ) ) {
$args[1] = $default[1];
}
if ( ! isset( $args[2] ) ) {
$args[2] = $default[2];
}
$id = $args[0];
$source = $args[1];
$destination = $args[2];
if ( isset( $assoc_args['network'] ) && $assoc_args['network'] == true && is_multisite() ) {
$network = true;
}
if ( $id == "" ) {
WP_CLI::error( "Plese enter ID of the slider which you want to search-replace into or 'all' to select all the sliders" );
return false;
}
if ( $source == "" ) {
WP_CLI::error( "Please enter source URL" );
return false;
}
if ( $destination == "" ) {
WP_CLI::error( "Please enter destination URL" );
return false;
}
$data = array(
'url_from' => $source,
'url_to' => $destination
);
$this->slider = new RevSliderSlider();
if ( $network == true ) {
if ( function_exists( 'get_sites' ) ) {
$blogs = get_sites();
} else {
$blogs = wp_get_sites();
}
foreach ( $blogs as $keys => $blog ) {
// Cast $blog as an array instead of WP_Site object
if ( is_object( $blog ) ) {
$blog = (array) $blog;
}
$blog_id = $blog['blog_id'];
switch_to_blog( $blog_id );
WP_CLI::success( "Switched to the blog " . get_option( 'home' ) );
$this->set_id_and_replace( $id, $data );
restore_current_blog();
}
} else {
$this->set_id_and_replace( $id, $data );
}
}
public function set_id_and_replace( $id, $data ) {
if ( $id == 'all' ) {
$arrSliders = $this->slider->getArrSliders();
foreach ( $arrSliders as $key => $value ) {
$data["sliderid"] = $value->getID();
$this->replace_revslider_urls( $data );
}
} else {
$data["sliderid"] = $id;
$this->replace_revslider_urls( $data );
}
}
public function replace_revslider_urls( $data ) {
$this->slider->replaceImageUrlsFromData( $data );
WP_CLI::success( "Search Replace complete for slider with id : " . $data['sliderid'] );
}
}
WP_CLI::add_command( 'rsr', 'Revslider_Search_Replace' );