Test Failed
Push — master ( 3995a3...673dde )
by Devin
05:40 queued 01:04
created

Give_Addon_Activation_Banner::print_css()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 72
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 72
rs 9.102
c 0
b 0
f 0

How to fix   Long Method   

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
 * 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;
39
		$this->banner_details       = $_banner_details;
40
		$this->test_mode            = ( $this->banner_details['testing'] == 'true' ) ? true : false;
41
		$this->nag_meta_key         = 'give_addon_activation_ignore_' . sanitize_title( $this->banner_details['name'] );
42
		$this->activate_by_meta_key = 'give_addon_' . sanitize_title( $this->banner_details['name'] ) . '_active_by_user';
43
44
		//Get current user
45
		$this->user_id = $current_user->ID;
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 );
0 ignored issues
show
introduced by
delete_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
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 ) ) {
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
111
112
			$this->print_css();
0 ignored issues
show
Unused Code introduced by
The call to the method Give_Addon_Activation_Banner::print_css() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
113
114
			// Output inline styles here because there's no reason
115
			// to enqueued them after the alert is dismissed.
116
117
			ob_start();
118
			?>
119
120
			<div class="updated give-addon-alert give-notice" style="display: none">
121
122
				<img src="<?php echo GIVE_PLUGIN_URL; ?>assets/images/svg/give-icon-full-circle.svg" class="give-logo"/>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'GIVE_PLUGIN_URL'
Loading history...
123
124
				<div class="give-alert-message">
125
					<h3><?php
126
						printf(
127
						/* translators: %s: Add-on name */
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 28 spaces, but found 24.
Loading history...
128
							esc_html__( "Thank you for installing Give's %s Add-on!", 'give' ),
129
							'<span>' . $this->banner_details['name'] . '</span>'
130
						);
131
						?></h3>
132
133
					<a href="<?php
134
					//The Dismiss Button.
135
					$nag_admin_dismiss_url = 'plugins.php?' . $this->nag_meta_key . '=0';
136
					echo admin_url( $nag_admin_dismiss_url ); ?>" class="dismiss"><span
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'admin_url'
Loading history...
137
							class="dashicons dashicons-dismiss"></span></a>
138
139
					<div class="alert-actions">
140
141
						<?php //Point them to your settings page.
142
						if ( isset( $this->banner_details['settings_url'] ) ) { ?>
143
							<a href="<?php echo $this->banner_details['settings_url']; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
144
								<span class="dashicons dashicons-admin-settings"></span><?php esc_html_e( 'Go to Settings', 'give' ); ?>
145
							</a>
146
						<?php } ?>
147
148
						<?php
149
						// Show them how to configure the Addon.
150
						if ( isset( $this->banner_details['documentation_url'] ) ) { ?>
151
							<a href="<?php echo $this->banner_details['documentation_url'] ?>" target="_blank">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
152
								<span class="dashicons dashicons-media-text"></span><?php
153
								printf(
154
								/* translators: %s: Add-on name */
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 36 spaces, but found 32.
Loading history...
155
									esc_html__( 'Documentation: %s Add-on', 'give' ),
156
									$this->banner_details['name']
157
								);
158
								?></a>
159
						<?php } ?>
160
						<?php
161
						//Let them signup for plugin updates
162
						if ( isset( $this->banner_details['support_url'] ) ) { ?>
163
164
							<a href="<?php echo $this->banner_details['support_url'] ?>" target="_blank">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
165
								<span class="dashicons dashicons-sos"></span><?php esc_html_e( 'Get Support', 'give' ); ?>
166
							</a>
167
168
						<?php } ?>
169
170
					</div>
171
				</div>
172
			</div>
173
			<?php
174
175
			$notice_html = ob_get_clean();
176
			
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
177
			
178
			// Register notice.
179
			Give()->notices->register_notice( array(
180
				'id'          => 'give_' . sanitize_title( $this->banner_details['name'] ) . '_notice',
181
				'type'        => 'updated',
182
				'description_html' => $notice_html,
183
				'show'        => true,
184
			) );
185
		}
186
	}
187
188
189
	/**
190
	 * Add activation banner css.
191
	 *
192
	 * @since 1.8.16
193
	 * @access private
194
	 */
