Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
10.00% covered (danger)
10.00%
1 / 10
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Minify_Excludes_State_Entry
10.00% covered (danger)
10.00%
1 / 10
25.00% covered (danger)
25.00%
1 / 4
32.24
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 set
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 sanitize_value
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace Automattic\Jetpack_Boost\Data_Sync;
3
4use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Get;
5use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Set;
6
7/**
8 * The Minify_Excludes_State_Entry class represents a single state entry for Jetpack Boost Data Sync.
9 *
10 * This class implements the Entry_Can_Get and Entry_Can_Set interfaces, allowing it to retrieve and set
11 * the current state of the Minify Excludes option.
12 *
13 * @package Automattic\Jetpack_Boost\Data_Sync
14 */
15class Minify_Excludes_State_Entry implements Entry_Can_Get, Entry_Can_Set {
16
17    /**
18     * The option key used to store the Minify Excludes option.
19     *
20     * @var string
21     */
22    private $option_key;
23
24    /**
25     * Constructs a new instance of the Minify_Excludes_State_Entry class.
26     *
27     * @param string $option_key The option key used to store the Minify Excludes option.
28     */
29    public function __construct( $option_key ) {
30        $this->option_key = 'jetpack_boost_ds_' . $option_key;
31    }
32
33    /**
34     * Retrieves the value of the specified option.
35     *
36     * If the option does not exist, it returns the provided fallback value
37     * or null if no fallback value is provided.
38     *
39     * @param mixed $fallback_value Optional. The value to return if the option does not exist.
40     *                              Default is false.
41     * @return mixed The value of the option, or the fallback value if the option does not exist.
42     */
43    public function get( $fallback_value = false ) {
44        if ( $fallback_value !== false ) {
45            return get_option( $this->option_key, $fallback_value );
46        }
47
48        return get_option( $this->option_key );
49    }
50
51    /**
52     * Sets the value of the Minify Excludes option.
53     *
54     * @param mixed $value The new value of the Minify Excludes option.
55     */
56    public function set( $value ) {
57        $value = $this->sanitize_value( $value );
58
59        update_option( $this->option_key, $value );
60    }
61
62    /**
63     * Sanitizes the given value, ensuring that it is a comma-separated list of unique, trimmed strings.
64     *
65     * @param mixed $value The value to sanitize.
66     *
67     * @return string The sanitized value, as a comma-separated list of unique, trimmed strings.
68     */
69    private function sanitize_value( $value ) {
70        if ( is_array( $value ) ) {
71            $value = array_values( array_unique( array_filter( array_map( 'trim', $value ) ) ) );
72        } else {
73            $value = array();
74        }
75
76        return $value;
77    }
78}