Test Failed
Pull Request — master (#3256)
by Devin
07:52 queued 10s
created

plugins.php ➔ give_get_recently_activated_addons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Plugins
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Plugins
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.4
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Plugins row action links
19
 *
20
 * @since 1.4
21
 *
22
 * @param array $actions An array of plugin action links.
23
 *
24
 * @return array An array of updated action links.
25
 */
26
function give_plugin_action_links( $actions ) {
27
	$new_actions = array(
28
		'settings' => sprintf(
29
			'<a href="%1$s">%2$s</a>',
30
			admin_url( 'edit.php?post_type=give_forms&page=give-settings' ),
31
			__( 'Settings', 'give' )
32
		),
33
	);
34
35
	return array_merge( $new_actions, $actions );
36
}
37
38
add_filter( 'plugin_action_links_' . GIVE_PLUGIN_BASENAME, 'give_plugin_action_links' );
39
40
41
/**
42
 * Plugin row meta links
43
 *
44
 * @since 1.4
45
 *
46
 * @param array  $plugin_meta An array of the plugin's metadata.
47
 * @param string $plugin_file Path to the plugin file, relative to the plugins directory.
48
 *
49
 * @return array
50
 */
51
function give_plugin_row_meta( $plugin_meta, $plugin_file ) {
52
	if ( GIVE_PLUGIN_BASENAME !== $plugin_file ) {
53
		return $plugin_meta;
54
	}
55
56
	$new_meta_links = array(
57
		sprintf(
58
			'<a href="%1$s" target="_blank">%2$s</a>',
59
			esc_url(
60
				add_query_arg(
61
					array(
62
						'utm_source'   => 'plugins-page',
63
						'utm_medium'   => 'plugin-row',
64
						'utm_campaign' => 'admin',
65
					), 'https://givewp.com/documentation/'
66
				)
67
			),
68
			__( 'Documentation', 'give' )
69
		),
70
		sprintf(
71
			'<a href="%1$s" target="_blank">%2$s</a>',
72
			esc_url(
73
				add_query_arg(
74
					array(
75
						'utm_source'   => 'plugins-page',
76
						'utm_medium'   => 'plugin-row',
77
						'utm_campaign' => 'admin',
78
					), 'https://givewp.com/addons/'
79
				)
80
			),
81
			__( 'Add-ons', 'give' )
82
		),
83
	);
84
85
	return array_merge( $plugin_meta, $new_meta_links );
86
}
87
88
add_filter( 'plugin_row_meta', 'give_plugin_row_meta', 10, 2 );
89
90
91
/**
92
 * Get the Parent Page Menu Title in admin section.
93
 * Based on get_admin_page_title WordPress Function.
94
 *
95
 * @since 1.8.17
96
 *
97
 * @global array  $submenu
98
 * @global string $plugin_page
99
 *
100
 * @return string $title Page title
101
 */
102
function give_get_admin_page_menu_title() {
103
	$title = '';
104
	global $submenu, $plugin_page;
105
106
	foreach ( array_keys( $submenu ) as $parent ) {
107
		if ( 'edit.php?post_type=give_forms' !== $parent ) {
108
			continue;
109
		}
110
111
		foreach ( $submenu[ $parent ] as $submenu_array ) {
112
			if ( $plugin_page !== $submenu_array[2] ) {
113
				continue;
114
			}
115
116
			$title = isset( $submenu_array[0] ) ?
117
				$submenu_array[0] :
118
				$submenu_array[3];
119
		}
120
	}
121
122
	return $title;
123
}
124
125
/**
126
 * Store recently activated Give's addons to wp options.
127
 *
128
 * @since 2.1.0
129
 */
130
function give_recently_activated_addons() {
131
	// Check if action is set.
132
	if ( isset( $_REQUEST['action'] ) ) {
133
		$plugin_action = ( '-1' !== $_REQUEST['action'] ) ? $_REQUEST['action'] : ( isset( $_REQUEST['action2'] ) ? $_REQUEST['action2'] : '' );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
134
		$plugins       = array();
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
135
136
		switch ( $plugin_action ) {
137
			case 'activate': // Single add-on activation.
138
				$plugins[] = $_REQUEST['plugin'];
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
139
				break;
140
			case 'activate-selected': // If multiple add-ons activated.
141
				$plugins = $_REQUEST['checked'];
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
142
				break;
143
		}
144
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
145
146
		if ( ! empty( $plugins ) ) {
147
148
			$give_addons = give_get_recently_activated_addons();
149
150
			foreach ( $plugins as $plugin ) {
151
				// Get plugins which has 'Give-' as prefix.
152
				if ( stripos( $plugin, 'Give-' ) !== false ) {
0 ignored issues
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
153
					$give_addons[] = $plugin;
154
				}
155
			}
156
157
			if ( ! empty( $give_addons ) ) {
158
				// Update the Give's activated add-ons.
159
				update_option( 'give_recently_activated_addons', $give_addons );
160
			}
161
		}
162
	}
163
}
164
165
// Add add-on plugins to wp option table.
166
add_action( 'activated_plugin', 'give_recently_activated_addons', 10 );
167
168
/**
169
 * Create new menu in plugin section that include all the add-on
170
 *
171
 * @since 2.1.0
172
 *
173
 * @param $plugin_menu
174
 *
175
 * @return mixed
176
 */
177
function give_filter_addons_do_filter_addons( $plugin_menu ) {
178
	global $plugins;
179
180
	foreach ( $plugins['all'] as $plugin_slug => $plugin_data ) {
181
182
		if ( false !== strpos( $plugin_data['Name'], 'Give' ) && false !== strpos( $plugin_data['AuthorName'], 'WordImpress' ) ) {
183
			$plugins['give'][ $plugin_slug ]           = $plugins['all'][ $plugin_slug ];
184
			$plugins['give'][ $plugin_slug ]['plugin'] = $plugin_slug;
185
			// replicate the next step.
186
			if ( current_user_can( 'update_plugins' ) ) {
187
				$current = get_site_transient( 'update_plugins' );
188
				if ( isset( $current->response[ $plugin_slug ] ) ) {
189
					$plugins['give'][ $plugin_slug ]['update'] = true;
190
				}
191
			}
192
		}
193
	}
194
195
	return $plugin_menu;
196
197
}
198
199
add_filter( 'show_advanced_plugins', 'give_filter_addons_do_filter_addons' );
200
201
/**
202
 * Make the Give Menu as an default menu and update the Menu Name
203
 *
204
 * @since 2.1.0
205
 *
206
 * @param $views
207
 *
208
 * @return mixed
209
 */
210
function give_filter_addons_filter_addons( $views ) {
211
212
	global $status, $plugins;
213
214
	if ( ! empty( $plugins['give'] ) ) {
215
		$class = '';
216
217
		if ( 'give' === $status ) {
218
			$class = 'current';
219
		}
220
221
		$views['give'] = sprintf(
222
			'<a class="%s" href="plugins.php?plugin_status=give"> %s <span class="count">(%s) </span></a>',
223
			$class,
224
			__( 'Give', 'give' ),
225
			count( $plugins['give'] )
226
		);
227
	}
228
229
	return $views;
230
}
231
232
add_filter( 'views_plugins', 'give_filter_addons_filter_addons' );
233
234
/**
235
 * Set the Give as the Main menu when admin click on the Give Menu in Plugin section.
236
 *
237
 * @since 2.1.0
238
 *
239
 * @param $plugins
240
 *
241
 * @return mixed
242
 */
243
function give_prepare_filter_addons( $plugins ) {
244
	global $status;
245
246
	if ( isset( $_REQUEST['plugin_status'] ) && 'give' === $_REQUEST['plugin_status'] ) {
247
		$status = 'give';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
248
	}
249
250
	return $plugins;
251
}
252
253
add_filter( 'all_plugins', 'give_prepare_filter_addons' );
254
255
256
/**
257
 * Display the upgrade notice message.
258
 *
259
 * @param array $data     Array of plugin metadata.
260
 * @param array $response An array of metadata about the available plugin update.
261
 *
262
 * @since 2.1
263
 */
264
function give_in_plugin_update_message( $data, $response ) {
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
265
	$new_version           = $data['new_version'];
266
	$current_version_parts = explode( '.', GIVE_VERSION );
267
	$new_version_parts     = explode( '.', $new_version );
268
269
	// If it is a minor upgrade then return.
270
	if ( version_compare( $current_version_parts[0] . '.' . $current_version_parts[1], $new_version_parts[0] . '.' . $new_version_parts[1], '=' ) ) {
271
272
		return;
273
	}
274
275
	// Get the upgrade notice from the trunk.
276
	$upgrade_notice = give_get_plugin_upgrade_notice( $new_version );
277
278
	// Display upgrade notice.
279
	echo apply_filters( 'give_in_plugin_update_message', $upgrade_notice ? '</p>' . wp_kses_post( $upgrade_notice ) . '<p class="dummy">' : '' );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'apply_filters'
Loading history...
280
}
281
282
// Display upgrade notice.
283
add_action( 'in_plugin_update_message-' . GIVE_PLUGIN_BASENAME, 'give_in_plugin_update_message', 10, 2 );
284
285
286
/**
287
 * Get the upgrade notice from WordPress.org.
288
 *
289
 * Note: internal purpose use only
290
 *
291
 * @since 2.1
292
 *
293
 * @param string $new_version New verison of the plugin.
294
 *
295
 * @return string
296
 */
297
function give_get_plugin_upgrade_notice( $new_version ) {
298
299
	// Cache the upgrade notice.
300
	$transient_name = "give_upgrade_notice_{$new_version}";
301
	$upgrade_notice = get_transient( $transient_name );
302
303
	if ( false === $upgrade_notice ) {
304
		$response = wp_safe_remote_get( 'https://plugins.svn.wordpress.org/give/trunk/readme.txt' );
305
306
		if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
307
			$upgrade_notice = give_parse_plugin_update_notice( $response['body'], $new_version );
308
			set_transient( $transient_name, $upgrade_notice, DAY_IN_SECONDS );
309
		}
310
	}
311
312
	return $upgrade_notice;
313
}
314
315
316
/**
317
 * Parse update notice from readme file.
318
 *
319
 * Note: internal purpose use only
320
 *
321
 * @since 2.1
322
 *
323
 * @param  string $content     Content of the readme.txt file.
324
 * @param  string $new_version The version with current version is compared.
325
 *
326
 * @return string
327
 */
