events = $events; } /** * {@inheritdoc} */ public function register() { $this->events->add_callback( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); } /** * Enqueues block editor assets. * * @since 4.4.7.1 * * @return void */ public function enqueue_block_editor_assets() { $asset_file = SIMPLE_PAY_INC . 'core/assets/js/dist/simpay-block-button.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $script_data = require $asset_file; wp_enqueue_script( 'simpay-block-button', SIMPLE_PAY_INC_URL . 'core/assets/js/dist/simpay-block-button.js', $script_data['dependencies'], $script_data['version'] ); wp_localize_script( 'simpay-block-button', 'simpayBlockButton', array( 'paymentForms' => $this->get_payment_form_options(), ) ); } /** * Returns the available payment forms as options for the widget control. * * @since 4.4.7 * * @return array> */ private function get_payment_form_options() { /** @var array> $options */ static $options = array(); if ( empty( $options ) ) { $forms = get_posts( array( 'post_type' => 'simple-pay', 'posts_per_page' => -1, 'fields' => 'ids', ) ); foreach ( $forms as $form_id ) { if ( false === $this->is_form_type_valid( $form_id ) ) { continue; } array_push( $options, array( 'label' => get_the_title( $form_id ), 'value' => intval( $form_id ), ) ); }; } return $options; } /** * Determines if a payment form can be used with the widget. * * Only accepts Stripe Checkout with no custom fields, or Overlay. * * @since 4.4.7 * * @param int $form_id Payment form ID. * @return bool */ private function is_form_type_valid( $form_id ) { // Only accept Stripe Checkout or Overlay. $type = simpay_get_saved_meta( $form_id, '_form_display_type', 'stripe_checkout' ); switch ( $type ) { case 'embedded': return false; case 'overlay': return true; default: /** @var array>> $custom_fields */ $custom_fields = simpay_get_saved_meta( $form_id, '_custom_fields', array() ); $_custom_fields = array(); foreach ( $custom_fields as $type => $fields ) { /** @var array> $fields */ foreach ( $fields as $k => $field ) { /** @var array $field */ $field['type'] = $type; $_custom_fields[] = $field; } } return count( $_custom_fields ) <= 2; } } }