Completed
Push — issues/611 ( 661115...758b1c )
by Ravinder
21:11
created

includes/admin/class-admin-notices.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
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
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() {
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() {
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;
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() {
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