Completed
Push — update/pay-with-paypal-save-fa... ( 0ce72f )
by
unknown
12:24
created

Jetpack_Simple_Payments::parse_shortcode()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 51

Duplication

Lines 8
Ratio 15.69 %

Importance

Changes 0
Metric Value
cc 12
nc 10
nop 2
dl 8
loc 51
rs 6.9666
c 0
b 0
f 0

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
2
/*
3
 * Simple Payments lets users embed a PayPal button fully integrated with wpcom to sell products on the site.
4
 * This is not a proper module yet, because not all the pieces are in place. Until everything is shipped, it can be turned
5
 * into module that can be enabled/disabled.
6
*/
7
class Jetpack_Simple_Payments {
8
	// These have to be under 20 chars because that is CPT limit.
9
	static $post_type_order = 'jp_pay_order';
10
	static $post_type_product = 'jp_pay_product';
11
12
	static $shortcode = 'simple-payment';
13
14
	static $css_classname_prefix = 'jetpack-simple-payments';
15
16
	static $required_plan;
17
18
	// Increase this number each time there's a change in CSS or JS to bust cache.
19
	static $version = '0.25';
20
21
	// Classic singleton pattern:
22
	private static $instance;
23
	private function __construct() {}
24 View Code Duplication
	static function getInstance() {
25
		if ( ! self::$instance ) {
26
			self::$instance = new self();
27
			self::$instance->register_init_hooks();
28
			self::$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'value_bundle' : 'jetpack_premium';
29
		}
30
		return self::$instance;
31
	}
32
33
	private function register_scripts_and_styles() {
34
		/**
35
		 * Paypal heavily discourages putting that script in your own server:
36
		 * @see https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/
37
		 */
38
		wp_register_script( 'paypal-checkout-js', 'https://www.paypalobjects.com/api/checkout.js', array(), null, true );
39
		wp_register_script( 'paypal-express-checkout', plugins_url( '/paypal-express-checkout.js', __FILE__ ),
40
			array( 'jquery', 'paypal-checkout-js' ), self::$version );
41
		wp_register_style( 'jetpack-simple-payments', plugins_url( '/simple-payments.css', __FILE__ ), array( 'dashicons' ) );
42
	}
43
44
	private function register_init_hooks() {
45
		add_action( 'init', array( $this, 'init_hook_action' ) );
46
		add_action( 'jetpack_register_gutenberg_extensions', array( $this, 'register_gutenberg_block' ) );
47
		add_action( 'enqueue_block_assets', array( $this, 'enqueue_frontend_assets' ) );
48
		add_action( 'rest_api_init', array( $this, 'register_meta_fields_in_rest_api' ) );
49
	}
50
51
	private function register_shortcode() {
52
		add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) );
53
	}
54
55
	public function init_hook_action() {
56
		add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) );
57
		add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) );
58
		if ( ! is_admin() ) {
59
			$this->register_scripts_and_styles();
60
		}
61
		$this->register_shortcode();
62
		$this->setup_cpts();
63
64
		add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 );
65
	}
66
67
	function register_gutenberg_block() {
68 View Code Duplication
		if ( $this->is_enabled_jetpack_simple_payments() ) {
69
			jetpack_register_block(
70
				'jetpack/simple-payments',
71
				array(
72
					'render_callback' => 'render_gutenberg_block',
73
				)
74
			);
75
		} else {
76
			Jetpack_Gutenberg::set_extension_unavailable(
77
				'jetpack/simple-payments',
78
				'missing_plan',
79
				array(
80
					'required_feature' => 'simple-payments',
81
					'required_plan'    => self::$required_plan,
82
				)
83
			);
84
		}
85
	}
86
87
	/**
88
	 * Enqueue the static assets needed in the frontend.
89
	 */
90
	public function enqueue_frontend_assets() {
91
		if ( ! wp_style_is( 'jetpack-simple-payments', 'enqueued' ) ) {
92
			wp_enqueue_style( 'jetpack-simple-payments' );
93
		}
94
95
		if ( ! wp_script_is( 'paypal-express-checkout', 'enqueued' ) ) {
96
			wp_enqueue_script( 'paypal-express-checkout' );
97
		}
98
	}
