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