Changeset 5156 for trunk/templates/default/bbpress-functions.php
- Timestamp:
- 11/20/2013 07:50:55 PM (12 years ago)
- File:
-
- 1 edited
-
trunk/templates/default/bbpress-functions.php (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/templates/default/bbpress-functions.php
r4944 r5156 85 85 /** Scripts ***********************************************************/ 86 86 87 add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); // Enqueue theme CSS 88 add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Enqueue theme JS 89 add_filter( 'bbp_enqueue_scripts', array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization 90 add_action( 'bbp_head', array( $this, 'head_scripts' ) ); // Output some extra JS in the <head> 91 add_action( 'bbp_ajax_favorite', array( $this, 'ajax_favorite' ) ); // Handles the ajax favorite/unfavorite 92 add_action( 'bbp_ajax_subscription', array( $this, 'ajax_subscription' ) ); // Handles the ajax subscribe/unsubscribe 87 add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); // Enqueue theme CSS 88 add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Enqueue theme JS 89 add_filter( 'bbp_enqueue_scripts', array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization 90 add_action( 'bbp_head', array( $this, 'head_scripts' ) ); // Output some extra JS in the <head> 91 add_action( 'bbp_ajax_favorite', array( $this, 'ajax_favorite' ) ); // Handles the topic ajax favorite/unfavorite 92 add_action( 'bbp_ajax_subscription', array( $this, 'ajax_subscription' ) ); // Handles the topic ajax subscribe/unsubscribe 93 add_action( 'bbp_ajax_forum_subscription', array( $this, 'ajax_forum_subscription' ) ); // Handles the forum ajax subscribe/unsubscribe 93 94 94 95 /** Template Wrappers *************************************************/ … … 169 170 * @since bbPress (r3732) 170 171 * 172 * @uses bbp_is_single_forum() To check if it's the forum page 171 173 * @uses bbp_is_single_topic() To check if it's the topic page 174 * @uses bbp_thread_replies() To check if threaded replies are enabled 172 175 * @uses bbp_is_single_user_edit() To check if it's the profile edit page 173 176 * @uses wp_enqueue_script() To enqueue the scripts … … 178 181 if ( bbp_use_wp_editor() ) { 179 182 wp_enqueue_script( 'jquery' ); 183 } 184 185 // Forum-specific scripts 186 if ( bbp_is_single_forum() ) { 187 188 // Forum subscribe/unsubscribe 189 wp_enqueue_script( 'bbpress-forum', $this->url . 'js/forum.js', array( 'jquery' ), $this->version ); 180 190 } 181 191 … … 286 296 * @since bbPress (r3732) 287 297 * 298 * @uses bbp_is_single_forum() To check if it's the forum page 288 299 * @uses bbp_is_single_topic() To check if it's the topic page 289 300 * @uses is_user_logged_in() To check if user is logged in 290 301 * @uses bbp_get_current_user_id() To get the current user id 302 * @uses bbp_get_forum_id() To get the forum id 291 303 * @uses bbp_get_topic_id() To get the topic id 292 304 * @uses bbp_get_favorites_permalink() To get the favorites permalink … … 299 311 public function localize_topic_script() { 300 312 301 // Bail if not viewing a single topic 302 if ( !bbp_is_single_topic() ) 303 return; 304 305 wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array( 306 'bbp_ajaxurl' => bbp_get_ajax_url(), 307 'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), 308 'is_user_logged_in' => is_user_logged_in(), 309 'fav_nonce' => wp_create_nonce( 'toggle-favorite_' . get_the_ID() ), 310 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 311 ) ); 313 // Single forum 314 if ( bbp_is_single_forum() ) { 315 wp_localize_script( 'bbpress-forum', 'bbpForumJS', array( 316 'bbp_ajaxurl' => bbp_get_ajax_url(), 317 'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), 318 'is_user_logged_in' => is_user_logged_in(), 319 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 320 ) ); 321 322 // Single topic 323 } elseif ( bbp_is_single_topic() ) { 324 wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array( 325 'bbp_ajaxurl' => bbp_get_ajax_url(), 326 'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), 327 'is_user_logged_in' => is_user_logged_in(), 328 'fav_nonce' => wp_create_nonce( 'toggle-favorite_' . get_the_ID() ), 329 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 330 ) ); 331 } 332 } 333 334 /** 335 * AJAX handler to Subscribe/Unsubscribe a user from a forum 336 * 337 * @since bbPress (r5155) 338 * 339 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 340 * @uses bbp_is_user_logged_in() To check if user is logged in 341 * @uses bbp_get_current_user_id() To get the current user id 342 * @uses current_user_can() To check if the current user can edit the user 343 * @uses bbp_get_forum() To get the forum 344 * @uses wp_verify_nonce() To verify the nonce 345 * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions 346 * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions 347 * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions 348 * @uses bbp_ajax_response() To return JSON 349 */ 350 public function ajax_forum_subscription() { 351 352 // Bail if subscriptions are not active 353 if ( ! bbp_is_subscriptions_active() ) { 354 bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 ); 355 } 356 357 // Bail if user is not logged in 358 if ( ! is_user_logged_in() ) { 359 bbp_ajax_response( false, __( 'Please login to subscribe to this forum.', 'bbpress' ), 301 ); 360 } 361 362 // Get user and forum data 363 $user_id = bbp_get_current_user_id(); 364 $id = intval( $_POST['id'] ); 365 366 // Bail if user cannot add favorites for this user 367 if ( ! current_user_can( 'edit_user', $user_id ) ) { 368 bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 ); 369 } 370 371 // Get the forum 372 $forum = bbp_get_forum( $id ); 373 374 // Bail if forum cannot be found 375 if ( empty( $forum ) ) { 376 bbp_ajax_response( false, __( 'The forum could not be found.', 'bbpress' ), 303 ); 377 } 378 379 // Bail if user did not take this action 380 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $forum->ID ) ) { 381 bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); 382 } 383 384 // Take action 385 $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 ); 386 387 // Bail if action failed 388 if ( empty( $status ) ) { 389 bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 ); 390 } 391 392 // Put subscription attributes in convenient array 393 $attrs = array( 394 'forum_id' => $forum->ID, 395 'user_id' => $user_id 396 ); 397 398 // Action succeeded 399 bbp_ajax_response( true, bbp_get_forum_subscription_link( $attrs, $user_id, false ), 200 ); 312 400 } 313 401 … … 317 405 * @since bbPress (r3732) 318 406 * 407 * @uses bbp_is_favorites_active() To check if favorites are active 408 * @uses bbp_is_user_logged_in() To check if user is logged in 319 409 * @uses bbp_get_current_user_id() To get the current user id 320 410 * @uses current_user_can() To check if the current user can edit the user … … 334 424 335 425 // Bail if user is not logged in 336 if ( ! is_user_logged_in() ) {426 if ( ! is_user_logged_in() ) { 337 427 bbp_ajax_response( false, __( 'Please login to make this topic a favorite.', 'bbpress' ), 301 ); 338 428 } … … 343 433 344 434 // Bail if user cannot add favorites for this user 345 if ( ! current_user_can( 'edit_user', $user_id ) ) {435 if ( ! current_user_can( 'edit_user', $user_id ) ) { 346 436 bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 ); 347 437 } … … 356 446 357 447 // Bail if user did not take this action 358 if ( ! isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $topic->ID ) ) {448 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $topic->ID ) ) { 359 449 bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); 360 450 } … … 384 474 * 385 475 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 476 * @uses bbp_is_user_logged_in() To check if user is logged in 386 477 * @uses bbp_get_current_user_id() To get the current user id 387 478 * @uses current_user_can() To check if the current user can edit the user … … 396 487 397 488 // Bail if subscriptions are not active 398 if ( ! bbp_is_subscriptions_active() ) {489 if ( ! bbp_is_subscriptions_active() ) { 399 490 bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 ); 400 491 } 401 492 402 493 // Bail if user is not logged in 403 if ( ! is_user_logged_in() ) {494 if ( ! is_user_logged_in() ) { 404 495 bbp_ajax_response( false, __( 'Please login to subscribe to this topic.', 'bbpress' ), 301 ); 405 496 } … … 410 501 411 502 // Bail if user cannot add favorites for this user 412 if ( ! current_user_can( 'edit_user', $user_id ) ) {503 if ( ! current_user_can( 'edit_user', $user_id ) ) { 413 504 bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 ); 414 505 } … … 423 514 424 515 // Bail if user did not take this action 425 if ( ! isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $topic->ID ) ) {516 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $topic->ID ) ) { 426 517 bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); 427 518 }
Note: See TracChangeset
for help on using the changeset viewer.