PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0-beta1
Elementor Website Builder – more than just a page builder v4.2.0-beta1
4.3.0-beta3 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 All 452 releases
elementor / modules / atomic-widgets / ajax / render-element-action.php

render-element-action.php in Elementor Website Builder – more than just a page builder 4.2.0-beta1, at modules/atomic-widgets/ajax/render-element-action.php

64 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\AtomicWidgets\Ajax;
4
5 use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
6 use Elementor\Plugin;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 class Render_Element_Action {
13 const ACTION = 'render_atomic_element';
14
15 public function register( Ajax $ajax ): void {
16 $ajax->register_ajax_action( self::ACTION, fn ( $request ) => $this->handle( $request ) );
17 }
18
19 public function handle( $request ): array {
20 $post_id = isset( $request['editor_post_id'] ) ? (int) $request['editor_post_id'] : 0;
21 $element_data = $request['data'] ?? null;
22
23 if ( ! $post_id || ! is_array( $element_data ) ) {
24 throw new \Exception( 'Invalid request payload.' );
25 }
26
27 $document = Plugin::$instance->documents->get_with_permissions( $post_id );
28
29 $editor = Plugin::$instance->editor;
30 $is_edit_mode = $editor->is_edit_mode();
31 $editor->set_edit_mode( true );
32
33 Plugin::$instance->db->switch_to_query( [
34 'p' => $post_id,
35 'post_type' => 'any',
36 ], true );
37
38 Plugin::$instance->documents->switch_to_document( $document );
39
40 try {
41 $render_html = $this->render_element( $element_data );
42 } finally {
43 $editor->set_edit_mode( $is_edit_mode );
44 Plugin::$instance->db->restore_current_query();
45 }
46
47 return [
48 'render' => $render_html,
49 ];
50 }
51
52 private function render_element( array $element_data ): string {
53 $element = Plugin::$instance->elements_manager->create_element_instance( $element_data );
54
55 if ( ! $element ) {
56 throw new \Exception( 'Element could not be instantiated.' );
57 }
58
59 ob_start();
60 $element->print_element();
61 return (string) ob_get_clean();
62 }
63 }
64