Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluent-smtp/app/Hooks/Handlers/ActionsRegistrar.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace FluentMail\App\Hooks\Handlers;
4 +
5 + use FluentMail\Includes\Core\Application;
6 + use FluentMail\App\Hooks\Handlers\AdminMenuHandler;
7 + use FluentMail\App\Hooks\Handlers\SchedulerHandler;
8 + use FluentMail\App\Hooks\Handlers\InitializeSiteHandler;
9 + use WP_REST_Request;
10 +
11 + class ActionsRegistrar
12 + {
13 + /**
14 + * Application instance.
15 + *
16 + * @var Application
17 + */
18 + protected $app;
19 +
20 + /**
21 + * Constructor.
22 + *
23 + * @param Application $app
24 + */
25 + public function __construct(Application $app)
26 + {
27 + $this->app = $app;
28 + }
29 +
30 + /**
31 + * Alternative static constructor.
32 + *
33 + * @param Application $app
34 + * @return static
35 + */
36 + public static function init(Application $app)
37 + {
38 + $instance = new self($app);
39 + $instance->registerHooks();
40 + return $instance;
41 + }
42 +
43 + /**
44 + * Register all core hooks and REST routes.
45 + *
46 + * @return void
47 + */
48 + public function registerHooks()
49 + {
50 + $this->registerAdminMenu();
51 + $this->registerScheduler();
52 + $this->registerSiteInitialization();
53 + $this->registerCustomActions();
54 + $this->registerRestRoutes();
55 + }
56 +
57 + /**
58 + * Register admin menu and notices.
59 + *
60 + * @return void
61 + */
62 + protected function registerAdminMenu()
63 + {
64 + $adminMenuHandler = new AdminMenuHandler($this->app);
65 + $adminMenuHandler->addFluentMailMenu();
66 +
67 + $this->app->addAction('admin_notices', 'AdminMenuHandler@maybeAdminNotice');
68 + }
69 +
70 + /**
71 + * Register background scheduler hooks.
72 + *
73 + * @return void
74 + */
75 + protected function registerScheduler()
76 + {
77 + (new SchedulerHandler)->register();
78 + }
79 +
80 + /**
81 + * Register site-level initialization logic.
82 + *
83 + * @return void
84 + */
85 + protected function registerSiteInitialization()
86 + {
87 + (new InitializeSiteHandler)->addHandler();
88 + }
89 +
90 + /**
91 + * Register custom application actions.
92 + *
93 + * @return void
94 + */
95 + protected function registerCustomActions()
96 + {
97 + $this->app->addCustomAction(
98 + 'handle_exception', 'ExceptionHandler@handle'
99 + );
100 + }
101 +
102 + /**
103 + * Register REST API routes.
104 + *
105 + * @return void
106 + */
107 + protected function registerRestRoutes()
108 + {
109 + $this->app->addAction('rest_api_init', function () {
110 + register_rest_route('fluent-smtp', '/outlook_callback/', [
111 + 'methods' => 'GET',
112 + 'callback' => [$this, 'handleOutlookCallback'],
113 + 'permission_callback' => [$this, 'verifyOutlookCallbackState'],
114 + ]);
115 + });
116 + }
117 +
118 + /**
119 + * Handle the Outlook OAuth callback.
120 + *
121 + * @param WP_REST_Request $request
122 + * @return void
123 + */
124 + public function handleOutlookCallback(WP_REST_Request $request)
125 + {
126 + $code = $request->get_param('code');
127 +
128 + $output = $this->app->view->make('admin.html_code', [
129 + 'title' => 'Your Access Code',
130 + 'body' => sprintf(
131 + '<p>Copy the following code and paste in the fluentSMTP settings</p><textarea readonly>%s</textarea>',
132 + sanitize_textarea_field($code)
133 + ),
134 + ]);
135 +
136 + wp_die($output, 'Access Code');
137 + }
138 +
139 + /**
140 + * Verify the 'state' parameter in the OAuth callback.
141 + *
142 + * @return bool
143 + */
144 + public function verifyOutlookCallbackState()
145 + {
146 + $state = $_REQUEST['state'] ?? null;
147 +
148 + return $state === get_option('_fluentmail_last_generated_state');
149 + }
150 + }
151 +