| 1 |
<?php |
| 2 |
namespace Elementor\Core\App; |
| 3 |
|
| 4 |
use Elementor\Core\Base\App as BaseApp; |
| 5 |
use Elementor\Core\Settings\Manager as SettingsManager; |
| 6 |
use Elementor\Plugin; |
| 7 |
use Elementor\TemplateLibrary\Source_Local; |
| 8 |
use Elementor\Utils; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
class App extends BaseApp { |
| 15 |
|
| 16 |
const PAGE_ID = 'elementor-app'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Get module name. |
| 20 |
* |
| 21 |
* Retrieve the module name. |
| 22 |
* |
| 23 |
* @since 3.0.0 |
| 24 |
* @access public |
| 25 |
* |
| 26 |
* @return string Module name. |
| 27 |
*/ |
| 28 |
public function get_name() { |
| 29 |
return 'app'; |
| 30 |
} |
| 31 |
|
| 32 |
public function get_base_url() { |
| 33 |
return admin_url( 'admin.php?page=' . self::PAGE_ID . '&ver=' . ELEMENTOR_VERSION ); |
| 34 |
} |
| 35 |
|
| 36 |
public function register_admin_menu() { |
| 37 |
add_submenu_page( |
| 38 |
Source_Local::ADMIN_MENU_SLUG, |
| 39 |
esc_html__( 'Theme Builder', 'elementor' ), |
| 40 |
esc_html__( 'Theme Builder', 'elementor' ), |
| 41 |
'manage_options', |
| 42 |
self::PAGE_ID |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
public function fix_submenu( $menu ) { |
| 47 |
global $submenu; |
| 48 |
|
| 49 |
if ( is_multisite() && is_network_admin() ) { |
| 50 |
return $menu; |
| 51 |
} |
| 52 |
|
| 53 |
// Non admin role / custom wp menu. |
| 54 |
if ( empty( $submenu[ Source_Local::ADMIN_MENU_SLUG ] ) ) { |
| 55 |
return $menu; |
| 56 |
} |
| 57 |
|
| 58 |
// Hack to add a link to sub menu. |
| 59 |
foreach ( $submenu[ Source_Local::ADMIN_MENU_SLUG ] as &$item ) { |
| 60 |
if ( self::PAGE_ID === $item[2] ) { |
| 61 |
$item[2] = $this->get_settings( 'menu_url' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 62 |
$item[4] = 'elementor-app-link'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return $menu; |
| 67 |
} |
| 68 |
|
| 69 |
public function is_current() { |
| 70 |
return ( ! empty( $_GET['page'] ) && self::PAGE_ID === $_GET['page'] ); |
| 71 |
} |
| 72 |
|
| 73 |
public function admin_init() { |
| 74 |
do_action( 'elementor/app/init', $this ); |
| 75 |
|
| 76 |
$this->enqueue_assets(); |
| 77 |
|
| 78 |
// Setup default heartbeat options |
| 79 |
// TODO: Enable heartbeat. |
| 80 |
add_filter( 'heartbeat_settings', function( $settings ) { |
| 81 |
$settings['interval'] = 15; |
| 82 |
return $settings; |
| 83 |
} ); |
| 84 |
|
| 85 |
$this->render(); |
| 86 |
die; |
| 87 |
} |
| 88 |
|
| 89 |
protected function get_init_settings() { |
| 90 |
return [ |
| 91 |
'menu_url' => $this->get_base_url() . '#site-editor/promotion', |
| 92 |
'assets_url' => ELEMENTOR_ASSETS_URL, |
| 93 |
'return_url' => isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : admin_url(), |
| 94 |
'hasPro' => Utils::has_pro(), |
| 95 |
'admin_url' => admin_url(), |
| 96 |
'login_url' => wp_login_url(), |
| 97 |
]; |
| 98 |
} |
| 99 |
|
| 100 |
private function render() { |
| 101 |
require __DIR__ . '/view.php'; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get Elementor UI theme preference. |
| 106 |
* |
| 107 |
* Retrieve the user UI theme preference as defined by editor preferences manager. |
| 108 |
* |
| 109 |
* @since 3.0.0 |
| 110 |
* @access private |
| 111 |
* |
| 112 |
* @return string Preferred UI theme. |
| 113 |
*/ |
| 114 |
private function get_elementor_ui_theme_preference() { |
| 115 |
$editor_preferences = SettingsManager::get_settings_managers( 'editorPreferences' ); |
| 116 |
|
| 117 |
return $editor_preferences->get_model()->get_settings( 'ui_theme' ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Enqueue dark theme detection script. |
| 122 |
* |
| 123 |
* Enqueues an inline script that detects user-agent settings for dark mode and adds a complimentary class to the body tag. |
| 124 |
* |
| 125 |
* @since 3.0.0 |
| 126 |
* @access private |
| 127 |
*/ |
| 128 |
private function enqueue_dark_theme_detection_script() { |
| 129 |
if ( 'auto' === $this->get_elementor_ui_theme_preference() ) { |
| 130 |
wp_add_inline_script( 'elementor-app', |
| 131 |
'if ( window.matchMedia && window.matchMedia( `(prefers-color-scheme: dark)` ).matches ) |
| 132 |
{ document.body.classList.add( `eps-theme-dark` ); }' ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
private function enqueue_assets() { |
| 137 |
Plugin::$instance->init_common(); |
| 138 |
Plugin::$instance->common->register_scripts(); |
| 139 |
|
| 140 |
wp_register_style( |
| 141 |
'select2', |
| 142 |
$this->get_css_assets_url( 'e-select2', 'assets/lib/e-select2/css/' ), |
| 143 |
[], |
| 144 |
'4.0.6-rc.1' |
| 145 |
); |
| 146 |
|
| 147 |
wp_register_style( |
| 148 |
'elementor-icons', |
| 149 |
$this->get_css_assets_url( 'elementor-icons', 'assets/lib/eicons/css/' ), |
| 150 |
[], |
| 151 |
'5.12.0' |
| 152 |
); |
| 153 |
|
| 154 |
wp_register_style( |
| 155 |
'elementor-common', |
| 156 |
$this->get_css_assets_url( 'common', null, 'default', true ), |
| 157 |
[], |
| 158 |
ELEMENTOR_VERSION |
| 159 |
); |
| 160 |
|
| 161 |
wp_register_style( |
| 162 |
'select2', |
| 163 |
ELEMENTOR_ASSETS_URL . 'lib/e-select2/css/e-select2.css', |
| 164 |
[], |
| 165 |
'4.0.6-rc.1' |
| 166 |
); |
| 167 |
|
| 168 |
wp_enqueue_style( |
| 169 |
'elementor-app', |
| 170 |
$this->get_css_assets_url( 'app', null, 'default', true ), |
| 171 |
[ |
| 172 |
'select2', |
| 173 |
'elementor-icons', |
| 174 |
'elementor-common', |
| 175 |
'select2', |
| 176 |
], |
| 177 |
ELEMENTOR_VERSION |
| 178 |
); |
| 179 |
|
| 180 |
wp_enqueue_script( |
| 181 |
'elementor-app-packages', |
| 182 |
$this->get_js_assets_url( 'app-packages' ), |
| 183 |
[ |
| 184 |
'wp-i18n', |
| 185 |
'react', |
| 186 |
], |
| 187 |
ELEMENTOR_VERSION, |
| 188 |
true |
| 189 |
); |
| 190 |
|
| 191 |
wp_register_script( |
| 192 |
'select2', |
| 193 |
$this->get_js_assets_url( 'e-select2.full', 'assets/lib/e-select2/js/' ), |
| 194 |
[ |
| 195 |
'jquery', |
| 196 |
], |
| 197 |
'4.0.6-rc.1', |
| 198 |
true |
| 199 |
); |
| 200 |
|
| 201 |
wp_enqueue_script( |
| 202 |
'elementor-app', |
| 203 |
$this->get_js_assets_url( 'app' ), |
| 204 |
[ |
| 205 |
'wp-url', |
| 206 |
'wp-i18n', |
| 207 |
'react', |
| 208 |
'react-dom', |
| 209 |
'select2', |
| 210 |
], |
| 211 |
ELEMENTOR_VERSION, |
| 212 |
true |
| 213 |
); |
| 214 |
|
| 215 |
$this->enqueue_dark_theme_detection_script(); |
| 216 |
|
| 217 |
wp_set_script_translations( 'elementor-app-packages', 'elementor' ); |
| 218 |
wp_set_script_translations( 'elementor-app', 'elementor' ); |
| 219 |
|
| 220 |
$this->print_config(); |
| 221 |
} |
| 222 |
|
| 223 |
public function enqueue_app_loader() { |
| 224 |
wp_enqueue_script( |
| 225 |
'elementor-app-loader', |
| 226 |
$this->get_js_assets_url( 'app-loader' ), |
| 227 |
[ |
| 228 |
'elementor-common', |
| 229 |
], |
| 230 |
ELEMENTOR_VERSION, |
| 231 |
true |
| 232 |
); |
| 233 |
|
| 234 |
$this->print_config( 'elementor-app-loader' ); |
| 235 |
} |
| 236 |
|
| 237 |
public function __construct() { |
| 238 |
$this->add_component( 'site-editor', new Modules\SiteEditor\Module() ); |
| 239 |
|
| 240 |
if ( current_user_can( 'manage_options' ) && Plugin::$instance->experiments->is_feature_active( 'e_import_export' ) || Utils::is_wp_cli() ) { |
| 241 |
$this->add_component( 'import-export', new Modules\ImportExport\Module() ); |
| 242 |
|
| 243 |
// Kit library is depended on import-export |
| 244 |
$this->add_component( 'kit-library', new Modules\KitLibrary\Module() ); |
| 245 |
} |
| 246 |
|
| 247 |
add_action( 'admin_menu', [ $this, 'register_admin_menu' ], 21 /* after Elementor page */ ); |
| 248 |
|
| 249 |
// Happens after WP plugin page validation. |
| 250 |
add_filter( 'add_menu_classes', [ $this, 'fix_submenu' ] ); |
| 251 |
|
| 252 |
if ( $this->is_current() ) { |
| 253 |
add_action( 'admin_init', [ $this, 'admin_init' ], 0 ); |
| 254 |
} else { |
| 255 |
add_action( 'elementor/common/after_register_scripts', [ $this, 'enqueue_app_loader' ] ); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|