99
100
	/**
101
	 * Add an inline script for setting up the PayPal checkout button.
102
	 *
103
	 * @param int     $id Product ID.
104
	 * @param int     $dom_id ID of the DOM element with the purchase message.
105
	 * @param boolean $is_multiple Whether multiple items of the same product can be purchased.
106
	 */
107
	public function setup_paypal_checkout_button( $id, $dom_id, $is_multiple ) {
108
		wp_add_inline_script(
109
			'paypal-express-checkout',
110
			sprintf(
111
				"try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}",
112
				esc_js( $this->get_blog_id() ),
113
				esc_js( $id ),
114
				esc_js( $dom_id ),
115
				esc_js( $is_multiple )
116
			)
117
		);
118
	}
119
120
	/**
121
	 * Render callback of the Pay with PayPal block.
122
	 *
123
	 * @param array  $attributes Block attributes.
124
	 * @param string $content string Block content.
125
	 *
126
	 * @return mixed
127
	 */
128
	public function render_gutenberg_block( $attributes, $content ) {
129
		$product_id = $attributes['productId'];
130
		$this->setup_paypal_checkout_button(
131
			$product_id,
132
			uniqid( self::$css_classname_prefix . '-' . $product_id . '_', true ),
133
			get_post_meta( $product_id, 'spay_multiple', true ) || '0'
134
		);
135
		return $content;
136
	}
137
138
	function remove_auto_paragraph_from_product_description( $content ) {
139
		if ( get_post_type() === self::$post_type_product ) {
140
			remove_filter( 'the_content', 'wpautop' );
141
		}
142
143
		return $content;
144
	}
145
146
	function get_blog_id() {
147
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
148
			return get_current_blog_id();
149
		}
150
151
		return Jetpack_Options::get_option( 'id' );
152
	}
153
154
	/**
155
	 * Used to check whether Simple Payments are enabled for given site.
156
	 *
157
	 * @return bool True if Simple Payments are enabled, false otherwise.
158
	 */
159
	function is_enabled_jetpack_simple_payments() {
160
		/**
161
		 * Can be used by plugin authors to disable the conflicting output of Simple Payments.
162
		 *
163
		 * @since 6.3.0
164
		 *
165
		 * @param bool True if Simple Payments should be disabled, false otherwise.
166
		 */
167
		if ( apply_filters( 'jetpack_disable_simple_payments', false ) ) {
168
			return false;
169
		}
170
171
		// For WPCOM sites
172 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_any_blog_stickers' ) ) {
173
			$site_id = $this->get_blog_id();
174
			return has_any_blog_stickers( array( 'premium-plan', 'business-plan', 'ecommerce-plan' ), $site_id );
175
		}
176
177
		// For all Jetpack sites
178
		return Jetpack::is_active() && Jetpack_Plan::supports( 'simple-payments');
179
	}
180
181
	function parse_shortcode( $attrs, $content = false ) {
182
		if ( empty( $attrs['id'] ) ) {
183
			return;
184
		}
185
		$product = get_post( $attrs['id'] );
186
		if ( ! $product || is_wp_error( $product ) ) {
187
			return;
188
		}
189
		if ( $product->post_type !== self::$post_type_product || 'publish' !== $product->post_status ) {
190
			return;
191
		}
192
193
		// We allow for overriding the presentation labels
194
		$data = shortcode_atts( array(
195
			'blog_id'     => $this->get_blog_id(),
196
			'dom_id'      => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ),
197
			'class'       => self::$css_classname_prefix . '-' . $product->ID,
198
			'title'       => get_the_title( $product ),
199
			'description' => $product->post_content,
200
			'cta'         => get_post_meta( $product->ID, 'spay_cta', true ),
201
			'multiple'    => get_post_meta( $product->ID, 'spay_multiple', true ) || '0'
202
		), $attrs );
