@@ -77,10 +77,10 @@ public function __construct( Container $container ) {
77
77
78
78
$ capabilities = ( new SiteCapabilities () )->all ();
79
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 ;
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 ;
82
82
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 ) {
84
84
delete_option ( self ::$ option_name );
85
85
return ;
86
86
}
@@ -93,34 +93,48 @@ public function __construct( Container $container ) {
93
93
}
94
94
95
95
/**
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 .
99
99
*
100
- * @return array Modified runtime object .
100
+ * @return array The current or default settings .
101
101
*/
102
- public function add_to_runtime ( $ sdk ) {
102
+ public function get_current_settings ( ) {
103
103
$ current_settings = get_option ( self ::$ option_name , false );
104
+ if ( false !== $ current_settings ) {
105
+ return $ current_settings ;
106
+ }
104
107
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
+ }
115
113
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
+ }
120
118
121
- $ current_settings = self ::$ default_settings ;
119
+ if ( self ::$ has_link_prefetch_hover ) {
120
+ $ final_settings ['behavior ' ] = 'mouseHover ' ;
121
+ $ final_settings ['mobileBehavior ' ] = 'viewport ' ;
122
122
}
123
123
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
+
124
138
return array_merge (
125
139
$ sdk ,
126
140
array ( 'linkPrefetch ' => array ( 'settings ' => $ current_settings ) )
@@ -133,10 +147,7 @@ public function add_to_runtime( $sdk ) {
133
147
* @return void
134
148
*/
135
149
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 ();
140
151
$ settings ['isMobile ' ] = wp_is_mobile ();
141
152
wp_enqueue_script (
142
153
'linkprefetcher ' ,
0 commit comments