| 1 | <?php |
| 2 | namespace WordPressdotorg\Plugin_Directory\API\Routes; |
| 3 | use WordPressdotorg\Plugin_Directory\Plugin_Directory; |
| 4 | use WordPressdotorg\Plugin_Directory\API\Base; |
| 5 | |
| 6 | /** |
| 7 | * An API endpoint for subscribing to commits for a particular plugin. |
| 8 | * |
| 9 | * @package WordPressdotorg_Plugin_Directory |
| 10 | */ |
| 11 | class Commit_Subscriptions extends Base { |
| 12 | |
| 13 | public function __construct() { |
| 14 | register_rest_route( 'plugins/v1', '/plugin/(?P<plugin_slug>[^/]+)/commit-subscribe?', array( |
| 15 | 'methods' => \WP_REST_Server::READABLE, |
| 16 | 'callback' => array( $this, 'subscribe' ), |
| 17 | 'args' => array( |
| 18 | 'plugin_slug' => array( |
| 19 | 'validate_callback' => array( $this, 'validate_plugin_slug_callback' ), |
| 20 | ), |
| 21 | 'subscribe' => array( |
| 22 | 'validate_callback' => 'is_bool', |
| 23 | ), |
| 24 | 'unsubscribe' => array( |
| 25 | 'validate_callback' => 'is_bool', |
| 26 | ), |
| 27 | ), |
| 28 | 'permission_callback' => 'is_user_logged_in' |
| 29 | ) ); |
| 30 | |
| 31 | register_rest_route( 'plugins/v1', '/plugin/(?P<plugin_slug>[^/]+)/commit-subscription?', array( |
| 32 | 'methods' => \WP_REST_Server::READABLE, |
| 33 | 'callback' => array( $this, 'subscription_list' ), |
| 34 | 'args' => array( |
| 35 | 'plugin_slug' => array( |
| 36 | 'validate_callback' => array( $this, 'validate_plugin_slug_callback' ), |
| 37 | ), |
| 38 | ), |
| 39 | ) ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Endpoint to subscribe to a plugin's commits. |
| 44 | * |
| 45 | * @param \WP_REST_Request $request The Rest API Request. |
| 46 | * @return bool True if the subscription was successful. |
| 47 | */ |
| 48 | public function subscribe( $request ) { |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Endpoint to return the list of subscribers to a plugin. |
| 53 | * |
| 54 | * If the internal API key is passed, a full list is returned. If the user is logged |
| 55 | * in, a list containing that user (if they're subscribed), or an empty list (if they're not) |
| 56 | * is returned. |
| 57 | * |
| 58 | * @param \WP_REST_Request $request The Rest API Request. |
| 59 | * @return array List of subscribed users |
| 60 | */ |
| 61 | public function subscription_list( $request ) { |
| 62 | $subscribers = array(); |
| 63 | |
| 64 | if ( $this->permission_check_internal_api_bearer( $request ) ) { |
| 65 | // This is an internal call, grab all subscribed users |
| 66 | } elseif ( is_user_logged_in() ) { |
| 67 | // A logged in user can only check if they're subscribed |
| 68 | } |
| 69 | |
| 70 | return $subscribers; |
| 71 | } |
| 72 | } |