Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Data_Point_Option
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 get_value
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 get_option_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 process_data_point
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Class Data Point Option.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Global_Styles;
9
10require_once __DIR__ . '/interface-data-point.php';
11
12/**
13 * Option Data Point.
14 */
15class Data_Point_Option implements Data_Point {
16
17    /**
18     * Option name.
19     *
20     * @var string
21     */
22    private $option_name;
23
24    /**
25     * Option property to access, if exist.
26     *
27     * @var string
28     */
29    private $option_property;
30
31    /**
32     * Default option value.
33     *
34     * @var mixed
35     */
36    private $default_value;
37
38    /**
39     * Constructor.
40     *
41     * @param array $meta Data point description.
42     */
43    public function __construct( $meta ) {
44        if ( is_array( $meta['name'] ) ) {
45            $this->option_name     = $meta['name'][0];
46            $this->option_property = $meta['name'][1];
47        } else {
48            $this->option_name = $meta['name'];
49        }
50
51        if ( array_key_exists( 'default', $meta ) ) {
52            $this->default_value = $meta['default'];
53        } else {
54            $this->default_value = false;
55        }
56    }
57
58    /**
59     * Implements \Automattic\Jetpack\Jetpack_Mu_Wpcom\Global_Styles\Data_Point interface.
60     *
61     * @return mixed The value to return.
62     */
63    public function get_value() {
64        if ( ! isset( $this->option_property ) ) {
65            return get_option( $this->option_name, $this->default_value );
66        }
67
68        $value = get_option( $this->option_name, array() );
69        if ( is_array( $value ) && array_key_exists( $this->option_property, $value ) ) {
70            return $value[ $this->option_property ];
71        }
72
73        return $this->default_value;
74    }
75
76    /**
77     * Return the option name this data point belongs to.
78     *
79     * @return string Option name
80     */
81    public function get_option_name() {
82        return $this->option_name;
83    }
84
85    /**
86     * Process new data.
87     *
88     * @param mixed $current_option_value Current option value.
89     * @param mixed $new_value Value to update.
90     * @return mixed The modified option value.
91     */
92    public function process_data_point( $current_option_value, $new_value ) {
93        $result = $current_option_value;
94
95        if ( isset( $this->option_property ) ) {
96            if ( is_array( $result ) ) {
97                $result[ $this->option_property ] = $new_value;
98            } else {
99                // Unexpected current option value. We were expecting an array,
100                // but it's something like a string or boolean and we can't
101                // update the single data point.  Throw away the current option
102                // value and make a new array.
103                $result = array( $this->option_property => $new_value );
104            }
105        } else {
106            $result = $new_value;
107        }
108
109        return $result;
110    }
111}