1414use phpbb \config \config ;
1515use phpbb \controller \helper ;
1616use phpbb \db \driver \driver_interface ;
17+ use phpbb \language \language ;
1718use phpbb \log \log_interface ;
1819use phpbb \notification \method \messenger_base ;
1920use phpbb \notification \type \type_interface ;
@@ -36,6 +37,9 @@ class webpush extends messenger_base implements extended_method_interface
3637 /** @var driver_interface */
3738 protected $ db ;
3839
40+ /** @var language */
41+ protected $ language ;
42+
3943 /** @var log_interface */
4044 protected $ log ;
4145
@@ -62,6 +66,7 @@ class webpush extends messenger_base implements extended_method_interface
6266 *
6367 * @param config $config
6468 * @param driver_interface $db
69+ * @param language $language
6570 * @param log_interface $log
6671 * @param user_loader $user_loader
6772 * @param user $user
@@ -71,13 +76,14 @@ class webpush extends messenger_base implements extended_method_interface
7176 * @param string $notification_webpush_table
7277 * @param string $push_subscriptions_table
7378 */
74- public function __construct (config $ config , driver_interface $ db , log_interface $ log , user_loader $ user_loader , user $ user , path_helper $ path_helper ,
79+ public function __construct (config $ config , driver_interface $ db , language $ language , log_interface $ log , user_loader $ user_loader , user $ user , path_helper $ path_helper ,
7580 string $ phpbb_root_path , string $ php_ext , string $ notification_webpush_table , string $ push_subscriptions_table )
7681 {
7782 parent ::__construct ($ user_loader , $ phpbb_root_path , $ php_ext );
7883
7984 $ this ->config = $ config ;
8085 $ this ->db = $ db ;
86+ $ this ->language = $ language ;
8187 $ this ->log = $ log ;
8288 $ this ->user = $ user ;
8389 $ this ->path_helper = $ path_helper ;
@@ -140,10 +146,21 @@ public function notify()
140146 {
141147 $ insert_buffer = new \phpbb \db \sql_insert_buffer ($ this ->db , $ this ->notification_webpush_table );
142148
149+ // Load all users data we want to notify
150+ $ notify_users = $ this ->load_recipients_data ();
151+
143152 /** @var type_interface $notification */
144153 foreach ($ this ->queue as $ notification )
145154 {
146155 $ data = $ notification ->get_insert_array ();
156+
157+ // Change notification language if needed only
158+ $ recipient_data = $ this ->user_loader ->get_user ($ notification ->user_id );
159+ if ($ this ->language ->get_used_language () !== $ recipient_data ['user_lang ' ])
160+ {
161+ $ this ->language ->set_user_language ($ recipient_data ['user_lang ' ], true );
162+ }
163+
147164 $ data += [
148165 'push_data ' => json_encode ([
149166 'heading ' => $ this ->config ['sitename ' ],
@@ -162,41 +179,30 @@ public function notify()
162179
163180 $ insert_buffer ->flush ();
164181
165- $ this ->notify_using_webpush ();
182+ // Restore current user's language if needed only
183+ if ($ this ->language ->get_used_language () !== $ this ->user ->data ['user_lang ' ])
184+ {
185+ $ this ->language ->set_user_language ($ this ->user ->data ['user_lang ' ], true );
186+ }
187+
188+ $ this ->notify_using_webpush ($ notify_users );
166189
167190 return false ;
168191 }
169192
170193 /**
171194 * Notify using Web Push
172195 *
196+ * @param array $notify_users Array of user ids to notify
173197 * @return void
174198 */
175- protected function notify_using_webpush (): void
199+ protected function notify_using_webpush ($ notify_users = [] ): void
176200 {
177201 if (empty ($ this ->queue ))
178202 {
179203 return ;
180204 }
181205
182- // Load all users we want to notify
183- $ user_ids = [];
184- foreach ($ this ->queue as $ notification )
185- {
186- $ user_ids [] = $ notification ->user_id ;
187- }
188-
189- // Do not send push notifications to banned users
190- if (!function_exists ('phpbb_get_banned_user_ids ' ))
191- {
192- include ($ this ->phpbb_root_path . 'includes/functions_user. ' . $ this ->php_ext );
193- }
194- $ banned_users = phpbb_get_banned_user_ids ($ user_ids );
195-
196- // Load all the users we need
197- $ notify_users = array_diff ($ user_ids , $ banned_users );
198- $ this ->user_loader ->load_users ($ notify_users , array (USER_IGNORE ));
199-
200206 // Get subscriptions for users
201207 $ user_subscription_map = $ this ->get_user_subscription_map ($ notify_users );
202208
@@ -518,4 +524,31 @@ protected function set_endpoint_padding(\Minishlink\WebPush\WebPush $web_push, s
518524 }
519525 }
520526 }
527+
528+ /**
529+ * Load all users data to send notifications
530+ *
531+ * @return array Array of user ids to notify
532+ */
533+ protected function load_recipients_data (): array
534+ {
535+ $ notify_users = $ user_ids = [];
536+ foreach ($ this ->queue as $ notification )
537+ {
538+ $ user_ids [] = $ notification ->user_id ;
539+ }
540+
541+ // Do not send push notifications to banned users
542+ if (!function_exists ('phpbb_get_banned_user_ids ' ))
543+ {
544+ include ($ this ->phpbb_root_path . 'includes/functions_user. ' . $ this ->php_ext );
545+ }
546+ $ banned_users = phpbb_get_banned_user_ids ($ user_ids );
547+
548+ // Load all the users we need
549+ $ notify_users = array_diff ($ user_ids , $ banned_users );
550+ $ this ->user_loader ->load_users ($ notify_users , [USER_IGNORE ]);
551+
552+ return $ notify_users ;
553+ }
521554}
0 commit comments