PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
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 2.2 2.2.1 All 72 releases
bbpress / templates / default / bbpress-functions.php

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

367 lines 10.3 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 (r3732)
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 (r3732)
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 (r3732)
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 (r3732)
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 (r3732)
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 (r3732)
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 (r3732)
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 (r3732)
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 (r3732)
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 (r3732)
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 = ! empty( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : 'post';
255
256 // Bail if user cannot add favorites for this user
257 if ( ! current_user_can( 'edit_user', $user_id ) ) {
258 bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 );
259 }
260
261 // Get the object
262 if ( 'post' === $type ) {
263 $object = get_post( $id );
264 }
265
266 // Bail if topic cannot be found
267 if ( empty( $object ) ) {
268 bbp_ajax_response( false, esc_html__( 'Favorite failed.', 'bbpress' ), 303 );
269 }
270
271 // Bail if user did not take this action
272 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $object->ID ) ) {
273 bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
274 }
275
276 // Take action
277 $status = bbp_is_user_favorite( $user_id, $object->ID )
278 ? bbp_remove_user_favorite( $user_id, $object->ID )
279 : bbp_add_user_favorite( $user_id, $object->ID );
280
281 // Bail if action failed
282 if ( empty( $status ) ) {
283 bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
284 }
285
286 // Put subscription attributes in convenient array
287 $attrs = array(
288 'object_id' => $object->ID,
289 'object_type' => $type,
290 'user_id' => $user_id
291 );
292
293 // Action succeeded
294 bbp_ajax_response( true, bbp_get_user_favorites_link( $attrs, $user_id, false ), 200 );
295 }
296
297 /**
298 * AJAX handler to Subscribe/Unsubscribe a user from a topic
299 *
300 * @since 2.1.0 bbPress (r3732)
301 */
302 public function ajax_subscription() {
303
304 // Bail if subscriptions are not active
305 if ( ! bbp_is_subscriptions_active() ) {
306 bbp_ajax_response( false, esc_html__( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
307 }
308
309 // Bail if user is not logged in
310 if ( ! is_user_logged_in() ) {
311 bbp_ajax_response( false, esc_html__( 'Please login to subscribe.', 'bbpress' ), 301 );
312 }
313
314 // Get user and topic data
315 $user_id = bbp_get_current_user_id();
316 $id = ! empty( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
317 $type = ! empty( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : 'post';
318
319 // Bail if user cannot add favorites for this user
320 if ( ! current_user_can( 'edit_user', $user_id ) ) {
321 bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 );
322 }
323
324 // Get the object
325 if ( 'post' === $type ) {
326 $object = get_post( $id );
327 }
328
329 // Bail if topic cannot be found
330 if ( empty( $object ) ) {
331 bbp_ajax_response( false, esc_html__( 'Subscription failed.', 'bbpress' ), 303 );
332 }
333
334 // Bail if user did not take this action
335 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $object->ID ) ) {
336 bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
337 }
338
339 // Take action
340 $status = bbp_is_user_subscribed( $user_id, $object->ID )
341 ? bbp_remove_user_subscription( $user_id, $object->ID )
342 : bbp_add_user_subscription( $user_id, $object->ID );
343
344 // Bail if action failed
345 if ( empty( $status ) ) {
346 bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
347 }
348
349 // Put subscription attributes in convenient array
350 $attrs = array(
351 'object_id' => $object->ID,
352 'object_type' => $type,
353 'user_id' => $user_id
354 );
355
356 // Add separator to topic if favorites is active
357 if ( ( 'post' === $type ) && ( bbp_get_topic_post_type() === get_post_type( $object ) ) && bbp_is_favorites_active() ) {
358 $attrs['before'] = '&nbsp;|&nbsp;';
359 }
360
361 // Action succeeded
362 bbp_ajax_response( true, bbp_get_user_subscribe_link( $attrs, $user_id, false ), 200 );
363 }
364 }
365 new BBP_Default();
366 endif;
367