|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Update Plugin |
|
4
|
|
|
* |
|
5
|
|
|
* @package SimpleCalendar/Updates |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace SimpleCalendar; |
|
8
|
|
|
|
|
9
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
10
|
|
|
exit; |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Update script. |
|
15
|
|
|
* |
|
16
|
|
|
* Updates the installed plugin to the current version. |
|
17
|
|
|
* |
|
18
|
|
|
* @since 3.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class Update { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Previous version. |
|
24
|
|
|
* |
|
25
|
|
|
* @access protected |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $installed_ver = '0.0.0'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Current version. |
|
32
|
|
|
* |
|
33
|
|
|
* @access private |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $new_ver = '0.0.0'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Existing posts. |
|
40
|
|
|
* |
|
41
|
|
|
* @access private |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
private $posts = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Update path. |
|
48
|
|
|
* |
|
49
|
|
|
* @access private |
|
50
|
|
|
* |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
private $update_path = array( |
|
54
|
|
|
'2.1.0', |
|
55
|
|
|
'2.2.0', |
|
56
|
|
|
'3.0.0', |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Constructor. |
|
61
|
|
|
* |
|
62
|
|
|
* @since 3.0.0 |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $version (optional) Current plugin version, defaults to value in plugin constant. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct( $version = SIMPLE_CALENDAR_VERSION ) { |
|
67
|
|
|
// Look for previous version in current or legacy option, null for fresh install. |
|
68
|
|
|
$installed = get_option( 'simple-calendar_version', null ); |
|
69
|
|
|
$this->installed_ver = is_null( $installed ) ? get_option( 'gce_version', null ) : $installed; |
|
70
|
|
|
$this->new_ver = $version; |
|
71
|
|
|
|
|
72
|
|
|
if ( version_compare( $this->installed_ver, $this->new_ver, '<' ) ) { |
|
73
|
|
|
$this->run_updates(); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Update to current version. |
|
79
|
|
|
* |
|
80
|
|
|
* Runs all the update scripts through version steps. |
|
81
|
|
|
* |
|
82
|
|
|
* @since 3.0.0 |
|
83
|
|
|
*/ |
|
84
|
|
|
public function run_updates() { |
|
85
|
|
|
|
|
86
|
|
|
do_action( 'simcal_before_update', $this->installed_ver ); |
|
87
|
|
|
|
|
88
|
|
|
if ( ! is_null( $this->installed_ver ) ) { |
|
89
|
|
|
|
|
90
|
|
|
if ( version_compare( $this->installed_ver, $this->new_ver ) === -1 ) { |
|
91
|
|
|
|
|
92
|
|
|
$post_type = version_compare( $this->installed_ver, '3.0.0' ) === -1 ? 'gce_feed' : 'calendar'; |
|
93
|
|
|
$this->posts = $this->get_posts( $post_type ); |
|
94
|
|
|
|
|
95
|
|
|
foreach ( $this->update_path as $update_to ) { |
|
96
|
|
|
if ( version_compare( $this->installed_ver, $update_to, '<' ) ) { |
|
97
|
|
|
$this->update( $update_to ); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
simcal_delete_feed_transients(); |
|
104
|
|
|
|
|
105
|
|
|
} else { |
|
106
|
|
|
|
|
107
|
|
|
new Post_Types(); |
|
108
|
|
|
flush_rewrite_rules(); |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
do_action( 'simcal_updated', $this->new_ver ); |
|
113
|
|
|
|
|
114
|
|
|
// Redirect to a welcome page if new install or major update. |
|
115
|
|
|
if ( is_null( $this->installed_ver ) ) { |
|
116
|
|
|
set_transient( '_simple-calendar_activation_redirect', 'fresh', 60 ); |
|
117
|
|
|
} else { |
|
118
|
|
|
$major_new = substr( $this->new_ver, 0, strrpos( $this->new_ver, '.' ) ); |
|
119
|
|
|
$major_old = substr( $this->installed_ver, 0, strrpos( $this->installed_ver, '.' ) ); |
|
120
|
|
|
if ( version_compare( $major_new, $major_old, '>' ) ) { |
|
121
|
|
|
set_transient( '_simple-calendar_activation_redirect', 'update', 60 ); |
|
122
|
|
|
} elseif ( $major_old == $major_new ) { |
|
123
|
|
|
$version = explode( '.', $this->new_ver ); |
|
124
|
|
|
end( $version ); |
|
125
|
|
|
if ( 0 === intval( current( $version ) ) ) { |
|
126
|
|
|
set_transient( '_simple-calendar_activation_redirect', 'update', 60 ); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->admin_redirects(); |
|
132
|
|
|
|
|
133
|
|
|
update_option( 'simple-calendar_version', $this->new_ver ); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Handle redirects to welcome page after install and updates. |
|
138
|
|
|
* |
|
139
|
|
|
* Transient must be present, the user must have access rights, and we must ignore the network/bulk plugin updaters. |
|
140
|
|
|
* |
|
141
|
|
|
* @since 3.0.0 |
|
142
|
|
|
*/ |
|
143
|
|
|
public function admin_redirects() { |
|
144
|
|
|
|
|
145
|
|
|
$transient = get_transient( '_simple-calendar_activation_redirect' ); |
|
146
|
|
|
|
|
147
|
|
|
if ( ! $transient || is_network_admin() || isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_options' ) ) { |
|
|
|
|
|
|
148
|
|
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
delete_transient( '_simple-calendar_activation_redirect' ); |
|
152
|
|
|
|
|
153
|
|
|
// Do not redirect if already on welcome page screen. |
|
154
|
|
|
if ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], array( 'simple-calendar_about' ) ) ) { |
|
|
|
|
|
|
155
|
|
|
return; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$url = add_query_arg( |
|
159
|
|
|
'simcal_install', |
|
|
|
|
|
|
160
|
|
|
esc_attr( $transient ), |
|
|
|
|
|
|
161
|
|
|
admin_url( 'index.php?page=simple-calendar_about' ) |
|
|
|
|
|
|
162
|
|
|
); |
|
163
|
|
|
wp_safe_redirect( $url ); |
|
164
|
|
|
exit; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get posts. |
|
169
|
|
|
* |
|
170
|
|
|
* @since 3.0.0 |
|
171
|
|
|
* |
|
172
|
|
|
* @param $post_type |
|
173
|
|
|
* |
|
174
|
|
|
* @return array |
|
175
|
|
|
*/ |
|
176
|
|
|
private function get_posts( $post_type ) { |
|
177
|
|
|
|
|
178
|
|
|
$posts = array(); |
|
179
|
|
|
|
|
180
|
|
|
if ( ! empty( $post_type ) ) { |
|
181
|
|
|
|
|
182
|
|
|
// https://core.trac.wordpress.org/ticket/18408 |
|
183
|
|
|
$posts = get_posts( array( |
|
184
|
|
|
'post_type' => $post_type, |
|
185
|
|
|
'post_status' => array( |
|
186
|
|
|
'draft', |
|
187
|
|
|
'future', |
|
188
|
|
|
'publish', |
|
189
|
|
|
'pending', |
|
190
|
|
|
'private', |
|
191
|
|
|
'trash', |
|
192
|
|
|
), |
|
193
|
|
|
'nopaging' => true, |
|
|
|
|
|
|
194
|
|
|
) ); |
|
195
|
|
|
|
|
196
|
|
|
wp_reset_postdata(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $posts; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Update. |
|
204
|
|
|
* |
|
205
|
|
|
* Runs an update script for the specified version passed in argument. |
|
206
|
|
|
* |
|
207
|
|
|
* @since 3.0.0 |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $version |
|
210
|
|
|
*/ |
|
211
|
|
|
private function update( $version ) { |
|
212
|
|
|
|
|
213
|
|
|
$update_v = '\\' . __NAMESPACE__ . '\Updates\\Update_V' . str_replace( '.', '', $version ); |
|
214
|
|
|
|
|
215
|
|
|
if ( class_exists( $update_v ) ) { |
|
216
|
|
|
new $update_v( $this->posts ); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
|