| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block: Button block extension |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.4.7 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core\Block; |
| 13 |
|
| 14 |
/** |
| 15 |
* ButtonBlock class. |
| 16 |
* |
| 17 |
* @since 4.4.7 |
| 18 |
*/ |
| 19 |
class ButtonBlock extends AbstractBlock { |
| 20 |
|
| 21 |
/** |
| 22 |
* Event manager. |
| 23 |
* |
| 24 |
* @since 4.4.7.1 |
| 25 |
* @var \SimplePay\Core\EventManagement\EventManager $events Event manager. |
| 26 |
*/ |
| 27 |
private $events; |
| 28 |
|
| 29 |
/** |
| 30 |
* ButtonBlock |
| 31 |
* |
| 32 |
* @since 4.4.7.1 |
| 33 |
* |
| 34 |
* @param \SimplePay\Core\EventManagement\EventManager $events Event manager. |
| 35 |
*/ |
| 36 |
public function __construct( $events ) { |
| 37 |
$this->events = $events; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* {@inheritdoc} |
| 42 |
*/ |
| 43 |
public function register() { |
| 44 |
$this->events->add_callback( |
| 45 |
'enqueue_block_editor_assets', |
| 46 |
array( $this, 'enqueue_block_editor_assets' ) |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Enqueues block editor assets. |
| 52 |
* |
| 53 |
* @since 4.4.7.1 |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function enqueue_block_editor_assets() { |
| 58 |
$asset_file = SIMPLE_PAY_INC . 'core/assets/js/dist/simpay-block-button.asset.php'; |
| 59 |
|
| 60 |
if ( ! file_exists( $asset_file ) ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$script_data = require $asset_file; |
| 65 |
|
| 66 |
wp_enqueue_script( |
| 67 |
'simpay-block-button', |
| 68 |
SIMPLE_PAY_INC_URL . 'core/assets/js/dist/simpay-block-button.js', |
| 69 |
$script_data['dependencies'], |
| 70 |
$script_data['version'] |
| 71 |
); |
| 72 |
|
| 73 |
wp_localize_script( |
| 74 |
'simpay-block-button', |
| 75 |
'simpayBlockButton', |
| 76 |
array( |
| 77 |
'paymentForms' => $this->get_payment_form_options(), |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns the available payment forms as options for the widget control. |
| 84 |
* |
| 85 |
* @since 4.4.7 |
| 86 |
* |
| 87 |
* @return array<array<string, int|string>> |
| 88 |
*/ |
| 89 |
private function get_payment_form_options() { |
| 90 |
/** @var array<array<string, int|string>> $options */ |
| 91 |
static $options = array(); |
| 92 |
|
| 93 |
if ( empty( $options ) ) { |
| 94 |
$forms = get_posts( |
| 95 |
array( |
| 96 |
'post_type' => 'simple-pay', |
| 97 |
'posts_per_page' => -1, |
| 98 |
'fields' => 'ids', |
| 99 |
) |
| 100 |
); |
| 101 |
|
| 102 |
foreach ( $forms as $form_id ) { |
| 103 |
if ( false === $this->is_form_type_valid( $form_id ) ) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
array_push( |
| 108 |
$options, |
| 109 |
array( |
| 110 |
'label' => get_the_title( $form_id ), |
| 111 |
'value' => intval( $form_id ), |
| 112 |
) |
| 113 |
); |
| 114 |
}; |
| 115 |
} |
| 116 |
|
| 117 |
return $options; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Determines if a payment form can be used with the widget. |
| 122 |
* |
| 123 |
* Only accepts Stripe Checkout with no custom fields, or Overlay. |
| 124 |
* |
| 125 |
* @since 4.4.7 |
| 126 |
* |
| 127 |
* @param int $form_id Payment form ID. |
| 128 |
* @return bool |
| 129 |
*/ |
| 130 |
private function is_form_type_valid( $form_id ) { |
| 131 |
// Only accept Stripe Checkout or Overlay. |
| 132 |
$type = simpay_get_saved_meta( |
| 133 |
$form_id, |
| 134 |
'_form_display_type', |
| 135 |
'stripe_checkout' |
| 136 |
); |
| 137 |
|
| 138 |
switch ( $type ) { |
| 139 |
case 'embedded': |
| 140 |
return false; |
| 141 |
case 'overlay': |
| 142 |
return true; |
| 143 |
default: |
| 144 |
/** @var array<string, array<string, array<string>>> $custom_fields */ |
| 145 |
$custom_fields = simpay_get_saved_meta( |
| 146 |
$form_id, |
| 147 |
'_custom_fields', |
| 148 |
array() |
| 149 |
); |
| 150 |
|
| 151 |
$_custom_fields = array(); |
| 152 |
|
| 153 |
foreach ( $custom_fields as $type => $fields ) { |
| 154 |
/** @var array<string, array<string>> $fields */ |
| 155 |
foreach ( $fields as $k => $field ) { |
| 156 |
/** @var array<string> $field */ |
| 157 |
$field['type'] = $type; |
| 158 |
$_custom_fields[] = $field; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return count( $_custom_fields ) <= 2; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|