Skip to content

Commit 506305d

Browse files
authored
Merge pull request #225 from newfold-labs/enhance/PRESS7-456-Enable-Plan-Based-Link-Prefetch-Behavior
Enhance/press7-456 enable plan based link prefetch behavior
2 parents e3ab303 + 0a01618 commit 506305d

File tree

5 files changed

+70
-27
lines changed

5 files changed

+70
-27
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '126efbd4b855c1411ec8');
1+
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '09a6845d879779c5f31a');

build/performance/performance.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/LinkPrefetch/LinkPrefetch.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace NewfoldLabs\WP\Module\Performance\LinkPrefetch;
44

5+
use NewfoldLabs\WP\Module\Data\SiteCapabilities;
56
use NewfoldLabs\WP\ModuleLoader\Container;
67

78
/**
@@ -37,6 +38,20 @@ class LinkPrefetch {
3738
*/
3839
public static $option_name = 'nfd_link_prefetch_settings';
3940

41+
/**
42+
* Site capabilities for link prefetch Click.
43+
*
44+
* @var bool
45+
*/
46+
public static $has_link_prefetch_click = false;
47+
48+
/**
49+
* Site capabilities for link prefetch Hover.
50+
*
51+
* @var bool
52+
*/
53+
public static $has_link_prefetch_hover = false;
54+
4055
/**
4156
* Default settings.
4257
*
@@ -59,6 +74,17 @@ class LinkPrefetch {
5974
*/
6075
public function __construct( Container $container ) {
6176
$this->container = $container;
77+
78+
$capabilities = ( new SiteCapabilities() )->all();
79+
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;
82+
83+
if ( false === $this::$has_link_prefetch_click && false === $this::$has_link_prefetch_hover ) {
84+
delete_option( self::$option_name );
85+
return;
86+
}
87+
6288
add_filter( 'newfold-runtime', array( $this, 'add_to_runtime' ) );
6389
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
6490
if ( ! is_admin() ) {
@@ -74,6 +100,9 @@ public function __construct( Container $container ) {
74100
* @return array Modified runtime object.
75101
*/
76102
public function add_to_runtime( $sdk ) {
103+
104+
self::$default_settings['behavior'] = $this::$has_link_prefetch_click && $this::$has_link_prefetch_hover ? 'mouseHover' : 'mouseDown';
105+
77106
$values = array(
78107
'settings' => get_option( self::$option_name, self::$default_settings ),
79108
);

src/components/App/index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import '../../styles/styles.css';
44

55
// Newfold
66
import { Container, Root, Page, Title } from '@newfold/ui-component-library';
7-
import { NewfoldRuntime } from '@newfold/wp-module-runtime';
87

98
// WordPress
109
import { useEffect } from '@wordpress/element';
@@ -23,7 +22,12 @@ import getAppText from './getAppText';
2322
import FontOptimization from '../../sections/FontOptimization';
2423

2524
const App = () => {
25+
2626
const { title, description } = getAppText();
27+
const capabilities = NewfoldRuntime.capabilities || {};
28+
29+
const hasLinkPrefetchClick = capabilities.hasOwnProperty('hasLinkPrefetchClick') ? capabilities.hasLinkPrefetchClick : true;
30+
const hasLinkPrefetchHover = capabilities.hasOwnProperty('hasLinkPrefetchHover') ? capabilities.hasLinkPrefetchHover : true;
2731

2832
useEffect( () => {
2933
const brand = NewfoldRuntime.sdk?.plugin?.brand;
@@ -69,12 +73,14 @@ const App = () => {
6973
>
7074
<JetpackBoost />
7175
</Container.Block>
72-
<Container.Block
73-
separator
74-
className="newfold-link-prefetch"
75-
>
76-
<LinkPrefetch />
77-
</Container.Block>
76+
{ ( false !== hasLinkPrefetchClick || false !== hasLinkPrefetchHover ) && (
77+
<Container.Block
78+
separator
79+
className="newfold-link-prefetch"
80+
>
81+
<LinkPrefetch hasLinkPrefetchClick={hasLinkPrefetchClick} hasLinkPrefetchHover={hasLinkPrefetchHover}/>
82+
</Container.Block>
83+
) }
7884
<Container.Block
7985
separator
8086
className="newfold-image-optimization"

src/sections/LinkPrefetch/index.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const useUpdateEffect = ( effect, deps ) => {
2626
}, deps );
2727
};
2828

29-
const LinkPrefetch = () => {
29+
const LinkPrefetch = ( { hasLinkPrefetchClick, hasLinkPrefetchHover } ) => {
3030
const {
3131
linkPrefetchTitle,
3232
linkPrefetchDescription,
@@ -149,14 +149,18 @@ const LinkPrefetch = () => {
149149
}
150150
className="nfd-mb-6"
151151
>
152-
<SelectField.Option
153-
label={ linkPrefetchBehaviorMouseHoverLabel }
154-
value="mouseHover"
155-
/>
156-
<SelectField.Option
157-
label={ linkPrefetchBehaviorMouseDownLabel }
158-
value="mouseDown"
159-
/>
152+
{ hasLinkPrefetchHover && (
153+
<SelectField.Option
154+
label={ linkPrefetchBehaviorMouseHoverLabel }
155+
value="mouseHover"
156+
/>
157+
)}
158+
{ hasLinkPrefetchClick && (
159+
<SelectField.Option
160+
label={ linkPrefetchBehaviorMouseDownLabel }
161+
value="mouseDown"
162+
/>
163+
) }
160164
</SelectField>
161165
) }
162166

@@ -208,14 +212,18 @@ const LinkPrefetch = () => {
208212
}
209213
className="nfd-mb-6"
210214
>
211-
<SelectField.Option
212-
label={ linkPrefetchBehaviorMobileTouchstartLabel }
213-
value="touchstart"
214-
/>
215-
<SelectField.Option
216-
label={ linkPrefetchBehaviorMobileViewportLabel }
217-
value="viewport"
218-
/>
215+
{ hasLinkPrefetchClick && (
216+
<SelectField.Option
217+
label={ linkPrefetchBehaviorMobileTouchstartLabel }
218+
value="touchstart"
219+
/>
220+
)}
221+
{ hasLinkPrefetchHover && (
222+
<SelectField.Option
223+
label={ linkPrefetchBehaviorMobileViewportLabel }
224+
value="viewport"
225+
/>
226+
)}
219227
</SelectField>
220228
) }
221229

0 commit comments

Comments
 (0)