| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Charitable Donation Form Widget |
| 5 |
* |
| 6 |
* @package Elementor_Example_Plugin |
| 7 |
*/ |
| 8 |
|
| 9 |
if (! defined('ABSPATH')) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Charitable_Elementor_Donation_Form_Widget |
| 15 |
*/ |
| 16 |
class Charitable_Elementor_Donation_Form_Widget extends \Elementor\Widget_Base |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Get widget name. |
| 20 |
* |
| 21 |
* @return string Widget name. |
| 22 |
*/ |
| 23 |
public function get_name() |
| 24 |
{ |
| 25 |
return 'charitable_donation_form'; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Get widget title. |
| 30 |
* |
| 31 |
* @return string Widget title. |
| 32 |
*/ |
| 33 |
public function get_title() |
| 34 |
{ |
| 35 |
return esc_html__('Donation Form', 'charitable'); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get widget icon. |
| 40 |
* |
| 41 |
* @return string Widget icon. |
| 42 |
*/ |
| 43 |
public function get_icon() |
| 44 |
{ |
| 45 |
return 'eicon-form-horizontal'; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get widget categories. |
| 50 |
* |
| 51 |
* @return array Widget categories. |
| 52 |
*/ |
| 53 |
public function get_categories() |
| 54 |
{ |
| 55 |
return array( 'wpcharitable' ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Register widget controls. |
| 60 |
*/ |
| 61 |
protected function register_controls() |
| 62 |
{ |
| 63 |
// Content Section. |
| 64 |
$this->start_controls_section( |
| 65 |
'content_section', |
| 66 |
array( |
| 67 |
'label' => esc_html__('Content', 'charitable'), |
| 68 |
'tab' => \Elementor\Controls_Manager::TAB_CONTENT, |
| 69 |
) |
| 70 |
); |
| 71 |
|
| 72 |
// Get campaigns for dropdown. |
| 73 |
$campaigns = get_posts( |
| 74 |
array( |
| 75 |
'post_type' => 'campaign', |
| 76 |
'numberposts' => -1, |
| 77 |
'orderby' => 'title', |
| 78 |
'order' => 'ASC', |
| 79 |
) |
| 80 |
); |
| 81 |
|
| 82 |
$campaign_options = array(); |
| 83 |
foreach ($campaigns as $campaign) { |
| 84 |
$campaign_options[ $campaign->ID ] = $campaign->post_title; |
| 85 |
} |
| 86 |
|
| 87 |
$this->add_control( |
| 88 |
'campaign_id', |
| 89 |
array( |
| 90 |
'label' => esc_html__('Select Campaign', 'charitable'), |
| 91 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 92 |
'options' => $campaign_options, |
| 93 |
'label_block' => true, |
| 94 |
'dynamic' => ['active' => true], |
| 95 |
) |
| 96 |
); |
| 97 |
|
| 98 |
$this->add_control( |
| 99 |
'form_type', |
| 100 |
array( |
| 101 |
'label' => esc_html__('Form Type', 'charitable'), |
| 102 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 103 |
'options' => array( |
| 104 |
'full' => esc_html__('Full Form', 'charitable'), |
| 105 |
'minimal' => esc_html__('Minimal Form', 'charitable'), |
| 106 |
), |
| 107 |
'default' => 'full', |
| 108 |
) |
| 109 |
); |
| 110 |
|
| 111 |
$this->end_controls_section(); |
| 112 |
|
| 113 |
// Style Section. |
| 114 |
$this->start_controls_section( |
| 115 |
'style_section', |
| 116 |
array( |
| 117 |
'label' => esc_html__('Style', 'charitable'), |
| 118 |
'tab' => \Elementor\Controls_Manager::TAB_STYLE, |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
$this->add_responsive_control( |
| 123 |
'padding', |
| 124 |
array( |
| 125 |
'label' => esc_html__('Padding', 'charitable'), |
| 126 |
'type' => \Elementor\Controls_Manager::DIMENSIONS, |
| 127 |
'size_units' => array( 'px', 'em', '%' ), |
| 128 |
'selectors' => array( |
| 129 |
'{{WRAPPER}} .charitable-elementor-editor-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 130 |
), |
| 131 |
) |
| 132 |
); |
| 133 |
|
| 134 |
$this->add_group_control( |
| 135 |
\Elementor\Group_Control_Border::get_type(), |
| 136 |
array( |
| 137 |
'name' => 'border', |
| 138 |
'selector' => '{{WRAPPER}} .charitable-elementor-editor-wrapper', |
| 139 |
) |
| 140 |
); |
| 141 |
|
| 142 |
$this->add_control( |
| 143 |
'background_color', |
| 144 |
array( |
| 145 |
'label' => esc_html__('Background Color', 'charitable'), |
| 146 |
'type' => \Elementor\Controls_Manager::COLOR, |
| 147 |
'selectors' => array( |
| 148 |
'{{WRAPPER}} .charitable-elementor-editor-wrapper' => 'background-color: {{VALUE}};', |
| 149 |
), |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
$this->end_controls_section(); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Render widget output on the frontend. |
| 158 |
*/ |
| 159 |
protected function render() |
| 160 |
{ |
| 161 |
$settings = $this->get_settings_for_display(); |
| 162 |
|
| 163 |
$output = '<div class="charitable-elementor-editor-wrapper">'; |
| 164 |
|
| 165 |
if (! empty($settings['campaign_id'])) { |
| 166 |
// Build shortcode attributes |
| 167 |
$shortcode_atts = array(); |
| 168 |
|
| 169 |
// Campaign ID (required) |
| 170 |
$shortcode_atts[] = 'campaign_id="' . absint($settings['campaign_id']) . '"'; |
| 171 |
|
| 172 |
// Form Type (optional) |
| 173 |
if (! empty($settings['form_type'])) { |
| 174 |
$shortcode_atts[] = 'type="' . esc_attr($settings['form_type']) . '"'; |
| 175 |
} |
| 176 |
|
| 177 |
// Build the complete shortcode |
| 178 |
|
| 179 |
// charitable_donation_form campaign_id="180" type="full" |
| 180 |
$shortcode = '[charitable_donation_form ' . implode(' ', $shortcode_atts) . ']'; |
| 181 |
|
| 182 |
|
| 183 |
// Process the shortcode |
| 184 |
$output .= do_shortcode($shortcode); |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
$output .= '</div>'; |
| 189 |
|
| 190 |
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 191 |
} |
| 192 |
} |
| 193 |
|