Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
LinkPrefetchCommandHandler
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 9
342
0.00% covered (danger)
0.00%
0 / 1
 update_single_setting
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 validate_status
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 active_desktop
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 active_mobile
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 instant_click
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 behavior
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 hover_delay
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 mobile_behavior
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 ignore_keywords
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\Module\Performance\LinkPrefetch\WPCLI;
4
5use NewfoldLabs\WP\Module\Performance\NFD_WPCLI;
6use NewfoldLabs\WP\Module\Performance\LinkPrefetch\LinkPrefetch;
7
8/**
9 * Handles WP-CLI commands for Link Prefetch settings.
10 */
11class LinkPrefetchCommandHandler {
12
13    /**
14     * Allowed status values.
15     *
16     * @var array
17     */
18    private const VALID_STATUSES = array( 'on', 'off' );
19
20    /**
21     * Updates a single setting value.
22     *
23     * @param string $key   The setting key to update.
24     * @param mixed  $value The new value.
25     *
26     * @return void
27     */
28    private function update_single_setting( $key, $value ) {
29        $settings         = LinkPrefetch::get_settings();
30        $settings[ $key ] = $value;
31        LinkPrefetch::update_settings( $settings );
32    }
33
34    /**
35     * Validates that a status value is either 'on' or 'off'.
36     *
37     * @param string|null $status The status value.
38     *
39     * @return string The validated status.
40     */
41    private function validate_status( $status ) {
42        if ( empty( $status ) ) {
43            NFD_WPCLI::error( __( "A status ('on' or 'off') is required.", 'wp-module-performance' ) );
44        }
45        $status = strtolower( $status );
46        if ( ! in_array( $status, self::VALID_STATUSES, true ) ) {
47            NFD_WPCLI::error( __( "Invalid status: Use 'on' or 'off'.", 'wp-module-performance' ) );
48        }
49        return $status;
50    }
51
52    /**
53     * Toggles the activeOnDesktop setting.
54     *
55     * ## OPTIONS
56     *
57     * <status>
58     * : Enable or disable activeOnDesktop. Accepts 'on' or 'off'.
59     *
60     * ## EXAMPLES
61     *
62     *     wp nfd performance link_prefetch active_desktop on
63     *     wp nfd performance link_prefetch active_desktop off
64     *
65     * @param array $args Positional arguments.
66     *
67     * @return void
68     */
69    public function active_desktop( $args ) {
70        $status = $this->validate_status( $args[0] ?? null );
71        $this->update_single_setting( 'activeOnDesktop', ( 'on' === $status ) );
72        NFD_WPCLI::success(
73            sprintf(
74                /* translators: %s is the on/off status. */
75                __( "Setting 'activeOnDesktop' has been turned %s.", 'wp-module-performance' ),
76                $status
77            )
78        );
79    }
80
81    /**
82     * Toggles the activeOnMobile setting.
83     *
84     * ## OPTIONS
85     *
86     * <status>
87     * : Enable or disable activeOnMobile. Accepts 'on' or 'off'.
88     *
89     * ## EXAMPLES
90     *
91     *     wp nfd performance link_prefetch active_mobile on
92     *     wp nfd performance link_prefetch active_mobile off
93     *
94     * @param array $args Positional arguments.
95     *
96     * @return void
97     */
98    public function active_mobile( $args ) {
99        $status = $this->validate_status( $args[0] ?? null );
100        $this->update_single_setting( 'activeOnMobile', ( 'on' === $status ) );
101        NFD_WPCLI::success(
102            sprintf(
103                /* translators: %s is the on/off status. */
104                __( "Setting 'activeOnMobile' has been turned %s.", 'wp-module-performance' ),
105                $status
106            )
107        );
108    }
109
110    /**
111     * Toggles the instantClick setting.
112     *
113     * ## OPTIONS
114     *
115     * <status>
116     * : Enable or disable instantClick. Accepts 'on' or 'off'.
117     *
118     * ## EXAMPLES
119     *
120     *     wp nfd performance link_prefetch instant_click on
121     *     wp nfd performance link_prefetch instant_click off
122     *
123     * @param array $args Positional arguments.
124     *
125     * @return void
126     */
127    public function instant_click( $args ) {
128        $status = $this->validate_status( $args[0] ?? null );
129        $this->update_single_setting( 'instantClick', ( 'on' === $status ) );
130        NFD_WPCLI::success(
131            sprintf(
132                /* translators: %s is the on/off status. */
133                __(
134                    "Setting 'instantClick' has been turned %s.",
135                    'wp-module-performance'
136                ),
137                $status
138            )
139        );
140    }
141
142    /**
143     * Sets the behavior setting.
144     *
145     * ## OPTIONS
146     *
147     * <value>
148     * : The desired behavior value. (mouseHover, mouseDown)
149     *
150     * ## EXAMPLES
151     *
152     *     wp nfd performance link_prefetch behavior mouseHover
153     *
154     * @param array $args Positional arguments.
155     *
156     * @return void
157     */
158    public function behavior( $args ) {
159        if ( empty( $args[0] ) ) {
160            NFD_WPCLI::error( __( 'A behavior value is required.', 'wp-module-performance' ) );
161        }
162        $value = $args[0];
163        if ( ! in_array( $value, LinkPrefetch::VALID_BEHAVIORS, true ) ) {
164            NFD_WPCLI::error(
165                sprintf(
166                    /* translators: %s is the list of valid mobile behaviors. */
167                    __( 'Invalid behavior: Use one of the following - %s.', 'wp-module-performance' ),
168                    implode( ', ', LinkPrefetch::VALID_BEHAVIORS )
169                )
170            );
171        }
172        $this->update_single_setting( 'behavior', $value );
173        NFD_WPCLI::success(
174            sprintf(
175                /* translators: %s is the new behavior value. */
176                __( "Setting 'behavior' has been set to '%s'.", 'wp-module-performance' ),
177                $value
178            )
179        );
180    }
181
182    /**
183     * Sets the hoverDelay setting.
184     *
185     * ## OPTIONS
186     *
187     * <value>
188     * : The numeric value for hoverDelay.
189     *
190     * ## EXAMPLES
191     *
192     *     wp nfd performance link_prefetch hover_delay 80
193     *
194     * @param array $args Positional arguments.
195     *
196     * @return void
197     */
198    public function hover_delay( $args ) {
199        if ( empty( $args[0] ) || ! is_numeric( $args[0] ) ) {
200            NFD_WPCLI::error( __( 'A numeric value for hoverDelay is required.', 'wp-module-performance' ) );
201        }
202        $value = (int) $args[0];
203        $this->update_single_setting( 'hoverDelay', $value );
204        NFD_WPCLI::success(
205            sprintf(
206                /* translators: %s is the numeric value for hoverDelay. */
207                __( "Setting 'hoverDelay' has been set to '%s'.", 'wp-module-performance' ),
208                $value
209            )
210        );
211    }
212
213    /**
214     * Sets the mobileBehavior setting.
215     *
216     * ## OPTIONS
217     *
218     * <value>
219     * : The desired mobile behavior value. (touchstart, viewport)
220     *
221     * ## EXAMPLES
222     *
223     *     wp nfd performance link_prefetch mobile_behavior touchstart
224     *
225     * @param array $args Positional arguments.
226     *
227     * @return void
228     */
229    public function mobile_behavior( $args ) {
230        if ( empty( $args[0] ) ) {
231            NFD_WPCLI::error( __( 'A mobile behavior value is required.', 'wp-module-performance' ) );
232        }
233        $value = $args[0];
234        if ( ! in_array( $value, LinkPrefetch::VALID_MOBILE_BEHAVIORS, true ) ) {
235            NFD_WPCLI::error(
236                sprintf(
237                    /* translators: %s is the list of valid mobile behaviors. */
238                    __( 'Invalid behavior: Use one of the following - %s.', 'wp-module-performance' ),
239                    implode( ', ', LinkPrefetch::VALID_MOBILE_BEHAVIORS )
240                )
241            );
242        }
243        $this->update_single_setting( 'mobileBehavior', $value );
244        NFD_WPCLI::success(
245            sprintf(
246                /* translators: %s is the new mobile behavior value. */
247                __( "Setting 'mobileBehavior' has been set to '%s'.", 'wp-module-performance' ),
248                $value
249            )
250        );
251    }
252
253    /**
254     * Sets the ignoreKeywords setting.
255     *
256     * ## OPTIONS
257     *
258     * <value>
259     * : The desired value for ignoreKeywords.
260     *
261     * ## EXAMPLES
262     *
263     *     wp nfd performance link_prefetch ignore_keywords "#,?"
264     *
265     * @param array $args Positional arguments.
266     *
267     * @return void
268     */
269    public function ignore_keywords( $args ) {
270        if ( ! isset( $args[0] ) ) {
271            NFD_WPCLI::error( __( 'A value for ignoreKeywords is required.', 'wp-module-performance' ) );
272        }
273
274        // Sanitize the entire input
275        $value = sanitize_text_field( $args[0] );
276
277        // Sanitize each keyword since it's a comma-separated list
278        $keywords = array_map( 'trim', explode( ',', $value ) );
279        $value    = implode( ',', $keywords );
280
281        $this->update_single_setting( 'ignoreKeywords', $value );
282
283        NFD_WPCLI::success(
284            sprintf(
285                /* translators: %s is the new ignoreKeywords value. */
286                __( "Setting 'ignoreKeywords' has been set to '%s'.", 'wp-module-performance' ),
287                $value
288            )
289        );
290    }
291}