203
204
		$data['price'] = $this->format_price(
205
			get_post_meta( $product->ID, 'spay_price', true ),
206
			get_post_meta( $product->ID, 'spay_currency', true )
207
		);
208
209
		$data['id'] = $attrs['id'];
210
211 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
212
			require_once WP_CONTENT_DIR . '/lib/display-context.php';
213
			$context = \A8C\Display_Context\get_current_context();
214
			if ( \A8C\Display_Context\EMAIL === $context ) {
215
				// Avoid enqueueing unsupported files by emails.
216
				return $this->output_shortcode( $data );
217
			}
218
		}
219
220
		if ( ! $this->is_enabled_jetpack_simple_payments() ) {
221
			if ( ! is_feed() ) {
222
				return $this->output_admin_warning( $data );
223
			}
224
			return;
225
		}
226
227
		$this->enqueue_frontend_assets();
228
		$this->setup_paypal_checkout_button( $attrs['id'], $data['dom_id'], $data['multiple'] );
229
230
		return $this->output_shortcode( $data );
231
	}
232
233
	function output_admin_warning( $data ) {
234
		if ( ! current_user_can( 'manage_options' ) ) {
235
			return;
236
		}
237
238
		jetpack_require_lib( 'components' );
239
		return Jetpack_Components::render_upgrade_nudge( array(
240
			'plan' => self::$required_plan
241
		) );
242
	}
243
244
	function output_shortcode( $data ) {
245
		$items = '';
246
		$css_prefix = self::$css_classname_prefix;
247
248
		$is_paypal_button_visible = true;
249 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
250
			require_once WP_CONTENT_DIR . '/lib/display-context.php';
251
			$context = \A8C\Display_Context\get_current_context();
252
			if ( \A8C\Display_Context\EMAIL === $context ) {
253
				$is_paypal_button_visible = false;
254
			}
255
		}
256
257
		if ( $data['multiple'] ) {
258
			$items = sprintf( '
259
				<div class="%1$s">
260
					<input class="%2$s" type="number" value="1" min="1" id="%3$s" />
261
				</div>
262
				',
263
				esc_attr( "${css_prefix}-items" ),
264
				esc_attr( "${css_prefix}-items-number" ),
265
				esc_attr( "{$data['dom_id']}_number" )
266
			);
267
		}
268
269
		$image = "";
270
		if( has_post_thumbnail( $data['id'] ) ) {
271
			$image = sprintf( '<div class="%1$s"><div class="%2$s">%3$s</div></div>',
272
				esc_attr( "${css_prefix}-product-image" ),
273
				esc_attr( "${css_prefix}-image" ),
274
				get_the_post_thumbnail( $data['id'], 'full' )
275
			);
276
		}
277
278
		if ( $is_paypal_button_visible ) {
279
			$purchase_box = sprintf(
280
				'<div class="%1$s">%2$s<div class="%3$s" id="%4$s"></div></div>',
281
				esc_attr( "${css_prefix}-purchase-box" ),
282
				$items,
283
				esc_attr( "${css_prefix}-button" ),
284
				esc_attr( "{$data['dom_id']}_button" )
285
			);
286
		} else {
287
			$purchase_box = sprintf(
288
				'<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>',
289
				esc_url( get_permalink( get_the_ID() ) ),
290
				__( 'Visit the site to purchase.', 'jetpack' )
291
			);
292
		}
