| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor SureForms Payment History widget. |
| 4 |
* |
| 5 |
* @package sureforms. |
| 6 |
* @since 2.12.2 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc\Page_Builders\Elementor; |
| 10 |
|
| 11 |
use Elementor\Controls_Manager; |
| 12 |
use Elementor\Widget_Base; |
| 13 |
use SRFM\Inc\Payments\Payment_History_Shortcode; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* SureForms Elementor widget that displays the Payment History (parity with the srfm/payment-history block). |
| 21 |
* |
| 22 |
* @since 2.12.2 |
| 23 |
*/ |
| 24 |
class Payment_History_Widget extends Widget_Base { |
| 25 |
/** |
| 26 |
* Get widget name. |
| 27 |
* |
| 28 |
* @since 2.12.2 |
| 29 |
* @return string Widget name. |
| 30 |
*/ |
| 31 |
public function get_name() { |
| 32 |
return 'srfm-payment-history'; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get widget title. |
| 37 |
* |
| 38 |
* @since 2.12.2 |
| 39 |
* @return string Widget title. |
| 40 |
*/ |
| 41 |
public function get_title() { |
| 42 |
return __( 'Payment History', 'sureforms' ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get widget icon. |
| 47 |
* |
| 48 |
* @since 2.12.2 |
| 49 |
* @return string Widget icon. |
| 50 |
*/ |
| 51 |
public function get_icon() { |
| 52 |
return 'eicon-price-list srfm-elementor-widget-icon'; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get widget categories. |
| 57 |
* |
| 58 |
* @since 2.12.2 |
| 59 |
* @return array<string> Widget categories. |
| 60 |
*/ |
| 61 |
public function get_categories() { |
| 62 |
return [ 'sureforms-elementor' ]; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get widget keywords. |
| 67 |
* |
| 68 |
* @since 2.12.2 |
| 69 |
* @return array<string> Widget keywords. |
| 70 |
*/ |
| 71 |
public function get_keywords() { |
| 72 |
return [ |
| 73 |
'sureforms', |
| 74 |
'payment', |
| 75 |
'payment history', |
| 76 |
'subscription', |
| 77 |
'transactions', |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Declare the payment-history stylesheet as a widget style dependency so Elementor |
| 83 |
* enqueues it in the <head>. This widget's content lives in postmeta, so the |
| 84 |
* shortcode's has_block()/has_shortcode() gate on wp_enqueue_scripts can't detect |
| 85 |
* it; without this the CSS would only load at render() time (footer) and the |
| 86 |
* dashboard would flash unstyled (FOUC). The handle is registered by |
| 87 |
* Payment_History_Shortcode::register_assets() on wp_enqueue_scripts (priority 1). |
| 88 |
* |
| 89 |
* @since 2.12.3 |
| 90 |
* @return array<string> Style handles this widget depends on. |
| 91 |
*/ |
| 92 |
public function get_style_depends() { |
| 93 |
return [ 'srfm-payment-history' ]; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Register widget controls. |
| 98 |
* |
| 99 |
* @since 2.12.2 |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
protected function register_controls() { |
| 103 |
$this->start_controls_section( |
| 104 |
'srfm_payment_history_section', |
| 105 |
[ |
| 106 |
'label' => __( 'Payment History', 'sureforms' ), |
| 107 |
] |
| 108 |
); |
| 109 |
|
| 110 |
$this->add_control( |
| 111 |
'per_page', |
| 112 |
[ |
| 113 |
'label' => __( 'Items per page', 'sureforms' ), |
| 114 |
'type' => Controls_Manager::NUMBER, |
| 115 |
'min' => 1, |
| 116 |
'default' => 10, |
| 117 |
] |
| 118 |
); |
| 119 |
|
| 120 |
$this->add_control( |
| 121 |
'show_subscription', |
| 122 |
[ |
| 123 |
'label' => __( 'Show Subscriptions', 'sureforms' ), |
| 124 |
'type' => Controls_Manager::SWITCHER, |
| 125 |
'label_on' => __( 'Show', 'sureforms' ), |
| 126 |
'label_off' => __( 'Hide', 'sureforms' ), |
| 127 |
'return_value' => 'true', |
| 128 |
'default' => 'true', |
| 129 |
] |
| 130 |
); |
| 131 |
|
| 132 |
$this->end_controls_section(); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Render the widget output on the frontend. |
| 137 |
* |
| 138 |
* Delegates to the Payment_History_Shortcode, mirroring the Gutenberg block. |
| 139 |
* |
| 140 |
* @since 2.12.2 |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
protected function render() { |
| 144 |
$settings = $this->get_settings_for_display(); |
| 145 |
|
| 146 |
if ( ! is_array( $settings ) ) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
$per_page = absint( $settings['per_page'] ?? 10 ); |
| 151 |
if ( $per_page <= 0 ) { |
| 152 |
$per_page = 10; |
| 153 |
} |
| 154 |
|
| 155 |
$atts = [ |
| 156 |
'per_page' => strval( $per_page ), |
| 157 |
'show_subscription' => ! empty( $settings['show_subscription'] ) && 'true' === $settings['show_subscription'] ? 'true' : 'false', |
| 158 |
]; |
| 159 |
|
| 160 |
echo Payment_History_Shortcode::get_instance()->render( $atts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped within the shortcode renderer. |
| 161 |
} |
| 162 |
} |
| 163 |
|