Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Dashboard_Section
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
6 / 6
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 derive_slug
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 is_available
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_default_layout
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 to_array
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 set_props
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2/**
3 * Dashboard Sections API: Dashboard_Section class.
4 *
5 * @package automattic/jetpack-premium-analytics
6 */
7
8namespace Automattic\Jetpack\PremiumAnalytics;
9
10/**
11 * Represents a dashboard section.
12 *
13 * A dashboard section is the server-owned model behind a top-level dashboard
14 * tab. It carries display metadata plus availability and default-layout
15 * callbacks that can be extended by consumers.
16 */
17final class Dashboard_Section {
18
19    /**
20     * Dashboard identifier.
21     *
22     * @var string
23     */
24    public $dashboard_name;
25
26    /**
27     * Section identifier.
28     *
29     * @var string
30     */
31    public $id;
32
33    /**
34     * URL-facing section slug, derived from the identifier.
35     *
36     * @var string
37     */
38    public $slug;
39
40    /**
41     * Display label.
42     *
43     * @var string
44     */
45    public $label;
46
47    /**
48     * Sort order.
49     *
50     * @var int
51     */
52    public $order = 10;
53
54    /**
55     * Availability flag or callback.
56     *
57     * @var bool|callable
58     */
59    private $is_available = true;
60
61    /**
62     * Default layout array or callback.
63     *
64     * @var array|callable
65     */
66    private $default_layout = array();
67
68    /**
69     * Constructor.
70     *
71     * @param string $dashboard_name Dashboard identifier.
72     * @param string $id             Section identifier.
73     * @param array  $args           Optional. Section arguments.
74     */
75    public function __construct( $dashboard_name, $id, $args = array() ) {
76        $this->dashboard_name = $dashboard_name;
77        $this->id             = $id;
78        $this->slug           = self::derive_slug( $id );
79        $this->label          = $id;
80
81        $this->set_props( $args );
82    }
83
84    /**
85     * Derives the URL-facing slug from a namespaced section identifier.
86     *
87     * @param string $id Section identifier, e.g. `analytics/traffic`.
88     * @return string The segment after the namespace, e.g. `traffic`.
89     */
90    private static function derive_slug( $id ) {
91        $separator = strpos( (string) $id, '/' );
92
93        return false === $separator ? (string) $id : substr( $id, $separator + 1 );
94    }
95
96    /**
97     * Returns whether this section should be exposed.
98     *
99     * @return bool
100     */
101    public function is_available() {
102        if ( is_callable( $this->is_available ) ) {
103            return (bool) call_user_func( $this->is_available, $this );
104        }
105
106        return (bool) $this->is_available;
107    }
108
109    /**
110     * Returns the section's default widget layout.
111     *
112     * @return array Array of widget instances.
113     */
114    public function get_default_layout() {
115        $layout = is_callable( $this->default_layout )
116            ? call_user_func( $this->default_layout, $this )
117            : $this->default_layout;
118
119        return is_array( $layout ) ? array_values( $layout ) : array();
120    }
121
122    /**
123     * Returns the public REST representation.
124     *
125     * @return array
126     */
127    public function to_array() {
128        return array(
129            'id'             => $this->id,
130            'slug'           => $this->slug,
131            'label'          => $this->label,
132            'order'          => (int) $this->order,
133            'default_layout' => $this->get_default_layout(),
134        );
135    }
136
137    /**
138     * Hydrates section properties from the args array.
139     *
140     * @param array $args Section arguments.
141     * @return void
142     */
143    private function set_props( $args ) {
144        if ( ! is_array( $args ) ) {
145            return;
146        }
147
148        if ( isset( $args['label'] ) ) {
149            $this->label = (string) $args['label'];
150        }
151
152        if ( isset( $args['order'] ) ) {
153            $this->order = (int) $args['order'];
154        }
155
156        if ( array_key_exists( 'is_available', $args ) ) {
157            $this->is_available = $args['is_available'];
158        }
159
160        if ( array_key_exists( 'default_layout', $args ) ) {
161            $this->default_layout = $args['default_layout'];
162        }
163    }
164}