195
	private function print_css(){
196
		?>
197
		<style>
198
			div.give-addon-alert.updated {
199
				padding: 20px;
200
				position: relative;
201
				border-color: #66BB6A;
202
			}
203
204
			div.give-alert-message {
205
				margin-left: 70px;
206
			}
207
208
			div.give-addon-alert img.give-logo {
209
				max-width: 50px;
210
				float: left;
211
			}
212
213
			div.give-addon-alert h3 {
214
				margin: -5px 0 10px;
215
				font-size: 22px;
216
				font-weight: 300;
217
				line-height: 30px;
218
			}
219
220
			div.give-addon-alert h3 span {
221
				font-weight: 700;
222
				color: #66BB6A;
223
			}
224
225
			div.give-addon-alert .alert-actions {
226
			}
227
228
			div.give-addon-alert a {
229
				color: #66BB6A;
230
			}
231
232
			div.give-addon-alert .alert-actions a {
233
				margin-right: 2em;
234
			}
235
236
			div.give-addon-alert .alert-actions a {
237
				text-decoration: underline;
238
			}
239
240
			div.give-addon-alert .alert-actions a:hover {
241
				color: #555555;
242
			}
243
244
			div.give-addon-alert .alert-actions a span {
245
				text-decoration: none;
246
				margin-right: 5px;
247
			}
248
249
			div.give-addon-alert .dismiss {
250
				position: absolute;
251
				right: 20px;
252
				height: 100%;
253
				top: 50%;
254
				margin-top: -10px;
255
				outline: none;
256
				box-shadow: none;
257
				text-decoration: none;
258
				color: #AAA;
259
			}
260
261
			div.give-addon-alert .dismiss:hover {
262
				color: #333;
263
			}
264
		</style>
265
		<?php
266
	}
267
268
269
	/**
270
	 * Ignore Nag.
271
	 *
272
	 * This is the action that allows the user to dismiss the banner it basically sets a tag to their user meta data
273
	 *
274
	 * @since  1.0
275
	 * @access public
276
	 */
277
	public function give_addon_notice_ignore() {
278
279
		/**
280
		 * If user clicks to ignore the notice, add that to their user meta the banner then checks whether this tag exists already or not.
281
		 * See here: http://codex.wordpress.org/Function_Reference/add_user_meta
282
		 */
283
		if ( isset( $_GET[ $this->nag_meta_key ] ) && '0' == $_GET[ $this->nag_meta_key ] ) {
284
285
			//Get the global user
286
			$current_user = wp_get_current_user();
287
			$user_id      = $current_user->ID;
288
289
			add_user_meta( $user_id, $this->nag_meta_key, 'true', true );
0 ignored issues
show
introduced by
add_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
290
		}
291
	}
292
293
	/**
294
	 * Setup user id to option
295
	 *
296
	 * @since  1.8
297
	 * @access private
298
	 */
299
	private function add_addon_activate_meta() {
300
		$user_id                  = get_option( $this->activate_by_meta_key );
301
		$this->plugin_activate_by = (int) $user_id;
302
303
		if ( ! $user_id ) {
304
			add_option( $this->activate_by_meta_key, $this->user_id, '', 'no' );
305
			$this->plugin_activate_by = (int) $this->user_id;
306
		}
307
	}
308
309
310
	/**
311
	 * Delete user id from option if plugin deactivated.
312
	 *
313
	 * @since  1.8
314
	 * @access public
315
	 */
316
	public function remove_addon_activate_meta() {
317
		$user_id = get_option( $this->activate_by_meta_key );
318
319
		if ( $user_id ) {
320
			delete_option( $this->activate_by_meta_key );
321
		}
322
	}
323
324
325
	/**
326
	 * Get plugin file name.
327
	 *
328
	 * @since   1.8
329
	 * @access  private
330
	 * @return mixed
331
	 */
332
	private function get_plugin_file_name() {
333
		$active_plugins = get_option( 'active_plugins' );
334
		$file_name      = '';
335
336
		try {
337
338
			// Check addon file path.
339
			if ( ! empty( $this->banner_details['file'] ) ) {
340
				$file_name = '';
341
				if ( $file_path = explode( '/plugins/', $this->banner_details['file'] ) ) {
342
					$file_path = array_pop( $file_path );
343
					$file_name = current( explode( '/', $file_path ) );
344
				}
345
346
				if ( empty( $file_name ) ) {
347
					return false;
348
				}
349
350
				foreach ( $active_plugins as $plugin ) {
351
					if ( false !== strpos( $plugin, $file_name ) ) {
352
						$file_name = $plugin;
353
						break;
354
					}
355
				}
356
			} elseif ( WP_DEBUG ) {
357
				throw new Exception( __( "File path must be added within the {$this->banner_details['name']} add-on in the banner details.", 'give' ) );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '"File path must be added within the {$this->banner_details['name']} add-on in the banner details."'
Loading history...
358
			}
359
360
			// Check plugin path calculated by addon file path.
361
			if ( empty( $file_name ) && WP_DEBUG ) {
362
				throw new Exception( __( "Empty add-on plugin path for {$this->banner_details['name']} add-on.", 'give' ) );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '"Empty add-on plugin path for {$this->banner_details['name']} add-on."'
Loading history...
363
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
364
365
		} catch ( Exception $e ) {
366
			echo $e->getMessage();
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$e'
Loading history...
367
		}
368
369
		return $file_name;
370
	}
371
372
}
373