| 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 |
|
| 43 |
add_filter( 'elementor/editor/v2/packages', fn( $packages ) => $this->add_packages( $packages ) ); |
| 44 |
add_action( 'elementor/elements/categories_registered', [ $this, 'maybe_register_custom_widgets_category_fallback' ], 100 ); |
| 45 |
add_action( 'rest_api_init', fn() => $this->register_consent_route() ); |
| 46 |
} |
| 47 |
|
| 48 |
private function register_consent_route(): void { |
| 49 |
register_rest_route( 'elementor/v1', '/angie/consent', [ |
| 50 |
'methods' => 'POST', |
| 51 |
'callback' => fn() => $this->handle_save_consent(), |
| 52 |
'permission_callback' => fn() => current_user_can( 'manage_options' ), |
| 53 |
] ); |
| 54 |
} |
| 55 |
|
| 56 |
private function handle_save_consent(): \WP_REST_Response { |
| 57 |
update_option( self::ANGIE_CONSENT_OPTION, 'yes' ); |
| 58 |
|
| 59 |
return new \WP_REST_Response( [ 'success' => true ], 200 ); |
| 60 |
} |
| 61 |
|
| 62 |
public function maybe_register_custom_widgets_category_fallback( Elements_Manager $elements_manager ): void { |
| 63 |
if ( ! Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
if ( Hints::is_plugin_active( 'angie' ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$categories = $elements_manager->get_categories(); |
| 72 |
|
| 73 |
if ( isset( $categories[ Elements_Manager::CATEGORY_CUSTOM_WIDGETS ] ) ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
$elements_manager->add_category( |
| 78 |
Elements_Manager::CATEGORY_CUSTOM_WIDGETS, |
| 79 |
[ |
| 80 |
'title' => esc_html__( 'Custom Widget', 'elementor' ), |
| 81 |
'icon' => 'eicon-ai', |
| 82 |
'hideIfEmpty' => false, |
| 83 |
'active' => true, |
| 84 |
] |
| 85 |
); |
| 86 |
|
| 87 |
add_action( 'elementor/editor/templates/panel/category', [ $this, 'render_custom_widgets_category_heading_cta' ] ); |
| 88 |
add_action( 'elementor/editor/templates/panel/category/content', [ $this, 'render_custom_widgets_category_empty_state' ] ); |
| 89 |
} |
| 90 |
|
| 91 |
public function render_custom_widgets_category_heading_cta(): void { |
| 92 |
if ( ! Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
?> |
| 97 |
<# if ( 'custom-widgets' === name ) { #> |
| 98 |
<button type="button" class="elementor-panel-custom-widgets__cta elementor-panel-custom-widgets__cta--heading"><?php echo esc_html__( 'Try for free', 'elementor' ); ?></button> |
| 99 |
<# } #> |
| 100 |
<?php |
| 101 |
} |
| 102 |
|
| 103 |
public function render_custom_widgets_category_empty_state(): void { |
| 104 |
if ( ! Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 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 |
|