service = $service; $this->domains = $domains; $this->import = $import; } /** * Registers the standard routes and the import route. * * @since 0.1.0 * * @return void */ public function register_routes() { parent::register_routes(); register_rest_route( $this->namespace, '/' . $this->rest_base . '/import', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'import' ), 'permission_callback' => Permissions::require_capability( $this->capability() ), ) ); } /** * Capability that protects every route. * * @since 0.1.0 * * @return string */ protected function capability(): string { return Capabilities::MANAGE_ORDERS; } /** * Arguments of the list route. * * @since 0.1.0 * * @return array> */ protected function collection_args(): array { return array_merge( $this->list_params( array_keys( DomainRepository::ORDERBY ), 'id' ), array( 'status' => array( 'type' => 'string', 'default' => '', 'enum' => array_merge( array( '' ), DomainStatus::all() ), 'validate_callback' => 'rest_validate_request_arg', ), 'customer_id' => array( 'type' => 'integer', 'default' => 0, 'sanitize_callback' => 'absint', ), 'expiring_within' => array( 'type' => 'integer', 'default' => 0, 'minimum' => 0, 'maximum' => 3650, 'sanitize_callback' => 'absint', ), ) ); } /** * Lists domains. `expiring_within` limits the list to domains that expire in that many days. * * @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 { $days = (int) $request['expiring_within']; return $this->domains->query( array( 'search' => $request['search'], 'status' => $request['status'], 'customer_id' => $request['customer_id'], 'expiring_before' => $days > 0 ? Date::add_days( Date::today_gmt(), $days ) : '', 'orderby' => $request['orderby'], 'order' => $request['order'], 'page' => $request['page'], 'per_page' => $request['per_page'], ) ); } /** * Lists domains and reports the number per status in a header. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function get_items( $request ) { $response = parent::get_items( $request ); $response->header( 'X-HostKhata-Status-Counts', wp_json_encode( $this->domains->status_counts() ) ); return $response; } /** * Finds one domain. * * @since 0.1.0 * * @param int $id Item id. * @return array|null */ protected function find_item( int $id ): ?array { return $this->domains->find( $id ); } /** * Creates a domain record. * * @since 0.1.0 * * @param array $body Request body. * @return array|WP_Error */ protected function create_resource( array $body ) { return $this->service->create( $body ); } /** * Updates a domain record. * * @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 ); } /** * Deletes a domain record. * * @since 0.1.0 * * @param int $id Item id. * @return true|WP_Error */ protected function delete_resource( int $id ) { return $this->service->delete( $id ); } /** * Message for a record that does not exist. * * @since 0.1.0 * * @return string */ protected function not_found_message(): string { return __( 'Domain not found.', 'hostkhata' ); } /** * Imports domain records from rows. * * @since 0.1.0 * * @param WP_REST_Request $request Request: `rows` and `dry_run`. * @return WP_REST_Response|WP_Error */ public function import( $request ) { $body = $this->request_body( $request ); $result = $this->import->import_domains( isset( $body['rows'] ) && is_array( $body['rows'] ) ? $body['rows'] : array(), ! empty( $body['dry_run'] ) ); return is_wp_error( $result ) ? $result : rest_ensure_response( $result ); } /** * Shapes a domain record for the response. * * @since 0.1.0 * * @param array $item Item from the repository. * @return array */ protected function present( array $item ): array { return array( 'id' => $item['id'], 'customer_id' => $item['customer_id'], 'customer_name' => $item['customer_name'], 'customer_email' => $item['customer_email'], 'order_id' => $item['order_id'], 'service_id' => $item['service_id'], 'domain_name' => $item['domain_name'], 'tld' => $item['tld'], 'registrar' => $item['registrar'], 'status' => $item['status'], 'registered_on' => $item['registered_on'], 'expires_on' => $item['expires_on'], 'renewal_years' => $item['renewal_years'], 'auto_renew' => $item['auto_renew'], 'whois_privacy' => $item['whois_privacy'], 'nameservers' => $item['nameservers'], 'notes' => $item['notes'], ); } }