-
Notifications
You must be signed in to change notification settings - Fork 3
/
codepotent-update-manager.php
554 lines (450 loc) · 15.9 KB
/
codepotent-update-manager.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<?php
/**
* -----------------------------------------------------------------------------
* Plugin Name: Update Manager
* Description: Painlessly push updates to your ClassicPress plugin users! Serve updates from GitHub, your own site, or somewhere in the cloud. 100% integrated with the ClassicPress update process; slim and performant.
* Version: 2.0.0
* Author: Code Potent
* Author URI: https://codepotent.com
* Plugin URI: https://codepotent.com/classicpress/plugins
* Text Domain: codepotent-update-manager
* Domain Path: /languages
* -----------------------------------------------------------------------------
* This is free software released under the terms of the General Public License,
* version 2, or later. It is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Full
* text of the license is available at https://www.gnu.org/licenses/gpl-2.0.txt.
* -----------------------------------------------------------------------------
* Copyright © 2019 - Code Potent
* -----------------------------------------------------------------------------
* ____ _ ____ _ _
* / ___|___ __| | ___| _ \ ___ | |_ ___ _ __ | |_
* | | / _ \ / _` |/ _ \ |_) / _ \| __/ _ \ '_ \| __|
* | |__| (_) | (_| | __/ __/ (_) | || __/ | | | |_
* \____\___/ \__,_|\___|_| \___/ \__\___|_| |_|\__|.com
*
* -----------------------------------------------------------------------------
*/
// Declare the namespace.
namespace CodePotent\UpdateManager;
// Prevent direct access.
if (!defined('ABSPATH')) {
die();
}
class UpdateManager {
/**
* A slim constructor makes the world a better place. Like, with sparkles.
*
* @author John Alarcon
*
* @since 1.0.0
*/
public function __construct() {
// Hook everything in!
$this->init();
}
/**
* Plugin initialization.
*
* This method loads the constants and functions used by the plugin. It then
* hooks the plugin's code into the system and creates the needed objects.
*
* @author John Alarcon
*
* @since 1.0.0
*/
public function init() {
// Load constants.
require_once(plugin_dir_path(__FILE__).'includes/constants.php');
// Load functions.
require_once(PATH_INCLUDES.'/functions.php');
// Register the autoload method.
spl_autoload_register(__CLASS__.'::autoload_classes');
//
add_action('admin_menu', [$this, 'register_admin_menu'], 9);
// Register privacy page content.
add_action('admin_init', [$this, 'register_privacy_disclosure']);
// Enqueue backend scripts.
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']);
// Add the plugins query variable.
add_filter('query_vars', [$this, 'filter_query_vars']);
// Filter the template to create JSON instead.
add_filter('template_include', [$this, 'filter_template_include']) ;
// Filter plugin admin action links.
add_filter('plugin_action_links_'.PLUGIN_IDENTIFIER, [$this, 'filter_plugin_action_links']);
// Replace footer text with plugin name and version info.
add_filter('admin_footer_text', [$this, 'filter_footer_text'], PHP_INT_MAX);
// Plugin upgrade.
add_action('upgrader_process_complete', [$this, 'upgrade_plugin'], 10, 2);
// Convert post types after upgrade, if necessary.
add_action('registered_post_type', [$this, 'update_cpt_identifiers']);
// Plugin activation.
register_activation_hook(__FILE__, [$this, 'activate_plugin']);
// Plugin deactivation.
register_deactivation_hook(__FILE__, [$this, 'deactivate_plugin']);
// Plugin deletion.
register_uninstall_hook(__FILE__, [__CLASS__, 'uninstall_plugin']);
// Run the CPT-generating code.
new PluginEndpoint;
new ThemeEndpoint;
// Run the update client.
UpdateClient::get_instance();
}
public function register_admin_menu() {
add_menu_page(
esc_html__('Update Manager', 'codepotent-update-manager'),
esc_html__('Update Manager', 'codepotent-update-manager'),
'manage_options',
'update-manager',
[$this, 'admin_placeholder'],
'dashicons-update',
apply_filters(PLUGIN_PREFIX.'_menu_pos', null),
);
add_submenu_page(
'update-manager',
esc_html__('Update Manager Overview', 'codepotent-update-manager'),
esc_html__(' Overview', 'codepotent-update-manager'),
'manage_options',
'update-manager-overview',
[$this, 'admin_placeholder'],
);
remove_submenu_page('update-manager', 'update-manager');
}
public function admin_placeholder() {
echo '<h1>';
echo sprintf(
esc_html__('%s Overview', 'codepotent-update-manager'),
PLUGIN_NAME);
echo '</h1>';
}
/**
* Privacy disclosure.
*
* This method creates a custom section on the core Privacy page to describe
* which data is received by the Update Manager plugin and how it used used.
* This method does not add anything to the actual Privacy Policy.
*
* @author John Alarcon
*
* @since 1.0.0
*/
public function register_privacy_disclosure() {
// Description of data collected.
$content = sprintf(
esc_html__('
%1$sWhat data is received and how is it used?%2$s
When a remote plugin queries for information from the Update Manager
plugin, the following data is sent along with the request:
%3$s
%5$sThe plugin identifier (i.e., plugin-folder/plugin-file.php)
is a unique identifier that ensures you get the correct data.%6$s
%5$sThe URL making the request is needed for queries when the
endpoint is in Pending status; for whitelisting.%6$s
%5$sThe URL to the local assets, if any; these are URLs to the
banner, icon, and screenshots used in the various views.%6$s
%5$sThe data from plugin headers provides version information;
for determining if an update is available.%6$s
%4$s
', 'codepotent-update-manager'),
'<h3>',
'</h3>',
'<ol>',
'</ol>',
'<li>',
'</li>'
);
// Example text for a Privacy Policy entry.
$content .= sprintf(
esc_html__('
%1$sExample Text for Privacy Policy%2$s
%3$sNOTE%4$s: %5$sThe following text is not legal advice and should
not be taken as such. It is merely a suggestion of how one might go
about disclosing to end users the data that is received and how it
is used by the Update Manager plugin. If collecting more information
or storing any data, it would likely require additional disclosures
to ensure compliance with GDPR.%6$s
%7$sIn an effort to keep you up to date with latest developments, we
will periodically make updates available for our plugins. When your
site checks in for updates, your URL is sent along with the request.
This allows us to whitelist certain domains to get updates early,
allowing for test-runs to make sure there are no issues, before
rolling out the update to all users. Additionally, if the plugin
contains image assets such as a banner, icon, or screenshots, those
URLs will also be transmitted with the request. These URLs are added
to the latest data (in memory) and reflected back to your site as a
data array that the core system can use to populate the various
related views. This allows images to be served from your own server,
speeding up the process a bit. Finally, the data from the header
section of plugin files is transmitted to help in the determination
of whether an update is available or not. All data sent withing the
request is used on a per-request basis and is not stored in any way.
However, while no transmitted data is stored, requests to a server
will generally be captured by server access/error logs. Data may
also be stored in other ways, such as in the logs of a security or
auditing plugin. The duration of such logged data is subject to the
policies under which the data was collected and stored and may be
outside our control.%8$s
', 'codepotent-update-manager'),
'<h3>',
'</h3>',
'<strong>',
'</strong>',
'<em style="color:#f00;">',
'</em>',
'<p>',
'</p>'
);
// Paragraph it up.
$content = wpautop($content, false);
// Add the content to the system.
wp_add_privacy_policy_content(PLUGIN_NAME, $content);
}
/**
* Enqueue admin scripts.
*
* The method enqueues both scripts and styles on an as-needed basis. Labels
* used in JavaScript functionality are also localized here.
*
* @author John Alarcon
*
* @since 1.0.0
*
* @param string $hook_suffix
*/
public function enqueue_admin_scripts($hook_suffix) {
// Assets for CPT-related views.
if (in_array($hook_suffix, ['post.php', 'post-new.php'])) {
wp_enqueue_style(PLUGIN_SLUG.'-post-edit', URL_STYLES.'/post-edit.css', [], time());
wp_enqueue_script(PLUGIN_SLUG.'-post-edit', URL_SCRIPTS.'/post-edit.js', ['jquery'], time());
wp_localize_script(PLUGIN_SLUG.'-post-edit', 'slug', PLUGIN_SLUG);
wp_localize_script(PLUGIN_SLUG.'-post-edit', 'confirmation', esc_html__('You are about to completely replace all the text currently in the editor! Is this what you meant to do?', 'codepotent-update-manager'));
}
}
/**
* Filter query variables.
*
* Add the endpoint's public queryable variable to the pile.
*
* @author John Alarcon
*
* @since 1.0.0
*
* @param array $vars
* @return array
*/
public function filter_query_vars($vars) {
// Add the endpoint variable.
$vars[] = ENDPOINT_VARIABLE;
// Return the array.
return $vars;
}
/**
* Filter template include.
*
* A filter to replace the template used for requests to this plugin. Rather
* than showing it as a post, it's output as JSON.
*
* @author John Alarcon
*
* @since 1.0.0
*
* @param string $template
* @return string
*/
public function filter_template_include($template) {
// Get the query variable.
$endpoint = get_query_var(ENDPOINT_VARIABLE);
// Array of permissed endpoint requests.
$endpoints = [
'query_plugins',
'plugin_information',
];
// If the query is for an endpoint, reset template path for JSON.
if (in_array($endpoint, $endpoints, true)) {
$template = PATH_SELF.'/endpoints/'.$endpoint.'.php';
}
// Return the template path.
return $template;
}
/**
* Filter for plugin list table action links.
*
* Adds a link to the plugin's admin page.
*
* @author John Alarcon
*
* @since 1.0.0
*
* @param array $links
* @return array
*/
public function filter_plugin_action_links($links) {
// Add a link to the plugin update manager.
$links['plugin_endpoint'] = '<a href="'.admin_url('edit.php?post_type='.CPT_FOR_PLUGIN_ENDPOINTS).'">'.esc_html__('Manage Endpoints', 'codepotent-update-manager').'</a>';
// Return all the things.
return $links;
}
/**
* Filter footer text.
*
* This method changes the left-hand admin footer text with the plugin name,
* version, and a Code Potent wordmark.
*
* @author John Alarcon
*
* @since 1.0.0
*
* @param string $text
* @return string
*/
public function filter_footer_text($text) {
// Get current screen.
$screen = get_current_screen();
// Are we on this post type's screen? If so, change the footer text.
if ($screen->post_type === CPT_FOR_PLUGIN_ENDPOINTS ||
$screen->post_type === CPT_FOR_THEME_ENDPOINTS) {
$text = '<span id="footer-thankyou" style="vertical-align:text-bottom;"><a href="'.VENDOR_PLUGIN_URL.'/" title="'.PLUGIN_DESCRIPTION.'">'.PLUGIN_NAME.'</a> '.PLUGIN_VERSION.' — by <a href="'.VENDOR_HOME_URL.'" title="'.VENDOR_TAGLINE.'"><img src="'.VENDOR_WORDMARK_URL.'" alt="'.VENDOR_TAGLINE.'" style="height:1.02em;vertical-align:sub !important;"></a></span>';
}
// Return the footer text.
return $text;
}
/**
* Plugin activation.
*
* This method is included for completeness.
*
* @author John Alarcon
*
* @since 1.0.0
*/
public function activate_plugin() {
// Bring database object into scope.
global $wpdb;
// Convert guids to new CPT identifier.
$wpdb->query("UPDATE $wpdb->posts SET guid = REPLACE(guid, 'plugin_repo', '".CPT_FOR_PLUGIN_ENDPOINTS."');");
// Convert old CPT identifiers to new CPT identifier.
$wpdb->query("UPDATE $wpdb->posts SET post_type = REPLACE(post_type, 'plugin_repo', '".CPT_FOR_PLUGIN_ENDPOINTS."');");
}
/**
* Plugin deactivation.
*
* This method is included for completeness.
*
* @author John Alarcon
*
* @since 1.0.0
*/
public function deactivate_plugin() {
// Nothing to do here at this time.
}
/**
* Plugin upgrade.
*
* This method sets a transient denoting that the plugin was just upgraded.
*
* @author John Alarcon
*
* @since 1.0.0
*
* @param object $upgrader_object The upgrader object.
* @param array $args Arguments from the upgrade process.
*/
public function upgrade_plugin($upgrader_object, $args) {
// Not dealing with a plugin update? Bail.
if ($args['action'] !== 'update' || $args['type'] !== 'plugin') {
return;
}
// Ensure the needed argument exists, or bail.
if (empty($args['plugins']) || !is_array($args['plugins'])) {
return;
}
// The Update Manager plugin wasn't just updated? Bail.
if (!in_array(PLUGIN_IDENTIFIER, $args['plugins'], true)) {
return;
}
// Set a transient to flag that plugin was upgraded.
set_transient(PLUGIN_IDENTIFIER.'_upgraded', 1, 120);
}
/**
* Update custom post types, if needed.
*
* This method deactivates and reactivates the plugin. This converts the RC1
* post types to RC2.
*
* @author John Alarcon
*
* @since 1.0.0
*/
public function update_cpt_identifiers() {
// No transient indicating plugin was just updated? Bail.
if (!get_transient(PLUGIN_IDENTIFIER.'_upgraded')) {
return;
}
// Deactivate the plugin.
deactivate_plugins(PLUGIN_IDENTIFIER);
// Reactivate the plugin; this converts RC1 post types to RC2.
activate_plugins(PLUGIN_IDENTIFIER);
// All done; delete the transient.
delete_transient(PLUGIN_IDENTIFIER.'_upgraded');
}
/**
* Plugin uninstallation/deletion.
*
* This method is included for completeness.
*
* @author John Alarcon
*
* @since 1.0.0
*/
public static function uninstall_plugin() {
// Make sure the plugin's constants are available.
if (!defined(__NAMESPACE__.'\PLUGIN_VERSION')) {
require_once('/includes/constants.php');
}
// Get ids for all CPT items created by the plugin.
$posts = get_posts([
'post_type' => CPT_FOR_PLUGIN_ENDPOINTS,
'post_status' => ['draft', 'pending', 'publish', 'trash'],
'posts_per_page' => -1,
'fields' => 'ids'
]);
// Delete posts, metadata, comments, all in one-fell-swoop.
if (!is_wp_error($posts) && !empty($posts)) {
foreach ($posts as $post) {
wp_delete_post($post->ID, true);
}
}
// Delete options set by the plugin.
delete_option('cp_latest_version');
}
/**
* Autoload classes.
*
* A method to autoload any needed classes.
*
* @author John Alarcon
*
* @since 1.0.0
*
* @param string $class
*/
public function autoload_classes($class) {
// If the class is not inside the plugin's namespace, bail.
if (!strstr($class, __NAMESPACE__)) {
return;
}
// Replace underscores with backslashes.
$class = str_replace('_', '\\', $class);
// Separate qualified class name on backslashes.
$class_parts = explode('\\', $class);
// Get the class name.
$class_name = array_pop($class_parts);
// If the file exists, require it.
if (file_exists($class_file = PATH_CLASSES.'/'.$class_name.'.class.php')) {
require_once $class_file;
return;
}
}
}
// Make run all the things!
new UpdateManager;