Passed
Pull Request — master (#258)
by
unknown
06:04
created

Control::json()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
dl 0
loc 16
rs 9.7998
c 1
b 0
f 1
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Flynt\Customizer\Typography;
4
5
use Flynt\Utils\Asset;
6
7
class Control extends \WP_Customize_Control
8
{
9
    public $type = 'flynt-typography';
10
    public $unit = '';
11
12
    public function getFonts()
13
    {
14
        $gfonts = Asset::getContents('../lib/Customizer/Typography/webfonts.json');
15
        $gfonts = json_decode($gfonts, 1);
0 ignored issues
show
Bug introduced by
It seems like $gfonts can also be of type false; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

15
        $gfonts = json_decode(/** @scrutinizer ignore-type */ $gfonts, 1);
Loading history...
16
        $fonts = [];
17
18
        foreach ($gfonts['items'] as $font) {
19
            $key = sanitize_title($font['family']);
20
            $font['variants'] = array_map('self::parseVariants', $font['variants']);
21
            $font['variants'] = array_values(array_filter($font['variants'], 'self::removeItalicVariants'));
22
            $fonts[$key] = array_merge([
23
                'id' => $key,
24
                'text' => $font['family'],
25
            ], $font);
26
        }
27
28
        return $fonts;
29
    }
30
31
    public static function parseVariants($variant)
32
    {
33
        $variants = [
34
            'regular' => '400',
35
            'italic' => '400italic',
36
        ];
37
38
        return $variants[$variant] ?? $variant;
39
    }
40
41
    public static function removeItalicVariants($variant)
42
    {
43
        return strpos($variant, 'italic') === false;
44
    }
45
46
    /**
47
	 * Refresh the parameters passed to the JavaScript via JSON.
48
     *
49
	 * @return array Array of parameters passed to the JavaScript.
50
	 */
51
    public function json() {
52
        $json = parent::json();
53
        $value = $this->value();
54
        $fonts = $this->getFonts();
55
        $key = sanitize_title($value['family']);
56
        $json['id'] = $this->id;
57
        $json['link'] = $this->get_link();
58
        $json['value'] = $value;
59
        $json['font'] = sanitize_title($value['family'] ?? '');
60
        $json['default'] = $this->default ?? $this->setting->default;
0 ignored issues
show
Bug introduced by
The property default does not exist on string.
Loading history...
Bug Best Practice introduced by
The property default does not exist on Flynt\Customizer\Typography\Control. Did you maybe forget to declare it?
Loading history...
61
        $json['defaultKey'] = sanitize_title($json['default']['family']);
62
        $json['fonts'] = $fonts;
63
        $json['variants'] = array_filter($fonts[$key]['variants'], function($variant) {
64
            return strpos($variant, 'italic') === false;
65
        });
66
		return $json;
67
	}
68
69
    /**
70
     * Don't render the control content from PHP, as it's rendered via JS on load.
71
     */
72
    public function render_content() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
73
    {
74
    }
75
76
    /**
77
     * Render a JS template for control display.
78
     */
79
    public function content_template() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
80
    {
81
        ?>
82
        <# if ( data.label ) { #><label for="{{{ data.id }}}" class="customize-control-title">{{{ data.label }}}</label><# } #>
83
        <# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
84
85
        <div class="customize-control-notifications-container"></div>
86
87
        <div class="customize-control-content">
88
            <div class="flynt-typography-field">
89
                <div class="flynt-typography-option flynt-typography-option--family">
90
                    <span class="flynt-typography-option-title"><?php esc_html_e('Family', 'flynt'); ?></span>
91
                    <select class="flynt-typography-family"></select>
92
                </div>
93
                <div class="flynt-typography-option flynt-typography-option--variant">
94
                    <span class="flynt-typography-option-title"><?php esc_html_e('Weight', 'flynt'); ?></span>
95
                    <select class="flynt-typography-variant">
96
                        <# _.each(data.variants, function(variant) { #>
97
                            <option
98
                                value="{{{ variant }}}"
99
                                <# if (_.isEqual(data.value.variant, variant)) { #>selected='selected'<# } #>
100
                            >{{{ variant }}}</option>
101
                        <# }); #>
102
                    </select>
103
                </div>
104
            </div>
105
            <button type="button" class="flynt-typography-reset button button-secondary" data-key="{{{ data.defaultKey }}}"><?php esc_html_e('Default', 'flynt'); ?></button>
106
        </div>
107
108
        <?php
109
    }
110
}
111