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 | |||
| 211 | // Localize strings & variables for JS. |
||
| 212 | $localized_data = array( |
||
| 213 | 'post_id' => isset( $post->ID ) ? $post->ID : null, |
||
| 214 | 'give_version' => GIVE_VERSION, |
||
| 215 | 'thousands_separator' => $thousand_separator, |
||
| 216 | 'decimal_separator' => $decimal_separator, |
||
| 217 | 'quick_edit_warning' => __( 'Not available for variable priced forms.', 'give' ), |
||
| 218 | 'delete_payment' => __( 'Are you sure you want to <strong>permanently</strong> delete this donation?', 'give' ), |
||
| 219 | 'delete_payment_note' => __( 'Are you sure you want to delete this note?', 'give' ), |
||
| 220 | 'revoke_api_key' => __( 'Are you sure you want to revoke this API key?', 'give' ), |
||
| 221 | 'regenerate_api_key' => __( 'Are you sure you want to regenerate this API key?', 'give' ), |
||
| 222 | 'resend_receipt' => __( 'Are you sure you want to resend the donation receipt?', 'give' ), |
||
| 223 | 'disconnect_user' => __( 'Are you sure you want to disconnect the user from this donor?', 'give' ), |
||
| 224 | 'one_option' => __( 'Choose a form', 'give' ), |
||
| 225 | 'one_or_more_option' => __( 'Choose one or more forms', 'give' ), |
||
| 226 | 'currency_sign' => give_currency_filter( '' ), |
||
| 227 | 'currency_pos' => isset( $give_options['currency_position'] ) ? $give_options['currency_position'] : 'before', |
||
| 228 | 'currency_decimals' => give_get_price_decimals(), |
||
| 229 | 'ok' => __( 'Ok', 'give' ), |
||
| 230 | 'cancel' => __( 'Cancel', 'give' ), |
||
| 231 | 'success' => __( 'Success', 'give' ), |
||
| 232 | 'error' => __( 'Error', 'give' ), |
||
| 233 | 'close' => __( 'Close', 'give' ), |
||
| 234 | 'confirm' => __( 'Confirm', 'give' ), |
||
| 235 | 'confirm_action' => __( 'Confirm Action', 'give' ), |
||
| 236 | 'confirm_deletion' => __( 'Confirm Deletion', 'give' ), |
||
| 237 | 'confirm_delete_donation' => __( 'Confirm Delete Donation', 'give' ), |
||
| 238 | 'confirm_resend' => __( 'Confirm re-send', 'give' ), |
||
| 239 | 'confirm_bulk_action' => __( 'Confirm bulk action', 'give' ), |
||
| 240 | 'restart_upgrade' => __( 'Do you want to restart the update process?', 'give' ), |
||
| 241 | 'restart_update' => __( 'It is recommended that you backup your database before proceeding. Do you want to run the update now?', 'give' ), |
||
| 242 | 'stop_upgrade' => __( 'Do you want to stop the update process now?', 'give' ), |
||
| 243 | 'import_failed' => __( 'Import failed', 'give' ), |
||
| 244 | 'flush_success' => __( 'Flush success', 'give' ), |
||
| 245 | 'flush_error' => __( 'Flush error', 'give' ), |
||
| 246 | 'no_form_selected' => __( 'No form selected', 'give' ), |
||
| 247 | 'batch_export_no_class' => __( 'You must choose a method.', 'give' ), |
||
| 248 | 'batch_export_no_reqs' => __( 'Required fields not completed.', 'give' ), |
||
| 249 | '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' ), |
||
| 250 | 'delete_test_donor' => __( 'Are you sure you want to delete all the test donors? This process will also delete test donations as well.', 'give' ), |
||
| 251 | 'delete_import_donor' => __( 'Are you sure you want to delete all the imported donors? This process will also delete imported donations as well.', 'give' ), |
||
| 252 | 'price_format_guide' => sprintf( __( 'Please enter amount in monetary decimal ( %1$s ) format without thousand separator ( %2$s ) .', 'give' ), $decimal_separator, $thousand_separator ), |
||
| 253 | /* translators : %s: Donation form options metabox */ |
||
| 254 | 'confirm_before_remove_row_text' => __( 'Do you want to delete this item?', 'give' ), |
||
| 255 | 'matched_success_failure_page' => __( 'You cannot set the success and failed pages to the same page', 'give' ), |
||
| 256 | 'dismiss_notice_text' => __( 'Dismiss this notice.', 'give' ), |
||
| 257 | 'search_placeholder' => __( 'Type to search all forms', 'give' ), |
||
| 258 | 'search_placeholder_donor' => __( 'Type to search all donors', 'give' ), |
||
| 259 | 'search_placeholder_country' => __( 'Type to search all countries', 'give' ), |
||
| 260 | 'search_placeholder_state' => __( 'Type to search all states/provinces', 'give' ), |
||
| 261 | 'unlock_donor_fields_title' => __( 'Action forbidden', 'give' ), |
||
| 262 | 'unlock_donor_fields_message' => __( 'To edit first name and last name, please go to user profile of the donor.', 'give' ), |
||
| 263 | 'remove_from_bulk_delete' => __( 'Remove from Bulk Delete', 'give' ), |
||
| 264 | 'donors_bulk_action' => array( |
||
| 265 | 'no_donor_selected' => array( |
||
| 266 | 'title' => __( 'No donors selected', 'give' ), |
||
| 267 | 'desc' => __( 'You must choose at least one or more donors to delete.', 'give' ) |
||
| 268 | ), |
||
| 269 | 'no_action_selected' => array( |
||
| 270 | 'title' => __( 'No action selected', 'give' ), |
||
| 271 | 'desc' => __( 'You must select a bulk action to proceed.', 'give' ), |
||
| 272 | ), |
||
| 273 | ), |
||
| 274 | 'donations_bulk_action' => array( |
||
| 275 | 'titles' => array( |
||
| 276 | 'zero' => __( 'No payments selected', 'give' ), |
||
| 277 | ), |
||
| 278 | 'delete' => array( |
||
| 279 | 'zero' => __( 'You must choose at least one or more donations to delete.', 'give' ), |
||
| 280 | 'single' => __( 'Are you sure you want to permanently delete this donation?', 'give' ), |
||
| 281 | 'multiple' => __( 'Are you sure you want to permanently delete the selected {payment_count} donations?', 'give' ), |
||
| 282 | ), |
||
| 283 | 'resend-receipt' => array( |
||
| 284 | 'zero' => __( 'You must choose at least one or more recipients to resend the email receipt.', 'give' ), |
||
| 285 | 'single' => __( 'Are you sure you want to resend the email receipt to this recipient?', 'give' ), |
||
| 286 | 'multiple' => __( 'Are you sure you want to resend the emails receipt to {payment_count} recipients?', 'give' ), |
||
| 287 | ), |
||
| 288 | 'set-to-status' => array( |
||
| 289 | 'zero' => __( 'You must choose at least one or more donations to set status to {status}.', 'give' ), |
||
| 290 | 'single' => __( 'Are you sure you want to set status of this donation to {status}?', 'give' ), |
||
| 291 | 'multiple' => __( 'Are you sure you want to set status of {payment_count} donations to {status}?', 'give' ), |
||
| 292 | ), |
||
| 293 | ), |
||
| 294 | 'updates' => array( |
||
| 295 | 'ajax_error' => __( 'Please reload this page and try again', 'give' ), |
||
| 296 | ), |
||
| 297 | 'metabox_fields' => array( |
||
| 298 | 'media' => array( |
||
| 299 | 'button_title' => __( 'Choose Image', 'give' ), |
||
| 300 | ), |
||
| 301 | 'file' => array( |
||
| 302 | 'button_title' => __( 'Choose File', 'give' ), |
||
| 303 | ), |
||
| 304 | ), |
||
| 305 | 'chosen' => array( |
||
| 306 | 'no_results_msg' => __( 'No results match {search_term}', 'give' ), |
||
| 307 | 'ajax_search_msg' => __( 'Searching results for match {search_term}', 'give' ), |
||
| 308 | ), |
||
| 309 | 'db_update_confirmation_msg_button' => __( 'Run Updates', 'give' ), |
||
| 310 | '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' ), |
||
| 311 | 'error_message' => __( 'Something went wrong kindly try again!', 'give' ), |
||
| 312 | 'give_donation_import' => 'give_donation_import', |
||
| 313 | 'core_settings_import' => 'give_core_settings_import', |
||
| 314 | 'setting_not_save_message' => __( 'Changes you made may not be saved.', 'give' ), |
||
| 315 | 'give_donation_amounts' => array( |
||
| 316 | 'minimum' => apply_filters( 'give_donation_minimum_limit', 1 ), |
||
| 317 | 'maximum' => apply_filters( 'give_donation_maximum_limit', 999999.99 ), |
||
| 318 | ), |
||
| 319 | 'chosen_add_title_prefix' => __( 'No result found. Press enter to add', 'give' ), |
||
| 320 | 'db_update_nonce' => wp_create_nonce( Give_Updates::$background_updater->get_identifier() ), |
||
| 321 | 'ajax' => give_test_ajax_works(), |
||
| 322 | 'date_format' => give_get_localized_date_format_to_js(), |
||
| 323 | ); |
||
| 324 | |||
| 325 | wp_localize_script( 'give-admin-scripts', 'give_vars', $localized_data ); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Global admin head. |
||
| 330 | */ |
||
| 331 | public function global_admin_head() { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Enqueues public styles. |
||
| 358 | * |
||
| 359 | * @since 2.1.0 |
||
| 360 | */ |
||
| 361 | public function public_enqueue_styles() { |
||
| 364 | |||
| 365 | |||
| 366 | /** |
||
| 367 | * Enqueues public scripts. |
||
| 368 | * |
||
| 369 | * @since 2.1.0 |
||
| 370 | */ |
||
| 371 | public function public_enqueue_scripts() { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Localize / PHP to AJAX vars. |
||
| 391 | */ |
||
| 392 | public function public_localize_scripts() { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Get the stylesheet URI. |
||
| 463 | * |
||
| 464 | * @since 1.6 |
||
| 465 | * @updated 2.0.1 Moved to class and renamed as method. |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function get_frontend_stylesheet_uri() { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Gutenberg admin scripts. |
||
| 512 | */ |
||
| 513 | public function gutenberg_admin_scripts() { |
||
| 535 | |||
| 536 | } |
||
| 537 |