= 28 ) {
$error = sprintf( __( 'Slug “%s” is too long (28 characters max). Shorten it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) );
} elseif ( in_array( $attribute_name, $reserved_terms ) ) {
$error = sprintf( __( 'Slug “%s” is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) );
} else {
$taxonomy_exists = taxonomy_exists( wc_attribute_taxonomy_name( $attribute_name ) );
if ( 'add' === $action && $taxonomy_exists ) {
$error = sprintf( __( 'Slug “%s” is already in use. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) );
}
if ( 'edit' === $action ) {
$old_attribute_name = $wpdb->get_var( "SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = $attribute_id" );
if ( $old_attribute_name != $attribute_name && wc_sanitize_taxonomy_name( $old_attribute_name ) != $attribute_name && $taxonomy_exists ) {
$error = sprintf( __( 'Slug “%s” is already in use. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) );
}
}
}
// Show the error message if any
if ( ! empty( $error ) ) {
echo '
';
} else {
// Add new attribute
if ( 'add' === $action ) {
$attribute = array(
'attribute_label' => $attribute_label,
'attribute_name' => $attribute_name,
'attribute_type' => $attribute_type,
'attribute_orderby' => $attribute_orderby,
'attribute_public' => $attribute_public
);
$wpdb->insert( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute );
do_action( 'woocommerce_attribute_added', $wpdb->insert_id, $attribute );
$action_completed = true;
}
// Edit existing attribute
if ( 'edit' === $action ) {
$attribute = array(
'attribute_label' => $attribute_label,
'attribute_name' => $attribute_name,
'attribute_type' => $attribute_type,
'attribute_orderby' => $attribute_orderby,
'attribute_public' => $attribute_public
);
$wpdb->update( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute, array( 'attribute_id' => $attribute_id ) );
do_action( 'woocommerce_attribute_updated', $attribute_id, $attribute, $old_attribute_name );
if ( $old_attribute_name != $attribute_name && ! empty( $old_attribute_name ) ) {
// Update taxonomies in the wp term taxonomy table
$wpdb->update(
$wpdb->term_taxonomy,
array( 'taxonomy' => wc_attribute_taxonomy_name( $attribute_name ) ),
array( 'taxonomy' => 'pa_' . $old_attribute_name )
);
// Update taxonomy ordering term meta
$wpdb->update(
$wpdb->prefix . 'woocommerce_termmeta',
array( 'meta_key' => 'order_pa_' . sanitize_title( $attribute_name ) ),
array( 'meta_key' => 'order_pa_' . sanitize_title( $old_attribute_name ) )
);
// Update product attributes which use this taxonomy
$old_attribute_name_length = strlen( $old_attribute_name ) + 3;
$attribute_name_length = strlen( $attribute_name ) + 3;
$wpdb->query( "
UPDATE {$wpdb->postmeta}
SET meta_value = REPLACE( meta_value, 's:{$old_attribute_name_length}:\"pa_{$old_attribute_name}\"', 's:{$attribute_name_length}:\"pa_{$attribute_name}\"' )
WHERE meta_key = '_product_attributes'"
);
// Update variations which use this taxonomy
$wpdb->update(
$wpdb->postmeta,
array( 'meta_key' => 'attribute_pa_' . sanitize_title( $attribute_name ) ),
array( 'meta_key' => 'attribute_pa_' . sanitize_title( $old_attribute_name ) )
);
}
echo '' . __( 'Attribute updated successfully', 'woocommerce' ) . '
';
$action_completed = true;
}
flush_rewrite_rules();
}
}
// Delete an attribute
if ( 'delete' === $action ) {
// Security check
$attribute_id = absint( $_GET['delete'] );
check_admin_referer( 'woocommerce-delete-attribute_' . $attribute_id );
$attribute_name = $wpdb->get_var( "SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = $attribute_id" );
if ( $attribute_name && $wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = $attribute_id" ) ) {
$taxonomy = wc_attribute_taxonomy_name( $attribute_name );
if ( taxonomy_exists( $taxonomy ) ) {
$terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' );
foreach ( $terms as $term ) {
wp_delete_term( $term->term_id, $taxonomy );
}
}
do_action( 'woocommerce_attribute_deleted', $attribute_id, $attribute_name, $taxonomy );
$action_completed = true;
}
}
// If an attribute was added, edited or deleted: clear cache
if ( ! empty( $action_completed ) ) {
delete_transient( 'wc_attribute_taxonomies' );
}
// Show admin interface
if ( ! empty( $_GET['edit'] ) ) {
self::edit_attribute();
} else {
self::add_attribute();
}
}
/**
* Edit Attribute admin panel
*
* Shows the interface for changing an attributes type between select and text
*/
public static function edit_attribute() {
global $wpdb;
$edit = absint( $_GET['edit'] );
$attribute_to_edit = $wpdb->get_row( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_id = '$edit'" );
$att_type = $attribute_to_edit->attribute_type;
$att_label = $attribute_to_edit->attribute_label;
$att_name = $attribute_to_edit->attribute_name;
$att_orderby = $attribute_to_edit->attribute_orderby;
$att_public = $attribute_to_edit->attribute_public;
?>
|
|
|
|
|
attribute_label ); ?>
|
attribute_name ); ?> |
attribute_type ) ); ?> attribute_public ? '(' . __( 'Public', 'woocommerce' ) . ')' : ''; ?> |
attribute_orderby ) {
case 'name' :
_e( 'Name', 'woocommerce' );
break;
case 'name_num' :
_e( 'Name (numeric)', 'woocommerce' );
break;
case 'id' :
_e( 'Term ID', 'woocommerce' );
break;
default:
_e( 'Custom ordering', 'woocommerce' );
break;
}
?> |
attribute_name );
if ( taxonomy_exists( $taxonomy ) ) {
$terms = get_terms( $taxonomy, 'hide_empty=0' );
switch ( $tax->attribute_orderby ) {
case 'name_num' :
usort( $terms, '_wc_get_product_terms_name_num_usort_callback' );
break;
case 'parent' :
usort( $terms, '_wc_get_product_terms_parent_usort_callback' );
break;
}
$terms_string = implode( ', ', wp_list_pluck( $terms, 'name' ) );
if ( $terms_string ) {
echo $terms_string;
} else {
echo '–';
}
} else {
echo '–';
}
?> |
|
|