Merge branch '4-4-term-meta-handling'

This commit is contained in:
Mike Jolley 2016-03-04 10:55:53 +00:00
commit 22dc056e16
7 changed files with 118 additions and 57 deletions

View File

@ -78,7 +78,7 @@ class WC_Admin_Taxonomies {
$term_id = absint( $term_id );
if ( $term_id ) {
if ( $term_id && get_option( 'db_version' ) < 34370 ) {
$wpdb->delete( $wpdb->woocommerce_termmeta, array( 'woocommerce_term_id' => $term_id ), array( '%d' ) );
}
}

View File

@ -319,7 +319,7 @@ class WC_Report_Sales_By_Category extends WC_Admin_Report {
$chart_data[ $category->term_id ]['category'] = $category->name;
$chart_data[ $category->term_id ]['data'] = $category_chart_data;
$index ++;
$index++;
}
?>
<div class="chart-container">

View File

@ -808,7 +808,9 @@ class WC_API_Products extends WC_API_Resource {
}
// When a term is deleted, delete its meta.
$wpdb->delete( $wpdb->woocommerce_termmeta, array( 'woocommerce_term_id' => $id ), array( '%d' ) );
if ( get_option( 'db_version' ) < 34370 ) {
$wpdb->delete( $wpdb->woocommerce_termmeta, array( 'woocommerce_term_id' => $id ), array( '%d' ) );
}
do_action( 'woocommerce_api_delete_product_category', $id, $this );

View File

@ -369,7 +369,7 @@ class WC_Install {
*/
$max_index_length = 191;
return "
$tables = "
CREATE TABLE {$wpdb->prefix}woocommerce_sessions (
session_id bigint(20) NOT NULL AUTO_INCREMENT,
session_key char(32) NOT NULL,
@ -402,15 +402,6 @@ CREATE TABLE {$wpdb->prefix}woocommerce_attribute_taxonomies (
PRIMARY KEY (attribute_id),
KEY attribute_name (attribute_name($max_index_length))
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
meta_id bigint(20) NOT NULL auto_increment,
woocommerce_term_id bigint(20) NOT NULL,
meta_key varchar(255) default NULL,
meta_value longtext NULL,
PRIMARY KEY (meta_id),
KEY woocommerce_term_id (woocommerce_term_id),
KEY meta_key (meta_key($max_index_length))
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_downloadable_product_permissions (
permission_id bigint(20) NOT NULL auto_increment,
download_id varchar(32) NOT NULL,
@ -495,6 +486,23 @@ CREATE TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods (
PRIMARY KEY (instance_id)
) $collate;
";
// Term meta is only needed for old installs.
if ( ! function_exists( 'get_term_meta' ) ) {
$tables .= "
CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
meta_id bigint(20) NOT NULL auto_increment,
woocommerce_term_id bigint(20) NOT NULL,
meta_key varchar(255) default NULL,
meta_value longtext NULL,
PRIMARY KEY (meta_id),
KEY woocommerce_term_id (woocommerce_term_id),
KEY meta_key (meta_key($max_index_length))
) $collate;
";
}
return $tables;
}
/**

View File

@ -14,13 +14,22 @@ if ( ! defined( 'ABSPATH' ) ) {
global $wpdb;
/**
* Migrate term meta to WordPress tables
*/
if ( get_option( 'db_version' ) >= 34370 && $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_termmeta';" ) ) {
if ( $wpdb->query( "INSERT INTO {$wpdb->termmeta} ( term_id, meta_key, meta_value ) SELECT woocommerce_term_id, meta_key, meta_value FROM {$wpdb->prefix}woocommerce_termmeta;" ) ) {
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_termmeta" );
}
}
/**
* Old (table rate) shipping zones to new core shipping zones migration.
* zone_enabled and zone_type are no longer used, but it's safe to leave them be.
*/
if ( $wpdb->get_var( "SHOW COLUMNS FROM `{$wpdb->prefix}woocommerce_shipping_zones` LIKE 'zone_enabled';" ) ) {
$wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zones CHANGE `zone_type` `zone_type` VARCHAR(40) NOT NULL DEFAULT '';" );
$wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zones CHANGE `zone_enabled` `zone_enabled` INT(1) NOT NULL DEFAULT 1;" );
$wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zones CHANGE `zone_type` `zone_type` VARCHAR(40) NOT NULL DEFAULT '';" );
$wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zones CHANGE `zone_enabled` `zone_enabled` INT(1) NOT NULL DEFAULT 1;" );
}
/**

View File

@ -804,7 +804,7 @@ function wc_array_cartesian( $input ) {
// Generate indexes from keys and values so we have a logical sort order
foreach ( $input as $key => $values ) {
foreach ( $values as $value ) {
$indexes[ $key ][ $value ] = $index ++;
$indexes[ $key ][ $value ] = $index++;
}
}

View File

@ -223,20 +223,22 @@ add_action( 'switch_blog', 'wc_taxonomy_metadata_wpdbfix', 0 );
function wc_taxonomy_metadata_update_content_for_split_terms( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
global $wpdb;
if ( 'product_cat' === $taxonomy || taxonomy_is_product_attribute( $taxonomy ) ) {
$old_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_termmeta WHERE woocommerce_term_id = %d;", $old_term_id ) );
if ( get_option( 'db_version' ) < 34370 ) {
if ( 'product_cat' === $taxonomy || taxonomy_is_product_attribute( $taxonomy ) ) {
$old_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_termmeta WHERE woocommerce_term_id = %d;", $old_term_id ) );
// Copy across to split term
if ( $old_meta_data ) {
foreach ( $old_meta_data as $meta_data ) {
$wpdb->insert(
"{$wpdb->prefix}woocommerce_termmeta",
array(
'woocommerce_term_id' => $new_term_id,
'meta_key' => $meta_data->meta_key,
'meta_value' => $meta_data->meta_value
)
);
// Copy across to split term
if ( $old_meta_data ) {
foreach ( $old_meta_data as $meta_data ) {
$wpdb->insert(
"{$wpdb->prefix}woocommerce_termmeta",
array(
'woocommerce_term_id' => $new_term_id,
'meta_key' => $meta_data->meta_key,
'meta_value' => $meta_data->meta_value
)
);
}
}
}
}
@ -244,7 +246,31 @@ function wc_taxonomy_metadata_update_content_for_split_terms( $old_term_id, $new
add_action( 'split_shared_term', 'wc_taxonomy_metadata_update_content_for_split_terms', 10, 4 );
/**
* WooCommerce Term Meta API - Update term meta.
* Migrate data from WC term meta to WP term meta
*
* When the database is updated to support term meta, migrate WC term meta data across.
* We do this when the new version is >= 34370, and the old version is < 34370 (34370 is when term meta table was added).
*
* @param string $wp_db_version The new $wp_db_version.
* @param string $wp_current_db_version The old (current) $wp_db_version.
*/
function wc_taxonomy_metadata_migrate_data( $wp_db_version, $wp_current_db_version ) {
if ( $wp_db_version >= 34370 && $wp_current_db_version < 34370 ) {
global $wpdb;
if ( $wpdb->query( "INSERT INTO {$wpdb->termmeta} ( term_id, meta_key, meta_value ) SELECT woocommerce_term_id, meta_key, meta_value FROM {$wpdb->prefix}woocommerce_termmeta;" ) ) {
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_termmeta" );
}
}
}
add_action( 'wp_upgrade', 'wc_taxonomy_metadata_migrate_data', 10, 2 );
/**
* WooCommerce Term Meta API
*
* WC tables for storing term meta are @deprecated from WordPress 4.4 since 4.4 has its own table.
* This function serves as a wrapper, using the new table if present, or falling back to the WC table.
*
* @todo These functions should be deprecated with notices in a future WC version, allowing users a chance to upgrade WordPress.
*
* @param mixed $term_id
* @param string $meta_key
@ -253,12 +279,16 @@ add_action( 'split_shared_term', 'wc_taxonomy_metadata_update_content_for_split_
* @return bool
*/
function update_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $prev_value );
return function_exists( 'update_term_meta' ) ? update_term_meta( $term_id, $meta_key, $meta_value, $prev_value ) : update_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $prev_value );
}
/**
* WooCommerce Term Meta API - Add term meta.
* WooCommerce Term Meta API
*
* WC tables for storing term meta are @deprecated from WordPress 4.4 since 4.4 has its own table.
* This function serves as a wrapper, using the new table if present, or falling back to the WC table.
*
* @todo These functions should be deprecated with notices in a future WC version, allowing users a chance to upgrade WordPress.
* @param mixed $term_id
* @param mixed $meta_key
* @param mixed $meta_value
@ -266,32 +296,40 @@ function update_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $prev_v
* @return bool
*/
function add_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $unique = false ){
return add_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $unique );
return function_exists( 'add_term_meta' ) ? add_term_meta( $term_id, $meta_key, $meta_value, $unique ) : add_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $unique );
}
/**
* WooCommerce Term Meta API - Delete term meta.
* WooCommerce Term Meta API
*
* WC tables for storing term meta are @deprecated from WordPress 4.4 since 4.4 has its own table.
* This function serves as a wrapper, using the new table if present, or falling back to the WC table.
*
* @todo These functions should be deprecated with notices in a future WC version, allowing users a chance to upgrade WordPress.
* @param mixed $term_id
* @param mixed $meta_key
* @param string $meta_value (default: '')
* @param bool $delete_all (default: false)
* @param bool $deprecated (default: false)
* @return bool
*/
function delete_woocommerce_term_meta( $term_id, $meta_key, $meta_value = '', $delete_all = false ) {
return delete_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $delete_all );
function delete_woocommerce_term_meta( $term_id, $meta_key, $meta_value = '', $deprecated = false ) {
return function_exists( 'delete_term_meta' ) ? delete_term_meta( $term_id, $meta_key, $meta_value ) : delete_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value );
}
/**
* WooCommerce Term Meta API - Get term meta.
* WooCommerce Term Meta API
*
* WC tables for storing term meta are @deprecated from WordPress 4.4 since 4.4 has its own table.
* This function serves as a wrapper, using the new table if present, or falling back to the WC table.
*
* @todo These functions should be deprecated with notices in a future WC version, allowing users a chance to upgrade WordPress.
* @param mixed $term_id
* @param string $key
* @param bool $single (default: true)
* @return mixed
*/
function get_woocommerce_term_meta( $term_id, $key, $single = true ) {
return get_metadata( 'woocommerce_term', $term_id, $key, $single );
return function_exists( 'get_term_meta' ) ? get_term_meta( $term_id, $key, $single ) : get_metadata( 'woocommerce_term', $term_id, $key, $single );
}
/**
@ -305,22 +343,21 @@ function get_woocommerce_term_meta( $term_id, $key, $single = true ) {
* @return int
*/
function wc_reorder_terms( $the_term, $next_id, $taxonomy, $index = 0, $terms = null ) {
if( ! $terms ) $terms = get_terms($taxonomy, 'menu_order=ASC&hide_empty=0&parent=0' );
if( empty( $terms ) ) return $index;
if ( ! $terms ) $terms = get_terms( $taxonomy, 'menu_order=ASC&hide_empty=0&parent=0' );
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) {
foreach ( $terms as $term ) {
if( $term->term_id == $id ) { // our term to order, we skip
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) {
if (null !== $next_id && $term->term_id == $next_id) {
$index++;
$index = wc_set_term_order($id, $index, $taxonomy, true);
}
@ -331,14 +368,15 @@ function wc_reorder_terms( $the_term, $next_id, $taxonomy, $index = 0, $terms =
// if that term has children we walk through them
$children = get_terms($taxonomy, "parent={$term->term_id}&menu_order=ASC&hide_empty=0");
if( !empty($children) ) {
if ( ! empty( $children ) ) {
$index = wc_reorder_terms( $the_term, $next_id, $taxonomy, $index, $children );
}
}
// no nextid meaning our term is in last position
if( $term_in_level && null === $next_id )
$index = wc_set_term_order($id, $index+1, $taxonomy, true);
if ( $term_in_level && null === $next_id ) {
$index = wc_set_term_order( $id, $index + 1, $taxonomy, true );
}
return $index;
}
@ -370,7 +408,7 @@ function wc_set_term_order( $term_id, $index, $taxonomy, $recursive = false ) {
$children = get_terms($taxonomy, "parent=$term_id&menu_order=ASC&hide_empty=0");
foreach ( $children as $term ) {
$index ++;
$index++;
$index = wc_set_term_order($term->term_id, $index, $taxonomy, true);
}
@ -424,7 +462,7 @@ function wc_terms_clauses( $clauses, $taxonomies, $args ) {
// Meta name
if ( ! empty( $taxonomies[0] ) && taxonomy_is_product_attribute( $taxonomies[0] ) ) {
$meta_name = 'order_' . esc_attr( $taxonomies[0] );
$meta_name = 'order_' . esc_attr( $taxonomies[0] );
} else {
$meta_name = 'order';
}
@ -434,21 +472,25 @@ function wc_terms_clauses( $clauses, $taxonomies, $args ) {
$clauses['fields'] .= ', tm.* ';
}
//query join
$clauses['join'] .= " LEFT JOIN {$wpdb->woocommerce_termmeta} AS tm ON (t.term_id = tm.woocommerce_term_id AND tm.meta_key = '". $meta_name ."') ";
// query join
if ( get_option( 'db_version' ) < 34370 ) {
$clauses['join'] .= " LEFT JOIN {$wpdb->woocommerce_termmeta} AS tm ON (t.term_id = tm.woocommerce_term_id AND tm.meta_key = '" . esc_sql( $meta_name ) . "') ";
} else {
$clauses['join'] .= " LEFT JOIN {$wpdb->termmeta} AS tm ON (t.term_id = tm.term_id AND tm.meta_key = '" . esc_sql( $meta_name ) . "') ";
}
// default to ASC
if ( ! isset( $args['menu_order'] ) || ! in_array( strtoupper($args['menu_order']), array('ASC', 'DESC')) ) {
if ( ! isset( $args['menu_order'] ) || ! in_array( strtoupper($args['menu_order']), array( 'ASC', 'DESC' ) ) ) {
$args['menu_order'] = 'ASC';
}
$order = "ORDER BY tm.meta_value+0 " . $args['menu_order'];
if ( $clauses['orderby'] ):
$clauses['orderby'] = str_replace('ORDER BY', $order . ',', $clauses['orderby'] );
else:
if ( $clauses['orderby'] ) {
$clauses['orderby'] = str_replace( 'ORDER BY', $order . ',', $clauses['orderby'] );
} else {
$clauses['orderby'] = $order;
endif;
}
return $clauses;
}