name/v$this->version", $this->endpoint . '/(?P\S+)/cancel/', [ [ 'methods' => \WP_REST_Server::EDITABLE, 'callback' => $this->callback( $this->controller, 'cancel' ), 'permission_callback' => [ $this, 'cancel_permissions_check' ], ], // Register our schema callback. 'schema' => [ $this, 'get_item_schema' ], ] ); register_rest_route( "$this->name/v$this->version", $this->endpoint . '/(?P\S+)/complete/', [ [ 'methods' => \WP_REST_Server::EDITABLE, 'callback' => $this->callback( $this->controller, 'complete' ), 'permission_callback' => [ $this, 'complete_permissions_check' ], ], // Register our schema callback. 'schema' => [ $this, 'get_item_schema' ], ] ); register_rest_route( "$this->name/v$this->version", $this->endpoint . '/(?P\S+)/restore/', [ [ 'methods' => \WP_REST_Server::EDITABLE, 'callback' => $this->callback( $this->controller, 'restore' ), 'permission_callback' => [ $this, 'restore_permissions_check' ], ], // Register our schema callback. 'schema' => [ $this, 'get_item_schema' ], ] ); register_rest_route( "$this->name/v$this->version", $this->endpoint . '/(?P\S+)/renew/', [ [ 'methods' => \WP_REST_Server::EDITABLE, 'callback' => $this->callback( $this->controller, 'renew' ), 'permission_callback' => [ $this, 'update_item_permissions_check' ], ], // Register our schema callback. 'schema' => [ $this, 'get_item_schema' ], ] ); register_rest_route( "$this->name/v$this->version", $this->endpoint . '/(?P\S+)/preserve/', [ [ 'methods' => \WP_REST_Server::EDITABLE, 'callback' => $this->callback( $this->controller, 'preserve' ), 'permission_callback' => [ $this, 'update_item_permissions_check' ], ], // Register our schema callback. 'schema' => [ $this, 'get_item_schema' ], ] ); register_rest_route( "$this->name/v$this->version", $this->endpoint . '/(?P\S+)/upcoming_period/', [ [ 'methods' => \WP_REST_Server::EDITABLE, 'callback' => $this->callback( $this->controller, 'upcomingPeriod' ), 'permission_callback' => [ $this, 'get_item_permissions_check' ], ], // Register our schema callback. 'schema' => [ $this, 'get_item_schema' ], ] ); register_rest_route( "$this->name/v$this->version", $this->endpoint . '/(?P\S+)/pay_off/', [ [ 'methods' => \WP_REST_Server::EDITABLE, 'callback' => $this->callback( $this->controller, 'payOff' ), 'permission_callback' => [ $this, 'payoff_item_permissions_check' ], ], // Register our schema callback. 'schema' => [ $this, 'get_item_schema' ], ] ); } /** * Get our sample schema for a post. * * @return array The sample schema for a post */ public function get_item_schema() { if ( $this->schema ) { // Since WordPress 5.3, the schema can be cached in the $schema property. return $this->schema; } $this->schema = [ // This tells the spec of JSON Schema we are using which is draft 4. '$schema' => 'http://json-schema.org/draft-04/schema#', // The title property marks the identity of the resource. 'title' => $this->endpoint, 'type' => 'object', // In JSON Schema you can specify object properties in the properties attribute. 'properties' => [ 'id' => [ 'description' => esc_html__( 'Unique identifier for the object.', 'surecart' ), 'type' => 'string', 'context' => [ 'view', 'edit', 'embed' ], 'readonly' => true, ], 'trial_end_at' => [ 'type' => 'integer', 'admin_only' => true, ], ], ]; return $this->schema; } /** * Get the collection params. * * @return array */ public function get_collection_params() { $to_literal_bool = function ( $value ) { return rest_sanitize_boolean( $value ) ? 'true' : 'false'; }; return [ 'cancel_at_period_end' => [ 'description' => esc_html__( 'Only return subscriptions scheduled to cancel at the end of the current period.', 'surecart' ), 'type' => 'boolean', 'sanitize_callback' => $to_literal_bool, ], 'with_restore_at' => [ 'description' => esc_html__( 'Only return canceled subscriptions that can still be restored.', 'surecart' ), 'type' => 'boolean', 'sanitize_callback' => $to_literal_bool, ], ]; } /** * Anyone can get a specific subscription * * @param \WP_REST_Request $request Full details about the request. * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. */ public function get_item_permissions_check( $request ) { return current_user_can( 'read_sc_subscription', $request['id'] ); } /** * Need priveleges to read checkout sessions. * * @param \WP_REST_Request $request Full details about the request. * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. */ public function get_items_permissions_check( $request ) { return current_user_can( 'read_sc_subscriptions', $request->get_params() ); } /** * Check update permissions. * * @param \WP_REST_Request $request Full details about the request. * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. */ public function update_item_permissions_check( $request ) { return current_user_can( 'edit_sc_subscription', $request['id'], $request->get_params() ); } /** * Check cancel permissions. * * @param \WP_REST_Request $request Full details about the request. * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. */ public function cancel_permissions_check( $request ) { return current_user_can( 'cancel_sc_subscription', $request['id'] ); } /** * Check cancel permissions. * * @param \WP_REST_Request $request Full details about the request. * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. */ public function restore_permissions_check( $request ) { return current_user_can( 'restore_sc_subscription', $request['id'] ); } /** * Check complete permissions. * Only users who can edit all subscriptions can complete a subscription. * * @param \WP_REST_Request $request Full details about the request. * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. */ public function complete_permissions_check( $request ) { return current_user_can( 'edit_sc_subscriptions' ); } /** * Only a person who has the ability to edit all subscription * can pay off a subscription early. * * @param \WP_REST_Request $request Full details about the request. * @return true|\WP_Error True if the request has access to create items, WP_Error object otherwise. */ public function payoff_item_permissions_check( $request ) { return current_user_can( 'edit_sc_subscriptions' ); } /** * Nobody can delete. * * @param \WP_REST_Request $request Full details about the request. * @return false */ public function delete_item_permissions_check( $request ) { return false; } }