Skip to content

Commit

Permalink
Add the ability to debug / force load the JS polyfill.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixarntz committed Oct 30, 2023
1 parent c1682e5 commit c8849b1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
17 changes: 17 additions & 0 deletions fast-smooth-scroll.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ function fast_smooth_scroll_enqueue_scripts() {
return;
}

/*
* Administrators can force the polyfill to load by adding a query parameter `fast_smooth_scroll_debug_polyfill=1`
* to any URL. In this case, the polyfill is unconditionally enqueued.
* It also overrides the default scroll behavior to 'auto' to simulate the experience without 'smooth' scrolling
* configured via CSS.
*/
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( current_user_can( 'manage_options' ) && ! empty( $_GET['fast_smooth_scroll_debug_polyfill'] ) ) {
wp_add_inline_script(
'fast-smooth-scroll-scroll-behavior-polyfill',
'document.documentElement.style.scrollBehavior = "auto";',
'before'
);
wp_enqueue_script( 'fast-smooth-scroll-scroll-behavior-polyfill' );
return;
}

wp_enqueue_script( 'fast-smooth-scroll-polyfills' );
}
add_action( 'wp_enqueue_scripts', 'fast_smooth_scroll_enqueue_scripts' );
10 changes: 10 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ add_filter( 'fast_smooth_scroll_enqueue_scripts', '__return_false' );

`

= How can I test the JavaScript polyfill? =

Most likely, you are using a modern browser which therefore does not trigger the JavaScript polyfill to load.

If you don't have a legacy browser handy, you can still test the behavior: You'll need to be logged in as an administrator, and then you can add a query parameter `fast_smooth_scroll_debug_polyfill=1` to any URL. For example, in case of the home page:

`
https://my-site.com/?fast_smooth_scroll_debug_polyfill=1
`

= Where should I submit my support request? =

For regular support requests, please use the [wordpress.org support forums](https://wordpress.org/support/plugin/fast-smooth-scroll). If you have a technical issue with the plugin where you already have more insight on how to fix it, you can also [open an issue on GitHub instead](https://github.com/felixarntz/fast-smooth-scroll/issues).
Expand Down
53 changes: 53 additions & 0 deletions tests/phpunit/tests/Plugin_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,57 @@ public function test_fast_smooth_scroll_enqueue_scripts_filter() {
$this->assertTrue( $loader_registered );
$this->assertFalse( $loader_enqueued );
}

public function test_fast_smooth_scroll_enqueue_scripts_with_debug_polyfill_param() {
global $wp_scripts;

// Store original `$wp_scripts`, then reset it.
$orig_wp_scripts = wp_scripts();
$wp_scripts = null;

// Set the query parameter. It shouldn't change any behavior by itself.
$_GET['fast_smooth_scroll_debug_polyfill'] = '1';

fast_smooth_scroll_register_scripts();
fast_smooth_scroll_enqueue_scripts();

$polyfill_enqueued = wp_script_is( 'fast-smooth-scroll-scroll-behavior-polyfill', 'enqueued' );
$loader_enqueued = wp_script_is( 'fast-smooth-scroll-polyfills', 'enqueued' );
$polyfill_inline_script = wp_scripts()->get_inline_script_data( 'fast-smooth-scroll-scroll-behavior-polyfill', 'before' );

// Restore original `$wp_scripts`.
$wp_scripts = $orig_wp_scripts;

// Ensure that still only the loader is enqueued, as the query parameter can only be used by administrators.
$this->assertFalse( $polyfill_enqueued );
$this->assertTrue( $loader_enqueued );
$this->assertSame( '', $polyfill_inline_script );
}

public function test_fast_smooth_scroll_enqueue_scripts_with_debug_polyfill_param_and_administrator() {
global $wp_scripts;

// Store original `$wp_scripts`, then reset it.
$orig_wp_scripts = wp_scripts();
$wp_scripts = null;

// Set the current user to an administrator, and set the query parameter. This should force load the polyfill.
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$_GET['fast_smooth_scroll_debug_polyfill'] = '1';

fast_smooth_scroll_register_scripts();
fast_smooth_scroll_enqueue_scripts();

$polyfill_enqueued = wp_script_is( 'fast-smooth-scroll-scroll-behavior-polyfill', 'enqueued' );
$loader_enqueued = wp_script_is( 'fast-smooth-scroll-polyfills', 'enqueued' );
$polyfill_inline_script = wp_scripts()->get_inline_script_data( 'fast-smooth-scroll-scroll-behavior-polyfill', 'before' );

// Restore original `$wp_scripts`.
$wp_scripts = $orig_wp_scripts;

// Ensure that the polyfill is now force enqueued instead of the loader, and the extra inline script is added.
$this->assertTrue( $polyfill_enqueued );
$this->assertFalse( $loader_enqueued );
$this->assertSame( 'document.documentElement.style.scrollBehavior = "auto";', $polyfill_inline_script );
}
}

0 comments on commit c8849b1

Please sign in to comment.