Completed
Push — issue/339 ( c16360 )
by Ravinder
511:00 queued 502:28
created

Give_Scripts::admin_enqueue_styles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
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 level?', '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
		) );
244
	}
245
246
	/**
247
	 * Global admin head.
248
	 */
249
	public function global_admin_head() {
250
		?>
251
		<style type="text/css" media="screen">
252
			@font-face {
253
				font-family: 'give-icomoon';
254
				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...
255
				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...
256
				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...
257
				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...
258
				font-weight: normal;
259
				font-style: normal;
260
			}
261
262
			.dashicons-give:before, #adminmenu div.wp-menu-image.dashicons-give:before {
263
				font-family: 'give-icomoon';
264
				font-size: 18px;
265
				width: 18px;
266
				height: 18px;
267
				content: "\e800";
268
			}
269
		</style>
270
		<?php
271
272
	}
273
274
	/**
275
	 * Enqueues public styles.
276
	 *
277
	 * @since 2.1.0
278
	 */
279
	public function public_enqueue_styles() {
280
		wp_enqueue_style( 'give-styles' );
281
	}
282
283
284
	/**
285
	 * Enqueues public scripts.
286
	 *
287
	 * @since 2.1.0
288
	 */
289
	public function public_enqueue_scripts() {
290
		wp_enqueue_script( 'give' );
291
292
		$this->public_localize_scripts();
293
	}
294
295
	/**
296
	 * Localize / PHP to AJAX vars.
297
	 */
298
	public function public_localize_scripts() {
299
300
		$localize_give_vars = apply_filters( 'give_global_script_vars', array(
301
			'ajaxurl'                    => give_get_ajax_url(),
302
			'checkout_nonce'             => wp_create_nonce( 'give_checkout_nonce' ), // Do not use this nonce. Its deprecated.
303
			'currency'                   => give_get_currency(),
304
			'currency_sign'              => give_currency_filter( '' ),
305
			'currency_pos'               => give_get_currency_position(),
306
			'thousands_separator'        => give_get_price_thousand_separator(),
307
			'decimal_separator'          => give_get_price_decimal_separator(),
308
			'no_gateway'                 => __( 'Please select a payment method.', 'give' ),
309
			'bad_minimum'                => __( 'The minimum custom donation amount for this form is', 'give' ),
310
			'general_loading'            => __( 'Loading...', 'give' ),
311
			'purchase_loading'           => __( 'Please Wait...', 'give' ),
312
			'number_decimals'            => give_get_price_decimals(),
313
			'give_version'               => GIVE_VERSION,
314
			'magnific_options'           => apply_filters(
315
				'give_magnific_options',
316
				array(
317
					'main_class'        => 'give-modal',
318
					'close_on_bg_click' => false,
319
				)
320
			),
321
			'form_translation'           => apply_filters(
322
				'give_form_translation_js',
323
				array(
324
					// Field name               Validation message.
325
					'payment-mode'           => __( 'Please select payment mode.', 'give' ),
326
					'give_first'             => __( 'Please enter your first name.', 'give' ),
327
					'give_email'             => __( 'Please enter a valid email address.', 'give' ),
328
					'give_user_login'        => __( 'Invalid username. Only lowercase letters (a-z) and numbers are allowed.', 'give' ),
329
					'give_user_pass'         => __( 'Enter a password.', 'give' ),
330
					'give_user_pass_confirm' => __( 'Enter the password confirmation.', 'give' ),
331
					'give_agree_to_terms'    => __( 'You must agree to the terms and conditions.', 'give' ),
332
				)
333
			),
334
			'confirm_email_sent_message' => __( 'Please check your email and click on the link to access your complete donation history.', 'give' ),
335
			'ajax_vars'                  => apply_filters( 'give_global_ajax_vars', array(
336
				'ajaxurl'         => give_get_ajax_url(),
337
				'ajaxNonce'       => wp_create_nonce( 'give_ajax_nonce' ),
338
				'loading'         => __( 'Loading', 'give' ),
339
				// General loading message.
340
				'select_option'   => __( 'Please select an option', 'give' ),
341
				// Variable pricing error with multi-donation option enabled.
342
				'default_gateway' => give_get_default_gateway( null ),
343
				'permalinks'      => get_option( 'permalink_structure' ) ? '1' : '0',
344
				'number_decimals' => give_get_price_decimals(),
345
			) ),
346
		) );
347
348
		wp_localize_script( 'give', 'give_global_vars', $localize_give_vars );
349
350
	}
351
352
	/**
353
	 * Get the stylesheet URI.
354
	 *
355
	 * @since   1.6
356
	 * @updated 2.0.1 Moved to class and renamed as method.
357
	 *
358
	 * @return string
359
	 */
360
	public function get_frontend_stylesheet_uri() {
361
362
		$file          = 'give' . $this->direction . '.css';
363
		$templates_dir = give_get_theme_template_dir_name();
364
365
		// Directory paths to CSS files to support checking via file_exists().
366
		$child_theme_style_sheet    = trailingslashit( get_stylesheet_directory() ) . $templates_dir . $file;
367
		$child_theme_style_sheet_2  = trailingslashit( get_stylesheet_directory() ) . $templates_dir . 'give' . $this->direction . '.css';
368
		$parent_theme_style_sheet   = trailingslashit( get_template_directory() ) . $templates_dir . $file;
369
		$parent_theme_style_sheet_2 = trailingslashit( get_template_directory() ) . $templates_dir . 'give' . $this->direction . '.css';
370
		$give_plugin_style_sheet    = trailingslashit( GIVE_PLUGIN_DIR ) . 'assets/dist/css/' . $file;
371
		$uri                        = false;
372
373
		/**
374
		 * Locate the Give stylesheet:
375
		 *
376
		 * a. Look in the child theme directory first, followed by the parent theme
377
		 * b. followed by the Give core templates directory also look for the min version first,
378
		 * c. followed by non minified version, even if SCRIPT_DEBUG is not enabled. This allows users to copy just give.css to their theme.
379
		 * d. Finally, fallback to the standard Give version. This is the default styles included within the plugin.
380
		 */
381
		if ( file_exists( $child_theme_style_sheet ) || ( ! empty( $suffix ) && ( $nonmin = file_exists( $child_theme_style_sheet_2 ) ) ) ) {
382 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...
383
				$uri = trailingslashit( get_stylesheet_directory_uri() ) . $templates_dir . 'give' . $this->direction . '.css';
384
			} else {
385
				$uri = trailingslashit( get_stylesheet_directory_uri() ) . $templates_dir . $file;
386
			}
387
		} elseif ( file_exists( $parent_theme_style_sheet ) || ( ! empty( $suffix ) && ( $nonmin = file_exists( $parent_theme_style_sheet_2 ) ) ) ) {
388 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...
389
				$uri = trailingslashit( get_template_directory_uri() ) . $templates_dir . 'give' . $this->direction . '.css';
390
			} else {
391
				$uri = trailingslashit( get_template_directory_uri() ) . $templates_dir . $file;
392
			}
393
		} elseif ( file_exists( $give_plugin_style_sheet ) ) {
394
			$uri = trailingslashit( GIVE_PLUGIN_URL ) . 'assets/dist/css/' . $file;
395
		}
396
397
		return apply_filters( 'give_get_stylesheet_uri', $uri );
398
399
	}
400
401
}
402