← All changes
|
modules/atomic-widgets/elements/base/has-template.php
+85
-19
4.2.2
→
3.35.7
View file →
| @@ -13,12 +13,8 @@ | ||
| 13 | 13 | * @mixin Has_Atomic_Base |
| 14 | 14 | */ |
| 15 | 15 | trait Has_Template { |
| 16 | 16 | |
| 17 | - private static function get_macros_template_key(): string { | |
| 18 | - return 'elementor/macros'; | |
| 19 | - } | |
| 20 | - | |
| 21 | 17 | public function get_initial_config() { |
| 22 | 18 | $config = parent::get_initial_config(); |
| 23 | 19 | |
| 24 | 20 | $config['twig_main_template'] = $this->get_main_template(); |
| @@ -25,25 +21,13 @@ | ||
| 25 | 21 | $config['twig_templates'] = $this->get_templates_contents(); |
| 26 | 22 | return $config; |
| 27 | 23 | } |
| 28 | 24 | |
| 29 | - protected function transform_link_for_render( array $parsed ): array { | |
| 30 | - return $parsed; | |
| 31 | - } | |
| 32 | - | |
| 33 | - protected function get_shared_templates(): array { | |
| 34 | - return [ | |
| 35 | - self::get_macros_template_key() => __DIR__ . '/_macros.html.twig', | |
| 36 | - ]; | |
| 37 | - } | |
| 38 | - | |
| 39 | 25 | protected function render() { |
| 40 | 26 | try { |
| 41 | 27 | $renderer = Template_Renderer::instance(); |
| 42 | 28 | |
| 43 | - $all_templates = array_merge( $this->get_shared_templates(), $this->get_templates() ); | |
| 44 | - | |
| 45 | - foreach ( $all_templates as $name => $path ) { | |
| 29 | + foreach ( $this->get_templates() as $name => $path ) { | |
| 46 | 30 | if ( $renderer->is_registered( $name ) ) { |
| 47 | 31 | continue; |
| 48 | 32 | } |
| 49 | 33 | |
| @@ -51,12 +35,12 @@ | ||
| 51 | 35 | } |
| 52 | 36 | |
| 53 | 37 | $context = [ |
| 54 | 38 | 'id' => $this->get_id(), |
| 55 | - 'interaction_id' => $this->get_interaction_id(), | |
| 56 | 39 | 'type' => $this->get_name(), |
| 57 | 40 | 'settings' => $this->get_atomic_settings(), |
| 58 | 41 | 'base_styles' => $this->get_base_styles_dictionary(), |
| 42 | + 'interactions' => $this->get_interactions_ids(), | |
| 59 | 43 | ]; |
| 60 | 44 | |
| 61 | 45 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 62 | 46 | echo $renderer->render( $this->get_main_template(), $context ); |
| @@ -66,12 +50,94 @@ | ||
| 66 | 50 | } |
| 67 | 51 | } |
| 68 | 52 | } |
| 69 | 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 | + $config = $this->extract_prop_value_simple( $animation, 'config' ); | |
| 95 | + | |
| 96 | + $duration = 300; | |
| 97 | + $delay = 0; | |
| 98 | + $replay = 0; | |
| 99 | + $relative_to = 'viewport'; | |
| 100 | + $offset_top = 15; | |
| 101 | + $offset_bottom = 85; | |
| 102 | + | |
| 103 | + if ( is_array( $timing_config ) ) { | |
| 104 | + $duration = $this->extract_prop_value_simple( $timing_config, 'duration', 300 ); | |
| 105 | + $delay = $this->extract_prop_value_simple( $timing_config, 'delay', 0 ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + if ( is_array( $config ) ) { | |
| 109 | + $relative_to = $this->extract_prop_value_simple( $config, 'relative_to', 'viewport' ); | |
| 110 | + $offset_top = $this->extract_prop_value_simple( $config, 'offset_top', 15 ); | |
| 111 | + $offset_bottom = $this->extract_prop_value_simple( $config, 'offset_bottom', 85 ); | |
| 112 | + $replay = $this->extract_prop_value_simple( $config, 'replay', 0 ); | |
| 113 | + if ( empty( $replay ) && 0 !== $replay && '0' !== $replay ) { | |
| 114 | + $replay = 0; | |
| 115 | + } | |
| 116 | + } else { | |
| 117 | + $replay = 0; | |
| 118 | + } | |
| 119 | + return implode( '-', [ $trigger, $effect, $type, $direction, $duration, $delay, $replay, $relative_to, $offset_top, $offset_bottom ] ); | |
| 120 | + } | |
| 121 | + | |
| 122 | + private function extract_prop_value_simple( $data, $key, $default = '' ) { | |
| 123 | + if ( ! is_array( $data ) || ! isset( $data[ $key ] ) ) { | |
| 124 | + return $default; | |
| 125 | + } | |
| 126 | + | |
| 127 | + $value = $data[ $key ]; | |
| 128 | + | |
| 129 | + if ( is_array( $value ) && isset( $value['$$type'] ) && isset( $value['value'] ) ) { | |
| 130 | + return $value['value']; | |
| 131 | + } | |
| 132 | + | |
| 133 | + return null !== $value ? $value : $default; | |
| 134 | + } | |
| 135 | + | |
| 70 | 136 | protected function get_templates_contents() { |
| 71 | 137 | return array_map( |
| 72 | 138 | fn ( $path ) => Utils::file_get_contents( $path ), |
| 73 | - array_merge( $this->get_shared_templates(), $this->get_templates() ) | |
| 139 | + $this->get_templates() | |
| 74 | 140 | ); |
| 75 | 141 | } |
| 76 | 142 | |
| 77 | 143 | protected function get_main_template() { |