Skip to:
Content

bbPress.org

Changeset 6544


Ignore:
Timestamp:
06/14/2017 06:45:49 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Performance: Last pass at 2.6 performance tuning.

  • Keep a local cache of non-options that aren't in the database, to avoid multiple database misses for options we know aren't in the database after wp_load_alloptions() is called
  • Stop getting all favorite IDs and subscription IDs when checking if a user has favorited or subscribed to something, because these queries are expensive joins that are difficult to cache & invalidate
  • Consolidate forum/topic favorites & subscriptions logic back into central routers, to make it easier to handle taxonomy term subscriptions in the future, and remove nested filter calls that make the call-stack confusing
  • Informally deprecate some forum & topic specific fav/sub functions
  • Rename one of the engagements remove functions to better match existing naming pattern
  • Typo fixes & general code quality improvements
  • Bump slow tests threshold up to 500 for now, and we can bring back down later (my 12" MacBook runs this pretty slowly, so I'd like to play with this more)
  • Unit tests are all passing, and more quickly, maybe surprisingly

This should result in around 20 fewer database hits on non-persistent-cache sites, on average. When viewing single topics & replies, this will result in more than 25 fewer database hits.

Location:
trunk
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/phpunit.xml.dist

    r6025 r6544  
    2222                <array>
    2323                    <element key="slowThreshold">
    24                         <integer>100</integer>
     24                        <integer>500</integer>
    2525                    </element>
    2626                </array>
  • trunk/src/bbpress.php

    r6542 r6544  
    7373     */
    7474    public $options = array();
     75
     76    /**
     77     * @var array Storage of options not in the database
     78     */
     79    public $not_options = array();
    7580
    7681    /**
  • trunk/src/includes/admin/classes/class-bbp-converter-base.php

    r6514 r6544  
    518518                                        // Add user ID to forums subscribed users
    519519                                        foreach ( $value as $fav ) {
    520                                             bbp_add_user_forum_subscription( $user_id, $this->callback_forumid( $fav ) );
     520                                            bbp_add_user_subscription( $user_id, $this->callback_forumid( $fav ) );
    521521                                        }
    522522                                    }
     
    539539                                        // Add user ID to topics subscribed users
    540540                                        foreach ( $value as $fav ) {
    541                                             bbp_add_user_topic_subscription( $user_id, $this->callback_topicid( $fav ) );
     541                                            bbp_add_user_subscription( $user_id, $this->callback_topicid( $fav ) );
    542542                                        }
    543543                                    }
  • trunk/src/includes/admin/settings.php

    r6514 r6544  
    433433
    434434            // Engagements slug setting
    435             '_bbp_user_engagements_slug' => array(
     435            '_bbp_user_engs_slug' => array(
    436436                'title'             => esc_html__( 'Engagements', 'bbpress' ),
    437437                'callback'          => 'bbp_admin_setting_callback_user_engagements_slug',
    438438                'sanitize_callback' => 'bbp_sanitize_slug',
    439                 'args'              => array( 'label_for'=>'_bbp_user_engagements_slug' )
     439                'args'              => array( 'label_for'=>'_bbp_user_engs_slug' )
    440440            )
    441441        ),
     
    12881288?>
    12891289
    1290     <input name="_bbp_user_engagements_slug" id="_bbp_user_engagements_slug" type="text" class="regular-text code" value="<?php bbp_form_option( '_bbp_user_engagements_slug', 'engagements', true ); ?>"<?php bbp_maybe_admin_setting_disabled( '_bbp_user_engagements_slug' ); ?> />
     1290    <input name="_bbp_user_engs_slug" id="_bbp_user_engs_slug" type="text" class="regular-text code" value="<?php bbp_form_option( '_bbp_user_engs_slug', 'engagements', true ); ?>"<?php bbp_maybe_admin_setting_disabled( '_bbp_user_engs_slug' ); ?> />
    12911291
    12921292<?php
    12931293    // Slug Check
    1294     bbp_form_slug_conflict_check( '_bbp_user_engagements_slug', 'engagements' );
     1294    bbp_form_slug_conflict_check( '_bbp_user_engs_slug', 'engagements' );
    12951295}
    12961296
  • trunk/src/includes/admin/tools/repair.php

    r6537 r6544  
    626626
    627627/**
    628  * Clean the users' topic subscriptions
     628 * Clean the user topic subscriptions
    629629 *
    630630 * @since 2.0.0 bbPress (r2668)
     
    695695
    696696/**
    697  * Clean the users' forum subscriptions
     697 * Clean the user forum subscriptions
    698698 *
    699699 * @since 2.5.0 bbPress (r5155)
  • trunk/src/includes/core/actions.php

    r6534 r6544  
    6868add_action( 'bbp_loaded', 'bbp_setup_option_filters',      10 );
    6969add_action( 'bbp_loaded', 'bbp_setup_user_option_filters', 12 );
    70 add_action( 'bbp_loaded', 'bbp_register_theme_packages',   14 );
     70add_action( 'bbp_loaded', 'bbp_pre_load_options',          14 );
     71add_action( 'bbp_loaded', 'bbp_register_theme_packages',   16 );
    7172
    7273/**
     
    427428add_action( 'bbp_get_request', 'bbp_subscriptions_handler',       1  );
    428429add_action( 'bbp_get_request', 'bbp_user_email_change_handler',   1  );
    429 add_action( 'bbp_get_request', 'bbp_forum_subscriptions_handler', 1  );
    430430add_action( 'bbp_get_request', 'bbp_search_results_redirect',     10 );
    431431
  • trunk/src/includes/core/options.php

    r6514 r6544  
    2323
    2424    // Filter & return
    25     return apply_filters( 'bbp_get_default_options', array(
     25    return (array) apply_filters( 'bbp_get_default_options', array(
    2626
    2727        /** DB Version ********************************************************/
     
    8484
    8585        '_bbp_user_slug'            => 'users',         // User profile slug
     86        '_bbp_user_engs_slug'       => 'engagements',   // User engagements slug
    8687        '_bbp_user_favs_slug'       => 'favorites',     // User favorites slug
    8788        '_bbp_user_subs_slug'       => 'subscriptions', // User subscriptions slug
     
    112113
    113114        '_bbp_enable_akismet'       => 1,           // Users from all sites can post
    114        
     115
    115116        /** Converter *********************************************************/
    116117
     
    193194    // Add filters to each bbPress option
    194195    foreach ( array_keys( bbp_get_default_options() ) as $key ) {
    195         add_filter( 'pre_option_' . $key, 'bbp_pre_get_option' );
     196        add_filter( 'pre_option_'     . $key, 'bbp_filter_pre_get_option', 10, 2 );
     197        add_filter( 'default_option_' . $key, 'bbp_filter_default_option', 10, 3 );
    196198    }
    197199
     
    201203
    202204/**
    203  * Filter default options and allow them to be overloaded from inside the
    204  * $bbp->options array.
     205 * Filter pre options and maybe overloaded from the $bbp->options array.
     206 *
     207 * This function should not be called directly.
    205208 *
    206209 * @since 2.0.0 bbPress (r3451)
    207  *
    208  * @param bool $value Optional. Default value false
     210 * @access private
     211 *
     212 * @param bool   $value  Default value false
     213 * @param string $option Name of the option
     214 *
    209215 * @return mixed false if not overloaded, mixed if set
    210216 */
    211 function bbp_pre_get_option( $value = '' ) {
    212 
    213     // Remove the filter prefix
    214     $option = str_replace( 'pre_option_', '', current_filter() );
     217function bbp_filter_pre_get_option( $value = false, $option = '' ) {
    215218
    216219    // Check the options global for preset value
     
    223226}
    224227
     228/**
     229 * Filter default_options set them from inside the $bbp->options array.
     230 *
     231 * This function should not be called directly.
     232 *
     233 * @since 2.6.0 bbPress (r3451)
     234 * @access private
     235 *
     236 * @param bool $value Optional. Default value false
     237 * @return mixed false if not overloaded, mixed if set
     238 */
     239function bbp_filter_default_option( $default = false, $option = '', $passed_default = false ) {
     240    $options = bbp_get_default_options();
     241
     242    // Maybe use the default value
     243    if ( isset( $options[ $option ] ) ) {
     244
     245        // Try to use the passed default and fallback to assumed default
     246        $default = ( true === $passed_default )
     247            ? $default
     248            : $options[ $option ];
     249    }
     250
     251    // Always return a value, even if false
     252    return $default;
     253}
     254
     255/**
     256 * Loads & caches bbPress options if a persistent cache is not being used.
     257 *
     258 * @since 2.6.0
     259 */
     260function bbp_pre_load_options() {
     261
     262    // Bail if using object cache or installing
     263    if ( wp_using_ext_object_cache() || wp_installing() ) {
     264        return;
     265    }
     266
     267    // Bail if strategy is overloaded to false|null
     268    $strategy = apply_filters( 'bbp_pre_load_options', 'notoptions' );
     269    if ( empty( $strategy ) ) {
     270        return;
     271    }
     272
     273    // Get variables
     274    $bbp         = bbpress();
     275    $bbp_options = bbp_get_default_options();
     276    $all_options = wp_load_alloptions();
     277    $not_options = (array) wp_cache_get( 'notoptions', 'options' );
     278
     279    // Loop through all bbPress options to maybe cache their non-existence
     280    foreach ( $bbp_options as $option => $value ) {
     281
     282        // Skip if already saved to database
     283        if ( isset( $all_options[ $option ] ) ) {
     284            continue;
     285
     286        // Skip if overloaded
     287        } elseif ( isset( $bbp->options[ $option ] ) ) {
     288            continue;
     289
     290        // Skip if already in cache
     291        } elseif ( wp_cache_get( $option, 'options' ) !== false ) {
     292            continue;
     293
     294        // Needs caching to avoid database hit
     295        } else {
     296
     297            // Store internally, for easier identification later
     298            $bbp->not_options[ $option ] = $value;
     299
     300            // Cache to notoptions
     301            if ( 'notoptions' === $strategy ) {
     302                $not_options[ $option ] = true;
     303                wp_cache_set( 'notoptions', $not_options, 'options' );
     304
     305            // Cache to option
     306            } elseif ( 'option' === $strategy ) {
     307                wp_cache_set( $option, $value, 'options' );
     308            }
     309        }
     310    }
     311}
     312
    225313/** Active? *******************************************************************/
    226314
     
    230318 * @since 2.0.0 bbPress (r2658)
    231319 *
    232  * @param $default bool Optional.Default value true
     320 * @param bool $default Optional.Default value true
    233321 * @uses get_option() To get the favorites option
    234322 * @return bool Is favorites enabled or not
     
    245333 * @since 2.0.0 bbPress (r2658)
    246334 *
    247  * @param $default bool Optional.Default value true
     335 * @param bool $default Optional.Default value true
    248336 * @uses get_option() To get the subscriptions option
    249337 * @return bool Is subscription enabled or not
     
    260348 * @since 2.6.0 bbPress (r6320)
    261349 *
    262  * @param $default bool Optional.Default value true
     350 * @param bool $default Optional.Default value true
    263351 * @uses get_option() To get the engagements option
    264352 * @return bool Is engagements enabled or not
     
    275363 * @since 2.6.0 bbPress (r6441)
    276364 *
    277  * @param $default bool Optional. Default value false
     365 * @param bool $default Optional. Default value false
    278366 * @uses get_option() To get the global content edit option
    279367 * @return bool Is content editing allowed?
     
    290378 * @since 2.6.0 bbPress (r6441)
    291379 *
    292  * @param $default bool Optional. Default value false
     380 * @param bool $default Optional. Default value false
    293381 * @uses get_option() To get the content throttle  option
    294382 * @return bool Is content throttling allowed?
     
    305393 * @since 2.2.0 bbPress (r4097)
    306394 *
    307  * @param $default bool Optional. Default value true
     395 * @param bool $default Optional. Default value true
    308396 * @uses get_option() To get the allow tags
    309397 * @return bool Are tags allowed?
     
    336424 * @since 2.4.0 bbPress (r4970)
    337425 *
    338  * @param $default bool Optional. Default value true
     426 * @param bool $default Optional. Default value true
    339427 * @uses get_option() To get the forum-wide search setting
    340428 * @return bool Is forum-wide searching allowed?
     
    351439 * @since 2.4.0 bbPress (r4964)
    352440 *
    353  * @param $default bool Optional. Default value false
     441 * @param bool $default Optional. Default value false
    354442 * @uses get_option() To get the threaded replies setting
    355443 * @return bool Are threaded replies allowed?
     
    383471 * @since 2.0.0 bbPress (r3412)
    384472 *
    385  * @param $default bool Optional. Default value true
     473 * @param bool $default Optional. Default value true
    386474 * @uses get_option() To get the allow revisions
    387475 * @return bool Are revisions allowed?
     
    398486 * @since 2.0.0 bbPress (r2659)
    399487 *
    400  * @param $default bool Optional. Default value
     488 * @param bool $default Optional. Default value
    401489 * @uses get_option() To get the allow anonymous option
    402490 * @return bool Is anonymous posting allowed?
     
    413501 * @since 2.0.0 bbPress (r3378)
    414502 *
    415  * @param $default bool Optional. Default value false
     503 * @param bool $default Optional. Default value false
    416504 * @uses get_option() To get the global access option
    417505 * @return bool Is global access allowed?
     
    428516 * @since 2.2.0 bbPress (r4294)
    429517 *
    430  * @param $default string Optional. Default value empty
     518 * @param string $default Optional. Default value empty
    431519 * @uses get_option() To get the default forums role option
    432520 * @return string The default forums user role
     
    443531 * @since 2.0.0 bbPress (r3386)
    444532 *
    445  * @param $default bool Optional. Default value true
     533 * @param bool $default Optional. Default value true
    446534 * @uses get_option() To get the WP editor option
    447535 * @return bool Use WP editor?
     
    458546 * @since 2.1.0 bbPress (r3752)
    459547 *
    460  * @param $default bool Optional. Default value true
     548 * @param bool $default Optional. Default value true
    461549 * @uses get_option() To get the oEmbed option
    462550 * @return bool Use oEmbed?
     
    473561 * @since 2.1.0 bbPress (r3829)
    474562 *
    475  * @param $default string Optional. Default value 'default'
     563 * @param string $default Optional. Default value 'default'
    476564 * @uses get_option() To get the theme-package option
    477565 * @return string ID of the theme-package
     
    488576 * @since 2.0.0 bbPress (r3246)
    489577 *
    490  * @param $default bool Optional. Default value 80
     578 * @param bool $default Optional. Default value 80
    491579 */
    492580function bbp_title_max_length( $default = 80 ) {
     
    498586     * @since 2.0.0 bbPress (r3246)
    499587     *
    500      * @param $default bool Optional. Default value 80
     588     * @param bool $default Optional. Default value 80
    501589     * @uses get_option() To get the maximum title length
    502590     * @return int Is anonymous posting allowed?
     
    513601 * @since 2.1.0 bbPress (r3575)
    514602 *
    515  * @param $default int Optional. Default value
     603 * @param int $default Optional. Default value
    516604 */
    517605function bbp_group_forums_root_id( $default = 0 ) {
     
    523611     * @since 2.1.0 bbPress (r3575)
    524612     *
    525      * @param $default bool Optional. Default value 0
     613     * @param bool $default Optional. Default value 0
    526614     * @uses get_option() To get the root group forum ID
    527615     * @return int The post ID for the root forum
     
    538626 * @since 2.1.0 bbPress (r3575)
    539627 *
    540  * @param $default bool Optional. Default value true
     628 * @param bool $default Optional. Default value true
    541629 * @uses get_option() To get the group forums option
    542630 * @return bool Is group forums enabled or not
     
    553641 * @since 2.1.0 bbPress (r3575)
    554642 *
    555  * @param $default bool Optional. Default value true
     643 * @param bool $default Optional. Default value true
    556644 * @uses get_option() To get the Akismet option
    557645 * @return bool Is Akismet enabled or not
     
    573661 * @since 2.4.0 bbPress (r4932)
    574662 *
    575  * @param $default bool Optional. Default value false
     663 * @param bool $default Optional. Default value false
    576664 * @uses get_option() To get the admin integration setting
    577665 * @return bool To deeply integrate settings, or not
     
    605693 * @since 2.1.0 bbPress (r3759)
    606694 *
     695 * @param string $default Optional. Default value 'forums'
     696 * @uses get_option() To get the slug
    607697 * @return string
    608698 */
     
    618708 * @since 2.1.0 bbPress (r3759)
    619709 *
     710 * @param bool $default Optional. Default value true
     711 * @uses get_option() To get the setting
    620712 * @return bool
    621713 */
     
    631723 * @since 2.4.0 bbPress (r4932)
    632724 *
     725 * @param string $default Optional. Default value 'forums'
     726 * @uses get_option() To get the setting
    633727 * @return string
    634728 */
     
    644738 * @since 2.1.0 bbPress (r3759)
    645739 *
     740 * @param string $default Optional. Default value 'forums'
     741 * @uses get_option() To get the slug
    646742 * @return string
    647743 */
    648744function bbp_maybe_get_root_slug() {
    649     $retval = '';
    650 
    651     if ( bbp_get_root_slug() && bbp_include_root_slug() ) {
    652         $retval = trailingslashit( bbp_get_root_slug() );
    653     }
     745    $slug   = bbp_get_root_slug();
     746    $retval = ( ! empty( $slug ) && bbp_include_root_slug() )
     747        ? trailingslashit( $slug )
     748        : '';
    654749
    655750    // Filter & return
     
    662757 * @since 2.1.0 bbPress (r3759)
    663758 *
     759 * @param string $default Optional. Default value 'forum'
     760 * @uses get_option() To get the slug
    664761 * @return string
    665762 */
     
    675772 * @since 2.1.0 bbPress (r3759)
    676773 *
     774 * @param string $default Optional. Default value 'topics'
     775 * @uses get_option() To get the slug
    677776 * @return string
    678777 */
     
    688787 * @since 2.4.0 bbPress (r4925)
    689788 *
     789 * @param string $default Optional. Default value 'replies'
     790 * @uses get_option() To get the slug
    690791 * @return string
    691792 */
     
    701802 * @since 2.1.0 bbPress (r3759)
    702803 *
     804 * @param string $default Optional. Default value 'topic'
     805 * @uses get_option() To get the slug
    703806 * @return string
    704807 */
     
    714817 * @since 2.1.0 bbPress (r3759)
    715818 *
     819 * @param string $default Optional. Default value 'topic-tag'
     820 * @uses get_option() To get the slug
    716821 * @return string
    717822 */
     
    727832 * @since 2.1.0 bbPress (r3759)
    728833 *
     834 * @param string $default Optional. Default value 'reply'
     835 * @uses get_option() To get the slug
    729836 * @return string
    730837 */
     
    740847 * @since 2.1.0 bbPress (r3759)
    741848 *
     849 * @param string $default Optional. Default value 'users'
     850 * @uses get_option() To get the slug
    742851 * @return string
    743852 */
     
    753862 * @since 2.2.0 bbPress (r4187)
    754863 *
     864 * @param string $default Optional. Default value 'favorites'
     865 * @uses get_option() To get the slug
    755866 * @return string
    756867 */
     
    766877 * @since 2.2.0 bbPress (r4187)
    767878 *
     879 * @param string $default Optional. Default value 'subscriptions'
     880 * @uses get_option() To get the slug
    768881 * @return string
    769882 */
     
    779892 * @since 2.6.0 bbPress (r6320)
    780893 *
     894 * @param string $default Optional. Default value 'engagements'
     895 * @uses get_option() To get the slug
    781896 * @return string
    782897 */
     
    784899
    785900    // Filter & return
    786     return apply_filters( 'bbp_get_user_engagements_slug', get_option( '_bbp_user_engagements_slug', $default ) );
     901    return apply_filters( 'bbp_get_user_engagements_slug', get_option( '_bbp_user_engs_slug', $default ) );
    787902}
    788903
     
    792907 * @since 2.1.0 bbPress (r3759)
    793908 *
     909 * @param string $default Optional. Default value 'view'
     910 * @uses get_option() To get the slug
    794911 * @return string
    795912 */
     
    805922 * @since 2.3.0 bbPress (r4579)
    806923 *
     924 * @param string $default Optional. Default value 'search'
     925 * @uses get_option() To get the slug
    807926 * @return string
    808927 */
     
    820939 * @since 2.1.0 bbPress (r3790)
    821940 *
    822  * @param $default string Optional. Default empty string
     941 * @param string $default Optional. Default empty string
    823942 * @uses get_option() To get the old bb-config.php location
    824943 * @return string The location of the bb-config.php file, if any
  • trunk/src/includes/forums/functions.php

    r6534 r6544  
    10731073
    10741074/**
    1075  * Remove a deleted forum from all users' subscriptions
     1075 * Remove a deleted forum from all user subscriptions
    10761076 *
    10771077 * @since 2.5.0 bbPress (r5156)
     
    10901090    }
    10911091
     1092    // Bail if no forum
    10921093    $forum_id = bbp_get_forum_id( $forum_id );
    1093 
    1094     // Bail if no forum
    10951094    if ( empty( $forum_id ) ) {
    10961095        return;
    10971096    }
    10981097
    1099     // Get users
    1100     $users = (array) bbp_get_forum_subscribers( $forum_id );
    1101 
    1102     // Users exist
    1103     if ( ! empty( $users ) ) {
    1104 
    1105         // Loop through users
    1106         foreach ( $users as $user ) {
    1107 
    1108             // Remove each user
    1109             bbp_remove_user_subscription( $user, $forum_id );
    1110         }
    1111     }
     1098    // Remove forum from all subscriptions
     1099    return bbp_remove_object_from_all_users( $forum_id, '_bbp_subscription', 'post' );
    11121100}
    11131101
  • trunk/src/includes/forums/template.php

    r6528 r6544  
    25052505     * @uses bbp_get_global_post_field() To get current post author
    25062506     * @uses bbp_get_current_user_id() To get the current user id
    2507      * @uses bbp_is_user_subscribed_to_forum() To check if the user is
    2508      *                                          subscribed to the forum
     2507     * @uses bbp_is_user_subscribed() To check if the user is subscribed
    25092508     * @uses apply_filters() Calls 'bbp_get_form_forum_subscribed' with the
    25102509     *                option
     
    25252524            // Post author is not the current user
    25262525            if ( bbp_get_current_user_id() !== $post_author ) {
    2527                 $forum_subscribed = bbp_is_user_subscribed_to_forum( $post_author );
     2526                $forum_subscribed = bbp_is_user_subscribed( $post_author, bbp_get_forum_id() );
    25282527
    25292528            // Post author is the current user
    25302529            } else {
    2531                 $forum_subscribed = bbp_is_user_subscribed_to_forum( bbp_get_current_user_id() );
     2530                $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id(), bbp_get_forum_id() );
    25322531            }
    25332532
    25342533        // Get current status
    25352534        } elseif ( bbp_is_single_forum() ) {
    2536             $forum_subscribed = bbp_is_user_subscribed_to_forum( bbp_get_current_user_id() );
     2535            $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id(), bbp_get_forum_id() );
    25372536
    25382537        // No data
  • trunk/src/includes/search/functions.php

    r6438 r6544  
    3939
    4040/**
    41  * Return the search's query args
     41 * Return the search query args
    4242 *
    4343 * @since 2.3.0 bbPress (r4579)
  • trunk/src/includes/topics/functions.php

    r6534 r6544  
    23812381
    23822382/**
    2383  * Remove a deleted topic from all users' favorites
     2383 * Remove a deleted topic from all user favorites
    23842384 *
    23852385 * @since 2.0.0 bbPress (r2652)
     
    24142414
    24152415/**
    2416  * Remove a deleted topic from all users' subscriptions
     2416 * Remove a deleted topic from all user subscriptions
    24172417 *
    24182418 * @since 2.0.0 bbPress (r2652)
     
    24312431    }
    24322432
     2433    // Bail if no topic
    24332434    $topic_id = bbp_get_topic_id( $topic_id );
    2434 
    2435     // Bail if no topic
    24362435    if ( empty( $topic_id ) ) {
    24372436        return;
    24382437    }
    24392438
    2440     // Get users
    2441     $users = (array) bbp_get_topic_subscribers( $topic_id );
    2442 
    2443     // Users exist
    2444     if ( ! empty( $users ) ) {
    2445 
    2446         // Loop through users
    2447         foreach ( $users as $user ) {
    2448 
    2449             // Remove each user
    2450             bbp_remove_user_subscription( $user, $topic_id );
    2451         }
    2452     }
     2439    // Remove all users
     2440    return bbp_remove_object_from_all_users( $topic_id, '_bbp_subscription', 'post' );
    24532441}
    24542442
  • trunk/src/includes/topics/template.php

    r6533 r6544  
    42574257     *
    42584258     * @uses bbp_is_topic_edit() To check if it's the topic edit page
    4259      * @uses bbp_is_user_subscribed_to_topic() To check if the user is
    4260      *                                          subscribed to the topic
     4259     * @uses bbp_is_user_subscribed() To check if the user is subscribed
    42614260     * @uses apply_filters() Calls 'bbp_get_form_topic_subscribed' with the
    42624261     *                        option
     
    42774276            // Post author is not the current user
    42784277            if ( bbp_get_current_user_id() !== $post_author ) {
    4279                 $topic_subscribed = bbp_is_user_subscribed_to_topic( $post_author );
     4278                $topic_subscribed = bbp_is_user_subscribed( $post_author, bbp_get_topic_id() );
    42804279
    42814280            // Post author is the current user
    42824281            } else {
    4283                 $topic_subscribed = bbp_is_user_subscribed_to_topic( bbp_get_current_user_id() );
     4282                $topic_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id(), bbp_get_topic_id() );
    42844283            }
    42854284
    42864285        // Get current status
    42874286        } elseif ( bbp_is_single_topic() ) {
    4288             $topic_subscribed = bbp_is_user_subscribed_to_topic( bbp_get_current_user_id() );
     4287            $topic_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id(), bbp_get_topic_id() );
    42894288
    42904289        // No data
  • trunk/src/includes/users/engagements.php

    r6534 r6544  
    5757
    5858/**
    59  * Remove all users from an object
     59 * Remove a user id from all objects
     60 *
     61 * @since 2.6.0 bbPress (r6109)
     62 *
     63 * @param int    $user_id   The user id
     64 * @param string $meta_key  The relationship key
     65 * @param string $meta_type The relationship type (usually 'post')
     66 *
     67 * @uses delete_metadata() To remove user from all objects
     68 *
     69 * @return bool Returns true on success, false on failure
     70 */
     71function bbp_remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
     72    $retval = delete_metadata( $meta_type, null, $meta_key, $user_id, true );
     73
     74    // Filter & return
     75    return (bool) apply_filters( 'bbp_remove_user_from_all_objects', (bool) $retval, $user_id, $meta_key, $meta_type );
     76}
     77
     78/**
     79 * Remove an object from all users
    6080 *
    6181 * @since 2.6.0 bbPress (r6109)
     
    7090 * @return bool Returns true on success, false on failure
    7191 */
    72 function bbp_remove_all_users_from_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
     92function bbp_remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
    7393    $retval = delete_metadata( $meta_type, $object_id, $meta_key, null, false );
    7494
    7595    // Filter & return
    76     return (bool) apply_filters( 'bbp_remove_all_users_from_object', (bool) $retval, $object_id, $meta_key, $meta_type );
    77 }
    78 
    79 /**
    80  * Remove a user id from all objects
    81  *
    82  * @since 2.6.0 bbPress (r6109)
    83  *
    84  * @param int    $user_id   The user id
    85  * @param string $meta_key  The relationship key
    86  * @param string $meta_type The relationship type (usually 'post')
    87  *
    88  * @uses delete_metadata() To remove user from all objects
    89  *
    90  * @return bool Returns true on success, false on failure
    91  */
    92 function bbp_remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
    93     $retval = delete_metadata( $meta_type, null, $meta_key, $user_id, true );
    94 
    95     // Filter & return
    96     return (bool) apply_filters( 'bbp_remove_user_from_all_objects', (bool) $retval, $user_id, $meta_key, $meta_type );
     96    return (bool) apply_filters( 'bbp_remove_object_from_all_users', (bool) $retval, $object_id, $meta_key, $meta_type );
    9797}
    9898
     
    416416
    417417        // Delete all engagements
    418         bbp_remove_all_users_from_object( $topic_id, '_bbp_engagement' );
     418        bbp_remove_object_from_all_users( $topic_id, '_bbp_engagement' );
    419419
    420420        // Update the voice count for this topic id
     
    519519
    520520/**
    521  * Get a user's favorite topic ids
    522  *
    523  * @since 2.0.0 bbPress (r2652)
    524  *
    525  * @param int $user_id Optional. User id
    526  * @uses bbp_get_user_id() To get the user id
    527  * @uses bbp_get_topic_post_type() To get the topic post type
    528  * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
    529  *                        the favorites and user id
    530  * @return array|bool Results if user has favorites, otherwise null
    531  */
    532 function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
    533     $user_id   = bbp_get_user_id( $user_id );
    534     $favorites = new WP_Query( array(
    535         'fields'        => 'ids',
    536         'post_type'     => bbp_get_topic_post_type(),
    537         'nopaging'      => true,
    538         'no_found_rows' => true,
    539         'meta_query'    => array( array(
    540             'key'     => '_bbp_favorite',
    541             'value'   => $user_id,
    542             'compare' => 'NUMERIC'
    543         ) )
    544     ) );
    545 
    546     // Filter & return
    547     return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites->posts, $user_id );
    548 }
    549 
    550 /**
    551521 * Check if a topic is in user's favorites or not
    552522 *
     
    556526 * @param int $topic_id Optional. Topic id
    557527 * @uses bbp_get_user_id() To get the user id
    558  * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
    559528 * @uses bbp_get_topic() To get the topic
    560529 * @uses bbp_get_topic_id() To get the topic id
     
    565534 */
    566535function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) {
    567     $retval    = false;
    568     $user_id   = bbp_get_user_id( $user_id, true, true );
    569     $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    570 
    571     if ( ! empty( $favorites ) ) {
    572 
    573         // Checking a specific topic id
    574         if ( ! empty( $topic_id ) ) {
    575             $topic    = bbp_get_topic( $topic_id );
    576             $topic_id = ! empty( $topic ) ? $topic->ID : 0;
    577 
    578         // Using the global topic id
    579         } elseif ( bbp_get_topic_id() ) {
    580             $topic_id = bbp_get_topic_id();
    581 
    582         // Use the current post id
    583         } elseif ( ! bbp_get_topic_id() ) {
    584             $topic_id = get_the_ID();
    585         }
    586 
    587         // Is topic_id in the user's favorites
    588         if ( ! empty( $topic_id ) ) {
    589             $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_favorite' );
    590         }
    591     }
    592 
    593     // Filter & return
    594     return (bool) apply_filters( 'bbp_is_user_favorite', $retval, $user_id, $topic_id, $favorites );
     536    $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_favorite' );
     537
     538    // Filter & return
     539    return (bool) apply_filters( 'bbp_is_user_favorite', $retval, $user_id, $topic_id );
    595540}
    596541
     
    612557 */
    613558function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) {
     559    $user_id  = bbp_get_user_id( $user_id, false, false );
     560    $topic_id = bbp_get_topic_id( $topic_id );
    614561
    615562    // Bail if not enough info
     
    646593 */
    647594function bbp_remove_user_favorite( $user_id, $topic_id ) {
     595    $user_id  = bbp_get_user_id( $user_id, false, false );
     596    $topic_id = bbp_get_topic_id( $topic_id );
    648597
    649598    // Bail if not enough info
     
    776725
    777726/**
    778  * Get the users who have subscribed to the forum
    779  *
    780  * @since 2.5.0 bbPress (r5156)
    781  *
    782  * @param int $forum_id Optional. forum id
    783  * @uses bbp_get_users_for_object() To get the forum subscribers
    784  * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers
    785  * @return array|bool Results if the forum has any subscribers, otherwise false
    786  */
    787 function bbp_get_forum_subscribers( $forum_id = 0 ) {
    788     $forum_id = bbp_get_forum_id( $forum_id );
    789     $users    = bbp_get_users_for_object( $forum_id, '_bbp_subscription' );
    790 
    791     // Filter & return
    792     return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id );
    793 }
    794 
    795 /**
    796  * Get the users who have subscribed to the topic
    797  *
    798  * @since 2.0.0 bbPress (r2668)
    799  *
    800  * @param int $topic_id Optional. Topic id
    801  * @uses bbp_get_users_for_object() To get the topic subscribers
    802  * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers
    803  * @return array|bool Results if the topic has any subscribers, otherwise false
    804  */
    805 function bbp_get_topic_subscribers( $topic_id = 0 ) {
    806     $topic_id = bbp_get_topic_id( $topic_id );
    807     $users    = bbp_get_users_for_object( $topic_id, '_bbp_subscription' );
    808 
    809     // Filter & return
    810     return (array) apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id );
    811 }
    812 
    813 /**
    814  * Get a user's subscribed topics
    815  *
    816  * @since 2.0.0 bbPress (r2668)
    817  *
    818  * @deprecated 2.5.0 bbPress (r5156)
    819  *
    820  * @param int $user_id Optional. User id
    821  * @uses bbp_get_user_topic_subscriptions() To get the user's subscriptions
    822  * @return array|bool Results if user has subscriptions, otherwise false
    823  */
    824 function bbp_get_user_subscriptions( $user_id = 0 ) {
    825     _deprecated_function( __FUNCTION__, 2.5, 'bbp_get_user_topic_subscriptions()' );
    826     $query = bbp_get_user_topic_subscriptions( $user_id );
    827 
    828     // Filter & return
    829     return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
     727 * Get the users who have subscribed
     728 *
     729 * @since 2.6.0 bbPress (r5156)
     730 *
     731 * @param int $object_id Optional. ID of forum, topic, or something else
     732 * @uses bbp_get_users_for_object() To get the subscribers
     733 * @uses apply_filters() Calls 'bbp_get_subscribers' with the subscribers
     734 * @return array|bool Results if subscribers, otherwise false
     735 */
     736function bbp_get_subscribers( $object_id = 0, $type = 'post' ) {
     737    $users = bbp_get_users_for_object( $object_id, '_bbp_subscription', $type );
     738
     739    // Filter & return
     740    return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $object_id, $type );
    830741}
    831742
     
    883794    return apply_filters( 'bbp_get_user_forum_subscriptions', $query, $user_id );
    884795}
     796
     797/**
     798 * Check if a topic or forum is in user's subscription list or not
     799 *
     800 * @since 2.5.0 bbPress (r5156)
     801 *
     802 * @param int $user_id Optional. User id
     803 * @param int $object_id Optional. Topic id
     804 * @uses get_post() To get the post object
     805 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
     806 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
     807 * @uses bbp_get_forum_post_type() To get the forum post type
     808 * @uses bbp_get_topic_post_type() To get the topic post type
     809 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     810 *                        forum/topic id and subscriptions
     811 * @return bool True if the forum or topic is in user's subscriptions, otherwise false
     812 */
     813function bbp_is_user_subscribed( $user_id = 0, $object_id = 0, $type = 'post' ) {
     814    $retval = bbp_is_object_of_user( $object_id, $user_id, '_bbp_subscription', $type );
     815
     816    // Filter & return
     817    return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $type );
     818}
     819
     820/**
     821 * Add a user subscription
     822 *
     823 * @since 2.5.0 bbPress (r5156)
     824 * @since 2.6.0 bbPress (r6544) Added $type parameter
     825 *
     826 * @param int    $user_id   Optional. User id
     827 * @param int    $object_id Optional. Topic id
     828 * @param string $type      Optional. Type of object being subscribed to
     829 *
     830 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & object id
     831 * @return bool Always true
     832 */
     833function bbp_add_user_subscription( $user_id = 0, $object_id = 0, $type = 'post' ) {
     834
     835    // Bail if not enough info
     836    if ( empty( $user_id ) || empty( $object_id ) ) {
     837        return false;
     838    }
     839
     840    // Bail if already subscribed
     841    if ( bbp_is_user_subscribed( $user_id, $object_id, $type ) ) {
     842        return false;
     843    }
     844
     845    // Bail if add fails
     846    if ( ! bbp_add_user_to_object( $object_id, $user_id, '_bbp_subscription', $type ) ) {
     847        return false;
     848    }
     849
     850    do_action( 'bbp_add_user_subscription', $user_id, $object_id, $type );
     851
     852    return true;
     853}
     854
     855/**
     856 * Remove a user subscription
     857 *
     858 * @since 2.5.0 bbPress (r5156)
     859 * @since 2.6.0 bbPress (r6544) Added $type parameter
     860 *
     861 * @param int    $user_id   Optional. User id
     862 * @param int    $object_id Optional. Topic id
     863 * @param string $type      Optional. Type of object being subscribed to
     864 *
     865 * @uses bbp_is_user_subscribed() To check if the user is already subscribed
     866 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
     867 *                    topic id
     868 * @return bool True if the topic was removed from user's subscriptions,
     869 *               otherwise false
     870 */
     871function bbp_remove_user_subscription( $user_id = 0, $object_id = 0, $type = 'post' ) {
     872
     873    // Bail if not enough info
     874    if ( empty( $user_id ) || empty( $object_id ) ) {
     875        return false;
     876    }
     877
     878    // Bail if not subscribed
     879    if ( ! bbp_is_user_subscribed( $user_id, $object_id, $type ) ) {
     880        return false;
     881    }
     882
     883    // Bail if remove fails
     884    if ( ! bbp_remove_user_from_object( $object_id, $user_id, '_bbp_subscription', $type ) ) {
     885        return false;
     886    }
     887
     888    do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $type );
     889
     890    return true;
     891}
     892
     893/**
     894 * Handles the front end toggling of user subscriptions
     895 *
     896 * @since 2.0.0 bbPress (r2790)
     897 * @since 2.6.l bbPress (r6543)
     898 *
     899 * @param string $action The requested action to compare this function to
     900 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     901 * @uses bbp_get_user_id() To get the user id
     902 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
     903 * @uses current_user_can() To check if the current user can edit the user
     904 * @uses bbPress:errors:add() To log the error messages
     905 * @uses bbp_is_user_subscribed() To check if the object is in user's
     906 *                                 subscriptions
     907 * @uses bbp_remove_user_subscription() To remove the user subscription
     908 * @uses bbp_add_user_subscription() To add the user subscription
     909 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
     910 *                    object id and action
     911 * @uses bbp_is_subscription() To check if it's the subscription page
     912 * @uses bbp_redirect() To redirect to the url
     913 */
     914function bbp_subscriptions_handler( $action = '' ) {
     915
     916    // Default
     917    $success = false;
     918
     919    // Bail if subscriptions not active
     920    if ( ! bbp_is_subscriptions_active() ) {
     921        return $success;
     922    }
     923
     924    // Bail if no object ID is passed
     925    if ( empty( $_GET['object_id'] ) ) {
     926        return $success;
     927    }
     928
     929    // Setup possible get actions
     930    $possible_actions = array(
     931        'bbp_subscribe',
     932        'bbp_unsubscribe'
     933    );
     934
     935    // Bail if actions aren't meant for this function
     936    if ( ! in_array( $action, $possible_actions, true ) ) {
     937        return $success;
     938    }
     939
     940    // Get required data
     941    $user_id     = bbp_get_current_user_id();
     942    $object_id   = absint( $_GET['object_id'] );
     943    $object_type = ! empty( $_GET['object_type'] )
     944        ? sanitize_key( $_GET['object_type'] )
     945        : 'post';
     946
     947    // Check for empty topic
     948    if ( empty( $object_id ) ) {
     949        bbp_add_error( 'bbp_subscription_object_id', __( '<strong>ERROR</strong>: Not found. What are you subscribing/unsubscribing to?', 'bbpress' ) );
     950
     951    // Check nonce
     952    } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $object_id ) ) {
     953        bbp_add_error( 'bbp_subscription_object_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
     954
     955    // Check current user's ability to edit the user
     956    } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
     957        bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit subscriptions of that user.', 'bbpress' ) );
     958    }
     959
     960    // Bail if we have errors
     961    if ( bbp_has_errors() ) {
     962        return $success;
     963    }
     964
     965    /** No errors *************************************************************/
     966
     967    if ( 'bbp_unsubscribe' === $action ) {
     968        $success = bbp_remove_user_subscription( $user_id, $object_id, $object_type );
     969    } elseif ( 'bbp_subscribe' === $action ) {
     970        $success = bbp_add_user_subscription( $user_id, $object_id, $object_type );
     971    }
     972
     973    // Do additional subscriptions actions
     974    do_action( 'bbp_subscriptions_handler', $success, $user_id, $object_id, $action, $object_type );
     975
     976    // Success!
     977    if ( true === $success ) {
     978
     979        // Redirect back from whence we came
     980        if ( ! empty( $_REQUEST['redirect_to'] ) ) {
     981            $redirect = $_REQUEST['redirect_to']; // Validated later
     982        } elseif ( bbp_is_subscriptions() ) {
     983            $redirect = bbp_get_subscriptions_permalink( $user_id );
     984        } elseif ( bbp_is_single_user() ) {
     985            $redirect = bbp_get_user_profile_url();
     986        } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
     987            $redirect = bbp_get_topic_permalink( $object_id );
     988        } elseif ( is_singular( bbp_get_forum_post_type() ) ) {
     989            $redirect = bbp_get_forum_permalink( $object_id );
     990        } elseif ( is_single() || is_page() ) {
     991            $redirect = get_permalink();
     992        } else {
     993            $redirect = get_permalink( $object_id );
     994        }
     995
     996        bbp_redirect( $redirect );
     997
     998    // Fail! Handle errors
     999    } elseif ( 'bbp_unsubscribe' === $action ) {
     1000        bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing.', 'bbpress' ) );
     1001    } elseif ( 'bbp_subscribe' === $action ) {
     1002        bbp_add_error( 'bbp_subscribe',   __( '<strong>ERROR</strong>: There was a problem subscribing.', 'bbpress' ) );
     1003    }
     1004
     1005    return (bool) $success;
     1006}
     1007
     1008/** Query Helpers *************************************************************/
     1009
     1010/**
     1011 * Get a user's favorite topic ids
     1012 *
     1013 * @since 2.0.0 bbPress (r2652)
     1014 *
     1015 * @param int $user_id Optional. User id
     1016 * @uses bbp_get_user_id() To get the user id
     1017 * @uses bbp_get_topic_post_type() To get the topic post type
     1018 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
     1019 *                        the favorites and user id
     1020 * @return array|bool Results if user has favorites, otherwise null
     1021 */
     1022function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
     1023    $user_id   = bbp_get_user_id( $user_id );
     1024    $favorites = new WP_Query( array(
     1025        'fields'        => 'ids',
     1026        'post_type'     => bbp_get_topic_post_type(),
     1027        'nopaging'      => true,
     1028        'no_found_rows' => true,
     1029        'meta_query'    => array( array(
     1030            'key'     => '_bbp_favorite',
     1031            'value'   => $user_id,
     1032            'compare' => 'NUMERIC'
     1033        ) )
     1034    ) );
     1035
     1036    // Filter & return
     1037    return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites->posts, $user_id );
     1038}
     1039
    8851040
    8861041/**
     
    9151070
    9161071/**
    917  * Get a user's subscribed topics' ids
     1072 * Get a user's subscribed topic ids
    9181073 *
    9191074 * @since 2.0.0 bbPress (r2668)
     
    9441099}
    9451100
    946 /**
    947  * Check if a topic or forum is in user's subscription list or not
     1101/** Deprecated ****************************************************************/
     1102
     1103/**
     1104 * Get a user's subscribed topics
     1105 *
     1106 * @since 2.0.0 bbPress (r2668)
     1107 * @deprecated 2.5.0 bbPress (r5156)
     1108 *
     1109 * @param int $user_id Optional. User id
     1110 *
     1111 * @return array|bool Results if user has subscriptions, otherwise false
     1112 */
     1113function bbp_get_user_subscriptions( $user_id = 0 ) {
     1114    _deprecated_function( __FUNCTION__, '2.5', 'bbp_get_user_topic_subscriptions()' );
     1115    $query = bbp_get_user_topic_subscriptions( $user_id );
     1116
     1117    // Filter & return
     1118    return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
     1119}
     1120
     1121/**
     1122 * Get the users who have subscribed to the forum
    9481123 *
    9491124 * @since 2.5.0 bbPress (r5156)
    950  *
    951  * @param int $user_id Optional. User id
    952  * @param int $object_id Optional. Topic id
    953  * @uses get_post() To get the post object
    954  * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
    955  * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
    956  * @uses bbp_get_forum_post_type() To get the forum post type
    957  * @uses bbp_get_topic_post_type() To get the topic post type
    958  * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
    959  *                        forum/topic id and subscriptions
    960  * @return bool True if the forum or topic is in user's subscriptions, otherwise false
    961  */
    962 function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) {
    963 
    964     // Assume user is not subscribed
    965     $retval = false;
    966 
    967     // Setup ID's array
    968     $subscribed_ids = array();
    969 
    970     // User and object ID's are passed
    971     if ( ! empty( $user_id ) && ! empty( $object_id ) ) {
    972 
    973         // Get the post type
    974         $post_type = get_post_type( $object_id );
    975 
    976         // Post exists, so check the types
    977         if ( ! empty( $post_type ) ) {
    978 
    979             switch( $post_type ) {
    980 
    981                 // Forum
    982                 case bbp_get_forum_post_type() :
    983                     $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );
    984                     $retval         = bbp_is_user_subscribed_to_forum( $user_id, $object_id, $subscribed_ids );
    985                     break;
    986 
    987                 // Topic (default)
    988                 case bbp_get_topic_post_type() :
    989                 default :
    990                     $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );
    991                     $retval         = bbp_is_user_subscribed_to_topic( $user_id, $object_id, $subscribed_ids );
    992                     break;
    993             }
    994         }
    995     }
    996 
    997     // Filter & return
    998     return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscribed_ids );
     1125 * @deprecated 2.6.0 bbPress (r6543)
     1126 *
     1127 * @param int $forum_id Optional. forum id
     1128 *
     1129 * @return array|bool Results if the forum has any subscribers, otherwise false
     1130 */
     1131function bbp_get_forum_subscribers( $forum_id = 0 ) {
     1132    $users = bbp_get_users_for_object( $forum_id, '_bbp_subscription' );
     1133
     1134    // Filter & return
     1135    return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id );
     1136}
     1137
     1138/**
     1139 * Get the users who have subscribed to the topic
     1140 *
     1141 * @since 2.0.0 bbPress (r2668)
     1142 * @deprecated 2.6.0 bbPress (r6543)
     1143 *
     1144 * @param int $topic_id Optional. Topic id
     1145 *
     1146 * @return array|bool Results if the topic has any subscribers, otherwise false
     1147 */
     1148function bbp_get_topic_subscribers( $topic_id = 0 ) {
     1149    $users = bbp_get_users_for_object( $topic_id, '_bbp_subscription' );
     1150
     1151    // Filter & return
     1152    return (array) apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id );
    9991153}
    10001154
     
    10031157 *
    10041158 * @since 2.5.0 bbPress (r5156)
    1005  *
    1006  * @param int $user_id Optional. User id
    1007  * @param int $forum_id Optional. Topic id
    1008  * @param array $subscribed_ids Optional. Array of forum ID's to check
    1009  * @uses bbp_get_user_id() To get the user id
    1010  * @uses bbp_get_forum() To get the forum
    1011  * @uses bbp_get_forum_id() To get the forum id
    1012  * @uses bbp_is_object_of_user() To check if the user has a subscription
    1013  * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_forum' with the bool, user id,
    1014  *                        forum id and subsriptions
     1159 * @deprecated 2.6.0 bbPress (r6543)
     1160 *
     1161 * @param int $user_id  Optional. User id
     1162 * @param int $forum_id Optional. Forum id
     1163 *
    10151164 * @return bool True if the forum is in user's subscriptions, otherwise false
    10161165 */
    1017 function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0, $subscribed_ids = array() ) {
    1018 
    1019     // Assume user is not subscribed
    1020     $retval = false;
    1021 
    1022     // Validate user
    1023     $user_id = bbp_get_user_id( $user_id, true, true );
    1024     if ( ! empty( $user_id ) ) {
    1025 
    1026         // Get subscription ID's if none passed
    1027         if ( empty( $subscribed_ids ) ) {
    1028             $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );
    1029         }
    1030 
    1031         // User has forum subscriptions
    1032         if ( ! empty( $subscribed_ids ) ) {
    1033 
    1034             // Checking a specific forum id
    1035             if ( ! empty( $forum_id ) ) {
    1036                 $forum    = bbp_get_forum( $forum_id );
    1037                 $forum_id = ! empty( $forum ) ? $forum->ID : 0;
    1038 
    1039             // Using the global forum id
    1040             } elseif ( bbp_get_forum_id() ) {
    1041                 $forum_id = bbp_get_forum_id();
    1042 
    1043             // Use the current post id
    1044             } elseif ( ! bbp_get_forum_id() ) {
    1045                 $forum_id = get_the_ID();
    1046             }
    1047 
    1048             // Is forum_id in the user's subscriptions
    1049             if ( ! empty( $forum_id ) ) {
    1050                 $retval = bbp_is_object_of_user( $forum_id, $user_id, '_bbp_subscription' );
    1051             }
    1052         }
    1053     }
    1054 
    1055     // Filter & return
    1056     return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', $retval, $user_id, $forum_id, $subscribed_ids );
     1166function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0 ) {
     1167    return bbp_is_user_subscribed( $user_id, $forum_id );
    10571168}
    10581169
     
    10611172 *
    10621173 * @since 2.5.0 bbPress (r5156)
     1174 * @deprecated 2.6.0 bbPress (r6543) Use bbp_is_user_subscribed()
    10631175 *
    10641176 * @param int $user_id Optional. User id
    10651177 * @param int $topic_id Optional. Topic id
    1066  * @param array $subscribed_ids Optional. Array of topic ID's to check
    1067  * @uses bbp_get_user_id() To get the user id
    1068  * @uses bbp_get_topic() To get the topic
    1069  * @uses bbp_get_topic_id() To get the topic id
    1070  * @uses bbp_is_object_of_user() To check if the user is subscribed
    1071  * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_topic' with the bool, user id,
    1072  *                        topic id and subsriptions
    10731178 * @return bool True if the topic is in user's subscriptions, otherwise false
    10741179 */
    1075 function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0, $subscribed_ids = array() ) {
    1076 
    1077     // Assume user is not subscribed
    1078     $retval = false;
    1079 
    1080     // Validate user
    1081     $user_id = bbp_get_user_id( $user_id, true, true );
    1082     if ( ! empty( $user_id ) ) {
    1083 
    1084         // Get subscription ID's if none passed
    1085         if ( empty( $subscribed_ids ) ) {
    1086             $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );
    1087         }
    1088 
    1089         // User has topic subscriptions
    1090         if ( ! empty( $subscribed_ids ) ) {
    1091 
    1092             // Checking a specific topic id
    1093             if ( ! empty( $topic_id ) ) {
    1094                 $topic    = bbp_get_topic( $topic_id );
    1095                 $topic_id = ! empty( $topic ) ? $topic->ID : 0;
    1096 
    1097             // Using the global topic id
    1098             } elseif ( bbp_get_topic_id() ) {
    1099                 $topic_id = bbp_get_topic_id();
    1100 
    1101             // Use the current post id
    1102             } elseif ( ! bbp_get_topic_id() ) {
    1103                 $topic_id = get_the_ID();
    1104             }
    1105 
    1106             // Is topic_id in the user's subscriptions
    1107             if ( ! empty( $topic_id ) ) {
    1108                 $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_subscription' );
    1109             }
    1110         }
    1111     }
    1112 
    1113     // Filter & return
    1114     return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', $retval, $user_id, $topic_id, $subscribed_ids );
    1115 }
    1116 
    1117 /**
    1118  * Add a user subscription
     1180function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0 ) {
     1181    return bbp_is_user_subscribed( $user_id, $topic_id );
     1182}
     1183
     1184/**
     1185 * Remove a forum from user's subscriptions
    11191186 *
    11201187 * @since 2.5.0 bbPress (r5156)
    1121  *
    1122  * @param int $user_id Optional. User id
    1123  * @param int $object_id Optional. Topic id
    1124  * @uses get_post() To get the post object
    1125  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & object id
     1188 * @deprecated 2.6.0 bbPress (r6543)
     1189 *
     1190 * @param int $user_id Optional. User id
     1191 * @param int $forum_id Optional. forum id
     1192 * @return bool True if the forum was removed from user's subscriptions,
     1193 *               otherwise false
     1194 */
     1195function bbp_remove_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
     1196    return bbp_remove_user_subscription( $user_id, $forum_id );
     1197}
     1198
     1199/**
     1200 * Remove a topic from user's subscriptions
     1201 *
     1202 * @since 2.5.0 bbPress (r5156)
     1203 * @deprecated 2.6.0 bbPress (r6543)
     1204 *
     1205 * @param int $user_id Optional. User id
     1206 * @param int $topic_id Optional. Topic id
     1207 * @return bool True if the topic was removed from user's subscriptions,
     1208 *               otherwise false
     1209 */
     1210function bbp_remove_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
     1211    return bbp_remove_user_subscription( $user_id, $topic_id );
     1212}
     1213
     1214/**
     1215 * Add a forum to user's subscriptions
     1216 *
     1217 * @since 2.5.0 bbPress (r5156)
     1218 * @deprecated 2.6.0 bbPress (r6543)
     1219 *
     1220 * @param int $user_id Optional. User id
     1221 * @param int $forum_id Optional. forum id
    11261222 * @return bool Always true
    11271223 */
    1128 function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) {
    1129 
    1130     // Bail if not enough info
    1131     if ( empty( $user_id ) || empty( $object_id ) ) {
    1132         return false;
    1133     }
    1134 
    1135     // Get the post type
    1136     $post_type = get_post_type( $object_id );
    1137     if ( empty( $post_type ) ) {
    1138         return false;
    1139     }
    1140 
    1141     // Bail if already subscribed
    1142     if ( bbp_is_user_subscribed( $user_id, $object_id ) ) {
    1143         return false;
    1144     }
    1145 
    1146     // Bail if add fails
    1147     if ( ! bbp_add_user_to_object( $object_id, $user_id, '_bbp_subscription' ) ) {
    1148         return false;
    1149     }
    1150 
    1151     do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type );
    1152 
    1153     return true;
    1154 }
    1155 
    1156 /**
    1157  * Add a forum to user's subscriptions
    1158  *
    1159  * @since 2.5.0 bbPress (r5156)
    1160  *
    1161  * @param int $user_id Optional. User id
    1162  * @param int $forum_id Optional. forum id
    1163  * @uses bbp_get_forum() To get the forum
    1164  * @uses bbp_add_user_subscription() To add the user subscription
    1165  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id
    1166  * @return bool Always true
    1167  */
    11681224function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
    1169 
    1170     // Bail if not enough info
    1171     if ( empty( $user_id ) || empty( $forum_id ) ) {
    1172         return false;
    1173     }
    1174 
    1175     // Bail if no forum
    1176     $forum = bbp_get_forum( $forum_id );
    1177     if ( empty( $forum ) ) {
    1178         return false;
    1179     }
    1180 
    1181     // Bail if already subscribed
    1182     if ( bbp_is_user_subscribed( $user_id, $forum_id ) ) {
    1183         return false;
    1184     }
    1185 
    1186     // Bail if add fails
    1187     if ( ! bbp_add_user_subscription( $user_id, $forum_id ) ) {
    1188         return false;
    1189     }
    1190 
    1191     do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id );
    1192 
    1193     return true;
     1225    return bbp_add_user_subscription( $user_id, $forum_id );
    11941226}
    11951227
     
    12031235 *
    12041236 * @since 2.0.0 bbPress (r2668)
     1237 * @deprecated 2.6.0 bbPress (r6543)
    12051238 *
    12061239 * @param int $user_id Optional. User id
    12071240 * @param int $topic_id Optional. Topic id
    1208  * @uses bbp_add_user_subscription() To add the subscription
    1209  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
    12101241 * @return bool Always true
    12111242 */
    12121243function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
    1213 
    1214     // Bail if not enough info
    1215     if ( empty( $user_id ) || empty( $topic_id ) ) {
    1216         return false;
    1217     }
    1218 
    1219     // Bail if already subscribed
    1220     if ( bbp_is_user_subscribed_to_topic( $user_id, $topic_id ) ) {
    1221         return false;
    1222     }
    1223 
    1224     // Bail if add fails
    1225     if ( ! bbp_add_user_subscription( $user_id, $topic_id ) ) {
    1226         return false;
    1227     }
    1228 
    1229     do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id );
    1230 
    1231     return true;
    1232 }
    1233 
    1234 /**
    1235  * Remove a user subscription
    1236  *
    1237  * @since 2.0.0 bbPress (r2668)
    1238  *
    1239  * @param int $user_id Optional. User id
    1240  * @param int $object_id Optional. Topic id
    1241  * @uses get_post() To get the post object
    1242  * @uses bbp_is_user_subscribed() To check if the user is already subscribed
    1243  * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    1244  *                    topic id
    1245  * @return bool True if the topic was removed from user's subscriptions,
    1246  *               otherwise false
    1247  */
    1248 function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) {
    1249 
    1250     // Bail if not enough info
    1251     if ( empty( $user_id ) || empty( $object_id ) ) {
    1252         return false;
    1253     }
    1254 
    1255     // Get post type
    1256     $post_type = get_post_type( $object_id );
    1257     if ( empty( $post_type ) ) {
    1258         return false;
    1259     }
    1260 
    1261     // Bail if not subscribed
    1262     if ( ! bbp_is_user_subscribed( $user_id, $object_id ) ) {
    1263         return false;
    1264     }
    1265 
    1266     // Bail if remove fails
    1267     if ( ! bbp_remove_user_from_object( $object_id, $user_id, '_bbp_subscription' ) ) {
    1268         return false;
    1269     }
    1270 
    1271     do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type );
    1272 
    1273     return true;
    1274 }
    1275 
    1276 /**
    1277  * Remove a forum from user's subscriptions
     1244    return bbp_add_user_subscription( $user_id, $topic_id );
     1245}
     1246
     1247/**
     1248 * Handles the front end toggling of forum subscriptions
    12781249 *
    12791250 * @since 2.5.0 bbPress (r5156)
    1280  *
    1281  * @param int $user_id Optional. User id
    1282  * @param int $forum_id Optional. forum id
    1283  * @uses bbp_remove_user_subscription() To remove the subscription
    1284  * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    1285  *                    forum id
    1286  * @return bool True if the forum was removed from user's subscriptions,
    1287  *               otherwise false
    1288  */
    1289 function bbp_remove_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
    1290 
    1291     // Bail if not enough info
    1292     if ( empty( $user_id ) || empty( $forum_id ) ) {
    1293         return false;
    1294     }
    1295 
    1296     // Bail if remove fails
    1297     if ( ! bbp_remove_user_subscription( $user_id, $forum_id ) ) {
    1298         return false;
    1299     }
    1300 
    1301     do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id );
    1302 
    1303     return true;
    1304 }
    1305 
    1306 /**
    1307  * Remove a topic from user's subscriptions
    1308  *
    1309  * @since 2.5.0 bbPress (r5156)
    1310  *
    1311  * @param int $user_id Optional. User id
    1312  * @param int $topic_id Optional. Topic id
    1313  * @uses bbp_remove_user_subscription() To remove the subscription
    1314  * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and
    1315  *                    topic id
    1316  * @return bool True if the topic was removed from user's subscriptions,
    1317  *               otherwise false
    1318  */
    1319 function bbp_remove_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
    1320 
    1321     // Bail if not enough info
    1322     if ( empty( $user_id ) || empty( $topic_id ) ) {
    1323         return false;
    1324     }
    1325 
    1326     // Bail if remove fails
    1327     if ( ! bbp_remove_user_subscription( $user_id, $topic_id ) ) {
    1328         return false;
    1329     }
    1330 
    1331     do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id );
    1332 
    1333     return true;
    1334 }
    1335 
    1336 /**
    1337  * Handles the front end subscribing and unsubscribing forums
    1338  *
    1339  * @since 2.5.0 bbPress (r5156)
    1340  *
    1341  * @param string $action The requested action to compare this function to
    1342  * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
    1343  * @uses bbp_get_user_id() To get the user id
    1344  * @uses bbp_verify_nonce_request() To verify the nonce and check the request
    1345  * @uses current_user_can() To check if the current user can edit the user
    1346  * @uses bbPress:errors:add() To log the error messages
    1347  * @uses bbp_is_user_subscribed() To check if the forum is in user's
    1348  *                                 subscriptions
    1349  * @uses bbp_remove_user_subscription() To remove the user subscription
    1350  * @uses bbp_add_user_subscription() To add the user subscription
    1351  * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
    1352  *                    forum id and action
    1353  * @uses bbp_is_subscription() To check if it's the subscription page
    1354  * @uses bbp_get_forum_permalink() To get the forum permalink
    1355  * @uses bbp_redirect() To redirect to the url
     1251 * @deprecated 2.6.0 bbPress (r6543)
    13561252 */
    13571253function bbp_forum_subscriptions_handler( $action = '' ) {
    1358 
    1359     // Default
    1360     $success = false;
    1361 
    1362     // Bail if subscriptions not active
    1363     if ( ! bbp_is_subscriptions_active() ) {
    1364         return $success;
    1365     }
    1366 
    1367     // Bail if no forum ID is passed
    1368     if ( empty( $_GET['forum_id'] ) ) {
    1369         return $success;
    1370     }
    1371 
    1372     // Setup possible get actions
    1373     $possible_actions = array(
    1374         'bbp_subscribe',
    1375         'bbp_unsubscribe',
    1376     );
    1377 
    1378     // Bail if actions aren't meant for this function
    1379     if ( ! in_array( $action, $possible_actions, true ) ) {
    1380         return $success;
    1381     }
    1382 
    1383     // Get required data
    1384     $forum_id = bbp_get_forum_id( $_GET['forum_id'] );
    1385     $user_id  = bbp_get_user_id( 0, true, true );
    1386 
    1387     // Check for empty forum
    1388     if ( empty( $forum_id ) ) {
    1389         bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found. Which forum are you subscribing/unsubscribing to?', 'bbpress' ) );
    1390 
    1391     // Check nonce
    1392     } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) {
    1393         bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
    1394 
    1395     // Check current user's ability to edit the user
    1396     } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
    1397         bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites of that user.', 'bbpress' ) );
    1398     }
    1399 
    1400     // Bail if we have errors
    1401     if ( bbp_has_errors() ) {
    1402         return $success;
    1403     }
    1404 
    1405     /** No errors *************************************************************/
    1406 
    1407     if ( 'bbp_unsubscribe' === $action ) {
    1408         $success = bbp_remove_user_subscription( $user_id, $forum_id );
    1409     } elseif ( 'bbp_subscribe' === $action ) {
    1410         $success = bbp_add_user_subscription( $user_id, $forum_id );
    1411     }
    1412 
    1413     // Do additional subscriptions actions
    1414     do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action );
    1415 
    1416     // Success!
    1417     if ( true === $success ) {
    1418 
    1419         // Redirect back from whence we came
    1420         if ( ! empty( $_REQUEST['redirect_to'] ) ) {
    1421             $redirect = $_REQUEST['redirect_to']; // Validated later
    1422         } elseif ( bbp_is_subscriptions() ) {
    1423             $redirect = bbp_get_subscriptions_permalink( $user_id );
    1424         } elseif ( bbp_is_single_user() ) {
    1425             $redirect = bbp_get_user_profile_url();
    1426         } elseif ( is_singular( bbp_get_forum_post_type() ) ) {
    1427             $redirect = bbp_get_forum_permalink( $forum_id );
    1428         } elseif ( is_single() || is_page() ) {
    1429             $redirect = get_permalink();
    1430         } else {
    1431             $redirect = get_permalink( $forum_id );
    1432         }
    1433 
    1434         bbp_redirect( $redirect );
    1435 
    1436     // Fail! Handle errors
    1437     } elseif ( 'bbp_unsubscribe' === $action ) {
    1438         bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum.', 'bbpress' ) );
    1439     } elseif ( 'bbp_subscribe' === $action ) {
    1440         bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that forum.', 'bbpress' ) );
    1441     }
    1442 
    1443     return (bool) $success;
    1444 }
    1445 
    1446 /**
    1447  * Handles the front end subscribing and unsubscribing topics
    1448  *
    1449  * @since 2.0.0 bbPress (r2790)
    1450  *
    1451  * @param string $action The requested action to compare this function to
    1452  * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
    1453  * @uses bbp_get_user_id() To get the user id
    1454  * @uses bbp_verify_nonce_request() To verify the nonce and check the request
    1455  * @uses current_user_can() To check if the current user can edit the user
    1456  * @uses bbPress:errors:add() To log the error messages
    1457  * @uses bbp_is_user_subscribed() To check if the topic is in user's
    1458  *                                 subscriptions
    1459  * @uses bbp_remove_user_subscription() To remove the user subscription
    1460  * @uses bbp_add_user_subscription() To add the user subscription
    1461  * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
    1462  *                    topic id and action
    1463  * @uses bbp_is_subscription() To check if it's the subscription page
    1464  * @uses bbp_get_topic_permalink() To get the topic permalink
    1465  * @uses bbp_redirect() To redirect to the url
    1466  */
    1467 function bbp_subscriptions_handler( $action = '' ) {
    1468 
    1469     // Default
    1470     $success = false;
    1471 
    1472     // Bail if subscriptions not active
    1473     if ( ! bbp_is_subscriptions_active() ) {
    1474         return $success;
    1475     }
    1476 
    1477     // Bail if no topic ID is passed
    1478     if ( empty( $_GET['topic_id'] ) ) {
    1479         return $success;
    1480     }
    1481 
    1482     // Setup possible get actions
    1483     $possible_actions = array(
    1484         'bbp_subscribe',
    1485         'bbp_unsubscribe',
    1486     );
    1487 
    1488     // Bail if actions aren't meant for this function
    1489     if ( ! in_array( $action, $possible_actions, true ) ) {
    1490         return $success;
    1491     }
    1492 
    1493     // Get required data
    1494     $topic_id = bbp_get_topic_id( $_GET['topic_id'] );
    1495     $user_id  = bbp_get_user_id( 0, true, true );
    1496 
    1497     // Check for empty topic
    1498     if ( empty( $topic_id ) ) {
    1499         bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found. Which topic are you subscribing/unsubscribing to?', 'bbpress' ) );
    1500 
    1501     // Check nonce
    1502     } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $topic_id ) ) {
    1503         bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
    1504 
    1505     // Check current user's ability to edit the user
    1506     } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
    1507         bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites of that user.', 'bbpress' ) );
    1508     }
    1509 
    1510     // Bail if we have errors
    1511     if ( bbp_has_errors() ) {
    1512         return $success;
    1513     }
    1514 
    1515     /** No errors *************************************************************/
    1516 
    1517     if ( 'bbp_unsubscribe' === $action ) {
    1518         $success = bbp_remove_user_subscription( $user_id, $topic_id );
    1519     } elseif ( 'bbp_subscribe' === $action ) {
    1520         $success = bbp_add_user_subscription( $user_id, $topic_id );
    1521     }
    1522 
    1523     // Do additional subscriptions actions
    1524     do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );
    1525 
    1526     // Success!
    1527     if ( true === $success ) {
    1528 
    1529         // Redirect back from whence we came
    1530         if ( ! empty( $_REQUEST['redirect_to'] ) ) {
    1531             $redirect = $_REQUEST['redirect_to']; // Validated later
    1532         } elseif ( bbp_is_subscriptions() ) {
    1533             $redirect = bbp_get_subscriptions_permalink( $user_id );
    1534         } elseif ( bbp_is_single_user() ) {
    1535             $redirect = bbp_get_user_profile_url();
    1536         } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
    1537             $redirect = bbp_get_topic_permalink( $topic_id );
    1538         } elseif ( is_single() || is_page() ) {
    1539             $redirect = get_permalink();
    1540         } else {
    1541             $redirect = get_permalink( $topic_id );
    1542         }
    1543 
    1544         bbp_redirect( $redirect );
    1545 
    1546     // Fail! Handle errors
    1547     } elseif ( 'bbp_unsubscribe' === $action ) {
    1548         bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic.', 'bbpress' ) );
    1549     } elseif ( 'bbp_subscribe' === $action ) {
    1550         bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that topic.', 'bbpress' ) );
    1551     }
    1552 
    1553     return (bool) $success;
    1554 }
     1254    return bbp_subscriptions_handler( $action );
     1255}
  • trunk/src/includes/users/template.php

    r6538 r6544  
    11671167
    11681168        // Validate user and topic ID's
    1169         $user_id  = bbp_get_user_id( $r['user_id'], true, true );
    1170         $topic_id = bbp_get_topic_id( $r['topic_id'] );
    1171         if ( empty( $user_id ) || empty( $topic_id ) ) {
     1169        $user_id   = bbp_get_user_id( $r['user_id'], true, true );
     1170        $object_id = bbp_get_topic_id( $r['topic_id'] );
     1171
     1172        // Bail if empty
     1173        if ( empty( $user_id ) || empty( $object_id ) ) {
    11721174            return false;
    11731175        }
     
    11791181
    11801182        // Decide which link to show
    1181         $is_fav = bbp_is_user_favorite( $user_id, $topic_id );
     1183        $is_fav = bbp_is_user_favorite( $user_id, $object_id );
    11821184        if ( ! empty( $is_fav ) ) {
    1183             $text       = $r['favorited'];
    1184             $query_args = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
     1185            $text   = $r['favorited'];
     1186            $q_args = array( 'action' => 'bbp_favorite_remove', 'object_id' => $object_id );
    11851187        } else {
    1186             $text       = $r['favorite'];
    1187             $query_args = array( 'action' => 'bbp_favorite_add',    'topic_id' => $topic_id );
     1188            $text   = $r['favorite'];
     1189            $q_args = array( 'action' => 'bbp_favorite_add',    'object_id' => $object_id );
    11881190        }
    11891191
    11901192        // Custom redirect
    11911193        if ( ! empty( $r['redirect_to'] ) ) {
    1192             $query_args['redirect_to'] = urlencode( $r['redirect_to'] );
    1193         }
    1194 
    1195         // Create the link based where the user is and if the topic is
    1196         // already the user's favorite
    1197         if ( bbp_is_favorites() ) {
    1198             $permalink = bbp_get_favorites_permalink( $user_id );
    1199         } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) {
    1200             $permalink = bbp_get_topic_permalink( $topic_id );
    1201         } else {
    1202             $permalink = get_permalink();
    1203         }
    1204 
    1205         $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-favorite_' . $topic_id ) );
     1194            $q_args['redirect_to'] = urlencode( $r['redirect_to'] );
     1195        }
     1196
     1197        // URL
     1198        $url  = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-favorite_' . $object_id ) );
    12061199        $sub  = $is_fav ? ' class="is-favorite"' : '';
    1207         $html = sprintf( '%s<span id="favorite-%d"  %s><a href="%s" class="favorite-toggle" data-topic="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, wp_create_nonce( 'toggle-favorite_' . $topic_id ), $text, $r['after'] );
     1200        $html = sprintf( '%s<span id="favorite-%d"  %s><a href="%s" class="favorite-toggle" data-object-id="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $object_id, $sub, $url, $object_id, wp_create_nonce( 'toggle-favorite_' . $object_id ), $text, $r['after'] );
    12081201
    12091202        // Initial output is wrapped in a span, ajax output is hooked to this
     
    12131206
    12141207        // Filter & return
    1215         return apply_filters( 'bbp_get_user_favorites_link', $html, $r, $user_id, $topic_id );
     1208        return apply_filters( 'bbp_get_user_favorites_link', $html, $r, $user_id, $object_id );
    12161209    }
    12171210
     
    13421335     * @uses bbp_get_forum_id() To get the forum id
    13431336     * @uses current_user_can() To check if the current user can edit user
    1344      * @uses bbp_is_user_subscribed_to_forum() To check if the user is subscribed to the forum
    1345      * @uses bbp_is_user_subscribed_to_topic() To check if the user is subscribed to the topic
     1337     * @uses bbp_is_user_subscribed() To check if the user is subscribed
    13461338     * @uses bbp_is_subscriptions() To check if it's the subscriptions page
    13471339     * @uses bbp_get_subscriptions_permalink() To get subscriptions link
     
    13741366        $topic_id = bbp_get_topic_id( $r['topic_id'] );
    13751367        $forum_id = bbp_get_forum_id( $r['forum_id'] );
     1368
     1369        // Bail if anything is missing
    13761370        if ( empty( $user_id ) || ( empty( $topic_id ) && empty( $forum_id ) ) ) {
    13771371            return false;
     
    13831377        }
    13841378
    1385         // Check if viewing a single forum
    1386         if ( empty( $topic_id ) && ! empty( $forum_id ) ) {
    1387 
    1388             // Decide which link to show
    1389             $is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id );
    1390             if ( ! empty( $is_subscribed ) ) {
    1391                 $text       = $r['unsubscribe'];
    1392                 $query_args = array( 'action' => 'bbp_unsubscribe', 'forum_id' => $forum_id );
    1393             } else {
    1394                 $text       = $r['subscribe'];
    1395                 $query_args = array( 'action' => 'bbp_subscribe',   'forum_id' => $forum_id );
    1396             }
    1397 
    1398             // Custom redirect
    1399             if ( ! empty( $r['redirect_to'] ) ) {
    1400                 $query_args['redirect_to'] = urlencode( $r['redirect_to'] );
    1401             }
    1402 
    1403             // Create the link based where the user is and if the user is
    1404             // subscribed already
    1405             if ( bbp_is_subscriptions() ) {
    1406                 $permalink = bbp_get_subscriptions_permalink( $user_id );
    1407             } elseif ( bbp_is_single_forum() || bbp_is_single_reply() ) {
    1408                 $permalink = bbp_get_forum_permalink( $forum_id );
    1409             } else {
    1410                 $permalink = get_permalink();
    1411             }
    1412 
    1413             $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $forum_id ) );
    1414             $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
    1415             $html = sprintf( '%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-forum="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $forum_id, $sub, $url, $forum_id, wp_create_nonce( 'toggle-subscription_' . $forum_id ), $text, $r['after'] );
    1416 
    1417             // Initial output is wrapped in a span, ajax output is hooked to this
    1418             if ( ! empty( $wrap ) ) {
    1419                 $html = '<span id="subscription-toggle">' . $html . '</span>';
    1420             }
    1421 
     1379        // Check if viewing forum or topic (more to do later)
     1380        if ( ! empty( $forum_id ) ) {
     1381            $object_id = $forum_id;
     1382        } elseif ( ! empty( $topic_id ) ) {
     1383            $object_id = $topic_id;
     1384        }
     1385
     1386        // Decide which link to show
     1387        $is_subscribed = bbp_is_user_subscribed( $user_id, $object_id );
     1388        if ( ! empty( $is_subscribed ) ) {
     1389            $text   = $r['unsubscribe'];
     1390            $q_args = array( 'action' => 'bbp_unsubscribe', 'object_id' => $object_id );
    14221391        } else {
    1423 
    1424             // Decide which link to show
    1425             $is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id );
    1426             if ( ! empty( $is_subscribed ) ) {
    1427                 $text       = $r['unsubscribe'];
    1428                 $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
    1429             } else {
    1430                 $text       = $r['subscribe'];
    1431                 $query_args = array( 'action' => 'bbp_subscribe',   'topic_id' => $topic_id );
    1432             }
    1433 
    1434             // Custom redirect
    1435             if ( ! empty( $r['redirect_to'] ) ) {
    1436                 $query_args['redirect_to'] = urlencode( $r['redirect_to'] );
    1437             }
    1438 
    1439             // Create the link based where the user is and if the user is
    1440             // subscribed already
    1441             if ( bbp_is_subscriptions() ) {
    1442                 $permalink = bbp_get_subscriptions_permalink( $user_id );
    1443             } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) {
    1444                 $permalink = bbp_get_topic_permalink( $topic_id );
    1445             } else {
    1446                 $permalink = get_permalink();
    1447             }
    1448 
    1449             $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
    1450             $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
    1451             $html = sprintf( '%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-topic="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, wp_create_nonce( 'toggle-subscription_' . $topic_id ), $text, $r['after'] );
    1452 
    1453             // Initial output is wrapped in a span, ajax output is hooked to this
    1454             if ( ! empty( $wrap ) ) {
    1455                 $html = '<span id="subscription-toggle">' . $html . '</span>';
    1456             }
    1457         }
    1458 
    1459         // Filter & return
    1460         return apply_filters( 'bbp_get_user_subscribe_link', $html, $r, $user_id, $topic_id );
     1392            $text   = $r['subscribe'];
     1393            $q_args = array( 'action' => 'bbp_subscribe',   'object_id' => $object_id );
     1394        }
     1395
     1396        // Custom redirect
     1397        if ( ! empty( $r['redirect_to'] ) ) {
     1398            $q_args['redirect_to'] = urlencode( $r['redirect_to'] );
     1399        }
     1400
     1401        // URL
     1402        $url  = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-subscription_' . $object_id ) );
     1403        $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
     1404        $html = sprintf( '%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-bbp-object-id="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $object_id, $sub, $url, $object_id, wp_create_nonce( 'toggle-subscription_' . $object_id ), $text, $r['after'] );
     1405
     1406        // Initial output is wrapped in a span, ajax output is hooked to this
     1407        if ( ! empty( $wrap ) ) {
     1408            $html = '<span id="subscription-toggle">' . $html . '</span>';
     1409        }
     1410
     1411        // Filter & return
     1412        return apply_filters( 'bbp_get_user_subscribe_link', $html, $r, $user_id, $object_id );
    14611413    }
    14621414
Note: See TracChangeset for help on using the changeset viewer.