Skip to content

Commit

Permalink
3.15.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabrisrp authored Jan 4, 2024
2 parents fa9eeed + f471c32 commit dc721cd
Show file tree
Hide file tree
Showing 40 changed files with 328 additions and 169 deletions.
5 changes: 3 additions & 2 deletions assets/js/lazyload-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function rocket_css_lazyload_launch() {
const usable_pairs = typeof rocket_pairs === 'undefined' ? [] : rocket_pairs;


const styleElement = document.querySelector('#wpr-lazyload-bg');
const styleElement = document.querySelector('#wpr-lazyload-bg-container');

const threshold = rocket_lazyload_css_data.threshold || 300;

Expand All @@ -13,7 +13,8 @@ function rocket_css_lazyload_launch() {
const pairs = usable_pairs.filter(s => entry.target.matches(s.selector));
pairs.map(pair => {
if (pair) {
styleElement.innerHTML += pair.style;
styleElement.innerHTML += `<style>${pair.style}</style>`;

pair.elements.forEach(el => {
// Stop observing the target element
observer.unobserve(el);
Expand Down
3 changes: 2 additions & 1 deletion assets/js/lazyload-css.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/js/lazyload-css.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dynamic-lists-delayjs.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dynamic-lists.json

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions inc/Engine/Common/Cache/FilesystemCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@ public function has( $key ) {
*/
public function generate_url( string $url ): string {
$path = $this->generate_path( $url );
if ( ! $this->filesystem->exists( $path ) ) {
return $url;
}

$wp_content_dir = rocket_get_constant( 'WP_CONTENT_DIR' );

Expand Down
64 changes: 56 additions & 8 deletions inc/Engine/Media/Lazyload/CSS/Front/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class Extractor {
* Extract background images from CSS.
*
* @param string $content CSS content.
* @param string $file_url File we are extracting css from.
* @return array
*/
public function extract( string $content ): array {
public function extract( string $content, string $file_url = '' ): array {

$this->comments_mapping = [];

Expand Down Expand Up @@ -77,7 +78,7 @@ function ( $matches ) {
$property = trim( $property );
$selector = trim( $selector );

$urls = $this->extract_urls( $property );
$urls = $this->extract_urls( $property, $file_url );
$block = trim( $match[0] );
foreach ( $this->comments_mapping as $id => $comment ) {
$block = str_replace( $id, $comment, $block );
Expand All @@ -100,9 +101,10 @@ function ( $matches ) {
* Extract URLS from a CSS property.
*
* @param string $content Content from the CSS property.
* @param string $file_url URL of the css file.
* @return array
*/
protected function extract_urls( string $content ): array {
protected function extract_urls( string $content, string $file_url = '' ): array {

/**
* Lazyload URL regex.
Expand All @@ -128,7 +130,7 @@ protected function extract_urls( string $content ): array {
$url = str_replace( '"', '', $url );
$url = str_replace( "'", '', $url );
$url = trim( $url );
$url = $this->make_url_complete( $url );
$url = $this->make_url_complete( $url, $file_url );
if ( ! key_exists( 'tag', $match ) || ! key_exists( 'url', $match ) || ! $url || $this->is_url_ignored( $url, $ignored_urls ) ) {
continue;
}
Expand All @@ -137,7 +139,6 @@ protected function extract_urls( string $content ): array {
$url = $this->apply_string_filter( 'css_url', $url );
}

$url = "url('$url')";
$original_url = trim( $match['tag'], ' ,' );
$output[] = [
'url' => $url,
Expand Down Expand Up @@ -187,19 +188,66 @@ protected function is_url_ignored( string $url, array $ignored_urls ): bool {
* Complete the URL if necessary.
*
* @param string $url URL to complete.
* @param string $file_url URL of the CSS File.
*
* @return string
*/
protected function make_url_complete( string $url ): string {
protected function make_url_complete( string $url, string $file_url ): string {
$host = wp_parse_url( $url, PHP_URL_HOST );

if ( $host || $this->is_relative( $url ) ) {
return $url;
if ( $host || $this->is_relative( $url ) && ! empty( $file_url ) ) {
return $this->transform_relative_to_absolute( $url, $file_url );
}

return rocket_get_home_url() . '/' . trim( $url, '/ ' );
}


/**
* Transform a relative URL to an absolute URL based on the base URL.
*
* @param string $rel Relative URL to transform.
* @param string $base Base URL.
*
* @return string
*/
protected function transform_relative_to_absolute( string $rel, string $base = '' ): string {
if ( ! is_null( wp_parse_url( $rel, PHP_URL_SCHEME ) ) ) {
return $rel;
}

if ( '#' === $rel[0] || '?' === $rel[0] ) {
return $base . $rel;
}

$base_url_parts = wp_parse_url( $base );
$path = ! empty( $base_url_parts['path'] ) ? $base_url_parts['path'] : '/';

$path = preg_replace( '#/[^/]*$#', '', $path );

if ( '/' === $rel[0] ) {
$path = '';
}

$abs = "$path/$rel";

$re = [ '#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#' ];

$dots_count = substr_count( $rel, '..' );

foreach ( range( 1, $dots_count ) as $n ) {
$abs_before = $abs;
$abs = preg_replace( $re, '/', $abs, -1, $n );

if ( $abs_before === $abs ) {
break;
}
}

return trailingslashit( rocket_get_home_url() ) . ltrim( $abs, '/' );
}


/**
* Check if the URL is external.
*
Expand Down
4 changes: 3 additions & 1 deletion inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public function format( array $data ): array {
$url = $datum['url'];

$placeholder = "--wpr-bg-$hash";
$variable_placeholder = ':root{' . $placeholder . ': ' . $url . ';}';
$img_url = "url('$url')";
$variable_placeholder = $datum['selector'] . '{' . $placeholder . ': ' . $img_url . ';}';
$formatted_urls[] = [
'selector' => $selector,
'style' => $variable_placeholder,
'hash' => $hash,
'url' => $url,
];
}

Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Media/Lazyload/CSS/Front/TagGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function ( $mapping_item ) use ( $loaded ) {
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>";
$loaded_tag = "<style id=\"wpr-lazyload-bg-container\"></style><style id=\"wpr-lazyload-bg-exclusion\">$loaded_content</style>";

$nostyle_content = '';
foreach ( $mapping as $item ) {
Expand Down
7 changes: 4 additions & 3 deletions inc/Engine/Media/Lazyload/CSS/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ protected function generate_css_file( string $url ) {
return [];
}

$output = $this->generate_content( $content );
$output = $this->generate_content( $content, $this->cache->generate_url( $url ) );

if ( count( $output->get_urls() ) === 0 ) {
return [];
Expand All @@ -434,10 +434,11 @@ protected function generate_css_file( string $url ) {
* Generate lazy content for a certain content.
*
* @param string $content Content to generate lazy for.
* @param string $url URL of the file we are extracting content from.
* @return LazyloadedContent
*/
protected function generate_content( string $content ): LazyloadedContent {
$urls = $this->extractor->extract( $content );
protected function generate_content( string $content, string $url = '' ): LazyloadedContent {
$urls = $this->extractor->extract( $content, $url );
$formatted_urls = [];
foreach ( $urls as $url_tags ) {
$url_tags = $this->add_hashes( $url_tags );
Expand Down
6 changes: 6 additions & 0 deletions inc/Engine/Optimization/RUCSS/AbstractAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ private function check_response( $response ): bool {
if ( 200 !== $this->response_code ) {
$previous_errors = (int) get_transient( 'wp_rocket_rucss_errors_count' );
set_transient( 'wp_rocket_rucss_errors_count', $previous_errors + 1, 5 * MINUTE_IN_SECONDS );

if ( empty( $response ) ) {
$this->error_message = 'API Client Error';
return false;
}

$this->error_message = is_array( $response )
? wp_remote_retrieve_response_message( $response )
: $response->get_error_message();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function execute( object $row_details, array $job_details ): void {
return;
}

$this->used_css_query->increment_retries( $row_details->id, (int) $row_details->retries, $job_details['message'] );
$this->used_css_query->increment_retries( $row_details->id, (int) $job_details['code'], $job_details['message'] );

$rucss_retry_duration = $this->time_table_retry[ $row_details->retries ] ?? $this->default_waiting_retry; // Default to 30 minutes.

Expand Down
20 changes: 18 additions & 2 deletions inc/Engine/Preload/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ public function update_cache_row() {
return;
}

if ( (bool) ! $this->options->get( 'manual_preload', true ) ) {
return; // Bail out if preload is disabled.
}

$url = home_url( add_query_arg( [], $wp->request ) );

$detected = $this->mobile_detect->isMobile() && ! $this->mobile_detect->isTablet() ? 'mobile' : 'desktop';
Expand Down Expand Up @@ -238,6 +242,10 @@ public function delete_url_on_not_found() {
public function on_permalink_changed() {
$this->query->remove_all();
$this->queue->cancel_pending_jobs();
if ( ! $this->options->get( 'manual_preload', false ) ) {
return;
}

$this->queue->add_job_preload_job_load_initial_sitemap_async();
}

Expand Down Expand Up @@ -272,7 +280,9 @@ public function on_deactivation() {
* @return void
*/
public function clean_url( string $url ) {

if ( ! $this->options->get( 'manual_preload', 0 ) ) {
return;
}
$this->clear_cache->partial_clean( [ $url ] );
}

Expand All @@ -296,6 +306,10 @@ public function clean_full_cache() {
* @return void
*/
public function clean_partial_cache( $object, array $urls, $lang ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.objectFound
if ( ! $this->options->get( 'manual_preload', false ) ) {
return;
}

// Add Homepage URL to $purge_urls for preload.
$urls[] = get_rocket_i18n_home_url( $lang );

Expand All @@ -310,6 +324,9 @@ public function clean_partial_cache( $object, array $urls, $lang ) { // phpcs:ig
* @return void
*/
public function clean_urls( array $urls ) {
if ( ! $this->options->get( 'manual_preload', 0 ) ) {
return;
}

$this->clear_cache->partial_clean( $urls );
}
Expand Down Expand Up @@ -465,7 +482,6 @@ public function add_preload_excluded_uri( $regexes ): array {
* @return void
*/
public function remove_private_post( string $new_status, string $old_status, $post ) {

if ( $new_status === $old_status ) {
return;
}
Expand Down
20 changes: 15 additions & 5 deletions inc/ThirdParty/Plugins/ContactForm7.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ContactForm7 implements Subscriber_Interface {
*/
public static function get_subscribed_events() {
return [
'template_redirect' => [ 'maybe_optimize_contact_form_7', 10 ],
'template_redirect' => 'maybe_optimize_contact_form_7',
];
}

Expand All @@ -33,8 +33,8 @@ public function maybe_optimize_contact_form_7() {
}

// Force scripts and styles to not load by default.
add_filter( 'wpcf7_load_js', '__return_false', PHP_INT_MAX );
add_filter( 'wpcf7_load_css', '__return_false', PHP_INT_MAX );
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );

// Conditionally enqueue scripts.
add_action( 'wpcf7_shortcode_callback', [ $this, 'conditionally_enqueue_scripts' ] );
Expand All @@ -45,17 +45,27 @@ public function maybe_optimize_contact_form_7() {
* Enqueue scripts if not already enqueued.
*/
public function conditionally_enqueue_scripts() {
if ( ! did_action( 'wpcf7_enqueue_scripts' ) ) { // Prevent double-enqueueing when multiple forms present.
if ( did_action( 'wpcf7_enqueue_scripts' ) ) { // Prevent double-enqueueing when multiple forms present.
return;
}
if ( did_action( 'wp_enqueue_scripts' ) ) {
wpcf7_enqueue_scripts();
return;
}
add_filter( 'wpcf7_load_js', '__return_true', 11 );
}

/**
* Enqueue styles if not already enqueued.
*/
public function conditionally_enqueue_styles() {
if ( ! did_action( 'wpcf7_enqueue_styles' ) ) { // Prevent double-enqueueing when multiple forms present.
if ( did_action( 'wpcf7_enqueue_styles' ) ) { // Prevent double-enqueueing when multiple forms present.
return;
}
if ( did_action( 'wp_enqueue_scripts' ) ) {
wpcf7_enqueue_styles();
return;
}
add_filter( 'wpcf7_load_css', '__return_true', 11 );
}
}
3 changes: 2 additions & 1 deletion inc/common/admin-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function rocket_admin_bar( $wp_admin_bar ) {
*
* @param string $uri Current uri
*/
$referer = (string) apply_filters( 'rocket_admin_bar_referer', esc_url( $uri ) );
$referer = (string) apply_filters( 'rocket_admin_bar_referer', $uri );
$referer = esc_url_raw( $referer );
$referer = '&_wp_http_referer=' . rawurlencode( remove_query_arg( 'fl_builder', $referer ) );
} else {
$referer = '';
Expand Down
8 changes: 8 additions & 0 deletions inc/functions/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ function rocket_url_to_postid( string $url ) {

$query['post_status'] = [ 'publish', 'private' ];

/**
* Filters WP_Query class passed args.
*
* @param array $query WP_Query passed args.
* @param string $url The URL to derive the post ID from.
*/
$query = (array) apply_filters( 'rocket_url_to_postid_query_args', $query, $url );

// Do the query.
$query = new WP_Query( $query );
if ( ! empty( $query->posts ) && $query->is_singular ) {
Expand Down
Loading

0 comments on commit dc721cd

Please sign in to comment.