class-router.php
8.98 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* Parent class to be used by hub endpoints that handle link actions.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.1.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\App\Broken_Links_Actions
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\App\Broken_Links_Actions;
// Abort if called directly.
defined( 'WPINC' ) || die;
use WPMUDEV_BLC\App\Options\Links_Queue\Model as Queue;
use WPMUDEV_BLC\App\Options\Settings\Model as Settings;
use WPMUDEV_BLC\App\Scheduled_Events\Edit_Links\Controller as Schedule;
use WPMUDEV_BLC\Core\Traits\Sanitize;
use WPMUDEV_BLC\Core\Utils\Abstracts\Base;
use WPMUDEV_BLC\Core\Utils\Utilities;
/**
* Class Controller
*
* @package WPMUDEV_BLC\App\Hub_Endpoint\Edit_Link
*/
class Router extends Base {
/**
* Use Sanitize trait.
*/
use Sanitize;
/**
* Directs the endpoint according to params.
*
* @param string|null $action
*
* @return array
*/
public function direct_endpoint( string $action = null ) {
if ( ! empty( Settings::instance()->get( 'use_legacy_blc_version' ) ) ) {
$response['error_code'] = 'blc_local_mode';
$response['message'] = esc_html__( 'BLC is set to local. Can not perform Cloud actions', 'broken-link-checker' );
return $response;
}
/**
* Input format:
* links => array(
* {LINK_KEY_1} => array(
* link => {LINK_URL},
* new_link => {NEW_LINK_URL} OR unlink => true OR nofollow => true,
* full_site => Bool (Default false. Value set by UI option if origins count >= FIXED_LIMIT),
* origins => array() (list of origins),
* ),
* {LINK_KEY_2} => array(
* link => {LINK_URL_2},
* new_link => {NEW_LINK_URL_2} OR unlink => true OR nofollow => true,
* full_site => Bool (Default false),
* origins => array(),
* ),
* )
*/
$input_json = file_get_contents( 'php://input' );
$use_subsite = false;
$prepared_input = $this->prepare_input( $input_json, $action );
$response = array(
'data_received' => true,
'completed' => false,
'links_processed' => null,
'total_links' => null,
'remaining_links' => null,
);
if ( is_wp_error( $prepared_input ) ) {
$response['error_code'] = $prepared_input->get_error_code();
$response['message'] = $prepared_input->get_error_message();
return $response;
} else if ( ! is_array( $prepared_input ) ) {
//$response['success'] = false;
$response['error_code'] = 'blc_unknown_error';
$response['message'] = esc_html__( 'Something went wrong', 'broken-link-checker' );
return $response;
}
// At this point we have checked if it multisite and if site id is valid in `normalize_data()`;
if ( ! empty( $prepared_input['site_id'] ) ) {
$use_subsite = true;
switch_to_blog( $prepared_input['site_id'] );
}
if ( ! empty( $action ) ) {
if ( ! empty( $prepared_input['links'] ) ) {
foreach ( $prepared_input['links'] as $key => $link ) {
$prepared_input['links'][$key][$action] = 'true';
}
}
}
$result = $this->push_to_queue( $prepared_input );
if ( $use_subsite ) {
restore_current_blog();
}
if ( is_wp_error( $result ) ) {
$response['error_code'] = $result->get_error_code();
$response['message'] = $result->get_error_message();
return $response;
}
return wp_parse_args( $result, $response );
}
/**
* @param array $data
*
* @return mixed|array|WP_Error
*/
public function push_to_queue( array $data = array() ) {
if ( empty( $data['links'] ) ) {
return array(
'success' => false,
'completed' => false,
'error_code' => 'blc_link_execution_error',
'message' => esc_html__( 'Empty link', 'broken-link-checker' ),
);
}
$first_link = Queue::instance()->queue_is_empty();
Queue::instance()->push( $data );
// Run schedule in 5 seconds after pushing.
//Schedule::instance()->setup( 5 );
// If Queue is empty, run first link immediately and schedule the rest links.
if ( $first_link ) {
Schedule::instance()->process_scheduled_event();
} else {
Schedule::instance()->setup( 5 );
}
return array(
'success' => true,
'completed' => false,
'links_processed' => Queue::instance()->links_processed(),
'total_links' => Queue::instance()->total_links(),
);
}
/**
* Prepares input json to be used for editing links.
*/
public function prepare_input( string $json_data = '', string $action = '' ) {
$schema = $this->get_sanitize_schema( $action );
$normalized_input = $this->normalize_data( $json_data, $schema );
if ( is_wp_error( $normalized_input ) ) {
return $normalized_input;
}
$links = $normalized_input['links'] ?? null;
if ( empty( $links ) ) {
return new \WP_Error(
'blc-link-action-no-links',
esc_html__(
'Aborting as there are no links',
'broken-link-checker'
)
);
}
return $normalized_input;
}
/**
* Provides the schema that the requested input should have in order to be escaped properly.
*
* @return string[]
*/
public function get_sanitize_schema( string $action = '' ) {
$schema = array(
//'site_id' => 'int',
'link' => 'url',
//'target_tags' => 'attr',
'origins' => 'url',
'full_site' => 'bool',
);
if ( 'edit' === $action ) {
$schema['new_link'] = 'url';
}
return apply_filters(
'wpmudev_blc_link_actions_sanitization_schema',
$schema,
$action,
$this
);
}
/**
* Ensures that requested input is present and sanitizes.
*
* @param string $json_data
* @param array $schema
*
* @return array|\WP_Error
*/
public function normalize_data( string $json_data = '', array $schema = array() ) {
if ( empty( $json_data ) || empty( $schema ) ) {
Utilities::log( 'BLC_LINK_ACTION_ERROR - Missing input conversion params.' );
return new \WP_Error(
'blc-link-action-failed',
esc_html__(
'Missing input conversion params',
'broken-link-checker'
)
);
}
$raw_input = json_decode( $json_data, true );
// Make sure input is valid json.
if ( json_last_error() !== JSON_ERROR_NONE ) {
Utilities::log( 'BLC_LINK_ACTION_ERROR - Exiting because input was not valid json string.' );
return new \WP_Error(
'blc-link-action-failed',
esc_html__(
'Invalid input json string',
'broken-link-checker'
)
);
}
$input_params = $raw_input['params'];
$input_links = $input_params['links'] ?? array();
$params = array();
// Check if there is any missing param.
if ( ! empty( $input_links ) ) {
foreach ( $input_links as $input_link_key => $input_link ) {
foreach ( $schema as $schema_key => $schema_scheme ) {
if ( ! isset( $input_link[ $schema_key ] ) ) {
return new \WP_Error(
'blc-link-action-normalization-error',
sprintf(
esc_html__(
'Links params do not follow schema. Key `%1$s` missing',
'broken-link-checker'
),
$schema_key
)
);
}
if ( 'bool' === $schema_scheme ) {
$input_link[ $schema_key ] = boolval( $input_link[ $schema_key ] );
}
$params['links'][ $input_link_key ][ $schema_key ] = $this->sanitize_params( $input_link[ $schema_key ], $schema_scheme );
if ( json_encode( $params['links'][ $input_link_key ][ $schema_key ] ) !== json_encode( $input_link[ $schema_key ] ) ) {
return new \WP_Error(
'blc-link-action-sanitization-error',
sprintf(
esc_html__(
'Links param `%1$s` potentially malicious',
'broken-link-checker'
),
$schema_key
)
);
}
}
$params['links'][ $input_link_key ]['link'] = sanitize_url( Utilities::make_link_relative( $input_link['link'] ) );
}
}
if ( isset( $input_params['site_id'] ) && is_multisite() ) {
if ( ! is_numeric( $input_params['site_id'] ) || ! Utilities::valid_subsite_id( $input_params['site_id'] ) ) {
return new \WP_Error(
'blc-link-action-invalid-subsite-id',
esc_html__(
'Invalid sub-site id',
'broken-link-checker'
)
);
}
$params['site_id'] = intval( $input_params['site_id'] );
}
return $params;
}
/**
* Sanitize input by scheme (string, url etc).
*
* @param $param
* @param string $scheme
*
* @return array|array[]|mixed|string[]|string[][]
*/
public function sanitize_params( $param = null, string $scheme = null ) {
if ( empty( $param ) ) {
return $param;
}
if ( is_object( $param ) ) {
$param = (array) $param;
}
if ( is_array( $param ) ) {
$param = empty( $param ) ? $param : array_map(
function ( $input ) use ( $scheme ) {
if ( is_array( $input ) ) {
return $this->sanitize_params( $input, $scheme );
}
return $this->sanitize( $input, $scheme );
},
$param
);
} else {
$param = $this->sanitize( $param, $scheme );
}
return $param;
}
/**
* Sets the endpoint's action vars to be used by Dash plugin.
*/
protected function setup_action_vars() {
}
}