Skip to:
Content

bbPress.org

Changeset 6551


Ignore:
Timestamp:
06/15/2017 04:31:59 AM (9 years ago)
Author:
johnjamesjacoby
Message:

Theme Compat: Improve loading & support for external template packs.

  • Better error handling if tempate pack is broken, unloaded, or missing
  • Consolidate favs & subs code to use the same central handlers, by passing an object_id and object_type around (for future support of term subscriptions)
  • Rename data addtributes from previous BuddyPress favs & subs fixes
  • Adjust load order of template pack setup so that it's as late as possible, allowing template pack authors more time to get registered
  • Automatically add the current template pack to the template stack (multiple packs can still be layered, and this improves support for that)
  • Merge topic.js and forum.js in "bbPress Default" template pack to use the same engagements.js script, which simplifies localization & uses 1 less script between pages

This change puts the finishing touches on favs & subs consolidation for 2.6, and updates several related & surrounding functions to better support the necessary mark-up changes.

Location:
trunk/src
Files:
1 deleted
8 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/bbpress.php

    r6547 r6551  
    210210        /** Versions **********************************************************/
    211211
    212         $this->version    = '2.6-rc-6541';
     212        $this->version    = '2.6-rc-6550';
    213213        $this->db_version = '262';
    214214
     
    446446    public function register_theme_packages() {
    447447
     448        // Register the basic theme stack. This is really dope.
     449        bbp_register_template_stack( 'get_stylesheet_directory', 6 );
     450        bbp_register_template_stack( 'get_template_directory',   8 );
     451
    448452        // Register the default theme compatibility package
    449453        bbp_register_theme_package( array(
    450454            'id'      => 'default',
    451             'name'    => __( 'bbPress Default', 'bbpress' ),
     455            'name'    => 'bbPress Default',
    452456            'version' => bbp_get_version(),
    453457            'dir'     => trailingslashit( $this->themes_dir . 'default' ),
    454458            'url'     => trailingslashit( $this->themes_url . 'default' )
    455459        ) );
    456 
    457         // Register the basic theme stack. This is really dope.
    458         bbp_register_template_stack( 'get_stylesheet_directory', 10 );
    459         bbp_register_template_stack( 'get_template_directory',   12 );
    460         bbp_register_template_stack( 'bbp_get_theme_compat_dir', 14 );
    461460    }
    462461
     
    467466     */
    468467    public function setup_theme() {
    469 
    470         // Bail if something already has this under control
    471         if ( ! empty( $this->theme_compat->theme ) ) {
    472             return;
    473         }
    474 
    475         // Setup the theme package to use for compatibility
    476468        bbp_setup_theme_compat( bbp_get_theme_package_id() );
    477469    }
  • trunk/src/includes/core/actions.php

    r6544 r6551  
    4141add_action( 'init',                     'bbp_init',                   0     ); // Early for bbp_register
    4242add_action( 'parse_query',              'bbp_parse_query',            2     ); // Early for overrides
     43add_action( 'generate_rewrite_rules',   'bbp_generate_rewrite_rules', 10    );
     44add_action( 'after_setup_theme',        'bbp_after_setup_theme',      10    );
     45add_action( 'setup_theme',              'bbp_setup_theme',            10    );
     46add_action( 'set_current_user',         'bbp_setup_current_user',     10    );
     47add_action( 'profile_update',           'bbp_profile_update',         10, 2 ); // user_id and old_user_data
     48add_action( 'user_register',            'bbp_user_register',          10    );
     49add_action( 'login_form_login',         'bbp_login_form_login',       10    );
     50add_action( 'template_redirect',        'bbp_template_redirect',      8     ); // Before BuddyPress's 10 [BB2225]
    4351add_action( 'widgets_init',             'bbp_widgets_init',           10    );
    4452add_action( 'wp_roles_init',            'bbp_roles_init',             10    );
    45 add_action( 'generate_rewrite_rules',   'bbp_generate_rewrite_rules', 10    );
    4653add_action( 'wp_enqueue_scripts',       'bbp_enqueue_scripts',        10    );
    4754add_action( 'wp_head',                  'bbp_head',                   10    );
    4855add_action( 'wp_footer',                'bbp_footer',                 10    );
    49 add_action( 'set_current_user',         'bbp_setup_current_user',     10    );
    50 add_action( 'setup_theme',              'bbp_setup_theme',            10    );
    51 add_action( 'after_setup_theme',        'bbp_after_setup_theme',      10    );
    52 add_action( 'template_redirect',        'bbp_template_redirect',      8     ); // Before BuddyPress's 10 [BB2225]
    53 add_action( 'login_form_login',         'bbp_login_form_login',       10    );
    54 add_action( 'profile_update',           'bbp_profile_update',         10, 2 ); // user_id and old_user_data
    55 add_action( 'user_register',            'bbp_user_register',          10    );
    5656
    5757/**
     
    6969add_action( 'bbp_loaded', 'bbp_setup_user_option_filters', 12 );
    7070add_action( 'bbp_loaded', 'bbp_pre_load_options',          14 );
    71 add_action( 'bbp_loaded', 'bbp_register_theme_packages',   16 );
    7271
    7372/**
     
    7978 */
    8079add_action( 'bbp_init', 'bbp_load_textdomain',   0   );
    81 add_action( 'bbp_init', 'bbp_register',          0   );
     80add_action( 'bbp_init', 'bbp_register',          10  );
    8281add_action( 'bbp_init', 'bbp_add_rewrite_tags',  20  );
    8382add_action( 'bbp_init', 'bbp_add_rewrite_rules', 30  );
    8483add_action( 'bbp_init', 'bbp_add_permastructs',  40  );
    8584add_action( 'bbp_init', 'bbp_ready',             999 );
     85
     86/**
     87 * bbp_setup_theme - Attached to 'setup_theme' above
     88 *
     89 * Attach various theme related actions to the setup_theme action.
     90 * The load order helps to execute code at the correct time.
     91 *                                                            v---Load order
     92 */
     93add_action( 'bbp_setup_theme', 'bbp_register_theme_packages', 2 ); // Lower than 5
    8694
    8795/**
  • trunk/src/includes/core/template-functions.php

    r6549 r6551  
    284284 * @param string $location_callback Callback function that returns the
    285285 * @param int $priority
    286  * @see bbp_register_template_stack()
     286 * @return bool Whether stack was removed
    287287 */
    288288function bbp_deregister_template_stack( $location_callback = '', $priority = 10 ) {
     
    300300 * Call the functions added to the 'bbp_template_stack' filter hook, and return
    301301 * an array of the template locations.
    302  *
    303  * @see bbp_register_template_stack()
    304302 *
    305303 * @since 2.2.0 bbPress (r4323)
  • trunk/src/includes/core/theme-compat.php

    r6501 r6551  
    2929 *
    3030 * This is only intended to be extended, and is included here as a basic guide
    31  * for future Theme Packs to use. @link BBP_Twenty_Ten is a good example of
    32  * extending this class, as is @link bbp_setup_theme_compat()
     31 * for future Template Packs to use. @link bbp_setup_theme_compat()
    3332 *
    3433 * @since 2.0.0 bbPress (r3506)
     
    8887            : '';
    8988    }
     89
     90    /**
     91     * Return the template directory.
     92     *
     93     * @since 2.6.0 bbPress (r6548)
     94     *
     95     * @return string
     96     */
     97    public function get_dir() {
     98        return $this->dir;
     99    }
    90100}
    91101
     
    93103
    94104/**
    95  * Setup the default theme compat theme
     105 * Setup the active template pack and register it's directory in the stack.
    96106 *
    97107 * @since 2.0.0 bbPress (r3311)
     
    99109 * @param BBP_Theme_Compat $theme
    100110 */
    101 function bbp_setup_theme_compat( $theme = '' ) {
     111function bbp_setup_theme_compat( $theme = 'default' ) {
    102112    $bbp = bbpress();
    103113
    104     // Make sure theme package is available, set to default if not
    105     if ( ! isset( $bbp->theme_compat->packages[ $theme ] ) || ! is_a( $bbp->theme_compat->packages[ $theme ], 'BBP_Theme_Compat' ) ) {
     114    // Bail if something already has this under control
     115    if ( ! empty( $bbp->theme_compat->theme ) ) {
     116        return;
     117    }
     118
     119    // Fallback for empty theme
     120    if ( empty( $theme ) ) {
    106121        $theme = 'default';
    107122    }
    108123
    109     // Try to set the active theme compat theme. If it's not in the registered
    110     // packages array, it doesn't exist, so do nothing with it.
     124    // If the theme is registered, use it and add it to the stack
    111125    if ( isset( $bbp->theme_compat->packages[ $theme ] ) ) {
    112126        $bbp->theme_compat->theme = $bbp->theme_compat->packages[ $theme ];
    113     }
     127
     128        // Setup the template stack for the active template pack
     129        bbp_register_template_stack( array( $bbp->theme_compat->theme, 'get_dir' ) );
     130    }
     131}
     132
     133/**
     134 * Get the current template pack package.
     135 *
     136 * @since 2.6.0 bbPress (r6548)
     137 *
     138 * @return BBP_Theme_Compat
     139 */
     140function bbp_get_current_template_pack() {
     141    $bbp = bbpress();
     142
     143    // Theme was not setup, so fallback to an empty object
     144    if ( empty( $bbp->theme_compat->theme ) ) {
     145        $bbp->theme_compat->theme = new BBP_Theme_Compat();
     146    }
     147
     148    // Filter & return
     149    return apply_filters( 'bbp_get_current_template_pack', $bbp->theme_compat->theme );
     150}
     151
     152/**
     153 * Gets the id of the bbPress compatible theme used, in the event the
     154 * currently active WordPress theme does not explicitly support bbPress.
     155 * This can be filtered or set manually. Tricky theme authors can override the
     156 * default and include their own bbPress compatibility layers for their themes.
     157 *
     158 * @since 2.0.0 bbPress (r3506)
     159 *
     160 * @uses apply_filters()
     161 * @return string
     162 */
     163function bbp_get_theme_compat_id() {
     164
     165    // Filter & return
     166    return apply_filters( 'bbp_get_theme_compat_id', bbp_get_current_template_pack()->id );
    114167}
    115168
     
    125178 * @return string
    126179 */
    127 function bbp_get_theme_compat_id() {
     180function bbp_get_theme_compat_name() {
    128181
    129182    // Filter & return
    130     return apply_filters( 'bbp_get_theme_compat_id', bbpress()->theme_compat->theme->id );
    131 }
    132 
    133 /**
    134  * Gets the name of the bbPress compatible theme used, in the event the
    135  * currently active WordPress theme does not explicitly support bbPress.
    136  * This can be filtered or set manually. Tricky theme authors can override the
    137  * default and include their own bbPress compatibility layers for their themes.
    138  *
    139  * @since 2.0.0 bbPress (r3506)
    140  *
    141  * @uses apply_filters()
    142  * @return string
    143  */
    144 function bbp_get_theme_compat_name() {
    145 
    146     // Filter & return
    147     return apply_filters( 'bbp_get_theme_compat_name', bbpress()->theme_compat->theme->name );
     183    return apply_filters( 'bbp_get_theme_compat_name', bbp_get_current_template_pack()->name );
    148184}
    149185
     
    162198
    163199    // Filter & return
    164     return apply_filters( 'bbp_get_theme_compat_version', bbpress()->theme_compat->theme->version );
     200    return apply_filters( 'bbp_get_theme_compat_version', bbp_get_current_template_pack()->version );
    165201}
    166202
     
    179215
    180216    // Filter & return
    181     return apply_filters( 'bbp_get_theme_compat_dir', bbpress()->theme_compat->theme->dir );
     217    return apply_filters( 'bbp_get_theme_compat_dir', bbp_get_current_template_pack()->dir );
    182218}
    183219
     
    196232
    197233    // Filter & return
    198     return apply_filters( 'bbp_get_theme_compat_url', bbpress()->theme_compat->theme->url );
     234    return apply_filters( 'bbp_get_theme_compat_url', bbp_get_current_template_pack()->url );
    199235}
    200236
     
    273309
    274310/**
    275  * Set the theme compat original_template global
     311 * Is a template the original_template global
    276312 *
    277313 * Stash the original template file for the current query. Useful for checking
     
    283319    $bbp = bbpress();
    284320
     321    // Bail if no original template
    285322    if ( empty( $bbp->theme_compat->original_template ) ) {
    286323        return false;
     
    317354    }
    318355}
     356
    319357/**
    320358 * This fun little function fills up some WordPress globals with dummy data to
  • trunk/src/includes/forums/template.php

    r6545 r6551  
    906906        // Parse the arguments
    907907        $r = bbp_parse_args( $args, array(
    908             'user_id'     => 0,
    909             'forum_id'    => 0,
     908            'user_id'     => bbp_get_current_user_id(),
     909            'object_id'   => bbp_get_forum_id(),
     910            'object_type' => 'post',
    910911            'before'      => '',
    911912            'after'       => '',
  • trunk/src/includes/topics/template.php

    r6544 r6551  
    19931993
    19941994    /**
    1995      * Get the forum subscription link
     1995     * Get the topic subscription link
    19961996     *
    19971997     * A custom wrapper for bbp_get_user_subscribe_link()
     
    20162016        // Parse the arguments
    20172017        $r = bbp_parse_args( $args, array(
    2018             'user_id'     => 0,
    2019             'topic_id'    => 0,
     2018            'user_id'     => bbp_get_current_user_id(),
     2019            'object_id'   => bbp_get_topic_id(),
     2020            'object_type' => 'post',
    20202021            'before'      => ' | ',
    20212022            'after'       => '',
     
    20702071        // Parse the arguments
    20712072        $r = bbp_parse_args( $args, array(
    2072             'user_id'     => 0,
    2073             'topic_id'    => 0,
     2073            'user_id'     => bbp_get_current_user_id(),
     2074            'object_id'   => bbp_get_topic_id(),
     2075            'object_type' => 'post',
    20742076            'before'      => '',
    20752077            'after'       => '',
  • trunk/src/includes/users/template.php

    r6544 r6551  
    11601160            'favorited'   => __( 'Unfavorite', 'bbpress' ),
    11611161            'user_id'     => 0,
    1162             'topic_id'    => 0,
     1162            'object_id'   => 0,
     1163            'object_type' => 'post',
    11631164            'before'      => '',
    11641165            'after'       => '',
    1165             'redirect_to' => ''
     1166            'redirect_to' => '',
     1167
     1168            // Deprecated. Use object_id.
     1169            'forum_id'    => 0,
     1170            'topic_id'    => 0
    11661171        ), 'get_user_favorites_link' );
    11671172
    1168         // Validate user and topic ID's
    1169         $user_id   = bbp_get_user_id( $r['user_id'], true, true );
    1170         $object_id = bbp_get_topic_id( $r['topic_id'] );
     1173        // Validate user and object ID's
     1174        $user_id     = bbp_get_user_id( $r['user_id'], true, true );
     1175        $object_type = sanitize_key( $r['object_type'] );
     1176
     1177        // Back-compat for deprecated arguments
     1178        if ( ! empty( $r['topic_id'] ) ) {
     1179            $object_id = absint( $r['topic_id'] );
     1180        } elseif ( ! empty( $r['forum_id'] ) ) {
     1181            $object_id = absint( $r['forum_id'] );
     1182        } else {
     1183            $object_id = absint( $r['object_id'] );
     1184        }
    11711185
    11721186        // Bail if empty
    1173         if ( empty( $user_id ) || empty( $object_id ) ) {
     1187        if ( empty( $user_id ) || empty( $object_id ) || empty( $object_type ) ) {
    11741188            return false;
    11751189        }
     
    11841198        if ( ! empty( $is_fav ) ) {
    11851199            $text   = $r['favorited'];
    1186             $q_args = array( 'action' => 'bbp_favorite_remove', 'object_id' => $object_id );
     1200            $q_args = array(
     1201                'action'    => 'bbp_favorite_remove',
     1202                'object_id' => $object_id
     1203            );
    11871204        } else {
    11881205            $text   = $r['favorite'];
    1189             $q_args = array( 'action' => 'bbp_favorite_add',    'object_id' => $object_id );
     1206            $q_args = array(
     1207                'action'    => 'bbp_favorite_add',
     1208                'object_id' => $object_id
     1209            );
    11901210        }
    11911211
     
    11981218        $url  = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-favorite_' . $object_id ) );
    11991219        $sub  = $is_fav ? ' class="is-favorite"' : '';
    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'] );
     1220        $html = sprintf( '%s<span id="favorite-%d"  %s><a href="%s" class="favorite-toggle" data-bbp-object-id="%d" data-bbp-object-type="%s" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $object_id, $sub, $url, $object_id, $object_type, wp_create_nonce( 'toggle-favorite_' . $object_id ), $text, $r['after'] );
    12011221
    12021222        // Initial output is wrapped in a span, ajax output is hooked to this
     
    13521372        // Parse arguments against default values
    13531373        $r = bbp_parse_args( $args, array(
    1354             'subscribe'   => __( 'Subscribe',   'bbpress' ),
    1355             'unsubscribe' => __( 'Unsubscribe', 'bbpress' ),
     1374            'subscribe'   => esc_html__( 'Subscribe',   'bbpress' ),
     1375            'unsubscribe' => esc_html__( 'Unsubscribe', 'bbpress' ),
    13561376            'user_id'     => 0,
    1357             'topic_id'    => 0,
     1377            'object_id'   => 0,
     1378            'object_type' => 'post',
     1379            'before'      => '',
     1380            'after'       => '',
     1381            'redirect_to' => '',
     1382
     1383            // Deprecated. Use object_id.
    13581384            'forum_id'    => 0,
    1359             'before'      => '&nbsp;|&nbsp;',
    1360             'after'       => '',
    1361             'redirect_to' => ''
     1385            'topic_id'    => 0
    13621386        ), 'get_user_subscribe_link' );
    13631387
    1364         // Validate user and object ID's
    1365         $user_id  = bbp_get_user_id( $r['user_id'], true, true );
    1366         $topic_id = bbp_get_topic_id( $r['topic_id'] );
    1367         $forum_id = bbp_get_forum_id( $r['forum_id'] );
     1388        // Validate user
     1389        $user_id     = bbp_get_user_id( $r['user_id'], true, true );
     1390        $object_type = sanitize_key( $r['object_type'] );
     1391
     1392        // Back-compat for deprecated arguments
     1393        if ( ! empty( $r['topic_id'] ) ) {
     1394            $object_id = absint( $r['topic_id'] );
     1395        } elseif ( ! empty( $r['forum_id'] ) ) {
     1396            $object_id = absint( $r['forum_id'] );
     1397        } else {
     1398            $object_id = absint( $r['object_id'] );
     1399        }
    13681400
    13691401        // Bail if anything is missing
    1370         if ( empty( $user_id ) || ( empty( $topic_id ) && empty( $forum_id ) ) ) {
     1402        if ( empty( $user_id ) || empty( $object_id ) || empty( $object_type ) ) {
    13711403            return false;
    13721404        }
     
    13751407        if ( ! current_user_can( 'edit_user', $user_id ) ) {
    13761408            return false;
    1377         }
    1378 
    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;
    13841409        }
    13851410
     
    13881413        if ( ! empty( $is_subscribed ) ) {
    13891414            $text   = $r['unsubscribe'];
    1390             $q_args = array( 'action' => 'bbp_unsubscribe', 'object_id' => $object_id );
     1415            $q_args = array(
     1416                'action'      => 'bbp_unsubscribe',
     1417                'object_id'   => $object_id,
     1418                'object_type' => $object_type
     1419            );
    13911420        } else {
    13921421            $text   = $r['subscribe'];
    1393             $q_args = array( 'action' => 'bbp_subscribe',   'object_id' => $object_id );
     1422            $q_args = array(
     1423                'action'      => 'bbp_subscribe',
     1424                'object_id'   => $object_id,
     1425                'object_type' => $object_type
     1426            );
    13941427        }
    13951428
     
    14021435        $url  = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-subscription_' . $object_id ) );
    14031436        $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'] );
     1437        $html = sprintf( '%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-bbp-object-id="%d" data-bbp-object-type="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $object_id, $sub, $url, $object_id, $object_type, wp_create_nonce( 'toggle-subscription_' . $object_id ), $text, $r['after'] );
    14051438
    14061439        // Initial output is wrapped in a span, ajax output is hooked to this
  • trunk/src/templates/default/bbpress-functions.php

    r6541 r6551  
    7272        /** Scripts ***********************************************************/
    7373
    74         add_action( 'bbp_enqueue_scripts',         array( $this, 'enqueue_styles'          ) ); // Enqueue theme CSS
    75         add_action( 'bbp_enqueue_scripts',         array( $this, 'enqueue_scripts'         ) ); // Enqueue theme JS
    76         add_filter( 'bbp_enqueue_scripts',         array( $this, 'localize_topic_script'   ) ); // Enqueue theme script localization
    77         add_action( 'bbp_ajax_favorite',           array( $this, 'ajax_favorite'           ) ); // Handles the topic ajax favorite/unfavorite
    78         add_action( 'bbp_ajax_subscription',       array( $this, 'ajax_subscription'       ) ); // Handles the topic ajax subscribe/unsubscribe
    79         add_action( 'bbp_ajax_forum_subscription', array( $this, 'ajax_forum_subscription' ) ); // Handles the forum ajax subscribe/unsubscribe
     74        add_action( 'bbp_enqueue_scripts',         array( $this, 'enqueue_styles'        ) ); // Enqueue theme CSS
     75        add_action( 'bbp_enqueue_scripts',         array( $this, 'enqueue_scripts'       ) ); // Enqueue theme JS
     76        add_filter( 'bbp_enqueue_scripts',         array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization
     77        add_action( 'bbp_ajax_favorite',           array( $this, 'ajax_favorite'         ) ); // Handles the topic ajax favorite/unfavorite
     78        add_action( 'bbp_ajax_subscription',       array( $this, 'ajax_subscription'     ) ); // Handles the topic ajax subscribe/unsubscribe
    8079
    8180        /** Template Wrappers *************************************************/
     
    175174        // Forum-specific scripts
    176175        if ( bbp_is_single_forum() ) {
    177             $scripts['bbpress-forum'] = array(
    178                 'file'         => 'js/forum' . $suffix . '.js',
     176            $scripts['bbpress-engagements'] = array(
     177                'file'         => 'js/engagements' . $suffix . '.js',
    179178                'dependencies' => array( 'jquery' )
    180179            );
     
    184183        if ( bbp_is_single_topic() || bbp_is_topic_edit() ) {
    185184
    186             // Topic favorite/unsubscribe
    187             $scripts['bbpress-topic'] = array(
    188                 'file'         => 'js/topic' . $suffix . '.js',
     185            // Engagements
     186            $scripts['bbpress-engagements'] = array(
     187                'file'         => 'js/engagements' . $suffix . '.js',
    189188                'dependencies' => array( 'jquery' )
    190189            );
     
    229228    public function localize_topic_script() {
    230229
    231         // Single forum
    232         if ( bbp_is_single_forum() ) {
    233             wp_localize_script( 'bbpress-forum', 'bbpForumJS', array(
    234                 'forum_id'           => get_the_ID(),
     230        // Single forum or topic
     231        if ( bbp_is_single_forum() || bbp_is_single_topic() ) {
     232            wp_localize_script( 'bbpress-engagements', 'bbpEngagementJS', array(
     233                'object_id'          => get_the_ID(),
    235234                'bbp_ajaxurl'        => bbp_get_ajax_url(),
    236235                'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
    237236            ) );
    238 
    239         // Single topic
    240         } elseif ( bbp_is_single_topic() ) {
    241             wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array(
    242                 'topic_id'           => get_the_ID(),
    243                 'bbp_ajaxurl'        => bbp_get_ajax_url(),
    244                 'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
    245             ) );
    246         }
    247     }
    248 
    249     /**
    250      * AJAX handler to Subscribe/Unsubscribe a user from a forum
    251      *
    252      * @since 2.5.0 bbPress (r5155)
    253      *
    254      * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
    255      * @uses bbp_is_user_logged_in() To check if user is logged in
    256      * @uses bbp_get_current_user_id() To get the current user id
    257      * @uses current_user_can() To check if the current user can edit the user
    258      * @uses bbp_get_forum() To get the forum
    259      * @uses wp_verify_nonce() To verify the nonce
    260      * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions
    261      * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions
    262      * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions
    263      * @uses bbp_ajax_response() To return JSON
    264      */
    265     public function ajax_forum_subscription() {
    266 
    267         // Bail if subscriptions are not active
    268         if ( ! bbp_is_subscriptions_active() ) {
    269             bbp_ajax_response( false, esc_html__( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
    270         }
    271 
    272         // Bail if user is not logged in
    273         if ( ! is_user_logged_in() ) {
    274             bbp_ajax_response( false, esc_html__( 'Please login to subscribe to this forum.', 'bbpress' ), 301 );
    275         }
    276 
    277         // Get user and forum data
    278         $user_id = bbp_get_current_user_id();
    279         $id      = intval( $_POST['id'] );
    280 
    281         // Bail if user cannot add favorites for this user
    282         if ( ! current_user_can( 'edit_user', $user_id ) ) {
    283             bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 );
    284         }
    285 
    286         // Get the forum
    287         $forum = bbp_get_forum( $id );
    288 
    289         // Bail if forum cannot be found
    290         if ( empty( $forum ) ) {
    291             bbp_ajax_response( false, esc_html__( 'The forum could not be found.', 'bbpress' ), 303 );
    292         }
    293 
    294         // Bail if user did not take this action
    295         if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $forum->ID ) ) {
    296             bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
    297         }
    298 
    299         // Take action
    300         $status = bbp_is_user_subscribed( $user_id, $forum->ID ) ? bbp_remove_user_subscription( $user_id, $forum->ID ) : bbp_add_user_subscription( $user_id, $forum->ID );
    301 
    302         // Bail if action failed
    303         if ( empty( $status ) ) {
    304             bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
    305         }
    306 
    307         // Put subscription attributes in convenient array
    308         $attrs = array(
    309             'forum_id' => $forum->ID,
    310             'user_id'  => $user_id
    311         );
    312 
    313         // Action succeeded
    314         bbp_ajax_response( true, bbp_get_forum_subscription_link( $attrs, $user_id, false ), 200 );
     237        }
    315238    }
    316239
     
    340263        // Bail if user is not logged in
    341264        if ( ! is_user_logged_in() ) {
    342             bbp_ajax_response( false, esc_html__( 'Please login to make this topic a favorite.', 'bbpress' ), 301 );
     265            bbp_ajax_response( false, esc_html__( 'Please login to favorite.', 'bbpress' ), 301 );
    343266        }
    344267
    345268        // Get user and topic data
    346269        $user_id = bbp_get_current_user_id();
    347         $id      = ! empty( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
     270        $id      = ! empty( $_POST['id']   ) ? intval( $_POST['id'] )         : 0;
     271        $type    = ! empty( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : 'post';
    348272
    349273        // Bail if user cannot add favorites for this user
     
    352276        }
    353277
    354         // Get the topic
    355         $topic = bbp_get_topic( $id );
     278        // Get the object
     279        if ( 'post' === $type ) {
     280            $object = get_post( $id );
     281        }
    356282
    357283        // Bail if topic cannot be found
    358         if ( empty( $topic ) ) {
    359             bbp_ajax_response( false, esc_html__( 'The topic could not be found.', 'bbpress' ), 303 );
     284        if ( empty( $object ) ) {
     285            bbp_ajax_response( false, esc_html__( 'Favorite failed.', 'bbpress' ), 303 );
    360286        }
    361287
    362288        // Bail if user did not take this action
    363         if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $topic->ID ) ) {
     289        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $object->ID ) ) {
    364290            bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
    365291        }
    366292
    367293        // Take action
    368         $status = bbp_is_user_favorite( $user_id, $topic->ID ) ? bbp_remove_user_favorite( $user_id, $topic->ID ) : bbp_add_user_favorite( $user_id, $topic->ID );
     294        $status = bbp_is_user_favorite( $user_id, $object->ID )
     295            ? bbp_remove_user_favorite( $user_id, $object->ID )
     296            : bbp_add_user_favorite( $user_id, $object->ID );
    369297
    370298        // Bail if action failed
     
    375303        // Put subscription attributes in convenient array
    376304        $attrs = array(
    377             'topic_id' => $topic->ID,
    378             'user_id'  => $user_id
     305            'object_id' => $object->ID,
     306            'user_id'   => $user_id
    379307        );
    380308
     
    408336        // Bail if user is not logged in
    409337        if ( ! is_user_logged_in() ) {
    410             bbp_ajax_response( false, esc_html__( 'Please login to subscribe to this topic.', 'bbpress' ), 301 );
     338            bbp_ajax_response( false, esc_html__( 'Please login to subscribe.', 'bbpress' ), 301 );
    411339        }
    412340
    413341        // Get user and topic data
    414342        $user_id = bbp_get_current_user_id();
    415         $id      = intval( $_POST['id'] );
     343        $id      = ! empty( $_POST['id']   ) ? intval( $_POST['id'] )         : 0;
     344        $type    = ! empty( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : 'post';
    416345
    417346        // Bail if user cannot add favorites for this user
     
    420349        }
    421350
    422         // Get the topic
    423         $topic = bbp_get_topic( $id );
     351        // Get the object
     352        if ( 'post' === $type ) {
     353            $object = get_post( $id );
     354        }
    424355
    425356        // Bail if topic cannot be found
    426         if ( empty( $topic ) ) {
    427             bbp_ajax_response( false, esc_html__( 'The topic could not be found.', 'bbpress' ), 303 );
     357        if ( empty( $object ) ) {
     358            bbp_ajax_response( false, esc_html__( 'Subcription failed.', 'bbpress' ), 303 );
    428359        }
    429360
    430361        // Bail if user did not take this action
    431         if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $topic->ID ) ) {
     362        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $object->ID ) ) {
    432363            bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
    433364        }
    434365
    435366        // Take action
    436         $status = bbp_is_user_subscribed( $user_id, $topic->ID ) ? bbp_remove_user_subscription( $user_id, $topic->ID ) : bbp_add_user_subscription( $user_id, $topic->ID );
     367        $status = bbp_is_user_subscribed( $user_id, $object->ID )
     368            ? bbp_remove_user_subscription( $user_id, $object->ID )
     369            : bbp_add_user_subscription( $user_id, $object->ID );
    437370
    438371        // Bail if action failed
     
    443376        // Put subscription attributes in convenient array
    444377        $attrs = array(
    445             'topic_id' => $topic->ID,
    446             'user_id'  => $user_id
     378            'object_id' => $object->ID,
     379            'user_id'   => $user_id
    447380        );
     381
     382        // Add separator to topic if favorites is active
     383        if ( ( 'post' === $type ) && ( bbp_get_topic_post_type() === get_post_type( $object ) ) && bbp_is_favorites_active() ) {
     384            $attrs['before'] = '&nbsp;|&nbsp;';
     385        }
    448386
    449387        // Action succeeded
  • trunk/src/templates/default/js/engagements.js

    r6550 r6551  
    1 /* global bbpTopicJS */
     1/* global bbpEngagementJS */
    22jQuery( document ).ready( function ( $ ) {
    33
    4     function bbp_ajax_call( action, topic_id, nonce, update_selector ) {
     4    function bbp_ajax_call( action, object, type, nonce, update_selector ) {
    55        var $data = {
    66            action : action,
    7             id     : topic_id,
     7            id     : object,
     8            type   : type,
    89            nonce  : nonce
    910        };
    1011
    11         $.post( bbpTopicJS.bbp_ajaxurl, $data, function ( response ) {
     12        $.post( bbpEngagementJS.bbp_ajaxurl, $data, function ( response ) {
    1213            if ( response.success ) {
    1314                $( update_selector ).html( response.content );
    1415            } else {
    1516                if ( !response.content ) {
    16                     response.content = bbpTopicJS.generic_ajax_error;
     17                    response.content = bbpEngagementJS.generic_ajax_error;
    1718                }
    1819                window.alert( response.content );
     
    2324    $( '#favorite-toggle' ).on( 'click', 'span a.favorite-toggle', function( e ) {
    2425        e.preventDefault();
    25         bbp_ajax_call( 'favorite', $( this ).attr( 'data-topic' ), $( this ).data( 'bbp-nonce' ), '#favorite-toggle' );
     26        bbp_ajax_call(
     27            'favorite',
     28            $( this ).data( 'bbp-object-id'   ),
     29            $( this ).data( 'bbp-object-type' ),
     30            $( this ).data( 'bbp-nonce'       ),
     31            '#favorite-toggle'
     32        );
    2633    } );
    2734
    2835    $( '#subscription-toggle' ).on( 'click', 'span a.subscription-toggle', function( e ) {
    2936        e.preventDefault();
    30         bbp_ajax_call( 'subscription', $( this ).attr( 'data-topic' ), $( this ).data( 'bbp-nonce' ), '#subscription-toggle' );
     37        bbp_ajax_call(
     38            'subscription',
     39            $( this ).data( 'bbp-object-id'   ),
     40            $( this ).data( 'bbp-object-type' ),
     41            $( this ).data( 'bbp-nonce'       ),
     42            '#subscription-toggle'
     43        );
    3144    } );
    3245
Note: See TracChangeset for help on using the changeset viewer.