AI
1 year ago
Agentic
7 months ago
AbstractAddressSchema.php
1 year ago
AbstractSchema.php
1 month ago
BatchSchema.php
2 years ago
BillingAddressSchema.php
2 years ago
CartCouponSchema.php
1 year ago
CartExtensionsSchema.php
1 year ago
CartFeeSchema.php
2 years ago
CartItemSchema.php
1 month ago
CartSchema.php
2 months ago
CartShippingRateSchema.php
1 month ago
CheckoutOrderSchema.php
2 years ago
CheckoutSchema.php
1 month ago
ErrorSchema.php
2 years ago
ImageAttachmentSchema.php
3 months ago
ItemSchema.php
11 months ago
OrderCouponSchema.php
2 years ago
OrderFeeSchema.php
2 years ago
OrderItemSchema.php
1 month ago
OrderSchema.php
2 months ago
PatternsSchema.php
1 year ago
ProductAttributeSchema.php
2 years ago
ProductAttributeTermSchema.php
1 month ago
ProductBrandSchema.php
1 year ago
ProductCategorySchema.php
2 years ago
ProductCollectionDataSchema.php
11 months ago
ProductReviewSchema.php
2 years ago
ProductSchema.php
1 month ago
ShippingAddressSchema.php
2 years ago
ShopperListItemSchema.php
1 month ago
ShopperListSchema.php
1 month ago
TermSchema.php
2 years ago
ProductReviewSchema.php
180 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Schemas\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 5 | use Automattic\WooCommerce\StoreApi\SchemaController; |
| 6 | |
| 7 | /** |
| 8 | * ProductReviewSchema class. |
| 9 | */ |
| 10 | class ProductReviewSchema extends AbstractSchema { |
| 11 | /** |
| 12 | * The schema item name. |
| 13 | * |
| 14 | * @var string |
| 15 | */ |
| 16 | protected $title = 'product_review'; |
| 17 | |
| 18 | /** |
| 19 | * The schema item identifier. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | const IDENTIFIER = 'product-review'; |
| 24 | |
| 25 | /** |
| 26 | * Image attachment schema instance. |
| 27 | * |
| 28 | * @var ImageAttachmentSchema |
| 29 | */ |
| 30 | protected $image_attachment_schema; |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | * |
| 35 | * @param ExtendSchema $extend Rest Extending instance. |
| 36 | * @param SchemaController $controller Schema Controller instance. |
| 37 | */ |
| 38 | public function __construct( ExtendSchema $extend, SchemaController $controller ) { |
| 39 | parent::__construct( $extend, $controller ); |
| 40 | $this->image_attachment_schema = $this->controller->get( ImageAttachmentSchema::IDENTIFIER ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Product review schema properties. |
| 45 | * |
| 46 | * @return array |
| 47 | */ |
| 48 | public function get_properties() { |
| 49 | $properties = [ |
| 50 | 'id' => [ |
| 51 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
| 52 | 'type' => 'integer', |
| 53 | 'context' => [ 'view', 'edit' ], |
| 54 | 'readonly' => true, |
| 55 | ], |
| 56 | 'date_created' => [ |
| 57 | 'description' => __( "The date the review was created, in the site's timezone.", 'woocommerce' ), |
| 58 | 'type' => 'string', |
| 59 | 'format' => 'date-time', |
| 60 | 'context' => [ 'view', 'edit' ], |
| 61 | 'readonly' => true, |
| 62 | ], |
| 63 | 'formatted_date_created' => [ |
| 64 | 'description' => __( "The date the review was created, in the site's timezone in human-readable format.", 'woocommerce' ), |
| 65 | 'type' => 'string', |
| 66 | 'context' => [ 'view', 'edit' ], |
| 67 | 'readonly' => true, |
| 68 | ], |
| 69 | 'date_created_gmt' => [ |
| 70 | 'description' => __( 'The date the review was created, as GMT.', 'woocommerce' ), |
| 71 | 'type' => 'string', |
| 72 | 'format' => 'date-time', |
| 73 | 'context' => [ 'view', 'edit' ], |
| 74 | 'readonly' => true, |
| 75 | ], |
| 76 | 'product_id' => [ |
| 77 | 'description' => __( 'Unique identifier for the product that the review belongs to.', 'woocommerce' ), |
| 78 | 'type' => 'integer', |
| 79 | 'context' => [ 'view', 'edit' ], |
| 80 | 'readonly' => true, |
| 81 | ], |
| 82 | 'product_name' => [ |
| 83 | 'description' => __( 'Name of the product that the review belongs to.', 'woocommerce' ), |
| 84 | 'type' => 'string', |
| 85 | 'context' => [ 'view', 'edit' ], |
| 86 | 'readonly' => true, |
| 87 | ], |
| 88 | 'product_permalink' => [ |
| 89 | 'description' => __( 'Permalink of the product that the review belongs to.', 'woocommerce' ), |
| 90 | 'type' => 'string', |
| 91 | 'context' => [ 'view', 'edit' ], |
| 92 | 'readonly' => true, |
| 93 | ], |
| 94 | 'product_image' => [ |
| 95 | 'description' => __( 'Image of the product that the review belongs to.', 'woocommerce' ), |
| 96 | 'type' => 'object', |
| 97 | 'context' => [ 'view', 'edit' ], |
| 98 | 'readonly' => true, |
| 99 | 'properties' => $this->image_attachment_schema->get_properties(), |
| 100 | ], |
| 101 | 'reviewer' => [ |
| 102 | 'description' => __( 'Reviewer name.', 'woocommerce' ), |
| 103 | 'type' => 'string', |
| 104 | 'context' => [ 'view', 'edit' ], |
| 105 | 'readonly' => true, |
| 106 | ], |
| 107 | 'review' => [ |
| 108 | 'description' => __( 'The content of the review.', 'woocommerce' ), |
| 109 | 'type' => 'string', |
| 110 | 'context' => [ 'view', 'edit' ], |
| 111 | 'arg_options' => [ |
| 112 | 'sanitize_callback' => 'wp_filter_post_kses', |
| 113 | ], |
| 114 | 'readonly' => true, |
| 115 | ], |
| 116 | 'rating' => [ |
| 117 | 'description' => __( 'Review rating (0 to 5).', 'woocommerce' ), |
| 118 | 'type' => 'integer', |
| 119 | 'context' => [ 'view', 'edit' ], |
| 120 | 'readonly' => true, |
| 121 | ], |
| 122 | 'verified' => [ |
| 123 | 'description' => __( 'Shows if the reviewer bought the product or not.', 'woocommerce' ), |
| 124 | 'type' => 'boolean', |
| 125 | 'context' => [ 'view', 'edit' ], |
| 126 | 'readonly' => true, |
| 127 | ], |
| 128 | ]; |
| 129 | |
| 130 | if ( get_option( 'show_avatars' ) ) { |
| 131 | $avatar_properties = array(); |
| 132 | $avatar_sizes = rest_get_avatar_sizes(); |
| 133 | |
| 134 | foreach ( $avatar_sizes as $size ) { |
| 135 | $avatar_properties[ $size ] = array( |
| 136 | /* translators: %d: avatar image size in pixels */ |
| 137 | 'description' => sprintf( __( 'Avatar URL with image size of %d pixels.', 'woocommerce' ), $size ), |
| 138 | 'type' => 'string', |
| 139 | 'format' => 'uri', |
| 140 | 'context' => array( 'embed', 'view', 'edit' ), |
| 141 | ); |
| 142 | } |
| 143 | $properties['reviewer_avatar_urls'] = array( |
| 144 | 'description' => __( 'Avatar URLs for the object reviewer.', 'woocommerce' ), |
| 145 | 'type' => 'object', |
| 146 | 'context' => array( 'view', 'edit' ), |
| 147 | 'readonly' => true, |
| 148 | 'properties' => $avatar_properties, |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | return $properties; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Convert a WooCommerce product into an object suitable for the response. |
| 157 | * |
| 158 | * @param \WP_Comment $review Product review object. |
| 159 | * @return array |
| 160 | */ |
| 161 | public function get_item_response( $review ) { |
| 162 | $rating = get_comment_meta( $review->comment_ID, 'rating', true ) === '' ? null : (int) get_comment_meta( $review->comment_ID, 'rating', true ); |
| 163 | return [ |
| 164 | 'id' => (int) $review->comment_ID, |
| 165 | 'date_created' => wc_rest_prepare_date_response( $review->comment_date ), |
| 166 | 'formatted_date_created' => get_comment_date( 'F j, Y', $review->comment_ID ), |
| 167 | 'date_created_gmt' => wc_rest_prepare_date_response( $review->comment_date_gmt ), |
| 168 | 'product_id' => (int) $review->comment_post_ID, |
| 169 | 'product_name' => get_the_title( (int) $review->comment_post_ID ), |
| 170 | 'product_permalink' => get_permalink( (int) $review->comment_post_ID ), |
| 171 | 'product_image' => $this->image_attachment_schema->get_item_response( get_post_thumbnail_id( (int) $review->comment_post_ID ) ), |
| 172 | 'reviewer' => $review->comment_author, |
| 173 | 'review' => wpautop( $review->comment_content ), |
| 174 | 'rating' => $rating, |
| 175 | 'verified' => wc_review_is_from_verified_owner( $review->comment_ID ), |
| 176 | 'reviewer_avatar_urls' => rest_get_avatar_urls( $review->comment_author_email ), |
| 177 | ]; |
| 178 | } |
| 179 | } |
| 180 |