| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Elements; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\TemplateRenderer\Template_Renderer; |
| 6 |
use Elementor\Utils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* @mixin Has_Atomic_Base |
| 14 |
*/ |
| 15 |
trait Has_Template { |
| 16 |
|
| 17 |
public function get_initial_config() { |
| 18 |
$config = parent::get_initial_config(); |
| 19 |
|
| 20 |
$config['twig_main_template'] = $this->get_main_template(); |
| 21 |
$config['twig_templates'] = $this->get_templates_contents(); |
| 22 |
|
| 23 |
return $config; |
| 24 |
} |
| 25 |
|
| 26 |
protected function render() { |
| 27 |
try { |
| 28 |
$renderer = Template_Renderer::instance(); |
| 29 |
|
| 30 |
foreach ( $this->get_templates() as $name => $path ) { |
| 31 |
if ( $renderer->is_registered( $name ) ) { |
| 32 |
continue; |
| 33 |
} |
| 34 |
|
| 35 |
$renderer->register( $name, $path ); |
| 36 |
} |
| 37 |
|
| 38 |
$context = [ |
| 39 |
'id' => $this->get_id(), |
| 40 |
'type' => $this->get_name(), |
| 41 |
'settings' => $this->get_atomic_settings(), |
| 42 |
'base_styles' => $this->get_base_styles_dictionary(), |
| 43 |
]; |
| 44 |
|
| 45 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 46 |
echo $renderer->render( $this->get_main_template(), $context ); |
| 47 |
} catch ( \Exception $e ) { |
| 48 |
if ( Utils::is_elementor_debug() ) { |
| 49 |
throw $e; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
protected function get_templates_contents() { |
| 55 |
return array_map( |
| 56 |
fn ( $path ) => Utils::file_get_contents( $path ), |
| 57 |
$this->get_templates() |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
protected function get_main_template() { |
| 62 |
$templates = $this->get_templates(); |
| 63 |
|
| 64 |
if ( count( $templates ) > 1 ) { |
| 65 |
Utils::safe_throw( 'When having more than one template, you should override this method to return the main template.' ); |
| 66 |
|
| 67 |
return null; |
| 68 |
} |
| 69 |
|
| 70 |
foreach ( $templates as $key => $path ) { |
| 71 |
// Returns first key in the array. |
| 72 |
return $key; |
| 73 |
} |
| 74 |
|
| 75 |
return null; |
| 76 |
} |
| 77 |
|
| 78 |
abstract protected function get_templates(): array; |
| 79 |
} |
| 80 |
|