@@ -196,54 +196,35 @@ private function registerHooks(): void
196196 ];
197197
198198 foreach ($ hooks as $ hook ) {
199- // Check if hook already exists (include callback_method to support
200- // multiple callbacks on the same hook_name, e.g. audio + PDF on digital_player )
199+ // Atomic upsert — relies on UNIQUE KEY uk_plugin_hook_callback
200+ // (plugin_id, hook_name, callback_class, callback_method )
201201 $ stmt = $ this ->db ->prepare ("
202- SELECT id FROM plugin_hooks
203- WHERE plugin_id = ? AND hook_name = ? AND callback_method = ?
202+ INSERT INTO plugin_hooks
203+ (plugin_id, hook_name, callback_class, callback_method, priority, is_active)
204+ VALUES (?, ?, ?, ?, ?, ?)
205+ ON DUPLICATE KEY UPDATE
206+ priority = VALUES(priority),
207+ is_active = VALUES(is_active)
204208 " );
205209 if (!$ stmt ) {
206- \App \Support \SecureLogger::error ('DigitalLibraryPlugin: prepare() failed for hook check ' , ['hook ' => $ hook ['hook_name ' ]]);
210+ \App \Support \SecureLogger::error ('DigitalLibraryPlugin: prepare() failed for hook upsert ' , ['hook ' => $ hook ['hook_name ' ]]);
207211 continue ;
208212 }
209- $ stmt ->bind_param ("iss " , $ this ->pluginId , $ hook ['hook_name ' ], $ hook ['callback_method ' ]);
210- $ stmt ->execute ();
211- $ result = $ stmt ->get_result ();
212- if (!$ result instanceof \mysqli_result) {
213- \App \Support \SecureLogger::error ('DigitalLibraryPlugin: get_result() failed for hook check ' , ['hook ' => $ hook ['hook_name ' ]]);
214- $ stmt ->close ();
215- continue ;
216- }
217-
218- if ($ result ->num_rows === 0 ) {
219- $ stmt ->close (); // close SELECT stmt before reassignment
220- // Insert new hook
221- $ stmt = $ this ->db ->prepare ("
222- INSERT INTO plugin_hooks
223- (plugin_id, hook_name, callback_class, callback_method, priority, is_active)
224- VALUES (?, ?, ?, ?, ?, ?)
225- " );
226- if (!$ stmt ) {
227- \App \Support \SecureLogger::error ('DigitalLibraryPlugin: prepare() failed for hook insert ' , ['hook ' => $ hook ['hook_name ' ]]);
228- continue ;
229- }
230- $ stmt ->bind_param (
231- "isssii " ,
232- $ this ->pluginId ,
233- $ hook ['hook_name ' ],
234- $ hook ['callback_class ' ],
235- $ hook ['callback_method ' ],
236- $ hook ['priority ' ],
237- $ hook ['is_active ' ]
238- );
239- if (!$ stmt ->execute ()) {
240- \App \Support \SecureLogger::error ('[Digital Library] Hook insert failed ' , [
241- 'hook ' => $ hook ['hook_name ' ],
242- 'error ' => $ stmt ->error ,
243- ]);
244- }
213+ $ stmt ->bind_param (
214+ "isssii " ,
215+ $ this ->pluginId ,
216+ $ hook ['hook_name ' ],
217+ $ hook ['callback_class ' ],
218+ $ hook ['callback_method ' ],
219+ $ hook ['priority ' ],
220+ $ hook ['is_active ' ]
221+ );
222+ if (!$ stmt ->execute ()) {
223+ \App \Support \SecureLogger::error ('[Digital Library] Hook upsert failed ' , [
224+ 'hook ' => $ hook ['hook_name ' ],
225+ 'error ' => $ stmt ->error ,
226+ ]);
245227 }
246-
247228 $ stmt ->close ();
248229 }
249230 }
@@ -320,8 +301,9 @@ public function renderAudioPlayer(array $book): void
320301 */
321302 public function renderPdfViewer (array $ book ): void
322303 {
323- $ fileUrl = $ book ['file_url ' ] ?? '' ;
324- if (!empty ($ fileUrl ) && strtolower (pathinfo ($ fileUrl , PATHINFO_EXTENSION )) === 'pdf ' ) {
304+ $ fileUrl = (string ) ($ book ['file_url ' ] ?? '' );
305+ $ filePath = (string ) (parse_url ($ fileUrl , PHP_URL_PATH ) ?? '' );
306+ if ($ filePath !== '' && strtolower (pathinfo ($ filePath , PATHINFO_EXTENSION )) === 'pdf ' ) {
325307 include __DIR__ . '/views/frontend-pdf-viewer.php ' ;
326308 }
327309 }
0 commit comments