service = $service; $this->rules = $rules; $this->runner = $runner; } /** * Registers the standard routes and the "run now" route. * * @since 0.1.0 * * @return void */ public function register_routes() { parent::register_routes(); register_rest_route( $this->namespace, '/' . $this->rest_base . '/run', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'run' ), '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_SETTINGS; } /** * Arguments of the list route. * * @since 0.1.0 * * @return array> */ protected function collection_args(): array { return $this->list_params( array( 'id' ), 'id' ); } /** * Lists all rules (there are few), creating the defaults on first use. * * @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 { $this->service->ensure_defaults(); $items = $this->rules->all(); return array( 'items' => $items, 'total' => count( $items ), ); } /** * Runs the reminders now, ignoring quiet hours. * * @since 0.1.0 * * @return WP_REST_Response */ public function run(): WP_REST_Response { return rest_ensure_response( $this->runner->run( true ) ); } /** * Finds one rule. * * @since 0.1.0 * * @param int $id Item id. * @return array|null */ protected function find_item( int $id ): ?array { return $this->rules->find( $id ); } /** * Creates a rule. * * @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 rule. * * @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 rule. * * @since 0.1.0 * * @param int $id Item id. * @return true|WP_Error */ protected function delete_resource( int $id ) { return $this->service->delete( $id ); } /** * Shapes a rule. * * @since 0.1.0 * * @param array $item Item from the repository. * @return array */ protected function present( array $item ): array { return array( 'id' => $item['id'], 'name' => $item['name'], 'rule_type' => $item['rule_type'], 'days_offset' => $item['days_offset'], 'template_key' => $item['template_key'], 'channel' => $item['channel'], 'send_admin_copy' => $item['send_admin_copy'], 'status' => $item['status'], 'sort_order' => $item['sort_order'], 'sent_count' => $this->rules->sent_count( $item['id'] ), ); } /** * Message for a rule that does not exist. * * @since 0.1.0 * * @return string */ protected function not_found_message(): string { return __( 'Reminder rule not found.', 'hostkhata' ); } }