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