| 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 |
$data['additional_data'] = apply_filters( 'elementor/element_manager/admin_app_data/additional_data', [] ); |
| 67 |
|
| 68 |
wp_send_json_success( $data ); |
| 69 |
} |
| 70 |
|
| 71 |
private function verify_permission() { |
| 72 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 73 |
wp_send_json_error( esc_html__( 'You do not have permission to edit these settings.', 'elementor' ) ); |
| 74 |
} |
| 75 |
|
| 76 |
$nonce = Utils::get_super_global_value( $_POST, 'nonce' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 77 |
if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'e-element-manager-app' ) ) { |
| 78 |
wp_send_json_error( esc_html__( 'Invalid nonce.', 'elementor' ) ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
private function force_enabled_all_elements() { |
| 83 |
remove_all_filters( 'elementor/widgets/is_widget_enabled' ); |
| 84 |
} |
| 85 |
|
| 86 |
private function get_plugin_name_from_widget_instance( $widget ) { |
| 87 |
if ( in_array( 'wordpress', $widget->get_categories() ) ) { |
| 88 |
return esc_html__( 'WordPress Widgets', 'elementor' ); |
| 89 |
} |
| 90 |
|
| 91 |
$class_reflection = new \ReflectionClass( $widget ); |
| 92 |
|
| 93 |
$plugin_basename = plugin_basename( $class_reflection->getFileName() ); |
| 94 |
|
| 95 |
$plugin_directory = strtok( $plugin_basename, '/' ); |
| 96 |
|
| 97 |
$plugins_data = get_plugins( '/' . $plugin_directory ); |
| 98 |
$plugin_data = array_shift( $plugins_data ); |
| 99 |
|
| 100 |
return $plugin_data['Name'] ?? esc_html__( 'Unknown', 'elementor' ); |
| 101 |
} |
| 102 |
|
| 103 |
public function ajax_save_disabled_elements() { |
| 104 |
$this->verify_permission(); |
| 105 |
|
| 106 |
$elements = Utils::get_super_global_value( $_POST, 'widgets' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 107 |
|
| 108 |
if ( empty( $elements ) ) { |
| 109 |
wp_send_json_error( esc_html__( 'No elements to save.', 'elementor' ) ); |
| 110 |
} |
| 111 |
|
| 112 |
$disabled_elements = json_decode( $elements ); |
| 113 |
|
| 114 |
if ( ! is_array( $disabled_elements ) ) { |
| 115 |
wp_send_json_error( esc_html__( 'Unexpected elements data.', 'elementor' ) ); |
| 116 |
} |
| 117 |
|
| 118 |
Options::update_disabled_elements( $disabled_elements ); |
| 119 |
|
| 120 |
do_action( 'elementor/element_manager/save_disabled_elements' ); |
| 121 |
|
| 122 |
wp_send_json_success(); |
| 123 |
} |
| 124 |
|
| 125 |
public function ajax_get_widgets_usage() { |
| 126 |
$this->verify_permission(); |
| 127 |
|
| 128 |
/** @var Usage_Module $usage_module */ |
| 129 |
$usage_module = Usage_Module::instance(); |
| 130 |
$usage_module->recalc_usage(); |
| 131 |
|
| 132 |
$widgets_usage = []; |
| 133 |
foreach ( $usage_module->get_formatted_usage( 'raw' ) as $data ) { |
| 134 |
foreach ( $data['elements'] as $element => $count ) { |
| 135 |
if ( ! isset( $widgets_usage[ $element ] ) ) { |
| 136 |
$widgets_usage[ $element ] = 0; |
| 137 |
} |
| 138 |
|
| 139 |
$widgets_usage[ $element ] += $count; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
wp_send_json_success( $widgets_usage ); |
| 144 |
} |
| 145 |
} |
| 146 |
|