Conditions | 31 |
Paths | 12296 |
Total Lines | 120 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
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.