| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bricks element: Donation Form. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
* @since 1.2.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SureDonation\Inc\Page_Builders\Bricks\Elements; |
| 10 |
|
| 11 |
use SureDonation\Inc\Page_Builders\Bricks\Base_Element; |
| 12 |
use SureDonation\Inc\Page_Builders\Page_Builders; |
| 13 |
use SureDonation\Inc\Post_Types\Donation_Form as Donation_Form_Cpt; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Donation_Form class. |
| 21 |
* |
| 22 |
* @since 1.2.0 |
| 23 |
*/ |
| 24 |
class Donation_Form extends Base_Element { |
| 25 |
/** |
| 26 |
* Element name. |
| 27 |
* |
| 28 |
* @since 1.2.0 |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $name = 'suredonation-donation-form'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Element icon. |
| 35 |
* |
| 36 |
* @since 1.2.0 |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
public $icon = 'ti-write'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Element label shown in the builder panel. |
| 43 |
* |
| 44 |
* @since 1.2.0 |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
public function get_label() { |
| 48 |
return esc_html__( 'Donation Form', 'suredonation' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritDoc} |
| 53 |
*/ |
| 54 |
public function get_keywords() { |
| 55 |
return array_merge( parent::get_keywords(), [ 'form', 'donate' ] ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Load the donation form assets on the front end and in the builder canvas. |
| 60 |
* |
| 61 |
* @since 1.2.0 |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function enqueue_scripts() { |
| 65 |
wp_enqueue_style( 'suredonation-donation-form' ); |
| 66 |
wp_enqueue_script( 'suredonation-form-frontend' ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Register the element controls. |
| 71 |
* |
| 72 |
* Mirrors the Gutenberg donation-form block inspector, which exposes only a |
| 73 |
* campaign selector and a form selector (the form's own display options are |
| 74 |
* configured in the form editor, not on the embed). |
| 75 |
* |
| 76 |
* @since 1.2.0 |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function set_controls() { |
| 80 |
$this->add_campaign_control(); |
| 81 |
|
| 82 |
$options = Page_Builders::get_donation_form_options(); |
| 83 |
unset( $options[''] ); // Bricks selects use 'placeholder', not an empty option. |
| 84 |
|
| 85 |
$this->controls['formId'] = [ |
| 86 |
'tab' => 'content', |
| 87 |
'label' => esc_html__( 'Donation form', 'suredonation' ), |
| 88 |
'type' => 'select', |
| 89 |
'options' => $options, |
| 90 |
'searchable' => true, |
| 91 |
'clearable' => true, |
| 92 |
'placeholder' => esc_html__( 'Select a donation form', 'suredonation' ), |
| 93 |
]; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Render the donation form block. Unlike the campaign-context elements this |
| 98 |
* gates on a selected form (a campaign is optional — the block falls back |
| 99 |
* to the form's own campaign meta). |
| 100 |
* |
| 101 |
* @since 1.2.0 |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
public function render() { |
| 105 |
$settings = $this->settings; |
| 106 |
$form_id = $this->setting_int( $settings, 'formId' ); |
| 107 |
|
| 108 |
if ( ! $form_id ) { |
| 109 |
$this->render_element_placeholder( |
| 110 |
[ |
| 111 |
'icon-class' => $this->icon, |
| 112 |
'description' => esc_html__( 'Select a donation form to display.', 'suredonation' ), |
| 113 |
] |
| 114 |
); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
$form = get_post( $form_id ); |
| 119 |
if ( ! $form instanceof \WP_Post || 'publish' !== $form->post_status || Donation_Form_Cpt::POST_TYPE !== $form->post_type ) { |
| 120 |
$this->render_element_placeholder( |
| 121 |
[ |
| 122 |
'icon-class' => $this->icon, |
| 123 |
'description' => esc_html__( 'The selected donation form is unavailable.', 'suredonation' ), |
| 124 |
] |
| 125 |
); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
echo '<div ' . $this->render_attributes( '_root' ) . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Bricks root attributes are escaped by the builder. |
| 130 |
|
| 131 |
echo render_block( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Block render output is escaped in the block's render callback. |
| 132 |
[ |
| 133 |
'blockName' => $this->block_name(), |
| 134 |
'attrs' => $this->get_block_attrs( $settings ), |
| 135 |
'innerBlocks' => [], |
| 136 |
'innerHTML' => '', |
| 137 |
'innerContent' => [], |
| 138 |
] |
| 139 |
); |
| 140 |
|
| 141 |
echo '</div>'; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* {@inheritDoc} |
| 146 |
*/ |
| 147 |
protected function block_name() { |
| 148 |
return 'suredonation/donation-form'; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Map the element settings to the block attributes. |
| 153 |
* |
| 154 |
* @since 1.2.0 |
| 155 |
* @param array<string, mixed> $settings Element settings. |
| 156 |
* @return array<string, mixed> |
| 157 |
*/ |
| 158 |
protected function get_block_attrs( $settings ) { |
| 159 |
$attrs = [ |
| 160 |
'formId' => $this->setting_int( $settings, 'formId' ), |
| 161 |
]; |
| 162 |
|
| 163 |
$campaign_id = $this->setting_int( $settings, 'campaignId' ); |
| 164 |
if ( $campaign_id ) { |
| 165 |
$attrs['campaignId'] = $campaign_id; |
| 166 |
} |
| 167 |
|
| 168 |
return $attrs; |
| 169 |
} |
| 170 |
} |
| 171 |
|