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