| 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 |
return $config; |
| 23 |
} |
| 24 |
|
| 25 |
protected function render() { |
| 26 |
try { |
| 27 |
$renderer = Template_Renderer::instance(); |
| 28 |
|
| 29 |
foreach ( $this->get_templates() as $name => $path ) { |
| 30 |
if ( $renderer->is_registered( $name ) ) { |
| 31 |
continue; |
| 32 |
} |
| 33 |
|
| 34 |
$renderer->register( $name, $path ); |
| 35 |
} |
| 36 |
|
| 37 |
$context = [ |
| 38 |
'id' => $this->get_id(), |
| 39 |
'type' => $this->get_name(), |
| 40 |
'settings' => $this->get_atomic_settings(), |
| 41 |
'base_styles' => $this->get_base_styles_dictionary(), |
| 42 |
'interactions' => $this->get_interactions_ids(), |
| 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 |
public function get_interactions_ids() { |
| 55 |
$animation_ids = []; |
| 56 |
|
| 57 |
$list_of_interactions = ( is_array( $this->interactions ) && isset( $this->interactions['items'] ) ) |
| 58 |
? $this->interactions['items'] |
| 59 |
: []; |
| 60 |
|
| 61 |
foreach ( $list_of_interactions as $interaction ) { |
| 62 |
|
| 63 |
if ( isset( $interaction['$$type'] ) && 'interaction-item' === $interaction['$$type'] ) { |
| 64 |
$animation_id = $this->extract_animation_id_from_prop_type( $interaction ); |
| 65 |
if ( $animation_id ) { |
| 66 |
$animation_ids[] = $animation_id; |
| 67 |
} |
| 68 |
} elseif ( isset( $interaction['animation']['animation_id'] ) ) { |
| 69 |
$animation_ids[] = $interaction['animation']['animation_id']; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return $animation_ids; |
| 74 |
} |
| 75 |
|
| 76 |
private function extract_animation_id_from_prop_type( $item ) { |
| 77 |
if ( ! isset( $item['value'] ) || ! is_array( $item['value'] ) ) { |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
$item_value = $item['value']; |
| 82 |
|
| 83 |
$trigger = $this->extract_prop_value_simple( $item_value, 'trigger' ); |
| 84 |
$animation = $this->extract_prop_value_simple( $item_value, 'animation' ); |
| 85 |
|
| 86 |
if ( ! is_array( $animation ) ) { |
| 87 |
return null; |
| 88 |
} |
| 89 |
|
| 90 |
$effect = $this->extract_prop_value_simple( $animation, 'effect' ); |
| 91 |
$type = $this->extract_prop_value_simple( $animation, 'type' ); |
| 92 |
$direction = $this->extract_prop_value_simple( $animation, 'direction' ); |
| 93 |
$timing_config = $this->extract_prop_value_simple( $animation, 'timing_config' ); |
| 94 |
|
| 95 |
$duration = 300; |
| 96 |
$delay = 0; |
| 97 |
|
| 98 |
if ( is_array( $timing_config ) ) { |
| 99 |
$duration = $this->extract_prop_value_simple( $timing_config, 'duration', 300 ); |
| 100 |
$delay = $this->extract_prop_value_simple( $timing_config, 'delay', 0 ); |
| 101 |
} |
| 102 |
|
| 103 |
return implode( '-', [ $trigger, $effect, $type, $direction, $duration, $delay ] ); |
| 104 |
} |
| 105 |
|
| 106 |
private function extract_prop_value_simple( $data, $key, $default = '' ) { |
| 107 |
if ( ! is_array( $data ) || ! isset( $data[ $key ] ) ) { |
| 108 |
return $default; |
| 109 |
} |
| 110 |
|
| 111 |
$value = $data[ $key ]; |
| 112 |
|
| 113 |
if ( is_array( $value ) && isset( $value['$$type'] ) && isset( $value['value'] ) ) { |
| 114 |
return $value['value']; |
| 115 |
} |
| 116 |
|
| 117 |
return null !== $value ? $value : $default; |
| 118 |
} |
| 119 |
|
| 120 |
protected function get_templates_contents() { |
| 121 |
return array_map( |
| 122 |
fn ( $path ) => Utils::file_get_contents( $path ), |
| 123 |
$this->get_templates() |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
protected function get_main_template() { |
| 128 |
$templates = $this->get_templates(); |
| 129 |
|
| 130 |
if ( count( $templates ) > 1 ) { |
| 131 |
Utils::safe_throw( 'When having more than one template, you should override this method to return the main template.' ); |
| 132 |
|
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
foreach ( $templates as $key => $path ) { |
| 137 |
// Returns first key in the array. |
| 138 |
return $key; |
| 139 |
} |
| 140 |
|
| 141 |
return null; |
| 142 |
} |
| 143 |
|
| 144 |
abstract protected function get_templates(): array; |
| 145 |
} |
| 146 |
|