| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Charitable Campaign 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_Campaign_Widget |
| 15 |
*/ |
| 16 |
class Charitable_Elementor_Button_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_button'; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Get widget title. |
| 30 |
* |
| 31 |
* @return string Widget title. |
| 32 |
*/ |
| 33 |
public function get_title() |
| 34 |
{ |
| 35 |
return esc_html__('Donate Button/Link', 'charitable'); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get widget icon. |
| 40 |
* |
| 41 |
* @return string Widget icon. |
| 42 |
*/ |
| 43 |
public function get_icon() |
| 44 |
{ |
| 45 |
return 'eicon-button'; |
| 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 |
$this->add_control( |
| 73 |
'label', |
| 74 |
array( |
| 75 |
'label' => esc_html__('Label', 'charitable'), |
| 76 |
'type' => \Elementor\Controls_Manager::TEXT, |
| 77 |
'default' => esc_html__('Donate', 'charitable'), |
| 78 |
) |
| 79 |
); |
| 80 |
|
| 81 |
// Get campaigns for dropdown. |
| 82 |
$campaigns = get_posts( |
| 83 |
array( |
| 84 |
'post_type' => 'campaign', |
| 85 |
'numberposts' => -1, |
| 86 |
'orderby' => 'title', |
| 87 |
'order' => 'ASC', |
| 88 |
) |
| 89 |
); |
| 90 |
|
| 91 |
$campaign_options = array(); |
| 92 |
foreach ($campaigns as $campaign) { |
| 93 |
$campaign_options[ $campaign->ID ] = $campaign->post_title; |
| 94 |
} |
| 95 |
|
| 96 |
$this->add_control( |
| 97 |
'campaign_id', |
| 98 |
array( |
| 99 |
'label' => esc_html__('Select Campaign', 'charitable'), |
| 100 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 101 |
'options' => $campaign_options, |
| 102 |
'label_block' => true, |
| 103 |
'dynamic' => ['active' => true], |
| 104 |
) |
| 105 |
); |
| 106 |
|
| 107 |
$this->add_control( |
| 108 |
'new_tab', |
| 109 |
array( |
| 110 |
'label' => esc_html__('Open in New Tab', 'charitable'), |
| 111 |
'type' => \Elementor\Controls_Manager::SWITCHER, |
| 112 |
'label_on' => esc_html__('Yes', 'charitable'), |
| 113 |
'label_off' => esc_html__('No', 'charitable'), |
| 114 |
'return_value' => 'yes', |
| 115 |
'default' => 'label_off', |
| 116 |
) |
| 117 |
); |
| 118 |
|
| 119 |
// add dropdown for button type - link or button |
| 120 |
$this->add_control( |
| 121 |
'button_type', |
| 122 |
array( |
| 123 |
'label' => esc_html__('Button Type', 'charitable'), |
| 124 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 125 |
'options' => array( |
| 126 |
'link' => esc_html__('Link', 'charitable'), |
| 127 |
'button' => esc_html__('Button', 'charitable'), |
| 128 |
), |
| 129 |
'default' => 'button', |
| 130 |
) |
| 131 |
); |
| 132 |
|
| 133 |
$this->end_controls_section(); |
| 134 |
|
| 135 |
// Style Section. |
| 136 |
$this->start_controls_section( |
| 137 |
'style_section', |
| 138 |
array( |
| 139 |
'label' => esc_html__('Style', 'charitable'), |
| 140 |
'tab' => \Elementor\Controls_Manager::TAB_STYLE, |
| 141 |
) |
| 142 |
); |
| 143 |
|
| 144 |
$this->add_responsive_control( |
| 145 |
'padding', |
| 146 |
array( |
| 147 |
'label' => esc_html__('Padding', 'charitable'), |
| 148 |
'type' => \Elementor\Controls_Manager::DIMENSIONS, |
| 149 |
'size_units' => array( 'px', 'em', '%' ), |
| 150 |
'selectors' => array( |
| 151 |
'{{WRAPPER}} .charitable-campaign-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 152 |
), |
| 153 |
) |
| 154 |
); |
| 155 |
|
| 156 |
$this->add_group_control( |
| 157 |
\Elementor\Group_Control_Border::get_type(), |
| 158 |
array( |
| 159 |
'name' => 'border', |
| 160 |
'selector' => '{{WRAPPER}} .charitable-campaign-wrapper', |
| 161 |
) |
| 162 |
); |
| 163 |
|
| 164 |
$this->add_control( |
| 165 |
'background_color', |
| 166 |
array( |
| 167 |
'label' => esc_html__('Background Color', 'charitable'), |
| 168 |
'type' => \Elementor\Controls_Manager::COLOR, |
| 169 |
'selectors' => array( |
| 170 |
'{{WRAPPER}} .charitable-campaign-wrapper' => 'background-color: {{VALUE}};', |
| 171 |
), |
| 172 |
) |
| 173 |
); |
| 174 |
|
| 175 |
$this->end_controls_section(); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Render widget output on the frontend. |
| 180 |
*/ |
| 181 |
protected function render() |
| 182 |
{ |
| 183 |
$settings = $this->get_settings_for_display(); |
| 184 |
|
| 185 |
$output = '<div class="charitable-elementor-editor-wrapper">'; |
| 186 |
|
| 187 |
if (! empty($settings['campaign_id'])) { |
| 188 |
// Build shortcode attributes |
| 189 |
$shortcode_atts = array(); |
| 190 |
|
| 191 |
// Campaign ID (required) |
| 192 |
$shortcode_atts[] = 'campaign="' . absint($settings['campaign_id']) . '"'; |
| 193 |
|
| 194 |
// Label (optional) |
| 195 |
if (! empty($settings['label'])) { |
| 196 |
$shortcode_atts[] = 'label="' . esc_attr($settings['label']) . '"'; |
| 197 |
} |
| 198 |
|
| 199 |
// New tab (optional) |
| 200 |
if (! empty($settings['new_tab']) && 'yes' === $settings['new_tab']) { |
| 201 |
$shortcode_atts[] = 'new_tab="true"'; |
| 202 |
} |
| 203 |
|
| 204 |
// Button type (optional) |
| 205 |
if (! empty($settings['button_type'])) { |
| 206 |
$shortcode_atts[] = 'type="' . esc_attr($settings['button_type']) . '"'; |
| 207 |
} |
| 208 |
|
| 209 |
// Build the complete shortcode |
| 210 |
$shortcode = '[charitable_donate_button ' . implode(' ', $shortcode_atts) . ']'; |
| 211 |
|
| 212 |
// Process the shortcode |
| 213 |
$output .= do_shortcode($shortcode); |
| 214 |
} |
| 215 |
|
| 216 |
$output .= '</div>'; |
| 217 |
|
| 218 |
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
} |
| 223 |
|