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