class-rest-api.php
2.05 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
<?php
/**
* Controller for rest endpoints.
*
* @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;
// Abort if called directly.
use WP_REST_Controller;
defined( 'WPINC' ) || die;
/**
* Class Admin_Page
*
* @package WPMUDEV_BLC\Core\Controllers
*/
abstract class Rest_Api extends WP_REST_Controller {
/**
* Holds the request param.
*
* @var array|object
*/
protected $request_action;
/**
* The version.
*
* @var string
*/
protected $version = 'v1';
/**
* The namespace.
*
* @var string
*/
protected $namespace;
/**
* Rest base for the current object.
*
* @var string
*/
protected $rest_base;
/**
* Constructor.
*/
private function __construct() {
}
/**
* Instance obtaining method.
*
* @since 2.0.0
*
* @return static Called class instance.
*/
public static function instance() {
static $instances = array();
$called_class_name = get_called_class();
if ( ! isset( $instances[ $called_class_name ] ) ) {
$instances[ $called_class_name ] = new $called_class_name();
}
return $instances[ $called_class_name ];
}
/**
* Formatting the response
*
* @since 2.0.0
*
* @param WP_REST_Request $request request object.
* @param array $item
*
* @return array|WP_Error|WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$fields = $this->get_fields_for_response( $request );
$data = array();
foreach ( $fields as $field_key ) {
if ( rest_is_field_included( $field_key, $fields ) ) {
$data[ $field_key ] = isset( $item[ $field_key ] ) ? $item[ $field_key ] : '';
}
}
return $data;
}
/**
* Sets up the proper HTTP status code for authorization.
*
* @since 2.0.0
*
* @return int
*/
public function authorization_status_code() {
$status = 401;
if ( is_user_logged_in() ) {
$status = 403;
}
return $status;
}
}