PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta1
Elementor Website Builder – more than just a page builder v4.3.0-beta1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / widget-creation / module.php

module.php in Elementor Website Builder – more than just a page builder 4.3.0-beta1, at modules/widget-creation/module.php

135 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\WidgetCreation;
4
5 use Elementor\Core\Base\Module as BaseModule;
6 use Elementor\Core\Experiments\Manager as Experiments_Manager;
7 use Elementor\Core\Utils\Hints;
8 use Elementor\Elements_Manager;
9 use Elementor\Modules\AtomicWidgets\Module as Atomic_Widgets_Module;
10 use Elementor\Plugin;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 class Module extends BaseModule {
17 const EXPERIMENT_NAME = Atomic_Widgets_Module::EXPERIMENT_NAME;
18 const ANGIE_IN_PANELS_EXPERIMENT_NAME = 'e_angie_in_panels';
19 const MODULE_NAME = 'widget-creation';
20
21 const PACKAGES = [
22 'editor-widget-creation',
23 ];
24
25 const ANGIE_CONSENT_OPTION = 'angie_external_scripts_consent';
26
27 public function get_name() {
28 return self::MODULE_NAME;
29 }
30
31 public static function get_angie_in_panels_experimental_data(): array {
32 return [
33 'name' => self::ANGIE_IN_PANELS_EXPERIMENT_NAME,
34 'title' => esc_html__( 'Angie in editor panels', 'elementor' ),
35 'description' => esc_html__( 'Load Angie inside the native Elementor editor panel instead of the legacy push sidebar.', 'elementor' ),
36 'hidden' => true,
37 'default' => Experiments_Manager::STATE_INACTIVE,
38 'release_status' => Experiments_Manager::RELEASE_STATUS_DEV,
39 ];
40 }
41
42 public function __construct() {
43 parent::__construct();
44 $this->register_angie_in_panels_experiment();
45 AngiePromotion::init();
46
47 add_filter( 'elementor/editor/v2/packages', fn( $packages ) => $this->add_packages( $packages ) );
48 add_action( 'elementor/elements/categories_registered', [ $this, 'maybe_register_custom_widgets_category_fallback' ], 100 );
49 add_action( 'rest_api_init', fn() => $this->register_consent_route() );
50 }
51
52 private function register_angie_in_panels_experiment(): void {
53 Plugin::$instance->experiments->add_feature( self::get_angie_in_panels_experimental_data() );
54 }
55
56 private function register_consent_route(): void {
57 register_rest_route( 'elementor/v1', '/angie/consent', [
58 'methods' => 'POST',
59 'callback' => fn() => $this->handle_save_consent(),
60 'permission_callback' => fn() => current_user_can( 'manage_options' ),
61 ] );
62 }
63
64 private function handle_save_consent(): \WP_REST_Response {
65 update_option( self::ANGIE_CONSENT_OPTION, 'yes' );
66
67 return new \WP_REST_Response( [ 'success' => true ], 200 );
68 }
69
70 public function maybe_register_custom_widgets_category_fallback( Elements_Manager $elements_manager ): void {
71 if ( Hints::is_plugin_active( 'angie' ) ) {
72 return;
73 }
74
75 $categories = $elements_manager->get_categories();
76
77 if ( isset( $categories[ Elements_Manager::CATEGORY_CUSTOM_WIDGETS ] ) ) {
78 return;
79 }
80
81 $elements_manager->add_category(
82 Elements_Manager::CATEGORY_CUSTOM_WIDGETS,
83 [
84 'title' => esc_html__( 'Custom Widget', 'elementor' ),
85 'icon' => 'eicon-ai',
86 'hideIfEmpty' => false,
87 'active' => true,
88 ]
89 );
90
91 add_action( 'elementor/editor/templates/panel/category', [ $this, 'render_custom_widgets_category_heading_cta' ] );
92 add_action( 'elementor/editor/templates/panel/category/content', [ $this, 'render_custom_widgets_category_empty_state' ] );
93 }
94
95 public function render_custom_widgets_category_heading_cta(): void {
96 if ( ! current_user_can( 'manage_options' ) ) {
97 return;
98 }
99
100 ?>
101 <# if ( 'custom-widgets' === name ) { #>
102 <button type="button" class="elementor-panel-custom-widgets__cta elementor-panel-custom-widgets__cta--heading"><?php echo esc_html__( 'Try for free', 'elementor' ); ?></button>
103 <# } #>
104 <?php
105 }
106
107 public function render_custom_widgets_category_empty_state(): void {
108 if ( $this->custom_widgets_category_has_widgets() ) {
109 return;
110 }
111
112 ?>
113 <# if ( 'custom-widgets' === name ) { #>
114 <div class="elementor-panel-category-custom-widgets-empty">
115 <p class="elementor-panel-category-custom-widgets-empty__message"><?php echo esc_html__( 'Create custom widgets by describing what you need.', 'elementor' ); ?></p>
116 </div>
117 <# } #>
118 <?php
119 }
120
121 private function custom_widgets_category_has_widgets(): bool {
122 foreach ( Plugin::$instance->widgets_manager->get_widget_types() as $widget ) {
123 if ( in_array( Elements_Manager::CATEGORY_CUSTOM_WIDGETS, $widget->get_categories(), true ) ) {
124 return true;
125 }
126 }
127
128 return false;
129 }
130
131 private function add_packages( array $packages ): array {
132 return array_merge( $packages, self::PACKAGES );
133 }
134 }
135