Making WordPress.org


Ignore:
Timestamp:
02/08/2022 07:19:44 AM (3 years ago)
Author:
dd32
Message:

Support Forums: Add a notice above a forum if you're subscribed to it.

This also adds a notice that you've successfully unsubscribed from the item too.

See #3456.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-term-subscription/inc/class-plugin.php

    r11528 r11529  
    6464        add_action( 'bbp_template_after_user_subscriptions', array( $this, 'user_subscriptions' ) );
    6565
     66        // Add a banner above the 'forum' about being subscribed to it.
     67        add_action( 'bbp_template_before_topics_index', array( $this, 'before_view' ) );
     68        add_action( 'wporg_compat_before_single_view',  array( $this, 'before_view' ) );
     69    }
     70
     71    /**
     72     * Add a notice that you're subscribed to the tag/plugin/theme, or have just unsubscribed.
     73     */
     74    public function before_view() {
     75        $term = false;
     76
     77        if ( $this->directory && $this->directory->slug && $this->directory->term ) {
     78            $term      = $this->directory->term;
     79            $term_name = $this->directory->title();
     80        } elseif ( bbp_is_topic_tag() ) {
     81            $term = get_queried_object();
     82            if ( empty( $term->taxonomy ) || $term->taxonomy !== $this->taxonomy ) {
     83                return;
     84            }
     85
     86            $term_name = $term->name;
     87        }
     88
     89        if ( empty( $term->term_id ) ) {
     90            return;
     91        }
     92
     93        $is_subscribed = self::is_user_subscribed_to_term( get_current_user_id(), $term->term_id );
     94
     95        if ( $is_subscribed ) {
     96            $message = sprintf(
     97                __( 'You are subscribed to this forum, and will recieve emails for future topic activity. <a href="%1$s">Unsubscribe from %2$s</a>', 'wporg-forums' ),
     98                self::get_subscription_url( get_current_user_id(), $term->term_id, $this->taxonomy ),
     99                esc_html( $term_name )
     100            );
     101        } elseif ( ! empty( $_GET['success'] ) && 'subscription-removed' === $_GET['success'] ) {
     102            $message = sprintf(
     103                __( 'You have been unsubscribed from future emails for %1$s.', 'wporg-forums' ),
     104                esc_html( $term_name )
     105            );
     106        } else {
     107            return;
     108        }
     109
     110        echo '<div class="notice notice-info notice-alt with-dashicon">';
     111        echo '<span class="dashicons dashicons-email-alt"></span>';
     112        echo "<p>{$message}</p>";
     113        echo '</div>';
    66114    }
    67115
     
    129177
    130178        $is_subscribed = self::is_user_subscribed_to_term( $user_id, $term_id );
    131         $success = false;
     179        $success       = false;
    132180
    133181        if ( true === $is_subscribed && 'wporg_bbp_unsubscribe_term' === $action ) {
     
    146194        // Success!
    147195        if ( true === $success ) {
     196            if ( 'wporg_bbp_unsubscribe_term' === $action ) {
     197                $redirect = add_query_arg( 'success', 'subscription-removed', $redirect );
     198            }
     199
    148200            bbp_redirect( $redirect );
    149201        } elseif ( true === $is_subscribed && 'wporg_bbp_subscribe_term' === $action ) {
     
    423475                    if ( get_current_user_id() == $user_id ) {
    424476                        $url = self::get_subscription_url( $user_id, $term->term_id, $this->taxonomy );
    425                         echo ' (<a href="' . esc_url( self::get_subscription_url( $user_id, $term->term_id, $this->taxonomy ) ) . '">' . esc_html( 'Unsubscribe', 'wporg-forums' ) . '</a>)';
     477                        echo ' (<a href="' . esc_url( $url ) . '">' . esc_html( 'Unsubscribe', 'wporg-forums' ) . '</a>)';
    426478                    }
    427479                    echo "</br>\n";
Note: See TracChangeset for help on using the changeset viewer.