service = $service; $this->coupons = $coupons; $this->settings = $settings; } /** * {@inheritdoc} */ protected function capability(): string { return Capabilities::MANAGE_ORDERS; } /** * {@inheritdoc} */ protected function collection_args(): array { return array_merge( $this->list_params( array_keys( CouponRepository::ORDERBY ), 'id' ), array( 'status' => array( 'type' => 'string', 'default' => '', 'enum' => array_merge( array( '' ), CouponInput::STATUSES ), 'validate_callback' => 'rest_validate_request_arg', ), ) ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return array{items: array>, total: int} */ protected function query_items( WP_REST_Request $request ): array { return $this->coupons->query( array( 'search' => $request['search'], 'status' => $request['status'], 'orderby' => $request['orderby'], 'order' => $request['order'], 'page' => $request['page'], 'per_page' => $request['per_page'], ) ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param int $id Item id. * @return array|null */ protected function find_item( int $id ): ?array { return $this->coupons->find( $id ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param array $body Request body. * @return array|\WP_Error */ protected function create_resource( array $body ) { return $this->service->create( $body ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param int $id Item id. * @param array $body Request body. * @return array|\WP_Error */ protected function update_resource( int $id, array $body ) { return $this->service->update( $id, $body ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param int $id Item id. * @return true|\WP_Error */ protected function delete_resource( int $id ) { return $this->service->delete( $id ); } /** * {@inheritdoc} */ protected function not_found_message(): string { return __( 'Coupon not found.', 'hostkhata' ); } /** * Implements the abstract method of the resource controller. * * @since 0.1.0 * * @param array $item Item from the repository. * @return array */ protected function present( array $item ): array { $today = Date::today_gmt(); if ( 'disabled' === $item['status'] ) { $state = 'disabled'; } elseif ( '' !== $item['expires_on'] && $today > $item['expires_on'] ) { $state = 'expired'; } elseif ( '' !== $item['starts_on'] && $today < $item['starts_on'] ) { $state = 'scheduled'; } elseif ( $item['usage_limit'] > 0 && $item['used_count'] >= $item['usage_limit'] ) { $state = 'used_up'; } else { $state = 'live'; } return array( 'id' => $item['id'], 'code' => $item['code'], 'type' => $item['type'], 'value' => $item['value'], 'value_formatted' => 'percent' === $item['type'] ? rtrim( rtrim( number_format( $item['value'] / 100, 2, '.', '' ), '0' ), '.' ) . '%' : $this->settings->format_money( $item['value'] ), 'starts_on' => $item['starts_on'], 'expires_on' => $item['expires_on'], 'usage_limit' => $item['usage_limit'], 'per_customer_limit' => $item['per_customer_limit'], 'used_count' => $item['used_count'], 'applies_to' => $item['applies_to'], 'status' => $item['status'], 'state' => $state, ); } }