admin-menu-items
4 months ago
admin-menu-app.php
2 years ago
ajax.php
7 months ago
module.php
2 months ago
options.php
2 years ago
ajax.php
185 lines
| 1 | <?php |
| 2 | namespace Elementor\Modules\ElementManager; |
| 3 | |
| 4 | use Elementor\Core\Utils\Promotions\Filtered_Promotions_Manager; |
| 5 | use Elementor\Modules\Usage\Module as Usage_Module; |
| 6 | use Elementor\Api; |
| 7 | use Elementor\Plugin; |
| 8 | use Elementor\User; |
| 9 | use Elementor\Utils; |
| 10 | use Elementor\Core\Utils\Promotions\Validate_Promotion; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // Exit if accessed directly. |
| 14 | } |
| 15 | |
| 16 | class Ajax { |
| 17 | |
| 18 | const ELEMENT_MANAGER_PROMOTION_URL = 'https://go.elementor.com/go-pro-element-manager/'; |
| 19 | |
| 20 | const FREE_TO_PRO_PERMISSIONS_PROMOTION_URL = 'https://go.elementor.com/go-pro-element-manager-permissions/'; |
| 21 | |
| 22 | const PRO_TO_ADVANCED_PERMISSIONS_PROMOTION_URL = 'https://go.elementor.com/go-pro-advanced-element-manager-permissions/'; |
| 23 | |
| 24 | public function register_endpoints() { |
| 25 | add_action( 'wp_ajax_elementor_element_manager_get_admin_app_data', [ $this, 'ajax_get_admin_page_data' ] ); |
| 26 | add_action( 'wp_ajax_elementor_element_manager_save_disabled_elements', [ $this, 'ajax_save_disabled_elements' ] ); |
| 27 | add_action( 'wp_ajax_elementor_element_manager_get_widgets_usage', [ $this, 'ajax_get_widgets_usage' ] ); |
| 28 | } |
| 29 | |
| 30 | public function ajax_get_admin_page_data() { |
| 31 | $this->verify_permission(); |
| 32 | $this->force_enabled_all_elements(); |
| 33 | |
| 34 | $widgets = []; |
| 35 | $plugins = []; |
| 36 | |
| 37 | foreach ( Plugin::$instance->widgets_manager->get_widget_types() as $widget ) { |
| 38 | $widget_title = sanitize_user( $widget->get_title() ); |
| 39 | if ( empty( $widget_title ) || ! $widget->show_in_panel() ) { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | $plugin_name = $this->get_plugin_name_from_widget_instance( $widget ); |
| 44 | |
| 45 | if ( ! in_array( $plugin_name, $plugins ) ) { |
| 46 | $plugins[] = $plugin_name; |
| 47 | } |
| 48 | |
| 49 | $widgets[] = [ |
| 50 | 'name' => $widget->get_name(), |
| 51 | 'plugin' => $plugin_name, |
| 52 | 'title' => $widget_title, |
| 53 | 'icon' => $widget->get_icon(), |
| 54 | ]; |
| 55 | } |
| 56 | |
| 57 | $notice_id = 'e-element-manager-intro-1'; |
| 58 | |
| 59 | $data = [ |
| 60 | 'disabled_elements' => Options::get_disabled_elements(), |
| 61 | 'promotion_widgets' => [], |
| 62 | 'widgets' => $widgets, |
| 63 | 'plugins' => $plugins, |
| 64 | 'notice_data' => [ |
| 65 | 'notice_id' => $notice_id, |
| 66 | 'is_viewed' => User::is_user_notice_viewed( $notice_id ), |
| 67 | 'nonce' => wp_create_nonce( 'elementor_set_admin_notice_viewed' ), |
| 68 | ], |
| 69 | 'promotion_data' => [ |
| 70 | 'manager_permissions' => [ |
| 71 | 'pro' => $this->get_element_manager_promotion( |
| 72 | [ |
| 73 | 'text' => esc_html__( 'Upgrade Now', 'elementor' ), |
| 74 | 'url' => self::FREE_TO_PRO_PERMISSIONS_PROMOTION_URL, |
| 75 | ], |
| 76 | 'pro_permissions' |
| 77 | ), |
| 78 | 'advanced' => $this->get_element_manager_promotion( |
| 79 | [ |
| 80 | 'text' => esc_html__( 'Upgrade Now', 'elementor' ), |
| 81 | 'url' => self::PRO_TO_ADVANCED_PERMISSIONS_PROMOTION_URL, |
| 82 | ], |
| 83 | 'advanced_permissions' |
| 84 | ), |
| 85 | ], |
| 86 | 'element_manager' => $this->get_element_manager_promotion( |
| 87 | [ |
| 88 | 'text' => esc_html__( 'Upgrade Now', 'elementor' ), |
| 89 | 'url' => self::ELEMENT_MANAGER_PROMOTION_URL, |
| 90 | ], |
| 91 | 'element_manager' |
| 92 | ), |
| 93 | ], |
| 94 | ]; |
| 95 | |
| 96 | if ( ! Utils::has_pro() ) { |
| 97 | $data['promotion_widgets'] = Api::get_promotion_widgets(); |
| 98 | } |
| 99 | |
| 100 | $data['additional_data'] = apply_filters( 'elementor/element_manager/admin_app_data/additional_data', [] ); |
| 101 | |
| 102 | wp_send_json_success( $data ); |
| 103 | } |
| 104 | |
| 105 | private function get_element_manager_promotion( $promotion_data, $filter_id ): array { |
| 106 | |
| 107 | return Filtered_Promotions_Manager::get_filtered_promotion_data( $promotion_data, 'elementor/element_manager/admin_app_data/promotion_data/' . $filter_id, 'url' ); |
| 108 | } |
| 109 | |
| 110 | private function verify_permission() { |
| 111 | if ( ! current_user_can( 'manage_options' ) ) { |
| 112 | wp_send_json_error( esc_html__( 'You do not have permission to edit these settings.', 'elementor' ) ); |
| 113 | } |
| 114 | |
| 115 | $nonce = Utils::get_super_global_value( $_POST, 'nonce' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 116 | if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'e-element-manager-app' ) ) { |
| 117 | wp_send_json_error( esc_html__( 'Invalid nonce.', 'elementor' ) ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private function force_enabled_all_elements() { |
| 122 | remove_all_filters( 'elementor/widgets/is_widget_enabled' ); |
| 123 | } |
| 124 | |
| 125 | private function get_plugin_name_from_widget_instance( $widget ) { |
| 126 | if ( in_array( 'wordpress', $widget->get_categories() ) ) { // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText |
| 127 | return esc_html__( 'WordPress Widgets', 'elementor' ); |
| 128 | } |
| 129 | |
| 130 | $class_reflection = new \ReflectionClass( $widget ); |
| 131 | |
| 132 | $plugin_basename = plugin_basename( $class_reflection->getFileName() ); |
| 133 | |
| 134 | $plugin_directory = strtok( $plugin_basename, '/' ); |
| 135 | |
| 136 | $plugins_data = get_plugins( '/' . $plugin_directory ); |
| 137 | $plugin_data = array_shift( $plugins_data ); |
| 138 | |
| 139 | return $plugin_data['Name'] ?? esc_html__( 'Unknown', 'elementor' ); |
| 140 | } |
| 141 | |
| 142 | public function ajax_save_disabled_elements() { |
| 143 | $this->verify_permission(); |
| 144 | |
| 145 | $elements = Utils::get_super_global_value( $_POST, 'widgets' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 146 | |
| 147 | if ( empty( $elements ) ) { |
| 148 | wp_send_json_error( esc_html__( 'No elements to save.', 'elementor' ) ); |
| 149 | } |
| 150 | |
| 151 | $disabled_elements = json_decode( $elements ); |
| 152 | |
| 153 | if ( ! is_array( $disabled_elements ) ) { |
| 154 | wp_send_json_error( esc_html__( 'Unexpected elements data.', 'elementor' ) ); |
| 155 | } |
| 156 | |
| 157 | Options::update_disabled_elements( $disabled_elements ); |
| 158 | |
| 159 | do_action( 'elementor/element_manager/save_disabled_elements' ); |
| 160 | |
| 161 | wp_send_json_success(); |
| 162 | } |
| 163 | |
| 164 | public function ajax_get_widgets_usage() { |
| 165 | $this->verify_permission(); |
| 166 | |
| 167 | /** @var Usage_Module $usage_module */ |
| 168 | $usage_module = Usage_Module::instance(); |
| 169 | $usage_module->recalc_usage(); |
| 170 | |
| 171 | $widgets_usage = []; |
| 172 | foreach ( $usage_module->get_formatted_usage( 'raw' ) as $data ) { |
| 173 | foreach ( $data['elements'] as $element => $count ) { |
| 174 | if ( ! isset( $widgets_usage[ $element ] ) ) { |
| 175 | $widgets_usage[ $element ] = 0; |
| 176 | } |
| 177 | |
| 178 | $widgets_usage[ $element ] += $count; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | wp_send_json_success( $widgets_usage ); |
| 183 | } |
| 184 | } |
| 185 |