2011-09-16 08:10:12 +00:00
< ? php
/**
* Functions used for taxonomies in admin
*
* These functions control admin interface bits like category ordering .
*
* @ author WooThemes
* @ category Admin
* @ package WooCommerce
*/
2011-10-07 19:27:10 +00:00
2011-09-16 08:10:12 +00:00
/**
2011-10-07 19:27:10 +00:00
* Category thumbnails
2011-09-16 08:10:12 +00:00
*/
2011-10-07 19:27:10 +00:00
add_action ( 'product_cat_add_form_fields' , 'woocommerce_add_category_thumbnail_field' );
add_action ( 'product_cat_edit_form_fields' , 'woocommerce_edit_category_thumbnail_field' , 10 , 2 );
function woocommerce_add_category_thumbnail_field () {
global $woocommerce ;
?>
< div class = " form-field " >
< label >< ? php _e ( 'Thumbnail' , 'woothemes' ); ?> </label>
< div id = " product_cat_thumbnail " style = " float:left;margin-right:10px; " >< img src = " <?php echo $woocommerce->plugin_url ().'/assets/images/placeholder.png' ?> " width = " 60px " height = " 60px " /></ div >
< div style = " line-height:60px; " >
< input type = " hidden " id = " product_cat_thumbnail_id " name = " product_cat_thumbnail_id " />
< button type = " submit " class = " upload_image_button button " >< ? php _e ( 'Upload/Add image' , 'woothemes' ); ?> </button>
< button type = " submit " class = " remove_image_button button " >< ? php _e ( 'Remove image' , 'woothemes' ); ?> </button>
</ div >
< script type = " text/javascript " >
window . send_to_termmeta = function ( html ) {
2011-10-26 19:39:03 +00:00
jQuery ( 'body' ) . append ( '<div id="temp_image">' + html + '</div>' );
var img = jQuery ( '#temp_image' ) . find ( 'img' );
imgurl = img . attr ( 'src' );
imgclass = img . attr ( 'class' );
imgid = parseInt ( imgclass . replace ( / \D / g , '' ), 10 );
2011-10-07 19:27:10 +00:00
jQuery ( '#product_cat_thumbnail_id' ) . val ( imgid );
jQuery ( '#product_cat_thumbnail img' ) . attr ( 'src' , imgurl );
2011-10-26 19:39:03 +00:00
jQuery ( '#temp_image' ) . remove ();
2011-10-07 19:27:10 +00:00
tb_remove ();
}
jQuery ( '.upload_image_button' ) . live ( 'click' , function (){
var post_id = 0 ;
window . send_to_editor = window . send_to_termmeta ;
tb_show ( '' , 'media-upload.php?post_id=' + post_id + '&type=image&TB_iframe=true' );
return false ;
});
jQuery ( '.remove_image_button' ) . live ( 'click' , function (){
jQuery ( '#product_cat_thumbnail img' ) . attr ( 'src' , '<?php echo $woocommerce->plugin_url().' / assets / images / placeholder . png '; ?>' );
jQuery ( '#product_cat_thumbnail_id' ) . val ( '' );
return false ;
});
</ script >
< div class = " clear " ></ div >
</ div >
< ? php
}
function woocommerce_edit_category_thumbnail_field ( $term , $taxonomy ) {
global $woocommerce ;
$image = '' ;
$thumbnail_id = get_woocommerce_term_meta ( $term -> term_id , 'thumbnail_id' , true );
if ( $thumbnail_id ) :
$image = wp_get_attachment_url ( $thumbnail_id );
else :
$image = $woocommerce -> plugin_url () . '/assets/images/placeholder.png' ;
endif ;
?>
< tr class = " form-field " >
< th scope = " row " valign = " top " >< label >< ? php _e ( 'Thumbnail' , 'woothemes' ); ?> </label></th>
< td >
< div id = " product_cat_thumbnail " style = " float:left;margin-right:10px; " >< img src = " <?php echo $image ; ?> " width = " 60px " height = " 60px " /></ div >
< div style = " line-height:60px; " >
< input type = " hidden " id = " product_cat_thumbnail_id " name = " product_cat_thumbnail_id " value = " <?php echo $thumbnail_id ; ?> " />
< button type = " submit " class = " upload_image_button button " >< ? php _e ( 'Upload/Add image' , 'woothemes' ); ?> </button>
< button type = " submit " class = " remove_image_button button " >< ? php _e ( 'Remove image' , 'woothemes' ); ?> </button>
</ div >
< script type = " text/javascript " >
window . send_to_termmeta = function ( html ) {
2011-10-28 08:56:19 +00:00
jQuery ( 'body' ) . append ( '<div id="temp_image">' + html + '</div>' );
var img = jQuery ( '#temp_image' ) . find ( 'img' );
imgurl = img . attr ( 'src' );
imgclass = img . attr ( 'class' );
imgid = parseInt ( imgclass . replace ( / \D / g , '' ), 10 );
2011-10-07 19:27:10 +00:00
jQuery ( '#product_cat_thumbnail_id' ) . val ( imgid );
jQuery ( '#product_cat_thumbnail img' ) . attr ( 'src' , imgurl );
2011-10-28 08:56:19 +00:00
jQuery ( '#temp_image' ) . remove ();
2011-10-07 19:27:10 +00:00
tb_remove ();
}
jQuery ( '.upload_image_button' ) . live ( 'click' , function (){
var post_id = 0 ;
window . send_to_editor = window . send_to_termmeta ;
tb_show ( '' , 'media-upload.php?post_id=' + post_id + '&type=image&TB_iframe=true' );
return false ;
});
jQuery ( '.remove_image_button' ) . live ( 'click' , function (){
jQuery ( '#product_cat_thumbnail img' ) . attr ( 'src' , '<?php echo $woocommerce->plugin_url().' / assets / images / placeholder . png '; ?>' );
jQuery ( '#product_cat_thumbnail_id' ) . val ( '' );
return false ;
});
</ script >
< div class = " clear " ></ div >
</ td >
</ tr >
< ? php
}
add_action ( 'created_term' , 'woocommerce_category_thumbnail_field_save' , 10 , 3 );
add_action ( 'edit_term' , 'woocommerce_category_thumbnail_field_save' , 10 , 3 );
2011-09-16 08:10:12 +00:00
2011-10-07 19:27:10 +00:00
function woocommerce_category_thumbnail_field_save ( $term_id , $tt_id , $taxonomy ) {
if ( isset ( $_POST [ 'product_cat_thumbnail_id' ])) {
update_woocommerce_term_meta ( $term_id , 'thumbnail_id' , $_POST [ 'product_cat_thumbnail_id' ]);
}
}
/**
2011-10-12 17:32:30 +00:00
* Category / Term ordering
2011-10-07 19:27:10 +00:00
*/
2011-09-16 08:10:12 +00:00
/**
2011-10-12 17:32:30 +00:00
* Reorder on term insertion
2011-09-16 08:10:12 +00:00
*/
2011-10-13 12:25:24 +00:00
add_action ( " create_term " , 'woocommerce_create_term' , 5 , 3 );
2011-09-16 08:10:12 +00:00
2011-10-12 17:32:30 +00:00
function woocommerce_create_term ( $term_id , $tt_id , $taxonomy ) {
2011-09-16 08:10:12 +00:00
2011-10-13 12:25:24 +00:00
if ( ! $taxonomy == 'product_cat' && ! strstr ( $taxonomy , 'pa_' )) return ;
2011-09-16 08:10:12 +00:00
$next_id = null ;
2011-10-12 17:32:30 +00:00
$term = get_term ( $term_id , $taxonomy );
2011-09-16 08:10:12 +00:00
// gets the sibling terms
2011-10-12 17:32:30 +00:00
$siblings = get_terms ( $taxonomy , " parent= { $term -> parent } &menu_order=ASC&hide_empty=0 " );
2011-09-16 08:10:12 +00:00
foreach ( $siblings as $sibling ) {
if ( $sibling -> term_id == $term_id ) continue ;
2011-10-12 17:32:30 +00:00
$next_id = $sibling -> term_id ; // first sibling term of the hierarchy level
2011-09-16 08:10:12 +00:00
break ;
}
// reorder
2011-10-12 17:32:30 +00:00
woocommerce_order_terms ( $term , $next_id , $taxonomy );
2011-09-16 08:10:12 +00:00
}
/**
* Delete terms metas on deletion
*/
2011-10-13 12:25:24 +00:00
add_action ( " delete_product_term " , 'woocommerce_delete_term' , 5 , 3 );
2011-09-16 08:10:12 +00:00
2011-10-12 17:32:30 +00:00
function woocommerce_delete_term ( $term_id , $tt_id , $taxonomy ) {
2011-09-16 08:10:12 +00:00
$term_id = ( int ) $term_id ;
if ( ! $term_id ) return ;
global $wpdb ;
$wpdb -> query ( " DELETE FROM { $wpdb -> woocommerce_termmeta } WHERE `woocommerce_term_id` = " . $term_id );
}
/**
2011-10-12 17:32:30 +00:00
* Move a term before the a given element of its hierarchy level
2011-09-16 08:10:12 +00:00
*
* @ param object $the_term
* @ param int $next_id the id of the next slibling element in save hierachy level
* @ param int $index
* @ param int $terms
*/
2011-10-12 17:32:30 +00:00
function woocommerce_order_terms ( $the_term , $next_id , $taxonomy , $index = 0 , $terms = null ) {
2011-09-16 08:10:12 +00:00
2011-10-12 17:32:30 +00:00
if ( ! $terms ) $terms = get_terms ( $taxonomy , 'menu_order=ASC&hide_empty=0&parent=0' );
2011-09-16 08:10:12 +00:00
if ( empty ( $terms ) ) return $index ;
$id = $the_term -> term_id ;
$term_in_level = false ; // flag: is our term to order in this level of terms
foreach ( $terms as $term ) {
if ( $term -> term_id == $id ) { // our term to order, we skip
$term_in_level = true ;
continue ; // our term to order, we skip
}
// the nextid of our term to order, lets move our term here
if ( null !== $next_id && $term -> term_id == $next_id ) {
$index ++ ;
2011-10-12 17:32:30 +00:00
$index = woocommerce_set_term_order ( $id , $index , $taxonomy , true );
2011-09-16 08:10:12 +00:00
}
// set order
$index ++ ;
2011-10-12 17:32:30 +00:00
$index = woocommerce_set_term_order ( $term -> term_id , $index , $taxonomy );
2011-09-16 08:10:12 +00:00
// if that term has children we walk through them
2011-10-12 17:32:30 +00:00
$children = get_terms ( $taxonomy , " parent= { $term -> term_id } &menu_order=ASC&hide_empty=0 " );
2011-09-16 08:10:12 +00:00
if ( ! empty ( $children ) ) {
2011-10-12 17:32:30 +00:00
$index = woocommerce_order_terms ( $the_term , $next_id , $taxonomy , $index , $children );
2011-09-16 08:10:12 +00:00
}
}
// no nextid meaning our term is in last position
if ( $term_in_level && null === $next_id )
2011-10-12 17:32:30 +00:00
$index = woocommerce_set_term_order ( $id , $index + 1 , $taxonomy , true );
2011-09-16 08:10:12 +00:00
return $index ;
}
/**
2011-10-12 17:32:30 +00:00
* Set the sort order of a term
2011-09-16 08:10:12 +00:00
*
* @ param int $term_id
* @ param int $index
* @ param bool $recursive
*/
2011-10-12 17:32:30 +00:00
function woocommerce_set_term_order ( $term_id , $index , $taxonomy , $recursive = false ) {
2011-09-16 08:10:12 +00:00
global $wpdb ;
$term_id = ( int ) $term_id ;
$index = ( int ) $index ;
2011-11-22 16:55:59 +00:00
// Meta name
if ( strstr ( $taxonomy , 'pa_' )) :
$meta_name = 'order_' . esc_attr ( $taxonomy );
else :
$meta_name = 'order' ;
endif ;
update_woocommerce_term_meta ( $term_id , $meta_name , $index );
2011-09-16 08:10:12 +00:00
if ( ! $recursive ) return $index ;
2011-10-12 17:32:30 +00:00
$children = get_terms ( $taxonomy , " parent= $term_id &menu_order=ASC&hide_empty=0 " );
2011-09-16 08:10:12 +00:00
foreach ( $children as $term ) {
$index ++ ;
2011-10-12 17:32:30 +00:00
$index = woocommerce_set_term_order ( $term -> term_id , $index , $taxonomy , true );
2011-09-16 08:10:12 +00:00
}
return $index ;
2011-12-02 18:54:52 +00:00
}
/**
* Description to shipping class page
*/
add_action ( 'product_shipping_class_pre_add_form' , 'woocommerce_shipping_class_description' );
2011-09-16 08:10:12 +00:00
2011-12-02 18:54:52 +00:00
function woocommerce_shipping_class_description () {
echo wpautop ( __ ( 'Shipping classes can be used to group products of similar type. These groups can then be used by certain shipping methods to provide different rates to different products.' , 'woothemes' ));
}