BBP_Default

Loads bbPress Default Theme functionality.

Defined (1)

The class is defined in the following location(s).

/templates/default/bbpress-functions.php  
  1. class BBP_Default extends BBP_Theme_Compat { 
  2.  
  3. /** Functions *************************************************************/ 
  4.  
  5. /** 
  6. * The main bbPress (Default) Loader 
  7. * @since bbPress (r3732) 
  8. * @uses BBP_Default::setup_globals() 
  9. * @uses BBP_Default::setup_actions() 
  10. */ 
  11. public function __construct( $properties = array() ) { 
  12.  
  13. parent::__construct( bbp_parse_args( $properties, array( 
  14. 'id' => 'default',  
  15. 'name' => __( 'bbPress Default', 'bbpress' ),  
  16. 'version' => bbp_get_version(),  
  17. 'dir' => trailingslashit( bbpress()->themes_dir . 'default' ),  
  18. 'url' => trailingslashit( bbpress()->themes_url . 'default' ),  
  19. ), 'default_theme' ) ); 
  20.  
  21. $this->setup_actions(); 
  22.  
  23. /** 
  24. * Setup the theme hooks 
  25. * @since bbPress (r3732) 
  26. * @access private 
  27. * @uses add_filter() To add various filters 
  28. * @uses add_action() To add various actions 
  29. */ 
  30. private function setup_actions() { 
  31.  
  32. /** Scripts ***********************************************************/ 
  33.  
  34. add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); // Enqueue theme CSS 
  35. add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Enqueue theme JS 
  36. add_filter( 'bbp_enqueue_scripts', array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization 
  37. add_action( 'bbp_ajax_favorite', array( $this, 'ajax_favorite' ) ); // Handles the topic ajax favorite/unfavorite 
  38. add_action( 'bbp_ajax_subscription', array( $this, 'ajax_subscription' ) ); // Handles the topic ajax subscribe/unsubscribe 
  39. add_action( 'bbp_ajax_forum_subscription', array( $this, 'ajax_forum_subscription' ) ); // Handles the forum ajax subscribe/unsubscribe 
  40.  
  41. /** Template Wrappers *************************************************/ 
  42.  
  43. add_action( 'bbp_before_main_content', array( $this, 'before_main_content' ) ); // Top wrapper HTML 
  44. add_action( 'bbp_after_main_content', array( $this, 'after_main_content' ) ); // Bottom wrapper HTML 
  45.  
  46. /** Override **********************************************************/ 
  47.  
  48. do_action_ref_array( 'bbp_theme_compat_actions', array( &$this ) ); 
  49.  
  50. /** 
  51. * Inserts HTML at the top of the main content area to be compatible with 
  52. * the Twenty Twelve theme. 
  53. * @since bbPress (r3732) 
  54. */ 
  55. public function before_main_content() { 
  56. ?> 
  57.  
  58. <div id="bbp-container"> 
  59. <div id="bbp-content" role="main"> 
  60.  
  61. <?php 
  62.  
  63. /** 
  64. * Inserts HTML at the bottom of the main content area to be compatible with 
  65. * the Twenty Twelve theme. 
  66. * @since bbPress (r3732) 
  67. */ 
  68. public function after_main_content() { 
  69. ?> 
  70.  
  71. </div><!-- #bbp-content --> 
  72. </div><!-- #bbp-container --> 
  73.  
  74. <?php 
  75.  
  76. /** 
  77. * Load the theme CSS 
  78. * @since bbPress (r3732) 
  79. * @uses wp_enqueue_style() To enqueue the styles 
  80. */ 
  81. public function enqueue_styles() { 
  82.  
  83. // Setup styles array 
  84. $styles = array(); 
  85.  
  86. // LTR 
  87. $styles['bbp-default'] = array( 
  88. 'file' => 'css/bbpress.css',  
  89. 'dependencies' => array() 
  90. ); 
  91.  
  92. // RTL helpers 
  93. if ( is_rtl() ) { 
  94. $styles['bbp-default-rtl'] = array( 
  95. 'file' => 'css/bbpress-rtl.css',  
  96. 'dependencies' => array( 'bbp-default' ) 
  97. ); 
  98.  
  99. // Filter the scripts 
  100. $styles = apply_filters( 'bbp_default_styles', $styles ); 
  101.  
  102. // Enqueue the styles 
  103. foreach ( $styles as $handle => $attributes ) { 
  104. bbp_enqueue_style( $handle, $attributes['file'], $attributes['dependencies'], $this->version, 'screen' ); 
  105.  
  106. /** 
  107. * Enqueue the required Javascript files 
  108. * @since bbPress (r3732) 
  109. * @uses bbp_is_single_forum() To check if it's the forum page 
  110. * @uses bbp_is_single_topic() To check if it's the topic page 
  111. * @uses bbp_thread_replies() To check if threaded replies are enabled 
  112. * @uses bbp_is_single_user_edit() To check if it's the profile edit page 
  113. * @uses wp_enqueue_script() To enqueue the scripts 
  114. */ 
  115. public function enqueue_scripts() { 
  116.  
  117. // Setup scripts array 
  118. $scripts = array(); 
  119.  
  120. // Always pull in jQuery for TinyMCE shortcode usage 
  121. if ( bbp_use_wp_editor() ) { 
  122. $scripts['bbpress-editor'] = array( 
  123. 'file' => 'js/editor.js',  
  124. 'dependencies' => array( 'jquery' ) 
  125. ); 
  126.  
  127. // Forum-specific scripts 
  128. if ( bbp_is_single_forum() ) { 
  129. $scripts['bbpress-forum'] = array( 
  130. 'file' => 'js/forum.js',  
  131. 'dependencies' => array( 'jquery' ) 
  132. ); 
  133.  
  134. // Topic-specific scripts 
  135. if ( bbp_is_single_topic() ) { 
  136.  
  137. // Topic favorite/unsubscribe 
  138. $scripts['bbpress-topic'] = array( 
  139. 'file' => 'js/topic.js',  
  140. 'dependencies' => array( 'jquery' ) 
  141. ); 
  142.  
  143. // Hierarchical replies 
  144. if ( bbp_thread_replies() ) { 
  145. $scripts['bbpress-reply'] = array( 
  146. 'file' => 'js/reply.js',  
  147. 'dependencies' => array( 'jquery' ) 
  148. ); 
  149.  
  150. // User Profile edit 
  151. if ( bbp_is_single_user_edit() ) { 
  152. $scripts['bbpress-user'] = array( 
  153. 'file' => 'js/user.js',  
  154. 'dependencies' => array( 'user-query' ) 
  155. ); 
  156.  
  157. // Filter the scripts 
  158. $scripts = apply_filters( 'bbp_default_scripts', $scripts ); 
  159.  
  160. // Enqueue the scripts 
  161. foreach ( $scripts as $handle => $attributes ) { 
  162. bbp_enqueue_script( $handle, $attributes['file'], $attributes['dependencies'], $this->version, 'screen' ); 
  163.  
  164. /** 
  165. * Load localizations for topic script 
  166. * These localizations require information that may not be loaded even by init. 
  167. * @since bbPress (r3732) 
  168. * @uses bbp_is_single_forum() To check if it's the forum page 
  169. * @uses bbp_is_single_topic() To check if it's the topic page 
  170. * @uses is_user_logged_in() To check if user is logged in 
  171. * @uses bbp_get_current_user_id() To get the current user id 
  172. * @uses bbp_get_forum_id() To get the forum id 
  173. * @uses bbp_get_topic_id() To get the topic id 
  174. * @uses bbp_get_favorites_permalink() To get the favorites permalink 
  175. * @uses bbp_is_user_favorite() To check if the topic is in user's favorites 
  176. * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 
  177. * @uses bbp_is_user_subscribed() To check if the user is subscribed to topic 
  178. * @uses bbp_get_topic_permalink() To get the topic permalink 
  179. * @uses wp_localize_script() To localize the script 
  180. */ 
  181. public function localize_topic_script() { 
  182.  
  183. // Single forum 
  184. if ( bbp_is_single_forum() ) { 
  185. wp_localize_script( 'bbpress-forum', 'bbpForumJS', array( 
  186. 'bbp_ajaxurl' => bbp_get_ajax_url(),  
  187. 'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),  
  188. 'is_user_logged_in' => is_user_logged_in(),  
  189. 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 
  190. ) ); 
  191.  
  192. // Single topic 
  193. } elseif ( bbp_is_single_topic() ) { 
  194. wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array( 
  195. 'bbp_ajaxurl' => bbp_get_ajax_url(),  
  196. 'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),  
  197. 'is_user_logged_in' => is_user_logged_in(),  
  198. 'fav_nonce' => wp_create_nonce( 'toggle-favorite_' . get_the_ID() ),  
  199. 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 
  200. ) ); 
  201.  
  202. /** 
  203. * AJAX handler to Subscribe/Unsubscribe a user from a forum 
  204. * @since bbPress (r5155) 
  205. * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 
  206. * @uses bbp_is_user_logged_in() To check if user is logged in 
  207. * @uses bbp_get_current_user_id() To get the current user id 
  208. * @uses current_user_can() To check if the current user can edit the user 
  209. * @uses bbp_get_forum() To get the forum 
  210. * @uses wp_verify_nonce() To verify the nonce 
  211. * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions 
  212. * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions 
  213. * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions 
  214. * @uses bbp_ajax_response() To return JSON 
  215. */ 
  216. public function ajax_forum_subscription() { 
  217.  
  218. // Bail if subscriptions are not active 
  219. if ( ! bbp_is_subscriptions_active() ) { 
  220. bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 ); 
  221.  
  222. // Bail if user is not logged in 
  223. if ( ! is_user_logged_in() ) { 
  224. bbp_ajax_response( false, __( 'Please login to subscribe to this forum.', 'bbpress' ), 301 ); 
  225.  
  226. // Get user and forum data 
  227. $user_id = bbp_get_current_user_id(); 
  228. $id = intval( $_POST['id'] ); 
  229.  
  230. // Bail if user cannot add favorites for this user 
  231. if ( ! current_user_can( 'edit_user', $user_id ) ) { 
  232. bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 ); 
  233.  
  234. // Get the forum 
  235. $forum = bbp_get_forum( $id ); 
  236.  
  237. // Bail if forum cannot be found 
  238. if ( empty( $forum ) ) { 
  239. bbp_ajax_response( false, __( 'The forum could not be found.', 'bbpress' ), 303 ); 
  240.  
  241. // Bail if user did not take this action 
  242. if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $forum->ID ) ) { 
  243. bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); 
  244.  
  245. // Take action 
  246. $status = bbp_is_user_subscribed( $user_id, $forum->ID ) ? bbp_remove_user_subscription( $user_id, $forum->ID ) : bbp_add_user_subscription( $user_id, $forum->ID ); 
  247.  
  248. // Bail if action failed 
  249. if ( empty( $status ) ) { 
  250. bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 ); 
  251.  
  252. // Put subscription attributes in convenient array 
  253. $attrs = array( 
  254. 'forum_id' => $forum->ID,  
  255. 'user_id' => $user_id 
  256. ); 
  257.  
  258. // Action succeeded 
  259. bbp_ajax_response( true, bbp_get_forum_subscription_link( $attrs, $user_id, false ), 200 ); 
  260.  
  261. /** 
  262. * AJAX handler to add or remove a topic from a user's favorites 
  263. * @since bbPress (r3732) 
  264. * @uses bbp_is_favorites_active() To check if favorites are active 
  265. * @uses bbp_is_user_logged_in() To check if user is logged in 
  266. * @uses bbp_get_current_user_id() To get the current user id 
  267. * @uses current_user_can() To check if the current user can edit the user 
  268. * @uses bbp_get_topic() To get the topic 
  269. * @uses wp_verify_nonce() To verify the nonce & check the referer 
  270. * @uses bbp_is_user_favorite() To check if the topic is user's favorite 
  271. * @uses bbp_remove_user_favorite() To remove the topic from user's favorites 
  272. * @uses bbp_add_user_favorite() To add the topic from user's favorites 
  273. * @uses bbp_ajax_response() To return JSON 
  274. */ 
  275. public function ajax_favorite() { 
  276.  
  277. // Bail if favorites are not active 
  278. if ( ! bbp_is_favorites_active() ) { 
  279. bbp_ajax_response( false, __( 'Favorites are no longer active.', 'bbpress' ), 300 ); 
  280.  
  281. // Bail if user is not logged in 
  282. if ( ! is_user_logged_in() ) { 
  283. bbp_ajax_response( false, __( 'Please login to make this topic a favorite.', 'bbpress' ), 301 ); 
  284.  
  285. // Get user and topic data 
  286. $user_id = bbp_get_current_user_id(); 
  287. $id = !empty( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; 
  288.  
  289. // Bail if user cannot add favorites for this user 
  290. if ( ! current_user_can( 'edit_user', $user_id ) ) { 
  291. bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 ); 
  292.  
  293. // Get the topic 
  294. $topic = bbp_get_topic( $id ); 
  295.  
  296. // Bail if topic cannot be found 
  297. if ( empty( $topic ) ) { 
  298. bbp_ajax_response( false, __( 'The topic could not be found.', 'bbpress' ), 303 ); 
  299.  
  300. // Bail if user did not take this action 
  301. if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $topic->ID ) ) { 
  302. bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); 
  303.  
  304. // Take action 
  305. $status = bbp_is_user_favorite( $user_id, $topic->ID ) ? bbp_remove_user_favorite( $user_id, $topic->ID ) : bbp_add_user_favorite( $user_id, $topic->ID ); 
  306.  
  307. // Bail if action failed 
  308. if ( empty( $status ) ) { 
  309. bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 ); 
  310.  
  311. // Put subscription attributes in convenient array 
  312. $attrs = array( 
  313. 'topic_id' => $topic->ID,  
  314. 'user_id' => $user_id 
  315. ); 
  316.  
  317. // Action succeeded 
  318. bbp_ajax_response( true, bbp_get_user_favorites_link( $attrs, $user_id, false ), 200 ); 
  319.  
  320. /** 
  321. * AJAX handler to Subscribe/Unsubscribe a user from a topic 
  322. * @since bbPress (r3732) 
  323. * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 
  324. * @uses bbp_is_user_logged_in() To check if user is logged in 
  325. * @uses bbp_get_current_user_id() To get the current user id 
  326. * @uses current_user_can() To check if the current user can edit the user 
  327. * @uses bbp_get_topic() To get the topic 
  328. * @uses wp_verify_nonce() To verify the nonce 
  329. * @uses bbp_is_user_subscribed() To check if the topic is in user's subscriptions 
  330. * @uses bbp_remove_user_subscriptions() To remove the topic from user's subscriptions 
  331. * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions 
  332. * @uses bbp_ajax_response() To return JSON 
  333. */ 
  334. public function ajax_subscription() { 
  335.  
  336. // Bail if subscriptions are not active 
  337. if ( ! bbp_is_subscriptions_active() ) { 
  338. bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 ); 
  339.  
  340. // Bail if user is not logged in 
  341. if ( ! is_user_logged_in() ) { 
  342. bbp_ajax_response( false, __( 'Please login to subscribe to this topic.', 'bbpress' ), 301 ); 
  343.  
  344. // Get user and topic data 
  345. $user_id = bbp_get_current_user_id(); 
  346. $id = intval( $_POST['id'] ); 
  347.  
  348. // Bail if user cannot add favorites for this user 
  349. if ( ! current_user_can( 'edit_user', $user_id ) ) { 
  350. bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 ); 
  351.  
  352. // Get the topic 
  353. $topic = bbp_get_topic( $id ); 
  354.  
  355. // Bail if topic cannot be found 
  356. if ( empty( $topic ) ) { 
  357. bbp_ajax_response( false, __( 'The topic could not be found.', 'bbpress' ), 303 ); 
  358.  
  359. // Bail if user did not take this action 
  360. if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $topic->ID ) ) { 
  361. bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); 
  362.  
  363. // Take action 
  364. $status = bbp_is_user_subscribed( $user_id, $topic->ID ) ? bbp_remove_user_subscription( $user_id, $topic->ID ) : bbp_add_user_subscription( $user_id, $topic->ID ); 
  365.  
  366. // Bail if action failed 
  367. if ( empty( $status ) ) { 
  368. bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 ); 
  369.  
  370. // Put subscription attributes in convenient array 
  371. $attrs = array( 
  372. 'topic_id' => $topic->ID,  
  373. 'user_id' => $user_id 
  374. ); 
  375.  
  376. // Action succeeded 
  377. bbp_ajax_response( true, bbp_get_user_subscribe_link( $attrs, $user_id, false ), 200 );