| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Interactions; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Collects interaction data from all rendered documents and provides centralized access. |
| 11 |
*/ |
| 12 |
class Interactions_Collector { |
| 13 |
/** |
| 14 |
* @var Interactions_Collector |
| 15 |
*/ |
| 16 |
private static $instance = null; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var array Stores interaction data keyed by element ID |
| 20 |
* Format: [ 'element_id' => [ interaction_items... ] ] |
| 21 |
*/ |
| 22 |
private $interactions_data = []; |
| 23 |
|
| 24 |
/** |
| 25 |
* Get singleton instance. |
| 26 |
* |
| 27 |
* @return Interactions_Collector |
| 28 |
*/ |
| 29 |
public static function instance() { |
| 30 |
if ( null === self::$instance ) { |
| 31 |
self::$instance = new self(); |
| 32 |
} |
| 33 |
|
| 34 |
return self::$instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Register interaction data for an element. |
| 39 |
* |
| 40 |
* @param string $element_id The element ID (data-id attribute value) |
| 41 |
* @param array $interactions The full interactions array from the element |
| 42 |
*/ |
| 43 |
public function register( $element_id, $interactions ) { |
| 44 |
if ( empty( $element_id ) || empty( $interactions ) ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$this->interactions_data[ $element_id ] = $interactions; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get all collected interaction data. |
| 53 |
* |
| 54 |
* @return array Format: [ 'element_id' => interactions_array ] |
| 55 |
*/ |
| 56 |
public function get_all() { |
| 57 |
return $this->interactions_data; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get interaction data for a specific element. |
| 62 |
* |
| 63 |
* @param string $element_id The element ID |
| 64 |
* @return array|null |
| 65 |
*/ |
| 66 |
public function get( $element_id ) { |
| 67 |
return $this->interactions_data[ $element_id ] ?? null; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Reset collected data (useful for testing or page reloads). |
| 72 |
*/ |
| 73 |
public function reset() { |
| 74 |
$this->interactions_data = []; |
| 75 |
} |
| 76 |
} |
| 77 |
|