PluginProbe
Elementor Website Builder – more than just a page builder / 3.25.7
Elementor Website Builder – more than just a page builder v3.25.7
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.25.7, at core/admin/admin.php

1,100 lines 32.2 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 $go_pro_text = esc_html__( 'Get Elementor Pro', 'elementor' );
349 if ( Utils::is_sale_time() ) {
350 $go_pro_text = esc_html__( 'Discounted Upgrades Now!', 'elementor' );
351 }
352
353 $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/', $go_pro_text );
354
355 return $links;
356 }
357
358 /**
359 * Plugin row meta.
360 *
361 * Adds row meta links to the plugin list table
362 *
363 * Fired by `plugin_row_meta` filter.
364 *
365 * @since 1.1.4
366 * @access public
367 *
368 * @param array $plugin_meta An array of the plugin's metadata, including
369 * the version, author, author URI, and plugin URI.
370 * @param string $plugin_file Path to the plugin file, relative to the plugins
371 * directory.
372 *
373 * @return array An array of plugin row meta links.
374 */
375 public function plugin_row_meta( $plugin_meta, $plugin_file ) {
376 if ( ELEMENTOR_PLUGIN_BASE === $plugin_file ) {
377 $row_meta = [
378 '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>',
379 '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>',
380 ];
381
382 $plugin_meta = array_merge( $plugin_meta, $row_meta );
383 }
384
385 return $plugin_meta;
386 }
387
388 /**
389 * Admin footer text.
390 *
391 * Modifies the "Thank you" text displayed in the admin footer.
392 *
393 * Fired by `admin_footer_text` filter.
394 *
395 * @since 1.0.0
396 * @access public
397 *
398 * @param string $footer_text The content that will be printed.
399 *
400 * @return string The content that will be printed.
401 */
402 public function admin_footer_text( $footer_text ) {
403 $current_screen = get_current_screen();
404 $is_elementor_screen = ( $current_screen && false !== strpos( $current_screen->id, 'elementor' ) );
405
406 if ( $is_elementor_screen ) {
407 $footer_text = sprintf(
408 /* translators: 1: Elementor, 2: Link to plugin review */
409 __( 'Enjoyed %1$s? Please leave us a %2$s rating. We really appreciate your support!', 'elementor' ),
410 '<strong>' . esc_html__( 'Elementor', 'elementor' ) . '</strong>',
411 '<a href="https://go.elementor.com/admin-review/" target="_blank">&#9733;&#9733;&#9733;&#9733;&#9733;</a>'
412 );
413 }
414
415 return $footer_text;
416 }
417
418 /**
419 * Register dashboard widgets.
420 *
421 * Adds a new Elementor widgets to WordPress dashboard.
422 *
423 * Fired by `wp_dashboard_setup` action.
424 *
425 * @since 1.9.0
426 * @access public
427 */
428 public function register_dashboard_widgets() {
429 wp_add_dashboard_widget( 'e-dashboard-overview', esc_html__( 'Elementor Overview', 'elementor' ), [ $this, 'elementor_dashboard_overview_widget' ] );
430
431 // Move our widget to top.
432 global $wp_meta_boxes;
433
434 $dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
435 $ours = [
436 'e-dashboard-overview' => $dashboard['e-dashboard-overview'],
437 ];
438
439 $wp_meta_boxes['dashboard']['normal']['core'] = array_merge( $ours, $dashboard ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
440 }
441
442 /**
443 * Displays the Elementor dashboard widget.
444 *
445 * Fired by `wp_add_dashboard_widget` function.
446 *
447 * @since 1.9.0
448 * @access public
449 */
450 public function elementor_dashboard_overview_widget() {
451 ?>
452 <div class="e-dashboard-overview e-dashboard-widget">
453 <?php
454 self::elementor_dashboard_overview_header();
455 self::elementor_dashboard_overview_recently_edited();
456 self::elementor_dashboard_overview_news_updates();
457 self::elementor_dashboard_overview_footer();
458 ?>
459 </div>
460 <?php
461 }
462
463 /**
464 * Displays the Elementor dashboard widget - header section.
465 * Fired by `elementor_dashboard_overview_widget` function.
466 *
467 * @param bool $show_versions
468 * @param bool $is_create_post_enabled
469 *
470 * @return void
471 * @since 3.12.0
472 */
473 public static function elementor_dashboard_overview_header( bool $show_versions = true, bool $is_create_post_enabled = true ) {
474 if ( User::is_current_user_can_edit_post_type( 'page' ) ) {
475 $create_new_label = esc_html__( 'Create New Page', 'elementor' );
476 $create_new_post_type = 'page';
477 } elseif ( User::is_current_user_can_edit_post_type( 'post' ) ) {
478 $create_new_label = esc_html__( 'Create New Post', 'elementor' );
479 $create_new_post_type = 'post';
480 }
481 ?>
482 <div class="e-overview__header">
483 <?php if ( $show_versions ) { ?>
484 <div class="e-overview__logo">
485 <div class="e-logo-wrapper"><i class="eicon-elementor"></i></div>
486 </div>
487 <div class="e-overview__versions">
488 <span class="e-overview__version"><?php echo esc_html__( 'Elementor', 'elementor' ); ?> v<?php echo ELEMENTOR_VERSION; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
489 <?php
490 /**
491 * Elementor dashboard widget after the version.
492 * Fires after Elementor version display in the dashboard widget.
493 *
494 * @since 1.9.0
495 */
496 do_action( 'elementor/admin/dashboard_overview_widget/after_version' );
497 ?>
498 </div>
499 <?php } ?>
500 <?php if ( ! empty( $create_new_post_type ) && $is_create_post_enabled ) { ?>
501 <div class="e-overview__create">
502 <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>
503 </div>
504 <?php } ?>
505 </div>
506 <?php
507 }
508
509 /**
510 * Displays the Elementor dashboard widget - recently edited section.
511 * Fired by `elementor_dashboard_overview_widget` function.
512 *
513 * @param array $args
514 * @param bool $show_heading
515 *
516 * @return void
517 * @since 3.12.0
518 */
519 public static function elementor_dashboard_overview_recently_edited( array $args = [], bool $show_heading = true ) {
520 $recently_edited_query = Utils::get_recently_edited_posts_query( $args );
521
522 if ( $recently_edited_query->have_posts() ) { ?>
523 <div class="e-overview__recently-edited">
524 <?php if ( $show_heading ) { ?>
525 <h3 class="e-heading e-divider_bottom"><?php echo esc_html__( 'Recently Edited', 'elementor' ); ?></h3>
526 <?php } ?>
527 <ul class="e-overview__posts">
528 <?php
529 while ( $recently_edited_query->have_posts() ) {
530 $recently_edited_query->the_post();
531 $document = Plugin::$instance->documents->get( get_the_ID() );
532
533 $date = date_i18n( _x( 'M jS', 'Dashboard Overview Widget Recently Date', 'elementor' ), get_the_modified_time( 'U' ) );
534 ?>
535 <li class="e-overview__post">
536 <a href="<?php echo esc_url( $document->get_edit_url() ); ?>" class="e-overview__post-link"><?php echo esc_html( get_the_title() ); ?>
537 <span class="dashicons dashicons-edit"></span></a>
538 <span><?php echo $date; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>, <?php the_modified_time(); ?></span>
539 </li>
540 <?php } ?>
541 </ul>
542 </div>
543 <?php }
544 }
545
546 /**
547 * Displays the Elementor dashboard widget - news and updates section.
548 * Fired by `elementor_dashboard_overview_widget` function.
549 *
550 * @param int $limit_feed
551 * @param bool $show_heading
552 *
553 * @return void
554 * @since 3.12.0
555 * @access public
556 */
557 public static function elementor_dashboard_overview_news_updates( int $limit_feed = 0, bool $show_heading = true ) {
558 $elementor_feed = Api::get_feed_data();
559 if ( $limit_feed > 0 ) {
560 $elementor_feed = array_slice( $elementor_feed, 0, $limit_feed );
561 }
562
563 if ( ! empty( $elementor_feed ) ) { ?>
564 <div class="e-overview__feed">
565 <?php if ( $show_heading ) { ?>
566 <h3 class="e-heading e-divider_bottom"><?php echo esc_html__( 'News & Updates', 'elementor' ); ?></h3>
567 <?php } ?>
568 <ul class="e-overview__posts">
569 <?php foreach ( $elementor_feed as $feed_item ) { ?>
570 <li class="e-overview__post">
571 <a href="<?php echo esc_url( $feed_item['url'] ); ?>" class="e-overview__post-link" target="_blank">
572 <?php if ( ! empty( $feed_item['badge'] ) ) { ?>
573 <span class="e-overview__badge"><?php echo esc_html( $feed_item['badge'] ); ?></span>
574 <?php } ?>
575 <?php echo esc_html( $feed_item['title'] ); ?>
576 </a>
577 <p class="e-overview__post-description"><?php echo esc_html( $feed_item['excerpt'] ); ?></p>
578 </li>
579 <?php } ?>
580 </ul>
581 </div>
582 <?php }
583 }
584
585 /**
586 * Displays the Elementor dashboard widget - footer section.
587 * Fired by `elementor_dashboard_overview_widget` function.
588 *
589 * @since 3.12.0
590 */
591 public static function elementor_dashboard_overview_footer() {
592 ?>
593 <div class="e-overview__footer e-divider_top">
594 <ul>
595 <?php foreach ( self::static_get_dashboard_overview_widget_footer_actions() as $action_id => $action ) { ?>
596 <li class="e-overview__<?php echo esc_attr( $action_id ); ?>">
597 <a href="<?php echo esc_url( $action['link'] ); ?>" target="_blank"><?php echo esc_html( $action['title'] ); ?>
598 <span class="screen-reader-text"><?php echo esc_html__( '(opens in a new window)', 'elementor' ); ?></span>
599 <span aria-hidden="true" class="dashicons dashicons-external"></span></a>
600 </li>
601 <?php } ?>
602 </ul>
603 </div>
604 <?php
605 }
606
607 /**
608 * Get elementor dashboard overview widget footer actions.
609 *
610 * Retrieves the footer action links displayed in elementor dashboard widget.
611 *
612 * @since 3.12.0
613 * @access public
614 */
615 public static function static_get_dashboard_overview_widget_footer_actions() {
616 $base_actions = [
617 'blog' => [
618 'title' => esc_html__( 'Blog', 'elementor' ),
619 'link' => 'https://go.elementor.com/overview-widget-blog/',
620 ],
621 'help' => [
622 'title' => esc_html__( 'Help', 'elementor' ),
623 'link' => 'https://go.elementor.com/overview-widget-docs/',
624 ],
625 ];
626
627 $additions_actions = [];
628
629 if ( User::get_introduction_meta( 'ai_get_started' ) ) {
630 $additions_actions['ai-library'] = [
631 'title' => esc_html__( 'AI Prompts Library', 'elementor' ),
632 'link' => 'https://go.elementor.com/overview-ai-prompts-library/',
633 ];
634 } else {
635 $additions_actions['ai'] = [
636 'title' => esc_html__( 'Build Smart with AI', 'elementor' ),
637 'link' => 'https://go.elementor.com/overview-widget-ai/',
638 ];
639 }
640
641 $additions_actions['go-pro'] = [
642 'title' => esc_html__( 'Upgrade', 'elementor' ),
643 'link' => 'https://go.elementor.com/go-pro-wp-overview-widget/',
644 ];
645
646 /**
647 * Dashboard widget footer actions.
648 *
649 * Filters the additions actions displayed in Elementor dashboard widget.
650 *
651 * Developers can add new action links to Elementor dashboard widget
652 * footer using this filter.
653 *
654 * @since 1.9.0
655 *
656 * @param array $additions_actions Elementor dashboard widget footer actions.
657 */
658 $additions_actions = apply_filters( 'elementor/admin/dashboard_overview_widget/footer_actions', $additions_actions );
659
660 $actions = $base_actions + $additions_actions;
661
662 return $actions;
663 }
664
665 /**
666 * Get elementor dashboard overview widget footer actions.
667 *
668 * Retrieves the footer action links displayed in elementor dashboard widget.
669 *
670 * @since 1.9.0
671 * @access private
672 */
673 private function get_dashboard_overview_widget_footer_actions() {
674 return self::static_get_dashboard_overview_widget_footer_actions();
675 }
676
677 /**
678 * Admin action new post.
679 *
680 * When a new post action is fired the title is set to 'Elementor' and the post ID.
681 *
682 * Fired by `admin_action_elementor_new_post` action.
683 *
684 * @since 1.9.0
685 * @access public
686 */
687 public function admin_action_new_post() {
688 check_admin_referer( 'elementor_action_new_post' );
689
690 $post_type = Utils::get_super_global_value( $_GET, 'post_type' ) ?? 'post';
691
692 if ( ! User::is_current_user_can_edit_post_type( $post_type ) ) {
693 return;
694 }
695
696 if ( empty( $_GET['template_type'] ) ) {
697 $type = 'post';
698 } else {
699 $type = sanitize_text_field( wp_unslash( $_GET['template_type'] ) );
700 }
701
702 $post_data = Utils::get_super_global_value( $_GET, 'post_data' ) ?? [];
703
704 $post_data = $this->filter_post_data( $post_data );
705
706 /**
707 * Create new post meta data.
708 *
709 * Filters the meta data of any new post created.
710 *
711 * @since 2.0.0
712 *
713 * @param array $meta Post meta data.
714 */
715 $meta = [];
716
717 if ( isset( $_GET['meta'] ) && is_array( $_GET['meta'] ) ) {
718 $meta = array_map( 'sanitize_text_field', wp_unslash( $_GET['meta'] ) );
719 }
720
721 $meta = apply_filters( 'elementor/admin/create_new_post/meta', $meta );
722
723 $post_data['post_type'] = $post_type;
724
725 $document = Plugin::$instance->documents->create( $type, $post_data, $meta );
726
727 if ( is_wp_error( $document ) ) {
728 wp_die( $document ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
729 }
730
731 wp_redirect( $document->get_edit_url() );
732
733 die;
734 }
735
736 private function get_allowed_fields_for_role() {
737 $allowed_fields = array(
738 'post_title',
739 'post_content',
740 'post_excerpt',
741 'post_category',
742 'post_type',
743 'tags_input',
744 );
745
746 if ( current_user_can( 'publish_posts' ) ) {
747 $allowed_fields[] = 'post_status';
748 }
749
750 if ( current_user_can( 'edit_others_posts' ) ) {
751 $allowed_fields[] = 'post_author';
752 }
753
754 return $allowed_fields;
755 }
756
757 private function filter_post_data( $post_data ) {
758 $allowed_fields = $this->get_allowed_fields_for_role();
759 return array_filter(
760 $post_data,
761 function( $key ) use ( $allowed_fields ) {
762 return in_array( $key, $allowed_fields, true );
763 },
764 ARRAY_FILTER_USE_KEY
765 );
766 }
767 /**
768 * @since 2.3.0
769 * @access public
770 */
771 public function add_new_template_template() {
772 Plugin::$instance->common->add_template( ELEMENTOR_PATH . 'includes/admin-templates/new-template.php' );
773 }
774
775 public function add_new_floating_elements_template() {
776 Plugin::$instance->common->add_template( ELEMENTOR_PATH . 'includes/admin-templates/new-floating-elements.php' );
777 }
778
779 public function enqueue_new_floating_elements_scripts() {
780 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
781
782 wp_enqueue_script(
783 'elementor-floating-elements-modal',
784 ELEMENTOR_ASSETS_URL . 'js/floating-elements-modal' . $suffix . '.js',
785 [],
786 ELEMENTOR_VERSION,
787 true
788 );
789
790 wp_set_script_translations( 'elementor-floating-elements-modal', 'elementor' );
791 }
792
793 /**
794 * @access public
795 */
796 public function enqueue_new_template_scripts() {
797 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
798
799 wp_enqueue_script(
800 'elementor-new-template',
801 ELEMENTOR_ASSETS_URL . 'js/new-template' . $suffix . '.js',
802 [],
803 ELEMENTOR_VERSION,
804 true
805 );
806
807 wp_set_script_translations( 'elementor-new-template', 'elementor' );
808 }
809
810 /**
811 * @since 2.6.0
812 * @access public
813 */
814 public function add_beta_tester_template() {
815 Plugin::$instance->common->add_template( ELEMENTOR_PATH . 'includes/admin-templates/beta-tester.php' );
816 }
817
818 /**
819 * @access public
820 */
821 public function enqueue_beta_tester_scripts() {
822 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
823
824 wp_enqueue_script(
825 'elementor-beta-tester',
826 ELEMENTOR_ASSETS_URL . 'js/beta-tester' . $suffix . '.js',
827 [],
828 ELEMENTOR_VERSION,
829 true
830 );
831
832 wp_set_script_translations( 'elementor-beta-tester', 'elementor' );
833 }
834
835 public function init_floating_elements() {
836 $screens = [
837 'elementor_library_page_e-floating-buttons' => true,
838 'edit-e-floating-buttons' => true,
839 ];
840
841 if ( ! isset( $screens[ get_current_screen()->id ] ) ) {
842 return;
843 }
844
845 add_action( 'admin_head', [ $this, 'add_new_floating_elements_template' ] );
846 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_new_floating_elements_scripts' ] );
847 }
848
849 /**
850 * @access public
851 */
852 public function init_new_template() {
853 if ( 'edit-elementor_library' !== get_current_screen()->id ) {
854 return;
855 }
856
857 // Allow plugins to add their templates on admin_head.
858 add_action( 'admin_head', [ $this, 'add_new_template_template' ] );
859 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_new_template_scripts' ] );
860 }
861
862 public function version_update_warning( $current_version, $new_version ) {
863 $current_version_minor_part = explode( '.', $current_version )[1];
864 $new_version_minor_part = explode( '.', $new_version )[1];
865
866 if ( $current_version_minor_part === $new_version_minor_part ) {
867 return;
868 }
869 ?>
870 <hr class="e-major-update-warning__separator" />
871 <div class="e-major-update-warning">
872 <div class="e-major-update-warning__icon">
873 <i class="eicon-info-circle"></i>
874 </div>
875 <div>
876 <div class="e-major-update-warning__title">
877 <?php echo esc_html__( 'Heads up, Please backup before upgrade!', 'elementor' ); ?>
878 </div>
879 <div class="e-major-update-warning__message">
880 <?php
881 printf(
882 /* translators: %1$s Link open tag, %2$s: Link close tag. */
883 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' ),
884 '<a href="https://go.elementor.com/wp-dash-update-backup/">',
885 '</a>'
886 );
887 ?>
888 </div>
889 </div>
890 </div>
891 <?php
892 }
893
894 /**
895 * @access public
896 */
897 public function init_beta_tester( $current_screen ) {
898 if ( ( 'toplevel_page_elementor' === $current_screen->base ) || 'elementor_page_elementor-tools' === $current_screen->id ) {
899 add_action( 'admin_head', [ $this, 'add_beta_tester_template' ] );
900 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_beta_tester_scripts' ] );
901 }
902 }
903
904 /**
905 * Admin constructor.
906 *
907 * Initializing Elementor in WordPress admin.
908 *
909 * @since 1.0.0
910 * @access public
911 */
912 public function __construct() {
913 Plugin::$instance->init_common();
914
915 $this->add_component( 'feedback', new Feedback() );
916 $this->add_component( 'admin-notices', new Admin_Notices() );
917
918 add_action( 'admin_init', [ $this, 'maybe_redirect_to_getting_started' ] );
919
920 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
921 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] );
922
923 add_action( 'edit_form_after_title', [ $this, 'print_switch_mode_button' ] );
924 add_action( 'save_post', [ $this, 'save_post' ] );
925
926 add_filter( 'display_post_states', [ $this, 'add_elementor_post_state' ], 10, 2 );
927
928 add_filter( 'plugin_action_links_' . ELEMENTOR_PLUGIN_BASE, [ $this, 'plugin_action_links' ] );
929 add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 2 );
930
931 add_filter( 'admin_body_class', [ $this, 'body_status_classes' ] );
932 add_filter( 'admin_footer_text', [ $this, 'admin_footer_text' ] );
933
934 // Register Dashboard Widgets.
935 add_action( 'wp_dashboard_setup', [ $this, 'register_dashboard_widgets' ] );
936
937 // Admin Actions
938 add_action( 'admin_action_elementor_new_post', [ $this, 'admin_action_new_post' ] );
939
940 add_action( 'current_screen', [ $this, 'init_new_template' ] );
941 add_action( 'current_screen', [ $this, 'init_floating_elements' ] );
942 add_action( 'current_screen', [ $this, 'init_beta_tester' ] );
943
944 add_action( 'in_plugin_update_message-' . ELEMENTOR_PLUGIN_BASE, function( $plugin_data ) {
945 $this->version_update_warning( ELEMENTOR_VERSION, $plugin_data['new_version'] );
946 } );
947
948 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_hints' ] );
949 }
950
951 /**
952 * @since 2.3.0
953 * @access protected
954 */
955 protected function get_init_settings() {
956 $beta_tester_email = get_user_meta( get_current_user_id(), User::BETA_TESTER_META_KEY, true );
957 $elementor_beta = get_option( 'elementor_beta', 'no' );
958 $all_introductions = User::get_introduction_meta();
959 $beta_tester_signup_dismissed = array_key_exists( Beta_Testers::BETA_TESTER_SIGNUP, $all_introductions );
960
961 $settings = [
962 'home_url' => home_url(),
963 'settings_url' => Settings::get_url(),
964 'user' => [
965 'introduction' => User::get_introduction_meta(),
966 'restrictions' => Plugin::$instance->role_manager->get_user_restrictions_array(),
967 'is_administrator' => current_user_can( 'manage_options' ),
968 ],
969 'beta_tester' => [
970 'beta_tester_signup' => Beta_Testers::BETA_TESTER_SIGNUP,
971 'has_email' => $beta_tester_email,
972 'option_enabled' => 'no' !== $elementor_beta,
973 'signup_dismissed' => $beta_tester_signup_dismissed,
974 ],
975 'experiments' => $this->get_experiments(),
976 ];
977
978 /**
979 * Localize settings.
980 *
981 * Filters the initial localize settings in the admin.
982 *
983 * WordPress has it's own way to pass localized data from PHP (backend) to
984 * JS (frontend). Elementor uses this method to pass localize data in the
985 * admin. This hook can be used to add more localized settings in addition
986 * to the initial Elementor settings.
987 *
988 * @since 2.3.0
989 *
990 * @param array $settings Initial localize settings.
991 */
992 $settings = apply_filters( 'elementor/admin/localize_settings', $settings );
993
994 return $settings;
995 }
996
997 private function get_experiments() {
998 return ( new Collection( Plugin::$instance->experiments->get_features() ) )
999 ->map( function ( $experiment_data ) {
1000 $dependencies = $experiment_data['dependencies'] ?? [];
1001
1002 $dependencies = ( new Collection( $dependencies ) )
1003 ->map( function ( $dependency ) {
1004 return $dependency->get_name();
1005 } )->all();
1006
1007 return [
1008 'name' => $experiment_data['name'],
1009 'title' => $experiment_data['title'] ?? $experiment_data['name'],
1010 'state' => $experiment_data['state'],
1011 'default' => $experiment_data['default'],
1012 'dependencies' => $dependencies,
1013 'messages' => $experiment_data['messages'] ?? [],
1014 ];
1015 } )->all();
1016 }
1017
1018 private function maybe_enqueue_hints() {
1019 if ( ! Hints::should_display_hint( 'image-optimization' ) ) {
1020 return;
1021 }
1022
1023 wp_register_script(
1024 'media-hints',
1025 $this->get_js_assets_url( 'media-hints' ),
1026 [],
1027 ELEMENTOR_VERSION,
1028 true
1029 );
1030
1031 $content = sprintf("%1\$s <a class='e-btn-1' href='%2\$s' target='_blank'>%3\$s</a>!",
1032 __( 'Optimize your images to enhance site performance by using Image Optimizer.', 'elementor' ),
1033 Hints::get_plugin_action_url( 'image-optimization' ),
1034 ( Hints::is_plugin_installed( 'image-optimization' ) ? __( 'Activate', 'elementor' ) : __( 'Install', 'elementor' ) ) . ' ' . __( 'Image Optimizer', 'elementor' )
1035 );
1036
1037 $dismissible = 'image_optimizer_hint';
1038
1039 wp_localize_script( 'media-hints', 'elementorAdminHints', [
1040 'mediaHint' => [
1041 'display' => true,
1042 'type' => 'info',
1043 'content' => $content,
1044 'icon' => true,
1045 'dismissible' => $dismissible,
1046 'dismiss' => __( 'Dismiss this notice.', 'elementor' ),
1047 'button_event' => $dismissible,
1048 'button_data' => base64_encode(
1049 json_encode( [
1050 'action_url' => Hints::get_plugin_action_url( 'image-optimization' ),
1051 ] ),
1052 ),
1053 ],
1054 ] );
1055
1056 wp_enqueue_script( 'media-hints' );
1057 }
1058
1059 public function register_ajax_hints( $ajax_manager ) {
1060 $ajax_manager->register_ajax_action( 'elementor_image_optimization_campaign', [ $this, 'ajax_set_image_optimization_campaign' ] );
1061 $ajax_manager->register_ajax_action( 'elementor_core_site_mailer_campaign', [ $this, 'ajax_site_mailer_campaign' ] );
1062 }
1063
1064 public function ajax_set_image_optimization_campaign( $request ) {
1065 if ( ! current_user_can( 'install_plugins' ) ) {
1066 return;
1067 }
1068
1069 if ( empty( $request['source'] ) ) {
1070 return;
1071 }
1072
1073 $campaign_data = [
1074 'source' => sanitize_key( $request['source'] ),
1075 'campaign' => 'io-plg',
1076 'medium' => 'wp-dash',
1077 ];
1078
1079 set_transient( 'elementor_image_optimization_campaign', $campaign_data, 30 * DAY_IN_SECONDS );
1080 }
1081
1082 public function ajax_site_mailer_campaign( $request ) {
1083 if ( ! current_user_can( 'install_plugins' ) ) {
1084 return;
1085 }
1086
1087 if ( empty( $request['source'] ) ) {
1088 return;
1089 }
1090
1091 $campaign_data = [
1092 'source' => sanitize_key( $request['source'] ),
1093 'campaign' => 'sm-plg',
1094 'medium' => 'wp-dash',
1095 ];
1096
1097 set_transient( 'elementor_site_mailer_campaign', $campaign_data, 30 * DAY_IN_SECONDS );
1098 }
1099 }
1100