Making WordPress.org

Changeset 4332


Ignore:
Timestamp:
11/06/2016 09:35:21 PM (8 years ago)
Author:
jmdodd
Message:

Support Forums: Add subscription management to user profiles.

File:
1 edited

Legend:

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

    r4048 r4332  
    2121            'taxonomy' => 'topic-tag',
    2222            'labels'   => array(
    23                 'receipt' => __( 'You are receiving this email because you are subscribed to a topic tag.', 'wporg-forums'),
     23                'subscribed_header'      => __( 'Subscribed Topic Tags', 'wporg-forums' ),
     24                'subscribed_user_notice' => __( 'You are not currently subscribed to any topic tags.', 'wporg-forums' ),
     25                'subscribed_anon_notice' => __( 'This user is not currently subscribed to any topic tags.', 'wporg-forums' ),
     26                'receipt'                => __( 'You are receiving this email because you are subscribed to a topic tag.', 'wporg-forums'),
    2427            ),
    2528        ) );
     
    5053        add_action( 'bbp_new_topic', array( $this, 'notify_term_subscribers_of_new_topic' ), 10, 4 );
    5154        add_action( 'bbp_new_reply', array( $this, 'notify_term_subscribers_of_new_reply' ), 10, 5 );
     55
     56        add_action( 'bbp_template_after_user_subscriptions', array( $this, 'user_subscriptions' ) );
    5257    }
    5358
     
    123128        }
    124129
     130        // Redirect
     131        if ( bbp_is_subscriptions() ) {
     132            $redirect = bbp_get_subscriptions_permalink( $user_id );
     133        } else {
     134            $redirect = get_term_link( $term_id );
     135        }
     136
    125137        // Success!
    126138        if ( true === $success ) {
    127             $redirect = get_term_link( $term_id );
    128139            bbp_redirect( $redirect );
    129140        } elseif ( true === $is_subscribed && 'wporg_bbp_subscribe_term' === $action ) {
     
    327338
    328339    /**
     340     * Add a term subscription block to the user's profile.
     341     */
     342    public function user_subscriptions() {
     343        $user_id = bbp_get_user_id();
     344        if ( empty( $user_id ) ) {
     345            return;
     346        }
     347        $terms = self::get_user_taxonomy_subscriptions( $user_id, $this->taxonomy );
     348        ?>
     349
     350        <div class="bbp-user-subscriptions">
     351            <h2 class="entry-title"><?php echo esc_html( $this->labels['subscribed_header'] ); ?></h2>
     352            <div class="bbp-user-section">
     353            <?php
     354            if ( $terms ) {
     355                echo '<p id="bbp-term-' . esc_attr( $this->taxonomy ) . '">' . "\n";
     356                foreach ( $terms as $term ) {
     357                    echo '<a href="' . esc_url( get_term_link( $term->term_id ) ) . '">' . esc_html( $term->slug ) . '</a>';
     358                    if ( get_current_user_id() == $user_id ) {
     359                        $url = self::get_subscription_url( $user_id, $term->term_id, $this->taxonomy );
     360                        echo ' (<a href="' . esc_url( self::get_subscription_url( $user_id, $term->term_id, $this->taxonomy ) ) . '">' . esc_html( 'Unsubscribe', 'wporg-forums' ) . '</a>)';
     361                    }
     362                    echo "</br>\n";
     363                }
     364                echo "</p>\n";
     365            } else {
     366                if ( bbp_get_user_id() == get_current_user_id() ) {
     367                    echo '<p>' . esc_html( $this->labels['subscribed_user_notice'] ) . '</p>';
     368                } else {
     369                    echo '<p>' . esc_html( $this->labels['subscribed_anon_notice'] ) . '</p>';
     370                }
     371            }
     372            ?>
     373            </div>
     374        </div>
     375
     376        <?php
     377    }
     378
     379    /**
    329380     * Get the user's subscriptions for a given taxonomy; defaults to 'topic-tag'.
    330381     *
     
    434485
    435486    /**
    436      * Create the link for subscribing to/unsubscribing from a given term.
    437      *
    438      * @param string $action The action
     487     * Create the url for subscribing to/unsubscribing from a given term.
     488     *
    439489     * @param int $user_id The user id
    440490     * @param int $term_id The term id
     491     * @param int $taxonomy The taxonomy
    441492     * @return string
    442493     */
    443     public static function get_subscription_link( $args = array() ) {
     494    public static function get_subscription_url( $user_id = 0, $term_id, $taxonomy ) {
     495        if ( empty( $user_id ) ) {
     496            $user_id = bbp_get_user_id();
     497        }
     498
     499        if ( empty( $user_id ) ) {
     500            return false;
     501        }
     502
     503        $term = get_term( $term_id, $taxonomy );
     504        if ( ! $term ) {
     505            return false;
     506        }
     507
     508        if ( self::is_user_subscribed_to_term( $user_id, $term_id ) ) {
     509            $query_args = array( 'action' => 'wporg_bbp_unsubscribe_term', 'term_id' => $term_id, 'taxonomy' => $taxonomy );
     510        } else {
     511            $query_args = array( 'action' => 'wporg_bbp_subscribe_term', 'term_id' => $term_id, 'taxonomy' => $taxonomy );
     512        }
     513
     514        if ( bbp_is_subscriptions() ) {
     515            $permalink = bbp_get_subscriptions_permalink( $user_id );
     516        } else {
     517            $permalink = get_term_link( $term_id );
     518        }
     519
     520        $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-term-subscription_' . $user_id . '_' . $term_id . '_' . $taxonomy ) );
     521        return $url;
     522    }
     523
     524    public static function get_subscription_link( $args ) {
    444525        if ( ! current_user_can( 'spectate' ) ) {
    445526            return false;
     
    456537            return false;
    457538        }
     539
    458540        $user_id  = $r['user_id'];
    459541        $term_id  = $r['term_id'];
    460542        $taxonomy = $r['taxonomy'];
    461543
    462         $term = get_term( $term_id, $taxonomy );
    463         if ( ! $term ) {
    464             return false;
    465         }
    466 
    467         if ( self::is_user_subscribed_to_term( $user_id, $term_id ) ) {
     544        $url = self::get_subscription_url( $r['user_id'], $r['term_id'], $r['taxonomy'] );
     545        $text = $r['subscribe'];
     546        if ( self::is_user_subscribed_to_term( $r['user_id'], $r['term_id'] ) ) {
    468547            $text = $r['unsubscribe'];
    469             $query_args = array( 'action' => 'wporg_bbp_unsubscribe_term', 'term_id' => $term_id, 'taxonomy' => $taxonomy );
    470         } else {
    471             $text = $r['subscribe'];
    472             $query_args = array( 'action' => 'wporg_bbp_subscribe_term', 'term_id' => $term_id, 'taxonomy' => $taxonomy );
    473         }
    474 
    475         $permalink = get_term_link( $term_id );
    476 
    477         $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-term-subscription_' . $user_id . '_' . $term_id . '_' . $taxonomy ) );
     548        }
    478549        return sprintf( "<div class='wporg-bbp-term-subscription'><a href='%s'>%s</a></div>",
    479550            $url,
Note: See TracChangeset for help on using the changeset viewer.