post_type_name ) )
return false;
$taxes = array(
'audience' => array(
'name' => _x( 'Audience', 'taxonomy general name', 'document-repository' ),
'singular_name' => _x( 'Audience', 'taxonomy singular name', 'document-repository' ),
'search_items' => __( 'Search Audiences', 'document-repository' ),
'all_items' => __( 'All Audiences', 'document-repository' ),
'parent_item' => __( 'Parent Audience', 'document-repository' ),
'parent_item_colon' => __( 'Parent Audience:', 'document-repository' ),
'edit_item' => __( 'Edit Audience', 'document-repository' ),
'update_item' => __( 'Update Audience', 'document-repository' ),
'add_new_item' => __( 'Add New Audience', 'document-repository' ),
'new_item_name' => __( 'New Audience Name', 'document-repository' )
),
'division' => array(
'name' => _x( 'Division', 'taxonomy general name', 'document-repository' ),
'singular_name' => _x( 'Division', 'taxonomy singular name', 'document-repository' ),
'search_items' => __( 'Search Divisions', 'document-repository' ),
'all_items' => __( 'All Divisions', 'document-repository' ),
'parent_item' => __( 'Parent Division', 'document-repository' ),
'parent_item_colon' => __( 'Parent Division:', 'document-repository' ),
'edit_item' => __( 'Edit Division', 'document-repository' ),
'update_item' => __( 'Update Division', 'document-repository' ),
'add_new_item' => __( 'Add New Division', 'document-repository' ),
'new_item_name' => __( 'New Division Name', 'document-repository' )
),
'process' => array(
'name' => _x( 'Business Process', 'taxonomy general name', 'document-repository' ),
'singular_name' => _x( 'Business Process', 'taxonomy singular name', 'document-repository' ),
'search_items' => __( 'Search Business Processes', 'document-repository' ),
'all_items' => __( 'All Business Processes', 'document-repository' ),
'parent_item' => __( 'Parent Business Process', 'document-repository' ),
'parent_item_colon' => __( 'Parent Business Process:', 'document-repository' ),
'edit_item' => __( 'Edit Business Process', 'document-repository' ),
'update_item' => __( 'Update Business Process', 'document-repository' ),
'add_new_item' => __( 'Add New Business Process', 'document-repository' ),
'new_item_name' => __( 'New Business Process Name', 'document-repository' )
)
);
foreach( $taxes as $tax => $labels ) {
register_taxonomy( $tax, array( $ra_document_library->post_type_name ), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => $tax )
));
}
add_action( 'document_search_widget', array( 'RA_Document_taxonomies', 'document_search_widget' ) );
add_action( 'save_post', array( 'RA_Document_taxonomies', 'save_post' ), 10, 2 );
add_filter( 'post_updated_messages', array( 'RA_Document_taxonomies', 'post_updated_messages' ), 12 );
add_filter( 'the_content', array( 'RA_Document_taxonomies', 'the_content' ), 9 );
add_filter( 'document_search_query_vars', array( 'RA_Document_taxonomies', 'document_search_query_vars' ) );
return true;
}
/*
require the document to have a term from all the custom taxonomies
*/
function save_post( $post_id, $post ) {
global $wpdb, $ra_document_library;
if( $post->post_status != 'publish' || $post->post_type != $ra_document_library->post_type_name )
return;
$has_terms = true;
foreach( array( 'audience', 'division', 'process' ) as $tax ) {
$terms = wp_get_object_terms( $post_id, $tax, array( 'fields' => 'names' ) );
if( empty( $terms ) ) {
$has_terms = false;
break;
}
}
if( !$has_terms ) {
$wpdb->update( $wpdb->posts, array( 'post_status' => 'draft' ), array( 'ID' => $post_id ) );
if( defined( 'DOING_AJAX' ) && DOING_AJAX )
wp_redirect( add_query_arg( array( 'post_type' => $this->post_type_name ), admin_url( 'edit.php' ) ) );
else
wp_redirect( add_query_arg( array( 'post' => $post_id, 'action' => 'edit', 'message' => 2 ), admin_url( 'post.php' ) ) );
exit;
}
}
/*
custom message for the requirement above
*/
function post_updated_messages( $messages ) {
global $ra_document_library;
if( !empty( $messages[$ra_document_library->post_type_name] ) )
$messages[$ra_document_library->post_type_name][2] = __( 'All published documents require an audience, division and business process.', 'document-repository' );
return $messages;
}
/*
add custom taxonomies to the search widget
*/
function document_search_widget() {
global $ra_document_library, $wp_query;
foreach( array( 'audience', 'division', 'process' ) as $tax ) {
$terms = get_terms( $tax );
if( empty( $terms ) )
continue;
$current = !empty( $wp_query->query_vars[$tax] ) ? $wp_query->query_vars[$tax] : '';
$taxonomy = get_taxonomy( $tax );
$output = "
';
}
}
/*
add custom taxonomies to the post content to eliminate need for custom templates
*/
function the_content( $content, $js_class = '' ) {
global $post, $ra_document_library;
if( $post->post_type != $ra_document_library->post_type_name )
return $content;
$term_content = '';
foreach( array( 'audience', 'division', 'process' ) as $tax ) {
$taxonomy = get_taxonomy( $tax );
$terms = get_the_terms( 0, $tax );
if( !empty( $terms ) ) {
$term_content .= '' . $taxonomy->labels->name . ': ';
if( !$ra_document_library->media_library ) {
$terms = get_the_term_list( 0, $tax, '', ',', ' ' );
if( !empty( $terms ) )
$term_content .= $terms;
continue;
}
$term_list = array();
foreach( $terms as $term )
$term_list[] = '' . esc_html( $term->name ) . '';
$term_content .= '' . implode( ', ', $term_list ) . '';
}
}
if( !empty( $term_content ) )
$content .= '