Completed
Push — es6/issue-1475 ( 93c1ad )
by Ravinder
1139:39 queued 1133:44
created

Give_Scripts::public_localize_scripts()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 54
rs 9.0036
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
2
3
/**
4
 * Loads the plugin's scripts and styles.
5
 *
6
 * Registers and enqueues plugin styles and scripts. Asset versions are based
7
 * on the current plugin version.
8
 *
9
 * All script and style handles should be registered in this class even if they
10
 * are enqueued dynamically by other classes.
11
 *
12
 * @since 2.1.0
13
 */
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() {
40
		$this->direction      = ( is_rtl() || isset( $_GET['d'] ) && 'rtl' === $_GET['d'] ) ? '.rtl' : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
41
		$this->scripts_footer = give_is_setting_enabled( give_get_option( 'scripts_footer' ) ) ? true : false;
42
		$this->init();
43
	}
44
45
	/**
46
	 * Fires off hooks to register assets in WordPress.
47
	 *
48
	 * @since 2.1.0
49
	 */
50
	public function init() {
51
52
		add_action( 'admin_enqueue_scripts', array( $this, 'register_styles' ) );
53
		add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
54
		add_action( 'wp_enqueue_scripts', array( $this, 'register_styles' ) );
55
		add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
56
57
		if ( is_admin() ) {
58
			add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
59
			add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ) );
60
			add_action( 'admin_head', array( $this, 'global_admin_head' ) );
61
62
		} else {
63
			add_action( 'wp_enqueue_scripts', array( $this, 'public_enqueue_styles' ) );
64
			add_action( 'wp_enqueue_scripts', array( $this, 'public_enqueue_scripts' ) );
65
		}
66
	}
67
68
	/**
69
	 * Registers all plugin styles.
70
	 *
71
	 * @since 2.1.0
72
	 */
73
	public function register_styles() {
74
75
		// WP-admin.
76
		wp_register_style( 'give-admin-styles', GIVE_PLUGIN_URL . 'assets/dist/css/admin' . $this->direction . '.css', array(), GIVE_VERSION );
77
78
		// Frontend.
79
		if ( give_is_setting_enabled( give_get_option( 'css' ) ) ) {
80
			wp_register_style( 'give-styles', $this->get_frontend_stylesheet_uri(), array(), GIVE_VERSION, 'all' );
81
		}
82
	}
83
84
	/**
85
	 * Registers all plugin scripts.
86
	 *
87
	 * @since 2.1.0
88
	 */
89
	public function register_scripts() {
90
91
		// WP-Admin.
92
		wp_register_script( 'give-admin-scripts', GIVE_PLUGIN_URL . 'assets/dist/js/admin.js', array(
93
			'jquery',
94
			'jquery-ui-datepicker',
95
			'wp-color-picker',
96
			'jquery-query',
97
		), GIVE_VERSION );
98
99
		// Frontend.
100
		wp_register_script( 'give', GIVE_PLUGIN_URL . 'assets/dist/js/give.js', array( 'jquery' ), GIVE_VERSION, $this->scripts_footer );
101
	}
102
103
	/**
104
	 * Enqueues admin styles.
105
	 *
106
	 * @since 2.1.0
107
	 *
108
	 * @param string $hook Page hook.
109
	 */
110
	public function admin_enqueue_styles( $hook ) {
111
112
		// Give Admin Only.
113
		if ( ! apply_filters( 'give_load_admin_styles', give_is_admin_page(), $hook ) ) {
114
			return;
115
		}
116
117
		// Give enqueues.
118
		wp_enqueue_style( 'give-admin-styles' );
119
		wp_enqueue_style( 'give-admin-bar-notification' );
120
121
		// WP Core enqueues.
122
		wp_enqueue_style( 'wp-color-picker' );
123
		wp_enqueue_style( 'thickbox' ); // @TODO remove once we have modal API.
124
125
	}
126
127
	/**
128
	 * Enqueues admin scripts.
129
	 *
130
	 * @since 2.1.0
131
	 *
132
	 * @param string $hook Page hook.
133
	 */
134
	public function admin_enqueue_scripts( $hook ) {
135
136
		// Give Admin Only.
137
		if ( ! apply_filters( 'give_load_admin_scripts', give_is_admin_page(), $hook ) ) {
138
			return;
139
		}
140
141
		// WP Scripts.
142
		wp_enqueue_script( 'wp-color-picker' );
143
		wp_enqueue_script( 'jquery-ui-datepicker' );
144
		wp_enqueue_script( 'thickbox' );
145
		wp_enqueue_media();
146
147
		// Give admin scripts.
148
		wp_enqueue_script( 'give-admin-scripts' );
149
150
		// Localize admin scripts
151
		$this->admin_localize_scripts();
152
153
	}
