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 ); $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' ) ); $wpdb->delete( $wpdb->woocommerce_termmeta, array( 'woocommerce_term_id' => $term_id ), array( '%d' ) );
} }
} }

View File

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

View File

@ -369,7 +369,7 @@ class WC_Install {
*/ */
$max_index_length = 191; $max_index_length = 191;
return " $tables = "
CREATE TABLE {$wpdb->prefix}woocommerce_sessions ( CREATE TABLE {$wpdb->prefix}woocommerce_sessions (
session_id bigint(20) NOT NULL AUTO_INCREMENT, session_id bigint(20) NOT NULL AUTO_INCREMENT,
session_key char(32) NOT NULL, session_key char(32) NOT NULL,
@ -402,15 +402,6 @@ CREATE TABLE {$wpdb->prefix}woocommerce_attribute_taxonomies (
PRIMARY KEY (attribute_id), PRIMARY KEY (attribute_id),
KEY attribute_name (attribute_name($max_index_length)) KEY attribute_name (attribute_name($max_index_length))
) $collate; ) $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 ( CREATE TABLE {$wpdb->prefix}woocommerce_downloadable_product_permissions (
permission_id bigint(20) NOT NULL auto_increment, permission_id bigint(20) NOT NULL auto_increment,
download_id varchar(32) NOT NULL, download_id varchar(32) NOT NULL,
@ -495,6 +486,23 @@ CREATE TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods (
PRIMARY KEY (instance_id) PRIMARY KEY (instance_id)
) $collate; ) $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,6 +14,15 @@ if ( ! defined( 'ABSPATH' ) ) {
global $wpdb; 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. * 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. * zone_enabled and zone_type are no longer used, but it's safe to leave them be.

View File

@ -223,6 +223,7 @@ 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 ) { function wc_taxonomy_metadata_update_content_for_split_terms( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
global $wpdb; global $wpdb;
if ( get_option( 'db_version' ) < 34370 ) {
if ( 'product_cat' === $taxonomy || taxonomy_is_product_attribute( $taxonomy ) ) { 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 ) ); $old_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_termmeta WHERE woocommerce_term_id = %d;", $old_term_id ) );
@ -241,10 +242,35 @@ 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 ); 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 mixed $term_id
* @param string $meta_key * @param string $meta_key
@ -253,12 +279,16 @@ add_action( 'split_shared_term', 'wc_taxonomy_metadata_update_content_for_split_
* @return bool * @return bool
*/ */
function update_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) { 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 $term_id
* @param mixed $meta_key * @param mixed $meta_key
* @param mixed $meta_value * @param mixed $meta_value
@ -266,32 +296,40 @@ function update_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $prev_v
* @return bool * @return bool
*/ */
function add_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $unique = false ){ 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 $term_id
* @param mixed $meta_key * @param mixed $meta_key
* @param string $meta_value (default: '') * @param string $meta_value (default: '')
* @param bool $delete_all (default: false) * @param bool $deprecated (default: false)
* @return bool * @return bool
*/ */
function delete_woocommerce_term_meta( $term_id, $meta_key, $meta_value = '', $delete_all = false ) { function delete_woocommerce_term_meta( $term_id, $meta_key, $meta_value = '', $deprecated = false ) {
return delete_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $delete_all ); 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 mixed $term_id
* @param string $key * @param string $key
* @param bool $single (default: true) * @param bool $single (default: true)
* @return mixed * @return mixed
*/ */
function get_woocommerce_term_meta( $term_id, $key, $single = true ) { 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,7 +343,6 @@ function get_woocommerce_term_meta( $term_id, $key, $single = true ) {
* @return int * @return int
*/ */
function wc_reorder_terms( $the_term, $next_id, $taxonomy, $index = 0, $terms = null ) { 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 ( ! $terms ) $terms = get_terms( $taxonomy, 'menu_order=ASC&hide_empty=0&parent=0' );
if ( empty( $terms ) ) return $index; if ( empty( $terms ) ) return $index;
@ -337,8 +374,9 @@ function wc_reorder_terms( $the_term, $next_id, $taxonomy, $index = 0, $terms =
} }
// no nextid meaning our term is in last position // no nextid meaning our term is in last position
if( $term_in_level && null === $next_id ) if ( $term_in_level && null === $next_id ) {
$index = wc_set_term_order( $id, $index + 1, $taxonomy, true ); $index = wc_set_term_order( $id, $index + 1, $taxonomy, true );
}
return $index; return $index;
} }
@ -435,7 +473,11 @@ function wc_terms_clauses( $clauses, $taxonomies, $args ) {
} }
// query join // 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 ."') "; 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 // 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' ) ) ) {
@ -444,11 +486,11 @@ function wc_terms_clauses( $clauses, $taxonomies, $args ) {
$order = "ORDER BY tm.meta_value+0 " . $args['menu_order']; $order = "ORDER BY tm.meta_value+0 " . $args['menu_order'];
if ( $clauses['orderby'] ): if ( $clauses['orderby'] ) {
$clauses['orderby'] = str_replace( 'ORDER BY', $order . ',', $clauses['orderby'] ); $clauses['orderby'] = str_replace( 'ORDER BY', $order . ',', $clauses['orderby'] );
else: } else {
$clauses['orderby'] = $order; $clauses['orderby'] = $order;
endif; }
return $clauses; return $clauses;
} }