public function ajax_render_widget()
{
if (empty($_POST['_nonce']) || !wp_verify_nonce($_POST['_nonce'], 'elementor-editing')) {
wp_send_json_error(new \WP_Error('token_expired'));
}
if (empty($_POST['post_id'])) {
wp_send_json_error(new \WP_Error('no_post_id', 'No post_id'));
}
if (!User::is_current_user_can_edit($_POST['post_id'])) {
wp_send_json_error(new \WP_Error('no_access'));
}
// Override the global $post for the render
$GLOBALS['post'] = get_post((int) $_POST['post_id']);
$data = json_decode(stripslashes(html_entity_decode($_POST['data'])), true);
// Start buffering
ob_start();
/** @var Widget_Base|Widget_WordPress $widget_type */
$widget_type = $this->get_widget_types($data['widgetType']);
$widget_class = $widget_type->get_class_name();
/** @var Widget_Base $widget */
$widget = new $widget_class($data, $widget_type->get_default_args());
$widget->render_content();
$render_html = ob_get_clean();
wp_send_json_success(['render' => $render_html]);
}