154
155
	/**
156
	 * Localize admin scripts.
157
	 */
158
	public function admin_localize_scripts() {
159
160
		global $post;
161
		$give_options = give_get_settings();
162
163
		// Price Separators.
164
		$thousand_separator = give_get_price_thousand_separator();
165
		$decimal_separator  = give_get_price_decimal_separator();
166
167
		// Localize strings & variables for JS.
168
		wp_localize_script( 'give-admin-scripts', 'give_vars', array(
169
			'post_id'                           => isset( $post->ID ) ? $post->ID : null,
170
			'give_version'                      => GIVE_VERSION,
171
			'thousands_separator'               => $thousand_separator,
172
			'decimal_separator'                 => $decimal_separator,
173
			'quick_edit_warning'                => __( 'Not available for variable priced forms.', 'give' ),
174
			'delete_payment'                    => __( 'Are you sure you want to delete this payment?', 'give' ),
175
			'delete_payment_note'               => __( 'Are you sure you want to delete this note?', 'give' ),
176
			'revoke_api_key'                    => __( 'Are you sure you want to revoke this API key?', 'give' ),
177
			'regenerate_api_key'                => __( 'Are you sure you want to regenerate this API key?', 'give' ),
178
			'resend_receipt'                    => __( 'Are you sure you want to resend the donation receipt?', 'give' ),
179
			'disconnect_user'                   => __( 'Are you sure you want to disconnect the user from this donor?', 'give' ),
180
			'one_option'                        => __( 'Choose a form', 'give' ),
181
			'one_or_more_option'                => __( 'Choose one or more forms', 'give' ),
182
			'currency_sign'                     => give_currency_filter( '' ),
183
			'currency_pos'                      => isset( $give_options['currency_position'] ) ? $give_options['currency_position'] : 'before',
184
			'currency_decimals'                 => give_get_price_decimals(),
185
			'batch_export_no_class'             => __( 'You must choose a method.', 'give' ),
186
			'batch_export_no_reqs'              => __( 'Required fields not completed.', 'give' ),
187
			'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' ),
188
			'delete_test_donor'                 => __( 'Are you sure you want to delete all the test donors? This process will also delete test donations as well.', 'give' ),
189
			'delete_import_donor'               => __( 'Are you sure you want to delete all the imported donors? This process will also delete imported donations as well.', 'give' ),
190
			'price_format_guide'                => sprintf( __( 'Please enter amount in monetary decimal ( %1$s ) format without thousand separator ( %2$s ) .', 'give' ), $decimal_separator, $thousand_separator ),
191
			/* translators : %s: Donation form options metabox */
192
			'confirm_before_remove_row_text'    => __( 'Do you want to delete this item?', 'give' ),
193
			'matched_success_failure_page'      => __( 'You cannot set the success and failed pages to the same page', 'give' ),
194
			'dismiss_notice_text'               => __( 'Dismiss this notice.', 'give' ),
195
			'search_placeholder'                => __( 'Type to search all forms', 'give' ),
196
			'search_placeholder_donor'          => __( 'Type to search all donors', 'give' ),
197
			'search_placeholder_country'        => __( 'Type to search all countries', 'give' ),
198
			'search_placeholder_state'          => __( 'Type to search all states/provinces', 'give' ),
199
			'unlock_donor_fields'               => __( 'To edit first name and last name, please go to user profile of the donor.', 'give' ),
200
			'remove_from_bulk_delete'           => __( 'Remove from Bulk Delete', 'give' ),
201
			'donors_bulk_action'                => array(
202
				'no_donor_selected'  => __( 'You must choose at least one or more donors to delete.', 'give' ),
203
				'no_action_selected' => __( 'You must select a bulk action to proceed.', 'give' ),
204
			),
205
			'donations_bulk_action'             => array(
206
				'delete'         => array(
207
					'zero'     => __( 'You must choose at least one or more donations to delete.', 'give' ),
208
					'single'   => __( 'Are you sure you want to permanently delete this donation?', 'give' ),
209
					'multiple' => __( 'Are you sure you want to permanently delete the selected {payment_count} donations?', 'give' ),
210
				),
211
				'resend-receipt' => array(
212
					'zero'     => __( 'You must choose at least one or more recipients to resend the email receipt.', 'give' ),
213
					'single'   => __( 'Are you sure you want to resend the email receipt to this recipient?', 'give' ),
214
					'multiple' => __( 'Are you sure you want to resend the emails receipt to {payment_count} recipients?', 'give' ),
215
				),
216
				'set-to-status'  => array(
217
					'zero'     => __( 'You must choose at least one or more donations to set status to {status}.', 'give' ),
218
					'single'   => __( 'Are you sure you want to set status of this donation to {status}?', 'give' ),
219
					'multiple' => __( 'Are you sure you want to set status of {payment_count} donations to {status}?', 'give' ),
220
				),
221
			),
222
			'updates'                           => array(
223
				'ajax_error' => __( 'Please reload this page and try again', 'give' ),
224
			),
225
			'metabox_fields'                    => array(
226
				'media' => array(
227
					'button_title' => __( 'Choose Image', 'give' ),
228
				),
229
				'file'  => array(
230
					'button_title' => __( 'Choose File', 'give' ),
231
				),
232
			),
233
			'chosen'                            => array(
234
				'no_results_msg'  => __( 'No results match {search_term}', 'give' ),
235
				'ajax_search_msg' => __( 'Searching results for match {search_term}', 'give' ),
236
			),
237
			'db_update_confirmation_msg_button' => __( 'Run Updates', 'give' ),
238
			'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' ),
239
			'error_message'                     => __( 'Something went wrong kindly try again!', 'give' ),
240
			'give_donation_import'              => 'give_donation_import',
241
			'core_settings_import'              => 'give_core_settings_import',
242
			'setting_not_save_message'          => __( 'Changes you made may not be saved.', 'give' ),
243
			'give_donation_amounts'             => array(
244
				'minimum' => apply_filters( 'give_donation_minimum_limit', 1 ),
245
				'maximum' => apply_filters( 'give_donation_maximum_limit', 999999.99 ),
246
			),
247
		) );
