| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\GlobalClasses\Usage; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Core\Base\Document as ElementorDocument; |
| 7 |
use Elementor\Modules\GlobalClasses\Global_Classes_Repository; |
| 8 |
|
| 9 |
/** |
| 10 |
* Tracks usage of global CSS classes within a specific Elementor document. |
| 11 |
*/ |
| 12 |
class Document_Usage { |
| 13 |
|
| 14 |
|
| 15 |
/** @var ElementorDocument */ |
| 16 |
private ElementorDocument $document; |
| 17 |
|
| 18 |
/** @var array<string, Css_Class_Usage> */ |
| 19 |
private array $usages = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor. |
| 23 |
* |
| 24 |
* @param ElementorDocument $document The Elementor document object. |
| 25 |
*/ |
| 26 |
public function __construct( ElementorDocument $document ) { |
| 27 |
$this->document = $document; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Analyze the document to find and record usage of global CSS classes. |
| 32 |
*/ |
| 33 |
public function analyze(): void { |
| 34 |
$page_id = $this->document->get_main_id(); |
| 35 |
$page_title = $this->document->get_post()->post_title; |
| 36 |
$class_ids = $this->get_all_global_class_ids(); |
| 37 |
$elements_data = $this->document->get_elements_raw_data() ?? []; |
| 38 |
|
| 39 |
$document_type = $this->document->get_type(); |
| 40 |
if ( empty( $document_type ) ) { |
| 41 |
$document_type = get_post_type( $page_id ) ?? 'unknown'; |
| 42 |
} |
| 43 |
|
| 44 |
if ( empty( $elements_data ) ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
Plugin::$instance->db->iterate_data( |
| 49 |
$elements_data, |
| 50 |
function ( $element_data ) use ( $class_ids, $page_id, $page_title, $document_type ) { |
| 51 |
$class_values = $element_data['settings']['classes']['value'] ?? []; |
| 52 |
|
| 53 |
if ( empty( $class_values ) || ! is_array( $class_values ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
foreach ( $class_values as $class_id ) { |
| 58 |
if ( ! in_array( $class_id, $class_ids, true ) ) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! isset( $this->usages[ $class_id ] ) ) { |
| 63 |
$this->usages[ $class_id ] = new Css_Class_Usage( $class_id ); |
| 64 |
} |
| 65 |
|
| 66 |
$this->usages[ $class_id ]->track_usage( |
| 67 |
$page_id, |
| 68 |
$page_title, |
| 69 |
$element_data['id'] ?? 'unknown', |
| 70 |
$document_type |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get all recorded usages of global CSS classes in this document. |
| 79 |
* |
| 80 |
* @return array<string, Css_Class_Usage> |
| 81 |
*/ |
| 82 |
public function get_usages(): array { |
| 83 |
return $this->usages; |
| 84 |
} |
| 85 |
|
| 86 |
/**֜ |
| 87 |
* Retrieve all registered global CSS class IDs. |
| 88 |
* |
| 89 |
* @return string[] |
| 90 |
*/ |
| 91 |
protected function get_all_global_class_ids(): array { |
| 92 |
return Global_Classes_Repository::make() |
| 93 |
->all() |
| 94 |
->get_items() |
| 95 |
->filter( fn( $item ) => ! empty( $item['id'] ?? null ) ) |
| 96 |
->keys() |
| 97 |
->all(); |
| 98 |
} |
| 99 |
} |
| 100 |
|