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