| 1 |
<?php |
| 2 |
/** |
| 3 |
* The DOM modifier, which passes HTML markup to modified through a pipeline. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Dom; |
| 11 |
|
| 12 |
use SolidWP\Performance\Http\Header_Factory; |
| 13 |
use SolidWP\Performance\Preload\Contracts\Preloadable; |
| 14 |
use SolidWP\Performance\Psr\Log\LoggerInterface; |
| 15 |
use SolidWP\Performance\StellarWP\Pipeline\Pipeline; |
| 16 |
use SolidWP\Performance\StellarWP\SuperGlobals\SuperGlobals; |
| 17 |
use SolidWP\Performance\Symfony\Component\DomCrawler\Crawler; |
| 18 |
use Throwable; |
| 19 |
|
| 20 |
/** |
| 21 |
* The DOM modifier, which passes HTML markup to modified through a pipeline. |
| 22 |
* |
| 23 |
* @see Provider::register_dom_modifier_pipeline() |
| 24 |
* |
| 25 |
* @package SolidWP\Performance |
| 26 |
*/ |
| 27 |
final class Modifier { |
| 28 |
|
| 29 |
/** |
| 30 |
* @var Pipeline |
| 31 |
*/ |
| 32 |
private Pipeline $pipeline; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var LoggerInterface |
| 36 |
*/ |
| 37 |
private LoggerInterface $logger; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var Header_Factory |
| 41 |
*/ |
| 42 |
private Header_Factory $header_factory; |
| 43 |
|
| 44 |
/** |
| 45 |
* @param Pipeline $pipeline The modifier pipeline. |
| 46 |
* @param LoggerInterface $logger The logger. |
| 47 |
* @param Header_Factory $header_factory The header factory. |
| 48 |
*/ |
| 49 |
public function __construct( |
| 50 |
Pipeline $pipeline, |
| 51 |
LoggerInterface $logger, |
| 52 |
Header_Factory $header_factory |
| 53 |
) { |
| 54 |
$this->pipeline = $pipeline; |
| 55 |
$this->logger = $logger; |
| 56 |
$this->header_factory = $header_factory; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Run the buffered HTML through our pipeline and modify where needed. |
| 61 |
* |
| 62 |
* @filter solidwp/performance/cache/html |
| 63 |
* |
| 64 |
* @param string $html The output buffer HTML. |
| 65 |
* |
| 66 |
* @return string The modified html. |
| 67 |
*/ |
| 68 |
public function modify( string $html ): string { |
| 69 |
if ( is_404() ) { |
| 70 |
return $html; |
| 71 |
} |
| 72 |
|
| 73 |
// No reason to parse a trigger request. |
| 74 |
if ( SuperGlobals::get_get_var( Preloadable::PRELOAD_TRIGGER_PARAM ) ) { |
| 75 |
return $html; |
| 76 |
} |
| 77 |
|
| 78 |
$header = $this->header_factory->make(); |
| 79 |
|
| 80 |
// Only allow text/html content type responses to be modified. |
| 81 |
if ( ! $header->starts_with( 'content-type', 'text/html' ) ) { |
| 82 |
return $html; |
| 83 |
} |
| 84 |
|
| 85 |
// Check if we have any enabled modifiers before attempting HTML modification. |
| 86 |
$has_enabled = $this->pipeline->send( false ) |
| 87 |
->via( 'enabled' ) |
| 88 |
->thenReturn(); |
| 89 |
|
| 90 |
if ( ! $has_enabled ) { |
| 91 |
$this->logger->debug( 'DOM Modifier: no pipeline modifiers enabled, skipping.' ); |
| 92 |
|
| 93 |
return $html; |
| 94 |
} |
| 95 |
|
| 96 |
$crawler = new Crawler( $html ); |
| 97 |
|
| 98 |
/** @var Crawler $modified_crawler */ |
| 99 |
$modified_crawler = $this->pipeline->send( $crawler ) |
| 100 |
->via( 'modify' ) |
| 101 |
->thenReturn(); |
| 102 |
|
| 103 |
try { |
| 104 |
// Return the owner document to ensure we have the doctype and html tags. |
| 105 |
return $modified_crawler->getNode( 0 )->ownerDocument->saveHtml(); |
| 106 |
} catch ( Throwable $e ) { |
| 107 |
$this->logger->warning( |
| 108 |
'DOM Modifier: Modified HTML was empty', |
| 109 |
[ |
| 110 |
'exception' => $e, |
| 111 |
] |
| 112 |
); |
| 113 |
|
| 114 |
return $html; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|