328
function give_parse_plugin_update_notice( $content, $new_version ) {
329
	$version_parts     = explode( '.', $new_version );
330
	$check_for_notices = array(
331
		$version_parts[0] . '.0',
332
		$version_parts[0] . '.0.0',
333
		$version_parts[0] . '.' . $version_parts[1] . '.' . '0',
334
	);
335
336
	// Regex to extract Upgrade notice from the readme.txt file.
337
	$notice_regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(=\s*' . preg_quote( $new_version ) . '\s*=|$)~Uis';
338
339
	$upgrade_notice = '';
340
341
	foreach ( $check_for_notices as $check_version ) {
342
		if ( version_compare( GIVE_VERSION, $check_version, '>' ) ) {
343
			continue;
344
		}
345
346
		$matches = null;
347
348
		if ( preg_match( $notice_regexp, $content, $matches ) ) {
349
			$notices = (array) preg_split( '~[\r\n]+~', trim( $matches[2] ) );
350
351
			if ( version_compare( trim( $matches[1] ), $check_version, '=' ) ) {
352
				$upgrade_notice .= '<p class="give-plugin-upgrade-notice">';
353
354
				foreach ( $notices as $index => $line ) {
355
					$upgrade_notice .= preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $line );
356
				}
357
358
				$upgrade_notice .= '</p>';
359
			}
360
361
			if ( ! empty( $upgrade_notice ) ) {
362
				break;
363
			}
364
		}
365
	}
