← All changes
|
modules/components/transformers/component-instance-transformer.php
+14
-49
4.2.3
→
3.34.1
View file →
| @@ -1,71 +1,45 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Components\Transformers; |
| 4 | 4 | |
| 5 | -use Elementor\Modules\AtomicWidgets\Elements\Base\Render_Context; | |
| 6 | 5 | use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver_Context; |
| 7 | 6 | use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base; |
| 7 | +use Elementor\Modules\Components\Documents\Component; | |
| 8 | 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 | 9 | |
| 10 | + | |
| 14 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | 12 | exit; // Exit if accessed directly. |
| 16 | 13 | } |
| 17 | 14 | |
| 18 | 15 | 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 | 16 | public function transform( $value, Props_Resolver_Context $context ) { |
| 27 | 17 | $component_id = $value['component_id']; |
| 28 | 18 | |
| 29 | - if ( $this->is_circular_reference( $component_id ) ) { | |
| 30 | - return ''; | |
| 31 | - } | |
| 19 | + $document = Plugin::$instance->documents->get_doc_for_frontend( $component_id ); | |
| 32 | 20 | |
| 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() || Plugin::$instance->editor->is_edit_mode(); | |
| 48 | - $component = $this->get_repository()->get( $component_id, $should_show_autosave ); | |
| 49 | - | |
| 50 | - if ( ! $component || ! $this->should_render_content( $component ) ) { | |
| 21 | + if ( | |
| 22 | + ! $document || | |
| 23 | + $this->is_password_protected( $document ) || | |
| 24 | + ! $this->is_component( $document ) || | |
| 25 | + ! $document->is_built_with_elementor() | |
| 26 | + ) { | |
| 51 | 27 | return ''; |
| 52 | 28 | } |
| 53 | 29 | |
| 54 | - Plugin::$instance->documents->switch_to_document( $component ); | |
| 30 | + Plugin::$instance->documents->switch_to_document( $document ); | |
| 55 | 31 | |
| 56 | - $data = $component->get_elements_data(); | |
| 32 | + $data = $document->get_elements_data(); | |
| 57 | 33 | |
| 58 | 34 | $data = apply_filters( 'elementor/frontend/builder_content_data', $data, $component_id ); |
| 59 | 35 | |
| 60 | - $data = Format_Component_Elements_Id::format( $data, [ $instance_element_id ] ); | |
| 61 | - | |
| 62 | 36 | $content = ''; |
| 63 | 37 | |
| 64 | 38 | if ( ! empty( $data ) ) { |
| 65 | 39 | ob_start(); |
| 66 | 40 | |
| 67 | - $component->print_elements_without_cache( $data ); | |
| 41 | + $document->print_elements( $data ); | |
| 68 | 42 | |
| 69 | 43 | $content = ob_get_clean(); |
| 70 | 44 | |
| 71 | 45 | $content = apply_filters( 'elementor/frontend/the_content', $content ); |
| @@ -75,21 +49,12 @@ | ||
| 75 | 49 | |
| 76 | 50 | return $content; |
| 77 | 51 | } |
| 78 | 52 | |
| 79 | - private function should_render_content( Component_Document $document ): bool { | |
| 80 | - return ! $this->is_password_protected( $document ) && | |
| 81 | - $document->is_built_with_elementor(); | |
| 53 | + private function is_component( $document ) { | |
| 54 | + return $document instanceof Component; | |
| 82 | 55 | } |
| 83 | 56 | |
| 84 | 57 | private function is_password_protected( $document ) { |
| 85 | 58 | 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 | 59 | } |
| 95 | 60 | } |