Changeset 6551
- Timestamp:
- 06/15/2017 04:31:59 AM (9 years ago)
- Location:
- trunk/src
- Files:
-
- 1 deleted
- 8 edited
- 1 moved
-
bbpress.php (modified) (3 diffs)
-
includes/core/actions.php (modified) (3 diffs)
-
includes/core/template-functions.php (modified) (2 diffs)
-
includes/core/theme-compat.php (modified) (11 diffs)
-
includes/forums/template.php (modified) (1 diff)
-
includes/topics/template.php (modified) (3 diffs)
-
includes/users/template.php (modified) (7 diffs)
-
templates/default/bbpress-functions.php (modified) (10 diffs)
-
templates/default/js/engagements.js (moved) (moved from trunk/src/templates/default/js/topic.js) (2 diffs)
-
templates/default/js/forum.js (deleted)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bbpress.php
r6547 r6551 210 210 /** Versions **********************************************************/ 211 211 212 $this->version = '2.6-rc-65 41';212 $this->version = '2.6-rc-6550'; 213 213 $this->db_version = '262'; 214 214 … … 446 446 public function register_theme_packages() { 447 447 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 448 452 // Register the default theme compatibility package 449 453 bbp_register_theme_package( array( 450 454 'id' => 'default', 451 'name' => __( 'bbPress Default', 'bbpress' ),455 'name' => 'bbPress Default', 452 456 'version' => bbp_get_version(), 453 457 'dir' => trailingslashit( $this->themes_dir . 'default' ), 454 458 'url' => trailingslashit( $this->themes_url . 'default' ) 455 459 ) ); 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 );461 460 } 462 461 … … 467 466 */ 468 467 public function setup_theme() { 469 470 // Bail if something already has this under control471 if ( ! empty( $this->theme_compat->theme ) ) {472 return;473 }474 475 // Setup the theme package to use for compatibility476 468 bbp_setup_theme_compat( bbp_get_theme_package_id() ); 477 469 } -
trunk/src/includes/core/actions.php
r6544 r6551 41 41 add_action( 'init', 'bbp_init', 0 ); // Early for bbp_register 42 42 add_action( 'parse_query', 'bbp_parse_query', 2 ); // Early for overrides 43 add_action( 'generate_rewrite_rules', 'bbp_generate_rewrite_rules', 10 ); 44 add_action( 'after_setup_theme', 'bbp_after_setup_theme', 10 ); 45 add_action( 'setup_theme', 'bbp_setup_theme', 10 ); 46 add_action( 'set_current_user', 'bbp_setup_current_user', 10 ); 47 add_action( 'profile_update', 'bbp_profile_update', 10, 2 ); // user_id and old_user_data 48 add_action( 'user_register', 'bbp_user_register', 10 ); 49 add_action( 'login_form_login', 'bbp_login_form_login', 10 ); 50 add_action( 'template_redirect', 'bbp_template_redirect', 8 ); // Before BuddyPress's 10 [BB2225] 43 51 add_action( 'widgets_init', 'bbp_widgets_init', 10 ); 44 52 add_action( 'wp_roles_init', 'bbp_roles_init', 10 ); 45 add_action( 'generate_rewrite_rules', 'bbp_generate_rewrite_rules', 10 );46 53 add_action( 'wp_enqueue_scripts', 'bbp_enqueue_scripts', 10 ); 47 54 add_action( 'wp_head', 'bbp_head', 10 ); 48 55 add_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_data55 add_action( 'user_register', 'bbp_user_register', 10 );56 56 57 57 /** … … 69 69 add_action( 'bbp_loaded', 'bbp_setup_user_option_filters', 12 ); 70 70 add_action( 'bbp_loaded', 'bbp_pre_load_options', 14 ); 71 add_action( 'bbp_loaded', 'bbp_register_theme_packages', 16 );72 71 73 72 /** … … 79 78 */ 80 79 add_action( 'bbp_init', 'bbp_load_textdomain', 0 ); 81 add_action( 'bbp_init', 'bbp_register', 0);80 add_action( 'bbp_init', 'bbp_register', 10 ); 82 81 add_action( 'bbp_init', 'bbp_add_rewrite_tags', 20 ); 83 82 add_action( 'bbp_init', 'bbp_add_rewrite_rules', 30 ); 84 83 add_action( 'bbp_init', 'bbp_add_permastructs', 40 ); 85 84 add_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 */ 93 add_action( 'bbp_setup_theme', 'bbp_register_theme_packages', 2 ); // Lower than 5 86 94 87 95 /** -
trunk/src/includes/core/template-functions.php
r6549 r6551 284 284 * @param string $location_callback Callback function that returns the 285 285 * @param int $priority 286 * @ see bbp_register_template_stack()286 * @return bool Whether stack was removed 287 287 */ 288 288 function bbp_deregister_template_stack( $location_callback = '', $priority = 10 ) { … … 300 300 * Call the functions added to the 'bbp_template_stack' filter hook, and return 301 301 * an array of the template locations. 302 *303 * @see bbp_register_template_stack()304 302 * 305 303 * @since 2.2.0 bbPress (r4323) -
trunk/src/includes/core/theme-compat.php
r6501 r6551 29 29 * 30 30 * 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() 33 32 * 34 33 * @since 2.0.0 bbPress (r3506) … … 88 87 : ''; 89 88 } 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 } 90 100 } 91 101 … … 93 103 94 104 /** 95 * Setup the default theme compat theme105 * Setup the active template pack and register it's directory in the stack. 96 106 * 97 107 * @since 2.0.0 bbPress (r3311) … … 99 109 * @param BBP_Theme_Compat $theme 100 110 */ 101 function bbp_setup_theme_compat( $theme = ' ' ) {111 function bbp_setup_theme_compat( $theme = 'default' ) { 102 112 $bbp = bbpress(); 103 113 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 ) ) { 106 121 $theme = 'default'; 107 122 } 108 123 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 111 125 if ( isset( $bbp->theme_compat->packages[ $theme ] ) ) { 112 126 $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 */ 140 function 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 */ 163 function bbp_get_theme_compat_id() { 164 165 // Filter & return 166 return apply_filters( 'bbp_get_theme_compat_id', bbp_get_current_template_pack()->id ); 114 167 } 115 168 … … 125 178 * @return string 126 179 */ 127 function bbp_get_theme_compat_ id() {180 function bbp_get_theme_compat_name() { 128 181 129 182 // 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 ); 148 184 } 149 185 … … 162 198 163 199 // Filter & return 164 return apply_filters( 'bbp_get_theme_compat_version', bbp ress()->theme_compat->theme->version );200 return apply_filters( 'bbp_get_theme_compat_version', bbp_get_current_template_pack()->version ); 165 201 } 166 202 … … 179 215 180 216 // Filter & return 181 return apply_filters( 'bbp_get_theme_compat_dir', bbp ress()->theme_compat->theme->dir );217 return apply_filters( 'bbp_get_theme_compat_dir', bbp_get_current_template_pack()->dir ); 182 218 } 183 219 … … 196 232 197 233 // Filter & return 198 return apply_filters( 'bbp_get_theme_compat_url', bbp ress()->theme_compat->theme->url );234 return apply_filters( 'bbp_get_theme_compat_url', bbp_get_current_template_pack()->url ); 199 235 } 200 236 … … 273 309 274 310 /** 275 * Set the theme compatoriginal_template global311 * Is a template the original_template global 276 312 * 277 313 * Stash the original template file for the current query. Useful for checking … … 283 319 $bbp = bbpress(); 284 320 321 // Bail if no original template 285 322 if ( empty( $bbp->theme_compat->original_template ) ) { 286 323 return false; … … 317 354 } 318 355 } 356 319 357 /** 320 358 * This fun little function fills up some WordPress globals with dummy data to -
trunk/src/includes/forums/template.php
r6545 r6551 906 906 // Parse the arguments 907 907 $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', 910 911 'before' => '', 911 912 'after' => '', -
trunk/src/includes/topics/template.php
r6544 r6551 1993 1993 1994 1994 /** 1995 * Get the forumsubscription link1995 * Get the topic subscription link 1996 1996 * 1997 1997 * A custom wrapper for bbp_get_user_subscribe_link() … … 2016 2016 // Parse the arguments 2017 2017 $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', 2020 2021 'before' => ' | ', 2021 2022 'after' => '', … … 2070 2071 // Parse the arguments 2071 2072 $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', 2074 2076 'before' => '', 2075 2077 'after' => '', -
trunk/src/includes/users/template.php
r6544 r6551 1160 1160 'favorited' => __( 'Unfavorite', 'bbpress' ), 1161 1161 'user_id' => 0, 1162 'topic_id' => 0, 1162 'object_id' => 0, 1163 'object_type' => 'post', 1163 1164 'before' => '', 1164 1165 'after' => '', 1165 'redirect_to' => '' 1166 'redirect_to' => '', 1167 1168 // Deprecated. Use object_id. 1169 'forum_id' => 0, 1170 'topic_id' => 0 1166 1171 ), 'get_user_favorites_link' ); 1167 1172 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 } 1171 1185 1172 1186 // Bail if empty 1173 if ( empty( $user_id ) || empty( $object_id ) ) {1187 if ( empty( $user_id ) || empty( $object_id ) || empty( $object_type ) ) { 1174 1188 return false; 1175 1189 } … … 1184 1198 if ( ! empty( $is_fav ) ) { 1185 1199 $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 ); 1187 1204 } else { 1188 1205 $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 ); 1190 1210 } 1191 1211 … … 1198 1218 $url = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-favorite_' . $object_id ) ); 1199 1219 $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'] ); 1201 1221 1202 1222 // Initial output is wrapped in a span, ajax output is hooked to this … … 1352 1372 // Parse arguments against default values 1353 1373 $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' ), 1356 1376 '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. 1358 1384 'forum_id' => 0, 1359 'before' => ' | ', 1360 'after' => '', 1361 'redirect_to' => '' 1385 'topic_id' => 0 1362 1386 ), 'get_user_subscribe_link' ); 1363 1387 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 } 1368 1400 1369 1401 // 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 ) ) { 1371 1403 return false; 1372 1404 } … … 1375 1407 if ( ! current_user_can( 'edit_user', $user_id ) ) { 1376 1408 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;1384 1409 } 1385 1410 … … 1388 1413 if ( ! empty( $is_subscribed ) ) { 1389 1414 $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 ); 1391 1420 } else { 1392 1421 $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 ); 1394 1427 } 1395 1428 … … 1402 1435 $url = esc_url( wp_nonce_url( add_query_arg( $q_args ), 'toggle-subscription_' . $object_id ) ); 1403 1436 $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'] ); 1405 1438 1406 1439 // Initial output is wrapped in a span, ajax output is hooked to this -
trunk/src/templates/default/bbpress-functions.php
r6541 r6551 72 72 /** Scripts ***********************************************************/ 73 73 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 80 79 81 80 /** Template Wrappers *************************************************/ … … 175 174 // Forum-specific scripts 176 175 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', 179 178 'dependencies' => array( 'jquery' ) 180 179 ); … … 184 183 if ( bbp_is_single_topic() || bbp_is_topic_edit() ) { 185 184 186 // Topic favorite/unsubscribe187 $scripts['bbpress- topic'] = array(188 'file' => 'js/ topic' . $suffix . '.js',185 // Engagements 186 $scripts['bbpress-engagements'] = array( 187 'file' => 'js/engagements' . $suffix . '.js', 189 188 'dependencies' => array( 'jquery' ) 190 189 ); … … 229 228 public function localize_topic_script() { 230 229 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(), 235 234 'bbp_ajaxurl' => bbp_get_ajax_url(), 236 235 'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), 237 236 ) ); 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 } 315 238 } 316 239 … … 340 263 // Bail if user is not logged in 341 264 if ( ! is_user_logged_in() ) { 342 bbp_ajax_response( false, esc_html__( 'Please login to make this topic afavorite.', 'bbpress' ), 301 );265 bbp_ajax_response( false, esc_html__( 'Please login to favorite.', 'bbpress' ), 301 ); 343 266 } 344 267 345 268 // Get user and topic data 346 269 $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'; 348 272 349 273 // Bail if user cannot add favorites for this user … … 352 276 } 353 277 354 // Get the topic 355 $topic = bbp_get_topic( $id ); 278 // Get the object 279 if ( 'post' === $type ) { 280 $object = get_post( $id ); 281 } 356 282 357 283 // 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 ); 360 286 } 361 287 362 288 // 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 ) ) { 364 290 bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); 365 291 } 366 292 367 293 // 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 ); 369 297 370 298 // Bail if action failed … … 375 303 // Put subscription attributes in convenient array 376 304 $attrs = array( 377 ' topic_id' => $topic->ID,378 'user_id' => $user_id305 'object_id' => $object->ID, 306 'user_id' => $user_id 379 307 ); 380 308 … … 408 336 // Bail if user is not logged in 409 337 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 ); 411 339 } 412 340 413 341 // Get user and topic data 414 342 $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'; 416 345 417 346 // Bail if user cannot add favorites for this user … … 420 349 } 421 350 422 // Get the topic 423 $topic = bbp_get_topic( $id ); 351 // Get the object 352 if ( 'post' === $type ) { 353 $object = get_post( $id ); 354 } 424 355 425 356 // 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 ); 428 359 } 429 360 430 361 // 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 ) ) { 432 363 bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); 433 364 } 434 365 435 366 // 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 ); 437 370 438 371 // Bail if action failed … … 443 376 // Put subscription attributes in convenient array 444 377 $attrs = array( 445 ' topic_id' => $topic->ID,446 'user_id' => $user_id378 'object_id' => $object->ID, 379 'user_id' => $user_id 447 380 ); 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'] = ' | '; 385 } 448 386 449 387 // Action succeeded -
trunk/src/templates/default/js/engagements.js
r6550 r6551 1 /* global bbp TopicJS */1 /* global bbpEngagementJS */ 2 2 jQuery( document ).ready( function ( $ ) { 3 3 4 function bbp_ajax_call( action, topic_id, nonce, update_selector ) {4 function bbp_ajax_call( action, object, type, nonce, update_selector ) { 5 5 var $data = { 6 6 action : action, 7 id : topic_id, 7 id : object, 8 type : type, 8 9 nonce : nonce 9 10 }; 10 11 11 $.post( bbp TopicJS.bbp_ajaxurl, $data, function ( response ) {12 $.post( bbpEngagementJS.bbp_ajaxurl, $data, function ( response ) { 12 13 if ( response.success ) { 13 14 $( update_selector ).html( response.content ); 14 15 } else { 15 16 if ( !response.content ) { 16 response.content = bbp TopicJS.generic_ajax_error;17 response.content = bbpEngagementJS.generic_ajax_error; 17 18 } 18 19 window.alert( response.content ); … … 23 24 $( '#favorite-toggle' ).on( 'click', 'span a.favorite-toggle', function( e ) { 24 25 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 ); 26 33 } ); 27 34 28 35 $( '#subscription-toggle' ).on( 'click', 'span a.subscription-toggle', function( e ) { 29 36 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 ); 31 44 } ); 32 45
Note: See TracChangeset
for help on using the changeset viewer.