actions.php ➔ give_upload_addon_handler()   F
last analyzed

Complexity

Conditions 14
Paths 512

Size

Total Lines 107

Duplication

Lines 15
Ratio 14.02 %

Importance

Changes 0
Metric Value
cc 14
nc 512
nop 0
dl 15
loc 107
rs 2.2222
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Admin Add-ons Actions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Add-ons/Actions
7
 * @copyright   Copyright (c) 2019, GiveWP
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       2.5.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Ajax addon upload handler
19
 *
20
 * Note: only for internal use
21
 *
22
 * @since 2.5.0
23
 */
24
function give_upload_addon_handler() {
25
	/* @var WP_Filesystem_Direct $wp_filesystem */
26
	global $wp_filesystem;
27
28
	check_admin_referer( 'give-upload-addon' );
29
30
	// Remove version from file name.
31
	$filename = preg_replace(  '/(.\d)+.zip/', '', $_FILES['file']['name']  );
32
	$filename = basename( $filename, '.zip' );
33
34
35
	// Bailout if user does not has permission.
36
	if ( ! current_user_can( 'upload_plugins' ) ) {
37
		wp_send_json_error( array( 'errorMsg' => __( 'Sorry, you are not allowed to upload add-ons on this site.', 'give' ) ) );
38
	}
39
40
	$access_type = get_filesystem_method();
41
42
	if ( 'direct' !== $access_type ) {
43
		wp_send_json_error(
44
			array(
45
				'errorMsg' => sprintf(
46
					__( 'Sorry, you can not upload plugins because Give does not have direct access to the file system. Please <a href="%1$s" target="_blank">click here</a> to upload the add-on.', 'give' ),
47
					admin_url( 'plugin-install.php?tab=upload' )
48
				),
49
			)
50
		);
51
	}
52
53
	$file_type = wp_check_filetype( $_FILES['file']['name'], array( 'zip' => 'application/zip' ) );
54
55
	if ( empty( $file_type['ext'] ) ) {
56
		wp_send_json_error( array( 'errorMsg' =>  __( 'Only zip file type allowed to upload. Please upload a valid add-on file.', 'give' ) ) );
57
	}
58
59
	$give_addons_list   = give_get_plugins();
60
	$is_addon_installed = array();
61
62 View Code Duplication
	if ( ! empty( $give_addons_list ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
		foreach ( $give_addons_list as $addon => $give_addon ) {
64
			if ( false !== stripos( $addon, $filename ) ) {
65
				$is_addon_installed = $give_addon;
66
			}
67
		}
68
	}
69
70
	// Bailout  if addon already installed
71
	if ( ! empty( $is_addon_installed ) ) {
72
		wp_send_json_error( array(
73
			'errorMsg'   => __( 'This add-on is already installed.', 'give' ),
74
			'pluginInfo' => $is_addon_installed,
75
		) );
76
	}
77
78
	$upload_status = wp_handle_upload( $_FILES['file'], array( 'test_form' => false ) );
79
80
	// Bailout if has any upload error
81
	if ( empty( $upload_status['file'] ) ) {
82
		wp_send_json_error( $upload_status );
83
	}
84
85
	// @todo: check how wordpress verify plugin files before uploading to plugin directory
86
87
	/* you can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */
88
	$creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() );
89
90
	/* initialize the API */
91
	if ( ! WP_Filesystem( $creds ) ) {
92
		/* any problems and we exit */
93
		wp_send_json_error(array(
94
			'errorMsg' => __( 'File system does not load correctly.', 'give' )
95
		));
96
	}
97
98
	$unzip_status = unzip_file( $upload_status['file'], $wp_filesystem->wp_plugins_dir() );
99
100
	// Remove file.
101
	@unlink( $upload_status['file'] );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
102
103
	// Bailout if not able to unzip file successfully
104
	if ( is_wp_error( $unzip_status ) ) {
105
		wp_send_json_error( array(
106
			'errorMsg' => $unzip_status
107
		) );
108
	}
109
110
	// Delete cache and get current installed addon plugin path.
111
	wp_cache_delete( 'plugins', 'plugins' );
112
	$give_addons_list   = give_get_plugins();
113
	$installed_addon  = array();
114
115 View Code Duplication
	if ( ! empty( $give_addons_list ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
		foreach ( $give_addons_list as $addon => $give_addon ) {
117
			if ( false !== stripos( $addon, $filename ) ) {
118
				$installed_addon         = $give_addon;
119
				$installed_addon['path'] = $addon;
120
			}
121
		}
122
	}
123
124
	wp_send_json_success( array(
125
		'pluginPath'         => $installed_addon['path'],
126
		'pluginName'         => $installed_addon['Name'],
127
		'nonce'              => wp_create_nonce( "give_activate-{$installed_addon['path']}" ),
128
		'licenseSectionHtml' => Give_License::render_licenses_list(),
129
	) );
130
}
131
132
add_action( 'wp_ajax_give_upload_addon', 'give_upload_addon_handler' );
133
134
/**
135
 * Ajax license inquiry handler
136
 *
137
 * Note: only for internal use
138
 *
139
 * @since 2.5.0
140
 */
141
function give_get_license_info_handler() {
142
	check_admin_referer( 'give-license-activator-nonce' );
143
144
	// check user permission.
145
	if ( ! current_user_can( 'manage_give_settings' ) ) {
146
		give_die();
147
	}
148
149
	$license_key                  = ! empty( $_POST['license'] ) ? give_clean( $_POST['license'] ) : '';
150
	$is_activating_single_license = ! empty( $_POST['single'] ) ? absint( $_POST['single'] ) : '';
151
	$is_reactivating_license      = ! empty( $_POST['reactivate'] ) ? absint( $_POST['reactivate'] ) : '';
152
	$plugin_slug                  = $is_activating_single_license ? give_clean( $_POST['addon'] ) : '';
153
	$licenses                     = get_option( 'give_licenses', array() );
154
155
156
	if ( ! $license_key ) {
157
		wp_send_json_error( array(
158
			'errorMsg' => __( 'Sorry, you entered an invalid key.', 'give' ),
159
		) );
160
161
	} else if (
162
		! $is_reactivating_license
163
		&& array_key_exists( $license_key, $licenses )
164
	) {
165
		// If admin already activated license but did not install add-on then send license info show notice to admin with download link.
166
		$license = $licenses[$license_key];
167
		if( empty( $license['is_all_access_pass'] ) ) {
168
			$plugin_data = Give_License::get_plugin_by_slug( $license['plugin_slug' ] );
169
170
			// Plugin license activated but does not install, sent notice which allow admin to download add-on.
171
			if( empty( $plugin_data ) ) {
172
				wp_send_json_success( $license );
173
			}
174
		}
175
176
		wp_send_json_error( array(
177
			'errorMsg' => __( 'This license key is already in use on this website.', 'give' ),
178
		) );
179
	}
180
181
182
	// Check license.
183
	$check_license_res = Give_License::request_license_api( array(
184
		'edd_action' => 'check_license',
185
		'license'    => $license_key,
186
	), true );
187
188
	// Make sure there are no errors.
189
	if ( is_wp_error( $check_license_res ) ) {
190
		wp_send_json_error( array(
191
			'errorMsg' => $check_license_res->get_error_message(),
192
		) );
193
	}
194
195
	// Check if license valid or not.
196
	if ( ! $check_license_res['success'] ) {
197
		wp_send_json_error( array(
198
			'errorMsg' => sprintf(
199
				__( 'Sorry, this license was unable to activate because the license status returned as <code>%2$s</code>. Please visit your <a href="%1$s" target="_blank">license dashboard</a> to check the details and access priority support.' ),
200
				Give_License::get_account_url(),
201
				$check_license_res['license']
202
			),
203
		) );
204
	}
205
206 View Code Duplication
	if(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
		$is_activating_single_license
208
		&& ! empty( $check_license_res['plugin_slug'] )
209
		&& $plugin_slug !== $check_license_res['plugin_slug']
210
	) {
211
		wp_send_json_error( array(
212
			'errorMsg' => sprintf(
213
				__( 'Sorry, we are unable to activate this license because this key does not belong to this add-on. Please visit your <a href="%1$s" target="_blank">license dashboard</a> to check the details and access priority support.' ),
214
				Give_License::get_account_url()
215
			),
216
		) );
217
	}
218
219
	// Activate license.
220
	$activate_license_res = Give_License::request_license_api( array(
221
		'edd_action' => 'activate_license',
222
		'item_name'  => $check_license_res['item_name'],
223
		'license'    => $license_key,
224
	), true );
225
226
	if ( is_wp_error( $activate_license_res ) ) {
227
		wp_send_json_error( array(
228
			'errorMsg' => $check_license_res->get_error_message(),
229
		) );
230
	}
231
232
	// Return error if license activation is not success and admin is not reactivating add-on.
233 View Code Duplication
	if ( ! $is_reactivating_license && ! $activate_license_res['success']  ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
235
		$response['errorMsg'] = sprintf(
236
			__( 'Sorry, this license was unable to activate because the license status returned as <code>%2$s</code>. Please visit your <a href="%1$s" target="_blank">license dashboard</a> to check the details and access priority support.' ),
237
			Give_License::get_account_url(),
238
			$check_license_res['license']
239
		);
240
241
		wp_send_json_error( $response );
242
	}
243
244
	$check_license_res['license']          = $activate_license_res['license'];
245
	$check_license_res['site_count']       = $activate_license_res['site_count'];
246
	$check_license_res['activations_left'] = $activate_license_res['activations_left'];
247
248
	$licenses[ $check_license_res['license_key'] ] = $check_license_res;
249
	update_option( 'give_licenses', $licenses );
250
251
	// Get license section HTML.
252
	$response         = $check_license_res;
253
	$response['html'] = $is_activating_single_license && empty( $check_license_res['is_all_access_pass'] )
254
		? Give_License::html_by_plugin( Give_License::get_plugin_by_slug( $check_license_res['plugin_slug'] ) )
255
		: Give_License::render_licenses_list();
256
257
	// Return error if license activation is not success and admin is reactivating add-on.
258 View Code Duplication
	if ( $is_reactivating_license && ! $activate_license_res['success'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
260
		$response['errorMsg'] = sprintf(
261
			__( 'Sorry, this license was unable to activate because the license status returned as <code>%2$s</code>. Please visit your <a href="%1$s" target="_blank">license dashboard</a> to check the details and access priority support.' ),
262
			Give_License::get_account_url(),
263
			$check_license_res['license']
264
		);
265
266
		wp_send_json_error( $response );
267
	}
268
269
270
	// Tell WordPress to look for updates.
271
	give_refresh_licenses();
272
273
	wp_send_json_success( $response );
274
}
275
276
add_action( 'wp_ajax_give_get_license_info', 'give_get_license_info_handler' );
277
278
279
/**
280
 * Activate addon handler
281
 *
282
 * Note: only for internal use
283
 *
284
 * @since 2.5.0
285
 */
286
function give_activate_addon_handler() {
287
	$plugin_path = give_clean( $_POST['plugin'] );
288
289
	check_admin_referer( "give_activate-{$plugin_path}" );
290
291
	// check user permission.
292
	if ( ! current_user_can( 'manage_give_settings' ) ) {
293
		give_die();
294
	}
295
296
	$status = activate_plugin( $plugin_path );
297
298
	if ( is_wp_error( $status ) ) {
299
		wp_send_json_error( array( 'errorMsg' => $status->get_error_message() ) );
300
	}
301
302
	// Tell WordPress to look for updates.
303
	give_refresh_licenses();
304
305
	wp_send_json_success( array(
306
		'licenseSectionHtml' => Give_License::render_licenses_list(),
307
	) );
308
}
309
310
add_action( 'wp_ajax_give_activate_addon', 'give_activate_addon_handler' );
311
312
313
/**
314
 * deactivate addon handler
315
 *
316
 * Note: only for internal use
317
 *
318
 * @since 2.5.0
319
 */
320
function give_deactivate_license_handler() {
321
	$license        = give_clean( $_POST['license'] );
322
	$item_name      = give_clean( $_POST['item_name'] );
323
	$plugin_dirname = give_clean( $_POST['plugin_dirname'] );
324
325
	if ( ! $license || ! $item_name ) {
326
		wp_send_json_error();
327
	}
328
329
	check_admin_referer( "give-deactivate-license-{$item_name}" );
330
331
	// check user permission.
332
	if ( ! current_user_can( 'manage_give_settings' ) ) {
333
		give_die();
334
	}
335
336
	$give_licenses = get_option( 'give_licenses', array() );
337
338
	if ( empty( $give_licenses[ $license ] ) ) {
339
		wp_send_json_error( array(
340
				'errorMsg' => __( 'We are unable to deactivate invalid license', 'give' ),
341
			)
342
		);
343
	}
344
345
	/* @var array|WP_Error $response */
346
	$response = Give_License::request_license_api( array(
347
		'edd_action' => 'deactivate_license',
348
		'license'    => $license,
349
		'item_name'  => $item_name,
350
	), true );
351
352
	if ( is_wp_error( $response ) ) {
353
		wp_send_json_error( array(
354
			'errorMsg' => $response->get_error_message(),
355
			'response' => $license,
356
		) );
357
	}
358
359
	$is_all_access_pass = $give_licenses[ $license ]['is_all_access_pass'];
360
361
	if ( ! empty( $give_licenses[ $license ] ) ) {
362
		unset( $give_licenses[ $license ] );
363
		update_option( 'give_licenses', $give_licenses );
364
	}
365
366
	$response['html'] = $is_all_access_pass
367
		? Give_License::render_licenses_list()
368
		: Give_License::html_by_plugin( Give_License::get_plugin_by_slug( $plugin_dirname ) );
0 ignored issues
show
Bug introduced by
It seems like $plugin_dirname defined by give_clean($_POST['plugin_dirname']) on line 323 can also be of type array; however, Give_License::get_plugin_by_slug() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
369
370
	$response['msg'] = __( 'You have successfully deactivated the license.', 'give' );
371
372
	// Tell WordPress to look for updates.
373
	give_refresh_licenses();
374
375
	wp_send_json_success( $response );
376
}
377
378
add_action( 'wp_ajax_give_deactivate_license', 'give_deactivate_license_handler' );
379
380
381
/**
382
 * Refresh all addons licenses handler
383
 *
384
 * Note: only for internal use
385
 *
386
 * @since 2.5.0
387
 */
388
function give_refresh_all_licenses_handler() {
389
	check_admin_referer( 'give-refresh-all-licenses' );
390
391
	// check user permission.
392
	if ( ! current_user_can( 'manage_give_settings' ) ) {
393
		give_die();
394
	}
395
396
	$data = Give_License::refresh_license_status();
397
398
	// Update date and reset counter.
399
	if ( $data['compare'] === date( 'Ymd' ) && 5 <= $data['count'] ) {
400
		wp_send_json_error();
401
	}
402
403
	// Update date and reset counter.
404
	if ( $data['compare'] < date( 'Ymd' ) ) {
405
		$data['compare'] = date( 'Ymd' );
406
		$data['count']   = 0;
407
	}
408
409
	// Update time.
410
	$data['time'] = current_time( 'timestamp', 1 );
411
412
	++ $data['count'];
413
414
	update_option( 'give_licenses_refreshed_last_checked', $data, 'no' );
415
416
	give_refresh_licenses();
417
418
	$local_date = strtotime( get_date_from_gmt( date( 'Y-m-d H:i:s', $data['time'] ) ) );
419
	wp_send_json_success( array(
420
		'html'          => Give_License::render_licenses_list(),
421
		'refreshButton' => 5 <= $data['count'],
422
		'refreshStatus' => $data,
423
		'lastUpdateMsg' => sprintf(
424
			__( 'Last refreshed on %1$s at %2$s', 'give' ),
425
			date( give_date_format(), $local_date ),
426
			date( 'g:i a', $local_date )
427
		),
428
	) );
429
}
430
431
add_action( 'wp_ajax_give_refresh_all_licenses', 'give_refresh_all_licenses_handler' );
432
433
434
/**
435
 * Updates information on the "View version x.x details" page with custom data.
436
 * Note: only for internal use
437
 *
438
 * @param mixed  $_data
439
 * @param string $_action
440
 * @param object $_args
441
 *
442
 * @return object $_data
443
 * @since 2.5.0
444
 * @uses  api_request()
445
 *
446
 */
447
function give_plugins_api_filter( $_data, $_action = '', $_args = null ) {
448
	// Exit.
449
	if ( 'plugin_information' !== $_action ) {
450
		return $_data;
451
	}
452
453
454
	$plugin = Give_License::get_plugin_by_slug( $_args->slug );
455
456
	if (
457
		! $plugin
0 ignored issues
show
Bug Best Practice introduced by
The expression $plugin of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
458
		|| 'add-on' !== $plugin['Type']
459
		|| false === strpos( $_args->slug, 'give-' )
460
	) {
461
		return $_data;
462
	}
463
464
	$plugin_data = get_site_transient( 'update_plugins' );
465
466
	if ( ! $plugin_data ) {
467
		give_refresh_licenses();
468
	}
469
470
	$plugin_data = ! empty( $plugin_data->response[ $plugin['Path'] ] )
471
		? $plugin_data->response[ $plugin['Path'] ]
472
		: array();
473
474
	if ( ! $plugin_data ) {
475
		return $_data;
476
	}
477
478
	$_data = $plugin_data;
479
480
	return $_data;
481
}
482
483
add_filter( 'plugins_api', 'give_plugins_api_filter', 9999, 3 );
484
485
486
/**
487
 * Check add-ons updates when WordPress check plugin updates
488
 *
489
 * @since 2.5.0
490
 */
491
add_filter( 'pre_set_site_transient_update_plugins', 'give_check_addon_updates', 999, 1 );
492
493
494
/**
495
 * Show plugin update notification on multi-site
496
 *
497
 * @param string $file
498
 * @param array  $plugin
499
 *
500
 * @since 2.5.0
501
 */
502
function give_show_update_notification_on_multisite( $file, $plugin ) {
503
	if ( is_network_admin() ) {
504
		return;
505
	}
506
507
	if ( ! current_user_can( 'update_plugins' ) ) {
508
		return;
509
	}
510
511
	if ( ! is_multisite() ) {
512
		return;
513
	}
514
515 View Code Duplication
	if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
516
		! $plugin
0 ignored issues
show
Bug Best Practice introduced by
The expression $plugin of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
517
		|| empty( $plugin['slug'] )
518
		|| false === strpos( $plugin['slug'], 'give-' )
519
	) {
520
		return;
521
	}
522
523
	$plugin_data = Give_License::get_plugin_by_slug( $plugin['slug'] );
524
525
	// Only show notices for Give add-ons
526
	if ( 'add-on' !== $plugin_data['Type']  ) {
527
		return;
528
	}
529
530
	// Do not print any message if updates does not exist.
531
	$update_cache = get_site_transient( 'update_plugins' );
532
533
	if( ! isset( $update_cache->response[$file] ) ) {
534
		return;
535
	}
536
537
538
	if ( ! empty( $update_cache->response[ $plugin_data['Path'] ] ) && version_compare( $plugin_data['Version'], $plugin['new_version'], '<' ) ) {
539
		printf(
540
			'<tr class="plugin-update-tr %3$s" id="%1$s-update" data-slug="%1$s" data-plugin="%2$s">',
541
			$plugin['slug'],
542
			$file,
543
			'active' === $plugin_data['Status'] ? 'active' : 'inactive'
544
		);
545
546
		echo '<td colspan="3" class="plugin-update colspanchange">';
547
		echo '<div class="update-message notice inline notice-warning notice-alt"><p>';
548
549
		$changelog_link = self_admin_url( "plugin-install.php?tab=plugin-information&plugin={$plugin['slug']}&section=changelog&TB_iframe=true&width=772&height=299" );
550
551
		if ( empty( $plugin['download_link'] ) ) {
552
			printf(
553
				__( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'give' ),
554
				esc_html( $plugin_data['Name'] ),
555
				'<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">',
556
				esc_html( $plugin['new_version'] ),
557
				'</a>'
558
			);
559
		} else {
560
			printf(
561
				__( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'give' ),
562
				esc_html( $plugin_data['Name'] ),
563
				'<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">',
564
				esc_html( $plugin['new_version'] ),
565
				'</a>',
566
				'<a target="_blank" class="update-link" href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $file, 'upgrade-plugin_' . $file ) ) . '">',
567
				'</a>'
568
			);
569
		}
570
571
		do_action( "in_plugin_update_message-{$file}", $plugin, $plugin );
572
573
		echo '</p></div></td></tr>';
574
	}
575
}
576
577
add_action( 'after_plugin_row', 'give_show_update_notification_on_multisite', 10, 2 );
578
579
/**
580
 * Show plugin update notification on single site
581
 *
582
 * @param $file
583
 * @param $plugin
584
 *
585
 * @since 2.5.0
586
 */
587
function give_show_update_notification_on_single_site( $file, $plugin ) {
588
	if ( ! current_user_can( 'update_plugins' ) || is_multisite() ) {
589
		return;
590
	}
591
592
593 View Code Duplication
	if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
594
		! $plugin
595
		|| empty( $plugin['slug'] )
596
		|| false === strpos( $plugin['slug'], 'give-' )
597
	) {
598
		return;
599
	}
600
601
	$plugin_data = Give_License::get_plugin_by_slug( $plugin['slug'] );
602
603
	// Only show notices for Give add-ons
604
	if (
605
		'add-on' !== $plugin_data['Type']
606
		|| $plugin_data['License']
607
	) {
608
		return;
609
	}
610
611
	// Do not print any message if updates does not exist.
612
	$update_plugins = get_site_transient( 'update_plugins' );
613
	if( ! isset( $update_plugins->response[$file] ) ) {
614
		return;
615
	}
616
617
618
	// Remove core update notice.
619
	remove_action( "after_plugin_row_{$file}", 'wp_plugin_update_row' );
620
621
	$update_notice_wrap = '<tr class="plugin-update-tr %3$s"><td colspan="3" class="colspanchange"><div class="update-message notice inline notice-warning notice-alt give-invalid-license"><p>%1$s %2$s</p></div></td></tr>';
622
	$changelog_link     = self_admin_url( "plugin-install.php?tab=plugin-information&plugin={$plugin['slug']}&section=changelog&TB_iframe=true&width=772&height=299" );
623
624
	echo sprintf(
625
		$update_notice_wrap,
626
		sprintf(
627
			__( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'give' ),
628
			esc_html( $plugin_data['Name'] ),
629
			'<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">',
630
			esc_html( $plugin['new_version'] ),
631
			'</a>'
632
		),
633
		sprintf(
634
			'Please <a href="%1$s" target="_blank">activate your license</a> to receive updates and support.',
635
			esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) )
636
		),
637
		'active' === $plugin_data['Status'] ? 'active' : 'inactive'
638
	);
639
}
640
641
add_action( 'after_plugin_row', 'give_show_update_notification_on_single_site', 1, 2 );
642
643
644
645