one-click-accessibility/modules/remediation/classes/remediation-base.php
Raz Ohad d07cb63d38
POC - FrontEnd remediations to handle Dynamic Data [APP-1644] (#312)
* POC - FrontEnd remediations to handle Dynamic Data

* Update modules/remediation/components/remediation-runner.php

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* added MutationObserver to cover elements that are added later on the page

runs until remediations are all done, and disconnect

* added $use_frontend flag instead of using "false"

* Update modules/remediation/components/remediation-runner.php

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>

* WIP on poc/fe-remediations

* [APP-0000] call trigger save async

* [APP-1644] apply FE remediation

* [APP-1644] apply FE remediation

* [APP-1644] apply FE remediation

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Add timeout (wait for FE remediation timeout)

* Small fixes

* Small fixes

* Small fixes

* Small fixes

---------

Co-authored-by: gitstream-cm[bot] <111687743+gitstream-cm[bot]@users.noreply.github.com>
Co-authored-by: vasyldinets <vasyld@elementor.red>
2025-07-03 17:26:50 +02:00

85 lines
1.7 KiB
PHP

<?php
namespace EA11y\Modules\Remediation\Classes;
use DOMDocument;
use DOMElement;
use DOMXPath;
class Remediation_Base {
public static string $type = 'remediation';
public DOMDocument $dom;
/**
* Use frontend remediation flag
*
* @var boolean
*/
public bool $use_frontend = false;
/**
* @var mixed
*/
public array $data;
public function get_type(): string {
return static::$type;
}
/**
* Check if the element exists
*
* @return boolean
*/
public function exists(): bool {
return $this->get_element_by_xpath( $this->data['xpath'] ) instanceof DOMElement;
}
/**
* get_element_by_xpath
* @param $xpath
*
* @return \DOMElement|\DOMNameSpaceNode|\DOMNode|null
*/
public function get_element_by_xpath( $query ) {
$xpath = new DOMXPath( $this->dom );
$elements = $xpath->query( $query );
if ( $elements->length > 0 ) {
return $elements->item( 0 );
}
return null;
}
public function run() : ?DOMDocument {
return $this->dom;
}
/**
* @throws \DOMException
*/
public function create_element( $data ) : DomElement {
$element = $this->dom->createElement( $data['tag'] );
if ( isset( $data['attributes'] ) ) {
foreach ( $data['attributes'] as $attribute ) {
$element->setAttribute( $attribute['name'], $attribute['value'] );
}
}
if ( isset( $data['content'] ) ) {
// @phpcs:ignore
$element->nodeValue = $data['content'];
}
return $element;
}
public function __construct( DOMDocument $dom, $data ) {
$this->dom = $dom;
$this->data = $data;
// if element does not exist, move the remediation to the Frontend
if ( ! $this->exists() ) {
$this->use_frontend = true;
return;
}
$this->run();
}
}