| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\OptIn; |
| 4 |
|
| 5 |
use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 6 |
use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 7 |
use Elementor\Modules\GlobalClasses\Module as GlobalClassesModule; |
| 8 |
use Elementor\Modules\NestedElements\Module as NestedElementsModule; |
| 9 |
use Elementor\Modules\AtomicWidgets\Module as AtomicWidgetsModule; |
| 10 |
use Elementor\Modules\Variables\Module as VariablesModule; |
| 11 |
use Elementor\Modules\Components\Module as ComponentsModule; |
| 12 |
use Elementor\Plugin; |
| 13 |
|
| 14 |
class Opt_In { |
| 15 |
const EXPERIMENT_NAME = 'e_opt_in_v4'; |
| 16 |
|
| 17 |
const OPT_OUT_FEATURES = [ |
| 18 |
self::EXPERIMENT_NAME, |
| 19 |
AtomicWidgetsModule::EXPERIMENT_NAME, |
| 20 |
GlobalClassesModule::NAME, |
| 21 |
VariablesModule::EXPERIMENT_NAME, |
| 22 |
ComponentsModule::EXPERIMENT_NAME, |
| 23 |
]; |
| 24 |
|
| 25 |
const OPT_IN_FEATURES = [ |
| 26 |
self::EXPERIMENT_NAME, |
| 27 |
'container', |
| 28 |
NestedElementsModule::EXPERIMENT_NAME, |
| 29 |
AtomicWidgetsModule::EXPERIMENT_NAME, |
| 30 |
GlobalClassesModule::NAME, |
| 31 |
VariablesModule::EXPERIMENT_NAME, |
| 32 |
ComponentsModule::EXPERIMENT_NAME, |
| 33 |
]; |
| 34 |
|
| 35 |
public function init() { |
| 36 |
$this->register_feature(); |
| 37 |
|
| 38 |
add_action( 'elementor/ajax/register_actions', fn( Ajax $ajax ) => $this->add_ajax_actions( $ajax ) ); |
| 39 |
add_action( 'rest_api_init', fn() => $this->register_routes() ); |
| 40 |
} |
| 41 |
|
| 42 |
private function register_feature() { |
| 43 |
Plugin::$instance->experiments->add_feature([ |
| 44 |
'name' => self::EXPERIMENT_NAME, |
| 45 |
'title' => esc_html__( 'Editor V4', 'elementor' ), |
| 46 |
'description' => esc_html__( 'Enable Editor V4.', 'elementor' ), |
| 47 |
'hidden' => true, |
| 48 |
'default' => Experiments_Manager::STATE_INACTIVE, |
| 49 |
'release_status' => Experiments_Manager::RELEASE_STATUS_ALPHA, |
| 50 |
'new_site' => [ |
| 51 |
'default_active' => true, |
| 52 |
'minimum_installation_version' => '4.0.0', |
| 53 |
], |
| 54 |
]); |
| 55 |
} |
| 56 |
|
| 57 |
private function opt_out_v4() { |
| 58 |
foreach ( self::OPT_OUT_FEATURES as $feature ) { |
| 59 |
$feature_key = Plugin::$instance->experiments->get_feature_option_key( $feature ); |
| 60 |
update_option( $feature_key, Experiments_Manager::STATE_INACTIVE ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private function opt_in_v4() { |
| 65 |
foreach ( self::OPT_IN_FEATURES as $feature ) { |
| 66 |
$feature_key = Plugin::$instance->experiments->get_feature_option_key( $feature ); |
| 67 |
update_option( $feature_key, Experiments_Manager::STATE_ACTIVE ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public function ajax_opt_out_v4() { |
| 72 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 73 |
throw new \Exception( 'Permission denied' ); |
| 74 |
} |
| 75 |
|
| 76 |
$this->opt_out_v4(); |
| 77 |
} |
| 78 |
|
| 79 |
public function ajax_opt_in_v4() { |
| 80 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 81 |
throw new \Exception( 'Permission denied' ); |
| 82 |
} |
| 83 |
|
| 84 |
$this->opt_in_v4(); |
| 85 |
} |
| 86 |
|
| 87 |
private function add_ajax_actions( Ajax $ajax ) { |
| 88 |
$ajax->register_ajax_action( 'editor_v4_opt_in', fn() => $this->ajax_opt_in_v4() ); |
| 89 |
$ajax->register_ajax_action( 'editor_v4_opt_out', fn() => $this->ajax_opt_out_v4() ); |
| 90 |
} |
| 91 |
|
| 92 |
private function handle_rest_opt_in_v4() { |
| 93 |
$this->ajax_opt_in_v4(); |
| 94 |
return new \WP_REST_Response( [ |
| 95 |
'success' => true, |
| 96 |
], 200 ); |
| 97 |
} |
| 98 |
|
| 99 |
private function register_routes() { |
| 100 |
register_rest_route( 'elementor/v1', '/operations/opt-in-v4', [ |
| 101 |
'methods' => 'POST', |
| 102 |
'callback' => fn() => $this->handle_rest_opt_in_v4(), |
| 103 |
'permission_callback' => fn() => current_user_can( 'manage_options' ), |
| 104 |
] ); |
| 105 |
} |
| 106 |
} |
| 107 |
|