PluginProbe
Fast Smooth Scroll / trunk
Fast Smooth Scroll vtrunk
1.0.1 trunk 1.0.0
fast-smooth-scroll / fast-smooth-scroll.php

fast-smooth-scroll.php in Fast Smooth Scroll trunk, at fast-smooth-scroll.php

162 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin main file.
4 *
5 * @package FastSmoothScroll
6 * @author Felix Arntz <hello@felix-arntz.me>
7 *
8 * @wordpress-plugin
9 * Plugin Name: Fast Smooth Scroll
10 * Plugin URI: https://wordpress.org/plugins/fast-smooth-scroll/
11 * Description: This lightweight plugin enhances user experience by enabling smooth scrolling for anchor links without the need for jQuery or other dependencies.
12 * Version: 1.0.1
13 * Requires at least: 6.1
14 * Requires PHP: 7.4
15 * Author: Felix Arntz
16 * Author URI: https://felix-arntz.me
17 * License: GPLv2 or later
18 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
19 * Text Domain: fast-smooth-scroll
20 * Tags: smooth scroll, scroll animation, performance, anchor links, lightweight
21 */
22
23 if ( ! defined( 'ABSPATH' ) ) {
24 exit; // Exit if accessed directly.
25 }
26
27 /**
28 * Prints the inline style tag to set the CSS 'scroll-behavior' property.
29 *
30 * @since 1.0.0
31 */
32 function fast_smooth_scroll_print_style() {
33 $html_rules = array( 'scroll-behavior: smooth' );
34
35 $scroll_offset = fast_smooth_scroll_get_offset();
36 if ( $scroll_offset ) {
37 $html_rules[] = 'scroll-padding-top: ' . (int) $scroll_offset . 'px';
38 }
39
40 ?>
41 <style id="fast-smooth-scroll-css" type="text/css">
42 html { <?php echo esc_js( implode( '; ', $html_rules ) . ';' ); ?> }
43 @media (prefers-reduced-motion: reduce) {
44 html { scroll-behavior: auto; }
45 }
46 </style>
47 <?php
48 }
49 add_action( 'wp_footer', 'fast_smooth_scroll_print_style' );
50
51 /**
52 * Registers the JavaScript polyfills for when the browser doesn't support the CSS 'scroll-behavior' property.
53 *
54 * @since 1.0.0
55 */
56 function fast_smooth_scroll_register_scripts() {
57 global $wp_scripts;
58
59 $script_metadata = require plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
60 $polyfill_src = SCRIPT_DEBUG ? 'src/index.js' : 'build/index.js';
61
62 $scroll_offset = fast_smooth_scroll_get_offset();
63 if ( $scroll_offset ) {
64 $data_script = 'var fastSmoothScrollOffset = ' . (int) $scroll_offset . ';';
65 }
66
67 wp_register_script(
68 'fast-smooth-scroll-scroll-behavior-polyfill',
69 plugin_dir_url( __FILE__ ) . $polyfill_src,
70 $script_metadata['dependencies'],
71 $script_metadata['version'],
72 array( 'in_footer' => true )
73 );
74 if ( isset( $data_script ) ) {
75 wp_add_inline_script( 'fast-smooth-scroll-scroll-behavior-polyfill', $data_script, 'before' );
76 }
77
78 wp_register_script(
79 'fast-smooth-scroll-polyfills',
80 false,
81 array(),
82 null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
83 array( 'in_footer' => true )
84 );
85 if ( isset( $data_script ) ) {
86 wp_add_inline_script( 'fast-smooth-scroll-polyfills', $data_script, 'before' );
87 }
88 wp_add_inline_script(
89 'fast-smooth-scroll-polyfills',
90 wp_get_script_polyfill(
91 $wp_scripts,
92 array(
93 'document.documentElement && document.documentElement.style.scrollBehavior !== undefined && ( document.documentElement.style.scrollBehavior = "smooth" || true ) && document.documentElement.style.scrollBehavior === "smooth" && ( document.documentElement.style.removeProperty( "scroll-behavior" ) || true )' => 'fast-smooth-scroll-scroll-behavior-polyfill',
94 )
95 )
96 );
97 }
98 add_action( 'init', 'fast_smooth_scroll_register_scripts' );
99
100 /**
101 * Enqueues the JavaScript that is conditionally used only if the browser doesn't support the CSS 'scroll-behavior' property.
102 *
103 * @since 1.0.0
104 */
105 function fast_smooth_scroll_enqueue_scripts() {
106 /**
107 * Filters whether the JavaScript polyfills for missing CSS 'scroll-behavior' property support should be enqueued.
108 *
109 * By default, this is the case, which ensures older browsers without support for the CSS property will load a
110 * JavaScript based replacement.
111 *
112 * While this replacement is still a very lightweight and performant implementation that follows modern JavaScript
113 * development best practices, there may still be situations where you want to disable it completely, depending on
114 * the browser support of the website's end users.
115 *
116 * @since 1.0.0
117 *
118 * @param bool $enqueue_scripts Whether to enqueue the JavaScript polyfills. Default true.
119 */
120 if ( ! apply_filters( 'fast_smooth_scroll_enqueue_scripts', true ) ) {
121 return;
122 }
123
124 /*
125 * Administrators can force the polyfill to load by adding a query parameter `fast_smooth_scroll_debug_polyfill=1`
126 * to any URL. In this case, the polyfill is unconditionally enqueued.
127 * It also overrides the default scroll behavior to 'auto' to simulate the experience without 'smooth' scrolling
128 * configured via CSS.
129 */
130 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
131 if ( current_user_can( 'manage_options' ) && ! empty( $_GET['fast_smooth_scroll_debug_polyfill'] ) ) {
132 wp_add_inline_script(
133 'fast-smooth-scroll-scroll-behavior-polyfill',
134 'document.documentElement.style.scrollBehavior = "auto";',
135 'before'
136 );
137 wp_enqueue_script( 'fast-smooth-scroll-scroll-behavior-polyfill' );
138 return;
139 }
140
141 wp_enqueue_script( 'fast-smooth-scroll-polyfills' );
142 }
143 add_action( 'wp_enqueue_scripts', 'fast_smooth_scroll_enqueue_scripts' );
144
145 /**
146 * Returns the scroll offset to use.
147 *
148 * @since 1.0.0
149 *
150 * @return int Scroll offset in pixels.
151 */
152 function fast_smooth_scroll_get_offset() {
153 /**
154 * Filters the scroll offset to use.
155 *
156 * @since 1.0.0
157 *
158 * @param int $offset Scroll offset in pixels. Will only be applied if greater than 0. Default 0.
159 */
160 return (int) apply_filters( 'fast_smooth_scroll_offset', 0 );
161 }
162