| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Elements\Base; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\Elements\TemplateRenderer\Template_Renderer; |
| 6 |
use Elementor\Utils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Trait for nested elements that render using Twig templates. |
| 14 |
* Provides Twig-based rendering with children support for nested elements. |
| 15 |
* |
| 16 |
* @mixin Has_Atomic_Base |
| 17 |
* @mixin Atomic_Element_Base |
| 18 |
*/ |
| 19 |
trait Has_Element_Template { |
| 20 |
|
| 21 |
public function get_initial_config() { |
| 22 |
$config = parent::get_initial_config(); |
| 23 |
|
| 24 |
$config['support_nesting'] = true; |
| 25 |
$config['twig_main_template'] = $this->get_main_template(); |
| 26 |
$config['twig_templates'] = $this->get_templates_contents(); |
| 27 |
$config['base_styles_dictionary'] = $this->get_base_styles_dictionary(); |
| 28 |
|
| 29 |
return $config; |
| 30 |
} |
| 31 |
|
| 32 |
protected function get_templates_contents() { |
| 33 |
return array_map( |
| 34 |
fn ( $path ) => Utils::file_get_contents( $path ), |
| 35 |
$this->get_templates() |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
protected function render() { |
| 40 |
try { |
| 41 |
$renderer = Template_Renderer::instance(); |
| 42 |
|
| 43 |
foreach ( $this->get_templates() as $name => $path ) { |
| 44 |
if ( $renderer->is_registered( $name ) ) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
$renderer->register( $name, $path ); |
| 49 |
} |
| 50 |
|
| 51 |
$context = $this->build_template_context(); |
| 52 |
$template_html = $renderer->render( $this->get_main_template(), $context ); |
| 53 |
$children_html = $this->render_children_to_html(); |
| 54 |
$output = str_replace( $this->get_children_placeholder(), $children_html, $template_html ); |
| 55 |
|
| 56 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 57 |
echo $output; |
| 58 |
} catch ( \Exception $e ) { |
| 59 |
if ( Utils::is_elementor_debug() ) { |
| 60 |
throw $e; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
protected function render_children_to_html(): string { |
| 66 |
$html = ''; |
| 67 |
|
| 68 |
foreach ( $this->get_children() as $child ) { |
| 69 |
ob_start(); |
| 70 |
$child->print_element(); |
| 71 |
$html .= ob_get_clean(); |
| 72 |
} |
| 73 |
|
| 74 |
return $html; |
| 75 |
} |
| 76 |
|
| 77 |
protected function get_children_placeholder(): string { |
| 78 |
return '<!-- elementor-children-placeholder -->'; |
| 79 |
} |
| 80 |
|
| 81 |
protected function build_base_template_context(): array { |
| 82 |
return [ |
| 83 |
'id' => $this->get_id(), |
| 84 |
'interaction_id' => $this->get_interaction_id(), |
| 85 |
'type' => $this->get_name(), |
| 86 |
'settings' => $this->get_atomic_settings(), |
| 87 |
'base_styles' => $this->get_base_styles_dictionary(), |
| 88 |
'children_placeholder' => $this->get_children_placeholder(), |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
public function before_render() { |
| 93 |
// Intentionally empty - Twig template handles full rendering |
| 94 |
} |
| 95 |
|
| 96 |
public function after_render() { |
| 97 |
// Intentionally empty - Twig template handles full rendering |
| 98 |
} |
| 99 |
|
| 100 |
public function print_content() { |
| 101 |
$defined_context = $this->define_render_context(); |
| 102 |
|
| 103 |
if ( empty( $defined_context ) ) { |
| 104 |
return $this->render(); |
| 105 |
} |
| 106 |
|
| 107 |
$this->set_render_context( $defined_context ); |
| 108 |
|
| 109 |
$this->render(); |
| 110 |
|
| 111 |
$this->clear_render_context( $defined_context ); |
| 112 |
} |
| 113 |
|
| 114 |
protected function get_main_template(): string { |
| 115 |
$templates = $this->get_templates(); |
| 116 |
|
| 117 |
foreach ( $templates as $key => $path ) { |
| 118 |
return $key; |
| 119 |
} |
| 120 |
|
| 121 |
return ''; |
| 122 |
} |
| 123 |
|
| 124 |
abstract protected function get_templates(): array; |
| 125 |
|
| 126 |
protected function build_template_context(): array { |
| 127 |
return $this->build_base_template_context(); |
| 128 |
} |
| 129 |
} |
| 130 |
|