PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 13.2.4
Jetpack – WP Security, Backup, Speed, & Growth v13.2.4
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / simple-payments / simple-payments.php
jetpack / modules / simple-payments Last commit date
paypal-express-checkout.js 4 years ago simple-payments.css 4 years ago simple-payments.php 3 years ago
simple-payments.php
805 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
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 * @package automattic/jetpack
8 */
9
10 use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
11
12 /**
13 * Jetpack_Simple_Payments
14 */
15 class Jetpack_Simple_Payments {
16 // These have to be under 20 chars because that is CPT limit.
17
18 /**
19 * Post type order.
20 *
21 * @var string
22 */
23 public static $post_type_order = 'jp_pay_order';
24
25 /**
26 * Post type product.
27 *
28 * @var string
29 */
30 public static $post_type_product = 'jp_pay_product';
31
32 /**
33 * Define simple payment shortcode.
34 *
35 * @var string
36 */
37 public static $shortcode = 'simple-payment';
38
39 /**
40 * Define simple payment CSS prefix.
41 *
42 * @var string
43 */
44 public static $css_classname_prefix = 'jetpack-simple-payments';
45
46 /**
47 * Which plan the user is on.
48 *
49 * @var string value_bundle or jetpack_premium
50 */
51 public static $required_plan;
52
53 /**
54 * Instance of the class.
55 *
56 * @var Jetpack_Simple_Payments
57 */
58 private static $instance;
59
60 /**
61 * Construction function.
62 */
63 private function __construct() {}
64
65 /**
66 * Original singleton.
67 *
68 * @todo Remove this when nothing calles getInstance anymore.
69 *
70 * @deprecated 10.8
71 */
72 public static function getInstance() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
73 _deprecated_function( __METHOD__, 'Jetpack 10.7.0', 'Jetpack_Simple_Payments::get_instance' );
74 return self::get_instance();
75 }
76
77 /**
78 * Create instance of class.
79 */
80 public static function get_instance() {
81 if ( ! self::$instance ) {
82 self::$instance = new self();
83 self::$instance->register_init_hooks();
84 self::$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'value_bundle' : 'jetpack_premium';
85 }
86 return self::$instance;
87 }
88
89 /**
90 * Register scripts and styles.
91 */
92 private function register_scripts_and_styles() {
93 /**
94 * Paypal heavily discourages putting that script in your own server:
95 *
96 * @see https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/
97 */
98 wp_register_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Ignored here instead of on the $ver param line since wpcom isn't in sync with ruleset changes in: https://github.com/Automattic/jetpack/pull/28199
99 'paypal-checkout-js',
100 'https://www.paypalobjects.com/api/checkout.js',
101 array(),
102 null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
103 true
104 );
105 wp_register_script(
106 'jetpack-paypal-express-checkout',
107 plugins_url( '/paypal-express-checkout.js', __FILE__ ),
108 array( 'jquery', 'paypal-checkout-js' ),
109 JETPACK__VERSION,
110 false
111 );
112 wp_register_style(
113 'jetpack-simple-payments',
114 plugins_url( '/simple-payments.css', __FILE__ ),
115 array( 'dashicons' ),
116 JETPACK__VERSION,
117 false
118 );
119 }
120
121 /**
122 * Register init hooks.
123 */
124 private function register_init_hooks() {
125 add_action( 'init', array( $this, 'init_hook_action' ) );
126 add_action( 'rest_api_init', array( $this, 'register_meta_fields_in_rest_api' ) );
127 }
128
129 /**
130 * Register the shortcode.
131 */
132 private function register_shortcode() {
133 add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) );
134 }
135
136 /**
137 * Actions that are run on init.
138 */
139 public function init_hook_action() {
140 add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) );
141 add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) );
142 if ( ! is_admin() ) {
143 $this->register_scripts_and_styles();
144 }
145 $this->register_shortcode();
146 $this->setup_cpts();
147
148 add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 );
149 }
150
151 /**
152 * Enqueue the static assets needed in the frontend.
153 */
154 public function enqueue_frontend_assets() {
155 if ( ! wp_style_is( 'jetpack-simple-payments', 'enqueued' ) ) {
156 wp_enqueue_style( 'jetpack-simple-payments' );
157 }
158
159 if ( ! wp_script_is( 'jetpack-paypal-express-checkout', 'enqueued' ) ) {
160 wp_enqueue_script( 'jetpack-paypal-express-checkout' );
161 }
162 }
163
164 /**
165 * Add an inline script for setting up the PayPal checkout button.
166 *
167 * @param int $id Product ID.
168 * @param int $dom_id ID of the DOM element with the purchase message.
169 * @param boolean $is_multiple Whether multiple items of the same product can be purchased.
170 */
171 public function setup_paypal_checkout_button( $id, $dom_id, $is_multiple ) {
172 wp_add_inline_script(
173 'jetpack-paypal-express-checkout',
174 sprintf(
175 "try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}",
176 esc_js( $this->get_blog_id() ),
177 esc_js( $id ),
178 esc_js( $dom_id ),
179 esc_js( $is_multiple )
180 )
181 );
182 }
183
184 /**
185 * Remove auto paragraph from product description.
186 *
187 * @param string $content - the content of the post.
188 */
189 public function remove_auto_paragraph_from_product_description( $content ) {
190 if ( get_post_type() === self::$post_type_product ) {
191 remove_filter( 'the_content', 'wpautop' );
192 }
193
194 return $content;
195 }
196
197 /** Return the blog ID */
198 public function get_blog_id() {
199 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
200 return get_current_blog_id();
201 }
202
203 return Jetpack_Options::get_option( 'id' );
204 }
205
206 /**
207 * Used to check whether Simple Payments are enabled for given site.
208 *
209 * @return bool True if Simple Payments are enabled, false otherwise.
210 */
211 public function is_enabled_jetpack_simple_payments() {
212 /**
213 * Can be used by plugin authors to disable the conflicting output of Simple Payments.
214 *
215 * @since 6.3.0
216 *
217 * @param bool True if Simple Payments should be disabled, false otherwise.
218 */
219 if ( apply_filters( 'jetpack_disable_simple_payments', false ) ) {
220 return false;
221 }
222
223 return ( ( defined( 'IS_WPCOM' ) && IS_WPCOM )
224 || Jetpack::is_connection_ready() )
225 &&
226 Jetpack_Plan::supports( 'simple-payments' );
227 }
228
229 /**
230 * Get a WP_Post representation of a product
231 *
232 * @param int $id The ID of the product.
233 *
234 * @return array|false|WP_Post
235 */
236 private function get_product( $id ) {
237 if ( ! $id ) {
238 return false;
239 }
240
241 $product = get_post( $id );
242 if ( ! $product || is_wp_error( $product ) ) {
243 return false;
244 }
245 if ( $product->post_type !== self::$post_type_product || 'publish' !== $product->post_status ) {
246 return false;
247 }
248 return $product;
249 }
250
251 /**
252 * Creates the content from a shortcode
253 *
254 * @param array $attrs Shortcode attributes.
255 * @param mixed $content unused.
256 *
257 * @return string|void
258 */
259 public function parse_shortcode( $attrs, $content = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
260 if ( empty( $attrs['id'] ) ) {
261 return;
262 }
263 $product = $this->get_product( $attrs['id'] );
264 if ( ! $product ) {
265 return;
266 }
267
268 // We allow for overriding the presentation labels.
269 $data = shortcode_atts(
270 array(
271 'blog_id' => $this->get_blog_id(),
272 'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ),
273 'class' => self::$css_classname_prefix . '-' . $product->ID,
274 'title' => get_the_title( $product ),
275 'description' => $product->post_content,
276 'cta' => get_post_meta( $product->ID, 'spay_cta', true ),
277 'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0',
278 ),
279 $attrs
280 );
281
282 $data['price'] = $this->format_price(
283 get_post_meta( $product->ID, 'spay_price', true ),
284 get_post_meta( $product->ID, 'spay_currency', true )
285 );
286
287 $data['id'] = $attrs['id'];
288
289 if ( ! $this->is_enabled_jetpack_simple_payments() ) {
290 if ( jetpack_is_frontend() ) {
291 return $this->output_admin_warning( $data );
292 }
293 return;
294 }
295
296 $this->enqueue_frontend_assets();
297 $this->setup_paypal_checkout_button( $attrs['id'], $data['dom_id'], $data['multiple'] );
298
299 return $this->output_shortcode( $data );
300 }
301
302 /**
303 * Output an admin warning if user can't use Pay with PayPal.
304 *
305 * @param array $data unused.
306 */
307 public function output_admin_warning( $data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
308 if ( ! current_user_can( 'manage_options' ) ) {
309 return;
310 }
311
312 require_once JETPACK__PLUGIN_DIR . '_inc/lib/components.php';
313 return Jetpack_Components::render_upgrade_nudge(
314 array(
315 'plan' => self::$required_plan,
316 )
317 );
318 }
319
320 /**
321 * Get the HTML output to use as PayPal purchase box.
322 *
323 * @param string $dom_id ID of the DOM element with the purchase message.
324 * @param boolean $is_multiple Whether multiple items of the same product can be purchased.
325 *
326 * @return string
327 */
328 public function output_purchase_box( $dom_id, $is_multiple ) {
329 $items = '';
330 $css_prefix = self::$css_classname_prefix;
331
332 if ( $is_multiple ) {
333 $items = sprintf(
334 '
335 <div class="%1$s">
336 <input class="%2$s" type="number" value="1" min="1" id="%3$s" />
337 </div>
338 ',
339 esc_attr( "{$css_prefix}-items" ),
340 esc_attr( "{$css_prefix}-items-number" ),
341 esc_attr( "{$dom_id}_number" )
342 );
343 }
344
345 return sprintf(
346 '<div class="%1$s" id="%2$s"></div><div class="%3$s">%4$s<div class="%5$s" id="%6$s"></div></div>',
347 esc_attr( "{$css_prefix}-purchase-message" ),
348 esc_attr( "{$dom_id}-message-container" ),
349 esc_attr( "{$css_prefix}-purchase-box" ),
350 $items,
351 esc_attr( "{$css_prefix}-button" ),
352 esc_attr( "{$dom_id}_button" )
353 );
354 }
355
356 /**
357 * Get the HTML output to replace the `simple-payments` shortcode.
358 *
359 * @param array $data Product data.
360 * @return string
361 */
362 public function output_shortcode( $data ) {
363 $css_prefix = self::$css_classname_prefix;
364
365 $image = '';
366 if ( has_post_thumbnail( $data['id'] ) ) {
367 $image = sprintf(
368 '<div class="%1$s"><div class="%2$s">%3$s</div></div>',
369 esc_attr( "{$css_prefix}-product-image" ),
370 esc_attr( "{$css_prefix}-image" ),
371 get_the_post_thumbnail( $data['id'], 'full' )
372 );
373 }
374
375 return sprintf(
376 '
377 <div class="%1$s">
378 <div class="%2$s">
379 %3$s
380 <div class="%4$s">
381 <div class="%5$s"><p>%6$s</p></div>
382 <div class="%7$s"><p>%8$s</p></div>
383 <div class="%9$s"><p>%10$s</p></div>
384 %11$s
385 </div>
386 </div>
387 </div>
388 ',
389 esc_attr( "{$data['class']} {$css_prefix}-wrapper" ),
390 esc_attr( "{$css_prefix}-product" ),
391 $image,
392 esc_attr( "{$css_prefix}-details" ),
393 esc_attr( "{$css_prefix}-title" ),
394 esc_html( $data['title'] ),
395 esc_attr( "{$css_prefix}-description" ),
396 wp_kses( $data['description'], wp_kses_allowed_html( 'post' ) ),
397 esc_attr( "{$css_prefix}-price" ),
398 esc_html( $data['price'] ),
399 $this->output_purchase_box( $data['dom_id'], $data['multiple'] )
400 );
401 }
402
403 /**
404 * Format a price with currency
405 *
406 * Uses currency-aware formatting to output a formatted price with a simple fallback.
407 *
408 * Largely inspired by WordPress.com's Store_Price::display_currency
409 *
410 * @param string $price Price.
411 * @param string $currency Currency.
412 * @return string Formatted price.
413 */
414 private function format_price( $price, $currency ) {
415 require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-currencies.php';
416 return Jetpack_Currencies::format_price( $price, $currency );
417 }
418
419 /**
420 * Allows custom post types to be used by REST API.
421 *
422 * @param array $post_types - the allows post types.
423 * @see hook 'rest_api_allowed_post_types'
424 * @return array
425 */
426 public function allow_rest_api_types( $post_types ) {
427 $post_types[] = self::$post_type_order;
428 $post_types[] = self::$post_type_product;
429 return $post_types;
430 }
431
432 /**
433 * Merge $post_meta with additional meta information.
434 *
435 * @param array $post_meta - the post's meta information.
436 */
437 public function allow_sync_post_meta( $post_meta ) {
438 return array_merge(
439 $post_meta,
440 array(
441 'spay_paypal_id',
442 'spay_status',
443 'spay_product_id',
444 'spay_quantity',
445 'spay_price',
446 'spay_customer_email',
447 'spay_currency',
448 'spay_cta',
449 'spay_email',
450 'spay_multiple',
451 'spay_formatted_price',
452 )
453 );
454 }
455
456 /**
457 * Enable Simple payments custom meta values for access through the REST API.
458 * Field’s value will be exposed on a .meta key in the endpoint response,
459 * and WordPress will handle setting up the callbacks for reading and writing
460 * to that meta key.
461 *
462 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
463 */
464 public function register_meta_fields_in_rest_api() {
465 register_meta(
466 'post',
467 'spay_price',
468 array(
469 'description' => esc_html__( 'Simple payments; price.', 'jetpack' ),
470 'object_subtype' => self::$post_type_product,
471 'sanitize_callback' => array( $this, 'sanitize_price' ),
472 'show_in_rest' => true,
473 'single' => true,
474 'type' => 'number',
475 )
476 );
477
478 register_meta(
479 'post',
480 'spay_currency',
481 array(
482 'description' => esc_html__( 'Simple payments; currency code.', 'jetpack' ),
483 'object_subtype' => self::$post_type_product,
484 'sanitize_callback' => array( $this, 'sanitize_currency' ),
485 'show_in_rest' => true,
486 'single' => true,
487 'type' => 'string',
488 )
489 );
490
491 register_meta(
492 'post',
493 'spay_cta',
494 array(
495 'description' => esc_html__( 'Simple payments; text with "Buy" or other CTA', 'jetpack' ),
496 'object_subtype' => self::$post_type_product,
497 'sanitize_callback' => 'sanitize_text_field',
498 'show_in_rest' => true,
499 'single' => true,
500 'type' => 'string',
501 )
502 );
503
504 register_meta(
505 'post',
506 'spay_multiple',
507 array(
508 'description' => esc_html__( 'Simple payments; allow multiple items', 'jetpack' ),
509 'object_subtype' => self::$post_type_product,
510 'sanitize_callback' => 'rest_sanitize_boolean',
511 'show_in_rest' => true,
512 'single' => true,
513 'type' => 'boolean',
514 )
515 );
516
517 register_meta(
518 'post',
519 'spay_email',
520 array(
521 'description' => esc_html__( 'Simple payments button; paypal email.', 'jetpack' ),
522 'object_subtype' => self::$post_type_product,
523 'sanitize_callback' => 'sanitize_email',
524 'show_in_rest' => true,
525 'single' => true,
526 'type' => 'string',
527 )
528 );
529
530 register_meta(
531 'post',
532 'spay_status',
533 array(
534 'description' => esc_html__( 'Simple payments; status.', 'jetpack' ),
535 'object_subtype' => self::$post_type_product,
536 'sanitize_callback' => 'sanitize_text_field',
537 'show_in_rest' => true,
538 'single' => true,
539 'type' => 'string',
540 )
541 );
542 }
543
544 /**
545 * Sanitize three-character ISO-4217 Simple payments currency
546 *
547 * List has to be in sync with list at the block's client side and widget's backend side:
548 *
549 * @param array $currency - list of currencies.
550 * @link https://github.com/Automattic/jetpack/blob/31efa189ad223c0eb7ad085ac0650a23facf9ef5/extensions/blocks/simple-payments/constants.js#L9-L39
551 * @link https://github.com/Automattic/jetpack/blob/31efa189ad223c0eb7ad085ac0650a23facf9ef5/modules/widgets/simple-payments.php#L19-L44
552 *
553 * Currencies should be supported by PayPal:
554 * @link https://developer.paypal.com/docs/api/reference/currency-codes/
555 *
556 * Indian Rupee (INR) not supported because at the time of the creation of this file
557 * because it's limited to in-country PayPal India accounts only.
558 * Discussion: https://github.com/Automattic/wp-calypso/pull/28236
559 */
560 public static function sanitize_currency( $currency ) {
561 $valid_currencies = array(
562 'USD',
563 'EUR',
564 'AUD',
565 'BRL',
566 'CAD',
567 'CZK',
568 'DKK',
569 'HKD',
570 'HUF',
571 'ILS',
572 'JPY',
573 'MYR',
574 'MXN',
575 'TWD',
576 'NZD',
577 'NOK',
578 'PHP',
579 'PLN',
580 'GBP',
581 'RUB',
582 'SGD',
583 'SEK',
584 'CHF',
585 'THB',
586 );
587
588 return in_array( $currency, $valid_currencies, true ) ? $currency : false;
589 }
590
591 /**
592 * Sanitize price:
593 *
594 * Positive integers and floats
595 * Supports two decimal places.
596 * Maximum length: 10.
597 *
598 * See `price` from PayPal docs:
599 *
600 * @link https://developer.paypal.com/docs/api/orders/v1/#definition-item
601 *
602 * @param string $price - the price we want to sanitize.
603 * @return null|string
604 */
605 public static function sanitize_price( $price ) {
606 return preg_match( '/^[0-9]{0,10}(\.[0-9]{0,2})?$/', $price ) ? $price : false;
607 }
608
609 /**
610 * Sets up the custom post types for the module.
611 */
612 public function setup_cpts() {
613 /*
614 * ORDER data structure. holds:
615 * title = customer_name | 4xproduct_name
616 * excerpt = customer_name + customer contact info + customer notes from paypal form
617 * metadata:
618 * spay_paypal_id - paypal id of transaction
619 * spay_status
620 * spay_product_id - post_id of bought product
621 * spay_quantity - quantity of product
622 * spay_price - item price at the time of purchase
623 * spay_customer_email - customer email
624 * ... (WIP)
625 */
626 $order_capabilities = array(
627 'edit_post' => 'edit_posts',
628 'read_post' => 'read_private_posts',
629 'delete_post' => 'delete_posts',
630 'edit_posts' => 'edit_posts',
631 'edit_others_posts' => 'edit_others_posts',
632 'publish_posts' => 'publish_posts',
633 'read_private_posts' => 'read_private_posts',
634 );
635 $order_args = array(
636 'label' => esc_html_x( 'Order', 'noun: a quantity of goods or items purchased or sold', 'jetpack' ),
637 'description' => esc_html__( 'Simple Payments orders', 'jetpack' ),
638 'supports' => array( 'custom-fields', 'excerpt' ),
639 'hierarchical' => false,
640 'public' => false,
641 'show_ui' => false,
642 'show_in_menu' => false,
643 'show_in_admin_bar' => false,
644 'show_in_nav_menus' => false,
645 'can_export' => true,
646 'has_archive' => false,
647 'exclude_from_search' => true,
648 'publicly_queryable' => false,
649 'rewrite' => false,
650 'capabilities' => $order_capabilities,
651 'show_in_rest' => true,
652 );
653 register_post_type( self::$post_type_order, $order_args );
654
655 /*
656 * PRODUCT data structure. Holds:
657 * title - title
658 * content - description
659 * thumbnail - image
660 * metadata:
661 * spay_price - price
662 * spay_formatted_price
663 * spay_currency - currency code
664 * spay_cta - text with "Buy" or other CTA
665 * spay_email - paypal email
666 * spay_multiple - allow for multiple items
667 * spay_status - status. { enabled | disabled }
668 */
669 $product_capabilities = array(
670 'edit_post' => 'edit_posts',
671 'read_post' => 'read_private_posts',
672 'delete_post' => 'delete_posts',
673 'edit_posts' => 'publish_posts',
674 'edit_others_posts' => 'edit_others_posts',
675 'publish_posts' => 'publish_posts',
676 'read_private_posts' => 'read_private_posts',
677 );
678 $product_args = array(
679 'label' => esc_html__( 'Product', 'jetpack' ),
680 'description' => esc_html__( 'Simple Payments products', 'jetpack' ),
681 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'author' ),
682 'hierarchical' => false,
683 'public' => false,
684 'show_ui' => false,
685 'show_in_menu' => false,
686 'show_in_admin_bar' => false,
687 'show_in_nav_menus' => false,
688 'can_export' => true,
689 'has_archive' => false,
690 'exclude_from_search' => true,
691 'publicly_queryable' => false,
692 'rewrite' => false,
693 'capabilities' => $product_capabilities,
694 'show_in_rest' => true,
695 );
696 register_post_type( self::$post_type_product, $product_args );
697 }
698
699 /**
700 * Validate the block attributes
701 *
702 * @param array $attrs The block attributes, expected to contain:
703 * * email - an email address.
704 * * price - a float between 0.01 and 9999999999.99.
705 * * productId - the ID of the product being paid for.
706 *
707 * @return bool
708 */
709 public function is_valid( $attrs ) {
710 if ( ! $this->validate_paypal_email( $attrs ) ) {
711 return false;
712 }
713
714 if ( ! $this->validate_price( $attrs ) ) {
715 return false;
716 }
717
718 if ( ! $this->validate_product( $attrs ) ) {
719 return false;
720 }
721
722 return true;
723 }
724
725 /**
726 * Check that the email address to make a payment to is valid
727 *
728 * @param array $attrs Key-value array of attributes.
729 *
730 * @return boolean
731 */
732 private function validate_paypal_email( $attrs ) {
733 if ( empty( $attrs['email'] ) ) {
734 return false;
735 }
736 return (bool) filter_var( $attrs['email'], FILTER_VALIDATE_EMAIL );
737 }
738
739 /**
740 * Check that the price is valid
741 *
742 * @param array $attrs Key-value array of attributes.
743 *
744 * @return bool
745 */
746 private function validate_price( $attrs ) {
747 if ( empty( $attrs['price'] ) ) {
748 return false;
749 }
750 return (bool) self::sanitize_price( $attrs['price'] );
751 }
752
753 /**
754 * Check that the stored product is valid
755 *
756 * Valid means it has a title, and the currency is accepted.
757 *
758 * @param array $attrs Key-value array of attributes.
759 *
760 * @return bool
761 */
762 private function validate_product( $attrs ) {
763 if ( empty( $attrs['productId'] ) ) {
764 return false;
765 }
766 $product = $this->get_product( $attrs['productId'] );
767 if ( ! $product ) {
768 return false;
769 }
770 // This title is the one used by paypal, it's set from the title set in the block content, unless the block
771 // content title is blank.
772 if ( ! get_the_title( $product ) ) {
773 return false;
774 }
775
776 $currency = get_post_meta( $product->ID, 'spay_currency', true );
777 return (bool) self::sanitize_currency( $currency );
778 }
779
780 /**
781 * Format a price for display
782 *
783 * Largely taken from WordPress.com Store_Price class
784 *
785 * The currency array will have the shape:
786 * format => string sprintf format with placeholders `%1$s`: Symbol `%2$s`: Price.
787 * symbol => string Symbol string
788 * desc => string Text description of currency
789 * decimal => int Number of decimal places
790 *
791 * @param string $the_currency The desired currency, e.g. 'USD'.
792 * @return ?array Currency object or null if not found.
793 */
794 private static function get_currency( $the_currency ) {
795 require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-currencies.php';
796 $currencies = Jetpack_Currencies::CURRENCIES;
797
798 if ( isset( $currencies[ $the_currency ] ) ) {
799 return $currencies[ $the_currency ];
800 }
801 return null;
802 }
803 }
804 Jetpack_Simple_Payments::get_instance();
805