293
		return sprintf( '
294
<div class="%1$s">
295
	<div class="%2$s">
296
		%3$s
297
		<div class="%4$s">
298
			<div class="%5$s"><p>%6$s</p></div>
299
			<div class="%7$s"><p>%8$s</p></div>
300
			<div class="%9$s"><p>%10$s</p></div>
301
			<div class="%11$s" id="%12$s"></div>
302
			%13$s
303
		</div>
304
	</div>
305
</div>
306
',
307
			esc_attr( "{$data['class']} ${css_prefix}-wrapper" ),
308
			esc_attr( "${css_prefix}-product" ),
309
			$image,
310
			esc_attr( "${css_prefix}-details" ),
311
			esc_attr( "${css_prefix}-title" ),
312
			esc_html( $data['title'] ),
313
			esc_attr( "${css_prefix}-description" ),
314
			wp_kses( $data['description'], wp_kses_allowed_html( 'post' ) ),
315
			esc_attr( "${css_prefix}-price" ),
316
			esc_html( $data['price'] ),
317
			esc_attr( "${css_prefix}-purchase-message" ),
318
			esc_attr( "{$data['dom_id']}-message-container" ),
319
			$purchase_box
320
		);
321
	}
322
323
	/**
324
	 * Format a price with currency
325
	 *
326
	 * Uses currency-aware formatting to output a formatted price with a simple fallback.
327
	 *
328
	 * Largely inspired by WordPress.com's Store_Price::display_currency
329
	 *
330
	 * @param  string $price    Price.
331
	 * @param  string $currency Currency.
332
	 * @return string           Formatted price.
333
	 */
334
	private function format_price( $price, $currency ) {
335
		$currency_details = self::get_currency( $currency );
336
337
		if ( $currency_details ) {
338
			// Ensure USD displays as 1234.56 even in non-US locales.
339
			$amount = 'USD' === $currency
340
				? number_format( $price, $currency_details['decimal'], '.', ',' )
341
				: number_format_i18n( $price, $currency_details['decimal'] );
342
343
			return sprintf(
344
				$currency_details['format'],
345
				$currency_details['symbol'],
346
				$amount
347
			);
348
		}
349
350
		// Fall back to unspecified currency symbol like `¤1,234.05`.
351
		// @link https://en.wikipedia.org/wiki/Currency_sign_(typography).
352
		if ( ! $currency ) {
353
			return '¤' . number_format_i18n( $price, 2 );
354
		}
355
356
		return number_format_i18n( $price, 2 ) . ' ' . $currency;
357
	}
358
359
	/**
360
	 * Allows custom post types to be used by REST API.
361
	 * @param $post_types
362
	 * @see hook 'rest_api_allowed_post_types'
363
	 * @return array
364
	 */
365
	function allow_rest_api_types( $post_types ) {
366
		$post_types[] = self::$post_type_order;
367
		$post_types[] = self::$post_type_product;
368
		return $post_types;
369
	}
370
371
	function allow_sync_post_meta( $post_meta ) {
372
		return array_merge( $post_meta, array(
373
			'spay_paypal_id',
374
			'spay_status',
375
			'spay_product_id',
376
			'spay_quantity',
377
			'spay_price',
378
			'spay_customer_email',
379
			'spay_currency',
380
			'spay_cta',
381
			'spay_email',
382
			'spay_multiple',
383
			'spay_formatted_price',
384
		) );
385
	}
386
387
	/**
388
	 * Enable Simple payments custom meta values for access through the REST API.
389
	 * Field’s value will be exposed on a .meta key in the endpoint response,
390
	 * and WordPress will handle setting up the callbacks for reading and writing
391
	 * to that meta key.
392
	 *
393
	 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
394
	 */
