Completed
Pull Request — master (#859)
by Devin
19:40
created

scripts.php ➔ give_get_stylesheet_uri()   C

Complexity

Conditions 13
Paths 24

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 34.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 23
c 1
b 0
f 0
nc 24
nop 0
dl 0
loc 38
ccs 8
cts 16
cp 0.5
crap 34.125
rs 5.1234

How to fix   Complexity   

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 27 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     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
 * Load Scripts
19
 *
20
 * Enqueues the required scripts.
21
 *
22
 * @since 1.0
23
 * @global $give_options
24
 * @global $post
25
 * @return void
26
 */
27
function give_load_scripts() {
28
29 1
	$js_dir         = GIVE_PLUGIN_URL . 'assets/js/frontend/';
30
	$js_plugins     = GIVE_PLUGIN_URL . 'assets/js/plugins/';
31 1
	$scripts_footer = ( give_get_option( 'scripts_footer' ) == 'on' ) ? true : false;
32 1
33 1
	// Use minified libraries if SCRIPT_DEBUG is turned off
34
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
35
36 1
	//Localize / PHP to AJAX vars
37
	$localize_give_checkout = apply_filters( 'give_global_script_vars', array(
38
		'ajaxurl'             => give_get_ajax_url(),
39 1
		'checkout_nonce'      => wp_create_nonce( 'give_checkout_nonce' ),
40 1
		'currency_sign'       => give_currency_filter( '' ),
41 1
		'currency_pos'        => give_get_currency_position(),
42 1
		'thousands_separator' => give_get_price_thousand_separator(),
43 1
		'decimal_separator'   => give_get_price_decimal_separator(),
44 1
		'no_gateway'          => esc_html__( 'Please select a payment method.', 'give' ),
45 1
		'bad_minimum'         => esc_html__( 'The minimum donation amount for this form is', 'give' ),
46 1
		'general_loading'     => esc_html__( 'Loading...', 'give' ),
47 1
		'purchase_loading'    => esc_html__( 'Please Wait...', 'give' ),
48 1
		'number_decimals'     => give_get_price_decimals(),
49 1
		'give_version'        => GIVE_VERSION
50 1
	) );
51
	$localize_give_ajax     = apply_filters( 'give_global_ajax_vars', array(
52 1
		'ajaxurl'         => give_get_ajax_url(),
53 1
		'loading'         => esc_html__( 'Loading', 'give' ),
54 1
		// General loading message
55 1
		'select_option'   => esc_html__( 'Please select an option', 'give' ),
56
		// Variable pricing error with multi-purchase option enabled
57 1
		'default_gateway' => give_get_default_gateway( null ),
58
		'permalinks'      => get_option( 'permalink_structure' ) ? '1' : '0',
59 1
		'number_decimals' => give_get_price_decimals()
60 1
	) );
61 1
62 1
	//DEBUG is On
63
	if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
64
65 1
		if ( give_is_cc_verify_enabled() ) {
66
			wp_register_script( 'give-cc-validator', $js_plugins . 'jquery.payment' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
67
			wp_enqueue_script( 'give-cc-validator' );
68
		}
69
70
		wp_register_script( 'give-float-labels', $js_plugins . 'float-labels' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
71
		wp_enqueue_script( 'give-float-labels' );
72
73
		wp_register_script( 'give-blockui', $js_plugins . 'jquery.blockUI' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
74
		wp_enqueue_script( 'give-blockui' );
75
76
		wp_register_script( 'give-qtip', $js_plugins . 'jquery.qtip' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
77
		wp_enqueue_script( 'give-qtip' );
78
79
		wp_register_script( 'give-accounting', $js_plugins . 'accounting' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
80
		wp_enqueue_script( 'give-accounting' );
81
82
		wp_register_script( 'give-magnific', $js_plugins . 'jquery.magnific-popup' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
83
		wp_enqueue_script( 'give-magnific' );
84
85
		wp_register_script( 'give-checkout-global', $js_dir . 'give-checkout-global' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
86
		wp_enqueue_script( 'give-checkout-global' );
87
88
		//General scripts
89
		wp_register_script( 'give-scripts', $js_dir . 'give' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
90
		wp_enqueue_script( 'give-scripts' );
91
92
		// Load AJAX scripts, if enabled
93
		wp_register_script( 'give-ajax', $js_dir . 'give-ajax' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
94
		wp_enqueue_script( 'give-ajax' );
95
96
		//Localize / Pass AJAX vars from PHP
97
		wp_localize_script( 'give-checkout-global', 'give_global_vars', $localize_give_checkout );
98
		wp_localize_script( 'give-ajax', 'give_scripts', $localize_give_ajax );
99
100
101
	} else {
102
103
		//DEBUG is OFF (one JS file to rule them all!)
104
		wp_register_script( 'give', $js_dir . 'give.all.min.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
105
		wp_enqueue_script( 'give' );
106 1
107 1
		//Localize / Pass AJAX vars from PHP
108
		wp_localize_script( 'give', 'give_global_vars', $localize_give_checkout );
109
		wp_localize_script( 'give', 'give_scripts', $localize_give_ajax );
110 1
111 1
	}
112
113
114
}
115
116 1
add_action( 'wp_enqueue_scripts', 'give_load_scripts' );
117
118
/**
119
 * Register styles.
120
 *
121
 * Checks the styles option and hooks the required filter.
122
 *
123
 * @since 1.0
124
 * @return void
125
 */
126
function give_register_styles() {
127
128
	if ( give_get_option( 'disable_css', false ) ) {
129
		return;
130 2
	}
131 1
132
	wp_register_style( 'give-styles', give_get_stylesheet_uri(), array(), GIVE_VERSION, 'all' );
133
	wp_enqueue_style( 'give-styles' );
134
135 1
}
136
137 1
add_action( 'wp_enqueue_scripts', 'give_register_styles' );
138 1
139
140 1
/**
141 1
 * Get the stylesheet URI.
142 1
 *
143 1
 * @since 1.6
144 1
 * @return mixed|void
145
 */
146
function give_get_stylesheet_uri() {
147
148
	// Use minified libraries if SCRIPT_DEBUG is turned off
149 1
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
150
151
	$file          = 'give' . $suffix . '.css';
152
	$templates_dir = give_get_theme_template_dir_name();
153
154
	$child_theme_style_sheet    = trailingslashit( get_stylesheet_directory() ) . $templates_dir . $file;
155 1
	$child_theme_style_sheet_2  = trailingslashit( get_stylesheet_directory() ) . $templates_dir . 'give.css';
156
	$parent_theme_style_sheet   = trailingslashit( get_template_directory() ) . $templates_dir . $file;
157
	$parent_theme_style_sheet_2 = trailingslashit( get_template_directory() ) . $templates_dir . 'give.css';
158
	$give_plugin_style_sheet    = trailingslashit( give_get_templates_dir() ) . $file;
159
160
	$uri = false;
161 1
162 1
	// Look in the child theme directory first, followed by the parent theme, followed by the Give core templates directory
163 1
	// Also look for the min version first, followed by non minified version, even if SCRIPT_DEBUG is not enabled.
164
	// This allows users to copy just give.css to their theme
165 1
	if ( file_exists( $child_theme_style_sheet ) || ( ! empty( $suffix ) && ( $nonmin = file_exists( $child_theme_style_sheet_2 ) ) ) ) {
166 1
		if ( ! empty( $nonmin ) ) {
167
			$uri = trailingslashit( get_stylesheet_directory_uri() ) . $templates_dir . 'give.css';
168 1
		} else {
169
			$uri = trailingslashit( get_stylesheet_directory_uri() ) . $templates_dir . $file;
170
		}
171
	} elseif ( file_exists( $parent_theme_style_sheet ) || ( ! empty( $suffix ) && ( $nonmin = file_exists( $parent_theme_style_sheet_2 ) ) ) ) {
172
		if ( ! empty( $nonmin ) ) {
173
			$uri = trailingslashit( get_template_directory_uri() ) . $templates_dir . 'give.css';
174
		} else {
175
			$uri = trailingslashit( get_template_directory_uri() ) . $templates_dir . $file;
176
		}
177
	} elseif ( file_exists( $give_plugin_style_sheet ) || file_exists( $give_plugin_style_sheet ) ) {
178
		$uri = trailingslashit( give_get_templates_url() ) . $file;
179
	}
180
181
	return apply_filters( 'give_get_stylesheet_uri', $uri );
182
183
}
184
185
/**
186 2
 * Load Admin Scripts
187
 *
188
 * Enqueues the required admin scripts.
189 2
 *
190 2
 * @since 1.0
191 2
 *
192
 * @global       $post
193
 * @global       $give_options
194 2
 *
195
 * @param string $hook Page hook
196
 *
197 2
 * @return void
198 2
 */
199
function give_load_admin_scripts( $hook ) {
200
201 2
	global $wp_version, $post, $post_type, $give_options;
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...
202 1
203
	//Directories of assets
204
	$js_dir     = GIVE_PLUGIN_URL . 'assets/js/admin/';
205
	$js_plugins = GIVE_PLUGIN_URL . 'assets/js/plugins/';
206 1
	$css_dir    = GIVE_PLUGIN_URL . 'assets/css/';
207 1
208 1
	// Use minified libraries if SCRIPT_DEBUG is turned off
209 1
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
210 1
211 1
	//Global Admin:
212 1
	wp_register_style( 'give-admin-bar-notification', $css_dir . 'adminbar-style.css' );
213
	wp_enqueue_style( 'give-admin-bar-notification' );
214
215 1
	//Give Admin Only:
216 1
	if ( ! apply_filters( 'give_load_admin_scripts', give_is_admin_page(), $hook ) ) {
217
		return;
218 1
	}
219 1
220
	//CSS
221 1
	wp_register_style( 'jquery-ui-css', $css_dir . 'jquery-ui-fresh' . $suffix . '.css' );
222 1
	wp_enqueue_style( 'jquery-ui-css' );
223
	wp_register_style( 'give-admin', $css_dir . 'give-admin' . $suffix . '.css', GIVE_VERSION );
224 1
	wp_enqueue_style( 'give-admin' );
225 1
	wp_register_style( 'jquery-chosen', $css_dir . 'chosen' . $suffix . '.css', array(), GIVE_VERSION );
226
	wp_enqueue_style( 'jquery-chosen' );
227 1
	wp_enqueue_style( 'thickbox' );
228 1
229
	//JS
230
	wp_register_script( 'jquery-chosen', $js_plugins . 'chosen.jquery' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION );
231 1
	wp_enqueue_script( 'jquery-chosen' );
232
233
	wp_register_script( 'give-accounting', $js_plugins . 'accounting' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
234
	wp_enqueue_script( 'give-accounting' );
235
236
	wp_register_script( 'give-admin-scripts', $js_dir . 'admin-scripts' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
237 1
	wp_enqueue_script( 'give-admin-scripts' );
238
239
	wp_register_script( 'jquery-flot', $js_plugins . 'jquery.flot' . $suffix . '.js' );
240
	wp_enqueue_script( 'jquery-flot' );
241
242
	wp_register_script( 'give-qtip', $js_plugins . 'jquery.qtip' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
243
	wp_enqueue_script( 'give-qtip' );
244 1
245 1
	wp_enqueue_script( 'jquery-ui-datepicker' );
246 1
	wp_enqueue_script( 'thickbox' );
247 1
248 1
	// Forms CPT Script.
249 1
	if ( $post_type === 'give_forms' ) {
250 1
		wp_register_script( 'give-admin-forms-scripts', $js_dir . 'admin-forms' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
251 1
		wp_enqueue_script( 'give-admin-forms-scripts' );
252 1
	}
253 1
254
	//Settings Scripts
255 1
	if ( isset( $_GET['page'] ) && $_GET['page'] == 'give-settings' ) {
256 1
		wp_register_script( 'give-admin-settings-scripts', $js_dir . 'admin-settings' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
257 1
		wp_enqueue_script( 'give-admin-settings-scripts' );
258 1
	}
259
260 1
	// Price Separators.
261
	$thousand_separator = give_get_price_thousand_separator();
262 1
	$decimal_separator  = give_get_price_decimal_separator();
263 1
264 1
	//Localize strings & variables for JS
265 1
	wp_localize_script( 'give-admin-scripts', 'give_vars', array(
266 1
		'post_id'                 => isset( $post->ID ) ? $post->ID : null,
267 1
		'give_version'            => GIVE_VERSION,
268 1
		'thousands_separator'     => $thousand_separator,
269 1
		'decimal_separator'       => $decimal_separator,
270
		'quick_edit_warning'      => esc_html__( 'Sorry, not available for variable priced forms.', 'give' ),
271 1
		'delete_payment'          => esc_html__( 'Are you sure you wish to delete this payment?', 'give' ),
272 1
		'delete_payment_note'     => esc_html__( 'Are you sure you wish to delete this note?', 'give' ),
273 1
		'revoke_api_key'          => esc_html__( 'Are you sure you wish to revoke this API key?', 'give' ),
274 1
		'regenerate_api_key'      => esc_html__( 'Are you sure you wish to regenerate this API key?', 'give' ),
275 1
		'resend_receipt'          => esc_html__( 'Are you sure you wish to resend the donation receipt?', 'give' ),
276
		'copy_download_link_text' => esc_html__( 'Copy these links to your clipboard and give them to your donor.', 'give' ),
277 1
		/* translators: %s: form singular label */
278
		'delete_payment_download' => sprintf( esc_html__( 'Are you sure you wish to delete this %s?', 'give' ), give_get_forms_label_singular() ),
279 1
		'one_price_min'           => esc_html__( 'You must have at least one price.', 'give' ),
280 1
		'one_file_min'            => esc_html__( 'You must have at least one file.', 'give' ),
281
		'one_field_min'           => esc_html__( 'You must have at least one field.', 'give' ),
282 1
		/* translators: %s: form singular label */
283
		'one_option'              => sprintf( esc_html__( 'Choose a %s', 'give' ), give_get_forms_label_singular() ),
284
		/* translators: %s: form plural label */
285
		'one_or_more_option'      => sprintf( esc_html__( 'Choose one or more %s', 'give' ), give_get_forms_label_plural() ),
286
		'numeric_item_price'      => esc_html__( 'Item price must be numeric.', 'give' ),
287
		'numeric_quantity'        => esc_html__( 'Quantity must be numeric.', 'give' ),
288
		'currency_sign'           => give_currency_filter( '' ),
289
		'currency_pos'            => isset( $give_options['currency_position'] ) ? $give_options['currency_position'] : 'before',
290
		'currency_decimals'       => give_currency_decimal_filter( give_get_price_decimals() ),
291
		'new_media_ui'            => apply_filters( 'give_use_35_media_ui', 1 ),
292
		'remove_text'             => esc_html__( 'Remove', 'give' ),
293
		/* translators: %s: form plural label */
294
		'type_to_search'          => sprintf( esc_html__( 'Type to search %s', 'give' ), give_get_forms_label_plural() ),
295
		'batch_export_no_class'   => esc_html__( 'You must choose a method.', 'give' ),
296
		'batch_export_no_reqs'    => esc_html__( 'Required fields not completed.', 'give' ),
297 1
		'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' ),
298
		'price_format_guide'      => sprintf( esc_html__( 'Please enter amount in monetary decimal ( %1$s ) format without thousand separator ( %2$s ) .', 'give' ), $decimal_separator, $thousand_separator )
299
	) );
300
301
	if ( function_exists( 'wp_enqueue_media' ) && version_compare( $wp_version, '3.5', '>=' ) ) {
302
		//call for new media manager
303
		wp_enqueue_media();
304
	}
305
306
}
307
308
add_action( 'admin_enqueue_scripts', 'give_load_admin_scripts', 100 );
309
310
/**
311
 * Admin Give Icon
312
 *
313
 * Echoes the CSS for the Give post type icon.
314
 *
315
 * @since 1.0
316
 * @global $post_type
317
 * @global $wp_version
318
 * @return void
319
 */
320
function give_admin_icon() {
321
	global $wp_version;
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...
322
	?>
323
	<style type="text/css" media="screen">
324
325 1
		<?php if( version_compare( $wp_version, '3.8-RC', '>=' ) || version_compare( $wp_version, '3.8', '>=' ) ) { ?>
326
		@font-face {
327
			font-family: 'give-icomoon';
328
			src: url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.eot?-ngjl88'; ?>');
329
			src: url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.eot?#iefix-ngjl88'?>') format('embedded-opentype'),
330
			url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.woff?-ngjl88'; ?>') format('woff'),
331
			url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.ttf?-ngjl88'; ?>') format('truetype'),
332
			url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.svg?-ngjl88#icomoon'; ?>') format('svg');
333
			font-weight: normal;
334
			font-style: normal;
335
		}
336
337
		.dashicons-give:before, #adminmenu div.wp-menu-image.dashicons-give:before {
338
			font-family: 'give-icomoon';
339
			font-size: 18px;
340
			width: 18px;
341
			height: 18px;
342
			content: "\e800";
343
		}
344
345
		<?php }  ?>
346
347
	</style>
348
	<?php
349
}
350
351
add_action( 'admin_head', 'give_admin_icon' );
352