diff --git a/farcaster-wp.php b/farcaster-wp.php index 3f5c762..e4aa6e8 100644 --- a/farcaster-wp.php +++ b/farcaster-wp.php @@ -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 @@ -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 ); diff --git a/includes/api/class-subscriptions-controller.php b/includes/api/class-subscriptions-controller.php new file mode 100644 index 0000000..43c2595 --- /dev/null +++ b/includes/api/class-subscriptions-controller.php @@ -0,0 +1,113 @@ +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', + ], + ], + ], + ], + ], + ]; + } +} diff --git a/includes/class-api.php b/includes/class-api.php index 1d5761d..6160da1 100644 --- a/includes/class-api.php +++ b/includes/class-api.php @@ -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; @@ -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' ] ); } } } diff --git a/includes/class-notifications.php b/includes/class-notifications.php index 76b5639..ed86942 100644 --- a/includes/class-notifications.php +++ b/includes/class-notifications.php @@ -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. @@ -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 ]; diff --git a/package.json b/package.json index f5d0bc1..e9065cc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.txt b/readme.txt index ef09789..30f6676 100644 --- a/readme.txt +++ b/readme.txt @@ -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 @@ -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 = diff --git a/src/components/SettingsPage.tsx b/src/components/SettingsPage.tsx index 2d7ce8e..308f6f2 100644 --- a/src/components/SettingsPage.tsx +++ b/src/components/SettingsPage.tsx @@ -21,6 +21,7 @@ import { NotificationsEnabledControl, } from './Controls'; import { ManifestViewer } from './ManifestViewer'; +import { SubscriptionsList } from './SubscriptionsList'; const SettingsTitle = () => { return ( @@ -143,6 +144,9 @@ const SettingsPage = () => { onChange={ setNotificationsEnabled } /> + + + diff --git a/src/components/SubscriptionsList.tsx b/src/components/SubscriptionsList.tsx new file mode 100644 index 0000000..b3b0d32 --- /dev/null +++ b/src/components/SubscriptionsList.tsx @@ -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 ( +
+
+ + { __( 'You have', 'farcaster-wp' ) }{ ' ' } + { subscriptions?.length }{ ' ' } + { __( 'subscriptions on your site:', 'farcaster-wp' ) } + +
+
+
{ JSON.stringify( subscriptions, null, 2 ) }
+
+ ); +}; diff --git a/src/hooks/use-subscriptions.ts b/src/hooks/use-subscriptions.ts new file mode 100644 index 0000000..74b2f5f --- /dev/null +++ b/src/hooks/use-subscriptions.ts @@ -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, + }; +}; diff --git a/src/utils/subscriptions.ts b/src/utils/subscriptions.ts new file mode 100644 index 0000000..f4f79b3 --- /dev/null +++ b/src/utils/subscriptions.ts @@ -0,0 +1,7 @@ +export type Subscription = { + fid: number; + token: string; + url: string; +}; + +export type Subscriptions = Subscription[];