app.php
273 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\Modules\WebCli\Module as WebCLIModule; |
| 7 | use Elementor\Core\Base\App as BaseApp; |
| 8 | use Elementor\Core\Settings\Manager as SettingsManager; |
| 9 | use Elementor\Plugin; |
| 10 | use Elementor\TemplateLibrary\Source_Local; |
| 11 | use Elementor\User; |
| 12 | use Elementor\Utils; |
| 13 | use Elementor\Core\Utils\Promotions\Filtered_Promotions_Manager; |
| 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 | 'is_administrator' => current_user_can( 'manage_options' ), |
| 79 | 'restrictions' => Plugin::$instance->role_manager->get_user_restrictions_array(), |
| 80 | ] ); |
| 81 | |
| 82 | $this->enqueue_assets(); |
| 83 | |
| 84 | remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 85 | |
| 86 | // Setup default heartbeat options |
| 87 | // TODO: Enable heartbeat. |
| 88 | add_filter( 'heartbeat_settings', function( $settings ) { |
| 89 | $settings['interval'] = 15; |
| 90 | return $settings; |
| 91 | } ); |
| 92 | |
| 93 | $this->render(); |
| 94 | die; |
| 95 | } |
| 96 | |
| 97 | protected function get_init_settings() { |
| 98 | $referer = wp_get_referer(); |
| 99 | |
| 100 | return [ |
| 101 | 'menu_url' => $this->get_base_url() . '#site-editor/promotion', |
| 102 | 'assets_url' => ELEMENTOR_ASSETS_URL, |
| 103 | 'pages_url' => admin_url( 'edit.php?post_type=page' ), |
| 104 | 'return_url' => $referer ? $referer : admin_url(), |
| 105 | 'hasPro' => Utils::has_pro(), |
| 106 | 'admin_url' => admin_url(), |
| 107 | 'login_url' => wp_login_url(), |
| 108 | 'base_url' => $this->get_base_url(), |
| 109 | 'promotion' => Filtered_Promotions_Manager::get_filtered_promotion_data( |
| 110 | [ 'upgrade_url' => 'https://go.elementor.com/go-pro-theme-builder/' ], |
| 111 | 'elementor/site-editor/promotion', |
| 112 | 'upgrade_url' |
| 113 | ), |
| 114 | ]; |
| 115 | } |
| 116 | |
| 117 | private function render() { |
| 118 | require __DIR__ . '/view.php'; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get Elementor editor theme color preference. |
| 123 | * |
| 124 | * Retrieve the user theme color preference as defined by editor preferences manager. |
| 125 | * |
| 126 | * @since 3.0.0 |
| 127 | * @access private |
| 128 | * |
| 129 | * @return string Preferred editor theme. |
| 130 | */ |
| 131 | private function get_elementor_ui_theme_preference() { |
| 132 | $editor_preferences = SettingsManager::get_settings_managers( 'editorPreferences' ); |
| 133 | |
| 134 | return $editor_preferences->get_model()->get_settings( 'ui_theme' ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Enqueue dark theme detection script. |
| 139 | * |
| 140 | * Enqueues an inline script that detects user-agent settings for dark mode and adds a complimentary class to the body tag. |
| 141 | * |
| 142 | * @since 3.0.0 |
| 143 | * @access private |
| 144 | */ |
| 145 | private function enqueue_dark_theme_detection_script() { |
| 146 | if ( 'auto' === $this->get_elementor_ui_theme_preference() ) { |
| 147 | wp_add_inline_script( 'elementor-app', |
| 148 | 'if ( window.matchMedia && window.matchMedia( `(prefers-color-scheme: dark)` ).matches ) |
| 149 | { document.body.classList.add( `eps-theme-dark` ); }' ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private function enqueue_assets() { |
| 154 | Plugin::$instance->init_common(); |
| 155 | |
| 156 | /** @var WebCLIModule $web_cli */ |
| 157 | $web_cli = Plugin::$instance->modules_manager->get_modules( 'web-cli' ); |
| 158 | $web_cli->register_scripts(); |
| 159 | |
| 160 | Plugin::$instance->common->register_scripts(); |
| 161 | |
| 162 | wp_register_style( |
| 163 | 'select2', |
| 164 | $this->get_css_assets_url( 'e-select2', 'assets/lib/e-select2/css/' ), |
| 165 | [], |
| 166 | '4.0.6-rc.1' |
| 167 | ); |
| 168 | |
| 169 | Plugin::$instance->common->register_styles(); |
| 170 | |
| 171 | wp_register_style( |
| 172 | 'select2', |
| 173 | ELEMENTOR_ASSETS_URL . 'lib/e-select2/css/e-select2.css', |
| 174 | [], |
| 175 | '4.0.6-rc.1' |
| 176 | ); |
| 177 | |
| 178 | wp_enqueue_style( |
| 179 | 'elementor-app', |
| 180 | $this->get_css_assets_url( 'app', null, 'default', true ), |
| 181 | [ |
| 182 | 'select2', |
| 183 | 'elementor-icons', |
| 184 | 'elementor-common', |
| 185 | 'select2', |
| 186 | ], |
| 187 | ELEMENTOR_VERSION |
| 188 | ); |
| 189 | |
| 190 | wp_enqueue_script( |
| 191 | 'elementor-app-packages', |
| 192 | $this->get_js_assets_url( 'app-packages' ), |
| 193 | [ |
| 194 | 'wp-i18n', |
| 195 | 'react', |
| 196 | ], |
| 197 | ELEMENTOR_VERSION, |
| 198 | true |
| 199 | ); |
| 200 | |
| 201 | wp_register_script( |
| 202 | 'select2', |
| 203 | $this->get_js_assets_url( 'e-select2.full', 'assets/lib/e-select2/js/' ), |
| 204 | [ |
| 205 | 'jquery', |
| 206 | ], |
| 207 | '4.0.6-rc.1', |
| 208 | true |
| 209 | ); |
| 210 | |
| 211 | wp_enqueue_script( |
| 212 | 'elementor-app', |
| 213 | $this->get_js_assets_url( 'app' ), |
| 214 | [ |
| 215 | 'wp-url', |
| 216 | 'wp-i18n', |
| 217 | 'react', |
| 218 | 'react-dom', |
| 219 | 'select2', |
| 220 | ], |
| 221 | ELEMENTOR_VERSION, |
| 222 | true |
| 223 | ); |
| 224 | |
| 225 | $this->enqueue_dark_theme_detection_script(); |
| 226 | |
| 227 | wp_set_script_translations( 'elementor-app-packages', 'elementor' ); |
| 228 | wp_set_script_translations( 'elementor-app', 'elementor' ); |
| 229 | |
| 230 | $this->print_config(); |
| 231 | } |
| 232 | |
| 233 | public function enqueue_app_loader() { |
| 234 | wp_enqueue_script( |
| 235 | 'elementor-app-loader', |
| 236 | $this->get_js_assets_url( 'app-loader' ), |
| 237 | [ |
| 238 | 'elementor-common', |
| 239 | ], |
| 240 | ELEMENTOR_VERSION, |
| 241 | true |
| 242 | ); |
| 243 | |
| 244 | $this->print_config( 'elementor-app-loader' ); |
| 245 | } |
| 246 | |
| 247 | public function __construct() { |
| 248 | $this->add_component( 'site-editor', new Modules\SiteEditor\Module() ); |
| 249 | |
| 250 | if ( current_user_can( 'manage_options' ) || Utils::is_wp_cli() ) { |
| 251 | $this->add_component( 'import-export', new Modules\ImportExport\Module() ); |
| 252 | |
| 253 | // Kit library is depended on import-export |
| 254 | $this->add_component( 'kit-library', new Modules\KitLibrary\Module() ); |
| 255 | } |
| 256 | |
| 257 | $this->add_component( 'onboarding', new Modules\Onboarding\Module() ); |
| 258 | |
| 259 | add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) { |
| 260 | $this->register_admin_menu( $admin_menu ); |
| 261 | }, Source_Local::ADMIN_MENU_PRIORITY + 10 ); |
| 262 | |
| 263 | // Happens after WP plugin page validation. |
| 264 | add_filter( 'add_menu_classes', [ $this, 'fix_submenu' ] ); |
| 265 | |
| 266 | if ( $this->is_current() ) { |
| 267 | add_action( 'admin_init', [ $this, 'admin_init' ], 0 ); |
| 268 | } else { |
| 269 | add_action( 'elementor/common/after_register_scripts', [ $this, 'enqueue_app_loader' ] ); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 |