| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\GlobalClasses\Usage; |
| 4 |
|
| 5 |
/** |
| 6 |
* Tracks usage of a specific global CSS class across multiple Elementor documents. |
| 7 |
*/ |
| 8 |
class Css_Class_Usage { |
| 9 |
|
| 10 |
/** @var string */ |
| 11 |
private string $class_id; |
| 12 |
|
| 13 |
/** @var int */ |
| 14 |
private int $total = 0; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var array<int, array{ |
| 18 |
* title: string, |
| 19 |
* total: int, |
| 20 |
* elements: string[], |
| 21 |
* type?: string |
| 22 |
* }> |
| 23 |
*/ |
| 24 |
private array $pages = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @param string $class_id Global CSS class ID. |
| 30 |
*/ |
| 31 |
public function __construct( string $class_id ) { |
| 32 |
$this->class_id = $class_id; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Track usage of this class on a specific document and element. |
| 37 |
* |
| 38 |
* @param int $page_id Document ID. |
| 39 |
* @param string $page_title Document title. |
| 40 |
* @param string $element_id Element ID using this class. |
| 41 |
* @param string|null $document_type Optional document type (e.g. header, footer, etc). |
| 42 |
*/ |
| 43 |
public function track_usage( int $page_id, string $page_title, string $element_id, ?string $document_type = null ): void { |
| 44 |
++$this->total; |
| 45 |
|
| 46 |
if ( ! isset( $this->pages[ $page_id ] ) ) { |
| 47 |
$this->pages[ $page_id ] = [ |
| 48 |
'title' => $page_title, |
| 49 |
'total' => 0, |
| 50 |
'elements' => [], |
| 51 |
]; |
| 52 |
|
| 53 |
if ( $document_type ) { |
| 54 |
$this->pages[ $page_id ]['type'] = $document_type; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
++$this->pages[ $page_id ]['total']; |
| 59 |
$this->pages[ $page_id ]['elements'][] = $element_id; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Merge usage data from another instance with the same class ID. |
| 64 |
* |
| 65 |
* @param Css_Class_Usage $other The other usage object to merge in. |
| 66 |
* |
| 67 |
* @throws \InvalidArgumentException If the class IDs do not match. |
| 68 |
*/ |
| 69 |
public function merge( Css_Class_Usage $other ): void { |
| 70 |
if ( $other->get_class_id() !== $this->class_id ) { |
| 71 |
throw new \InvalidArgumentException( 'Mismatched class ID' ); |
| 72 |
} |
| 73 |
|
| 74 |
$this->total += $other->get_total_usage(); |
| 75 |
|
| 76 |
foreach ( $other->get_pages() as $page_id => $data ) { |
| 77 |
if ( ! isset( $this->pages[ $page_id ] ) ) { |
| 78 |
$this->pages[ $page_id ] = $data; |
| 79 |
} else { |
| 80 |
$this->pages[ $page_id ]['total'] += $data['total']; |
| 81 |
$this->pages[ $page_id ]['elements'] = array_merge( |
| 82 |
$this->pages[ $page_id ]['elements'], |
| 83 |
$data['elements'] |
| 84 |
); |
| 85 |
|
| 86 |
if ( empty( $this->pages[ $page_id ]['type'] ) && ! empty( $data['type'] ) ) { |
| 87 |
$this->pages[ $page_id ]['type'] = $data['type']; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the global class ID this instance tracks. |
| 95 |
* |
| 96 |
* @return string |
| 97 |
*/ |
| 98 |
public function get_class_id(): string { |
| 99 |
return $this->class_id; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get the total number of elements using this class. |
| 104 |
* |
| 105 |
* @return int |
| 106 |
*/ |
| 107 |
public function get_total_usage(): int { |
| 108 |
return $this->total; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get a map of document usages. |
| 113 |
* |
| 114 |
* @return array<int, array{title: string, total: int, elements: string[], type?: string}> |
| 115 |
*/ |
| 116 |
public function get_pages(): array { |
| 117 |
return $this->pages; |
| 118 |
} |
| 119 |
} |
| 120 |
|