From d89fa61ff7fa82468af4630c2dd20f617fb775dd Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Mon, 6 Jan 2025 18:07:15 +0100 Subject: [PATCH] Add more functions for a browser extension (#427) --- friends-admin.css | 6 +++ friends-admin.js | 12 ++++++ includes/class-admin.php | 62 +++++++++++++++++++++++---- includes/class-rest.php | 32 ++++++++++++++ includes/class-user.php | 2 +- templates/admin/browser-extension.php | 27 ++++++++++++ templates/admin/settings-header.php | 4 ++ 7 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 templates/admin/browser-extension.php diff --git a/friends-admin.css b/friends-admin.css index 48cee004..865d2cbd 100644 --- a/friends-admin.css +++ b/friends-admin.css @@ -188,6 +188,12 @@ div.button.unfriend { border-color: #b00; } +.button.button-destructive { + border-color: #b00; + color: #a00; + text-decoration: none; +} + div.button.unfriend:hover { border-color: #a00; } diff --git a/friends-admin.js b/friends-admin.js index 54684d8a..9d398406 100644 --- a/friends-admin.js +++ b/friends-admin.js @@ -402,6 +402,18 @@ jQuery( function ( $ ) { setInterval( updateDashboardWidgets, 60000 ); } + $( document ).on( 'click', '#copy-api-key', function ( e ) { + e.preventDefault(); + const $this = $( this ); + const $input = $( '#api-key' ); + navigator.clipboard.writeText( $input.val() ); + $this.text( friends.copied_text ); + setTimeout( function () { + $this.text( friends.copy_text ); + }, 2000 ); + return false; + } ); + setTimeout( function () { if ( $( '#fetch-feeds' ).length ) { $( '#fetch-feeds' ).append( ' ' ); diff --git a/includes/class-admin.php b/includes/class-admin.php index 1250e5d3..164ebb2a 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -204,6 +204,11 @@ public function admin_menu() { add_submenu_page( 'friends', $title, $title, $required_role, 'friends-logs', array( $this, 'render_friends_logs' ) ); } + if ( isset( $_GET['page'] ) && 'friends-browser-extension' === $_GET['page'] ) { + $title = __( 'Browser Extension', 'friends' ); + add_submenu_page( 'friends', $title, $title, $required_role, 'friends-browser-extension', array( $this, 'render_browser_extension' ) ); + } + if ( isset( $_GET['page'] ) && 'unfriend' === $_GET['page'] ) { $user = new User( intval( $_GET['user'] ) ); if ( $user ) { @@ -310,6 +315,8 @@ public function admin_enqueue_scripts() { 'ajax_url' => admin_url( 'admin-ajax.php' ), 'add_friend_url' => self_admin_url( 'admin.php?page=add-friend' ), 'add_friend_text' => __( 'Add a Friend', 'friends' ), + 'copy_text' => __( 'Copy', 'friends' ), + 'copied_text' => __( 'Copied!', 'friends' ), 'delete_feed_question' => __( 'Delete the feed? You need to click "Save Changes" to really delete it.', 'friends' ), 'role_friend' => __( 'Friend', 'friends' ), 'role_acquaintance' => __( 'Acquaintance', 'friends' ), @@ -667,7 +674,6 @@ public function render_admin_home() { null, array( 'active' => 'friends', - 'title' => __( 'Friends', 'friends' ), ) ); @@ -685,7 +691,6 @@ public function render_admin_settings() { null, array( 'active' => 'friends-settings', - 'title' => __( 'Friends', 'friends' ), ) ); $this->check_admin_settings(); @@ -946,7 +951,6 @@ public function render_friends_list() { __( 'Your Friend Requests', 'friends' ) => 'friends-list-requests', ), 'active' => $page, - 'title' => __( 'Friends', 'friends' ), ) ); @@ -2734,22 +2738,64 @@ public function render_admin_duplicate_remover() { Friends::template_loader()->get_template_part( 'admin/duplicates', null, $args ); } + public function render_browser_extension() { + add_filter( + 'friends_admin_tabs', + function ( $menu ) { + $menu[ __( 'Browser Extension', 'friends' ) ] = 'friends-browser-extension'; + return $menu; + } + ); + Friends::template_loader()->get_template_part( + 'admin/settings-header', + null, + array( + 'active' => 'friends-browser-extension', + ) + ); + $this->check_admin_settings(); + $browser_api_key = get_option( 'friends_browser_api_key' ); + + if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'friends-browser-extension' ) ) { + if ( isset( $_POST['revoke-api-key'] ) ) { + $browser_api_key = false; + } + } + + if ( ! $browser_api_key ) { + $browser_api_key = wp_generate_password( 32, false ); + update_option( 'friends_browser_api_key', $browser_api_key ); + } + + Friends::template_loader()->get_template_part( + 'admin/browser-extension', + null, + array( + 'browser-api-key' => $browser_api_key, + ) + ); + + Friends::template_loader()->get_template_part( 'admin/settings-footer' ); + } public function render_friends_logs() { + add_filter( + 'friends_admin_tabs', + function ( $menu ) { + $menu[ __( 'Logs', 'friends' ) ] = 'friends-logs'; + return $menu; + } + ); + Friends::template_loader()->get_template_part( 'admin/settings-header', null, array( 'active' => 'friends-logs', - 'title' => __( 'Friends', 'friends' ), ) ); $this->check_admin_settings(); - ?> -

- get_template_part( 'admin/logs', null, diff --git a/includes/class-rest.php b/includes/class-rest.php index ce2939e4..34989f0b 100644 --- a/includes/class-rest.php +++ b/includes/class-rest.php @@ -188,6 +188,22 @@ public function add_rest_routes() { }, ) ); + + register_rest_route( + self::PREFIX, + 'extension', + array( + 'methods' => array( 'GET', 'POST' ), + 'callback' => array( $this, 'rest_extension' ), + 'permission_callback' => '__return_true', // Public. + 'params' => array( + 'key' => array( + 'type' => 'string', + 'required' => false, + ), + ), + ) + ); } /** @@ -654,7 +670,23 @@ function ( &$feed ) { ); } + public function rest_extension( $request ) { + $return = array( + 'version' => Friends::VERSION, + 'friends_url' => home_url( '/friends/' ), + 'settings_url' => admin_url( 'admin.php?page=friends-browser-extension' ), + ); + + if ( 'POST' === $request->get_method() && $request->get_param( 'key' ) ) { + if ( $request->get_param( 'key' ) === get_option( 'friends_browser_api_key' ) ) { + $return = apply_filters( 'friends_browser_extension_rest_info', $return ); + } else { + $return['error'] = 'Invalid API key'; + } + } + return $return; + } /** diff --git a/includes/class-user.php b/includes/class-user.php index de12a70f..3e2d6cb7 100644 --- a/includes/class-user.php +++ b/includes/class-user.php @@ -1236,7 +1236,7 @@ public function set_starred( $starred ) { */ public function get_local_friends_page_url( $post_id = null ) { $path = '/'; - if ( $post_id ) { + if ( $post_id && ! is_wp_error( $post_id ) ) { $path = '/' . $post_id . '/'; } diff --git a/templates/admin/browser-extension.php b/templates/admin/browser-extension.php new file mode 100644 index 00000000..9be32f7a --- /dev/null +++ b/templates/admin/browser-extension.php @@ -0,0 +1,27 @@ +
+ + + + + + + + +
+ + + + + + +

+
+
diff --git a/templates/admin/settings-header.php b/templates/admin/settings-header.php index f09b6ea3..4b107b6c 100644 --- a/templates/admin/settings-header.php +++ b/templates/admin/settings-header.php @@ -22,6 +22,10 @@ $args['active'] = false; } +if ( empty( $args['title'] ) ) { + $args['title'] = __( 'Friends', 'friends' ); +} + ?>