'parent', 'id' => 'term_id' ); //TODO: decouple this function start_lvl( &$output, $depth, $args ) { $indent = str_repeat( "\t", $depth ); $output .= "$indent\n"; } function start_el( &$output, $category, $depth, $args ) { extract( $args ); if ( empty( $taxonomy ) ) $taxonomy = 'category'; if ( $taxonomy == 'category' ) $name = 'post_category'; else $name = 'tax_input['.$taxonomy.']'; $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : ''; $output .= "\n
  • " . ''; } function end_el( &$output, $category, $depth, $args ) { $output .= "
  • \n"; } } class AttachmentTaxSupp_Admin { /** * Configure Admin */ function AttachmentTaxSupp_Admin() { add_action( 'admin_menu', array( $this, 'add_media_taxonomy_menus' ) ); add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), null, 2 ); add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), null, 2 ); add_filter( 'get_edit_term_link', array( $this, 'get_edit_term_link' ), 10, 4 ); add_action( 'admin_head', array( $this, 'admin_head_edit_tags' ) ); add_filter( 'admin_body_class', array( $this, 'admin_body_class' ), 20 ); add_filter( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 20 ); } /** * Admin Enqueue Scripts */ function admin_enqueue_scripts() { global $AttachmentTaxSupp, $current_screen; if ( ( $current_screen->id == 'media' ) || ( isset( $_GET['taxonomy'] ) && isset( $_GET['post_type'] ) && 'attachment' == $_GET['post_type'] ) ) { wp_enqueue_script( 'media_taxonomies', plugins_url( dirname( $AttachmentTaxSupp->plugin_basename ) . '/admin/js/admin.js' ), array( 'jquery', 'suggest', 'post' ) ); } } /** * Admin Body Classes * Add a 'attachmenttaxsupp' class to make it easy to hook using jQuery. * * @param string $classes Classes. * @return string Classes. */ function admin_body_class( $classes ) { global $current_screen; // This doesn't work in earlier WordPress 3.x branches //if ( 'edit-tags' == $current_screen->base && 'attachment' == $current_screen->post_type ) { if ( 'edit-tags' == $current_screen->base && 'attachment' == $_REQUEST['post_type'] ) { $classes .= ' attachmenttaxsupp'; } return $classes; } /** * Admin Head - Edit Tags * Add a JavaScript variable so that we can check which menus * to show/hide with jQuery. */ function admin_head_edit_tags() { global $current_screen; // This doesn't work in earlier WordPress 3.x branches //if ( 'edit-tags' == $current_screen->base && 'attachment' == $current_screen->post_type ) { if ( 'edit-tags' == $current_screen->base && isset( $_REQUEST['post_type'] ) && 'attachment' == $_REQUEST['post_type'] ) { ?> object_type ) ) { if ( isset( $_GET['post_type'] ) && 'attachment' == $_GET['post_type'] && in_array( 'attachment', $tax->object_type ) ) { $object_type = 'attachment'; } else { $object_type = $tax->object_type[0]; } return add_query_arg( 'post_type', $object_type, $location ); } return $location; } /** * Add Media Taxonomy Menus * * @todo Check if taxonomy has UI etc. */ function add_media_taxonomy_menus() { foreach ( get_taxonomies( array( 'object_type' => array( 'attachment' ) ), 'objects' ) as $tax ) { // Need to check object type in early WordPress 3.x branches if ( in_array( 'attachment', $tax->object_type ) ) { add_submenu_page( 'upload.php', $tax->label, $tax->labels->menu_name, 'edit_posts', 'edit-tags.php?taxonomy=' . $tax->name . '&post_type=attachment' ); } } } /** * Media Taxonomy Page * This is just a plceholder function * as we actually redirect here instead. */ function media_taxonomy_edit_page() { } /** * Edit Image Form * * @param array $form_fields Form fields array. * @param object $post Post. * @return array Form fields. */ function attachment_fields_to_edit( $form_fields, $post ) { global $AttachmentTaxSupp, $current_screen; foreach ( $form_fields as $key => $val ) { if ( isset( $val['hierarchical'] ) && taxonomy_exists( $val['name'] ) ) { $tax_name = esc_attr( $val['name'] ); $taxonomy = get_taxonomy( $val['name'] ); if ( $val['hierarchical'] == true) { //$popular_ids = wp_popular_terms_checklist( $tax_name ); ob_start(); //wp_terms_checklist( $post->ID, array( 'taxonomy' => $tax_name, 'popular_cats' => $popular_ids, 'walker' => new AttachmentTaxSupp_Walker_Category_Checklist ) ); $wp_terms_checklist_walker = new AttachmentTaxSupp_Walker_Category_Checklist; $wp_terms_checklist_walker->attachment_id = $post->ID; wp_terms_checklist( $post->ID, array( 'taxonomy' => $tax_name, 'walker' => $wp_terms_checklist_walker ) ); $checklist = ob_get_contents(); ob_end_clean(); $html = '
      ' . $checklist . '
    '; /* if ( current_user_can( $taxonomy->cap->edit_terms ) ) : $html .= '

    + Add New Category

    '; endif; */ $html .= '

    ' . __( 'Manage', 'attachmenttaxsupp' ) . ' ' . $taxonomy->labels->name . '

    '; $html .= wp_nonce_field( 'update_attachment', '_wpnonce_attachmenttaxsupp', true, false ); $form_fields[$key]['input'] = 'html'; $form_fields[$key]['html'] = $html; } else { $disabled = !current_user_can( $taxonomy->cap->assign_terms ) ? ' disabled="disabled"' : ''; $html = '

    ' . $taxonomy->labels->add_or_remove_items . '

    '; if ( current_user_can( $taxonomy->cap->assign_terms ) ) : $html .= '
    ' . $taxonomy->labels->add_new_item . '

    ' . esc_attr( $taxonomy->labels->separate_items_with_commas ) . '

    '; endif; $html .= '
    '; if ( current_user_can( $taxonomy->cap->assign_terms ) ) : $html .= '

    ' . $taxonomy->labels->choose_from_most_used . '

    '; endif; $html .= '
    '; $html .= '

    ' . __( 'Manage', 'attachmenttaxsupp' ) . ' ' . $taxonomy->labels->name . '

    '; $html .= wp_nonce_field( 'update_attachment', '_wpnonce_attachmenttaxsupp', true, false ); if ( 'async-upload' == $current_screen->id ) { $html .= ''; } $form_fields[$key]['input'] = 'html'; $form_fields[$key]['html'] = $html; } } } return $form_fields; } /** * Save Image Form * * @todo If some checkboxes are checked and you uncheck all on a media page, it doesn't save * * @param object $post Post. * @param object $attachment Attchment. * @return object Post. */ function attachment_fields_to_save( $post, $attachment ) { if ( empty( $_POST ) || !wp_verify_nonce( $_POST['_wpnonce_attachmenttaxsupp'], 'update_attachment' ) ) return $post; if ( isset( $_POST['tax_input'] ) && is_array( $_POST['tax_input'] ) ) { foreach ( $_POST['tax_input'] as $tax => $arr ) { if ( taxonomy_exists( $tax ) ) { $val = null; if ( isset( $arr[$post['ID']] ) ) $val = array_map( 'absint', $arr[$post['ID']] ); wp_set_object_terms( $post['ID'], $val, $tax ); } } } else { $taxes = get_taxonomies( array( 'show_ui' => true, '_builtin' => false ) ); foreach ( $taxes as $tax ) { wp_set_object_terms( $post['ID'], array(), $tax ); } } return $post; } } ?>