/includes/core/filters.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress Filters 
  5. * 
  6. * @package bbPress 
  7. * @subpackage Core 
  8. * 
  9. * This file contains the filters that are used through-out bbPress. They are 
  10. * consolidated here to make searching for them easier, and to help developers 
  11. * understand at a glance the order in which things occur. 
  12. * 
  13. * There are a few common places that additional filters can currently be found 
  14. * 
  15. * - bbPress: In {@link bbPress::setup_actions()} in bbpress.php 
  16. * - Admin: More in {@link BBP_Admin::setup_actions()} in admin.php 
  17. * 
  18. * @see /core/actions.php 
  19. */ 
  20.  
  21. // Exit if accessed directly 
  22. if ( !defined( 'ABSPATH' ) ) exit; 
  23.  
  24. /** 
  25. * Attach bbPress to WordPress 
  26. * 
  27. * bbPress uses its own internal actions to help aid in third-party plugin 
  28. * development, and to limit the amount of potential future code changes when 
  29. * updates to WordPress core occur. 
  30. * 
  31. * These actions exist to create the concept of 'plugin dependencies'. They 
  32. * provide a safe way for plugins to execute code *only* when bbPress is 
  33. * installed and activated, without needing to do complicated guesswork. 
  34. * 
  35. * For more information on how this works, see the 'Plugin Dependency' section 
  36. * near the bottom of this file. 
  37. * 
  38. * v--WordPress Actions v--bbPress Sub-actions 
  39. */ 
  40. add_filter( 'request', 'bbp_request', 10 ); 
  41. add_filter( 'template_include', 'bbp_template_include', 10 ); 
  42. add_filter( 'wp_title', 'bbp_title', 10, 3 ); 
  43. add_filter( 'body_class', 'bbp_body_class', 10, 2 ); 
  44. add_filter( 'map_meta_cap', 'bbp_map_meta_caps', 10, 4 ); 
  45. add_filter( 'allowed_themes', 'bbp_allowed_themes', 10 ); 
  46. add_filter( 'redirect_canonical', 'bbp_redirect_canonical', 10 ); 
  47. add_filter( 'login_redirect', 'bbp_redirect_login', 2, 3 ); 
  48. add_filter( 'logout_url', 'bbp_logout_url', 2, 2 ); 
  49. add_filter( 'plugin_locale', 'bbp_plugin_locale', 10, 2 ); 
  50.  
  51. // Fix post author id for anonymous posts (set it back to 0) when the post status is changed 
  52. add_filter( 'wp_insert_post_data', 'bbp_fix_post_author', 30, 2 ); 
  53.  
  54. // Force comments_status on bbPress post types 
  55. add_filter( 'comments_open', 'bbp_force_comment_status' ); 
  56.  
  57. // Add post_parent__in to posts_where 
  58. add_filter( 'posts_where', 'bbp_query_post_parent__in', 10, 2 ); 
  59.  
  60. // Remove forums roles from list of all roles 
  61. add_filter( 'editable_roles', 'bbp_filter_blog_editable_roles' ); 
  62.  
  63. // Reply title fallback 
  64. add_filter( 'the_title', 'bbp_get_reply_title_fallback', 2, 2 ); 
  65.  
  66. /** 
  67. * Feeds 
  68. * 
  69. * bbPress comes with a number of custom RSS2 feeds that get handled outside 
  70. * the normal scope of feeds that WordPress would normally serve. To do this,  
  71. * we filter every page request, listen for a feed request, and trap it. 
  72. */ 
  73. add_filter( 'bbp_request', 'bbp_request_feed_trap' ); 
  74.  
  75. /** 
  76. * Template Compatibility 
  77. * 
  78. * If you want to completely bypass this and manage your own custom bbPress 
  79. * template hierarchy, start here by removing this filter, then look at how 
  80. * bbp_template_include() works and do something similar. :) 
  81. */ 
  82. add_filter( 'bbp_template_include', 'bbp_template_include_theme_supports', 2, 1 ); 
  83. add_filter( 'bbp_template_include', 'bbp_template_include_theme_compat', 4, 2 ); 
  84.  
  85. // Filter bbPress template locations 
  86. add_filter( 'bbp_get_template_stack', 'bbp_add_template_stack_locations' ); 
  87.  
  88. // Links 
  89. add_filter( 'paginate_links', 'bbp_add_view_all' ); 
  90. add_filter( 'bbp_get_topic_permalink', 'bbp_add_view_all' ); 
  91. add_filter( 'bbp_get_reply_permalink', 'bbp_add_view_all' ); 
  92. add_filter( 'bbp_get_forum_permalink', 'bbp_add_view_all' ); 
  93.  
  94. // wp_filter_kses on new/edit topic/reply title 
  95. add_filter( 'bbp_new_reply_pre_title', 'wp_filter_kses' ); 
  96. add_filter( 'bbp_new_topic_pre_title', 'wp_filter_kses' ); 
  97. add_filter( 'bbp_edit_reply_pre_title', 'wp_filter_kses' ); 
  98. add_filter( 'bbp_edit_topic_pre_title', 'wp_filter_kses' ); 
  99.  
  100. // Prevent posting malicious or malformed content on new/edit topic/reply 
  101. add_filter( 'bbp_new_reply_pre_content', 'bbp_encode_bad', 10 ); 
  102. add_filter( 'bbp_new_reply_pre_content', 'bbp_code_trick', 20 ); 
  103. add_filter( 'bbp_new_reply_pre_content', 'bbp_filter_kses', 30 ); 
  104. add_filter( 'bbp_new_reply_pre_content', 'balanceTags', 40 ); 
  105. add_filter( 'bbp_new_topic_pre_content', 'bbp_encode_bad', 10 ); 
  106. add_filter( 'bbp_new_topic_pre_content', 'bbp_code_trick', 20 ); 
  107. add_filter( 'bbp_new_topic_pre_content', 'bbp_filter_kses', 30 ); 
  108. add_filter( 'bbp_new_topic_pre_content', 'balanceTags', 40 ); 
  109. add_filter( 'bbp_new_forum_pre_content', 'bbp_encode_bad', 10 ); 
  110. add_filter( 'bbp_new_forum_pre_content', 'bbp_code_trick', 20 ); 
  111. add_filter( 'bbp_new_forum_pre_content', 'bbp_filter_kses', 30 ); 
  112. add_filter( 'bbp_new_forum_pre_content', 'balanceTags', 40 ); 
  113. add_filter( 'bbp_edit_reply_pre_content', 'bbp_encode_bad', 10 ); 
  114. add_filter( 'bbp_edit_reply_pre_content', 'bbp_code_trick', 20 ); 
  115. add_filter( 'bbp_edit_reply_pre_content', 'bbp_filter_kses', 30 ); 
  116. add_filter( 'bbp_edit_reply_pre_content', 'balanceTags', 40 ); 
  117. add_filter( 'bbp_edit_topic_pre_content', 'bbp_encode_bad', 10 ); 
  118. add_filter( 'bbp_edit_topic_pre_content', 'bbp_code_trick', 20 ); 
  119. add_filter( 'bbp_edit_topic_pre_content', 'bbp_filter_kses', 30 ); 
  120. add_filter( 'bbp_edit_topic_pre_content', 'balanceTags', 40 ); 
  121. add_filter( 'bbp_edit_forum_pre_content', 'bbp_encode_bad', 10 ); 
  122. add_filter( 'bbp_edit_forum_pre_content', 'bbp_code_trick', 20 ); 
  123. add_filter( 'bbp_edit_forum_pre_content', 'bbp_filter_kses', 30 ); 
  124. add_filter( 'bbp_edit_forum_pre_content', 'balanceTags', 40 ); 
  125.  
  126. // No follow and stripslashes on user profile links 
  127. add_filter( 'bbp_get_reply_author_link', 'bbp_rel_nofollow' ); 
  128. add_filter( 'bbp_get_reply_author_link', 'stripslashes' ); 
  129. add_filter( 'bbp_get_topic_author_link', 'bbp_rel_nofollow' ); 
  130. add_filter( 'bbp_get_topic_author_link', 'stripslashes' ); 
  131. add_filter( 'bbp_get_user_favorites_link', 'bbp_rel_nofollow' ); 
  132. add_filter( 'bbp_get_user_favorites_link', 'stripslashes' ); 
  133. add_filter( 'bbp_get_user_subscribe_link', 'bbp_rel_nofollow' ); 
  134. add_filter( 'bbp_get_user_subscribe_link', 'stripslashes' ); 
  135. add_filter( 'bbp_get_user_profile_link', 'bbp_rel_nofollow' ); 
  136. add_filter( 'bbp_get_user_profile_link', 'stripslashes' ); 
  137. add_filter( 'bbp_get_user_profile_edit_link', 'bbp_rel_nofollow' ); 
  138. add_filter( 'bbp_get_user_profile_edit_link', 'stripslashes' ); 
  139.  
  140. // Run filters on reply content 
  141. add_filter( 'bbp_get_reply_content', 'bbp_make_clickable', 4 ); 
  142. add_filter( 'bbp_get_reply_content', 'wptexturize', 6 ); 
  143. add_filter( 'bbp_get_reply_content', 'convert_chars', 8 ); 
  144. add_filter( 'bbp_get_reply_content', 'capital_P_dangit', 10 ); 
  145. add_filter( 'bbp_get_reply_content', 'convert_smilies', 20 ); 
  146. add_filter( 'bbp_get_reply_content', 'force_balance_tags', 30 ); 
  147. add_filter( 'bbp_get_reply_content', 'wpautop', 40 ); 
  148. add_filter( 'bbp_get_reply_content', 'bbp_rel_nofollow', 50 ); 
  149.  
  150. // Run filters on topic content 
  151. add_filter( 'bbp_get_topic_content', 'bbp_make_clickable', 4 ); 
  152. add_filter( 'bbp_get_topic_content', 'wptexturize', 6 ); 
  153. add_filter( 'bbp_get_topic_content', 'convert_chars', 8 ); 
  154. add_filter( 'bbp_get_topic_content', 'capital_P_dangit', 10 ); 
  155. add_filter( 'bbp_get_topic_content', 'convert_smilies', 20 ); 
  156. add_filter( 'bbp_get_topic_content', 'force_balance_tags', 30 ); 
  157. add_filter( 'bbp_get_topic_content', 'wpautop', 40 ); 
  158. add_filter( 'bbp_get_topic_content', 'bbp_rel_nofollow', 50 ); 
  159.  
  160. // Form textarea output - undo the code-trick done pre-save, and sanitize 
  161. add_filter( 'bbp_get_form_forum_content', 'bbp_code_trick_reverse' ); 
  162. add_filter( 'bbp_get_form_forum_content', 'esc_textarea' ); 
  163. add_filter( 'bbp_get_form_forum_content', 'trim' ); 
  164. add_filter( 'bbp_get_form_topic_content', 'bbp_code_trick_reverse' ); 
  165. add_filter( 'bbp_get_form_topic_content', 'esc_textarea' ); 
  166. add_filter( 'bbp_get_form_topic_content', 'trim' ); 
  167. add_filter( 'bbp_get_form_reply_content', 'bbp_code_trick_reverse' ); 
  168. add_filter( 'bbp_get_form_reply_content', 'esc_textarea' ); 
  169. add_filter( 'bbp_get_form_reply_content', 'trim' ); 
  170.  
  171. // Add number format filter to functions requiring numeric output 
  172. add_filter( 'bbp_get_user_topic_count', 'bbp_number_format', 10 ); 
  173. add_filter( 'bbp_get_user_reply_count', 'bbp_number_format', 10 ); 
  174. add_filter( 'bbp_get_user_post_count', 'bbp_number_format', 10 ); 
  175. add_filter( 'bbp_get_forum_subforum_count', 'bbp_number_format', 10 ); 
  176. add_filter( 'bbp_get_forum_topic_count', 'bbp_number_format', 10 ); 
  177. add_filter( 'bbp_get_forum_reply_count', 'bbp_number_format', 10 ); 
  178. add_filter( 'bbp_get_forum_post_count', 'bbp_number_format', 10 ); 
  179. add_filter( 'bbp_get_topic_voice_count', 'bbp_number_format', 10 ); 
  180. add_filter( 'bbp_get_topic_reply_count', 'bbp_number_format', 10 ); 
  181. add_filter( 'bbp_get_topic_post_count', 'bbp_number_format', 10 ); 
  182.  
  183. // Sanitize displayed user data 
  184. add_filter( 'bbp_get_displayed_user_field', 'bbp_sanitize_displayed_user_field', 10, 3 ); 
  185.  
  186. // Run wp_kses_data on topic/reply content in admin section 
  187. if ( is_admin() ) { 
  188. add_filter( 'bbp_get_reply_content', 'bbp_kses_data' ); 
  189. add_filter( 'bbp_get_topic_content', 'bbp_kses_data' ); 
  190.  
  191. // Revisions (only when not in admin) 
  192. } else { 
  193. add_filter( 'bbp_get_reply_content', 'bbp_reply_content_append_revisions', 99, 2 ); 
  194. add_filter( 'bbp_get_topic_content', 'bbp_topic_content_append_revisions', 99, 2 ); 
  195.  
  196. // Suppress private forum details 
  197. add_filter( 'bbp_get_forum_topic_count', 'bbp_suppress_private_forum_meta', 10, 2 ); 
  198. add_filter( 'bbp_get_forum_reply_count', 'bbp_suppress_private_forum_meta', 10, 2 ); 
  199. add_filter( 'bbp_get_forum_post_count', 'bbp_suppress_private_forum_meta', 10, 2 ); 
  200. add_filter( 'bbp_get_forum_freshness_link', 'bbp_suppress_private_forum_meta', 10, 2 ); 
  201. add_filter( 'bbp_get_author_link', 'bbp_suppress_private_author_link', 10, 2 ); 
  202. add_filter( 'bbp_get_topic_author_link', 'bbp_suppress_private_author_link', 10, 2 ); 
  203. add_filter( 'bbp_get_reply_author_link', 'bbp_suppress_private_author_link', 10, 2 ); 
  204.  
  205. // Topic and reply author display names 
  206. add_filter( 'bbp_get_topic_author_display_name', 'wptexturize' ); 
  207. add_filter( 'bbp_get_topic_author_display_name', 'convert_chars' ); 
  208. add_filter( 'bbp_get_topic_author_display_name', 'esc_html' ); 
  209. add_filter( 'bbp_get_reply_author_display_name', 'wptexturize' ); 
  210. add_filter( 'bbp_get_reply_author_display_name', 'convert_chars' ); 
  211. add_filter( 'bbp_get_reply_author_display_name', 'esc_html' ); 
  212.  
  213. /** 
  214. * Add filters to anonymous post author data 
  215. */ 
  216. // Post author name 
  217. add_filter( 'bbp_pre_anonymous_post_author_name', 'trim', 10 ); 
  218. add_filter( 'bbp_pre_anonymous_post_author_name', 'sanitize_text_field', 10 ); 
  219. add_filter( 'bbp_pre_anonymous_post_author_name', 'wp_filter_kses', 10 ); 
  220. add_filter( 'bbp_pre_anonymous_post_author_name', '_wp_specialchars', 30 ); 
  221.  
  222. // Save email 
  223. add_filter( 'bbp_pre_anonymous_post_author_email', 'trim', 10 ); 
  224. add_filter( 'bbp_pre_anonymous_post_author_email', 'sanitize_email', 10 ); 
  225. add_filter( 'bbp_pre_anonymous_post_author_email', 'wp_filter_kses', 10 ); 
  226.  
  227. // Save URL 
  228. add_filter( 'bbp_pre_anonymous_post_author_website', 'trim', 10 ); 
  229. add_filter( 'bbp_pre_anonymous_post_author_website', 'wp_strip_all_tags', 10 ); 
  230. add_filter( 'bbp_pre_anonymous_post_author_website', 'esc_url_raw', 10 ); 
  231. add_filter( 'bbp_pre_anonymous_post_author_website', 'wp_filter_kses', 10 ); 
  232.  
  233. // Queries 
  234. add_filter( 'posts_request', '_bbp_has_replies_where', 10, 2 ); 
  235.  
  236. // Capabilities 
  237. add_filter( 'bbp_map_meta_caps', 'bbp_map_primary_meta_caps', 10, 4 ); // Primary caps 
  238. add_filter( 'bbp_map_meta_caps', 'bbp_map_forum_meta_caps', 10, 4 ); // Forums 
  239. add_filter( 'bbp_map_meta_caps', 'bbp_map_topic_meta_caps', 10, 4 ); // Topics 
  240. add_filter( 'bbp_map_meta_caps', 'bbp_map_reply_meta_caps', 10, 4 ); // Replies 
  241. add_filter( 'bbp_map_meta_caps', 'bbp_map_topic_tag_meta_caps', 10, 4 ); // Topic tags 
  242.  
  243. // Clickables 
  244. add_filter( 'bbp_make_clickable', 'bbp_make_urls_clickable', 2 ); // https://bbpress.org 
  245. add_filter( 'bbp_make_clickable', 'bbp_make_ftps_clickable', 4 ); // ftps://bbpress.org 
  246. add_filter( 'bbp_make_clickable', 'bbp_make_emails_clickable', 6 ); // jjj@bbpress.org 
  247. add_filter( 'bbp_make_clickable', 'bbp_make_mentions_clickable', 8 ); // @jjj 
  248.  
  249. /** Deprecated ****************************************************************/ 
  250.  
  251. /** 
  252. * The following filters are deprecated. 
  253. * 
  254. * These filters were most likely replaced by bbp_parse_args(), which includes 
  255. * both passive and aggressive filters anywhere parse_args is used to compare 
  256. * default arguments to passed arguments, without needing to litter the 
  257. * codebase with _before_ and _after_ filters everywhere. 
  258. */ 
  259.  
  260. /** 
  261. * Deprecated locale filter 
  262. * 
  263. * @since bbPress (r4213) 
  264. * 
  265. * @param string $locale 
  266. * @return string $domain 
  267. */ 
  268. function _bbp_filter_locale( $locale = '', $domain = '' ) { 
  269.  
  270. // Only apply to the bbPress text-domain 
  271. if ( bbpress()->domain !== $domain ) { 
  272. return $locale; 
  273.  
  274. return apply_filters( 'bbpress_locale', $locale, $domain ); 
  275. add_filter( 'bbp_plugin_locale', '_bbp_filter_locale', 10, 1 ); 
  276.  
  277. /** 
  278. * Deprecated forums query filter 
  279. * 
  280. * @since bbPress (r3961) 
  281. * @param array $args 
  282. * @return array 
  283. */ 
  284. function _bbp_has_forums_query( $args = array() ) { 
  285. return apply_filters( 'bbp_has_forums_query', $args ); 
  286. add_filter( 'bbp_after_has_forums_parse_args', '_bbp_has_forums_query' ); 
  287.  
  288. /** 
  289. * Deprecated topics query filter 
  290. * 
  291. * @since bbPress (r3961) 
  292. * @param array $args 
  293. * @return array 
  294. */ 
  295. function _bbp_has_topics_query( $args = array() ) { 
  296. return apply_filters( 'bbp_has_topics_query', $args ); 
  297. add_filter( 'bbp_after_has_topics_parse_args', '_bbp_has_topics_query' ); 
  298.  
  299. /** 
  300. * Deprecated replies query filter 
  301. * 
  302. * @since bbPress (r3961) 
  303. * @param array $args 
  304. * @return array 
  305. */ 
  306. function _bbp_has_replies_query( $args = array() ) { 
  307. return apply_filters( 'bbp_has_replies_query', $args ); 
  308. add_filter( 'bbp_after_has_replies_parse_args', '_bbp_has_replies_query' ); 
.