PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / admin / actions / AdminActionRegistry.php

AdminActionRegistry.php in 404 Solution 4.3.0, at includes/admin/actions/AdminActionRegistry.php

85 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /**
8 * Resolves admin action names to handler instances.
9 *
10 * Exact action names are checked before prefix handlers so broad operations
11 * like "bulk*" cannot shadow a specific registered action.
12 */
13 class ABJ_404_Solution_AdminActionRegistry {
14
15 /** @var ABJ_404_Solution_PluginLogicAdminActions */
16 private $parent;
17
18 public function __construct(ABJ_404_Solution_PluginLogicAdminActions $parent) {
19 $this->parent = $parent;
20 }
21
22 /**
23 * @param string $action
24 * @return ABJ_404_Solution_AdminActionHandlerInterface|null
25 */
26 public function resolve(string $action) {
27 $exact = $this->actionHandlerMap();
28 if (isset($exact[$action])) {
29 return $this->instantiateHandler($exact[$action]);
30 }
31 foreach ($this->actionHandlerPrefixMap() as $prefix => $cls) {
32 if ($action !== '' && strpos($action, $prefix) === 0) {
33 return $this->instantiateHandler($cls);
34 }
35 }
36 return null;
37 }
38
39 /**
40 * @return array<string, class-string<ABJ_404_Solution_AdminActionHandlerInterface>>
41 */
42 private function actionHandlerMap(): array {
43 return array(
44 'updateOptions' => 'ABJ_404_Solution_UpdateOptionsHandler',
45 'addRedirect' => 'ABJ_404_Solution_AddRedirectHandler',
46 'emptyRedirectTrash' => 'ABJ_404_Solution_EmptyRedirectTrashHandler',
47 'emptyCapturedTrash' => 'ABJ_404_Solution_EmptyCapturedTrashHandler',
48 'purgeRedirects' => 'ABJ_404_Solution_PurgeRedirectsHandler',
49 'runMaintenance' => 'ABJ_404_Solution_RunMaintenanceHandler',
50 'rebuildNgramCache' => 'ABJ_404_Solution_RebuildNgramCacheHandler',
51 'clearSpellingCache' => 'ABJ_404_Solution_ClearSpellingCacheHandler',
52 'saveGscSettings' => 'ABJ_404_Solution_SaveGscSettingsHandler',
53 'importFromPlugin' => 'ABJ_404_Solution_ImportFromPluginHandler',
54 'undoRegexAutoPromote' => 'ABJ_404_Solution_UndoRegexAutoPromoteHandler',
55 );
56 }
57
58 /**
59 * @return array<string, class-string<ABJ_404_Solution_AdminActionHandlerInterface>>
60 */
61 private function actionHandlerPrefixMap(): array {
62 return array(
63 'bulk' => 'ABJ_404_Solution_BulkActionHandler',
64 );
65 }
66
67 /**
68 * @param class-string<ABJ_404_Solution_AdminActionHandlerInterface> $cls
69 * @return ABJ_404_Solution_AdminActionHandlerInterface
70 */
71 private function instantiateHandler(string $cls): ABJ_404_Solution_AdminActionHandlerInterface {
72 $reflect = new ReflectionClass($cls);
73 $ctor = $reflect->getConstructor();
74 if ($ctor === null || $ctor->getNumberOfParameters() === 0) {
75 $instance = $reflect->newInstance();
76 } else {
77 $instance = $reflect->newInstance($this->parent);
78 }
79 if (!$instance instanceof ABJ_404_Solution_AdminActionHandlerInterface) {
80 throw new RuntimeException("Handler class {$cls} does not implement AdminActionHandlerInterface.");
81 }
82 return $instance;
83 }
84 }
85