PluginProbe
bbPress / 2.0.1
bbPress v2.0.1
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 2.2.2 All 71 releases
bbpress / bbp-includes / bbp-core-classes.php

bbp-core-classes.php in bbPress 2.0.1, at bbp-includes/bbp-core-classes.php

382 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Classes
5 *
6 * @package bbPress
7 * @subpackage Classes
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 if ( !class_exists( 'BBP_Component' ) ) :
14 /**
15 * bbPress Component Class
16 *
17 * The bbPress component class is responsible for simplifying the creation
18 * of components that share similar behaviors and routines. It is used
19 * internally by bbPress to create forums, topics and replies, but can be
20 * extended to create other really neat things.
21 *
22 * @package bbPress
23 * @subpackage Classes
24 *
25 * @since bbPress (r2688)
26 */
27 class BBP_Component {
28
29 /**
30 * @var string Unique name (for internal identification)
31 * @internal
32 */
33 var $name;
34
35 /**
36 * @var Unique ID (normally for custom post type)
37 */
38 var $id;
39
40 /**
41 * @var string Unique slug (used in query string and permalinks)
42 */
43 var $slug;
44
45 /**
46 * @var WP_Query The loop for this component
47 */
48 var $query;
49
50 /**
51 * @var string The current ID of the queried object
52 */
53 var $current_id;
54
55
56 /**
57 * bbPress Component loader
58 *
59 * @since bbPress (r2700)
60 *
61 * @param mixed $args Required. Supports these args:
62 * - name: Unique name (for internal identification)
63 * - id: Unique ID (normally for custom post type)
64 * - slug: Unique slug (used in query string and permalinks)
65 * - query: The loop for this component (WP_Query)
66 * - current_id: The current ID of the queried object
67 * @uses BBP_Component::setup_globals() Setup the globals needed
68 * @uses BBP_Component::includes() Include the required files
69 * @uses BBP_Component::setup_actions() Setup the hooks and actions
70 */
71 function BBP_Component( $args = '' ) {
72 if ( empty( $args ) )
73 return;
74
75 $this->setup_globals( $args );
76 $this->includes();
77 $this->setup_actions();
78 }
79
80 /**
81 * Component global variables
82 *
83 * @since bbPress (r2700)
84 * @access private
85 *
86 * @uses apply_filters() Calls 'bbp_{@link BBP_Component::name}_id'
87 * @uses apply_filters() Calls 'bbp_{@link BBP_Component::name}_slug'
88 */
89 function setup_globals( $args = '' ) {
90 $this->name = $args['name'];
91 $this->id = apply_filters( 'bbp_' . $this->name . '_id', $args['id'] );
92 $this->slug = apply_filters( 'bbp_' . $this->name . '_slug', $args['slug'] );
93 }
94
95 /**
96 * Include required files
97 *
98 * @since bbPress (r2700)
99 * @access private
100 *
101 * @uses do_action() Calls 'bbp_{@link BBP_Component::name}includes'
102 */
103 function includes() {
104 do_action( 'bbp_' . $this->name . 'includes' );
105 }
106
107 /**
108 * Setup the actions
109 *
110 * @since bbPress (r2700)
111 * @access private
112 *
113 * @uses add_action() To add various actions
114 * @uses do_action() Calls
115 * 'bbp_{@link BBP_Component::name}setup_actions'
116 */
117 function setup_actions() {
118 // Register post types
119 add_action( 'bbp_register_post_types', array ( $this, 'register_post_types' ), 10, 2 );
120
121 // Register taxonomies
122 add_action( 'bbp_register_taxonomies', array ( $this, 'register_taxonomies' ), 10, 2 );
123
124 // Add the rewrite tags
125 add_action( 'bbp_add_rewrite_tags', array ( $this, 'add_rewrite_tags' ), 10, 2 );
126
127 // Generate rewrite rules
128 add_action( 'bbp_generate_rewrite_rules', array ( $this, 'generate_rewrite_rules' ), 10, 2 );
129
130 // Additional actions can be attached here
131 do_action( 'bbp_' . $this->name . 'setup_actions' );
132 }
133
134 /**
135 * Setup the component post types
136 *
137 * @since bbPress (r2700)
138 *
139 * @uses do_action() Calls 'bbp_{@link BBP_Component::name}_register_post_types'
140 */
141 function register_post_types() {
142 do_action( 'bbp_' . $this->name . '_register_post_types' );
143 }
144
145 /**
146 * Register component specific taxonomies
147 *
148 * @since bbPress (r2700)
149 *
150 * @uses do_action() Calls 'bbp_{@link BBP_Component::name}_register_taxonomies'
151 */
152 function register_taxonomies() {
153 do_action( 'bbp_' . $this->name . '_register_taxonomies' );
154 }
155
156 /**
157 * Add any additional rewrite tags
158 *
159 * @since bbPress (r2700)
160 *
161 * @uses do_action() Calls 'bbp_{@link BBP_Component::name}_add_rewrite_tags'
162 */
163 function add_rewrite_tags() {
164 do_action( 'bbp_' . $this->name . '_add_rewrite_tags' );
165 }
166
167 /**
168 * Generate any additional rewrite rules
169 *
170 * @since bbPress (r2700)
171 *
172 * @uses do_action() Calls 'bbp_{@link BBP_Component::name}_generate_rewrite_rules'
173 */
174 function generate_rewrite_rules ( $wp_rewrite ) {
175 do_action( 'bbp_' . $this->name . '_generate_rewrite_rules' );
176 }
177 }
178 endif; // BBP_Component
179
180 if ( class_exists( 'Walker' ) ) :
181 /**
182 * Create HTML list of forums.
183 *
184 * @package bbPress
185 * @subpackage Classes
186 *
187 * @since bbPress (r2514)
188 *
189 * @uses Walker
190 */
191 class BBP_Walker_Forum extends Walker {
192 /**
193 * @see Walker::$tree_type
194 *
195 * @since bbPress (r2514)
196 *
197 * @var string
198 */
199 var $tree_type;
200
201 /**
202 * @see Walker::$db_fields
203 *
204 * @since bbPress (r2514)
205 *
206 * @var array
207 */
208 var $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
209
210 /**
211 * Set the tree_type
212 *
213 * @since bbPress (r2514)
214 */
215 function BBP_Walker_Forum() {
216 $this->tree_type = bbp_get_forum_post_type();
217 }
218
219 /**
220 * @see Walker::start_lvl()
221 *
222 * @since bbPress (r2514)
223 *
224 * @param string $output Passed by reference. Used to append additional
225 * content.
226 * @param int $depth Depth of page. Used for padding.
227 */
228 function start_lvl( &$output, $depth ) {
229 $indent = str_repeat( "\t", $depth );
230 $output .= "\n$indent<ul class='children'>\n";
231 }
232
233 /**
234 * @see Walker::end_lvl()
235 *
236 * @since bbPress (r2514)
237 *
238 * @param string $output Passed by reference. Used to append additional
239 * content.
240 * @param int $depth Depth of page. Used for padding.
241 */
242 function end_lvl( &$output, $depth ) {
243 $indent = str_repeat( "\t", $depth );
244 $output .= "$indent</ul>\n";
245 }
246
247 /**
248 * @see Walker::start_el()
249 *
250 * @since bbPress (r2514)
251 *
252 * @param string $output Passed by reference. Used to append additional
253 * content.
254 * @param object $forum Page data object.
255 * @param int $depth Depth of page. Used for padding.
256 * @param int $current_forum Page ID.
257 * @param array $args
258 */
259 function start_el( &$output, $forum, $depth, $args, $current_forum ) {
260
261 $indent = $depth ? str_repeat( "\t", $depth ) : '';
262
263 extract( $args, EXTR_SKIP );
264 $css_class = array( 'bbp-forum-item', 'bbp-forum-item-' . $forum->ID );
265
266 if ( !empty( $current_forum ) ) {
267 $_current_page = bbp_get_forum( $current_forum );
268
269 if ( isset( $_current_page->ancestors ) && in_array( $forum->ID, (array) $_current_page->ancestors ) )
270 $css_class[] = 'bbp-current-forum-ancestor';
271
272 if ( $forum->ID == $current_forum )
273 $css_class[] = 'bbp_current_forum_item';
274 elseif ( $_current_page && $forum->ID == $_current_page->post_parent )
275 $css_class[] = 'bbp-current-forum-parent';
276
277 } elseif ( $forum->ID == get_option( 'page_for_posts' ) ) {
278 $css_class[] = 'bbp-current-forum-parent';
279 }
280
281 $css_class = implode( ' ', apply_filters( 'bbp_forum_css_class', $css_class, $forum ) );
282 $output .= $indent . '<li class="' . $css_class . '"><a href="' . bbp_get_forum_permalink( $forum->ID ) . '" title="' . esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $forum->post_title, $forum->ID ) ) ) . '">' . $link_before . apply_filters( 'the_title', $forum->post_title, $forum->ID ) . $link_after . '</a>';
283
284 if ( !empty( $show_date ) ) {
285 $time = ( 'modified' == $show_date ) ? $forum->post_modified : $time = $forum->post_date;
286 $output .= " " . mysql2date( $date_format, $time );
287 }
288 }
289
290 /**
291 * @see Walker::end_el()
292 *
293 * @since bbPress (r2514)
294 *
295 * @param string $output Passed by reference. Used to append additional
296 * content.
297 * @param object $forum Page data object. Not used.
298 * @param int $depth Depth of page. Not Used.
299 */
300 function end_el( &$output, $forum, $depth ) {
301 $output .= "</li>\n";
302 }
303 }
304
305 /**
306 * Create HTML dropdown list of bbPress forums/topics.
307 *
308 * @package bbPress
309 * @subpackage Classes
310 *
311 * @since bbPress (r2746)
312 * @uses Walker
313 */
314 class BBP_Walker_Dropdown extends Walker {
315 /**
316 * @see Walker::$tree_type
317 *
318 * @since bbPress (r2746)
319 *
320 * @var string
321 */
322 var $tree_type;
323
324 /**
325 * @see Walker::$db_fields
326 *
327 * @since bbPress (r2746)
328 *
329 * @var array
330 */
331 var $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
332
333 /**
334 * Set the tree_type
335 *
336 * @since bbPress (r2746)
337 */
338 function BBP_Walker_Dropdown() {
339 $this->tree_type = bbp_get_forum_post_type();
340 }
341
342 /**
343 * @see Walker::start_el()
344 *
345 * @since bbPress (r2746)
346 *
347 * @param string $output Passed by reference. Used to append additional
348 * content.
349 * @param object $post Post data object.
350 * @param int $depth Depth of post in reference to parent posts. Used
351 * for padding.
352 * @param array $args Uses 'selected' argument for selected post to set
353 * selected HTML attribute for option element.
354 * @uses bbp_is_forum_category() To check if the forum is a category
355 * @uses current_user_can() To check if the current user can post in
356 * closed forums
357 * @uses bbp_is_forum_closed() To check if the forum is closed
358 * @uses apply_filters() Calls 'bbp_walker_dropdown_post_title' with the
359 * title, output, post, depth and args
360 */
361 function start_el( &$output, $post, $depth, $args ) {
362 $pad = str_repeat( '&nbsp;', $depth * 3 );
363 $output .= "\t<option class=\"level-$depth\"";
364
365 // Disable the <option> if we're told to do so, the post type is bbp_forum and the forum is a category or is closed
366 if ( true == $args['disable_categories'] && $post->post_type == bbp_get_forum_post_type() && ( bbp_is_forum_category( $post->ID ) || ( !current_user_can( 'edit_forum', $post->ID ) && bbp_is_forum_closed( $post->ID ) ) ) )
367 $output .= ' disabled="disabled" value=""';
368 else
369 $output .= ' value="' .$post->ID .'"' . selected( $args['selected'], $post->ID, false );
370
371 $output .= '>';
372 $title = esc_html( $post->post_title );
373 $title = apply_filters( 'bbp_walker_dropdown_post_title', $post->post_title, $output, $post, $depth, $args );
374 $output .= $pad . $title;
375 $output .= "</option>\n";
376 }
377 }
378
379 endif; // class_exists check
380
381 ?>
382