app.php (5668B)
1 <?php 2 namespace Elementor\Core\Common; 3 4 use Elementor\Core\Base\App as BaseApp; 5 use Elementor\Core\Common\Modules\Ajax\Module as Ajax; 6 use Elementor\Core\Common\Modules\Finder\Module as Finder; 7 use Elementor\Core\Common\Modules\Connect\Module as Connect; 8 use Elementor\Plugin; 9 10 if ( ! defined( 'ABSPATH' ) ) { 11 exit; // Exit if accessed directly. 12 } 13 14 /** 15 * App 16 * 17 * Elementor's common app that groups shared functionality, components and configuration 18 * 19 * @since 2.3.0 20 */ 21 class App extends BaseApp { 22 23 private $templates = []; 24 25 /** 26 * App constructor. 27 * 28 * @since 2.3.0 29 * @access public 30 */ 31 public function __construct() { 32 $this->add_default_templates(); 33 34 add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'register_scripts' ] ); 35 add_action( 'admin_enqueue_scripts', [ $this, 'register_scripts' ] ); 36 add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ] ); 37 38 add_action( 'elementor/editor/before_enqueue_styles', [ $this, 'register_styles' ] ); 39 add_action( 'admin_enqueue_scripts', [ $this, 'register_styles' ] ); 40 add_action( 'wp_enqueue_scripts', [ $this, 'register_styles' ], 9 ); 41 42 add_action( 'elementor/editor/footer', [ $this, 'print_templates' ] ); 43 add_action( 'admin_footer', [ $this, 'print_templates' ] ); 44 add_action( 'wp_footer', [ $this, 'print_templates' ] ); 45 } 46 47 /** 48 * Init components 49 * 50 * Initializing common components. 51 * 52 * @since 2.3.0 53 * @access public 54 */ 55 public function init_components() { 56 $this->add_component( 'ajax', new Ajax() ); 57 58 if ( current_user_can( 'manage_options' ) ) { 59 if ( ! is_customize_preview() ) { 60 $this->add_component( 'finder', new Finder() ); 61 } 62 } 63 64 $this->add_component( 'connect', new Connect() ); 65 } 66 67 /** 68 * Get name. 69 * 70 * Retrieve the app name. 71 * 72 * @since 2.3.0 73 * @access public 74 * 75 * @return string Common app name. 76 */ 77 public function get_name() { 78 return 'common'; 79 } 80 81 /** 82 * Register scripts. 83 * 84 * Register common scripts. 85 * 86 * @since 2.3.0 87 * @access public 88 */ 89 public function register_scripts() { 90 wp_register_script( 91 'elementor-common-modules', 92 $this->get_js_assets_url( 'common-modules' ), 93 [], 94 ELEMENTOR_VERSION, 95 true 96 ); 97 98 wp_register_script( 99 'backbone-marionette', 100 $this->get_js_assets_url( 'backbone.marionette', 'assets/lib/backbone/' ), 101 [ 102 'backbone', 103 ], 104 '2.4.5.e1', 105 true 106 ); 107 108 wp_register_script( 109 'backbone-radio', 110 $this->get_js_assets_url( 'backbone.radio', 'assets/lib/backbone/' ), 111 [ 112 'backbone', 113 ], 114 '1.0.4', 115 true 116 ); 117 118 wp_register_script( 119 'elementor-dialog', 120 $this->get_js_assets_url( 'dialog', 'assets/lib/dialog/' ), 121 [ 122 'jquery-ui-position', 123 ], 124 '4.8.1', 125 true 126 ); 127 128 wp_enqueue_script( 129 'elementor-common', 130 $this->get_js_assets_url( 'common' ), 131 [ 132 'jquery', 133 'jquery-ui-draggable', 134 'backbone-marionette', 135 'backbone-radio', 136 'elementor-common-modules', 137 'elementor-dialog', 138 'wp-api-request', 139 ], 140 ELEMENTOR_VERSION, 141 true 142 ); 143 144 wp_set_script_translations( 'elementor-common', 'elementor' ); 145 146 $this->print_config(); 147 148 // Used for external plugins. 149 do_action( 'elementor/common/after_register_scripts', $this ); 150 } 151 152 /** 153 * Register styles. 154 * 155 * Register common styles. 156 * 157 * @since 2.3.0 158 * @access public 159 */ 160 public function register_styles() { 161 wp_register_style( 162 'elementor-icons', 163 $this->get_css_assets_url( 'elementor-icons', 'assets/lib/eicons/css/' ), 164 [], 165 '5.12.0' 166 ); 167 168 wp_enqueue_style( 169 'elementor-common', 170 $this->get_css_assets_url( 'common', null, 'default', true ), 171 [ 172 'elementor-icons', 173 ], 174 ELEMENTOR_VERSION 175 ); 176 } 177 178 /** 179 * Add template. 180 * 181 * @since 2.3.0 182 * @access public 183 * 184 * @param string $template Can be either a link to template file or template 185 * HTML content. 186 * @param string $type Optional. Whether to handle the template as path 187 * or text. Default is `path`. 188 */ 189 public function add_template( $template, $type = 'path' ) { 190 if ( 'path' === $type ) { 191 ob_start(); 192 193 include $template; 194 195 $template = ob_get_clean(); 196 } 197 198 $this->templates[] = $template; 199 } 200 201 /** 202 * Print Templates 203 * 204 * Prints all registered templates. 205 * 206 * @since 2.3.0 207 * @access public 208 */ 209 public function print_templates() { 210 foreach ( $this->templates as $template ) { 211 echo $template; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 212 } 213 } 214 215 /** 216 * Get init settings. 217 * 218 * Define the default/initial settings of the common app. 219 * 220 * @since 2.3.0 221 * @access protected 222 * 223 * @return array 224 */ 225 protected function get_init_settings() { 226 $active_experimental_features = Plugin::$instance->experiments->get_active_features(); 227 228 $active_experimental_features = array_fill_keys( array_keys( $active_experimental_features ), true ); 229 230 return [ 231 'version' => ELEMENTOR_VERSION, 232 'isRTL' => is_rtl(), 233 'isDebug' => ( defined( 'WP_DEBUG' ) && WP_DEBUG ), 234 'isElementorDebug' => ( defined( 'ELEMENTOR_DEBUG' ) && ELEMENTOR_DEBUG ), 235 'activeModules' => array_keys( $this->get_components() ), 236 'experimentalFeatures' => $active_experimental_features, 237 'urls' => [ 238 'assets' => ELEMENTOR_ASSETS_URL, 239 'rest' => get_rest_url(), 240 ], 241 ]; 242 } 243 244 /** 245 * Add default templates. 246 * 247 * Register common app default templates. 248 * @since 2.3.0 249 * @access private 250 */ 251 private function add_default_templates() { 252 $default_templates = [ 253 'includes/editor-templates/library-layout.php', 254 ]; 255 256 foreach ( $default_templates as $template ) { 257 $this->add_template( ELEMENTOR_PATH . $template ); 258 } 259 } 260 }