395
	public function register_meta_fields_in_rest_api() {
396
		register_meta( 'post', 'spay_price', array(
397
			'description'       => esc_html__( 'Simple payments; price.', 'jetpack' ),
398
			'object_subtype'    => self::$post_type_product,
399
			'sanitize_callback' => array( $this, 'sanitize_price' ),
400
			'show_in_rest'      => true,
401
			'single'            => true,
402
			'type'              => 'number',
403
		) );
404
405
		register_meta( 'post', 'spay_currency', array(
406
			'description'       => esc_html__( 'Simple payments; currency code.', 'jetpack' ),
407
			'object_subtype'    => self::$post_type_product,
408
			'sanitize_callback' => array( $this, 'sanitize_currency' ),
409
			'show_in_rest'      => true,
410
			'single'            => true,
411
			'type'              => 'string',
412
		) );
413
414
		register_meta( 'post', 'spay_cta', array(
415
			'description'       => esc_html__( 'Simple payments; text with "Buy" or other CTA', 'jetpack' ),
416
			'object_subtype'    => self::$post_type_product,
417
			'sanitize_callback' => 'sanitize_text_field',
418
			'show_in_rest'      => true,
419
			'single'            => true,
420
			'type'              => 'string',
421
		) );
422
423
		register_meta( 'post', 'spay_multiple', array(
424
			'description'       => esc_html__( 'Simple payments; allow multiple items', 'jetpack' ),
425
			'object_subtype'    => self::$post_type_product,
426
			'sanitize_callback' => 'rest_sanitize_boolean',
427
			'show_in_rest'      => true,
428
			'single'            => true,
429
			'type'              => 'boolean',
430
		) );
431
432
		register_meta( 'post', 'spay_email', array(
433
			'description'       => esc_html__( 'Simple payments button; paypal email.', 'jetpack' ),
434
			'sanitize_callback' => 'sanitize_email',
435
			'show_in_rest'      => true,
436
			'single'            => true,
437
			'type'              => 'string',
438
		) );
439
440
		register_meta( 'post', 'spay_status', array(
441
			'description'       => esc_html__( 'Simple payments; status.', 'jetpack' ),
442
			'object_subtype'    => self::$post_type_product,
443
			'sanitize_callback' => 'sanitize_text_field',
444
			'show_in_rest'      => true,
445
			'single'            => true,
446
			'type'              => 'string',
447
		) );
448
	}
449
450
	/**
451
	 * Sanitize three-character ISO-4217 Simple payments currency
452
	 *
453
	 * List has to be in sync with list at the block's client side and widget's backend side:
454
	 * @link https://github.com/Automattic/jetpack/blob/31efa189ad223c0eb7ad085ac0650a23facf9ef5/extensions/blocks/simple-payments/constants.js#L9-L39
455
	 * @link https://github.com/Automattic/jetpack/blob/31efa189ad223c0eb7ad085ac0650a23facf9ef5/modules/widgets/simple-payments.php#L19-L44
456
	 *
457
	 * Currencies should be supported by PayPal:
458
	 * @link https://developer.paypal.com/docs/api/reference/currency-codes/
459
	 *
460
	 * Indian Rupee (INR) not supported because at the time of the creation of this file
461
	 * because it's limited to in-country PayPal India accounts only.
462
	 * Discussion: https://github.com/Automattic/wp-calypso/pull/28236
463
	 */
464
	public static function sanitize_currency( $currency ) {
465
		$valid_currencies = array(
466
			'USD',
467
			'EUR',
468
			'AUD',
469
			'BRL',
470
			'CAD',
471
			'CZK',
472
			'DKK',
473
			'HKD',
474
			'HUF',
475
			'ILS',
476
			'JPY',
477
			'MYR',
478
			'MXN',
479
			'TWD',
480
			'NZD',
481
			'NOK',
482
			'PHP',
483
			'PLN',
484
			'GBP',
485
			'RUB',
486
			'SGD',
487
			'SEK',
488
			'CHF',
489
			'THB',
490
		);
491
492
		return in_array( $currency, $valid_currencies ) ? $currency : false;
493
	}
494
495
	/**
496
	 * Sanitize price:
497
	 *
498
	 * Positive integers and floats
499
	 * Supports two decimal places.
500
	 * Maximum length: 10.
501
	 *
502
	 * See `price` from PayPal docs:
503
	 * @link https://developer.paypal.com/docs/api/orders/v1/#definition-item
504
	 *
505
	 * @param      $value
506
	 * @return null|string
507
	 */
508
	public static function sanitize_price( $price ) {
509
		return preg_match( '/^[0-9]{0,10}(\.[0-9]{0,2})?$/', $price ) ? $price : false;
510
	}
511
512
	/**
513
	 * Sets up the custom post types for the module.
514
	 */
