Completed
Pull Request — master (#1055)
by Rami
19:01
created

Give_Notices   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 191
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 120 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     http://opensource.org/licenses/gpl-2.0.php 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_attr__( 'Dismiss Notice', 'give' ) . '</a></p>';
80
			echo '</div>';
81
		}
82
83
84
		if ( isset( $_GET['give-message'] ) ) {
85
86
			// Donation reports errors.
87
			if ( current_user_can( 'view_give_reports' ) ) {
88
				switch ( $_GET['give-message'] ) {
89
					case 'payment_deleted' :
90
						$notices['updated']['give-payment-deleted'] = esc_attr__( 'The donation has been deleted.', 'give' );
91
						break;
92
					case 'email_sent' :
93
						$notices['updated']['give-payment-sent'] = esc_attr__( 'The donation receipt has been resent.', 'give' );
94
						break;
95
					case 'refreshed-reports' :
96
						$notices['updated']['give-refreshed-reports'] = esc_attr__( 'The reports cache has been cleared.', 'give' );
97
						break;
98
					case 'payment-note-deleted' :
99
						$notices['updated']['give-payment-note-deleted'] = esc_attr__( 'The donation note has been deleted.', 'give' );
100
						break;
101
				}
102
			}
103
104
			// Give settings notices and errors.
105
			if ( current_user_can( 'manage_give_settings' ) ) {
106
				switch ( $_GET['give-message'] ) {
107
					case 'settings-imported' :
108
						$notices['updated']['give-settings-imported'] = esc_attr__( 'The settings have been imported.', 'give' );
109
						break;
110
					case 'api-key-generated' :
111
						$notices['updated']['give-api-key-generated'] = esc_attr__( 'API keys have been generated.', 'give' );
112
						break;
113
					case 'api-key-exists' :
114
						$notices['error']['give-api-key-exists'] = esc_attr__( 'The specified user already has API keys.', 'give' );
115
						break;
116
					case 'api-key-regenerated' :
117
						$notices['updated']['give-api-key-regenerated'] = esc_attr__( 'API keys have been regenerated.', 'give' );
118
						break;
119
					case 'api-key-revoked' :
120
						$notices['updated']['give-api-key-revoked'] = esc_attr__( 'API keys have been revoked.', 'give' );
121
						break;
122
					case 'sent-test-email' :
123
						$notices['updated']['give-sent-test-email'] = esc_attr__( 'The test email has been sent.', 'give' );
124
						break;
125
				}
126
			}
127
			// Payments errors.
128
			if ( current_user_can( 'edit_give_payments' ) ) {
129
				switch ( $_GET['give-message'] ) {
130
					case 'note-added' :
131
						$notices['updated']['give-note-added'] = esc_attr__( 'The donation note has been added.', 'give' );
132
						break;
133
					case 'payment-updated' :
134
						$notices['updated']['give-payment-updated'] = esc_attr__( 'The donation has been updated.', 'give' );
135
						break;
136
				}
137
			}
138
139
			// Customer Notices.
140
			if ( current_user_can( 'edit_give_payments' ) ) {
141
				switch ( $_GET['give-message'] ) {
142
					case 'customer-deleted' :
143
						$notices['updated']['give-customer-deleted'] = esc_attr__( 'The donor has been deleted.', 'give' );
144
						break;
145
146
					case 'email-added' :
147
						$notices['updated']['give-customer-email-added'] = __( 'Donor email added', 'give' );
148
						break;
149
150
					case 'email-removed' :
151
						$notices['updated']['give-customer-email-removed'] = __( 'Donor email removed', 'give');
152
						break;
153
154
					case 'email-remove-failed' :
155
						$notices['error']['give-customer-email-remove-failed'] = __( 'Failed to remove donor email', 'give');
156
						break;
157
158
					case 'primary-email-updated' :
159
						$notices['updated']['give-customer-primary-email-updated'] = __( 'Primary email updated for donors', 'give');
160
						break;
161
162
					case 'primary-email-failed' :
163
						$notices['error']['give-customer-primary-email-failed'] = __( 'Failed to set primary email', 'give');
164
165
				}
166
			}
167
168
		}
169
170
		if ( count( $notices['updated'] ) > 0 ) {
171
			foreach ( $notices['updated'] as $notice => $message ) {
172
				add_settings_error( 'give-notices', $notice, $message, 'updated' );
173
			}
174
		}
175
176
		if ( count( $notices['error'] ) > 0 ) {
177
			foreach ( $notices['error'] as $notice => $message ) {
178
				add_settings_error( 'give-notices', $notice, $message, 'error' );
179
			}
180
		}
181
182
		settings_errors( 'give-notices' );
183
184
	}
185
186
187
	/**
188
	 * Admin Add-ons Notices.
189
	 *
190
	 * @since 1.0
191
	 * @return void
192
	 */
193
	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...
194
		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' );
195
		settings_errors( 'give-notices' );
196
	}
197
198
199
	/**
200
	 * Dismiss admin notices when Dismiss links are clicked.
201
	 *
202
	 * @since 1.0
203
	 * @return void
204
	 */
205
	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...
206
		if ( isset( $_GET['give_notice'] ) ) {
207
			update_user_meta( get_current_user_id(), '_give_' . $_GET['give_notice'] . '_dismissed', 1 );
208
			wp_redirect( remove_query_arg( array( 'give_action', 'give_notice' ) ) );
209
			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...
210
		}
211
	}
212
}
213
214
new Give_Notices();