-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathswitch-to-classicpress.php
339 lines (303 loc) · 8.62 KB
/
switch-to-classicpress.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/*
* Plugin Name: Switch to ClassicPress
* Plugin URI: https://github.com/ClassicPress/ClassicPress-Migration-Plugin
* Description: Switch your WordPress installation to ClassicPress.
* Version: 1.5.1
* Author: ClassicPress
* Author URI: https://www.classicpress.net
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
* Text Domain: switch-to-classicpress
* Requires at least: 4.9
* Network: true
* @package ClassicPress
*/
/**
* Prevent direct access to plugin files.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Global actions and filters.
*
* @since 0.1.0
*/
add_filter( 'plugins_loaded', 'classicpress_load_plugin_textdomain' );
/**
* Load the plugin's translated strings.
*
* @since 0.1.0
*/
function classicpress_load_plugin_textdomain() {
load_plugin_textdomain(
'switch-to-classicpress',
false,
basename( dirname( __FILE__ ) ) . '/languages'
);
}
/**
* Load the plugin's admin page.
*
* @since 0.1.0
*/
require_once dirname( __FILE__ ) . '/lib/admin-page.php';
/**
* Load helper functions.
*
* @since 0.2.0
*/
require_once dirname( __FILE__ ) . '/lib/check-core-files.php';
/**
* Load the update hijacking mechanism.
*
* @since 0.1.0
*/
require_once dirname( __FILE__ ) . '/lib/update.php';
/**
* On multisite, the plugin must be network activated.
*
* @since 0.2.0
*/
function classicpress_ensure_network_activated() {
if ( ! is_multisite() ) {
return;
}
if ( ! is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) {
add_action( 'admin_notices', 'classicpress_deactivated_notice' );
deactivate_plugins( array( plugin_basename( __FILE__ ) ) );
// HACK: Prevent the "Plugin activated" notice from showing.
unset( $_GET['activate'] );
}
}
add_action( 'admin_head', 'classicpress_ensure_network_activated' );
/**
* Shows a notice that the plugin was deactivated.
*
* @since 0.2.0
*/
function classicpress_deactivated_notice() {
echo '<div class="error"><p>';
_e(
'The "Switch to ClassicPress" plugin must be <strong>network activated</strong> on multisite installations.',
'switch-to-classicpress'
);
echo '</p><p>';
_e(
'If you need help with this, please contact your site administrator.',
'switch-to-classicpress'
);
echo '</p></div>';
}
/**
* Add plugin action links.
*
* Add a link to the Switch page on the plugins.php page.
*
* @since 0.1.0
*
* @param array $links List of existing plugin action links.
*
* @return array List of modified plugin action links.
*/
function classicpress_plugin_action_links( $links ) {
if ( function_exists( 'classicpress_version' ) ) {
// Already running ClassicPress - showing this link is more confusing
// than helpful.
return $links;
}
if ( is_multisite() ) {
// Multisite: Only add the "Switch" link if the plugin is network
// activated and the current user can upgrade core.
if (
! is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ||
! current_user_can( 'update_core' )
) {
return $links;
}
$upgrade_page_url = network_admin_url( 'index.php?page=switch-to-classicpress' );
} else {
// Single site: Always add the "Switch" link.
$upgrade_page_url = admin_url( 'tools.php?page=switch-to-classicpress' );
}
$links = array_merge( array(
'<a class="cp-migration-action" href="' . esc_url( $upgrade_page_url ) . '">'
. __( 'Switch', 'switch-to-classicpress' )
. '</a>'
), $links );
return $links;
}
add_filter(
'plugin_action_links_' . plugin_basename( __FILE__ ),
'classicpress_plugin_action_links'
);
add_filter(
'network_admin_plugin_action_links_' . plugin_basename( __FILE__ ),
'classicpress_plugin_action_links'
);
/**
* Call the ClassicPress API to determine which versions of WordPress and
* ClassicPress are supported by the migration plugin.
*
* This is handled on the ClassicPress servers, because most of the time a new
* version of WordPress or ClassicPress does not require a new version of the
* migration plugin.
*
* @since 1.0.1
*
* @return array {
* "wordpress": {
* "min": "4.9.0",
* "max": "5.x.x",
* "other": [
* "^4\\.9$",
* // patterns for development versions ...
* ]
* },
* "classicpress": {
* "build": "https://github.com/[...].zip",
* "version": "1.x.x"
* }
* }
*/
function classicpress_migration_parameters() {
$cp_api_parameters = get_transient( 'classicpress_migration_parameters' );
if ( ! $cp_api_parameters ) {
$response = wp_remote_get( 'https://api-v1.classicpress.net/migration/' );
$cp_api_parameters = null;
if ( is_wp_error( $response ) ) {
$status = $response->get_error_message();
} else {
$status = wp_remote_retrieve_response_code( $response );
}
if ( $status === 200 ) {
$cp_api_parameters = json_decode( wp_remote_retrieve_body( $response ), true );
if ( is_array( $cp_api_parameters ) ) {
set_transient(
'classicpress_migration_parameters',
$cp_api_parameters,
1 * HOUR_IN_SECONDS
);
}
}
if ( ! is_array( $cp_api_parameters ) ) {
return new WP_Error(
'classicpress_server_error',
__(
'Could not communicate with the ClassicPress API server',
'switch-to-classicpress'
),
array( 'status' => $status )
);
}
}
return $cp_api_parameters;
}
/**
* Get a list of ClassicPress released versions from api-v1.classicpress.net.
*
* @return array|false Array of CP versions or false on API failure.
*/
function get_cp_versions() {
$cp_versions = get_transient( 'classicpress_release_versions' );
if ( ! $cp_versions ) {
$response = wp_remote_get('https://api-v1.classicpress.net/v1/upgrade/index.php', ['timeout'=>3]);
if ( is_wp_error( $response ) ) {
$status = $response->get_error_message();
} else {
$status = wp_remote_retrieve_response_code( $response );
}
if ( $status === 200 ) {
$cp_versions = json_decode( wp_remote_retrieve_body( $response ), true );
if ( is_array( $cp_versions ) ) {
set_transient(
'classicpress_release_versions',
$cp_versions,
1 * HOUR_IN_SECONDS
);
}
}
if ( ! is_array( $cp_versions ) ) {
return new WP_Error(
'classicpress_server_error',
__(
'Could not communicate with the ClassicPress API server',
'switch-to-classicpress'
),
array( 'status' => $status )
);
}
}
// $versions = json_decode(wp_remote_retrieve_body($response));
// Get only stable releases
foreach ($cp_versions as $key => $version) {
if(!strpos($version, 'nightly') && !strpos($version, 'rc') && !strpos($version, 'alpha') && !strpos($version, 'beta')) {
continue;
}
unset($cp_versions[$key]);
}
// Strip .json from version
$cp_versions = array_map(
function($v) {
return substr($v, 0, -5);
},
$cp_versions
);
// Sort using SemVer
usort($cp_versions, 'version_compare');
return array_values($cp_versions);
}
function get_migration_from_cp_version($version) {
$response = wp_remote_get('https://api.github.com/repos/ClassicPress/ClassicPress-release/releases/tags/'.$version, ['timeout'=>3]);
if (is_wp_error($response) || empty($response)) {
return false;
}
$data = json_decode(wp_remote_retrieve_body($response), true);
if(isset($data['message']) && $data['message'] === 'Not Found') {
return '';
}
$created_at = new \DateTime($data['created_at']);
$day = $created_at->format('Ymd');
$exploded = explode('.', $version);
$major = $exploded[0];
$url = 'https://github.com/ClassyBot/ClassicPress-v'.$major.'-nightly/releases/download/'.$version.'%2Bmigration.'.$day.'/ClassicPress-nightly-'.$version.'-migration.'.$day.'.zip';
return $url;
}
/**
* Get release URL.
*
* @param string $version Version to retrive migration URL.
*
* @return string URL for release.
*/
function getReleaseFromCPVersion($version) {
return 'https://github.com/ClassicPress/ClassicPress-release/archive/refs/tags/'.$version.'.zip';
}
/**
* Get previous release version.
*
* @param string $version Version to get previous release.
* @param array $versions Array of ClassicPress versions as
* returned by getCPVersions().
* Used for caching.
*
* @return string|bool Previous version. False if not found.
*/
function get_previous_version($version, $versions = []) {
if (empty($versions)) {
// $versions = self::getCPVersions();
$versions = get_cp_versions();
} else {
usort($versions, 'version_compare');
}
if(!in_array($version, $versions)) {
return false;
}
$pos = array_search($version, $versions, true);
if(!isset($versions[$pos - 1])) {
return false;
}
return $versions[$pos - 1];;
}