Completed
Push — issues/1038 ( 42eee0...d48073 )
by Ravinder
21:04
created

scripts.php ➔ give_load_scripts()   D

Complexity

Conditions 9
Paths 48

Size

Total Lines 119
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 71
nc 48
nop 0
dl 0
loc 119
rs 4.8196
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

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 26 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
 * Scripts
4
 *
5
 * @package     Give
6
 * @subpackage  Functions
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Load Scripts
19
 *
20
 * Enqueues the required scripts.
21
 *
22
 * @since 1.0
23
 *
24
 * @return void
25
 */
26
function give_load_scripts() {
27
28
	$js_dir         = GIVE_PLUGIN_URL . 'assets/js/frontend/';
29
	$js_plugins     = GIVE_PLUGIN_URL . 'assets/js/plugins/';
30
	$scripts_footer = ( give_is_setting_enabled( give_get_option( 'scripts_footer' ) ) ) ? true : false;
31
32
	// Use minified libraries if SCRIPT_DEBUG is turned off.
33
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
34
35
	// Localize / PHP to AJAX vars.
36
	$localize_give_vars = apply_filters( 'give_global_script_vars', array(
37
		'ajaxurl'             => give_get_ajax_url(),
38
		'checkout_nonce'      => wp_create_nonce( 'give_checkout_nonce' ),
39
		'currency_sign'       => give_currency_filter( '' ),
40
		'currency_pos'        => give_get_currency_position(),
41
		'thousands_separator' => give_get_price_thousand_separator(),
42
		'decimal_separator'   => give_get_price_decimal_separator(),
43
		'no_gateway'          => __( 'Please select a payment method.', 'give' ),
44
		'bad_minimum'         => __( 'The minimum donation amount for this form is', 'give' ),
45
		'general_loading'     => __( 'Loading...', 'give' ),
46
		'purchase_loading'    => __( 'Please Wait...', 'give' ),
47
		'number_decimals'     => give_get_price_decimals(),
48
		'give_version'        => GIVE_VERSION,
49
		'magnific_options'    => apply_filters(
50
			'give_magnific_options',
51
			array(
52
				'main_class'        => 'give-modal',
53
				'close_on_bg_click' => false,
54
			)
55
		),
56
		'form_translation'    => apply_filters(
57
			'give_form_translation_js',
58
			array(
59
				// Field name               Validation message.
60
				'payment-mode'           => __( 'Please select payment mode.', 'give' ),
61
				'give_first'             => __( 'Please enter your first name.', 'give' ),
62
				'give_email'             => __( 'Please enter a valid email address.', 'give' ),
63
				'give_user_login'        => __( 'Invalid username. Only lowercase letters (a-z) and numbers are allowed.', 'give' ),
64
				'give_user_pass'         => __( 'Enter a password.', 'give' ),
65
				'give_user_pass_confirm' => __( 'Enter the password confirmation.', 'give' ),
66
				'give_agree_to_terms'    => __( 'You must agree to the terms and conditions.', 'give' ),
67
			)
68
		),
69
	) );
70
71
	$localize_give_ajax = apply_filters( 'give_global_ajax_vars', array(
72
		'ajaxurl'         => give_get_ajax_url(),
73
		'loading'         => __( 'Loading', 'give' ),
74
		// General loading message.
75
		'select_option'   => __( 'Please select an option', 'give' ),
76
		// Variable pricing error with multi-donation option enabled.
77
		'default_gateway' => give_get_default_gateway( null ),
78
		'permalinks'      => get_option( 'permalink_structure' ) ? '1' : '0',
79
		'number_decimals' => give_get_price_decimals(),
80
	) );
81
82
	// DEBUG is On.
83
	if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
84
85
		if ( give_is_cc_verify_enabled() ) {
86
			wp_register_script( 'give-cc-validator', $js_plugins . 'jquery.payment' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
87
			wp_enqueue_script( 'give-cc-validator' );
88
		}
89
90
		wp_register_script( 'give-float-labels', $js_plugins . 'float-labels' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
91
		wp_enqueue_script( 'give-float-labels' );
92
93
		wp_register_script( 'give-blockui', $js_plugins . 'jquery.blockUI' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
94
		wp_enqueue_script( 'give-blockui' );
95
96
		wp_register_script( 'give-qtip', $js_plugins . 'jquery.qtip' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
97
		wp_enqueue_script( 'give-qtip' );
98
99
		wp_register_script( 'give-accounting', $js_plugins . 'accounting' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
100
		wp_enqueue_script( 'give-accounting' );
101
102
		wp_register_script( 'give-magnific', $js_plugins . 'jquery.magnific-popup' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
103
		wp_enqueue_script( 'give-magnific' );
104
105
		wp_register_script( 'give-checkout-global', $js_dir . 'give-checkout-global' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
106
		wp_enqueue_script( 'give-checkout-global' );
107
108
		// General scripts.
109
		wp_register_script( 'give-scripts', $js_dir . 'give' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
110
		wp_enqueue_script( 'give-scripts' );
111
112
		// Load AJAX scripts, if enabled.
113
		wp_register_script( 'give-ajax', $js_dir . 'give-ajax' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
114
		wp_enqueue_script( 'give-ajax' );
115
116
		// Localize / Pass AJAX vars from PHP,
117
		wp_localize_script( 'give-checkout-global', 'give_global_vars', $localize_give_vars );
118
		wp_localize_script( 'give-ajax', 'give_scripts', $localize_give_ajax );
119
120
	} else {
121
122
		// DEBUG is OFF (one JS file to rule them all!).
123
		wp_register_script( 'give', $js_dir . 'give.all.min.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
124
		wp_enqueue_script( 'give' );
125
126
		// Localize / Pass AJAX vars from PHP.
127
		wp_localize_script( 'give', 'give_global_vars', $localize_give_vars );
128
		wp_localize_script( 'give', 'give_scripts', $localize_give_ajax );
129
130
	}
131
132
	// CSS
133
	if ( give_is_setting_enabled( give_get_option( 'css' ) ) ) {
134
		wp_register_style( 'give-styles', give_get_stylesheet_uri(), array(), GIVE_VERSION, 'all' );
135
		wp_enqueue_style( 'give-styles' );
136
	}
137
138
	/**
139
	 * Fire the action after frontend scripts and styles load.
140
	 *
141
	 * @since 1.9
142
	 */
143
	do_action( 'give_wp_enqueue_scripts' );
144
}
145
146
add_action( 'wp_enqueue_scripts', 'give_load_scripts' );
147
148
149
/**
150
 * Get the stylesheet URI.
151
 *
152
 * @since 1.6
153
 *
154
 * @return string
155
 */
156
function give_get_stylesheet_uri() {
157
158
	// Use minified libraries if SCRIPT_DEBUG is turned off.
159
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
160
161
	// LTR or RTL files.
162
	$direction = ( is_rtl() ) ? '-rtl' : '';
163
164
	$file          = 'give' . $direction . $suffix . '.css';
165
	$templates_dir = give_get_theme_template_dir_name();
166
167
	$child_theme_style_sheet    = trailingslashit( get_stylesheet_directory() ) . $templates_dir . $file;
168
	$child_theme_style_sheet_2  = trailingslashit( get_stylesheet_directory() ) . $templates_dir . 'give' . $direction . '.css';
169
	$parent_theme_style_sheet   = trailingslashit( get_template_directory() ) . $templates_dir . $file;
170
	$parent_theme_style_sheet_2 = trailingslashit( get_template_directory() ) . $templates_dir . 'give' . $direction . '.css';
171
	$give_plugin_style_sheet    = trailingslashit( give_get_templates_dir() ) . $file;
172
173
	$uri = false;
174
175
	/**
176
	 * Look in the child theme directory first, followed by the parent theme,
177
	 * followed by the Give core templates directory also look for the min version first,
178
	 * followed by non minified version, even if SCRIPT_DEBUG is not enabled.
179
	 * This allows users to copy just give.css to their theme.
180
	 */
181
	if ( file_exists( $child_theme_style_sheet ) || ( ! empty( $suffix ) && ( $nonmin = file_exists( $child_theme_style_sheet_2 ) ) ) ) {
182
		if ( ! empty( $nonmin ) ) {
183
			$uri = trailingslashit( get_stylesheet_directory_uri() ) . $templates_dir . 'give' . $direction . '.css';
184
		} else {
185
			$uri = trailingslashit( get_stylesheet_directory_uri() ) . $templates_dir . $file;
186
		}
187
	} elseif ( file_exists( $parent_theme_style_sheet ) || ( ! empty( $suffix ) && ( $nonmin = file_exists( $parent_theme_style_sheet_2 ) ) ) ) {
188
		if ( ! empty( $nonmin ) ) {
189
			$uri = trailingslashit( get_template_directory_uri() ) . $templates_dir . 'give' . $direction . '.css';
190
		} else {
191
			$uri = trailingslashit( get_template_directory_uri() ) . $templates_dir . $file;
192
		}
193
	} elseif ( file_exists( $give_plugin_style_sheet ) || file_exists( $give_plugin_style_sheet ) ) {
194
		$uri = trailingslashit( give_get_templates_url() ) . $file;
195
	}
196
197
	return apply_filters( 'give_get_stylesheet_uri', $uri );
198
199
}
200
201
/**
202
 * Load Admin Scripts
203
 *
204
 * Enqueues the required admin scripts.
205
 *
206
 * @since 1.0
207
 *
208
 * @global       $post
209
 *
210
 * @param string $hook Page hook.
211
 *
212
 * @return void
213
 */
214
function give_load_admin_scripts( $hook ) {
215
216
	global $post, $post_type;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
217
218
	$give_options = give_get_settings();
219
220
	// Directories of assets.
221
	$js_dir     = GIVE_PLUGIN_URL . 'assets/js/admin/';
222
	$js_plugins = GIVE_PLUGIN_URL . 'assets/js/plugins/';
223
	$css_dir    = GIVE_PLUGIN_URL . 'assets/css/';
224
225
	// Use minified libraries if SCRIPT_DEBUG is turned off.
226
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
227
228
	// LTR or RTL files.
229
	$direction = ( is_rtl() ) ? '-rtl' : '';
230
231
	// Global Admin.
232
	wp_register_style( 'give-admin-bar-notification', $css_dir . 'adminbar-style.css' );
233
	wp_enqueue_style( 'give-admin-bar-notification' );
234
235
	// Give Admin Only.
236
	if ( ! apply_filters( 'give_load_admin_scripts', give_is_admin_page(), $hook ) ) {
237
		return;
238
	}
239
240
	// CSS.
241
	wp_register_style( 'jquery-ui-css', $css_dir . 'jquery-ui-fresh' . $suffix . '.css' );
242
	wp_enqueue_style( 'jquery-ui-css' );
243
	wp_register_style( 'give-admin', $css_dir . 'give-admin' . $direction . $suffix . '.css', array(), GIVE_VERSION );
244
	wp_enqueue_style( 'give-admin' );
245
	wp_register_style( 'jquery-chosen', $css_dir . 'chosen' . $suffix . '.css', array(), GIVE_VERSION );
246
	wp_enqueue_style( 'jquery-chosen' );
247
	wp_enqueue_style( 'thickbox' );
248
	wp_enqueue_style( 'wp-color-picker' );
249
250
251
	// JS.
252
	wp_register_script( 'jquery-chosen', $js_plugins . 'chosen.jquery' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION );
253
	wp_enqueue_script( 'jquery-chosen' );
254
255
	wp_register_script( 'give-accounting', $js_plugins . 'accounting' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
256
	wp_enqueue_script( 'give-accounting' );
257
258
	wp_enqueue_script( 'wp-color-picker' );
259
	wp_enqueue_script( 'jquery-ui-datepicker' );
260
	wp_enqueue_script( 'thickbox' );
261
262
	wp_register_script( 'give-admin-scripts', $js_dir . 'admin-scripts' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker', 'wp-color-picker' ), GIVE_VERSION, false );
263
	wp_enqueue_script( 'give-admin-scripts' );
264
265
	wp_register_script( 'jquery-flot', $js_plugins . 'jquery.flot' . $suffix . '.js' );
266
	wp_enqueue_script( 'jquery-flot' );
267
268
	wp_register_script( 'give-qtip', $js_plugins . 'jquery.qtip' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
269
	wp_enqueue_script( 'give-qtip' );
270
271
272
	// Forms CPT Script.
273
	if ( $post_type === 'give_forms' ) {
274
		wp_register_script( 'give-admin-forms-scripts', $js_dir . 'admin-forms' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
275
		wp_enqueue_script( 'give-admin-forms-scripts' );
276
	}
277
278
	// Settings Scripts.
279
	if ( isset( $_GET['page'] ) && $_GET['page'] == 'give-settings' ) {
280
		wp_register_script( 'give-admin-settings-scripts', $js_dir . 'admin-settings' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
281
		wp_enqueue_script( 'give-admin-settings-scripts' );
282
	}
283
284
	// Price Separators.
285
	$thousand_separator = give_get_price_thousand_separator();
286
	$decimal_separator  = give_get_price_decimal_separator();
287
288
	// Localize strings & variables for JS.
289
	wp_localize_script( 'give-admin-scripts', 'give_vars', array(
290
		'post_id'                        => isset( $post->ID ) ? $post->ID : null,
291
		'give_version'                   => GIVE_VERSION,
292
		'thousands_separator'            => $thousand_separator,
293
		'decimal_separator'              => $decimal_separator,
294
		'quick_edit_warning'             => __( 'Not available for variable priced forms.', 'give' ),
295
		'delete_payment'                 => __( 'Are you sure you wish to delete this payment?', 'give' ),
296
		'delete_payment_note'            => __( 'Are you sure you wish to delete this note?', 'give' ),
297
		'revoke_api_key'                 => __( 'Are you sure you wish to revoke this API key?', 'give' ),
298
		'regenerate_api_key'             => __( 'Are you sure you wish to regenerate this API key?', 'give' ),
299
		'resend_receipt'                 => __( 'Are you sure you wish to resend the donation receipt?', 'give' ),
300
		'logo'                           => __( 'Logo', 'give' ),
301
		'use_this_image'                 => __( 'Use this image', 'give' ),
302
		'one_option'                     => __( 'Choose a form', 'give' ),
303
		'one_or_more_option'             => __( 'Choose one or more forms', 'give' ),
304
		'currency_sign'                  => give_currency_filter( '' ),
305
		'currency_pos'                   => isset( $give_options['currency_position'] ) ? $give_options['currency_position'] : 'before',
306
		'currency_decimals'              => give_currency_decimal_filter( give_get_price_decimals() ),
307
		'batch_export_no_class'          => __( 'You must choose a method.', 'give' ),
308
		'batch_export_no_reqs'           => __( 'Required fields not completed.', 'give' ),
309
		'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' ),
310
		'price_format_guide'             => sprintf( __( 'Please enter amount in monetary decimal ( %1$s ) format without thousand separator ( %2$s ) .', 'give' ), $decimal_separator, $thousand_separator ),
311
		'matched_success_failure_page'   => __( 'You cannot set the success and failed pages to the same page', 'give' ),
312
		'dismiss_notice_text'            => __( 'Dismiss this notice.', 'give' ),
313
		'bulk_action' => array(
314
			'delete'         => array(
315
				'zero_payment_selected' => __( 'You must choose at least one or more payments to delete.', 'give' ),
316
				'delete_payment'        => __( 'Are you sure you want to permanently delete this donation?', 'give' ),
317
				'delete_payments'       => __( 'Are you sure you want to permanently delete the selected {payment_count} donations?', 'give' ),
318
			),
319
			'resend_receipt' => array(
320
				'zero_recipient_selected' => __( 'You must choose at least one or more recipients to resend the email receipt.', 'give' ),
321
				'resend_receipt'          => __( 'Are you sure you want to resend the email receipt to this recipient?', 'give' ),
322
				'resend_receipts'         => __( 'Are you sure you want to resend the emails receipt to {payment_count} recipients?', 'give' ),
323
			),
324
		)
325
	) );
326
327
	if ( function_exists( 'wp_enqueue_media' ) && version_compare( get_bloginfo( 'version' ), '3.5', '>=' ) ) {
328
		// call for new media manager.
329
		wp_enqueue_media();
330
	}
331
332
	/**
333
	 * Fire the action after admin scripts and styles load.
334
	 *
335
	 * @since 1.9
336
	 */
337
	do_action( 'give_admin_enqueue_scripts' );
338
}
339
340
add_action( 'admin_enqueue_scripts', 'give_load_admin_scripts', 100 );
341
342
/**
343
 * Admin Give Icon
344
 *
345
 * Echoes the CSS for the Give post type icon.
346
 *
347
 * @since 1.0
348
 *
349
 * @return void
350
 */
351
function give_admin_icon() {
352
	?>
353
    <style type="text/css" media="screen">
354
355
        <?php if ( version_compare( get_bloginfo( 'version' ), '3.8-RC', '>=' ) || version_compare( get_bloginfo( 'version' ), '3.8', '>=' ) ) { ?>
356
        @font-face {
357
            font-family: 'give-icomoon';
358
            src: url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.eot?ngjl88'; ?>');
359
            src: url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.eot?#iefixngjl88'?>') format('embedded-opentype'),
360
            url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.woff?ngjl88'; ?>') format('woff'),
361
            url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.svg?ngjl88#icomoon'; ?>') format('svg');
362
            font-weight: normal;
363
            font-style: normal;
364
        }
365
366
        .dashicons-give:before, #adminmenu div.wp-menu-image.dashicons-give:before {
367
            font-family: 'give-icomoon';
368
            font-size: 18px;
369
            width: 18px;
370
            height: 18px;
371
            content: "\e800";
372
        }
373
374
        <?php }  ?>
375
376
    </style>
377
	<?php
378
}
379
380
add_action( 'admin_head', 'give_admin_icon' );
381
382
/**
383
 * Admin js code
384
 *
385
 * This code helps to hide license notices for 24 hour if admin user dismissed notice.
386
 *
387
 * @since 1.7
388
 *
389
 * @return void
390
 */
391
function give_admin_hide_notice_shortly_js() {
392
	?>
393
    <script>
394
		jQuery(document).ready(function ($) {
395
			$('.give-license-notice').on('click', 'button.notice-dismiss', function (e) {
396
				e.preventDefault();
397
398
				var parent             = $(this).parents('.give-license-notice'),
399
				    dismiss_notice_url = parent.data('dismiss-notice-shortly');
400
401
				if (dismiss_notice_url) {
402
					window.location.assign(dismiss_notice_url);
403
				}
404
			});
405
		});
406
    </script>
407
	<?php
408
}
409
410
add_action( 'admin_head', 'give_admin_hide_notice_shortly_js' );
411