Skip to content

Commit

Permalink
Remember open state of widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Nov 21, 2024
1 parent 4e0a95d commit 5beec0a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 6 deletions.
13 changes: 13 additions & 0 deletions friends.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,4 +721,17 @@
return false;
} );

$document.on( 'click', '.friends-widget summary', function () {
const $this = $( this ).closest( 'details' );
const previously_open = $this.prop( 'open' );
wp.ajax.send( 'friends-set-widget-open-state', {
data: {
_ajax_nonce: $this.data( 'nonce' ),
widget: $this.data( 'id' ),
state: previously_open ? 'closed' : 'open',
},
success() {},
} );
} );

} )( jQuery, window.wp, window.friends );
33 changes: 33 additions & 0 deletions includes/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private function register_hooks() {
add_action( 'wp_ajax_friends-change-post-format', array( $this, 'ajax_change_post_format' ) );
add_action( 'wp_ajax_friends-load-next-page', array( $this, 'ajax_load_next_page' ) );
add_action( 'wp_ajax_friends-autocomplete', array( $this, 'ajax_autocomplete' ) );
add_action( 'wp_ajax_friends-set-widget-open-state', array( $this, 'ajax_set_widget_open_state' ) );
add_action( 'friends_search_autocomplete', array( $this, 'autocomplete_user_search' ), 10, 2 );
add_action( 'wp_ajax_friends-star', array( $this, 'ajax_star_friend_user' ) );
add_action( 'wp_ajax_friends-load-comments', array( $this, 'ajax_load_comments' ) );
Expand Down Expand Up @@ -932,6 +933,38 @@ public static function get_link( $url, $text, array $html_attributes = array(),
return $link;
}

public static function get_widget_open_state( $widget ) {
static $state = null;
if ( is_null( $state ) ) {
$state = get_user_meta( get_current_user_id(), 'friends_widget_state', true );
if ( ! is_array( $state ) ) {
$state = array();
}
}
return isset( $state[ $widget ] ) && is_string( $state[ $widget ] ) ? $state[ $widget ] : 'open';
}

public function ajax_set_widget_open_state() {
if ( ! isset( $_POST['widget'] ) || ! isset( $_POST['state'] ) ) {
wp_send_json_error();
exit;
}

check_ajax_referer( 'friends_widget_state' );

$widget = sanitize_text_field( wp_unslash( $_POST['widget'] ) );
$open_state = sanitize_text_field( wp_unslash( $_POST['state'] ) );

$state = get_user_meta( get_current_user_id(), 'friends_widget_state', true );
if ( ! is_array( $state ) ) {
$state = array();
}
$state[ $widget ] = $open_state;
update_user_meta( get_current_user_id(), 'friends_widget_state', $state );

wp_send_json_success();
}

/**
* Don't show the edit link for friend posts.
*
Expand Down
3 changes: 2 additions & 1 deletion widgets/class-widget-base-friends-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public function __construct( $id_base, $name, $widget_options = array(), $contro
* @param \WP_User_Query $friends The friends to list.
*/
public function list_friends( $args, $title, \WP_User_Query $friends ) {
$open = Frontend::get_widget_open_state( $args['widget_id'] );
?>
<details class="accordion">
<details class="accordion" <?php echo esc_attr( $open ); ?> data-id="<?php echo esc_attr( $args['widget_id'] ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'friends_widget_state' ) ); ?>">
<summary class="accordion-header">
<?php
echo $args['before_title'];
Expand Down
4 changes: 3 additions & 1 deletion widgets/class-widget-friend-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ public function widget( $args, $instance ) {
$show_followers = true;
}
echo $args['before_widget'];

$open = Frontend::get_widget_open_state( $args['widget_id'] );
?>
<details class="accordion">
<details class="accordion" <?php echo esc_attr( $open ); ?> data-id="<?php echo esc_attr( $args['widget_id'] ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'friends_widget_state' ) ); ?>">
<summary class="accordion-header">
<?php
echo $args['before_title'];
Expand Down
21 changes: 18 additions & 3 deletions widgets/class-widget-friends-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ public function widget( $args, $instance ) {
if ( $all_friends->get_total() > 0 || ( ! $friend_requests->get_total() && ! $subscriptions->get_total() ) ) {
echo $args['before_widget'];
$this->list_friends(
$args,
array_merge(
array(
'widget_id' => $args['widget_id'] . '-all',
),
$args
),
$friends_title,
$all_friends
);
Expand All @@ -66,7 +71,12 @@ public function widget( $args, $instance ) {
if ( $friend_requests->get_total() > 0 ) {
echo $args['before_widget'];
$this->list_friends(
$args,
array_merge(
array(
'widget_id' => $args['widget_id'] . '-requests',
),
$args
),
// translators: %1$s is the string "%s Friend", %2$s is a URL, %3$s is the number of open friend requests.
sprintf( _n( '%1$s <a href=%2$s>(%3$s request)</a>', '%1$s <a href=%2$s>(%3$s requests)</a>', $friend_requests->get_total(), 'friends' ), $friends_title, '"' . esc_attr( self_admin_url( 'users.php?role=friend_request' ) ) . '" class="open-requests"', $friend_requests->get_total() ),
$friend_requests
Expand All @@ -77,7 +87,12 @@ public function widget( $args, $instance ) {
if ( 0 !== $subscriptions->get_total() ) {
echo $args['before_widget'];
$this->list_friends(
$args,
array_merge(
array(
'widget_id' => $args['widget_id'] . '-subscriptions',
),
$args
),
'<span class="dashicons dashicons-admin-users"></span> ' . sprintf(
// translators: %s is the number of subscriptions.
_n( 'Subscription %s', 'Subscriptions %s', $subscriptions->get_total(), 'friends' ),
Expand Down
3 changes: 2 additions & 1 deletion widgets/class-widget-post-formats.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public function widget( $args, $instance ) {
$friends = Friends::get_instance();

echo wp_kses( $args['before_widget'], 'post' );
$open = Frontend::get_widget_open_state( $args['widget_id'] );
?>
<details class="accordion">
<details class="accordion" <?php echo esc_attr( $open ); ?> data-id="<?php echo esc_attr( $args['widget_id'] ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'friends_widget_state' ) ); ?>">
<summary class="accordion-header">
<?php
if ( ! empty( $instance['title'] ) ) {
Expand Down

0 comments on commit 5beec0a

Please sign in to comment.