| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Components\Transformers; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\Elements\Base\Render_Context; |
| 6 |
use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver_Context; |
| 7 |
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base; |
| 8 |
use Elementor\Plugin; |
| 9 |
use Elementor\Core\Base\Document as Component_Document; |
| 10 |
use Elementor\Modules\Components\Components_Repository; |
| 11 |
use Elementor\Modules\Components\Utils\Format_Component_Elements_Id; |
| 12 |
use Elementor\Modules\Components\Widgets\Component_Instance; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
class Component_Instance_Transformer extends Transformer_Base { |
| 19 |
private static array $rendering_stack = []; |
| 20 |
private static $repository; |
| 21 |
|
| 22 |
public static function reset_rendering_stack(): void { |
| 23 |
self::$rendering_stack = []; |
| 24 |
} |
| 25 |
|
| 26 |
public function transform( $value, Props_Resolver_Context $context ) { |
| 27 |
$component_id = $value['component_id']; |
| 28 |
|
| 29 |
if ( $this->is_circular_reference( $component_id ) ) { |
| 30 |
return ''; |
| 31 |
} |
| 32 |
|
| 33 |
$instance_element_id = Render_Context::get( Component_Instance::class )['instance_id'] ?? ''; |
| 34 |
|
| 35 |
self::$rendering_stack[] = $component_id; |
| 36 |
$content = $this->get_rendered_content( $component_id, $instance_element_id ); |
| 37 |
array_pop( self::$rendering_stack ); |
| 38 |
|
| 39 |
return $content; |
| 40 |
} |
| 41 |
|
| 42 |
private function is_circular_reference( int $component_id ): bool { |
| 43 |
return in_array( $component_id, self::$rendering_stack, true ); |
| 44 |
} |
| 45 |
|
| 46 |
private function get_rendered_content( int $component_id, ?string $instance_element_id ): string { |
| 47 |
$should_show_autosave = is_preview(); |
| 48 |
$component = $this->get_repository()->get( $component_id, $should_show_autosave ); |
| 49 |
|
| 50 |
if ( ! $component || ! $this->should_render_content( $component ) ) { |
| 51 |
return ''; |
| 52 |
} |
| 53 |
|
| 54 |
Plugin::$instance->documents->switch_to_document( $component ); |
| 55 |
|
| 56 |
$data = $component->get_elements_data(); |
| 57 |
|
| 58 |
$data = apply_filters( 'elementor/frontend/builder_content_data', $data, $component_id ); |
| 59 |
|
| 60 |
$data = Format_Component_Elements_Id::format( $data, [ $instance_element_id ] ); |
| 61 |
|
| 62 |
$content = ''; |
| 63 |
|
| 64 |
if ( ! empty( $data ) ) { |
| 65 |
ob_start(); |
| 66 |
|
| 67 |
$component->print_elements_without_cache( $data ); |
| 68 |
|
| 69 |
$content = ob_get_clean(); |
| 70 |
|
| 71 |
$content = apply_filters( 'elementor/frontend/the_content', $content ); |
| 72 |
} |
| 73 |
|
| 74 |
Plugin::$instance->documents->restore_document(); |
| 75 |
|
| 76 |
return $content; |
| 77 |
} |
| 78 |
|
| 79 |
private function should_render_content( Component_Document $document ): bool { |
| 80 |
return ! $this->is_password_protected( $document ) && |
| 81 |
$document->is_built_with_elementor(); |
| 82 |
} |
| 83 |
|
| 84 |
private function is_password_protected( $document ) { |
| 85 |
return post_password_required( $document->get_post()->ID ); |
| 86 |
} |
| 87 |
|
| 88 |
private function get_repository(): Components_Repository { |
| 89 |
if ( ! self::$repository ) { |
| 90 |
self::$repository = new Components_Repository(); |
| 91 |
} |
| 92 |
|
| 93 |
return self::$repository; |
| 94 |
} |
| 95 |
} |
| 96 |
|