PluginProbe
Elementor Website Builder – more than just a page builder / 3.31.2
Elementor Website Builder – more than just a page builder v3.31.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / global-classes / usage / css-class-usage.php

css-class-usage.php in Elementor Website Builder – more than just a page builder 3.31.2, at modules/global-classes/usage/css-class-usage.php

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