| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Interactions; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Modules\Interactions\Cache\Interactions_Postmeta; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Handles frontend-specific interaction logic including: |
| 14 |
* - Collecting interactions from document elements during render |
| 15 |
* - Outputting interaction data as JSON in the page footer |
| 16 |
* |
| 17 |
* This class is responsible for the frontend rendering pipeline of interactions, |
| 18 |
* working with the Interactions_Collector for data storage and Adapter for data transformation. |
| 19 |
*/ |
| 20 |
class Interactions_Frontend_Handler { |
| 21 |
/** |
| 22 |
* @var callable|null |
| 23 |
*/ |
| 24 |
private $config_provider; |
| 25 |
|
| 26 |
public function __construct( $config_provider = null ) { |
| 27 |
$this->config_provider = is_callable( $config_provider ) ? $config_provider : null; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Collect interactions from document elements during frontend render. |
| 32 |
* |
| 33 |
* This method is hooked to 'elementor/frontend/builder_content_data' filter |
| 34 |
* to capture interactions from all documents (header, footer, post content) |
| 35 |
* as they are rendered. |
| 36 |
* |
| 37 |
* @param array $elements_data The document's elements data. |
| 38 |
* @param int $post_id The document's post ID. |
| 39 |
* @return array The unmodified elements data (pass-through filter). |
| 40 |
*/ |
| 41 |
public function collect_document_interactions( $elements_data, $post_id ) { |
| 42 |
// Only collect on frontend, not in editor |
| 43 |
if ( Plugin::$instance->editor->is_edit_mode() ) { |
| 44 |
return $elements_data; |
| 45 |
} |
| 46 |
|
| 47 |
if ( empty( $elements_data ) || ! is_array( $elements_data ) ) { |
| 48 |
return $elements_data; |
| 49 |
} |
| 50 |
|
| 51 |
$interactions_postmeta = new Interactions_Postmeta(); |
| 52 |
$cached_rows = $interactions_postmeta->load_content( $post_id ); |
| 53 |
|
| 54 |
if ( empty( $cached_rows ) ) { |
| 55 |
$cached_rows = $interactions_postmeta->process_content( $post_id, [ |
| 56 |
'elements' => $elements_data, |
| 57 |
] ); |
| 58 |
} |
| 59 |
|
| 60 |
$collector = Interactions_Collector::instance(); |
| 61 |
foreach ( $cached_rows as $element_id => $interactions ) { |
| 62 |
$collector->register( $element_id, $interactions ); |
| 63 |
} |
| 64 |
|
| 65 |
return $elements_data; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Output collected interaction data as a JSON script tag in the footer. |
| 70 |
* |
| 71 |
* This method is hooked to 'wp_footer' to output all collected interactions |
| 72 |
* as a centralized JSON data block that the frontend JavaScript can consume. |
| 73 |
*/ |
| 74 |
public function print_interactions_data() { |
| 75 |
// Only output on frontend, not in editor |
| 76 |
if ( Plugin::$instance->editor->is_edit_mode() ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$elements_with_interactions = $this->elements_with_interactions(); |
| 81 |
|
| 82 |
if ( empty( $elements_with_interactions ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$this->enqueue_interactions_assets(); |
| 87 |
|
| 88 |
// Output as JSON script tag |
| 89 |
$json_data = wp_json_encode( $elements_with_interactions, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); |
| 90 |
|
| 91 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON data is already encoded |
| 92 |
echo '<script type="application/json" id="' . Module::SCRIPT_ID_INTERACTIONS_DATA . '">' . $json_data . '</script>'; |
| 93 |
} |
| 94 |
|
| 95 |
private function elements_with_interactions() { |
| 96 |
$all_interactions = Interactions_Collector::instance()->get_all(); |
| 97 |
|
| 98 |
$elements_with_interactions = []; |
| 99 |
|
| 100 |
foreach ( $all_interactions as $element_id => $interactions ) { |
| 101 |
$elements_with_interactions[] = [ |
| 102 |
'elementId' => $element_id, |
| 103 |
'dataId' => $element_id, |
| 104 |
'interactions' => $interactions, |
| 105 |
]; |
| 106 |
} |
| 107 |
|
| 108 |
return $elements_with_interactions; |
| 109 |
} |
| 110 |
|
| 111 |
private function get_interactions_config() { |
| 112 |
return $this->config_provider ? call_user_func( $this->config_provider ) : []; |
| 113 |
} |
| 114 |
|
| 115 |
private function enqueue_interactions_assets() { |
| 116 |
wp_enqueue_script( Module::HANDLE_MOTION_JS ); |
| 117 |
wp_enqueue_script( Module::HANDLE_FRONTEND ); |
| 118 |
|
| 119 |
wp_localize_script( |
| 120 |
Module::HANDLE_FRONTEND, |
| 121 |
Module::JS_CONFIG_OBJECT, |
| 122 |
$this->get_interactions_config() |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|