1414class 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}
0 commit comments