515
	function setup_cpts() {
516
517
		/*
518
		 * ORDER data structure. holds:
519
		 * title = customer_name | 4xproduct_name
520
		 * excerpt = customer_name + customer contact info + customer notes from paypal form
521
		 * metadata:
522
		 * spay_paypal_id - paypal id of transaction
523
		 * spay_status
524
		 * spay_product_id - post_id of bought product
525
		 * spay_quantity - quantity of product
526
		 * spay_price - item price at the time of purchase
527
		 * spay_customer_email - customer email
528
		 * ... (WIP)
529
		 */
530
		$order_capabilities = array(
531
			'edit_post'             => 'edit_posts',
532
			'read_post'             => 'read_private_posts',
533
			'delete_post'           => 'delete_posts',
534
			'edit_posts'            => 'edit_posts',
535
			'edit_others_posts'     => 'edit_others_posts',
536
			'publish_posts'         => 'publish_posts',
537
			'read_private_posts'    => 'read_private_posts',
538
		);
539
		$order_args = array(
540
			'label'                 => esc_html_x( 'Order', 'noun: a quantity of goods or items purchased or sold', 'jetpack' ),
541
			'description'           => esc_html__( 'Simple Payments orders', 'jetpack' ),
542
			'supports'              => array( 'custom-fields', 'excerpt' ),
543
			'hierarchical'          => false,
544
			'public'                => false,
545
			'show_ui'               => false,
546
			'show_in_menu'          => false,
547
			'show_in_admin_bar'     => false,
548
			'show_in_nav_menus'     => false,
549
			'can_export'            => true,
550
			'has_archive'           => false,
551
			'exclude_from_search'   => true,
552
			'publicly_queryable'    => false,
553
			'rewrite'               => false,
554
			'capabilities'          => $order_capabilities,
555
			'show_in_rest'          => true,
556
		);
557
		register_post_type( self::$post_type_order, $order_args );
558
559
		/*
560
		 * PRODUCT data structure. Holds:
561
		 * title - title
562
		 * content - description
563
		 * thumbnail - image
564
		 * metadata:
565
		 * spay_price - price
566
		 * spay_formatted_price
567
		 * spay_currency - currency code
568
		 * spay_cta - text with "Buy" or other CTA
569
		 * spay_email - paypal email
570
		 * spay_multiple - allow for multiple items
571
		 * spay_status - status. { enabled | disabled }
572
		 */
573
		$product_capabilities = array(
574
			'edit_post'             => 'edit_posts',
575
			'read_post'             => 'read_private_posts',
576
			'delete_post'           => 'delete_posts',
577
			'edit_posts'            => 'publish_posts',
578
			'edit_others_posts'     => 'edit_others_posts',
579
			'publish_posts'         => 'publish_posts',
580
			'read_private_posts'    => 'read_private_posts',
581
		);
582
		$product_args = array(
583
			'label'                 => esc_html__( 'Product', 'jetpack' ),
584
			'description'           => esc_html__( 'Simple Payments products', 'jetpack' ),
585
			'supports'              => array( 'title', 'editor','thumbnail', 'custom-fields', 'author' ),
586
			'hierarchical'          => false,
587
			'public'                => false,
588
			'show_ui'               => false,
589
			'show_in_menu'          => false,
590
			'show_in_admin_bar'     => false,
591
			'show_in_nav_menus'     => false,
592
			'can_export'            => true,
593
			'has_archive'           => false,
594
			'exclude_from_search'   => true,
595
			'publicly_queryable'    => false,
596
			'rewrite'               => false,
597
			'capabilities'          => $product_capabilities,
598
			'show_in_rest'          => true,
599
		);
600
		register_post_type( self::$post_type_product, $product_args );
601
	}
