PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0-dev3
Elementor Website Builder – more than just a page builder v4.1.0-dev3
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.1.0-dev3, at modules/widget-creation/module.php

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