| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — Games REST routes. | |
| 3 | + * OpenStation — Games REST routes. | |
| 4 | 4 | * |
| 5 | 5 | * Namespace `desktop-mode/v1`: |
| 6 | 6 | * |
| 7 | 7 | * GET /games/(?P<game>[a-z0-9_\-]+)/scores Leaderboard (paged). |
| @@ -18,12 +18,11 @@ | ||
| 18 | 18 | * Permission: every route requires a logged-in user with desktop |
| 19 | 19 | * mode enabled and the `read` capability (subscribers play games |
| 20 | 20 | * too). Unknown game ids 404. Challenge routes additionally verify |
| 21 | 21 | * party membership; sending gates through the |
| 22 | - * `desktop_mode_games_can_challenge` filter. | |
| 22 | + * `openstation_games_can_challenge` filter. | |
| 23 | 23 | * |
| 24 | - * @package WPDesktopMode | |
| 25 | - * @since 0.9.6 | |
| 24 | + * @package OpenStation | |
| 26 | 25 | */ |
| 27 | 26 | |
| 28 | 27 | defined( 'ABSPATH' ) || exit; |
| 29 | 28 | |
| @@ -28,20 +27,18 @@ | ||
| 28 | 27 | defined( 'ABSPATH' ) || exit; |
| 29 | 28 | |
| 30 | 29 | /** |
| 31 | 30 | * Base permission gate shared by every games route. |
| 32 | - * | |
| 33 | - * @since 0.9.6 | |
| 34 | 31 | */ |
| 35 | -function desktop_mode_games_rest_permission() { | |
| 32 | +function openstation_games_rest_permission() { | |
| 36 | 33 | if ( ! is_user_logged_in() ) { |
| 37 | - return new WP_Error( 'desktop_mode_games_unauthenticated', __( 'You must be logged in.', 'desktop-mode' ), array( 'status' => 401 ) ); | |
| 34 | + return new WP_Error( 'openstation_games_unauthenticated', __( 'You must be logged in.', 'desktop-mode' ), array( 'status' => 401 ) ); | |
| 38 | 35 | } |
| 39 | - if ( function_exists( 'desktop_mode_is_enabled' ) && ! desktop_mode_is_enabled( get_current_user_id() ) ) { | |
| 40 | - return new WP_Error( 'desktop_mode_games_disabled', __( 'Desktop mode is not enabled for this user.', 'desktop-mode' ), array( 'status' => 403 ) ); | |
| 36 | + if ( function_exists( 'openstation_is_enabled' ) && ! openstation_is_enabled( get_current_user_id() ) ) { | |
| 37 | + return new WP_Error( 'openstation_games_disabled', __( 'OpenStation is not enabled for this user.', 'desktop-mode' ), array( 'status' => 403 ) ); | |
| 41 | 38 | } |
| 42 | 39 | if ( ! current_user_can( 'read' ) ) { |
| 43 | - return new WP_Error( 'desktop_mode_games_forbidden', __( 'You cannot use desktop games.', 'desktop-mode' ), array( 'status' => 403 ) ); | |
| 40 | + return new WP_Error( 'openstation_games_forbidden', __( 'You cannot use desktop games.', 'desktop-mode' ), array( 'status' => 403 ) ); | |
| 44 | 41 | } |
| 45 | 42 | |
| 46 | 43 | /** |
| 47 | 44 | * Filters the base games REST permission verdict. Return a |
| @@ -47,19 +44,17 @@ | ||
| 47 | 44 | * Filters the base games REST permission verdict. Return a |
| 48 | 45 | * `WP_Error` (or `false`) to lock the whole surface down below |
| 49 | 46 | * the default logged-in + `read` gate. |
| 50 | 47 | * |
| 51 | - * @since 0.9.6 | |
| 52 | - * | |
| 53 | 48 | * @param true|false|WP_Error $allowed Default `true`. |
| 54 | 49 | * @param int $user_id Current user. |
| 55 | 50 | */ |
| 56 | - $allowed = apply_filters( 'desktop_mode_games_rest_permission', true, get_current_user_id() ); | |
| 51 | + $allowed = apply_filters( 'openstation_games_rest_permission', true, get_current_user_id() ); | |
| 57 | 52 | if ( is_wp_error( $allowed ) ) { |
| 58 | 53 | return $allowed; |
| 59 | 54 | } |
| 60 | 55 | if ( true !== $allowed ) { |
| 61 | - return new WP_Error( 'desktop_mode_games_forbidden', __( 'You cannot use desktop games.', 'desktop-mode' ), array( 'status' => 403 ) ); | |
| 56 | + return new WP_Error( 'openstation_games_forbidden', __( 'You cannot use desktop games.', 'desktop-mode' ), array( 'status' => 403 ) ); | |
| 62 | 57 | } |
| 63 | 58 | return true; |
| 64 | 59 | } |
| 65 | 60 | |
| @@ -64,125 +59,219 @@ | ||
| 64 | 59 | } |
| 65 | 60 | |
| 66 | 61 | /** |
| 67 | 62 | * Register the routes. |
| 68 | - * | |
| 69 | - * @since 0.9.6 | |
| 70 | 63 | */ |
| 71 | -function desktop_mode_games_register_rest_routes() { | |
| 64 | +function openstation_games_register_rest_routes() { | |
| 72 | 65 | $ns = 'desktop-mode/v1'; |
| 73 | 66 | |
| 74 | - register_rest_route( $ns, '/games/(?P<game>[a-z0-9_\-]+)/scores', array( | |
| 67 | + register_rest_route( | |
| 68 | + $ns, | |
| 69 | + '/games/(?P<game>[a-z0-9_\-]+)/scores', | |
| 75 | 70 | array( |
| 76 | - 'methods' => WP_REST_Server::READABLE, | |
| 77 | - 'permission_callback' => 'desktop_mode_games_rest_permission', | |
| 78 | - 'callback' => 'desktop_mode_games_rest_list_scores', | |
| 79 | - 'args' => array( | |
| 80 | - 'page' => array( 'type' => 'integer', 'default' => 1, 'minimum' => 1 ), | |
| 81 | - 'per_page' => array( 'type' => 'integer', 'default' => 25, 'minimum' => 1, 'maximum' => 100 ), | |
| 82 | - 'orderby' => array( 'type' => 'string', 'default' => 'score', 'enum' => array( 'score', 'created' ) ), | |
| 83 | - 'order' => array( 'type' => 'string', 'default' => 'desc', 'enum' => array( 'asc', 'desc' ) ), | |
| 84 | - 'user_id' => array( 'type' => 'integer', 'default' => 0 ), | |
| 71 | + array( | |
| 72 | + 'methods' => WP_REST_Server::READABLE, | |
| 73 | + 'permission_callback' => 'openstation_games_rest_permission', | |
| 74 | + 'callback' => 'openstation_games_rest_list_scores', | |
| 75 | + 'args' => array( | |
| 76 | + 'page' => array( | |
| 77 | + 'type' => 'integer', | |
| 78 | + 'default' => 1, | |
| 79 | + 'minimum' => 1, | |
| 80 | + ), | |
| 81 | + 'per_page' => array( | |
| 82 | + 'type' => 'integer', | |
| 83 | + 'default' => 25, | |
| 84 | + 'minimum' => 1, | |
| 85 | + 'maximum' => 100, | |
| 86 | + ), | |
| 87 | + 'orderby' => array( | |
| 88 | + 'type' => 'string', | |
| 89 | + 'default' => 'score', | |
| 90 | + 'enum' => array( 'score', 'created' ), | |
| 91 | + ), | |
| 92 | + 'order' => array( | |
| 93 | + 'type' => 'string', | |
| 94 | + 'default' => 'desc', | |
| 95 | + 'enum' => array( 'asc', 'desc' ), | |
| 96 | + ), | |
| 97 | + 'user_id' => array( | |
| 98 | + 'type' => 'integer', | |
| 99 | + 'default' => 0, | |
| 100 | + ), | |
| 101 | + ), | |
| 85 | 102 | ), |
| 86 | - ), | |
| 103 | + array( | |
| 104 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 105 | + 'permission_callback' => 'openstation_games_rest_permission', | |
| 106 | + 'callback' => 'openstation_games_rest_submit_score', | |
| 107 | + 'args' => array( | |
| 108 | + 'score' => array( | |
| 109 | + 'type' => 'integer', | |
| 110 | + 'required' => true, | |
| 111 | + 'minimum' => 0, | |
| 112 | + ), | |
| 113 | + 'meta' => array( | |
| 114 | + 'type' => 'object', | |
| 115 | + 'default' => array(), | |
| 116 | + ), | |
| 117 | + ), | |
| 118 | + ), | |
| 119 | + ) | |
| 120 | + ); | |
| 121 | + | |
| 122 | + register_rest_route( | |
| 123 | + $ns, | |
| 124 | + '/games/challenges', | |
| 87 | 125 | array( |
| 126 | + array( | |
| 127 | + 'methods' => WP_REST_Server::READABLE, | |
| 128 | + 'permission_callback' => 'openstation_games_rest_permission', | |
| 129 | + 'callback' => 'openstation_games_rest_list_challenges', | |
| 130 | + 'args' => array( | |
| 131 | + 'box' => array( | |
| 132 | + 'type' => 'string', | |
| 133 | + 'default' => 'all', | |
| 134 | + 'enum' => array( 'incoming', 'outgoing', 'all' ), | |
| 135 | + ), | |
| 136 | + 'state' => array( | |
| 137 | + 'type' => 'string', | |
| 138 | + 'default' => '', | |
| 139 | + 'enum' => array( '', 'pending', 'accepted', 'declined', 'completed' ), | |
| 140 | + ), | |
| 141 | + ), | |
| 142 | + ), | |
| 143 | + array( | |
| 144 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 145 | + 'permission_callback' => 'openstation_games_rest_permission', | |
| 146 | + 'callback' => 'openstation_games_rest_create_challenge', | |
| 147 | + 'args' => array( | |
| 148 | + 'game' => array( | |
| 149 | + 'type' => 'string', | |
| 150 | + 'required' => true, | |
| 151 | + ), | |
| 152 | + 'recipient_id' => array( | |
| 153 | + 'type' => 'integer', | |
| 154 | + 'required' => true, | |
| 155 | + ), | |
| 156 | + 'score' => array( | |
| 157 | + 'type' => 'integer', | |
| 158 | + 'required' => true, | |
| 159 | + 'minimum' => 0, | |
| 160 | + ), | |
| 161 | + 'meta' => array( | |
| 162 | + 'type' => 'object', | |
| 163 | + 'default' => array(), | |
| 164 | + ), | |
| 165 | + ), | |
| 166 | + ), | |
| 167 | + ) | |
| 168 | + ); | |
| 169 | + | |
| 170 | + register_rest_route( | |
| 171 | + $ns, | |
| 172 | + '/games/challenges/(?P<id>\d+)/accept', | |
| 173 | + array( | |
| 88 | 174 | 'methods' => WP_REST_Server::CREATABLE, |
| 89 | - 'permission_callback' => 'desktop_mode_games_rest_permission', | |
| 90 | - 'callback' => 'desktop_mode_games_rest_submit_score', | |
| 175 | + 'permission_callback' => 'openstation_games_rest_permission', | |
| 176 | + 'callback' => 'openstation_games_rest_accept_challenge', | |
| 177 | + ) | |
| 178 | + ); | |
| 179 | + | |
| 180 | + register_rest_route( | |
| 181 | + $ns, | |
| 182 | + '/games/challenges/(?P<id>\d+)/decline', | |
| 183 | + array( | |
| 184 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 185 | + 'permission_callback' => 'openstation_games_rest_permission', | |
| 186 | + 'callback' => 'openstation_games_rest_decline_challenge', | |
| 187 | + ) | |
| 188 | + ); | |
| 189 | + | |
| 190 | + register_rest_route( | |
| 191 | + $ns, | |
| 192 | + '/games/challenges/(?P<id>\d+)/complete', | |
| 193 | + array( | |
| 194 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 195 | + 'permission_callback' => 'openstation_games_rest_permission', | |
| 196 | + 'callback' => 'openstation_games_rest_complete_challenge', | |
| 91 | 197 | 'args' => array( |
| 92 | - 'score' => array( 'type' => 'integer', 'required' => true, 'minimum' => 0 ), | |
| 93 | - 'meta' => array( 'type' => 'object', 'default' => array() ), | |
| 198 | + 'score' => array( | |
| 199 | + 'type' => 'integer', | |
| 200 | + 'required' => true, | |
| 201 | + 'minimum' => 0, | |
| 202 | + ), | |
| 203 | + 'meta' => array( | |
| 204 | + 'type' => 'object', | |
| 205 | + 'default' => array(), | |
| 206 | + ), | |
| 94 | 207 | ), |
| 95 | - ), | |
| 96 | - ) ); | |
| 208 | + ) | |
| 209 | + ); | |
| 97 | 210 | |
| 98 | - register_rest_route( $ns, '/games/challenges', array( | |
| 211 | + register_rest_route( | |
| 212 | + $ns, | |
| 213 | + '/games/playtime', | |
| 99 | 214 | array( |
| 100 | 215 | 'methods' => WP_REST_Server::READABLE, |
| 101 | - 'permission_callback' => 'desktop_mode_games_rest_permission', | |
| 102 | - 'callback' => 'desktop_mode_games_rest_list_challenges', | |
| 216 | + 'permission_callback' => 'openstation_games_rest_permission', | |
| 217 | + 'callback' => 'openstation_games_rest_get_playtime', | |
| 218 | + ) | |
| 219 | + ); | |
| 220 | + | |
| 221 | + register_rest_route( | |
| 222 | + $ns, | |
| 223 | + '/games/(?P<game>[a-z0-9_\-]+)/playtime', | |
| 224 | + array( | |
| 225 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 226 | + 'permission_callback' => 'openstation_games_rest_permission', | |
| 227 | + 'callback' => 'openstation_games_rest_record_playtime', | |
| 103 | 228 | 'args' => array( |
| 104 | - 'box' => array( 'type' => 'string', 'default' => 'all', 'enum' => array( 'incoming', 'outgoing', 'all' ) ), | |
| 105 | - 'state' => array( 'type' => 'string', 'default' => '', 'enum' => array( '', 'pending', 'accepted', 'declined', 'completed' ) ), | |
| 229 | + 'seconds' => array( | |
| 230 | + 'type' => 'integer', | |
| 231 | + 'required' => true, | |
| 232 | + 'minimum' => 1, | |
| 233 | + ), | |
| 106 | 234 | ), |
| 107 | - ), | |
| 235 | + ) | |
| 236 | + ); | |
| 237 | + | |
| 238 | + register_rest_route( | |
| 239 | + $ns, | |
| 240 | + '/games/users/search', | |
| 108 | 241 | array( |
| 109 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 110 | - 'permission_callback' => 'desktop_mode_games_rest_permission', | |
| 111 | - 'callback' => 'desktop_mode_games_rest_create_challenge', | |
| 242 | + 'methods' => WP_REST_Server::READABLE, | |
| 243 | + 'permission_callback' => 'openstation_games_rest_permission', | |
| 244 | + 'callback' => 'openstation_games_rest_search_users', | |
| 112 | 245 | 'args' => array( |
| 113 | - 'game' => array( 'type' => 'string', 'required' => true ), | |
| 114 | - 'recipient_id' => array( 'type' => 'integer', 'required' => true ), | |
| 115 | - 'score' => array( 'type' => 'integer', 'required' => true, 'minimum' => 0 ), | |
| 116 | - 'meta' => array( 'type' => 'object', 'default' => array() ), | |
| 246 | + 'q' => array( | |
| 247 | + 'type' => 'string', | |
| 248 | + 'default' => '', | |
| 249 | + ), | |
| 250 | + 'exclude' => array( | |
| 251 | + 'type' => 'string', | |
| 252 | + 'default' => '', | |
| 253 | + ), | |
| 117 | 254 | ), |
| 118 | - ), | |
| 119 | - ) ); | |
| 120 | - | |
| 121 | - register_rest_route( $ns, '/games/challenges/(?P<id>\d+)/accept', array( | |
| 122 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 123 | - 'permission_callback' => 'desktop_mode_games_rest_permission', | |
| 124 | - 'callback' => 'desktop_mode_games_rest_accept_challenge', | |
| 125 | - ) ); | |
| 126 | - | |
| 127 | - register_rest_route( $ns, '/games/challenges/(?P<id>\d+)/decline', array( | |
| 128 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 129 | - 'permission_callback' => 'desktop_mode_games_rest_permission', | |
| 130 | - 'callback' => 'desktop_mode_games_rest_decline_challenge', | |
| 131 | - ) ); | |
| 132 | - | |
| 133 | - register_rest_route( $ns, '/games/challenges/(?P<id>\d+)/complete', array( | |
| 134 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 135 | - 'permission_callback' => 'desktop_mode_games_rest_permission', | |
| 136 | - 'callback' => 'desktop_mode_games_rest_complete_challenge', | |
| 137 | - 'args' => array( | |
| 138 | - 'score' => array( 'type' => 'integer', 'required' => true, 'minimum' => 0 ), | |
| 139 | - 'meta' => array( 'type' => 'object', 'default' => array() ), | |
| 140 | - ), | |
| 141 | - ) ); | |
| 142 | - | |
| 143 | - register_rest_route( $ns, '/games/playtime', array( | |
| 144 | - 'methods' => WP_REST_Server::READABLE, | |
| 145 | - 'permission_callback' => 'desktop_mode_games_rest_permission', | |
| 146 | - 'callback' => 'desktop_mode_games_rest_get_playtime', | |
| 147 | - ) ); | |
| 148 | - | |
| 149 | - register_rest_route( $ns, '/games/(?P<game>[a-z0-9_\-]+)/playtime', array( | |
| 150 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 151 | - 'permission_callback' => 'desktop_mode_games_rest_permission', | |
| 152 | - 'callback' => 'desktop_mode_games_rest_record_playtime', | |
| 153 | - 'args' => array( | |
| 154 | - 'seconds' => array( 'type' => 'integer', 'required' => true, 'minimum' => 1 ), | |
| 155 | - ), | |
| 156 | - ) ); | |
| 157 | - | |
| 158 | - register_rest_route( $ns, '/games/users/search', array( | |
| 159 | - 'methods' => WP_REST_Server::READABLE, | |
| 160 | - 'permission_callback' => 'desktop_mode_games_rest_permission', | |
| 161 | - 'callback' => 'desktop_mode_games_rest_search_users', | |
| 162 | - 'args' => array( | |
| 163 | - 'q' => array( 'type' => 'string', 'default' => '' ), | |
| 164 | - 'exclude' => array( 'type' => 'string', 'default' => '' ), | |
| 165 | - ), | |
| 166 | - ) ); | |
| 255 | + ) | |
| 256 | + ); | |
| 167 | 257 | } |
| 168 | -add_action( 'rest_api_init', 'desktop_mode_games_register_rest_routes' ); | |
| 258 | +add_action( 'rest_api_init', 'openstation_games_register_rest_routes' ); | |
| 169 | 259 | |
| 170 | 260 | /** |
| 171 | 261 | * Resolve + validate the `game` path param. 404s unknown ids so the |
| 172 | 262 | * scores surface doesn't leak which games exist server-side. |
| 173 | 263 | * |
| 174 | - * @since 0.9.6 | |
| 175 | 264 | * @internal |
| 176 | 265 | * |
| 177 | 266 | * @param WP_REST_Request $req Request. |
| 178 | 267 | * @return string|WP_Error The sanitized game id. |
| 179 | 268 | */ |
| 180 | -function desktop_mode_games_rest_resolve_game( WP_REST_Request $req ) { | |
| 269 | +function openstation_games_rest_resolve_game( WP_REST_Request $req ) { | |
| 181 | 270 | $game = sanitize_key( (string) $req->get_param( 'game' ) ); |
| 182 | - if ( '' === $game || ! desktop_mode_games_is_registered( $game ) ) { | |
| 271 | + if ( '' === $game || ! openstation_games_is_registered( $game ) ) { | |
| 183 | 272 | return new WP_Error( |
| 184 | - 'desktop_mode_unknown_game', | |
| 273 | + 'openstation_unknown_game', | |
| 185 | 274 | __( 'Unknown game.', 'desktop-mode' ), |
| 186 | 275 | array( 'status' => 404 ) |
| 187 | 276 | ); |
| 188 | 277 | } |
| @@ -190,41 +279,42 @@ | ||
| 190 | 279 | } |
| 191 | 280 | |
| 192 | 281 | /** |
| 193 | 282 | * GET /games/{game}/scores |
| 194 | - * | |
| 195 | - * @since 0.9.6 | |
| 196 | 283 | */ |
| 197 | -function desktop_mode_games_rest_list_scores( WP_REST_Request $req ) { | |
| 198 | - $game = desktop_mode_games_rest_resolve_game( $req ); | |
| 284 | +function openstation_games_rest_list_scores( WP_REST_Request $req ) { | |
| 285 | + $game = openstation_games_rest_resolve_game( $req ); | |
| 199 | 286 | if ( is_wp_error( $game ) ) { |
| 200 | 287 | return $game; |
| 201 | 288 | } |
| 202 | - $result = desktop_mode_games_get_scores( $game, array( | |
| 203 | - 'page' => (int) $req->get_param( 'page' ), | |
| 204 | - 'per_page' => (int) $req->get_param( 'per_page' ), | |
| 205 | - 'orderby' => (string) $req->get_param( 'orderby' ), | |
| 206 | - 'order' => (string) $req->get_param( 'order' ), | |
| 207 | - 'user_id' => (int) $req->get_param( 'user_id' ), | |
| 208 | - ) ); | |
| 209 | - return rest_ensure_response( array( | |
| 210 | - 'scores' => $result['rows'], | |
| 211 | - 'total' => $result['total'], | |
| 212 | - ) ); | |
| 289 | + $result = openstation_games_get_scores( | |
| 290 | + $game, | |
| 291 | + array( | |
| 292 | + 'page' => (int) $req->get_param( 'page' ), | |
| 293 | + 'per_page' => (int) $req->get_param( 'per_page' ), | |
| 294 | + 'orderby' => (string) $req->get_param( 'orderby' ), | |
| 295 | + 'order' => (string) $req->get_param( 'order' ), | |
| 296 | + 'user_id' => (int) $req->get_param( 'user_id' ), | |
| 297 | + ) | |
| 298 | + ); | |
| 299 | + return rest_ensure_response( | |
| 300 | + array( | |
| 301 | + 'scores' => $result['rows'], | |
| 302 | + 'total' => $result['total'], | |
| 303 | + ) | |
| 304 | + ); | |
| 213 | 305 | } |
| 214 | 306 | |
| 215 | 307 | /** |
| 216 | 308 | * POST /games/{game}/scores — always records for the current user; |
| 217 | 309 | * there is no way to submit a score on someone else's behalf. |
| 218 | - * | |
| 219 | - * @since 0.9.6 | |
| 220 | 310 | */ |
| 221 | -function desktop_mode_games_rest_submit_score( WP_REST_Request $req ) { | |
| 222 | - $game = desktop_mode_games_rest_resolve_game( $req ); | |
| 311 | +function openstation_games_rest_submit_score( WP_REST_Request $req ) { | |
| 312 | + $game = openstation_games_rest_resolve_game( $req ); | |
| 223 | 313 | if ( is_wp_error( $game ) ) { |
| 224 | 314 | return $game; |
| 225 | 315 | } |
| 226 | - $id = desktop_mode_games_save_score( | |
| 316 | + $id = openstation_games_save_score( | |
| 227 | 317 | $game, |
| 228 | 318 | get_current_user_id(), |
| 229 | 319 | (int) $req->get_param( 'score' ), |
| 230 | 320 | (array) $req->get_param( 'meta' ) |
| @@ -236,37 +326,35 @@ | ||
| 236 | 326 | } |
| 237 | 327 | |
| 238 | 328 | /** |
| 239 | 329 | * GET /games/playtime — the current user's `game id => seconds` map. |
| 240 | - * | |
| 241 | - * @since 0.9.7 | |
| 242 | 330 | */ |
| 243 | -function desktop_mode_games_rest_get_playtime() { | |
| 331 | +function openstation_games_rest_get_playtime() { | |
| 244 | 332 | $user_id = get_current_user_id(); |
| 245 | 333 | // Day maps are cast per-game so empty buckets JSON-encode as `{}`. |
| 246 | 334 | $daily = array(); |
| 247 | - foreach ( desktop_mode_games_get_playtime_daily( $user_id ) as $game => $days ) { | |
| 335 | + foreach ( openstation_games_get_playtime_daily( $user_id ) as $game => $days ) { | |
| 248 | 336 | $daily[ $game ] = (object) $days; |
| 249 | 337 | } |
| 250 | - return rest_ensure_response( array( | |
| 251 | - 'playtime' => (object) desktop_mode_games_get_playtime( $user_id ), | |
| 252 | - 'daily' => (object) $daily, | |
| 253 | - 'today' => desktop_mode_games_playtime_today_key(), | |
| 254 | - ) ); | |
| 338 | + return rest_ensure_response( | |
| 339 | + array( | |
| 340 | + 'playtime' => (object) openstation_games_get_playtime( $user_id ), | |
| 341 | + 'daily' => (object) $daily, | |
| 342 | + 'today' => openstation_games_playtime_today_key(), | |
| 343 | + ) | |
| 344 | + ); | |
| 255 | 345 | } |
| 256 | 346 | |
| 257 | 347 | /** |
| 258 | 348 | * POST /games/{game}/playtime — always records for the current user; |
| 259 | 349 | * there is no way to record play time on someone else's behalf. |
| 260 | - * | |
| 261 | - * @since 0.9.7 | |
| 262 | 350 | */ |
| 263 | -function desktop_mode_games_rest_record_playtime( WP_REST_Request $req ) { | |
| 264 | - $game = desktop_mode_games_rest_resolve_game( $req ); | |
| 351 | +function openstation_games_rest_record_playtime( WP_REST_Request $req ) { | |
| 352 | + $game = openstation_games_rest_resolve_game( $req ); | |
| 265 | 353 | if ( is_wp_error( $game ) ) { |
| 266 | 354 | return $game; |
| 267 | 355 | } |
| 268 | - $total = desktop_mode_games_add_playtime( | |
| 356 | + $total = openstation_games_add_playtime( | |
| 269 | 357 | $game, |
| 270 | 358 | get_current_user_id(), |
| 271 | 359 | (int) $req->get_param( 'seconds' ) |
| 272 | 360 | ); |
| @@ -277,17 +365,15 @@ | ||
| 277 | 365 | } |
| 278 | 366 | |
| 279 | 367 | /** |
| 280 | 368 | * GET /games/challenges — challenges involving the current user. |
| 281 | - * | |
| 282 | - * @since 0.9.6 | |
| 283 | 369 | */ |
| 284 | -function desktop_mode_games_rest_list_challenges( WP_REST_Request $req ) { | |
| 370 | +function openstation_games_rest_list_challenges( WP_REST_Request $req ) { | |
| 285 | 371 | $user_id = get_current_user_id(); |
| 286 | 372 | $box = (string) $req->get_param( 'box' ); |
| 287 | 373 | $state = (string) $req->get_param( 'state' ); |
| 288 | 374 | |
| 289 | - $rows = desktop_mode_games_get_challenges_for_user( $user_id, 0, 100 ); | |
| 375 | + $rows = openstation_games_get_challenges_for_user( $user_id, 0, 100 ); | |
| 290 | 376 | $out = array(); |
| 291 | 377 | foreach ( $rows as $row ) { |
| 292 | 378 | if ( 'incoming' === $box && (int) $row['recipient_id'] !== $user_id ) { |
| 293 | 379 | continue; |
| @@ -297,9 +383,9 @@ | ||
| 297 | 383 | } |
| 298 | 384 | if ( '' !== $state && $row['state'] !== $state ) { |
| 299 | 385 | continue; |
| 300 | 386 | } |
| 301 | - $out[] = desktop_mode_games_shape_challenge( $row ); | |
| 387 | + $out[] = openstation_games_shape_challenge( $row ); | |
| 302 | 388 | } |
| 303 | 389 | // Newest change first for the inbox view. |
| 304 | 390 | $out = array_reverse( $out ); |
| 305 | 391 | return rest_ensure_response( array( 'challenges' => $out ) ); |
| @@ -306,19 +392,17 @@ | ||
| 306 | 392 | } |
| 307 | 393 | |
| 308 | 394 | /** |
| 309 | 395 | * POST /games/challenges |
| 310 | - * | |
| 311 | - * @since 0.9.6 | |
| 312 | 396 | */ |
| 313 | -function desktop_mode_games_rest_create_challenge( WP_REST_Request $req ) { | |
| 397 | +function openstation_games_rest_create_challenge( WP_REST_Request $req ) { | |
| 314 | 398 | $challenger_id = get_current_user_id(); |
| 315 | 399 | $recipient_id = (int) $req->get_param( 'recipient_id' ); |
| 316 | 400 | $game = sanitize_key( (string) $req->get_param( 'game' ) ); |
| 317 | 401 | |
| 318 | - if ( ! desktop_mode_games_is_registered( $game ) ) { | |
| 402 | + if ( ! openstation_games_is_registered( $game ) ) { | |
| 319 | 403 | return new WP_Error( |
| 320 | - 'desktop_mode_unknown_game', | |
| 404 | + 'openstation_unknown_game', | |
| 321 | 405 | __( 'Unknown game.', 'desktop-mode' ), |
| 322 | 406 | array( 'status' => 404 ) |
| 323 | 407 | ); |
| 324 | 408 | } |
| @@ -327,28 +411,26 @@ | ||
| 327 | 411 | * Filters whether a user may challenge another user. Return |
| 328 | 412 | * `false` (or a `WP_Error`) to block — e.g. respecting a |
| 329 | 413 | * do-not-disturb setting or a per-role policy. |
| 330 | 414 | * |
| 331 | - * @since 0.9.6 | |
| 332 | - * | |
| 333 | 415 | * @param bool|WP_Error $allowed Default `true`. |
| 334 | 416 | * @param int $challenger_id Sender. |
| 335 | 417 | * @param int $recipient_id Receiver. |
| 336 | 418 | * @param string $game Game id. |
| 337 | 419 | */ |
| 338 | - $allowed = apply_filters( 'desktop_mode_games_can_challenge', true, $challenger_id, $recipient_id, $game ); | |
| 420 | + $allowed = apply_filters( 'openstation_games_can_challenge', true, $challenger_id, $recipient_id, $game ); | |
| 339 | 421 | if ( is_wp_error( $allowed ) ) { |
| 340 | 422 | return $allowed; |
| 341 | 423 | } |
| 342 | 424 | if ( true !== $allowed ) { |
| 343 | 425 | return new WP_Error( |
| 344 | - 'desktop_mode_challenge_blocked', | |
| 426 | + 'openstation_challenge_blocked', | |
| 345 | 427 | __( 'You cannot challenge this user.', 'desktop-mode' ), |
| 346 | 428 | array( 'status' => 403 ) |
| 347 | 429 | ); |
| 348 | 430 | } |
| 349 | 431 | |
| 350 | - $id = desktop_mode_games_create_challenge( | |
| 432 | + $id = openstation_games_create_challenge( | |
| 351 | 433 | $game, |
| 352 | 434 | $challenger_id, |
| 353 | 435 | $recipient_id, |
| 354 | 436 | (int) $req->get_param( 'score' ), |
| @@ -356,33 +438,32 @@ | ||
| 356 | 438 | ); |
| 357 | 439 | if ( is_wp_error( $id ) ) { |
| 358 | 440 | return $id; |
| 359 | 441 | } |
| 360 | - $row = desktop_mode_games_get_challenge( $id ); | |
| 361 | - return rest_ensure_response( array( 'challenge' => desktop_mode_games_shape_challenge( $row ) ) ); | |
| 442 | + $row = openstation_games_get_challenge( $id ); | |
| 443 | + return rest_ensure_response( array( 'challenge' => openstation_games_shape_challenge( $row ) ) ); | |
| 362 | 444 | } |
| 363 | 445 | |
| 364 | 446 | /** |
| 365 | 447 | * Load a challenge and verify the current user is its recipient. |
| 366 | 448 | * |
| 367 | - * @since 0.9.6 | |
| 368 | 449 | * @internal |
| 369 | 450 | * |
| 370 | 451 | * @param WP_REST_Request $req Request. |
| 371 | 452 | * @return array|WP_Error The raw challenge row. |
| 372 | 453 | */ |
| 373 | -function desktop_mode_games_rest_resolve_recipient_challenge( WP_REST_Request $req ) { | |
| 374 | - $row = desktop_mode_games_get_challenge( (int) $req->get_param( 'id' ) ); | |
| 454 | +function openstation_games_rest_resolve_recipient_challenge( WP_REST_Request $req ) { | |
| 455 | + $row = openstation_games_get_challenge( (int) $req->get_param( 'id' ) ); | |
| 375 | 456 | if ( ! $row ) { |
| 376 | 457 | return new WP_Error( |
| 377 | - 'desktop_mode_challenge_not_found', | |
| 458 | + 'openstation_challenge_not_found', | |
| 378 | 459 | __( 'Challenge not found.', 'desktop-mode' ), |
| 379 | 460 | array( 'status' => 404 ) |
| 380 | 461 | ); |
| 381 | 462 | } |
| 382 | - if ( (int) $row['recipient_id'] !== get_current_user_id() ) { | |
| 463 | + if ( get_current_user_id() !== (int) $row['recipient_id'] ) { | |
| 383 | 464 | return new WP_Error( |
| 384 | - 'desktop_mode_challenge_forbidden', | |
| 465 | + 'openstation_challenge_forbidden', | |
| 385 | 466 | __( 'Only the challenged user can act on this challenge.', 'desktop-mode' ), |
| 386 | 467 | array( 'status' => 403 ) |
| 387 | 468 | ); |
| 388 | 469 | } |
| @@ -390,53 +471,47 @@ | ||
| 390 | 471 | } |
| 391 | 472 | |
| 392 | 473 | /** |
| 393 | 474 | * POST /games/challenges/{id}/accept |
| 394 | - * | |
| 395 | - * @since 0.9.6 | |
| 396 | 475 | */ |
| 397 | -function desktop_mode_games_rest_accept_challenge( WP_REST_Request $req ) { | |
| 398 | - $row = desktop_mode_games_rest_resolve_recipient_challenge( $req ); | |
| 476 | +function openstation_games_rest_accept_challenge( WP_REST_Request $req ) { | |
| 477 | + $row = openstation_games_rest_resolve_recipient_challenge( $req ); | |
| 399 | 478 | if ( is_wp_error( $row ) ) { |
| 400 | 479 | return $row; |
| 401 | 480 | } |
| 402 | - $result = desktop_mode_games_set_challenge_state( (int) $row['id'], 'accepted' ); | |
| 481 | + $result = openstation_games_set_challenge_state( (int) $row['id'], 'accepted' ); | |
| 403 | 482 | if ( is_wp_error( $result ) ) { |
| 404 | 483 | return $result; |
| 405 | 484 | } |
| 406 | - $updated = desktop_mode_games_get_challenge( (int) $row['id'] ); | |
| 407 | - return rest_ensure_response( array( 'challenge' => desktop_mode_games_shape_challenge( $updated ) ) ); | |
| 485 | + $updated = openstation_games_get_challenge( (int) $row['id'] ); | |
| 486 | + return rest_ensure_response( array( 'challenge' => openstation_games_shape_challenge( $updated ) ) ); | |
| 408 | 487 | } |
| 409 | 488 | |
| 410 | 489 | /** |
| 411 | 490 | * POST /games/challenges/{id}/decline |
| 412 | - * | |
| 413 | - * @since 0.9.6 | |
| 414 | 491 | */ |
| 415 | -function desktop_mode_games_rest_decline_challenge( WP_REST_Request $req ) { | |
| 416 | - $row = desktop_mode_games_rest_resolve_recipient_challenge( $req ); | |
| 492 | +function openstation_games_rest_decline_challenge( WP_REST_Request $req ) { | |
| 493 | + $row = openstation_games_rest_resolve_recipient_challenge( $req ); | |
| 417 | 494 | if ( is_wp_error( $row ) ) { |
| 418 | 495 | return $row; |
| 419 | 496 | } |
| 420 | - $result = desktop_mode_games_set_challenge_state( (int) $row['id'], 'declined' ); | |
| 497 | + $result = openstation_games_set_challenge_state( (int) $row['id'], 'declined' ); | |
| 421 | 498 | if ( is_wp_error( $result ) ) { |
| 422 | 499 | return $result; |
| 423 | 500 | } |
| 424 | - $updated = desktop_mode_games_get_challenge( (int) $row['id'] ); | |
| 425 | - return rest_ensure_response( array( 'challenge' => desktop_mode_games_shape_challenge( $updated ) ) ); | |
| 501 | + $updated = openstation_games_get_challenge( (int) $row['id'] ); | |
| 502 | + return rest_ensure_response( array( 'challenge' => openstation_games_shape_challenge( $updated ) ) ); | |
| 426 | 503 | } |
| 427 | 504 | |
| 428 | 505 | /** |
| 429 | 506 | * POST /games/challenges/{id}/complete |
| 430 | - * | |
| 431 | - * @since 0.9.6 | |
| 432 | 507 | */ |
| 433 | -function desktop_mode_games_rest_complete_challenge( WP_REST_Request $req ) { | |
| 434 | - $row = desktop_mode_games_rest_resolve_recipient_challenge( $req ); | |
| 508 | +function openstation_games_rest_complete_challenge( WP_REST_Request $req ) { | |
| 509 | + $row = openstation_games_rest_resolve_recipient_challenge( $req ); | |
| 435 | 510 | if ( is_wp_error( $row ) ) { |
| 436 | 511 | return $row; |
| 437 | 512 | } |
| 438 | - $updated = desktop_mode_games_complete_challenge( | |
| 513 | + $updated = openstation_games_complete_challenge( | |
| 439 | 514 | (int) $row['id'], |
| 440 | 515 | (int) $req->get_param( 'score' ), |
| 441 | 516 | (array) $req->get_param( 'meta' ) |
| 442 | 517 | ); |
| @@ -442,9 +517,9 @@ | ||
| 442 | 517 | ); |
| 443 | 518 | if ( is_wp_error( $updated ) ) { |
| 444 | 519 | return $updated; |
| 445 | 520 | } |
| 446 | - return rest_ensure_response( array( 'challenge' => desktop_mode_games_shape_challenge( $updated ) ) ); | |
| 521 | + return rest_ensure_response( array( 'challenge' => openstation_games_shape_challenge( $updated ) ) ); | |
| 447 | 522 | } |
| 448 | 523 | |
| 449 | 524 | /** |
| 450 | 525 | * GET /games/users/search?q=<>&exclude=<csv> — autocomplete for the |
| @@ -449,12 +524,10 @@ | ||
| 449 | 524 | /** |
| 450 | 525 | * GET /games/users/search?q=<>&exclude=<csv> — autocomplete for the |
| 451 | 526 | * opponent picker. Thin sibling of the folder-share picker, gated on |
| 452 | 527 | * `read` instead of `edit_posts` so subscribers can be challenged. |
| 453 | - * | |
| 454 | - * @since 0.9.6 | |
| 455 | 528 | */ |
| 456 | -function desktop_mode_games_rest_search_users( WP_REST_Request $req ) { | |
| 529 | +function openstation_games_rest_search_users( WP_REST_Request $req ) { | |
| 457 | 530 | $q = trim( (string) $req->get_param( 'q' ) ); |
| 458 | 531 | $exclude = array_filter( array_map( 'intval', explode( ',', (string) $req->get_param( 'exclude' ) ) ) ); |
| 459 | 532 | |
| 460 | 533 | // Always exclude the current viewer — self-challenges are |
| @@ -476,14 +549,12 @@ | ||
| 476 | 549 | |
| 477 | 550 | /** |
| 478 | 551 | * Filter the WP_User_Query args used by the opponent picker. |
| 479 | 552 | * |
| 480 | - * @since 0.9.6 | |
| 481 | - * | |
| 482 | 553 | * @param array $args Default args. |
| 483 | 554 | * @param array $req Request params (`q`, `exclude`). |
| 484 | 555 | */ |
| 485 | - $args = (array) apply_filters( 'desktop_mode_games_user_query_args', $args, $req->get_params() ); | |
| 556 | + $args = (array) apply_filters( 'openstation_games_user_query_args', $args, $req->get_params() ); | |
| 486 | 557 | |
| 487 | 558 | $query = new WP_User_Query( $args ); |
| 488 | 559 | $users = $query->get_results(); |
| 489 | 560 | $out = array(); |