Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Palette
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 5
420
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 __set
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 __get
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
72
 format_colors
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3/**
4 * Custom pallette control.
5 */
6class Palette {
7
8    /**
9     * Pallete data storage.
10     *
11     * @var array
12     */
13    protected $data;
14
15    /**
16     * Constructor
17     *
18     * @param ?array $initial_data initial pallette data.
19     */
20    public function __construct( $initial_data = null ) {
21        if ( $initial_data ) {
22            foreach ( $initial_data as $key => $value ) {
23                $this->{$key} = $value;
24            }
25        }
26    }
27
28    /**
29     * Data setter method.
30     *
31     * @param string $member the data key.
32     * @param mixed  $value the data value.
33     */
34    public function __set( $member, $value ) {
35        $this->data[ $member ] = $value;
36
37        if ( 'colors' === $member ) {
38            $this->format_colors();
39        }
40    }
41
42    /**
43     * Data getter method.
44     *
45     * @param string $member the data key.
46     * @return mixed $value
47     */
48    public function __get( $member ) {
49        return $this->data[ $member ];
50    }
51
52    /**
53     * A search method for palettes. Specify an ID or a set of colors to find the matching palette.
54     *
55     * @param array{id?:int,colors?:array} $args initial arguments.
56     * @return Palette|false
57     */
58    public static function get( $args = array() ) {
59        $defaults = array(
60            'id'     => 0,
61            'colors' => array(),
62        );
63
64        $args           = wp_parse_args( $args, $defaults );
65        $args['id']     = intval( $args['id'] );
66        $args['colors'] = (array) $args['colors'];
67
68        if ( ! $args['id'] && ! empty( $args['colors'] ) ) {
69            foreach ( $args['colors'] as $color_index => $color_code ) {
70                $args['colors'][ $color_index ] = Colors_Manager::normalize_color( $color_code );
71            }
72
73            $args['colors'] = implode( ',', array_unique( $args['colors'] ) );
74
75            $palette_by_colors = Colors_API::call( 'palettes', array( 'colors' => $args['colors'] ) );
76            if ( $palette_by_colors ) {
77                $args['id'] = $palette_by_colors['id'];
78            }
79        }
80
81        if ( $args['id'] ) {
82            $palette_data = wp_cache_get( 'palette:' . $args['id'], 'colors' );
83
84            if ( false === $palette_data ) {
85                $palette_data = Colors_API::call( 'palettes', array(), $args['id'] );
86                wp_cache_set( 'palette:' . $args['id'], $palette_data, 'colors' );
87            }
88
89            if ( $palette_data ) {
90                return new Palette( $palette_data );
91            }
92        }
93
94        return false;
95    }
96
97    /**
98     * COLOURLovers formats color sets as arrays of [hex=>, width=>] pairs, but we only care about the hex.
99     * This function assigns the colors in the array to the five color roles.
100     */
101    public function format_colors() {
102        if ( is_string( $this->data['colors'] ) ) {
103            $this->data['colors'] = json_decode( $this->data['colors'], true );
104        } elseif ( is_array( $this->data['colors'] ) && isset( $this->data['colors']['bg'] ) ) {
105            return;
106        }
107
108        $colors = array();
109
110        foreach ( array( 'bg', 'txt', 'link', 'fg1', 'fg2' ) as $color_index => $color_key ) {
111            if ( count( $this->data['colors'] ) === $color_index ) {
112                break;
113            }
114
115            $colors[ $color_key ] = $this->data['colors'][ $color_index ]['hex'];
116        }
117
118        $this->data['colors'] = $colors;
119    }
120}