| @@ -3,8 +3,11 @@ | ||
| 3 | 3 | namespace Elementor\Modules\WidgetCreation; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Core\Base\Module as BaseModule; |
| 6 | 6 | use Elementor\Core\Experiments\Manager as ExperimentsManager; |
| 7 | +use Elementor\Core\Utils\Hints; | |
| 8 | +use Elementor\Elements_Manager; | |
| 9 | +use Elementor\Plugin; | |
| 7 | 10 | |
| 8 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | 12 | exit; |
| 10 | 13 | } |
| @@ -16,8 +19,10 @@ | ||
| 16 | 19 | const PACKAGES = [ |
| 17 | 20 | 'editor-widget-creation', |
| 18 | 21 | ]; |
| 19 | 22 | |
| 23 | + const ANGIE_CONSENT_OPTION = 'angie_external_scripts_consent'; | |
| 24 | + | |
| 20 | 25 | public function get_name() { |
| 21 | 26 | return self::MODULE_NAME; |
| 22 | 27 | } |
| 23 | 28 | |
| @@ -33,10 +38,100 @@ | ||
| 33 | 38 | } |
| 34 | 39 | |
| 35 | 40 | public function __construct() { |
| 36 | 41 | parent::__construct(); |
| 42 | + AngiePromotion::init(); | |
| 37 | 43 | |
| 38 | 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; | |
| 39 | 134 | } |
| 40 | 135 | |
| 41 | 136 | private function add_packages( array $packages ): array { |
| 42 | 137 | return array_merge( $packages, self::PACKAGES ); |