__construct(); } /** * PHP 5 constructor * * @since 1.0-beta */ function __construct() { // Register our custom taxonomy add_filter( 'bp_docs_init', array( &$this, 'register_taxonomy' ), 11 ); // Make sure that the bp_docs post type supports our post taxonomies add_filter( 'bp_docs_init', array( $this, 'register_with_post_type' ), 12 ); // Hook into post saves to save any taxonomy terms. add_action( 'bp_docs_doc_saved', array( $this, 'save_post' ) ); // Display a doc's terms on its single doc page add_action( 'bp_docs_single_doc_meta', array( $this, 'show_terms' ) ); // Modify the main tax_query in the doc loop add_filter( 'bp_docs_tax_query', array( $this, 'modify_tax_query' ) ); // Add the Tags column to the docs loop add_filter( 'bp_docs_loop_additional_th', array( $this, 'tags_th' ) ); add_filter( 'bp_docs_loop_additional_td', array( $this, 'tags_td' ) ); // Filter the message in the docs info header add_filter( 'bp_docs_info_header_message', array( $this, 'info_header_message' ), 10, 2 ); // Add the tags filter markup add_filter( 'bp_docs_filter_types', array( $this, 'filter_type' ) ); add_filter( 'bp_docs_filter_sections', array( $this, 'filter_markup' ) ); // Determine whether the directory view is filtered by bpd_tag. add_filter( 'bp_docs_is_directory_view_filtered', array( $this, 'is_directory_view_filtered' ), 10, 2 ); // Adds filter arguments to a URL add_filter( 'bp_docs_handle_filters', array( $this, 'handle_filters' ) ); } /** * Registers the custom taxonomy for BP doc tags * * @since 1.0-beta * */ function register_taxonomy() { global $bp; $this->docs_tag_tax_name = apply_filters( 'bp_docs_docs_tag_tax_name', 'bp_docs_tag' ); $bp->bp_docs->docs_tag_tax_name = $this->docs_tag_tax_name; // Define the labels to be used by the taxonomy bp_docs_tag $doc_tags_labels = array( 'name' => __( 'Docs Tags', 'bp-docs' ), 'singular_name' => __( 'Docs Tag', 'bp-docs' ) ); // Register the bp_docs_associated_item taxonomy register_taxonomy( $this->docs_tag_tax_name, array( $bp->bp_docs->post_type_name ), array( 'labels' => $doc_tags_labels, 'hierarchical' => false, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'item' ), ) ); } /** * Registers the post taxonomies with the bp_docs post type * * @since 1.0-beta * * @param array The $bp_docs_post_type_args array created in BP_Docs::register_post_type() * @return array $args The modified parameters */ function register_with_post_type() { $this->taxonomies = array( /* 'category', */ $this->docs_tag_tax_name ); foreach ( $this->taxonomies as $tax ) { register_taxonomy_for_object_type( $tax, bp_docs_get_post_type_name() ); } } /** * Saves post taxonomy terms to a doc when saved from the front end * * @since 1.0-beta * * @param object $query The query object created by BP_Docs_Query * @return int $post_id Returns the doc's post_id on success */ function save_post( $query ) { foreach ( $this->taxonomies as $tax_name ) { if ( $tax_name == 'category' ) $tax_name = 'post_category'; // Separate out the terms $terms = !empty( $_POST[$tax_name] ) ? explode( ',', $_POST[$tax_name] ) : array(); // Strip whitespace from the terms foreach ( $terms as $key => $term ) { $terms[$key] = trim( $term ); } $tax = get_taxonomy( $tax_name ); // Hierarchical terms like categories have to be handled differently, with // term IDs rather than the term names themselves if ( !empty( $tax->hierarchical ) ) { $term_ids = array(); foreach( $terms as $term ) { $parent = 0; $term_ids[] = term_exists( $term, $tax_id, $parent ); } } wp_set_post_terms( $query->doc_id, $terms, $tax_name ); } do_action( 'bp_docs_taxonomy_saved', $query ); } /** * Handles taxonomy cleanup when a post is deleted * * No longer needed, because item taxonomies are generated dynamically * * @since 1.0-beta * * @param int $doc_id */ function delete_post( $new_status, $old_status, $post ) { return; } /** * Shows a doc's taxonomy terms * * @since 1.0-beta */ function show_terms() { foreach ( $this->taxonomies as $tax_name ) { $html = ''; $tagtext = array(); $tags = wp_get_post_terms( get_the_ID(), $tax_name ); foreach ( $tags as $tag ) { $tagtext[] = bp_docs_get_tag_link( array( 'tag' => $tag->name ) ); } if ( ! empty( $tagtext ) ) { $html = '
' . sprintf( __( 'Tags: %s', 'bp-docs' ), implode( ', ', $tagtext ) ) . '
'; } echo apply_filters( 'bp_docs_taxonomy_show_terms', $html, $tagtext ); } } /** * Gets the list of terms used by an item's docs * * This is a dummy function that allows specific item types to hook in their own methods * for retrieving metadata (groups_update_groupmeta(), get_user_meta(), etc) * * @since 1.0-beta * * @return array $terms The item's terms */ function get_item_terms() { $terms = array(); return apply_filters( 'bp_docs_taxonomy_get_item_terms', $terms ); } /** * Save list of terms used by an item's docs * * Just a dummy hook for the moment, for the integration modules to hook into * * @since 1.0-beta * * @return array $terms The item's terms */ function save_item_terms( $terms ) { do_action( 'bp_docs_taxonomy_save_item_terms', $terms ); } /** * Modifies the tax_query on the doc loop to account for doc tags * * @since 1.0-beta * * @return array $terms The item's terms */ function modify_tax_query( $tax_query ) { // Check for the existence tag filters in the request URL if ( ! empty( $_REQUEST['bpd_tag'] ) ) { // The bpd_tag argument may be comma-separated $tags = explode( ',', urldecode( $_REQUEST['bpd_tag'] ) ); // Clean up the tag input foreach ( $tags as $key => $value ) { $tags[$key] = esc_attr( $value ); } $tax_query[] = array( 'taxonomy' => $this->docs_tag_tax_name, 'terms' => $tags, 'field' => 'slug', ); if ( !empty( $_REQUEST['bool'] ) && $_REQUEST['bool'] == 'and' ) { $tax_query['operator'] = 'AND'; } } return apply_filters( 'bp_docs_modify_tax_query_for_tax', $tax_query ); } /** * Markup for the Tags$taxonomy, 'hide_empty' => 0, 'name' => 'new'.$taxonomy.'_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => '— ' . $tax->labels->parent_item . ' —', 'tab_index' => 3 ) ); ?>