248
	}
249
250
	/**
251
	 * Global admin head.
252
	 */
253
	public function global_admin_head() {
254
		?>
255
		<style type="text/css" media="screen">
256
			@font-face {
257
				font-family: 'give-icomoon';
258
				src: url('<?php echo GIVE_PLUGIN_URL . 'assets/dist/fonts/icomoon.eot?ngjl88'; ?>');
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'GIVE_PLUGIN_URL'
Loading history...
259
				src: url('<?php echo GIVE_PLUGIN_URL . 'assets/dist/fonts/icomoon.eot?#iefixngjl88'?>') format('embedded-opentype'),
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'GIVE_PLUGIN_URL'
Loading history...
260
				url('<?php echo GIVE_PLUGIN_URL . 'assets/dist/fonts/icomoon.woff?ngjl88'; ?>') format('woff'),
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'GIVE_PLUGIN_URL'
Loading history...
261
				url('<?php echo GIVE_PLUGIN_URL . 'assets/dist/fonts/icomoon.svg?ngjl88#icomoon'; ?>') format('svg');
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'GIVE_PLUGIN_URL'
Loading history...
262
				font-weight: normal;
263
				font-style: normal;
264
			}
265
266
			.dashicons-give:before, #adminmenu div.wp-menu-image.dashicons-give:before {
267
				font-family: 'give-icomoon';
268
				font-size: 18px;
269
				width: 18px;
270
				height: 18px;
271
				content: "\e800";
272
			}
273
		</style>
274
		<?php
275
276
	}
277
278
	/**
279
	 * Enqueues public styles.
280
	 *
281
	 * @since 2.1.0
282
	 */
283
	public function public_enqueue_styles() {
284
		wp_enqueue_style( 'give-styles' );
285
	}
286
287
288
	/**
289
	 * Enqueues public scripts.
290
	 *
291
	 * @since 2.1.0
292
	 */
293
	public function public_enqueue_scripts() {
294
		wp_enqueue_script( 'give' );
295
296
		$this->public_localize_scripts();
297
	}
298
299
	/**
300
	 * Localize / PHP to AJAX vars.
301
	 */
302
	public function public_localize_scripts() {
303
304
		$localize_give_vars = apply_filters( 'give_global_script_vars', array(
305
			'ajaxurl'                    => give_get_ajax_url(),
306
			'checkout_nonce'             => wp_create_nonce( 'give_checkout_nonce' ), // Do not use this nonce. Its deprecated.
307
			'currency'                   => give_get_currency(),
308
			'currency_sign'              => give_currency_filter( '' ),
309
			'currency_pos'               => give_get_currency_position(),
310
			'thousands_separator'        => give_get_price_thousand_separator(),
311
			'decimal_separator'          => give_get_price_decimal_separator(),
312
			'no_gateway'                 => __( 'Please select a payment method.', 'give' ),
313
			'bad_minimum'                => __( 'The minimum custom donation amount for this form is', 'give' ),
314
			'bad_maximum'                => __( 'The maximum custom donation amount for this form is', 'give' ),
315
			'general_loading'            => __( 'Loading...', 'give' ),
316
			'purchase_loading'           => __( 'Please Wait...', 'give' ),
317
			'number_decimals'            => give_get_price_decimals(),
318
			'give_version'               => GIVE_VERSION,
319
			'magnific_options'           => apply_filters(
320
				'give_magnific_options',
321
				array(
322
					'main_class'        => 'give-modal',
323
					'close_on_bg_click' => false,
324
				)
325
			),
326
			'form_translation'           => apply_filters(
327
				'give_form_translation_js',
328
				array(
329
					// Field name               Validation message.
330
					'payment-mode'           => __( 'Please select payment mode.', 'give' ),
331
					'give_first'             => __( 'Please enter your first name.', 'give' ),
332
					'give_email'             => __( 'Please enter a valid email address.', 'give' ),
333
					'give_user_login'        => __( 'Invalid username. Only lowercase letters (a-z) and numbers are allowed.', 'give' ),
334
					'give_user_pass'         => __( 'Enter a password.', 'give' ),
335
					'give_user_pass_confirm' => __( 'Enter the password confirmation.', 'give' ),
336
					'give_agree_to_terms'    => __( 'You must agree to the terms and conditions.', 'give' ),
337
				)
338
			),
339
			'confirm_email_sent_message' => __( 'Please check your email and click on the link to access your complete donation history.', 'give' ),
340
			'ajax_vars'                  => apply_filters( 'give_global_ajax_vars', array(
341
				'ajaxurl'         => give_get_ajax_url(),
342
				'ajaxNonce'       => wp_create_nonce( 'give_ajax_nonce' ),
343
				'loading'         => __( 'Loading', 'give' ),
344
				// General loading message.
345
				'select_option'   => __( 'Please select an option', 'give' ),
346
				// Variable pricing error with multi-donation option enabled.
347
				'default_gateway' => give_get_default_gateway( null ),
348
				'permalinks'      => get_option( 'permalink_structure' ) ? '1' : '0',
349
				'number_decimals' => give_get_price_decimals(),
350
			) ),
351
		) );
352
353
		wp_localize_script( 'give', 'give_global_vars', $localize_give_vars );
354
355
	}
356
357
	/**
358
	 * Get the stylesheet URI.
359
	 *
360
	 * @since   1.6
361
	 * @updated 2.0.1 Moved to class and renamed as method.
362
	 *
363
	 * @return string
364
	 */
365
	public function get_frontend_stylesheet_uri() {
366
367
		$file          = 'give' . $this->direction . '.css';
368
		$templates_dir = give_get_theme_template_dir_name();
369
370
		// Directory paths to CSS files to support checking via file_exists().
371
		$child_theme_style_sheet    = trailingslashit( get_stylesheet_directory() ) . $templates_dir . $file;
372
		$child_theme_style_sheet_2  = trailingslashit( get_stylesheet_directory() ) . $templates_dir . 'give' . $this->direction . '.css';
373
		$parent_theme_style_sheet   = trailingslashit( get_template_directory() ) . $templates_dir . $file;
374
		$parent_theme_style_sheet_2 = trailingslashit( get_template_directory() ) . $templates_dir . 'give' . $this->direction . '.css';
375
		$give_plugin_style_sheet    = trailingslashit( GIVE_PLUGIN_DIR ) . 'assets/dist/css/' . $file;
376
		$uri                        = false;
377
378
		/**
379
		 * Locate the Give stylesheet:
380
		 *
381
		 * a. Look in the child theme directory first, followed by the parent theme
382
		 * b. followed by the Give core templates directory also look for the min version first,
383
		 * c. followed by non minified version, even if SCRIPT_DEBUG is not enabled. This allows users to copy just give.css to their theme.
384
		 * d. Finally, fallback to the standard Give version. This is the default styles included within the plugin.
385
		 */
386
		if ( file_exists( $child_theme_style_sheet ) || ( ! empty( $suffix ) && ( $nonmin = file_exists( $child_theme_style_sheet_2 ) ) ) ) {
387 View Code Duplication
			if ( ! empty( $nonmin ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
388
				$uri = trailingslashit( get_stylesheet_directory_uri() ) . $templates_dir . 'give' . $this->direction . '.css';
389
			} else {
390
				$uri = trailingslashit( get_stylesheet_directory_uri() ) . $templates_dir . $file;
391
			}
392
		} elseif ( file_exists( $parent_theme_style_sheet ) || ( ! empty( $suffix ) && ( $nonmin = file_exists( $parent_theme_style_sheet_2 ) ) ) ) {
393 View Code Duplication
			if ( ! empty( $nonmin ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
394
				$uri = trailingslashit( get_template_directory_uri() ) . $templates_dir . 'give' . $this->direction . '.css';
395
			} else {
396
				$uri = trailingslashit( get_template_directory_uri() ) . $templates_dir . $file;
397
			}
398
		} elseif ( file_exists( $give_plugin_style_sheet ) ) {
399
			$uri = trailingslashit( GIVE_PLUGIN_URL ) . 'assets/dist/css/' . $file;
400
		}
401
402
		return apply_filters( 'give_get_stylesheet_uri', $uri );
403
404
	}
405
406
}
407