Skip to content

Commit 969c44c

Browse files
committed
Fix Link Prefetch not working on first load
1 parent 620f01f commit 969c44c

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

includes/LinkPrefetch/LinkPrefetch.php

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ public function __construct( Container $container ) {
7777

7878
$capabilities = ( new SiteCapabilities() )->all();
7979

80-
$this::$has_link_prefetch_click = array_key_exists( 'hasLinkPrefetchClick', $capabilities ) ? $capabilities['hasLinkPrefetchClick'] : null;
81-
$this::$has_link_prefetch_hover = array_key_exists( 'hasLinkPrefetchHover', $capabilities ) ? $capabilities['hasLinkPrefetchHover'] : null;
80+
self::$has_link_prefetch_click = array_key_exists( 'hasLinkPrefetchClick', $capabilities ) ? $capabilities['hasLinkPrefetchClick'] : null;
81+
self::$has_link_prefetch_hover = array_key_exists( 'hasLinkPrefetchHover', $capabilities ) ? $capabilities['hasLinkPrefetchHover'] : null;
8282

83-
if ( false === $this::$has_link_prefetch_click && false === $this::$has_link_prefetch_hover ) {
83+
if ( false === self::$has_link_prefetch_click && false === self::$has_link_prefetch_hover ) {
8484
delete_option( self::$option_name );
8585
return;
8686
}
@@ -93,34 +93,48 @@ public function __construct( Container $container ) {
9393
}
9494

9595
/**
96-
* Adds values to the runtime object.
97-
*
98-
* @param array $sdk The runtime object.
96+
* Retrieves the current plugin settings from the options table.
97+
* If no settings are stored, returns and saves the default settings
98+
* based on feature flags.
9999
*
100-
* @return array Modified runtime object.
100+
* @return array The current or default settings.
101101
*/
102-
public function add_to_runtime( $sdk ) {
102+
public function get_current_settings() {
103103
$current_settings = get_option( self::$option_name, false );
104+
if ( false !== $current_settings ) {
105+
return $current_settings;
106+
}
104107

105-
if ( false === $current_settings ) {
106-
if ( $this::$has_link_prefetch_click || $this::$has_link_prefetch_hover ) {
107-
self::$default_settings['activeOnDesktop'] = true;
108-
self::$default_settings['activeOnMobile'] = true;
109-
}
110-
111-
if ( $this::$has_link_prefetch_click ) {
112-
self::$default_settings['behavior'] = 'mouseDown';
113-
self::$default_settings['mobileBehavior'] = 'touchstart';
114-
}
108+
$final_settings = self::$default_settings;
109+
if ( self::$has_link_prefetch_click || self::$has_link_prefetch_hover ) {
110+
$final_settings['activeOnDesktop'] = true;
111+
$final_settings['activeOnMobile'] = true;
112+
}
115113

116-
if ( $this::$has_link_prefetch_hover ) {
117-
self::$default_settings['behavior'] = 'mouseHover';
118-
self::$default_settings['mobileBehavior'] = 'viewport';
119-
}
114+
if ( self::$has_link_prefetch_click ) {
115+
$final_settings['behavior'] = 'mouseDown';
116+
$final_settings['mobileBehavior'] = 'touchstart';
117+
}
120118

121-
$current_settings = self::$default_settings;
119+
if ( self::$has_link_prefetch_hover ) {
120+
$final_settings['behavior'] = 'mouseHover';
121+
$final_settings['mobileBehavior'] = 'viewport';
122122
}
123123

124+
update_option( self::$option_name, $final_settings );
125+
return $final_settings;
126+
}
127+
128+
/**
129+
* Adds values to the runtime object.
130+
*
131+
* @param array $sdk The runtime object.
132+
*
133+
* @return array Modified runtime object.
134+
*/
135+
public function add_to_runtime( $sdk ) {
136+
$current_settings = $this->get_current_settings();
137+
124138
return array_merge(
125139
$sdk,
126140
array( 'linkPrefetch' => array( 'settings' => $current_settings ) )
@@ -133,10 +147,7 @@ public function add_to_runtime( $sdk ) {
133147
* @return void
134148
*/
135149
public function enqueue_scripts() {
136-
$settings = get_option( self::$option_name, self::$default_settings );
137-
if ( ! $settings['activeOnDesktop'] && ! $settings['activeOnMobile'] ) {
138-
return;
139-
}
150+
$settings = $this->get_current_settings();
140151
$settings['isMobile'] = wp_is_mobile();
141152
wp_enqueue_script(
142153
'linkprefetcher',

0 commit comments

Comments
 (0)