Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Dashboard_Section | |
100.00% |
26 / 26 |
|
100.00% |
5 / 5 |
13 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| is_available | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| get_default_layout | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| to_array | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| set_props | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Dashboard Sections API: Dashboard_Section class. |
| 4 | * |
| 5 | * @package automattic/jetpack-premium-analytics |
| 6 | */ |
| 7 | |
| 8 | namespace 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 | */ |
| 17 | final 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 | * Display label. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $label; |
| 39 | |
| 40 | /** |
| 41 | * Sort order. |
| 42 | * |
| 43 | * @var int |
| 44 | */ |
| 45 | public $order = 10; |
| 46 | |
| 47 | /** |
| 48 | * Availability flag or callback. |
| 49 | * |
| 50 | * @var bool|callable |
| 51 | */ |
| 52 | private $is_available = true; |
| 53 | |
| 54 | /** |
| 55 | * Default layout array or callback. |
| 56 | * |
| 57 | * @var array|callable |
| 58 | */ |
| 59 | private $default_layout = array(); |
| 60 | |
| 61 | /** |
| 62 | * Constructor. |
| 63 | * |
| 64 | * @param string $dashboard_name Dashboard identifier. |
| 65 | * @param string $id Section identifier. |
| 66 | * @param array $args Optional. Section arguments. |
| 67 | */ |
| 68 | public function __construct( $dashboard_name, $id, $args = array() ) { |
| 69 | $this->dashboard_name = $dashboard_name; |
| 70 | $this->id = $id; |
| 71 | $this->label = $id; |
| 72 | |
| 73 | $this->set_props( $args ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns whether this section should be exposed. |
| 78 | * |
| 79 | * @return bool |
| 80 | */ |
| 81 | public function is_available() { |
| 82 | if ( is_callable( $this->is_available ) ) { |
| 83 | return (bool) call_user_func( $this->is_available, $this ); |
| 84 | } |
| 85 | |
| 86 | return (bool) $this->is_available; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the section's default widget layout. |
| 91 | * |
| 92 | * @return array Array of widget instances. |
| 93 | */ |
| 94 | public function get_default_layout() { |
| 95 | $layout = is_callable( $this->default_layout ) |
| 96 | ? call_user_func( $this->default_layout, $this ) |
| 97 | : $this->default_layout; |
| 98 | |
| 99 | return is_array( $layout ) ? array_values( $layout ) : array(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the public REST representation. |
| 104 | * |
| 105 | * @return array |
| 106 | */ |
| 107 | public function to_array() { |
| 108 | return array( |
| 109 | 'id' => $this->id, |
| 110 | 'label' => $this->label, |
| 111 | 'order' => (int) $this->order, |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Hydrates section properties from the args array. |
| 117 | * |
| 118 | * @param array $args Section arguments. |
| 119 | * @return void |
| 120 | */ |
| 121 | private function set_props( $args ) { |
| 122 | if ( ! is_array( $args ) ) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if ( isset( $args['label'] ) ) { |
| 127 | $this->label = (string) $args['label']; |
| 128 | } |
| 129 | |
| 130 | if ( isset( $args['order'] ) ) { |
| 131 | $this->order = (int) $args['order']; |
| 132 | } |
| 133 | |
| 134 | if ( array_key_exists( 'is_available', $args ) ) { |
| 135 | $this->is_available = $args['is_available']; |
| 136 | } |
| 137 | |
| 138 | if ( array_key_exists( 'default_layout', $args ) ) { |
| 139 | $this->default_layout = $args['default_layout']; |
| 140 | } |
| 141 | } |
| 142 | } |