Actions
9 months ago
Settings
10 months ago
Traits
10 months ago
Widgets
9 months ago
ServiceProvider.php
9 months ago
ServiceProvider.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\ThirdPartySupport\Elementor; |
| 4 | |
| 5 | use Give\Helpers\Hooks; |
| 6 | use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface; |
| 7 | use Give\ThirdPartySupport\Elementor\Actions\RegisterWidgets; |
| 8 | use Give\ThirdPartySupport\Elementor\Actions\RegisterWidgetEditorScripts; |
| 9 | use Give\ThirdPartySupport\Elementor\Actions\SetupElementorCampaignTemplate; |
| 10 | use Give\ThirdPartySupport\Elementor\Actions\UnregisterV1Widgets; |
| 11 | use Give\ThirdPartySupport\Elementor\Settings\RegisterSection; |
| 12 | use Give\ThirdPartySupport\Elementor\Settings\RegisterSettings; |
| 13 | |
| 14 | /** |
| 15 | * @since 4.7.0 |
| 16 | */ |
| 17 | class ServiceProvider implements ServiceProviderInterface |
| 18 | { |
| 19 | /** |
| 20 | * @since 4.7.0 |
| 21 | */ |
| 22 | public function register() {} |
| 23 | |
| 24 | /** |
| 25 | * @since 4.7.0 |
| 26 | */ |
| 27 | public function boot() |
| 28 | { |
| 29 | $this->registerElementorSettings(); |
| 30 | |
| 31 | if (!defined('ELEMENTOR_VERSION')) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | // If the old version of the GiveWP Elementor Widgets plugin is installed, unregister the legacy widgets to prevent conflicts with the new widgets that are registered in the RegisterWidgets class |
| 36 | $this->maybeUnregisterExistingLegacyWidgets(); |
| 37 | |
| 38 | // Register core widgets with priority 11 to override any widgets from previously migrated plugin givewp-elementor-widgets |
| 39 | Hooks::addFilter('elementor/widgets/register', RegisterWidgets::class, '__invoke', 11, 1); |
| 40 | |
| 41 | // Register widget scripts for the editor preview |
| 42 | Hooks::addAction('elementor/preview/enqueue_scripts', RegisterWidgetEditorScripts::class); |
| 43 | |
| 44 | // Register admin styles |
| 45 | add_action('elementor/editor/before_enqueue_scripts', function () { |
| 46 | wp_enqueue_style('give-elementor-admin-styles', GIVE_PLUGIN_URL . 'src/ThirdPartySupport/Elementor/Widgets/resources/styles/give-elementor-admin.css', array(), GIVE_VERSION); |
| 47 | }); |
| 48 | |
| 49 | // Register elementor categories |
| 50 | add_action('elementor/elements/categories_registered', function ($elements_manager) { |
| 51 | /** @var \Elementor\Elements_Manager $elements_manager */ |
| 52 | $elements_manager->add_category( |
| 53 | 'givewp-category-legacy', |
| 54 | [ |
| 55 | 'title' => __('GiveWP (Legacy)', 'give'), |
| 56 | 'icon' => 'dashicons dashicons-give', |
| 57 | ] |
| 58 | ); |
| 59 | |
| 60 | $elements_manager->add_category( |
| 61 | 'givewp-category', |
| 62 | [ |
| 63 | 'title' => __('GiveWP', 'give'), |
| 64 | 'icon' => 'dashicons dashicons-give', |
| 65 | ] |
| 66 | ); |
| 67 | }); |
| 68 | |
| 69 | Hooks::addAction('givewp_campaign_page_created', SetupElementorCampaignTemplate::class); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Register the GiveWP Elementor Widgets settings |
| 74 | * |
| 75 | * @since @since 4.7.0 |
| 76 | */ |
| 77 | public function registerElementorSettings() |
| 78 | { |
| 79 | Hooks::addFilter('give_get_sections_display', RegisterSection::class); |
| 80 | Hooks::addFilter('give_get_settings_display', RegisterSettings::class); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * If the old version of the GiveWP Elementor Widgets plugin is installed, unregister the legacy widgets to prevent conflicts with the new widgets that are registered in the RegisterWidgets class |
| 85 | * |
| 86 | * @since @since 4.7.0 |
| 87 | */ |
| 88 | private function maybeUnregisterExistingLegacyWidgets() |
| 89 | { |
| 90 | // This would determine if the old version of the GiveWP Elementor Widgets plugin is installed |
| 91 | if (!defined('GiveWP_DW_4_Elementor_VERSION')) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | // If the option is not set, set it to disabled |
| 96 | if (give_get_option('givewp_elementor_legacy_widgets_enabled') === false) { |
| 97 | // update the option initially to enable the legacy widgets |
| 98 | // users can disable the legacy widgets in the GiveWP Elementor Widgets settings from here on out |
| 99 | give_update_option('givewp_elementor_legacy_widgets_enabled', 'enabled'); |
| 100 | } |
| 101 | |
| 102 | // Unregister the legacy widgets from the GiveWP Elementor Widgets plugin to prevent conflicts with the new widgets that are registered in the RegisterWidgets class |
| 103 | Hooks::addFilter('elementor/widgets/register', UnregisterV1Widgets::class, '__invoke', 11, 1); |
| 104 | |
| 105 | add_action('admin_notices', function () { |
| 106 | // Define notice configuration |
| 107 | $notice_config = [ |
| 108 | 'id' => 'givewp-elementor-widgets-plugin-legacy-plugin-notice', |
| 109 | 'description' => __( |
| 110 | 'GiveWP now includes Elementor widgets! You can safely deactivate and remove the GiveWP Elementor Widgets plugin as it is no longer needed. Rest assured, all of your widgets currently being used will remain working properly.', |
| 111 | 'give' |
| 112 | ), |
| 113 | 'type' => 'info', |
| 114 | 'dismissible_type' => 'user', |
| 115 | 'dismiss_interval' => 'permanent', |
| 116 | ]; |
| 117 | |
| 118 | // Only register the notice if it hasn't been dismissed |
| 119 | if (!Give()->notices->is_notice_dismissed($notice_config)) { |
| 120 | Give()->notices->register_notice($notice_config); |
| 121 | } |
| 122 | }); |
| 123 | } |
| 124 | } |
| 125 |