Skip to content

Commit

Permalink
feat: display subscriptions list on settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
davisshaver committed Dec 13, 2024
1 parent 2bbf2aa commit 0a85641
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 8 deletions.
4 changes: 2 additions & 2 deletions farcaster-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Plugin Name: Farcaster WP
* Plugin URI: https://farcaster-wp.davisshaver.com/
* Description: Farcaster WP connects your WordPress site to Farcaster.
* Version: 0.0.18
* Version: 0.0.19
* Author: Davis Shaver
* Author URI: https://davisshaver.com/
* License: GPL v2 or later
Expand All @@ -22,7 +22,7 @@

defined( 'ABSPATH' ) || exit;

define( 'FARCASTER_WP_VERSION', '0.0.18' );
define( 'FARCASTER_WP_VERSION', '0.0.19' );

define( 'FARCASTER_WP_API_NAMESPACE', 'farcaster-wp/v1' );
define( 'FARCASTER_WP_API_URL', get_site_url() . '/wp-json/' . FARCASTER_WP_API_NAMESPACE );
Expand Down
113 changes: 113 additions & 0 deletions includes/api/class-subscriptions-controller.php
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',
],
],
],
],
],
];
}
}
5 changes: 5 additions & 0 deletions includes/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Farcaster_WP\API\Manifest_Controller;
use Farcaster_WP\API\Webhook_Controller;
use Farcaster_WP\API\Subscriptions_Controller;

defined( 'ABSPATH' ) || exit;

Expand All @@ -29,6 +30,10 @@ public static function init() {
include_once 'api/class-webhook-controller.php';
$webhook_api = new Webhook_Controller();
add_action( 'rest_api_init', [ $webhook_api, 'register_routes' ] );

include_once 'api/class-subscriptions-controller.php';
$subscriptions_api = new Subscriptions_Controller();
add_action( 'rest_api_init', [ $subscriptions_api, 'register_routes' ] );
}
}
}
7 changes: 4 additions & 3 deletions includes/class-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Notifications {
*
* @var string
*/
protected static $notifications_option_name = 'farcaster_wp_subscriptions';
public static $notifications_option_name = 'farcaster_wp_subscriptions';

/**
* Runs the initialization.
Expand Down Expand Up @@ -277,8 +277,9 @@ public static function process_webhook( $header, $payload ) {
public static function add_subscription( $fid, $key, $url, $token ) {
$current_subscriptions = get_option( self::$notifications_option_name, array() );
$current_subscriptions[ $fid ][ $key ] = [
'url' => $url,
'token' => $token,
'url' => $url,
'token' => $token,
'timestamp' => time(),
];
update_option( self::$notifications_option_name, $current_subscriptions );
return [ 'success' => true ];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "farcaster-wp",
"version": "0.0.18",
"version": "0.0.19",
"description": "Farcaster WP connects your WordPress site to Farcaster.",
"author": "Davis Shaver",
"license": "GPL-2.0-or-later",
Expand Down
4 changes: 2 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: WordPress, web3, Farcaster, Ethereum
Tested up to: 6.7.1
Requires at least: 6.7.0
Requires PHP: 7.0
Stable tag: 0.0.18
Stable tag: 0.0.19
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -26,7 +26,7 @@ Farcaster WP makes it easy to setup [Farcaster frames](https://docs.farcaster.xy

== Changelog ==

= 0.0.18 =
= 0.0.19 =
* Initial plugin release to WordPress.org

== Screenshots =
Expand Down
4 changes: 4 additions & 0 deletions src/components/SettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
NotificationsEnabledControl,
} from './Controls';
import { ManifestViewer } from './ManifestViewer';
import { SubscriptionsList } from './SubscriptionsList';

const SettingsTitle = () => {
return (
Expand Down Expand Up @@ -143,6 +144,9 @@ const SettingsPage = () => {
onChange={ setNotificationsEnabled }
/>
</PanelRow>
<PanelRow>
<SubscriptionsList />
</PanelRow>
</PanelBody>
</Panel>
<Panel header="Manifest">
Expand Down
22 changes: 22 additions & 0 deletions src/components/SubscriptionsList.tsx
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>
);
};
24 changes: 24 additions & 0 deletions src/hooks/use-subscriptions.ts
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,
};
};
7 changes: 7 additions & 0 deletions src/utils/subscriptions.ts
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[];

0 comments on commit 0a85641

Please sign in to comment.