Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
14 | class Give_Scripts { |
||
15 | |||
16 | /** |
||
17 | * Whether RTL or not. |
||
18 | * |
||
19 | * @since 2.1.0 |
||
20 | * @var string |
||
21 | * @access private |
||
22 | */ |
||
23 | private $direction; |
||
24 | |||
25 | /** |
||
26 | * Whether scripts should be loaded in the footer or not. |
||
27 | * |
||
28 | * @since 2.1.0 |
||
29 | * @var bool |
||
30 | * @access private |
||
31 | */ |
||
32 | private $scripts_footer; |
||
33 | |||
34 | /** |
||
35 | * Instantiates the Assets class. |
||
36 | * |
||
37 | * @since 2.1.0 |
||
38 | */ |
||
39 | public function __construct() { |
||
44 | |||
45 | /** |
||
46 | * Fires off hooks to register assets in WordPress. |
||
47 | * |
||
48 | * @since 2.1.0 |
||
49 | */ |
||
50 | public function init() { |
||
68 | |||
69 | /** |
||
70 | * Registers all plugin styles. |
||
71 | * |
||
72 | * @since 2.1.0 |
||
73 | */ |
||
74 | public function register_styles() { |
||
92 | |||
93 | /** |
||
94 | * Registers all plugin scripts. |
||
95 | * |
||
96 | * @since 2.1.0 |
||
97 | */ |
||
98 | public function register_scripts() { |
||
119 | |||
120 | /** |
||
121 | * Enqueues admin styles. |
||
122 | * |
||
123 | * @since 2.1.0 |
||
124 | * |
||
125 | * @param string $hook Page hook. |
||
126 | */ |
||
127 | public function admin_enqueue_styles( $hook ) { |
||
142 | |||
143 | /** |
||
144 | * Enqueues admin scripts. |
||
145 | * |
||
146 | * @since 2.1.0 |
||
147 | * |
||
148 | * @param string $hook Page hook. |
||
149 | */ |
||
150 | public function admin_enqueue_scripts( $hook ) { |
||
175 | |||
176 | /** |
||
177 | * Load admin plugin page related scripts, styles andd localize param |
||
178 | * |
||
179 | * @since 2.2.0 |
||
180 | * @access private |
||
181 | */ |
||
182 | private function plugin_equeue_scripts() { |
||
183 | wp_enqueue_style( 'plugin-deactivation-survey-css' ); |
||
184 | wp_enqueue_script( 'plugin-deactivation-survey-js' ); |
||
185 | |||
186 | $localized_data = array( |
||
187 | 'nonce' => wp_create_nonce( 'deactivation_survey_nonce' ), |
||
188 | 'cancel' => __( 'Cancel', 'give' ), |
||
189 | 'deactivation_no_option_selected' => __( 'Error: Please select at least one option.', 'give' ), |
||
190 | 'submit_and_deactivate' => __( 'Submit and Deactivate', 'give' ), |
||
191 | 'skip_and_deactivate' => __( 'Skip & Deactivate', 'give' ), |
||
192 | 'please_fill_field' => __( 'Error: Please fill the field.', 'give' ), |
||
193 | |||
194 | ); |
||
195 | |||
196 | wp_localize_script( 'plugin-deactivation-survey-js', 'give_vars', $localized_data ); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Localize admin scripts. |
||
201 | */ |
||
202 | public function admin_localize_scripts() { |
||
203 | |||
204 | global $post, $pagenow; |
||
205 | $give_options = give_get_settings(); |
||
206 | |||
207 | // Price Separators. |
||
208 | $thousand_separator = give_get_price_thousand_separator(); |
||
209 | $decimal_separator = give_get_price_decimal_separator(); |
||
210 | $number_decimals = give_get_price_decimals(); |
||
211 | |||
212 | // Localize strings & variables for JS. |
||
213 | $localized_data = array( |
||
214 | 'post_id' => isset( $post->ID ) ? $post->ID : null, |
||
215 | 'give_version' => GIVE_VERSION, |
||
216 | 'thousands_separator' => $thousand_separator, |
||
217 | 'decimal_separator' => $decimal_separator, |
||
218 | 'number_decimals' => $number_decimals, // Use this for number of decimals instead of `currency_decimals`. |
||
219 | 'currency_decimals' => $number_decimals, // If you find usage of this variable then replace it with `number_decimals`. |
||
220 | 'currency_sign' => give_currency_filter( '' ), |
||
221 | 'currency_pos' => isset( $give_options['currency_position'] ) ? $give_options['currency_position'] : 'before', |
||
222 | 'quick_edit_warning' => __( 'Not available for variable priced forms.', 'give' ), |
||
223 | 'delete_payment' => __( 'Are you sure you want to <strong>permanently</strong> delete this donation?', 'give' ), |
||
224 | 'delete_payment_note' => __( 'Are you sure you want to delete this note?', 'give' ), |
||
225 | 'revoke_api_key' => __( 'Are you sure you want to revoke this API key?', 'give' ), |
||
226 | 'regenerate_api_key' => __( 'Are you sure you want to regenerate this API key?', 'give' ), |
||
227 | 'resend_receipt' => __( 'Are you sure you want to resend the donation receipt?', 'give' ), |
||
228 | 'disconnect_user' => __( 'Are you sure you want to disconnect the user from this donor?', 'give' ), |
||
229 | 'one_option' => __( 'Choose a form', 'give' ), |
||
230 | 'one_or_more_option' => __( 'Choose one or more forms', 'give' ), |
||
231 | 'ok' => __( 'Ok', 'give' ), |
||
232 | 'cancel' => __( 'Cancel', 'give' ), |
||
233 | 'success' => __( 'Success', 'give' ), |
||
234 | 'error' => __( 'Error', 'give' ), |
||
235 | 'close' => __( 'Close', 'give' ), |
||
236 | 'confirm' => __( 'Confirm', 'give' ), |
||
237 | 'copied' => __( 'Copied!', 'give' ), |
||
238 | 'shortcode_not_copy' => __( 'Shortcode could not be copied.', 'give' ), |
||
239 | 'confirm_action' => __( 'Confirm Action', 'give' ), |
||
240 | 'confirm_deletion' => __( 'Confirm Deletion', 'give' ), |
||
241 | 'confirm_delete_donation' => __( 'Confirm Delete Donation', 'give' ), |
||
242 | 'confirm_resend' => __( 'Confirm re-send', 'give' ), |
||
243 | 'confirm_bulk_action' => __( 'Confirm bulk action', 'give' ), |
||
244 | 'restart_upgrade' => __( 'Do you want to restart the update process?', 'give' ), |
||
245 | 'restart_update' => __( 'It is recommended that you backup your database before proceeding. Do you want to run the update now?', 'give' ), |
||
246 | 'stop_upgrade' => __( 'Do you want to stop the update process now?', 'give' ), |
||
247 | 'import_failed' => __( 'Import failed', 'give' ), |
||
248 | 'flush_success' => __( 'Flush success', 'give' ), |
||
249 | 'flush_error' => __( 'Flush error', 'give' ), |
||
250 | 'no_form_selected' => __( 'No form selected', 'give' ), |
||
251 | 'batch_export_no_class' => __( 'You must choose a method.', 'give' ), |
||
252 | 'batch_export_no_reqs' => __( 'Required fields not completed.', 'give' ), |
||
253 | 'reset_stats_warn' => __( 'Are you sure you want to reset Give? This process is <strong><em>not reversible</em></strong> and will delete all data regardless of test or live mode. Please be sure you have a recent backup before proceeding.', 'give' ), |
||
254 | 'delete_test_donor' => __( 'Are you sure you want to delete all the test donors? This process will also delete test donations as well.', 'give' ), |
||
255 | 'delete_import_donor' => __( 'Are you sure you want to delete all the imported donors? This process will also delete imported donations as well.', 'give' ), |
||
256 | 'delete_donations_only' => __( 'Are you sure you want to delete all the donations in the specfied date range?', 'give' ), |
||
257 | 'price_format_guide' => sprintf( __( 'Please enter amount in monetary decimal ( %1$s ) format without thousand separator ( %2$s ) .', 'give' ), $decimal_separator, $thousand_separator ), |
||
258 | /* translators : %s: Donation form options metabox */ |
||
259 | 'confirm_before_remove_row_text' => __( 'Do you want to delete this item?', 'give' ), |
||
260 | 'matched_success_failure_page' => __( 'You cannot set the success and failed pages to the same page', 'give' ), |
||
261 | 'dismiss_notice_text' => __( 'Dismiss this notice.', 'give' ), |
||
262 | 'search_placeholder' => __( 'Type to search all forms', 'give' ), |
||
263 | 'search_placeholder_donor' => __( 'Type to search all donors', 'give' ), |
||
264 | 'search_placeholder_country' => __( 'Type to search all countries', 'give' ), |
||
265 | 'search_placeholder_state' => __( 'Type to search all states/provinces', 'give' ), |
||
266 | 'unlock_donor_fields_title' => __( 'Action forbidden', 'give' ), |
||
267 | 'unlock_donor_fields_message' => __( 'To edit first name and last name, please go to user profile of the donor.', 'give' ), |
||
268 | 'remove_from_bulk_delete' => __( 'Remove from Bulk Delete', 'give' ), |
||
269 | 'donors_bulk_action' => array( |
||
270 | 'no_donor_selected' => array( |
||
271 | 'title' => __( 'No donors selected', 'give' ), |
||
272 | 'desc' => __( 'You must choose at least one or more donors to delete.', 'give' ), |
||
273 | ), |
||
274 | 'no_action_selected' => array( |
||
275 | 'title' => __( 'No action selected', 'give' ), |
||
276 | 'desc' => __( 'You must select a bulk action to proceed.', 'give' ), |
||
277 | ), |
||
278 | ), |
||
279 | 'donations_bulk_action' => array( |
||
280 | 'titles' => array( |
||
281 | 'zero' => __( 'No payments selected', 'give' ), |
||
282 | ), |
||
283 | 'delete' => array( |
||
284 | 'zero' => __( 'You must choose at least one or more donations to delete.', 'give' ), |
||
285 | 'single' => __( 'Are you sure you want to permanently delete this donation?', 'give' ), |
||
286 | 'multiple' => __( 'Are you sure you want to permanently delete the selected {payment_count} donations?', 'give' ), |
||
287 | ), |
||
288 | 'resend-receipt' => array( |
||
289 | 'zero' => __( 'You must choose at least one or more recipients to resend the email receipt.', 'give' ), |
||
290 | 'single' => __( 'Are you sure you want to resend the email receipt to this recipient?', 'give' ), |
||
291 | 'multiple' => __( 'Are you sure you want to resend the emails receipt to {payment_count} recipients?', 'give' ), |
||
292 | ), |
||
293 | 'set-to-status' => array( |
||
294 | 'zero' => __( 'You must choose at least one or more donations to set status to {status}.', 'give' ), |
||
295 | 'single' => __( 'Are you sure you want to set status of this donation to {status}?', 'give' ), |
||
296 | 'multiple' => __( 'Are you sure you want to set status of {payment_count} donations to {status}?', 'give' ), |
||
297 | ), |
||
298 | ), |
||
299 | 'updates' => array( |
||
300 | 'ajax_error' => __( 'Please reload this page and try again', 'give' ), |
||
301 | ), |
||
302 | 'metabox_fields' => array( |
||
303 | 'media' => array( |
||
304 | 'button_title' => __( 'Choose Image', 'give' ), |
||
305 | ), |
||
306 | 'file' => array( |
||
307 | 'button_title' => __( 'Choose File', 'give' ), |
||
308 | ), |
||
309 | ), |
||
310 | 'chosen' => array( |
||
311 | 'no_results_msg' => __( 'No results match {search_term}', 'give' ), |
||
312 | 'ajax_search_msg' => __( 'Searching results for match {search_term}', 'give' ), |
||
313 | ), |
||
314 | 'db_update_confirmation_msg_button' => __( 'Run Updates', 'give' ), |
||
315 | 'db_update_confirmation_msg' => __( 'The following process will make updates to your site\'s database. Please create a database backup before proceeding with updates.', 'give' ), |
||
316 | 'error_message' => __( 'Something went wrong kindly try again!', 'give' ), |
||
317 | 'give_donation_import' => 'give_donation_import', |
||
318 | 'core_settings_import' => 'give_core_settings_import', |
||
319 | 'setting_not_save_message' => __( 'Changes you made may not be saved.', 'give' ), |
||
320 | 'give_donation_amounts' => array( |
||
321 | 'minimum' => apply_filters( 'give_donation_minimum_limit', 1 ), |
||
322 | 'maximum' => apply_filters( 'give_donation_maximum_limit', 999999.99 ), |
||
323 | ), |
||
324 | 'chosen_add_title_prefix' => __( 'No result found. Press enter to add', 'give' ), |
||
325 | 'db_update_nonce' => wp_create_nonce( Give_Updates::$background_updater->get_identifier() ), |
||
326 | 'ajax' => give_test_ajax_works(), |
||
327 | 'donor_note_confirm_msg' => __( 'Please confirm you would like to add a donor note. An email notification will be sent to the donor with the note. If you do not want to notify the donor you may add a private note or disable the donor note email.', 'give' ), |
||
328 | 'email_notification' => array( |
||
329 | 'donor_note' => array( |
||
330 | 'status' => Give_Email_Notification_Util::is_email_notification_active( Give_Email_Notification::get_instance('donor-note' ) ) |
||
331 | ) |
||
332 | ), |
||
333 | ); |
||
334 | |||
335 | wp_localize_script( 'give-admin-scripts', 'give_vars', $localized_data ); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Global admin head. |
||
340 | */ |
||
341 | public function global_admin_head() { |
||
365 | |||
366 | /** |
||
367 | * Enqueues public styles. |
||
368 | * |
||
369 | * @since 2.1.0 |
||
370 | */ |
||
371 | public function public_enqueue_styles() { |
||
374 | |||
375 | |||
376 | /** |
||
377 | * Enqueues public scripts. |
||
378 | * |
||
379 | * @since 2.1.0 |
||
380 | */ |
||
381 | public function public_enqueue_scripts() { |
||
400 | |||
401 | /** |
||
402 | * Localize / PHP to AJAX vars. |
||
403 | */ |
||
404 | public function public_localize_scripts() { |
||
473 | |||
474 | /** |
||
475 | * Get the stylesheet URI. |
||
476 | * |
||
477 | * @since 1.6 |
||
478 | * @updated 2.0.1 Moved to class and renamed as method. |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | public function get_frontend_stylesheet_uri() { |
||
522 | |||
523 | /** |
||
524 | * Gutenberg admin scripts. |
||
525 | */ |
||
526 | public function gutenberg_admin_scripts() { |
||
553 | |||
554 | } |
||
555 |