| @@ -1,20 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * Functions of bbPress's Default theme | |
| 4 | + * Functions of the Default template-pack | |
| 5 | 5 | * |
| 6 | 6 | * @package bbPress |
| 7 | 7 | * @subpackage BBP_Theme_Compat |
| 8 | - * @since bbPress (r3732) | |
| 8 | + * @since 2.1.0 bbPress (r3732) | |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | // Exit if accessed directly |
| 12 | -if ( !defined( 'ABSPATH' ) ) exit; | |
| 12 | +defined( 'ABSPATH' ) || exit; | |
| 13 | 13 | |
| 14 | 14 | /** Theme Setup ***************************************************************/ |
| 15 | 15 | |
| 16 | -if ( !class_exists( 'BBP_Default' ) ) : | |
| 16 | +if ( ! class_exists( 'BBP_Default' ) ) : | |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Loads bbPress Default Theme functionality |
| 20 | 20 | * |
| @@ -26,9 +26,9 @@ | ||
| 26 | 26 | * together; bbPress should safely handle the rest. |
| 27 | 27 | * |
| 28 | 28 | * See @link BBP_Theme_Compat() for more. |
| 29 | 29 | * |
| 30 | - * @since bbPress (r3732) | |
| 30 | + * @since 2.1.0 bbPress (r3732) | |
| 31 | 31 | * |
| 32 | 32 | * @package bbPress |
| 33 | 33 | * @subpackage BBP_Theme_Compat |
| 34 | 34 | */ |
| @@ -38,59 +38,45 @@ | ||
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * The main bbPress (Default) Loader |
| 41 | 41 | * |
| 42 | - * @since bbPress (r3732) | |
| 43 | - * | |
| 44 | - * @uses BBP_Default::setup_globals() | |
| 45 | - * @uses BBP_Default::setup_actions() | |
| 42 | + * @since 2.1.0 bbPress (r3732) | |
| 46 | 43 | */ |
| 47 | - public function __construct() { | |
| 48 | - $this->setup_globals(); | |
| 44 | + public function __construct( $properties = array() ) { | |
| 45 | + | |
| 46 | + parent::__construct( | |
| 47 | + bbp_parse_args( | |
| 48 | + $properties, | |
| 49 | + array( | |
| 50 | + 'id' => 'default', | |
| 51 | + 'name' => 'bbPress Default', | |
| 52 | + 'version' => bbp_get_asset_version(), | |
| 53 | + 'dir' => trailingslashit( bbpress()->themes_dir . 'default' ), | |
| 54 | + 'url' => trailingslashit( bbpress()->themes_url . 'default' ), | |
| 55 | + ), | |
| 56 | + 'default_theme' | |
| 57 | + ) | |
| 58 | + ); | |
| 59 | + | |
| 49 | 60 | $this->setup_actions(); |
| 50 | 61 | } |
| 51 | 62 | |
| 52 | 63 | /** |
| 53 | - * Component global variables | |
| 64 | + * Setup the theme hooks | |
| 54 | 65 | * |
| 55 | - * Note that this function is currently commented out in the constructor. | |
| 56 | - * It will only be used if you copy this file into your current theme and | |
| 57 | - * uncomment the line above. | |
| 66 | + * @since 2.1.0 bbPress (r3732) | |
| 58 | 67 | * |
| 59 | - * You'll want to customize the values in here, so they match whatever your | |
| 60 | - * needs are. | |
| 61 | - * | |
| 62 | - * @since bbPress (r3732) | |
| 63 | 68 | * @access private |
| 64 | 69 | */ |
| 65 | - private function setup_globals() { | |
| 66 | - $bbp = bbpress(); | |
| 67 | - $this->id = 'default'; | |
| 68 | - $this->name = __( 'bbPress Default', 'bbpress' ); | |
| 69 | - $this->version = bbp_get_version(); | |
| 70 | - $this->dir = trailingslashit( $bbp->themes_dir . 'default' ); | |
| 71 | - $this->url = trailingslashit( $bbp->themes_url . 'default' ); | |
| 72 | - } | |
| 73 | - | |
| 74 | - /** | |
| 75 | - * Setup the theme hooks | |
| 76 | - * | |
| 77 | - * @since bbPress (r3732) | |
| 78 | - * @access private | |
| 79 | - * | |
| 80 | - * @uses add_filter() To add various filters | |
| 81 | - * @uses add_action() To add various actions | |
| 82 | - */ | |
| 83 | 70 | private function setup_actions() { |
| 84 | 71 | |
| 85 | 72 | /** Scripts ***********************************************************/ |
| 86 | 73 | |
| 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( 'wp_ajax_dim-favorite', array( $this, 'ajax_favorite' ) ); // Handles the ajax favorite/unfavorite | |
| 92 | - add_action( 'wp_ajax_dim-subscription', array( $this, 'ajax_subscription' ) ); // Handles the 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 | |
| 93 | 79 | |
| 94 | 80 | /** Template Wrappers *************************************************/ |
| 95 | 81 | |
| 96 | 82 | add_action( 'bbp_before_main_content', array( $this, 'before_main_content' ) ); // Top wrapper HTML |
| @@ -104,9 +90,9 @@ | ||
| 104 | 90 | /** |
| 105 | 91 | * Inserts HTML at the top of the main content area to be compatible with |
| 106 | 92 | * the Twenty Twelve theme. |
| 107 | 93 | * |
| 108 | - * @since bbPress (r3732) | |
| 94 | + * @since 2.1.0 bbPress (r3732) | |
| 109 | 95 | */ |
| 110 | 96 | public function before_main_content() { |
| 111 | 97 | ?> |
| 112 | 98 | |
| @@ -119,9 +105,9 @@ | ||
| 119 | 105 | /** |
| 120 | 106 | * Inserts HTML at the bottom of the main content area to be compatible with |
| 121 | 107 | * the Twenty Twelve theme. |
| 122 | 108 | * |
| 123 | - * @since bbPress (r3732) | |
| 109 | + * @since 2.1.0 bbPress (r3732) | |
| 124 | 110 | */ |
| 125 | 111 | public function after_main_content() { |
| 126 | 112 | ?> |
| 127 | 113 | |
| @@ -133,80 +119,94 @@ | ||
| 133 | 119 | |
| 134 | 120 | /** |
| 135 | 121 | * Load the theme CSS |
| 136 | 122 | * |
| 137 | - * @since bbPress (r3732) | |
| 138 | - * | |
| 139 | - * @uses wp_enqueue_style() To enqueue the styles | |
| 123 | + * @since 2.1.0 bbPress (r3732) | |
| 140 | 124 | */ |
| 141 | 125 | public function enqueue_styles() { |
| 142 | 126 | |
| 143 | - // LTR or RTL | |
| 144 | - $file = is_rtl() ? 'css/bbpress-rtl.css' : 'css/bbpress.css'; | |
| 127 | + // Setup the default styling | |
| 128 | + $defaults = array( | |
| 129 | + 'bbp-default' => array( | |
| 130 | + 'file' => 'css/bbpress.css', | |
| 131 | + 'dependencies' => array() | |
| 132 | + ) | |
| 133 | + ); | |
| 145 | 134 | |
| 146 | - // Check child theme | |
| 147 | - if ( file_exists( trailingslashit( get_stylesheet_directory() ) . $file ) ) { | |
| 148 | - $location = trailingslashit( get_stylesheet_directory_uri() ); | |
| 149 | - $handle = 'bbp-child-bbpress'; | |
| 135 | + // Optionally support an RTL variant | |
| 136 | + if ( is_rtl() ) { | |
| 137 | + $defaults['bbp-default-rtl'] = array( | |
| 138 | + 'file' => 'css/bbpress-rtl.css', | |
| 139 | + 'dependencies' => array() | |
| 140 | + ); | |
| 141 | + } | |
| 150 | 142 | |
| 151 | - // Check parent theme | |
| 152 | - } elseif ( file_exists( trailingslashit( get_template_directory() ) . $file ) ) { | |
| 153 | - $location = trailingslashit( get_template_directory_uri() ); | |
| 154 | - $handle = 'bbp-parent-bbpress'; | |
| 143 | + // Get and filter the bbp-default style | |
| 144 | + $styles = apply_filters( 'bbp_default_styles', $defaults ); | |
| 155 | 145 | |
| 156 | - // bbPress Theme Compatibility | |
| 157 | - } else { | |
| 158 | - $location = trailingslashit( $this->url ); | |
| 159 | - $handle = 'bbp-default-bbpress'; | |
| 146 | + // Enqueue the styles | |
| 147 | + foreach ( $styles as $handle => $attributes ) { | |
| 148 | + bbp_enqueue_style( $handle, $attributes['file'], $attributes['dependencies'], $this->version ); | |
| 160 | 149 | } |
| 161 | - | |
| 162 | - // Enqueue the bbPress styling | |
| 163 | - wp_enqueue_style( $handle, $location . $file, array(), $this->version, 'screen' ); | |
| 164 | 150 | } |
| 165 | 151 | |
| 166 | 152 | /** |
| 167 | - * Enqueue the required Javascript files | |
| 153 | + * Enqueue the required JavaScript files | |
| 168 | 154 | * |
| 169 | - * @since bbPress (r3732) | |
| 170 | - * | |
| 171 | - * @uses bbp_is_single_topic() To check if it's the topic page | |
| 172 | - * @uses bbp_is_single_user_edit() To check if it's the profile edit page | |
| 173 | - * @uses wp_enqueue_script() To enqueue the scripts | |
| 155 | + * @since 2.1.0 bbPress (r3732) | |
| 174 | 156 | */ |
| 175 | 157 | public function enqueue_scripts() { |
| 176 | 158 | |
| 177 | - if ( bbp_is_single_topic() ) | |
| 178 | - wp_enqueue_script( 'bbpress-topic', $this->url . 'js/topic.js', array( 'wp-lists' ), $this->version, true ); | |
| 159 | + // Setup scripts array | |
| 160 | + $scripts = array(); | |
| 179 | 161 | |
| 180 | - elseif ( bbp_is_single_user_edit() ) | |
| 181 | - wp_enqueue_script( 'user-profile' ); | |
| 182 | - } | |
| 162 | + // Editor scripts | |
| 163 | + // @see https://bbpress.trac.wordpress.org/ticket/2930 | |
| 164 | + if ( bbp_use_wp_editor() && is_bbpress() ) { | |
| 165 | + $scripts['bbpress-editor'] = array( | |
| 166 | + 'file' => 'js/editor.js', | |
| 167 | + 'dependencies' => array( 'jquery' ) | |
| 168 | + ); | |
| 169 | + } | |
| 183 | 170 | |
| 184 | - /** | |
| 185 | - * Put some scripts in the header, like AJAX url for wp-lists | |
| 186 | - * | |
| 187 | - * @since bbPress (r3732) | |
| 188 | - * | |
| 189 | - * @uses bbp_is_single_topic() To check if it's the topic page | |
| 190 | - * @uses admin_url() To get the admin url | |
| 191 | - * @uses bbp_is_single_user_edit() To check if it's the profile edit page | |
| 192 | - */ | |
| 193 | - public function head_scripts() { | |
| 194 | - ?> | |
| 171 | + // Forum-specific scripts | |
| 172 | + if ( bbp_is_single_forum() ) { | |
| 173 | + $scripts['bbpress-engagements'] = array( | |
| 174 | + 'file' => 'js/engagements.js', | |
| 175 | + 'dependencies' => array( 'jquery' ) | |
| 176 | + ); | |
| 177 | + } | |
| 195 | 178 | |
| 196 | - <script type="text/javascript"> | |
| 197 | - /* <![CDATA[ */ | |
| 198 | - var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>'; | |
| 179 | + // Topic-specific scripts | |
| 180 | + if ( bbp_is_single_topic() || bbp_is_topic_edit() ) { | |
| 199 | 181 | |
| 200 | - <?php if ( bbp_is_single_user_edit() ) : ?> | |
| 201 | - if ( window.location.hash == '#password' ) { | |
| 202 | - document.getElementById('pass1').focus(); | |
| 182 | + // Engagements | |
| 183 | + $scripts['bbpress-engagements'] = array( | |
| 184 | + 'file' => 'js/engagements.js', | |
| 185 | + 'dependencies' => array( 'jquery' ) | |
| 186 | + ); | |
| 187 | + | |
| 188 | + // Hierarchical replies | |
| 189 | + if ( bbp_thread_replies() ) { | |
| 190 | + $scripts['bbpress-reply'] = array( | |
| 191 | + 'file' => 'js/reply.js', | |
| 192 | + 'dependencies' => array( 'jquery' ) | |
| 193 | + ); | |
| 203 | 194 | } |
| 204 | - <?php endif; ?> | |
| 205 | - /* ]]> */ | |
| 206 | - </script> | |
| 195 | + } | |
| 207 | 196 | |
| 208 | - <?php | |
| 197 | + // User Profile edit | |
| 198 | + if ( bbp_is_single_user_edit() ) { | |
| 199 | + wp_enqueue_script( 'user-profile' ); | |
| 200 | + } | |
| 201 | + | |
| 202 | + // Filter the scripts | |
| 203 | + $scripts = apply_filters( 'bbp_default_scripts', $scripts ); | |
| 204 | + | |
| 205 | + // Enqueue the scripts | |
| 206 | + foreach ( $scripts as $handle => $attributes ) { | |
| 207 | + bbp_enqueue_script( $handle, $attributes['file'], $attributes['dependencies'], $this->version, true ); | |
| 208 | + } | |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | /** |
| 212 | 212 | * Load localizations for topic script |
| @@ -212,151 +212,155 @@ | ||
| 212 | 212 | * Load localizations for topic script |
| 213 | 213 | * |
| 214 | 214 | * These localizations require information that may not be loaded even by init. |
| 215 | 215 | * |
| 216 | - * @since bbPress (r3732) | |
| 217 | - * | |
| 218 | - * @uses bbp_is_single_topic() To check if it's the topic page | |
| 219 | - * @uses is_user_logged_in() To check if user is logged in | |
| 220 | - * @uses bbp_get_current_user_id() To get the current user id | |
| 221 | - * @uses bbp_get_topic_id() To get the topic id | |
| 222 | - * @uses bbp_get_favorites_permalink() To get the favorites permalink | |
| 223 | - * @uses bbp_is_user_favorite() To check if the topic is in user's favorites | |
| 224 | - * @uses bbp_is_subscriptions_active() To check if the subscriptions are active | |
| 225 | - * @uses bbp_is_user_subscribed() To check if the user is subscribed to topic | |
| 226 | - * @uses bbp_get_topic_permalink() To get the topic permalink | |
| 227 | - * @uses wp_localize_script() To localize the script | |
| 216 | + * @since 2.1.0 bbPress (r3732) | |
| 228 | 217 | */ |
| 229 | 218 | public function localize_topic_script() { |
| 230 | 219 | |
| 231 | - // Bail if not viewing a single topic | |
| 232 | - if ( !bbp_is_single_topic() ) | |
| 233 | - return; | |
| 234 | - | |
| 235 | - // Bail if user is not logged in | |
| 236 | - if ( !is_user_logged_in() ) | |
| 237 | - return; | |
| 238 | - | |
| 239 | - $user_id = bbp_get_current_user_id(); | |
| 240 | - | |
| 241 | - $localizations = array( | |
| 242 | - 'currentUserId' => $user_id, | |
| 243 | - 'topicId' => bbp_get_topic_id(), | |
| 244 | - ); | |
| 245 | - | |
| 246 | - // Favorites | |
| 247 | - if ( bbp_is_favorites_active() ) { | |
| 248 | - $localizations['favoritesActive'] = 1; | |
| 249 | - $localizations['favoritesLink'] = bbp_get_favorites_permalink( $user_id ); | |
| 250 | - $localizations['isFav'] = (int) bbp_is_user_favorite( $user_id ); | |
| 251 | - $localizations['favLinkYes'] = __( 'favorites', 'bbpress' ); | |
| 252 | - $localizations['favLinkNo'] = __( '?', 'bbpress' ); | |
| 253 | - $localizations['favYes'] = __( 'This topic is one of your %favLinkYes% [%favDel%]', 'bbpress' ); | |
| 254 | - $localizations['favNo'] = __( '%favAdd% (%favLinkNo%)', 'bbpress' ); | |
| 255 | - $localizations['favDel'] = __( '×', 'bbpress' ); | |
| 256 | - $localizations['favAdd'] = __( 'Add this topic to your favorites', 'bbpress' ); | |
| 257 | - } else { | |
| 258 | - $localizations['favoritesActive'] = 0; | |
| 220 | + // Single forum or topic | |
| 221 | + if ( bbp_is_single_forum() || bbp_is_single_topic() ) { | |
| 222 | + wp_localize_script( | |
| 223 | + 'bbpress-engagements', | |
| 224 | + 'bbpEngagementJS', | |
| 225 | + array( | |
| 226 | + 'object_id' => get_the_ID(), | |
| 227 | + 'bbp_ajaxurl' => bbp_get_ajax_url(), | |
| 228 | + 'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), | |
| 229 | + ) | |
| 230 | + ); | |
| 259 | 231 | } |
| 260 | - | |
| 261 | - // Subscriptions | |
| 262 | - if ( bbp_is_subscriptions_active() ) { | |
| 263 | - $localizations['subsActive'] = 1; | |
| 264 | - $localizations['isSubscribed'] = (int) bbp_is_user_subscribed( $user_id ); | |
| 265 | - $localizations['subsSub'] = __( 'Subscribe', 'bbpress' ); | |
| 266 | - $localizations['subsUns'] = __( 'Unsubscribe', 'bbpress' ); | |
| 267 | - $localizations['subsLink'] = bbp_get_topic_permalink(); | |
| 268 | - } else { | |
| 269 | - $localizations['subsActive'] = 0; | |
| 270 | - } | |
| 271 | - | |
| 272 | - wp_localize_script( 'bbpress-topic', 'bbpTopicJS', $localizations ); | |
| 273 | 232 | } |
| 274 | 233 | |
| 275 | 234 | /** |
| 276 | - * Add or remove a topic from a user's favorites | |
| 235 | + * AJAX handler to add or remove a topic from a user's favorites | |
| 277 | 236 | * |
| 278 | - * @since bbPress (r3732) | |
| 279 | - * | |
| 280 | - * @uses bbp_get_current_user_id() To get the current user id | |
| 281 | - * @uses current_user_can() To check if the current user can edit the user | |
| 282 | - * @uses bbp_get_topic() To get the topic | |
| 283 | - * @uses check_ajax_referer() To verify the nonce & check the referer | |
| 284 | - * @uses bbp_is_user_favorite() To check if the topic is user's favorite | |
| 285 | - * @uses bbp_remove_user_favorite() To remove the topic from user's favorites | |
| 286 | - * @uses bbp_add_user_favorite() To add the topic from user's favorites | |
| 237 | + * @since 2.1.0 bbPress (r3732) | |
| 287 | 238 | */ |
| 288 | 239 | public function ajax_favorite() { |
| 240 | + | |
| 241 | + // Bail if favorites are not active | |
| 242 | + if ( ! bbp_is_favorites_active() ) { | |
| 243 | + bbp_ajax_response( false, esc_html__( 'Favorites are no longer active.', 'bbpress' ), 300 ); | |
| 244 | + } | |
| 245 | + | |
| 246 | + // Bail if user is not logged in | |
| 247 | + if ( ! is_user_logged_in() ) { | |
| 248 | + bbp_ajax_response( false, esc_html__( 'Please login to favorite.', 'bbpress' ), 301 ); | |
| 249 | + } | |
| 250 | + | |
| 251 | + // Get user and topic data | |
| 289 | 252 | $user_id = bbp_get_current_user_id(); |
| 290 | - $id = intval( $_POST['id'] ); | |
| 253 | + $id = ! empty( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; | |
| 254 | + $type = ! empty( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : 'post'; | |
| 291 | 255 | |
| 292 | - if ( !current_user_can( 'edit_user', $user_id ) ) | |
| 293 | - die( '-1' ); | |
| 256 | + // Bail if user cannot add favorites for this user | |
| 257 | + if ( ! current_user_can( 'edit_user', $user_id ) ) { | |
| 258 | + bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 ); | |
| 259 | + } | |
| 294 | 260 | |
| 295 | - $topic = bbp_get_topic( $id ); | |
| 261 | + // Get the object | |
| 262 | + if ( 'post' === $type ) { | |
| 263 | + $object = get_post( $id ); | |
| 264 | + } | |
| 296 | 265 | |
| 297 | - if ( empty( $topic ) ) | |
| 298 | - die( '0' ); | |
| 266 | + // Bail if topic cannot be found | |
| 267 | + if ( empty( $object ) ) { | |
| 268 | + bbp_ajax_response( false, esc_html__( 'Favorite failed.', 'bbpress' ), 303 ); | |
| 269 | + } | |
| 299 | 270 | |
| 300 | - check_ajax_referer( 'toggle-favorite_' . $topic->ID ); | |
| 271 | + // Bail if user did not take this action | |
| 272 | + if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $object->ID ) ) { | |
| 273 | + bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); | |
| 274 | + } | |
| 301 | 275 | |
| 302 | - if ( bbp_is_user_favorite( $user_id, $topic->ID ) ) { | |
| 303 | - if ( bbp_remove_user_favorite( $user_id, $topic->ID ) ) { | |
| 304 | - die( '1' ); | |
| 305 | - } | |
| 306 | - } else { | |
| 307 | - if ( bbp_add_user_favorite( $user_id, $topic->ID ) ) { | |
| 308 | - die( '1' ); | |
| 309 | - } | |
| 276 | + // Take action | |
| 277 | + $status = bbp_is_user_favorite( $user_id, $object->ID ) | |
| 278 | + ? bbp_remove_user_favorite( $user_id, $object->ID ) | |
| 279 | + : bbp_add_user_favorite( $user_id, $object->ID ); | |
| 280 | + | |
| 281 | + // Bail if action failed | |
| 282 | + if ( empty( $status ) ) { | |
| 283 | + bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 ); | |
| 310 | 284 | } |
| 311 | 285 | |
| 312 | - die( '0' ); | |
| 286 | + // Put subscription attributes in convenient array | |
| 287 | + $attrs = array( | |
| 288 | + 'object_id' => $object->ID, | |
| 289 | + 'object_type' => $type, | |
| 290 | + 'user_id' => $user_id | |
| 291 | + ); | |
| 292 | + | |
| 293 | + // Action succeeded | |
| 294 | + bbp_ajax_response( true, bbp_get_user_favorites_link( $attrs, $user_id, false ), 200 ); | |
| 313 | 295 | } |
| 314 | 296 | |
| 315 | 297 | /** |
| 316 | - * Subscribe/Unsubscribe a user from a topic | |
| 298 | + * AJAX handler to Subscribe/Unsubscribe a user from a topic | |
| 317 | 299 | * |
| 318 | - * @since bbPress (r3732) | |
| 319 | - * | |
| 320 | - * @uses bbp_is_subscriptions_active() To check if the subscriptions are active | |
| 321 | - * @uses bbp_get_current_user_id() To get the current user id | |
| 322 | - * @uses current_user_can() To check if the current user can edit the user | |
| 323 | - * @uses bbp_get_topic() To get the topic | |
| 324 | - * @uses check_ajax_referer() To verify the nonce & check the referer | |
| 325 | - * @uses bbp_is_user_subscribed() To check if the topic is in user's | |
| 326 | - * subscriptions | |
| 327 | - * @uses bbp_remove_user_subscriptions() To remove the topic from user's | |
| 328 | - * subscriptions | |
| 329 | - * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions | |
| 300 | + * @since 2.1.0 bbPress (r3732) | |
| 330 | 301 | */ |
| 331 | 302 | public function ajax_subscription() { |
| 332 | - if ( !bbp_is_subscriptions_active() ) | |
| 333 | - return; | |
| 334 | 303 | |
| 304 | + // Bail if subscriptions are not active | |
| 305 | + if ( ! bbp_is_subscriptions_active() ) { | |
| 306 | + bbp_ajax_response( false, esc_html__( 'Subscriptions are no longer active.', 'bbpress' ), 300 ); | |
| 307 | + } | |
| 308 | + | |
| 309 | + // Bail if user is not logged in | |
| 310 | + if ( ! is_user_logged_in() ) { | |
| 311 | + bbp_ajax_response( false, esc_html__( 'Please login to subscribe.', 'bbpress' ), 301 ); | |
| 312 | + } | |
| 313 | + | |
| 314 | + // Get user and topic data | |
| 335 | 315 | $user_id = bbp_get_current_user_id(); |
| 336 | - $id = intval( $_POST['id'] ); | |
| 316 | + $id = ! empty( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; | |
| 317 | + $type = ! empty( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : 'post'; | |
| 337 | 318 | |
| 338 | - if ( !current_user_can( 'edit_user', $user_id ) ) | |
| 339 | - die( '-1' ); | |
| 319 | + // Bail if user cannot add favorites for this user | |
| 320 | + if ( ! current_user_can( 'edit_user', $user_id ) ) { | |
| 321 | + bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 ); | |
| 322 | + } | |
| 340 | 323 | |
| 341 | - $topic = bbp_get_topic( $id ); | |
| 324 | + // Get the object | |
| 325 | + if ( 'post' === $type ) { | |
| 326 | + $object = get_post( $id ); | |
| 327 | + } | |
| 342 | 328 | |
| 343 | - if ( empty( $topic ) ) | |
| 344 | - die( '0' ); | |
| 329 | + // Bail if topic cannot be found | |
| 330 | + if ( empty( $object ) ) { | |
| 331 | + bbp_ajax_response( false, esc_html__( 'Subscription failed.', 'bbpress' ), 303 ); | |
| 332 | + } | |
| 345 | 333 | |
| 346 | - check_ajax_referer( 'toggle-subscription_' . $topic->ID ); | |
| 334 | + // Bail if user did not take this action | |
| 335 | + if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $object->ID ) ) { | |
| 336 | + bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); | |
| 337 | + } | |
| 347 | 338 | |
| 348 | - if ( bbp_is_user_subscribed( $user_id, $topic->ID ) ) { | |
| 349 | - if ( bbp_remove_user_subscription( $user_id, $topic->ID ) ) { | |
| 350 | - die( '1' ); | |
| 351 | - } | |
| 352 | - } else { | |
| 353 | - if ( bbp_add_user_subscription( $user_id, $topic->ID ) ) { | |
| 354 | - die( '1' ); | |
| 355 | - } | |
| 339 | + // Take action | |
| 340 | + $status = bbp_is_user_subscribed( $user_id, $object->ID ) | |
| 341 | + ? bbp_remove_user_subscription( $user_id, $object->ID ) | |
| 342 | + : bbp_add_user_subscription( $user_id, $object->ID ); | |
| 343 | + | |
| 344 | + // Bail if action failed | |
| 345 | + if ( empty( $status ) ) { | |
| 346 | + bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 ); | |
| 356 | 347 | } |
| 357 | 348 | |
| 358 | - die( '0' ); | |
| 349 | + // Put subscription attributes in convenient array | |
| 350 | + $attrs = array( | |
| 351 | + 'object_id' => $object->ID, | |
| 352 | + 'object_type' => $type, | |
| 353 | + 'user_id' => $user_id | |
| 354 | + ); | |
| 355 | + | |
| 356 | + // Add separator to topic if favorites is active | |
| 357 | + if ( ( 'post' === $type ) && ( bbp_get_topic_post_type() === get_post_type( $object ) ) && bbp_is_favorites_active() ) { | |
| 358 | + $attrs['before'] = ' | '; | |
| 359 | + } | |
| 360 | + | |
| 361 | + // Action succeeded | |
| 362 | + bbp_ajax_response( true, bbp_get_user_subscribe_link( $attrs, $user_id, false ), 200 ); | |
| 359 | 363 | } |
| 360 | 364 | } |
| 361 | 365 | new BBP_Default(); |
| 362 | 366 | endif; |