Completed
Push — issue/1680 ( 7086f3 )
by Ravinder
18:25
created

Give_Addon_Activation_Banner::is_plugin_page()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Give Activation Banner
4
 *
5
 * @author  WordImpress
6
 * @version 1.0
7
 * https://github.com/WordImpress/plugin-activation-banner-demo
8
 */
9
10
// Exit if accessed directly.
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Class Give_Addon_Activation_Banner
17
 */
18
class Give_Addon_Activation_Banner {
19
20
	/**
21
	 * Class constructor.
22
	 *
23
	 * @since  1.0
24
	 * @access public
25
	 *
26
	 * @param array $_banner_details {
27
	 *                               'file'              => __FILE__, // (required) Directory path to the main plugin file
28
	 *                               'name'              => __( 'Authorize.net Gateway', 'give-authorize' ), // (required) Name of the Add-on
29
	 *                               'version'           => GIVE_AUTHORIZE_VERSION, // (required)The most current version
30
	 *                               'documentation_url' => 'http://docs.givewp.com/addon-authorize',// (required)
31
	 *                               'support_url'       => 'https://givewp.com/support/', // (required)Location of Add-on settings page, leave blank to hide
32
	 *                               'testing'           => false, // (required) Never leave as "true" in production!!!
33
	 *                               }
34
	 */
35
	function __construct( $_banner_details ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
		$current_user = wp_get_current_user();
37
38
		$this->plugin_activate_by   = 0;
0 ignored issues
show
Bug introduced by
The property plugin_activate_by does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
		$this->banner_details       = $_banner_details;
0 ignored issues
show
Bug introduced by
The property banner_details does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
		$this->test_mode            = ( $this->banner_details['testing'] == 'true' ) ? true : false;
0 ignored issues
show
Bug introduced by
The property test_mode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
		$this->nag_meta_key         = 'give_addon_activation_ignore_' . sanitize_title( $this->banner_details['name'] );
0 ignored issues
show
Bug introduced by
The property nag_meta_key does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
		$this->activate_by_meta_key = 'give_addon_' . sanitize_title( $this->banner_details['name'] ) . '_active_by_user';
0 ignored issues
show
Bug introduced by
The property activate_by_meta_key does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
44
		//Get current user
45
		$this->user_id = $current_user->ID;
0 ignored issues
show
Bug introduced by
The property user_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
47
		// Set up hooks.
48
		$this->init();
49
50
		// Store user id who activate plugin.
51
		$this->add_addon_activate_meta();
52
	}
53
54
	/**
55
	 * Set up WordPress filters to hook into WP's update process.
56
	 *
57
	 * @since  1.0
58
	 * @access public
59
	 *
60
	 * @return void
61
	 */
62
	public function init() {
63
64
		//Testing?
65
		if ( $this->test_mode ) {
66
			delete_user_meta( $this->user_id, $this->nag_meta_key );
67
		}
68
69
		//Get the current page to add the notice to
70
		add_action( 'current_screen', array( $this, 'give_addon_notice_ignore' ) );
71
		add_action( 'admin_notices', array( $this, 'give_addon_activation_admin_notice' ) );
72
73
		// File path of addon must be included in banner detail other addon activate meta will not delete.
74
		$file_name = $this->get_plugin_file_name();
75
76
		if ( ! empty( $file_name ) ) {
77
			add_action( 'deactivate_' . $file_name, array( $this, 'remove_addon_activate_meta' ) );
78
		}
79
	}
80
81
82
	/**
83
	 * Check if current page is plugin page or not.
84
	 *
85
	 * @since  1.8
86
	 * @access private
87
	 * @return bool
88
	 */
89
	private function is_plugin_page() {
90
		$screen = get_current_screen();
91
92
		return ( $screen->parent_file === 'plugins.php' );
93
	}
94
95
96
	/**
97
	 * Give Addon Activation Banner
98
	 *
99
	 * @since  1.0
100
	 * @access public
101
	 */
102
	public function give_addon_activation_admin_notice() {
103
104
		// Bailout.
105
		if ( ! $this->is_plugin_page() || $this->user_id !== $this->plugin_activate_by ) {
106
			return;
107
		}
108
109
		// If the user hasn't already dismissed the alert, output activation banner.
110
		if ( ! get_user_meta( $this->user_id, $this->nag_meta_key ) ) {
111
112
			// Output inline styles here because there's no reason
113
			// to enqueued them after the alert is dismissed.
114
			?>
115
			<style>
116
				div.give-addon-alert.updated {
117
					padding: 20px;
118
					position: relative;
119
					border-color: #66BB6A;
120
				}
121
122
				div.give-alert-message {
123
					margin-left: 70px;
124
				}
125
126
				div.give-addon-alert img.give-logo {
127
					max-width: 50px;
128
					float: left;
129
				}
130
131
				div.give-addon-alert h3 {
132
					margin: -5px 0 10px;
133
					font-size: 22px;
134
					font-weight: 300;
135
					line-height: 30px;
136
				}
137
138
				div.give-addon-alert h3 span {
139
					font-weight: 700;
140
					color: #66BB6A;
141
				}
142
143
				div.give-addon-alert .alert-actions {
144
				}
145
146
				div.give-addon-alert a {
147
					color: #66BB6A;
148
				}
149
150
				div.give-addon-alert .alert-actions a {
151
					margin-right: 2em;
152
				}
153
154
				div.give-addon-alert .alert-actions a {
155
					text-decoration: underline;
156
				}
157
158
				div.give-addon-alert .alert-actions a:hover {
159
					color: #555555;
160
				}
161
162
				div.give-addon-alert .alert-actions a span {
163
					text-decoration: none;
164
					margin-right: 5px;
165
				}
166
167
				div.give-addon-alert .dismiss {
168
					position: absolute;
169
					right: 20px;
170
					height: 100%;
171
					top: 50%;
172
					margin-top: -10px;
173
					outline: none;
174
					box-shadow: none;
175
					text-decoration: none;
176
					color: #AAA;
177
				}
178
179
				div.give-addon-alert .dismiss:hover {
180
					color: #333;
181
				}
182
			</style>
183
184
			<div class="updated give-addon-alert">
185
186
				<img src="<?php echo GIVE_PLUGIN_URL; ?>assets/images/svg/give-icon-full-circle.svg" class="give-logo"/>
187
188
				<div class="give-alert-message">
189
					<h3><?php
190
						printf(
191
						/* translators: %s: Add-on name */
192
							esc_html__( "Thank you for installing Give's %s Add-on!", 'give' ),
193
							'<span>' . $this->banner_details['name'] . '</span>'
194
						);
195
						?></h3>
196
197
					<a href="<?php
198
					//The Dismiss Button.
199
					$nag_admin_dismiss_url = 'plugins.php?' . $this->nag_meta_key . '=0';
200
					echo admin_url( $nag_admin_dismiss_url ); ?>" class="dismiss"><span
201
							class="dashicons dashicons-dismiss"></span></a>
202
203
					<div class="alert-actions">
204
205
						<?php //Point them to your settings page.
206
						if ( isset( $this->banner_details['settings_url'] ) ) { ?>
207
							<a href="<?php echo $this->banner_details['settings_url']; ?>">
208
								<span class="dashicons dashicons-admin-settings"></span><?php esc_html_e( 'Go to Settings', 'give' ); ?>
209
							</a>
210
						<?php } ?>
211
212
						<?php
213
						// Show them how to configure the Addon.
214
						if ( isset( $this->banner_details['documentation_url'] ) ) { ?>
215
							<a href="<?php echo $this->banner_details['documentation_url'] ?>" target="_blank">
216
								<span class="dashicons dashicons-media-text"></span><?php
217
								printf(
218
								/* translators: %s: Add-on name */
219
									esc_html__( 'Documentation: %s Add-on', 'give' ),
220
									$this->banner_details['name']
221
								);
222
								?></a>
223
						<?php } ?>
224
						<?php
225
						//Let them signup for plugin updates
226
						if ( isset( $this->banner_details['support_url'] ) ) { ?>
227
228
							<a href="<?php echo $this->banner_details['support_url'] ?>" target="_blank">
229
								<span class="dashicons dashicons-sos"></span><?php esc_html_e( 'Get Support', 'give' ); ?>
230
							</a>
231
232
						<?php } ?>
233
234
					</div>
235
				</div>
236
			</div>
237
			<?php
238
		}
239
	}
240
241
242
	/**
243
	 * Ignore Nag.
244
	 *
245
	 * This is the action that allows the user to dismiss the banner it basically sets a tag to their user meta data
246
	 *
247
	 * @since  1.0
248
	 * @access public
249
	 */
250
	public function give_addon_notice_ignore() {
251
252
		/**
253
		 * If user clicks to ignore the notice, add that to their user meta the banner then checks whether this tag exists already or not.
254
		 * See here: http://codex.wordpress.org/Function_Reference/add_user_meta
255
		 */
256
		if ( isset( $_GET[ $this->nag_meta_key ] ) && '0' == $_GET[ $this->nag_meta_key ] ) {
257
258
			//Get the global user
259
			$current_user = wp_get_current_user();
260
			$user_id      = $current_user->ID;
261
262
			add_user_meta( $user_id, $this->nag_meta_key, 'true', true );
263
		}
264
	}
265
266
	/**
267
	 * Setup user id to option
268
	 *
269
	 * @since  1.8
270
	 * @access private
271
	 */
272
	private function add_addon_activate_meta() {
273
		$user_id                  = get_option( $this->activate_by_meta_key );
274
		$this->plugin_activate_by = (int) $user_id;
275
276
		if ( ! $user_id ) {
277
			add_option( $this->activate_by_meta_key, $this->user_id, '', 'no' );
278
			$this->plugin_activate_by = (int) $this->user_id;
279
		}
280
	}
281
282
283
	/**
284
	 * Delete user id from option if plugin deactivated.
285
	 *
286
	 * @since  1.8
287
	 * @access public
288
	 */
289
	public function remove_addon_activate_meta() {
290
		$user_id = get_option( $this->activate_by_meta_key );
291
292
		if ( $user_id ) {
293
			delete_option( $this->activate_by_meta_key );
294
		}
295
	}
296
297
298
	/**
299
	 * Get plugin file name.
300
	 *
301
	 * @since   1.8
302
	 * @access  private
303
	 * @return mixed
304
	 */
305
	private function get_plugin_file_name() {
306
		$active_plugins = get_option( 'active_plugins' );
307
		$file_name      = '';
308
309
		try {
310
311
			// Check addon file path.
312
			if ( ! empty( $this->banner_details['file'] ) ) {
313
				$file_name = '';
314
				if ( $file_path = explode( '/plugins/', $this->banner_details['file'] ) ) {
315
					$file_path = array_pop( $file_path );
316
					$file_name = current( explode( '/', $file_path ) );
317
				}
318
319
				if ( empty( $file_name ) ) {
320
					return false;
321
				}
322
323
				foreach ( $active_plugins as $plugin ) {
324
					if ( false !== strpos( $plugin, $file_name ) ) {
325
						$file_name = $plugin;
326
						break;
327
					}
328
				}
329
			} elseif ( WP_DEBUG ) {
330
				throw new Exception( __( "File path must be added within the {$this->banner_details['name']} add-on in the banner details.", 'give' ) );
331
			}
332
333
			// Check plugin path calculated by addon file path.
334
			if ( empty( $file_name ) && WP_DEBUG ) {
335
				throw new Exception( __( "Empty add-on plugin path for {$this->banner_details['name']} add-on.", 'give' ) );
336
			}
337
338
		} catch ( Exception $e ) {
339
			echo $e->getMessage();
340
		}
341
342
		return $file_name;
343
	}
344
345
}
346