22
33namespace SimplyBook \Controllers ;
44
5- use SimplyBook \Bootstrap \ App ;
5+ use SimplyBook \Http \ ApiClient ;
66use SimplyBook \Traits \HasViews ;
77use SimplyBook \Traits \LegacyLoad ;
88use SimplyBook \Traits \HasUserAccess ;
99use SimplyBook \Traits \HasAllowlistControl ;
1010use SimplyBook \Services \ThemeColorService ;
1111use SimplyBook \Interfaces \ControllerInterface ;
12+ use SimplyBook \Support \Helpers \Storages \GeneralConfig ;
13+ use SimplyBook \Support \Helpers \Storages \RequestStorage ;
14+ use SimplyBook \Support \Helpers \Storages \EnvironmentConfig ;
1215
1316class DashboardController implements ControllerInterface
1417{
@@ -17,12 +20,18 @@ class DashboardController implements ControllerInterface
1720 use HasUserAccess;
1821 use HasAllowlistControl;
1922
20- private App $ app ;
23+ private ApiClient $ client ;
24+ private EnvironmentConfig $ env ;
25+ private RequestStorage $ request ;
26+ private GeneralConfig $ config ;
2127 private ThemeColorService $ themeColorService ;
2228
23- public function __construct (App $ app , ThemeColorService $ themeColorService )
29+ public function __construct (ApiClient $ client , EnvironmentConfig $ env , GeneralConfig $ config , RequestStorage $ request , ThemeColorService $ themeColorService )
2430 {
25- $ this ->app = $ app ;
31+ $ this ->client = $ client ;
32+ $ this ->env = $ env ;
33+ $ this ->request = $ request ;
34+ $ this ->config = $ config ;
2635 $ this ->themeColorService = $ themeColorService ;
2736 }
2837
@@ -46,8 +55,8 @@ public function register(): void
4655 */
4756 public function enqueueSimplyBookDashiconStyle (): void
4857 {
49- $ iconCss = $ this ->app -> env ->getUrl ('plugin.assets_url ' ) . 'css/simplybook-icon.css ' ;
50- wp_enqueue_style ('simplybook-font ' , $ iconCss , [], $ this ->app -> env ->getString ('plugin.version ' ));
58+ $ iconCss = $ this ->env ->getUrl ('plugin.assets_url ' ) . 'css/simplybook-icon.css ' ;
59+ wp_enqueue_style ('simplybook-font ' , $ iconCss , [], $ this ->env ->getString ('plugin.version ' ));
5160 }
5261
5362 /**
@@ -64,7 +73,7 @@ public function maybeRedirectToDashboard(string $pageSource = ''): void
6473 return ;
6574 }
6675
67- wp_safe_redirect ($ this ->app -> env ->getUrl ('plugin.dashboard_url ' ));
76+ wp_safe_redirect ($ this ->env ->getUrl ('plugin.dashboard_url ' ));
6877 exit ;
6978 }
7079
@@ -109,22 +118,22 @@ public function addDashboardPage(): void
109118 */
110119 public function renderReactApp (): void
111120 {
112- $ this ->render ('admin/dashboard ' , [], ' html ' );
121+ $ this ->render ('admin/dashboard ' );
113122 }
114123
115124 /**
116125 * Enqueue the Tailwind CSS for the dashboard in the header
117126 */
118127 public function enqueueDashboardStyles (): void
119128 {
120- $ chunkTranslation = $ this ->getReactChunkTranslations ();
129+ $ chunkTranslation = $ this ->getReactChunksAndTranslations ();
121130 if (empty ($ chunkTranslation )) {
122131 return ;
123132 }
124133
125134 wp_enqueue_style (
126135 'simplybook-tailwind ' ,
127- $ this ->app -> env ->getUrl ('plugin.assets_url ' ) . '/css/tailwind.generated.css ' ,
136+ $ this ->env ->getUrl ('plugin.assets_url ' ) . '/css/tailwind.generated.css ' ,
128137 [],
129138 ($ chunkTranslation ['version ' ] ?? '' )
130139 );
@@ -139,23 +148,23 @@ public function enqueueDashboardStyles(): void
139148 */
140149 public function enqueueReactScripts (): void
141150 {
142- $ chunkTranslation = $ this ->getReactChunkTranslations ();
151+ $ chunkTranslation = $ this ->getReactChunksAndTranslations ();
143152 if (empty ($ chunkTranslation )) {
144153 return ;
145154 }
146155
147156 // Enqueue SimplyBook Widget script for preview functionality
148157 wp_enqueue_script (
149158 'simplybookMePl_widget_scripts ' ,
150- $ this ->app -> env ->getUrl ('simplybook.widget_script_url ' ),
159+ $ this ->env ->getUrl ('simplybook.widget_script_url ' ),
151160 [],
152- $ this ->app -> env ->getString ('simplybook.widget_script_version ' ),
161+ $ this ->env ->getString ('simplybook.widget_script_version ' ),
153162 true
154163 );
155164
156165 wp_enqueue_script (
157166 'simplybook-main-script ' ,
158- $ this ->app -> env ->getUrl ('plugin.react_url ' ) . '/build/ ' . ($ chunkTranslation ['js_file_name ' ] ?? '' ),
167+ $ this ->env ->getUrl ('plugin.react_url ' ) . '/build/ ' . ($ chunkTranslation ['js_file_name ' ] ?? '' ),
159168 ($ chunkTranslation ['dependencies ' ] ?? '' ),
160169 ($ chunkTranslation ['version ' ] ?? '' ),
161170 true
@@ -177,15 +186,17 @@ public function enqueueReactScripts(): void
177186 * simplest approach. Simplicity is king here.
178187 * @see https://wordpress.com/blog/2022/01/06/wordpress-plugin-i18n-webpack-and-composer/
179188 */
180- private function getReactChunkTranslations (): array
189+ private function getReactChunksAndTranslations (): array
181190 {
182- $ cacheName = 'simplybook-react-chunk-translations ' ;
183- if ($ cache = wp_cache_get ($ cacheName , 'simplybook ' )) {
184- return $ cache ;
191+ $ cacheName = 'simplybook-react-chunk-and-translations ' ;
192+ $ cacheValue = wp_cache_get ($ cacheName , 'simplybook ' , false , $ found );
193+
194+ if ($ found && is_array ($ cacheValue )) {
195+ return $ cacheValue ;
185196 }
186197
187198 // get all files from the settings/build folder
188- $ buildDirPath = $ this ->app -> env ->getString ('plugin.react_path ' ) . '/build ' ;
199+ $ buildDirPath = $ this ->env ->getString ('plugin.react_path ' ) . '/build ' ;
189200 $ filenames = scandir ($ buildDirPath );
190201
191202 $ jsFileName = '' ;
@@ -209,8 +220,8 @@ private function getReactChunkTranslations(): array
209220 // remove extension from $filename
210221 $ chunkHandle = str_replace ('.js ' , '' , $ filename );
211222 // temporarily register the script, so we can get a translations object.
212- $ chunkSource = $ this ->app -> env ->getUrl ('plugin.react_url ' ) . '/build/ ' . $ filename ;
213- wp_register_script ($ chunkHandle , $ chunkSource , [], $ this ->app -> env ->getString ('plugin.version ' ), true );
223+ $ chunkSource = $ this ->env ->getUrl ('plugin.react_url ' ) . '/build/ ' . $ filename ;
224+ wp_register_script ($ chunkHandle , $ chunkSource , [], $ this ->env ->getString ('plugin.version ' ), true );
214225
215226 //as there is no pro version of this plugin, no need to declare a path
216227 $ localeData = load_script_textdomain ($ chunkHandle , 'simplybook ' );
@@ -233,7 +244,7 @@ private function getReactChunkTranslations(): array
233244 'version ' => $ assetFileData ['version ' ] ?? '' ,
234245 ];
235246
236- wp_cache_set ($ cacheName , $ chunkTranslations , 'simplybook ' );
247+ wp_cache_set ($ cacheName , $ chunkTranslations , 'simplybook ' , ( 5 * MINUTE_IN_SECONDS ) );
237248 return $ chunkTranslations ;
238249 }
239250
@@ -250,21 +261,21 @@ private function localizedReactSettings(array $chunkTranslation): array
250261 'x_wp_nonce ' => wp_create_nonce ('wp_rest ' ),
251262 'ajax_url ' => admin_url ('admin-ajax.php ' ),
252263 'rest_url ' => get_rest_url (),
253- 'rest_namespace ' => $ this ->app -> env ->getString ('http.namespace ' ),
254- 'rest_version ' => $ this ->app -> env ->getString ('http.version ' ),
264+ 'rest_namespace ' => $ this ->env ->getString ('http.namespace ' ),
265+ 'rest_version ' => $ this ->env ->getString ('http.version ' ),
255266 'site_url ' => site_url (),
256- 'dashboard_url ' => $ this ->app -> env ->getUrl ('plugin.dashboard_url ' ),
257- 'assets_url ' => $ this ->app -> env ->getUrl ('plugin.assets_url ' ),
267+ 'dashboard_url ' => $ this ->env ->getUrl ('plugin.dashboard_url ' ),
268+ 'assets_url ' => $ this ->env ->getUrl ('plugin.assets_url ' ),
258269 'debug ' => defined ('SIMPLYBOOK_DEBUG ' ) && SIMPLYBOOK_DEBUG ,
259270 'json_translations ' => ($ chunkTranslation ['json_translations ' ] ?? []),
260271 'settings_menu ' => $ this ->menu (),
261272 'settings_fields ' => $ this ->fields (true ),
262273 'is_onboarding_completed ' => $ this ->isOnboardingCompleted (),
263274 'first_name ' => $ this ->getCurrentUserFirstName (),
264275 'completed_step ' => get_option ('simplybook_completed_step ' , 0 ),
265- 'simplybook_domains ' => $ this ->app -> env ->get ('simplybook.domains ' ),
266- 'simplybook_countries ' => $ this ->app -> config ->get ('countries ' ),
267- 'support ' => $ this ->app -> env ->get ('simplybook.support ' ),
276+ 'simplybook_domains ' => $ this ->env ->get ('simplybook.domains ' ),
277+ 'simplybook_countries ' => $ this ->config ->get ('countries ' ),
278+ 'support ' => $ this ->env ->get ('simplybook.support ' ),
268279 'fallback_colors ' => $ this ->themeColorService ->getFallbackColors (),
269280 ]
270281 );
@@ -281,11 +292,11 @@ private function isOnboardingCompleted(): bool
281292 */
282293 public function maybeResetRegistration (): void
283294 {
284- if ($ this ->app -> request ->getString ('reset_registration ' , 'false ' ) !== 'true ' ) {
295+ if ($ this ->request ->getString ('global. reset_registration ' , 'false ' ) !== 'true ' ) {
285296 return ;
286297 }
287298
288- $ this ->app -> client ->reset_registration ();
299+ $ this ->client ->reset_registration ();
289300 }
290301
291302 /**
0 commit comments