Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Data_Set | |
0.00% |
0 / 46 |
|
0.00% |
0 / 8 |
650 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| build_data_set | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| is_data_point_literal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| is_data_point_option | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| is_data_point_updatable | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| is_data_point_theme | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| get_data | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| save_data | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Class Data Set. |
| 4 | * |
| 5 | * @package automattic/jetpack-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\Global_Styles; |
| 9 | |
| 10 | /** |
| 11 | * Utility to retrieve data from a description. |
| 12 | */ |
| 13 | class Data_Set { |
| 14 | |
| 15 | /** |
| 16 | * Description of the data points to work with. |
| 17 | * |
| 18 | * @var array |
| 19 | */ |
| 20 | private $data_meta = array(); |
| 21 | |
| 22 | /** |
| 23 | * Set of objects that implement the Data_Point interface. |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | private $data_set = array(); |
| 28 | |
| 29 | /** |
| 30 | * Constructor |
| 31 | * |
| 32 | * @param array $data_meta Description of the data points to work with. |
| 33 | */ |
| 34 | public function __construct( $data_meta ) { |
| 35 | $this->data_meta = $data_meta; |
| 36 | $this->data_set = $this->build_data_set( $data_meta ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Build data set from the meta data provided. |
| 41 | * |
| 42 | * @param array $data_meta Meta data description. |
| 43 | * @return array The data set structure. |
| 44 | */ |
| 45 | private function build_data_set( $data_meta ) { |
| 46 | require_once __DIR__ . '/class-data-point-literal.php'; |
| 47 | require_once __DIR__ . '/class-data-point-option.php'; |
| 48 | require_once __DIR__ . '/class-data-point-theme.php'; |
| 49 | |
| 50 | $result = array(); |
| 51 | foreach ( $data_meta as $key => $meta ) { |
| 52 | if ( $this->is_data_point_literal( $meta ) ) { |
| 53 | $result[ $key ] = new Data_Point_Literal( $meta ); |
| 54 | } elseif ( $this->is_data_point_option( $meta ) ) { |
| 55 | $result[ $key ] = new Data_Point_Option( $meta ); |
| 56 | } elseif ( $this->is_data_point_theme( $meta ) ) { |
| 57 | $result[ $key ] = new Data_Point_Theme( $meta ); |
| 58 | } |
| 59 | } |
| 60 | return $result; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Whether the description provided is a data point |
| 65 | * whose value should be taken literally. |
| 66 | * |
| 67 | * @param array $meta Data point description. |
| 68 | * @return boolean |
| 69 | */ |
| 70 | private function is_data_point_literal( $meta ) { |
| 71 | return array_key_exists( 'type', $meta ) && 'literal' === $meta['type']; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Whether the description provided is a data point |
| 76 | * whose value should be taken from an option. |
| 77 | * |
| 78 | * @param array $meta Data point description. |
| 79 | * @return boolean |
| 80 | */ |
| 81 | private function is_data_point_option( $meta ) { |
| 82 | return array_key_exists( 'type', $meta ) && |
| 83 | 'option' === $meta['type'] && |
| 84 | array_key_exists( 'name', $meta ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Whether the description provided is a data point |
| 89 | * that can be updated. |
| 90 | * |
| 91 | * @param array $meta Data point description. |
| 92 | * @return boolean |
| 93 | */ |
| 94 | private function is_data_point_updatable( $meta ) { |
| 95 | return $this->is_data_point_option( $meta ) && |
| 96 | array_key_exists( 'updatable', $meta ) && |
| 97 | $meta['updatable']; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Whether the description provided is a data point |
| 102 | * whose value should be taken from theme support. |
| 103 | * |
| 104 | * @param array $meta Data point description. |
| 105 | * @return boolean |
| 106 | */ |
| 107 | private function is_data_point_theme( $meta ) { |
| 108 | return array_key_exists( 'name', $meta ) && |
| 109 | array_key_exists( 'type', $meta ) && |
| 110 | 'theme' === $meta['type']; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Process the data description given and return the values. |
| 115 | * |
| 116 | * @return array Values. |
| 117 | */ |
| 118 | public function get_data() { |
| 119 | $result = array(); |
| 120 | foreach ( $this->data_set as $key => $data_point ) { |
| 121 | $result[ $key ] = $data_point->get_value(); |
| 122 | } |
| 123 | |
| 124 | $result = apply_filters( 'jetpack_global_styles_data_set_get_data', $result ); |
| 125 | |
| 126 | return $result; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Process incoming data. |
| 131 | * |
| 132 | * @param array $incoming_data Incoming data. |
| 133 | */ |
| 134 | public function save_data( $incoming_data ) { |
| 135 | $to_update = array(); |
| 136 | |
| 137 | $incoming_data = apply_filters( 'jetpack_global_styles_data_set_save_data', $incoming_data ); |
| 138 | |
| 139 | $options_updatable = array_filter( |
| 140 | $this->data_meta, |
| 141 | array( $this, 'is_data_point_updatable' ) |
| 142 | ); |
| 143 | foreach ( $options_updatable as $key => $meta ) { |
| 144 | $option_name = $this->data_set[ $key ]->get_option_name(); |
| 145 | |
| 146 | // Get current value, if we haven't yet. |
| 147 | if ( ! array_key_exists( $option_name, $to_update ) ) { |
| 148 | $to_update[ $option_name ] = get_option( $option_name ); |
| 149 | } |
| 150 | |
| 151 | // Override with incoming value, if appropiate. |
| 152 | // At this point it should have been validated, sanitized, etc. |
| 153 | if ( array_key_exists( $key, $incoming_data ) ) { |
| 154 | $to_update[ $option_name ] = $this->data_set[ $key ]->process_data_point( $to_update[ $option_name ], $incoming_data[ $key ] ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | $did_update = false; |
| 159 | foreach ( $to_update as $key => $value ) { |
| 160 | if ( update_option( $key, $value ) ) { |
| 161 | $did_update = true; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return $did_update; |
| 166 | } |
| 167 | } |