← All changes
|
modules/interactions/interactions-frontend-handler.php
+27
-118
4.0.9
→
4.3.0-beta3
View file →
| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Interactions; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Plugin; |
| 6 | +use Elementor\Modules\Interactions\Cache\Interactions_Postmeta; | |
| 6 | 7 | |
| 7 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | 9 | exit; // Exit if accessed directly. |
| 9 | 10 | } |
| @@ -46,74 +47,23 @@ | ||
| 46 | 47 | if ( empty( $elements_data ) || ! is_array( $elements_data ) ) { |
| 47 | 48 | return $elements_data; |
| 48 | 49 | } |
| 49 | 50 | |
| 50 | - $collector = Interactions_Collector::instance(); | |
| 51 | + $interactions_postmeta = new Interactions_Postmeta(); | |
| 52 | + $cached_rows = $interactions_postmeta->load_content( $post_id ); | |
| 51 | 53 | |
| 52 | - // Recursively collect interactions from all elements | |
| 53 | - $this->collect_interactions_recursive( $elements_data, $collector ); | |
| 54 | + if ( empty( $cached_rows ) ) { | |
| 55 | + $cached_rows = $interactions_postmeta->process_content( $post_id, [ | |
| 56 | + 'elements' => $elements_data, | |
| 57 | + ] ); | |
| 58 | + } | |
| 54 | 59 | |
| 55 | - return $elements_data; | |
| 56 | - } | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * Recursively iterate through all elements and collect interactions. | |
| 60 | - * | |
| 61 | - * @param array $elements Array of element data. | |
| 62 | - * @param Interactions_Collector $collector The collector instance. | |
| 63 | - */ | |
| 64 | - private function collect_interactions_recursive( $elements, $collector ) { | |
| 65 | - if ( ! is_array( $elements ) ) { | |
| 66 | - return; | |
| 60 | + $collector = Interactions_Collector::instance(); | |
| 61 | + foreach ( $cached_rows as $element_id => $interactions ) { | |
| 62 | + $collector->register( $element_id, $interactions ); | |
| 67 | 63 | } |
| 68 | 64 | |
| 69 | - foreach ( $elements as $element ) { | |
| 70 | - if ( ! is_array( $element ) ) { | |
| 71 | - continue; | |
| 72 | - } | |
| 73 | - | |
| 74 | - // Check if this element has interactions | |
| 75 | - if ( ! empty( $element['id'] ) && isset( $element['interactions'] ) ) { | |
| 76 | - $element_id = $element['id']; | |
| 77 | - $interactions = $element['interactions']; | |
| 78 | - | |
| 79 | - // Decode if it's a JSON string | |
| 80 | - if ( is_string( $interactions ) ) { | |
| 81 | - $decoded = json_decode( $interactions, true ); | |
| 82 | - if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) { | |
| 83 | - $interactions = $decoded; | |
| 84 | - } else { | |
| 85 | - $interactions = null; | |
| 86 | - } | |
| 87 | - } | |
| 88 | - | |
| 89 | - // Normalize the interactions format - ensure we have items array | |
| 90 | - if ( is_array( $interactions ) ) { | |
| 91 | - // If interactions has 'items' key, it's already in the right format | |
| 92 | - // If not, check if it's a direct array of items or has other structure | |
| 93 | - if ( ! isset( $interactions['items'] ) ) { | |
| 94 | - // Check if this looks like a direct array of interaction items | |
| 95 | - // (first element has 'trigger' or 'animation' or '$$type') | |
| 96 | - $first_item = reset( $interactions ); | |
| 97 | - if ( is_array( $first_item ) && ( isset( $first_item['trigger'] ) || isset( $first_item['animation'] ) || isset( $first_item['$$type'] ) ) ) { | |
| 98 | - // It's a direct array of items, wrap it | |
| 99 | - $interactions = [ 'items' => $interactions ]; | |
| 100 | - } | |
| 101 | - } | |
| 102 | - | |
| 103 | - // Register with collector if we have valid items | |
| 104 | - $items = $interactions['items'] ?? []; | |
| 105 | - if ( ! empty( $items ) || ! empty( $interactions ) ) { | |
| 106 | - $collector->register( $element_id, $interactions ); | |
| 107 | - } | |
| 108 | - } | |
| 109 | - } | |
| 110 | - | |
| 111 | - // Recursively process child elements | |
| 112 | - if ( ! empty( $element['elements'] ) && is_array( $element['elements'] ) ) { | |
| 113 | - $this->collect_interactions_recursive( $element['elements'], $collector ); | |
| 114 | - } | |
| 115 | - } | |
| 65 | + return $elements_data; | |
| 116 | 66 | } |
| 117 | 67 | |
| 118 | 68 | /** |
| 119 | 69 | * Output collected interaction data as a JSON script tag in the footer. |
| @@ -126,32 +76,10 @@ | ||
| 126 | 76 | if ( Plugin::$instance->editor->is_edit_mode() ) { |
| 127 | 77 | return; |
| 128 | 78 | } |
| 129 | 79 | |
| 130 | - $collector = Interactions_Collector::instance(); | |
| 131 | - $all_interactions = $collector->get_all(); | |
| 80 | + $elements_with_interactions = $this->elements_with_interactions(); | |
| 132 | 81 | |
| 133 | - if ( empty( $all_interactions ) ) { | |
| 134 | - return; | |
| 135 | - } | |
| 136 | - | |
| 137 | - // Format: array of elements, each with elementId, dataId, and cleaned interactions | |
| 138 | - $elements_with_interactions = []; | |
| 139 | - foreach ( $all_interactions as $element_id => $interactions ) { | |
| 140 | - $items = $this->extract_interaction_items( $interactions ); | |
| 141 | - | |
| 142 | - if ( empty( $items ) ) { | |
| 143 | - continue; | |
| 144 | - } | |
| 145 | - | |
| 146 | - // Build element entry with elementId, dataId, and cleaned interactions array | |
| 147 | - $elements_with_interactions[] = [ | |
| 148 | - 'elementId' => $element_id, | |
| 149 | - 'dataId' => $element_id, | |
| 150 | - 'interactions' => $items, | |
| 151 | - ]; | |
| 152 | - } | |
| 153 | - | |
| 154 | 82 | if ( empty( $elements_with_interactions ) ) { |
| 155 | 83 | return; |
| 156 | 84 | } |
| 157 | 85 | |
| @@ -163,43 +91,26 @@ | ||
| 163 | 91 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON data is already encoded |
| 164 | 92 | echo '<script type="application/json" id="' . Module::SCRIPT_ID_INTERACTIONS_DATA . '">' . $json_data . '</script>'; |
| 165 | 93 | } |
| 166 | 94 | |
| 167 | - /** | |
| 168 | - * Extract interaction items from various data formats. | |
| 169 | - * | |
| 170 | - * Handles multiple formats: | |
| 171 | - * - v1 format: { items: [...] } | |
| 172 | - * - v2 format with $$type: { items: { $$type: '...', value: [...] } } | |
| 173 | - * - Direct arrays: [{ trigger: ..., animation: ... }, ...] | |
| 174 | - * | |
| 175 | - * @param array $interactions The interactions data. | |
| 176 | - * @return array The extracted items array. | |
| 177 | - */ | |
| 178 | - private function extract_interaction_items( $interactions ) { | |
| 179 | - if ( ! is_array( $interactions ) ) { | |
| 180 | - return []; | |
| 181 | - } | |
| 95 | + private function elements_with_interactions() { | |
| 96 | + $all_interactions = Interactions_Collector::instance()->get_all(); | |
| 182 | 97 | |
| 183 | - // Check if it has 'items' key (standard format) | |
| 184 | - if ( isset( $interactions['items'] ) ) { | |
| 185 | - $items = $interactions['items']; | |
| 98 | + $elements_with_interactions = []; | |
| 186 | 99 | |
| 187 | - return is_array( $items ) ? $items : []; | |
| 100 | + foreach ( $all_interactions as $element_id => $interactions ) { | |
| 101 | + $elements_with_interactions[] = [ | |
| 102 | + 'elementId' => $element_id, | |
| 103 | + 'dataId' => $element_id, | |
| 104 | + 'interactions' => $interactions, | |
| 105 | + ]; | |
| 188 | 106 | } |
| 189 | 107 | |
| 190 | - // Check if interactions itself is a direct array of items | |
| 191 | - // (first element has interaction-related keys) | |
| 192 | - $first_item = reset( $interactions ); | |
| 193 | - if ( is_array( $first_item ) && ( | |
| 194 | - isset( $first_item['trigger'] ) || | |
| 195 | - isset( $first_item['animation'] ) || | |
| 196 | - isset( $first_item['$$type'] ) | |
| 197 | - ) ) { | |
| 198 | - return $interactions; | |
| 199 | - } | |
| 108 | + return $elements_with_interactions; | |
| 109 | + } | |
| 200 | 110 | |
| 201 | - return []; | |
| 111 | + private function get_interactions_config() { | |
| 112 | + return $this->config_provider ? call_user_func( $this->config_provider ) : []; | |
| 202 | 113 | } |
| 203 | 114 | |
| 204 | 115 | private function enqueue_interactions_assets() { |
| 205 | 116 | wp_enqueue_script( Module::HANDLE_MOTION_JS ); |
| @@ -204,13 +115,11 @@ | ||
| 204 | 115 | private function enqueue_interactions_assets() { |
| 205 | 116 | wp_enqueue_script( Module::HANDLE_MOTION_JS ); |
| 206 | 117 | wp_enqueue_script( Module::HANDLE_FRONTEND ); |
| 207 | 118 | |
| 208 | - $config = $this->config_provider ? call_user_func( $this->config_provider ) : []; | |
| 209 | - | |
| 210 | 119 | wp_localize_script( |
| 211 | 120 | Module::HANDLE_FRONTEND, |
| 212 | 121 | Module::JS_CONFIG_OBJECT, |
| 213 | - $config | |
| 122 | + $this->get_interactions_config() | |
| 214 | 123 | ); |
| 215 | 124 | } |
| 216 | 125 | } |