| 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 |
protected function render() { |
| 17 |
try { |
| 18 |
$renderer = Template_Renderer::instance(); |
| 19 |
|
| 20 |
foreach ( $this->get_templates() as $name => $path ) { |
| 21 |
if ( $renderer->is_registered( $name ) ) { |
| 22 |
continue; |
| 23 |
} |
| 24 |
|
| 25 |
$renderer->register( $name, $path ); |
| 26 |
} |
| 27 |
|
| 28 |
$context = [ |
| 29 |
'id' => $this->get_id(), |
| 30 |
'type' => $this->get_name(), |
| 31 |
'settings' => $this->get_atomic_settings(), |
| 32 |
'base_styles' => $this->get_base_styles_dictionary(), |
| 33 |
]; |
| 34 |
|
| 35 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 36 |
echo $renderer->render( $this->get_main_template(), $context ); |
| 37 |
} catch ( \Exception $e ) { |
| 38 |
if ( Utils::is_elementor_debug() ) { |
| 39 |
throw $e; |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
protected function get_main_template() { |
| 45 |
$templates = $this->get_templates(); |
| 46 |
|
| 47 |
if ( count( $templates ) > 1 ) { |
| 48 |
Utils::safe_throw( 'When having more than one template, you should override this method to return the main template.' ); |
| 49 |
|
| 50 |
return null; |
| 51 |
} |
| 52 |
|
| 53 |
foreach ( $templates as $key => $path ) { |
| 54 |
// Returns first key in the array. |
| 55 |
return $key; |
| 56 |
} |
| 57 |
|
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
abstract protected function get_templates(): array; |
| 62 |
} |
| 63 |
|