| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Campaign Widget |
| 4 |
* |
| 5 |
* @package Elementor_Example_Plugin |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Charitable_Campaign_Widget |
| 14 |
*/ |
| 15 |
class Charitable_Elementor_Campaigns_Widget extends \Elementor\Widget_Base { |
| 16 |
|
| 17 |
/** |
| 18 |
* Get widget name. |
| 19 |
* |
| 20 |
* @return string Widget name. |
| 21 |
*/ |
| 22 |
public function get_name() { |
| 23 |
return 'charitable_campaigns'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get widget title. |
| 28 |
* |
| 29 |
* @return string Widget title. |
| 30 |
*/ |
| 31 |
public function get_title() { |
| 32 |
return esc_html__( 'Campaigns', 'charitable' ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get widget icon. |
| 37 |
* |
| 38 |
* @return string Widget icon. |
| 39 |
*/ |
| 40 |
public function get_icon() { |
| 41 |
return 'eicon-gallery-grid'; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get widget categories. |
| 46 |
* |
| 47 |
* @return array Widget categories. |
| 48 |
*/ |
| 49 |
public function get_categories() { |
| 50 |
return array( 'wpcharitable' ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Check if we're in Elementor editor mode. |
| 55 |
* |
| 56 |
* @return bool Whether we're in editor mode. |
| 57 |
*/ |
| 58 |
private function is_elementor_editor() { |
| 59 |
return \Elementor\Plugin::$instance->editor->is_edit_mode() || \Elementor\Plugin::$instance->preview->is_preview_mode(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Register widget styles. |
| 64 |
*/ |
| 65 |
public function register_styles() { |
| 66 |
wp_register_style( |
| 67 |
'charitable-elementor-editor', |
| 68 |
charitable()->get_path( 'assets', false ) . 'css/charitable-elementor-editor.css', |
| 69 |
array(), |
| 70 |
charitable()->get_version() |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Enqueue editor-specific styles. |
| 76 |
*/ |
| 77 |
public function enqueue_editor_styles() { |
| 78 |
if ( $this->is_elementor_editor() ) { |
| 79 |
wp_enqueue_style( 'charitable-elementor-editor' ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Widget constructor. |
| 85 |
* |
| 86 |
* @param array $data Widget data. |
| 87 |
* @param array $args Widget arguments. |
| 88 |
*/ |
| 89 |
public function __construct( $data = array(), $args = null ) { |
| 90 |
parent::__construct( $data, $args ); |
| 91 |
|
| 92 |
// Register and enqueue styles |
| 93 |
add_action( 'elementor/frontend/after_register_styles', array( $this, 'register_styles' ) ); |
| 94 |
add_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'enqueue_editor_styles' ) ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Register widget controls. |
| 99 |
*/ |
| 100 |
protected function register_controls() { |
| 101 |
// Content Section. |
| 102 |
$this->start_controls_section( |
| 103 |
'content_section', |
| 104 |
array( |
| 105 |
'label' => esc_html__( 'Campaign Display Settings', 'charitable' ), |
| 106 |
'tab' => \Elementor\Controls_Manager::TAB_CONTENT, |
| 107 |
) |
| 108 |
); |
| 109 |
|
| 110 |
// Number of campaigns |
| 111 |
$this->add_control( |
| 112 |
'number', |
| 113 |
array( |
| 114 |
'label' => esc_html__( 'Number of Campaigns', 'charitable' ), |
| 115 |
'type' => \Elementor\Controls_Manager::NUMBER, |
| 116 |
'description' => esc_html__( 'Set to -1 to show all campaigns. Default is the number of blog posts configured in WordPress settings.', 'charitable' ), |
| 117 |
'default' => '', |
| 118 |
'placeholder' => '', |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
// Order By |
| 123 |
$this->add_control( |
| 124 |
'orderby', |
| 125 |
array( |
| 126 |
'label' => esc_html__( 'Order By', 'charitable' ), |
| 127 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 128 |
'options' => array( |
| 129 |
'post_date' => esc_html__( 'Date', 'charitable' ), |
| 130 |
'popular' => esc_html__( 'Popularity', 'charitable' ), |
| 131 |
'ending' => esc_html__( 'Ending Soon', 'charitable' ), |
| 132 |
'title' => esc_html__( 'Title', 'charitable' ), |
| 133 |
'rand' => esc_html__( 'Random', 'charitable' ), |
| 134 |
), |
| 135 |
'default' => 'post_date', |
| 136 |
) |
| 137 |
); |
| 138 |
|
| 139 |
// Order |
| 140 |
$this->add_control( |
| 141 |
'order', |
| 142 |
array( |
| 143 |
'label' => esc_html__( 'Sort Order', 'charitable' ), |
| 144 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 145 |
'options' => array( |
| 146 |
'DESC' => esc_html__( 'Descending', 'charitable' ), |
| 147 |
'ASC' => esc_html__( 'Ascending', 'charitable' ), |
| 148 |
), |
| 149 |
'default' => 'DESC', |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
// Categories |
| 154 |
$this->add_control( |
| 155 |
'category', |
| 156 |
array( |
| 157 |
'label' => esc_html__( 'Categories', 'charitable' ), |
| 158 |
'type' => \Elementor\Controls_Manager::TEXT, |
| 159 |
'description' => esc_html__( 'Filter by category slug. For multiple categories, separate with commas.', 'charitable' ), |
| 160 |
'placeholder' => 'category-1,category-2', |
| 161 |
) |
| 162 |
); |
| 163 |
|
| 164 |
// Creator |
| 165 |
$this->add_control( |
| 166 |
'creator', |
| 167 |
array( |
| 168 |
'label' => esc_html__( 'Creator', 'charitable' ), |
| 169 |
'type' => \Elementor\Controls_Manager::NUMBER, |
| 170 |
'description' => esc_html__( 'Only show campaigns created by a specific user ID.', 'charitable' ), |
| 171 |
'placeholder' => esc_html__( 'User ID', 'charitable' ), |
| 172 |
) |
| 173 |
); |
| 174 |
|
| 175 |
// Exclude |
| 176 |
$this->add_control( |
| 177 |
'exclude', |
| 178 |
array( |
| 179 |
'label' => esc_html__( 'Exclude Campaigns', 'charitable' ), |
| 180 |
'type' => \Elementor\Controls_Manager::TEXT, |
| 181 |
'description' => esc_html__( 'Campaign IDs to exclude. Separate multiple IDs with commas.', 'charitable' ), |
| 182 |
'placeholder' => '12,34,56', |
| 183 |
) |
| 184 |
); |
| 185 |
|
| 186 |
// Include Inactive |
| 187 |
$this->add_control( |
| 188 |
'include_inactive', |
| 189 |
array( |
| 190 |
'label' => esc_html__( 'Include Inactive Campaigns', 'charitable' ), |
| 191 |
'type' => \Elementor\Controls_Manager::SWITCHER, |
| 192 |
'label_on' => esc_html__( 'Yes', 'charitable' ), |
| 193 |
'label_off' => esc_html__( 'No', 'charitable' ), |
| 194 |
'return_value' => 'true', |
| 195 |
'default' => '', |
| 196 |
) |
| 197 |
); |
| 198 |
|
| 199 |
// Columns |
| 200 |
$this->add_control( |
| 201 |
'columns', |
| 202 |
array( |
| 203 |
'label' => esc_html__( 'Columns', 'charitable' ), |
| 204 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 205 |
'options' => array( |
| 206 |
'1' => esc_html__( '1 Column', 'charitable' ), |
| 207 |
'2' => esc_html__( '2 Columns', 'charitable' ), |
| 208 |
'3' => esc_html__( '3 Columns', 'charitable' ), |
| 209 |
'4' => esc_html__( '4 Columns', 'charitable' ), |
| 210 |
), |
| 211 |
'default' => '2', |
| 212 |
) |
| 213 |
); |
| 214 |
|
| 215 |
// Button |
| 216 |
// $this->add_control( |
| 217 |
// 'button', |
| 218 |
// array( |
| 219 |
// 'label' => esc_html__('Button Type', 'charitable'), |
| 220 |
// 'type' => \Elementor\Controls_Manager::SELECT, |
| 221 |
// 'options' => array( |
| 222 |
// 'donate' => esc_html__('Donate Button', 'charitable'), |
| 223 |
// 'details' => esc_html__('Read More Link', 'charitable'), |
| 224 |
// '0' => esc_html__('None', 'charitable'), |
| 225 |
// ), |
| 226 |
// 'default' => 'donate', |
| 227 |
// ) |
| 228 |
// ); |
| 229 |
|
| 230 |
// // Specific IDs |
| 231 |
// $this->add_control( |
| 232 |
// 'id', |
| 233 |
// array( |
| 234 |
// 'label' => esc_html__('Specific Campaign IDs', 'charitable'), |
| 235 |
// 'type' => \Elementor\Controls_Manager::TEXT, |
| 236 |
// 'description' => esc_html__('Show only specific campaigns by ID. Separate multiple IDs with commas.', 'charitable'), |
| 237 |
// 'placeholder' => '123,456,789', |
| 238 |
// ) |
| 239 |
// ); |
| 240 |
|
| 241 |
// Responsive |
| 242 |
// $this->add_control( |
| 243 |
// 'responsive', |
| 244 |
// array( |
| 245 |
// 'label' => esc_html__('Responsive Layout', 'charitable'), |
| 246 |
// 'type' => \Elementor\Controls_Manager::SELECT, |
| 247 |
// 'options' => array( |
| 248 |
// '' => esc_html__('Default (768px)', 'charitable'), |
| 249 |
// '0' => esc_html__('Disabled', 'charitable'), |
| 250 |
// 'custom' => esc_html__('Custom Breakpoint', 'charitable'), |
| 251 |
// ), |
| 252 |
// 'default' => '', |
| 253 |
// ) |
| 254 |
// ); |
| 255 |
|
| 256 |
// // Custom responsive breakpoint (shown conditionally) |
| 257 |
// $this->add_control( |
| 258 |
// 'responsive_custom', |
| 259 |
// array( |
| 260 |
// 'label' => esc_html__('Custom Breakpoint', 'charitable'), |
| 261 |
// 'type' => \Elementor\Controls_Manager::TEXT, |
| 262 |
// 'description' => esc_html__('Enter a value with px or em (e.g. 480px)', 'charitable'), |
| 263 |
// 'placeholder' => '480px', |
| 264 |
// 'condition' => array( |
| 265 |
// 'responsive' => 'custom', |
| 266 |
// ), |
| 267 |
// ) |
| 268 |
// ); |
| 269 |
|
| 270 |
// Masonry |
| 271 |
// $this->add_control( |
| 272 |
// 'masonry', |
| 273 |
// array( |
| 274 |
// 'label' => esc_html__('Masonry Layout', 'charitable'), |
| 275 |
// 'type' => \Elementor\Controls_Manager::SWITCHER, |
| 276 |
// 'label_on' => esc_html__('Yes', 'charitable'), |
| 277 |
// 'label_off' => esc_html__('No', 'charitable'), |
| 278 |
// 'return_value' => '1', |
| 279 |
// 'default' => '', |
| 280 |
// ) |
| 281 |
// ); |
| 282 |
|
| 283 |
$this->end_controls_section(); |
| 284 |
|
| 285 |
// Style Section. |
| 286 |
$this->start_controls_section( |
| 287 |
'style_section', |
| 288 |
array( |
| 289 |
'label' => esc_html__( 'Style', 'charitable' ), |
| 290 |
'tab' => \Elementor\Controls_Manager::TAB_STYLE, |
| 291 |
) |
| 292 |
); |
| 293 |
|
| 294 |
$this->add_responsive_control( |
| 295 |
'padding', |
| 296 |
array( |
| 297 |
'label' => esc_html__( 'Padding', 'charitable' ), |
| 298 |
'type' => \Elementor\Controls_Manager::DIMENSIONS, |
| 299 |
'size_units' => array( 'px', 'em', '%' ), |
| 300 |
'selectors' => array( |
| 301 |
'{{WRAPPER}} .charitable-campaigns-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 302 |
), |
| 303 |
) |
| 304 |
); |
| 305 |
|
| 306 |
$this->add_group_control( |
| 307 |
\Elementor\Group_Control_Border::get_type(), |
| 308 |
array( |
| 309 |
'name' => 'border', |
| 310 |
'selector' => '{{WRAPPER}} .charitable-campaigns-wrapper', |
| 311 |
) |
| 312 |
); |
| 313 |
|
| 314 |
$this->add_control( |
| 315 |
'background_color', |
| 316 |
array( |
| 317 |
'label' => esc_html__( 'Background Color', 'charitable' ), |
| 318 |
'type' => \Elementor\Controls_Manager::COLOR, |
| 319 |
'selectors' => array( |
| 320 |
'{{WRAPPER}} .charitable-campaigns-wrapper' => 'background-color: {{VALUE}};', |
| 321 |
), |
| 322 |
) |
| 323 |
); |
| 324 |
|
| 325 |
$this->end_controls_section(); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Render widget output on the frontend. |
| 330 |
*/ |
| 331 |
protected function render() { |
| 332 |
$settings = $this->get_settings_for_display(); |
| 333 |
|
| 334 |
// Determine if we're in Elementor editor |
| 335 |
$is_editor = $this->is_elementor_editor(); |
| 336 |
|
| 337 |
// Start wrapper with appropriate classes |
| 338 |
$wrapper_classes = array( 'charitable-elementor-editor-wrapper' ); |
| 339 |
if ( $is_editor ) { |
| 340 |
$wrapper_classes[] = 'in-elementor-editor'; |
| 341 |
} |
| 342 |
|
| 343 |
$output = '<div class="' . esc_attr( implode( ' ', $wrapper_classes ) ) . '">'; |
| 344 |
|
| 345 |
// Build shortcode attributes |
| 346 |
$shortcode_atts = array(); |
| 347 |
|
| 348 |
// Number of campaigns |
| 349 |
if ( ! empty( $settings['number'] ) ) { |
| 350 |
$shortcode_atts[] = 'number="' . absint( $settings['number'] ) . '"'; |
| 351 |
} |
| 352 |
|
| 353 |
// Order By |
| 354 |
if ( ! empty( $settings['orderby'] ) ) { |
| 355 |
$shortcode_atts[] = 'orderby="' . esc_attr( $settings['orderby'] ) . '"'; |
| 356 |
} |
| 357 |
|
| 358 |
// Order |
| 359 |
if ( ! empty( $settings['order'] ) ) { |
| 360 |
$shortcode_atts[] = 'order="' . esc_attr( $settings['order'] ) . '"'; |
| 361 |
} |
| 362 |
|
| 363 |
// Categories |
| 364 |
if ( ! empty( $settings['category'] ) ) { |
| 365 |
$shortcode_atts[] = 'category="' . esc_attr( $settings['category'] ) . '"'; |
| 366 |
} |
| 367 |
|
| 368 |
// Creator |
| 369 |
if ( ! empty( $settings['creator'] ) ) { |
| 370 |
$shortcode_atts[] = 'creator="' . absint( $settings['creator'] ) . '"'; |
| 371 |
} |
| 372 |
|
| 373 |
// Exclude |
| 374 |
if ( ! empty( $settings['exclude'] ) ) { |
| 375 |
$shortcode_atts[] = 'exclude="' . esc_attr( $settings['exclude'] ) . '"'; |
| 376 |
} |
| 377 |
|
| 378 |
// Include Inactive |
| 379 |
if ( ! empty( $settings['include_inactive'] ) && 'true' === $settings['include_inactive'] ) { |
| 380 |
$shortcode_atts[] = 'include_inactive="1"'; |
| 381 |
} |
| 382 |
|
| 383 |
// Columns |
| 384 |
if ( ! empty( $settings['columns'] ) ) { |
| 385 |
$shortcode_atts[] = 'columns="' . absint( $settings['columns'] ) . '"'; |
| 386 |
} |
| 387 |
|
| 388 |
// Build the complete shortcode |
| 389 |
$shortcode = '[campaigns ' . implode( ' ', $shortcode_atts ) . ']'; |
| 390 |
|
| 391 |
// Process the shortcode |
| 392 |
$output .= do_shortcode( $shortcode ); |
| 393 |
|
| 394 |
$output .= '</div>'; |
| 395 |
|
| 396 |
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 397 |
} |
| 398 |
} |
| 399 |
|