Initial commit xmarticaims

This commit is contained in:
2026-11-02 10:26:32 -06:00
commit e74236f5c1
9764 changed files with 2880647 additions and 0 deletions
@@ -0,0 +1,64 @@
<?php
namespace WP_Rocket\Engine\Media\Lazyload\CSS\Front;
use WP_Filesystem_Direct;
use WP_Rocket\Engine\Optimization\CSSTrait;
class ContentFetcher {
use CSSTrait;
/**
* WordPress filesystem.
*
* @var WP_Filesystem_Direct
*/
protected $filesystem;
/**
* Instance.
*
* @param WP_Filesystem_Direct $filesystem WordPress filesystem.
*/
public function __construct( WP_Filesystem_Direct $filesystem = null ) {
$this->filesystem = $filesystem ?: rocket_direct_filesystem();
}
/**
* Fetch content from the resource.
*
* @param string $path Path from the resource.
* @param string $destination Destination path.
* @return false|string
*/
public function fetch( string $path, string $destination ) {
$content = $this->fetch_content( $path );
if ( ! $content ) {
return false;
}
$content = $this->move( $this->get_converter( $path, $destination ), $content, $path );
$this->set_cached_import( $path );
$content = $this->combine_imports( $content, $destination );
return $content;
}
/**
* Fetch the content from the file.
*
* @param string $path Path to fetch.
*
* @return false|string
*/
protected function fetch_content( string $path ) {
if ( ! wp_http_validate_url( $path ) ) {
return $this->filesystem->get_contents( $path );
}
return wp_remote_retrieve_body( wp_remote_get( $path ) );
}
}
@@ -0,0 +1,230 @@
<?php
namespace WP_Rocket\Engine\Media\Lazyload\CSS\Front;
use WP_Rocket\Engine\Optimization\RegexTrait;
class Extractor {
use RegexTrait;
/**
* Comment mapping.
*
* @var array
*/
protected $comments_mapping = [];
/**
* Extract background images from CSS.
*
* @param string $content CSS content.
* @return array
*/
public function extract( string $content ): array {
$this->comments_mapping = [];
$comment_regex = '#/\*[^*]*\*+([^/][^*]*\*+)*/#';
$content = preg_replace_callback(
$comment_regex,
function ( $matches ) {
$id = '/*' . uniqid( 'bg_css_comment' ) . '*/';
$this->comments_mapping[ $id ] = $matches[0];
return $id;
},
$content
);
$background_regex = '(?<selector>[ \-,:\w.()\n\r^>[*"\'=\]#]+)\s?{[^{}]*background\s*:(?<property>[^;}]*)[^}]*}';
$background_image_regex = '(?<selector>[ \-,:\w.()\n\r>^[*"\'=\]#]+)\s?{[^{}]*background-image\s*:(?<property>[^;}]*)[^}]*}';
/**
* Lazyload background property regex.
*
* @param string $regex Lazyload background property regex.
*/
$background_regex = $this->apply_string_filter( 'lazyload_css_extract_bg_property_regex', $background_regex );
/**
* Lazyload background property regex.
*
* @param string $regex Lazyload background property regex.
*/
$background_image_regex = $this->apply_string_filter( 'lazyload_css_extract_bg_img_property_regex', $background_image_regex );
$background_matches = $this->find( $background_regex, $content, 'mi' );
$background_image_matches = $this->find( $background_image_regex, $content, 'mi' );
$matches = array_merge( $background_matches, $background_image_matches );
if ( empty( $matches ) ) {
return [];
}
$results = [];
foreach ( $matches as $match ) {
if ( ! key_exists( 'property', $match ) || ! key_exists( 'selector', $match ) ) {
continue;
}
$property = $match['property'];
$selector = $match['selector'];
$property = trim( $property );
$selector = trim( $selector );
$urls = $this->extract_urls( $property );
$block = trim( $match[0] );
foreach ( $this->comments_mapping as $id => $comment ) {
$block = str_replace( $id, $comment, $block );
}
foreach ( $urls as $url ) {
$results[ $selector ][] = [
'selector' => $selector,
'url' => $url['url'],
'original' => $url['original'],
'block' => $block,
];
}
}
return $results;
}
/**
* Extract URLS from a CSS property.
*
* @param string $content Content from the CSS property.
* @return array
*/
protected function extract_urls( string $content ): array {
/**
* Lazyload URL regex.
*
* @param string $regex Lazyload URL regex.
*/
$regex = $this->apply_string_filter( 'lazyload_css_extract_url_regex', '(?<tag>url\([\'"]?(?<url>[^\)]*)[\'"]?\))' );
$matches = $this->find( $regex, $content, 'mi' );
$output = [];
/**
* Lazyload ignored URLs.
*
* @param string[] $urls Ignored URLs.
*/
$ignored_urls = (array) apply_filters( 'rocket_lazyload_css_ignored_urls', [] );
foreach ( $matches as $match ) {
$url = $match['url'] ?: '';
$url = str_replace( '"', '', $url );
$url = str_replace( "'", '', $url );
$url = trim( $url );
$url = $this->make_url_complete( $url );
if ( ! key_exists( 'tag', $match ) || ! key_exists( 'url', $match ) || ! $url || $this->is_url_ignored( $url, $ignored_urls ) ) {
continue;
}
if ( ! $this->is_url_external( $url ) && ! $this->is_relative( $url ) ) {
$url = $this->apply_string_filter( 'css_url', $url );
}
$url = "url('$url')";
$original_url = trim( $match['tag'], ' ,' );
$output[] = [
'url' => $url,
'original' => $original_url,
];
}
return $output;
}
/**
* Apply a string filter.
*
* @param string $name Name from the filter.
* @param string $value Value to pass to the filter that will be returned.
* @param array ...$args Additional values.
* @return string
*/
protected function apply_string_filter( string $name, string $value, ...$args ) {
$output = apply_filters( 'rocket_' . $name, $value, ...$args );
if ( ! is_string( $output ) ) {
return $value;
}
return $output;
}
/**
* Check if the URLs is ignored.
*
* @param string $url URL to check.
* @param array $ignored_urls List of ignored URLs.
*
* @return bool
*/
protected function is_url_ignored( string $url, array $ignored_urls ): bool {
foreach ( $ignored_urls as $ignored_url ) {
if ( strpos( $url, $ignored_url ) !== false ) {
return true;
}
}
return false;
}
/**
* Complete the URL if necessary.
*
* @param string $url URL to complete.
*
* @return string
*/
protected function make_url_complete( string $url ): string {
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( $host || $this->is_relative( $url ) ) {
return $url;
}
return rocket_get_home_url() . '/' . trim( $url, '/ ' );
}
/**
* Check if the URL is external.
*
* @param string $url URL to check.
*
* @return bool
*/
protected function is_url_external( string $url ): bool {
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( ! $host ) {
return false;
}
$home_host = wp_parse_url( rocket_get_home_url(), PHP_URL_HOST );
return $host !== $home_host;
}
/**
* Check if the URL is relative.
*
* @param string $url URL to check.
* @return bool
*/
protected function is_relative( string $url ): bool {
return preg_match( '/^\./', $url );
}
}
@@ -0,0 +1,23 @@
<?php
namespace WP_Rocket\Engine\Media\Lazyload\CSS\Front;
class FileResolver {
/**
* Resolves the name from the file from its URL.
*
* @param string $url URL from the file to resolve.
* @return string
*/
public function resolve( string $url ): string {
$path = rocket_url_to_path( strtok( $url, '?' ) );
if ( ! $path ) {
return '';
}
return $path;
}
}
@@ -0,0 +1,124 @@
<?php
namespace WP_Rocket\Engine\Media\Lazyload\CSS\Front;
class MappingFormatter {
/**
* Format data for the Mapping file.
*
* @param array $data Data to format.
* @return array
*/
public function format( array $data ): array {
$formatted_urls = [];
foreach ( $data as $datum ) {
$hash = $datum['hash'];
$selector = $datum['selector'];
$selector = $this->remove_pseudo_classes( $selector );
$url = $datum['url'];
$placeholder = "--wpr-bg-$hash";
$variable_placeholder = ':root{' . $placeholder . ': ' . $url . ';}';
$formatted_urls[] = [
'selector' => $selector,
'style' => $variable_placeholder,
'hash' => $hash,
];
}
return $formatted_urls;
}
/**
* Get pseudo elements to remove.
*
* @return string[]
*/
private function get_pseudo_elements_to_remove() {
$original_pseudo_elements = [
':before',
':after',
':first-line',
':first-letter',
':active',
':hover',
':focus',
':visited',
':focus-within',
':focus-visible',
];
/**
* Pseudo elements to remove from lazyload CSS selector.
*
* @param string[] Pseudo elements to remove.
*/
$pseudo_elements_to_remove = apply_filters( 'rocket_lazyload_css_ignored_pseudo_elements', $original_pseudo_elements );
if ( ! is_array( $original_pseudo_elements ) ) {
$pseudo_elements_to_remove = $original_pseudo_elements;
}
usort(
$pseudo_elements_to_remove,
static function ( $first, $second ) {
if ( strlen( $first ) === strlen( $second ) ) {
return 0;
}
return ( strlen( $first ) > strlen( $second ) ) ? -1 : 1;
}
);
return $pseudo_elements_to_remove;
}
/**
* Remove pseudo classes from the selector while mapping on each selector.
*
* @param string $selector Selector to clean.
* @return string
*/
public function remove_pseudo_classes_for_selector( string $selector ): string {
$selector = preg_replace( '/::[\w-]+/', '', $selector );
$pseudo_elements_to_remove = $this->get_pseudo_elements_to_remove();
foreach ( $pseudo_elements_to_remove as $element ) {
$selector = str_replace( $element, '', $selector );
}
if ( in_array( substr( $selector, -1 ), [ '&', '~', '+', '>' ], true ) ) {
$selector .= '*';
}
if ( ! $selector ) {
return 'body';
}
return $selector;
}
/**
* Remove pseudo classes from the selector.
*
* @param string $selector Selector to clean.
* @return string
*/
protected function remove_pseudo_classes( string $selector ): string {
$selectors = explode( ',', $selector );
$selectors = array_map( [ $this, 'remove_pseudo_classes_for_selector' ], $selectors );
$selectors = array_unique( $selectors );
$selector = implode( ',', $selectors );
if ( ! $selector ) {
return 'body';
}
return (string) $selector;
}
}
@@ -0,0 +1,71 @@
<?php
namespace WP_Rocket\Engine\Media\Lazyload\CSS\Front;
class RuleFormatter {
/**
* Format the CSS rule inside the CSS content.
*
* @param string $css CSS content.
* @param array $data Data to format.
* @return string
*/
public function format( string $css, array $data ): string {
if ( count( $data ) === 0 ) {
return $css;
}
$block = '';
$replaced_block = null;
$blocks = [];
foreach ( $data as $datum ) {
$added = false;
foreach ( $blocks as &$block ) {
if ( $block['block'] === $datum['block'] ) {
$block['items'] [] = $datum;
$added = true;
break;
}
}
if ( $added ) {
continue;
}
$blocks [] = [
'items' => [
$datum,
],
'block' => $datum['block'],
];
}
foreach ( $blocks as $block ) {
$replaced_block = null;
foreach ( $block['items'] as $datum ) {
if ( ! key_exists( 'selector', $datum ) || ! key_exists( 'original', $datum ) || ! key_exists( 'block', $datum ) || ! key_exists( 'hash', $datum ) ) {
return $css;
}
$original_block = $datum['block'];
$replaced_block = $replaced_block ?: $datum['block'];
$url = $datum['original'];
$hash = $datum['hash'];
$placeholder = "--wpr-bg-$hash";
$variable_placeholder = "var($placeholder)";
$replaced_block = str_replace( $url, $variable_placeholder, $replaced_block );
}
$css = str_replace( $original_block, $replaced_block, $css );
}
return $css;
}
}
@@ -0,0 +1,50 @@
<?php
namespace WP_Rocket\Engine\Media\Lazyload\CSS\Front;
class TagGenerator {
/**
* Generate tags from the mapping of lazyloaded images.
*
* @param array $mapping Lazyload images mapping.
* @param array $loaded Excluded images.
* @return string
*/
public function generate( array $mapping, array $loaded = [] ): string {
$mapping = array_filter(
$mapping,
function ( $mapping_item ) use ( $loaded ) {
foreach ( $loaded as $exclude_item ) {
if ( $exclude_item['style'] === $mapping_item['style'] ) {
return false; // Exclude matching elements.
}
}
return true; // Include non-matching elements.
}
);
$mapping = array_values( $mapping );
$loaded_content = '';
foreach ( $loaded as $item ) {
$loaded_content .= $item['style'];
}
$loaded_tag = "<style id=\"wpr-lazyload-bg\"></style><style id=\"wpr-lazyload-bg-exclusion\">$loaded_content</style>";
$nostyle_content = '';
foreach ( $mapping as $item ) {
$nostyle_content .= $item['style'];
}
$nostyle_tag = "<noscript>
<style id=\"wpr-lazyload-bg-nostyle\">$nostyle_content</style>
</noscript>";
$mapping_json = wp_json_encode( $mapping );
$loaded_json = wp_json_encode( $loaded );
$script_content = "const rocket_pairs = $mapping_json; const rocket_excluded_pairs = $loaded_json;";
$script_tag = "<script type=\"application/javascript\">$script_content</script>";
return "$loaded_tag\n$nostyle_tag\n$script_tag";
}
}