Completed
Pull Request — master (#791)
by Devin
17:46
created

scripts.php ➔ give_load_admin_scripts()   F

Complexity

Conditions 15
Paths 260

Size

Total Lines 111
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 60
CRAP Score 15.2567

Importance

Changes 8
Bugs 1 Features 3
Metric Value
cc 15
eloc 70
c 8
b 1
f 3
nc 260
nop 1
dl 0
loc 111
rs 3.7313
ccs 60
cts 67
cp 0.8955
crap 15.2567

How to fix   Long Method    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
	global $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...
30
31 1
	$js_dir         = GIVE_PLUGIN_URL . 'assets/js/frontend/';
32 1
	$js_plugins     = GIVE_PLUGIN_URL . 'assets/js/plugins/';
33 1
	$scripts_footer = ( give_get_option( 'scripts_footer' ) == 'on' ) ? true : false;
34
35
	// Use minified libraries if SCRIPT_DEBUG is turned off
36 1
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
37
38
	//Localize / PHP to AJAX vars
39 1
	$localize_give_checkout = apply_filters( 'give_global_script_vars', array(
40 1
		'ajaxurl'             => give_get_ajax_url(),
41 1
		'checkout_nonce'      => wp_create_nonce( 'give_checkout_nonce' ),
42 1
		'currency_sign'       => give_currency_filter( '' ),
43 1
		'currency_pos'        => give_get_currency_position(),
44 1
		'thousands_separator' => isset( $give_options['thousands_separator'] ) ? $give_options['thousands_separator'] : ',',
45 1
		'decimal_separator'   => isset( $give_options['decimal_separator'] ) ? $give_options['decimal_separator'] : '.',
46 1
		'no_gateway'          => __( 'Please select a payment method.', 'give' ),
47 1
		'bad_minimum'         => __( 'The minimum donation amount for this form is', 'give' ),
48 1
		'general_loading'     => __( 'Loading...', 'give' ),
49 1
		'purchase_loading'    => __( 'Please Wait...', 'give' ),
50 1
		'number_decimals'  => give_get_price_decimals(),
51
		'give_version'        => GIVE_VERSION
52 1
	) );
53 1
	$localize_give_ajax     = apply_filters( 'give_global_ajax_vars', array(
54 1
		'ajaxurl'          => give_get_ajax_url(),
55 1
		'loading'          => __( 'Loading', 'give' ),
56
		// General loading message
57 1
		'select_option'    => __( 'Please select an option', 'give' ),
58
		// Variable pricing error with multi-purchase option enabled
59 1
		'default_gateway'  => give_get_default_gateway( null ),
60 1
		'permalinks'       => get_option( 'permalink_structure' ) ? '1' : '0',
61 1
		'number_decimals'  => give_get_price_decimals()
62 1
	) );
63
64
	//DEBUG is On
65 1
	if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
66
67
		if ( give_is_cc_verify_enabled() ) {
68
			wp_register_script( 'give-cc-validator', $js_plugins . 'jquery.payment' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
69
			wp_enqueue_script( 'give-cc-validator' );
70
		}
71
72
		wp_register_script( 'give-float-labels', $js_plugins . 'float-labels' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
73
		wp_enqueue_script( 'give-float-labels' );
74
75
		wp_register_script( 'give-blockui', $js_plugins . 'jquery.blockUI' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
76
		wp_enqueue_script( 'give-blockui' );
77
78
		wp_register_script( 'give-qtip', $js_plugins . 'jquery.qtip' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
79
		wp_enqueue_script( 'give-qtip' );
80
81
		wp_register_script( 'give-accounting', $js_plugins . 'accounting' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
82
		wp_enqueue_script( 'give-accounting' );
83
84
		wp_register_script( 'give-magnific', $js_plugins . 'jquery.magnific-popup' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
85
		wp_enqueue_script( 'give-magnific' );
86
87
		wp_register_script( 'give-checkout-global', $js_dir . 'give-checkout-global' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
88
		wp_enqueue_script( 'give-checkout-global' );
89
90
		//General scripts
91
		wp_register_script( 'give-scripts', $js_dir . 'give' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
92
		wp_enqueue_script( 'give-scripts' );
93
94
		// Load AJAX scripts, if enabled
95
		wp_register_script( 'give-ajax', $js_dir . 'give-ajax' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
96
		wp_enqueue_script( 'give-ajax' );
97
98
		//Localize / Pass AJAX vars from PHP
99
		wp_localize_script( 'give-checkout-global', 'give_global_vars', $localize_give_checkout );
100
		wp_localize_script( 'give-ajax', 'give_scripts', $localize_give_ajax );
101
102
103
	} else {
104
105
		//DEBUG is OFF (one JS file to rule them all!)
106 1
		wp_register_script( 'give', $js_dir . 'give.all.min.js', array( 'jquery' ), GIVE_VERSION, $scripts_footer );
107 1
		wp_enqueue_script( 'give' );
108
109
		//Localize / Pass AJAX vars from PHP
110 1
		wp_localize_script( 'give', 'give_global_vars', $localize_give_checkout );
111 1
		wp_localize_script( 'give', 'give_scripts', $localize_give_ajax );
112
113
	}
114
115
116 1
}
117
118
add_action( 'wp_enqueue_scripts', 'give_load_scripts' );
119
120
/**
121
 * Register Styles
122
 *
123
 * Checks the styles option and hooks the required filter.
124
 *
125
 * @since 1.0
126
 * @return void
127
 */
128
function give_register_styles() {
129
130 2
	if ( give_get_option( 'disable_css', false ) ) {
131 1
		return;
132
	}
133
134
	// Use minified libraries if SCRIPT_DEBUG is turned off
135 1
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
136
137 1
	$file          = 'give' . $suffix . '.css';
138 1
	$templates_dir = give_get_theme_template_dir_name();
139
140 1
	$child_theme_style_sheet    = trailingslashit( get_stylesheet_directory() ) . $templates_dir . $file;
141 1
	$child_theme_style_sheet_2  = trailingslashit( get_stylesheet_directory() ) . $templates_dir . 'give.css';
142 1
	$parent_theme_style_sheet   = trailingslashit( get_template_directory() ) . $templates_dir . $file;
143 1
	$parent_theme_style_sheet_2 = trailingslashit( get_template_directory() ) . $templates_dir . 'give.css';
144 1
	$give_plugin_style_sheet    = trailingslashit( give_get_templates_dir() ) . $file;
145
146
	// Look in the child theme directory first, followed by the parent theme, followed by the Give core templates directory
147
	// Also look for the min version first, followed by non minified version, even if SCRIPT_DEBUG is not enabled.
148
	// This allows users to copy just give.css to their theme
149 1
	if ( file_exists( $child_theme_style_sheet ) || ( ! empty( $suffix ) && ( $nonmin = file_exists( $child_theme_style_sheet_2 ) ) ) ) {
150
		if ( ! empty( $nonmin ) ) {
151
			$url = trailingslashit( get_stylesheet_directory_uri() ) . $templates_dir . 'give.css';
152
		} else {
153
			$url = trailingslashit( get_stylesheet_directory_uri() ) . $templates_dir . $file;
154
		}
155 1
	} elseif ( file_exists( $parent_theme_style_sheet ) || ( ! empty( $suffix ) && ( $nonmin = file_exists( $parent_theme_style_sheet_2 ) ) ) ) {
156
		if ( ! empty( $nonmin ) ) {
157
			$url = trailingslashit( get_template_directory_uri() ) . $templates_dir . 'give.css';
158
		} else {
159
			$url = trailingslashit( get_template_directory_uri() ) . $templates_dir . $file;
160
		}
161 1
	} elseif ( file_exists( $give_plugin_style_sheet ) || file_exists( $give_plugin_style_sheet ) ) {
162 1
		$url = trailingslashit( give_get_templates_url() ) . $file;
163 1
	}
164
165 1
	wp_register_style( 'give-styles', $url, array(), GIVE_VERSION, 'all' );
0 ignored issues
show
Bug introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
166 1
	wp_enqueue_style( 'give-styles' );
167
168 1
}
169
170
add_action( 'wp_enqueue_scripts', 'give_register_styles' );
171
172
/**
173
 * Load Admin Scripts
174
 *
175
 * Enqueues the required admin scripts.
176
 *
177
 * @since 1.0
178
 *
179
 * @global       $post
180
 * @global       $give_options
181
 *
182
 * @param string $hook Page hook
183
 *
184
 * @return void
185
 */
186 2
function give_load_admin_scripts( $hook ) {
187
188
	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...
189 2
190 2
	//Directories of assets
191 2
	$js_dir     = GIVE_PLUGIN_URL . 'assets/js/admin/';
192
	$js_plugins = GIVE_PLUGIN_URL . 'assets/js/plugins/';
193
	$css_dir    = GIVE_PLUGIN_URL . 'assets/css/';
194 2
195
	// Use minified libraries if SCRIPT_DEBUG is turned off
196
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
197 2
198 2
	//Global Admin:
199
	wp_register_style( 'give-admin-bar-notification', $css_dir . 'adminbar-style.css' );
200
	wp_enqueue_style( 'give-admin-bar-notification' );
201 2
202 1
	//Give Admin Only:
203
	if ( ! apply_filters( 'give_load_admin_scripts', give_is_admin_page(), $hook ) ) {
204
		return;
205
	}
206 1
207 1
	//CSS
208 1
	wp_register_style( 'jquery-ui-css', $css_dir . 'jquery-ui-fresh' . $suffix . '.css' );
209 1
	wp_enqueue_style( 'jquery-ui-css' );
210 1
	wp_register_style( 'give-admin', $css_dir . 'give-admin' . $suffix . '.css', GIVE_VERSION );
211 1
	wp_enqueue_style( 'give-admin' );
212 1
	wp_register_style( 'jquery-chosen', $css_dir . 'chosen' . $suffix . '.css', array(), GIVE_VERSION );
213
	wp_enqueue_style( 'jquery-chosen' );
214
	wp_enqueue_style( 'thickbox' );
215 1
216 1
	//JS
217
	wp_register_script( 'jquery-chosen', $js_plugins . 'chosen.jquery' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION );
218 1
	wp_enqueue_script( 'jquery-chosen' );
219 1
220
	wp_register_script( 'give-admin-scripts', $js_dir . 'admin-scripts' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
221 1
	wp_enqueue_script( 'give-admin-scripts' );
222 1
223
	wp_register_script( 'jquery-flot', $js_plugins . 'jquery.flot' . $suffix . '.js' );
224 1
	wp_enqueue_script( 'jquery-flot' );
225 1
226
	wp_register_script( 'give-qtip', $js_plugins . 'jquery.qtip' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
227 1
	wp_enqueue_script( 'give-qtip' );
228 1
229
	wp_enqueue_script( 'jquery-ui-datepicker' );
230
	wp_enqueue_script( 'thickbox' );
231 1
232
	// Forms CPT Script.
233
	if ( $post_type === 'give_forms' ) {
234
		wp_register_script( 'give-admin-forms-scripts', $js_dir . 'admin-forms' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
235
		wp_enqueue_script( 'give-admin-forms-scripts' );
236
	}
237 1
238
	// Report Scripts.
239
	if ( isset($_GET['page']) && $_GET['page'] == 'give-reports'  ) {
240
		wp_register_script( 'give-accounting', $js_plugins . 'accounting' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
241
		wp_enqueue_script( 'give-accounting' );
242
	}
243
244 1
    //Settings Scripts
245 1
    if (isset($_GET['page']) && $_GET['page'] == 'give-settings'  ) {
246 1
        wp_register_script( 'give-admin-settings-scripts', $js_dir . 'admin-settings' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false );
247 1
        wp_enqueue_script( 'give-admin-settings-scripts' );
248 1
    }
249 1
250 1
    // Price Separators.
251 1
    $thousand_separator = isset( $give_options['thousands_separator'] ) ? $give_options['thousands_separator'] : ',';
252 1
    $decimal_separator  = isset( $give_options['decimal_separator'] ) ? $give_options['decimal_separator'] : '.';
253 1
254
    //Localize strings & variables for JS
255 1
	wp_localize_script( 'give-admin-scripts', 'give_vars', array(
256 1
		'post_id'                 => isset( $post->ID ) ? $post->ID : null,
257 1
		'give_version'            => GIVE_VERSION,
258 1
        'thousands_separator'     => $thousand_separator,
259
        'decimal_separator'       => $decimal_separator,
260 1
        'quick_edit_warning'      => __( 'Sorry, not available for variable priced forms.', 'give' ),
261
		'delete_payment'          => __( 'Are you sure you wish to delete this payment?', 'give' ),
262 1
		'delete_payment_note'     => __( 'Are you sure you wish to delete this note?', 'give' ),
263 1
		'revoke_api_key'          => __( 'Are you sure you wish to revoke this API key?', 'give' ),
264 1
		'regenerate_api_key'      => __( 'Are you sure you wish to regenerate this API key?', 'give' ),
265 1
		'resend_receipt'          => __( 'Are you sure you wish to resend the donation receipt?', 'give' ),
266 1
		'copy_download_link_text' => __( 'Copy these links to your clipboard and give them to your donor.', 'give' ),
267 1
		/* translators: %s: form singular label */
268 1
		'delete_payment_download' => sprintf( __( 'Are you sure you wish to delete this %s?', 'give' ), give_get_forms_label_singular() ),
269 1
		'one_price_min'           => __( 'You must have at least one price.', 'give' ),
270
		'one_file_min'            => __( 'You must have at least one file.', 'give' ),
271 1
		'one_field_min'           => __( 'You must have at least one field.', 'give' ),
272 1
		/* translators: %s: form singular label */
273 1
		'one_option'              => sprintf( __( 'Choose a %s', 'give' ), give_get_forms_label_singular() ),
274 1
		/* translators: %s: form plural label */
275 1
		'one_or_more_option'      => sprintf( __( 'Choose one or more %s', 'give' ), give_get_forms_label_plural() ),
276
		'numeric_item_price'      => __( 'Item price must be numeric.', 'give' ),
277 1
		'numeric_quantity'        => __( 'Quantity must be numeric.', 'give' ),
278
		'currency_sign'           => give_currency_filter( '' ),
279 1
		'currency_pos'            => isset( $give_options['currency_position'] ) ? $give_options['currency_position'] : 'before',
280 1
		'currency_decimals'       => give_currency_decimal_filter(),
281
		'new_media_ui'            => apply_filters( 'give_use_35_media_ui', 1 ),
282 1
		'remove_text'             => __( 'Remove', 'give' ),
283
		/* translators: %s: form plural label */
284
		'type_to_search'          => sprintf( __( 'Type to search %s', 'give' ), give_get_forms_label_plural() ),
285
		'batch_export_no_class'   => __( 'You must choose a method.', 'give' ),
286
		'batch_export_no_reqs'    => __( 'Required fields not completed.', 'give' ),
287
		'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' ),
288
        'price_format_guide'      => sprintf( __( 'Please enter amount in monetary decimal ( %1$s ) format without thousand separator ( %2$s ) .', 'give' ), $decimal_separator, $thousand_separator )
289
    ) );
290
291
	if ( function_exists( 'wp_enqueue_media' ) && version_compare( $wp_version, '3.5', '>=' ) ) {
292
		//call for new media manager
293
		wp_enqueue_media();
294
	}
295
296
}
297 1
298
add_action( 'admin_enqueue_scripts', 'give_load_admin_scripts', 100 );
299
300
/**
301
 * Admin Give Icon
302
 *
303
 * Echoes the CSS for the Give post type icon.
304
 *
305
 * @since 1.0
306
 * @global $post_type
307
 * @global $wp_version
308
 * @return void
309
 */
310
function give_admin_icon() {
311
	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...
312
	?>
313
	<style type="text/css" media="screen">
314
315
		<?php if( version_compare( $wp_version, '3.8-RC', '>=' ) || version_compare( $wp_version, '3.8', '>=' ) ) { ?>
316
		@font-face {
317
			font-family: 'give-icomoon';
318
			src: url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.eot?-ngjl88'; ?>');
319
			src: url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.eot?#iefix-ngjl88'?>') format('embedded-opentype'),
320
			url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.woff?-ngjl88'; ?>') format('woff'),
321
			url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.ttf?-ngjl88'; ?>') format('truetype'),
322
			url('<?php echo GIVE_PLUGIN_URL . '/assets/fonts/icomoon.svg?-ngjl88#icomoon'; ?>') format('svg');
323
			font-weight: normal;
324
			font-style: normal;
325 1
		}
326
327
		.dashicons-give:before, #adminmenu div.wp-menu-image.dashicons-give:before {
328
			font-family: 'give-icomoon';
329
			font-size:18px;
330
			width:18px;
331
			height:18px;
332
			content: "\e800";
333
		}
334
335
		<?php }  ?>
336
337
	</style>
338
	<?php
339
}
340
341
add_action( 'admin_head', 'give_admin_icon' );
342