93 lines
1.8 KiB
PHP
93 lines
1.8 KiB
PHP
<?php
|
|
|
|
|
|
/**
|
|
* Generates REST API notice response
|
|
*
|
|
* @param string $message
|
|
* @param string $code
|
|
* @param array|null $data
|
|
*
|
|
* @return WP_REST_Response
|
|
*/
|
|
function wiaas_api_notice($message, $code, $data = null) {
|
|
return rest_ensure_response(array(
|
|
'messages' => [
|
|
array(
|
|
'code' => $code,
|
|
'message' => $message
|
|
)
|
|
],
|
|
'data' => $data
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Generates REST API notice responses with wc error notices
|
|
*
|
|
* @param array|null $data
|
|
*
|
|
* @return WP_REST_Response
|
|
*/
|
|
function wiaas_api_cart_error_notices($data = null) {
|
|
return wiaas_api_cart_notices('error', $data);
|
|
}
|
|
|
|
/**
|
|
* Generates REST API notice responses with wc warning notices
|
|
*
|
|
* @param array|null $data
|
|
*
|
|
* @return WP_REST_Response
|
|
*/
|
|
function wiaas_api_cart_warning_notices($data = null) {
|
|
return wiaas_api_cart_notices('notice', $data);
|
|
}
|
|
|
|
/**
|
|
* Generates REST API notice responses with wc notices
|
|
*
|
|
* @param string $types Notice types (error, success)
|
|
* @param array|null $data
|
|
*
|
|
* @return WP_REST_Response
|
|
*/
|
|
function wiaas_api_cart_notices($types, $data = null) {
|
|
|
|
$types = is_array($types) ? $types : array( $types );
|
|
|
|
$messages = array();
|
|
foreach ($types as $type) {
|
|
$messages = array_merge($messages, wc_get_notices($type));
|
|
}
|
|
wc_clear_notices();
|
|
|
|
$messages = array_map(function($message) {
|
|
return array(
|
|
'code' => 'error',
|
|
'message' => $message
|
|
);
|
|
}, $messages);
|
|
|
|
return rest_ensure_response(array(
|
|
'messages' => $messages,
|
|
'data' => $data
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Generate REST API error
|
|
*
|
|
* @param string $message
|
|
* @param int $code
|
|
*
|
|
* @return WP_REST_Response
|
|
*/
|
|
function wiaas_api_generate_error($message, $code = 500) {
|
|
$response = rest_ensure_response(array(
|
|
'status' => $code,
|
|
'message' => $message,
|
|
));
|
|
$response->set_status($code);
|
|
return $response;
|
|
} |