class-hub-endpoint.php
3.36 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
<?php
/**
* Hub endpoints controller.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\Core\Controllers
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\Core\Controllers;
// If this file is called directly, abort.
defined( 'WPINC' ) || die;
use WPMUDEV_BLC\Core\Utils\Abstracts\Base;
use WPMUDEV_BLC\Core\Utils\Utilities;
use WPMUDEV_BLC\Core\Traits\Sanitize;
/**
* Class Hub_Endpoint
*
* @package WPMUDEV_BLC\Core\Controllers
*/
abstract class Hub_Endpoint extends Base {
use Sanitize;
/**
* The name of the endpoint action.
*
* @var string
*/
protected $endpoint_action_name = '';
/**
* The name of the endpoint action callback method.
*
* @var string
*/
protected $endpoint_action_callback = '';
/**
* Init class
*
* @since 2.0.0
*
* @return void
*/
public function init() {
add_filter( 'wdp_register_hub_action', array( $this, 'register_endpoints' ) );
}
/**
* Registers all endpoints for using Dash plugin's wdp_register_hub_action filter.
*
* @param array $actions An array of actions passed from wdp_register_hub_action filter.
*
* @return array
*/
public function register_endpoints( array $actions = null ) {
if ( ! is_array( $actions ) ) {
$actions = array();
}
$endpoint_actions = \is_callable( array( $this, 'get_endpoint_actions' ) ) ? $this->get_endpoint_actions() : array();
if ( ! empty( $endpoint_actions ) ) {
$actions = wp_parse_args( $endpoint_actions, $actions );
}
return apply_filters( 'wpmudev_blc_hub_endpoints_actions', $actions, $this );
}
/**
* Formats the input into json response.
* When $success = false the input array needs to contain following keys:
* `code` with value string and default value ''
* `message` with value string and default value a generic error response message and
* `data` with mixed value and default value ''
*
* @param array $input An array that will be transformed to json. In case of error input array needs tocontain `code`, `message` and `data` indexes.
* @param bool $success When true returns wp_send_json_success when false returns wp_send_json_error.
*
* @return void
*/
protected function output_formatted_response( array $input = array(), bool $success = true ) {
if ( ! empty( $input ) ) {
if ( $success ) {
\wp_send_json_success( $input, 200 );
} else {
$input['code'] = $input['code'] ? sanitize_text_field( $input['code'] ) : 'BLC_ERROR';
$input['message'] = $input['message'] ? \wp_kses_post( $input['message'] ) : \esc_html__( 'Something went wrong with this HTTP request', 'broken-link-checker' );
$input['data'] = $input['data'] ? $this->sanitize_array( $input['data'] ) : '';
\wp_send_json_error( new \WP_Error( $input['code'], $input['message'], $input['data'] ) );
}
}
}
/**
* Returns the endpoint's actions to be used by Dash plugin.
*
* @return array.
*/
protected function get_endpoint_actions() {
$this->setup_action_vars();
if ( ! empty( $this->endpoint_action_name ) && ! empty( $this->endpoint_action_callback ) ) {
return array(
$this->endpoint_action_name => array( $this, $this->endpoint_action_callback ),
);
}
return array();
}
/**
* Sets up the action variables.
*/
abstract protected function setup_action_vars();
}