Completed
Push — master ( 0af146...0e0d02 )
by Devin
13s
created

Give_Notices::add_payment_bulk_action_notice()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 18
nc 11
nop 0
dl 0
loc 27
rs 5.2653
c 0
b 0
f 0

How to fix   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
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 22 and the first side effect is on line 14.

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
 * Admin Notices Class.
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Notices
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Give_Notices Class
19
 *
20
 * @since 1.0
21
 */
22
class Give_Notices {
23
	/**
24
	 * List of notices
25
	 * @var array
26
	 * @since 1.8
27
	 */
28
	private $notices;
29
30
	/**
31
	 * Get things started.
32
	 *
33
	 * @since 1.0
34
	 */
35
	public function __construct() {
36
		add_action( 'admin_notices', array( $this, 'show_notices' ) );
37
		add_action( 'give_dismiss_notices', array( $this, 'dismiss_notices' ) );
38
		add_action( 'admin_bar_menu', array( $this, 'give_admin_bar_menu' ), 1000, 1 );
39
40
		$this->notices = array(
41
			'updated' => array(),
42
			'error'   => array(),
43
		);
44
	}
45
46
47
	/**
48
	 * Display admin bar when active.
49
	 *
50
	 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference.
51
	 *
52
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
53
	 */
54
	public function give_admin_bar_menu( $wp_admin_bar ) {
55
56
		if ( ! give_is_test_mode() || ! current_user_can( 'view_give_reports' ) ) {
57
			return false;
58
		}
59
60
		// Add the main siteadmin menu item.
61
		$wp_admin_bar->add_menu( array(
62
			'id'     => 'give-test-notice',
63
			'href'   => admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=gateways' ),
64
			'parent' => 'top-secondary',
65
			'title'  => esc_html__( 'Give Test Mode Active', 'give' ),
66
			'meta'   => array( 'class' => 'give-test-mode-active' ),
67
		) );
68
69
	}
70
71
	/**
72
	 * Show relevant notices.
73
	 *
74
	 * @since 1.0
75
	 */
76
	public function show_notices() {
77
78
		if ( ! give_test_ajax_works() && ! get_user_meta( get_current_user_id(), '_give_admin_ajax_inaccessible_dismissed', true ) && current_user_can( 'manage_give_settings' ) ) {
79
			echo '<div class="error">';
80
			echo '<p>' . esc_html__( 'Your site appears to be blocking the WordPress ajax interface. This may cause issues with Give.', 'give' ) . '</p>';
81
			/* translators: %s: http://docs.givewp.com/ajax-blocked */
82
			echo '<p>' . sprintf( __( 'Please see <a href="%s" target="_blank">this reference</a> for possible solutions.', 'give' ), esc_url( 'http://docs.givewp.com/ajax-blocked' ) ) . '</p>';
83
			echo '<p><a href="' . add_query_arg( array(
84
					'give_action' => 'dismiss_notices',
85
					'give_notice' => 'admin_ajax_inaccessible',
86
				) ) . '">' . esc_html__( 'Dismiss Notice', 'give' ) . '</a></p>';
87
			echo '</div>';
88
		}
89
90
		if ( isset( $_GET['give-message'] ) ) {
91
92
			// Donation reports errors.
93
			if ( current_user_can( 'view_give_reports' ) ) {
94
				switch ( $_GET['give-message'] ) {
95
					case 'donation_deleted' :
96
						$this->notices['updated']['give-donation-deleted'] = esc_attr__( 'The donation has been deleted.', 'give' );
97
						break;
98
					case 'email_sent' :
99
						$this->notices['updated']['give-payment-sent'] = esc_attr__( 'The donation receipt has been resent.', 'give' );
100
						break;
101
					case 'refreshed-reports' :
102
						$this->notices['updated']['give-refreshed-reports'] = esc_attr__( 'The reports cache has been cleared.', 'give' );
103
						break;
104
					case 'donation-note-deleted' :
105
						$this->notices['updated']['give-donation-note-deleted'] = esc_attr__( 'The donation note has been deleted.', 'give' );
106
						break;
107
				}
108
			}
109
110
			// Give settings notices and errors.
111
			if ( current_user_can( 'manage_give_settings' ) ) {
112
				switch ( $_GET['give-message'] ) {
113
					case 'settings-imported' :
114
						$this->notices['updated']['give-settings-imported'] = esc_attr__( 'The settings have been imported.', 'give' );
115
						break;
116
					case 'api-key-generated' :
117
						$this->notices['updated']['give-api-key-generated'] = esc_attr__( 'API keys have been generated.', 'give' );
118
						break;
119
					case 'api-key-exists' :
120
						$this->notices['error']['give-api-key-exists'] = esc_attr__( 'The specified user already has API keys.', 'give' );
121
						break;
122
					case 'api-key-regenerated' :
123
						$this->notices['updated']['give-api-key-regenerated'] = esc_attr__( 'API keys have been regenerated.', 'give' );
124
						break;
125
					case 'api-key-revoked' :
126
						$this->notices['updated']['give-api-key-revoked'] = esc_attr__( 'API keys have been revoked.', 'give' );
127
						break;
128
					case 'sent-test-email' :
129
						$this->notices['updated']['give-sent-test-email'] = esc_attr__( 'The test email has been sent.', 'give' );
130
						break;
131
					case 'matched-success-failure-page':
132
						$this->notices['updated']['give-matched-success-failure-page'] = esc_html__( 'You cannot set the success and failed pages to the same page', 'give' );
133
				}
134
			}
135
			// Payments errors.
136
			if ( current_user_can( 'edit_give_payments' ) ) {
137
				switch ( $_GET['give-message'] ) {
138
					case 'note-added' :
139
						$this->notices['updated']['give-note-added'] = esc_attr__( 'The donation note has been added.', 'give' );
140
						break;
141
					case 'payment-updated' :
142
						$this->notices['updated']['give-payment-updated'] = esc_attr__( 'The donation has been updated.', 'give' );
143
						break;
144
				}
145
			}
146
147
			// Customer Notices.
148
			if ( current_user_can( 'edit_give_payments' ) ) {
149
				switch ( $_GET['give-message'] ) {
150
					case 'customer-deleted' :
151
						$this->notices['updated']['give-customer-deleted'] = esc_attr__( 'The donor has been deleted.', 'give' );
152
						break;
153
154
					case 'email-added' :
155
						$this->notices['updated']['give-customer-email-added'] = esc_attr__( 'Donor email added', 'give' );
156
						break;
157
158
					case 'email-removed' :
159
						$this->notices['updated']['give-customer-email-removed'] = esc_attr__( 'Donor email removed', 'give' );
160
						break;
161
162
					case 'email-remove-failed' :
163
						$this->notices['error']['give-customer-email-remove-failed'] = esc_attr__( 'Failed to remove donor email', 'give' );
164
						break;
165
166
					case 'primary-email-updated' :
167
						$this->notices['updated']['give-customer-primary-email-updated'] = esc_attr__( 'Primary email updated for donor', 'give' );
168
						break;
169
170
					case 'primary-email-failed' :
171
						$this->notices['error']['give-customer-primary-email-failed'] = esc_attr__( 'Failed to set primary email', 'give' );
172
173
				}
174
			}
175
		}
176
177
		$this->add_payment_bulk_action_notice();
178
179
		if ( count( $this->notices['updated'] ) > 0 ) {
180
			foreach ( $this->notices['updated'] as $notice => $message ) {
181
				add_settings_error( 'give-notices', $notice, $message, 'updated' );
182
			}
183
		}
184
185
		if ( count( $this->notices['error'] ) > 0 ) {
186
			foreach ( $this->notices['error'] as $notice => $message ) {
187
				add_settings_error( 'give-notices', $notice, $message, 'error' );
188
			}
189
		}
190
191
		settings_errors( 'give-notices' );
192
193
	}
194
195
196
	/**
197
	 * Admin Add-ons Notices.
198
	 *
199
	 * @since 1.0
200
	 * @return void
201
	 */
202
	function give_admin_addons_notices() {
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...
203
		add_settings_error( 'give-notices', 'give-addons-feed-error', esc_attr__( 'There seems to be an issue with the server. Please try again in a few minutes.', 'give' ), 'error' );
204
		settings_errors( 'give-notices' );
205
	}
206
207
208
	/**
209
	 * Dismiss admin notices when Dismiss links are clicked.
210
	 *
211
	 * @since 1.0
212
	 * @return void
213
	 */
214
	function dismiss_notices() {
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...
215
		if ( isset( $_GET['give_notice'] ) ) {
216
			update_user_meta( get_current_user_id(), '_give_' . $_GET['give_notice'] . '_dismissed', 1 );
217
			wp_redirect( remove_query_arg( array( 'give_action', 'give_notice' ) ) );
218
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method dismiss_notices() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
219
		}
220
	}
221
222
223
	/**
224
	 * Add payment bulk notice.
225
	 *
226
	 * @since 1.8
227
	 *
228
	 * @return array
229
	 */
230
	function add_payment_bulk_action_notice() {
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...
231
		if (
232
			current_user_can( 'edit_give_payments' )
233
			&& isset( $_GET['action'] )
234
			&& ! empty( $_GET['action'] )
235
			&& isset( $_GET['payment'] )
236
			&& ! empty( $_GET['payment'] )
237
		) {
238
			$payment_count = isset( $_GET['payment'] ) ? count( $_GET['payment'] ) : 0;
239
240
			switch ( $_GET['action'] ) {
241
				case 'delete':
242
					if ( $payment_count ) {
243
						$this->notices['updated']['bulk_action_delete'] = sprintf( _n( 'Successfully deleted only one transaction.', 'Successfully deleted %d number of transactions.', $payment_count, 'give' ), $payment_count );
244
					}
245
					break;
246
247
				case 'resend-receipt':
248
					if ( $payment_count ) {
249
						$this->notices['updated']['bulk_action_resend_receipt'] = sprintf( _n( 'Successfully send email receipt to only one recipient.', 'Successfully send email receipts to %d recipients.', $payment_count, 'give' ), $payment_count );
250
					}
251
					break;
252
			}
253
		}
254
255
		return $this->notices;
256
	}
257
258
259
	/**
260
	 * Get give style admin notice.
261
	 *
262
	 * @since  1.8
263
	 * @access public
264
	 *
265
	 * @param string $message
266
	 * @param string $type
267
	 *
268
	 * @return string
269
	 */
270
	public static function notice_html( $message, $type = 'updated' ) {
271
		ob_start();
272
		?>
273
		<div class="<?php echo $type; ?> notice">
274
			<p><?php echo $message; ?></p>
275
		</div>
276
		<?php
277
278
		return ob_get_clean();
279
	}
280
}
281
282
new Give_Notices();
283