Skip to content

Commit

Permalink
Add more functions for a browser extension (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk authored Jan 6, 2025
1 parent 06fdf72 commit d89fa61
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 9 deletions.
6 changes: 6 additions & 0 deletions friends-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions friends-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( ' <i class="friends-loading"></i>' );
Expand Down
62 changes: 54 additions & 8 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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' ),
Expand Down Expand Up @@ -667,7 +674,6 @@ public function render_admin_home() {
null,
array(
'active' => 'friends',
'title' => __( 'Friends', 'friends' ),
)
);

Expand All @@ -685,7 +691,6 @@ public function render_admin_settings() {
null,
array(
'active' => 'friends-settings',
'title' => __( 'Friends', 'friends' ),
)
);
$this->check_admin_settings();
Expand Down Expand Up @@ -946,7 +951,6 @@ public function render_friends_list() {
__( 'Your Friend Requests', 'friends' ) => 'friends-list-requests',
),
'active' => $page,
'title' => __( 'Friends', 'friends' ),
)
);

Expand Down Expand Up @@ -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();

?>
<h1><?php esc_html_e( 'Logs', 'friends' ); ?></h1>
<?php

Friends::template_loader()->get_template_part(
'admin/logs',
null,
Expand Down
32 changes: 32 additions & 0 deletions includes/class-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
),
)
);
}

/**
Expand Down Expand Up @@ -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;
}


/**
Expand Down
2 changes: 1 addition & 1 deletion includes/class-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '/';
}

Expand Down
27 changes: 27 additions & 0 deletions templates/admin/browser-extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This template contains the browser extension settings
*
* @version 1.0
* @package Friends
*/

?><form method="post">
<?php wp_nonce_field( 'friends-browser-extension' ); ?>
<table class="form-table">
<tbody>
<tr>
<th><label for="api-key"><?php esc_html_e( 'API Key', 'friends' ); ?></label></th>
<td>
<input type="text" id="api-key" value="<?php echo esc_attr( $args['browser-api-key'] ); ?>" class="regular-text" readonly />
<button class="button" id="copy-api-key"><?php esc_html_e( 'Copy', 'friends' ); ?></button>
<!-- Revoke API Key -->
<?php if ( ! empty( $args['browser-api-key'] ) ) : ?>
<button class="button button-destructive" name="revoke-api-key"><?php esc_html_e( 'Revoke', 'friends' ); ?></button>
<?php endif; ?>
<p class="description"><?php esc_html_e( 'With this API key, more features will be enabled in the browser extension.', 'friends' ); ?></p>
</td>
</tr>
</tbody>
</table>
</form>
4 changes: 4 additions & 0 deletions templates/admin/settings-header.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
$args['active'] = false;
}

if ( empty( $args['title'] ) ) {
$args['title'] = __( 'Friends', 'friends' );
}

?>
<div class="wrap">
<div class="friends-header">
Expand Down

0 comments on commit d89fa61

Please sign in to comment.