woocommerce/includes/abstracts/abstract-wc-rest-posts-cont...

112 lines
2.6 KiB
PHP
Raw Normal View History

<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Abstract Rest Posts Controler Class
*
* @author WooThemes
* @category API
* @package WooCommerce/Abstracts
* @version 2.6.0
*/
abstract class WC_REST_Posts_Controller extends WP_REST_Controller {
/**
* Route base.
*
* @var string
*/
protected $rest_base = '';
/**
* Post type.
*
* @var string
*/
protected $post_type = '';
/**
* Controls visibility on frontend.
*
* @var string
*/
protected $public = false;
/**
2016-02-22 20:48:38 +00:00
* Check if a given request has access to read a item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
$post = get_post( (int) $request['id'] );
if ( $post ) {
2016-02-22 20:48:38 +00:00
$post_type = get_post_type_object( $this->post_type );
return 'revision' !== $post->post_type && current_user_can( $post_type->cap->read_private_posts, $post->ID );
}
return true;
}
/**
2016-02-22 20:48:38 +00:00
* Check if a given request has access to read items.
*
2016-02-22 20:48:38 +00:00
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
2016-02-22 20:48:38 +00:00
public function get_items_permissions_check( $request ) {
$post_type = get_post_type_object( $this->post_type );
2016-02-22 20:48:38 +00:00
return current_user_can( $post_type->cap->read_private_posts );
}
/**
2016-02-22 20:48:38 +00:00
* Get a single item.
*
2016-02-22 20:48:38 +00:00
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
2016-02-22 20:48:38 +00:00
public function get_item( $request ) {
$id = (int) $request['id'];
$post = get_post( $id );
2016-02-22 20:48:38 +00:00
if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
return new WP_Error( sprintf( 'woocommerce_rest_api_invalid_%s_id', $this->post_type ), __( 'Invalid id.', 'woocommerce' ), array( 'status' => 404 ) );
}
2016-02-22 20:48:38 +00:00
$data = $this->prepare_item_for_response( $post, $request );
$response = rest_ensure_response( $data );
2016-02-22 20:48:38 +00:00
if ( $this->public ) {
$response->link_header( 'alternate', get_permalink( $id ), array( 'type' => 'text/html' ) );
}
2016-02-22 20:48:38 +00:00
return $response;
}
/**
* Check the post_date_gmt or modified_gmt and prepare any post or
* modified date for single post output.
*
* @param string $date_gmt
* @param string|null $date
* @return string|null ISO8601/RFC3339 formatted datetime.
*/
protected function prepare_date_response( $date_gmt, $date = null ) {
// Use the date if passed.
if ( isset( $date ) ) {
return mysql_to_rfc3339( $date );
}
// Return null if $date_gmt is empty/zeros.
if ( '0000-00-00 00:00:00' === $date_gmt ) {
return null;
}
// Return the formatted datetime.
return mysql_to_rfc3339( $date_gmt );
}
}