Completed
Push — issues/611 ( df05ad...753a69 )
by Ravinder
32:37 queued 11:53
created

get_plugin_file_name()   C

Complexity

Conditions 7
Paths 23

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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