| @@ -1,20 +1,256 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\Core\App; |
| 3 | 3 | |
| 4 | 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; | |
| 5 | 9 | |
| 6 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | 11 | exit; // Exit if accessed directly. |
| 8 | 12 | } |
| 9 | 13 | |
| 10 | -/** | |
| 11 | - * This App class was introduced for backwards compatibility with 3rd parties. | |
| 12 | - */ | |
| 13 | 14 | class App extends BaseApp { |
| 14 | 15 | |
| 15 | 16 | const PAGE_ID = 'elementor-app'; |
| 16 | 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 | + */ | |
| 17 | 28 | public function get_name() { |
| 18 | - return 'app-bc'; | |
| 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 | + ]; | |
| 96 | + } | |
| 97 | + | |
| 98 | + private function render() { | |
| 99 | + require __DIR__ . '/view.php'; | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * Get Elementor UI theme preference. | |
| 104 | + * | |
| 105 | + * Retrieve the user UI theme preference as defined by editor preferences manager. | |
| 106 | + * | |
| 107 | + * @since 3.0.0 | |
| 108 | + * @access private | |
| 109 | + * | |
| 110 | + * @return string Preferred UI theme. | |
| 111 | + */ | |
| 112 | + private function get_elementor_ui_theme_preference() { | |
| 113 | + $editor_preferences = SettingsManager::get_settings_managers( 'editorPreferences' ); | |
| 114 | + | |
| 115 | + return $editor_preferences->get_model()->get_settings( 'ui_theme' ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Enqueue dark theme detection script. | |
| 120 | + * | |
| 121 | + * Enqueues an inline script that detects user-agent settings for dark mode and adds a complimentary class to the body tag. | |
| 122 | + * | |
| 123 | + * @since 3.0.0 | |
| 124 | + * @access private | |
| 125 | + */ | |
| 126 | + private function enqueue_dark_theme_detection_script() { | |
| 127 | + if ( 'auto' === $this->get_elementor_ui_theme_preference() ) { | |
| 128 | + wp_add_inline_script( 'elementor-app', | |
| 129 | + 'if ( window.matchMedia && window.matchMedia( `(prefers-color-scheme: dark)` ).matches ) | |
| 130 | + { document.body.classList.add( `eps-theme-dark` ); }' ); | |
| 131 | + } | |
| 132 | + } | |
| 133 | + | |
| 134 | + private function enqueue_assets() { | |
| 135 | + Plugin::$instance->init_common(); | |
| 136 | + Plugin::$instance->common->register_scripts(); | |
| 137 | + | |
| 138 | + wp_register_style( | |
| 139 | + 'select2', | |
| 140 | + $this->get_css_assets_url( 'e-select2', 'assets/lib/e-select2/css/' ), | |
| 141 | + [], | |
| 142 | + '4.0.6-rc.1' | |
| 143 | + ); | |
| 144 | + | |
| 145 | + wp_register_style( | |
| 146 | + 'elementor-icons', | |
| 147 | + $this->get_css_assets_url( 'elementor-icons', 'assets/lib/eicons/css/' ), | |
| 148 | + [], | |
| 149 | + '5.12.0' | |
| 150 | + ); | |
| 151 | + | |
| 152 | + wp_register_style( | |
| 153 | + 'elementor-common', | |
| 154 | + $this->get_css_assets_url( 'common', null, 'default', true ), | |
| 155 | + [], | |
| 156 | + ELEMENTOR_VERSION | |
| 157 | + ); | |
| 158 | + | |
| 159 | + wp_register_style( | |
| 160 | + 'select2', | |
| 161 | + ELEMENTOR_ASSETS_URL . 'lib/e-select2/css/e-select2.css', | |
| 162 | + [], | |
| 163 | + '4.0.6-rc.1' | |
| 164 | + ); | |
| 165 | + | |
| 166 | + wp_enqueue_style( | |
| 167 | + 'elementor-app', | |
| 168 | + $this->get_css_assets_url( 'app', null, 'default', true ), | |
| 169 | + [ | |
| 170 | + 'select2', | |
| 171 | + 'elementor-icons', | |
| 172 | + 'elementor-common', | |
| 173 | + 'select2', | |
| 174 | + ], | |
| 175 | + ELEMENTOR_VERSION | |
| 176 | + ); | |
| 177 | + | |
| 178 | + wp_enqueue_script( | |
| 179 | + 'elementor-app-packages', | |
| 180 | + $this->get_js_assets_url( 'app-packages' ), | |
| 181 | + [ | |
| 182 | + 'wp-i18n', | |
| 183 | + 'react', | |
| 184 | + ], | |
| 185 | + ELEMENTOR_VERSION, | |
| 186 | + true | |
| 187 | + ); | |
| 188 | + | |
| 189 | + wp_register_script( | |
| 190 | + 'select2', | |
| 191 | + $this->get_js_assets_url( 'e-select2.full', 'assets/lib/e-select2/js/' ), | |
| 192 | + [ | |
| 193 | + 'jquery', | |
| 194 | + ], | |
| 195 | + '4.0.6-rc.1', | |
| 196 | + true | |
| 197 | + ); | |
| 198 | + | |
| 199 | + wp_enqueue_script( | |
| 200 | + 'elementor-app', | |
| 201 | + $this->get_js_assets_url( 'app' ), | |
| 202 | + [ | |
| 203 | + 'wp-url', | |
| 204 | + 'wp-i18n', | |
| 205 | + 'react', | |
| 206 | + 'react-dom', | |
| 207 | + 'select2', | |
| 208 | + ], | |
| 209 | + ELEMENTOR_VERSION, | |
| 210 | + true | |
| 211 | + ); | |
| 212 | + | |
| 213 | + $this->enqueue_dark_theme_detection_script(); | |
| 214 | + | |
| 215 | + wp_set_script_translations( 'elementor-app-packages', 'elementor' ); | |
| 216 | + wp_set_script_translations( 'elementor-app', 'elementor' ); | |
| 217 | + | |
| 218 | + $this->print_config(); | |
| 219 | + } | |
| 220 | + | |
| 221 | + public function enqueue_app_loader() { | |
| 222 | + wp_enqueue_script( | |
| 223 | + 'elementor-app-loader', | |
| 224 | + $this->get_js_assets_url( 'app-loader' ), | |
| 225 | + [ | |
| 226 | + 'elementor-common', | |
| 227 | + ], | |
| 228 | + ELEMENTOR_VERSION, | |
| 229 | + true | |
| 230 | + ); | |
| 231 | + | |
| 232 | + $this->print_config( 'elementor-app-loader' ); | |
| 233 | + } | |
| 234 | + | |
| 235 | + public function __construct() { | |
| 236 | + $this->add_component( 'site-editor', new Modules\SiteEditor\Module() ); | |
| 237 | + | |
| 238 | + if ( current_user_can( 'manage_options' ) && Plugin::$instance->experiments->is_feature_active( 'e_import_export' ) || Utils::is_wp_cli() ) { | |
| 239 | + $this->add_component( 'import-export', new Modules\ImportExport\Module() ); | |
| 240 | + | |
| 241 | + // Kit library is depended on import-export | |
| 242 | + $this->add_component( 'kit-library', new Modules\KitLibrary\Module() ); | |
| 243 | + } | |
| 244 | + | |
| 245 | + add_action( 'admin_menu', [ $this, 'register_admin_menu' ], 21 /* after Elementor page */ ); | |
| 246 | + | |
| 247 | + // Happens after WP plugin page validation. | |
| 248 | + add_filter( 'add_menu_classes', [ $this, 'fix_submenu' ] ); | |
| 249 | + | |
| 250 | + if ( $this->is_current() ) { | |
| 251 | + add_action( 'admin_init', [ $this, 'admin_init' ], 0 ); | |
| 252 | + } else { | |
| 253 | + add_action( 'elementor/common/after_register_scripts', [ $this, 'enqueue_app_loader' ] ); | |
| 254 | + } | |
| 19 | 255 | } |
| 20 | 256 | } |