Skip to content

Commit

Permalink
Adds filter for Preload Links script configuration parameters. (#3022)
Browse files Browse the repository at this point in the history
* Adds "rocket_preload_links_config" filter to allow 3rd party plugin/theme to modify/tune the feature for a specific use case.

* Abstracts the config to method and merges filtered config with defaults, ie to ensure structure is correct.

* Sets `usesTrailingSlash` to 0 or 1, instead of boolean.
  • Loading branch information
Tonya Mork authored Aug 25, 2020
1 parent 1f6fd15 commit 5fc6b7f
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions inc/Engine/Preload/Links/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,53 @@ public function add_preload_script() {
'rocket-preload-links',
$this->filesystem->get_contents( "{$js_assets_path}{$preload_filename}" )
);

$use_trailing_slash = $this->use_trailing_slash();
$images_ext = 'jpg|jpeg|gif|png|tiff|bmp|webp|avif';
wp_localize_script(
'rocket-preload-links',
'RocketPreloadLinksConfig',
[
'excludeUris' => $this->get_uris_to_exclude( $use_trailing_slash ),
'usesTrailingSlash' => $use_trailing_slash,
'imageExt' => $images_ext,
'fileExt' => $images_ext . '|php|pdf|html|htm',
'siteUrl' => site_url(),
'onHoverDelay' => 100, // milliseconds. -1 disables the "on hover" feature.
'rateThrottle' => 3, // on hover: limits the number of links preloaded per second.
]
$this->get_preload_links_config()
);
}

/**
* Gets the Preload Links script configuration parameters.
*
* @since 3.7
*
* @return string[] Preload Links script configuration parameters.
*/
private function get_preload_links_config() {
$use_trailing_slash = $this->use_trailing_slash();
$images_ext = 'jpg|jpeg|gif|png|tiff|bmp|webp|avif';

$config = [
'excludeUris' => $this->get_uris_to_exclude( $use_trailing_slash ),
'usesTrailingSlash' => (int) $use_trailing_slash,
'imageExt' => $images_ext,
'fileExt' => $images_ext . '|php|pdf|html|htm',
'siteUrl' => site_url(),
'onHoverDelay' => 100, // milliseconds. -1 disables the "on hover" feature.
'rateThrottle' => 3, // on hover: limits the number of links preloaded per second.
];

/**
* Preload Links script configuration parameters.
*
* This array of parameters are passed as RocketPreloadLinksConfig object and used by the
* `preload-links.min.js` script to configure the behavior of the Preload Links feature.
*
* @since 3.7
*
* @param string[] $config Preload Links script configuration parameters.
*/
$filtered_config = apply_filters( 'rocket_preload_links_config', $config );

if ( ! is_array( $filtered_config ) ) {
return $config;
}

return array_merge( $config, $filtered_config );
}

/**
* Gets the URIs to exclude.
*
Expand Down

0 comments on commit 5fc6b7f

Please sign in to comment.