PluginProbe
Elementor Website Builder – more than just a page builder / 3.18.2
Elementor Website Builder – more than just a page builder v3.18.2
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 / modules / element-manager / ajax.php

ajax.php in Elementor Website Builder – more than just a page builder 3.18.2, at modules/element-manager/ajax.php

142 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\ElementManager;
3
4 use Elementor\Modules\Usage\Module as Usage_Module;
5 use Elementor\Api;
6 use Elementor\Plugin;
7 use Elementor\User;
8 use Elementor\Utils;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 class Ajax {
15
16 public function register_endpoints() {
17 add_action( 'wp_ajax_elementor_element_manager_get_admin_app_data', [ $this, 'ajax_get_admin_page_data' ] );
18 add_action( 'wp_ajax_elementor_element_manager_save_disabled_elements', [ $this, 'ajax_save_disabled_elements' ] );
19 add_action( 'wp_ajax_elementor_element_manager_get_widgets_usage', [ $this, 'ajax_get_widgets_usage' ] );
20 }
21
22 public function ajax_get_admin_page_data() {
23 $this->verify_permission();
24 $this->force_enabled_all_elements();
25
26 $widgets = [];
27 $plugins = [];
28
29 foreach ( Plugin::$instance->widgets_manager->get_widget_types() as $widget ) {
30 $widget_title = sanitize_user( $widget->get_title() );
31 if ( empty( $widget_title ) || ! $widget->show_in_panel() ) {
32 continue;
33 }
34
35 $plugin_name = $this->get_plugin_name_from_widget_instance( $widget );
36
37 if ( ! in_array( $plugin_name, $plugins ) ) {
38 $plugins[] = $plugin_name;
39 }
40
41 $widgets[] = [
42 'name' => $widget->get_name(),
43 'plugin' => $plugin_name,
44 'title' => $widget_title,
45 'icon' => $widget->get_icon(),
46 ];
47 }
48
49 $notice_id = 'e-element-manager-intro-1';
50
51 $data = [
52 'disabled_elements' => Options::get_disabled_elements(),
53 'promotion_widgets' => [],
54 'widgets' => $widgets,
55 'plugins' => $plugins,
56 'notice_data' => [
57 'notice_id' => $notice_id,
58 'is_viewed' => User::is_user_notice_viewed( $notice_id ),
59 ],
60 ];
61
62 if ( ! Utils::has_pro() ) {
63 $data['promotion_widgets'] = Api::get_promotion_widgets();
64 }
65
66 wp_send_json_success( $data );
67 }
68
69 private function verify_permission() {
70 if ( ! current_user_can( 'manage_options' ) ) {
71 wp_send_json_error( esc_html__( 'You do not have permission to edit these settings.', 'elementor' ) );
72 }
73
74 $nonce = Utils::get_super_global_value( $_POST, 'nonce' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
75 if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'e-element-manager-app' ) ) {
76 wp_send_json_error( esc_html__( 'Invalid nonce.', 'elementor' ) );
77 }
78 }
79
80 private function force_enabled_all_elements() {
81 remove_all_filters( 'elementor/widgets/is_widget_enabled' );
82 }
83
84 private function get_plugin_name_from_widget_instance( $widget ) {
85 if ( in_array( 'wordpress', $widget->get_categories() ) ) {
86 return esc_html__( 'WordPress Widgets', 'elementor' );
87 }
88
89 $class_reflection = new \ReflectionClass( $widget );
90
91 $plugin_basename = plugin_basename( $class_reflection->getFileName() );
92
93 $plugin_directory = strtok( $plugin_basename, '/' );
94
95 $plugins_data = get_plugins( '/' . $plugin_directory );
96 $plugin_data = array_shift( $plugins_data );
97
98 return $plugin_data['Name'] ?? esc_html__( 'Unknown', 'elementor' );
99 }
100
101 public function ajax_save_disabled_elements() {
102 $this->verify_permission();
103
104 $elements = Utils::get_super_global_value( $_POST, 'widgets' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
105
106 if ( empty( $elements ) ) {
107 wp_send_json_error( esc_html__( 'No elements to save.', 'elementor' ) );
108 }
109
110 $disabled_elements = json_decode( $elements );
111
112 if ( ! is_array( $disabled_elements ) ) {
113 wp_send_json_error( esc_html__( 'Unexpected elements data.', 'elementor' ) );
114 }
115
116 Options::update_disabled_elements( $disabled_elements );
117
118 wp_send_json_success();
119 }
120
121 public function ajax_get_widgets_usage() {
122 $this->verify_permission();
123
124 /** @var Usage_Module $usage_module */
125 $usage_module = Usage_Module::instance();
126 $usage_module->recalc_usage();
127
128 $widgets_usage = [];
129 foreach ( $usage_module->get_formatted_usage( 'raw' ) as $data ) {
130 foreach ( $data['elements'] as $element => $count ) {
131 if ( ! isset( $widgets_usage[ $element ] ) ) {
132 $widgets_usage[ $element ] = 0;
133 }
134
135 $widgets_usage[ $element ] += $count;
136 }
137 }
138
139 wp_send_json_success( $widgets_usage );
140 }
141 }
142