PluginProbe
Elementor Website Builder – more than just a page builder / 3.20.3
Elementor Website Builder – more than just a page builder v3.20.3
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / admin / admin.php

admin.php in Elementor Website Builder – more than just a page builder 3.20.3, at core/admin/admin.php

1,002 lines 30.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Admin;
3
4 use Elementor\Api;
5 use Elementor\Beta_Testers;
6 use Elementor\Core\Admin\Menu\Main as MainMenu;
7 use Elementor\App\Modules\Onboarding\Module as Onboarding_Module;
8 use Elementor\Core\Base\App;
9 use Elementor\Core\Upgrade\Manager as Upgrade_Manager;
10 use Elementor\Core\Utils\Assets_Config_Provider;
11 use Elementor\Core\Utils\Collection;
12 use Elementor\Plugin;
13 use Elementor\Settings;
14 use Elementor\User;
15 use Elementor\Utils;
16 use Elementor\Core\Utils\Hints;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit; // Exit if accessed directly.
20 }
21
22 class Admin extends App {
23
24 private $menus = [];
25
26 /**
27 * Get module name.
28 *
29 * Retrieve the module name.
30 *
31 * @since 2.3.0
32 * @access public
33 *
34 * @return string Module name.
35 */
36 public function get_name() {
37 return 'admin';
38 }
39
40 /**
41 * @since 2.2.0
42 * @access public
43 */
44 public function maybe_redirect_to_getting_started() {
45 if ( ! get_transient( 'elementor_activation_redirect' ) ) {
46 return;
47 }
48
49 if ( wp_doing_ajax() ) {
50 return;
51 }
52
53 delete_transient( 'elementor_activation_redirect' );
54
55 if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
56 return;
57 }
58
59 $already_had_onboarding = get_option( Onboarding_Module::ONBOARDING_OPTION );
60
61 // Get the latest installation from Elementor's Install log in the DB.
62 $latest_install = key( Upgrade_Manager::get_installs_history() );
63
64 if ( ! empty( $latest_install ) ) {
65 $is_new_install = version_compare( $latest_install, '3.6.0-beta', '>=' );
66 } else {
67 // If `$latest_install` is not set, Elementor was never installed on this site.
68 $is_new_install = true;
69 }
70
71 if ( $already_had_onboarding || ! $is_new_install ) {
72 return;
73 }
74
75 wp_safe_redirect( admin_url( 'admin.php?page=elementor-app#onboarding' ) );
76
77 exit;
78 }
79
80 private function register_packages() {
81 $assets_config_provider = ( new Assets_Config_Provider() )
82 ->set_path_resolver( function ( $name ) {
83 return ELEMENTOR_ASSETS_PATH . "js/packages/{$name}/{$name}.asset.php";
84 } );
85
86 Collection::make( [ 'ui', 'icons', 'query' ] )
87 ->each( function( $package ) use ( $assets_config_provider ) {
88 $suffix = Utils::is_script_debug() ? '' : '.min';
89 $config = $assets_config_provider->load( $package )->get( $package );
90
91 if ( ! $config ) {
92 return;
93 }
94
95 wp_register_script(
96 $config['handle'],
97 ELEMENTOR_ASSETS_URL . "js/packages/{$package}/{$package}{$suffix}.js",
98 $config['deps'],
99 ELEMENTOR_VERSION,
100 true
101 );
102 } );
103 }
104
105 /**
106 * Enqueue admin scripts.
107 *
108 * Registers all the admin scripts and enqueues them.
109 *
110 * Fired by `admin_enqueue_scripts` action.
111 *
112 * @since 1.0.0
113 * @access public
114 */
115 public function enqueue_scripts() {
116 wp_register_script(
117 'elementor-admin-modules',
118 $this->get_js_assets_url( 'admin-modules' ),
119 [],
120 ELEMENTOR_VERSION,
121 true
122 );
123
124 $this->register_packages();
125
126 // Temporary solution for the admin.
127 wp_register_script(
128 'elementor-ai-admin',
129 $this->get_js_assets_url( 'ai-admin' ),
130 [
131 'elementor-common',
132 'elementor-v2-ui',
133 'elementor-v2-icons',
134 ],
135 ELEMENTOR_VERSION,
136 true
137 );
138
139 wp_register_script(
140 'elementor-admin',
141 $this->get_js_assets_url( 'admin' ),
142 [
143 'elementor-common',
144 'elementor-admin-modules',
145 ],
146 ELEMENTOR_VERSION,
147 true
148 );
149
150 wp_enqueue_script( 'elementor-admin' );
151
152 wp_set_script_translations( 'elementor-admin', 'elementor' );
153
154 $this->maybe_enqueue_hints();
155
156 $this->print_config();
157 }
158
159 /**
160 * Enqueue admin styles.
161 *
162 * Registers all the admin styles and enqueues them.
163 *
164 * Fired by `admin_enqueue_scripts` action.
165 *
166 * @since 1.0.0
167 * @access public
168 */
169 public function enqueue_styles() {
170 $direction_suffix = is_rtl() ? '-rtl' : '';
171
172 wp_register_style(
173 'elementor-admin',
174 $this->get_css_assets_url( 'admin' . $direction_suffix ),
175 [
176 'elementor-common',
177 ],
178 ELEMENTOR_VERSION
179 );
180
181 wp_enqueue_style( 'elementor-admin' );
182
183 // It's for upgrade notice.
184 // TODO: enqueue this just if needed.
185 add_thickbox();
186 }
187
188 /**
189 * Print switch mode button.
190 *
191 * Adds a switch button in post edit screen (which has cpt support). To allow
192 * the user to switch from the native WordPress editor to Elementor builder.
193 *
194 * Fired by `edit_form_after_title` action.
195 *
196 * @since 1.0.0
197 * @access public
198 *
199 * @param \WP_Post $post The current post object.
200 */
201 public function print_switch_mode_button( $post ) {
202 // Exit if Gutenberg are active.
203 if ( did_action( 'enqueue_block_editor_assets' ) ) {
204 return;
205 }
206
207 $document = Plugin::$instance->documents->get( $post->ID );
208
209 if ( ! $document || ! $document->is_editable_by_current_user() ) {
210 return;
211 }
212
213 wp_nonce_field( basename( __FILE__ ), '_elementor_edit_mode_nonce' );
214 ?>
215 <div id="elementor-switch-mode">
216 <input id="elementor-switch-mode-input" type="hidden" name="_elementor_post_mode" value="<?php echo esc_attr( $document->is_built_with_elementor() ); ?>" />
217 <button id="elementor-switch-mode-button" type="button" class="button button-primary button-hero">
218 <span class="elementor-switch-mode-on">
219 <i class="eicon-arrow-<?php echo ( is_rtl() ) ? 'right' : 'left'; ?>" aria-hidden="true"></i>
220 <?php echo esc_html__( 'Back to WordPress Editor', 'elementor' ); ?>
221 </span>
222 <span class="elementor-switch-mode-off">
223 <i class="eicon-elementor-square" aria-hidden="true"></i>
224 <?php echo esc_html__( 'Edit with Elementor', 'elementor' ); ?>
225 </span>
226 </button>
227 </div>
228 <div id="elementor-editor">
229 <a id="elementor-go-to-edit-page-link" href="<?php echo esc_url( $document->get_edit_url() ); ?>">
230 <div id="elementor-editor-button" class="button button-primary button-hero">
231 <i class="eicon-elementor-square" aria-hidden="true"></i>
232 <?php echo esc_html__( 'Edit with Elementor', 'elementor' ); ?>
233 </div>
234 <div class="elementor-loader-wrapper">
235 <div class="elementor-loader">
236 <div class="elementor-loader-boxes">
237 <div class="elementor-loader-box"></div>
238 <div class="elementor-loader-box"></div>
239 <div class="elementor-loader-box"></div>
240 <div class="elementor-loader-box"></div>
241 </div>
242 </div>
243 <div class="elementor-loading-title"><?php echo esc_html__( 'Loading', 'elementor' ); ?></div>
244 </div>
245 </a>
246 </div>
247 <?php
248 }
249
250 /**
251 * Save post.
252 *
253 * Flag the post mode when the post is saved.
254 *
255 * Fired by `save_post` action.
256 *
257 * @since 1.0.0
258 * @access public
259 *
260 * @param int $post_id Post ID.
261 */
262 public function save_post( $post_id ) {
263 if ( ! wp_verify_nonce( Utils::get_super_global_value( $_POST, '_elementor_edit_mode_nonce' ), basename( __FILE__ ) ) ) {
264 return;
265 }
266
267 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
268 return;
269 }
270
271 Plugin::$instance->documents->get( $post_id )->set_is_built_with_elementor( ! empty( $_POST['_elementor_post_mode'] ) );
272 }
273
274 /**
275 * Add Elementor post state.
276 *
277 * Adds a new "Elementor" post state to the post table.
278 *
279 * Fired by `display_post_states` filter.
280 *
281 * @since 1.8.0
282 * @access public
283 *
284 * @param array $post_states An array of post display states.
285 * @param \WP_Post $post The current post object.
286 *
287 * @return array A filtered array of post display states.
288 */
289 public function add_elementor_post_state( $post_states, $post ) {
290 $document = Plugin::$instance->documents->get( $post->ID );
291
292 if ( $document && $document->is_built_with_elementor() && $document->is_editable_by_current_user() ) {
293 $post_states['elementor'] = esc_html__( 'Elementor', 'elementor' );
294 }
295
296 return $post_states;
297 }
298
299 /**
300 * Body status classes.
301 *
302 * Adds CSS classes to the admin body tag.
303 *
304 * Fired by `admin_body_class` filter.
305 *
306 * @since 1.0.0
307 * @access public
308 *
309 * @param string $classes Space-separated list of CSS classes.
310 *
311 * @return string Space-separated list of CSS classes.
312 */
313 public function body_status_classes( $classes ) {
314 global $pagenow;
315
316 if ( in_array( $pagenow, [ 'post.php', 'post-new.php' ], true ) && Utils::is_post_support() ) {
317 $post = get_post();
318
319 $document = Plugin::$instance->documents->get( $post->ID );
320
321 $mode_class = $document && $document->is_built_with_elementor() ? 'elementor-editor-active' : 'elementor-editor-inactive';
322
323 $classes .= ' ' . $mode_class;
324 }
325
326 return $classes;
327 }
328
329 /**
330 * Plugin action links.
331 *
332 * Adds action links to the plugin list table
333 *
334 * Fired by `plugin_action_links` filter.
335 *
336 * @since 1.0.0
337 * @access public
338 *
339 * @param array $links An array of plugin action links.
340 *
341 * @return array An array of plugin action links.
342 */
343 public function plugin_action_links( $links ) {
344 $settings_link = sprintf( '<a href="%1$s">%2$s</a>', admin_url( 'admin.php?page=' . Settings::PAGE_ID ), esc_html__( 'Settings', 'elementor' ) );
345
346 array_unshift( $links, $settings_link );
347
348 $links['go_pro'] = sprintf( '<a href="%1$s" target="_blank" class="elementor-plugins-gopro">%2$s</a>', 'https://go.elementor.com/go-pro-wp-plugins/', esc_html__( 'Get Elementor Pro', 'elementor' ) );
349
350 return $links;
351 }
352
353 /**
354 * Plugin row meta.
355 *
356 * Adds row meta links to the plugin list table
357 *
358 * Fired by `plugin_row_meta` filter.
359 *
360 * @since 1.1.4
361 * @access public
362 *
363 * @param array $plugin_meta An array of the plugin's metadata, including
364 * the version, author, author URI, and plugin URI.
365 * @param string $plugin_file Path to the plugin file, relative to the plugins
366 * directory.
367 *
368 * @return array An array of plugin row meta links.
369 */
370 public function plugin_row_meta( $plugin_meta, $plugin_file ) {
371 if ( ELEMENTOR_PLUGIN_BASE === $plugin_file ) {
372 $row_meta = [
373 'docs' => '<a href="https://go.elementor.com/docs-admin-plugins/" aria-label="' . esc_attr( esc_html__( 'View Elementor Documentation', 'elementor' ) ) . '" target="_blank">' . esc_html__( 'Docs & FAQs', 'elementor' ) . '</a>',
374 'ideo' => '<a href="https://go.elementor.com/yt-admin-plugins/" aria-label="' . esc_attr( esc_html__( 'View Elementor Video Tutorials', 'elementor' ) ) . '" target="_blank">' . esc_html__( 'Video Tutorials', 'elementor' ) . '</a>',
375 ];
376
377 $plugin_meta = array_merge( $plugin_meta, $row_meta );
378 }
379
380 return $plugin_meta;
381 }
382
383 /**
384 * Admin footer text.
385 *
386 * Modifies the "Thank you" text displayed in the admin footer.
387 *
388 * Fired by `admin_footer_text` filter.
389 *
390 * @since 1.0.0
391 * @access public
392 *
393 * @param string $footer_text The content that will be printed.
394 *
395 * @return string The content that will be printed.
396 */
397 public function admin_footer_text( $footer_text ) {
398 $current_screen = get_current_screen();
399 $is_elementor_screen = ( $current_screen && false !== strpos( $current_screen->id, 'elementor' ) );
400
401 if ( $is_elementor_screen ) {
402 $footer_text = sprintf(
403 /* translators: 1: Elementor, 2: Link to plugin review */
404 __( 'Enjoyed %1$s? Please leave us a %2$s rating. We really appreciate your support!', 'elementor' ),
405 '<strong>' . esc_html__( 'Elementor', 'elementor' ) . '</strong>',
406 '<a href="https://go.elementor.com/admin-review/" target="_blank">&#9733;&#9733;&#9733;&#9733;&#9733;</a>'
407 );
408 }
409
410 return $footer_text;
411 }
412
413 /**
414 * Register dashboard widgets.
415 *
416 * Adds a new Elementor widgets to WordPress dashboard.
417 *
418 * Fired by `wp_dashboard_setup` action.
419 *
420 * @since 1.9.0
421 * @access public
422 */
423 public function register_dashboard_widgets() {
424 wp_add_dashboard_widget( 'e-dashboard-overview', esc_html__( 'Elementor Overview', 'elementor' ), [ $this, 'elementor_dashboard_overview_widget' ] );
425
426 // Move our widget to top.
427 global $wp_meta_boxes;
428
429 $dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
430 $ours = [
431 'e-dashboard-overview' => $dashboard['e-dashboard-overview'],
432 ];
433
434 $wp_meta_boxes['dashboard']['normal']['core'] = array_merge( $ours, $dashboard ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
435 }
436
437 /**
438 * Displays the Elementor dashboard widget.
439 *
440 * Fired by `wp_add_dashboard_widget` function.
441 *
442 * @since 1.9.0
443 * @access public
444 */
445 public function elementor_dashboard_overview_widget() {
446 ?>
447 <div class="e-dashboard-overview e-dashboard-widget">
448 <?php
449 self::elementor_dashboard_overview_header();
450 self::elementor_dashboard_overview_recently_edited();
451 self::elementor_dashboard_overview_news_updates();
452 self::elementor_dashboard_overview_footer();
453 ?>
454 </div>
455 <?php
456 }
457
458 /**
459 * Displays the Elementor dashboard widget - header section.
460 * Fired by `elementor_dashboard_overview_widget` function.
461 *
462 * @param bool $show_versions
463 * @param bool $is_create_post_enabled
464 *
465 * @return void
466 * @since 3.12.0
467 */
468 public static function elementor_dashboard_overview_header( bool $show_versions = true, bool $is_create_post_enabled = true ) {
469 if ( User::is_current_user_can_edit_post_type( 'page' ) ) {
470 $create_new_label = esc_html__( 'Create New Page', 'elementor' );
471 $create_new_post_type = 'page';
472 } elseif ( User::is_current_user_can_edit_post_type( 'post' ) ) {
473 $create_new_label = esc_html__( 'Create New Post', 'elementor' );
474 $create_new_post_type = 'post';
475 }
476 ?>
477 <div class="e-overview__header">
478 <?php if ( $show_versions ) { ?>
479 <div class="e-overview__logo">
480 <div class="e-logo-wrapper"><i class="eicon-elementor"></i></div>
481 </div>
482 <div class="e-overview__versions">
483 <span class="e-overview__version"><?php echo esc_html__( 'Elementor', 'elementor' ); ?> v<?php echo ELEMENTOR_VERSION; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
484 <?php
485 /**
486 * Elementor dashboard widget after the version.
487 * Fires after Elementor version display in the dashboard widget.
488 *
489 * @since 1.9.0
490 */
491 do_action( 'elementor/admin/dashboard_overview_widget/after_version' );
492 ?>
493 </div>
494 <?php } ?>
495 <?php if ( ! empty( $create_new_post_type ) && $is_create_post_enabled ) { ?>
496 <div class="e-overview__create">
497 <a href="<?php echo esc_url( Plugin::$instance->documents->get_create_new_post_url( $create_new_post_type ) ); ?>" class="button"><span aria-hidden="true" class="dashicons dashicons-plus"></span> <?php echo esc_html( $create_new_label ); ?></a>
498 </div>
499 <?php } ?>
500 </div>
501 <?php
502 }
503
504 /**
505 * Displays the Elementor dashboard widget - recently edited section.
506 * Fired by `elementor_dashboard_overview_widget` function.
507 *
508 * @param array $args
509 * @param bool $show_heading
510 *
511 * @return void
512 * @since 3.12.0
513 */
514 public static function elementor_dashboard_overview_recently_edited( array $args = [], bool $show_heading = true ) {
515 $recently_edited_query = Utils::get_recently_edited_posts_query( $args );
516
517 if ( $recently_edited_query->have_posts() ) { ?>
518 <div class="e-overview__recently-edited">
519 <?php if ( $show_heading ) { ?>
520 <h3 class="e-heading e-divider_bottom"><?php echo esc_html__( 'Recently Edited', 'elementor' ); ?></h3>
521 <?php } ?>
522 <ul class="e-overview__posts">
523 <?php
524 while ( $recently_edited_query->have_posts() ) {
525 $recently_edited_query->the_post();
526 $document = Plugin::$instance->documents->get( get_the_ID() );
527
528 $date = date_i18n( _x( 'M jS', 'Dashboard Overview Widget Recently Date', 'elementor' ), get_the_modified_time( 'U' ) );
529 ?>
530 <li class="e-overview__post">
531 <a href="<?php echo esc_url( $document->get_edit_url() ); ?>" class="e-overview__post-link"><?php echo esc_html( get_the_title() ); ?>
532 <span class="dashicons dashicons-edit"></span></a>
533 <span><?php echo $date; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>, <?php the_modified_time(); ?></span>
534 </li>
535 <?php } ?>
536 </ul>
537 </div>
538 <?php }
539 }
540
541 /**
542 * Displays the Elementor dashboard widget - news and updates section.
543 * Fired by `elementor_dashboard_overview_widget` function.
544 *
545 * @param int $limit_feed
546 * @param bool $show_heading
547 *
548 * @return void
549 * @since 3.12.0
550 * @access public
551 */
552 public static function elementor_dashboard_overview_news_updates( int $limit_feed = 0, bool $show_heading = true ) {
553 $elementor_feed = Api::get_feed_data();
554 if ( $limit_feed > 0 ) {
555 $elementor_feed = array_slice( $elementor_feed, 0, $limit_feed );
556 }
557
558 if ( ! empty( $elementor_feed ) ) { ?>
559 <div class="e-overview__feed">
560 <?php if ( $show_heading ) { ?>
561 <h3 class="e-heading e-divider_bottom"><?php echo esc_html__( 'News & Updates', 'elementor' ); ?></h3>
562 <?php } ?>
563 <ul class="e-overview__posts">
564 <?php foreach ( $elementor_feed as $feed_item ) { ?>
565 <li class="e-overview__post">
566 <a href="<?php echo esc_url( $feed_item['url'] ); ?>" class="e-overview__post-link" target="_blank">
567 <?php if ( ! empty( $feed_item['badge'] ) ) { ?>
568 <span class="e-overview__badge"><?php echo esc_html( $feed_item['badge'] ); ?></span>
569 <?php } ?>
570 <?php echo esc_html( $feed_item['title'] ); ?>
571 </a>
572 <p class="e-overview__post-description"><?php echo esc_html( $feed_item['excerpt'] ); ?></p>
573 </li>
574 <?php } ?>
575 </ul>
576 </div>
577 <?php }
578 }
579
580 /**
581 * Displays the Elementor dashboard widget - footer section.
582 * Fired by `elementor_dashboard_overview_widget` function.
583 *
584 * @since 3.12.0
585 */
586 public static function elementor_dashboard_overview_footer() {
587 ?>
588 <div class="e-overview__footer e-divider_top">
589 <ul>
590 <?php foreach ( self::static_get_dashboard_overview_widget_footer_actions() as $action_id => $action ) { ?>
591 <li class="e-overview__<?php echo esc_attr( $action_id ); ?>">
592 <a href="<?php echo esc_url( $action['link'] ); ?>" target="_blank"><?php echo esc_html( $action['title'] ); ?>
593 <span class="screen-reader-text"><?php echo esc_html__( '(opens in a new window)', 'elementor' ); ?></span>
594 <span aria-hidden="true" class="dashicons dashicons-external"></span></a>
595 </li>
596 <?php } ?>
597 </ul>
598 </div>
599 <?php
600 }
601
602 /**
603 * Get elementor dashboard overview widget footer actions.
604 *
605 * Retrieves the footer action links displayed in elementor dashboard widget.
606 *
607 * @since 3.12.0
608 * @access public
609 */
610 public static function static_get_dashboard_overview_widget_footer_actions() {
611 $base_actions = [
612 'blog' => [
613 'title' => esc_html__( 'Blog', 'elementor' ),
614 'link' => 'https://go.elementor.com/overview-widget-blog/',
615 ],
616 'help' => [
617 'title' => esc_html__( 'Help', 'elementor' ),
618 'link' => 'https://go.elementor.com/overview-widget-docs/',
619 ],
620 ];
621
622 $additions_actions = [];
623
624 if ( User::get_introduction_meta( 'ai_get_started' ) ) {
625 $additions_actions['ai-library'] = [
626 'title' => esc_html__( 'AI Prompts Library', 'elementor' ),
627 'link' => 'https://go.elementor.com/overview-ai-prompts-library/',
628 ];
629 } else {
630 $additions_actions['ai'] = [
631 'title' => esc_html__( 'Build Smart with AI', 'elementor' ),
632 'link' => 'https://go.elementor.com/overview-widget-ai/',
633 ];
634 }
635
636 $additions_actions['go-pro'] = [
637 'title' => esc_html__( 'Upgrade', 'elementor' ),
638 'link' => 'https://go.elementor.com/go-pro-wp-overview-widget/',
639 ];
640
641 /**
642 * Dashboard widget footer actions.
643 *
644 * Filters the additions actions displayed in Elementor dashboard widget.
645 *
646 * Developers can add new action links to Elementor dashboard widget
647 * footer using this filter.
648 *
649 * @since 1.9.0
650 *
651 * @param array $additions_actions Elementor dashboard widget footer actions.
652 */
653 $additions_actions = apply_filters( 'elementor/admin/dashboard_overview_widget/footer_actions', $additions_actions );
654
655 $actions = $base_actions + $additions_actions;
656
657 return $actions;
658 }
659
660 /**
661 * Get elementor dashboard overview widget footer actions.
662 *
663 * Retrieves the footer action links displayed in elementor dashboard widget.
664 *
665 * @since 1.9.0
666 * @access private
667 */
668 private function get_dashboard_overview_widget_footer_actions() {
669 return self::static_get_dashboard_overview_widget_footer_actions();
670 }
671
672 /**
673 * Admin action new post.
674 *
675 * When a new post action is fired the title is set to 'Elementor' and the post ID.
676 *
677 * Fired by `admin_action_elementor_new_post` action.
678 *
679 * @since 1.9.0
680 * @access public
681 */
682 public function admin_action_new_post() {
683 check_admin_referer( 'elementor_action_new_post' );
684
685 $post_type = Utils::get_super_global_value( $_GET, 'post_type' ) ?? 'post';
686
687 if ( ! User::is_current_user_can_edit_post_type( $post_type ) ) {
688 return;
689 }
690
691 if ( empty( $_GET['template_type'] ) ) {
692 $type = 'post';
693 } else {
694 $type = sanitize_text_field( wp_unslash( $_GET['template_type'] ) );
695 }
696
697 $post_data = Utils::get_super_global_value( $_GET, 'post_data' ) ?? [];
698
699 /**
700 * Create new post meta data.
701 *
702 * Filters the meta data of any new post created.
703 *
704 * @since 2.0.0
705 *
706 * @param array $meta Post meta data.
707 */
708 $meta = [];
709
710 if ( isset( $_GET['meta'] ) && is_array( $_GET['meta'] ) ) {
711 $meta = array_map( 'sanitize_text_field', wp_unslash( $_GET['meta'] ) );
712 }
713
714 $meta = apply_filters( 'elementor/admin/create_new_post/meta', $meta );
715
716 $post_data['post_type'] = $post_type;
717
718 $document = Plugin::$instance->documents->create( $type, $post_data, $meta );
719
720 if ( is_wp_error( $document ) ) {
721 wp_die( $document ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
722 }
723
724 wp_redirect( $document->get_edit_url() );
725
726 die;
727 }
728
729 /**
730 * @since 2.3.0
731 * @access public
732 */
733 public function add_new_template_template() {
734 Plugin::$instance->common->add_template( ELEMENTOR_PATH . 'includes/admin-templates/new-template.php' );
735 }
736
737 /**
738 * @access public
739 */
740 public function enqueue_new_template_scripts() {
741 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
742
743 wp_enqueue_script(
744 'elementor-new-template',
745 ELEMENTOR_ASSETS_URL . 'js/new-template' . $suffix . '.js',
746 [],
747 ELEMENTOR_VERSION,
748 true
749 );
750
751 wp_set_script_translations( 'elementor-new-template', 'elementor' );
752 }
753
754 /**
755 * @since 2.6.0
756 * @access public
757 */
758 public function add_beta_tester_template() {
759 Plugin::$instance->common->add_template( ELEMENTOR_PATH . 'includes/admin-templates/beta-tester.php' );
760 }
761
762 /**
763 * @access public
764 */
765 public function enqueue_beta_tester_scripts() {
766 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
767
768 wp_enqueue_script(
769 'elementor-beta-tester',
770 ELEMENTOR_ASSETS_URL . 'js/beta-tester' . $suffix . '.js',
771 [],
772 ELEMENTOR_VERSION,
773 true
774 );
775
776 wp_set_script_translations( 'elementor-beta-tester', 'elementor' );
777 }
778
779 /**
780 * @access public
781 */
782 public function init_new_template() {
783 if ( 'edit-elementor_library' !== get_current_screen()->id ) {
784 return;
785 }
786
787 // Allow plugins to add their templates on admin_head.
788 add_action( 'admin_head', [ $this, 'add_new_template_template' ] );
789 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_new_template_scripts' ] );
790 }
791
792 public function version_update_warning( $current_version, $new_version ) {
793 $current_version_minor_part = explode( '.', $current_version )[1];
794 $new_version_minor_part = explode( '.', $new_version )[1];
795
796 if ( $current_version_minor_part === $new_version_minor_part ) {
797 return;
798 }
799 ?>
800 <hr class="e-major-update-warning__separator" />
801 <div class="e-major-update-warning">
802 <div class="e-major-update-warning__icon">
803 <i class="eicon-info-circle"></i>
804 </div>
805 <div>
806 <div class="e-major-update-warning__title">
807 <?php echo esc_html__( 'Heads up, Please backup before upgrade!', 'elementor' ); ?>
808 </div>
809 <div class="e-major-update-warning__message">
810 <?php
811 printf(
812 /* translators: %1$s Link open tag, %2$s: Link close tag. */
813 esc_html__( 'The latest update includes some substantial changes across different areas of the plugin. We highly recommend you %1$sbackup your site before upgrading%2$s, and make sure you first update in a staging environment', 'elementor' ),
814 '<a href="https://go.elementor.com/wp-dash-update-backup/">',
815 '</a>'
816 );
817 ?>
818 </div>
819 </div>
820 </div>
821 <?php
822 }
823
824 /**
825 * @access public
826 */
827 public function init_beta_tester( $current_screen ) {
828 if ( ( 'toplevel_page_elementor' === $current_screen->base ) || 'elementor_page_elementor-tools' === $current_screen->id ) {
829 add_action( 'admin_head', [ $this, 'add_beta_tester_template' ] );
830 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_beta_tester_scripts' ] );
831 }
832 }
833
834 /**
835 * Admin constructor.
836 *
837 * Initializing Elementor in WordPress admin.
838 *
839 * @since 1.0.0
840 * @access public
841 */
842 public function __construct() {
843 Plugin::$instance->init_common();
844
845 $this->add_component( 'feedback', new Feedback() );
846 $this->add_component( 'admin-notices', new Admin_Notices() );
847
848 if ( Plugin::$instance->experiments->is_feature_active( 'admin_menu_rearrangement' ) ) {
849 $this->register_menu();
850 }
851
852 add_action( 'admin_init', [ $this, 'maybe_redirect_to_getting_started' ] );
853
854 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
855 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] );
856
857 add_action( 'edit_form_after_title', [ $this, 'print_switch_mode_button' ] );
858 add_action( 'save_post', [ $this, 'save_post' ] );
859
860 add_filter( 'display_post_states', [ $this, 'add_elementor_post_state' ], 10, 2 );
861
862 add_filter( 'plugin_action_links_' . ELEMENTOR_PLUGIN_BASE, [ $this, 'plugin_action_links' ] );
863 add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 2 );
864
865 add_filter( 'admin_body_class', [ $this, 'body_status_classes' ] );
866 add_filter( 'admin_footer_text', [ $this, 'admin_footer_text' ] );
867
868 // Register Dashboard Widgets.
869 add_action( 'wp_dashboard_setup', [ $this, 'register_dashboard_widgets' ] );
870
871 // Admin Actions
872 add_action( 'admin_action_elementor_new_post', [ $this, 'admin_action_new_post' ] );
873
874 add_action( 'current_screen', [ $this, 'init_new_template' ] );
875 add_action( 'current_screen', [ $this, 'init_beta_tester' ] );
876
877 add_action( 'in_plugin_update_message-' . ELEMENTOR_PLUGIN_BASE, function( $plugin_data ) {
878 $this->version_update_warning( ELEMENTOR_VERSION, $plugin_data['new_version'] );
879 } );
880 }
881
882 /**
883 * @since 2.3.0
884 * @access protected
885 */
886 protected function get_init_settings() {
887 $beta_tester_email = get_user_meta( get_current_user_id(), User::BETA_TESTER_META_KEY, true );
888 $elementor_beta = get_option( 'elementor_beta', 'no' );
889 $all_introductions = User::get_introduction_meta();
890 $beta_tester_signup_dismissed = array_key_exists( Beta_Testers::BETA_TESTER_SIGNUP, $all_introductions );
891
892 $settings = [
893 'home_url' => home_url(),
894 'settings_url' => Settings::get_url(),
895 'user' => [
896 'introduction' => User::get_introduction_meta(),
897 'restrictions' => Plugin::$instance->role_manager->get_user_restrictions_array(),
898 'is_administrator' => current_user_can( 'manage_options' ),
899 ],
900 'beta_tester' => [
901 'beta_tester_signup' => Beta_Testers::BETA_TESTER_SIGNUP,
902 'has_email' => $beta_tester_email,
903 'option_enabled' => 'no' !== $elementor_beta,
904 'signup_dismissed' => $beta_tester_signup_dismissed,
905 ],
906 'experiments' => $this->get_experiments(),
907 ];
908
909 /**
910 * Localize settings.
911 *
912 * Filters the initial localize settings in the admin.
913 *
914 * WordPress has it's own way to pass localized data from PHP (backend) to
915 * JS (frontend). Elementor uses this method to pass localize data in the
916 * admin. This hook can be used to add more localized settings in addition
917 * to the initial Elementor settings.
918 *
919 * @since 2.3.0
920 *
921 * @param array $settings Initial localize settings.
922 */
923 $settings = apply_filters( 'elementor/admin/localize_settings', $settings );
924
925 return $settings;
926 }
927
928 private function get_experiments() {
929 return ( new Collection( Plugin::$instance->experiments->get_features() ) )
930 ->map( function ( $experiment_data ) {
931 $dependencies = $experiment_data['dependencies'] ?? [];
932
933 $dependencies = ( new Collection( $dependencies ) )
934 ->map( function ( $dependency ) {
935 return $dependency->get_name();
936 } )->all();
937
938 return [
939 'name' => $experiment_data['name'],
940 'title' => $experiment_data['title'] ?? $experiment_data['name'],
941 'state' => $experiment_data['state'],
942 'default' => $experiment_data['default'],
943 'dependencies' => $dependencies,
944 'messages' => $experiment_data['messages'] ?? [],
945 ];
946 } )->all();
947 }
948
949 private function register_menu() {
950 $this->menus['main'] = new MainMenu();
951 }
952
953 private function maybe_enqueue_hints() {
954 if ( ! Hints::should_display_hint( 'image-optimization-once-media-modal' ) && ! Hints::should_display_hint( 'image-optimization-media-modal' ) ) {
955 return;
956 }
957
958 wp_register_script(
959 'media-hints',
960 $this->get_js_assets_url( 'media-hints' ),
961 [],
962 ELEMENTOR_VERSION,
963 true
964 );
965
966 $once_dismissed = Hints::is_dismissed( 'image-optimization-once-media-modal' );
967 $content = $once_dismissed ?
968 sprintf("%1\$s <a href='%2\$s' class='e-btn-1' target='_blank'>%3\$s</a> %4\$s",
969 __( 'This image is large and may slow things down.', 'elementor' ),
970 Hints::get_plugin_action_url( 'image-optimization' ),
971 ( Hints::is_plugin_installed( 'image-optimization' ) ? __( 'Activate', 'elementor' ) : __( 'Install', 'elementor' ) ) . ' ' . __( 'Image Optimizer', 'elementor' ),
972 __( 'to reduce size without losing quality.', 'elementor' )
973 ) :
974 sprintf("%1\$s <a class='e-btn-1' href='%2\$s' target='_blank'>%3\$s</a>!",
975 __( 'Don’t let unoptimized images be the downfall of your site’s performance.', 'elementor' ),
976 Hints::get_plugin_action_url( 'image-optimization' ),
977 ( Hints::is_plugin_installed( 'image-optimization' ) ? __( 'Activate', 'elementor' ) : __( 'Install', 'elementor' ) ) . ' ' . __( 'Image Optimizer', 'elementor' )
978 );
979
980 $dismissible = $once_dismissed ? 'image-optimization-media-modal' : 'image-optimization-once-media-modal';
981
982 wp_localize_script( 'media-hints', 'elementorAdminHints', [
983 'mediaHint' => [
984 'display' => ! $once_dismissed,
985 'type' => $once_dismissed ? 'warning' : 'info',
986 'content' => $content,
987 'icon' => true,
988 'dismissible' => $dismissible,
989 'dismiss' => __( 'Dismiss this notice.', 'elementor' ),
990 'button_event' => $dismissible,
991 'button_data' => base64_encode(
992 json_encode( [
993 'action_url' => Hints::get_plugin_action_url( 'image-optimization' ),
994 ] ),
995 ),
996 ],
997 ] );
998
999 wp_enqueue_script( 'media-hints' );
1000 }
1001 }
1002