1
|
|
|
<?php |
|
|
|
|
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() { |
|
|
|
|
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() { |
|
|
|
|
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; |
|
|
|
|
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
new Give_Notices(); |
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.