= (int) $answer; } /** * What the results screen needs to show the request, or null when it shouldn't. * * @return array{url:string}|null */ public static function config() { return self::should_ask() ? array( 'url' => self::URL ) : null; } /** * Saves the current user's answer. * * @param string $choice One of self::CHOICES. * @return bool Whether it was a valid answer. */ public static function answer( $choice ) { $user = get_current_user_id(); if ( ! $user || ! in_array( $choice, self::CHOICES, true ) ) { return false; } update_user_meta( $user, self::META, 'later' === $choice ? (string) ( time() + self::SNOOZE ) : $choice ); return true; } /** * POST /review: saves the answer from the results screen. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public static function rest_answer( \WP_REST_Request $request ) { if ( ! self::answer( (string) $request['choice'] ) ) { return new \WP_Error( 'airworthy_bad_choice', __( 'Unknown answer.', 'airworthy' ), array( 'status' => 400 ) ); } return new \WP_REST_Response( array( 'saved' => true ), 200 ); } /** * Removes every user's answer (on uninstall). */ public static function forget_all() { delete_metadata( 'user', 0, self::META, '', true ); } }