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

1,091 lines 31.9 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 $additions_actions['ai'] = [
629 'title' => esc_html__( 'Build Smart with AI', 'elementor' ),
630 'link' => 'https://go.elementor.com/overview-widget-ai/',
631 ];
632 $additions_actions['go-pro'] = [
633 'title' => esc_html__( 'Upgrade', 'elementor' ),
634 'link' => 'https://go.elementor.com/go-pro-wp-overview-widget/',
635 ];
636
637 /**
638 * Dashboard widget footer actions.
639 *
640 * Filters the additions actions displayed in Elementor dashboard widget.
641 *
642 * Developers can add new action links to Elementor dashboard widget
643 * footer using this filter.
644 *
645 * @since 1.9.0
646 *
647 * @param array $additions_actions Elementor dashboard widget footer actions.
648 */
649 $additions_actions = apply_filters( 'elementor/admin/dashboard_overview_widget/footer_actions', $additions_actions );
650
651 $actions = $base_actions + $additions_actions;
652
653 return $actions;
654 }
655
656 /**
657 * Get elementor dashboard overview widget footer actions.
658 *
659 * Retrieves the footer action links displayed in elementor dashboard widget.
660 *
661 * @since 1.9.0
662 * @access private
663 */
664 private function get_dashboard_overview_widget_footer_actions() {
665 return self::static_get_dashboard_overview_widget_footer_actions();
666 }
667
668 /**
669 * Admin action new post.
670 *
671 * When a new post action is fired the title is set to 'Elementor' and the post ID.
672 *
673 * Fired by `admin_action_elementor_new_post` action.
674 *
675 * @since 1.9.0
676 * @access public
677 */
678 public function admin_action_new_post() {
679 check_admin_referer( 'elementor_action_new_post' );
680
681 $post_type = Utils::get_super_global_value( $_GET, 'post_type' ) ?? 'post';
682
683 if ( ! User::is_current_user_can_edit_post_type( $post_type ) ) {
684 return;
685 }
686
687 if ( empty( $_GET['template_type'] ) ) {
688 $type = 'post';
689 } else {
690 $type = sanitize_text_field( wp_unslash( $_GET['template_type'] ) );
691 }
692
693 $post_data = Utils::get_super_global_value( $_GET, 'post_data' ) ?? [];
694
695 $post_data = $this->filter_post_data( $post_data );
696
697 /**
698 * Create new post meta data.
699 *
700 * Filters the meta data of any new post created.
701 *
702 * @since 2.0.0
703 *
704 * @param array $meta Post meta data.
705 */
706 $meta = [];
707
708 if ( isset( $_GET['meta'] ) && is_array( $_GET['meta'] ) ) {
709 $meta = array_map( 'sanitize_text_field', wp_unslash( $_GET['meta'] ) );
710 }
711
712 $meta = apply_filters( 'elementor/admin/create_new_post/meta', $meta );
713
714 $post_data['post_type'] = $post_type;
715
716 $document = Plugin::$instance->documents->create( $type, $post_data, $meta );
717
718 if ( is_wp_error( $document ) ) {
719 wp_die( $document ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
720 }
721
722 wp_redirect( $document->get_edit_url() );
723
724 die;
725 }
726
727 private function get_allowed_fields_for_role() {
728 $allowed_fields = [
729 'post_title',
730 'post_content',
731 'post_excerpt',
732 'post_category',
733 'post_type',
734 'tags_input',
735 ];
736
737 if ( current_user_can( 'publish_posts' ) ) {
738 $allowed_fields[] = 'post_status';
739 }
740
741 if ( current_user_can( 'edit_others_posts' ) ) {
742 $allowed_fields[] = 'post_author';
743 }
744
745 return $allowed_fields;
746 }
747
748 private function filter_post_data( $post_data ) {
749 $allowed_fields = $this->get_allowed_fields_for_role();
750 return array_filter(
751 $post_data,
752 function( $key ) use ( $allowed_fields ) {
753 return in_array( $key, $allowed_fields, true );
754 },
755 ARRAY_FILTER_USE_KEY
756 );
757 }
758 /**
759 * @since 2.3.0
760 * @access public
761 */
762 public function add_new_template_template() {
763 Plugin::$instance->common->add_template( ELEMENTOR_PATH . 'includes/admin-templates/new-template.php' );
764 }
765
766 public function add_new_floating_elements_template() {
767 Plugin::$instance->common->add_template( ELEMENTOR_PATH . 'includes/admin-templates/new-floating-elements.php' );
768 }
769
770 public function enqueue_new_floating_elements_scripts() {
771 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
772
773 wp_enqueue_script(
774 'elementor-floating-elements-modal',
775 ELEMENTOR_ASSETS_URL . 'js/floating-elements-modal' . $suffix . '.js',
776 [],
777 ELEMENTOR_VERSION,
778 true
779 );
780
781 wp_set_script_translations( 'elementor-floating-elements-modal', 'elementor' );
782 }
783
784 /**
785 * @access public
786 */
787 public function enqueue_new_template_scripts() {
788 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
789
790 wp_enqueue_script(
791 'elementor-new-template',
792 ELEMENTOR_ASSETS_URL . 'js/new-template' . $suffix . '.js',
793 [],
794 ELEMENTOR_VERSION,
795 true
796 );
797
798 wp_set_script_translations( 'elementor-new-template', 'elementor' );
799 }
800
801 /**
802 * @since 2.6.0
803 * @access public
804 */
805 public function add_beta_tester_template() {
806 Plugin::$instance->common->add_template( ELEMENTOR_PATH . 'includes/admin-templates/beta-tester.php' );
807 }
808
809 /**
810 * @access public
811 */
812 public function enqueue_beta_tester_scripts() {
813 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
814
815 wp_enqueue_script(
816 'elementor-beta-tester',
817 ELEMENTOR_ASSETS_URL . 'js/beta-tester' . $suffix . '.js',
818 [],
819 ELEMENTOR_VERSION,
820 true
821 );
822
823 wp_set_script_translations( 'elementor-beta-tester', 'elementor' );
824 }
825
826 public function init_floating_elements() {
827 $screens = [
828 'elementor_library_page_e-floating-buttons' => true,
829 'edit-e-floating-buttons' => true,
830 ];
831
832 if ( ! isset( $screens[ get_current_screen()->id ] ) ) {
833 return;
834 }
835
836 add_action( 'admin_head', [ $this, 'add_new_floating_elements_template' ] );
837 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_new_floating_elements_scripts' ] );
838 }
839
840 /**
841 * @access public
842 */
843 public function init_new_template() {
844 if ( 'edit-elementor_library' !== get_current_screen()->id ) {
845 return;
846 }
847
848 // Allow plugins to add their templates on admin_head.
849 add_action( 'admin_head', [ $this, 'add_new_template_template' ] );
850 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_new_template_scripts' ] );
851 }
852
853 public function version_update_warning( $current_version, $new_version ) {
854 $current_version_minor_part = explode( '.', $current_version )[1];
855 $new_version_minor_part = explode( '.', $new_version )[1];
856
857 if ( $current_version_minor_part === $new_version_minor_part ) {
858 return;
859 }
860 ?>
861 <hr class="e-major-update-warning__separator" />
862 <div class="e-major-update-warning">
863 <div class="e-major-update-warning__icon">
864 <i class="eicon-info-circle"></i>
865 </div>
866 <div>
867 <div class="e-major-update-warning__title">
868 <?php echo esc_html__( 'Heads up, Please backup before upgrade!', 'elementor' ); ?>
869 </div>
870 <div class="e-major-update-warning__message">
871 <?php
872 printf(
873 /* translators: %1$s Link open tag, %2$s: Link close tag. */
874 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' ),
875 '<a href="https://go.elementor.com/wp-dash-update-backup/">',
876 '</a>'
877 );
878 ?>
879 </div>
880 </div>
881 </div>
882 <?php
883 }
884
885 /**
886 * @access public
887 */
888 public function init_beta_tester( $current_screen ) {
889 if ( ( 'toplevel_page_elementor' === $current_screen->base ) || 'elementor_page_elementor-tools' === $current_screen->id ) {
890 add_action( 'admin_head', [ $this, 'add_beta_tester_template' ] );
891 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_beta_tester_scripts' ] );
892 }
893 }
894
895 /**
896 * Admin constructor.
897 *
898 * Initializing Elementor in WordPress admin.
899 *
900 * @since 1.0.0
901 * @access public
902 */
903 public function __construct() {
904 Plugin::$instance->init_common();
905
906 $this->add_component( 'feedback', new Feedback() );
907 $this->add_component( 'admin-notices', new Admin_Notices() );
908
909 add_action( 'admin_init', [ $this, 'maybe_redirect_to_getting_started' ] );
910
911 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
912 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] );
913
914 add_action( 'edit_form_after_title', [ $this, 'print_switch_mode_button' ] );
915 add_action( 'save_post', [ $this, 'save_post' ] );
916
917 add_filter( 'display_post_states', [ $this, 'add_elementor_post_state' ], 10, 2 );
918
919 add_filter( 'plugin_action_links_' . ELEMENTOR_PLUGIN_BASE, [ $this, 'plugin_action_links' ] );
920 add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 2 );
921
922 add_filter( 'admin_body_class', [ $this, 'body_status_classes' ] );
923 add_filter( 'admin_footer_text', [ $this, 'admin_footer_text' ] );
924
925 // Register Dashboard Widgets.
926 add_action( 'wp_dashboard_setup', [ $this, 'register_dashboard_widgets' ] );
927
928 // Admin Actions
929 add_action( 'admin_action_elementor_new_post', [ $this, 'admin_action_new_post' ] );
930
931 add_action( 'current_screen', [ $this, 'init_new_template' ] );
932 add_action( 'current_screen', [ $this, 'init_floating_elements' ] );
933 add_action( 'current_screen', [ $this, 'init_beta_tester' ] );
934
935 add_action( 'in_plugin_update_message-' . ELEMENTOR_PLUGIN_BASE, function( $plugin_data ) {
936 $this->version_update_warning( ELEMENTOR_VERSION, $plugin_data['new_version'] );
937 } );
938
939 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_hints' ] );
940 }
941
942 /**
943 * @since 2.3.0
944 * @access protected
945 */
946 protected function get_init_settings() {
947 $beta_tester_email = get_user_meta( get_current_user_id(), User::BETA_TESTER_META_KEY, true );
948 $elementor_beta = get_option( 'elementor_beta', 'no' );
949 $all_introductions = User::get_introduction_meta();
950 $beta_tester_signup_dismissed = array_key_exists( Beta_Testers::BETA_TESTER_SIGNUP, $all_introductions );
951
952 $settings = [
953 'home_url' => home_url(),
954 'settings_url' => Settings::get_url(),
955 'user' => [
956 'introduction' => User::get_introduction_meta(),
957 'restrictions' => Plugin::$instance->role_manager->get_user_restrictions_array(),
958 'is_administrator' => current_user_can( 'manage_options' ),
959 ],
960 'beta_tester' => [
961 'beta_tester_signup' => Beta_Testers::BETA_TESTER_SIGNUP,
962 'has_email' => $beta_tester_email,
963 'option_enabled' => 'no' !== $elementor_beta,
964 'signup_dismissed' => $beta_tester_signup_dismissed,
965 ],
966 'experiments' => $this->get_experiments(),
967 ];
968
969 /**
970 * Localize settings.
971 *
972 * Filters the initial localize settings in the admin.
973 *
974 * WordPress has it's own way to pass localized data from PHP (backend) to
975 * JS (frontend). Elementor uses this method to pass localize data in the
976 * admin. This hook can be used to add more localized settings in addition
977 * to the initial Elementor settings.
978 *
979 * @since 2.3.0
980 *
981 * @param array $settings Initial localize settings.
982 */
983 $settings = apply_filters( 'elementor/admin/localize_settings', $settings );
984
985 return $settings;
986 }
987
988 private function get_experiments() {
989 return ( new Collection( Plugin::$instance->experiments->get_features() ) )
990 ->map( function ( $experiment_data ) {
991 $dependencies = $experiment_data['dependencies'] ?? [];
992
993 $dependencies = ( new Collection( $dependencies ) )
994 ->map( function ( $dependency ) {
995 return $dependency->get_name();
996 } )->all();
997
998 return [
999 'name' => $experiment_data['name'],
1000 'title' => $experiment_data['title'] ?? $experiment_data['name'],
1001 'state' => $experiment_data['state'],
1002 'default' => $experiment_data['default'],
1003 'dependencies' => $dependencies,
1004 'messages' => $experiment_data['messages'] ?? [],
1005 ];
1006 } )->all();
1007 }
1008
1009 private function maybe_enqueue_hints() {
1010 if ( ! Hints::should_display_hint( 'image-optimization' ) ) {
1011 return;
1012 }
1013
1014 wp_register_script(
1015 'media-hints',
1016 $this->get_js_assets_url( 'media-hints' ),
1017 [],
1018 ELEMENTOR_VERSION,
1019 true
1020 );
1021
1022 $content = sprintf("%1\$s <a class='e-btn-1' href='%2\$s' target='_blank'>%3\$s</a>!",
1023 __( 'Optimize your images to enhance site performance by using Image Optimizer.', 'elementor' ),
1024 Hints::get_plugin_action_url( 'image-optimization' ),
1025 ( Hints::is_plugin_installed( 'image-optimization' ) ? __( 'Activate', 'elementor' ) : __( 'Install', 'elementor' ) ) . ' ' . __( 'Image Optimizer', 'elementor' )
1026 );
1027
1028 $dismissible = 'image_optimizer_hint';
1029
1030 wp_localize_script( 'media-hints', 'elementorAdminHints', [
1031 'mediaHint' => [
1032 'display' => true,
1033 'type' => 'info',
1034 'content' => $content,
1035 'icon' => true,
1036 'dismissible' => $dismissible,
1037 'dismiss' => __( 'Dismiss this notice.', 'elementor' ),
1038 'button_event' => $dismissible,
1039 'button_data' => base64_encode(
1040 json_encode( [
1041 'action_url' => Hints::get_plugin_action_url( 'image-optimization' ),
1042 ] ),
1043 ),
1044 ],
1045 ] );
1046
1047 wp_enqueue_script( 'media-hints' );
1048 }
1049
1050 public function register_ajax_hints( $ajax_manager ) {
1051 $ajax_manager->register_ajax_action( 'elementor_image_optimization_campaign', [ $this, 'ajax_set_image_optimization_campaign' ] );
1052 $ajax_manager->register_ajax_action( 'elementor_core_site_mailer_campaign', [ $this, 'ajax_site_mailer_campaign' ] );
1053 }
1054
1055 public function ajax_set_image_optimization_campaign( $request ) {
1056 if ( ! current_user_can( 'install_plugins' ) ) {
1057 return;
1058 }
1059
1060 if ( empty( $request['source'] ) ) {
1061 return;
1062 }
1063
1064 $campaign_data = [
1065 'source' => sanitize_key( $request['source'] ),
1066 'campaign' => 'io-plg',
1067 'medium' => 'wp-dash',
1068 ];
1069
1070 set_transient( 'elementor_image_optimization_campaign', $campaign_data, 30 * DAY_IN_SECONDS );
1071 }
1072
1073 public function ajax_site_mailer_campaign( $request ) {
1074 if ( ! current_user_can( 'install_plugins' ) ) {
1075 return;
1076 }
1077
1078 if ( empty( $request['source'] ) ) {
1079 return;
1080 }
1081
1082 $campaign_data = [
1083 'source' => sanitize_key( $request['source'] ),
1084 'campaign' => 'sm-plg',
1085 'medium' => 'wp-dash',
1086 ];
1087
1088 set_transient( 'elementor_site_mailer_campaign', $campaign_data, 30 * DAY_IN_SECONDS );
1089 }
1090 }
1091