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