PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3-a.3 16.3-a.1 16.2 16.2-beta 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 All 504 releases
← All changes | modules/simple-payments/simple-payments.php +29 -558 12.2.3 → 16.3-a.1 View file →
@@ -6,8 +6,14 @@
6 6 *
7 7 * @package automattic/jetpack
8 8 */
9 9
10 +use Automattic\Jetpack\Paypal_Payments\Simple_Payments as PayPal_Simple_Payments;
11 +
12 +if ( ! defined( 'ABSPATH' ) ) {
13 + exit( 0 );
14 +}
15 +
10 16 /**
11 17 * Jetpack_Simple_Payments
12 18 */
13 19 class Jetpack_Simple_Payments {
@@ -63,13 +69,10 @@
63 69 /**
64 70 * Original singleton.
65 71 *
66 72 * @todo Remove this when nothing calles getInstance anymore.
67 - *
68 - * @deprecated 10.8
69 73 */
70 74 public static function getInstance() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
71 - _deprecated_function( __METHOD__, 'Jetpack 10.7.0', 'Jetpack_Simple_Payments::get_instance' );
72 75 return self::get_instance();
73 76 }
74 77
75 78 /**
@@ -77,9 +80,9 @@
77 80 */
78 81 public static function get_instance() {
79 82 if ( ! self::$instance ) {
80 83 self::$instance = new self();
81 - self::$instance->register_init_hooks();
84 + self::$instance->init_hook_action();
82 85 self::$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'value_bundle' : 'jetpack_premium';
83 86 }
84 87 return self::$instance;
85 88 }
@@ -84,67 +87,12 @@
84 87 return self::$instance;
85 88 }
86 89
87 90 /**
88 - * Register scripts and styles.
89 - */
90 - private function register_scripts_and_styles() {
91 - /**
92 - * Paypal heavily discourages putting that script in your own server:
93 - *
94 - * @see https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/
95 - */
96 - 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
97 - 'paypal-checkout-js',
98 - 'https://www.paypalobjects.com/api/checkout.js',
99 - array(),
100 - null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
101 - true
102 - );
103 - wp_register_script(
104 - 'jetpack-paypal-express-checkout',
105 - plugins_url( '/paypal-express-checkout.js', __FILE__ ),
106 - array( 'jquery', 'paypal-checkout-js' ),
107 - JETPACK__VERSION,
108 - false
109 - );
110 - wp_register_style(
111 - 'jetpack-simple-payments',
112 - plugins_url( '/simple-payments.css', __FILE__ ),
113 - array( 'dashicons' ),
114 - JETPACK__VERSION,
115 - false
116 - );
117 - }
118 -
119 - /**
120 - * Register init hooks.
121 - */
122 - private function register_init_hooks() {
123 - add_action( 'init', array( $this, 'init_hook_action' ) );
124 - add_action( 'rest_api_init', array( $this, 'register_meta_fields_in_rest_api' ) );
125 - }
126 -
127 - /**
128 - * Register the shortcode.
129 - */
130 - private function register_shortcode() {
131 - add_shortcode( self::$shortcode, array( $this, 'parse_shortcode' ) );
132 - }
133 -
134 - /**
135 91 * Actions that are run on init.
136 92 */
137 93 public function init_hook_action() {
138 - add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_rest_api_types' ) );
139 - add_filter( 'jetpack_sync_post_meta_whitelist', array( $this, 'allow_sync_post_meta' ) );
140 - if ( ! is_admin() ) {
141 - $this->register_scripts_and_styles();
142 - }
143 - $this->register_shortcode();
144 - $this->setup_cpts();
145 -
146 - add_filter( 'the_content', array( $this, 'remove_auto_paragraph_from_product_description' ), 0 );
94 + return PayPal_Simple_Payments::get_instance()->init_hook_action();
147 95 }
148 96
149 97 /**
150 98 * Enqueue the static assets needed in the frontend.
@@ -149,15 +97,9 @@
149 97 /**
150 98 * Enqueue the static assets needed in the frontend.
151 99 */
152 100 public function enqueue_frontend_assets() {
153 - if ( ! wp_style_is( 'jetpack-simple-payments', 'enqueued' ) ) {
154 - wp_enqueue_style( 'jetpack-simple-payments' );
155 - }
156 -
157 - if ( ! wp_script_is( 'jetpack-paypal-express-checkout', 'enqueued' ) ) {
158 - wp_enqueue_script( 'jetpack-paypal-express-checkout' );
159 - }
101 + return PayPal_Simple_Payments::get_instance()->enqueue_frontend_assets();
160 102 }
161 103
162 104 /**
163 105 * Add an inline script for setting up the PayPal checkout button.
@@ -166,18 +108,9 @@
166 108 * @param int $dom_id ID of the DOM element with the purchase message.
167 109 * @param boolean $is_multiple Whether multiple items of the same product can be purchased.
168 110 */
169 111 public function setup_paypal_checkout_button( $id, $dom_id, $is_multiple ) {
170 - wp_add_inline_script(
171 - 'jetpack-paypal-express-checkout',
172 - sprintf(
173 - "try{PaypalExpressCheckout.renderButton( '%d', '%d', '%s', '%d' );}catch(e){}",
174 - esc_js( $this->get_blog_id() ),
175 - esc_js( $id ),
176 - esc_js( $dom_id ),
177 - esc_js( $is_multiple )
178 - )
179 - );
112 + return PayPal_Simple_Payments::get_instance()->setup_paypal_checkout_button( $id, $dom_id, $is_multiple );
180 113 }
181 114
182 115 /**
183 116 * Remove auto paragraph from product description.
@@ -184,22 +117,14 @@
184 117 *
185 118 * @param string $content - the content of the post.
186 119 */
187 120 public function remove_auto_paragraph_from_product_description( $content ) {
188 - if ( get_post_type() === self::$post_type_product ) {
189 - remove_filter( 'the_content', 'wpautop' );
190 - }
191 -
192 - return $content;
121 + return PayPal_Simple_Payments::get_instance()->remove_auto_paragraph_from_product_description( $content );
193 122 }
194 123
195 124 /** Return the blog ID */
196 125 public function get_blog_id() {
197 - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
198 - return get_current_blog_id();
199 - }
200 -
201 - return Jetpack_Options::get_option( 'id' );
126 + return PayPal_Simple_Payments::get_instance()->get_blog_id();
202 127 }
203 128
204 129 /**
205 130 * Used to check whether Simple Payments are enabled for given site.
@@ -206,48 +131,12 @@
206 131 *
207 132 * @return bool True if Simple Payments are enabled, false otherwise.
208 133 */
209 134 public function is_enabled_jetpack_simple_payments() {
210 - /**
211 - * Can be used by plugin authors to disable the conflicting output of Simple Payments.
212 - *
213 - * @since 6.3.0
214 - *
215 - * @param bool True if Simple Payments should be disabled, false otherwise.
216 - */
217 - if ( apply_filters( 'jetpack_disable_simple_payments', false ) ) {
218 - return false;
219 - }
220 -
221 - return ( ( defined( 'IS_WPCOM' ) && IS_WPCOM )
222 - || Jetpack::is_connection_ready() )
223 - &&
224 - Jetpack_Plan::supports( 'simple-payments' );
135 + return PayPal_Simple_Payments::is_enabled_jetpack_simple_payments();
225 136 }
226 137
227 138 /**
228 - * Get a WP_Post representation of a product
229 - *
230 - * @param int $id The ID of the product.
231 - *
232 - * @return array|false|WP_Post
233 - */
234 - private function get_product( $id ) {
235 - if ( ! $id ) {
236 - return false;
237 - }
238 -
239 - $product = get_post( $id );
240 - if ( ! $product || is_wp_error( $product ) ) {
241 - return false;
242 - }
243 - if ( $product->post_type !== self::$post_type_product || 'publish' !== $product->post_status ) {
244 - return false;
245 - }
246 - return $product;
247 - }
248 -
249 - /**
250 139 * Creates the content from a shortcode
251 140 *
252 141 * @param array $attrs Shortcode attributes.
253 142 * @param mixed $content unused.
@@ -253,49 +142,10 @@
253 142 * @param mixed $content unused.
254 143 *
255 144 * @return string|void
256 145 */
257 - public function parse_shortcode( $attrs, $content = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
258 - if ( empty( $attrs['id'] ) ) {
259 - return;
260 - }
261 - $product = $this->get_product( $attrs['id'] );
262 - if ( ! $product ) {
263 - return;
264 - }
265 -
266 - // We allow for overriding the presentation labels.
267 - $data = shortcode_atts(
268 - array(
269 - 'blog_id' => $this->get_blog_id(),
270 - 'dom_id' => uniqid( self::$css_classname_prefix . '-' . $product->ID . '_', true ),
271 - 'class' => self::$css_classname_prefix . '-' . $product->ID,
272 - 'title' => get_the_title( $product ),
273 - 'description' => $product->post_content,
274 - 'cta' => get_post_meta( $product->ID, 'spay_cta', true ),
275 - 'multiple' => get_post_meta( $product->ID, 'spay_multiple', true ) || '0',
276 - ),
277 - $attrs
278 - );
279 -
280 - $data['price'] = $this->format_price(
281 - get_post_meta( $product->ID, 'spay_price', true ),
282 - get_post_meta( $product->ID, 'spay_currency', true )
283 - );
284 -
285 - $data['id'] = $attrs['id'];
286 -
287 - if ( ! $this->is_enabled_jetpack_simple_payments() ) {
288 - if ( jetpack_is_frontend() ) {
289 - return $this->output_admin_warning( $data );
290 - }
291 - return;
292 - }
293 -
294 - $this->enqueue_frontend_assets();
295 - $this->setup_paypal_checkout_button( $attrs['id'], $data['dom_id'], $data['multiple'] );
296 -
297 - return $this->output_shortcode( $data );
146 + public function parse_shortcode( $attrs, $content = false ) {
147 + return PayPal_Simple_Payments::get_instance()->parse_shortcode( $attrs, $content );
298 148 }
299 149
300 150 /**
301 151 * Output an admin warning if user can't use Pay with PayPal.
@@ -301,19 +151,10 @@
301 151 * Output an admin warning if user can't use Pay with PayPal.
302 152 *
303 153 * @param array $data unused.
304 154 */
305 - public function output_admin_warning( $data ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
306 - if ( ! current_user_can( 'manage_options' ) ) {
307 - return;
308 - }
309 -
310 - require_once JETPACK__PLUGIN_DIR . '_inc/lib/components.php';
311 - return Jetpack_Components::render_upgrade_nudge(
312 - array(
313 - 'plan' => self::$required_plan,
314 - )
315 - );
155 + public function output_admin_warning( $data ) {
156 + return PayPal_Simple_Payments::get_instance()->output_admin_warning( $data );
316 157 }
317 158
318 159 /**
319 160 * Get the HTML output to use as PayPal purchase box.
@@ -323,33 +164,9 @@
323 164 *
324 165 * @return string
325 166 */
326 167 public function output_purchase_box( $dom_id, $is_multiple ) {
327 - $items = '';
328 - $css_prefix = self::$css_classname_prefix;
329 -
330 - if ( $is_multiple ) {
331 - $items = sprintf(
332 - '
333 - <div class="%1$s">
334 - <input class="%2$s" type="number" value="1" min="1" id="%3$s" />
335 - </div>
336 - ',
337 - esc_attr( "{$css_prefix}-items" ),
338 - esc_attr( "{$css_prefix}-items-number" ),
339 - esc_attr( "{$dom_id}_number" )
340 - );
341 - }
342 -
343 - return sprintf(
344 - '<div class="%1$s" id="%2$s"></div><div class="%3$s">%4$s<div class="%5$s" id="%6$s"></div></div>',
345 - esc_attr( "{$css_prefix}-purchase-message" ),
346 - esc_attr( "{$dom_id}-message-container" ),
347 - esc_attr( "{$css_prefix}-purchase-box" ),
348 - $items,
349 - esc_attr( "{$css_prefix}-button" ),
350 - esc_attr( "{$dom_id}_button" )
351 - );
168 + return PayPal_Simple_Payments::get_instance()->output_purchase_box( $dom_id, $is_multiple );
352 169 }
353 170
354 171 /**
355 172 * Get the HTML output to replace the `simple-payments` shortcode.
@@ -357,65 +174,12 @@
357 174 * @param array $data Product data.
358 175 * @return string
359 176 */
360 177 public function output_shortcode( $data ) {
361 - $css_prefix = self::$css_classname_prefix;
362 -
363 - $image = '';
364 - if ( has_post_thumbnail( $data['id'] ) ) {
365 - $image = sprintf(
366 - '<div class="%1$s"><div class="%2$s">%3$s</div></div>',
367 - esc_attr( "{$css_prefix}-product-image" ),
368 - esc_attr( "{$css_prefix}-image" ),
369 - get_the_post_thumbnail( $data['id'], 'full' )
370 - );
371 - }
372 -
373 - return sprintf(
374 - '
375 -<div class="%1$s">
376 - <div class="%2$s">
377 - %3$s
378 - <div class="%4$s">
379 - <div class="%5$s"><p>%6$s</p></div>
380 - <div class="%7$s"><p>%8$s</p></div>
381 - <div class="%9$s"><p>%10$s</p></div>
382 - %11$s
383 - </div>
384 - </div>
385 -</div>
386 -',
387 - esc_attr( "{$data['class']} {$css_prefix}-wrapper" ),
388 - esc_attr( "{$css_prefix}-product" ),
389 - $image,
390 - esc_attr( "{$css_prefix}-details" ),
391 - esc_attr( "{$css_prefix}-title" ),
392 - esc_html( $data['title'] ),
393 - esc_attr( "{$css_prefix}-description" ),
394 - wp_kses( $data['description'], wp_kses_allowed_html( 'post' ) ),
395 - esc_attr( "{$css_prefix}-price" ),
396 - esc_html( $data['price'] ),
397 - $this->output_purchase_box( $data['dom_id'], $data['multiple'] )
398 - );
178 + return PayPal_Simple_Payments::get_instance()->output_shortcode( $data );
399 179 }
400 180
401 181 /**
402 - * Format a price with currency
403 - *
404 - * Uses currency-aware formatting to output a formatted price with a simple fallback.
405 - *
406 - * Largely inspired by WordPress.com's Store_Price::display_currency
407 - *
408 - * @param string $price Price.
409 - * @param string $currency Currency.
410 - * @return string Formatted price.
411 - */
412 - private function format_price( $price, $currency ) {
413 - require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-currencies.php';
414 - return Jetpack_Currencies::format_price( $price, $currency );
415 - }
416 -
417 - /**
418 182 * Allows custom post types to be used by REST API.
419 183 *
420 184 * @param array $post_types - the allows post types.
421 185 * @see hook 'rest_api_allowed_post_types'
@@ -421,11 +185,9 @@
421 185 * @see hook 'rest_api_allowed_post_types'
422 186 * @return array
423 187 */
424 188 public function allow_rest_api_types( $post_types ) {
425 - $post_types[] = self::$post_type_order;
426 - $post_types[] = self::$post_type_product;
427 - return $post_types;
189 + return PayPal_Simple_Payments::get_instance()->allow_rest_api_types( $post_types );
428 190 }
429 191
430 192 /**
431 193 * Merge $post_meta with additional meta information.
@@ -432,29 +194,14 @@
432 194 *
433 195 * @param array $post_meta - the post's meta information.
434 196 */
435 197 public function allow_sync_post_meta( $post_meta ) {
436 - return array_merge(
437 - $post_meta,
438 - array(
439 - 'spay_paypal_id',
440 - 'spay_status',
441 - 'spay_product_id',
442 - 'spay_quantity',
443 - 'spay_price',
444 - 'spay_customer_email',
445 - 'spay_currency',
446 - 'spay_cta',
447 - 'spay_email',
448 - 'spay_multiple',
449 - 'spay_formatted_price',
450 - )
451 - );
198 + return PayPal_Simple_Payments::get_instance()->allow_sync_post_meta( $post_meta );
452 199 }
453 200
454 201 /**
455 202 * Enable Simple payments custom meta values for access through the REST API.
456 - * Field’s value will be exposed on a .meta key in the endpoint response,
203 + * Field's value will be exposed on a .meta key in the endpoint response,
457 204 * and WordPress will handle setting up the callbacks for reading and writing
458 205 * to that meta key.
459 206 *
460 207 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
@@ -459,85 +206,9 @@
459 206 *
460 207 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
461 208 */
462 209 public function register_meta_fields_in_rest_api() {
463 - register_meta(
464 - 'post',
465 - 'spay_price',
466 - array(
467 - 'description' => esc_html__( 'Simple payments; price.', 'jetpack' ),
468 - 'object_subtype' => self::$post_type_product,
469 - 'sanitize_callback' => array( $this, 'sanitize_price' ),
470 - 'show_in_rest' => true,
471 - 'single' => true,
472 - 'type' => 'number',
473 - )
474 - );
475 -
476 - register_meta(
477 - 'post',
478 - 'spay_currency',
479 - array(
480 - 'description' => esc_html__( 'Simple payments; currency code.', 'jetpack' ),
481 - 'object_subtype' => self::$post_type_product,
482 - 'sanitize_callback' => array( $this, 'sanitize_currency' ),
483 - 'show_in_rest' => true,
484 - 'single' => true,
485 - 'type' => 'string',
486 - )
487 - );
488 -
489 - register_meta(
490 - 'post',
491 - 'spay_cta',
492 - array(
493 - 'description' => esc_html__( 'Simple payments; text with "Buy" or other CTA', 'jetpack' ),
494 - 'object_subtype' => self::$post_type_product,
495 - 'sanitize_callback' => 'sanitize_text_field',
496 - 'show_in_rest' => true,
497 - 'single' => true,
498 - 'type' => 'string',
499 - )
500 - );
501 -
502 - register_meta(
503 - 'post',
504 - 'spay_multiple',
505 - array(
506 - 'description' => esc_html__( 'Simple payments; allow multiple items', 'jetpack' ),
507 - 'object_subtype' => self::$post_type_product,
508 - 'sanitize_callback' => 'rest_sanitize_boolean',
509 - 'show_in_rest' => true,
510 - 'single' => true,
511 - 'type' => 'boolean',
512 - )
513 - );
514 -
515 - register_meta(
516 - 'post',
517 - 'spay_email',
518 - array(
519 - 'description' => esc_html__( 'Simple payments button; paypal email.', 'jetpack' ),
520 - 'object_subtype' => self::$post_type_product,
521 - 'sanitize_callback' => 'sanitize_email',
522 - 'show_in_rest' => true,
523 - 'single' => true,
524 - 'type' => 'string',
525 - )
526 - );
527 -
528 - register_meta(
529 - 'post',
530 - 'spay_status',
531 - array(
532 - 'description' => esc_html__( 'Simple payments; status.', 'jetpack' ),
533 - 'object_subtype' => self::$post_type_product,
534 - 'sanitize_callback' => 'sanitize_text_field',
535 - 'show_in_rest' => true,
536 - 'single' => true,
537 - 'type' => 'string',
538 - )
539 - );
210 + return PayPal_Simple_Payments::get_instance()->register_meta_fields_in_rest_api();
540 211 }
541 212
542 213 /**
543 214 * Sanitize three-character ISO-4217 Simple payments currency
@@ -555,36 +226,9 @@
555 226 * because it's limited to in-country PayPal India accounts only.
556 227 * Discussion: https://github.com/Automattic/wp-calypso/pull/28236
557 228 */
558 229 public static function sanitize_currency( $currency ) {
559 - $valid_currencies = array(
560 - 'USD',
561 - 'EUR',
562 - 'AUD',
563 - 'BRL',
564 - 'CAD',
565 - 'CZK',
566 - 'DKK',
567 - 'HKD',
568 - 'HUF',
569 - 'ILS',
570 - 'JPY',
571 - 'MYR',
572 - 'MXN',
573 - 'TWD',
574 - 'NZD',
575 - 'NOK',
576 - 'PHP',
577 - 'PLN',
578 - 'GBP',
579 - 'RUB',
580 - 'SGD',
581 - 'SEK',
582 - 'CHF',
583 - 'THB',
584 - );
585 -
586 - return in_array( $currency, $valid_currencies, true ) ? $currency : false;
230 + return PayPal_Simple_Payments::sanitize_currency( $currency );
587 231 }
588 232
589 233 /**
590 234 * Sanitize price:
@@ -600,9 +244,9 @@
600 244 * @param string $price - the price we want to sanitize.
601 245 * @return null|string
602 246 */
603 247 public static function sanitize_price( $price ) {
604 - return preg_match( '/^[0-9]{0,10}(\.[0-9]{0,2})?$/', $price ) ? $price : false;
248 + return PayPal_Simple_Payments::sanitize_price( $price );
605 249 }
606 250
607 251 /**
608 252 * Sets up the custom post types for the module.
@@ -607,92 +251,9 @@
607 251 /**
608 252 * Sets up the custom post types for the module.
609 253 */
610 254 public function setup_cpts() {
611 - /*
612 - * ORDER data structure. holds:
613 - * title = customer_name | 4xproduct_name
614 - * excerpt = customer_name + customer contact info + customer notes from paypal form
615 - * metadata:
616 - * spay_paypal_id - paypal id of transaction
617 - * spay_status
618 - * spay_product_id - post_id of bought product
619 - * spay_quantity - quantity of product
620 - * spay_price - item price at the time of purchase
621 - * spay_customer_email - customer email
622 - * ... (WIP)
623 - */
624 - $order_capabilities = array(
625 - 'edit_post' => 'edit_posts',
626 - 'read_post' => 'read_private_posts',
627 - 'delete_post' => 'delete_posts',
628 - 'edit_posts' => 'edit_posts',
629 - 'edit_others_posts' => 'edit_others_posts',
630 - 'publish_posts' => 'publish_posts',
631 - 'read_private_posts' => 'read_private_posts',
632 - );
633 - $order_args = array(
634 - 'label' => esc_html_x( 'Order', 'noun: a quantity of goods or items purchased or sold', 'jetpack' ),
635 - 'description' => esc_html__( 'Simple Payments orders', 'jetpack' ),
636 - 'supports' => array( 'custom-fields', 'excerpt' ),
637 - 'hierarchical' => false,
638 - 'public' => false,
639 - 'show_ui' => false,
640 - 'show_in_menu' => false,
641 - 'show_in_admin_bar' => false,
642 - 'show_in_nav_menus' => false,
643 - 'can_export' => true,
644 - 'has_archive' => false,
645 - 'exclude_from_search' => true,
646 - 'publicly_queryable' => false,
647 - 'rewrite' => false,
648 - 'capabilities' => $order_capabilities,
649 - 'show_in_rest' => true,
650 - );
651 - register_post_type( self::$post_type_order, $order_args );
652 -
653 - /*
654 - * PRODUCT data structure. Holds:
655 - * title - title
656 - * content - description
657 - * thumbnail - image
658 - * metadata:
659 - * spay_price - price
660 - * spay_formatted_price
661 - * spay_currency - currency code
662 - * spay_cta - text with "Buy" or other CTA
663 - * spay_email - paypal email
664 - * spay_multiple - allow for multiple items
665 - * spay_status - status. { enabled | disabled }
666 - */
667 - $product_capabilities = array(
668 - 'edit_post' => 'edit_posts',
669 - 'read_post' => 'read_private_posts',
670 - 'delete_post' => 'delete_posts',
671 - 'edit_posts' => 'publish_posts',
672 - 'edit_others_posts' => 'edit_others_posts',
673 - 'publish_posts' => 'publish_posts',
674 - 'read_private_posts' => 'read_private_posts',
675 - );
676 - $product_args = array(
677 - 'label' => esc_html__( 'Product', 'jetpack' ),
678 - 'description' => esc_html__( 'Simple Payments products', 'jetpack' ),
679 - 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'author' ),
680 - 'hierarchical' => false,
681 - 'public' => false,
682 - 'show_ui' => false,
683 - 'show_in_menu' => false,
684 - 'show_in_admin_bar' => false,
685 - 'show_in_nav_menus' => false,
686 - 'can_export' => true,
687 - 'has_archive' => false,
688 - 'exclude_from_search' => true,
689 - 'publicly_queryable' => false,
690 - 'rewrite' => false,
691 - 'capabilities' => $product_capabilities,
692 - 'show_in_rest' => true,
693 - );
694 - register_post_type( self::$post_type_product, $product_args );
255 + return PayPal_Simple_Payments::get_instance()->setup_cpts();
695 256 }
696 257
697 258 /**
698 259 * Validate the block attributes
@@ -704,99 +265,9 @@
704 265 *
705 266 * @return bool
706 267 */
707 268 public function is_valid( $attrs ) {
708 - if ( ! $this->validate_paypal_email( $attrs ) ) {
709 - return false;
710 - }
711 -
712 - if ( ! $this->validate_price( $attrs ) ) {
713 - return false;
714 - }
715 -
716 - if ( ! $this->validate_product( $attrs ) ) {
717 - return false;
718 - }
719 -
720 - return true;
269 + return PayPal_Simple_Payments::get_instance()->is_valid( $attrs );
721 270 }
271 +}
722 272
723 - /**
724 - * Check that the email address to make a payment to is valid
725 - *
726 - * @param array $attrs Key-value array of attributes.
727 - *
728 - * @return boolean
729 - */
730 - private function validate_paypal_email( $attrs ) {
731 - if ( empty( $attrs['email'] ) ) {
732 - return false;
733 - }
734 - return (bool) filter_var( $attrs['email'], FILTER_VALIDATE_EMAIL );
735 - }
736 -
737 - /**
738 - * Check that the price is valid
739 - *
740 - * @param array $attrs Key-value array of attributes.
741 - *
742 - * @return bool
743 - */
744 - private function validate_price( $attrs ) {
745 - if ( empty( $attrs['price'] ) ) {
746 - return false;
747 - }
748 - return (bool) self::sanitize_price( $attrs['price'] );
749 - }
750 -
751 - /**
752 - * Check that the stored product is valid
753 - *
754 - * Valid means it has a title, and the currency is accepted.
755 - *
756 - * @param array $attrs Key-value array of attributes.
757 - *
758 - * @return bool
759 - */
760 - private function validate_product( $attrs ) {
761 - if ( empty( $attrs['productId'] ) ) {
762 - return false;
763 - }
764 - $product = $this->get_product( $attrs['productId'] );
765 - if ( ! $product ) {
766 - return false;
767 - }
768 - // This title is the one used by paypal, it's set from the title set in the block content, unless the block
769 - // content title is blank.
770 - if ( ! get_the_title( $product ) ) {
771 - return false;
772 - }
773 -
774 - $currency = get_post_meta( $product->ID, 'spay_currency', true );
775 - return (bool) self::sanitize_currency( $currency );
776 - }
777 -
778 - /**
779 - * Format a price for display
780 - *
781 - * Largely taken from WordPress.com Store_Price class
782 - *
783 - * The currency array will have the shape:
784 - * format => string sprintf format with placeholders `%1$s`: Symbol `%2$s`: Price.
785 - * symbol => string Symbol string
786 - * desc => string Text description of currency
787 - * decimal => int Number of decimal places
788 - *
789 - * @param string $the_currency The desired currency, e.g. 'USD'.
790 - * @return ?array Currency object or null if not found.
791 - */
792 - private static function get_currency( $the_currency ) {
793 - require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-currencies.php';
794 - $currencies = Jetpack_Currencies::CURRENCIES;
795 -
796 - if ( isset( $currencies[ $the_currency ] ) ) {
797 - return $currencies[ $the_currency ];
798 - }
799 - return null;
800 - }
801 -}
802 -Jetpack_Simple_Payments::get_instance();
273 +PayPal_Simple_Payments::get_instance();