366
367
	return wp_kses_post( $upgrade_notice );
368
}
369
370
371
/**
372
 * Add styling to the plugin upgrade notice.
373
 *
374
 * @since 2.1
375
 */
376
function give_plugin_notice_css() {
377
?>
378
	<style type="text/css">
379
	#give-update .give-plugin-upgrade-notice {
380
		font-weight: 400;
381
		background: #fff8e5!important;
382
		border-left: 4px solid #ffb900;
383
		border-top: 1px solid #ffb900;
384
		padding: 9px 0 9px 12px!important;
385
		margin: 0 -12px 0 -16px!important;
386
	}
387
388
	#give-update .give-plugin-upgrade-notice:before {
389
		content: '\f348';
390
		display: inline-block;
391
		font: 400 18px/1 dashicons;
392
		speak: none;
393
		margin: 0 8px 0 -2px;
394
		vertical-align: top;
395
	}
396
397
	#give-update .dummy {
398
		display: none;
399
	}
400
	</style>
401
<?php
402
}
403
404
add_action( 'admin_head', 'give_plugin_notice_css' );
405
406
/**
407
 * Get list of add-on last activated.
408
 *
409
 * @since 2.1.3
410
 *
411
 * @return mixed|array list of recently activated add-on
412
 */
413
function give_get_recently_activated_addons() {
414
	return get_option( 'give_recently_activated_addons', array() );
415
}
416