Completed
Pull Request — master (#1201)
by Ravinder
23:20
created

Give_Notices   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 189
rs 8.3999
c 0
b 0
f 0
wmc 38
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A give_admin_bar_menu() 0 16 3
F show_notices() 0 118 31
A give_admin_addons_notices() 0 4 1
A dismiss_notices() 0 7 2
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
	/**
25
	 * Get things started.
26
	 *
27
	 * @since 1.0
28
	 */
29
	public function __construct() {
30
		add_action( 'admin_notices', array( $this, 'show_notices' ) );
31
		add_action( 'give_dismiss_notices', array( $this, 'dismiss_notices' ) );
32
		add_action( 'admin_bar_menu', array( $this, 'give_admin_bar_menu' ), 1000, 1 );
33
	}
34
35
36
	/**
37
	 * Display admin bar when active.
38
	 *
39
	 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference.
40
	 *
41
	 * @return bool
42
	 */
43
	public function give_admin_bar_menu( $wp_admin_bar ) {
44
45
		if ( ! give_is_test_mode() || ! current_user_can( 'view_give_reports' ) ) {
46
			return false;
47
		}
48
49
		// Add the main siteadmin menu item.
50
		$wp_admin_bar->add_menu( array(
51
			'id'     => 'give-test-notice',
52
			'href'   => admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=gateways' ),
53
			'parent' => 'top-secondary',
54
			'title'  => esc_html__( 'Give Test Mode Active', 'give' ),
55
			'meta'   => array( 'class' => 'give-test-mode-active' ),
56
		) );
57
58
	}
59
60
	/**
61
	 * Show relevant notices.
62
	 *
63
	 * @since 1.0
64
	 */
65
	public function show_notices() {
66
		$notices = array(
67
			'updated' => array(),
68
			'error'   => array(),
69
		);
70
71
		if ( ! give_test_ajax_works() && ! get_user_meta( get_current_user_id(), '_give_admin_ajax_inaccessible_dismissed', true ) && current_user_can( 'manage_give_settings' ) ) {
72
			echo '<div class="error">';
73
			echo '<p>' . esc_html__( 'Your site appears to be blocking the WordPress ajax interface. This may cause issues with Give.', 'give' ) . '</p>';
74
			/* translators: %s: https://givewp.com/documentation/core/troubleshooting/admin-ajax-blocked/ */
75
			echo '<p>' . sprintf( __( 'Please see <a href="%s" target="_blank">this reference</a> for possible solutions.', 'give' ), esc_url( 'https://givewp.com/documentation/core/troubleshooting/admin-ajax-blocked/' ) ) . '</p>';
76
			echo '<p><a href="' . add_query_arg( array(
77
					'give_action' => 'dismiss_notices',
78
					'give_notice' => 'admin_ajax_inaccessible',
79
			) ) . '">' . esc_html__( 'Dismiss Notice', 'give' ) . '</a></p>';
80
			echo '</div>';
81
		}
82
83
		if ( isset( $_GET['give-message'] ) ) {
84
85
			// Donation reports errors.
86
			if ( current_user_can( 'view_give_reports' ) ) {
87
				switch ( $_GET['give-message'] ) {
88
					case 'donation_deleted' :
89
						$notices['updated']['give-donation-deleted'] = esc_attr__( 'The donation has been deleted.', 'give' );
90
						break;
91
					case 'email_sent' :
92
						$notices['updated']['give-payment-sent'] = esc_attr__( 'The donation receipt has been resent.', 'give' );
93
						break;
94
					case 'refreshed-reports' :
95
						$notices['updated']['give-refreshed-reports'] = esc_attr__( 'The reports cache has been cleared.', 'give' );
96
						break;
97
					case 'donation-note-deleted' :
98
						$notices['updated']['give-donation-note-deleted'] = esc_attr__( 'The donation note has been deleted.', 'give' );
99
						break;
100
				}
101
			}
102
103
			// Give settings notices and errors.
104
			if ( current_user_can( 'manage_give_settings' ) ) {
105
				switch ( $_GET['give-message'] ) {
106
					case 'settings-imported' :
107
						$notices['updated']['give-settings-imported'] = esc_attr__( 'The settings have been imported.', 'give' );
108
						break;
109
					case 'api-key-generated' :
110
						$notices['updated']['give-api-key-generated'] = esc_attr__( 'API keys have been generated.', 'give' );
111
						break;
112
					case 'api-key-exists' :
113
						$notices['error']['give-api-key-exists'] = esc_attr__( 'The specified user already has API keys.', 'give' );
114
						break;
115
					case 'api-key-regenerated' :
116
						$notices['updated']['give-api-key-regenerated'] = esc_attr__( 'API keys have been regenerated.', 'give' );
117
						break;
118
					case 'api-key-revoked' :
119
						$notices['updated']['give-api-key-revoked'] = esc_attr__( 'API keys have been revoked.', 'give' );
120
						break;
121
					case 'sent-test-email' :
122
						$notices['updated']['give-sent-test-email'] = esc_attr__( 'The test email has been sent.', 'give' );
123
						break;
124
				}
125
			}
126
			// Payments errors.
127
			if ( current_user_can( 'edit_give_payments' ) ) {
128
				switch ( $_GET['give-message'] ) {
129
					case 'note-added' :
130
						$notices['updated']['give-note-added'] = esc_attr__( 'The donation note has been added.', 'give' );
131
						break;
132
					case 'payment-updated' :
133
						$notices['updated']['give-payment-updated'] = esc_attr__( 'The donation has been updated.', 'give' );
134
						break;
135
				}
136
			}
137
138
			// Customer Notices.
139
			if ( current_user_can( 'edit_give_payments' ) ) {
140
				switch ( $_GET['give-message'] ) {
141
					case 'customer-deleted' :
142
						$notices['updated']['give-customer-deleted'] = esc_attr__( 'The donor has been deleted.', 'give' );
143
						break;
144
145
					case 'email-added' :
146
						$notices['updated']['give-customer-email-added'] = esc_attr__( 'Donor email added', 'give' );
147
						break;
148
149
					case 'email-removed' :
150
						$notices['updated']['give-customer-email-removed'] = esc_attr__( 'Donor email removed', 'give' );
151
						break;
152
153
					case 'email-remove-failed' :
154
						$notices['error']['give-customer-email-remove-failed'] = esc_attr__( 'Failed to remove donor email', 'give' );
155
						break;
156
157
					case 'primary-email-updated' :
158
						$notices['updated']['give-customer-primary-email-updated'] = esc_attr__( 'Primary email updated for donors', 'give' );
159
						break;
160
161
					case 'primary-email-failed' :
162
						$notices['error']['give-customer-primary-email-failed'] = esc_attr__( 'Failed to set primary email', 'give' );
163
164
				}
165
			}
166
		}
167
168
		if ( count( $notices['updated'] ) > 0 ) {
169
			foreach ( $notices['updated'] as $notice => $message ) {
170
				add_settings_error( 'give-notices', $notice, $message, 'updated' );
171
			}
172
		}
173
174
		if ( count( $notices['error'] ) > 0 ) {
175
			foreach ( $notices['error'] as $notice => $message ) {
176
				add_settings_error( 'give-notices', $notice, $message, 'error' );
177
			}
178
		}
179
180
		settings_errors( 'give-notices' );
181
182
	}
183
184
185
	/**
186
	 * Admin Add-ons Notices.
187
	 *
188
	 * @since 1.0
189
	 * @return void
190
	 */
191
	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...
192
		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' );
193
		settings_errors( 'give-notices' );
194
	}
195
196
197
	/**
198
	 * Dismiss admin notices when Dismiss links are clicked.
199
	 *
200
	 * @since 1.0
201
	 * @return void
202
	 */
203
	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...
204
		if ( isset( $_GET['give_notice'] ) ) {
205
			update_user_meta( get_current_user_id(), '_give_' . $_GET['give_notice'] . '_dismissed', 1 );
206
			wp_redirect( remove_query_arg( array( 'give_action', 'give_notice' ) ) );
207
			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...
208
		}
209
	}
210
}
211
212
new Give_Notices();
213