/var/www/html_uk/wp-content/plugins/automatewoo/admin/controllers/tools.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php

namespace AutomateWoo\Admin\Controllers;

use 
AutomateWoo\Clean;
use 
AutomateWoo\Tool_Abstract;

defined'ABSPATH' ) || exit;

/**
 * Controller for the tools page.
 *
 * @class Tools_Controller
 */
class Tools_Controller extends Base {
    
/**
     * Handle requests to the tools page.
     *
     * @return void
     */
    
public function handle() {
        
$action  $this->get_current_action();
        
$tool_id Clean::stringaw_request'tool_id' ) );
        
$tool    AW()->tools_service()->get_tool$tool_id );

        if ( 
$this->default_route !== $action && ! $tool ) {
            
wp_dieesc_html__'Invalid tool.''automatewoo' ) );
        }

        switch ( 
$action ) {
            case 
'view':
                if ( 
$tool->get_form_fields() ) {
                    
$this->output_view_form$tool_id );
                } else {
                    
// Jump to confirm view if no fields are present.
                    
$this->output_view_confirm$tool_id );
                }
                break;

            case 
'validate':
                if ( 
$this->validate_process$tool_id ) ) {
                    
$this->output_view_confirm$tool_id );
                } else {
                    
$this->output_view_form$tool_id );
                }

                break;

            case 
'confirm':
                
$this->confirm_process$tool_id );
                
$this->output_view_listing();
                break;

            default:
                
$this->output_view_listing();
        }

        
wp_enqueue_script'automatewoo-tools' );
    }

    
/**
     * Output the view for the default tools page.
     *
     * @return void
     */
    
private function output_view_listing() {
        
$this->output_view(
            
'page-tools-list',
            [
                
'tools' => AW()->tools_service()->get_tools(),
            ]
        );
    }


    
/**
     * Output a form view for a given tool
     *
     * @param string $tool_id The ID of the current tool.
     *
     * @return void
     */
    
private function output_view_form$tool_id ) {
        
$tool AW()->tools_service()->get_tool$tool_id );

        
$this->output_view(
            
'page-tools-form',
            [
                
'tool' => $tool,
            ]
        );
    }


    
/**
     * Output the confirmation view for a given tool
     *
     * @param string $tool_id The ID of the current tool.
     *
     * @return void
     */
    
private function output_view_confirm$tool_id ) {
        
$tool AW()->tools_service()->get_tool$tool_id );
        
$args $tool->sanitize_argsaw_request'args' ) );

        
$this->output_view(
            
'page-tools-form-confirm',
            [
                
'tool' => $tool,
                
'args' => $args,
            ]
        );
    }

    
/**
     * Return true if init was successful
     *
     * @param string $tool_id The ID of the current tool.
     *
     * @return bool
     */
    
private function validate_process$tool_id ) {
        
$tool AW()->tools_service()->get_tool$tool_id );
        
$args $tool->sanitize_argsaw_request'args' ) );

        if ( ! 
$tool ) {
            
wp_dieesc_html__'Invalid tool.''automatewoo' ) );
        }

        
$valid $tool->validate_process$args );

        if ( 
false === $valid ) {
            
$this->add_error__'Failed to init process.''automatewoo' ) );
            return 
false;
        } elseif ( 
is_wp_error$valid ) ) {
            
$this->add_error$valid->get_error_message() );
            return 
false;
        } elseif ( 
true === $valid ) {
            return 
true;
        }
        return 
false;
    }

    
/**
     * Run security checks and process the tool after confirmation
     *
     * @param string $tool_id The ID of the current tool.
     *
     * @return void
     */
    
private function confirm_process$tool_id ) {

        
$nonce Clean::stringaw_request'_wpnonce' ) );

        if ( ! 
wp_verify_nonce$nonce$tool_id ) ) {
            
wp_dieesc_html__'Security check failed.''automatewoo' ) );
        }

        
// Process should be valid at this point but just in case.
        
if ( ! $this->validate_process$tool_id ) ) {
            
wp_dieesc_html__'Process could not be validated.''automatewoo' ) );
        }

        
$tool AW()->tools_service()->get_tool$tool_id );
        
$args aw_request'args' );

        
$processed $tool->process$args );

        if ( 
false === $processed ) {
            
$this->add_error__'Process failed.''automatewoo' ) );
        } elseif ( 
is_wp_error$processed ) ) {
            
$this->add_error$processed->get_error_message() );
        } elseif ( 
true === $processed ) {
            
$this->add_message__'Success - Items may be still be processing in the background.''automatewoo' ) );
        }
    }

    
/**
     * Get URL for the main tools page or a specific tool.
     *
     * @param string|bool        $route Value to set for the action query arg.
     * @param Tool_Abstract|bool $tool  Tool to set for the tool_id query arg.
     *
     * @return string
     */
    
public function get_route_url$route false$tool false ) {
        
$base_url admin_url'admin.php?page=automatewoo-tools' );

        if ( ! 
$route ) {
            return 
$base_url;
        }

        
// SEMGREP WARNING EXPLANATION
        // This is being escaped later in the consumer call (if not, a warning will be produced by PHPCS).
        
return add_query_arg(
            [
                
'action'  => $route,
                
'tool_id' => $tool->get_id(),
            ],
            
$base_url
        
);
    }
}

return new 
Tools_Controller();