| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor service provider. |
| 4 |
* |
| 5 |
* Registers the SureDonation widget category and all campaign/donation widgets |
| 6 |
* with Elementor, and loads the campaign block styles into the editor preview |
| 7 |
* so the widgets render styled while editing. |
| 8 |
* |
| 9 |
* @package SureDonation |
| 10 |
* @since 1.2.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace SureDonation\Inc\Page_Builders\Elementor; |
| 14 |
|
| 15 |
use SureDonation\Inc\Page_Builders\Elementor\Widgets\Campaign_Donate_Button_Widget; |
| 16 |
use SureDonation\Inc\Page_Builders\Elementor\Widgets\Campaign_Donations_Widget; |
| 17 |
use SureDonation\Inc\Page_Builders\Elementor\Widgets\Campaign_Donors_Widget; |
| 18 |
use SureDonation\Inc\Page_Builders\Elementor\Widgets\Campaign_Goal_Widget; |
| 19 |
use SureDonation\Inc\Page_Builders\Elementor\Widgets\Campaign_Social_Sharing_Widget; |
| 20 |
use SureDonation\Inc\Page_Builders\Elementor\Widgets\Campaign_Stats_Widget; |
| 21 |
use SureDonation\Inc\Page_Builders\Elementor\Widgets\Donation_Form_Widget; |
| 22 |
use SureDonation\Inc\Traits\Get_Instance; |
| 23 |
|
| 24 |
if ( ! defined( 'ABSPATH' ) ) { |
| 25 |
exit; // Exit if accessed directly. |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Service_Provider class. |
| 30 |
* |
| 31 |
* @since 1.2.0 |
| 32 |
*/ |
| 33 |
class Service_Provider { |
| 34 |
use Get_Instance; |
| 35 |
|
| 36 |
/** |
| 37 |
* Widget category slug. |
| 38 |
* |
| 39 |
* @since 1.2.0 |
| 40 |
*/ |
| 41 |
public const CATEGORY = 'suredonation'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor — register hooks only when Elementor is available. |
| 45 |
* |
| 46 |
* @since 1.2.0 |
| 47 |
*/ |
| 48 |
public function __construct() { |
| 49 |
if ( ! class_exists( '\Elementor\Plugin' ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
add_action( 'elementor/elements/categories_registered', [ $this, 'register_category' ] ); |
| 54 |
add_action( 'elementor/widgets/register', [ $this, 'register_widgets' ] ); |
| 55 |
add_action( 'elementor/preview/enqueue_styles', [ $this, 'enqueue_preview_assets' ] ); |
| 56 |
add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'enqueue_editor_assets' ] ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Load the editor-panel script that backs the Donation Form widget's |
| 61 |
* "Edit Form" button. |
| 62 |
* |
| 63 |
* Runs in the Elementor editor panel (not the preview iframe), which is |
| 64 |
* where the control's `event` is broadcast on `elementor.channels.editor`. |
| 65 |
* |
| 66 |
* @since 1.4.0 |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function enqueue_editor_assets() { |
| 70 |
wp_enqueue_script( |
| 71 |
'suredonation-elementor-editor', |
| 72 |
plugins_url( 'assets/editor.js', __FILE__ ), |
| 73 |
[], |
| 74 |
SUREDONATION_VER, |
| 75 |
true |
| 76 |
); |
| 77 |
|
| 78 |
wp_localize_script( |
| 79 |
'suredonation-elementor-editor', |
| 80 |
'suredonationElementorData', |
| 81 |
[ |
| 82 |
'adminUrl' => admin_url(), |
| 83 |
] |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Register the SureDonation widget category. |
| 89 |
* |
| 90 |
* @since 1.2.0 |
| 91 |
* @param \Elementor\Elements_Manager $elements_manager Elementor elements manager. |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function register_category( $elements_manager ) { |
| 95 |
if ( ! method_exists( $elements_manager, 'add_category' ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$elements_manager->add_category( |
| 100 |
self::CATEGORY, |
| 101 |
[ |
| 102 |
'title' => esc_html__( 'SureDonation', 'suredonation' ), |
| 103 |
'icon' => 'eicon-heart', |
| 104 |
] |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Register every SureDonation widget. |
| 110 |
* |
| 111 |
* @since 1.2.0 |
| 112 |
* @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager. |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public function register_widgets( $widgets_manager ) { |
| 116 |
if ( ! method_exists( $widgets_manager, 'register' ) ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
$widget_classes = [ |
| 121 |
Donation_Form_Widget::class, |
| 122 |
Campaign_Goal_Widget::class, |
| 123 |
Campaign_Stats_Widget::class, |
| 124 |
Campaign_Donations_Widget::class, |
| 125 |
Campaign_Donors_Widget::class, |
| 126 |
Campaign_Donate_Button_Widget::class, |
| 127 |
Campaign_Social_Sharing_Widget::class, |
| 128 |
]; |
| 129 |
|
| 130 |
// Construct inside the loop so one broken widget can't abort |
| 131 |
// registration of the others. |
| 132 |
foreach ( $widget_classes as $widget_class ) { |
| 133 |
$widgets_manager->register( new $widget_class() ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Load the campaign block styles into the Elementor editor preview so the |
| 139 |
* server-rendered widgets look correct while editing. On the front end each |
| 140 |
* block's render_callback enqueues the same handle itself. |
| 141 |
* |
| 142 |
* @since 1.2.0 |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function enqueue_preview_assets() { |
| 146 |
wp_enqueue_style( 'suredonation-campaign-blocks' ); |
| 147 |
} |
| 148 |
} |
| 149 |
|