Completed
Push — add/blocks-package-registratio... ( 1f7e2a...98552c )
by Jeremy
08:34
created

Jetpack_Simple_Payments   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 696
Duplicated Lines 3.88 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 27
loc 696
rs 3.504
c 0
b 0
f 0
wmc 60
lcom 1
cbo 5

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A register_scripts_and_styles() 0 10 1
A register_init_hooks() 0 5 1
A register_shortcode() 0 3 1
A init_hook_action() 0 11 2
A remove_auto_paragraph_from_product_description() 0 7 2
A get_blog_id() 0 7 3
A register_gutenberg_block() 0 14 2
A getInstance() 8 8 4
B is_enabled_jetpack_simple_payments() 4 21 6
C parse_shortcode() 8 64 14
A output_admin_warning() 0 10 2
B output_shortcode() 7 78 7
A format_price() 0 24 4
A allow_rest_api_types() 0 5 1
A allow_sync_post_meta() 0 15 1
A register_meta_fields_in_rest_api() 0 54 1
A sanitize_currency() 0 30 2
A sanitize_price() 0 3 2
B setup_cpts() 0 87 1
B get_currency() 0 129 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Simple_Payments often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Simple_Payments, and based on these observations, apply Extract Interface, too.

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