/var/www/html_fr/wp-content/plugins/fluent-smtp/app/Hooks/Handlers/ActionsRegistrar.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

namespace FluentMail\App\Hooks\Handlers;

use 
FluentMail\Includes\Core\Application;
use 
FluentMail\App\Hooks\Handlers\AdminMenuHandler;
use 
FluentMail\App\Hooks\Handlers\SchedulerHandler;
use 
FluentMail\App\Hooks\Handlers\InitializeSiteHandler;
use 
WP_REST_Request;

class 
ActionsRegistrar
{
    
/**
     * Application instance.
     *
     * @var Application
     */
    
protected $app;

    
/**
     * Constructor.
     *
     * @param Application $app
     */
    
public function __construct(Application $app)
    {
        
$this->app $app;
    }

    
/**
     * Alternative static constructor.
     *
     * @param Application $app
     * @return static
     */
    
public static function init(Application $app)
    {
        
$instance = new static($app);
        
$instance->registerHooks();
        return 
$instance;
    }

    
/**
     * Register all core hooks and REST routes.
     *
     * @return void
     */
    
public function registerHooks()
    {
        
$this->registerAdminMenu();
        
$this->registerScheduler();
        
$this->registerSiteInitialization();
        
$this->registerCustomActions();
        
$this->registerRestRoutes();
    }

    
/**
     * Register admin menu and notices.
     *
     * @return void
     */
    
protected function registerAdminMenu()
    {
        
$adminMenuHandler = new AdminMenuHandler($this->app);
        
$adminMenuHandler->addFluentMailMenu();

        
$this->app->addAction('admin_notices''AdminMenuHandler@maybeAdminNotice');
    }

    
/**
     * Register background scheduler hooks.
     *
     * @return void
     */
    
protected function registerScheduler()
    {
        (new 
SchedulerHandler)->register();
    }

    
/**
     * Register site-level initialization logic.
     *
     * @return void
     */
    
protected function registerSiteInitialization()
    {
        (new 
InitializeSiteHandler)->addHandler();
    }

    
/**
     * Register custom application actions.
     *
     * @return void
     */
    
protected function registerCustomActions()
    {
        
$this->app->addCustomAction(
            
'handle_exception''ExceptionHandler@handle'
        
);
    }

    
/**
     * Register REST API routes.
     *
     * @return void
     */
    
protected function registerRestRoutes()
    {
        
$this->app->addAction('rest_api_init', function () {
            
register_rest_route('fluent-smtp''/outlook_callback/', [
                
'methods'             => 'GET',
                
'callback'            => [$this'handleOutlookCallback'],
                
'permission_callback' => [$this'verifyOutlookCallbackState'],
            ]);
        });
    }

    
/**
     * Handle the Outlook OAuth callback.
     *
     * @param WP_REST_Request $request
     * @return void
     */
    
public function handleOutlookCallback(WP_REST_Request $request)
    {
        
$code $request->get_param('code');

        
$output $this->app->view->make('admin.html_code', [
            
'title' => 'Your Access Code',
            
'body'  => sprintf(
                
'<p>Copy the following code and paste in the fluentSMTP settings</p><textarea readonly>%s</textarea>',
                
sanitize_textarea_field($code)
            ),
        ]);

        
wp_die($output'Access Code');
    }

    
/**
     * Verify the 'state' parameter in the OAuth callback.
     *
     * @return bool
     */
    
public function verifyOutlookCallbackState()
    {
        
$state $_REQUEST['state'] ?? null;

        return 
$state === get_option('_fluentmail_last_generated_state');
    }
}