| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | * @subpackage Functions |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | // Exit if accessed directly |
| 11 | -if ( !defined( 'ABSPATH' ) ) exit; | |
| 11 | +defined( 'ABSPATH' ) || exit; | |
| 12 | 12 | |
| 13 | 13 | /** Insert ********************************************************************/ |
| 14 | 14 | |
| 15 | 15 | /** |
| @@ -15,15 +15,10 @@ | ||
| 15 | 15 | /** |
| 16 | 16 | * A wrapper for wp_insert_post() that also includes the necessary meta values |
| 17 | 17 | * for the forum to function properly. |
| 18 | 18 | * |
| 19 | - * @since bbPress (r3349) | |
| 19 | + * @since 2.0.0 bbPress (r3349) | |
| 20 | 20 | * |
| 21 | - * @uses bbp_parse_args() | |
| 22 | - * @uses bbp_get_forum_post_type() | |
| 23 | - * @uses wp_insert_post() | |
| 24 | - * @uses update_post_meta() | |
| 25 | - * | |
| 26 | 21 | * @param array $forum_data Forum post data |
| 27 | 22 | * @param arrap $forum_meta Forum meta data |
| 28 | 23 | */ |
| 29 | 24 | function bbp_insert_forum( $forum_data = array(), $forum_meta = array() ) { |
| @@ -28,9 +23,9 @@ | ||
| 28 | 23 | */ |
| 29 | 24 | function bbp_insert_forum( $forum_data = array(), $forum_meta = array() ) { |
| 30 | 25 | |
| 31 | 26 | // Forum |
| 32 | - $default_forum = array( | |
| 27 | + $forum_data = bbp_parse_args( $forum_data, array( | |
| 33 | 28 | 'post_parent' => 0, // forum ID |
| 34 | 29 | 'post_status' => bbp_get_public_status_id(), |
| 35 | 30 | 'post_type' => bbp_get_forum_post_type(), |
| 36 | 31 | 'post_author' => bbp_get_current_user_id(), |
| @@ -38,20 +33,22 @@ | ||
| 38 | 33 | 'post_content' => '', |
| 39 | 34 | 'post_title' => '', |
| 40 | 35 | 'menu_order' => 0, |
| 41 | 36 | 'comment_status' => 'closed' |
| 42 | - ); | |
| 43 | - $forum_data = bbp_parse_args( $forum_data, $default_forum, 'insert_forum' ); | |
| 37 | + ), 'insert_forum' ); | |
| 44 | 38 | |
| 45 | 39 | // Insert forum |
| 46 | - $forum_id = wp_insert_post( $forum_data ); | |
| 40 | + $forum_id = wp_insert_post( $forum_data, false ); | |
| 47 | 41 | |
| 48 | 42 | // Bail if no forum was added |
| 49 | - if ( empty( $forum_id ) ) | |
| 43 | + if ( empty( $forum_id ) ) { | |
| 50 | 44 | return false; |
| 45 | + } | |
| 51 | 46 | |
| 52 | 47 | // Forum meta |
| 53 | - $default_meta = array( | |
| 48 | + $forum_meta = bbp_parse_args( $forum_meta, array( | |
| 49 | + 'forum_type' => 'forum', | |
| 50 | + 'status' => 'open', | |
| 54 | 51 | 'reply_count' => 0, |
| 55 | 52 | 'topic_count' => 0, |
| 56 | 53 | 'topic_count_hidden' => 0, |
| 57 | 54 | 'total_reply_count' => 0, |
| @@ -60,16 +57,54 @@ | ||
| 60 | 57 | 'last_reply_id' => 0, |
| 61 | 58 | 'last_active_id' => 0, |
| 62 | 59 | 'last_active_time' => 0, |
| 63 | 60 | 'forum_subforum_count' => 0, |
| 64 | - ); | |
| 65 | - $forum_meta = bbp_parse_args( $forum_meta, $default_meta, 'insert_forum_meta' ); | |
| 61 | + ), 'insert_forum_meta' ); | |
| 66 | 62 | |
| 67 | 63 | // Insert forum meta |
| 68 | - foreach ( $forum_meta as $meta_key => $meta_value ) | |
| 69 | - update_post_meta( $forum_id, '_bbp_' . $meta_key, $meta_value ); | |
| 64 | + foreach ( $forum_meta as $meta_key => $meta_value ) { | |
| 70 | 65 | |
| 71 | - // Return new forum ID | |
| 66 | + // Prefix if not prefixed | |
| 67 | + if ( '_bbp_' !== substr( $meta_key, 0, 5 ) ) { | |
| 68 | + $meta_key = '_bbp_' . $meta_key; | |
| 69 | + } | |
| 70 | + | |
| 71 | + // Update the meta | |
| 72 | + update_post_meta( $forum_id, $meta_key, $meta_value ); | |
| 73 | + } | |
| 74 | + | |
| 75 | + // Update the forum and hierarchy | |
| 76 | + bbp_update_forum( array( | |
| 77 | + 'forum_id' => $forum_id, | |
| 78 | + 'post_parent' => $forum_data['post_parent'] | |
| 79 | + ) ); | |
| 80 | + | |
| 81 | + // Maybe make private | |
| 82 | + if ( bbp_is_forum_private( $forum_id, false ) ) { | |
| 83 | + bbp_privatize_forum( $forum_id ); | |
| 84 | + | |
| 85 | + // Maybe make hidden | |
| 86 | + } elseif ( bbp_is_forum_hidden( $forum_id, false ) ) { | |
| 87 | + bbp_hide_forum( $forum_id ); | |
| 88 | + | |
| 89 | + // Publicize | |
| 90 | + } else { | |
| 91 | + bbp_publicize_forum( $forum_id ); | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * Fires after forum has been inserted via `bbp_insert_forum`. | |
| 96 | + * | |
| 97 | + * @since 2.6.0 bbPress (r6036) | |
| 98 | + * | |
| 99 | + * @param int $forum_id The forum id. | |
| 100 | + */ | |
| 101 | + do_action( 'bbp_insert_forum', (int) $forum_id ); | |
| 102 | + | |
| 103 | + // Bump the last changed cache | |
| 104 | + wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' ); | |
| 105 | + | |
| 106 | + // Return forum_id | |
| 72 | 107 | return $forum_id; |
| 73 | 108 | } |
| 74 | 109 | |
| 75 | 110 | /** Post Form Handlers ********************************************************/ |
| @@ -76,63 +111,34 @@ | ||
| 76 | 111 | |
| 77 | 112 | /** |
| 78 | 113 | * Handles the front end forum submission |
| 79 | 114 | * |
| 80 | - * @uses bbPress:errors::add() To log various error messages | |
| 81 | - * @uses bbp_verify_nonce_request() To verify the nonce and check the request | |
| 82 | - * @uses bbp_is_anonymous() To check if an anonymous post is being made | |
| 83 | - * @uses current_user_can() To check if the current user can publish forum | |
| 84 | - * @uses bbp_get_current_user_id() To get the current user id | |
| 85 | - * @uses bbp_filter_anonymous_post_data() To filter anonymous data | |
| 86 | - * @uses bbp_set_current_anonymous_user_data() To set the anonymous user cookies | |
| 87 | - * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error} | |
| 88 | - * @uses esc_attr() For sanitization | |
| 89 | - * @uses bbp_is_forum_category() To check if the forum is a category | |
| 90 | - * @uses bbp_is_forum_closed() To check if the forum is closed | |
| 91 | - * @uses bbp_is_forum_private() To check if the forum is private | |
| 92 | - * @uses bbp_check_for_flood() To check for flooding | |
| 93 | - * @uses bbp_check_for_duplicate() To check for duplicates | |
| 94 | - * @uses bbp_get_forum_post_type() To get the forum post type | |
| 95 | - * @uses remove_filter() To remove 'wp_filter_kses' filters if needed | |
| 96 | - * @uses apply_filters() Calls 'bbp_new_forum_pre_title' with the content | |
| 97 | - * @uses apply_filters() Calls 'bbp_new_forum_pre_content' with the content | |
| 98 | - * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors | |
| 99 | - * @uses wp_insert_post() To insert the forum | |
| 100 | - * @uses do_action() Calls 'bbp_new_forum' with the forum id, forum id, | |
| 101 | - * anonymous data and reply author | |
| 102 | - * @uses bbp_stick_forum() To stick or super stick the forum | |
| 103 | - * @uses bbp_unstick_forum() To unstick the forum | |
| 104 | - * @uses bbp_get_forum_permalink() To get the forum permalink | |
| 105 | - * @uses wp_safe_redirect() To redirect to the forum link | |
| 106 | - * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error | |
| 107 | - * messages | |
| 115 | + * @param string $action The requested action to compare this function to | |
| 108 | 116 | */ |
| 109 | -function bbp_new_forum_handler() { | |
| 117 | +function bbp_new_forum_handler( $action = '' ) { | |
| 110 | 118 | |
| 111 | - // Bail if not a POST action | |
| 112 | - if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) | |
| 113 | - return; | |
| 114 | - | |
| 115 | 119 | // Bail if action is not bbp-new-forum |
| 116 | - if ( empty( $_POST['action'] ) || ( 'bbp-new-forum' !== $_POST['action'] ) ) | |
| 120 | + if ( 'bbp-new-forum' !== $action ) { | |
| 117 | 121 | return; |
| 122 | + } | |
| 118 | 123 | |
| 119 | 124 | // Nonce check |
| 120 | 125 | if ( ! bbp_verify_nonce_request( 'bbp-new-forum' ) ) { |
| 121 | - bbp_add_error( 'bbp_new_forum_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); | |
| 126 | + bbp_add_error( 'bbp_new_forum_nonce', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); | |
| 122 | 127 | return; |
| 123 | 128 | } |
| 124 | 129 | |
| 125 | 130 | // Define local variable(s) |
| 126 | - $view_all = $anonymous_data = false; | |
| 131 | + $view_all = false; | |
| 127 | 132 | $forum_parent_id = $forum_author = 0; |
| 128 | 133 | $forum_title = $forum_content = ''; |
| 134 | + $anonymous_data = array(); | |
| 129 | 135 | |
| 130 | 136 | /** Forum Author **********************************************************/ |
| 131 | 137 | |
| 132 | 138 | // User cannot create forums |
| 133 | - if ( !current_user_can( 'publish_forums' ) ) { | |
| 134 | - bbp_add_error( 'bbp_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new forums.', 'bbpress' ) ); | |
| 139 | + if ( ! current_user_can( 'publish_forums' ) ) { | |
| 140 | + bbp_add_error( 'bbp_forum_permission', __( '<strong>Error</strong>: You do not have permission to create new forums.', 'bbpress' ) ); | |
| 135 | 141 | return; |
| 136 | 142 | } |
| 137 | 143 | |
| 138 | 144 | // Forum author is current user |
| @@ -137,95 +143,110 @@ | ||
| 137 | 143 | |
| 138 | 144 | // Forum author is current user |
| 139 | 145 | $forum_author = bbp_get_current_user_id(); |
| 140 | 146 | |
| 141 | - // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified | |
| 142 | - if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_forum'] ) && wp_create_nonce( 'bbp-unfiltered-html-forum_new' ) == $_POST['_bbp_unfiltered_html_forum'] ) { | |
| 143 | - remove_filter( 'bbp_new_forum_pre_title', 'wp_filter_kses' ); | |
| 144 | - remove_filter( 'bbp_new_forum_pre_content', 'wp_filter_kses' ); | |
| 147 | + // Remove kses filters from title and content for capable users and if the nonce is verified | |
| 148 | + if ( current_user_can( 'unfiltered_html' ) && ! empty( $_POST['_bbp_unfiltered_html_forum'] ) && wp_create_nonce( 'bbp-unfiltered-html-forum_new' ) === $_POST['_bbp_unfiltered_html_forum'] ) { | |
| 149 | + remove_filter( 'bbp_new_forum_pre_title', 'wp_filter_kses' ); | |
| 150 | + remove_filter( 'bbp_new_forum_pre_content', 'bbp_encode_bad', 10 ); | |
| 151 | + remove_filter( 'bbp_new_forum_pre_content', 'bbp_filter_kses', 30 ); | |
| 145 | 152 | } |
| 146 | 153 | |
| 147 | 154 | /** Forum Title ***********************************************************/ |
| 148 | 155 | |
| 149 | - if ( !empty( $_POST['bbp_forum_title'] ) ) | |
| 150 | - $forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) ); | |
| 156 | + if ( ! empty( $_POST['bbp_forum_title'] ) ) { | |
| 157 | + $forum_title = sanitize_text_field( $_POST['bbp_forum_title'] ); | |
| 158 | + } | |
| 151 | 159 | |
| 152 | 160 | // Filter and sanitize |
| 153 | 161 | $forum_title = apply_filters( 'bbp_new_forum_pre_title', $forum_title ); |
| 154 | 162 | |
| 155 | 163 | // No forum title |
| 156 | - if ( empty( $forum_title ) ) | |
| 157 | - bbp_add_error( 'bbp_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) ); | |
| 164 | + if ( empty( $forum_title ) ) { | |
| 165 | + bbp_add_error( 'bbp_forum_title', __( '<strong>Error</strong>: Your forum needs a title.', 'bbpress' ) ); | |
| 166 | + } | |
| 158 | 167 | |
| 168 | + // Title too long | |
| 169 | + if ( bbp_is_title_too_long( $forum_title ) ) { | |
| 170 | + bbp_add_error( 'bbp_forum_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) ); | |
| 171 | + } | |
| 172 | + | |
| 159 | 173 | /** Forum Content *********************************************************/ |
| 160 | 174 | |
| 161 | - if ( !empty( $_POST['bbp_forum_content'] ) ) | |
| 175 | + if ( ! empty( $_POST['bbp_forum_content'] ) ) { | |
| 162 | 176 | $forum_content = $_POST['bbp_forum_content']; |
| 177 | + } | |
| 163 | 178 | |
| 164 | 179 | // Filter and sanitize |
| 165 | 180 | $forum_content = apply_filters( 'bbp_new_forum_pre_content', $forum_content ); |
| 166 | 181 | |
| 167 | 182 | // No forum content |
| 168 | - if ( empty( $forum_content ) ) | |
| 169 | - bbp_add_error( 'bbp_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress' ) ); | |
| 183 | + if ( empty( $forum_content ) ) { | |
| 184 | + bbp_add_error( 'bbp_forum_content', __( '<strong>Error</strong>: Your forum description cannot be empty.', 'bbpress' ) ); | |
| 185 | + } | |
| 170 | 186 | |
| 171 | 187 | /** Forum Parent **********************************************************/ |
| 172 | 188 | |
| 173 | 189 | // Forum parent was passed (the norm) |
| 174 | - if ( !empty( $_POST['bbp_forum_parent_id'] ) ) | |
| 175 | - $forum_parent_id = (int) $_POST['bbp_forum_parent_id']; | |
| 176 | - | |
| 190 | + if ( ! empty( $_POST['bbp_forum_parent_id'] ) ) { | |
| 191 | + $forum_parent_id = bbp_get_forum_id( $_POST['bbp_forum_parent_id'] ); | |
| 192 | + } | |
| 193 | + | |
| 177 | 194 | // Filter and sanitize |
| 178 | 195 | $forum_parent_id = apply_filters( 'bbp_new_forum_pre_parent_id', $forum_parent_id ); |
| 179 | 196 | |
| 180 | 197 | // No forum parent was passed (should never happen) |
| 181 | 198 | if ( empty( $forum_parent_id ) ) { |
| 182 | - bbp_add_error( 'bbp_new_forum_missing_parent', __( '<strong>ERROR</strong>: Your forum must have a parent.', 'bbpress' ) ); | |
| 199 | + bbp_add_error( 'bbp_new_forum_missing_parent', __( '<strong>Error</strong>: Your forum must have a parent.', 'bbpress' ) ); | |
| 183 | 200 | |
| 184 | 201 | // Forum exists |
| 185 | - } elseif ( !empty( $forum_parent_id ) ) { | |
| 202 | + } elseif ( ! empty( $forum_parent_id ) ) { | |
| 186 | 203 | |
| 187 | 204 | // Forum is a category |
| 188 | 205 | if ( bbp_is_forum_category( $forum_parent_id ) ) { |
| 189 | - bbp_add_error( 'bbp_new_forum_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No forums can be created in this forum.', 'bbpress' ) ); | |
| 206 | + bbp_add_error( 'bbp_new_forum_forum_category', __( '<strong>Error</strong>: This forum is a category. No forums can be created in this forum.', 'bbpress' ) ); | |
| 190 | 207 | } |
| 191 | 208 | |
| 192 | 209 | // Forum is closed and user cannot access |
| 193 | - if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) { | |
| 194 | - bbp_add_error( 'bbp_new_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) ); | |
| 210 | + if ( bbp_is_forum_closed( $forum_parent_id ) && ! current_user_can( 'edit_forum', $forum_parent_id ) ) { | |
| 211 | + bbp_add_error( 'bbp_new_forum_forum_closed', __( '<strong>Error</strong>: This forum has been closed to new forums.', 'bbpress' ) ); | |
| 195 | 212 | } |
| 196 | 213 | |
| 197 | 214 | // Forum is private and user cannot access |
| 198 | - if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) { | |
| 199 | - bbp_add_error( 'bbp_new_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); | |
| 215 | + if ( bbp_is_forum_private( $forum_parent_id ) && ! current_user_can( 'read_forum', $forum_parent_id ) ) { | |
| 216 | + bbp_add_error( 'bbp_new_forum_forum_private', __( '<strong>Error</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); | |
| 200 | 217 | } |
| 201 | 218 | |
| 202 | 219 | // Forum is hidden and user cannot access |
| 203 | - if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) { | |
| 204 | - bbp_add_error( 'bbp_new_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); | |
| 205 | - } | |
| 220 | + if ( bbp_is_forum_hidden( $forum_parent_id ) && ! current_user_can( 'read_forum', $forum_parent_id ) ) { | |
| 221 | + bbp_add_error( 'bbp_new_forum_forum_hidden', __( '<strong>Error</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); | |
| 222 | + } | |
| 206 | 223 | } |
| 207 | 224 | |
| 208 | 225 | /** Forum Flooding ********************************************************/ |
| 209 | 226 | |
| 210 | - if ( !bbp_check_for_flood( $anonymous_data, $forum_author ) ) | |
| 211 | - bbp_add_error( 'bbp_forum_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) ); | |
| 227 | + if ( ! bbp_check_for_flood( $anonymous_data, $forum_author ) ) { | |
| 228 | + bbp_add_error( 'bbp_forum_flood', __( '<strong>Error</strong>: Slow down; you move too fast.', 'bbpress' ) ); | |
| 229 | + } | |
| 212 | 230 | |
| 213 | 231 | /** Forum Duplicate *******************************************************/ |
| 214 | 232 | |
| 215 | - if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_forum_post_type(), 'post_author' => $forum_author, 'post_content' => $forum_content, 'anonymous_data' => $anonymous_data ) ) ) | |
| 216 | - bbp_add_error( 'bbp_forum_duplicate', __( '<strong>ERROR</strong>: This forum already exists.', 'bbpress' ) ); | |
| 233 | + if ( ! bbp_check_for_duplicate( array( 'post_type' => bbp_get_forum_post_type(), 'post_author' => $forum_author, 'post_content' => $forum_content, 'anonymous_data' => $anonymous_data ) ) ) { | |
| 234 | + bbp_add_error( 'bbp_forum_duplicate', __( '<strong>Error</strong>: This forum already exists.', 'bbpress' ) ); | |
| 235 | + } | |
| 217 | 236 | |
| 218 | - /** Forum Blacklist *******************************************************/ | |
| 237 | + /** Forum Bad Words *******************************************************/ | |
| 219 | 238 | |
| 220 | - if ( !bbp_check_for_blacklist( $anonymous_data, $forum_author, $forum_title, $forum_content ) ) | |
| 221 | - bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be created at this time.', 'bbpress' ) ); | |
| 239 | + if ( ! bbp_check_for_moderation( $anonymous_data, $forum_author, $forum_title, $forum_content, true ) ) { | |
| 240 | + bbp_add_error( 'bbp_forum_moderation', __( '<strong>Error</strong>: Your forum cannot be created at this time.', 'bbpress' ) ); | |
| 241 | + } | |
| 222 | 242 | |
| 223 | 243 | /** Forum Moderation ******************************************************/ |
| 224 | 244 | |
| 225 | 245 | $post_status = bbp_get_public_status_id(); |
| 226 | - if ( !bbp_check_for_moderation( $anonymous_data, $forum_author, $forum_title, $forum_content ) ) | |
| 246 | + if ( ! bbp_check_for_moderation( $anonymous_data, $forum_author, $forum_title, $forum_content ) ) { | |
| 227 | 247 | $post_status = bbp_get_pending_status_id(); |
| 248 | + } | |
| 228 | 249 | |
| 229 | 250 | /** Additional Actions (Before Save) **************************************/ |
| 230 | 251 | |
| 231 | 252 | do_action( 'bbp_new_forum_pre_extras', $forum_parent_id ); |
| @@ -230,10 +251,11 @@ | ||
| 230 | 251 | |
| 231 | 252 | do_action( 'bbp_new_forum_pre_extras', $forum_parent_id ); |
| 232 | 253 | |
| 233 | 254 | // Bail if errors |
| 234 | - if ( bbp_has_errors() ) | |
| 255 | + if ( bbp_has_errors() ) { | |
| 235 | 256 | return; |
| 257 | + } | |
| 236 | 258 | |
| 237 | 259 | /** No Errors *************************************************************/ |
| 238 | 260 | |
| 239 | 261 | // Add the content of the form to $forum_data as an array |
| @@ -248,19 +270,19 @@ | ||
| 248 | 270 | 'comment_status' => 'closed' |
| 249 | 271 | ) ); |
| 250 | 272 | |
| 251 | 273 | // Insert forum |
| 252 | - $forum_id = wp_insert_post( $forum_data ); | |
| 274 | + $forum_id = wp_insert_post( $forum_data, true ); | |
| 253 | 275 | |
| 254 | 276 | /** No Errors *************************************************************/ |
| 255 | 277 | |
| 256 | - if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) { | |
| 278 | + if ( ! empty( $forum_id ) && ! is_wp_error( $forum_id ) ) { | |
| 257 | 279 | |
| 258 | 280 | /** Trash Check *******************************************************/ |
| 259 | 281 | |
| 260 | 282 | // If the forum is trash, or the forum_status is switched to |
| 261 | 283 | // trash, trash it properly |
| 262 | - if ( ( get_post_field( 'post_status', $forum_id ) == bbp_get_trash_status_id() ) || ( $forum_data['post_status'] == bbp_get_trash_status_id() ) ) { | |
| 284 | + if ( ( get_post_field( 'post_status', $forum_id ) === bbp_get_trash_status_id() ) || ( $forum_data['post_status'] === bbp_get_trash_status_id() ) ) { | |
| 263 | 285 | |
| 264 | 286 | // Trash the reply |
| 265 | 287 | wp_trash_post( $forum_id ); |
| 266 | 288 | |
| @@ -270,9 +292,9 @@ | ||
| 270 | 292 | |
| 271 | 293 | /** Spam Check ********************************************************/ |
| 272 | 294 | |
| 273 | 295 | // If reply or forum are spam, officially spam this reply |
| 274 | - if ( $forum_data['post_status'] == bbp_get_spam_status_id() ) { | |
| 296 | + if ( $forum_data['post_status'] === bbp_get_spam_status_id() ) { | |
| 275 | 297 | add_post_meta( $forum_id, '_bbp_spam_meta_status', bbp_get_public_status_id() ); |
| 276 | 298 | |
| 277 | 299 | // Force view=all |
| 278 | 300 | $view_all = true; |
| @@ -279,9 +301,9 @@ | ||
| 279 | 301 | } |
| 280 | 302 | |
| 281 | 303 | /** Update counts, etc... *********************************************/ |
| 282 | 304 | |
| 283 | - $forum_args = array( | |
| 305 | + do_action( 'bbp_new_forum', array( | |
| 284 | 306 | 'forum_id' => $forum_id, |
| 285 | 307 | 'post_parent' => $forum_parent_id, |
| 286 | 308 | 'forum_author' => $forum_author, |
| 287 | 309 | 'last_topic_id' => 0, |
| @@ -288,10 +310,9 @@ | ||
| 288 | 310 | 'last_reply_id' => 0, |
| 289 | 311 | 'last_active_id' => 0, |
| 290 | 312 | 'last_active_time' => 0, |
| 291 | 313 | 'last_active_status' => bbp_get_public_status_id() |
| 292 | - ); | |
| 293 | - do_action( 'bbp_new_forum', $forum_args ); | |
| 314 | + ) ); | |
| 294 | 315 | |
| 295 | 316 | /** Additional Actions (After Save) ***********************************/ |
| 296 | 317 | |
| 297 | 318 | do_action( 'bbp_new_forum_post_extras', $forum_id ); |
| @@ -298,18 +319,18 @@ | ||
| 298 | 319 | |
| 299 | 320 | /** Redirect **********************************************************/ |
| 300 | 321 | |
| 301 | 322 | // Redirect to |
| 302 | - $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : ''; | |
| 323 | + $redirect_to = bbp_get_redirect_to(); | |
| 303 | 324 | |
| 304 | 325 | // Get the forum URL |
| 305 | 326 | $redirect_url = bbp_get_forum_permalink( $forum_id, $redirect_to ); |
| 306 | 327 | |
| 307 | 328 | // Add view all? |
| 308 | - if ( bbp_get_view_all() || !empty( $view_all ) ) { | |
| 329 | + if ( bbp_get_view_all() || ! empty( $view_all ) ) { | |
| 309 | 330 | |
| 310 | 331 | // User can moderate, so redirect to forum with view all set |
| 311 | - if ( current_user_can( 'moderate' ) ) { | |
| 332 | + if ( current_user_can( 'moderate', $forum_id ) ) { | |
| 312 | 333 | $redirect_url = bbp_add_view_all( $redirect_url ); |
| 313 | 334 | |
| 314 | 335 | // User cannot moderate, so redirect to forum |
| 315 | 336 | } else { |
| @@ -322,17 +343,19 @@ | ||
| 322 | 343 | |
| 323 | 344 | /** Successful Save ***************************************************/ |
| 324 | 345 | |
| 325 | 346 | // Redirect back to new forum |
| 326 | - wp_safe_redirect( $redirect_url ); | |
| 347 | + bbp_redirect( $redirect_url ); | |
| 327 | 348 | |
| 328 | - // For good measure | |
| 329 | - exit(); | |
| 349 | + /** Errors ****************************************************************/ | |
| 330 | 350 | |
| 331 | - // Errors | |
| 351 | + // WP_Error | |
| 352 | + } elseif ( is_wp_error( $forum_id ) ) { | |
| 353 | + bbp_add_error( 'bbp_forum_error', sprintf( __( '<strong>Error</strong>: The following problem(s) occurred: %s', 'bbpress' ), $forum_id->get_error_message() ) ); | |
| 354 | + | |
| 355 | + // Generic error | |
| 332 | 356 | } else { |
| 333 | - $append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : ''; | |
| 334 | - bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error, 'bbpress' ) ); | |
| 357 | + bbp_add_error( 'bbp_forum_error', __( '<strong>Error</strong>: The forum was not created.', 'bbpress' ) ); | |
| 335 | 358 | } |
| 336 | 359 | } |
| 337 | 360 | |
| 338 | 361 | /** |
| @@ -337,46 +360,16 @@ | ||
| 337 | 360 | |
| 338 | 361 | /** |
| 339 | 362 | * Handles the front end edit forum submission |
| 340 | 363 | * |
| 341 | - * @uses bbPress:errors::add() To log various error messages | |
| 342 | - * @uses bbp_get_forum() To get the forum | |
| 343 | - * @uses bbp_verify_nonce_request() To verify the nonce and check the request | |
| 344 | - * @uses bbp_is_forum_anonymous() To check if forum is by an anonymous user | |
| 345 | - * @uses current_user_can() To check if the current user can edit the forum | |
| 346 | - * @uses bbp_filter_anonymous_post_data() To filter anonymous data | |
| 347 | - * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error} | |
| 348 | - * @uses esc_attr() For sanitization | |
| 349 | - * @uses bbp_is_forum_category() To check if the forum is a category | |
| 350 | - * @uses bbp_is_forum_closed() To check if the forum is closed | |
| 351 | - * @uses bbp_is_forum_private() To check if the forum is private | |
| 352 | - * @uses remove_filter() To remove 'wp_filter_kses' filters if needed | |
| 353 | - * @uses apply_filters() Calls 'bbp_edit_forum_pre_title' with the title and | |
| 354 | - * forum id | |
| 355 | - * @uses apply_filters() Calls 'bbp_edit_forum_pre_content' with the content | |
| 356 | - * and forum id | |
| 357 | - * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors | |
| 358 | - * @uses wp_save_post_revision() To save a forum revision | |
| 359 | - * @uses bbp_update_forum_revision_log() To update the forum revision log | |
| 360 | - * @uses wp_update_post() To update the forum | |
| 361 | - * @uses do_action() Calls 'bbp_edit_forum' with the forum id, forum id, | |
| 362 | - * anonymous data and reply author | |
| 363 | - * @uses bbp_move_forum_handler() To handle movement of a forum from one forum | |
| 364 | - * to another | |
| 365 | - * @uses bbp_get_forum_permalink() To get the forum permalink | |
| 366 | - * @uses wp_safe_redirect() To redirect to the forum link | |
| 367 | - * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error | |
| 368 | - * messages | |
| 364 | + * @param string $action The requested action to compare this function to | |
| 369 | 365 | */ |
| 370 | -function bbp_edit_forum_handler() { | |
| 366 | +function bbp_edit_forum_handler( $action = '' ) { | |
| 371 | 367 | |
| 372 | - // Bail if not a POST action | |
| 373 | - if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) | |
| 374 | - return; | |
| 375 | - | |
| 376 | 368 | // Bail if action is not bbp-edit-forum |
| 377 | - if ( empty( $_POST['action'] ) || ( 'bbp-edit-forum' !== $_POST['action'] ) ) | |
| 369 | + if ( 'bbp-edit-forum' !== $action ) { | |
| 378 | 370 | return; |
| 371 | + } | |
| 379 | 372 | |
| 380 | 373 | // Define local variable(s) |
| 381 | 374 | $anonymous_data = array(); |
| 382 | 375 | $forum = $forum_id = $forum_parent_id = 0; |
| @@ -385,9 +378,9 @@ | ||
| 385 | 378 | /** Forum *****************************************************************/ |
| 386 | 379 | |
| 387 | 380 | // Forum id was not passed |
| 388 | 381 | if ( empty( $_POST['bbp_forum_id'] ) ) { |
| 389 | - bbp_add_error( 'bbp_edit_forum_id', __( '<strong>ERROR</strong>: Forum ID not found.', 'bbpress' ) ); | |
| 382 | + bbp_add_error( 'bbp_edit_forum_id', __( '<strong>Error</strong>: Forum ID not found.', 'bbpress' ) ); | |
| 390 | 383 | return; |
| 391 | 384 | |
| 392 | 385 | // Forum id was passed |
| 393 | 386 | } elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) { |
| @@ -396,33 +389,34 @@ | ||
| 396 | 389 | } |
| 397 | 390 | |
| 398 | 391 | // Nonce check |
| 399 | 392 | if ( ! bbp_verify_nonce_request( 'bbp-edit-forum_' . $forum_id ) ) { |
| 400 | - bbp_add_error( 'bbp_edit_forum_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); | |
| 393 | + bbp_add_error( 'bbp_edit_forum_nonce', __( '<strong>Error</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); | |
| 401 | 394 | return; |
| 402 | 395 | |
| 403 | 396 | // Forum does not exist |
| 404 | 397 | } elseif ( empty( $forum ) ) { |
| 405 | - bbp_add_error( 'bbp_edit_forum_not_found', __( '<strong>ERROR</strong>: The forum you want to edit was not found.', 'bbpress' ) ); | |
| 398 | + bbp_add_error( 'bbp_edit_forum_not_found', __( '<strong>Error</strong>: The forum you want to edit was not found.', 'bbpress' ) ); | |
| 406 | 399 | return; |
| 407 | 400 | |
| 408 | 401 | // User cannot edit this forum |
| 409 | - } elseif ( !current_user_can( 'edit_forum', $forum_id ) ) { | |
| 410 | - bbp_add_error( 'bbp_edit_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that forum.', 'bbpress' ) ); | |
| 402 | + } elseif ( ! current_user_can( 'edit_forum', $forum_id ) ) { | |
| 403 | + bbp_add_error( 'bbp_edit_forum_permission', __( '<strong>Error</strong>: You do not have permission to edit that forum.', 'bbpress' ) ); | |
| 411 | 404 | return; |
| 412 | 405 | } |
| 413 | 406 | |
| 414 | - // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified | |
| 415 | - if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_forum'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-forum_' . $forum_id ) == $_POST['_bbp_unfiltered_html_forum'] ) ) { | |
| 416 | - remove_filter( 'bbp_edit_forum_pre_title', 'wp_filter_kses' ); | |
| 417 | - remove_filter( 'bbp_edit_forum_pre_content', 'wp_filter_kses' ); | |
| 407 | + // Remove kses filters from title and content for capable users and if the nonce is verified | |
| 408 | + if ( current_user_can( 'unfiltered_html' ) && ! empty( $_POST['_bbp_unfiltered_html_forum'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-forum_' . $forum_id ) === $_POST['_bbp_unfiltered_html_forum'] ) ) { | |
| 409 | + remove_filter( 'bbp_edit_forum_pre_title', 'wp_filter_kses' ); | |
| 410 | + remove_filter( 'bbp_edit_forum_pre_content', 'bbp_encode_bad', 10 ); | |
| 411 | + remove_filter( 'bbp_edit_forum_pre_content', 'bbp_filter_kses', 30 ); | |
| 418 | 412 | } |
| 419 | 413 | |
| 420 | 414 | /** Forum Parent ***********************************************************/ |
| 421 | 415 | |
| 422 | 416 | // Forum parent id was passed |
| 423 | - if ( is_numeric( $_POST['bbp_forum_parent_id'] ) ) { | |
| 424 | - $forum_parent_id = (int) $_POST['bbp_forum_parent_id']; | |
| 417 | + if ( ! empty( $_POST['bbp_forum_parent_id'] ) ) { | |
| 418 | + $forum_parent_id = bbp_get_forum_id( $_POST['bbp_forum_parent_id'] ); | |
| 425 | 419 | } |
| 426 | 420 | |
| 427 | 421 | // Current forum this forum is in |
| 428 | 422 | $current_parent_forum_id = bbp_get_forum_parent_id( $forum_id ); |
| @@ -427,60 +421,71 @@ | ||
| 427 | 421 | // Current forum this forum is in |
| 428 | 422 | $current_parent_forum_id = bbp_get_forum_parent_id( $forum_id ); |
| 429 | 423 | |
| 430 | 424 | // Forum exists |
| 431 | - if ( !empty( $forum_parent_id ) && ( $forum_parent_id !== $current_parent_forum_id ) ) { | |
| 425 | + if ( ! empty( $forum_parent_id ) && ( $forum_parent_id !== $current_parent_forum_id ) ) { | |
| 432 | 426 | |
| 433 | 427 | // Forum is closed and user cannot access |
| 434 | - if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) { | |
| 435 | - bbp_add_error( 'bbp_edit_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) ); | |
| 428 | + if ( bbp_is_forum_closed( $forum_parent_id ) && ! current_user_can( 'edit_forum', $forum_parent_id ) ) { | |
| 429 | + bbp_add_error( 'bbp_edit_forum_forum_closed', __( '<strong>Error</strong>: This forum has been closed to new forums.', 'bbpress' ) ); | |
| 436 | 430 | } |
| 437 | 431 | |
| 438 | 432 | // Forum is private and user cannot access |
| 439 | - if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) { | |
| 440 | - bbp_add_error( 'bbp_edit_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); | |
| 433 | + if ( bbp_is_forum_private( $forum_parent_id ) && ! current_user_can( 'read_forum', $forum_parent_id ) ) { | |
| 434 | + bbp_add_error( 'bbp_edit_forum_forum_private', __( '<strong>Error</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); | |
| 441 | 435 | } |
| 442 | 436 | |
| 443 | 437 | // Forum is hidden and user cannot access |
| 444 | - if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) { | |
| 445 | - bbp_add_error( 'bbp_edit_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); | |
| 438 | + if ( bbp_is_forum_hidden( $forum_parent_id ) && ! current_user_can( 'read_forum', $forum_parent_id ) ) { | |
| 439 | + bbp_add_error( 'bbp_edit_forum_forum_hidden', __( '<strong>Error</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); | |
| 446 | 440 | } |
| 447 | 441 | } |
| 448 | 442 | |
| 449 | 443 | /** Forum Title ***********************************************************/ |
| 450 | 444 | |
| 451 | - if ( !empty( $_POST['bbp_forum_title'] ) ) | |
| 452 | - $forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) ); | |
| 445 | + if ( ! empty( $_POST['bbp_forum_title'] ) ) { | |
| 446 | + $forum_title = sanitize_text_field( $_POST['bbp_forum_title'] ); | |
| 447 | + } | |
| 453 | 448 | |
| 454 | 449 | // Filter and sanitize |
| 455 | 450 | $forum_title = apply_filters( 'bbp_edit_forum_pre_title', $forum_title, $forum_id ); |
| 456 | 451 | |
| 457 | 452 | // No forum title |
| 458 | - if ( empty( $forum_title ) ) | |
| 459 | - bbp_add_error( 'bbp_edit_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) ); | |
| 453 | + if ( empty( $forum_title ) ) { | |
| 454 | + bbp_add_error( 'bbp_edit_forum_title', __( '<strong>Error</strong>: Your forum needs a title.', 'bbpress' ) ); | |
| 455 | + } | |
| 460 | 456 | |
| 457 | + // Title too long | |
| 458 | + if ( bbp_is_title_too_long( $forum_title ) ) { | |
| 459 | + bbp_add_error( 'bbp_forum_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) ); | |
| 460 | + } | |
| 461 | + | |
| 461 | 462 | /** Forum Content *********************************************************/ |
| 462 | 463 | |
| 463 | - if ( !empty( $_POST['bbp_forum_content'] ) ) | |
| 464 | + if ( ! empty( $_POST['bbp_forum_content'] ) ) { | |
| 464 | 465 | $forum_content = $_POST['bbp_forum_content']; |
| 466 | + } | |
| 465 | 467 | |
| 466 | 468 | // Filter and sanitize |
| 467 | 469 | $forum_content = apply_filters( 'bbp_edit_forum_pre_content', $forum_content, $forum_id ); |
| 468 | 470 | |
| 469 | 471 | // No forum content |
| 470 | - if ( empty( $forum_content ) ) | |
| 471 | - bbp_add_error( 'bbp_edit_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress' ) ); | |
| 472 | + if ( empty( $forum_content ) ) { | |
| 473 | + bbp_add_error( 'bbp_edit_forum_content', __( '<strong>Error</strong>: Your forum description cannot be empty.', 'bbpress' ) ); | |
| 474 | + } | |
| 472 | 475 | |
| 473 | - /** Forum Blacklist *******************************************************/ | |
| 476 | + /** Forum Bad Words *******************************************************/ | |
| 474 | 477 | |
| 475 | - if ( !bbp_check_for_blacklist( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) ) | |
| 476 | - bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be edited at this time.', 'bbpress' ) ); | |
| 478 | + if ( ! bbp_check_for_moderation( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content, true ) ) { | |
| 479 | + bbp_add_error( 'bbp_forum_moderation', __( '<strong>Error</strong>: Your forum cannot be edited at this time.', 'bbpress' ) ); | |
| 480 | + } | |
| 477 | 481 | |
| 478 | 482 | /** Forum Moderation ******************************************************/ |
| 479 | 483 | |
| 480 | 484 | $post_status = bbp_get_public_status_id(); |
| 481 | - if ( !bbp_check_for_moderation( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) ) | |
| 485 | + if ( ! bbp_check_for_moderation( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) ) { | |
| 482 | 486 | $post_status = bbp_get_pending_status_id(); |
| 487 | + } | |
| 483 | 488 | |
| 484 | 489 | /** Additional Actions (Before Save) **************************************/ |
| 485 | 490 | |
| 486 | 491 | do_action( 'bbp_edit_forum_pre_extras', $forum_id ); |
| @@ -485,10 +490,11 @@ | ||
| 485 | 490 | |
| 486 | 491 | do_action( 'bbp_edit_forum_pre_extras', $forum_id ); |
| 487 | 492 | |
| 488 | 493 | // Bail if errors |
| 489 | - if ( bbp_has_errors() ) | |
| 494 | + if ( bbp_has_errors() ) { | |
| 490 | 495 | return; |
| 496 | + } | |
| 491 | 497 | |
| 492 | 498 | /** No Errors *************************************************************/ |
| 493 | 499 | |
| 494 | 500 | // Add the content of the form to $forum_data as an array |
| @@ -503,33 +509,14 @@ | ||
| 503 | 509 | |
| 504 | 510 | // Insert forum |
| 505 | 511 | $forum_id = wp_update_post( $forum_data ); |
| 506 | 512 | |
| 507 | - /** Revisions *************************************************************/ | |
| 508 | - | |
| 509 | - /** | |
| 510 | - * @todo omitted for 2.1 | |
| 511 | - // Revision Reason | |
| 512 | - if ( !empty( $_POST['bbp_forum_edit_reason'] ) ) | |
| 513 | - $forum_edit_reason = esc_attr( strip_tags( $_POST['bbp_forum_edit_reason'] ) ); | |
| 514 | - | |
| 515 | - // Update revision log | |
| 516 | - if ( !empty( $_POST['bbp_log_forum_edit'] ) && ( 1 == $_POST['bbp_log_forum_edit'] ) && ( $revision_id = wp_save_post_revision( $forum_id ) ) ) { | |
| 517 | - bbp_update_forum_revision_log( array( | |
| 518 | - 'forum_id' => $forum_id, | |
| 519 | - 'revision_id' => $revision_id, | |
| 520 | - 'author_id' => bbp_get_current_user_id(), | |
| 521 | - 'reason' => $forum_edit_reason | |
| 522 | - ) ); | |
| 523 | - } | |
| 524 | - */ | |
| 525 | - | |
| 526 | 513 | /** No Errors *************************************************************/ |
| 527 | 514 | |
| 528 | - if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) { | |
| 515 | + if ( ! empty( $forum_id ) && ! is_wp_error( $forum_id ) ) { | |
| 529 | 516 | |
| 530 | 517 | // Update counts, etc... |
| 531 | - $forum_args = array( | |
| 518 | + do_action( 'bbp_edit_forum', array( | |
| 532 | 519 | 'forum_id' => $forum_id, |
| 533 | 520 | 'post_parent' => $forum_parent_id, |
| 534 | 521 | 'forum_author' => $forum->post_author, |
| 535 | 522 | 'last_topic_id' => 0, |
| @@ -536,18 +523,42 @@ | ||
| 536 | 523 | 'last_reply_id' => 0, |
| 537 | 524 | 'last_active_id' => 0, |
| 538 | 525 | 'last_active_time' => 0, |
| 539 | 526 | 'last_active_status' => bbp_get_public_status_id() |
| 540 | - ); | |
| 541 | - do_action( 'bbp_edit_forum', $forum_args ); | |
| 527 | + ) ); | |
| 542 | 528 | |
| 529 | + /** Revisions *********************************************************/ | |
| 530 | + | |
| 531 | + // Update locks | |
| 532 | + update_post_meta( $forum_id, '_edit_last', bbp_get_current_user_id() ); | |
| 533 | + delete_post_meta( $forum_id, '_edit_lock' ); | |
| 534 | + | |
| 535 | + /** | |
| 536 | + * @todo omitted for now | |
| 537 | + // Revision Reason | |
| 538 | + if ( ! empty( $_POST['bbp_forum_edit_reason'] ) ) | |
| 539 | + $forum_edit_reason = sanitize_text_field( $_POST['bbp_forum_edit_reason'] ); | |
| 540 | + | |
| 541 | + // Update revision log | |
| 542 | + if ( ! empty( $_POST['bbp_log_forum_edit'] ) && ( "1" === $_POST['bbp_log_forum_edit'] ) && ( $revision_id = wp_save_post_revision( $forum_id ) ) ) { | |
| 543 | + bbp_update_forum_revision_log( array( | |
| 544 | + 'forum_id' => $forum_id, | |
| 545 | + 'revision_id' => $revision_id, | |
| 546 | + 'author_id' => bbp_get_current_user_id(), | |
| 547 | + 'reason' => $forum_edit_reason | |
| 548 | + ) ); | |
| 549 | + } | |
| 550 | + | |
| 543 | 551 | // If the new forum parent id is not equal to the old forum parent |
| 544 | 552 | // id, run the bbp_move_forum action and pass the forum's parent id |
| 545 | - // as the first arg and new forum parent id as the second. | |
| 553 | + // as the first argument and new forum parent id as the second. | |
| 546 | 554 | // @todo implement |
| 547 | - //if ( $forum_id != $forum->post_parent ) | |
| 548 | - // bbp_move_forum_handler( $forum_parent_id, $forum->post_parent, $forum_id ); | |
| 555 | + if ( $forum_id !== $forum->post_parent ) { | |
| 556 | + bbp_move_forum_handler( $forum_parent_id, $forum->post_parent, $forum_id ); | |
| 557 | + } | |
| 549 | 558 | |
| 559 | + */ | |
| 560 | + | |
| 550 | 561 | /** Additional Actions (After Save) ***********************************/ |
| 551 | 562 | |
| 552 | 563 | do_action( 'bbp_edit_forum_post_extras', $forum_id ); |
| 553 | 564 | |
| @@ -553,9 +564,9 @@ | ||
| 553 | 564 | |
| 554 | 565 | /** Redirect **********************************************************/ |
| 555 | 566 | |
| 556 | 567 | // Redirect to |
| 557 | - $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : ''; | |
| 568 | + $redirect_to = bbp_get_redirect_to(); | |
| 558 | 569 | |
| 559 | 570 | // View all? |
| 560 | 571 | $view_all = bbp_get_view_all(); |
| 561 | 572 | |
| @@ -562,10 +573,11 @@ | ||
| 562 | 573 | // Get the forum URL |
| 563 | 574 | $forum_url = bbp_get_forum_permalink( $forum_id, $redirect_to ); |
| 564 | 575 | |
| 565 | 576 | // Add view all? |
| 566 | - if ( !empty( $view_all ) ) | |
| 577 | + if ( ! empty( $view_all ) ) { | |
| 567 | 578 | $forum_url = bbp_add_view_all( $forum_url ); |
| 579 | + } | |
| 568 | 580 | |
| 569 | 581 | // Allow to be filtered |
| 570 | 582 | $forum_url = apply_filters( 'bbp_edit_forum_redirect_to', $forum_url, $view_all, $redirect_to ); |
| 571 | 583 | |
| @@ -571,18 +583,15 @@ | ||
| 571 | 583 | |
| 572 | 584 | /** Successful Edit ***************************************************/ |
| 573 | 585 | |
| 574 | 586 | // Redirect back to new forum |
| 575 | - wp_safe_redirect( $forum_url ); | |
| 587 | + bbp_redirect( $forum_url ); | |
| 576 | 588 | |
| 577 | - // For good measure | |
| 578 | - exit(); | |
| 579 | - | |
| 580 | 589 | /** Errors ****************************************************************/ |
| 581 | 590 | |
| 582 | 591 | } else { |
| 583 | 592 | $append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : ''; |
| 584 | - bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error . 'Please try again.', 'bbpress' ) ); | |
| 593 | + bbp_add_error( 'bbp_forum_error', __( '<strong>Error</strong>: The following problem(s) have been found with your forum:' . $append_error . 'Please try again.', 'bbpress' ) ); | |
| 585 | 594 | } |
| 586 | 595 | } |
| 587 | 596 | |
| 588 | 597 | /** |
| @@ -587,23 +596,11 @@ | ||
| 587 | 596 | |
| 588 | 597 | /** |
| 589 | 598 | * Handle the saving of core forum metadata (Status, Visibility, and Type) |
| 590 | 599 | * |
| 591 | - * @since bbPress (r3678) | |
| 600 | + * @since 2.1.0 bbPress (r3678) | |
| 601 | + * | |
| 592 | 602 | * @param int $forum_id |
| 593 | - * @uses bbp_is_forum_closed() To check if forum is closed | |
| 594 | - * @uses bbp_close_forum() To close forum | |
| 595 | - * @uses bbp_open_forum() To open forum | |
| 596 | - * @uses bbp_is_forum_category() To check if forum is a category | |
| 597 | - * @uses bbp_categorize_forum() To turn forum into a category | |
| 598 | - * @uses bbp_normalize_forum() To turn category into forum | |
| 599 | - * @uses bbp_get_public_status_id() To get the public status ID | |
| 600 | - * @uses bbp_get_private_status_id() To get the private status ID | |
| 601 | - * @uses bbp_get_hidden_status_id() To get the hidden status ID | |
| 602 | - * @uses bbp_get_forum_visibility() To get the forums visibility | |
| 603 | - * @uses bbp_hide_forum() To hide a forum | |
| 604 | - * @uses bbp_privatize_forum() To make a forum private | |
| 605 | - * @uses bbp_publicize_forum() To make a forum public | |
| 606 | 603 | * @return If forum ID is empty |
| 607 | 604 | */ |
| 608 | 605 | function bbp_save_forum_extras( $forum_id = 0 ) { |
| 609 | 606 | |
| @@ -610,90 +607,115 @@ | ||
| 610 | 607 | // Validate the forum ID |
| 611 | 608 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 612 | 609 | |
| 613 | 610 | // Bail if forum ID is empty |
| 614 | - if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) | |
| 611 | + if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { | |
| 615 | 612 | return; |
| 613 | + } | |
| 616 | 614 | |
| 617 | - /** Forum Status ******************************************************/ | |
| 615 | + /** Forum Status **********************************************************/ | |
| 618 | 616 | |
| 619 | - if ( !empty( $_POST['bbp_forum_status'] ) && in_array( $_POST['bbp_forum_status'], array( 'open', 'closed' ) ) ) { | |
| 620 | - if ( 'closed' == $_POST['bbp_forum_status'] && !bbp_is_forum_closed( $forum_id, false ) ) { | |
| 617 | + if ( ! empty( $_POST['bbp_forum_status'] ) && in_array( $_POST['bbp_forum_status'], array( 'open', 'closed' ), true ) ) { | |
| 618 | + if ( 'closed' === $_POST['bbp_forum_status'] && ! bbp_is_forum_closed( $forum_id, false ) ) { | |
| 621 | 619 | bbp_close_forum( $forum_id ); |
| 622 | - } elseif ( 'open' == $_POST['bbp_forum_status'] && bbp_is_forum_closed( $forum_id, false ) ) { | |
| 620 | + } elseif ( 'open' === $_POST['bbp_forum_status'] && bbp_is_forum_open( $forum_id, false ) ) { | |
| 623 | 621 | bbp_open_forum( $forum_id ); |
| 622 | + } elseif ( 'open' === $_POST['bbp_forum_status'] && bbp_is_forum_closed( $forum_id, false ) ) { | |
| 623 | + bbp_open_forum( $forum_id ); | |
| 624 | 624 | } |
| 625 | 625 | } |
| 626 | 626 | |
| 627 | - /** Forum Type ********************************************************/ | |
| 627 | + /** Forum Type ************************************************************/ | |
| 628 | 628 | |
| 629 | - if ( !empty( $_POST['bbp_forum_type'] ) && in_array( $_POST['bbp_forum_type'], array( 'forum', 'category' ) ) ) { | |
| 630 | - if ( 'category' == $_POST['bbp_forum_type'] && !bbp_is_forum_category( $forum_id ) ) { | |
| 629 | + if ( ! empty( $_POST['bbp_forum_type'] ) && in_array( $_POST['bbp_forum_type'], array( 'forum', 'category' ), true ) ) { | |
| 630 | + if ( 'category' === $_POST['bbp_forum_type'] && ! bbp_is_forum_category( $forum_id ) ) { | |
| 631 | 631 | bbp_categorize_forum( $forum_id ); |
| 632 | - } elseif ( 'forum' == $_POST['bbp_forum_type'] && bbp_is_forum_category( $forum_id ) ) { | |
| 632 | + } elseif ( 'forum' === $_POST['bbp_forum_type'] && ! bbp_is_forum_category( $forum_id ) ) { | |
| 633 | 633 | bbp_normalize_forum( $forum_id ); |
| 634 | + } elseif ( 'forum' === $_POST['bbp_forum_type'] && bbp_is_forum_category( $forum_id ) ) { | |
| 635 | + bbp_normalize_forum( $forum_id ); | |
| 634 | 636 | } |
| 635 | 637 | } |
| 636 | 638 | |
| 637 | - /** Forum Visibility **************************************************/ | |
| 639 | + /** Forum Visibility ******************************************************/ | |
| 638 | 640 | |
| 639 | - if ( !empty( $_POST['bbp_forum_visibility'] ) && in_array( $_POST['bbp_forum_visibility'], array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id() ) ) ) { | |
| 641 | + if ( ! empty( $_POST['bbp_forum_visibility'] ) && in_array( $_POST['bbp_forum_visibility'], array_keys( bbp_get_forum_visibilities() ), true ) ) { | |
| 640 | 642 | |
| 641 | 643 | // Get forums current visibility |
| 642 | - $visibility = bbp_get_forum_visibility( $forum_id ); | |
| 644 | + $old_visibility = bbp_get_forum_visibility( $forum_id ); | |
| 643 | 645 | |
| 646 | + // Sanitize the new visibility | |
| 647 | + $new_visibility = sanitize_key( $_POST['bbp_forum_visibility'] ); | |
| 648 | + | |
| 644 | 649 | // What is the new forum visibility setting? |
| 645 | - switch ( $_POST['bbp_forum_visibility'] ) { | |
| 650 | + switch ( $new_visibility ) { | |
| 646 | 651 | |
| 647 | 652 | // Hidden |
| 648 | 653 | case bbp_get_hidden_status_id() : |
| 649 | - bbp_hide_forum( $forum_id, $visibility ); | |
| 654 | + bbp_hide_forum( $forum_id, $old_visibility ); | |
| 650 | 655 | break; |
| 651 | 656 | |
| 652 | 657 | // Private |
| 653 | 658 | case bbp_get_private_status_id() : |
| 654 | - bbp_privatize_forum( $forum_id, $visibility ); | |
| 659 | + bbp_privatize_forum( $forum_id, $old_visibility ); | |
| 655 | 660 | break; |
| 656 | 661 | |
| 657 | 662 | // Publish (default) |
| 658 | 663 | case bbp_get_public_status_id() : |
| 659 | - default : | |
| 660 | - bbp_publicize_forum( $forum_id, $visibility ); | |
| 664 | + default : | |
| 665 | + bbp_publicize_forum( $forum_id, $old_visibility ); | |
| 661 | 666 | break; |
| 662 | 667 | } |
| 668 | + | |
| 669 | + /** | |
| 670 | + * Allow custom forum visibility save actions | |
| 671 | + * | |
| 672 | + * @since 2.6.0 bbPress (r5855) | |
| 673 | + * | |
| 674 | + * @param int $forum_id The forum ID | |
| 675 | + * @param string $old_visibility The current forum visibility | |
| 676 | + * @param string $new_visibility The new forum visibility | |
| 677 | + */ | |
| 678 | + do_action( 'bbp_update_forum_visibility', $forum_id, $old_visibility, $new_visibility ); | |
| 663 | 679 | } |
| 664 | -} | |
| 665 | 680 | |
| 666 | -/** Walk **********************************************************************/ | |
| 681 | + /** Forum Moderators ******************************************************/ | |
| 667 | 682 | |
| 668 | -/** | |
| 669 | - * Walk the forum tree | |
| 670 | - * | |
| 671 | - * @param object $forums Forums | |
| 672 | - * @param int $depth Depth | |
| 673 | - * @param int $current Current forum | |
| 674 | - * @param array $r Parsed arguments, supported by the walker. If you want to | |
| 675 | - * use your own walker, pass the 'walker' arg with the walker. | |
| 676 | - * The walker defaults to {@link BBP_Walker_Forum} | |
| 677 | - * @return object Walked forum tree | |
| 678 | - */ | |
| 679 | -function bbp_walk_forum( $forums, $depth, $current, $r ) { | |
| 680 | - $walker = empty( $r['walker'] ) ? new BBP_Walker_Forum : $r['walker']; | |
| 681 | - $args = array( $forums, $depth, $r, $current ); | |
| 682 | - return call_user_func_array( array( &$walker, 'walk' ), $args ); | |
| 683 | + // Either replace terms | |
| 684 | + if ( bbp_allow_forum_mods() ) { | |
| 685 | + if ( current_user_can( 'assign_moderators' ) && ! empty( $_POST['bbp_moderators'] ) ) { | |
| 686 | + | |
| 687 | + // Escape tag input | |
| 688 | + $users = sanitize_text_field( $_POST['bbp_moderators'] ); | |
| 689 | + $user_ids = bbp_get_user_ids_from_nicenames( $users ); | |
| 690 | + | |
| 691 | + // Update forum moderators | |
| 692 | + if ( ! empty( $user_ids ) ) { | |
| 693 | + | |
| 694 | + // Remove all moderators | |
| 695 | + bbp_remove_moderator( $forum_id, null ); | |
| 696 | + | |
| 697 | + // Add moderators | |
| 698 | + foreach ( $user_ids as $user_id ) { | |
| 699 | + bbp_add_moderator( $forum_id, $user_id ); | |
| 700 | + } | |
| 701 | + } | |
| 702 | + | |
| 703 | + // ...or remove them. | |
| 704 | + } elseif ( isset( $_POST['bbp_moderators'] ) ) { | |
| 705 | + bbp_remove_moderator( $forum_id, null ); | |
| 706 | + } | |
| 707 | + } | |
| 683 | 708 | } |
| 684 | 709 | |
| 685 | -/** Forum Actions *************************************************************/ | |
| 710 | +/** Forum Open/Close **********************************************************/ | |
| 686 | 711 | |
| 687 | 712 | /** |
| 688 | 713 | * Closes a forum |
| 689 | 714 | * |
| 690 | - * @since bbPress (r2746) | |
| 715 | + * @since 2.0.0 bbPress (r2746) | |
| 691 | 716 | * |
| 692 | 717 | * @param int $forum_id forum id |
| 693 | - * @uses do_action() Calls 'bbp_close_forum' with the forum id | |
| 694 | - * @uses update_post_meta() To add the previous status to a meta | |
| 695 | - * @uses do_action() Calls 'bbp_opened_forum' with the forum id | |
| 696 | 718 | * @return mixed False or {@link WP_Error} on failure, forum id on success |
| 697 | 719 | */ |
| 698 | 720 | function bbp_close_forum( $forum_id = 0 ) { |
| 699 | 721 | |
| @@ -710,15 +732,11 @@ | ||
| 710 | 732 | |
| 711 | 733 | /** |
| 712 | 734 | * Opens a forum |
| 713 | 735 | * |
| 714 | - * @since bbPress (r2746) | |
| 736 | + * @since 2.0.0 bbPress (r2746) | |
| 715 | 737 | * |
| 716 | 738 | * @param int $forum_id forum id |
| 717 | - * @uses do_action() Calls 'bbp_open_forum' with the forum id | |
| 718 | - * @uses get_post_meta() To get the previous status | |
| 719 | - * @uses update_post_meta() To delete the previous status meta | |
| 720 | - * @uses do_action() Calls 'bbp_opened_forum' with the forum id | |
| 721 | 739 | * @return mixed False or {@link WP_Error} on failure, forum id on success |
| 722 | 740 | */ |
| 723 | 741 | function bbp_open_forum( $forum_id = 0 ) { |
| 724 | 742 | |
| @@ -732,15 +750,16 @@ | ||
| 732 | 750 | |
| 733 | 751 | return $forum_id; |
| 734 | 752 | } |
| 735 | 753 | |
| 754 | +/** Forum Type ****************************************************************/ | |
| 755 | + | |
| 736 | 756 | /** |
| 737 | 757 | * Make the forum a category |
| 738 | 758 | * |
| 739 | - * @since bbPress (r2746) | |
| 759 | + * @since 2.0.0 bbPress (r2746) | |
| 740 | 760 | * |
| 741 | 761 | * @param int $forum_id Optional. Forum id |
| 742 | - * @uses update_post_meta() To update the forum category meta | |
| 743 | 762 | * @return bool False on failure, true on success |
| 744 | 763 | */ |
| 745 | 764 | function bbp_categorize_forum( $forum_id = 0 ) { |
| 746 | 765 | |
| @@ -757,12 +776,11 @@ | ||
| 757 | 776 | |
| 758 | 777 | /** |
| 759 | 778 | * Remove the category status from a forum |
| 760 | 779 | * |
| 761 | - * @since bbPress (r2746) | |
| 780 | + * @since 2.0.0 bbPress (r2746) | |
| 762 | 781 | * |
| 763 | 782 | * @param int $forum_id Optional. Forum id |
| 764 | - * @uses delete_post_meta() To delete the forum category meta | |
| 765 | 783 | * @return bool False on failure, true on success |
| 766 | 784 | */ |
| 767 | 785 | function bbp_normalize_forum( $forum_id = 0 ) { |
| 768 | 786 | |
| @@ -776,15 +794,16 @@ | ||
| 776 | 794 | |
| 777 | 795 | return $forum_id; |
| 778 | 796 | } |
| 779 | 797 | |
| 798 | +/** Forum Visibility **********************************************************/ | |
| 799 | + | |
| 780 | 800 | /** |
| 781 | 801 | * Mark the forum as public |
| 782 | 802 | * |
| 783 | - * @since bbPress (r2746) | |
| 803 | + * @since 2.0.0 bbPress (r2746) | |
| 784 | 804 | * |
| 785 | 805 | * @param int $forum_id Optional. Forum id |
| 786 | - * @uses update_post_meta() To update the forum private meta | |
| 787 | 806 | * @return bool False on failure, true on success |
| 788 | 807 | */ |
| 789 | 808 | function bbp_publicize_forum( $forum_id = 0, $current_visibility = '' ) { |
| 790 | 809 | |
| @@ -795,17 +814,17 @@ | ||
| 795 | 814 | // Get private forums |
| 796 | 815 | $private = bbp_get_private_forum_ids(); |
| 797 | 816 | |
| 798 | 817 | // Find this forum in the array |
| 799 | - if ( in_array( $forum_id, $private ) ) { | |
| 818 | + if ( in_array( $forum_id, $private, true ) ) { | |
| 800 | 819 | |
| 801 | - $offset = array_search( $forum_id, $private ); | |
| 820 | + $offset = array_search( $forum_id, $private, true ); | |
| 802 | 821 | |
| 803 | 822 | // Splice around it |
| 804 | 823 | array_splice( $private, $offset, 1 ); |
| 805 | 824 | |
| 806 | 825 | // Update private forums minus this one |
| 807 | - update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) ); | |
| 826 | + update_option( '_bbp_private_forums', bbp_get_unique_array_values( $private ) ); | |
| 808 | 827 | } |
| 809 | 828 | |
| 810 | 829 | // Get hidden forums |
| 811 | 830 | $hidden = bbp_get_hidden_forum_ids(); |
| @@ -810,26 +829,25 @@ | ||
| 810 | 829 | // Get hidden forums |
| 811 | 830 | $hidden = bbp_get_hidden_forum_ids(); |
| 812 | 831 | |
| 813 | 832 | // Find this forum in the array |
| 814 | - if ( in_array( $forum_id, $hidden ) ) { | |
| 833 | + if ( in_array( $forum_id, $hidden, true ) ) { | |
| 815 | 834 | |
| 816 | - $offset = array_search( $forum_id, $hidden ); | |
| 835 | + $offset = array_search( $forum_id, $hidden, true ); | |
| 817 | 836 | |
| 818 | 837 | // Splice around it |
| 819 | 838 | array_splice( $hidden, $offset, 1 ); |
| 820 | 839 | |
| 821 | 840 | // Update hidden forums minus this one |
| 822 | - update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) ); | |
| 841 | + update_option( '_bbp_hidden_forums', bbp_get_unique_array_values( $hidden ) ); | |
| 823 | 842 | } |
| 824 | 843 | |
| 825 | 844 | // Only run queries if visibility is changing |
| 826 | - if ( bbp_get_public_status_id() != $current_visibility ) { | |
| 827 | - | |
| 828 | - // Update forum post_status | |
| 829 | - global $wpdb; | |
| 830 | - $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_public_status_id() ), array( 'ID' => $forum_id ) ); | |
| 845 | + if ( bbp_get_public_status_id() !== $current_visibility ) { | |
| 846 | + $bbp_db = bbp_db(); | |
| 847 | + $bbp_db->update( $bbp_db->posts, array( 'post_status' => bbp_get_public_status_id() ), array( 'ID' => $forum_id ) ); | |
| 831 | 848 | wp_transition_post_status( bbp_get_public_status_id(), $current_visibility, get_post( $forum_id ) ); |
| 849 | + clean_post_cache( $forum_id ); | |
| 832 | 850 | } |
| 833 | 851 | |
| 834 | 852 | do_action( 'bbp_publicized_forum', $forum_id ); |
| 835 | 853 | |
| @@ -838,12 +856,11 @@ | ||
| 838 | 856 | |
| 839 | 857 | /** |
| 840 | 858 | * Mark the forum as private |
| 841 | 859 | * |
| 842 | - * @since bbPress (r2746) | |
| 860 | + * @since 2.0.0 bbPress (r2746) | |
| 843 | 861 | * |
| 844 | 862 | * @param int $forum_id Optional. Forum id |
| 845 | - * @uses update_post_meta() To update the forum private meta | |
| 846 | 863 | * @return bool False on failure, true on success |
| 847 | 864 | */ |
| 848 | 865 | function bbp_privatize_forum( $forum_id = 0, $current_visibility = '' ) { |
| 849 | 866 | |
| @@ -851,34 +868,35 @@ | ||
| 851 | 868 | |
| 852 | 869 | do_action( 'bbp_privatize_forum', $forum_id ); |
| 853 | 870 | |
| 854 | 871 | // Only run queries if visibility is changing |
| 855 | - if ( bbp_get_private_status_id() != $current_visibility ) { | |
| 872 | + if ( bbp_get_private_status_id() !== $current_visibility ) { | |
| 856 | 873 | |
| 857 | 874 | // Get hidden forums |
| 858 | 875 | $hidden = bbp_get_hidden_forum_ids(); |
| 859 | 876 | |
| 860 | 877 | // Find this forum in the array |
| 861 | - if ( in_array( $forum_id, $hidden ) ) { | |
| 878 | + if ( in_array( $forum_id, $hidden, true ) ) { | |
| 862 | 879 | |
| 863 | - $offset = array_search( $forum_id, $hidden ); | |
| 880 | + $offset = array_search( $forum_id, $hidden, true ); | |
| 864 | 881 | |
| 865 | 882 | // Splice around it |
| 866 | 883 | array_splice( $hidden, $offset, 1 ); |
| 867 | 884 | |
| 868 | 885 | // Update hidden forums minus this one |
| 869 | - update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) ); | |
| 886 | + update_option( '_bbp_hidden_forums', bbp_get_unique_array_values( $hidden ) ); | |
| 870 | 887 | } |
| 871 | 888 | |
| 872 | 889 | // Add to '_bbp_private_forums' site option |
| 873 | 890 | $private = bbp_get_private_forum_ids(); |
| 874 | 891 | $private[] = $forum_id; |
| 875 | - update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) ); | |
| 892 | + update_option( '_bbp_private_forums', bbp_get_unique_array_values( $private ) ); | |
| 876 | 893 | |
| 877 | 894 | // Update forums visibility setting |
| 878 | - global $wpdb; | |
| 879 | - $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_private_status_id() ), array( 'ID' => $forum_id ) ); | |
| 895 | + $bbp_db = bbp_db(); | |
| 896 | + $bbp_db->update( $bbp_db->posts, array( 'post_status' => bbp_get_private_status_id() ), array( 'ID' => $forum_id ) ); | |
| 880 | 897 | wp_transition_post_status( bbp_get_private_status_id(), $current_visibility, get_post( $forum_id ) ); |
| 898 | + clean_post_cache( $forum_id ); | |
| 881 | 899 | } |
| 882 | 900 | |
| 883 | 901 | do_action( 'bbp_privatized_forum', $forum_id ); |
| 884 | 902 | |
| @@ -887,12 +905,11 @@ | ||
| 887 | 905 | |
| 888 | 906 | /** |
| 889 | 907 | * Mark the forum as hidden |
| 890 | 908 | * |
| 891 | - * @since bbPress (r2996) | |
| 909 | + * @since 2.0.0 bbPress (r2996) | |
| 892 | 910 | * |
| 893 | 911 | * @param int $forum_id Optional. Forum id |
| 894 | - * @uses update_post_meta() To update the forum private meta | |
| 895 | 912 | * @return bool False on failure, true on success |
| 896 | 913 | */ |
| 897 | 914 | function bbp_hide_forum( $forum_id = 0, $current_visibility = '' ) { |
| 898 | 915 | |
| @@ -900,34 +917,35 @@ | ||
| 900 | 917 | |
| 901 | 918 | do_action( 'bbp_hide_forum', $forum_id ); |
| 902 | 919 | |
| 903 | 920 | // Only run queries if visibility is changing |
| 904 | - if ( bbp_get_hidden_status_id() != $current_visibility ) { | |
| 921 | + if ( bbp_get_hidden_status_id() !== $current_visibility ) { | |
| 905 | 922 | |
| 906 | 923 | // Get private forums |
| 907 | 924 | $private = bbp_get_private_forum_ids(); |
| 908 | 925 | |
| 909 | 926 | // Find this forum in the array |
| 910 | - if ( in_array( $forum_id, $private ) ) { | |
| 927 | + if ( in_array( $forum_id, $private, true ) ) { | |
| 911 | 928 | |
| 912 | - $offset = array_search( $forum_id, $private ); | |
| 929 | + $offset = array_search( $forum_id, $private, true ); | |
| 913 | 930 | |
| 914 | 931 | // Splice around it |
| 915 | 932 | array_splice( $private, $offset, 1 ); |
| 916 | 933 | |
| 917 | 934 | // Update private forums minus this one |
| 918 | - update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) ); | |
| 935 | + update_option( '_bbp_private_forums', bbp_get_unique_array_values( $private ) ); | |
| 919 | 936 | } |
| 920 | 937 | |
| 921 | 938 | // Add to '_bbp_hidden_forums' site option |
| 922 | 939 | $hidden = bbp_get_hidden_forum_ids(); |
| 923 | 940 | $hidden[] = $forum_id; |
| 924 | - update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) ); | |
| 941 | + update_option( '_bbp_hidden_forums', bbp_get_unique_array_values( $hidden ) ); | |
| 925 | 942 | |
| 926 | 943 | // Update forums visibility setting |
| 927 | - global $wpdb; | |
| 928 | - $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_hidden_status_id() ), array( 'ID' => $forum_id ) ); | |
| 944 | + $bbp_db = bbp_db(); | |
| 945 | + $bbp_db->update( $bbp_db->posts, array( 'post_status' => bbp_get_hidden_status_id() ), array( 'ID' => $forum_id ) ); | |
| 929 | 946 | wp_transition_post_status( bbp_get_hidden_status_id(), $current_visibility, get_post( $forum_id ) ); |
| 947 | + clean_post_cache( $forum_id ); | |
| 930 | 948 | } |
| 931 | 949 | |
| 932 | 950 | do_action( 'bbp_hid_forum', $forum_id ); |
| 933 | 951 | |
| @@ -933,34 +951,138 @@ | ||
| 933 | 951 | |
| 934 | 952 | return $forum_id; |
| 935 | 953 | } |
| 936 | 954 | |
| 955 | +/** | |
| 956 | + * Recaches the private and hidden forums | |
| 957 | + * | |
| 958 | + * @since 2.4.0 bbPress (r5017) | |
| 959 | + * | |
| 960 | + * @return array An array of the status code and the message | |
| 961 | + */ | |
| 962 | +function bbp_repair_forum_visibility() { | |
| 963 | + | |
| 964 | + // First, delete everything. | |
| 965 | + delete_option( '_bbp_private_forums' ); | |
| 966 | + delete_option( '_bbp_hidden_forums' ); | |
| 967 | + | |
| 968 | + /** | |
| 969 | + * Don't search for both private/hidden statuses. Since 'pre_get_posts' is an | |
| 970 | + * action, it's not removed by suppress_filters. We need to make sure that | |
| 971 | + * we're only searching for the supplied post_status. | |
| 972 | + * | |
| 973 | + * @see https://bbpress.trac.wordpress.org/ticket/2512 | |
| 974 | + */ | |
| 975 | + remove_action( 'pre_get_posts', 'bbp_pre_get_posts_normalize_forum_visibility', 4 ); | |
| 976 | + | |
| 977 | + // Query for private forums | |
| 978 | + $private_forums = new WP_Query( array( | |
| 979 | + 'fields' => 'ids', | |
| 980 | + 'post_type' => bbp_get_forum_post_type(), | |
| 981 | + 'post_status' => bbp_get_private_status_id(), | |
| 982 | + 'posts_per_page' => -1, | |
| 983 | + | |
| 984 | + // Performance | |
| 985 | + 'nopaging' => true, | |
| 986 | + 'suppress_filters' => true, | |
| 987 | + 'update_post_term_cache' => false, | |
| 988 | + 'update_post_meta_cache' => false, | |
| 989 | + 'ignore_sticky_posts' => true, | |
| 990 | + 'no_found_rows' => true | |
| 991 | + ) ); | |
| 992 | + | |
| 993 | + // Query for hidden forums | |
| 994 | + $hidden_forums = new WP_Query( array( | |
| 995 | + 'fields' => 'ids', | |
| 996 | + 'suppress_filters' => true, | |
| 997 | + 'post_type' => bbp_get_forum_post_type(), | |
| 998 | + 'post_status' => bbp_get_hidden_status_id(), | |
| 999 | + 'posts_per_page' => -1, | |
| 1000 | + | |
| 1001 | + // Performance | |
| 1002 | + 'nopaging' => true, | |
| 1003 | + 'suppress_filters' => true, | |
| 1004 | + 'update_post_term_cache' => false, | |
| 1005 | + 'update_post_meta_cache' => false, | |
| 1006 | + 'ignore_sticky_posts' => true, | |
| 1007 | + 'no_found_rows' => true | |
| 1008 | + ) ); | |
| 1009 | + | |
| 1010 | + // Enable forum visibilty normalization | |
| 1011 | + add_action( 'pre_get_posts', 'bbp_pre_get_posts_normalize_forum_visibility', 4 ); | |
| 1012 | + | |
| 1013 | + // Reset the $post global | |
| 1014 | + wp_reset_postdata(); | |
| 1015 | + | |
| 1016 | + // Private | |
| 1017 | + if ( ! is_wp_error( $private_forums ) ) { | |
| 1018 | + update_option( '_bbp_private_forums', $private_forums->posts ); | |
| 1019 | + } | |
| 1020 | + | |
| 1021 | + // Hidden forums | |
| 1022 | + if ( ! is_wp_error( $hidden_forums ) ) { | |
| 1023 | + update_option( '_bbp_hidden_forums', $hidden_forums->posts ); | |
| 1024 | + } | |
| 1025 | + | |
| 1026 | + // Complete results | |
| 1027 | + return true; | |
| 1028 | +} | |
| 1029 | + | |
| 1030 | +/** Subscriptions *************************************************************/ | |
| 1031 | + | |
| 1032 | +/** | |
| 1033 | + * Remove a deleted forum from all user subscriptions | |
| 1034 | + * | |
| 1035 | + * @since 2.5.0 bbPress (r5156) | |
| 1036 | + * | |
| 1037 | + * @param int $forum_id Get the forum ID to remove | |
| 1038 | + */ | |
| 1039 | +function bbp_remove_forum_from_all_subscriptions( $forum_id = 0 ) { | |
| 1040 | + | |
| 1041 | + // Subscriptions are not active | |
| 1042 | + if ( ! bbp_is_subscriptions_active() ) { | |
| 1043 | + return; | |
| 1044 | + } | |
| 1045 | + | |
| 1046 | + // Bail if no forum | |
| 1047 | + $forum_id = bbp_get_forum_id( $forum_id ); | |
| 1048 | + if ( empty( $forum_id ) ) { | |
| 1049 | + return; | |
| 1050 | + } | |
| 1051 | + | |
| 1052 | + // Remove forum from all subscriptions | |
| 1053 | + return bbp_remove_object_from_all_users( $forum_id, '_bbp_subscription', 'post' ); | |
| 1054 | +} | |
| 1055 | + | |
| 937 | 1056 | /** Count Bumpers *************************************************************/ |
| 938 | 1057 | |
| 939 | 1058 | /** |
| 940 | 1059 | * Bump the total topic count of a forum |
| 941 | 1060 | * |
| 942 | - * @since bbPress (r3825) | |
| 1061 | + * @since 2.1.0 bbPress (r3825) | |
| 943 | 1062 | * |
| 944 | 1063 | * @param int $forum_id Optional. Forum id. |
| 945 | 1064 | * @param int $difference Optional. Default 1 |
| 946 | 1065 | * @param bool $update_ancestors Optional. Default true |
| 947 | - * @uses bbp_get_forum_id() To get the forum id | |
| 948 | - * @uses update_post_meta() To update the forum's topic count meta | |
| 949 | - * @uses apply_filters() Calls 'bbp_bump_forum_topic_count' with the topic | |
| 950 | - * count, forum id, and difference | |
| 1066 | + * | |
| 951 | 1067 | * @return int Forum topic count |
| 952 | 1068 | */ |
| 953 | 1069 | function bbp_bump_forum_topic_count( $forum_id = 0, $difference = 1, $update_ancestors = true ) { |
| 954 | 1070 | |
| 1071 | + // Bail if no bump | |
| 1072 | + if ( empty( $difference ) ) { | |
| 1073 | + return false; | |
| 1074 | + } | |
| 1075 | + | |
| 955 | 1076 | // Get some counts |
| 956 | 1077 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 957 | - $topic_count = bbp_get_forum_topic_count( $forum_id, false, false ); | |
| 958 | - $total_topic_count = bbp_get_forum_topic_count( $forum_id, true, false ); | |
| 1078 | + $topic_count = bbp_get_forum_topic_count( $forum_id, false, true ); | |
| 1079 | + $total_topic_count = bbp_get_forum_topic_count( $forum_id, true, true ); | |
| 1080 | + $difference = (int) $difference; | |
| 959 | 1081 | |
| 960 | 1082 | // Update this forum id |
| 961 | - update_post_meta( $forum_id, '_bbp_topic_count', (int) $topic_count + (int) $difference ); | |
| 962 | - update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topic_count + (int) $difference ); | |
| 1083 | + update_post_meta( $forum_id, '_bbp_topic_count', (int) ( $topic_count + $difference ) ); | |
| 1084 | + update_post_meta( $forum_id, '_bbp_total_topic_count', (int) ( $total_topic_count + $difference ) ); | |
| 963 | 1085 | |
| 964 | 1086 | // Check for ancestors |
| 965 | 1087 | if ( true === $update_ancestors ) { |
| 966 | 1088 | |
| @@ -968,75 +1090,244 @@ | ||
| 968 | 1090 | $forum = get_post( $forum_id ); |
| 969 | 1091 | $ancestors = get_post_ancestors( $forum ); |
| 970 | 1092 | |
| 971 | 1093 | // If has ancestors, loop through them... |
| 972 | - if ( !empty( $ancestors ) ) { | |
| 1094 | + if ( ! empty( $ancestors ) ) { | |
| 973 | 1095 | foreach ( (array) $ancestors as $parent_forum_id ) { |
| 974 | 1096 | |
| 975 | - // Get forum counts | |
| 976 | - $parent_topic_count = bbp_get_forum_topic_count( $parent_forum_id, false, false ); | |
| 977 | - $parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true, false ); | |
| 1097 | + // Only update topic count when an ancestor is not a category. | |
| 1098 | + if ( ! bbp_is_forum_category( $parent_forum_id ) ) { | |
| 978 | 1099 | |
| 979 | - // Update counts | |
| 980 | - update_post_meta( $parent_forum_id, '_bbp_topic_count', (int) $parent_topic_count + (int) $difference ); | |
| 981 | - update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) $parent_total_topic_count + (int) $difference ); | |
| 1100 | + $parent_topic_count = bbp_get_forum_topic_count( $parent_forum_id, false, true ); | |
| 1101 | + update_post_meta( $parent_forum_id, '_bbp_topic_count', (int) ( $parent_topic_count + $difference ) ); | |
| 1102 | + } | |
| 1103 | + | |
| 1104 | + // Update the total topic count. | |
| 1105 | + $parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true, true ); | |
| 1106 | + update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) ( $parent_total_topic_count + $difference ) ); | |
| 982 | 1107 | } |
| 983 | 1108 | } |
| 984 | 1109 | } |
| 985 | 1110 | |
| 986 | - return (int) apply_filters( 'bbp_bump_forum_topic_count', (int) $total_topic_count + (int) $difference, $forum_id, (int) $difference, (bool) $update_ancestors ); | |
| 1111 | + $forum_topic_count = (int) ( $total_topic_count + $difference ); | |
| 1112 | + | |
| 1113 | + // Filter & return | |
| 1114 | + return (int) apply_filters( 'bbp_bump_forum_topic_count', $forum_topic_count, $forum_id, $difference, $update_ancestors ); | |
| 987 | 1115 | } |
| 988 | 1116 | |
| 989 | 1117 | /** |
| 990 | - * Bump the total hidden topic count of a forum | |
| 1118 | + * Increase the total topic count of a forum by one. | |
| 991 | 1119 | * |
| 992 | - * @since bbPress (r3825) | |
| 1120 | + * @since 2.6.0 bbPress (r6036) | |
| 993 | 1121 | * |
| 1122 | + * @param int $forum_id The forum id. | |
| 1123 | + * @return void | |
| 1124 | + */ | |
| 1125 | +function bbp_increase_forum_topic_count( $forum_id = 0 ) { | |
| 1126 | + | |
| 1127 | + // Bail early if no id is passed. | |
| 1128 | + if ( empty( $forum_id ) ) { | |
| 1129 | + return; | |
| 1130 | + } | |
| 1131 | + | |
| 1132 | + // If it's a topic, get the forum id. | |
| 1133 | + if ( bbp_is_topic( $forum_id ) ) { | |
| 1134 | + $topic_id = $forum_id; | |
| 1135 | + $forum_id = bbp_get_topic_forum_id( $topic_id ); | |
| 1136 | + | |
| 1137 | + // Update inverse based on item status | |
| 1138 | + if ( ! bbp_is_topic_public( $topic_id ) ) { | |
| 1139 | + bbp_increase_forum_topic_count_hidden( $forum_id ); | |
| 1140 | + return; | |
| 1141 | + } | |
| 1142 | + } | |
| 1143 | + | |
| 1144 | + // Bump up | |
| 1145 | + bbp_bump_forum_topic_count( $forum_id ); | |
| 1146 | +} | |
| 1147 | + | |
| 1148 | +/** | |
| 1149 | + * Decrease the total topic count of a forum by one. | |
| 1150 | + * | |
| 1151 | + * @since 2.6.0 bbPress (r6036) | |
| 1152 | + * | |
| 1153 | + * @param int $forum_id The forum id. | |
| 1154 | + * | |
| 1155 | + * @return void | |
| 1156 | + */ | |
| 1157 | +function bbp_decrease_forum_topic_count( $forum_id = 0 ) { | |
| 1158 | + | |
| 1159 | + // Bail early if no id is passed. | |
| 1160 | + if ( empty( $forum_id ) ) { | |
| 1161 | + return; | |
| 1162 | + } | |
| 1163 | + | |
| 1164 | + // If it's a topic, get the forum id. | |
| 1165 | + if ( bbp_is_topic( $forum_id ) ) { | |
| 1166 | + $topic_id = $forum_id; | |
| 1167 | + $forum_id = bbp_get_topic_forum_id( $topic_id ); | |
| 1168 | + | |
| 1169 | + // Update inverse based on item status | |
| 1170 | + if ( ! bbp_is_topic_public( $topic_id ) ) { | |
| 1171 | + bbp_decrease_forum_topic_count_hidden( $forum_id ); | |
| 1172 | + return; | |
| 1173 | + } | |
| 1174 | + } | |
| 1175 | + | |
| 1176 | + // Bump down | |
| 1177 | + bbp_bump_forum_topic_count( $forum_id, -1 ); | |
| 1178 | +} | |
| 1179 | + | |
| 1180 | +/** | |
| 1181 | + * Bump the total topic count of a forum | |
| 1182 | + * | |
| 1183 | + * @since 2.1.0 bbPress (r3825) | |
| 1184 | + * | |
| 994 | 1185 | * @param int $forum_id Optional. Forum id. |
| 995 | 1186 | * @param int $difference Optional. Default 1 |
| 996 | - * @uses bbp_get_forum_id() To get the forum id | |
| 997 | - * @uses update_post_meta() To update the forum's topic count meta | |
| 998 | - * @uses apply_filters() Calls 'bbp_bump_forum_topic_count_hidden' with the | |
| 999 | - * topic count, forum id, and difference | |
| 1000 | - * @return int Forum hidden topic count | |
| 1187 | + * @param bool $update_ancestors Optional. Default true | |
| 1188 | + * | |
| 1189 | + * @return int Forum topic count | |
| 1001 | 1190 | */ |
| 1002 | -function bbp_bump_forum_topic_count_hidden( $forum_id = 0, $difference = 1 ) { | |
| 1191 | +function bbp_bump_forum_topic_count_hidden( $forum_id = 0, $difference = 1, $update_ancestors = true ) { | |
| 1003 | 1192 | |
| 1193 | + // Bail if no bump | |
| 1194 | + if ( empty( $difference ) ) { | |
| 1195 | + return false; | |
| 1196 | + } | |
| 1197 | + | |
| 1004 | 1198 | // Get some counts |
| 1005 | - $forum_id = bbp_get_forum_id( $forum_id ); | |
| 1006 | - $topic_count = bbp_get_forum_topic_count_hidden( $forum_id, false ); | |
| 1007 | - $new_count = (int) $topic_count + (int) $difference; | |
| 1199 | + $forum_id = bbp_get_forum_id( $forum_id ); | |
| 1200 | + $reply_count = bbp_get_forum_topic_count_hidden( $forum_id, false, true ); | |
| 1201 | + $total_topic_count = bbp_get_forum_topic_count_hidden( $forum_id, true, true ); | |
| 1202 | + $difference = (int) $difference; | |
| 1008 | 1203 | |
| 1009 | 1204 | // Update this forum id |
| 1010 | - update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $new_count ); | |
| 1205 | + update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) ( $reply_count + $difference ) ); | |
| 1206 | + update_post_meta( $forum_id, '_bbp_total_topic_count_hidden', (int) ( $total_topic_count + $difference ) ); | |
| 1011 | 1207 | |
| 1012 | - return (int) apply_filters( 'bbp_bump_forum_topic_count_hidden', (int) $new_count, $forum_id, (int) $difference ); | |
| 1208 | + // Check for ancestors | |
| 1209 | + if ( true === $update_ancestors ) { | |
| 1210 | + | |
| 1211 | + // Get post ancestors | |
| 1212 | + $forum = get_post( $forum_id ); | |
| 1213 | + $ancestors = get_post_ancestors( $forum ); | |
| 1214 | + | |
| 1215 | + // If has ancestors, loop through them... | |
| 1216 | + if ( ! empty( $ancestors ) ) { | |
| 1217 | + foreach ( (array) $ancestors as $parent_forum_id ) { | |
| 1218 | + | |
| 1219 | + // Only update topic count when an ancestor is not a category. | |
| 1220 | + if ( ! bbp_is_forum_category( $parent_forum_id ) ) { | |
| 1221 | + | |
| 1222 | + $parent_topic_count = bbp_get_forum_topic_count_hidden( $parent_forum_id, false, true ); | |
| 1223 | + update_post_meta( $parent_forum_id, '_bbp_topic_count_hidden', (int) ( $parent_topic_count + $difference ) ); | |
| 1224 | + } | |
| 1225 | + | |
| 1226 | + // Update the total topic count. | |
| 1227 | + $parent_total_topic_count = bbp_get_forum_topic_count_hidden( $parent_forum_id, true, true ); | |
| 1228 | + update_post_meta( $parent_forum_id, '_bbp_total_topic_count_hidden', (int) ( $parent_total_topic_count + $difference ) ); | |
| 1229 | + } | |
| 1230 | + } | |
| 1231 | + } | |
| 1232 | + | |
| 1233 | + $forum_topic_count = (int) ( $total_topic_count + $difference ); | |
| 1234 | + | |
| 1235 | + // Filter & return | |
| 1236 | + return (int) apply_filters( 'bbp_bump_forum_topic_count_hidden', $forum_topic_count, $forum_id, $difference, $update_ancestors ); | |
| 1013 | 1237 | } |
| 1014 | 1238 | |
| 1015 | 1239 | /** |
| 1240 | + * Increase the total hidden topic count of a forum by one. | |
| 1241 | + * | |
| 1242 | + * @since 2.6.0 bbPress (r6036) | |
| 1243 | + * | |
| 1244 | + * @param int $forum_id The forum id. | |
| 1245 | + * | |
| 1246 | + * @return void | |
| 1247 | + */ | |
| 1248 | +function bbp_increase_forum_topic_count_hidden( $forum_id = 0 ) { | |
| 1249 | + | |
| 1250 | + // Bail early if no id is passed. | |
| 1251 | + if ( empty( $forum_id ) ) { | |
| 1252 | + return; | |
| 1253 | + } | |
| 1254 | + | |
| 1255 | + // If it's a topic, get the forum id. | |
| 1256 | + if ( bbp_is_topic( $forum_id ) ) { | |
| 1257 | + $topic_id = $forum_id; | |
| 1258 | + $forum_id = bbp_get_topic_forum_id( $topic_id ); | |
| 1259 | + | |
| 1260 | + // Update inverse based on item status | |
| 1261 | + if ( bbp_is_topic_public( $topic_id ) ) { | |
| 1262 | + bbp_increase_forum_topic_count( $forum_id ); | |
| 1263 | + return; | |
| 1264 | + } | |
| 1265 | + } | |
| 1266 | + | |
| 1267 | + // Bump up | |
| 1268 | + bbp_bump_forum_topic_count_hidden( $forum_id ); | |
| 1269 | +} | |
| 1270 | + | |
| 1271 | +/** | |
| 1272 | + * Decrease the total hidden topic count of a forum by one. | |
| 1273 | + * | |
| 1274 | + * @since 2.6.0 bbPress (r6036) | |
| 1275 | + * | |
| 1276 | + * @param int $forum_id The forum id. | |
| 1277 | + * | |
| 1278 | + * @return void | |
| 1279 | + */ | |
| 1280 | +function bbp_decrease_forum_topic_count_hidden( $forum_id = 0 ) { | |
| 1281 | + | |
| 1282 | + // Bail early if no id is passed. | |
| 1283 | + if ( empty( $forum_id ) ) { | |
| 1284 | + return; | |
| 1285 | + } | |
| 1286 | + | |
| 1287 | + // If it's a topic, get the forum id. | |
| 1288 | + if ( bbp_is_topic( $forum_id ) ) { | |
| 1289 | + $topic_id = $forum_id; | |
| 1290 | + $forum_id = bbp_get_topic_forum_id( $topic_id ); | |
| 1291 | + | |
| 1292 | + // Update inverse based on item status | |
| 1293 | + if ( bbp_is_topic_public( $topic_id ) ) { | |
| 1294 | + bbp_decrease_forum_topic_count( $forum_id ); | |
| 1295 | + return; | |
| 1296 | + } | |
| 1297 | + } | |
| 1298 | + | |
| 1299 | + // Bump down | |
| 1300 | + bbp_bump_forum_topic_count_hidden( $forum_id, -1 ); | |
| 1301 | +} | |
| 1302 | + | |
| 1303 | +/** | |
| 1016 | 1304 | * Bump the total topic count of a forum |
| 1017 | 1305 | * |
| 1018 | - * @since bbPress (r3825) | |
| 1306 | + * @since 2.1.0 bbPress (r3825) | |
| 1019 | 1307 | * |
| 1020 | 1308 | * @param int $forum_id Optional. Forum id. |
| 1021 | 1309 | * @param int $difference Optional. Default 1 |
| 1022 | 1310 | * @param bool $update_ancestors Optional. Default true |
| 1023 | - * @uses bbp_get_forum_id() To get the forum id | |
| 1024 | - * @uses update_post_meta() To update the forum's topic count meta | |
| 1025 | - * @uses apply_filters() Calls 'bbp_bump_forum_reply_count' with the topic | |
| 1026 | - * count, forum id, and difference | |
| 1311 | + * | |
| 1027 | 1312 | * @return int Forum topic count |
| 1028 | 1313 | */ |
| 1029 | 1314 | function bbp_bump_forum_reply_count( $forum_id = 0, $difference = 1, $update_ancestors = true ) { |
| 1030 | 1315 | |
| 1316 | + // Bail if no bump | |
| 1317 | + if ( empty( $difference ) ) { | |
| 1318 | + return false; | |
| 1319 | + } | |
| 1320 | + | |
| 1031 | 1321 | // Get some counts |
| 1032 | 1322 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 1033 | - $topic_count = bbp_get_forum_reply_count( $forum_id, false ); | |
| 1034 | - $total_reply_count = bbp_get_forum_reply_count( $forum_id, true ); | |
| 1323 | + $reply_count = bbp_get_forum_reply_count( $forum_id, false, true ); | |
| 1324 | + $total_reply_count = bbp_get_forum_reply_count( $forum_id, true, true ); | |
| 1325 | + $difference = (int) $difference; | |
| 1035 | 1326 | |
| 1036 | 1327 | // Update this forum id |
| 1037 | - update_post_meta( $forum_id, '_bbp_reply_count', (int) $topic_count + (int) $difference ); | |
| 1038 | - update_post_meta( $forum_id, '_bbp_total_reply_count', (int) $total_reply_count + (int) $difference ); | |
| 1328 | + update_post_meta( $forum_id, '_bbp_reply_count', (int) ( $reply_count + $difference ) ); | |
| 1329 | + update_post_meta( $forum_id, '_bbp_total_reply_count', (int) ( $total_reply_count + $difference ) ); | |
| 1039 | 1330 | |
| 1040 | 1331 | // Check for ancestors |
| 1041 | 1332 | if ( true === $update_ancestors ) { |
| 1042 | 1333 | |
| @@ -1044,43 +1335,256 @@ | ||
| 1044 | 1335 | $forum = get_post( $forum_id ); |
| 1045 | 1336 | $ancestors = get_post_ancestors( $forum ); |
| 1046 | 1337 | |
| 1047 | 1338 | // If has ancestors, loop through them... |
| 1048 | - if ( !empty( $ancestors ) ) { | |
| 1339 | + if ( ! empty( $ancestors ) ) { | |
| 1049 | 1340 | foreach ( (array) $ancestors as $parent_forum_id ) { |
| 1050 | 1341 | |
| 1051 | - // Get forum counts | |
| 1052 | - $parent_topic_count = bbp_get_forum_reply_count( $parent_forum_id, false ); | |
| 1053 | - $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true ); | |
| 1342 | + // Only update reply count when an ancestor is not a category. | |
| 1343 | + if ( ! bbp_is_forum_category( $parent_forum_id ) ) { | |
| 1054 | 1344 | |
| 1055 | - // Update counts | |
| 1056 | - update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) $parent_topic_count + (int) $difference ); | |
| 1057 | - update_post_meta( $parent_forum_id, '_bbp_total_reply_count', (int) $parent_total_reply_count + (int) $difference ); | |
| 1345 | + $parent_reply_count = bbp_get_forum_reply_count( $parent_forum_id, false, true ); | |
| 1346 | + update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) ( $parent_reply_count + $difference ) ); | |
| 1347 | + } | |
| 1348 | + | |
| 1349 | + // Update the total reply count. | |
| 1350 | + $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true, true ); | |
| 1351 | + update_post_meta( $parent_forum_id, '_bbp_total_reply_count', (int) ( $parent_total_reply_count + $difference ) ); | |
| 1058 | 1352 | } |
| 1059 | 1353 | } |
| 1060 | 1354 | } |
| 1061 | 1355 | |
| 1062 | - return (int) apply_filters( 'bbp_bump_forum_reply_count', (int) $total_reply_count + (int) $difference, $forum_id, (int) $difference, (bool) $update_ancestors ); | |
| 1356 | + $forum_reply_count = (int) ( $total_reply_count + $difference ); | |
| 1357 | + | |
| 1358 | + // Filter & return | |
| 1359 | + return (int) apply_filters( 'bbp_bump_forum_reply_count', $forum_reply_count, $forum_id, $difference, $update_ancestors ); | |
| 1063 | 1360 | } |
| 1064 | 1361 | |
| 1362 | +/** | |
| 1363 | + * Bump the total topic count of a forum | |
| 1364 | + * | |
| 1365 | + * @since 2.6.0 bbPress (r6922) | |
| 1366 | + * | |
| 1367 | + * @param int $forum_id Optional. Forum id. | |
| 1368 | + * @param int $difference Optional. Default 1 | |
| 1369 | + * @param bool $update_ancestors Optional. Default true | |
| 1370 | + * | |
| 1371 | + * @return int Forum topic count | |
| 1372 | + */ | |
| 1373 | +function bbp_bump_forum_reply_count_hidden( $forum_id = 0, $difference = 1, $update_ancestors = true ) { | |
| 1374 | + | |
| 1375 | + // Bail if no bump | |
| 1376 | + if ( empty( $difference ) ) { | |
| 1377 | + return false; | |
| 1378 | + } | |
| 1379 | + | |
| 1380 | + // Get some counts | |
| 1381 | + $forum_id = bbp_get_forum_id( $forum_id ); | |
| 1382 | + $reply_count = bbp_get_forum_reply_count_hidden( $forum_id, false, true ); | |
| 1383 | + $total_reply_count = bbp_get_forum_reply_count_hidden( $forum_id, true, true ); | |
| 1384 | + $difference = (int) $difference; | |
| 1385 | + | |
| 1386 | + // Update this forum id | |
| 1387 | + update_post_meta( $forum_id, '_bbp_reply_count_hidden', (int) ( $reply_count + $difference ) ); | |
| 1388 | + update_post_meta( $forum_id, '_bbp_total_reply_count_hidden', (int) ( $total_reply_count + $difference ) ); | |
| 1389 | + | |
| 1390 | + // Check for ancestors | |
| 1391 | + if ( true === $update_ancestors ) { | |
| 1392 | + | |
| 1393 | + // Get post ancestors | |
| 1394 | + $forum = get_post( $forum_id ); | |
| 1395 | + $ancestors = get_post_ancestors( $forum ); | |
| 1396 | + | |
| 1397 | + // If has ancestors, loop through them... | |
| 1398 | + if ( ! empty( $ancestors ) ) { | |
| 1399 | + foreach ( (array) $ancestors as $parent_forum_id ) { | |
| 1400 | + | |
| 1401 | + // Only update reply count when an ancestor is not a category. | |
| 1402 | + if ( ! bbp_is_forum_category( $parent_forum_id ) ) { | |
| 1403 | + | |
| 1404 | + $parent_reply_count = bbp_get_forum_reply_count_hidden( $parent_forum_id, false, true ); | |
| 1405 | + update_post_meta( $parent_forum_id, '_bbp_reply_count_hidden', (int) ( $parent_reply_count + $difference ) ); | |
| 1406 | + } | |
| 1407 | + | |
| 1408 | + // Update the total reply count. | |
| 1409 | + $parent_total_reply_count = bbp_get_forum_reply_count_hidden( $parent_forum_id, true, true ); | |
| 1410 | + update_post_meta( $parent_forum_id, '_bbp_total_reply_count_hidden', (int) ( $parent_total_reply_count + $difference ) ); | |
| 1411 | + } | |
| 1412 | + } | |
| 1413 | + } | |
| 1414 | + | |
| 1415 | + $forum_reply_count = (int) ( $total_reply_count + $difference ); | |
| 1416 | + | |
| 1417 | + // Filter & return | |
| 1418 | + return (int) apply_filters( 'bbp_bump_forum_reply_count_hidden', $forum_reply_count, $forum_id, $difference, $update_ancestors ); | |
| 1419 | +} | |
| 1420 | + | |
| 1421 | +/** | |
| 1422 | + * Increase the total reply count of a forum by one. | |
| 1423 | + * | |
| 1424 | + * @since 2.6.0 bbPress (r6036) | |
| 1425 | + * | |
| 1426 | + * @param int $forum_id The forum id. | |
| 1427 | + * | |
| 1428 | + * @return void | |
| 1429 | + */ | |
| 1430 | +function bbp_increase_forum_reply_count( $forum_id = 0 ) { | |
| 1431 | + | |
| 1432 | + // Bail early if no id is passed. | |
| 1433 | + if ( empty( $forum_id ) ) { | |
| 1434 | + return; | |
| 1435 | + } | |
| 1436 | + | |
| 1437 | + // If it's a reply, get the forum id. | |
| 1438 | + if ( bbp_is_reply( $forum_id ) ) { | |
| 1439 | + $reply_id = $forum_id; | |
| 1440 | + $forum_id = bbp_get_reply_forum_id( $reply_id ); | |
| 1441 | + | |
| 1442 | + // Update inverse based on item status | |
| 1443 | + if ( ! bbp_is_reply_public( $reply_id ) ) { | |
| 1444 | + bbp_increase_forum_reply_count_hidden( $forum_id ); | |
| 1445 | + return; | |
| 1446 | + } | |
| 1447 | + } | |
| 1448 | + | |
| 1449 | + // Bump up | |
| 1450 | + bbp_bump_forum_reply_count( $forum_id ); | |
| 1451 | +} | |
| 1452 | + | |
| 1453 | +/** | |
| 1454 | + * Decrease the total reply count of a forum by one. | |
| 1455 | + * | |
| 1456 | + * @since 2.6.0 bbPress (r6036) | |
| 1457 | + * | |
| 1458 | + * @param int $forum_id The forum id. | |
| 1459 | + * | |
| 1460 | + * @return void | |
| 1461 | + */ | |
| 1462 | +function bbp_decrease_forum_reply_count( $forum_id = 0 ) { | |
| 1463 | + | |
| 1464 | + // Bail early if no id is passed. | |
| 1465 | + if ( empty( $forum_id ) ) { | |
| 1466 | + return; | |
| 1467 | + } | |
| 1468 | + | |
| 1469 | + // If it's a reply, get the forum id. | |
| 1470 | + if ( bbp_is_reply( $forum_id ) ) { | |
| 1471 | + $reply_id = $forum_id; | |
| 1472 | + $forum_id = bbp_get_reply_forum_id( $reply_id ); | |
| 1473 | + | |
| 1474 | + // Update inverse based on item status | |
| 1475 | + if ( ! bbp_is_reply_public( $reply_id ) ) { | |
| 1476 | + bbp_decrease_forum_reply_count_hidden( $forum_id ); | |
| 1477 | + return; | |
| 1478 | + } | |
| 1479 | + } | |
| 1480 | + | |
| 1481 | + // Bump down | |
| 1482 | + bbp_bump_forum_reply_count( $forum_id, -1 ); | |
| 1483 | +} | |
| 1484 | + | |
| 1485 | +/** | |
| 1486 | + * Increase the total hidden reply count of a forum by one. | |
| 1487 | + * | |
| 1488 | + * @since 2.6.0 bbPress (r6036) | |
| 1489 | + * | |
| 1490 | + * @param int $forum_id The forum id. | |
| 1491 | + * | |
| 1492 | + * @return void | |
| 1493 | + */ | |
| 1494 | +function bbp_increase_forum_reply_count_hidden( $forum_id = 0 ) { | |
| 1495 | + | |
| 1496 | + // Bail early if no id is passed. | |
| 1497 | + if ( empty( $forum_id ) ) { | |
| 1498 | + return; | |
| 1499 | + } | |
| 1500 | + | |
| 1501 | + // If it's a reply, get the forum id. | |
| 1502 | + if ( bbp_is_reply( $forum_id ) ) { | |
| 1503 | + $reply_id = $forum_id; | |
| 1504 | + $forum_id = bbp_get_reply_forum_id( $reply_id ); | |
| 1505 | + | |
| 1506 | + // Update inverse based on item status | |
| 1507 | + if ( bbp_is_reply_public( $reply_id ) ) { | |
| 1508 | + bbp_increase_forum_reply_count( $forum_id ); | |
| 1509 | + return; | |
| 1510 | + } | |
| 1511 | + } | |
| 1512 | + | |
| 1513 | + // Bump up | |
| 1514 | + bbp_bump_forum_reply_count_hidden( $forum_id ); | |
| 1515 | +} | |
| 1516 | + | |
| 1517 | +/** | |
| 1518 | + * Decrease the total hidden reply count of a forum by one. | |
| 1519 | + * | |
| 1520 | + * @since 2.6.0 bbPress (r6036) | |
| 1521 | + * | |
| 1522 | + * @param int $forum_id The forum id. | |
| 1523 | + * | |
| 1524 | + * @return void | |
| 1525 | + */ | |
| 1526 | +function bbp_decrease_forum_reply_count_hidden( $forum_id = 0 ) { | |
| 1527 | + | |
| 1528 | + // Bail early if no id is passed. | |
| 1529 | + if ( empty( $forum_id ) ) { | |
| 1530 | + return; | |
| 1531 | + } | |
| 1532 | + | |
| 1533 | + // If it's a reply, get the forum id. | |
| 1534 | + if ( bbp_is_reply( $forum_id ) ) { | |
| 1535 | + $reply_id = $forum_id; | |
| 1536 | + $forum_id = bbp_get_reply_forum_id( $reply_id ); | |
| 1537 | + | |
| 1538 | + // Update inverse based on item status | |
| 1539 | + if ( bbp_is_reply_public( $reply_id ) ) { | |
| 1540 | + bbp_decrease_forum_reply_count( $forum_id ); | |
| 1541 | + return; | |
| 1542 | + } | |
| 1543 | + } | |
| 1544 | + | |
| 1545 | + // Bump down | |
| 1546 | + bbp_bump_forum_reply_count_hidden( $forum_id, -1 ); | |
| 1547 | +} | |
| 1548 | + | |
| 1549 | +/** | |
| 1550 | + * Update forum reply counts when a topic is approved or unapproved. | |
| 1551 | + * | |
| 1552 | + * @since 2.6.0 bbPress (r6036) | |
| 1553 | + * | |
| 1554 | + * @param int $topic_id The topic id. | |
| 1555 | + * | |
| 1556 | + * @return void | |
| 1557 | + */ | |
| 1558 | +function bbp_approved_unapproved_topic_update_forum_reply_count( $topic_id = 0 ) { | |
| 1559 | + | |
| 1560 | + // Bail early if we don't have a topic id. | |
| 1561 | + if ( empty( $topic_id ) ) { | |
| 1562 | + return; | |
| 1563 | + } | |
| 1564 | + | |
| 1565 | + // Get the topic's replies. | |
| 1566 | + $count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() ); | |
| 1567 | + | |
| 1568 | + // If we're unapproving, set count to negative. | |
| 1569 | + if ( 'bbp_unapproved_topic' === current_filter() ) { | |
| 1570 | + $count = -$count; | |
| 1571 | + } | |
| 1572 | + | |
| 1573 | + // Bump up or down | |
| 1574 | + bbp_bump_forum_reply_count( bbp_get_topic_forum_id( $topic_id ), $count ); | |
| 1575 | +} | |
| 1576 | + | |
| 1065 | 1577 | /** Forum Updaters ************************************************************/ |
| 1066 | 1578 | |
| 1067 | 1579 | /** |
| 1068 | 1580 | * Update the forum last topic id |
| 1069 | 1581 | * |
| 1070 | - * @since bbPress (r2625) | |
| 1582 | + * @since 2.0.0 bbPress (r2625) | |
| 1071 | 1583 | * |
| 1072 | - * @param int $forum_id Optional. Forum id | |
| 1073 | - * @param int $topic_id Optional. Topic id | |
| 1074 | - * @uses bbp_get_forum_id() To get the forum id | |
| 1075 | - * @uses bbp_forum_query_subforum_ids() To get the subforum ids | |
| 1076 | - * @uses bbp_update_forum_last_topic_id() To update the last topic id of child | |
| 1077 | - * forums | |
| 1078 | - * @uses get_posts() To get the most recent topic in the forum | |
| 1079 | - * @uses update_post_meta() To update the forum's last active id meta | |
| 1080 | - * @uses apply_filters() Calls 'bbp_update_forum_last_topic_id' with the last | |
| 1081 | - * reply id and forum id | |
| 1082 | - * @return bool True on success, false on failure | |
| 1584 | + * @param int $forum_id Optional. Forum id. | |
| 1585 | + * @param int $topic_id Optional. Topic id. | |
| 1586 | + * @return int Id of the forums most recent topic | |
| 1083 | 1587 | */ |
| 1084 | 1588 | function bbp_update_forum_last_topic_id( $forum_id = 0, $topic_id = 0 ) { |
| 1085 | 1589 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 1086 | 1590 | |
| @@ -1091,10 +1595,10 @@ | ||
| 1091 | 1595 | if ( empty( $topic_id ) ) { |
| 1092 | 1596 | |
| 1093 | 1597 | // Loop through children and add together forum reply counts |
| 1094 | 1598 | $children = bbp_forum_query_subforum_ids( $forum_id ); |
| 1095 | - if ( !empty( $children ) ) { | |
| 1096 | - foreach ( (array) $children as $child ) { | |
| 1599 | + if ( ! empty( $children ) ) { | |
| 1600 | + foreach ( $children as $child ) { | |
| 1097 | 1601 | $children_last_topic = bbp_update_forum_last_topic_id( $child ); // Recursive |
| 1098 | 1602 | } |
| 1099 | 1603 | } |
| 1100 | 1604 | |
| @@ -1102,8 +1606,9 @@ | ||
| 1102 | 1606 | $post_vars = array( |
| 1103 | 1607 | 'post_parent' => $forum_id, |
| 1104 | 1608 | 'post_type' => bbp_get_topic_post_type(), |
| 1105 | 1609 | 'meta_key' => '_bbp_last_active_time', |
| 1610 | + 'meta_type' => 'DATETIME', | |
| 1106 | 1611 | 'orderby' => 'meta_value', |
| 1107 | 1612 | 'numberposts' => 1 |
| 1108 | 1613 | ); |
| 1109 | 1614 | |
| @@ -1108,9 +1613,9 @@ | ||
| 1108 | 1613 | ); |
| 1109 | 1614 | |
| 1110 | 1615 | // Get the most recent topic in this forum_id |
| 1111 | 1616 | $recent_topic = get_posts( $post_vars ); |
| 1112 | - if ( !empty( $recent_topic ) ) { | |
| 1617 | + if ( ! empty( $recent_topic ) ) { | |
| 1113 | 1618 | $topic_id = $recent_topic[0]->ID; |
| 1114 | 1619 | } |
| 1115 | 1620 | } |
| 1116 | 1621 | |
| @@ -1118,15 +1623,16 @@ | ||
| 1118 | 1623 | $topic_id = (int) $topic_id; |
| 1119 | 1624 | $children_last_topic = (int) $children_last_topic; |
| 1120 | 1625 | |
| 1121 | 1626 | // If child forums have higher id, use that instead |
| 1122 | - if ( !empty( $children ) && ( $children_last_topic > $topic_id ) ) | |
| 1627 | + if ( ! empty( $children ) && ( $children_last_topic > $topic_id ) ) { | |
| 1123 | 1628 | $topic_id = $children_last_topic; |
| 1629 | + } | |
| 1124 | 1630 | |
| 1125 | 1631 | // Update the last public topic ID |
| 1126 | - if ( bbp_is_topic_published( $topic_id ) ) | |
| 1127 | - update_post_meta( $forum_id, '_bbp_last_topic_id', $topic_id ); | |
| 1632 | + update_post_meta( $forum_id, '_bbp_last_topic_id', $topic_id ); | |
| 1128 | 1633 | |
| 1634 | + // Filter & return | |
| 1129 | 1635 | return (int) apply_filters( 'bbp_update_forum_last_topic_id', $topic_id, $forum_id ); |
| 1130 | 1636 | } |
| 1131 | 1637 | |
| 1132 | 1638 | /** |
| @@ -1131,23 +1637,13 @@ | ||
| 1131 | 1637 | |
| 1132 | 1638 | /** |
| 1133 | 1639 | * Update the forum last reply id |
| 1134 | 1640 | * |
| 1135 | - * @since bbPress (r2625) | |
| 1641 | + * @since 2.0.0 bbPress (r2625) | |
| 1136 | 1642 | * |
| 1137 | - * @param int $forum_id Optional. Forum id | |
| 1138 | - * @param int $reply_id Optional. Reply id | |
| 1139 | - * @uses bbp_get_forum_id() To get the forum id | |
| 1140 | - * @uses bbp_forum_query_subforum_ids() To get the subforum ids | |
| 1141 | - * @uses bbp_update_forum_last_reply_id() To update the last reply id of child | |
| 1142 | - * forums | |
| 1143 | - * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum | |
| 1144 | - * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id | |
| 1145 | - * @uses bbp_is_reply_published() To make sure the reply is published | |
| 1146 | - * @uses update_post_meta() To update the forum's last active id meta | |
| 1147 | - * @uses apply_filters() Calls 'bbp_update_forum_last_reply_id' with the last | |
| 1148 | - * reply id and forum id | |
| 1149 | - * @return bool True on success, false on failure | |
| 1643 | + * @param int $forum_id Optional. Forum id. | |
| 1644 | + * @param int $reply_id Optional. Reply id. | |
| 1645 | + * @return int Id of the forums most recent reply | |
| 1150 | 1646 | */ |
| 1151 | 1647 | function bbp_update_forum_last_reply_id( $forum_id = 0, $reply_id = 0 ) { |
| 1152 | 1648 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 1153 | 1649 | |
| @@ -1158,10 +1654,10 @@ | ||
| 1158 | 1654 | if ( empty( $reply_id ) ) { |
| 1159 | 1655 | |
| 1160 | 1656 | // Loop through children and get the most recent reply id |
| 1161 | 1657 | $children = bbp_forum_query_subforum_ids( $forum_id ); |
| 1162 | - if ( !empty( $children ) ) { | |
| 1163 | - foreach ( (array) $children as $child ) { | |
| 1658 | + if ( ! empty( $children ) ) { | |
| 1659 | + foreach ( $children as $child ) { | |
| 1164 | 1660 | $children_last_reply = bbp_update_forum_last_reply_id( $child ); // Recursive |
| 1165 | 1661 | } |
| 1166 | 1662 | } |
| 1167 | 1663 | |
| @@ -1166,15 +1662,17 @@ | ||
| 1166 | 1662 | } |
| 1167 | 1663 | |
| 1168 | 1664 | // If this forum has topics... |
| 1169 | 1665 | $topic_ids = bbp_forum_query_topic_ids( $forum_id ); |
| 1170 | - if ( !empty( $topic_ids ) ) { | |
| 1666 | + if ( ! empty( $topic_ids ) ) { | |
| 1171 | 1667 | |
| 1172 | 1668 | // ...get the most recent reply from those topics... |
| 1173 | 1669 | $reply_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids ); |
| 1174 | 1670 | |
| 1175 | 1671 | // ...and compare it to the most recent topic id... |
| 1176 | - $reply_id = ( $reply_id > max( $topic_ids ) ) ? $reply_id : max( $topic_ids ); | |
| 1672 | + $reply_id = ( $reply_id > max( $topic_ids ) ) | |
| 1673 | + ? $reply_id | |
| 1674 | + : max( $topic_ids ); | |
| 1177 | 1675 | } |
| 1178 | 1676 | } |
| 1179 | 1677 | |
| 1180 | 1678 | // Cast as integer in case of empty or string |
| @@ -1179,17 +1677,18 @@ | ||
| 1179 | 1677 | |
| 1180 | 1678 | // Cast as integer in case of empty or string |
| 1181 | 1679 | $reply_id = (int) $reply_id; |
| 1182 | 1680 | $children_last_reply = (int) $children_last_reply; |
| 1183 | - | |
| 1681 | + | |
| 1184 | 1682 | // If child forums have higher ID, check for newer reply id |
| 1185 | - if ( !empty( $children ) && ( $children_last_reply > $reply_id ) ) | |
| 1683 | + if ( ! empty( $children ) && ( $children_last_reply > $reply_id ) ) { | |
| 1186 | 1684 | $reply_id = $children_last_reply; |
| 1685 | + } | |
| 1187 | 1686 | |
| 1188 | 1687 | // Update the last public reply ID |
| 1189 | - if ( bbp_is_reply_published( $reply_id ) ) | |
| 1190 | - update_post_meta( $forum_id, '_bbp_last_reply_id', $reply_id ); | |
| 1688 | + update_post_meta( $forum_id, '_bbp_last_reply_id', $reply_id ); | |
| 1191 | 1689 | |
| 1690 | + // Filter & return | |
| 1192 | 1691 | return (int) apply_filters( 'bbp_update_forum_last_reply_id', $reply_id, $forum_id ); |
| 1193 | 1692 | } |
| 1194 | 1693 | |
| 1195 | 1694 | /** |
| @@ -1194,23 +1693,13 @@ | ||
| 1194 | 1693 | |
| 1195 | 1694 | /** |
| 1196 | 1695 | * Update the forum last active post id |
| 1197 | 1696 | * |
| 1198 | - * @since bbPress (r2860) | |
| 1697 | + * @since 2.0.0 bbPress (r2860) | |
| 1199 | 1698 | * |
| 1200 | - * @param int $forum_id Optional. Forum id | |
| 1201 | - * @param int $active_id Optional. Active post id | |
| 1202 | - * @uses bbp_get_forum_id() To get the forum id | |
| 1203 | - * @uses bbp_forum_query_subforum_ids() To get the subforum ids | |
| 1204 | - * @uses bbp_update_forum_last_active_id() To update the last active id of | |
| 1205 | - * child forums | |
| 1206 | - * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum | |
| 1207 | - * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id | |
| 1208 | - * @uses get_post_status() To make sure the reply is published | |
| 1209 | - * @uses update_post_meta() To update the forum's last active id meta | |
| 1210 | - * @uses apply_filters() Calls 'bbp_update_forum_last_active_id' with the last | |
| 1211 | - * active post id and forum id | |
| 1212 | - * @return bool True on success, false on failure | |
| 1699 | + * @param int $forum_id Optional. Forum id. | |
| 1700 | + * @param int $active_id Optional. Active post id. | |
| 1701 | + * @return int Id of the forums last active post | |
| 1213 | 1702 | */ |
| 1214 | 1703 | function bbp_update_forum_last_active_id( $forum_id = 0, $active_id = 0 ) { |
| 1215 | 1704 | |
| 1216 | 1705 | $forum_id = bbp_get_forum_id( $forum_id ); |
| @@ -1220,21 +1709,25 @@ | ||
| 1220 | 1709 | |
| 1221 | 1710 | // Do some calculation if not manually set |
| 1222 | 1711 | if ( empty( $active_id ) ) { |
| 1223 | 1712 | |
| 1224 | - // Loop through children and add together forum reply counts | |
| 1713 | + // Loop through children and get the last active ID | |
| 1225 | 1714 | $children = bbp_forum_query_subforum_ids( $forum_id ); |
| 1226 | - if ( !empty( $children ) ) { | |
| 1227 | - foreach ( (array) $children as $child ) { | |
| 1715 | + if ( ! empty( $children ) ) { | |
| 1716 | + foreach ( $children as $child ) { | |
| 1228 | 1717 | $children_last_active = bbp_update_forum_last_active_id( $child, $active_id ); |
| 1229 | 1718 | } |
| 1230 | 1719 | } |
| 1231 | 1720 | |
| 1232 | - // Don't count replies if the forum is a category | |
| 1721 | + // Get topic IDs and only accept larger IDs | |
| 1233 | 1722 | $topic_ids = bbp_forum_query_topic_ids( $forum_id ); |
| 1234 | - if ( !empty( $topic_ids ) ) { | |
| 1723 | + if ( ! empty( $topic_ids ) ) { | |
| 1724 | + | |
| 1725 | + // Make sure ID is larger | |
| 1235 | 1726 | $active_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids ); |
| 1236 | - $active_id = $active_id > max( $topic_ids ) ? $active_id : max( $topic_ids ); | |
| 1727 | + $active_id = $active_id > max( $topic_ids ) | |
| 1728 | + ? $active_id | |
| 1729 | + : max( $topic_ids ); | |
| 1237 | 1730 | |
| 1238 | 1731 | // Forum has no topics |
| 1239 | 1732 | } else { |
| 1240 | 1733 | $active_id = 0; |
| @@ -1244,85 +1737,78 @@ | ||
| 1244 | 1737 | // Cast as integer in case of empty or string |
| 1245 | 1738 | $active_id = (int) $active_id; |
| 1246 | 1739 | $children_last_active = (int) $children_last_active; |
| 1247 | 1740 | |
| 1248 | - // If child forums have higher id, use that instead | |
| 1249 | - if ( !empty( $children ) && ( $children_last_active > $active_id ) ) | |
| 1741 | + // If child forums have higher ID, use that instead | |
| 1742 | + if ( ! empty( $children ) && ( $children_last_active > $active_id ) ) { | |
| 1250 | 1743 | $active_id = $children_last_active; |
| 1744 | + } | |
| 1251 | 1745 | |
| 1252 | - // Update only if published | |
| 1253 | - if ( bbp_get_public_status_id() == get_post_status( $active_id ) ) | |
| 1254 | - update_post_meta( $forum_id, '_bbp_last_active_id', (int) $active_id ); | |
| 1746 | + update_post_meta( $forum_id, '_bbp_last_active_id', $active_id ); | |
| 1255 | 1747 | |
| 1256 | - return (int) apply_filters( 'bbp_update_forum_last_active_id', (int) $active_id, $forum_id ); | |
| 1748 | + // Filter & return | |
| 1749 | + return (int) apply_filters( 'bbp_update_forum_last_active_id', $active_id, $forum_id ); | |
| 1257 | 1750 | } |
| 1258 | 1751 | |
| 1259 | 1752 | /** |
| 1260 | 1753 | * Update the forums last active date/time (aka freshness) |
| 1261 | 1754 | * |
| 1262 | - * @since bbPress (r2680) | |
| 1755 | + * @since 2.0.0 bbPress (r2680) | |
| 1263 | 1756 | * |
| 1264 | - * @param int $forum_id Optional. Topic id | |
| 1265 | - * @param string $new_time Optional. New time in mysql format | |
| 1266 | - * @uses bbp_get_forum_id() To get the forum id | |
| 1267 | - * @uses bbp_get_forum_last_active_id() To get the forum's last post id | |
| 1268 | - * @uses get_post_field() To get the post date of the forum's last post | |
| 1269 | - * @uses update_post_meta() To update the forum last active time | |
| 1270 | - * @uses apply_filters() Calls 'bbp_update_forum_last_active' with the new time | |
| 1271 | - * and forum id | |
| 1272 | - * @return bool True on success, false on failure | |
| 1757 | + * @param int $forum_id Optional. Topic id. | |
| 1758 | + * @param string $new_time Optional. New time in mysql format. | |
| 1759 | + * | |
| 1760 | + * @return string MySQL timestamp of last active topic or reply | |
| 1273 | 1761 | */ |
| 1274 | 1762 | function bbp_update_forum_last_active_time( $forum_id = 0, $new_time = '' ) { |
| 1275 | 1763 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 1276 | 1764 | |
| 1277 | 1765 | // Check time and use current if empty |
| 1278 | - if ( empty( $new_time ) ) | |
| 1766 | + if ( empty( $new_time ) ) { | |
| 1279 | 1767 | $new_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $forum_id ) ); |
| 1768 | + } | |
| 1280 | 1769 | |
| 1281 | 1770 | // Update only if there is a time |
| 1282 | - if ( !empty( $new_time ) ) | |
| 1771 | + if ( ! empty( $new_time ) ) { | |
| 1283 | 1772 | update_post_meta( $forum_id, '_bbp_last_active_time', $new_time ); |
| 1773 | + } | |
| 1284 | 1774 | |
| 1285 | - return (int) apply_filters( 'bbp_update_forum_last_active', $new_time, $forum_id ); | |
| 1775 | + // Filter & return | |
| 1776 | + return apply_filters( 'bbp_update_forum_last_active', $new_time, $forum_id ); | |
| 1286 | 1777 | } |
| 1287 | 1778 | |
| 1288 | 1779 | /** |
| 1289 | 1780 | * Update the forum sub-forum count |
| 1290 | 1781 | * |
| 1291 | - * @since bbPress (r2625) | |
| 1782 | + * @since 2.0.0 bbPress (r2625) | |
| 1292 | 1783 | * |
| 1293 | 1784 | * @param int $forum_id Optional. Forum id |
| 1294 | - * @uses bbp_get_forum_id() To get the forum id | |
| 1785 | + * @param int $subforums Optional. Number of subforums | |
| 1295 | 1786 | * @return bool True on success, false on failure |
| 1296 | 1787 | */ |
| 1297 | -function bbp_update_forum_subforum_count( $forum_id = 0, $subforums = 0 ) { | |
| 1788 | +function bbp_update_forum_subforum_count( $forum_id = 0, $subforums = false ) { | |
| 1298 | 1789 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 1299 | 1790 | |
| 1300 | - if ( empty( $subforums ) ) | |
| 1301 | - $subforums = count( bbp_forum_query_subforum_ids( $forum_id ) ); | |
| 1791 | + // Maybe query for counts | |
| 1792 | + $subforums = ! is_int( $subforums ) | |
| 1793 | + ? bbp_get_public_child_count( $forum_id, bbp_get_forum_post_type() ) | |
| 1794 | + : (int) $subforums; | |
| 1302 | 1795 | |
| 1303 | - update_post_meta( $forum_id, '_bbp_forum_subforum_count', (int) $subforums ); | |
| 1796 | + update_post_meta( $forum_id, '_bbp_forum_subforum_count', $subforums ); | |
| 1304 | 1797 | |
| 1305 | - return (int) apply_filters( 'bbp_update_forum_subforum_count', (int) $subforums, $forum_id ); | |
| 1798 | + // Filter & return | |
| 1799 | + return (int) apply_filters( 'bbp_update_forum_subforum_count', $subforums, $forum_id ); | |
| 1306 | 1800 | } |
| 1307 | 1801 | |
| 1308 | 1802 | /** |
| 1309 | 1803 | * Adjust the total topic count of a forum |
| 1310 | 1804 | * |
| 1311 | - * @since bbPress (r2464) | |
| 1805 | + * @since 2.0.0 bbPress (r2464) | |
| 1312 | 1806 | * |
| 1313 | 1807 | * @param int $forum_id Optional. Forum id or topic id. It is checked whether it |
| 1314 | 1808 | * is a topic or a forum. If it's a topic, its parent, |
| 1315 | 1809 | * i.e. the forum is automatically retrieved. |
| 1316 | - * @param bool $total_count Optional. To return the total count or normal | |
| 1317 | - * count? | |
| 1318 | - * @uses bbp_get_forum_id() To get the forum id | |
| 1319 | - * @uses bbp_forum_query_subforum_ids() To get the subforum ids | |
| 1320 | - * @uses bbp_update_forum_topic_count() To update the forum topic count | |
| 1321 | - * @uses bbp_forum_query_topic_ids() To get the forum topic ids | |
| 1322 | - * @uses update_post_meta() To update the forum's topic count meta | |
| 1323 | - * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the topic | |
| 1324 | - * count and forum id | |
| 1810 | + * @param bool $total_count Optional. To return the total count or normal count? | |
| 1325 | 1811 | * @return int Forum topic count |
| 1326 | 1812 | */ |
| 1327 | 1813 | function bbp_update_forum_topic_count( $forum_id = 0 ) { |
| 1328 | 1814 | $forum_id = bbp_get_forum_id( $forum_id ); |
| @@ -1329,47 +1815,41 @@ | ||
| 1329 | 1815 | $children_topic_count = 0; |
| 1330 | 1816 | |
| 1331 | 1817 | // Loop through subforums and add together forum topic counts |
| 1332 | 1818 | $children = bbp_forum_query_subforum_ids( $forum_id ); |
| 1333 | - if ( !empty( $children ) ) { | |
| 1334 | - foreach ( (array) $children as $child ) { | |
| 1819 | + if ( ! empty( $children ) ) { | |
| 1820 | + foreach ( $children as $child ) { | |
| 1335 | 1821 | $children_topic_count += bbp_update_forum_topic_count( $child ); // Recursive |
| 1336 | 1822 | } |
| 1337 | 1823 | } |
| 1338 | 1824 | |
| 1339 | 1825 | // Get total topics for this forum |
| 1340 | - $topics = (int) count( bbp_forum_query_topic_ids( $forum_id ) ); | |
| 1826 | + $topics = bbp_get_public_child_count( $forum_id, bbp_get_topic_post_type() ); | |
| 1341 | 1827 | |
| 1342 | 1828 | // Calculate total topics in this forum |
| 1343 | - $total_topics = $topics + $children_topic_count; | |
| 1829 | + $total_topics = (int) ( $topics + $children_topic_count ); | |
| 1344 | 1830 | |
| 1345 | 1831 | // Update the count |
| 1346 | - update_post_meta( $forum_id, '_bbp_topic_count', (int) $topics ); | |
| 1347 | - update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topics ); | |
| 1832 | + update_post_meta( $forum_id, '_bbp_topic_count', $topics ); | |
| 1833 | + update_post_meta( $forum_id, '_bbp_total_topic_count', $total_topics ); | |
| 1348 | 1834 | |
| 1349 | - return (int) apply_filters( 'bbp_update_forum_topic_count', (int) $total_topics, $forum_id ); | |
| 1835 | + // Filter & return | |
| 1836 | + return (int) apply_filters( 'bbp_update_forum_topic_count', $total_topics, $forum_id ); | |
| 1350 | 1837 | } |
| 1351 | 1838 | |
| 1352 | 1839 | /** |
| 1353 | - * Adjust the total hidden topic count of a forum (hidden includes trashed and spammed topics) | |
| 1840 | + * Adjust the total hidden topic count of a forum (hidden includes trashed, | |
| 1841 | + * spammed and pending topics) | |
| 1354 | 1842 | * |
| 1355 | - * @since bbPress (r2888) | |
| 1843 | + * @since 2.0.0 bbPress (r2888) | |
| 1844 | + * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects | |
| 1356 | 1845 | * |
| 1357 | - * @param int $forum_id Optional. Topic id to update | |
| 1358 | - * @param int $topic_count Optional. Set the topic count manually | |
| 1359 | - * @uses bbp_is_topic() To check if the supplied id is a topic | |
| 1360 | - * @uses bbp_get_topic_id() To get the topic id | |
| 1361 | - * @uses bbp_get_topic_forum_id() To get the topic forum id | |
| 1362 | - * @uses bbp_get_forum_id() To get the forum id | |
| 1363 | - * @uses wpdb::prepare() To prepare our sql query | |
| 1364 | - * @uses wpdb::get_col() To execute our query and get the column back | |
| 1365 | - * @uses update_post_meta() To update the forum hidden topic count meta | |
| 1366 | - * @uses apply_filters() Calls 'bbp_update_forum_topic_count_hidden' with the | |
| 1367 | - * hidden topic count and forum id | |
| 1846 | + * @param int $forum_id Optional. Topic id to update. | |
| 1847 | + * @param int $topic_count Optional. Set the topic count manually. | |
| 1848 | + * | |
| 1368 | 1849 | * @return int Topic hidden topic count |
| 1369 | 1850 | */ |
| 1370 | -function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) { | |
| 1371 | - global $wpdb; | |
| 1851 | +function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = false ) { | |
| 1372 | 1852 | |
| 1373 | 1853 | // If topic_id was passed as $forum_id, then get its forum |
| 1374 | 1854 | if ( bbp_is_topic( $forum_id ) ) { |
| 1375 | 1855 | $topic_id = bbp_get_topic_id( $forum_id ); |
| @@ -1380,44 +1860,54 @@ | ||
| 1380 | 1860 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 1381 | 1861 | } |
| 1382 | 1862 | |
| 1383 | 1863 | // Can't update what isn't there |
| 1384 | - if ( !empty( $forum_id ) ) { | |
| 1864 | + if ( ! empty( $forum_id ) ) { | |
| 1385 | 1865 | |
| 1386 | 1866 | // Get topics of forum |
| 1387 | - if ( empty( $topic_count ) ) | |
| 1388 | - $topic_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( '\',\'', array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ) ) . "') AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) ); | |
| 1867 | + if ( ! is_int( $topic_count ) ) { | |
| 1868 | + $query = new WP_Query( array( | |
| 1869 | + 'fields' => 'ids', | |
| 1870 | + 'post_parent' => $forum_id, | |
| 1871 | + 'post_status' => bbp_get_non_public_topic_statuses(), | |
| 1872 | + 'post_type' => bbp_get_topic_post_type(), | |
| 1873 | + 'posts_per_page' => -1, | |
| 1389 | 1874 | |
| 1875 | + // Performance | |
| 1876 | + 'nopaging' => true, | |
| 1877 | + 'suppress_filters' => true, | |
| 1878 | + 'update_post_term_cache' => false, | |
| 1879 | + 'update_post_meta_cache' => false, | |
| 1880 | + 'ignore_sticky_posts' => true, | |
| 1881 | + 'no_found_rows' => true | |
| 1882 | + ) ); | |
| 1883 | + $topic_count = $query->post_count; | |
| 1884 | + unset( $query ); | |
| 1885 | + } | |
| 1886 | + | |
| 1887 | + $topic_count = (int) $topic_count; | |
| 1888 | + | |
| 1390 | 1889 | // Update the count |
| 1391 | - update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $topic_count ); | |
| 1890 | + update_post_meta( $forum_id, '_bbp_topic_count_hidden', $topic_count ); | |
| 1392 | 1891 | } |
| 1393 | 1892 | |
| 1394 | - return (int) apply_filters( 'bbp_update_forum_topic_count_hidden', (int) $topic_count, $forum_id ); | |
| 1893 | + // Filter & return | |
| 1894 | + return (int) apply_filters( 'bbp_update_forum_topic_count_hidden', $topic_count, $forum_id ); | |
| 1395 | 1895 | } |
| 1396 | 1896 | |
| 1397 | 1897 | /** |
| 1398 | 1898 | * Adjust the total reply count of a forum |
| 1399 | 1899 | * |
| 1400 | - * @since bbPress (r2464) | |
| 1900 | + * @since 2.0.0 bbPress (r2464) | |
| 1901 | + * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects | |
| 1401 | 1902 | * |
| 1402 | - * @param int $forum_id Optional. Forum id or topic id. It is checked whether it | |
| 1903 | + * @param int $forum_id Optional. Forum id or topic id. It is checked whether it | |
| 1403 | 1904 | * is a topic or a forum. If it's a topic, its parent, |
| 1404 | 1905 | * i.e. the forum is automatically retrieved. |
| 1405 | - * @param bool $total_count Optional. To return the total count or normal | |
| 1406 | - * count? | |
| 1407 | - * @uses bbp_get_forum_id() To get the forum id | |
| 1408 | - * @uses bbp_forum_query_subforum_ids() To get the subforum ids | |
| 1409 | - * @uses bbp_update_forum_reply_count() To update the forum reply count | |
| 1410 | - * @uses bbp_forum_query_topic_ids() To get the forum topic ids | |
| 1411 | - * @uses wpdb::prepare() To prepare the sql statement | |
| 1412 | - * @uses wpdb::get_var() To execute the query and get the var back | |
| 1413 | - * @uses update_post_meta() To update the forum's reply count meta | |
| 1414 | - * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the reply | |
| 1415 | - * count and forum id | |
| 1906 | + * | |
| 1416 | 1907 | * @return int Forum reply count |
| 1417 | 1908 | */ |
| 1418 | 1909 | function bbp_update_forum_reply_count( $forum_id = 0 ) { |
| 1419 | - global $wpdb; | |
| 1420 | 1910 | |
| 1421 | 1911 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 1422 | 1912 | $children_reply_count = 0; |
| 1423 | 1913 | |
| @@ -1422,9 +1912,9 @@ | ||
| 1422 | 1912 | $children_reply_count = 0; |
| 1423 | 1913 | |
| 1424 | 1914 | // Loop through children and add together forum reply counts |
| 1425 | 1915 | $children = bbp_forum_query_subforum_ids( $forum_id ); |
| 1426 | - if ( !empty( $children ) ) { | |
| 1916 | + if ( ! empty( $children ) ) { | |
| 1427 | 1917 | foreach ( (array) $children as $child ) { |
| 1428 | 1918 | $children_reply_count += bbp_update_forum_reply_count( $child ); |
| 1429 | 1919 | } |
| 1430 | 1920 | } |
| @@ -1429,25 +1919,64 @@ | ||
| 1429 | 1919 | } |
| 1430 | 1920 | } |
| 1431 | 1921 | |
| 1432 | 1922 | // Don't count replies if the forum is a category |
| 1433 | - $topic_ids = bbp_forum_query_topic_ids( $forum_id ); | |
| 1434 | - if ( !empty( $topic_ids ) ) | |
| 1435 | - $reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) ); | |
| 1436 | - else | |
| 1437 | - $reply_count = 0; | |
| 1923 | + $reply_count = ! bbp_is_forum_category( $forum_id ) | |
| 1924 | + ? bbp_get_public_child_count( $forum_id, bbp_get_reply_post_type() ) | |
| 1925 | + : 0; | |
| 1438 | 1926 | |
| 1439 | 1927 | // Calculate total replies in this forum |
| 1440 | - $total_replies = (int) $reply_count + $children_reply_count; | |
| 1928 | + $total_replies = (int) ( $reply_count + $children_reply_count ); | |
| 1441 | 1929 | |
| 1442 | - // Update the count | |
| 1443 | - update_post_meta( $forum_id, '_bbp_reply_count', (int) $reply_count ); | |
| 1444 | - update_post_meta( $forum_id, '_bbp_total_reply_count', (int) $total_replies ); | |
| 1930 | + // Update the counts | |
| 1931 | + update_post_meta( $forum_id, '_bbp_reply_count', $reply_count ); | |
| 1932 | + update_post_meta( $forum_id, '_bbp_total_reply_count', $total_replies ); | |
| 1445 | 1933 | |
| 1446 | - return (int) apply_filters( 'bbp_update_forum_reply_count', (int) $total_replies, $forum_id ); | |
| 1934 | + // Filter & return | |
| 1935 | + return (int) apply_filters( 'bbp_update_forum_reply_count', $total_replies, $forum_id ); | |
| 1447 | 1936 | } |
| 1448 | 1937 | |
| 1449 | 1938 | /** |
| 1939 | + * Adjust the total hidden reply count of a forum | |
| 1940 | + * | |
| 1941 | + * @since 2.6.0 bbPress (r6922) | |
| 1942 | + * | |
| 1943 | + * @param int $forum_id Optional. Forum id or topic id. It is checked whether it | |
| 1944 | + * is a topic or a forum. If it's a topic, its parent, | |
| 1945 | + * i.e. the forum is automatically retrieved. | |
| 1946 | + * | |
| 1947 | + * @return int Forum reply count | |
| 1948 | + */ | |
| 1949 | +function bbp_update_forum_reply_count_hidden( $forum_id = 0 ) { | |
| 1950 | + | |
| 1951 | + $forum_id = bbp_get_forum_id( $forum_id ); | |
| 1952 | + $children_reply_count = 0; | |
| 1953 | + | |
| 1954 | + // Loop through children and add together forum reply counts | |
| 1955 | + $children = bbp_forum_query_subforum_ids( $forum_id ); | |
| 1956 | + if ( ! empty( $children ) ) { | |
| 1957 | + foreach ( (array) $children as $child ) { | |
| 1958 | + $children_reply_count += bbp_update_forum_reply_count_hidden( $child ); | |
| 1959 | + } | |
| 1960 | + } | |
| 1961 | + | |
| 1962 | + // Don't count replies if the forum is a category | |
| 1963 | + $reply_count = ! bbp_is_forum_category( $forum_id ) | |
| 1964 | + ? bbp_get_non_public_child_count( $forum_id, bbp_get_reply_post_type() ) | |
| 1965 | + : 0; | |
| 1966 | + | |
| 1967 | + // Calculate total replies in this forum | |
| 1968 | + $total_replies = (int) ( $reply_count + $children_reply_count ); | |
| 1969 | + | |
| 1970 | + // Update the counts | |
| 1971 | + update_post_meta( $forum_id, '_bbp_reply_count_hidden', $reply_count ); | |
| 1972 | + update_post_meta( $forum_id, '_bbp_total_reply_count_hidden', $total_replies ); | |
| 1973 | + | |
| 1974 | + // Filter & return | |
| 1975 | + return (int) apply_filters( 'bbp_update_forum_reply_count_hidden', $total_replies, $forum_id ); | |
| 1976 | +} | |
| 1977 | + | |
| 1978 | +/** | |
| 1450 | 1979 | * Updates the counts of a forum. |
| 1451 | 1980 | * |
| 1452 | 1981 | * This calls a few internal functions that all run manual queries against the |
| 1453 | 1982 | * database to get their results. As such, this function can be costly to run |
| @@ -1452,28 +1981,21 @@ | ||
| 1452 | 1981 | * This calls a few internal functions that all run manual queries against the |
| 1453 | 1982 | * database to get their results. As such, this function can be costly to run |
| 1454 | 1983 | * but is necessary to keep everything accurate. |
| 1455 | 1984 | * |
| 1456 | - * @since bbPress (r2908) | |
| 1985 | + * @since 2.0.0 bbPress (r2908) | |
| 1457 | 1986 | * |
| 1458 | - * @param mixed $args Supports these arguments: | |
| 1987 | + * @param array $args Supports these arguments: | |
| 1459 | 1988 | * - forum_id: Forum id |
| 1460 | 1989 | * - last_topic_id: Last topic id |
| 1461 | 1990 | * - last_reply_id: Last reply id |
| 1462 | 1991 | * - last_active_id: Last active post id |
| 1463 | 1992 | * - last_active_time: last active time |
| 1464 | - * @uses bbp_update_forum_last_topic_id() To update the forum last topic id | |
| 1465 | - * @uses bbp_update_forum_last_reply_id() To update the forum last reply id | |
| 1466 | - * @uses bbp_update_forum_last_active_id() To update the last active post id | |
| 1467 | - * @uses get_post_field() To get the post date of the last active id | |
| 1468 | - * @uses bbp_update_forum_last_active_time() To update the last active time | |
| 1469 | - * @uses bbp_update_forum_subforum_count() To update the subforum count | |
| 1470 | - * @uses bbp_update_forum_topic_count() To update the forum topic count | |
| 1471 | - * @uses bbp_update_forum_reply_count() To update the forum reply count | |
| 1472 | - * @uses bbp_update_forum_topic_count_hidden() To update the hidden topic count | |
| 1473 | 1993 | */ |
| 1474 | -function bbp_update_forum( $args = '' ) { | |
| 1475 | - $defaults = array( | |
| 1994 | +function bbp_update_forum( $args = array() ) { | |
| 1995 | + | |
| 1996 | + // Parse arguments against default values | |
| 1997 | + $r = bbp_parse_args( $args, array( | |
| 1476 | 1998 | 'forum_id' => 0, |
| 1477 | 1999 | 'post_parent' => 0, |
| 1478 | 2000 | 'last_topic_id' => 0, |
| 1479 | 2001 | 'last_reply_id' => 0, |
| @@ -1479,42 +2001,148 @@ | ||
| 1479 | 2001 | 'last_reply_id' => 0, |
| 1480 | 2002 | 'last_active_id' => 0, |
| 1481 | 2003 | 'last_active_time' => 0, |
| 1482 | 2004 | 'last_active_status' => bbp_get_public_status_id() |
| 1483 | - ); | |
| 1484 | - $r = bbp_parse_args( $args, $defaults, 'update_forum' ); | |
| 1485 | - extract( $r ); | |
| 2005 | + ), 'update_forum' ); | |
| 1486 | 2006 | |
| 2007 | + // Update the forum parent | |
| 2008 | + bbp_update_forum_id( $r['forum_id'], $r['post_parent'] ); | |
| 2009 | + | |
| 1487 | 2010 | // Last topic and reply ID's |
| 1488 | - bbp_update_forum_last_topic_id( $forum_id, $last_topic_id ); | |
| 1489 | - bbp_update_forum_last_reply_id( $forum_id, $last_reply_id ); | |
| 2011 | + bbp_update_forum_last_topic_id( $r['forum_id'], $r['last_topic_id'] ); | |
| 2012 | + bbp_update_forum_last_reply_id( $r['forum_id'], $r['last_reply_id'] ); | |
| 1490 | 2013 | |
| 1491 | 2014 | // Active dance |
| 1492 | - $last_active_id = bbp_update_forum_last_active_id( $forum_id, $last_active_id ); | |
| 2015 | + $r['last_active_id'] = bbp_update_forum_last_active_id( $r['forum_id'], $r['last_active_id'] ); | |
| 1493 | 2016 | |
| 1494 | 2017 | // If no active time was passed, get it from the last_active_id |
| 1495 | - if ( empty( $last_active_time ) ) | |
| 1496 | - $last_active_time = get_post_field( 'post_date', $last_active_id ); | |
| 2018 | + if ( empty( $r['last_active_time'] ) ) { | |
| 2019 | + $r['last_active_time'] = get_post_field( 'post_date', $r['last_active_id'] ); | |
| 2020 | + } | |
| 1497 | 2021 | |
| 1498 | - if ( bbp_get_public_status_id() == $last_active_status ) { | |
| 1499 | - bbp_update_forum_last_active_time( $forum_id, $last_active_time ); | |
| 2022 | + if ( bbp_get_public_status_id() === $r['last_active_status'] ) { | |
| 2023 | + bbp_update_forum_last_active_time( $r['forum_id'], $r['last_active_time'] ); | |
| 1500 | 2024 | } |
| 1501 | 2025 | |
| 1502 | 2026 | // Counts |
| 1503 | - bbp_update_forum_subforum_count ( $forum_id ); | |
| 1504 | - bbp_update_forum_reply_count ( $forum_id ); | |
| 1505 | - bbp_update_forum_topic_count ( $forum_id ); | |
| 1506 | - bbp_update_forum_topic_count_hidden( $forum_id ); | |
| 2027 | + bbp_update_forum_subforum_count( $r['forum_id'] ); | |
| 1507 | 2028 | |
| 2029 | + // Only update topic count if we've deleted a topic | |
| 2030 | + if ( in_array( current_filter(), array( 'bbp_deleted_topic', 'save_post' ), true ) ) { | |
| 2031 | + bbp_update_forum_reply_count( $r['forum_id'] ); | |
| 2032 | + bbp_update_forum_topic_count( $r['forum_id'] ); | |
| 2033 | + bbp_update_forum_topic_count_hidden( $r['forum_id'] ); | |
| 2034 | + bbp_update_forum_reply_count_hidden( $r['forum_id'] ); | |
| 2035 | + } | |
| 2036 | + | |
| 1508 | 2037 | // Update the parent forum if one was passed |
| 1509 | - if ( !empty( $post_parent ) && is_numeric( $post_parent ) ) { | |
| 2038 | + if ( ! empty( $r['post_parent'] ) && is_numeric( $r['post_parent'] ) ) { | |
| 1510 | 2039 | bbp_update_forum( array( |
| 1511 | - 'forum_id' => $post_parent, | |
| 1512 | - 'post_parent' => get_post_field( 'post_parent', $post_parent ) | |
| 2040 | + 'forum_id' => $r['post_parent'], | |
| 2041 | + 'post_parent' => get_post_field( 'post_parent', $r['post_parent'] ) | |
| 1513 | 2042 | ) ); |
| 1514 | 2043 | } |
| 2044 | + | |
| 2045 | + // Bump the custom query cache | |
| 2046 | + wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' ); | |
| 1515 | 2047 | } |
| 1516 | 2048 | |
| 2049 | +/** Helpers *******************************************************************/ | |
| 2050 | + | |
| 2051 | +/** | |
| 2052 | + * Return an associative array of available topic statuses | |
| 2053 | + * | |
| 2054 | + * Developers note: these statuses are actually stored as meta data, and | |
| 2055 | + * Visibilities are stored in post_status. | |
| 2056 | + * | |
| 2057 | + * @since 2.4.0 bbPress (r5059) | |
| 2058 | + * | |
| 2059 | + * @param int $forum_id Optional. Forum id. | |
| 2060 | + * | |
| 2061 | + * @return array | |
| 2062 | + */ | |
| 2063 | +function bbp_get_forum_statuses( $forum_id = 0 ) { | |
| 2064 | + | |
| 2065 | + // Filter & return | |
| 2066 | + return (array) apply_filters( 'bbp_get_forum_statuses', array( | |
| 2067 | + 'open' => _x( 'Open', 'Open the forum', 'bbpress' ), | |
| 2068 | + 'closed' => _x( 'Closed', 'Close the forum', 'bbpress' ) | |
| 2069 | + ), $forum_id ); | |
| 2070 | +} | |
| 2071 | + | |
| 2072 | +/** | |
| 2073 | + * Return an associative array of forum types | |
| 2074 | + * | |
| 2075 | + * @since 2.4.0 bbPress (r5059) | |
| 2076 | + * | |
| 2077 | + * @param int $forum_id Optional. Forum id. | |
| 2078 | + * | |
| 2079 | + * @return array | |
| 2080 | + */ | |
| 2081 | +function bbp_get_forum_types( $forum_id = 0 ) { | |
| 2082 | + | |
| 2083 | + // Filter & return | |
| 2084 | + return (array) apply_filters( 'bbp_get_forum_types', array( | |
| 2085 | + 'forum' => _x( 'Forum', 'Forum accepts new topics', 'bbpress' ), | |
| 2086 | + 'category' => _x( 'Category', 'Forum is a category', 'bbpress' ) | |
| 2087 | + ), $forum_id ); | |
| 2088 | +} | |
| 2089 | + | |
| 2090 | +/** | |
| 2091 | + * Return an associative array of forum visibility | |
| 2092 | + * | |
| 2093 | + * Developers note: these visibilities are actually stored in post_status, and | |
| 2094 | + * Statuses are stored in meta data. | |
| 2095 | + * | |
| 2096 | + * @since 2.4.0 bbPress (r5059) | |
| 2097 | + * | |
| 2098 | + * @param int $forum_id Optional. Forum id. | |
| 2099 | + * | |
| 2100 | + * @return array | |
| 2101 | + */ | |
| 2102 | +function bbp_get_forum_visibilities( $forum_id = 0) { | |
| 2103 | + | |
| 2104 | + // Filter & return | |
| 2105 | + return (array) apply_filters( 'bbp_get_forum_visibilities', array( | |
| 2106 | + bbp_get_public_status_id() => _x( 'Public', 'Make forum public', 'bbpress' ), | |
| 2107 | + bbp_get_private_status_id() => _x( 'Private', 'Make forum private', 'bbpress' ), | |
| 2108 | + bbp_get_hidden_status_id() => _x( 'Hidden', 'Make forum hidden', 'bbpress' ) | |
| 2109 | + ), $forum_id ); | |
| 2110 | +} | |
| 2111 | + | |
| 2112 | +/** | |
| 2113 | + * Return array of public forum statuses. | |
| 2114 | + * | |
| 2115 | + * @since 2.6.0 bbPress (r6921) | |
| 2116 | + * | |
| 2117 | + * @return array | |
| 2118 | + */ | |
| 2119 | +function bbp_get_public_forum_statuses() { | |
| 2120 | + $statuses = array( | |
| 2121 | + bbp_get_public_status_id() | |
| 2122 | + ); | |
| 2123 | + | |
| 2124 | + // Filter & return | |
| 2125 | + return (array) apply_filters( 'bbp_get_public_forum_statuses', $statuses ); | |
| 2126 | +} | |
| 2127 | + | |
| 2128 | +/** | |
| 2129 | + * Return array of non-public forum statuses. | |
| 2130 | + * | |
| 2131 | + * @since 2.6.0 bbPress (r6921) | |
| 2132 | + * | |
| 2133 | + * @return array | |
| 2134 | + */ | |
| 2135 | +function bbp_get_non_public_forum_statuses() { | |
| 2136 | + $statuses = array( | |
| 2137 | + bbp_get_private_status_id(), | |
| 2138 | + bbp_get_hidden_status_id() | |
| 2139 | + ); | |
| 2140 | + | |
| 2141 | + // Filter & return | |
| 2142 | + return (array) apply_filters( 'bbp_get_non_public_forum_statuses', $statuses ); | |
| 2143 | +} | |
| 2144 | + | |
| 1517 | 2145 | /** Queries *******************************************************************/ |
| 1518 | 2146 | |
| 1519 | 2147 | /** |
| 1520 | 2148 | * Returns the hidden forum ids |
| @@ -1520,18 +2148,18 @@ | ||
| 1520 | 2148 | * Returns the hidden forum ids |
| 1521 | 2149 | * |
| 1522 | 2150 | * Only hidden forum ids are returned. Public and private ids are not. |
| 1523 | 2151 | * |
| 1524 | - * @since bbPress (r3007) | |
| 1525 | - * | |
| 1526 | - * @uses get_option() Returns the unserialized array of hidden forum ids | |
| 1527 | - * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids | |
| 1528 | - * and forum id | |
| 2152 | + * @since 2.0.0 bbPress (r3007) | |
| 1529 | 2153 | */ |
| 1530 | 2154 | function bbp_get_hidden_forum_ids() { |
| 1531 | - $forum_ids = get_option( '_bbp_hidden_forums', array() ); | |
| 2155 | + $forum_ids = get_option( '_bbp_hidden_forums', array() ); | |
| 2156 | + $forum_ids = ! empty( $forum_ids ) | |
| 2157 | + ? wp_parse_id_list( $forum_ids ) | |
| 2158 | + : array(); | |
| 1532 | 2159 | |
| 1533 | - return apply_filters( 'bbp_get_hidden_forum_ids', (array) $forum_ids ); | |
| 2160 | + // Filter & return | |
| 2161 | + return (array) apply_filters( 'bbp_get_hidden_forum_ids', $forum_ids ); | |
| 1534 | 2162 | } |
| 1535 | 2163 | |
| 1536 | 2164 | /** |
| 1537 | 2165 | * Returns the private forum ids |
| @@ -1537,182 +2165,188 @@ | ||
| 1537 | 2165 | * Returns the private forum ids |
| 1538 | 2166 | * |
| 1539 | 2167 | * Only private forum ids are returned. Public and hidden ids are not. |
| 1540 | 2168 | * |
| 1541 | - * @since bbPress (r3007) | |
| 1542 | - * | |
| 1543 | - * @uses get_option() Returns the unserialized array of private forum ids | |
| 1544 | - * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids | |
| 1545 | - * and forum id | |
| 2169 | + * @since 2.0.0 bbPress (r3007) | |
| 1546 | 2170 | */ |
| 1547 | 2171 | function bbp_get_private_forum_ids() { |
| 1548 | - $forum_ids = get_option( '_bbp_private_forums', array() ); | |
| 2172 | + $forum_ids = get_option( '_bbp_private_forums', array() ); | |
| 2173 | + $forum_ids = ! empty( $forum_ids ) | |
| 2174 | + ? wp_parse_id_list( $forum_ids ) | |
| 2175 | + : array(); | |
| 1549 | 2176 | |
| 1550 | - return apply_filters( 'bbp_get_private_forum_ids', (array) $forum_ids ); | |
| 2177 | + // Filter & return | |
| 2178 | + return (array) apply_filters( 'bbp_get_private_forum_ids', $forum_ids ); | |
| 1551 | 2179 | } |
| 1552 | 2180 | |
| 1553 | 2181 | /** |
| 1554 | - * Returns a meta_query that either includes or excludes hidden forum IDs | |
| 1555 | - * from a query. | |
| 2182 | + * Returns the forum IDs that should be excluded from various views & queries, | |
| 2183 | + * based on the current user's capabilities. | |
| 1556 | 2184 | * |
| 1557 | - * @since bbPress (r3291) | |
| 2185 | + * These results are automatically filtered by bbp_allow_forums_of_user(), to | |
| 2186 | + * allow per-forum moderators to see forums that would otherwise be private or | |
| 2187 | + * hidden to them. | |
| 1558 | 2188 | * |
| 1559 | - * @param string Optional. The type of value to return. (string|array|meta_query) | |
| 2189 | + * If you have a need to filter these results based on your own custom | |
| 2190 | + * engagements API usages, please see: bbp_allow_forums_of_user() | |
| 1560 | 2191 | * |
| 1561 | - * @uses is_super_admin() | |
| 1562 | - * @uses bbp_get_hidden_forum_ids() | |
| 1563 | - * @uses bbp_get_private_forum_ids() | |
| 1564 | - * @uses apply_filters() | |
| 2192 | + * @since 2.6.0 bbPress (r6425) | |
| 2193 | + * | |
| 2194 | + * @return array Forum IDs to exclude, or an empty array | |
| 1565 | 2195 | */ |
| 1566 | -function bbp_exclude_forum_ids( $type = 'string' ) { | |
| 2196 | +function bbp_get_excluded_forum_ids() { | |
| 1567 | 2197 | |
| 1568 | - // Setup arrays | |
| 1569 | - $private = $hidden = $meta_query = $forum_ids = array(); | |
| 2198 | + // Private forums | |
| 2199 | + $private = ! current_user_can( 'read_private_forums' ) | |
| 2200 | + ? bbp_get_private_forum_ids() | |
| 2201 | + : array(); | |
| 1570 | 2202 | |
| 1571 | - // Default return value | |
| 1572 | - switch ( $type ) { | |
| 1573 | - case 'string' : | |
| 1574 | - $retval = ''; | |
| 1575 | - break; | |
| 2203 | + // Hidden forums | |
| 2204 | + $hidden = ! current_user_can( 'read_hidden_forums' ) | |
| 2205 | + ? bbp_get_hidden_forum_ids() | |
| 2206 | + : array(); | |
| 1576 | 2207 | |
| 1577 | - case 'array' : | |
| 1578 | - $retval = array(); | |
| 1579 | - break; | |
| 2208 | + // Merge private & hidden forums together, and remove any empties | |
| 2209 | + $forum_ids = ( ! empty( $private ) || ! empty( $hidden ) ) | |
| 2210 | + ? array_filter( wp_parse_id_list( array_merge( $private, $hidden ) ) ) | |
| 2211 | + : array(); | |
| 1580 | 2212 | |
| 1581 | - case 'meta_query' : | |
| 1582 | - $retval = array( array() ) ; | |
| 1583 | - break; | |
| 1584 | - } | |
| 1585 | - | |
| 1586 | - // Exclude for everyone but super admins | |
| 1587 | - if ( !is_super_admin() ) { | |
| 2213 | + // Filter & return | |
| 2214 | + return (array) apply_filters( 'bbp_get_excluded_forum_ids', $forum_ids, $private, $hidden ); | |
| 2215 | +} | |
| 1588 | 2216 | |
| 1589 | - // Private forums | |
| 1590 | - if ( !current_user_can( 'read_private_forums' ) ) | |
| 1591 | - $private = bbp_get_private_forum_ids(); | |
| 2217 | +/** | |
| 2218 | + * Returns a meta_query that either includes or excludes hidden forum IDs | |
| 2219 | + * from a query. | |
| 2220 | + * | |
| 2221 | + * @since 2.0.0 bbPress (r3291) | |
| 2222 | + * | |
| 2223 | + * @param string Optional. The type of value to return. (string|array|meta_query) | |
| 2224 | + */ | |
| 2225 | +function bbp_exclude_forum_ids( $type = 'string' ) { | |
| 1592 | 2226 | |
| 1593 | - // Hidden forums | |
| 1594 | - if ( !current_user_can( 'read_hidden_forums' ) ) | |
| 1595 | - $hidden = bbp_get_hidden_forum_ids(); | |
| 2227 | + // Setup arrays | |
| 2228 | + $forum_ids = array(); | |
| 1596 | 2229 | |
| 1597 | - // Merge private and hidden forums together | |
| 1598 | - $forum_ids = (array) array_filter( array_merge( $private, $hidden ) ); | |
| 2230 | + // Types | |
| 2231 | + $types = array( | |
| 2232 | + 'array' => array(), | |
| 2233 | + 'string' => '', | |
| 2234 | + 'meta_query' => array() | |
| 2235 | + ); | |
| 1599 | 2236 | |
| 1600 | - // There are forums that need to be excluded | |
| 1601 | - if ( !empty( $forum_ids ) ) { | |
| 2237 | + // Exclude for everyone but keymasters | |
| 2238 | + if ( ! bbp_is_user_keymaster() ) { | |
| 1602 | 2239 | |
| 1603 | - switch ( $type ) { | |
| 2240 | + // Get forum IDs to exclude | |
| 2241 | + $forum_ids = bbp_get_excluded_forum_ids(); | |
| 1604 | 2242 | |
| 1605 | - // Separate forum ID's into a comma separated string | |
| 1606 | - case 'string' : | |
| 1607 | - $retval = implode( ',', $forum_ids ); | |
| 1608 | - break; | |
| 2243 | + // Store return values in static types array | |
| 2244 | + if ( ! empty( $forum_ids ) ) { | |
| 1609 | 2245 | |
| 1610 | - // Use forum_ids array | |
| 1611 | - case 'array' : | |
| 1612 | - $retval = $forum_ids; | |
| 1613 | - break; | |
| 2246 | + // Comparison | |
| 2247 | + $compare = ( 1 < count( $forum_ids ) ) | |
| 2248 | + ? 'NOT IN' | |
| 2249 | + : '!='; | |
| 1614 | 2250 | |
| 1615 | - // Build a meta_query | |
| 1616 | - case 'meta_query' : | |
| 1617 | - $retval = array( | |
| 1618 | - 'key' => '_bbp_forum_id', | |
| 1619 | - 'value' => implode( ',', $forum_ids ), | |
| 1620 | - 'type' => 'numeric', | |
| 1621 | - 'compare' => ( 1 < count( $forum_ids ) ) ? 'NOT IN' : '!=' | |
| 1622 | - ); | |
| 1623 | - break; | |
| 1624 | - } | |
| 2251 | + // Setup types | |
| 2252 | + $types['array'] = $forum_ids; | |
| 2253 | + $types['string'] = implode( ',', $forum_ids ); | |
| 2254 | + $types['meta_query'] = array( | |
| 2255 | + 'key' => '_bbp_forum_id', | |
| 2256 | + 'value' => $types['string'], | |
| 2257 | + 'type' => 'NUMERIC', | |
| 2258 | + 'compare' => $compare | |
| 2259 | + ); | |
| 1625 | 2260 | } |
| 1626 | 2261 | } |
| 1627 | 2262 | |
| 1628 | - // Filter and return the results | |
| 2263 | + // There are forums that need to be excluded | |
| 2264 | + $retval = $types[ $type ]; | |
| 2265 | + | |
| 2266 | + // Filter & return | |
| 1629 | 2267 | return apply_filters( 'bbp_exclude_forum_ids', $retval, $forum_ids, $type ); |
| 1630 | 2268 | } |
| 1631 | 2269 | |
| 1632 | 2270 | /** |
| 1633 | - * Adjusts topic and reply queries to exclude items that might be contained | |
| 1634 | - * inside hidden or private forums that the user does not have the capability | |
| 1635 | - * to view. | |
| 2271 | + * Adjusts forum, topic, and reply queries to exclude items that might be | |
| 2272 | + * contained inside hidden or private forums that the user does not have the | |
| 2273 | + * capability to view. | |
| 1636 | 2274 | * |
| 1637 | - * @since bbPress (r3291) | |
| 2275 | + * Doing it with an action allows us to trap all WP_Query's rather than needing | |
| 2276 | + * to hardcode this logic into each query. It also protects forum content for | |
| 2277 | + * plugins that might be doing their own queries. | |
| 1638 | 2278 | * |
| 2279 | + * @since 2.0.0 bbPress (r3291) | |
| 2280 | + * | |
| 1639 | 2281 | * @param WP_Query $posts_query |
| 1640 | 2282 | * |
| 1641 | - * @uses apply_filters() | |
| 1642 | - * @uses bbp_exclude_forum_ids() | |
| 1643 | - * @uses bbp_get_topic_post_type() | |
| 1644 | - * @uses bbp_get_reply_post_type() | |
| 1645 | 2283 | * @return WP_Query |
| 1646 | 2284 | */ |
| 1647 | -function bbp_pre_get_posts_exclude_forums( $posts_query ) { | |
| 2285 | +function bbp_pre_get_posts_normalize_forum_visibility( $posts_query = null ) { | |
| 1648 | 2286 | |
| 1649 | 2287 | // Bail if all forums are explicitly allowed |
| 1650 | - if ( true === apply_filters( 'bbp_include_all_forums', $posts_query ) ) | |
| 2288 | + if ( true === apply_filters( 'bbp_include_all_forums', false, $posts_query ) ) { | |
| 1651 | 2289 | return; |
| 2290 | + } | |
| 1652 | 2291 | |
| 1653 | 2292 | // Bail if $posts_query is not an object or of incorrect class |
| 1654 | - if ( !is_object( $posts_query ) || !is_a( $posts_query, 'WP_Query' ) ) | |
| 2293 | + if ( ! is_object( $posts_query ) || ! is_a( $posts_query, 'WP_Query' ) ) { | |
| 1655 | 2294 | return; |
| 2295 | + } | |
| 1656 | 2296 | |
| 1657 | - // Bail if filters are suppressed on this query | |
| 1658 | - if ( true == $posts_query->get( 'suppress_filters' ) ) | |
| 1659 | - return; | |
| 2297 | + // Get query post types array . | |
| 2298 | + $post_types = (array) $posts_query->get( 'post_type' ); | |
| 1660 | 2299 | |
| 1661 | - // Only exclude forums on bbPress queries | |
| 1662 | - switch ( $posts_query->get( 'post_type' ) ) { | |
| 2300 | + // Forums | |
| 2301 | + if ( bbp_get_forum_post_type() === implode( '', $post_types ) ) { | |
| 1663 | 2302 | |
| 1664 | - // Forums | |
| 1665 | - case bbp_get_forum_post_type() : | |
| 2303 | + // Prevent accidental wp-admin post_row override | |
| 2304 | + if ( is_admin() && isset( $_REQUEST['post_status'] ) ) { | |
| 2305 | + return; | |
| 2306 | + } | |
| 1666 | 2307 | |
| 1667 | - // Prevent accidental wp-admin post_row override | |
| 1668 | - if ( is_admin() && isset( $_REQUEST['post_status'] ) ) | |
| 1669 | - break; | |
| 2308 | + /** Default ***********************************************************/ | |
| 1670 | 2309 | |
| 1671 | - // Define local variable | |
| 1672 | - $status = array(); | |
| 2310 | + // Add all supported forum visibilities | |
| 2311 | + $posts_query->set( 'post_status', array_keys( bbp_get_forum_visibilities() ) ); | |
| 1673 | 2312 | |
| 1674 | - // All users can see published forums | |
| 1675 | - $status[] = bbp_get_public_status_id(); | |
| 2313 | + // Get forums to exclude | |
| 2314 | + $hidden_ids = bbp_exclude_forum_ids( 'array' ); | |
| 1676 | 2315 | |
| 1677 | - // Add bbp_get_private_status_id() if user is capable | |
| 1678 | - if ( current_user_can( 'read_private_forums' ) ) { | |
| 1679 | - $status[] = bbp_get_private_status_id(); | |
| 1680 | - } | |
| 2316 | + // Bail if no forums to exclude | |
| 2317 | + if ( empty( $hidden_ids ) ) { | |
| 2318 | + return; | |
| 2319 | + } | |
| 1681 | 2320 | |
| 1682 | - // Add bbp_get_hidden_status_id() if user is capable | |
| 1683 | - if ( current_user_can( 'read_hidden_forums' ) ) { | |
| 1684 | - $status[] = bbp_get_hidden_status_id(); | |
| 1685 | - } | |
| 2321 | + // Get any existing meta queries | |
| 2322 | + $not_in = $posts_query->get( 'post__not_in', array() ); | |
| 1686 | 2323 | |
| 1687 | - // Implode and add the statuses | |
| 1688 | - $posts_query->set( 'post_status', implode( ',', $status ) ); | |
| 2324 | + // Add our meta query to existing | |
| 2325 | + $not_in = array_unique( array_merge( $not_in, $hidden_ids ) ); | |
| 1689 | 2326 | |
| 1690 | - break; | |
| 2327 | + // Set the meta_query var | |
| 2328 | + $posts_query->set( 'post__not_in', $not_in ); | |
| 1691 | 2329 | |
| 1692 | - // Topics | |
| 1693 | - case bbp_get_topic_post_type() : | |
| 2330 | + // Some other post type besides Forums, Topics, or Replies | |
| 2331 | + } elseif ( ! array_diff( $post_types, bbp_get_post_types() ) ) { | |
| 1694 | 2332 | |
| 1695 | - // Replies | |
| 1696 | - case bbp_get_reply_post_type() : | |
| 2333 | + // Get forums to exclude | |
| 2334 | + $forum_ids = bbp_exclude_forum_ids( 'meta_query' ); | |
| 1697 | 2335 | |
| 1698 | - // Get forums to exclude | |
| 1699 | - $forum_ids = bbp_exclude_forum_ids( 'meta_query' ); | |
| 2336 | + // Bail if no forums to exclude | |
| 2337 | + if ( empty( $forum_ids ) ) { | |
| 2338 | + return; | |
| 2339 | + } | |
| 1700 | 2340 | |
| 1701 | - // Bail if no forums to exclude | |
| 1702 | - if ( empty( $forum_ids ) ) | |
| 1703 | - return; | |
| 2341 | + // Get any existing meta queries | |
| 2342 | + $meta_query = (array) $posts_query->get( 'meta_query', array() ); | |
| 1704 | 2343 | |
| 1705 | - // Get any existing meta queries | |
| 1706 | - $meta_query = $posts_query->get( 'meta_query' ); | |
| 2344 | + // Add our meta query to existing | |
| 2345 | + $meta_query[] = $forum_ids; | |
| 1707 | 2346 | |
| 1708 | - // Add our meta query to existing | |
| 1709 | - $meta_query[] = $forum_ids; | |
| 1710 | - | |
| 1711 | - // Set the meta_query var | |
| 1712 | - $posts_query->set( 'meta_query', $meta_query ); | |
| 1713 | - | |
| 1714 | - break; | |
| 2347 | + // Set the meta_query var | |
| 2348 | + $posts_query->set( 'meta_query', $meta_query ); | |
| 1715 | 2349 | } |
| 1716 | 2350 | } |
| 1717 | 2351 | |
| 1718 | 2352 | /** |
| @@ -1719,20 +2353,17 @@ | ||
| 1719 | 2353 | * Returns the forum's topic ids |
| 1720 | 2354 | * |
| 1721 | 2355 | * Only topics with published and closed statuses are returned |
| 1722 | 2356 | * |
| 1723 | - * @since bbPress (r2908) | |
| 2357 | + * @since 2.0.0 bbPress (r2908) | |
| 1724 | 2358 | * |
| 1725 | 2359 | * @param int $forum_id Forum id |
| 1726 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 1727 | - * @uses bbp_get_public_child_ids() To get the topic ids | |
| 1728 | - * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids | |
| 1729 | - * and forum id | |
| 1730 | 2360 | */ |
| 1731 | 2361 | function bbp_forum_query_topic_ids( $forum_id ) { |
| 1732 | - $topic_ids = bbp_get_public_child_ids( $forum_id, bbp_get_topic_post_type() ); | |
| 2362 | + $topic_ids = bbp_get_public_child_ids( $forum_id, bbp_get_topic_post_type() ); | |
| 1733 | 2363 | |
| 1734 | - return apply_filters( 'bbp_forum_query_topic_ids', $topic_ids, $forum_id ); | |
| 2364 | + // Filter & return | |
| 2365 | + return (array) apply_filters( 'bbp_forum_query_topic_ids', $topic_ids, $forum_id ); | |
| 1735 | 2366 | } |
| 1736 | 2367 | |
| 1737 | 2368 | /** |
| 1738 | 2369 | * Returns the forum's subforum ids |
| @@ -1738,74 +2369,63 @@ | ||
| 1738 | 2369 | * Returns the forum's subforum ids |
| 1739 | 2370 | * |
| 1740 | 2371 | * Only forums with published status are returned |
| 1741 | 2372 | * |
| 1742 | - * @since bbPress (r2908) | |
| 2373 | + * @since 2.0.0 bbPress (r2908) | |
| 1743 | 2374 | * |
| 1744 | 2375 | * @param int $forum_id Forum id |
| 1745 | - * @uses bbp_get_forum_post_type() To get the forum post type | |
| 1746 | - * @uses bbp_get_public_child_ids() To get the forum ids | |
| 1747 | - * @uses apply_filters() Calls 'bbp_forum_query_subforum_ids' with the subforum | |
| 1748 | - * ids and forum id | |
| 1749 | 2376 | */ |
| 1750 | 2377 | function bbp_forum_query_subforum_ids( $forum_id ) { |
| 1751 | - $subforum_ids = bbp_get_public_child_ids( $forum_id, bbp_get_forum_post_type() ); | |
| 1752 | - //usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' ); | |
| 2378 | + $subforum_ids = bbp_get_all_child_ids( $forum_id, bbp_get_forum_post_type() ); | |
| 1753 | 2379 | |
| 1754 | - return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id ); | |
| 2380 | + // Filter & return | |
| 2381 | + return (array) apply_filters( 'bbp_forum_query_subforum_ids', $subforum_ids, $forum_id ); | |
| 1755 | 2382 | } |
| 1756 | 2383 | |
| 1757 | 2384 | /** |
| 1758 | - * Callback to sort forum ID's based on last active time | |
| 1759 | - * | |
| 1760 | - * @since bbPress (r3789) | |
| 1761 | - * @param int $a First forum ID to compare | |
| 1762 | - * @param int $b Second forum ID to compare | |
| 1763 | - * @return Position change based on sort | |
| 1764 | - */ | |
| 1765 | -function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) { | |
| 1766 | - $ta = get_post_meta( $a, '_bbp_last_active_time', true ); | |
| 1767 | - $tb = get_post_meta( $b, '_bbp_last_active_time', true ); | |
| 1768 | - return ( $ta < $tb ) ? -1 : 1; | |
| 1769 | -} | |
| 1770 | - | |
| 1771 | -/** | |
| 1772 | 2385 | * Returns the forum's last reply id |
| 1773 | 2386 | * |
| 1774 | - * @since bbPress (r2908) | |
| 2387 | + * @since 2.0.0 bbPress (r2908) | |
| 2388 | + * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects | |
| 1775 | 2389 | * |
| 1776 | - * @param int $forum_id Forum id | |
| 1777 | - * @param int $topic_ids Optional. Topic ids | |
| 1778 | - * @uses wp_cache_get() To check for cache and retrieve it | |
| 1779 | - * @uses bbp_forum_query_topic_ids() To get the forum's topic ids | |
| 1780 | - * @uses wpdb::prepare() To prepare the query | |
| 1781 | - * @uses wpdb::get_var() To execute the query and get the var back | |
| 1782 | - * @uses bbp_get_reply_post_type() To get the reply post type | |
| 1783 | - * @uses wp_cache_set() To set the cache for future use | |
| 1784 | - * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id | |
| 1785 | - * and forum id | |
| 2390 | + * @param int $forum_id Forum id. | |
| 2391 | + * @param int $topic_ids Optional. Topic ids. | |
| 1786 | 2392 | */ |
| 1787 | -function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) { | |
| 1788 | - global $wpdb; | |
| 2393 | +function bbp_forum_query_last_reply_id( $forum_id = 0, $topic_ids = 0 ) { | |
| 1789 | 2394 | |
| 1790 | - $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id'; | |
| 1791 | - $reply_id = (int) wp_cache_get( $cache_id, 'bbpress' ); | |
| 2395 | + // Validate forum | |
| 2396 | + $forum_id = bbp_get_forum_id( $forum_id ); | |
| 1792 | 2397 | |
| 1793 | - if ( empty( $reply_id ) ) { | |
| 2398 | + // Get topic ID's if none were passed | |
| 2399 | + if ( empty( $topic_ids ) ) { | |
| 2400 | + $topic_ids = bbp_forum_query_topic_ids( $forum_id ); | |
| 2401 | + } | |
| 1794 | 2402 | |
| 1795 | - if ( empty( $topic_ids ) ) { | |
| 1796 | - $topic_ids = bbp_forum_query_topic_ids( $forum_id ); | |
| 1797 | - } | |
| 2403 | + $query = new WP_Query( array( | |
| 2404 | + 'fields' => 'ids', | |
| 2405 | + 'suppress_filters' => true, | |
| 2406 | + 'post_parent__in' => $topic_ids, | |
| 2407 | + 'post_status' => bbp_get_public_status_id(), | |
| 2408 | + 'post_type' => bbp_get_reply_post_type(), | |
| 2409 | + 'posts_per_page' => 1, | |
| 2410 | + 'orderby' => array( | |
| 2411 | + 'post_date' => 'DESC', | |
| 2412 | + 'ID' => 'DESC' | |
| 2413 | + ), | |
| 1798 | 2414 | |
| 1799 | - if ( !empty( $topic_ids ) ) { | |
| 1800 | - $reply_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) ); | |
| 1801 | - wp_cache_set( $cache_id, $reply_id, 'bbpress' ); // May be (int) 0 | |
| 1802 | - } else { | |
| 1803 | - wp_cache_set( $cache_id, '0', 'bbpress' ); | |
| 1804 | - } | |
| 1805 | - } | |
| 2415 | + // Performance | |
| 2416 | + 'update_post_term_cache' => false, | |
| 2417 | + 'update_post_meta_cache' => false, | |
| 2418 | + 'ignore_sticky_posts' => true, | |
| 2419 | + 'no_found_rows' => true | |
| 2420 | + ) ); | |
| 1806 | 2421 | |
| 1807 | - return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id ); | |
| 2422 | + $reply_id = array_shift( $query->posts ); | |
| 2423 | + | |
| 2424 | + unset( $query ); | |
| 2425 | + | |
| 2426 | + // Filter & return | |
| 2427 | + return (int) apply_filters( 'bbp_forum_query_last_reply_id', $reply_id, $forum_id ); | |
| 1808 | 2428 | } |
| 1809 | 2429 | |
| 1810 | 2430 | /** Listeners *****************************************************************/ |
| 1811 | 2431 | |
| @@ -1812,30 +2432,20 @@ | ||
| 1812 | 2432 | /** |
| 1813 | 2433 | * Check if it's a hidden forum or a topic or reply of a hidden forum and if |
| 1814 | 2434 | * the user can't view it, then sets a 404 |
| 1815 | 2435 | * |
| 1816 | - * @since bbPress (r2996) | |
| 1817 | - * | |
| 1818 | - * @uses current_user_can() To check if the current user can read private forums | |
| 1819 | - * @uses is_singular() To check if it's a singular page | |
| 1820 | - * @uses bbp_get_forum_post_type() To get the forum post type | |
| 1821 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 1822 | - * @uses bbp_get_reply_post_type() TO get the reply post type | |
| 1823 | - * @uses bbp_get_topic_forum_id() To get the topic forum id | |
| 1824 | - * @uses bbp_get_reply_forum_id() To get the reply forum id | |
| 1825 | - * @uses bbp_is_forum_hidden() To check if the forum is hidden or not | |
| 1826 | - * @uses bbp_set_404() To set a 404 status | |
| 2436 | + * @since 2.0.0 bbPress (r2996) | |
| 1827 | 2437 | */ |
| 1828 | 2438 | function bbp_forum_enforce_hidden() { |
| 1829 | 2439 | |
| 1830 | 2440 | // Bail if not viewing a single item or if user has caps |
| 1831 | - if ( !is_singular() || is_super_admin() || current_user_can( 'read_hidden_forums' ) ) | |
| 2441 | + if ( ! is_singular() || bbp_is_user_keymaster() || current_user_can( 'read_hidden_forums' ) ) { | |
| 1832 | 2442 | return; |
| 2443 | + } | |
| 1833 | 2444 | |
| 1834 | - global $wp_query; | |
| 1835 | - | |
| 1836 | - // Define local variable | |
| 2445 | + // Define local variables | |
| 1837 | 2446 | $forum_id = 0; |
| 2447 | + $wp_query = bbp_get_wp_query(); | |
| 1838 | 2448 | |
| 1839 | 2449 | // Check post type |
| 1840 | 2450 | switch ( $wp_query->get( 'post_type' ) ) { |
| 1841 | 2451 | |
| @@ -1852,14 +2462,14 @@ | ||
| 1852 | 2462 | // Reply |
| 1853 | 2463 | case bbp_get_reply_post_type() : |
| 1854 | 2464 | $forum_id = bbp_get_reply_forum_id( $wp_query->post->ID ); |
| 1855 | 2465 | break; |
| 1856 | - | |
| 1857 | 2466 | } |
| 1858 | 2467 | |
| 1859 | 2468 | // If forum is explicitly hidden and user not capable, set 404 |
| 1860 | - if ( !empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) ) | |
| 1861 | - bbp_set_404(); | |
| 2469 | + if ( ! empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) { | |
| 2470 | + bbp_set_404( $wp_query ); | |
| 2471 | + } | |
| 1862 | 2472 | } |
| 1863 | 2473 | |
| 1864 | 2474 | /** |
| 1865 | 2475 | * Check if it's a private forum or a topic or reply of a private forum and if |
| @@ -1864,30 +2474,20 @@ | ||
| 1864 | 2474 | /** |
| 1865 | 2475 | * Check if it's a private forum or a topic or reply of a private forum and if |
| 1866 | 2476 | * the user can't view it, then sets a 404 |
| 1867 | 2477 | * |
| 1868 | - * @since bbPress (r2996) | |
| 1869 | - * | |
| 1870 | - * @uses current_user_can() To check if the current user can read private forums | |
| 1871 | - * @uses is_singular() To check if it's a singular page | |
| 1872 | - * @uses bbp_get_forum_post_type() To get the forum post type | |
| 1873 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 1874 | - * @uses bbp_get_reply_post_type() TO get the reply post type | |
| 1875 | - * @uses bbp_get_topic_forum_id() To get the topic forum id | |
| 1876 | - * @uses bbp_get_reply_forum_id() To get the reply forum id | |
| 1877 | - * @uses bbp_is_forum_private() To check if the forum is private or not | |
| 1878 | - * @uses bbp_set_404() To set a 404 status | |
| 2478 | + * @since 2.0.0 bbPress (r2996) | |
| 1879 | 2479 | */ |
| 1880 | 2480 | function bbp_forum_enforce_private() { |
| 1881 | 2481 | |
| 1882 | 2482 | // Bail if not viewing a single item or if user has caps |
| 1883 | - if ( !is_singular() || is_super_admin() || current_user_can( 'read_private_forums' ) ) | |
| 2483 | + if ( ! is_singular() || bbp_is_user_keymaster() || current_user_can( 'read_private_forums' ) ) { | |
| 1884 | 2484 | return; |
| 2485 | + } | |
| 1885 | 2486 | |
| 1886 | - global $wp_query; | |
| 1887 | - | |
| 1888 | - // Define local variable | |
| 2487 | + // Define local variables | |
| 1889 | 2488 | $forum_id = 0; |
| 2489 | + $wp_query = bbp_get_wp_query(); | |
| 1890 | 2490 | |
| 1891 | 2491 | // Check post type |
| 1892 | 2492 | switch ( $wp_query->get( 'post_type' ) ) { |
| 1893 | 2493 | |
| @@ -1908,35 +2508,30 @@ | ||
| 1908 | 2508 | |
| 1909 | 2509 | } |
| 1910 | 2510 | |
| 1911 | 2511 | // If forum is explicitly hidden and user not capable, set 404 |
| 1912 | - if ( !empty( $forum_id ) && bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) ) | |
| 1913 | - bbp_set_404(); | |
| 2512 | + if ( ! empty( $forum_id ) && bbp_is_forum_private( $forum_id ) && ! current_user_can( 'read_forum', $forum_id ) ) { | |
| 2513 | + bbp_set_404( $wp_query ); | |
| 2514 | + } | |
| 1914 | 2515 | } |
| 1915 | 2516 | |
| 1916 | 2517 | /** Permissions ***************************************************************/ |
| 1917 | 2518 | |
| 1918 | 2519 | /** |
| 1919 | - * Redirect if unathorized user is attempting to edit a forum | |
| 1920 | - * | |
| 1921 | - * @since bbPress (r3607) | |
| 2520 | + * Redirect if unauthorized user is attempting to edit a forum | |
| 1922 | 2521 | * |
| 1923 | - * @uses bbp_is_forum_edit() | |
| 1924 | - * @uses current_user_can() | |
| 1925 | - * @uses bbp_get_forum_id() | |
| 1926 | - * @uses wp_safe_redirect() | |
| 1927 | - * @uses bbp_get_forum_permalink() | |
| 2522 | + * @since 2.1.0 bbPress (r3607) | |
| 1928 | 2523 | */ |
| 1929 | 2524 | function bbp_check_forum_edit() { |
| 1930 | 2525 | |
| 1931 | 2526 | // Bail if not editing a topic |
| 1932 | - if ( !bbp_is_forum_edit() ) | |
| 2527 | + if ( ! bbp_is_forum_edit() ) { | |
| 1933 | 2528 | return; |
| 2529 | + } | |
| 1934 | 2530 | |
| 1935 | 2531 | // User cannot edit topic, so redirect back to reply |
| 1936 | - if ( !current_user_can( 'edit_forum', bbp_get_forum_id() ) ) { | |
| 1937 | - wp_safe_redirect( bbp_get_forum_permalink() ); | |
| 1938 | - exit(); | |
| 2532 | + if ( ! current_user_can( 'edit_forum', bbp_get_forum_id() ) ) { | |
| 2533 | + bbp_redirect( bbp_get_forum_permalink() ); | |
| 1939 | 2534 | } |
| 1940 | 2535 | } |
| 1941 | 2536 | |
| 1942 | 2537 | /** |
| @@ -1941,17 +2536,11 @@ | ||
| 1941 | 2536 | |
| 1942 | 2537 | /** |
| 1943 | 2538 | * Delete all topics (and their replies) for a specific forum ID |
| 1944 | 2539 | * |
| 1945 | - * @since bbPress (r3668) | |
| 2540 | + * @since 2.1.0 bbPress (r3668) | |
| 1946 | 2541 | * |
| 1947 | 2542 | * @param int $forum_id |
| 1948 | - * @uses bbp_get_forum_id() To validate the forum ID | |
| 1949 | - * @uses bbp_is_forum() To make sure it's a forum | |
| 1950 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 1951 | - * @uses bbp_topics() To make sure there are topics to loop through | |
| 1952 | - * @uses wp_trash_post() To trash the post | |
| 1953 | - * @uses update_post_meta() To update the forum meta of trashed topics | |
| 1954 | 2543 | * @return If forum is not valid |
| 1955 | 2544 | */ |
| 1956 | 2545 | function bbp_delete_forum_topics( $forum_id = 0 ) { |
| 1957 | 2546 | |
| @@ -1956,21 +2545,33 @@ | ||
| 1956 | 2545 | function bbp_delete_forum_topics( $forum_id = 0 ) { |
| 1957 | 2546 | |
| 1958 | 2547 | // Validate forum ID |
| 1959 | 2548 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 1960 | - if ( empty( $forum_id ) ) | |
| 2549 | + if ( empty( $forum_id ) ) { | |
| 1961 | 2550 | return; |
| 2551 | + } | |
| 1962 | 2552 | |
| 1963 | - // Forum is being permanently deleted, so its topics gotta go too | |
| 1964 | - if ( $topics = new WP_Query( array( | |
| 1965 | - 'suppress_filters' => true, | |
| 1966 | - 'post_type' => bbp_get_topic_post_type(), | |
| 1967 | - 'post_parent' => $forum_id, | |
| 1968 | - 'post_status' => 'any', | |
| 1969 | - 'posts_per_page' => -1, | |
| 1970 | - 'nopaging' => true, | |
| 1971 | - 'fields' => 'id=>parent' | |
| 1972 | - ) ) ) { | |
| 2553 | + // Forum is being permanently deleted, so its content has go too | |
| 2554 | + // Note that we get all post statuses here | |
| 2555 | + $topics = new WP_Query( array( | |
| 2556 | + 'fields' => 'id=>parent', | |
| 2557 | + 'post_type' => bbp_get_topic_post_type(), | |
| 2558 | + 'post_parent' => $forum_id, | |
| 2559 | + 'post_status' => array_keys( get_post_stati() ), | |
| 2560 | + 'posts_per_page' => -1, | |
| 2561 | + | |
| 2562 | + // Performance | |
| 2563 | + 'nopaging' => true, | |
| 2564 | + 'suppress_filters' => true, | |
| 2565 | + 'update_post_term_cache' => false, | |
| 2566 | + 'update_post_meta_cache' => false, | |
| 2567 | + 'ignore_sticky_posts' => true, | |
| 2568 | + 'no_found_rows' => true | |
| 2569 | + ) ); | |
| 2570 | + | |
| 2571 | + // Loop through and delete child topics. Topic replies will get deleted by | |
| 2572 | + // the bbp_delete_topic() action. | |
| 2573 | + if ( ! empty( $topics->posts ) ) { | |
| 1973 | 2574 | foreach ( $topics->posts as $topic ) { |
| 1974 | 2575 | wp_delete_post( $topic->ID, true ); |
| 1975 | 2576 | } |
| 1976 | 2577 | |
| @@ -1976,24 +2577,19 @@ | ||
| 1976 | 2577 | |
| 1977 | 2578 | // Reset the $post global |
| 1978 | 2579 | wp_reset_postdata(); |
| 1979 | 2580 | } |
| 2581 | + | |
| 2582 | + // Cleanup | |
| 2583 | + unset( $topics ); | |
| 1980 | 2584 | } |
| 1981 | 2585 | |
| 1982 | 2586 | /** |
| 1983 | 2587 | * Trash all topics inside a forum |
| 1984 | - * | |
| 1985 | - * @since bbPress (r3668) | |
| 1986 | 2588 | * |
| 2589 | + * @since 2.1.0 bbPress (r3668) | |
| 2590 | + * | |
| 1987 | 2591 | * @param int $forum_id |
| 1988 | - * @uses bbp_get_forum_id() To validate the forum ID | |
| 1989 | - * @uses bbp_is_forum() To make sure it's a forum | |
| 1990 | - * @uses bbp_get_public_status_id() To return public post status | |
| 1991 | - * @uses bbp_get_closed_status_id() To return closed post status | |
| 1992 | - * @uses bbp_get_pending_status_id() To return pending post status | |
| 1993 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 1994 | - * @uses wp_trash_post() To trash the post | |
| 1995 | - * @uses update_post_meta() To update the forum meta of trashed topics | |
| 1996 | 2592 | * @return If forum is not valid |
| 1997 | 2593 | */ |
| 1998 | 2594 | function bbp_trash_forum_topics( $forum_id = 0 ) { |
| 1999 | 2595 | |
| @@ -1998,28 +2594,39 @@ | ||
| 1998 | 2594 | function bbp_trash_forum_topics( $forum_id = 0 ) { |
| 1999 | 2595 | |
| 2000 | 2596 | // Validate forum ID |
| 2001 | 2597 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 2002 | - if ( empty( $forum_id ) ) | |
| 2598 | + if ( empty( $forum_id ) ) { | |
| 2003 | 2599 | return; |
| 2600 | + } | |
| 2004 | 2601 | |
| 2005 | 2602 | // Allowed post statuses to pre-trash |
| 2006 | - $post_stati = join( ',', array( | |
| 2603 | + $post_stati = array( | |
| 2007 | 2604 | bbp_get_public_status_id(), |
| 2008 | 2605 | bbp_get_closed_status_id(), |
| 2009 | 2606 | bbp_get_pending_status_id() |
| 2607 | + ); | |
| 2608 | + | |
| 2609 | + // Forum is being trashed, so its topics (and replies) are trashed too | |
| 2610 | + $topics = new WP_Query( array( | |
| 2611 | + 'fields' => 'id=>parent', | |
| 2612 | + 'post_type' => bbp_get_topic_post_type(), | |
| 2613 | + 'post_parent' => $forum_id, | |
| 2614 | + 'post_status' => $post_stati, | |
| 2615 | + 'posts_per_page' => -1, | |
| 2616 | + | |
| 2617 | + // Performance | |
| 2618 | + 'nopaging' => true, | |
| 2619 | + 'suppress_filters' => true, | |
| 2620 | + 'update_post_term_cache' => false, | |
| 2621 | + 'update_post_meta_cache' => false, | |
| 2622 | + 'ignore_sticky_posts' => true, | |
| 2623 | + 'no_found_rows' => true | |
| 2010 | 2624 | ) ); |
| 2011 | 2625 | |
| 2012 | - // Forum is being trashed, so its topics are trashed too | |
| 2013 | - if ( $topics = new WP_Query( array( | |
| 2014 | - 'suppress_filters' => true, | |
| 2015 | - 'post_type' => bbp_get_topic_post_type(), | |
| 2016 | - 'post_parent' => $forum_id, | |
| 2017 | - 'post_status' => $post_stati, | |
| 2018 | - 'posts_per_page' => -1, | |
| 2019 | - 'nopaging' => true, | |
| 2020 | - 'fields' => 'id=>parent' | |
| 2021 | - ) ) ) { | |
| 2626 | + // Loop through and trash child topics. Topic replies will get trashed by | |
| 2627 | + // the bbp_trash_topic() action. | |
| 2628 | + if ( ! empty( $topics->posts ) ) { | |
| 2022 | 2629 | |
| 2023 | 2630 | // Prevent debug notices |
| 2024 | 2631 | $pre_trashed_topics = array(); |
| 2025 | 2632 | |
| @@ -2036,20 +2643,19 @@ | ||
| 2036 | 2643 | |
| 2037 | 2644 | // Reset the $post global |
| 2038 | 2645 | wp_reset_postdata(); |
| 2039 | 2646 | } |
| 2647 | + | |
| 2648 | + // Cleanup | |
| 2649 | + unset( $topics ); | |
| 2040 | 2650 | } |
| 2041 | 2651 | |
| 2042 | 2652 | /** |
| 2043 | - * Trash all topics inside a forum | |
| 2653 | + * Untrash all topics inside a forum | |
| 2044 | 2654 | * |
| 2045 | - * @since bbPress (r3668) | |
| 2655 | + * @since 2.1.0 bbPress (r3668) | |
| 2046 | 2656 | * |
| 2047 | 2657 | * @param int $forum_id |
| 2048 | - * @uses bbp_get_forum_id() To validate the forum ID | |
| 2049 | - * @uses bbp_is_forum() To make sure it's a forum | |
| 2050 | - * @uses get_post_meta() To update the forum meta of trashed topics | |
| 2051 | - * @uses wp_untrash_post() To trash the post | |
| 2052 | 2658 | * @return If forum is not valid |
| 2053 | 2659 | */ |
| 2054 | 2660 | function bbp_untrash_forum_topics( $forum_id = 0 ) { |
| 2055 | 2661 | |
| @@ -2055,20 +2661,22 @@ | ||
| 2055 | 2661 | |
| 2056 | 2662 | // Validate forum ID |
| 2057 | 2663 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 2058 | 2664 | |
| 2059 | - if ( empty( $forum_id ) ) | |
| 2665 | + if ( empty( $forum_id ) ) { | |
| 2060 | 2666 | return; |
| 2667 | + } | |
| 2061 | 2668 | |
| 2062 | 2669 | // Get the topics that were not previously trashed |
| 2063 | 2670 | $pre_trashed_topics = get_post_meta( $forum_id, '_bbp_pre_trashed_topics', true ); |
| 2064 | 2671 | |
| 2065 | 2672 | // There are topics to untrash |
| 2066 | - if ( !empty( $pre_trashed_topics ) ) { | |
| 2673 | + if ( ! empty( $pre_trashed_topics ) ) { | |
| 2067 | 2674 | |
| 2068 | 2675 | // Maybe reverse the trashed topics array |
| 2069 | - if ( is_array( $pre_trashed_topics ) ) | |
| 2676 | + if ( is_array( $pre_trashed_topics ) ) { | |
| 2070 | 2677 | $pre_trashed_topics = array_reverse( $pre_trashed_topics ); |
| 2678 | + } | |
| 2071 | 2679 | |
| 2072 | 2680 | // Loop through topics |
| 2073 | 2681 | foreach ( (array) $pre_trashed_topics as $topic ) { |
| 2074 | 2682 | wp_untrash_post( $topic ); |
| @@ -2084,18 +2692,16 @@ | ||
| 2084 | 2692 | * This function is supplemental to the actual forum deletion which is |
| 2085 | 2693 | * handled by WordPress core API functions. It is used to clean up after |
| 2086 | 2694 | * a forum that is being deleted. |
| 2087 | 2695 | * |
| 2088 | - * @since bbPress (r3668) | |
| 2089 | - * @uses bbp_get_forum_id() To get the forum id | |
| 2090 | - * @uses bbp_is_forum() To check if the passed id is a forum | |
| 2091 | - * @uses do_action() Calls 'bbp_delete_forum' with the forum id | |
| 2696 | + * @since 2.1.0 bbPress (r3668) | |
| 2092 | 2697 | */ |
| 2093 | 2698 | function bbp_delete_forum( $forum_id = 0 ) { |
| 2094 | 2699 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 2095 | 2700 | |
| 2096 | - if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) | |
| 2701 | + if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { | |
| 2097 | 2702 | return false; |
| 2703 | + } | |
| 2098 | 2704 | |
| 2099 | 2705 | do_action( 'bbp_delete_forum', $forum_id ); |
| 2100 | 2706 | } |
| 2101 | 2707 | |
| @@ -2105,18 +2711,16 @@ | ||
| 2105 | 2711 | * This function is supplemental to the actual forum being trashed which is |
| 2106 | 2712 | * handled by WordPress core API functions. It is used to clean up after |
| 2107 | 2713 | * a forum that is being trashed. |
| 2108 | 2714 | * |
| 2109 | - * @since bbPress (r3668) | |
| 2110 | - * @uses bbp_get_forum_id() To get the forum id | |
| 2111 | - * @uses bbp_is_forum() To check if the passed id is a forum | |
| 2112 | - * @uses do_action() Calls 'bbp_trash_forum' with the forum id | |
| 2715 | + * @since 2.1.0 bbPress (r3668) | |
| 2113 | 2716 | */ |
| 2114 | 2717 | function bbp_trash_forum( $forum_id = 0 ) { |
| 2115 | 2718 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 2116 | 2719 | |
| 2117 | - if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) | |
| 2720 | + if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { | |
| 2118 | 2721 | return false; |
| 2722 | + } | |
| 2119 | 2723 | |
| 2120 | 2724 | do_action( 'bbp_trash_forum', $forum_id ); |
| 2121 | 2725 | } |
| 2122 | 2726 | |
| @@ -2122,18 +2726,16 @@ | ||
| 2122 | 2726 | |
| 2123 | 2727 | /** |
| 2124 | 2728 | * Called before untrashing a forum |
| 2125 | 2729 | * |
| 2126 | - * @since bbPress (r3668) | |
| 2127 | - * @uses bbp_get_forum_id() To get the forum id | |
| 2128 | - * @uses bbp_is_forum() To check if the passed id is a forum | |
| 2129 | - * @uses do_action() Calls 'bbp_untrash_forum' with the forum id | |
| 2730 | + * @since 2.1.0 bbPress (r3668) | |
| 2130 | 2731 | */ |
| 2131 | 2732 | function bbp_untrash_forum( $forum_id = 0 ) { |
| 2132 | 2733 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 2133 | 2734 | |
| 2134 | - if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) | |
| 2735 | + if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { | |
| 2135 | 2736 | return false; |
| 2737 | + } | |
| 2136 | 2738 | |
| 2137 | 2739 | do_action( 'bbp_untrash_forum', $forum_id ); |
| 2138 | 2740 | } |
| 2139 | 2741 | |
| @@ -2141,18 +2743,20 @@ | ||
| 2141 | 2743 | |
| 2142 | 2744 | /** |
| 2143 | 2745 | * Called after deleting a forum |
| 2144 | 2746 | * |
| 2145 | - * @since bbPress (r3668) | |
| 2146 | - * @uses bbp_get_forum_id() To get the forum id | |
| 2147 | - * @uses bbp_is_forum() To check if the passed id is a forum | |
| 2148 | - * @uses do_action() Calls 'bbp_deleted_forum' with the forum id | |
| 2747 | + * Try not to use this action. All meta & taxonomy terms have already been | |
| 2748 | + * deleted, making them impossible to use. | |
| 2749 | + * | |
| 2750 | + * @since 2.1.0 bbPress (r3668) | |
| 2751 | + * @since 2.6.0 bbPress (r6526) Not recommend for usage | |
| 2149 | 2752 | */ |
| 2150 | 2753 | function bbp_deleted_forum( $forum_id = 0 ) { |
| 2151 | 2754 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 2152 | 2755 | |
| 2153 | - if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) | |
| 2756 | + if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { | |
| 2154 | 2757 | return false; |
| 2758 | + } | |
| 2155 | 2759 | |
| 2156 | 2760 | do_action( 'bbp_deleted_forum', $forum_id ); |
| 2157 | 2761 | } |
| 2158 | 2762 | |
| @@ -2158,18 +2762,16 @@ | ||
| 2158 | 2762 | |
| 2159 | 2763 | /** |
| 2160 | 2764 | * Called after trashing a forum |
| 2161 | 2765 | * |
| 2162 | - * @since bbPress (r3668) | |
| 2163 | - * @uses bbp_get_forum_id() To get the forum id | |
| 2164 | - * @uses bbp_is_forum() To check if the passed id is a forum | |
| 2165 | - * @uses do_action() Calls 'bbp_trashed_forum' with the forum id | |
| 2766 | + * @since 2.1.0 bbPress (r3668) | |
| 2166 | 2767 | */ |
| 2167 | 2768 | function bbp_trashed_forum( $forum_id = 0 ) { |
| 2168 | 2769 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 2169 | 2770 | |
| 2170 | - if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) | |
| 2771 | + if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { | |
| 2171 | 2772 | return false; |
| 2773 | + } | |
| 2172 | 2774 | |
| 2173 | 2775 | do_action( 'bbp_trashed_forum', $forum_id ); |
| 2174 | 2776 | } |
| 2175 | 2777 | |
| @@ -2175,17 +2777,15 @@ | ||
| 2175 | 2777 | |
| 2176 | 2778 | /** |
| 2177 | 2779 | * Called after untrashing a forum |
| 2178 | 2780 | * |
| 2179 | - * @since bbPress (r3668) | |
| 2180 | - * @uses bbp_get_forum_id() To get the forum id | |
| 2181 | - * @uses bbp_is_forum() To check if the passed id is a forum | |
| 2182 | - * @uses do_action() Calls 'bbp_untrashed_forum' with the forum id | |
| 2781 | + * @since 2.1.0 bbPress (r3668) | |
| 2183 | 2782 | */ |
| 2184 | 2783 | function bbp_untrashed_forum( $forum_id = 0 ) { |
| 2185 | 2784 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 2186 | 2785 | |
| 2187 | - if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) | |
| 2786 | + if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) { | |
| 2188 | 2787 | return false; |
| 2788 | + } | |
| 2189 | 2789 | |
| 2190 | 2790 | do_action( 'bbp_untrashed_forum', $forum_id ); |
| 2191 | 2791 | } |