PluginProbe
Code Snippets / 4.0.0-beta.2
Code Snippets v4.0.0-beta.2
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / Admin / Menus / Insights / Insights_Summary.php

Insights_Summary.php in Code Snippets 4.0.0-beta.2, at php/Admin/Menus/Insights/Insights_Summary.php

218 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\Admin\Menus\Insights;
4
5 use Code_Snippets\Model\Snippet;
6 use function Code_Snippets\get_snippets;
7
8 /**
9 * Builds aggregate data for the Insights screen.
10 */
11 final class Insights_Summary {
12
13 /**
14 * Display order for snippet types.
15 *
16 * @var string[]
17 */
18 private const TYPE_ORDER = [ 'php', 'html', 'css', 'js', 'cond' ];
19
20 /**
21 * Recognised executable locations, in display order. Labels are resolved on
22 * the front-end, which already maintains these strings for the editor.
23 *
24 * @var string[]
25 */
26 private const LOCATION_ORDER = [
27 'global',
28 'admin',
29 'front-end',
30 'single-use',
31 'content',
32 'head-content',
33 'body-content',
34 'footer-content',
35 'admin-css',
36 'site-css',
37 'site-head-js',
38 'site-footer-js',
39 ];
40
41 /**
42 * Build an aggregate of saved snippets for the Insights screen.
43 *
44 * @return array{
45 * active: int,
46 * inactive: int,
47 * typeCounts: array<string, array{label: string, count: int}>,
48 * conditionCounts: array<string, array{label: string, count: int}>,
49 * locationCounts: array<string, int>,
50 * tagCounts: array<string, array{label: string, count: int}>
51 * }
52 */
53 public function get(): array {
54 return $this->create_summary( get_snippets() );
55 }
56
57 /**
58 * Create summary data from snippet models.
59 *
60 * @param Snippet[] $snippets Snippet models.
61 *
62 * @return array{
63 * active: int,
64 * inactive: int,
65 * typeCounts: array<string, array{label: string, count: int}>,
66 * conditionCounts: array<string, array{label: string, count: int}>,
67 * locationCounts: array<string, int>,
68 * tagCounts: array<string, array{label: string, count: int}>
69 * }
70 */
71 private function create_summary( array $snippets ): array {
72 $snippets = array_values(
73 array_filter(
74 $snippets,
75 static function ( Snippet $snippet ): bool {
76 return ! $snippet->trashed;
77 }
78 )
79 );
80 $active_condition_ids = [];
81
82 foreach ( $snippets as $snippet ) {
83 if ( 'condition' !== $snippet->scope && $snippet->active && $snippet->condition_id ) {
84 $active_condition_ids[ $snippet->condition_id ] = true;
85 }
86 }
87
88 $type_counts = array_fill_keys( self::TYPE_ORDER, 0 );
89 $condition_counts = array_fill_keys( [ 'with', 'without' ], 0 );
90 $location_counts = array_fill_keys( self::LOCATION_ORDER, 0 );
91 $tag_counts = [];
92 $active = 0;
93
94 foreach ( $snippets as $snippet ) {
95 $type = Snippet::get_type_from_scope( $snippet->scope );
96
97 if ( isset( $type_counts[ $type ] ) ) {
98 ++$type_counts[ $type ];
99 }
100
101 ++$condition_counts[ $snippet->condition_id ? 'with' : 'without' ];
102
103 if ( 'condition' === $snippet->scope ) {
104 $is_active = isset( $active_condition_ids[ $snippet->id ] );
105 } else {
106 $is_active = $snippet->active;
107
108 if ( isset( $location_counts[ $snippet->scope ] ) ) {
109 ++$location_counts[ $snippet->scope ];
110 }
111 }
112
113 if ( $is_active ) {
114 ++$active;
115 }
116
117 foreach ( array_unique( $snippet->tags ) as $tag ) {
118 if ( '' !== $tag ) {
119 $tag_counts[ $tag ] = ( $tag_counts[ $tag ] ?? 0 ) + 1;
120 }
121 }
122 }
123
124 return [
125 'active' => $active,
126 'inactive' => count( $snippets ) - $active,
127 'typeCounts' => $this->create_chart_entries( $type_counts, $this->get_type_labels() ),
128 'conditionCounts' => $this->create_chart_entries( $condition_counts, $this->get_condition_usage_labels() ),
129 'locationCounts' => array_filter( $location_counts ),
130 'tagCounts' => $this->create_tag_chart_entries( $tag_counts ),
131 ];
132 }
133
134 /**
135 * Convert tag counts into usage-ordered chart entries.
136 *
137 * @param array<string, int> $counts Count for each tag.
138 *
139 * @return array<string, array{label: string, count: int}>
140 */
141 private function create_tag_chart_entries( array $counts ): array {
142 uksort(
143 $counts,
144 static function ( string $first_tag, string $second_tag ) use ( $counts ): int {
145 $count_comparison = $counts[ $second_tag ] <=> $counts[ $first_tag ];
146
147 if ( 0 !== $count_comparison ) {
148 return $count_comparison;
149 }
150
151 $label_comparison = strnatcasecmp( $first_tag, $second_tag );
152 return 0 !== $label_comparison ? $label_comparison : strcmp( $first_tag, $second_tag );
153 }
154 );
155
156 $entries = [];
157
158 foreach ( $counts as $tag => $count ) {
159 $entries[ $tag ] = [
160 'label' => $tag,
161 'count' => $count,
162 ];
163 }
164
165 return $entries;
166 }
167
168 /**
169 * Convert aggregate counts into labeled chart entries.
170 *
171 * @param array<string, int> $counts Count for each type.
172 * @param array<string, string> $labels Display label for each type.
173 *
174 * @return array<string, array{label: string, count: int}>
175 */
176 private function create_chart_entries( array $counts, array $labels ): array {
177 $entries = [];
178
179 foreach ( $counts as $key => $count ) {
180 if ( isset( $labels[ $key ] ) ) {
181 $entries[ $key ] = [
182 'label' => $labels[ $key ],
183 'count' => $count,
184 ];
185 }
186 }
187
188 return $entries;
189 }
190
191 /**
192 * Retrieve display labels for snippet types.
193 *
194 * @return array<string, string>
195 */
196 private function get_type_labels(): array {
197 return [
198 'php' => __( 'PHP', 'code-snippets' ),
199 'html' => __( 'HTML', 'code-snippets' ),
200 'css' => __( 'CSS', 'code-snippets' ),
201 'js' => __( 'JS', 'code-snippets' ),
202 'cond' => __( 'Conditions', 'code-snippets' ),
203 ];
204 }
205
206 /**
207 * Retrieve display labels for condition usage.
208 *
209 * @return array<string, string>
210 */
211 private function get_condition_usage_labels(): array {
212 return [
213 'with' => __( 'Uses conditions', 'code-snippets' ),
214 'without' => __( 'Does not use conditions', 'code-snippets' ),
215 ];
216 }
217 }
218