602
603
	/**
604
	 * Format a price for display
605
	 *
606
	 * Largely taken from WordPress.com Store_Price class
607
	 *
608
	 * The currency array will have the shape:
609
	 *   format  => string sprintf format with placeholders `%1$s`: Symbol `%2$s`: Price.
610
	 *   symbol  => string Symbol string
611
	 *   desc    => string Text description of currency
612
	 *   decimal => int    Number of decimal places
613
	 *
614
	 * @param  string $the_currency The desired currency, e.g. 'USD'.
615
	 * @return ?array               Currency object or null if not found.
0 ignored issues
show
Documentation introduced by
The doc-type ?array could not be parsed: Unknown type name "?array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
616
	 */
617
	private static function get_currency( $the_currency ) {
618
		$currencies = array(
619
			'USD' => array(
620
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
621
				'symbol'  => '$',
622
				'decimal' => 2,
623
			),
624
			'GBP' => array(
625
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
626
				'symbol'  => '&#163;',
627
				'decimal' => 2,
628
			),
629
			'JPY' => array(
630
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
631
				'symbol'  => '&#165;',
632
				'decimal' => 0,
633
			),
634
			'BRL' => array(
635
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
636
				'symbol'  => 'R$',
637
				'decimal' => 2,
638
			),
639
			'EUR' => array(
640
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
641
				'symbol'  => '&#8364;',
642
				'decimal' => 2,
643
			),
644
			'NZD' => array(
645
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
646
				'symbol'  => 'NZ$',
647
				'decimal' => 2,
648
			),
649
			'AUD' => array(
650
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
651
				'symbol'  => 'A$',
652
				'decimal' => 2,
653
			),
654
			'CAD' => array(
655
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
656
				'symbol'  => 'C$',
657
				'decimal' => 2,
658
			),
659
			'ILS' => array(
660
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
661
				'symbol'  => '₪',
662
				'decimal' => 2,
663
			),
664
			'RUB' => array(
665
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
666
				'symbol'  => '₽',
667
				'decimal' => 2,
668
			),
669
			'MXN' => array(
670
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
671
				'symbol'  => 'MX$',
672
				'decimal' => 2,
673
			),
674
			'MYR' => array(
675
				'format'  => '%2$s%1$s', // 1: Symbol 2: currency value
676
				'symbol'  => 'RM',
677
				'decimal' => 2,
678
			),
679
			'SEK' => array(
680
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
681
				'symbol'  => 'Skr',
682
				'decimal' => 2,
683
			),
684
			'HUF' => array(
685
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
686
				'symbol'  => 'Ft',
687
				'decimal' => 0, // Decimals are supported by Stripe but not by PayPal.
688
			),
689
			'CHF' => array(
690
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
691
				'symbol'  => 'CHF',
692
				'decimal' => 2,
693
			),
694
			'CZK' => array(
695
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
696
				'symbol'  => 'Kč',
697
				'decimal' => 2,
698
			),
699
			'DKK' => array(
700
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
701
				'symbol'  => 'Dkr',
702
				'decimal' => 2,
703
			),
704
			'HKD' => array(
705
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
706
				'symbol'  => 'HK$',
707
				'decimal' => 2,
708
			),
709
			'NOK' => array(
710
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
711
				'symbol'  => 'Kr',
712
				'decimal' => 2,
713
			),
714
			'PHP' => array(
715
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
716
				'symbol'  => '₱',
717
				'decimal' => 2,
718
			),
719
			'PLN' => array(
720
				'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
721
				'symbol'  => 'PLN',
722
				'decimal' => 2,
723
			),
724
			'SGD' => array(
725
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
726
				'symbol'  => 'S$',
727
				'decimal' => 2,
728
			),
729
			'TWD' => array(
730
				'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
731
				'symbol'  => 'NT$',
732
				'decimal' => 0, // Decimals are supported by Stripe but not by PayPal.
733
			),
734
			'THB' => array(
735
				'format'  => '%2$s%1$s', // 1: Symbol 2: currency value
736
				'symbol'  => '฿',
737
				'decimal' => 2,
738
			),
739
		);
740
741
		if ( isset( $currencies[ $the_currency ] ) ) {
742
			return $currencies[ $the_currency ];
743
		}
744
		return null;
745
	}
746
}
747
Jetpack_Simple_Payments::getInstance();
748