Skip to content

Commit d6f6887

Browse files
committed
Allow admin banners to be provided by the welcome API.
1 parent 06c21d0 commit d6f6887

File tree

6 files changed

+372
-204
lines changed

6 files changed

+372
-204
lines changed

src/php/admin-menus/class-welcome-menu.php

Lines changed: 8 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,25 @@
1414
class Welcome_Menu extends Admin_Menu {
1515

1616
/**
17-
* URL for the welcome page data.
17+
* Instance of Welcome_API class.
1818
*
19-
* @var string
19+
* @var Welcome_API
2020
*/
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-
*/
49-
private ?array $welcome_data = null;
21+
protected Welcome_API $api;
5022

5123
/**
5224
* Class constructor
25+
*
26+
* @param Welcome_API $api Instance of API class.
5327
*/
54-
public function __construct() {
28+
public function __construct( $api ) {
5529
parent::__construct(
5630
'welcome',
5731
_x( "What's New", 'menu label', 'code-snippets' ),
5832
__( 'Welcome to Code Snippets', 'code-snippets' )
5933
);
34+
35+
$this->api = $api;
6036
}
6137

6238
/**
@@ -73,129 +49,6 @@ public function enqueue_assets() {
7349
);
7450
}
7551

76-
/**
77-
* Load remote welcome data when the page is loaded.
78-
*
79-
* @return void
80-
*/
81-
public function load() {
82-
parent::load();
83-
84-
if ( ! is_array( $this->welcome_data ) ) {
85-
$this->welcome_data = get_transient( self::CACHE_KEY );
86-
}
87-
88-
if ( ! is_array( $this->welcome_data ) ) {
89-
$this->welcome_data = [];
90-
$this->fetch_remote_welcome_data();
91-
$this->build_changelog_data();
92-
set_transient( self::CACHE_KEY, $this->welcome_data, DAY_IN_SECONDS * 2 );
93-
}
94-
}
95-
96-
/**
97-
* Purge the welcome data cache.
98-
*
99-
* @return void
100-
*/
101-
public static function clear_cache() {
102-
delete_transient( self::CACHE_KEY );
103-
}
104-
105-
/**
106-
* Fetch remote welcome data from the remote server and add it to the stored data.
107-
*
108-
* @return void
109-
*/
110-
protected function fetch_remote_welcome_data() {
111-
$remote_welcome_data = wp_remote_get( self::WELCOME_JSON_URL );
112-
if ( is_wp_error( $remote_welcome_data ) ) {
113-
return;
114-
}
115-
116-
$remote_welcome_data = json_decode( wp_remote_retrieve_body( $remote_welcome_data ), true );
117-
if ( ! is_array( $remote_welcome_data ) ) {
118-
return;
119-
}
120-
121-
$this->welcome_data['hero-item'] = array_merge(
122-
[
123-
'follow_url' => '',
124-
'name' => '',
125-
'image_url' => '',
126-
],
127-
isset( $remote_welcome_data['hero-item'][0] ) && is_array( $remote_welcome_data['hero-item'][0] ) ?
128-
$remote_welcome_data['hero-item'][0] : []
129-
);
130-
131-
$default_item = [
132-
'follow_url' => '',
133-
'image_url' => '',
134-
'title' => '',
135-
];
136-
137-
foreach ( [ 'features', 'partners' ] as $items_key ) {
138-
$this->welcome_data[ $items_key ] = [];
139-
140-
if ( ! isset( $this->welcome_data[ $items_key ] ) || ! is_array( $this->welcome_data[ $items_key ] ) ) {
141-
continue;
142-
}
143-
144-
$limit = max( self::ITEM_LIMIT, count( $this->welcome_data[ $items_key ] ) );
145-
146-
for ( $i = 0; $i < $limit; $i++ ) {
147-
$this->welcome_data[ $items_key ][] = array_merge(
148-
$default_item,
149-
$remote_welcome_data[ $items_key ][ $i ]
150-
);
151-
}
152-
}
153-
}
154-
155-
/**
156-
* Build the full list of latest changes for caching.
157-
*
158-
* @return void
159-
*/
160-
protected function build_changelog_data() {
161-
$valid_sections = [ 'Added', 'Improved', 'Fixed' ];
162-
$changelog = [];
163-
164-
$changelog_file = file_get_contents( plugin_dir_path( PLUGIN_FILE ) . 'CHANGELOG.md' );
165-
$changelog_entries = explode( "\n## ", $changelog_file );
166-
167-
foreach ( array_slice( $changelog_entries, 1, self::MAX_CHANGELOG_ENTRIES ) as $lines ) {
168-
$lines = explode( "\n", $lines );
169-
$version = explode( '(', $lines[0], 2 );
170-
$version = $version[0];
171-
172-
$changelog[ $version ] = [];
173-
174-
foreach ( array_slice( $lines, 1 ) as $raw_line ) {
175-
$entry = trim( str_replace( '(PRO)', '', str_replace( '*', '', $raw_line ) ) );
176-
$parts = explode( ': ', $entry, 2 );
177-
$entry = end( $parts );
178-
179-
if ( $entry ) {
180-
$section = in_array( $parts[0], $valid_sections, true ) ? $parts[0] : 'Other';
181-
$subsection = str_contains( $raw_line, '(PRO)' ) ? 'pro' : 'core';
182-
183-
if ( ! isset( $changelog[ $version ][ $section ] ) ) {
184-
$changelog[ $version ][ $section ] = [
185-
$subsection => [ $entry ],
186-
];
187-
} elseif ( ! isset( $changelog[ $version ][ $section ][ $subsection ] ) ) {
188-
$changelog[ $version ][ $section ][ $subsection ] = [ $entry ];
189-
} else {
190-
$changelog[ $version ][ $section ][ $subsection ][] = $entry;
191-
}
192-
}
193-
}
194-
}
195-
196-
$this->welcome_data['changelog'] = $changelog;
197-
}
198-
19952
/**
20053
* Retrieve a list of links to display in the page header.
20154
*
@@ -230,38 +83,4 @@ protected function get_header_links(): array {
23083
],
23184
];
23285
}
233-
234-
/**
235-
* Retrieve remote data for the hero item.
236-
*
237-
* @return array{follow_url: string, name: string, image_url: string}
238-
*/
239-
protected function get_hero_item(): array {
240-
return $this->welcome_data['hero-item'] ?? [];
241-
}
242-
243-
/**
244-
* Parse a list of remote items, ensuring there are no missing keys and a limit number is enforced.
245-
*
246-
* @param 'features'|'partners' $remote_key Key from remote data to parse.
247-
*
248-
* @return array<array{follow_url: string, image_url: string, title: string}>
249-
*/
250-
protected function get_remote_items( string $remote_key ): array {
251-
return $this->welcome_data[ $remote_key ] ?? [];
252-
}
253-
254-
/**
255-
* Retrieve a list of latest changes for display.
256-
*
257-
* @return array<string, array{
258-
* 'Added': ?array<'core' | 'pro', string>,
259-
* 'Fixed': ?array<'core' | 'pro', string>,
260-
* 'Improved': ?array<'core' | 'pro', string>,
261-
* 'Other': ?array<'core' | 'pro', string>
262-
* }>
263-
*/
264-
protected function get_changelog(): array {
265-
return $this->welcome_data['changelog'] ?? [];
266-
}
26786
}

src/php/class-admin.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Code_Snippets;
44

5-
use DateTime;
5+
use DateTimeImmutable;
66

77
/**
88
* Functions specific to the administration interface
@@ -18,11 +18,19 @@ class Admin {
1818
*/
1919
public array $menus = array();
2020

21+
/**
22+
* Welcome_API class instance.
23+
*
24+
* @var Welcome_API
25+
*/
26+
public Welcome_API $welcome_api;
27+
2128
/**
2229
* Class constructor
2330
*/
2431
public function __construct() {
2532
if ( is_admin() ) {
33+
$this->welcome_api = new Welcome_API();
2634
$this->run();
2735
}
2836
}
@@ -39,7 +47,7 @@ public function load_classes() {
3947
$this->menus['settings'] = new Settings_Menu();
4048
}
4149

42-
$this->menus['welcome'] = new Welcome_Menu();
50+
$this->menus['welcome'] = new Welcome_Menu( $this->welcome_api );
4351

4452
foreach ( $this->menus as $menu ) {
4553
$menu->run();
@@ -231,15 +239,27 @@ public function debug_information( array $info ): array {
231239
public function print_notices() {
232240
global $current_user;
233241

234-
$key = 'ignore_code_snippets_survey_message';
235-
$dismissed = get_user_meta( $current_user->ID, $key );
242+
$meta_key = 'ignore_code_snippets_survey_message';
243+
$dismissed = get_user_meta( $current_user->ID, $meta_key );
236244

237-
if ( isset( $_GET[ $key ], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $key ) ) {
238-
add_user_meta( $current_user->ID, $key, sanitize_key( wp_unslash( $_GET[ $key ] ) ) );
245+
if ( isset( $_GET[ $meta_key ], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $meta_key ) ) {
246+
add_user_meta( $current_user->ID, $meta_key, sanitize_key( wp_unslash( $_GET[ $meta_key ] ) ) );
239247
return;
240248
}
241249

242-
if ( ! in_array( 'pro', $dismissed, true ) ) {
250+
$welcome = $this->welcome_api->get_banner();
251+
$now = new DateTimeImmutable( 'now' );
252+
253+
if ( isset( $welcome['key'] ) && ! in_array( $welcome['key'], $dismissed, true ) &&
254+
( empty( $welcome['start_datetime'] ) || $now > $welcome['start_datetime'] ) &&
255+
( empty( $welcome['end_datetime'] ) || $now < $welcome['end_datetime'] ) ) {
256+
$notice = $welcome['key'];
257+
258+
$text = $welcome['text_free'];
259+
$action_url = $welcome['action_url_free'];
260+
$action_label = $welcome['action_label_free'];
261+
262+
} elseif ( ! in_array( 'pro', $dismissed, true ) ) {
243263
$notice = 'pro';
244264
$action_url = 'https://snipco.de/Mlll';
245265
$action_label = __( 'Upgrade now', 'code-snippets' );
@@ -259,7 +279,7 @@ public function print_notices() {
259279
esc_attr( sanitize_key( $notice ) )
260280
);
261281

262-
echo wp_kses( $text, [ 'strong' => [] ] );
282+
echo wp_kses_post( $text );
263283

264284
printf(
265285
'<a href="%s" class="button button-secondary" target="_blank" style="margin: auto .5em;">%s</a>',
@@ -269,7 +289,7 @@ public function print_notices() {
269289

270290
printf(
271291
'<a href="%s" class="notice-dismiss"><span class="screen-reader-text">%s</span></a>',
272-
esc_url( wp_nonce_url( add_query_arg( $key, $notice ), $key ) ),
292+
esc_url( wp_nonce_url( add_query_arg( $meta_key, $notice ), $meta_key ) ),
273293
esc_attr__( 'Dismiss', 'code-snippets' )
274294
);
275295

src/php/class-upgrade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private function do_site_upgrades() {
9898
}
9999

100100
clean_snippets_cache( $table_name );
101-
Welcome_Menu::clear_cache();
101+
Welcome_API::clear_cache();
102102
}
103103

104104
/**

0 commit comments

Comments
 (0)