diff --git a/inc/admin/namespace.php b/inc/admin/namespace.php index d64d365..17ccf46 100644 --- a/inc/admin/namespace.php +++ b/inc/admin/namespace.php @@ -2,19 +2,28 @@ namespace MiniFAIR\Admin; +use Exception; use MiniFAIR; -use MiniFAIR\PLC\DID; use MiniFAIR\Keys; +use MiniFAIR\PLC\DID; use WP_Post; -const NONCE_CREATE_ACTION = 'minifair_create'; -const NONCE_SYNC_ACTION = 'minifair_sync'; +const ACTION_CREATE = 'create'; +const ACTION_EXPORT = 'export'; +const ACTION_IMPORT = 'import'; +const ACTION_KEY_ADD = 'key_add'; +const ACTION_KEY_REVOKE = 'key_revoke'; +const ACTION_SYNC = 'sync'; +const NONCE_PREFIX = 'minifair_'; const PAGE_SLUG = 'minifair'; function bootstrap() { // Register the admin menu and page before the PLC DID post type is registered. add_action( 'admin_menu', __NAMESPACE__ . '\\add_admin_menu', 0 ); - add_action( 'post_action_sync', __NAMESPACE__ . '\\maybe_on_sync' ); + add_action( 'post_action_' . ACTION_EXPORT, __NAMESPACE__ . '\\handle_action', 10, 1 ); + add_action( 'post_action_' . ACTION_KEY_ADD, __NAMESPACE__ . '\\handle_action', 10, 1 ); + add_action( 'post_action_' . ACTION_KEY_REVOKE, __NAMESPACE__ . '\\handle_action', 10, 1 ); + add_action( 'post_action_' . ACTION_SYNC, __NAMESPACE__ . '\\handle_action', 10, 1 ); // Hijack the post-new.php page to render our own form. add_action( 'replace_editor', function ( $res, WP_Post $post ) { @@ -44,6 +53,15 @@ function add_admin_menu() { } function load_settings_page() { + if ( ! isset( $_POST['action'] ) ) { + return; + } + + switch ( $_POST['action'] ) { + case ACTION_IMPORT: + on_import(); + break; + } } function render_settings_page() { @@ -115,6 +133,8 @@ function render_settings_page() { +
+

@@ -122,10 +142,71 @@ function render_settings_page() {

+ +

+

+

Note: Registering a single DID with multiple sites may break your DID.', 'minifair' ) ); ?>

+
+

+ +

+ + + 'error', + ] + ); + return; + } + + try { + $did = DID::import( $data ); + wp_redirect( get_edit_post_link( $did->get_internal_post_id(), 'raw' ) ); + exit; + } catch ( Exception $e ) { + wp_admin_notice( + sprintf( + __( 'Could not import DID: %s', 'minifair' ), + $e->getMessage() + ), + [ + 'type' => 'error', + ] + ); + } +} + function fetch_did( DID $did ) { $url = DID::DIRECTORY_API . '/' . $did->id; $res = MiniFAIR\get_remote_url( $url ); @@ -137,6 +218,10 @@ function fetch_did( DID $did ) { } function render_editor() { + if ( isset( $_POST['action'] ) && $_POST['action'] === ACTION_CREATE ) { + on_create(); + } + require_once ABSPATH . 'wp-admin/admin-header.php'; echo '
'; @@ -155,27 +240,37 @@ function render_editor() { } } -function render_new_page( WP_Post $post ) { +function on_create() { + check_admin_referer( NONCE_PREFIX . ACTION_CREATE ); + // Check user permissions. if ( ! current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have sufficient permissions to access this page.', 'minifair' ) ); } - if ( isset( $_POST['action'] ) ) { - if ( $_POST['action'] !== 'create' ) { - wp_die( __( 'Invalid action.', 'minifair' ) ); - } - - check_admin_referer( NONCE_CREATE_ACTION ); + // Handle the form submission to create a new PLC DID. + $did = DID::create(); + if ( is_wp_error( $did ) ) { + wp_admin_notice( + sprintf( + __( 'Could not create DID: %s', 'minifair' ), + $did->get_error_message() + ), + [ + 'type' => 'error', + 'additional_classes' => [ 'notice-alt' ], + ] + ); + } else { + wp_redirect( get_edit_post_link( $did->get_internal_post_id(), 'raw' ) ); + exit; + } +} - // Handle the form submission to create a new PLC DID. - $did = DID::create(); - if ( is_wp_error( $did ) ) { - echo '

' . esc_html( $did->get_error_message() ) . '

'; - } else { - wp_redirect( get_edit_post_link( $did->get_internal_post_id(), 'raw' ) ); - exit; - } +function render_new_page( WP_Post $post ) { + // Check user permissions. + if ( ! current_user_can( 'manage_options' ) ) { + wp_die( __( 'You do not have sufficient permissions to access this page.', 'minifair' ) ); } ?> @@ -183,9 +278,9 @@ function render_new_page( WP_Post $post ) {

- + - +