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