-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: display subscriptions list on settings page
- Loading branch information
1 parent
2bbf2aa
commit 0a85641
Showing
10 changed files
with
184 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
/** | ||
* Subscriptions API endpoint | ||
* | ||
* @package Farcaster_WP\API | ||
*/ | ||
|
||
namespace Farcaster_WP\API; | ||
|
||
use WP_REST_Controller; | ||
use WP_Error; | ||
use WP_REST_Response; | ||
use Farcaster_WP\Notifications; | ||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* REST API endpoints for Farcaster subscriptions. | ||
*/ | ||
class Subscriptions_Controller extends WP_REST_Controller { | ||
|
||
/** | ||
* Endpoint namespace. | ||
* | ||
* @var string | ||
*/ | ||
protected $namespace = FARCASTER_WP_API_NAMESPACE; | ||
|
||
/** | ||
* Endpoint resource. | ||
* | ||
* @var string | ||
*/ | ||
protected $resource_name = 'subscriptions'; | ||
|
||
/** | ||
* Register the routes. | ||
*/ | ||
public function register_routes() { | ||
// Register farcaster-wp/v1/subscriptions endpoint. | ||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->resource_name, | ||
[ | ||
[ | ||
'methods' => 'GET', | ||
'callback' => [ $this, 'get_subscriptions' ], | ||
], | ||
'schema' => [ $this, 'get_subscriptions_schema' ], | ||
'permission_callback' => function() { | ||
return current_user_can( 'manage_options' ); | ||
}, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Get the subscribers. | ||
* | ||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | ||
*/ | ||
public function get_subscriptions() { | ||
$subscriptions = get_option( Notifications::$notifications_option_name, array() ); | ||
$subscriptions_list = array(); | ||
|
||
foreach ( $subscriptions as $fid => $subscription ) { | ||
foreach ( $subscription as $key => $data ) { | ||
$subscriptions_list[] = array( | ||
'fid' => (int) $fid, | ||
'key' => $key, | ||
'url' => $data['url'], | ||
'token' => $data['token'], | ||
'timestamp' => $data['timestamp'] ?? 'not set', | ||
); | ||
} | ||
} | ||
|
||
return new WP_REST_Response( $subscriptions_list ); | ||
} | ||
|
||
/** | ||
* Get the REST schema for the endpoints. | ||
* | ||
* @return array | ||
*/ | ||
public function get_subscriptions_schema() { | ||
return [ | ||
'$schema' => 'http://json-schema.org/draft-04/schema#', | ||
'title' => $this->resource_name, | ||
'type' => 'object', | ||
'properties' => [ | ||
'subscriptions' => [ | ||
'type' => 'array', | ||
'items' => [ | ||
'type' => 'object', | ||
'required' => [ 'fid', 'token', 'url' ], | ||
'properties' => [ | ||
'fid' => [ | ||
'type' => 'integer', | ||
], | ||
'token' => [ | ||
'type' => 'string', | ||
], | ||
'url' => [ | ||
'type' => 'string', | ||
'format' => 'uri', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis | ||
import { __experimentalText as Text } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useSubscriptions } from '../hooks/use-subscriptions'; | ||
|
||
export const SubscriptionsList = () => { | ||
const { subscriptions } = useSubscriptions(); | ||
|
||
return ( | ||
<div> | ||
<div style={ { marginTop: '16px' } }> | ||
<Text> | ||
{ __( 'You have', 'farcaster-wp' ) }{ ' ' } | ||
{ subscriptions?.length }{ ' ' } | ||
{ __( 'subscriptions on your site:', 'farcaster-wp' ) } | ||
</Text> | ||
</div> | ||
<div style={ { marginTop: '8px' } } /> | ||
<pre>{ JSON.stringify( subscriptions, null, 2 ) }</pre> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { useCallback, useEffect, useState } from '@wordpress/element'; | ||
import { Subscriptions } from '../utils/subscriptions'; | ||
|
||
export const useSubscriptions = () => { | ||
const [ subscriptions, setSubscriptions ] = useState< Subscriptions >(); | ||
|
||
const fetchSubscriptions = useCallback( () => { | ||
apiFetch< Subscriptions >( { | ||
path: '/farcaster-wp/v1/subscriptions', | ||
} ).then( ( fetchedSubscriptions ) => { | ||
setSubscriptions( fetchedSubscriptions ); | ||
} ); | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
fetchSubscriptions(); | ||
}, [ fetchSubscriptions ] ); | ||
|
||
return { | ||
subscriptions, | ||
fetchSubscriptions, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type Subscription = { | ||
fid: number; | ||
token: string; | ||
url: string; | ||
}; | ||
|
||
export type Subscriptions = Subscription[]; |