PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.0
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 3.0.1 All 64 releases
code-snippets / php / Client / Welcome_Client.php

Welcome_Client.php in Code Snippets 3.10.0, at php/Client/Welcome_Client.php

381 lines 10.1 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\Client;
4
5 use DateTimeImmutable;
6 use Exception;
7 use WP_Filesystem_Direct;
8 use const Code_Snippets\PLUGIN_FILE;
9
10 /**
11 * Class for loading data from the Code Snippets website.
12 *
13 * @package Code_Snippets
14 */
15 class Welcome_Client {
16
17 /**
18 * URL for the welcome page data.
19 *
20 * @var string
21 */
22 protected const WELCOME_JSON_URL = 'https://codesnippets.pro/wp-content/uploads/cs_welcome/cs_welcome.json';
23
24 /**
25 * Limit of number of items to display when loading lists of items.
26 *
27 * @var int
28 */
29 protected const ITEM_LIMIT = 4;
30
31 /**
32 * Limit of number of items of historic versions to display in the changelog.
33 *
34 * @var int
35 */
36 protected const MAX_CHANGELOG_ENTRIES = 4;
37
38 /**
39 * Key used for caching welcome page data.
40 *
41 * @var string
42 */
43 protected const CACHE_KEY = 'code_snippets_welcome_data';
44
45 /**
46 * Data fetched from the remote API.
47 *
48 * @var array{
49 * banner: ?array,
50 * hero-item: ?array,
51 * features: ?array,
52 * partners: ?array,
53 * changelog: ?array
54 * }
55 */
56 private array $welcome_data;
57
58 /**
59 * Populate the $welcome_data variable when the class is loaded.
60 *
61 * @return void
62 */
63 public function __construct() {
64 $stored_data = get_transient( self::CACHE_KEY );
65
66 if ( is_array( $stored_data ) ) {
67 $this->welcome_data = $stored_data;
68 } else {
69 $this->welcome_data = [];
70 $this->fetch_remote_welcome_data();
71 $this->build_changelog_data();
72 set_transient( self::CACHE_KEY, $this->welcome_data, DAY_IN_SECONDS * 2 );
73 }
74 }
75
76 /**
77 * Purge the welcome data cache.
78 *
79 * @return void
80 */
81 public static function clear_cache() {
82 delete_transient( self::CACHE_KEY );
83 }
84
85 /**
86 * Safely retrieve an array from an object, ensuring it exists and is valid, returning a default value if not.
87 *
88 * @param array<string, mixed> $items Associative array containing array to extract.
89 * @param string $key Array key.
90 *
91 * @return array Extracted array, or empty array if array is missing.
92 */
93 private static function safe_get_array( array $items, string $key ): array {
94 return isset( $items[ $key ] ) && is_array( $items[ $key ] ) ? $items[ $key ] : [];
95 }
96
97 /**
98 * Parse DateTime value from a string without triggering an error.
99 *
100 * @param string $datetime String representation of DateTime value.
101 *
102 * @return DateTimeImmutable|null
103 */
104 private static function safe_parse_datetime( string $datetime ): ?DateTimeImmutable {
105 try {
106 return new DateTimeImmutable( $datetime );
107 } catch ( Exception $e ) {
108 return null;
109 }
110 }
111
112 /**
113 * Parse remote hero item data.
114 *
115 * @param array $remote Remote hero item data.
116 *
117 * @return array
118 */
119 private function parse_hero_item( array $remote ): array {
120 return [
121 'name' => $remote[0]['name'] ?? '',
122 'follow_url' => $remote[0]['follow_url'] ?? '',
123 'image_url' => $remote[0]['image_url'] ?? '',
124 ];
125 }
126
127 /**
128 * Parse remote banner data.
129 *
130 * @param array $remote Remote hero item data.
131 *
132 * @return array
133 */
134 private function parse_banner( array $remote ): array {
135 return [
136 'key' => sanitize_key( $remote['key'] ) ?? '',
137 'start_datetime' => self::safe_parse_datetime( $remote['start_datetime'] ),
138 'end_datetime' => self::safe_parse_datetime( $remote['end_datetime'] ),
139 'text_free' => $remote['text_free'] ?? '',
140 'action_url_free' => $remote['action_url_free'] ?? '',
141 'action_label_free' => $remote['action_label_free'] ?? '',
142 'text_pro' => $remote['text_pro'] ?? '',
143 'action_url_pro' => $remote['action_url_pro'] ?? '',
144 'action_label_pro' => $remote['action_label_pro'] ?? '',
145 ];
146 }
147
148 /**
149 * Parse a list of features from a remote dataset.
150 *
151 * @param array $remote Remote data.
152 *
153 * @return array[] Parsed feature data.
154 */
155 private function parse_features( array $remote ): array {
156 $limit = max( self::ITEM_LIMIT, count( $remote ) );
157 $features = [];
158
159 for ( $i = 0; $i < $limit; $i++ ) {
160 $feature = $remote[ $i ];
161
162 $features[] = [
163 'title' => $feature['title'] ?? '',
164 'follow_url' => $feature['follow_url'] ?? '',
165 'image_url' => $feature['image_url'] ?? '',
166 'category' => $feature['category'] ?? '',
167 'description' => $feature['description'] ?? '',
168 ];
169 }
170
171 return $features;
172 }
173
174 /**
175 * Parse a list of partners from a remote dataset.
176 *
177 * @param array $remote Remote data.
178 *
179 * @return array[] Parsed partner data.
180 */
181 private function parse_partners( array $remote ): array {
182 $limit = max( self::ITEM_LIMIT, count( $remote ) );
183 $partners = [];
184
185 for ( $i = 0; $i < $limit; $i++ ) {
186 $partner = $remote[ $i ];
187
188 $partners[] = [
189 'title' => $partner['title'] ?? '',
190 'follow_url' => $partner['follow_url'] ?? '',
191 'image_url' => $partner['image_url'] ?? '',
192 ];
193 }
194
195 return $partners;
196 }
197
198 /**
199 * Fetch remote welcome data from the remote server and add it to the stored data.
200 *
201 * @return void
202 */
203 protected function fetch_remote_welcome_data() {
204 $remote_welcome_data = wp_remote_get( self::WELCOME_JSON_URL );
205
206 if ( is_wp_error( $remote_welcome_data ) ) {
207 return;
208 }
209
210 $remote_welcome_data = json_decode( wp_remote_retrieve_body( $remote_welcome_data ), true );
211
212 if ( ! is_array( $remote_welcome_data ) ) {
213 return;
214 }
215
216 $this->welcome_data['banner'] = $this->parse_banner( self::safe_get_array( $remote_welcome_data, 'banner' ) );
217 $this->welcome_data['hero-item'] = $this->parse_hero_item( self::safe_get_array( $remote_welcome_data, 'hero-item' ) );
218 $this->welcome_data['features'] = $this->parse_features( self::safe_get_array( $remote_welcome_data, 'features' ) );
219 $this->welcome_data['partners'] = $this->parse_partners( self::safe_get_array( $remote_welcome_data, 'partners' ) );
220 }
221
222 /**
223 * Build the full list of latest changes for caching.
224 *
225 * @return void
226 */
227 protected function build_changelog_data() {
228 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
229 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
230 $filesystem = new WP_Filesystem_Direct( null );
231
232 $changelog_filename = 'CHANGELOG.md';
233 $changelog = [];
234
235 $section_titles = [ 'Added', 'Changed', 'Deprecated', 'Removed', 'Fixed', 'Security', 'Other' ];
236 $changelog_dir = plugin_dir_path( PLUGIN_FILE );
237
238 while ( plugin_dir_path( $changelog_dir ) !== $changelog_dir && ! $filesystem->exists( $changelog_dir . $changelog_filename ) ) {
239 $changelog_dir = plugin_dir_path( $changelog_dir );
240 }
241
242 if ( ! $filesystem->exists( $changelog_dir . $changelog_filename ) ) {
243 return;
244 }
245
246 $changelog_contents = $filesystem->get_contents( $changelog_dir . $changelog_filename );
247 $changelog_releases = explode( "\n## ", $changelog_contents );
248
249 foreach ( array_slice( $changelog_releases, 1, self::MAX_CHANGELOG_ENTRIES ) as $changelog_release ) {
250 $sections = explode( "\n### ", $changelog_release );
251
252 if ( count( $sections ) < 2 ) {
253 continue;
254 }
255
256 $entries = array_fill_keys( $section_titles, [] );
257
258 foreach ( array_slice( $sections, 1 ) as $section_contents ) {
259 $lines = array_filter( array_map( 'trim', explode( "\n", $section_contents ) ) );
260 $section_type = $lines[0];
261
262 if ( ! isset( $entries[ $section_type ] ) ) {
263 $section_type = 'Other';
264 }
265
266 foreach ( array_slice( $lines, 1 ) as $line ) {
267 $entry = trim( str_replace( '(PRO)', '', str_replace( '*', '', $line ) ) );
268 $core_or_pro = false === strpos( $line, '(PRO)' ) ? 'core' : 'pro';
269
270 $entry = str_replace( '`', '', $entry );
271 $entry = preg_replace( '/\[(.+?)]\(.+?\)/', '$1', $entry );
272
273 if ( ! isset( $entries[ $section_type ][ $core_or_pro ] ) ) {
274 $entries[ $section_type ][ $core_or_pro ] = [ $entry ];
275 } else {
276 $entries[ $section_type ][ $core_or_pro ][] = $entry;
277 }
278 }
279 }
280
281 $header_parts = explode( '(', $sections[0], 2 );
282 $version = trim( trim( $header_parts[0] ), '[]' );
283 $date = trim( trim( $header_parts[1] ?? '' ), '()' );
284
285 try {
286 $datetime = new DateTimeImmutable( $date );
287 $parsed_date = $datetime->format( get_option( 'date_format' ) );
288 } catch ( Exception $e ) {
289 $parsed_date = $date;
290 }
291
292 $changelog[] = [
293 'version' => $version,
294 'date' => $parsed_date,
295 'entries' => array_filter(
296 $entries,
297 function ( $section ) {
298 return ! empty( $section );
299 }
300 ),
301 ];
302 }
303
304 $this->welcome_data['changelog'] = $changelog;
305 }
306
307 /**
308 * Retrieve banner information.
309 *
310 * @return array{
311 * key: string,
312 * start_datetime: ?DateTimeImmutable,
313 * end_datetime: ?DateTimeImmutable,
314 * text_free: string,
315 * action_url_free: string,
316 * action_label_free: string,
317 * text_pro: string,
318 * action_url_pro: string,
319 * action_label_pro: string tet
320 * }
321 */
322 public function get_banner(): array {
323 return $this->welcome_data['banner'] ?? [];
324 }
325
326 /**
327 * Retrieve hero information.
328 *
329 * @return array{
330 * name: string,
331 * follow_url: string,
332 * image_url: string
333 * }
334 */
335 public function get_hero_item(): array {
336 return $this->welcome_data['hero-item'] ?? [];
337 }
338
339 /**
340 * Retrieve the list of features retrieved from the remote API.
341 *
342 * @return array{
343 * title: string,
344 * follow_url: string,
345 * image_url: string,
346 * category: string,
347 * description: string
348 * }[] Feature details.
349 */
350 public function get_features(): array {
351 return $this->welcome_data['features'] ?? [];
352 }
353
354 /**
355 * Retrieve the list of partners retrieved from the remote API.
356 *
357 * @return array{
358 * title: string,
359 * follow_url: string,
360 * image_url: string
361 * }[] Partner details.
362 */
363 public function get_partners(): array {
364 return $this->welcome_data['partners'] ?? [];
365 }
366
367 /**
368 * Retrieve a list of latest changes for display.
369 *
370 * @return array<string, array{
371 * 'Added': ?array<'core' | 'pro', string>,
372 * 'Fixed': ?array<'core' | 'pro', string>,
373 * 'Improved': ?array<'core' | 'pro', string>,
374 * 'Other': ?array<'core' | 'pro', string>
375 * }>
376 */
377 public function get_changelog(): array {
378 return $this->welcome_data['changelog'] ?? [];
379 }
380 }
381