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

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