-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-sample-plugin.php
More file actions
331 lines (286 loc) · 8.89 KB
/
code-sample-plugin.php
File metadata and controls
331 lines (286 loc) · 8.89 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
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
<?php
/**
* Plugin Name: Code Sample Plugin
* Plugin URI: https://regan.dev
* Description: A code sample plugin
* Author: johnregan3
* Author URI: https://regan.dev
* Version: 0.0.1
* Text Domain: code-sample-plugin
* Domain Path: /lang
* Network: True
* License: GPLv2
* Requires at least: 6.5
* Requires PHP: 7.4
*
* @package Code_Sample_Plugin
*/
define( 'CODE_SAMPLE_PLUGIN_FILE', __FILE__ );
if ( ! function_exists( 'code_sample_plugin_register_updater' ) ) {
/**
* Register the plugin with the Proprietary Updater.
*
* @param Proprietary_Updater_Settings $updater The Updater Settings.
*
* @return void
*/
function code_sample_plugin_register_updater( Proprietary_Updater_Settings $updater ) {
$updater->register( 'code-sample-plugin', __FILE__ );
}
add_action( 'proprietary_updater_register', 'code_sample_plugin_register_updater' );
if ( file_exists( __DIR__ . '/lib/updater/load.php' ) ) {
require_once __DIR__ . '/lib/updater/load.php';
}
}
/**
* Render the settings page.
*
* @return void
*/
function code_sample_plugin_settings_page_html() {
?>
<div class="wrap code-sample-plugin-settings-page"></div>
<?php
}
/**
* Load the onboard page scripts.
*
* @return void
*/
function code_sample_plugin_load_onboard_page_scripts() {
$plugin_path = realpath( plugin_dir_path( CODE_SAMPLE_PLUGIN_FILE ) ) . '/';
$asset_path = $plugin_path . 'dist/onboard.asset.php';
if ( ! file_exists( $asset_path ) ) {
return;
}
$script_meta = include $asset_path;
add_action(
'admin_enqueue_scripts',
function () use ( $script_meta ) {
$url_path = trailingslashit( plugin_dir_url( CODE_SAMPLE_PLUGIN_FILE ) );
// Enqueue the settings page scripts.
wp_enqueue_script( 'code-sample-plugin-onboard', $url_path . 'dist/onboard.js', $script_meta['dependencies'], $script_meta['version'], true );
}
);
}
/**
* Load the settings page scripts.
*
* @return void
*/
function code_sample_plugin_load_settings_page_scripts() {
$plugin_path = realpath( plugin_dir_path( CODE_SAMPLE_PLUGIN_FILE ) ) . '/';
$asset_path = $plugin_path . 'dist/settings.asset.php';
if ( ! file_exists( $asset_path ) ) {
return;
}
$script_meta = include $asset_path;
add_action(
'admin_enqueue_scripts',
function () use ( $script_meta ) {
$site_id = code_sample_plugin_get_hub_site_id();
$url_path = trailingslashit( plugin_dir_url( CODE_SAMPLE_PLUGIN_FILE ) );
// Enqueue the settings page scripts.
wp_enqueue_script( 'code-sample-plugin-settings', $url_path . 'dist/settings.js', $script_meta['dependencies'], $script_meta['version'], true );
wp_add_inline_script('code-sample-plugin-settings', 'window.backupsExports = ' . json_encode( [
'site_id' => $site_id,
'status' => code_sample_plugin_get_connection_status( $site_id ),
'is_authed_user' => code_sample_plugin_is_authed_user( $site_id ),
'links' => [
'timeline' => apply_filters( 'sync_api_request_url', 'https://proprietary-api.com/timeline?' . build_query(
[
'filters' => [
[
'field' => 'site',
'value' => [ $site_id ],
'operator' => 'isAny'
]
]
]
)
),
'edit_connection' => apply_filters( 'sync_api_request_url', "https://proprietary-api.com/site/$site_id/backups" ),
]
]
), 'before' );
}
);
}
/**
* Load the onboard page styles.
*
* @return void
*/
function code_sample_plugin_load_onboard_page_styles() {
remove_all_actions( 'all_admin_notices' );
remove_all_actions( 'network_admin_notices' );
remove_all_actions( 'admin_notices' );
wp_enqueue_style( 'code-sample-plugin-onboard', plugins_url( 'assets/css/onboard.css', __FILE__ ), [ 'wp-components' ] );
}
/**
* Load the settings page styles.
*
* @return void
*/
function code_sample_plugin_load_settings_page_styles() {
remove_all_actions( 'all_admin_notices' );
remove_all_actions( 'network_admin_notices' );
remove_all_actions( 'admin_notices' );
wp_enqueue_style( 'wp-components' );
}
/**
* Check if the user has already completed the onboarding steps.
*
* @return bool
*/
function code_sample_plugin_user_has_onboarded() {
if ( ! is_plugin_active( 'proprietary-sync/init.php' ) ) {
return false;
}
if ( empty( code_sample_plugin_get_authentications() ) ) {
return false;
}
if ( ! isset( $GLOBALS['proprietary_updater_path'] ) ) {
return false;
}
include_once $GLOBALS['proprietary_updater_path'] . '/keys.php';
$license_keys = Proprietary_Updater_Keys::get( [ 'code-sample-plugin' ] );
return ( ! empty( $license_keys['code-sample-plugin'] ) );
}
/**
* Add the settings page to the admin menu.
*
* @return void
*/
function code_sample_plugin_settings_page() {
$page = add_submenu_page(
'options-general.php',
__( 'Code Sample Plugin', 'code-sample-plugin' ),
__( 'Code Sample Plugin', 'code-sample-plugin' ),
'manage_options',
'code-sample-plugin',
'code_sample_plugin_settings_page_html'
);
if ( code_sample_plugin_user_has_onboarded() ) {
add_action( "load-{$page}", 'code_sample_plugin_load_settings_page_scripts' );
add_action( "load-{$page}", 'code_sample_plugin_load_settings_page_styles' );
} else {
add_action( "load-{$page}", 'code_sample_plugin_load_onboard_page_scripts' );
add_action( "load-{$page}", 'code_sample_plugin_load_onboard_page_styles' );
}
}
add_action( 'admin_menu', 'code_sample_plugin_settings_page' );
/**
* Link to the Proprietary Hub onboarding sequence on WordPress 6.5.
*
* The onboarding JS requires WordPress 6.6. For users who are on older
* WordPress versions, we redirect them to the Proprietary Hub to complete onboarding.
*
* @return void
*/
function code_sample_plugin_override_onboard_link() {
if ( code_sample_plugin_user_has_onboarded() ) {
return;
}
require ABSPATH . WPINC . '/version.php';
if ( version_compare( $wp_version, '6.6', '>=' ) ) {
return;
}
global $submenu;
foreach ( $submenu['options-general.php'] as $key => $menu_item ) {
if ( $menu_item[2] !== 'code-sample-plugin' ) {
continue;
}
$submenu['options-general.php'][ $key ][2] = 'https://proprietary-hub.com/onboard/backups';
}
}
add_action( 'admin_head', 'code_sample_plugin_override_onboard_link' );
/**
* Get the authentications from the Proprietary sync plugin.
*
* @return array
*/
function code_sample_plugin_get_authentications() {
if ( ! isset( $GLOBALS['proprietary-sync-settings'] ) ) {
return [];
}
$sync_settings = $GLOBALS['proprietary-sync-settings'];
return $sync_settings->get_option( 'authentications' );
}
/**
* Get the Connection Status from the Proprietary Hub.
*
* @param int|string $site_id The Hub Site ID.
*
* @return string
*/
function code_sample_plugin_get_connection_status( $site_id ) {
// Check if we already have the status.
$ping_status = get_transient( "proprietary_api_ping_$site_id" );
$has_status = $ping_status && isset( $ping_status['backups']['connection_status'] );
// If the status is 'unknown', check for it again.
if ( $has_status && 'unknown' === $ping_status['backups']['connection_status'] ) {
$has_status = false;
}
if ( ! $has_status ) {
// Run the ping check.
require_once $GLOBALS['proprietary_sync_path'] . '/server.php';
$result = $GLOBALS['proprietary-sync-settings']->do_ping_check( $site_id );
if ( ! is_wp_error( $result ) && isset( $result['success'] ) ) {
unset( $result['success'] );
set_transient( "proprietary_ping_$site_id", $result, DAY_IN_SECONDS );
$ping_status = $result;
if ( isset ( $ping_status['connection']['connection_status'] ) ) {
$has_status = true;
}
}
}
if ( ! $has_status ) {
return 'unknown';
}
return $ping_status['connection']['connection_status'];
}
/**
* Get the current user's Hub's site ID.
*
* If a Hub user is currently logged in, this will
* return their Hub Site ID.
*
* If the current user has not authorized with the Hub,
* this will return the first Hub Site ID in the list.
*
* @return int
*/
function code_sample_plugin_get_hub_site_id() {
$authentications = code_sample_plugin_get_authentications();
$current_user = wp_get_current_user();
$user_login = $current_user->user_login;
foreach( $authentications as $site_id => $data ) {
if ( ! isset( $data['local_user'] ) ) {
continue;
}
if ( $data['local_user'] === $user_login ) {
return (int) $site_id;
}
}
return (int) array_keys( $authentications )[0];
}
/**
* Check if the current user is the one who authenticated with the Hub.
*
* This is used to display different content to the user who authenticated.
*
* @param int|string $site_id Hub Site ID.
*
* @return bool
*/
function code_sample_plugin_is_authed_user( $site_id ) {
$authentications = code_sample_plugin_get_authentications();
$current_user = wp_get_current_user();
$user_login = $current_user->user_login;
// Sanity check.
if ( ! isset( $authentications[ (int) $site_id ] ) ) {
return false;
}
return $authentications[ (int) $site_id ]['local_user'] === $user_login;
}