-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-partnership-notice.php
564 lines (485 loc) · 16.6 KB
/
class-partnership-notice.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
555
556
557
558
559
560
561
562
563
564
<?php
/**
* Extendify_Partner class
*
* @package Extendify
* @since 1.0.0
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'Extendify_Partner', false ) ) {
/**
* Extendify_Partner class.
*/
class Extendify_Partner {
/**
* Query Themes or Plugins.
*
* @var array
*/
private $pages = array();
/**
* Display label array.
*
* @var array
*/
private $labels = array();
/**
* Banner image.
*
* @var string
*/
private $image = '';
/**
* Notice key.
*
* @var string
*/
private $key = '';
/**
* Extendify_Partner constructor.
*
* @param string $project Project name.
* @param array|string $image Array of image data or SVG string.
* @param array $pages Page name or array of pages.
* @param array|string $labels Label array.
*/
public function __construct( string $project = '', $image = array(), $pages = array('themes'), array $labels = array() ) {
// Checking for active banner.
if ( isset( $GLOBALS['extendify_d_notice_showing'] ) && true === $GLOBALS['extendify_d_notice_showing'] ) {
return;
}
if ( '' === $project ) {
echo 'Extendify Partner: No project name given. Please specify the name of your theme or plugin.';
return;
}
$GLOBALS['extendify_partner_name'] = $project;
// Labels.
$default_labels = array(
// translators: %1$s = theme name, %2$s = Extendify brand.
'header' => wp_sprintf( esc_html__( '%1$s + %2$s = Awesomeness' ), $project, 'Extendify' ),
// translators: %1$s = theme name.
'main_content' => wp_sprintf( esc_html__( 'We\'re excited to announce that %1$s is partnering with the %2$s library of Gutenberg patterns and templates to bring %1$s users even more beautiful block patterns and templates! Install and activate the %2$s plugin to receive access to the library. Note: this is an optional step and %1$s will continue to work without %2$s.' ), $project, 'Extendify' ),
// translators: %1$s = Extendify Brand.
'install' => wp_sprintf( esc_html__( 'Install & Activate %1$s' ), 'Extendify' ),
'installing' => esc_html__( 'Installing...' ),
'reloading' => esc_html__( 'Finished. Reloading...' ),
// translators: %1$s = Extendify Brand.
'dismiss_label' => wp_sprintf( esc_attr__( 'Dismiss %1$s notice' ), 'Extendify' ),
);
$this->labels = wp_parse_args( $labels, $default_labels );
// Detect SVG or custom HTML.
if ( ! is_array( $image ) ) {
$this->image = $image;
} else {
$default_image = array(
'img' => '', // https://ps.w.org/extendify/assets/icon-128x128.png.
'width' => 96,
'height' => 96,
);
$image = wp_parse_args( $image, $default_image );
$this->image = '<img style="width:' . esc_attr( $image['width'] ) . 'px;height:' . esc_attr( $image['height'] ) . 'px" width=' . esc_attr( $image['width'] ) . ' height=' . esc_attr( $image['height'] ) . ' src="' . esc_url( $image['img'] ) . '" alt="" />';
}
// Pages.
if ( ! is_array( $pages ) ) {
$pages = array( $pages );
}
$this->pages = $pages;
$this->key = 'extendify_complete_install_' . mb_strtolower( str_replace( ' ', '_', $project ) );
if ( $this->check_standalone_active() ) {
return;
}
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
add_action( 'admin_notices', array( $this, 'display_banner' ) );
add_action( 'wp_ajax_handle_install_' . $this->key, array( $this, 'install' ) );
add_action( 'wp_ajax_handle_dismiss_' . $this->key, array( $this, 'dismiss' ) );
}
/**
* Will be true if the plugin is installed.
*
* @return bool
*/
private function check_standalone_active(): bool {
if ( ! function_exists( 'get_plugins' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
foreach ( get_plugins() as $plugin => $data ) {
if ( 'extendify' === $data['TextDomain'] ) {
// Side effect - just set this global to extendify in case it's not set.
$GLOBALS['extendify_sdk_partner'] = 'standalone';
return is_plugin_active( $plugin );
}
}
return isset( $GLOBALS['extendify_sdk_partner'] ) && 'standalone' === $GLOBALS['extendify_sdk_partner'];
}
/**
* Display Extendify partner banner.
*
* @return void
*/
public function display_banner() {
$current_page = get_current_screen();
if (
! $current_page ||
! in_array( $current_page->base, $this->pages, true ) ||
isset( $GLOBALS['extendify_d_notice_showing'] ) ||
get_user_option( $this->key )
) {
return;
}
$GLOBALS['extendify_d_notice_showing'] = true;
$nonce = wp_create_nonce( $this->key );
ob_start();
?>
<p>
<?php echo $this->labels['main_content']; // phpcs:ignore WordPress.Security.EscapeOutput ?>
</p>
<button id="extendify-install-button" type="button" class="button-primary"
style="margin-bottom: 1rem;margin-top: 0.5rem;"><?php echo $this->labels['install']; // phpcs:ignore WordPress.Security.EscapeOutput ?></button>
<script>
jQuery(
function( $ ) {
$( '#extendify-install-button' ).on(
'click',
function() {
var _this = $( this );
var data = {
action: 'handle_install_<?php echo $this->key; // phpcs:ignore WordPress.Security.EscapeOutput ?>',
_wpnonce: '<?php echo $nonce; // phpcs:ignore WordPress.Security.EscapeOutput ?>'
};
_this.attr( 'disabled', true ).text( "<?php echo $this->labels['installing']; // phpcs:ignore WordPress.Security.EscapeOutput ?>" );
$.post(
ajaxurl,
data,
function() {
_this.text( "<?php echo $this->labels['reloading']; // phpcs:ignore WordPress.Security.EscapeOutput ?>" );
setTimeout(
function() {
// Regardless of pass/fail, refresh to hide the notice.
window.location.reload();
},
1500
);
}
);
}
);
}
);
</script>
<?php $extendify_d_notices_content = ob_get_clean(); ?>
<div id="<?php echo $this->key; // phpcs:ignore WordPress.Security.EscapeOutput ?>" class="notice notice-info"
style="display:flex;align-items:stretch;justify-content:space-between;position:relative;border-left-color:#29375B">
<div style="display:flex;align-items:flex-start;position:relative">
<div style="margin-right:1.25rem;margin-left:0.5rem; margin-top:1.25rem">
<?php echo $this->image; // phpcs:ignore WordPress.Security.EscapeOutput ?>
</div>
<div>
<h3 style="margin-bottom:0.25rem;">
<?php echo $this->labels['header']; // phpcs:ignore WordPress.Security.EscapeOutput ?></h3>
<div>
<?php echo $extendify_d_notices_content; // phpcs:ignore WordPress.Security.EscapeOutput ?>
</div>
</div>
</div>
<div style="margin:5px -5px 0 0;">
<button
style="max-width:15px;border:0;background:0;color: #7b7b7b;white-space:nowrap;cursor: pointer;padding: 0"
title="<?php echo $this->labels['dismiss_label'];// phpcs:ignore WordPress.Security.EscapeOutput ?>"
aria-label="<?php echo $this->labels['dismiss_label']; // phpcs:ignore WordPress.Security.EscapeOutput ?>"
onclick="jQuery('#<?php echo $this->key; // phpcs:ignore WordPress.Security.EscapeOutput ?>').remove();
jQuery.post(
window.ajaxurl,
{
action: 'handle_dismiss_<?php echo $this->key; // phpcs:ignore WordPress.Security.EscapeOutput ?>',
_wpnonce: '<?php echo $nonce; // phpcs:ignore WordPress.Security.EscapeOutput ?>'
}
);">
<svg width="15" height="15" xmlns="http://www.w3.org/2000/svg" fill="none"
viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
</svg>
</button>
</div>
</div>
<?php
ob_end_flush();
}
/**
* Install button click.
*
* @return void
*/
public function install() {
if ( isset( $_REQUEST['_wpnonce'] ) && ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), $this->key ) ) {
wp_send_json_error(
array(
'message' => esc_html__( 'The security check failed. Please refresh the page and try again.' ),
),
401
);
}
update_user_option( get_current_user_id(), $this->key, time() );
try {
$this->install_and_activate_plugin( 'extendify' );
} catch ( Exception $e ) {
wp_send_json_error( array( 'message' => $e->getMessage() ), 500 );
}
wp_send_json_success();
}
/**
* Dismiss button click.
*
* @return void
*/
public function dismiss() {
if ( isset( $_REQUEST['_wpnonce'] ) && ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), $this->key ) ) {
wp_send_json_error(
array(
'message' => esc_html__( 'The security check failed. Please refresh the page and try again.' ),
),
401
);
}
update_user_option( get_current_user_id(), $this->key, time() );
wp_send_json_success();
}
/**
* Install and activate a plugin.
*
* @param string $slug Plugin slug.
*
* @return bool|WP_Error True if installation succeeded, error object otherwise.
* @since 5.8.0
*/
private function install_and_activate_plugin( $slug ) {
$plugin_id = $this->get_plugin_id_by_slug( $slug );
if ( ! $plugin_id ) {
$installed = $this->install_plugin( $slug );
if ( is_wp_error( $installed ) ) {
return $installed;
}
$plugin_id = $this->get_plugin_id_by_slug( $slug );
} elseif ( is_plugin_active( $plugin_id ) ) {
return true; // Already installed and active.
}
if ( ! current_user_can( 'activate_plugins' ) ) {
return new WP_Error( 'not_allowed', esc_html__( 'You are not allowed to activate plugins on this site.' ) );
}
$activated = activate_plugin( $plugin_id );
if ( is_wp_error( $activated ) ) {
return $activated;
}
return true;
}
/**
* Install a plugin.
*
* @param string $slug Plugin slug.
*
* @return WP_Error|array True if installation succeeded, error object otherwise.
* @since 5.8.0
*/
private function install_plugin( string $slug ) {
if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
return new WP_Error( 'not_allowed', __( 'You are not allowed to install plugins on this site.' ) );
}
$skin = new Extendify_D_PluginUpgraderSkin();
$upgrader = new Plugin_Upgrader( $skin );
$zip_url = $this->generate_wordpress_org_plugin_download_link( $slug );
$result = $upgrader->install( $zip_url );
if ( is_wp_error( $result ) ) {
return $result;
}
$plugin = $this->get_plugin_id_by_slug( $slug );
$error_code = 'install_error';
if ( ! $plugin ) {
$error = __( 'There was an error installing your plugin' );
}
if ( ! $result ) {
$error_code = $upgrader->skin->get_main_error_code();
$message = $upgrader->skin->get_main_error_message();
$error = $message ? $message : __( 'An unknown error occurred during installation' );
}
if ( ! empty( $error ) ) {
if ( 'download_failed' === $error_code ) {
// For backwards compatibility: versions prior to 3.9 would return no_package instead of download_failed.
$error_code = 'no_package';
}
return new WP_Error( $error_code, $error, 400 );
}
return $upgrader->skin->get_upgrade_messages();
}
/**
* Get WordPress.org zip download link from a plugin slug
*
* @param string $plugin_slug Plugin slug.
*/
protected function generate_wordpress_org_plugin_download_link( string $plugin_slug ): string {
return "https://downloads.wordpress.org/plugin/$plugin_slug.latest-stable.zip";
}
/**
* Get the plugin ID (composed of the plugin slug and the name of the main plugin file) from a plugin slug.
*
* @param string $slug Plugin slug.
*/
private function get_plugin_id_by_slug( string $slug ) {
// Check if get_plugins() function exists. This is required on the front end of the
// site, since it is in a file that is normally only loaded in the admin.
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
$plugins = apply_filters( 'all_plugins', get_plugins() );
if ( ! is_array( $plugins ) ) {
return false;
}
foreach ( $plugins as $plugin_file => $plugin_data ) {
if ( $this->get_slug_from_file_path( $plugin_file ) === $slug ) {
return $plugin_file;
}
}
return false;
}
/**
* Get the plugin slug from the plugin ID (composed of the plugin slug and the name of the main plugin file)
*
* @param string $plugin_file Plugin file (ID -- e.g. hello-dolly/hello.php).
*/
protected function get_slug_from_file_path( string $plugin_file ) {
// Similar to get_plugin_slug() method.
$slug = dirname( $plugin_file );
if ( '.' === $slug ) {
$slug = preg_replace( '/(.+)\.php$/', '$1', $plugin_file );
}
return $slug;
}
}
}
/**
* Allows us to capture that the site doesn't have proper file system access.
* In order to update the plugin.
*/
if ( ! class_exists( 'Extendify_D_PluginUpgraderSkin' ) ) {
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
class Extendify_D_PluginUpgraderSkin extends \Automatic_Upgrader_Skin {
/**
* Stores the last error key;
*
* @var string
**/
protected $main_error_code = 'install_error';
/**
* Stores the last error message.
*
* @var string
**/
protected $main_error_message = 'An unknown error occurred during installation';
/**
* Overwrites the set_upgrader to be able to tell if we e ven have the ability to write to the files.
*
* @param WP_Upgrader $upgrader WP Upgrader class.
*/
public function set_upgrader( &$upgrader ) {
parent::set_upgrader( $upgrader );
// Check if we even have permission to.
$result = $upgrader->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
if ( ! $result ) {
// set the string here since they are not available just yet.
$upgrader->generic_strings();
$this->feedback( 'fs_unavailable' );
}
}
/**
* Overwrites the error function.
*
* @param WP_Error $errors WP error.
*/
public function error( $errors ) {
if ( is_wp_error( $errors ) ) {
$this->feedback( $errors );
}
}
/**
* Sets the main error code.
*
* @param string|null $code Error code.
*
* @return void
*/
private function set_main_error_code( $code ) {
// Don't set the process_failed as code since it is not that helpful unless we don't have one already set.
$this->main_error_code = ( 'process_failed' === $code && $this->main_error_code ? $this->main_error_code : $code );
}
/**
* Sets the main error message.
*
* @param string $message Error message.
* @param string|null $code Error code.
*
* @return void
*/
private function set_main_error_message( string $message, $code ) {
// Don't set the process_failed as message since it is not that helpful unless we don't have one already set.
$this->main_error_message = ( 'process_failed' === $code && $this->main_error_code ? $this->main_error_code : $message );
}
/**
* Gets the main error code.
*
* @return string
*/
public function get_main_error_code(): string {
return $this->main_error_code;
}
/**
* Gets the main error message.
*
* @return string
*/
public function get_main_error_message(): string {
return $this->main_error_message;
}
/**
* Overwrites the feedback function
*
* @param string|array|WP_Error $feedback Data.
* @param mixed ...$args Optional text replacements.
*/
public function feedback( $feedback, ...$args ) {
$current_error = null;
if ( is_wp_error( $feedback ) ) {
$this->set_main_error_code( $feedback->get_error_code() );
$string = $feedback->get_error_message();
} elseif ( is_array( $feedback ) ) {
return;
} else {
$string = $feedback;
}
if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
$this->set_main_error_code( $string );
$current_error = $string;
$string = $this->upgrader->strings[ $string ];
}
if ( strpos( $string, '%' ) !== false ) {
if ( ! empty( $args ) ) {
$string = vsprintf( $string, $args );
}
}
$string = trim( $string );
$string = wp_kses(
$string,
array(
'a' => array(
'href' => true,
),
'br' => true,
'em' => true,
'strong' => true,
)
);
$this->set_main_error_message( $string, $current_error );
$this->messages[] = $string;
}
}
}