| @@ -10,245 +10,67 @@ | ||
| 10 | 10 | * @subpackage Functions |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | // Exit if accessed directly |
| 14 | -if ( !defined( 'ABSPATH' ) ) exit; | |
| 14 | +defined( 'ABSPATH' ) || exit; | |
| 15 | 15 | |
| 16 | -/** Formatting ****************************************************************/ | |
| 17 | - | |
| 18 | 16 | /** |
| 19 | - * A bbPress specific method of formatting numeric values | |
| 17 | + * Return array of bbPress registered post types | |
| 20 | 18 | * |
| 21 | - * @since bbPress (r2486) | |
| 19 | + * @since 2.6.0 bbPress (r6813) | |
| 22 | 20 | * |
| 23 | - * @param string $number Number to format | |
| 24 | - * @param string $decimals Optional. Display decimals | |
| 25 | - * @uses apply_filters() Calls 'bbp_number_format' with the formatted values, | |
| 26 | - * number and display decimals bool | |
| 27 | - * @return string Formatted string | |
| 28 | - */ | |
| 29 | -function bbp_number_format( $number = 0, $decimals = false, $dec_point = '.', $thousands_sep = ',' ) { | |
| 30 | - | |
| 31 | - // If empty, set $number to (int) 0 | |
| 32 | - if ( ! is_numeric( $number ) ) | |
| 33 | - $number = 0; | |
| 34 | - | |
| 35 | - return apply_filters( 'bbp_number_format', number_format( $number, $decimals, $dec_point, $thousands_sep ), $number, $decimals, $dec_point, $thousands_sep ); | |
| 36 | -} | |
| 37 | - | |
| 38 | -/** | |
| 39 | - * A bbPress specific method of formatting numeric values | |
| 21 | + * @param array $args Array of arguments to pass into `get_post_types()` | |
| 40 | 22 | * |
| 41 | - * @since bbPress (r3857) | |
| 42 | - * | |
| 43 | - * @param string $number Number to format | |
| 44 | - * @param string $decimals Optional. Display decimals | |
| 45 | - * @uses apply_filters() Calls 'bbp_number_format' with the formatted values, | |
| 46 | - * number and display decimals bool | |
| 47 | - * @return string Formatted string | |
| 23 | + * @return array | |
| 48 | 24 | */ |
| 49 | -function bbp_number_format_i18n( $number = 0, $decimals = false ) { | |
| 25 | +function bbp_get_post_types( $args = array() ) { | |
| 50 | 26 | |
| 51 | - // If empty, set $number to (int) 0 | |
| 52 | - if ( ! is_numeric( $number ) ) | |
| 53 | - $number = 0; | |
| 27 | + // Parse args | |
| 28 | + $r = bbp_parse_args( $args, array( | |
| 29 | + 'source' => 'bbpress' | |
| 30 | + ), 'get_post_types' ); | |
| 54 | 31 | |
| 55 | - return apply_filters( 'bbp_number_format_i18n', number_format_i18n( $number, $decimals ), $number, $decimals ); | |
| 32 | + // Return post types | |
| 33 | + return get_post_types( $r ); | |
| 56 | 34 | } |
| 57 | 35 | |
| 58 | -/** | |
| 59 | - * Convert time supplied from database query into specified date format. | |
| 60 | - * | |
| 61 | - * @since bbPress (r2455) | |
| 62 | - * | |
| 63 | - * @param int|object $post Optional. Default is global post object. A post_id or | |
| 64 | - * post object | |
| 65 | - * @param string $d Optional. Default is 'U'. Either 'G', 'U', or php date | |
| 66 | - * format | |
| 67 | - * @param bool $translate Optional. Default is false. Whether to translate the | |
| 68 | - * result | |
| 69 | - * @uses mysql2date() To convert the format | |
| 70 | - * @uses apply_filters() Calls 'bbp_convert_date' with the time, date format | |
| 71 | - * and translate bool | |
| 72 | - * @return string Returns timestamp | |
| 73 | - */ | |
| 74 | -function bbp_convert_date( $time, $d = 'U', $translate = false ) { | |
| 75 | - $time = mysql2date( $d, $time, $translate ); | |
| 36 | +/** URLs **********************************************************************/ | |
| 76 | 37 | |
| 77 | - return apply_filters( 'bbp_convert_date', $time, $d, $translate ); | |
| 78 | -} | |
| 79 | - | |
| 80 | 38 | /** |
| 81 | - * Output formatted time to display human readable time difference. | |
| 39 | + * Return the unescaped redirect_to request value | |
| 82 | 40 | * |
| 83 | - * @since bbPress (r2544) | |
| 41 | + * @bbPress (r4655) | |
| 84 | 42 | * |
| 85 | - * @param string $older_date Unix timestamp from which the difference begins. | |
| 86 | - * @param string $newer_date Optional. Unix timestamp from which the | |
| 87 | - * difference ends. False for current time. | |
| 88 | - * @uses bbp_get_time_since() To get the formatted time | |
| 43 | + * @return string The URL to redirect to, if set | |
| 89 | 44 | */ |
| 90 | -function bbp_time_since( $older_date, $newer_date = false ) { | |
| 91 | - echo bbp_get_time_since( $older_date, $newer_date = false ); | |
| 92 | -} | |
| 93 | - /** | |
| 94 | - * Return formatted time to display human readable time difference. | |
| 95 | - * | |
| 96 | - * @since bbPress (r2544) | |
| 97 | - * | |
| 98 | - * @param string $older_date Unix timestamp from which the difference begins. | |
| 99 | - * @param string $newer_date Optional. Unix timestamp from which the | |
| 100 | - * difference ends. False for current time. | |
| 101 | - * @uses current_time() To get the current time in mysql format | |
| 102 | - * @uses human_time_diff() To get the time differene in since format | |
| 103 | - * @uses apply_filters() Calls 'bbp_get_time_since' with the time | |
| 104 | - * difference and time | |
| 105 | - * @return string Formatted time | |
| 106 | - */ | |
| 107 | - function bbp_get_time_since( $older_date, $newer_date = false ) { | |
| 108 | - | |
| 109 | - // Setup the strings | |
| 110 | - $unknown_text = apply_filters( 'bbp_core_time_since_unknown_text', __( 'sometime', 'bbpress' ) ); | |
| 111 | - $right_now_text = apply_filters( 'bbp_core_time_since_right_now_text', __( 'right now', 'bbpress' ) ); | |
| 112 | - $ago_text = apply_filters( 'bbp_core_time_since_ago_text', __( '%s ago', 'bbpress' ) ); | |
| 45 | +function bbp_get_redirect_to() { | |
| 113 | 46 | |
| 114 | - // array of time period chunks | |
| 115 | - $chunks = array( | |
| 116 | - array( 60 * 60 * 24 * 365 , __( 'year', 'bbpress' ), __( 'years', 'bbpress' ) ), | |
| 117 | - array( 60 * 60 * 24 * 30 , __( 'month', 'bbpress' ), __( 'months', 'bbpress' ) ), | |
| 118 | - array( 60 * 60 * 24 * 7, __( 'week', 'bbpress' ), __( 'weeks', 'bbpress' ) ), | |
| 119 | - array( 60 * 60 * 24 , __( 'day', 'bbpress' ), __( 'days', 'bbpress' ) ), | |
| 120 | - array( 60 * 60 , __( 'hour', 'bbpress' ), __( 'hours', 'bbpress' ) ), | |
| 121 | - array( 60 , __( 'minute', 'bbpress' ), __( 'minutes', 'bbpress' ) ), | |
| 122 | - array( 1, __( 'second', 'bbpress' ), __( 'seconds', 'bbpress' ) ) | |
| 123 | - ); | |
| 47 | + // Check 'redirect_to' request parameter | |
| 48 | + $retval = ! empty( $_REQUEST['redirect_to'] ) | |
| 49 | + ? $_REQUEST['redirect_to'] | |
| 50 | + : ''; | |
| 124 | 51 | |
| 125 | - if ( !empty( $older_date ) && !is_numeric( $older_date ) ) { | |
| 126 | - $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) ); | |
| 127 | - $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) ); | |
| 128 | - $older_date = gmmktime( (int) $time_chunks[1], (int) $time_chunks[2], (int) $time_chunks[3], (int) $date_chunks[1], (int) $date_chunks[2], (int) $date_chunks[0] ); | |
| 129 | - } | |
| 130 | - | |
| 131 | - // $newer_date will equal false if we want to know the time elapsed | |
| 132 | - // between a date and the current time. $newer_date will have a value if | |
| 133 | - // we want to work out time elapsed between two known dates. | |
| 134 | - $newer_date = ( !$newer_date ) ? strtotime( current_time( 'mysql' ) ) : $newer_date; | |
| 135 | - | |
| 136 | - // Difference in seconds | |
| 137 | - $since = $newer_date - $older_date; | |
| 138 | - | |
| 139 | - // Something went wrong with date calculation and we ended up with a negative date. | |
| 140 | - if ( 0 > $since ) { | |
| 141 | - $output = $unknown_text; | |
| 142 | - | |
| 143 | - // We only want to output two chunks of time here, eg: | |
| 144 | - // x years, xx months | |
| 145 | - // x days, xx hours | |
| 146 | - // so there's only two bits of calculation below: | |
| 147 | - } else { | |
| 148 | - | |
| 149 | - // Step one: the first chunk | |
| 150 | - for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) { | |
| 151 | - $seconds = $chunks[$i][0]; | |
| 152 | - | |
| 153 | - // Finding the biggest chunk (if the chunk fits, break) | |
| 154 | - $count = floor( $since / $seconds ); | |
| 155 | - if ( 0 != $count ) { | |
| 156 | - break; | |
| 157 | - } | |
| 158 | - } | |
| 159 | - | |
| 160 | - // If $i iterates all the way to $j, then the event happened 0 seconds ago | |
| 161 | - if ( !isset( $chunks[$i] ) ) { | |
| 162 | - $output = $right_now_text; | |
| 163 | - | |
| 164 | - } else { | |
| 165 | - | |
| 166 | - // Set output var | |
| 167 | - $output = ( 1 == $count ) ? '1 '. $chunks[$i][1] : $count . ' ' . $chunks[$i][2]; | |
| 168 | - | |
| 169 | - // Step two: the second chunk | |
| 170 | - if ( $i + 2 < $j ) { | |
| 171 | - $seconds2 = $chunks[$i + 1][0]; | |
| 172 | - $name2 = $chunks[$i + 1][1]; | |
| 173 | - $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ); | |
| 174 | - | |
| 175 | - // Add to output var | |
| 176 | - if ( 0 != $count2 ) { | |
| 177 | - $output .= ( 1 == $count2 ) ? _x( ',', 'Separator in time since', 'bbpress' ) . ' 1 '. $name2 : _x( ',', 'Separator in time since', 'bbpress' ) . ' ' . $count2 . ' ' . $chunks[$i + 1][2]; | |
| 178 | - } | |
| 179 | - } | |
| 180 | - | |
| 181 | - // No output, so happened right now | |
| 182 | - if ( ! (int) trim( $output ) ) { | |
| 183 | - $output = $right_now_text; | |
| 184 | - } | |
| 185 | - } | |
| 186 | - } | |
| 187 | - | |
| 188 | - // Append 'ago' to the end of time-since if not 'right now' | |
| 189 | - if ( $output != $right_now_text ) { | |
| 190 | - $output = sprintf( $ago_text, $output ); | |
| 191 | - } | |
| 192 | - | |
| 193 | - return apply_filters( 'bbp_get_time_since', $output, $older_date, $newer_date ); | |
| 194 | - } | |
| 195 | - | |
| 196 | -/** | |
| 197 | - * Formats the reason for editing the topic/reply. | |
| 198 | - * | |
| 199 | - * Does these things: | |
| 200 | - * - Trimming | |
| 201 | - * - Removing periods from the end of the string | |
| 202 | - * - Trimming again | |
| 203 | - * | |
| 204 | - * @since bbPress (r2782) | |
| 205 | - * | |
| 206 | - * @param int $topic_id Optional. Topic id | |
| 207 | - * @return string Status of topic | |
| 208 | - */ | |
| 209 | -function bbp_format_revision_reason( $reason = '' ) { | |
| 210 | - $reason = (string) $reason; | |
| 211 | - | |
| 212 | - // Format reason for proper display | |
| 213 | - if ( empty( $reason ) ) | |
| 214 | - return $reason; | |
| 215 | - | |
| 216 | - // Trimming | |
| 217 | - $reason = trim( $reason ); | |
| 218 | - | |
| 219 | - // We add our own full stop. | |
| 220 | - while ( substr( $reason, -1 ) == '.' ) | |
| 221 | - $reason = substr( $reason, 0, -1 ); | |
| 222 | - | |
| 223 | - // Trim again | |
| 224 | - $reason = trim( $reason ); | |
| 225 | - | |
| 226 | - return $reason; | |
| 52 | + // Filter & return | |
| 53 | + return apply_filters( 'bbp_get_redirect_to', $retval ); | |
| 227 | 54 | } |
| 228 | 55 | |
| 229 | -/** Misc **********************************************************************/ | |
| 230 | - | |
| 231 | 56 | /** |
| 232 | 57 | * Append 'view=all' to query string if it's already there from referer |
| 233 | 58 | * |
| 234 | - * @since bbPress (r3325) | |
| 59 | + * @since 2.0.0 bbPress (r3325) | |
| 235 | 60 | * |
| 236 | 61 | * @param string $original_link Original Link to be modified |
| 237 | 62 | * @param bool $force Override bbp_get_view_all() check |
| 238 | - * @uses current_user_can() To check if the current user can moderate | |
| 239 | - * @uses add_query_arg() To add args to the url | |
| 240 | - * @uses apply_filters() Calls 'bbp_add_view_all' with the link and original link | |
| 241 | 63 | * @return string The link with 'view=all' appended if necessary |
| 242 | 64 | */ |
| 243 | 65 | function bbp_add_view_all( $original_link = '', $force = false ) { |
| 244 | 66 | |
| 245 | 67 | // Are we appending the view=all vars? |
| 246 | - if ( bbp_get_view_all() || !empty( $force ) ) | |
| 247 | - $link = add_query_arg( array( 'view' => 'all' ), $original_link ); | |
| 248 | - else | |
| 249 | - $link = $original_link; | |
| 68 | + $link = ( bbp_get_view_all() || ! empty( $force ) ) | |
| 69 | + ? add_query_arg( array( 'view' => 'all' ), $original_link ) | |
| 70 | + : $original_link; | |
| 250 | 71 | |
| 72 | + // Filter & return | |
| 251 | 73 | return apply_filters( 'bbp_add_view_all', $link, $original_link ); |
| 252 | 74 | } |
| 253 | 75 | |
| 254 | 76 | /** |
| @@ -253,44 +75,47 @@ | ||
| 253 | 75 | |
| 254 | 76 | /** |
| 255 | 77 | * Remove 'view=all' from query string |
| 256 | 78 | * |
| 257 | - * @since bbPress (r3325) | |
| 79 | + * @since 2.0.0 bbPress (r3325) | |
| 258 | 80 | * |
| 259 | 81 | * @param string $original_link Original Link to be modified |
| 260 | - * @uses current_user_can() To check if the current user can moderate | |
| 261 | - * @uses add_query_arg() To add args to the url | |
| 262 | - * @uses apply_filters() Calls 'bbp_add_view_all' with the link and original link | |
| 263 | 82 | * @return string The link with 'view=all' appended if necessary |
| 264 | 83 | */ |
| 265 | 84 | function bbp_remove_view_all( $original_link = '' ) { |
| 266 | - return apply_filters( 'bbp_add_view_all', remove_query_arg( 'view', $original_link ), $original_link ); | |
| 85 | + | |
| 86 | + // Remove `view' argument | |
| 87 | + $link = remove_query_arg( 'view', $original_link ); | |
| 88 | + | |
| 89 | + // Filter & return | |
| 90 | + return apply_filters( 'bbp_remove_view_all', $link, $original_link ); | |
| 267 | 91 | } |
| 268 | 92 | |
| 269 | 93 | /** |
| 270 | - * If current user can and is vewing all topics/replies | |
| 94 | + * If current user can and is viewing all topics/replies | |
| 271 | 95 | * |
| 272 | - * @since bbPress (r3325) | |
| 96 | + * @since 2.0.0 bbPress (r3325) | |
| 273 | 97 | * |
| 274 | - * @uses current_user_can() To check if the current user can moderate | |
| 275 | - * @uses apply_filters() Calls 'bbp_get_view_all' with the link and original link | |
| 98 | + * @param string $cap Capability used to ensure user can view all | |
| 99 | + * | |
| 276 | 100 | * @return bool Whether current user can and is viewing all |
| 277 | 101 | */ |
| 278 | 102 | function bbp_get_view_all( $cap = 'moderate' ) { |
| 279 | - $retval = ( ( !empty( $_GET['view'] ) && ( 'all' == $_GET['view'] ) && current_user_can( $cap ) ) ); | |
| 280 | - return apply_filters( 'bbp_get_view_all', (bool) $retval ); | |
| 103 | + $retval = ( ( ! empty( $_GET['view'] ) && ( 'all' === $_GET['view'] ) && current_user_can( $cap ) ) ); | |
| 104 | + | |
| 105 | + // Filter & return | |
| 106 | + return (bool) apply_filters( 'bbp_get_view_all', (bool) $retval, $cap ); | |
| 281 | 107 | } |
| 282 | 108 | |
| 283 | 109 | /** |
| 284 | 110 | * Assist pagination by returning correct page number |
| 285 | 111 | * |
| 286 | - * @since bbPress (r2628) | |
| 112 | + * @since 2.0.0 bbPress (r2628) | |
| 287 | 113 | * |
| 288 | - * @uses get_query_var() To get the 'paged' value | |
| 289 | 114 | * @return int Current page number |
| 290 | 115 | */ |
| 291 | 116 | function bbp_get_paged() { |
| 292 | - global $wp_query; | |
| 117 | + $wp_query = bbp_get_wp_query(); | |
| 293 | 118 | |
| 294 | 119 | // Check the query var |
| 295 | 120 | if ( get_query_var( 'paged' ) ) { |
| 296 | 121 | $paged = get_query_var( 'paged' ); |
| @@ -295,21 +120,37 @@ | ||
| 295 | 120 | if ( get_query_var( 'paged' ) ) { |
| 296 | 121 | $paged = get_query_var( 'paged' ); |
| 297 | 122 | |
| 298 | 123 | // Check query paged |
| 299 | - } elseif ( !empty( $wp_query->query['paged'] ) ) { | |
| 124 | + } elseif ( ! empty( $wp_query->query['paged'] ) ) { | |
| 300 | 125 | $paged = $wp_query->query['paged']; |
| 301 | 126 | } |
| 302 | 127 | |
| 303 | 128 | // Paged found |
| 304 | - if ( !empty( $paged ) ) | |
| 129 | + if ( ! empty( $paged ) ) { | |
| 305 | 130 | return (int) $paged; |
| 131 | + } | |
| 306 | 132 | |
| 307 | 133 | // Default to first page |
| 308 | 134 | return 1; |
| 309 | 135 | } |
| 310 | 136 | |
| 137 | +/** Misc **********************************************************************/ | |
| 138 | + | |
| 311 | 139 | /** |
| 140 | + * Return the unique non-empty values of an array. | |
| 141 | + * | |
| 142 | + * @since 2.6.0 bbPress (r6481) | |
| 143 | + * | |
| 144 | + * @param array $array Array to get values of | |
| 145 | + * | |
| 146 | + * @return array | |
| 147 | + */ | |
| 148 | +function bbp_get_unique_array_values( $array = array() ) { | |
| 149 | + return array_unique( array_filter( array_values( $array ) ) ); | |
| 150 | +} | |
| 151 | + | |
| 152 | +/** | |
| 312 | 153 | * Fix post author id on post save |
| 313 | 154 | * |
| 314 | 155 | * When a logged in user changes the status of an anonymous reply or topic, or |
| 315 | 156 | * edits it, the post_author field is set to the logged in user's id. This |
| @@ -314,32 +155,31 @@ | ||
| 314 | 155 | * When a logged in user changes the status of an anonymous reply or topic, or |
| 315 | 156 | * edits it, the post_author field is set to the logged in user's id. This |
| 316 | 157 | * function fixes that. |
| 317 | 158 | * |
| 318 | - * @since bbPress (r2734) | |
| 159 | + * @since 2.0.0 bbPress (r2734) | |
| 319 | 160 | * |
| 320 | 161 | * @param array $data Post data |
| 321 | 162 | * @param array $postarr Original post array (includes post id) |
| 322 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 323 | - * @uses bbp_get_reply_post_type() To get the reply post type | |
| 324 | - * @uses bbp_is_topic_anonymous() To check if the topic is by an anonymous user | |
| 325 | - * @uses bbp_is_reply_anonymous() To check if the reply is by an anonymous user | |
| 326 | 163 | * @return array Data |
| 327 | 164 | */ |
| 328 | 165 | function bbp_fix_post_author( $data = array(), $postarr = array() ) { |
| 329 | 166 | |
| 330 | 167 | // Post is not being updated or the post_author is already 0, return |
| 331 | - if ( empty( $postarr['ID'] ) || empty( $data['post_author'] ) ) | |
| 168 | + if ( empty( $postarr['ID'] ) || empty( $data['post_author'] ) ) { | |
| 332 | 169 | return $data; |
| 170 | + } | |
| 333 | 171 | |
| 334 | 172 | // Post is not a topic or reply, return |
| 335 | - if ( !in_array( $data['post_type'], array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) ) | |
| 173 | + if ( ! in_array( $data['post_type'], array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ), true ) ) { | |
| 336 | 174 | return $data; |
| 175 | + } | |
| 337 | 176 | |
| 338 | 177 | // Is the post by an anonymous user? |
| 339 | - if ( ( bbp_get_topic_post_type() == $data['post_type'] && !bbp_is_topic_anonymous( $postarr['ID'] ) ) || | |
| 340 | - ( bbp_get_reply_post_type() == $data['post_type'] && !bbp_is_reply_anonymous( $postarr['ID'] ) ) ) | |
| 178 | + if ( ( bbp_get_topic_post_type() === $data['post_type'] && ! bbp_is_topic_anonymous( $postarr['ID'] ) ) || | |
| 179 | + ( bbp_get_reply_post_type() === $data['post_type'] && ! bbp_is_reply_anonymous( $postarr['ID'] ) ) ) { | |
| 341 | 180 | return $data; |
| 181 | + } | |
| 342 | 182 | |
| 343 | 183 | // The post is being updated. It is a topic or a reply and is written by an anonymous user. |
| 344 | 184 | // Set the post_author back to 0 |
| 345 | 185 | $data['post_author'] = 0; |
| @@ -347,60 +187,108 @@ | ||
| 347 | 187 | return $data; |
| 348 | 188 | } |
| 349 | 189 | |
| 350 | 190 | /** |
| 351 | - * Check the date against the _bbp_edit_lock setting. | |
| 191 | + * Check a date against the length of time something can be edited. | |
| 352 | 192 | * |
| 353 | - * @since bbPress (r3133) | |
| 193 | + * It is recommended to leave $utc set to true and to work with UTC/GMT dates. | |
| 194 | + * Turning this off will use the WordPress offset which is likely undesirable. | |
| 354 | 195 | * |
| 355 | - * @param string $post_date_gmt | |
| 196 | + * @since 2.0.0 bbPress (r3133) | |
| 197 | + * @since 2.6.0 bbPress (r6868) Inverted some logic and added unit tests | |
| 356 | 198 | * |
| 357 | - * @uses get_option() Get the edit lock time | |
| 358 | - * @uses current_time() Get the current time | |
| 359 | - * @uses strtotime() Convert strings to time | |
| 360 | - * @uses apply_filters() Allow output to be manipulated | |
| 199 | + * @param string $datetime Gets run through strtotime() | |
| 200 | + * @param boolean $utc Default true. Is the timestamp in UTC? | |
| 361 | 201 | * |
| 362 | - * @return bool | |
| 202 | + * @return bool True by default, if date is past, or editing is disabled. | |
| 363 | 203 | */ |
| 364 | -function bbp_past_edit_lock( $post_date_gmt ) { | |
| 204 | +function bbp_past_edit_lock( $datetime = '', $utc = true ) { | |
| 365 | 205 | |
| 366 | - // Assume editing is allowed | |
| 367 | - $retval = false; | |
| 206 | + // Default value | |
| 207 | + $retval = true; | |
| 368 | 208 | |
| 369 | - // Bail if empty date | |
| 370 | - if ( ! empty( $post_date_gmt ) ) { | |
| 209 | + // Check if date and editing is allowed | |
| 210 | + if ( bbp_allow_content_edit() ) { | |
| 371 | 211 | |
| 372 | - // Period of time | |
| 373 | - $lockable = '+' . get_option( '_bbp_edit_lock', '5' ) . ' minutes'; | |
| 212 | + // Get number of minutes to allow editing for | |
| 213 | + $minutes = bbp_get_edit_lock(); | |
| 374 | 214 | |
| 375 | - // Now | |
| 376 | - $cur_time = current_time( 'timestamp', true ); | |
| 215 | + // 0 minutes means forever, so can never be past edit-lock time | |
| 216 | + if ( 0 === $minutes ) { | |
| 217 | + $retval = false; | |
| 377 | 218 | |
| 378 | - // Add lockable time to post time | |
| 379 | - $lock_time = strtotime( $lockable, strtotime( $post_date_gmt ) ); | |
| 219 | + // Checking against a specific datetime | |
| 220 | + } elseif ( ! empty( $datetime ) ) { | |
| 380 | 221 | |
| 381 | - // Compare | |
| 382 | - if ( $cur_time >= $lock_time ) { | |
| 383 | - $retval = true; | |
| 222 | + // Period of time | |
| 223 | + $lockable = "+{$minutes} minutes"; | |
| 224 | + if ( true === $utc ) { | |
| 225 | + $lockable .= " UTC"; | |
| 226 | + } | |
| 227 | + | |
| 228 | + // Now | |
| 229 | + $cur_time = current_time( 'timestamp', $utc ); | |
| 230 | + | |
| 231 | + // Get the duration in seconds | |
| 232 | + $duration = strtotime( $lockable ) - $cur_time; | |
| 233 | + | |
| 234 | + // Diff the times down to seconds | |
| 235 | + $lock_time = strtotime( $lockable, $cur_time ); | |
| 236 | + $past_time = strtotime( $datetime, $cur_time ); | |
| 237 | + $diff_time = ( $lock_time - $past_time ) - $duration; | |
| 238 | + | |
| 239 | + // Check if less than lock time | |
| 240 | + if ( $diff_time < $duration ) { | |
| 241 | + $retval = false; | |
| 242 | + } | |
| 384 | 243 | } |
| 385 | 244 | } |
| 386 | 245 | |
| 387 | - return apply_filters( 'bbp_past_edit_lock', (bool) $retval, $cur_time, $lock_time, $post_date_gmt ); | |
| 246 | + // Filter & return | |
| 247 | + return (bool) apply_filters( 'bbp_past_edit_lock', $retval, $datetime, $utc ); | |
| 388 | 248 | } |
| 389 | 249 | |
| 250 | +/** | |
| 251 | + * Get number of days something should remain trashed for before it is cleaned | |
| 252 | + * up by WordPress Cron. If set to 0, items will skip trash and be deleted | |
| 253 | + * immediately. | |
| 254 | + * | |
| 255 | + * @since 2.6.0 bbPress (r6424) | |
| 256 | + * | |
| 257 | + * @param string $context Provide context for additional filtering | |
| 258 | + * @return int Number of days items remain in trash | |
| 259 | + */ | |
| 260 | +function bbp_get_trash_days( $context = 'forum' ) { | |
| 261 | + | |
| 262 | + // Sanitize the context | |
| 263 | + $context = sanitize_key( $context ); | |
| 264 | + | |
| 265 | + // Check the WordPress constant | |
| 266 | + $days = defined( 'EMPTY_TRASH_DAYS' ) | |
| 267 | + ? (int) EMPTY_TRASH_DAYS | |
| 268 | + : 30; | |
| 269 | + | |
| 270 | + // Filter & return | |
| 271 | + return (int) apply_filters( 'bbp_get_trash_days', $days, $context ); | |
| 272 | +} | |
| 273 | + | |
| 390 | 274 | /** Statistics ****************************************************************/ |
| 391 | 275 | |
| 392 | 276 | /** |
| 393 | 277 | * Get the forum statistics |
| 394 | 278 | * |
| 395 | - * @since bbPress (r2769) | |
| 279 | + * @since 2.0.0 bbPress (r2769) | |
| 280 | + * @since 2.6.0 bbPress (r6055) Introduced the `count_pending_topics` and | |
| 281 | + * `count_pending_replies` arguments. | |
| 396 | 282 | * |
| 397 | - * @param mixed $args Optional. The function supports these arguments (all | |
| 283 | + * @param array $args Optional. The function supports these arguments (all | |
| 398 | 284 | * default to true): |
| 399 | 285 | * - count_users: Count users? |
| 400 | 286 | * - count_forums: Count forums? |
| 401 | 287 | * - count_topics: Count topics? If set to false, private, spammed and trashed |
| 402 | 288 | * topics are also not counted. |
| 289 | + * - count_pending_topics: Count pending topics? (only counted if the current | |
| 290 | + * user has edit_others_topics cap) | |
| 403 | 291 | * - count_private_topics: Count private topics? (only counted if the current |
| 404 | 292 | * user has read_private_topics cap) |
| 405 | 293 | * - count_spammed_topics: Count spammed topics? (only counted if the current |
| 406 | 294 | * user has edit_others_topics cap) |
| @@ -407,8 +295,10 @@ | ||
| 407 | 295 | * - count_trashed_topics: Count trashed topics? (only counted if the current |
| 408 | 296 | * user has view_trash cap) |
| 409 | 297 | * - count_replies: Count replies? If set to false, private, spammed and |
| 410 | 298 | * trashed replies are also not counted. |
| 299 | + * - count_pending_replies: Count pending replies? (only counted if the current | |
| 300 | + * user has edit_others_replies cap) | |
| 411 | 301 | * - count_private_replies: Count private replies? (only counted if the current |
| 412 | 302 | * user has read_private_replies cap) |
| 413 | 303 | * - count_spammed_replies: Count spammed replies? (only counted if the current |
| 414 | 304 | * user has edit_others_replies cap) |
| @@ -415,49 +305,59 @@ | ||
| 415 | 305 | * - count_trashed_replies: Count trashed replies? (only counted if the current |
| 416 | 306 | * user has view_trash cap) |
| 417 | 307 | * - count_tags: Count tags? If set to false, empty tags are also not counted |
| 418 | 308 | * - count_empty_tags: Count empty tags? |
| 419 | - * @uses bbp_count_users() To count the number of registered users | |
| 420 | - * @uses bbp_get_forum_post_type() To get the forum post type | |
| 421 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 422 | - * @uses bbp_get_reply_post_type() To get the reply post type | |
| 423 | - * @uses wp_count_posts() To count the number of forums, topics and replies | |
| 424 | - * @uses wp_count_terms() To count the number of topic tags | |
| 425 | - * @uses current_user_can() To check if the user is capable of doing things | |
| 426 | - * @uses number_format_i18n() To format the number | |
| 427 | - * @uses apply_filters() Calls 'bbp_get_statistics' with the statistics and args | |
| 428 | 309 | * @return object Walked forum tree |
| 429 | 310 | */ |
| 430 | -function bbp_get_statistics( $args = '' ) { | |
| 311 | +function bbp_get_statistics( $args = array() ) { | |
| 431 | 312 | |
| 432 | - $defaults = array ( | |
| 313 | + // Parse arguments against default values | |
| 314 | + $r = bbp_parse_args( $args, array( | |
| 315 | + | |
| 316 | + // Users | |
| 433 | 317 | 'count_users' => true, |
| 318 | + | |
| 319 | + // Forums | |
| 434 | 320 | 'count_forums' => true, |
| 321 | + | |
| 322 | + // Topics | |
| 435 | 323 | 'count_topics' => true, |
| 324 | + 'count_pending_topics' => true, | |
| 436 | 325 | 'count_private_topics' => true, |
| 437 | 326 | 'count_spammed_topics' => true, |
| 438 | 327 | 'count_trashed_topics' => true, |
| 328 | + | |
| 329 | + // Replies | |
| 439 | 330 | 'count_replies' => true, |
| 331 | + 'count_pending_replies' => true, | |
| 440 | 332 | 'count_private_replies' => true, |
| 441 | 333 | 'count_spammed_replies' => true, |
| 442 | 334 | 'count_trashed_replies' => true, |
| 335 | + | |
| 336 | + // Topic tags | |
| 443 | 337 | 'count_tags' => true, |
| 444 | 338 | 'count_empty_tags' => true |
| 445 | - ); | |
| 446 | - $r = bbp_parse_args( $args, $defaults, 'get_statistics' ); | |
| 447 | - extract( $r ); | |
| 448 | 339 | |
| 340 | + ), 'get_statistics' ); | |
| 341 | + | |
| 342 | + // Defaults | |
| 343 | + $topic_count = $topic_count_hidden = 0; | |
| 344 | + $reply_count = $reply_count_hidden = 0; | |
| 345 | + $topic_tag_count = $empty_topic_tag_count = 0; | |
| 346 | + $hidden_topic_title = $hidden_reply_title = ''; | |
| 347 | + | |
| 449 | 348 | // Users |
| 450 | - if ( !empty( $count_users ) ) | |
| 451 | - $user_count = bbp_get_total_users(); | |
| 349 | + $user_count = ! empty( $r['count_users'] ) | |
| 350 | + ? bbp_get_total_users() | |
| 351 | + : 0; | |
| 452 | 352 | |
| 453 | 353 | // Forums |
| 454 | - if ( !empty( $count_forums ) ) { | |
| 455 | - $forum_count = wp_count_posts( bbp_get_forum_post_type() ); | |
| 456 | - $forum_count = $forum_count->publish; | |
| 457 | - } | |
| 354 | + $forum_count = ! empty( $r['count_forums'] ) | |
| 355 | + ? wp_count_posts( bbp_get_forum_post_type() )->publish | |
| 356 | + : 0; | |
| 458 | 357 | |
| 459 | 358 | // Post statuses |
| 359 | + $pending = bbp_get_pending_status_id(); | |
| 460 | 360 | $private = bbp_get_private_status_id(); |
| 461 | 361 | $spam = bbp_get_spam_status_id(); |
| 462 | 362 | $trash = bbp_get_trash_status_id(); |
| 463 | 363 | $closed = bbp_get_closed_status_id(); |
| @@ -462,10 +362,9 @@ | ||
| 462 | 362 | $trash = bbp_get_trash_status_id(); |
| 463 | 363 | $closed = bbp_get_closed_status_id(); |
| 464 | 364 | |
| 465 | 365 | // Topics |
| 466 | - if ( !empty( $count_topics ) ) { | |
| 467 | - | |
| 366 | + if ( ! empty( $r['count_topics'] ) ) { | |
| 468 | 367 | $all_topics = wp_count_posts( bbp_get_topic_post_type() ); |
| 469 | 368 | |
| 470 | 369 | // Published (publish + closed) |
| 471 | 370 | $topic_count = $all_topics->publish + $all_topics->{$closed}; |
| @@ -471,25 +370,51 @@ | ||
| 471 | 370 | $topic_count = $all_topics->publish + $all_topics->{$closed}; |
| 472 | 371 | |
| 473 | 372 | if ( current_user_can( 'read_private_topics' ) || current_user_can( 'edit_others_topics' ) || current_user_can( 'view_trash' ) ) { |
| 474 | 373 | |
| 374 | + // Declare empty arrays | |
| 375 | + $topics = $topic_titles = array(); | |
| 376 | + | |
| 377 | + // Pending | |
| 378 | + $topics['pending'] = ( ! empty( $r['count_pending_topics'] ) && current_user_can( 'edit_others_topics' ) ) | |
| 379 | + ? (int) $all_topics->{$pending} | |
| 380 | + : 0; | |
| 381 | + | |
| 475 | 382 | // Private |
| 476 | - $topics['private'] = ( !empty( $count_private_topics ) && current_user_can( 'read_private_topics' ) ) ? (int) $all_topics->{$private} : 0; | |
| 383 | + $topics['private'] = ( ! empty( $r['count_private_topics'] ) && current_user_can( 'read_private_topics' ) ) | |
| 384 | + ? (int) $all_topics->{$private} | |
| 385 | + : 0; | |
| 477 | 386 | |
| 478 | 387 | // Spam |
| 479 | - $topics['spammed'] = ( !empty( $count_spammed_topics ) && current_user_can( 'edit_others_topics' ) ) ? (int) $all_topics->{$spam} : 0; | |
| 388 | + $topics['spammed'] = ( ! empty( $r['count_spammed_topics'] ) && current_user_can( 'edit_others_topics' ) ) | |
| 389 | + ? (int) $all_topics->{$spam} | |
| 390 | + : 0; | |
| 480 | 391 | |
| 481 | 392 | // Trash |
| 482 | - $topics['trashed'] = ( !empty( $count_trashed_topics ) && current_user_can( 'view_trash' ) ) ? (int) $all_topics->{$trash} : 0; | |
| 393 | + $topics['trashed'] = ( ! empty( $r['count_trashed_topics'] ) && current_user_can( 'view_trash' ) ) | |
| 394 | + ? (int) $all_topics->{$trash} | |
| 395 | + : 0; | |
| 483 | 396 | |
| 484 | - // Total hidden (private + spam + trash) | |
| 485 | - $topic_count_hidden = $topics['private'] + $topics['spammed'] + $topics['trashed']; | |
| 397 | + // Total hidden (pending + private + spam + trash) | |
| 398 | + $topic_count_hidden = $topics['pending'] + $topics['private'] + $topics['spammed'] + $topics['trashed']; | |
| 486 | 399 | |
| 487 | 400 | // Generate the hidden topic count's title attribute |
| 488 | - $topic_titles[] = !empty( $topics['private'] ) ? sprintf( __( 'Private: %s', 'bbpress' ), number_format_i18n( $topics['private'] ) ) : ''; | |
| 489 | - $topic_titles[] = !empty( $topics['spammed'] ) ? sprintf( __( 'Spammed: %s', 'bbpress' ), number_format_i18n( $topics['spammed'] ) ) : ''; | |
| 490 | - $topic_titles[] = !empty( $topics['trashed'] ) ? sprintf( __( 'Trashed: %s', 'bbpress' ), number_format_i18n( $topics['trashed'] ) ) : ''; | |
| 401 | + $topic_titles[] = ! empty( $topics['pending'] ) | |
| 402 | + ? sprintf( esc_html__( 'Pending: %s', 'bbpress' ), bbp_number_format_i18n( $topics['pending'] ) ) | |
| 403 | + : ''; | |
| 491 | 404 | |
| 405 | + $topic_titles[] = ! empty( $topics['private'] ) | |
| 406 | + ? ''//sprintf( esc_html__( 'Private: %s', 'bbpress' ), bbp_number_format_i18n( $topics['private'] ) ) | |
| 407 | + : ''; | |
| 408 | + | |
| 409 | + $topic_titles[] = ! empty( $topics['spammed'] ) | |
| 410 | + ? sprintf( esc_html__( 'Spammed: %s', 'bbpress' ), bbp_number_format_i18n( $topics['spammed'] ) ) | |
| 411 | + : ''; | |
| 412 | + | |
| 413 | + $topic_titles[] = ! empty( $topics['trashed'] ) | |
| 414 | + ? sprintf( esc_html__( 'Trashed: %s', 'bbpress' ), bbp_number_format_i18n( $topics['trashed'] ) ) | |
| 415 | + : ''; | |
| 416 | + | |
| 492 | 417 | // Compile the hidden topic title |
| 493 | 418 | $hidden_topic_title = implode( ' | ', array_filter( $topic_titles ) ); |
| 494 | 419 | } |
| 495 | 420 | } |
| @@ -494,9 +419,9 @@ | ||
| 494 | 419 | } |
| 495 | 420 | } |
| 496 | 421 | |
| 497 | 422 | // Replies |
| 498 | - if ( !empty( $count_replies ) ) { | |
| 423 | + if ( ! empty( $r['count_replies'] ) ) { | |
| 499 | 424 | |
| 500 | 425 | $all_replies = wp_count_posts( bbp_get_reply_post_type() ); |
| 501 | 426 | |
| 502 | 427 | // Published |
| @@ -503,56 +428,96 @@ | ||
| 503 | 428 | $reply_count = $all_replies->publish; |
| 504 | 429 | |
| 505 | 430 | if ( current_user_can( 'read_private_replies' ) || current_user_can( 'edit_others_replies' ) || current_user_can( 'view_trash' ) ) { |
| 506 | 431 | |
| 432 | + // Declare empty arrays | |
| 433 | + $replies = $reply_titles = array(); | |
| 434 | + | |
| 435 | + // Pending | |
| 436 | + $replies['pending'] = ( ! empty( $r['count_pending_replies'] ) && current_user_can( 'edit_others_replies' ) ) | |
| 437 | + ? (int) $all_replies->{$pending} | |
| 438 | + : 0; | |
| 439 | + | |
| 507 | 440 | // Private |
| 508 | - $replies['private'] = ( !empty( $count_private_replies ) && current_user_can( 'read_private_replies' ) ) ? (int) $all_replies->{$private} : 0; | |
| 441 | + $replies['private'] = ( ! empty( $r['count_private_replies'] ) && current_user_can( 'read_private_replies' ) ) | |
| 442 | + ? (int) $all_replies->{$private} | |
| 443 | + : 0; | |
| 509 | 444 | |
| 510 | 445 | // Spam |
| 511 | - $replies['spammed'] = ( !empty( $count_spammed_replies ) && current_user_can( 'edit_others_replies' ) ) ? (int) $all_replies->{$spam} : 0; | |
| 446 | + $replies['spammed'] = ( ! empty( $r['count_spammed_replies'] ) && current_user_can( 'edit_others_replies' ) ) | |
| 447 | + ? (int) $all_replies->{$spam} | |
| 448 | + : 0; | |
| 512 | 449 | |
| 513 | 450 | // Trash |
| 514 | - $replies['trashed'] = ( !empty( $count_trashed_replies ) && current_user_can( 'view_trash' ) ) ? (int) $all_replies->{$trash} : 0; | |
| 451 | + $replies['trashed'] = ( ! empty( $r['count_trashed_replies'] ) && current_user_can( 'view_trash' ) ) | |
| 452 | + ? (int) $all_replies->{$trash} | |
| 453 | + : 0; | |
| 515 | 454 | |
| 516 | - // Total hidden (private + spam + trash) | |
| 517 | - $reply_count_hidden = $replies['private'] + $replies['spammed'] + $replies['trashed']; | |
| 455 | + // Total hidden (pending + private + spam + trash) | |
| 456 | + $reply_count_hidden = $replies['pending'] + $replies['private'] + $replies['spammed'] + $replies['trashed']; | |
| 518 | 457 | |
| 519 | 458 | // Generate the hidden topic count's title attribute |
| 520 | - $reply_titles[] = !empty( $replies['private'] ) ? sprintf( __( 'Private: %s', 'bbpress' ), number_format_i18n( $replies['private'] ) ) : ''; | |
| 521 | - $reply_titles[] = !empty( $replies['spammed'] ) ? sprintf( __( 'Spammed: %s', 'bbpress' ), number_format_i18n( $replies['spammed'] ) ) : ''; | |
| 522 | - $reply_titles[] = !empty( $replies['trashed'] ) ? sprintf( __( 'Trashed: %s', 'bbpress' ), number_format_i18n( $replies['trashed'] ) ) : ''; | |
| 459 | + $reply_titles[] = ! empty( $replies['pending'] ) | |
| 460 | + ? sprintf( esc_html__( 'Pending: %s', 'bbpress' ), bbp_number_format_i18n( $replies['pending'] ) ) | |
| 461 | + : ''; | |
| 523 | 462 | |
| 463 | + $reply_titles[] = ! empty( $replies['private'] ) | |
| 464 | + ? sprintf( esc_html__( 'Private: %s', 'bbpress' ), bbp_number_format_i18n( $replies['private'] ) ) | |
| 465 | + : ''; | |
| 466 | + | |
| 467 | + $reply_titles[] = ! empty( $replies['spammed'] ) | |
| 468 | + ? sprintf( esc_html__( 'Spammed: %s', 'bbpress' ), bbp_number_format_i18n( $replies['spammed'] ) ) | |
| 469 | + : ''; | |
| 470 | + | |
| 471 | + $reply_titles[] = ! empty( $replies['trashed'] ) | |
| 472 | + ? sprintf( esc_html__( 'Trashed: %s', 'bbpress' ), bbp_number_format_i18n( $replies['trashed'] ) ) | |
| 473 | + : ''; | |
| 474 | + | |
| 524 | 475 | // Compile the hidden replies title |
| 525 | 476 | $hidden_reply_title = implode( ' | ', array_filter( $reply_titles ) ); |
| 526 | - | |
| 527 | 477 | } |
| 528 | 478 | } |
| 529 | 479 | |
| 530 | 480 | // Topic Tags |
| 531 | - if ( !empty( $count_tags ) && bbp_allow_topic_tags() ) { | |
| 481 | + if ( ! empty( $r['count_tags'] ) && bbp_allow_topic_tags() ) { | |
| 532 | 482 | |
| 533 | 483 | // Get the count |
| 534 | 484 | $topic_tag_count = wp_count_terms( bbp_get_topic_tag_tax_id(), array( 'hide_empty' => true ) ); |
| 535 | 485 | |
| 536 | 486 | // Empty tags |
| 537 | - if ( !empty( $count_empty_tags ) && current_user_can( 'edit_topic_tags' ) ) { | |
| 487 | + if ( ! empty( $r['count_empty_tags'] ) && current_user_can( 'edit_topic_tags' ) ) { | |
| 538 | 488 | $empty_topic_tag_count = wp_count_terms( bbp_get_topic_tag_tax_id() ) - $topic_tag_count; |
| 539 | 489 | } |
| 540 | 490 | } |
| 541 | 491 | |
| 542 | 492 | // Tally the tallies |
| 543 | - $statistics = compact( 'user_count', 'forum_count', 'topic_count', 'topic_count_hidden', 'reply_count', 'reply_count_hidden', 'topic_tag_count', 'empty_topic_tag_count' ); | |
| 544 | - $statistics = array_map( 'absint', $statistics ); | |
| 545 | - $statistics = array_map( 'number_format_i18n', $statistics ); | |
| 493 | + $counts = array_filter( array_map( 'absint', compact( | |
| 494 | + 'user_count', | |
| 495 | + 'forum_count', | |
| 496 | + 'topic_count', | |
| 497 | + 'topic_count_hidden', | |
| 498 | + 'reply_count', | |
| 499 | + 'reply_count_hidden', | |
| 500 | + 'topic_tag_count', | |
| 501 | + 'empty_topic_tag_count' | |
| 502 | + ) ) ); | |
| 546 | 503 | |
| 547 | - // Add the hidden (topic/reply) count title attribute strings because we don't need to run the math functions on these (see above) | |
| 548 | - if ( isset( $hidden_topic_title ) ) | |
| 549 | - $statistics['hidden_topic_title'] = $hidden_topic_title; | |
| 504 | + // Define return value | |
| 505 | + $statistics = array(); | |
| 550 | 506 | |
| 551 | - if ( isset( $hidden_reply_title ) ) | |
| 552 | - $statistics['hidden_reply_title'] = $hidden_reply_title; | |
| 507 | + // Loop through and store the integer and i18n formatted counts. | |
| 508 | + foreach ( $counts as $key => $count ) { | |
| 509 | + $statistics[ $key ] = bbp_number_format_i18n( $count ); | |
| 510 | + $statistics[ "{$key}_int" ] = $count; | |
| 511 | + } | |
| 553 | 512 | |
| 554 | - return apply_filters( 'bbp_get_statistics', $statistics, $args ); | |
| 513 | + // Add the hidden (topic/reply) count title attribute strings because we | |
| 514 | + // don't need to run the math functions on these (see above) | |
| 515 | + $statistics['hidden_topic_title'] = $hidden_topic_title; | |
| 516 | + $statistics['hidden_reply_title'] = $hidden_reply_title; | |
| 517 | + | |
| 518 | + // Filter & return | |
| 519 | + return (array) apply_filters( 'bbp_get_statistics', $statistics, $r, $args ); | |
| 555 | 520 | } |
| 556 | 521 | |
| 557 | 522 | /** New/edit topic/reply helpers **********************************************/ |
| 558 | 523 | |
| @@ -560,9 +525,9 @@ | ||
| 560 | 525 | * Filter anonymous post data |
| 561 | 526 | * |
| 562 | 527 | * We use REMOTE_ADDR here directly. If you are behind a proxy, you should |
| 563 | 528 | * ensure that it is properly set, such as in wp-config.php, for your |
| 564 | - * environment. See {@link http://core.trac.wordpress.org/ticket/9235} | |
| 529 | + * environment. See {@link https://core.trac.wordpress.org/ticket/9235} | |
| 565 | 530 | * |
| 566 | 531 | * Note that bbp_pre_anonymous_filters() is responsible for sanitizing each |
| 567 | 532 | * of the filtered core anonymous values here. |
| 568 | 533 | * |
| @@ -567,50 +532,123 @@ | ||
| 567 | 532 | * of the filtered core anonymous values here. |
| 568 | 533 | * |
| 569 | 534 | * If there are any errors, those are directly added to {@link bbPress:errors} |
| 570 | 535 | * |
| 571 | - * @since bbPress (r2734) | |
| 536 | + * @since 2.0.0 bbPress (r2734) | |
| 572 | 537 | * |
| 573 | - * @param mixed $args Optional. If no args are there, then $_POST values are | |
| 574 | - * used. | |
| 575 | - * @uses apply_filters() Calls 'bbp_pre_anonymous_post_author_name' with the | |
| 576 | - * anonymous user name | |
| 577 | - * @uses apply_filters() Calls 'bbp_pre_anonymous_post_author_email' with the | |
| 578 | - * anonymous user email | |
| 579 | - * @uses apply_filters() Calls 'bbp_pre_anonymous_post_author_website' with the | |
| 580 | - * anonymous user website | |
| 538 | + * @param array $args Optional. If no args are there, then $_POST values are | |
| 581 | 539 | * @return bool|array False on errors, values in an array on success |
| 582 | 540 | */ |
| 583 | -function bbp_filter_anonymous_post_data( $args = '' ) { | |
| 541 | +function bbp_filter_anonymous_post_data( $args = array() ) { | |
| 584 | 542 | |
| 585 | - // Assign variables | |
| 586 | - $defaults = array ( | |
| 587 | - 'bbp_anonymous_name' => !empty( $_POST['bbp_anonymous_name'] ) ? $_POST['bbp_anonymous_name'] : false, | |
| 588 | - 'bbp_anonymous_email' => !empty( $_POST['bbp_anonymous_email'] ) ? $_POST['bbp_anonymous_email'] : false, | |
| 589 | - 'bbp_anonymous_website' => !empty( $_POST['bbp_anonymous_website'] ) ? $_POST['bbp_anonymous_website'] : false, | |
| 543 | + // Parse arguments against default values | |
| 544 | + $r = bbp_parse_args( $args, array( | |
| 545 | + 'bbp_anonymous_name' => ! empty( $_POST['bbp_anonymous_name'] ) ? $_POST['bbp_anonymous_name'] : false, | |
| 546 | + 'bbp_anonymous_email' => ! empty( $_POST['bbp_anonymous_email'] ) ? $_POST['bbp_anonymous_email'] : false, | |
| 547 | + 'bbp_anonymous_website' => ! empty( $_POST['bbp_anonymous_website'] ) ? $_POST['bbp_anonymous_website'] : false, | |
| 548 | + ), 'filter_anonymous_post_data' ); | |
| 549 | + | |
| 550 | + // Strip invalid characters | |
| 551 | + $r = bbp_sanitize_anonymous_post_author( $r ); | |
| 552 | + | |
| 553 | + // Filter name | |
| 554 | + $r['bbp_anonymous_name'] = apply_filters( 'bbp_pre_anonymous_post_author_name', $r['bbp_anonymous_name'] ); | |
| 555 | + if ( empty( $r['bbp_anonymous_name'] ) ) { | |
| 556 | + bbp_add_error( 'bbp_anonymous_name', __( '<strong>Error</strong>: Invalid author name.', 'bbpress' ) ); | |
| 557 | + } | |
| 558 | + | |
| 559 | + // Filter email address | |
| 560 | + $r['bbp_anonymous_email'] = apply_filters( 'bbp_pre_anonymous_post_author_email', $r['bbp_anonymous_email'] ); | |
| 561 | + if ( empty( $r['bbp_anonymous_email'] ) ) { | |
| 562 | + bbp_add_error( 'bbp_anonymous_email', __( '<strong>Error</strong>: Invalid email address.', 'bbpress' ) ); | |
| 563 | + } | |
| 564 | + | |
| 565 | + // Website is optional (can be empty) | |
| 566 | + $r['bbp_anonymous_website'] = apply_filters( 'bbp_pre_anonymous_post_author_website', $r['bbp_anonymous_website'] ); | |
| 567 | + | |
| 568 | + // Filter & return | |
| 569 | + return (array) apply_filters( 'bbp_filter_anonymous_post_data', $r, $args ); | |
| 570 | +} | |
| 571 | + | |
| 572 | +/** | |
| 573 | + * Sanitize an array of anonymous post author data | |
| 574 | + * | |
| 575 | + * @since 2.6.0 bbPress (r6400) | |
| 576 | + * | |
| 577 | + * @param array $anonymous_data | |
| 578 | + * @return array | |
| 579 | + */ | |
| 580 | +function bbp_sanitize_anonymous_post_author( $anonymous_data = array() ) { | |
| 581 | + | |
| 582 | + // Make sure anonymous data is an array | |
| 583 | + if ( ! is_array( $anonymous_data ) ) { | |
| 584 | + $anonymous_data = array(); | |
| 585 | + } | |
| 586 | + | |
| 587 | + // Map meta data to comment fields (as guides for stripping invalid text) | |
| 588 | + $fields = array( | |
| 589 | + 'bbp_anonymous_name' => 'comment_author', | |
| 590 | + 'bbp_anonymous_email' => 'comment_author_email', | |
| 591 | + 'bbp_anonymous_website' => 'comment_author_url' | |
| 590 | 592 | ); |
| 591 | - $r = bbp_parse_args( $args, $defaults, 'filter_anonymous_post_data' ); | |
| 592 | - extract( $r ); | |
| 593 | 593 | |
| 594 | - // Filter variables and add errors if necessary | |
| 595 | - $bbp_anonymous_name = apply_filters( 'bbp_pre_anonymous_post_author_name', $bbp_anonymous_name ); | |
| 596 | - if ( empty( $bbp_anonymous_name ) ) | |
| 597 | - bbp_add_error( 'bbp_anonymous_name', __( '<strong>ERROR</strong>: Invalid author name submitted!', 'bbpress' ) ); | |
| 594 | + // Setup a new return array | |
| 595 | + $r = $anonymous_data; | |
| 598 | 596 | |
| 599 | - $bbp_anonymous_email = apply_filters( 'bbp_pre_anonymous_post_author_email', $bbp_anonymous_email ); | |
| 600 | - if ( empty( $bbp_anonymous_email ) ) | |
| 601 | - bbp_add_error( 'bbp_anonymous_email', __( '<strong>ERROR</strong>: Invalid email address submitted!', 'bbpress' ) ); | |
| 597 | + // Get the database | |
| 598 | + $bbp_db = bbp_db(); | |
| 602 | 599 | |
| 603 | - // Website is optional | |
| 604 | - $bbp_anonymous_website = apply_filters( 'bbp_pre_anonymous_post_author_website', $bbp_anonymous_website ); | |
| 600 | + // Strip invalid text from fields | |
| 601 | + foreach ( $fields as $bbp_field => $comment_field ) { | |
| 602 | + if ( ! empty( $r[ $bbp_field ] ) ) { | |
| 603 | + $r[ $bbp_field ] = $bbp_db->strip_invalid_text_for_column( $bbp_db->comments, $comment_field, $r[ $bbp_field ] ); | |
| 604 | + } | |
| 605 | + } | |
| 605 | 606 | |
| 606 | - if ( !bbp_has_errors() ) | |
| 607 | - $retval = compact( 'bbp_anonymous_name', 'bbp_anonymous_email', 'bbp_anonymous_website' ); | |
| 608 | - else | |
| 609 | - $retval = false; | |
| 607 | + // Filter & return | |
| 608 | + return (array) apply_filters( 'bbp_sanitize_anonymous_post_author', $r, $anonymous_data ); | |
| 609 | +} | |
| 610 | 610 | |
| 611 | - // Finally, return sanitized data or false | |
| 612 | - return apply_filters( 'bbp_filter_anonymous_post_data', $retval, $args ); | |
| 611 | +/** | |
| 612 | + * Update the relevant meta-data for an anonymous post author | |
| 613 | + * | |
| 614 | + * @since 2.6.0 bbPress (r6400) | |
| 615 | + * | |
| 616 | + * @param int $post_id | |
| 617 | + * @param array $anonymous_data | |
| 618 | + * @param string $post_type | |
| 619 | + */ | |
| 620 | +function bbp_update_anonymous_post_author( $post_id = 0, $anonymous_data = array(), $post_type = '' ) { | |
| 621 | + | |
| 622 | + // Maybe look for anonymous | |
| 623 | + if ( empty( $anonymous_data ) ) { | |
| 624 | + $anonymous_data = bbp_filter_anonymous_post_data(); | |
| 625 | + } | |
| 626 | + | |
| 627 | + // Sanitize parameters | |
| 628 | + $post_id = (int) $post_id; | |
| 629 | + $post_type = sanitize_key( $post_type ); | |
| 630 | + | |
| 631 | + // Bail if missing required data | |
| 632 | + if ( empty( $post_id ) || empty( $post_type ) || empty( $anonymous_data ) ) { | |
| 633 | + return; | |
| 634 | + } | |
| 635 | + | |
| 636 | + // Parse arguments against default values | |
| 637 | + $r = bbp_parse_args( $anonymous_data, array( | |
| 638 | + 'bbp_anonymous_name' => '', | |
| 639 | + 'bbp_anonymous_email' => '', | |
| 640 | + 'bbp_anonymous_website' => '', | |
| 641 | + ), "update_{$post_type}" ); | |
| 642 | + | |
| 643 | + // Update all anonymous metas | |
| 644 | + foreach ( $r as $anon_key => $anon_value ) { | |
| 645 | + | |
| 646 | + // Update, or delete if empty | |
| 647 | + ! empty( $anon_value ) | |
| 648 | + ? update_post_meta( $post_id, '_' . $anon_key, (string) $anon_value, false ) | |
| 649 | + : delete_post_meta( $post_id, '_' . $anon_key ); | |
| 650 | + } | |
| 613 | 651 | } |
| 614 | 652 | |
| 615 | 653 | /** |
| 616 | 654 | * Check for duplicate topics/replies |
| @@ -616,57 +654,82 @@ | ||
| 616 | 654 | * Check for duplicate topics/replies |
| 617 | 655 | * |
| 618 | 656 | * Check to make sure that a user is not making a duplicate post |
| 619 | 657 | * |
| 620 | - * @since bbPress (r2763) | |
| 658 | + * @since 2.0.0 bbPress (r2763) | |
| 621 | 659 | * |
| 622 | 660 | * @param array $post_data Contains information about the comment |
| 623 | - * @uses current_user_can() To check if the current user can throttle | |
| 624 | - * @uses get_meta_sql() To generate the meta sql for checking anonymous email | |
| 625 | - * @uses apply_filters() Calls 'bbp_check_for_duplicate_query' with the | |
| 626 | - * duplicate check query and post data | |
| 627 | - * @uses wpdb::get_var() To execute our query and get the var back | |
| 628 | - * @uses get_post_meta() To get the anonymous user email post meta | |
| 629 | - * @uses do_action() Calls 'bbp_post_duplicate_trigger' with the post data when | |
| 630 | - * it is found that it is a duplicate | |
| 631 | 661 | * @return bool True if it is not a duplicate, false if it is |
| 632 | 662 | */ |
| 633 | -function bbp_check_for_duplicate( $post_data ) { | |
| 663 | +function bbp_check_for_duplicate( $post_data = array() ) { | |
| 634 | 664 | |
| 665 | + // Parse arguments against default values | |
| 666 | + $r = bbp_parse_args( $post_data, array( | |
| 667 | + 'post_author' => 0, | |
| 668 | + 'post_type' => array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ), | |
| 669 | + 'post_parent' => 0, | |
| 670 | + 'post_content' => '', | |
| 671 | + 'post_status' => bbp_get_trash_status_id(), | |
| 672 | + 'anonymous_data' => array() | |
| 673 | + ), 'check_for_duplicate' ); | |
| 674 | + | |
| 635 | 675 | // No duplicate checks for those who can throttle |
| 636 | - if ( current_user_can( 'throttle' ) ) | |
| 676 | + if ( user_can( (int) $r['post_author'], 'throttle' ) ) { | |
| 637 | 677 | return true; |
| 678 | + } | |
| 638 | 679 | |
| 639 | - global $wpdb; | |
| 680 | + // Get the DB | |
| 681 | + $bbp_db = bbp_db(); | |
| 640 | 682 | |
| 641 | - extract( $post_data, EXTR_SKIP ); | |
| 683 | + // Default clauses | |
| 684 | + $join = $where = ''; | |
| 642 | 685 | |
| 643 | 686 | // Check for anonymous post |
| 644 | - if ( empty( $post_author ) && ( isset( $anonymous_data ) && !empty( $anonymous_data['bbp_anonymous_email'] ) ) ) { | |
| 645 | - $clauses = get_meta_sql( array( array( | |
| 646 | - 'key' => '_bbp_anonymous_email', | |
| 647 | - 'value' => $anonymous_data['bbp_anonymous_email'] | |
| 648 | - ) ), 'post', $wpdb->posts, 'ID' ); | |
| 687 | + if ( empty( $r['post_author'] ) && ( ! empty( $r['anonymous_data'] ) && ! empty( $r['anonymous_data']['bbp_anonymous_email'] ) ) ) { | |
| 649 | 688 | |
| 650 | - $join = $clauses['join']; | |
| 651 | - $where = $clauses['where']; | |
| 652 | - } else{ | |
| 653 | - $join = $where = ''; | |
| 689 | + // Sanitize the email address for querying | |
| 690 | + $email = sanitize_email( $r['anonymous_data']['bbp_anonymous_email'] ); | |
| 691 | + | |
| 692 | + // Only proceed | |
| 693 | + if ( ! empty( $email ) && is_email( $email ) ) { | |
| 694 | + | |
| 695 | + // Get the meta SQL | |
| 696 | + $clauses = get_meta_sql( array( array( | |
| 697 | + 'key' => '_bbp_anonymous_email', | |
| 698 | + 'value' => $email, | |
| 699 | + ) ), 'post', $bbp_db->posts, 'ID' ); | |
| 700 | + | |
| 701 | + // Set clauses | |
| 702 | + $join = $clauses['join']; | |
| 703 | + | |
| 704 | + // "'", "%", "$" and are valid characters in email addresses | |
| 705 | + $where = $bbp_db->remove_placeholder_escape( $clauses['where'] ); | |
| 706 | + } | |
| 654 | 707 | } |
| 655 | 708 | |
| 656 | - // Simple duplicate check | |
| 657 | - // Expected slashed ($post_type, $post_parent, $post_author, $post_content, $anonymous_data) | |
| 658 | - $status = bbp_get_trash_status_id(); | |
| 659 | - $dupe = "SELECT ID FROM {$wpdb->posts} {$join} WHERE post_type = '{$post_type}' AND post_status != '{$status}' AND post_author = {$post_author} AND post_content = '{$post_content}' {$where}"; | |
| 660 | - $dupe .= !empty( $post_parent ) ? " AND post_parent = '{$post_parent}'" : ''; | |
| 661 | - $dupe .= " LIMIT 1"; | |
| 662 | - $dupe = apply_filters( 'bbp_check_for_duplicate_query', $dupe, $post_data ); | |
| 709 | + // Unslash $r to pass through DB->prepare() | |
| 710 | + // | |
| 711 | + // @see: https://bbpress.trac.wordpress.org/ticket/2185/ | |
| 712 | + // @see: https://core.trac.wordpress.org/changeset/23973/ | |
| 713 | + $r = wp_unslash( $r ); | |
| 663 | 714 | |
| 664 | - if ( $wpdb->get_var( $dupe ) ) { | |
| 715 | + // Prepare duplicate check query | |
| 716 | + $query = "SELECT ID FROM {$bbp_db->posts} {$join}"; | |
| 717 | + $query .= $bbp_db->prepare( "WHERE post_type = %s AND post_status != %s AND post_author = %d AND post_content = %s", $r['post_type'], $r['post_status'], $r['post_author'], $r['post_content'] ); | |
| 718 | + $query .= ! empty( $r['post_parent'] ) | |
| 719 | + ? $bbp_db->prepare( " AND post_parent = %d", $r['post_parent'] ) | |
| 720 | + : ''; | |
| 721 | + $query .= $where; | |
| 722 | + $query .= " LIMIT 1"; | |
| 723 | + $dupe = apply_filters( 'bbp_check_for_duplicate_query', $query, $r ); | |
| 724 | + | |
| 725 | + // Dupe found | |
| 726 | + if ( $bbp_db->get_var( $dupe ) ) { | |
| 665 | 727 | do_action( 'bbp_check_for_duplicate_trigger', $post_data ); |
| 666 | 728 | return false; |
| 667 | 729 | } |
| 668 | 730 | |
| 731 | + // Dupe not found | |
| 669 | 732 | return true; |
| 670 | 733 | } |
| 671 | 734 | |
| 672 | 735 | /** |
| @@ -674,45 +737,44 @@ | ||
| 674 | 737 | * |
| 675 | 738 | * Check to make sure that a user is not making too many posts in a short amount |
| 676 | 739 | * of time. |
| 677 | 740 | * |
| 678 | - * @since bbPress (r2734) | |
| 741 | + * @since 2.0.0 bbPress (r2734) | |
| 679 | 742 | * |
| 680 | - * @param false|array $anonymous_data Optional - if it's an anonymous post. Do | |
| 681 | - * not supply if supplying $author_id. | |
| 682 | - * Should have key 'bbp_author_ip'. | |
| 683 | - * Should be sanitized (see | |
| 684 | - * {@link bbp_filter_anonymous_post_data()} | |
| 685 | - * for sanitization) | |
| 743 | + * @param array $anonymous_data Optional - if it's an anonymous post. Do not | |
| 744 | + * supply if supplying $author_id. Should be | |
| 745 | + * sanitized (see {@link bbp_filter_anonymous_post_data()} | |
| 686 | 746 | * @param int $author_id Optional. Supply if it's a post by a logged in user. |
| 687 | 747 | * Do not supply if supplying $anonymous_data. |
| 688 | - * @uses get_option() To get the throttle time | |
| 689 | - * @uses get_transient() To get the last posted transient of the ip | |
| 690 | - * @uses bbp_get_user_last_posted() To get the last posted time of the user | |
| 691 | - * @uses current_user_can() To check if the current user can throttle | |
| 692 | 748 | * @return bool True if there is no flooding, false if there is |
| 693 | 749 | */ |
| 694 | -function bbp_check_for_flood( $anonymous_data = false, $author_id = 0 ) { | |
| 750 | +function bbp_check_for_flood( $anonymous_data = array(), $author_id = 0 ) { | |
| 695 | 751 | |
| 752 | + // Allow for flood check to be skipped | |
| 753 | + if ( apply_filters( 'bbp_bypass_check_for_flood', false, $anonymous_data, $author_id ) ) { | |
| 754 | + return true; | |
| 755 | + } | |
| 756 | + | |
| 696 | 757 | // Option disabled. No flood checks. |
| 697 | 758 | $throttle_time = get_option( '_bbp_throttle_time' ); |
| 698 | - if ( empty( $throttle_time ) ) | |
| 759 | + if ( empty( $throttle_time ) || ! bbp_allow_content_throttle() ) { | |
| 699 | 760 | return true; |
| 761 | + } | |
| 700 | 762 | |
| 701 | 763 | // User is anonymous, so check a transient based on the IP |
| 702 | - if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) { | |
| 764 | + if ( ! empty( $anonymous_data ) ) { | |
| 703 | 765 | $last_posted = get_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted' ); |
| 704 | 766 | |
| 705 | - if ( !empty( $last_posted ) && time() < $last_posted + $throttle_time ) { | |
| 767 | + if ( ! empty( $last_posted ) && ( time() < ( $last_posted + $throttle_time ) ) ) { | |
| 706 | 768 | return false; |
| 707 | 769 | } |
| 708 | - | |
| 770 | + | |
| 709 | 771 | // User is logged in, so check their last posted time |
| 710 | - } elseif ( !empty( $author_id ) ) { | |
| 772 | + } elseif ( ! empty( $author_id ) ) { | |
| 711 | 773 | $author_id = (int) $author_id; |
| 712 | 774 | $last_posted = bbp_get_user_last_posted( $author_id ); |
| 713 | 775 | |
| 714 | - if ( isset( $last_posted ) && time() < $last_posted + $throttle_time && !current_user_can( 'throttle' ) ) { | |
| 776 | + if ( ! empty( $last_posted ) && ( time() < ( $last_posted + $throttle_time ) ) && ! user_can( $author_id, 'throttle' ) ) { | |
| 715 | 777 | return false; |
| 716 | 778 | } |
| 717 | 779 | } else { |
| 718 | 780 | return false; |
| @@ -723,54 +785,125 @@ | ||
| 723 | 785 | |
| 724 | 786 | /** |
| 725 | 787 | * Checks topics and replies against the discussion moderation of blocked keys |
| 726 | 788 | * |
| 727 | - * @since bbPress (r3581) | |
| 789 | + * @since 2.1.0 bbPress (r3581) | |
| 728 | 790 | * |
| 729 | - * @param array $anonymous_data Anonymous user data | |
| 791 | + * @param array $anonymous_data Optional - if it's an anonymous post. Do not | |
| 792 | + * supply if supplying $author_id. Should be | |
| 793 | + * sanitized (see {@link bbp_filter_anonymous_post_data()} | |
| 730 | 794 | * @param int $author_id Topic or reply author ID |
| 731 | 795 | * @param string $title The title of the content |
| 732 | 796 | * @param string $content The content being posted |
| 733 | - * @uses is_super_admin() Allow super admins to bypass blacklist | |
| 734 | - * @uses bbp_current_author_ip() To get current user IP address | |
| 735 | - * @uses bbp_current_author_ua() To get current user agent | |
| 797 | + * @param mixed $strict False for moderation_keys. True for blacklist_keys. | |
| 798 | + * String for custom keys. | |
| 736 | 799 | * @return bool True if test is passed, false if fail |
| 737 | 800 | */ |
| 738 | -function bbp_check_for_moderation( $anonymous_data = false, $author_id = 0, $title = '', $content = '' ) { | |
| 801 | +function bbp_check_for_moderation( $anonymous_data = array(), $author_id = 0, $title = '', $content = '', $strict = false ) { | |
| 739 | 802 | |
| 740 | - // Bail if super admin is author | |
| 741 | - if ( is_super_admin( $author_id ) ) | |
| 803 | + // Custom moderation option key | |
| 804 | + if ( is_string( $strict ) ) { | |
| 805 | + $strict = sanitize_key( $strict ); | |
| 806 | + | |
| 807 | + // Use custom key | |
| 808 | + if ( ! empty( $strict ) ) { | |
| 809 | + $hook_name = $strict; | |
| 810 | + $option_name = "{$strict}_keys"; | |
| 811 | + | |
| 812 | + // Key was invalid, so default to moderation keys | |
| 813 | + } else { | |
| 814 | + $strict = false; | |
| 815 | + } | |
| 816 | + } | |
| 817 | + | |
| 818 | + // Strict mode uses WordPress "blacklist" settings | |
| 819 | + if ( true === $strict ) { | |
| 820 | + $hook_name = 'blacklist'; | |
| 821 | + $option_name = 'blacklist_keys'; | |
| 822 | + | |
| 823 | + // Non-strict uses WordPress "moderation" settings | |
| 824 | + } elseif ( false === $strict ) { | |
| 825 | + $hook_name = 'moderation'; | |
| 826 | + $option_name = 'moderation_keys'; | |
| 827 | + } | |
| 828 | + | |
| 829 | + // Allow for moderation check to be skipped | |
| 830 | + if ( apply_filters( "bbp_bypass_check_for_{$hook_name}", false, $anonymous_data, $author_id, $title, $content, $strict ) ) { | |
| 742 | 831 | return true; |
| 832 | + } | |
| 743 | 833 | |
| 834 | + // Maybe perform some author-specific capability checks | |
| 835 | + if ( ! empty( $author_id ) ) { | |
| 836 | + | |
| 837 | + // Bail if user is a keymaster | |
| 838 | + if ( bbp_is_user_keymaster( $author_id ) ) { | |
| 839 | + return true; | |
| 840 | + | |
| 841 | + // Bail if user can moderate | |
| 842 | + // https://bbpress.trac.wordpress.org/ticket/2726 | |
| 843 | + } elseif ( ( false === $strict ) && user_can( $author_id, 'moderate' ) ) { | |
| 844 | + return true; | |
| 845 | + } | |
| 846 | + } | |
| 847 | + | |
| 744 | 848 | // Define local variable(s) |
| 745 | 849 | $_post = array(); |
| 746 | 850 | $match_out = ''; |
| 747 | 851 | |
| 748 | - /** Blacklist *************************************************************/ | |
| 852 | + /** Max Links *************************************************************/ | |
| 749 | 853 | |
| 750 | - // Get the moderation keys | |
| 751 | - $blacklist = trim( get_option( 'moderation_keys' ) ); | |
| 854 | + // Only check max_links when not being strict | |
| 855 | + if ( false === $strict ) { | |
| 856 | + $max_links = get_option( 'comment_max_links' ); | |
| 857 | + if ( ! empty( $max_links ) ) { | |
| 752 | 858 | |
| 753 | - // Bail if blacklist is empty | |
| 754 | - if ( empty( $blacklist ) ) | |
| 859 | + // How many links? | |
| 860 | + $num_links = preg_match_all( '/(http|ftp|https):\/\//i', $content, $match_out ); | |
| 861 | + | |
| 862 | + // Allow for bumping the max to include the user's URL | |
| 863 | + if ( ! empty( $_post['url'] ) ) { | |
| 864 | + $num_links = apply_filters( 'comment_max_links_url', $num_links, $_post['url'], $content ); | |
| 865 | + } | |
| 866 | + | |
| 867 | + // Das ist zu viele links! | |
| 868 | + if ( $num_links >= $max_links ) { | |
| 869 | + return false; | |
| 870 | + } | |
| 871 | + } | |
| 872 | + } | |
| 873 | + | |
| 874 | + /** Moderation ************************************************************/ | |
| 875 | + | |
| 876 | + /** | |
| 877 | + * Filters the bbPress moderation keys. | |
| 878 | + * | |
| 879 | + * @since 2.6.0 bbPress (r6050) | |
| 880 | + * | |
| 881 | + * @param string $moderation List of moderation keys. One per new line. | |
| 882 | + */ | |
| 883 | + $moderation = apply_filters( "bbp_{$hook_name}_keys", trim( get_option( $option_name ) ) ); | |
| 884 | + | |
| 885 | + // Bail if no words to look for | |
| 886 | + if ( empty( $moderation ) ) { | |
| 755 | 887 | return true; |
| 888 | + } | |
| 756 | 889 | |
| 757 | 890 | /** User Data *************************************************************/ |
| 758 | 891 | |
| 759 | 892 | // Map anonymous user data |
| 760 | - if ( !empty( $anonymous_data ) ) { | |
| 893 | + if ( ! empty( $anonymous_data ) ) { | |
| 761 | 894 | $_post['author'] = $anonymous_data['bbp_anonymous_name']; |
| 762 | 895 | $_post['email'] = $anonymous_data['bbp_anonymous_email']; |
| 763 | 896 | $_post['url'] = $anonymous_data['bbp_anonymous_website']; |
| 764 | 897 | |
| 765 | 898 | // Map current user data |
| 766 | - } elseif ( !empty( $author_id ) ) { | |
| 899 | + } elseif ( ! empty( $author_id ) ) { | |
| 767 | 900 | |
| 768 | 901 | // Get author data |
| 769 | 902 | $user = get_userdata( $author_id ); |
| 770 | 903 | |
| 771 | 904 | // If data exists, map it |
| 772 | - if ( !empty( $user ) ) { | |
| 905 | + if ( ! empty( $user ) ) { | |
| 773 | 906 | $_post['author'] = $user->display_name; |
| 774 | 907 | $_post['email'] = $user->user_email; |
| 775 | 908 | $_post['url'] = $user->user_url; |
| 776 | 909 | } |
| @@ -783,29 +916,15 @@ | ||
| 783 | 916 | // Post title and content |
| 784 | 917 | $_post['title'] = $title; |
| 785 | 918 | $_post['content'] = $content; |
| 786 | 919 | |
| 787 | - /** Max Links *************************************************************/ | |
| 920 | + // Ensure HTML tags are not being used to bypass the moderation list. | |
| 921 | + $_post['comment_without_html'] = wp_strip_all_tags( $content ); | |
| 788 | 922 | |
| 789 | - $max_links = get_option( 'comment_max_links' ); | |
| 790 | - if ( !empty( $max_links ) ) { | |
| 791 | - | |
| 792 | - // How many links? | |
| 793 | - $num_links = preg_match_all( '/<a [^>]*href/i', $content, $match_out ); | |
| 794 | - | |
| 795 | - // Allow for bumping the max to include the user's URL | |
| 796 | - $num_links = apply_filters( 'comment_max_links_url', $num_links, $_post['url'] ); | |
| 797 | - | |
| 798 | - // Das ist zu viele links! | |
| 799 | - if ( $num_links >= $max_links ) { | |
| 800 | - return false; | |
| 801 | - } | |
| 802 | - } | |
| 803 | - | |
| 804 | 923 | /** Words *****************************************************************/ |
| 805 | 924 | |
| 806 | 925 | // Get words separated by new lines |
| 807 | - $words = explode( "\n", $blacklist ); | |
| 926 | + $words = explode( "\n", $moderation ); | |
| 808 | 927 | |
| 809 | 928 | // Loop through words |
| 810 | 929 | foreach ( (array) $words as $word ) { |
| 811 | 930 | |
| @@ -812,17 +931,19 @@ | ||
| 812 | 931 | // Trim the whitespace from the word |
| 813 | 932 | $word = trim( $word ); |
| 814 | 933 | |
| 815 | 934 | // Skip empty lines |
| 816 | - if ( empty( $word ) ) { continue; } | |
| 935 | + if ( empty( $word ) ) { | |
| 936 | + continue; | |
| 937 | + } | |
| 817 | 938 | |
| 818 | 939 | // Do some escaping magic so that '#' chars in the |
| 819 | 940 | // spam words don't break things: |
| 820 | 941 | $word = preg_quote( $word, '#' ); |
| 821 | - $pattern = "#$word#i"; | |
| 942 | + $pattern = "#{$word}#i"; | |
| 822 | 943 | |
| 823 | 944 | // Loop through post data |
| 824 | - foreach( $_post as $post_data ) { | |
| 945 | + foreach ( $_post as $post_data ) { | |
| 825 | 946 | |
| 826 | 947 | // Check each user data for current word |
| 827 | 948 | if ( preg_match( $pattern, $post_data ) ) { |
| 828 | 949 | |
| @@ -836,262 +957,597 @@ | ||
| 836 | 957 | return true; |
| 837 | 958 | } |
| 838 | 959 | |
| 839 | 960 | /** |
| 840 | - * Checks topics and replies against the discussion blacklist of blocked keys | |
| 961 | + * Deprecated. Use bbp_check_for_moderation() with strict flag set. | |
| 841 | 962 | * |
| 842 | - * @since bbPress (r3446) | |
| 963 | + * @since 2.0.0 bbPress (r3446) | |
| 964 | + * @since 2.6.0 bbPress (r6854) | |
| 965 | + * @deprecated 2.6.0 Use bbp_check_for_moderation() with strict flag set | |
| 966 | + */ | |
| 967 | +function bbp_check_for_blacklist( $anonymous_data = array(), $author_id = 0, $title = '', $content = '' ) { | |
| 968 | + return bbp_check_for_moderation( $anonymous_data, $author_id, $title, $content, true ); | |
| 969 | +} | |
| 970 | + | |
| 971 | +/** Subscriptions *************************************************************/ | |
| 972 | + | |
| 973 | +/** | |
| 974 | + * Get the "Do Not Reply" email address to use when sending subscription emails. | |
| 843 | 975 | * |
| 844 | - * @param array $anonymous_data Anonymous user data | |
| 845 | - * @param int $author_id Topic or reply author ID | |
| 846 | - * @param string $title The title of the content | |
| 847 | - * @param string $content The content being posted | |
| 848 | - * @uses is_super_admin() Allow super admins to bypass blacklist | |
| 849 | - * @uses bbp_current_author_ip() To get current user IP address | |
| 850 | - * @uses bbp_current_author_ua() To get current user agent | |
| 851 | - * @return bool True if test is passed, false if fail | |
| 976 | + * We make some educated guesses here based on the home URL. Filters are | |
| 977 | + * available to customize this address further. In the future, we may consider | |
| 978 | + * using `admin_email` instead, though this is not normally publicized. | |
| 979 | + * | |
| 980 | + * We use `$_SERVER['SERVER_NAME']` here to mimic similar functionality in | |
| 981 | + * WordPress core. Previously, we used `get_home_url()` to use already validated | |
| 982 | + * user input, but it was causing issues in some installations. | |
| 983 | + * | |
| 984 | + * @since 2.6.0 bbPress (r5409) | |
| 985 | + * | |
| 986 | + * @see wp_mail | |
| 987 | + * @see wp_notify_postauthor | |
| 988 | + * @link https://bbpress.trac.wordpress.org/ticket/2618 | |
| 989 | + * | |
| 990 | + * @return string | |
| 852 | 991 | */ |
| 853 | -function bbp_check_for_blacklist( $anonymous_data = false, $author_id = 0, $title = '', $content = '' ) { | |
| 992 | +function bbp_get_do_not_reply_address() { | |
| 993 | + $sitename = strtolower( $_SERVER['SERVER_NAME'] ); | |
| 994 | + if ( substr( $sitename, 0, 4 ) === 'www.' ) { | |
| 995 | + $sitename = substr( $sitename, 4 ); | |
| 996 | + } | |
| 854 | 997 | |
| 855 | - // Bail if super admin is author | |
| 856 | - if ( is_super_admin( $author_id ) ) | |
| 857 | - return true; | |
| 998 | + // Filter & return | |
| 999 | + return apply_filters( 'bbp_get_do_not_reply_address', 'noreply@' . $sitename ); | |
| 1000 | +} | |
| 858 | 1001 | |
| 859 | - // Define local variable | |
| 860 | - $_post = array(); | |
| 1002 | +/** | |
| 1003 | + * Sends notification emails for new replies to subscribed topics | |
| 1004 | + * | |
| 1005 | + * Gets new post ID and check if there are subscribed users to that topic, and | |
| 1006 | + * if there are, send notifications | |
| 1007 | + * | |
| 1008 | + * Note: in bbPress 2.6, we've moved away from 1 email per subscriber to 1 email | |
| 1009 | + * with everyone BCC'd. This may have negative repercussions for email services | |
| 1010 | + * that limit the number of addresses in a BCC field (often to around 500.) In | |
| 1011 | + * those cases, we recommend unhooking this function and creating your own | |
| 1012 | + * custom email script. | |
| 1013 | + * | |
| 1014 | + * @since 2.6.0 bbPress (r5413) | |
| 1015 | + * | |
| 1016 | + * @param int $reply_id ID of the newly made reply | |
| 1017 | + * @param int $topic_id ID of the topic of the reply | |
| 1018 | + * @param int $forum_id ID of the forum of the reply | |
| 1019 | + * @param array $anonymous_data Optional - if it's an anonymous post. Do not | |
| 1020 | + * supply if supplying $author_id. Should be | |
| 1021 | + * sanitized (see {@link bbp_filter_anonymous_post_data()} | |
| 1022 | + * @param int $reply_author ID of the topic author ID | |
| 1023 | + * @return bool True on success, false on failure | |
| 1024 | + */ | |
| 1025 | +function bbp_notify_topic_subscribers( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $reply_author = 0 ) { | |
| 861 | 1026 | |
| 862 | - /** Blacklist *************************************************************/ | |
| 1027 | + // Bail if subscriptions are turned off | |
| 1028 | + if ( ! bbp_is_subscriptions_active() ) { | |
| 1029 | + return false; | |
| 1030 | + } | |
| 863 | 1031 | |
| 864 | - // Get the moderation keys | |
| 865 | - $blacklist = trim( get_option( 'blacklist_keys' ) ); | |
| 1032 | + // Bail if importing | |
| 1033 | + if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { | |
| 1034 | + return false; | |
| 1035 | + } | |
| 866 | 1036 | |
| 867 | - // Bail if blacklist is empty | |
| 868 | - if ( empty( $blacklist ) ) | |
| 869 | - return true; | |
| 1037 | + /** Validation ************************************************************/ | |
| 870 | 1038 | |
| 871 | - /** User Data *************************************************************/ | |
| 1039 | + $reply_id = bbp_get_reply_id( $reply_id ); | |
| 1040 | + $topic_id = bbp_get_topic_id( $topic_id ); | |
| 1041 | + $forum_id = bbp_get_forum_id( $forum_id ); | |
| 872 | 1042 | |
| 873 | - // Map anonymous user data | |
| 874 | - if ( !empty( $anonymous_data ) ) { | |
| 875 | - $_post['author'] = $anonymous_data['bbp_anonymous_name']; | |
| 876 | - $_post['email'] = $anonymous_data['bbp_anonymous_email']; | |
| 877 | - $_post['url'] = $anonymous_data['bbp_anonymous_website']; | |
| 1043 | + /** Topic *****************************************************************/ | |
| 878 | 1044 | |
| 879 | - // Map current user data | |
| 880 | - } elseif ( !empty( $author_id ) ) { | |
| 1045 | + // Bail if topic is not public (includes closed) | |
| 1046 | + if ( ! bbp_is_topic_public( $topic_id ) ) { | |
| 1047 | + return false; | |
| 1048 | + } | |
| 881 | 1049 | |
| 882 | - // Get author data | |
| 883 | - $user = get_userdata( $author_id ); | |
| 1050 | + /** Reply *****************************************************************/ | |
| 884 | 1051 | |
| 885 | - // If data exists, map it | |
| 886 | - if ( !empty( $user ) ) { | |
| 887 | - $_post['author'] = $user->display_name; | |
| 888 | - $_post['email'] = $user->user_email; | |
| 889 | - $_post['url'] = $user->user_url; | |
| 890 | - } | |
| 1052 | + // Bail if reply is not published | |
| 1053 | + if ( ! bbp_is_reply_published( $reply_id ) ) { | |
| 1054 | + return false; | |
| 891 | 1055 | } |
| 892 | 1056 | |
| 893 | - // Current user IP and user agent | |
| 894 | - $_post['user_ip'] = bbp_current_author_ip(); | |
| 895 | - $_post['user_ua'] = bbp_current_author_ua(); | |
| 1057 | + // Poster name | |
| 1058 | + $reply_author_name = bbp_get_reply_author_display_name( $reply_id ); | |
| 896 | 1059 | |
| 897 | - // Post title and content | |
| 898 | - $_post['title'] = $title; | |
| 899 | - $_post['content'] = $content; | |
| 1060 | + /** Users *****************************************************************/ | |
| 900 | 1061 | |
| 901 | - /** Words *****************************************************************/ | |
| 1062 | + // Get topic subscribers and bail if empty | |
| 1063 | + $user_ids = bbp_get_subscribers( $topic_id ); | |
| 902 | 1064 | |
| 903 | - // Get words separated by new lines | |
| 904 | - $words = explode( "\n", $blacklist ); | |
| 1065 | + // Remove the reply author from the list. | |
| 1066 | + $reply_author_key = array_search( (int) $reply_author, $user_ids, true ); | |
| 1067 | + if ( false !== $reply_author_key ) { | |
| 1068 | + unset( $user_ids[ $reply_author_key ] ); | |
| 1069 | + } | |
| 905 | 1070 | |
| 906 | - // Loop through words | |
| 907 | - foreach ( (array) $words as $word ) { | |
| 1071 | + // Dedicated filter to manipulate user ID's to send emails to | |
| 1072 | + $user_ids = (array) apply_filters( 'bbp_topic_subscription_user_ids', $user_ids, $reply_id, $topic_id ); | |
| 908 | 1073 | |
| 909 | - // Trim the whitespace from the word | |
| 910 | - $word = trim( $word ); | |
| 1074 | + // Bail of the reply author was the only one subscribed. | |
| 1075 | + if ( empty( $user_ids ) ) { | |
| 1076 | + return false; | |
| 1077 | + } | |
| 911 | 1078 | |
| 912 | - // Skip empty lines | |
| 913 | - if ( empty( $word ) ) { continue; } | |
| 1079 | + // Get email addresses, bail if empty | |
| 1080 | + $email_addresses = bbp_get_email_addresses_from_user_ids( $user_ids ); | |
| 1081 | + if ( empty( $email_addresses ) ) { | |
| 1082 | + return false; | |
| 1083 | + } | |
| 914 | 1084 | |
| 915 | - // Do some escaping magic so that '#' chars in the | |
| 916 | - // spam words don't break things: | |
| 917 | - $word = preg_quote( $word, '#' ); | |
| 918 | - $pattern = "#$word#i"; | |
| 1085 | + /** Mail ******************************************************************/ | |
| 919 | 1086 | |
| 920 | - // Loop through post data | |
| 921 | - foreach( $_post as $post_data ) { | |
| 1087 | + // Remove filters from reply content and topic title to prevent content | |
| 1088 | + // from being encoded with HTML entities, wrapped in paragraph tags, etc... | |
| 1089 | + bbp_remove_all_filters( 'bbp_get_reply_content' ); | |
| 1090 | + bbp_remove_all_filters( 'bbp_get_topic_title' ); | |
| 1091 | + bbp_remove_all_filters( 'the_title' ); | |
| 922 | 1092 | |
| 923 | - // Check each user data for current word | |
| 924 | - if ( preg_match( $pattern, $post_data ) ) { | |
| 1093 | + // Strip tags from text and setup mail data | |
| 1094 | + $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); | |
| 1095 | + $topic_title = wp_specialchars_decode( strip_tags( bbp_get_topic_title( $topic_id ) ), ENT_QUOTES ); | |
| 1096 | + $reply_author_name = wp_specialchars_decode( strip_tags( $reply_author_name ), ENT_QUOTES ); | |
| 1097 | + $reply_content = wp_specialchars_decode( strip_tags( bbp_get_reply_content( $reply_id ) ), ENT_QUOTES ); | |
| 1098 | + $reply_url = bbp_get_reply_url( $reply_id ); | |
| 925 | 1099 | |
| 926 | - // Post does not pass | |
| 927 | - return false; | |
| 928 | - } | |
| 929 | - } | |
| 1100 | + // For plugins to filter messages per reply/topic/user | |
| 1101 | + $message = sprintf( esc_html__( '%1$s wrote: | |
| 1102 | + | |
| 1103 | +%2$s | |
| 1104 | + | |
| 1105 | +Post Link: %3$s | |
| 1106 | + | |
| 1107 | +----------- | |
| 1108 | + | |
| 1109 | +You are receiving this email because you subscribed to a forum topic. | |
| 1110 | + | |
| 1111 | +Login and visit the topic to unsubscribe from these emails.', 'bbpress' ), | |
| 1112 | + | |
| 1113 | + $reply_author_name, | |
| 1114 | + $reply_content, | |
| 1115 | + $reply_url | |
| 1116 | + ); | |
| 1117 | + | |
| 1118 | + $message = apply_filters( 'bbp_subscription_mail_message', $message, $reply_id, $topic_id ); | |
| 1119 | + if ( empty( $message ) ) { | |
| 1120 | + return; | |
| 930 | 1121 | } |
| 931 | 1122 | |
| 932 | - // Check passed successfully | |
| 1123 | + // For plugins to filter titles per reply/topic/user | |
| 1124 | + $subject = apply_filters( 'bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id ); | |
| 1125 | + if ( empty( $subject ) ) { | |
| 1126 | + return; | |
| 1127 | + } | |
| 1128 | + | |
| 1129 | + /** Headers ***************************************************************/ | |
| 1130 | + | |
| 1131 | + // Default bbPress X-header | |
| 1132 | + $headers = array( bbp_get_email_header() ); | |
| 1133 | + | |
| 1134 | + // Get the noreply@ address | |
| 1135 | + $no_reply = bbp_get_do_not_reply_address(); | |
| 1136 | + | |
| 1137 | + // Setup "From" email address | |
| 1138 | + $from_email = apply_filters( 'bbp_subscription_from_email', $no_reply ); | |
| 1139 | + | |
| 1140 | + // Setup the From header | |
| 1141 | + $headers[] = 'From: ' . get_bloginfo( 'name' ) . ' <' . $from_email . '>'; | |
| 1142 | + | |
| 1143 | + // Loop through addresses | |
| 1144 | + foreach ( (array) $email_addresses as $address ) { | |
| 1145 | + $headers[] = 'Bcc: ' . $address; | |
| 1146 | + } | |
| 1147 | + | |
| 1148 | + /** Send it ***************************************************************/ | |
| 1149 | + | |
| 1150 | + // Custom headers | |
| 1151 | + $headers = apply_filters( 'bbp_subscription_mail_headers', $headers ); | |
| 1152 | + $to_email = apply_filters( 'bbp_subscription_to_email', $no_reply ); | |
| 1153 | + | |
| 1154 | + // Before | |
| 1155 | + do_action( 'bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids ); | |
| 1156 | + | |
| 1157 | + // Send notification email | |
| 1158 | + wp_mail( $to_email, $subject, $message, $headers ); | |
| 1159 | + | |
| 1160 | + // After | |
| 1161 | + do_action( 'bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids ); | |
| 1162 | + | |
| 1163 | + // Restore previously removed filters | |
| 1164 | + bbp_restore_all_filters( 'bbp_get_topic_content' ); | |
| 1165 | + bbp_restore_all_filters( 'bbp_get_topic_title' ); | |
| 1166 | + bbp_restore_all_filters( 'the_title' ); | |
| 1167 | + | |
| 933 | 1168 | return true; |
| 934 | 1169 | } |
| 935 | 1170 | |
| 936 | -/** Subscriptions *************************************************************/ | |
| 937 | - | |
| 938 | 1171 | /** |
| 939 | - * Sends notification emails for new posts | |
| 1172 | + * Sends notification emails for new topics to subscribed forums | |
| 940 | 1173 | * |
| 941 | - * Gets new post's ID and check if there are subscribed users to that topic, and | |
| 1174 | + * Gets new post ID and check if there are subscribed users to that forum, and | |
| 942 | 1175 | * if there are, send notifications |
| 943 | 1176 | * |
| 944 | - * @since bbPress (r2668) | |
| 1177 | + * Note: in bbPress 2.6, we've moved away from 1 email per subscriber to 1 email | |
| 1178 | + * with everyone BCC'd. This may have negative repercussions for email services | |
| 1179 | + * that limit the number of addresses in a BCC field (often to around 500.) In | |
| 1180 | + * those cases, we recommend unhooking this function and creating your own | |
| 1181 | + * custom email script. | |
| 945 | 1182 | * |
| 946 | - * @param int $reply_id ID of the newly made reply | |
| 947 | - * @uses bbp_is_subscriptions_active() To check if the subscriptions are active | |
| 948 | - * @uses bbp_get_reply_id() To validate the reply ID | |
| 949 | - * @uses bbp_get_reply() To get the reply | |
| 950 | - * @uses bbp_get_reply_topic_id() To get the topic ID of the reply | |
| 951 | - * @uses bbp_is_reply_published() To make sure the reply is published | |
| 952 | - * @uses bbp_get_topic_id() To validate the topic ID | |
| 953 | - * @uses bbp_get_topic() To get the reply's topic | |
| 954 | - * @uses bbp_is_topic_published() To make sure the topic is published | |
| 955 | - * @uses get_the_author_meta() To get the author's display name | |
| 956 | - * @uses do_action() Calls 'bbp_pre_notify_subscribers' with the reply id and | |
| 957 | - * topic id | |
| 958 | - * @uses bbp_get_topic_subscribers() To get the topic subscribers | |
| 959 | - * @uses apply_filters() Calls 'bbp_subscription_mail_message' with the | |
| 960 | - * message, reply id, topic id and user id | |
| 961 | - * @uses get_userdata() To get the user data | |
| 962 | - * @uses wp_mail() To send the mail | |
| 963 | - * @uses do_action() Calls 'bbp_post_notify_subscribers' with the reply id | |
| 964 | - * and topic id | |
| 1183 | + * @since 2.5.0 bbPress (r5156) | |
| 1184 | + * | |
| 1185 | + * @param int $topic_id ID of the newly made reply | |
| 1186 | + * @param int $forum_id ID of the forum for the topic | |
| 1187 | + * @param array $anonymous_data Optional - if it's an anonymous post. Do not | |
| 1188 | + * supply if supplying $author_id. Should be | |
| 1189 | + * sanitized (see {@link bbp_filter_anonymous_post_data()} | |
| 1190 | + * @param int $topic_author ID of the topic author ID | |
| 965 | 1191 | * @return bool True on success, false on failure |
| 966 | 1192 | */ |
| 967 | -function bbp_notify_subscribers( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $reply_author = 0 ) { | |
| 1193 | +function bbp_notify_forum_subscribers( $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $topic_author = 0 ) { | |
| 968 | 1194 | |
| 969 | 1195 | // Bail if subscriptions are turned off |
| 970 | - if ( !bbp_is_subscriptions_active() ) | |
| 1196 | + if ( ! bbp_is_subscriptions_active() ) { | |
| 971 | 1197 | return false; |
| 1198 | + } | |
| 972 | 1199 | |
| 1200 | + // Bail if importing | |
| 1201 | + if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { | |
| 1202 | + return false; | |
| 1203 | + } | |
| 1204 | + | |
| 973 | 1205 | /** Validation ************************************************************/ |
| 974 | 1206 | |
| 975 | - $reply_id = bbp_get_reply_id( $reply_id ); | |
| 976 | 1207 | $topic_id = bbp_get_topic_id( $topic_id ); |
| 977 | 1208 | $forum_id = bbp_get_forum_id( $forum_id ); |
| 978 | 1209 | |
| 979 | - /** Reply *****************************************************************/ | |
| 1210 | + /** | |
| 1211 | + * Necessary for backwards compatibility | |
| 1212 | + * | |
| 1213 | + * @see https://bbpress.trac.wordpress.org/ticket/2620 | |
| 1214 | + */ | |
| 1215 | + $user_id = 0; | |
| 980 | 1216 | |
| 981 | - // Bail if reply is not published | |
| 982 | - if ( !bbp_is_reply_published( $reply_id ) ) | |
| 1217 | + /** Topic *****************************************************************/ | |
| 1218 | + | |
| 1219 | + // Bail if topic is not public (includes closed) | |
| 1220 | + if ( ! bbp_is_topic_public( $topic_id ) ) { | |
| 983 | 1221 | return false; |
| 1222 | + } | |
| 984 | 1223 | |
| 985 | - /** Topic *****************************************************************/ | |
| 1224 | + // Poster name | |
| 1225 | + $topic_author_name = bbp_get_topic_author_display_name( $topic_id ); | |
| 986 | 1226 | |
| 987 | - // Bail if topic is not published | |
| 988 | - if ( !bbp_is_topic_published( $topic_id ) ) | |
| 989 | - return false; | |
| 1227 | + /** Users *****************************************************************/ | |
| 990 | 1228 | |
| 991 | - /** User ******************************************************************/ | |
| 1229 | + // Get topic subscribers and bail if empty | |
| 1230 | + $user_ids = bbp_get_subscribers( $forum_id ); | |
| 992 | 1231 | |
| 993 | - // Get subscribers and bail if empty | |
| 994 | - $user_ids = bbp_get_topic_subscribers( $topic_id, true ); | |
| 995 | - if ( empty( $user_ids ) ) | |
| 1232 | + // Remove the topic author from the list. | |
| 1233 | + $topic_author_key = array_search( (int) $topic_author, $user_ids, true ); | |
| 1234 | + if ( false !== $topic_author_key ) { | |
| 1235 | + unset( $user_ids[ $topic_author_key ] ); | |
| 1236 | + } | |
| 1237 | + | |
| 1238 | + // Dedicated filter to manipulate user ID's to send emails to | |
| 1239 | + $user_ids = (array) apply_filters( 'bbp_forum_subscription_user_ids', $user_ids, $topic_id, $forum_id ); | |
| 1240 | + | |
| 1241 | + // Bail of the reply author was the only one subscribed. | |
| 1242 | + if ( empty( $user_ids ) ) { | |
| 996 | 1243 | return false; |
| 1244 | + } | |
| 997 | 1245 | |
| 998 | - // Poster name | |
| 999 | - $reply_author_name = bbp_get_reply_author_display_name( $reply_id ); | |
| 1246 | + // Get email addresses, bail if empty | |
| 1247 | + $email_addresses = bbp_get_email_addresses_from_user_ids( $user_ids ); | |
| 1248 | + if ( empty( $email_addresses ) ) { | |
| 1249 | + return false; | |
| 1250 | + } | |
| 1000 | 1251 | |
| 1001 | 1252 | /** Mail ******************************************************************/ |
| 1002 | 1253 | |
| 1003 | - do_action( 'bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids ); | |
| 1004 | - | |
| 1005 | 1254 | // Remove filters from reply content and topic title to prevent content |
| 1006 | 1255 | // from being encoded with HTML entities, wrapped in paragraph tags, etc... |
| 1007 | - remove_all_filters( 'bbp_get_reply_content' ); | |
| 1008 | - remove_all_filters( 'bbp_get_topic_title' ); | |
| 1256 | + bbp_remove_all_filters( 'bbp_get_topic_content' ); | |
| 1257 | + bbp_remove_all_filters( 'bbp_get_topic_title' ); | |
| 1258 | + bbp_remove_all_filters( 'the_title' ); | |
| 1009 | 1259 | |
| 1010 | - // Strip tags from text | |
| 1011 | - $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) ); | |
| 1012 | - $reply_content = strip_tags( bbp_get_reply_content( $reply_id ) ); | |
| 1013 | - $reply_url = bbp_get_reply_url( $reply_id ); | |
| 1014 | - $blog_name = get_option( 'blogname' ); | |
| 1260 | + // Strip tags from text and setup mail data | |
| 1261 | + $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); | |
| 1262 | + $topic_title = wp_specialchars_decode( strip_tags( bbp_get_topic_title( $topic_id ) ), ENT_QUOTES ); | |
| 1263 | + $topic_author_name = wp_specialchars_decode( strip_tags( $topic_author_name ), ENT_QUOTES ); | |
| 1264 | + $topic_content = wp_specialchars_decode( strip_tags( bbp_get_topic_content( $topic_id ) ), ENT_QUOTES ); | |
| 1265 | + $topic_url = get_permalink( $topic_id ); | |
| 1015 | 1266 | |
| 1016 | - // Loop through users | |
| 1017 | - foreach ( (array) $user_ids as $user_id ) { | |
| 1267 | + // For plugins to filter messages per reply/topic/user | |
| 1268 | + $message = sprintf( esc_html__( '%1$s wrote: | |
| 1018 | 1269 | |
| 1019 | - // Don't send notifications to the person who made the post | |
| 1020 | - if ( !empty( $reply_author ) && (int) $user_id == (int) $reply_author ) | |
| 1021 | - continue; | |
| 1270 | +%2$s | |
| 1022 | 1271 | |
| 1023 | - // For plugins to filter messages per reply/topic/user | |
| 1024 | - $message = sprintf( __( '%1$s wrote: | |
| 1272 | +Topic Link: %3$s | |
| 1025 | 1273 | |
| 1026 | -%2$s | |
| 1027 | - | |
| 1028 | -Post Link: %3$s | |
| 1029 | - | |
| 1030 | 1274 | ----------- |
| 1031 | 1275 | |
| 1032 | -You are receiving this email because you subscribed to a forum topic. | |
| 1276 | +You are receiving this email because you subscribed to a forum. | |
| 1033 | 1277 | |
| 1034 | 1278 | Login and visit the topic to unsubscribe from these emails.', 'bbpress' ), |
| 1035 | - | |
| 1036 | - $reply_author_name, | |
| 1037 | - $reply_content, | |
| 1038 | - $reply_url | |
| 1039 | - ); | |
| 1040 | 1279 | |
| 1041 | - $message = apply_filters( 'bbp_subscription_mail_message', $message, $reply_id, $topic_id, $user_id ); | |
| 1042 | - if ( empty( $message ) ) | |
| 1043 | - continue; | |
| 1280 | + $topic_author_name, | |
| 1281 | + $topic_content, | |
| 1282 | + $topic_url | |
| 1283 | + ); | |
| 1044 | 1284 | |
| 1045 | - // For plugins to filter titles per reply/topic/user | |
| 1046 | - $subject = apply_filters( 'bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id, $user_id ); | |
| 1047 | - if ( empty( $subject ) ) | |
| 1048 | - continue; | |
| 1285 | + $message = apply_filters( 'bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id ); | |
| 1286 | + if ( empty( $message ) ) { | |
| 1287 | + return; | |
| 1288 | + } | |
| 1049 | 1289 | |
| 1050 | - // Custom headers | |
| 1051 | - $headers = apply_filters( 'bbp_subscription_mail_headers', array() ); | |
| 1290 | + // For plugins to filter titles per reply/topic/user | |
| 1291 | + $subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id ); | |
| 1292 | + if ( empty( $subject ) ) { | |
| 1293 | + return; | |
| 1294 | + } | |
| 1052 | 1295 | |
| 1053 | - // Get user data of this user | |
| 1054 | - $user = get_userdata( $user_id ); | |
| 1296 | + /** Headers ***************************************************************/ | |
| 1055 | 1297 | |
| 1056 | - // Send notification email | |
| 1057 | - wp_mail( $user->user_email, $subject, $message, $headers ); | |
| 1298 | + // Default bbPress X-header | |
| 1299 | + $headers = array( bbp_get_email_header() ); | |
| 1300 | + | |
| 1301 | + // Get the noreply@ address | |
| 1302 | + $no_reply = bbp_get_do_not_reply_address(); | |
| 1303 | + | |
| 1304 | + // Setup "From" email address | |
| 1305 | + $from_email = apply_filters( 'bbp_subscription_from_email', $no_reply ); | |
| 1306 | + | |
| 1307 | + // Setup the From header | |
| 1308 | + $headers[] = 'From: ' . get_bloginfo( 'name' ) . ' <' . $from_email . '>'; | |
| 1309 | + | |
| 1310 | + // Loop through addresses | |
| 1311 | + foreach ( (array) $email_addresses as $address ) { | |
| 1312 | + $headers[] = 'Bcc: ' . $address; | |
| 1058 | 1313 | } |
| 1059 | 1314 | |
| 1060 | - do_action( 'bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids ); | |
| 1315 | + /** Send it ***************************************************************/ | |
| 1061 | 1316 | |
| 1317 | + // Custom headers | |
| 1318 | + $headers = apply_filters( 'bbp_subscription_mail_headers', $headers ); | |
| 1319 | + $to_email = apply_filters( 'bbp_subscription_to_email', $no_reply ); | |
| 1320 | + | |
| 1321 | + // Before | |
| 1322 | + do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids ); | |
| 1323 | + | |
| 1324 | + // Send notification email | |
| 1325 | + wp_mail( $to_email, $subject, $message, $headers ); | |
| 1326 | + | |
| 1327 | + // After | |
| 1328 | + do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids ); | |
| 1329 | + | |
| 1330 | + // Restore previously removed filters | |
| 1331 | + bbp_restore_all_filters( 'bbp_get_topic_content' ); | |
| 1332 | + bbp_restore_all_filters( 'bbp_get_topic_title' ); | |
| 1333 | + bbp_restore_all_filters( 'the_title' ); | |
| 1334 | + | |
| 1062 | 1335 | return true; |
| 1063 | 1336 | } |
| 1064 | 1337 | |
| 1338 | +/** | |
| 1339 | + * Sends notification emails for new replies to subscribed topics | |
| 1340 | + * | |
| 1341 | + * This function is deprecated. Please use: bbp_notify_topic_subscribers() | |
| 1342 | + * | |
| 1343 | + * @since 2.0.0 bbPress (r2668) | |
| 1344 | + * | |
| 1345 | + * @deprecated 2.6.0 bbPress (r5412) | |
| 1346 | + * | |
| 1347 | + * @param int $reply_id ID of the newly made reply | |
| 1348 | + * @param int $topic_id ID of the topic of the reply | |
| 1349 | + * @param int $forum_id ID of the forum of the reply | |
| 1350 | + * @param array $anonymous_data Optional - if it's an anonymous post. Do not | |
| 1351 | + * supply if supplying $author_id. Should be | |
| 1352 | + * sanitized (see {@link bbp_filter_anonymous_post_data()} | |
| 1353 | + * @param int $reply_author ID of the topic author ID | |
| 1354 | + * | |
| 1355 | + * @return bool True on success, false on failure | |
| 1356 | + */ | |
| 1357 | +function bbp_notify_subscribers( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $reply_author = 0 ) { | |
| 1358 | + return bbp_notify_topic_subscribers( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author ); | |
| 1359 | +} | |
| 1360 | + | |
| 1361 | +/** | |
| 1362 | + * Return an array of user email addresses from an array of user IDs | |
| 1363 | + * | |
| 1364 | + * @since 2.6.0 bbPress (r6722) | |
| 1365 | + * | |
| 1366 | + * @param array $user_ids | |
| 1367 | + * @return array | |
| 1368 | + */ | |
| 1369 | +function bbp_get_email_addresses_from_user_ids( $user_ids = array() ) { | |
| 1370 | + | |
| 1371 | + // Default return value | |
| 1372 | + $retval = array(); | |
| 1373 | + | |
| 1374 | + // Maximum number of users to get per database query | |
| 1375 | + $limit = apply_filters( 'bbp_get_users_chunk_limit', 100 ); | |
| 1376 | + | |
| 1377 | + // Only do the work if there are user IDs to query for | |
| 1378 | + if ( ! empty( $user_ids ) ) { | |
| 1379 | + | |
| 1380 | + // Get total number of sets | |
| 1381 | + $steps = ceil( count( $user_ids ) / $limit ); | |
| 1382 | + $range = array_map( 'intval', range( 1, $steps ) ); | |
| 1383 | + | |
| 1384 | + // Loop through users | |
| 1385 | + foreach ( $range as $loop ) { | |
| 1386 | + | |
| 1387 | + // Initial loop has no offset | |
| 1388 | + $offset = $limit * ( $loop - 1 ); | |
| 1389 | + | |
| 1390 | + // Calculate user IDs to include | |
| 1391 | + $loop_ids = array_slice( $user_ids, $offset, $limit ); | |
| 1392 | + | |
| 1393 | + // Skip if something went wrong | |
| 1394 | + if ( empty( $loop_ids ) ) { | |
| 1395 | + continue; | |
| 1396 | + } | |
| 1397 | + | |
| 1398 | + // Call get_users() in a way that users are cached | |
| 1399 | + $loop_users = get_users( array( | |
| 1400 | + 'blog_id' => 0, | |
| 1401 | + 'fields' => 'all_with_meta', | |
| 1402 | + 'include' => $loop_ids | |
| 1403 | + ) ); | |
| 1404 | + | |
| 1405 | + // Pluck emails from users | |
| 1406 | + $loop_emails = wp_list_pluck( $loop_users, 'user_email' ); | |
| 1407 | + | |
| 1408 | + // Clean-up memory, for big user sets | |
| 1409 | + unset( $loop_users ); | |
| 1410 | + | |
| 1411 | + // Merge users into return value | |
| 1412 | + if ( ! empty( $loop_emails ) ) { | |
| 1413 | + $retval = array_merge( $retval, $loop_emails ); | |
| 1414 | + } | |
| 1415 | + } | |
| 1416 | + | |
| 1417 | + // No duplicates | |
| 1418 | + $retval = bbp_get_unique_array_values( $retval ); | |
| 1419 | + } | |
| 1420 | + | |
| 1421 | + // Filter & return | |
| 1422 | + return apply_filters( 'bbp_get_email_addresses_from_user_ids', $retval, $user_ids, $limit ); | |
| 1423 | +} | |
| 1424 | + | |
| 1425 | +/** | |
| 1426 | + * Automatically splits bbPress emails with many Bcc recipients into chunks. | |
| 1427 | + * | |
| 1428 | + * This middleware is useful because topics and forums with many subscribers | |
| 1429 | + * run into problems with Bcc limits, and many hosting companies & third-party | |
| 1430 | + * services limit the size of a Bcc audience to prevent spamming. | |
| 1431 | + * | |
| 1432 | + * The default "chunk" size is 40 users per iteration, and can be filtered if | |
| 1433 | + * desired. A future version of bbPress will introduce a setting to more easily | |
| 1434 | + * tune this. | |
| 1435 | + * | |
| 1436 | + * @since 2.6.0 bbPress (r6918) | |
| 1437 | + * | |
| 1438 | + * @param array $args Original arguments passed to wp_mail(). | |
| 1439 | + * @return array | |
| 1440 | + */ | |
| 1441 | +function bbp_chunk_emails( $args = array() ) { | |
| 1442 | + | |
| 1443 | + // Get the maximum number of Bcc's per chunk | |
| 1444 | + $max_num = apply_filters( 'bbp_get_bcc_chunk_limit', 40, $args ); | |
| 1445 | + | |
| 1446 | + // Look for "bcc: " in a case-insensitive way, and split into 2 sets | |
| 1447 | + $match = '/^bcc: (\w+)/i'; | |
| 1448 | + $old_headers = preg_grep( $match, $args['headers'], PREG_GREP_INVERT ); | |
| 1449 | + $bcc_headers = preg_grep( $match, $args['headers'] ); | |
| 1450 | + | |
| 1451 | + // Bail if less than $max_num recipients | |
| 1452 | + if ( empty( $bcc_headers ) || ( count( $bcc_headers ) < $max_num ) ) { | |
| 1453 | + return $args; | |
| 1454 | + } | |
| 1455 | + | |
| 1456 | + // Reindex the headers arrays | |
| 1457 | + $old_headers = array_values( $old_headers ); | |
| 1458 | + $bcc_headers = array_values( $bcc_headers ); | |
| 1459 | + | |
| 1460 | + // Break the Bcc emails into chunks | |
| 1461 | + foreach ( array_chunk( $bcc_headers, $max_num ) as $i => $chunk ) { | |
| 1462 | + | |
| 1463 | + // Skip the first chunk (it will get used in the original wp_mail() call) | |
| 1464 | + if ( 0 === $i ) { | |
| 1465 | + $first_chunk = $chunk; | |
| 1466 | + continue; | |
| 1467 | + } | |
| 1468 | + | |
| 1469 | + // Send out the chunk | |
| 1470 | + $chunk_headers = array_merge( $old_headers, $chunk ); | |
| 1471 | + | |
| 1472 | + // Recursion alert, but should be OK! | |
| 1473 | + wp_mail( | |
| 1474 | + $args['to'], | |
| 1475 | + $args['subject'], | |
| 1476 | + $args['message'], | |
| 1477 | + $chunk_headers, | |
| 1478 | + $args['attachments'] | |
| 1479 | + ); | |
| 1480 | + } | |
| 1481 | + | |
| 1482 | + // Set headers to old headers + the $first_chunk of Bcc's | |
| 1483 | + $args['headers'] = array_merge( $old_headers, $first_chunk ); | |
| 1484 | + | |
| 1485 | + // Return the reduced args, with the first chunk of Bcc's | |
| 1486 | + return $args; | |
| 1487 | +} | |
| 1488 | + | |
| 1489 | +/** | |
| 1490 | + * Return the string used for the bbPress specific X-header. | |
| 1491 | + * | |
| 1492 | + * @since 2.6.0 bbPress (r6919) | |
| 1493 | + * | |
| 1494 | + * @return string | |
| 1495 | + */ | |
| 1496 | +function bbp_get_email_header() { | |
| 1497 | + return apply_filters( 'bbp_get_email_header', 'X-bbPress: ' . bbp_get_version() ); | |
| 1498 | +} | |
| 1499 | + | |
| 1065 | 1500 | /** Login *********************************************************************/ |
| 1066 | 1501 | |
| 1067 | 1502 | /** |
| 1068 | 1503 | * Return a clean and reliable logout URL |
| 1069 | 1504 | * |
| 1070 | - * @param string $url URL | |
| 1505 | + * This function is used to filter `logout_url`. If no $redirect_to value is | |
| 1506 | + * passed, it will default to the request uri, then the forum root. | |
| 1507 | + * | |
| 1508 | + * See: `wp_logout_url()` | |
| 1509 | + * | |
| 1510 | + * @since 2.1.0 bbPress (2815) | |
| 1511 | + * | |
| 1512 | + * @param string $url URL used to log out | |
| 1071 | 1513 | * @param string $redirect_to Where to redirect to? |
| 1072 | - * @uses add_query_arg() To add args to the url | |
| 1073 | - * @uses apply_filters() Calls 'bbp_logout_url' with the url and redirect to | |
| 1514 | + * | |
| 1074 | 1515 | * @return string The url |
| 1075 | 1516 | */ |
| 1076 | 1517 | function bbp_logout_url( $url = '', $redirect_to = '' ) { |
| 1077 | 1518 | |
| 1078 | - // Make sure we are directing somewhere | |
| 1079 | - if ( empty( $redirect_to ) && !strstr( $url, 'redirect_to' ) ) { | |
| 1519 | + // If there is no redirect in the URL, let's add one... | |
| 1520 | + if ( ! strstr( $url, 'redirect_to' ) ) { | |
| 1080 | 1521 | |
| 1081 | - // Rejig the $redirect_to | |
| 1082 | - if ( !isset( $_SERVER['REDIRECT_URL'] ) || ( $redirect_to != home_url( $_SERVER['REDIRECT_URL'] ) ) ) { | |
| 1083 | - $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : ''; | |
| 1522 | + // Get the forum root, to maybe use as a default | |
| 1523 | + $forum_root = bbp_get_root_url(); | |
| 1524 | + | |
| 1525 | + // No redirect passed, so check referer and fallback to request uri | |
| 1526 | + if ( empty( $redirect_to ) ) { | |
| 1527 | + | |
| 1528 | + // Check for a valid referer | |
| 1529 | + $redirect_to = wp_get_referer(); | |
| 1530 | + | |
| 1531 | + // Fallback to request uri if invalid referer | |
| 1532 | + if ( false === $redirect_to ) { | |
| 1533 | + $redirect_to = bbp_get_url_scheme() . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; | |
| 1534 | + } | |
| 1084 | 1535 | } |
| 1085 | 1536 | |
| 1086 | - $redirect_to = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; | |
| 1537 | + // Filter the $redirect_to destination | |
| 1538 | + $filtered = apply_filters( 'bbp_logout_url_redirect_to', $redirect_to ); | |
| 1087 | 1539 | |
| 1088 | - // Sanitize $redirect_to and add it to full $url | |
| 1089 | - $redirect_to = add_query_arg( array( 'loggedout' => 'true' ), esc_url( $redirect_to ) ); | |
| 1090 | - $url = add_query_arg( array( 'redirect_to' => urlencode( $redirect_to ) ), $url ); | |
| 1540 | + // Validate $redirect_to, default to root | |
| 1541 | + $validated = wp_validate_redirect( $filtered, $forum_root ); | |
| 1542 | + | |
| 1543 | + // Assemble $redirect_to and add it (encoded) to full $url | |
| 1544 | + $appended = add_query_arg( array( 'loggedout' => 'true' ), $validated ); | |
| 1545 | + $encoded = urlencode( $appended ); | |
| 1546 | + $url = add_query_arg( array( 'redirect_to' => $encoded ), $url ); | |
| 1091 | 1547 | } |
| 1092 | 1548 | |
| 1093 | - // Filter and return | |
| 1549 | + // Filter & return | |
| 1094 | 1550 | return apply_filters( 'bbp_logout_url', $url, $redirect_to ); |
| 1095 | 1551 | } |
| 1096 | 1552 | |
| 1097 | 1553 | /** Queries *******************************************************************/ |
| @@ -1103,9 +1559,9 @@ | ||
| 1103 | 1559 | * to be merged into another array. It is identical to wp_parse_args() except |
| 1104 | 1560 | * it allows for arguments to be passively or aggressively filtered using the |
| 1105 | 1561 | * optional $filter_key parameter. |
| 1106 | 1562 | * |
| 1107 | - * @since bbPress (r3839) | |
| 1563 | + * @since 2.1.0 bbPress (r3839) | |
| 1108 | 1564 | * |
| 1109 | 1565 | * @param string|array $args Value to merge with $defaults |
| 1110 | 1566 | * @param array $defaults Array that serves as the defaults. |
| 1111 | 1567 | * @param string $filter_key String to key the filters from |
| @@ -1110,29 +1566,33 @@ | ||
| 1110 | 1566 | * @param array $defaults Array that serves as the defaults. |
| 1111 | 1567 | * @param string $filter_key String to key the filters from |
| 1112 | 1568 | * @return array Merged user defined values with defaults. |
| 1113 | 1569 | */ |
| 1114 | -function bbp_parse_args( $args, $defaults = '', $filter_key = '' ) { | |
| 1570 | +function bbp_parse_args( $args, $defaults = array(), $filter_key = '' ) { | |
| 1115 | 1571 | |
| 1116 | 1572 | // Setup a temporary array from $args |
| 1117 | - if ( is_object( $args ) ) | |
| 1573 | + if ( is_object( $args ) ) { | |
| 1118 | 1574 | $r = get_object_vars( $args ); |
| 1119 | - elseif ( is_array( $args ) ) | |
| 1575 | + } elseif ( is_array( $args ) ) { | |
| 1120 | 1576 | $r =& $args; |
| 1121 | - else | |
| 1577 | + } else { | |
| 1122 | 1578 | wp_parse_str( $args, $r ); |
| 1579 | + } | |
| 1123 | 1580 | |
| 1124 | 1581 | // Passively filter the args before the parse |
| 1125 | - if ( !empty( $filter_key ) ) | |
| 1126 | - $r = apply_filters( 'bbp_before_' . $filter_key . '_parse_args', $r ); | |
| 1582 | + if ( ! empty( $filter_key ) ) { | |
| 1583 | + $r = apply_filters( "bbp_before_{$filter_key}_parse_args", $r, $args, $defaults ); | |
| 1584 | + } | |
| 1127 | 1585 | |
| 1128 | 1586 | // Parse |
| 1129 | - if ( is_array( $defaults ) ) | |
| 1587 | + if ( is_array( $defaults ) && ! empty( $defaults ) ) { | |
| 1130 | 1588 | $r = array_merge( $defaults, $r ); |
| 1589 | + } | |
| 1131 | 1590 | |
| 1132 | 1591 | // Aggressively filter the args after the parse |
| 1133 | - if ( !empty( $filter_key ) ) | |
| 1134 | - $r = apply_filters( 'bbp_after_' . $filter_key . '_parse_args', $r ); | |
| 1592 | + if ( ! empty( $filter_key ) ) { | |
| 1593 | + $r = apply_filters( "bbp_after_{$filter_key}_parse_args", $r, $args, $defaults ); | |
| 1594 | + } | |
| 1135 | 1595 | |
| 1136 | 1596 | // Return the parsed results |
| 1137 | 1597 | return $r; |
| 1138 | 1598 | } |
| @@ -1139,11 +1599,12 @@ | ||
| 1139 | 1599 | |
| 1140 | 1600 | /** |
| 1141 | 1601 | * Adds ability to include or exclude specific post_parent ID's |
| 1142 | 1602 | * |
| 1143 | - * @since bbPress (r2996) | |
| 1603 | + * @since 2.0.0 bbPress (r2996) | |
| 1144 | 1604 | * |
| 1145 | - * @global DB $wpdb | |
| 1605 | + * @deprecated 2.5.8 bbPress (r5814) | |
| 1606 | + * | |
| 1146 | 1607 | * @global WP $wp |
| 1147 | 1608 | * @param string $where |
| 1148 | 1609 | * @param WP_Query $object |
| 1149 | 1610 | * @return string |
| @@ -1148,31 +1609,37 @@ | ||
| 1148 | 1609 | * @param WP_Query $object |
| 1149 | 1610 | * @return string |
| 1150 | 1611 | */ |
| 1151 | 1612 | function bbp_query_post_parent__in( $where, $object = '' ) { |
| 1152 | - global $wpdb, $wp; | |
| 1613 | + global $wp; | |
| 1153 | 1614 | |
| 1154 | 1615 | // Noop if WP core supports this already |
| 1155 | - if ( in_array( 'post_parent__in', $wp->private_query_vars ) ) | |
| 1616 | + if ( in_array( 'post_parent__in', $wp->private_query_vars, true ) ) { | |
| 1156 | 1617 | return $where; |
| 1618 | + } | |
| 1157 | 1619 | |
| 1158 | 1620 | // Bail if no object passed |
| 1159 | - if ( empty( $object ) ) | |
| 1621 | + if ( empty( $object ) ) { | |
| 1160 | 1622 | return $where; |
| 1623 | + } | |
| 1161 | 1624 | |
| 1162 | 1625 | // Only 1 post_parent so return $where |
| 1163 | - if ( is_numeric( $object->query_vars['post_parent'] ) ) | |
| 1626 | + if ( is_numeric( $object->query_vars['post_parent'] ) ) { | |
| 1164 | 1627 | return $where; |
| 1628 | + } | |
| 1165 | 1629 | |
| 1630 | + // Get the DB | |
| 1631 | + $bbp_db = bbp_db(); | |
| 1632 | + | |
| 1166 | 1633 | // Including specific post_parent's |
| 1167 | 1634 | if ( ! empty( $object->query_vars['post_parent__in'] ) ) { |
| 1168 | - $ids = implode( ',', array_map( 'absint', $object->query_vars['post_parent__in'] ) ); | |
| 1169 | - $where .= " AND $wpdb->posts.post_parent IN ($ids)"; | |
| 1635 | + $ids = implode( ',', wp_parse_id_list( $object->query_vars['post_parent__in'] ) ); | |
| 1636 | + $where .= " AND {$bbp_db->posts}.post_parent IN ($ids)"; | |
| 1170 | 1637 | |
| 1171 | 1638 | // Excluding specific post_parent's |
| 1172 | 1639 | } elseif ( ! empty( $object->query_vars['post_parent__not_in'] ) ) { |
| 1173 | - $ids = implode( ',', array_map( 'absint', $object->query_vars['post_parent__not_in'] ) ); | |
| 1174 | - $where .= " AND $wpdb->posts.post_parent NOT IN ($ids)"; | |
| 1640 | + $ids = implode( ',', wp_parse_id_list( $object->query_vars['post_parent__not_in'] ) ); | |
| 1641 | + $where .= " AND {$bbp_db->posts}.post_parent NOT IN ($ids)"; | |
| 1175 | 1642 | } |
| 1176 | 1643 | |
| 1177 | 1644 | // Return possibly modified $where |
| 1178 | 1645 | return $where; |
| @@ -1180,195 +1647,514 @@ | ||
| 1180 | 1647 | |
| 1181 | 1648 | /** |
| 1182 | 1649 | * Query the DB and get the last public post_id that has parent_id as post_parent |
| 1183 | 1650 | * |
| 1184 | - * @param int $parent_id Parent id | |
| 1185 | - * @param string $post_type Post type. Defaults to 'post' | |
| 1186 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 1187 | - * @uses wp_cache_get() To check if there is a cache of the last child id | |
| 1188 | - * @uses wpdb::prepare() To prepare the query | |
| 1189 | - * @uses wpdb::get_var() To get the result of the query in a variable | |
| 1190 | - * @uses wp_cache_set() To set the cache for future use | |
| 1191 | - * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child | |
| 1192 | - * id, parent id and post type | |
| 1651 | + * @since 2.0.0 bbPress (r2868) | |
| 1652 | + * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects | |
| 1653 | + * | |
| 1654 | + * @param int $parent_id Parent id. | |
| 1655 | + * @param string $post_type Post type. Defaults to 'post'. | |
| 1193 | 1656 | * @return int The last active post_id |
| 1194 | 1657 | */ |
| 1195 | 1658 | function bbp_get_public_child_last_id( $parent_id = 0, $post_type = 'post' ) { |
| 1196 | - global $wpdb; | |
| 1197 | 1659 | |
| 1198 | 1660 | // Bail if nothing passed |
| 1199 | - if ( empty( $parent_id ) ) | |
| 1661 | + if ( empty( $parent_id ) ) { | |
| 1200 | 1662 | return false; |
| 1663 | + } | |
| 1201 | 1664 | |
| 1202 | - // The ID of the cached query | |
| 1203 | - $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id'; | |
| 1204 | - $post_status = array( bbp_get_public_status_id() ); | |
| 1665 | + // Which statuses | |
| 1666 | + switch ( $post_type ) { | |
| 1205 | 1667 | |
| 1206 | - // Add closed status if topic post type | |
| 1207 | - if ( $post_type == bbp_get_topic_post_type() ) | |
| 1208 | - $post_status[] = bbp_get_closed_status_id(); | |
| 1668 | + // Forum | |
| 1669 | + case bbp_get_forum_post_type() : | |
| 1670 | + $post_status = bbp_get_public_forum_statuses(); | |
| 1671 | + break; | |
| 1209 | 1672 | |
| 1210 | - // Join post statuses together | |
| 1211 | - $post_status = "'" . join( "', '", $post_status ) . "'"; | |
| 1673 | + // Topic | |
| 1674 | + case bbp_get_topic_post_type() : | |
| 1675 | + $post_status = bbp_get_public_topic_statuses(); | |
| 1676 | + break; | |
| 1212 | 1677 | |
| 1678 | + // Reply | |
| 1679 | + case bbp_get_reply_post_type() : | |
| 1680 | + default : | |
| 1681 | + $post_status = bbp_get_public_reply_statuses(); | |
| 1682 | + break; | |
| 1683 | + } | |
| 1684 | + | |
| 1685 | + $query = new WP_Query( array( | |
| 1686 | + 'fields' => 'ids', | |
| 1687 | + 'post_parent' => $parent_id, | |
| 1688 | + 'post_status' => $post_status, | |
| 1689 | + 'post_type' => $post_type, | |
| 1690 | + 'posts_per_page' => 1, | |
| 1691 | + 'orderby' => array( | |
| 1692 | + 'post_date' => 'DESC', | |
| 1693 | + 'ID' => 'DESC' | |
| 1694 | + ), | |
| 1695 | + | |
| 1696 | + // Performance | |
| 1697 | + 'suppress_filters' => true, | |
| 1698 | + 'update_post_term_cache' => false, | |
| 1699 | + 'update_post_meta_cache' => false, | |
| 1700 | + 'ignore_sticky_posts' => true, | |
| 1701 | + 'no_found_rows' => true | |
| 1702 | + ) ); | |
| 1703 | + $child_id = array_shift( $query->posts ); | |
| 1704 | + unset( $query ); | |
| 1705 | + | |
| 1706 | + // Filter & return | |
| 1707 | + return (int) apply_filters( 'bbp_get_public_child_last_id', $child_id, $parent_id, $post_type ); | |
| 1708 | +} | |
| 1709 | + | |
| 1710 | +/** | |
| 1711 | + * Query the database for child counts, grouped by type & status | |
| 1712 | + * | |
| 1713 | + * @since 2.6.0 bbPress (r6826) | |
| 1714 | + * | |
| 1715 | + * @param int $parent_id | |
| 1716 | + */ | |
| 1717 | +function bbp_get_child_counts( $parent_id = 0 ) { | |
| 1718 | + | |
| 1719 | + // Create cache key | |
| 1720 | + $parent_id = absint( $parent_id ); | |
| 1721 | + $key = md5( serialize( array( 'parent_id' => $parent_id, 'post_type' => bbp_get_post_types() ) ) ); | |
| 1722 | + $last_changed = wp_cache_get_last_changed( 'bbpress_posts' ); | |
| 1723 | + $cache_key = "bbp_child_counts:{$key}:{$last_changed}"; | |
| 1724 | + | |
| 1213 | 1725 | // Check for cache and set if needed |
| 1214 | - $child_id = wp_cache_get( $cache_id, 'bbpress' ); | |
| 1215 | - if ( empty( $child_id ) ) { | |
| 1216 | - $child_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", $parent_id, $post_type ) ); | |
| 1217 | - wp_cache_set( $cache_id, $child_id, 'bbpress' ); | |
| 1726 | + $retval = wp_cache_get( $cache_key, 'bbpress_posts' ); | |
| 1727 | + if ( false === $retval ) { | |
| 1728 | + | |
| 1729 | + // Setup the DB & query | |
| 1730 | + $bbp_db = bbp_db(); | |
| 1731 | + $sql = "SELECT | |
| 1732 | + p.post_type AS type, | |
| 1733 | + p.post_status AS status, | |
| 1734 | + COUNT( * ) AS count | |
| 1735 | + FROM {$bbp_db->posts} AS p | |
| 1736 | + LEFT JOIN {$bbp_db->postmeta} AS pm | |
| 1737 | + ON p.ID = pm.post_id | |
| 1738 | + AND pm.meta_key = %s | |
| 1739 | + WHERE pm.meta_value = %s | |
| 1740 | + GROUP BY p.post_status, p.post_type"; | |
| 1741 | + | |
| 1742 | + // Get prepare vars | |
| 1743 | + $post_type = get_post_type( $parent_id ); | |
| 1744 | + $meta_key = "_bbp_{$post_type}_id"; | |
| 1745 | + | |
| 1746 | + // Prepare & get results | |
| 1747 | + $query = $bbp_db->prepare( $sql, $meta_key, $parent_id ); | |
| 1748 | + $results = $bbp_db->get_results( $query, ARRAY_A ); | |
| 1749 | + | |
| 1750 | + // Setup return value | |
| 1751 | + $retval = wp_list_pluck( $results, 'type', 'type' ); | |
| 1752 | + $statuses = get_post_stati(); | |
| 1753 | + | |
| 1754 | + // Loop through results | |
| 1755 | + foreach ( $results as $row ) { | |
| 1756 | + | |
| 1757 | + // Setup empties | |
| 1758 | + if ( ! is_array( $retval[ $row['type'] ] ) ) { | |
| 1759 | + $retval[ $row['type'] ] = array_fill_keys( $statuses, 0 ); | |
| 1760 | + } | |
| 1761 | + | |
| 1762 | + // Set statuses | |
| 1763 | + $retval[ $row['type'] ][ $row['status'] ] = bbp_number_not_negative( $row['count'] ); | |
| 1764 | + } | |
| 1765 | + | |
| 1766 | + // Always cache the results | |
| 1767 | + wp_cache_set( $cache_key, $retval, 'bbpress_posts' ); | |
| 1218 | 1768 | } |
| 1219 | 1769 | |
| 1220 | - // Filter and return | |
| 1221 | - return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, (int) $parent_id, $post_type ); | |
| 1770 | + // Make sure results are INTs | |
| 1771 | + return (array) apply_filters( 'bbp_get_child_counts', $retval, $parent_id ); | |
| 1222 | 1772 | } |
| 1223 | 1773 | |
| 1224 | 1774 | /** |
| 1775 | + * Filter a list of child counts, from `bbp_get_child_counts()` | |
| 1776 | + * | |
| 1777 | + * @since 2.6.0 bbPress (r6826) | |
| 1778 | + * | |
| 1779 | + * @param int $parent_id ID of post to get child counts from | |
| 1780 | + * @param array $types Optional. An array of post types to filter by | |
| 1781 | + * @param array $statuses Optional. An array of post statuses to filter by | |
| 1782 | + * | |
| 1783 | + * @return array A list of objects or object fields. | |
| 1784 | + */ | |
| 1785 | +function bbp_filter_child_counts_list( $parent_id = 0, $types = array( 'post' ), $statuses = array() ) { | |
| 1786 | + | |
| 1787 | + // Setup local vars | |
| 1788 | + $retval = array(); | |
| 1789 | + $types = array_flip( (array) $types ); | |
| 1790 | + $statuses = array_flip( (array) $statuses ); | |
| 1791 | + $counts = bbp_get_child_counts( $parent_id ); | |
| 1792 | + | |
| 1793 | + // Loop through counts by type | |
| 1794 | + foreach ( $counts as $type => $type_counts ) { | |
| 1795 | + | |
| 1796 | + // Skip if not this type | |
| 1797 | + if ( ! isset( $types[ $type ] ) ) { | |
| 1798 | + continue; | |
| 1799 | + } | |
| 1800 | + | |
| 1801 | + // Maybe filter statuses | |
| 1802 | + if ( ! empty( $statuses ) ) { | |
| 1803 | + $type_counts = array_intersect_key( $type_counts, $statuses ); | |
| 1804 | + } | |
| 1805 | + | |
| 1806 | + // Add type counts to return array | |
| 1807 | + $retval[ $type ] = $type_counts; | |
| 1808 | + } | |
| 1809 | + | |
| 1810 | + // Filter & return | |
| 1811 | + return (array) apply_filters( 'bbp_filter_child_counts_list', $retval, $parent_id, $types, $statuses ); | |
| 1812 | +} | |
| 1813 | + | |
| 1814 | +/** | |
| 1225 | 1815 | * Query the DB and get a count of public children |
| 1226 | 1816 | * |
| 1227 | - * @param int $parent_id Parent id | |
| 1228 | - * @param string $post_type Post type. Defaults to 'post' | |
| 1229 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 1230 | - * @uses wp_cache_get() To check if there is a cache of the children count | |
| 1231 | - * @uses wpdb::prepare() To prepare the query | |
| 1232 | - * @uses wpdb::get_var() To get the result of the query in a variable | |
| 1233 | - * @uses wp_cache_set() To set the cache for future use | |
| 1234 | - * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child | |
| 1235 | - * count, parent id and post type | |
| 1817 | + * @since 2.0.0 bbPress (r2868) | |
| 1818 | + * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects | |
| 1819 | + * | |
| 1820 | + * @param int $parent_id Parent id. | |
| 1821 | + * @param string $post_type Post type. Defaults to 'post'. | |
| 1236 | 1822 | * @return int The number of children |
| 1237 | 1823 | */ |
| 1238 | 1824 | function bbp_get_public_child_count( $parent_id = 0, $post_type = 'post' ) { |
| 1239 | - global $wpdb; | |
| 1240 | 1825 | |
| 1241 | 1826 | // Bail if nothing passed |
| 1242 | - if ( empty( $parent_id ) ) | |
| 1827 | + if ( empty( $post_type ) ) { | |
| 1243 | 1828 | return false; |
| 1829 | + } | |
| 1244 | 1830 | |
| 1245 | - // The ID of the cached query | |
| 1246 | - $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count'; | |
| 1247 | - $post_status = array( bbp_get_public_status_id() ); | |
| 1831 | + // Which statuses | |
| 1832 | + switch ( $post_type ) { | |
| 1248 | 1833 | |
| 1249 | - // Add closed status if topic post type | |
| 1250 | - if ( $post_type == bbp_get_topic_post_type() ) | |
| 1251 | - $post_status[] = bbp_get_closed_status_id(); | |
| 1834 | + // Forum | |
| 1835 | + case bbp_get_forum_post_type() : | |
| 1836 | + $post_status = bbp_get_public_forum_statuses(); | |
| 1837 | + break; | |
| 1252 | 1838 | |
| 1253 | - // Join post statuses together | |
| 1254 | - $post_status = "'" . join( "', '", $post_status ) . "'"; | |
| 1839 | + // Topic | |
| 1840 | + case bbp_get_topic_post_type() : | |
| 1841 | + $post_status = bbp_get_public_topic_statuses(); | |
| 1842 | + break; | |
| 1255 | 1843 | |
| 1256 | - // Check for cache and set if needed | |
| 1257 | - $child_count = wp_cache_get( $cache_id, 'bbpress' ); | |
| 1258 | - if ( empty( $child_count ) ) { | |
| 1259 | - $child_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $parent_id, $post_type ) ); | |
| 1260 | - wp_cache_set( $cache_id, $child_count, 'bbpress' ); | |
| 1844 | + // Reply | |
| 1845 | + case bbp_get_reply_post_type() : | |
| 1846 | + default : | |
| 1847 | + $post_status = bbp_get_public_reply_statuses(); | |
| 1848 | + break; | |
| 1261 | 1849 | } |
| 1262 | 1850 | |
| 1263 | - // Filter and return | |
| 1264 | - return apply_filters( 'bbp_get_public_child_count', (int) $child_count, (int) $parent_id, $post_type ); | |
| 1851 | + // Get counts | |
| 1852 | + $counts = bbp_filter_child_counts_list( $parent_id, $post_type, $post_status ); | |
| 1853 | + $child_count = isset( $counts[ $post_type ] ) | |
| 1854 | + ? bbp_number_not_negative( array_sum( array_values( $counts[ $post_type ] ) ) ) | |
| 1855 | + : 0; | |
| 1856 | + | |
| 1857 | + // Filter & return | |
| 1858 | + return (int) apply_filters( 'bbp_get_public_child_count', $child_count, $parent_id, $post_type ); | |
| 1265 | 1859 | } |
| 1266 | - | |
| 1267 | 1860 | /** |
| 1268 | - * Query the DB and get a the child id's of public children | |
| 1861 | + * Query the DB and get a count of public children | |
| 1269 | 1862 | * |
| 1270 | - * @param int $parent_id Parent id | |
| 1271 | - * @param string $post_type Post type. Defaults to 'post' | |
| 1272 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 1273 | - * @uses wp_cache_get() To check if there is a cache of the children | |
| 1274 | - * @uses wpdb::prepare() To prepare the query | |
| 1275 | - * @uses wpdb::get_col() To get the result of the query in an array | |
| 1276 | - * @uses wp_cache_set() To set the cache for future use | |
| 1277 | - * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids, | |
| 1278 | - * parent id and post type | |
| 1279 | - * @return array The array of children | |
| 1863 | + * @since 2.0.0 bbPress (r2868) | |
| 1864 | + * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects | |
| 1865 | + * | |
| 1866 | + * @param int $parent_id Parent id. | |
| 1867 | + * @param string $post_type Post type. Defaults to 'post'. | |
| 1868 | + * @return int The number of children | |
| 1280 | 1869 | */ |
| 1281 | -function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) { | |
| 1282 | - global $wpdb; | |
| 1870 | +function bbp_get_non_public_child_count( $parent_id = 0, $post_type = 'post' ) { | |
| 1283 | 1871 | |
| 1284 | 1872 | // Bail if nothing passed |
| 1285 | - if ( empty( $parent_id ) ) | |
| 1873 | + if ( empty( $parent_id ) || empty( $post_type ) ) { | |
| 1286 | 1874 | return false; |
| 1875 | + } | |
| 1287 | 1876 | |
| 1288 | - // The ID of the cached query | |
| 1289 | - $cache_id = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids'; | |
| 1290 | - $post_status = array( bbp_get_public_status_id() ); | |
| 1877 | + // Which statuses | |
| 1878 | + switch ( $post_type ) { | |
| 1291 | 1879 | |
| 1292 | - // Add closed status if topic post type | |
| 1293 | - if ( $post_type == bbp_get_topic_post_type() ) | |
| 1294 | - $post_status[] = bbp_get_closed_status_id(); | |
| 1880 | + // Forum | |
| 1881 | + case bbp_get_forum_post_type() : | |
| 1882 | + $post_status = bbp_get_non_public_forum_statuses(); | |
| 1883 | + break; | |
| 1295 | 1884 | |
| 1296 | - // Join post statuses together | |
| 1297 | - $post_status = "'" . join( "', '", $post_status ) . "'"; | |
| 1885 | + // Topic | |
| 1886 | + case bbp_get_topic_post_type() : | |
| 1887 | + $post_status = bbp_get_non_public_topic_statuses(); | |
| 1888 | + break; | |
| 1298 | 1889 | |
| 1299 | - // Check for cache and set if needed | |
| 1300 | - $child_ids = wp_cache_get( $cache_id, 'bbpress' ); | |
| 1301 | - if ( empty( $child_ids ) ) { | |
| 1302 | - $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) ); | |
| 1303 | - wp_cache_set( $cache_id, $child_ids, 'bbpress' ); | |
| 1890 | + // Reply | |
| 1891 | + case bbp_get_reply_post_type() : | |
| 1892 | + $post_status = bbp_get_non_public_reply_statuses(); | |
| 1893 | + break; | |
| 1894 | + | |
| 1895 | + // Any | |
| 1896 | + default : | |
| 1897 | + $post_status = bbp_get_public_status_id(); | |
| 1898 | + break; | |
| 1304 | 1899 | } |
| 1305 | 1900 | |
| 1306 | - // Filter and return | |
| 1307 | - return apply_filters( 'bbp_get_public_child_ids', $child_ids, (int) $parent_id, $post_type ); | |
| 1901 | + // Get counts | |
| 1902 | + $counts = bbp_filter_child_counts_list( $parent_id, $post_type, $post_status ); | |
| 1903 | + $child_count = isset( $counts[ $post_type ] ) | |
| 1904 | + ? bbp_number_not_negative( array_sum( array_values( $counts[ $post_type ] ) ) ) | |
| 1905 | + : 0; | |
| 1906 | + | |
| 1907 | + // Filter & return | |
| 1908 | + return (int) apply_filters( 'bbp_get_non_public_child_count', $child_count, $parent_id, $post_type ); | |
| 1308 | 1909 | } |
| 1910 | + | |
| 1309 | 1911 | /** |
| 1310 | - * Query the DB and get a the child id's of all children | |
| 1912 | + * Query the DB and get the child id's of public children | |
| 1311 | 1913 | * |
| 1312 | - * @param int $parent_id Parent id | |
| 1313 | - * @param string $post_type Post type. Defaults to 'post' | |
| 1314 | - * @uses bbp_get_topic_post_type() To get the topic post type | |
| 1315 | - * @uses wp_cache_get() To check if there is a cache of the children | |
| 1316 | - * @uses wpdb::prepare() To prepare the query | |
| 1317 | - * @uses wpdb::get_col() To get the result of the query in an array | |
| 1318 | - * @uses wp_cache_set() To set the cache for future use | |
| 1319 | - * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids, | |
| 1320 | - * parent id and post type | |
| 1914 | + * @since 2.0.0 bbPress (r2868) | |
| 1915 | + * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects | |
| 1916 | + * | |
| 1917 | + * @param int $parent_id Parent id. | |
| 1918 | + * @param string $post_type Post type. Defaults to 'post'. | |
| 1919 | + * | |
| 1321 | 1920 | * @return array The array of children |
| 1322 | 1921 | */ |
| 1323 | -function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) { | |
| 1324 | - global $wpdb; | |
| 1922 | +function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) { | |
| 1325 | 1923 | |
| 1326 | 1924 | // Bail if nothing passed |
| 1327 | - if ( empty( $parent_id ) ) | |
| 1328 | - return false; | |
| 1925 | + if ( empty( $parent_id ) || empty( $post_type ) ) { | |
| 1926 | + return array(); | |
| 1927 | + } | |
| 1329 | 1928 | |
| 1330 | - // The ID of the cached query | |
| 1331 | - $cache_id = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids'; | |
| 1332 | - $post_status = array( bbp_get_public_status_id() ); | |
| 1333 | - | |
| 1334 | - // Extra post statuses based on post type | |
| 1929 | + // Which statuses | |
| 1335 | 1930 | switch ( $post_type ) { |
| 1336 | 1931 | |
| 1337 | 1932 | // Forum |
| 1338 | 1933 | case bbp_get_forum_post_type() : |
| 1339 | - $post_status[] = bbp_get_private_status_id(); | |
| 1340 | - $post_status[] = bbp_get_hidden_status_id(); | |
| 1934 | + $post_status = bbp_get_public_forum_statuses(); | |
| 1341 | 1935 | break; |
| 1342 | 1936 | |
| 1343 | 1937 | // Topic |
| 1344 | 1938 | case bbp_get_topic_post_type() : |
| 1345 | - $post_status[] = bbp_get_closed_status_id(); | |
| 1346 | - $post_status[] = bbp_get_trash_status_id(); | |
| 1347 | - $post_status[] = bbp_get_spam_status_id(); | |
| 1939 | + $post_status = bbp_get_public_topic_statuses(); | |
| 1348 | 1940 | break; |
| 1349 | 1941 | |
| 1350 | 1942 | // Reply |
| 1351 | 1943 | case bbp_get_reply_post_type() : |
| 1352 | - $post_status[] = bbp_get_trash_status_id(); | |
| 1353 | - $post_status[] = bbp_get_spam_status_id(); | |
| 1944 | + default : | |
| 1945 | + $post_status = bbp_get_public_reply_statuses(); | |
| 1354 | 1946 | break; |
| 1355 | 1947 | } |
| 1356 | 1948 | |
| 1357 | - // Join post statuses together | |
| 1358 | - $post_status = "'" . join( "', '", $post_status ) . "'"; | |
| 1949 | + $query = new WP_Query( array( | |
| 1950 | + 'fields' => 'ids', | |
| 1951 | + 'post_parent' => $parent_id, | |
| 1952 | + 'post_status' => $post_status, | |
| 1953 | + 'post_type' => $post_type, | |
| 1954 | + 'posts_per_page' => -1, | |
| 1955 | + 'orderby' => array( | |
| 1956 | + 'post_date' => 'DESC', | |
| 1957 | + 'ID' => 'DESC' | |
| 1958 | + ), | |
| 1359 | 1959 | |
| 1960 | + // Performance | |
| 1961 | + 'nopaging' => true, | |
| 1962 | + 'suppress_filters' => true, | |
| 1963 | + 'update_post_term_cache' => false, | |
| 1964 | + 'update_post_meta_cache' => false, | |
| 1965 | + 'ignore_sticky_posts' => true, | |
| 1966 | + 'no_found_rows' => true | |
| 1967 | + ) ); | |
| 1968 | + | |
| 1969 | + $child_ids = ! empty( $query->posts ) | |
| 1970 | + ? $query->posts | |
| 1971 | + : array(); | |
| 1972 | + | |
| 1973 | + unset( $query ); | |
| 1974 | + | |
| 1975 | + // Filter & return | |
| 1976 | + return (array) apply_filters( 'bbp_get_public_child_ids', $child_ids, $parent_id, $post_type ); | |
| 1977 | +} | |
| 1978 | + | |
| 1979 | +/** | |
| 1980 | + * Query the DB and get the child id's of all children | |
| 1981 | + * | |
| 1982 | + * @since 2.0.0 bbPress (r3325) | |
| 1983 | + * | |
| 1984 | + * @param int $parent_id Parent id | |
| 1985 | + * @param string $post_type Post type. Defaults to 'post' | |
| 1986 | + * | |
| 1987 | + * @return array The array of children | |
| 1988 | + */ | |
| 1989 | +function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) { | |
| 1990 | + | |
| 1991 | + // Bail if nothing passed | |
| 1992 | + if ( empty( $parent_id ) || empty( $post_type ) ) { | |
| 1993 | + return array(); | |
| 1994 | + } | |
| 1995 | + | |
| 1996 | + // Make cache key | |
| 1997 | + $not_in = array( 'draft', 'future' ); | |
| 1998 | + $key = md5( serialize( array( | |
| 1999 | + 'parent_id' => $parent_id, | |
| 2000 | + 'post_type' => $post_type, | |
| 2001 | + 'post_status' => $not_in | |
| 2002 | + ) ) ); | |
| 2003 | + | |
| 2004 | + // Check last changed | |
| 2005 | + $last_changed = wp_cache_get_last_changed( 'bbpress_posts' ); | |
| 2006 | + $cache_key = "bbp_child_ids:{$key}:{$last_changed}"; | |
| 2007 | + | |
| 1360 | 2008 | // Check for cache and set if needed |
| 1361 | - $child_ids = wp_cache_get( $cache_id, 'bbpress' ); | |
| 1362 | - if ( empty( $child_ids ) ) { | |
| 1363 | - $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) ); | |
| 1364 | - wp_cache_set( $cache_id, $child_ids, 'bbpress' ); | |
| 2009 | + $child_ids = wp_cache_get( $cache_key, 'bbpress_posts' ); | |
| 2010 | + | |
| 2011 | + // Not already cached | |
| 2012 | + if ( false === $child_ids ) { | |
| 2013 | + | |
| 2014 | + // Join post statuses to specifically exclude together | |
| 2015 | + $post_status = "'" . implode( "', '", $not_in ) . "'"; | |
| 2016 | + $bbp_db = bbp_db(); | |
| 2017 | + | |
| 2018 | + // Note that we can't use WP_Query here thanks to post_status assumptions | |
| 2019 | + $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status NOT IN ( {$post_status} ) AND post_type = %s ORDER BY ID DESC", $parent_id, $post_type ); | |
| 2020 | + $child_ids = (array) $bbp_db->get_col( $query ); | |
| 2021 | + | |
| 2022 | + // Always cache the results | |
| 2023 | + wp_cache_set( $cache_key, $child_ids, 'bbpress_posts' ); | |
| 1365 | 2024 | } |
| 1366 | 2025 | |
| 1367 | - // Filter and return | |
| 1368 | - return apply_filters( 'bbp_get_all_child_ids', $child_ids, (int) $parent_id, $post_type ); | |
| 2026 | + // Make sure results are INTs | |
| 2027 | + $child_ids = wp_parse_id_list( $child_ids ); | |
| 2028 | + | |
| 2029 | + // Filter & return | |
| 2030 | + return (array) apply_filters( 'bbp_get_all_child_ids', $child_ids, $parent_id, $post_type ); | |
| 1369 | 2031 | } |
| 1370 | 2032 | |
| 2033 | +/** | |
| 2034 | + * Prime familial post caches. | |
| 2035 | + * | |
| 2036 | + * This function uses _prime_post_caches() to prepare the object cache for | |
| 2037 | + * imminent requests to post objects that aren't naturally cached by the primary | |
| 2038 | + * WP_Query calls themselves. Post author caches are also primed. | |
| 2039 | + * | |
| 2040 | + * This is triggered when a `update_post_family_cache` argument is set to true. | |
| 2041 | + * | |
| 2042 | + * Also see: bbp_update_post_author_caches() | |
| 2043 | + * | |
| 2044 | + * @since 2.6.0 bbPress (r6699) | |
| 2045 | + * | |
| 2046 | + * @param array $objects Array of objects, fresh from a query | |
| 2047 | + * | |
| 2048 | + * @return bool True if some IDs were cached | |
| 2049 | + */ | |
| 2050 | +function bbp_update_post_family_caches( $objects = array() ) { | |
| 2051 | + | |
| 2052 | + // Bail if no posts | |
| 2053 | + if ( empty( $objects ) ) { | |
| 2054 | + return false; | |
| 2055 | + } | |
| 2056 | + | |
| 2057 | + // Default value | |
| 2058 | + $post_ids = array(); | |
| 2059 | + | |
| 2060 | + // Filter the types of IDs to prime | |
| 2061 | + $ids = apply_filters( 'bbp_update_post_family_caches', array( | |
| 2062 | + '_bbp_last_active_id', | |
| 2063 | + '_bbp_last_reply_id', | |
| 2064 | + '_bbp_last_topic_id', | |
| 2065 | + '_bbp_reply_to' | |
| 2066 | + ), $objects ); | |
| 2067 | + | |
| 2068 | + // Get the last active IDs | |
| 2069 | + foreach ( $objects as $object ) { | |
| 2070 | + $object = get_post( $object ); | |
| 2071 | + | |
| 2072 | + // Skip if post ID is empty. | |
| 2073 | + if ( empty( $object->ID ) ) { | |
| 2074 | + continue; | |
| 2075 | + } | |
| 2076 | + | |
| 2077 | + // Meta IDs | |
| 2078 | + foreach ( $ids as $key ) { | |
| 2079 | + $post_ids[] = get_post_meta( $object->ID, $key, true ); | |
| 2080 | + } | |
| 2081 | + | |
| 2082 | + // This post ID is already cached, but the post author may not be | |
| 2083 | + $post_ids[] = $object->ID; | |
| 2084 | + } | |
| 2085 | + | |
| 2086 | + // Unique, non-zero values | |
| 2087 | + $post_ids = bbp_get_unique_array_values( $post_ids ); | |
| 2088 | + | |
| 2089 | + // Bail if no IDs to prime | |
| 2090 | + if ( empty( $post_ids ) ) { | |
| 2091 | + return false; | |
| 2092 | + } | |
| 2093 | + | |
| 2094 | + // Prime post caches | |
| 2095 | + _prime_post_caches( $post_ids, true, true ); | |
| 2096 | + | |
| 2097 | + // Prime post author caches | |
| 2098 | + bbp_update_post_author_caches( $post_ids ); | |
| 2099 | + | |
| 2100 | + // Return | |
| 2101 | + return true; | |
| 2102 | +} | |
| 2103 | + | |
| 2104 | +/** | |
| 2105 | + * Prime post author caches. | |
| 2106 | + * | |
| 2107 | + * This function uses cache_users() to prepare the object cache for | |
| 2108 | + * imminent requests to user objects that aren't naturally cached by the primary | |
| 2109 | + * WP_Query calls themselves. | |
| 2110 | + * | |
| 2111 | + * This is triggered when a `update_post_author_cache` argument is set to true. | |
| 2112 | + * | |
| 2113 | + * @since 2.6.0 bbPress (r6699) | |
| 2114 | + * | |
| 2115 | + * @param array $objects Array of objects, fresh from a query | |
| 2116 | + * | |
| 2117 | + * @return bool True if some IDs were cached | |
| 2118 | + */ | |
| 2119 | +function bbp_update_post_author_caches( $objects = array() ) { | |
| 2120 | + | |
| 2121 | + // Bail if no posts | |
| 2122 | + if ( empty( $objects ) ) { | |
| 2123 | + return false; | |
| 2124 | + } | |
| 2125 | + | |
| 2126 | + // Default value | |
| 2127 | + $user_ids = array(); | |
| 2128 | + | |
| 2129 | + // Get the user IDs (could use wp_list_pluck() if this is ever a bottleneck) | |
| 2130 | + foreach ( $objects as $object ) { | |
| 2131 | + $object = get_post( $object ); | |
| 2132 | + | |
| 2133 | + // Skip if post does not have an author ID. | |
| 2134 | + if ( empty( $object->post_author ) ) { | |
| 2135 | + continue; | |
| 2136 | + } | |
| 2137 | + | |
| 2138 | + // If post exists, add post author to the array. | |
| 2139 | + $user_ids[] = (int) $object->post_author; | |
| 2140 | + } | |
| 2141 | + | |
| 2142 | + // Unique, non-zero values | |
| 2143 | + $user_ids = bbp_get_unique_array_values( $user_ids ); | |
| 2144 | + | |
| 2145 | + // Bail if no IDs to prime | |
| 2146 | + if ( empty( $user_ids ) ) { | |
| 2147 | + return false; | |
| 2148 | + } | |
| 2149 | + | |
| 2150 | + // Try to prime user caches | |
| 2151 | + cache_users( $user_ids ); | |
| 2152 | + | |
| 2153 | + // Return | |
| 2154 | + return true; | |
| 2155 | +} | |
| 2156 | + | |
| 1371 | 2157 | /** Globals *******************************************************************/ |
| 1372 | 2158 | |
| 1373 | 2159 | /** |
| 1374 | 2160 | * Get the unfiltered value of a global $post's key |
| @@ -1374,22 +2160,24 @@ | ||
| 1374 | 2160 | * Get the unfiltered value of a global $post's key |
| 1375 | 2161 | * |
| 1376 | 2162 | * Used most frequently when editing a forum/topic/reply |
| 1377 | 2163 | * |
| 1378 | - * @since bbPress (r3694) | |
| 2164 | + * @since 2.1.0 bbPress (r3694) | |
| 1379 | 2165 | * |
| 1380 | - * @global WP_Query $post | |
| 1381 | 2166 | * @param string $field Name of the key |
| 1382 | 2167 | * @param string $context How to sanitize - raw|edit|db|display|attribute|js |
| 1383 | 2168 | * @return string Field value |
| 1384 | 2169 | */ |
| 1385 | 2170 | function bbp_get_global_post_field( $field = 'ID', $context = 'edit' ) { |
| 1386 | - global $post; | |
| 1387 | 2171 | |
| 1388 | - $retval = isset( $post->$field ) ? $post->$field : ''; | |
| 1389 | - $retval = sanitize_post_field( $field, $retval, $post->ID, $context ); | |
| 2172 | + // Get the post, and maybe get a field from it | |
| 2173 | + $post = get_post(); | |
| 2174 | + $retval = isset( $post->{$field} ) | |
| 2175 | + ? sanitize_post_field( $field, $post->{$field}, $post->ID, $context ) | |
| 2176 | + : ''; | |
| 1390 | 2177 | |
| 1391 | - return apply_filters( 'bbp_get_global_post_field', $retval, $post ); | |
| 2178 | + // Filter & return | |
| 2179 | + return apply_filters( 'bbp_get_global_post_field', $retval, $post, $field, $context ); | |
| 1392 | 2180 | } |
| 1393 | 2181 | |
| 1394 | 2182 | /** Nonces ********************************************************************/ |
| 1395 | 2183 | |
| @@ -1397,34 +2185,75 @@ | ||
| 1397 | 2185 | * Makes sure the user requested an action from another page on this site. |
| 1398 | 2186 | * |
| 1399 | 2187 | * To avoid security exploits within the theme. |
| 1400 | 2188 | * |
| 1401 | - * @since bbPress (r4022) | |
| 2189 | + * @since 2.1.0 bbPress (r4022) | |
| 1402 | 2190 | * |
| 1403 | - * @uses do_action() Calls 'bbp_check_referer' on $action. | |
| 1404 | 2191 | * @param string $action Action nonce |
| 1405 | 2192 | * @param string $query_arg where to look for nonce in $_REQUEST |
| 1406 | 2193 | */ |
| 1407 | 2194 | function bbp_verify_nonce_request( $action = '', $query_arg = '_wpnonce' ) { |
| 1408 | 2195 | |
| 1409 | - // Get the home URL | |
| 1410 | - $home_url = strtolower( home_url() ); | |
| 2196 | + /** Home URL **************************************************************/ | |
| 1411 | 2197 | |
| 2198 | + // Parse home_url() into pieces to remove query-strings, strange characters, | |
| 2199 | + // and other funny things that plugins might to do to it. | |
| 2200 | + $parsed_home = parse_url( home_url( '/', ( is_ssl() ? 'https' : 'http' ) ) ); | |
| 2201 | + | |
| 2202 | + // Maybe include the port, if it's included | |
| 2203 | + if ( isset( $parsed_home['port'] ) ) { | |
| 2204 | + $parsed_host = $parsed_home['host'] . ':' . $parsed_home['port']; | |
| 2205 | + } else { | |
| 2206 | + $parsed_host = $parsed_home['host']; | |
| 2207 | + } | |
| 2208 | + | |
| 2209 | + // Set the home URL for use in comparisons | |
| 2210 | + $home_url = trim( strtolower( $parsed_home['scheme'] . '://' . $parsed_host . $parsed_home['path'] ), '/' ); | |
| 2211 | + | |
| 2212 | + /** Requested URL *********************************************************/ | |
| 2213 | + | |
| 2214 | + // Maybe include the port, if it's included in home_url() | |
| 2215 | + if ( isset( $parsed_home['port'] ) && false === strpos( $_SERVER['HTTP_HOST'], ':' ) ) { | |
| 2216 | + $request_host = $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT']; | |
| 2217 | + } else { | |
| 2218 | + $request_host = $_SERVER['HTTP_HOST']; | |
| 2219 | + } | |
| 2220 | + | |
| 1412 | 2221 | // Build the currently requested URL |
| 1413 | - $scheme = is_ssl() ? 'https://' : 'http://'; | |
| 1414 | - $requested_url = strtolower( $scheme . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); | |
| 2222 | + $scheme = bbp_get_url_scheme(); | |
| 2223 | + $requested_url = strtolower( $scheme . $request_host . $_SERVER['REQUEST_URI'] ); | |
| 1415 | 2224 | |
| 1416 | - // Filter the requested URL, for configurations like reverse proxying | |
| 1417 | - $matched_url = apply_filters( 'bbp_verify_nonce_request_url', $requested_url ); | |
| 2225 | + /** Look for match ********************************************************/ | |
| 1418 | 2226 | |
| 2227 | + /** | |
| 2228 | + * Filters the requested URL being nonce-verified. | |
| 2229 | + * | |
| 2230 | + * Useful for configurations like reverse proxying. | |
| 2231 | + * | |
| 2232 | + * @since 2.2.0 bbPress (r4361) | |
| 2233 | + * | |
| 2234 | + * @param string $requested_url The requested URL. | |
| 2235 | + */ | |
| 2236 | + $matched_url = apply_filters( 'bbp_verify_nonce_request_url', $requested_url ); | |
| 2237 | + | |
| 1419 | 2238 | // Check the nonce |
| 1420 | - $result = isset( $_REQUEST[$query_arg] ) ? wp_verify_nonce( $_REQUEST[$query_arg], $action ) : false; | |
| 2239 | + $result = isset( $_REQUEST[ $query_arg ] ) | |
| 2240 | + ? wp_verify_nonce( $_REQUEST[ $query_arg ], $action ) | |
| 2241 | + : false; | |
| 1421 | 2242 | |
| 1422 | 2243 | // Nonce check failed |
| 1423 | - if ( empty( $result ) || empty( $action ) || ( strpos( $matched_url, $home_url ) !== 0 ) ) | |
| 2244 | + if ( empty( $result ) || empty( $action ) || ( strpos( $matched_url, $home_url ) !== 0 ) ) { | |
| 1424 | 2245 | $result = false; |
| 2246 | + } | |
| 1425 | 2247 | |
| 1426 | - // Do extra things | |
| 2248 | + /** | |
| 2249 | + * Fires at the end of the nonce verification check. | |
| 2250 | + * | |
| 2251 | + * @since 2.1.0 bbPress (r4023) | |
| 2252 | + * | |
| 2253 | + * @param string $action Action nonce. | |
| 2254 | + * @param bool $result Boolean result of nonce verification. | |
| 2255 | + */ | |
| 1427 | 2256 | do_action( 'bbp_verify_nonce_request', $action, $result ); |
| 1428 | 2257 | |
| 1429 | 2258 | return $result; |
| 1430 | 2259 | } |
| @@ -1435,9 +2264,9 @@ | ||
| 1435 | 2264 | * This function is hooked into the WordPress 'request' action and is |
| 1436 | 2265 | * responsible for sniffing out the query vars and serving up RSS2 feeds if |
| 1437 | 2266 | * the stars align and the user has requested a feed of any bbPress type. |
| 1438 | 2267 | * |
| 1439 | - * @since bbPress (r3171) | |
| 2268 | + * @since 2.0.0 bbPress (r3171) | |
| 1440 | 2269 | * |
| 1441 | 2270 | * @param array $query_vars |
| 1442 | 2271 | * @return array |
| 1443 | 2272 | */ |
| @@ -1448,160 +2277,197 @@ | ||
| 1448 | 2277 | |
| 1449 | 2278 | // Forum/Topic/Reply Feed |
| 1450 | 2279 | if ( isset( $query_vars['post_type'] ) ) { |
| 1451 | 2280 | |
| 1452 | - // What bbPress post type are we looking for feeds on? | |
| 1453 | - switch ( $query_vars['post_type'] ) { | |
| 2281 | + // Matched post type | |
| 2282 | + $post_type = false; | |
| 1454 | 2283 | |
| 1455 | - // Forum | |
| 1456 | - case bbp_get_forum_post_type() : | |
| 2284 | + // Post types to check | |
| 2285 | + $post_types = array( | |
| 2286 | + bbp_get_forum_post_type(), | |
| 2287 | + bbp_get_topic_post_type(), | |
| 2288 | + bbp_get_reply_post_type() | |
| 2289 | + ); | |
| 1457 | 2290 | |
| 1458 | - // Define local variable(s) | |
| 1459 | - $meta_query = array(); | |
| 2291 | + // Cast query vars as array outside of foreach loop | |
| 2292 | + $qv_array = (array) $query_vars['post_type']; | |
| 1460 | 2293 | |
| 1461 | - // Single forum | |
| 1462 | - if ( isset( $query_vars[bbp_get_forum_post_type()] ) ) { | |
| 2294 | + // Check if this query is for a bbPress post type | |
| 2295 | + foreach ( $post_types as $bbp_pt ) { | |
| 2296 | + if ( in_array( $bbp_pt, $qv_array, true ) ) { | |
| 2297 | + $post_type = $bbp_pt; | |
| 2298 | + break; | |
| 2299 | + } | |
| 2300 | + } | |
| 1463 | 2301 | |
| 1464 | - // Get the forum by the path | |
| 1465 | - $forum = get_page_by_path( $query_vars[bbp_get_forum_post_type()], OBJECT, bbp_get_forum_post_type() ); | |
| 1466 | - $forum_id = $forum->ID; | |
| 2302 | + // Looking at a bbPress post type | |
| 2303 | + if ( ! empty( $post_type ) ) { | |
| 1467 | 2304 | |
| 1468 | - // Load up our own query | |
| 1469 | - query_posts( array( | |
| 1470 | - 'post_type' => bbp_get_forum_post_type(), | |
| 1471 | - 'ID' => $forum_id, | |
| 1472 | - 'feed' => true | |
| 1473 | - ) ); | |
| 2305 | + // Supported select query vars | |
| 2306 | + $select_query_vars = array( | |
| 2307 | + 'p' => false, | |
| 2308 | + 'name' => false, | |
| 2309 | + $post_type => false, | |
| 2310 | + ); | |
| 1474 | 2311 | |
| 1475 | - // Restrict to specific forum ID | |
| 1476 | - $meta_query = array( array( | |
| 1477 | - 'key' => '_bbp_forum_id', | |
| 1478 | - 'value' => $forum_id, | |
| 1479 | - 'type' => 'numeric', | |
| 1480 | - 'compare' => '=' | |
| 1481 | - ) ); | |
| 2312 | + // Setup matched variables to select | |
| 2313 | + foreach ( $query_vars as $key => $value ) { | |
| 2314 | + if ( isset( $select_query_vars[ $key ] ) ) { | |
| 2315 | + $select_query_vars[ $key ] = $value; | |
| 1482 | 2316 | } |
| 2317 | + } | |
| 1483 | 2318 | |
| 1484 | - // Only forum replies | |
| 1485 | - if ( !empty( $_GET['type'] ) && ( bbp_get_reply_post_type() == $_GET['type'] ) ) { | |
| 2319 | + // Remove any empties | |
| 2320 | + $select_query_vars = array_filter( $select_query_vars ); | |
| 1486 | 2321 | |
| 1487 | - // The query | |
| 1488 | - $the_query = array( | |
| 1489 | - 'author' => 0, | |
| 1490 | - 'feed' => true, | |
| 1491 | - 'post_type' => bbp_get_reply_post_type(), | |
| 1492 | - 'post_parent' => 'any', | |
| 1493 | - 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ), | |
| 1494 | - 'posts_per_page' => bbp_get_replies_per_rss_page(), | |
| 1495 | - 'order' => 'DESC', | |
| 1496 | - 'meta_query' => $meta_query | |
| 1497 | - ); | |
| 2322 | + // What bbPress post type are we looking for feeds on? | |
| 2323 | + switch ( $post_type ) { | |
| 1498 | 2324 | |
| 1499 | - // Output the feed | |
| 1500 | - bbp_display_replies_feed_rss2( $the_query ); | |
| 2325 | + // Forum | |
| 2326 | + case bbp_get_forum_post_type() : | |
| 1501 | 2327 | |
| 1502 | - // Only forum topics | |
| 1503 | - } elseif ( !empty( $_GET['type'] ) && ( bbp_get_topic_post_type() == $_GET['type'] ) ) { | |
| 2328 | + // Define local variable(s) | |
| 2329 | + $meta_query = array(); | |
| 1504 | 2330 | |
| 1505 | - // The query | |
| 1506 | - $the_query = array( | |
| 1507 | - 'author' => 0, | |
| 1508 | - 'feed' => true, | |
| 1509 | - 'post_type' => bbp_get_topic_post_type(), | |
| 1510 | - 'post_parent' => $forum_id, | |
| 1511 | - 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ), | |
| 1512 | - 'posts_per_page' => bbp_get_topics_per_rss_page(), | |
| 1513 | - 'order' => 'DESC' | |
| 1514 | - ); | |
| 2331 | + // Single forum | |
| 2332 | + if ( ! empty( $select_query_vars ) ) { | |
| 1515 | 2333 | |
| 1516 | - // Output the feed | |
| 1517 | - bbp_display_topics_feed_rss2( $the_query ); | |
| 2334 | + // Load up our own query | |
| 2335 | + query_posts( array_merge( array( | |
| 2336 | + 'post_type' => bbp_get_forum_post_type(), | |
| 2337 | + 'feed' => true | |
| 2338 | + ), $select_query_vars ) ); | |
| 1518 | 2339 | |
| 1519 | - // All forum topics and replies | |
| 1520 | - } else { | |
| 2340 | + // Restrict to specific forum ID | |
| 2341 | + $meta_query = array( array( | |
| 2342 | + 'key' => '_bbp_forum_id', | |
| 2343 | + 'value' => bbp_get_forum_id(), | |
| 2344 | + 'type' => 'NUMERIC', | |
| 2345 | + 'compare' => '=' | |
| 2346 | + ) ); | |
| 2347 | + } | |
| 1521 | 2348 | |
| 1522 | - // Exclude private/hidden forums if not looking at single | |
| 1523 | - if ( empty( $query_vars['forum'] ) ) | |
| 1524 | - $meta_query = array( bbp_exclude_forum_ids( 'meta_query' ) ); | |
| 2349 | + // Only forum replies | |
| 2350 | + if ( ! empty( $_GET['type'] ) && ( bbp_get_reply_post_type() === $_GET['type'] ) ) { | |
| 1525 | 2351 | |
| 1526 | - // The query | |
| 1527 | - $the_query = array( | |
| 1528 | - 'author' => 0, | |
| 1529 | - 'feed' => true, | |
| 1530 | - 'post_type' => array( bbp_get_reply_post_type(), bbp_get_topic_post_type() ), | |
| 1531 | - 'post_parent' => 'any', | |
| 1532 | - 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ), | |
| 1533 | - 'posts_per_page' => bbp_get_replies_per_rss_page(), | |
| 1534 | - 'order' => 'DESC', | |
| 1535 | - 'meta_query' => $meta_query | |
| 1536 | - ); | |
| 2352 | + // The query | |
| 2353 | + $the_query = array( | |
| 2354 | + 'author' => 0, | |
| 2355 | + 'feed' => true, | |
| 2356 | + 'post_type' => bbp_get_reply_post_type(), | |
| 2357 | + 'post_parent' => 'any', | |
| 2358 | + 'post_status' => bbp_get_public_reply_statuses(), | |
| 2359 | + 'posts_per_page' => bbp_get_replies_per_rss_page(), | |
| 2360 | + 'order' => 'DESC', | |
| 2361 | + 'meta_query' => $meta_query | |
| 2362 | + ); | |
| 1537 | 2363 | |
| 1538 | - // Output the feed | |
| 1539 | - bbp_display_replies_feed_rss2( $the_query ); | |
| 1540 | - } | |
| 2364 | + // Output the feed | |
| 2365 | + bbp_display_replies_feed_rss2( $the_query ); | |
| 1541 | 2366 | |
| 1542 | - break; | |
| 2367 | + // Only forum topics | |
| 2368 | + } elseif ( ! empty( $_GET['type'] ) && ( bbp_get_topic_post_type() === $_GET['type'] ) ) { | |
| 1543 | 2369 | |
| 1544 | - // Topic feed - Show replies | |
| 1545 | - case bbp_get_topic_post_type() : | |
| 2370 | + // The query | |
| 2371 | + $the_query = array( | |
| 2372 | + 'author' => 0, | |
| 2373 | + 'feed' => true, | |
| 2374 | + 'post_type' => bbp_get_topic_post_type(), | |
| 2375 | + 'post_parent' => bbp_get_forum_id(), | |
| 2376 | + 'post_status' => bbp_get_public_topic_statuses(), | |
| 2377 | + 'posts_per_page' => bbp_get_topics_per_rss_page(), | |
| 2378 | + 'order' => 'DESC' | |
| 2379 | + ); | |
| 1546 | 2380 | |
| 1547 | - // Single topic | |
| 1548 | - if ( isset( $query_vars[bbp_get_topic_post_type()] ) ) { | |
| 2381 | + // Output the feed | |
| 2382 | + bbp_display_topics_feed_rss2( $the_query ); | |
| 1549 | 2383 | |
| 1550 | - // Load up our own query | |
| 1551 | - query_posts( array( | |
| 1552 | - 'post_type' => bbp_get_topic_post_type(), | |
| 1553 | - 'name' => $query_vars[bbp_get_topic_post_type()], | |
| 1554 | - 'feed' => true | |
| 1555 | - ) ); | |
| 2384 | + // All forum topics and replies | |
| 2385 | + } else { | |
| 1556 | 2386 | |
| 1557 | - // Output the feed | |
| 1558 | - bbp_display_replies_feed_rss2( array( 'feed' => true ) ); | |
| 2387 | + // Exclude private/hidden forums if not looking at single | |
| 2388 | + if ( empty( $select_query_vars ) ) { | |
| 2389 | + $meta_query = array( bbp_exclude_forum_ids( 'meta_query' ) ); | |
| 2390 | + } | |
| 1559 | 2391 | |
| 1560 | - // All topics | |
| 1561 | - } else { | |
| 2392 | + // The query | |
| 2393 | + $the_query = array( | |
| 2394 | + 'author' => 0, | |
| 2395 | + 'feed' => true, | |
| 2396 | + 'post_type' => array( bbp_get_reply_post_type(), bbp_get_topic_post_type() ), | |
| 2397 | + 'post_parent' => 'any', | |
| 2398 | + 'post_status' => bbp_get_public_topic_statuses(), | |
| 2399 | + 'posts_per_page' => bbp_get_replies_per_rss_page(), | |
| 2400 | + 'order' => 'DESC', | |
| 2401 | + 'meta_query' => $meta_query | |
| 2402 | + ); | |
| 1562 | 2403 | |
| 1563 | - // The query | |
| 1564 | - $the_query = array( | |
| 1565 | - 'author' => 0, | |
| 1566 | - 'feed' => true, | |
| 1567 | - 'post_parent' => 'any', | |
| 1568 | - 'posts_per_page' => bbp_get_topics_per_rss_page(), | |
| 1569 | - 'show_stickies' => false | |
| 1570 | - ); | |
| 2404 | + // Output the feed | |
| 2405 | + bbp_display_replies_feed_rss2( $the_query ); | |
| 2406 | + } | |
| 1571 | 2407 | |
| 1572 | - // Output the feed | |
| 1573 | - bbp_display_topics_feed_rss2( $the_query ); | |
| 1574 | - } | |
| 2408 | + break; | |
| 1575 | 2409 | |
| 1576 | - break; | |
| 2410 | + // Topic feed - Show replies | |
| 2411 | + case bbp_get_topic_post_type() : | |
| 1577 | 2412 | |
| 1578 | - // Replies | |
| 1579 | - case bbp_get_reply_post_type() : | |
| 2413 | + // Single topic | |
| 2414 | + if ( ! empty( $select_query_vars ) ) { | |
| 1580 | 2415 | |
| 1581 | - // The query | |
| 1582 | - $the_query = array( | |
| 1583 | - 'posts_per_page' => bbp_get_replies_per_rss_page(), | |
| 1584 | - 'meta_query' => array( array( ) ), | |
| 1585 | - 'feed' => true | |
| 1586 | - ); | |
| 2416 | + // Load up our own query | |
| 2417 | + query_posts( array_merge( array( | |
| 2418 | + 'post_type' => bbp_get_topic_post_type(), | |
| 2419 | + 'feed' => true | |
| 2420 | + ), $select_query_vars ) ); | |
| 1587 | 2421 | |
| 1588 | - // All replies | |
| 1589 | - if ( !isset( $query_vars[bbp_get_reply_post_type()] ) ) { | |
| 1590 | - bbp_display_replies_feed_rss2( $the_query ); | |
| 1591 | - } | |
| 2422 | + // Output the feed | |
| 2423 | + bbp_display_replies_feed_rss2( array( 'feed' => true ) ); | |
| 1592 | 2424 | |
| 1593 | - break; | |
| 2425 | + // All topics | |
| 2426 | + } else { | |
| 2427 | + | |
| 2428 | + // The query | |
| 2429 | + $the_query = array( | |
| 2430 | + 'author' => 0, | |
| 2431 | + 'feed' => true, | |
| 2432 | + 'post_parent' => 'any', | |
| 2433 | + 'posts_per_page' => bbp_get_topics_per_rss_page(), | |
| 2434 | + 'show_stickies' => false | |
| 2435 | + ); | |
| 2436 | + | |
| 2437 | + // Output the feed | |
| 2438 | + bbp_display_topics_feed_rss2( $the_query ); | |
| 2439 | + } | |
| 2440 | + | |
| 2441 | + break; | |
| 2442 | + | |
| 2443 | + // Replies | |
| 2444 | + case bbp_get_reply_post_type() : | |
| 2445 | + | |
| 2446 | + // The query | |
| 2447 | + $the_query = array( | |
| 2448 | + 'posts_per_page' => bbp_get_replies_per_rss_page(), | |
| 2449 | + 'meta_query' => array( array( ) ), | |
| 2450 | + 'feed' => true | |
| 2451 | + ); | |
| 2452 | + | |
| 2453 | + // All replies | |
| 2454 | + if ( empty( $select_query_vars ) ) { | |
| 2455 | + bbp_display_replies_feed_rss2( $the_query ); | |
| 2456 | + } | |
| 2457 | + | |
| 2458 | + break; | |
| 2459 | + } | |
| 1594 | 2460 | } |
| 1595 | 2461 | |
| 1596 | 2462 | // Single Topic Vview |
| 1597 | - } elseif ( isset( $query_vars['bbp_view'] ) ) { | |
| 2463 | + } elseif ( isset( $query_vars[ bbp_get_view_rewrite_id() ] ) ) { | |
| 1598 | 2464 | |
| 1599 | 2465 | // Get the view |
| 1600 | - $view = $query_vars['bbp_view']; | |
| 2466 | + $view = $query_vars[ bbp_get_view_rewrite_id() ]; | |
| 1601 | 2467 | |
| 1602 | 2468 | // We have a view to display a feed |
| 1603 | - if ( !empty( $view ) ) { | |
| 2469 | + if ( ! empty( $view ) ) { | |
| 1604 | 2470 | |
| 1605 | 2471 | // Get the view query |
| 1606 | 2472 | $the_query = bbp_get_view_query_args( $view ); |
| 1607 | 2473 | |
| @@ -1621,13 +2487,10 @@ | ||
| 1621 | 2487 | |
| 1622 | 2488 | /** |
| 1623 | 2489 | * Used to guess if page exists at requested path |
| 1624 | 2490 | * |
| 1625 | - * @since bbPress (r3304) | |
| 2491 | + * @since 2.0.0 bbPress (r3304) | |
| 1626 | 2492 | * |
| 1627 | - * @uses get_option() To see if pretty permalinks are enabled | |
| 1628 | - * @uses get_page_by_path() To see if page exists at path | |
| 1629 | - * | |
| 1630 | 2493 | * @param string $path |
| 1631 | 2494 | * @return mixed False if no page, Page object if true |
| 1632 | 2495 | */ |
| 1633 | 2496 | function bbp_get_page_by_path( $path = '' ) { |
| @@ -1635,9 +2498,9 @@ | ||
| 1635 | 2498 | // Default to false |
| 1636 | 2499 | $retval = false; |
| 1637 | 2500 | |
| 1638 | 2501 | // Path is not empty |
| 1639 | - if ( !empty( $path ) ) { | |
| 2502 | + if ( ! empty( $path ) ) { | |
| 1640 | 2503 | |
| 1641 | 2504 | // Pretty permalinks are on so path might exist |
| 1642 | 2505 | if ( get_option( 'permalink_structure' ) ) { |
| 1643 | 2506 | $retval = get_page_by_path( $path ); |
| @@ -1643,8 +2506,9 @@ | ||
| 1643 | 2506 | $retval = get_page_by_path( $path ); |
| 1644 | 2507 | } |
| 1645 | 2508 | } |
| 1646 | 2509 | |
| 2510 | + // Filter & return | |
| 1647 | 2511 | return apply_filters( 'bbp_get_page_by_path', $retval, $path ); |
| 1648 | 2512 | } |
| 1649 | 2513 | |
| 1650 | 2514 | /** |
| @@ -1651,19 +2515,121 @@ | ||
| 1651 | 2515 | * Sets the 404 status. |
| 1652 | 2516 | * |
| 1653 | 2517 | * Used primarily with topics/replies inside hidden forums. |
| 1654 | 2518 | * |
| 1655 | - * @since bbPress (r3051) | |
| 2519 | + * @since 2.0.0 bbPress (r3051) | |
| 2520 | + * @since 2.6.0 bbPress (r6583) Use status_header() & nocache_headers() | |
| 1656 | 2521 | * |
| 1657 | - * @global WP_Query $wp_query | |
| 1658 | - * @uses WP_Query::set_404() | |
| 2522 | + * @param WP_Query $query The query being checked | |
| 2523 | + * | |
| 2524 | + * @return bool Always returns true | |
| 1659 | 2525 | */ |
| 1660 | -function bbp_set_404() { | |
| 1661 | - global $wp_query; | |
| 2526 | +function bbp_set_404( $query = null ) { | |
| 1662 | 2527 | |
| 1663 | - if ( ! isset( $wp_query ) ) { | |
| 1664 | - _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.', 'bbpress' ), '3.1' ); | |
| 1665 | - return false; | |
| 2528 | + // Global fallback | |
| 2529 | + if ( empty( $query ) ) { | |
| 2530 | + $query = bbp_get_wp_query(); | |
| 1666 | 2531 | } |
| 1667 | 2532 | |
| 1668 | - $wp_query->set_404(); | |
| 2533 | + // Setup environment | |
| 2534 | + $query->set_404(); | |
| 2535 | + | |
| 2536 | + // Setup request | |
| 2537 | + status_header( 404 ); | |
| 2538 | + nocache_headers(); | |
| 2539 | +} | |
| 2540 | + | |
| 2541 | +/** | |
| 2542 | + * Sets the 200 status header. | |
| 2543 | + * | |
| 2544 | + * @since 2.6.0 bbPress (r6583) | |
| 2545 | + */ | |
| 2546 | +function bbp_set_200() { | |
| 2547 | + status_header( 200 ); | |
| 2548 | +} | |
| 2549 | + | |
| 2550 | +/** | |
| 2551 | + * Maybe handle the default 404 handling for some bbPress conditions | |
| 2552 | + * | |
| 2553 | + * Some conditions (like private/hidden forums and edits) have their own checks | |
| 2554 | + * on `bbp_template_redirect` and are not currently 404s. | |
| 2555 | + * | |
| 2556 | + * @since 2.6.0 bbPress (r6555) | |
| 2557 | + * | |
| 2558 | + * @param bool $override Whether to override the default handler | |
| 2559 | + * @param WP_Query $wp_query The posts query being referenced | |
| 2560 | + * | |
| 2561 | + * @return bool False to leave alone, true to override | |
| 2562 | + */ | |
| 2563 | +function bbp_pre_handle_404( $override = false, $wp_query = false ) { | |
| 2564 | + | |
| 2565 | + // Handle a bbPress 404 condition | |
| 2566 | + if ( isset( $wp_query->bbp_is_404 ) ) { | |
| 2567 | + | |
| 2568 | + // Either force a 404 when 200, or a 200 when 404 | |
| 2569 | + $override = ( true === $wp_query->bbp_is_404 ) | |
| 2570 | + ? bbp_set_404( $wp_query ) | |
| 2571 | + : bbp_set_200(); | |
| 2572 | + } | |
| 2573 | + | |
| 2574 | + // Return, maybe overridden | |
| 2575 | + return $override; | |
| 2576 | +} | |
| 2577 | + | |
| 2578 | +/** | |
| 2579 | + * Maybe pre-assign the posts that are returned from a WP_Query. | |
| 2580 | + * | |
| 2581 | + * This effectively short-circuits the default query for posts, which is | |
| 2582 | + * currently only used to avoid calling the main query when it's not necessary. | |
| 2583 | + * | |
| 2584 | + * @since 2.6.0 bbPress (r6580) | |
| 2585 | + * | |
| 2586 | + * @param mixed $posts Default null. Array of posts (possibly empty) | |
| 2587 | + * @param WP_Query $wp_query | |
| 2588 | + * | |
| 2589 | + * @return mixed Null if no override. Array if overridden. | |
| 2590 | + */ | |
| 2591 | +function bbp_posts_pre_query( $posts = null, $wp_query = false ) { | |
| 2592 | + | |
| 2593 | + // Custom 404 handler is set, so set posts to empty array to avoid 2 queries | |
| 2594 | + if ( ! empty( $wp_query->bbp_is_404 ) ) { | |
| 2595 | + $posts = array(); | |
| 2596 | + } | |
| 2597 | + | |
| 2598 | + // Return, maybe overridden | |
| 2599 | + return $posts; | |
| 2600 | +} | |
| 2601 | + | |
| 2602 | +/** | |
| 2603 | + * Get scheme for a URL based on is_ssl() results. | |
| 2604 | + * | |
| 2605 | + * @since 2.6.0 bbPress (r6759) | |
| 2606 | + * | |
| 2607 | + * @return string https:// if is_ssl(), otherwise http:// | |
| 2608 | + */ | |
| 2609 | +function bbp_get_url_scheme() { | |
| 2610 | + return is_ssl() | |
| 2611 | + ? 'https://' | |
| 2612 | + : 'http://'; | |
| 2613 | +} | |
| 2614 | + | |
| 2615 | +/** Titles ********************************************************************/ | |
| 2616 | + | |
| 2617 | +/** | |
| 2618 | + * Is a title longer that the maximum title length? | |
| 2619 | + * | |
| 2620 | + * Uses mb_strlen() in `8bit` mode to treat strings as raw. This matches the | |
| 2621 | + * behavior present in Comments, PHPMailer, RandomCompat, and others. | |
| 2622 | + * | |
| 2623 | + * @since 2.6.0 bbPress (r6783) | |
| 2624 | + * | |
| 2625 | + * @param string $title | |
| 2626 | + * @return bool | |
| 2627 | + */ | |
| 2628 | +function bbp_is_title_too_long( $title = '' ) { | |
| 2629 | + $max = bbp_get_title_max_length(); | |
| 2630 | + $len = mb_strlen( $title, '8bit' ); | |
| 2631 | + $result = ( $len > $max ); | |
| 2632 | + | |
| 2633 | + // Filter & return | |
| 2634 | + return (bool) apply_filters( 'bbp_is_title_too_long', $result, $title, $max, $len ); | |
| 1669 | 2635 | } |