| 1 |
<?php |
| 2 |
/** |
| 3 |
* Controller for rest endpoints. |
| 4 |
* |
| 5 |
* @link https://wpmudev.com/ |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @author WPMUDEV (https://wpmudev.com) |
| 9 |
* @package WPMUDEV\Plugin_Cross_Sell |
| 10 |
* |
| 11 |
* @copyright (c) 2025, WPMU DEV (http://wpmudev.com) |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace WPMUDEV\Modules\Plugin_Cross_Sell; |
| 15 |
|
| 16 |
// Abort if called directly. |
| 17 |
use WP_REST_Controller; |
| 18 |
use WPMUDEV\Modules\Plugin_Cross_Sell\Container; |
| 19 |
|
| 20 |
defined( 'WPINC' ) || die; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class Rest_Api |
| 24 |
* |
| 25 |
* @package WPMUDEV\Plugin_Cross_Sell |
| 26 |
*/ |
| 27 |
abstract class Rest_Api extends WP_REST_Controller { |
| 28 |
/** |
| 29 |
* Holds the request param. |
| 30 |
* |
| 31 |
* @var array|object |
| 32 |
*/ |
| 33 |
protected $request_action; |
| 34 |
|
| 35 |
/** |
| 36 |
* The version. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $version = 'v1'; |
| 41 |
|
| 42 |
/** |
| 43 |
* The namespace. |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
protected $namespace = 'wpmudev_pcs/v1'; |
| 48 |
|
| 49 |
/** |
| 50 |
* API endpoint for the current endpoint. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* |
| 54 |
* @var string $endpoint |
| 55 |
*/ |
| 56 |
protected $endpoint = ''; |
| 57 |
|
| 58 |
/** |
| 59 |
* Dependency container. |
| 60 |
* |
| 61 |
* @since 1.0.0 |
| 62 |
* |
| 63 |
* @var Container |
| 64 |
*/ |
| 65 |
protected $di_container = null; |
| 66 |
|
| 67 |
/** |
| 68 |
* Utilities object. |
| 69 |
* |
| 70 |
* @since 1.0.0 |
| 71 |
* |
| 72 |
* @var Utilities |
| 73 |
*/ |
| 74 |
protected $utilities = null; |
| 75 |
|
| 76 |
/** |
| 77 |
* Prepares the class properties. |
| 78 |
* |
| 79 |
* @param Container $container Dependency container. |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function init( ?Container $container = null ): void { |
| 83 |
$this->di_container = $container; |
| 84 |
$this->utilities = $container->get( 'utilities' ); |
| 85 |
|
| 86 |
if ( ! $this->utilities instanceof Utilities ) { |
| 87 |
$this->utilities = new Utilities(); |
| 88 |
} |
| 89 |
|
| 90 |
// Allow to prepare some params early if required. |
| 91 |
$this->prepare_endpoint_params(); |
| 92 |
|
| 93 |
// If the single instance hasn't been set, set it now. |
| 94 |
$this->register_hooks(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Set up WordPress hooks and filters |
| 99 |
* |
| 100 |
* @since 1.0.0 |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
protected function register_hooks(): void { |
| 105 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Formatting the response |
| 110 |
* |
| 111 |
* @since 1.0.0 |
| 112 |
* |
| 113 |
* @param mixed $item The item to format. |
| 114 |
* @param WP_REST_Request $request request object. |
| 115 |
* |
| 116 |
* @return array|WP_Error|WP_REST_Response |
| 117 |
*/ |
| 118 |
public function prepare_item_for_response( $item, $request ): array { |
| 119 |
$fields = $this->get_fields_for_response( $request ); |
| 120 |
$data = array(); |
| 121 |
|
| 122 |
foreach ( $fields as $field_key ) { |
| 123 |
if ( rest_is_field_included( $field_key, $fields ) ) { |
| 124 |
$data[ $field_key ] = isset( $item[ $field_key ] ) ? $item[ $field_key ] : ''; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $data; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Sets up the proper HTTP status code for authorization. |
| 133 |
* |
| 134 |
* @since 2.0.0 |
| 135 |
* |
| 136 |
* @return int |
| 137 |
*/ |
| 138 |
public function authorization_status_code(): int { |
| 139 |
$status = 401; |
| 140 |
|
| 141 |
if ( is_user_logged_in() ) { |
| 142 |
$status = 403; |
| 143 |
} |
| 144 |
|
| 145 |
return $status; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Check if a given request has access to manage settings. |
| 150 |
* |
| 151 |
* @param \WP_REST_Request $request Request object. |
| 152 |
* @param string $capability Capability to check. |
| 153 |
* |
| 154 |
* @return bool |
| 155 |
* @since 1.0.0 |
| 156 |
*/ |
| 157 |
public function has_permission( ?\WP_REST_Request $request = null, string $capability = 'manage_options' ): bool { |
| 158 |
$capable = current_user_can( $capability ); |
| 159 |
|
| 160 |
/** |
| 161 |
* Filter to modify settings rest capability. |
| 162 |
* |
| 163 |
* @param WP_REST_Request $request Request object. |
| 164 |
* |
| 165 |
* @param bool $capable Is user capable?. |
| 166 |
* |
| 167 |
* @since 1.0.0 |
| 168 |
*/ |
| 169 |
return boolval( apply_filters( 'wpmudev_pluginscrosssell_rest_settings_permission', $capable, $request ) ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get formatted response for the current request. |
| 174 |
* |
| 175 |
* @param array $data Response data. |
| 176 |
* @param bool $success Is request success. |
| 177 |
* |
| 178 |
* @return \WP_REST_Response |
| 179 |
* @since 1.0.0 |
| 180 |
*/ |
| 181 |
public function get_response( $data = array(), $success = true ): \WP_REST_Response { |
| 182 |
// Response status. |
| 183 |
$status = $success ? 200 : 400; |
| 184 |
|
| 185 |
return new \WP_REST_Response( |
| 186 |
array( |
| 187 |
'success' => $success, |
| 188 |
'message' => $data['message'] ?? '', |
| 189 |
'data' => $data, |
| 190 |
), |
| 191 |
$status |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Get the Endpoint's namespace. |
| 197 |
* |
| 198 |
* @return string |
| 199 |
*/ |
| 200 |
public function get_namespace(): string { |
| 201 |
return $this->namespace; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Set the Endpoint's namespace. |
| 206 |
* |
| 207 |
* @param string $api_namespace The namespace. |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
protected function set_namespace( string $api_namespace = '' ): void { |
| 211 |
$this->namespace = $api_namespace; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Get the Endpoint's endpoint part |
| 216 |
* |
| 217 |
* @return string |
| 218 |
*/ |
| 219 |
public function get_endpoint(): string { |
| 220 |
return $this->endpoint; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Gives the full url of the Rest endpoint (with site url). |
| 225 |
* |
| 226 |
* @return string |
| 227 |
*/ |
| 228 |
public function get_endpoint_url(): string { |
| 229 |
return trailingslashit( rest_url() ) . trailingslashit( $this->get_namespace() ) . $this->get_endpoint(); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Gives the path of the Rest endpoint without site url |
| 234 |
* |
| 235 |
* @return string |
| 236 |
*/ |
| 237 |
public function get_endpoint_path(): string { |
| 238 |
return trailingslashit( $this->get_namespace() ) . $this->get_endpoint(); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Register the routes for the objects of the controller. This should be defined in extending class. |
| 243 |
* |
| 244 |
* @since 1.0.0 |
| 245 |
* |
| 246 |
* @return void |
| 247 |
*/ |
| 248 |
public function register_routes(): void { |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* A helper function called early to allow prepare some variables |
| 253 |
* |
| 254 |
* @since 1.0.0 |
| 255 |
* |
| 256 |
* @return void |
| 257 |
*/ |
| 258 |
public function prepare_endpoint_params(): void { |
| 259 |
} |
| 260 |
} |
| 261 |
|