PluginProbe
bbPress / 2.6.19
bbPress v2.6.19
2.6.19 2.6.18 2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 All 74 releases
bbpress / templates / default / bbpress-functions.php

bbpress-functions.php in bbPress 2.6.19, at templates/default/bbpress-functions.php

379 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Functions of the Default template-pack
5 *
6 * @package bbPress
7 * @subpackage BBP_Theme_Compat
8 * @since 2.1.0 bbPress (r3734)
9 */
10
11 // Exit if accessed directly
12 defined( 'ABSPATH' ) || exit;
13
14 /** Theme Setup ***************************************************************/
15
16 if ( ! class_exists( 'BBP_Default' ) ) :
17
18 /**
19 * Loads bbPress Default Theme functionality
20 *
21 * This is not a real theme by WordPress standards, and is instead used as the
22 * fallback for any WordPress theme that does not have bbPress templates in it.
23 *
24 * To make your custom theme bbPress compatible and customize the templates, you
25 * can copy these files into your theme without needing to merge anything
26 * together; bbPress should safely handle the rest.
27 *
28 * See @link BBP_Theme_Compat() for more.
29 *
30 * @since 2.1.0 bbPress (r3734)
31 *
32 * @package bbPress
33 * @subpackage BBP_Theme_Compat
34 */
35 class BBP_Default extends BBP_Theme_Compat {
36
37 /** Functions *************************************************************/
38
39 /**
40 * The main bbPress (Default) Loader
41 *
42 * @since 2.1.0 bbPress (r3734)
43 */
44 public function __construct( $properties = array() ) {
45
46 parent::__construct(
47 bbp_parse_args(
48 $properties,
49 array(
50 'id' => 'default',
51 'name' => 'bbPress Default',
52 'version' => bbp_get_asset_version(),
53 'dir' => trailingslashit( bbpress()->themes_dir . 'default' ),
54 'url' => trailingslashit( bbpress()->themes_url . 'default' ),
55 ),
56 'default_theme'
57 )
58 );
59
60 $this->setup_actions();
61 }
62
63 /**
64 * Setup the theme hooks
65 *
66 * @since 2.1.0 bbPress (r3734)
67 *
68 * @access private
69 */
70 private function setup_actions() {
71
72 /** Scripts ***********************************************************/
73
74 add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); // Enqueue theme CSS
75 add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Enqueue theme JS
76 add_filter( 'bbp_enqueue_scripts', array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization
77 add_action( 'bbp_ajax_favorite', array( $this, 'ajax_favorite' ) ); // Handles the topic ajax favorite/unfavorite
78 add_action( 'bbp_ajax_subscription', array( $this, 'ajax_subscription' ) ); // Handles the topic ajax subscribe/unsubscribe
79
80 /** Template Wrappers *************************************************/
81
82 add_action( 'bbp_before_main_content', array( $this, 'before_main_content' ) ); // Top wrapper HTML
83 add_action( 'bbp_after_main_content', array( $this, 'after_main_content' ) ); // Bottom wrapper HTML
84
85 /** Override **********************************************************/
86
87 do_action_ref_array( 'bbp_theme_compat_actions', array( &$this ) );
88 }
89
90 /**
91 * Inserts HTML at the top of the main content area to be compatible with
92 * the Twenty Twelve theme.
93 *
94 * @since 2.1.0 bbPress (r3734)
95 */
96 public function before_main_content() {
97 ?>
98
99 <div id="bbp-container">
100 <div id="bbp-content" role="main">
101
102 <?php
103 }
104
105 /**
106 * Inserts HTML at the bottom of the main content area to be compatible with
107 * the Twenty Twelve theme.
108 *
109 * @since 2.1.0 bbPress (r3734)
110 */
111 public function after_main_content() {
112 ?>
113
114 </div><!-- #bbp-content -->
115 </div><!-- #bbp-container -->
116
117 <?php
118 }
119
120 /**
121 * Load the theme CSS
122 *
123 * @since 2.1.0 bbPress (r3734)
124 */
125 public function enqueue_styles() {
126
127 // Setup the default styling
128 $defaults = array(
129 'bbp-default' => array(
130 'file' => 'css/bbpress.css',
131 'dependencies' => array()
132 )
133 );
134
135 // Optionally support an RTL variant
136 if ( is_rtl() ) {
137 $defaults['bbp-default-rtl'] = array(
138 'file' => 'css/bbpress-rtl.css',
139 'dependencies' => array()
140 );
141 }
142
143 // Get and filter the bbp-default style
144 $styles = apply_filters( 'bbp_default_styles', $defaults );
145
146 // Enqueue the styles
147 foreach ( $styles as $handle => $attributes ) {
148 bbp_enqueue_style( $handle, $attributes['file'], $attributes['dependencies'], $this->version );
149 }
150 }
151
152 /**
153 * Enqueue the required JavaScript files
154 *
155 * @since 2.1.0 bbPress (r3734)
156 */
157 public function enqueue_scripts() {
158
159 // Setup scripts array
160 $scripts = array();
161
162 // Editor scripts
163 // @see https://bbpress.trac.wordpress.org/ticket/2930
164 if ( bbp_use_wp_editor() && is_bbpress() ) {
165 $scripts['bbpress-editor'] = array(
166 'file' => 'js/editor.js',
167 'dependencies' => array( 'jquery' )
168 );
169 }
170
171 // Forum-specific scripts
172 if ( bbp_is_single_forum() ) {
173 $scripts['bbpress-engagements'] = array(
174 'file' => 'js/engagements.js',
175 'dependencies' => array( 'jquery' )
176 );
177 }
178
179 // Topic-specific scripts
180 if ( bbp_is_single_topic() || bbp_is_topic_edit() ) {
181
182 // Engagements
183 $scripts['bbpress-engagements'] = array(
184 'file' => 'js/engagements.js',
185 'dependencies' => array( 'jquery' )
186 );
187
188 // Hierarchical replies
189 if ( bbp_thread_replies() ) {
190 $scripts['bbpress-reply'] = array(
191 'file' => 'js/reply.js',
192 'dependencies' => array( 'jquery' )
193 );
194 }
195 }
196
197 // User Profile edit
198 if ( bbp_is_single_user_edit() ) {
199 wp_enqueue_script( 'user-profile' );
200 }
201
202 // Filter the scripts
203 $scripts = apply_filters( 'bbp_default_scripts', $scripts );
204
205 // Enqueue the scripts
206 foreach ( $scripts as $handle => $attributes ) {
207 bbp_enqueue_script( $handle, $attributes['file'], $attributes['dependencies'], $this->version, true );
208 }
209 }
210
211 /**
212 * Load localizations for topic script
213 *
214 * These localizations require information that may not be loaded even by init.
215 *
216 * @since 2.1.0 bbPress (r3734)
217 */
218 public function localize_topic_script() {
219
220 // Single forum or topic
221 if ( bbp_is_single_forum() || bbp_is_single_topic() ) {
222 wp_localize_script(
223 'bbpress-engagements',
224 'bbpEngagementJS',
225 array(
226 'object_id' => get_the_ID(),
227 'bbp_ajaxurl' => bbp_get_ajax_url(),
228 'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
229 )
230 );
231 }
232 }
233
234 /**
235 * AJAX handler to add or remove a topic from a user's favorites
236 *
237 * @since 2.1.0 bbPress (r3734)
238 */
239 public function ajax_favorite() {
240
241 // Bail if favorites are not active
242 if ( ! bbp_is_favorites_active() ) {
243 bbp_ajax_response( false, esc_html__( 'Favorites are no longer active.', 'bbpress' ), 300 );
244 }
245
246 // Bail if user is not logged in
247 if ( ! is_user_logged_in() ) {
248 bbp_ajax_response( false, esc_html__( 'Please login to favorite.', 'bbpress' ), 301 );
249 }
250
251 // Get user and topic data
252 $user_id = bbp_get_current_user_id();
253 $id = ! empty( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
254 $type = 'post';
255 if ( ! empty( $_POST['type'] ) ) {
256 $type = is_string( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : '';
257 }
258
259 // Bail if user cannot add favorites for this user
260 if ( ! current_user_can( 'edit_user', $user_id ) ) {
261 bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 );
262 }
263
264 // Get the object
265 $object = get_post( $id );
266
267 // Validate the object before checking its favorite state
268 if ( empty( $object ) || ! bbp_current_user_can_toggle_engagement( $object->ID, $type, 'favorite', 'remove' ) ) {
269 bbp_ajax_response( false, esc_html__( 'Favorite failed.', 'bbpress' ), 303 );
270 }
271 $is_favorite = bbp_is_user_favorite( $user_id, $object->ID );
272 $toggle_action = $is_favorite ? 'remove' : 'add';
273 if ( ! bbp_current_user_can_toggle_engagement( $object->ID, $type, 'favorite', $toggle_action ) ) {
274 bbp_ajax_response( false, esc_html__( 'Favorite failed.', 'bbpress' ), 303 );
275 }
276
277 // Bail if user did not take this action
278 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $object->ID ) ) {
279 bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
280 }
281
282 // Take action
283 $status = $is_favorite
284 ? bbp_remove_user_favorite( $user_id, $object->ID )
285 : bbp_add_user_favorite( $user_id, $object->ID );
286
287 // Bail if action failed
288 if ( empty( $status ) ) {
289 bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
290 }
291
292 // Put subscription attributes in convenient array
293 $attrs = array(
294 'object_id' => $object->ID,
295 'object_type' => $type,
296 'user_id' => $user_id
297 );
298
299 // Action succeeded
300 bbp_ajax_response( true, bbp_get_user_favorites_link( $attrs, $user_id, false ), 200 );
301 }
302
303 /**
304 * AJAX handler to Subscribe/Unsubscribe a user from a topic
305 *
306 * @since 2.1.0 bbPress (r3734)
307 */
308 public function ajax_subscription() {
309
310 // Bail if subscriptions are not active
311 if ( ! bbp_is_subscriptions_active() ) {
312 bbp_ajax_response( false, esc_html__( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
313 }
314
315 // Bail if user is not logged in
316 if ( ! is_user_logged_in() ) {
317 bbp_ajax_response( false, esc_html__( 'Please login to subscribe.', 'bbpress' ), 301 );
318 }
319
320 // Get user and topic data
321 $user_id = bbp_get_current_user_id();
322 $id = ! empty( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
323 $type = 'post';
324 if ( ! empty( $_POST['type'] ) ) {
325 $type = is_string( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : '';
326 }
327
328 // Bail if user cannot add favorites for this user
329 if ( ! current_user_can( 'edit_user', $user_id ) ) {
330 bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 );
331 }
332
333 // Get the object
334 $object = get_post( $id );
335
336 // Validate the object before checking its subscription state
337 if ( empty( $object ) || ! bbp_current_user_can_toggle_engagement( $object->ID, $type, 'subscription', 'remove' ) ) {
338 bbp_ajax_response( false, esc_html__( 'Subscription failed.', 'bbpress' ), 303 );
339 }
340 $is_subscribed = bbp_is_user_subscribed( $user_id, $object->ID, $type );
341 $toggle_action = $is_subscribed ? 'remove' : 'add';
342 if ( ! bbp_current_user_can_toggle_engagement( $object->ID, $type, 'subscription', $toggle_action ) ) {
343 bbp_ajax_response( false, esc_html__( 'Subscription failed.', 'bbpress' ), 303 );
344 }
345
346 // Bail if user did not take this action
347 if ( ! isset( $_POST['nonce'] ) || ( ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_post_' . $object->ID ) && ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $object->ID ) ) ) {
348 bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
349 }
350
351 // Take action
352 $status = $is_subscribed
353 ? bbp_remove_user_subscription( $user_id, $object->ID )
354 : bbp_add_user_subscription( $user_id, $object->ID );
355
356 // Bail if action failed
357 if ( empty( $status ) ) {
358 bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
359 }
360
361 // Put subscription attributes in convenient array
362 $attrs = array(
363 'object_id' => $object->ID,
364 'object_type' => $type,
365 'user_id' => $user_id
366 );
367
368 // Add separator to topic if favorites is active
369 if ( ( 'post' === $type ) && ( bbp_get_topic_post_type() === get_post_type( $object ) ) && bbp_is_favorites_active() ) {
370 $attrs['before'] = '&nbsp;|&nbsp;';
371 }
372
373 // Action succeeded
374 bbp_ajax_response( true, bbp_get_user_subscribe_link( $attrs, $user_id, false ), 200 );
375 }
376 }
377 new BBP_Default();
378 endif;
379