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
ProductBrandSchema.php
127 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\StoreApi\Schemas\V1; |
| 5 | |
| 6 | use Automattic\WooCommerce\StoreApi\SchemaController; |
| 7 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 8 | |
| 9 | |
| 10 | /** |
| 11 | * ProductBrandSchema class. |
| 12 | */ |
| 13 | class ProductBrandSchema extends TermSchema { |
| 14 | /** |
| 15 | * The schema item name. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | protected $title = 'product-brand'; |
| 20 | |
| 21 | /** |
| 22 | * The schema item identifier. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | const IDENTIFIER = 'product-brand'; |
| 27 | |
| 28 | /** |
| 29 | * Image attachment schema instance. |
| 30 | * |
| 31 | * @var ImageAttachmentSchema |
| 32 | */ |
| 33 | protected $image_attachment_schema; |
| 34 | |
| 35 | /** |
| 36 | * Constructor. |
| 37 | * |
| 38 | * @param ExtendSchema $extend Rest Extending instance. |
| 39 | * @param SchemaController $controller Schema Controller instance. |
| 40 | */ |
| 41 | public function __construct( ExtendSchema $extend, SchemaController $controller ) { |
| 42 | parent::__construct( $extend, $controller ); |
| 43 | $this->image_attachment_schema = $this->controller->get( ImageAttachmentSchema::IDENTIFIER ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Term properties. |
| 48 | * |
| 49 | * @return array |
| 50 | */ |
| 51 | public function get_properties() { |
| 52 | $schema = parent::get_properties(); |
| 53 | $schema['image'] = [ |
| 54 | 'description' => __( 'Brand image.', 'woocommerce' ), |
| 55 | 'type' => 'object', |
| 56 | 'context' => [ 'view', 'edit', 'embed' ], |
| 57 | 'readonly' => true, |
| 58 | 'properties' => $this->image_attachment_schema->get_properties(), |
| 59 | ]; |
| 60 | $schema['review_count'] = [ |
| 61 | 'description' => __( 'Number of reviews for products of this brand.', 'woocommerce' ), |
| 62 | 'type' => 'integer', |
| 63 | 'context' => [ 'view', 'edit' ], |
| 64 | 'readonly' => true, |
| 65 | ]; |
| 66 | $schema['permalink'] = [ |
| 67 | 'description' => __( 'Brand URL.', 'woocommerce' ), |
| 68 | 'type' => 'string', |
| 69 | 'format' => 'uri', |
| 70 | 'context' => [ 'view', 'edit', 'embed' ], |
| 71 | 'readonly' => true, |
| 72 | ]; |
| 73 | return $schema; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Convert a term object into an object suitable for the response. |
| 78 | * |
| 79 | * @param \WP_Term $term Term object. |
| 80 | * @return array |
| 81 | */ |
| 82 | public function get_item_response( $term ) { |
| 83 | $response = parent::get_item_response( $term ); |
| 84 | $count = get_term_meta( $term->term_id, 'product_count_product_brand', true ); |
| 85 | |
| 86 | if ( $count ) { |
| 87 | $response['count'] = (int) $count; |
| 88 | } |
| 89 | |
| 90 | $response['image'] = $this->image_attachment_schema->get_item_response( get_term_meta( $term->term_id, 'thumbnail_id', true ) ); |
| 91 | $response['review_count'] = $this->get_brand_review_count( $term ); |
| 92 | $response['permalink'] = get_term_link( $term->term_id, 'product_brand' ); |
| 93 | |
| 94 | return $response; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get total number of reviews for products of a brand. |
| 99 | * |
| 100 | * @param \WP_Term $term Term object. |
| 101 | * @return int |
| 102 | */ |
| 103 | protected function get_brand_review_count( $term ) { |
| 104 | global $wpdb; |
| 105 | |
| 106 | $children = get_term_children( $term->term_id, 'product_brand' ); |
| 107 | |
| 108 | if ( ! $children || is_wp_error( $children ) ) { |
| 109 | $terms_to_count_str = absint( $term->term_id ); |
| 110 | } else { |
| 111 | $terms_to_count = array_unique( array_map( 'absint', array_merge( array( $term->term_id ), $children ) ) ); |
| 112 | $terms_to_count_str = implode( ',', $terms_to_count ); |
| 113 | } |
| 114 | |
| 115 | $products_of_brand_sql = " |
| 116 | SELECT SUM(comment_count) as review_count |
| 117 | FROM {$wpdb->posts} AS posts |
| 118 | INNER JOIN {$wpdb->term_relationships} AS term_relationships ON posts.ID = term_relationships.object_id |
| 119 | WHERE term_relationships.term_taxonomy_id IN (" . esc_sql( $terms_to_count_str ) . ') |
| 120 | '; |
| 121 | |
| 122 | $review_count = $wpdb->get_var( $products_of_brand_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 123 | |
| 124 | return (int) $review_count; |
| 125 | } |
| 126 | } |
| 127 |