Geen omschrijving

simple-payments.php 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Pay with PayPal block (aka Simple Payments).
  4. *
  5. * @since 9.0.0
  6. *
  7. * @package automattic/jetpack
  8. */
  9. namespace Automattic\Jetpack\Extensions\SimplePayments;
  10. use Automattic\Jetpack\Blocks;
  11. use Jetpack_Simple_Payments;
  12. const FEATURE_NAME = 'simple-payments';
  13. const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
  14. /**
  15. * Registers the block for use in Gutenberg
  16. * This is done via an action so that we can disable
  17. * registration if we need to.
  18. */
  19. function register_block() {
  20. Blocks::jetpack_register_block(
  21. BLOCK_NAME,
  22. array(
  23. 'render_callback' => __NAMESPACE__ . '\render_block',
  24. 'plan_check' => true,
  25. )
  26. );
  27. }
  28. add_action( 'init', __NAMESPACE__ . '\register_block' );
  29. /**
  30. * Pay with PayPal block dynamic rendering.
  31. *
  32. * @param array $attr Array containing the block attributes.
  33. * @param string $content String containing the block content.
  34. *
  35. * @return string
  36. */
  37. function render_block( $attr, $content ) {
  38. // Do nothing if block content is a `simple-payment` shortcode.
  39. if ( preg_match( '/\[simple-payment(.*)]/', $content ) ) {
  40. return $content;
  41. }
  42. // Keep content as-is if rendered in other contexts than frontend (i.e. feed, emails, API, etc.).
  43. if ( ! jetpack_is_frontend() ) {
  44. return $content;
  45. }
  46. $simple_payments = Jetpack_Simple_Payments::getInstance();
  47. $simple_payments->enqueue_frontend_assets();
  48. // For AMP requests, make sure the purchase link redirects to the non-AMP post URL.
  49. if ( Blocks::is_amp_request() ) {
  50. $content = preg_replace(
  51. '#(<a class="jetpack-simple-payments-purchase".*)rel="(.*)"(.*>.*</a>)#i',
  52. '$1rel="$2 noamphtml"$3',
  53. $content
  54. );
  55. return $content;
  56. }
  57. // Augment block UI with a PayPal button if rendered on the frontend.
  58. $product_id = $attr['productId'];
  59. $dom_id = wp_unique_id( "jetpack-simple-payments-{$product_id}_" );
  60. $is_multiple = get_post_meta( $product_id, 'spay_multiple', true ) || '0';
  61. $simple_payments->setup_paypal_checkout_button( $product_id, $dom_id, $is_multiple );
  62. $purchase_box = $simple_payments->output_purchase_box( $dom_id, $is_multiple );
  63. $content = preg_replace( '#<a class="jetpack-simple-payments-purchase(.*)</a>#i', $purchase_box, $content );
  64. return $content;
  65. }
  66. /**
  67. * Determine if AMP should be disabled on posts having "Pay with PayPal" blocks.
  68. *
  69. * @param bool $skip Skipped.
  70. * @param int $post_id Post ID.
  71. * @param WP_Post $post Post.
  72. *
  73. * @return bool Whether to skip the post from AMP.
  74. */
  75. function amp_skip_post( $skip, $post_id, $post ) {
  76. // When AMP is on standard mode, there are no non-AMP posts to link to where the purchase can be completed, so let's
  77. // prevent the post from being available in AMP.
  78. if ( function_exists( 'amp_is_canonical' ) && \amp_is_canonical() && has_block( BLOCK_NAME, $post->post_content ) ) {
  79. return true;
  80. }
  81. return $skip;
  82. }
  83. add_filter( 'amp_skip_post', __NAMESPACE__ . '\amp_skip_post', 10, 3 );