forked from ClassicPress/ClassicPress-Migration-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch-to-classicpress.php
217 lines (196 loc) · 5.25 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
<?php
/*
* Plugin Name: Switch to ClassicPress
* Plugin URI: https://github.com/ClassicPress/ClassicPress-Migration-Plugin
* Description: Switch your WordPress installation to ClassicPress.
* Version: 1.1.0
* 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
*
* @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() {
$parameters = get_transient( 'classicpress_migration_parameters' );
if ( ! $parameters ) {
$response = wp_remote_get( 'https://api-v1.classicpress.net/migration/' );
$parameters = null;
if ( is_wp_error( $response ) ) {
$status = $response->get_error_message();
} else {
$status = wp_remote_retrieve_response_code( $response );
}
if ( $status === 200 ) {
$parameters = json_decode( wp_remote_retrieve_body( $response ), true );
if ( is_array( $parameters ) ) {
set_transient(
'classicpress_migration_parameters',
$parameters,
1 * HOUR_IN_SECONDS
);
}
}
if ( ! is_array( $parameters ) ) {
return new WP_Error(
'classicpress_server_error',
__(
'Could not communicate with the ClassicPress API server',
'switch-to-classicpress'
),
array( 'status' => $status )
);
}
}
return $parameters;
}