| 1 |
<?php |
| 2 |
namespace Elementor\App\Modules\SiteBuilder; |
| 3 |
|
| 4 |
use Elementor\App\Modules\SiteBuilder\Connect\App; |
| 5 |
use Elementor\App\Modules\SiteBuilder\Rest\Rest_Api; |
| 6 |
use Elementor\Core\Base\Module as BaseModule; |
| 7 |
use Elementor\Plugin; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Module extends BaseModule { |
| 14 |
|
| 15 |
public function get_name() { |
| 16 |
return 'site-builder'; |
| 17 |
} |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
parent::__construct(); |
| 21 |
|
| 22 |
$this->register_experiment(); |
| 23 |
|
| 24 |
if ( ! $this->is_experiment_active() ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
add_action( 'elementor/init', [ $this, 'on_elementor_init' ], 12 ); |
| 29 |
|
| 30 |
add_action( 'elementor/connect/apps/register', function ( $connect_module ) { |
| 31 |
$connect_module->register_app( 'site-builder', App::get_class_name() ); |
| 32 |
} ); |
| 33 |
|
| 34 |
add_action( 'rest_api_init', function () { |
| 35 |
( new Rest_Api() )->register_routes(); |
| 36 |
} ); |
| 37 |
} |
| 38 |
|
| 39 |
private function register_experiment() { |
| 40 |
Plugin::instance()->experiments->add_feature([ |
| 41 |
'name' => 'site-builder', |
| 42 |
'title' => esc_html__( 'Site Builder', 'elementor' ), |
| 43 |
'description' => esc_html__( 'Enable Site Builder.', 'elementor' ), |
| 44 |
'release_status' => Plugin::$instance->experiments::RELEASE_STATUS_DEV, |
| 45 |
'hidden' => true, |
| 46 |
]); |
| 47 |
} |
| 48 |
|
| 49 |
private function is_experiment_active(): bool { |
| 50 |
return Plugin::$instance->experiments->is_feature_active( 'site-builder' ); |
| 51 |
} |
| 52 |
|
| 53 |
public function on_elementor_init() { |
| 54 |
if ( ! Plugin::instance()->app->is_current() ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$settings = [ |
| 59 |
'iframeUrl' => $this->get_iframe_url(), |
| 60 |
'isAdmin' => current_user_can( 'manage_options' ), |
| 61 |
'exitTo' => admin_url( 'admin.php?page=elementor' ), |
| 62 |
'elementorAiCurrentContext' => $this->get_elementor_ai_current_context(), |
| 63 |
]; |
| 64 |
|
| 65 |
$connect_auth = $this->get_connect_auth(); |
| 66 |
|
| 67 |
if ( $connect_auth ) { |
| 68 |
$settings['connectAuth'] = $connect_auth; |
| 69 |
} |
| 70 |
|
| 71 |
Plugin::$instance->app->set_settings( 'site-builder', $settings ); |
| 72 |
} |
| 73 |
|
| 74 |
private function get_elementor_ai_current_context(): array { |
| 75 |
$choices = get_option( 'elementor_onboarding_choices', [] ); |
| 76 |
$site_about = $choices['site_about'] ?? []; |
| 77 |
return [ |
| 78 |
'siteTitle' => (string) get_bloginfo( 'name' ), |
| 79 |
'siteAbout' => $site_about, |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
private function get_iframe_url(): string { |
| 84 |
if ( defined( 'ELEMENTOR_SITE_BUILDER_IFRAME_URL' ) ) { |
| 85 |
return ELEMENTOR_SITE_BUILDER_IFRAME_URL; |
| 86 |
} |
| 87 |
|
| 88 |
return 'https://planner.elementor.com/chat.html'; |
| 89 |
} |
| 90 |
|
| 91 |
public function get_config(): ?array { |
| 92 |
if ( ! $this->is_experiment_active() ) { |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
$connect_auth = $this->get_connect_auth(); |
| 97 |
|
| 98 |
if ( ! $connect_auth ) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
return [ |
| 103 |
'siteKey' => $connect_auth['siteKey'], |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
private function get_connect_auth(): ?array { |
| 108 |
if ( ! Plugin::instance()->common ) { |
| 109 |
return null; |
| 110 |
} |
| 111 |
|
| 112 |
$connect = Plugin::instance()->common->get_component( 'connect' ); |
| 113 |
|
| 114 |
if ( ! $connect ) { |
| 115 |
return null; |
| 116 |
} |
| 117 |
|
| 118 |
$app = $connect->get_app( 'library' ); |
| 119 |
|
| 120 |
if ( ! $app || ! $app->is_connected() ) { |
| 121 |
return null; |
| 122 |
} |
| 123 |
|
| 124 |
$access_token = $app->get( 'access_token' ); |
| 125 |
$client_id = $app->get( 'client_id' ); |
| 126 |
$home_url = trailingslashit( home_url() ); |
| 127 |
$site_key = $app->get_site_key(); |
| 128 |
$access_token_secret = $app->get( 'access_token_secret' ); |
| 129 |
|
| 130 |
$connect_data = [ |
| 131 |
'access-token' => $access_token, |
| 132 |
'app' => 'library', |
| 133 |
'client-id' => $client_id, |
| 134 |
'home-url' => $home_url, |
| 135 |
'site-key' => $site_key, |
| 136 |
]; |
| 137 |
|
| 138 |
ksort( $connect_data ); |
| 139 |
|
| 140 |
$signature = hash_hmac( |
| 141 |
'sha256', |
| 142 |
wp_json_encode( $connect_data, JSON_NUMERIC_CHECK ), |
| 143 |
$access_token_secret |
| 144 |
); |
| 145 |
|
| 146 |
return [ |
| 147 |
'signature' => $signature, |
| 148 |
'accessToken' => $access_token, |
| 149 |
'clientId' => $client_id, |
| 150 |
'homeUrl' => $home_url, |
| 151 |
'siteKey' => $site_key, |
| 152 |
]; |
| 153 |
} |
| 154 |
} |
| 155 |
|