Use WP term meta functions

This commit is contained in:
Mike Jolley 2019-01-25 20:47:54 +00:00
parent 8823559880
commit 32ae0192e5
10 changed files with 101 additions and 134 deletions

View File

@ -97,7 +97,7 @@ class WC_Admin_Taxonomies {
$meta_name = taxonomy_is_product_attribute( $taxonomy ) ? 'order_' . esc_attr( $taxonomy ) : 'order';
update_woocommerce_term_meta( $term_id, $meta_name, 0 );
update_term_meta( $term_id, $meta_name, 0 );
}
/**
@ -212,8 +212,8 @@ class WC_Admin_Taxonomies {
*/
public function edit_category_fields( $term ) {
$display_type = get_woocommerce_term_meta( $term->term_id, 'display_type', true );
$thumbnail_id = absint( get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true ) );
$display_type = get_term_meta( $term->term_id, 'display_type', true );
$thumbnail_id = absint( get_term_meta( $term->term_id, 'thumbnail_id', true ) );
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( $thumbnail_id );
@ -307,10 +307,10 @@ class WC_Admin_Taxonomies {
*/
public function save_category_fields( $term_id, $tt_id = '', $taxonomy = '' ) {
if ( isset( $_POST['display_type'] ) && 'product_cat' === $taxonomy ) { // WPCS: CSRF ok, input var ok.
update_woocommerce_term_meta( $term_id, 'display_type', esc_attr( $_POST['display_type'] ) ); // WPCS: CSRF ok, sanitization ok, input var ok.
update_term_meta( $term_id, 'display_type', esc_attr( $_POST['display_type'] ) ); // WPCS: CSRF ok, sanitization ok, input var ok.
}
if ( isset( $_POST['product_cat_thumbnail_id'] ) && 'product_cat' === $taxonomy ) { // WPCS: CSRF ok, input var ok.
update_woocommerce_term_meta( $term_id, 'thumbnail_id', absint( $_POST['product_cat_thumbnail_id'] ) ); // WPCS: CSRF ok, input var ok.
update_term_meta( $term_id, 'thumbnail_id', absint( $_POST['product_cat_thumbnail_id'] ) ); // WPCS: CSRF ok, input var ok.
}
}
@ -433,7 +433,7 @@ class WC_Admin_Taxonomies {
$columns .= wc_help_tip( __( 'This is the default category and it cannot be deleted. It will be automatically assigned to products with no category.', 'woocommerce' ) );
}
$thumbnail_id = get_woocommerce_term_meta( $id, 'thumbnail_id', true );
$thumbnail_id = get_term_meta( $id, 'thumbnail_id', true );
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( $thumbnail_id );

View File

@ -34,10 +34,10 @@ class WC_REST_Product_Categories_Controller extends WC_REST_Product_Categories_V
*/
public function prepare_item_for_response( $item, $request ) {
// Get category display type.
$display_type = get_woocommerce_term_meta( $item->term_id, 'display_type' );
$display_type = get_term_meta( $item->term_id, 'display_type' );
// Get category order.
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order' );
$menu_order = get_term_meta( $item->term_id, 'order' );
$data = array(
'id' => (int) $item->term_id,
@ -52,7 +52,7 @@ class WC_REST_Product_Categories_Controller extends WC_REST_Product_Categories_V
);
// Get category image.
$image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' );
$image_id = get_term_meta( $item->term_id, 'thumbnail_id' );
if ( $image_id ) {
$attachment = get_post( $image_id );

View File

@ -607,11 +607,11 @@ class WC_API_Products extends WC_API_Resource {
$term_id = intval( $term->term_id );
// Get category display type
$display_type = get_woocommerce_term_meta( $term_id, 'display_type' );
$display_type = get_term_meta( $term_id, 'display_type' );
// Get category image
$image = '';
if ( $image_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id' ) ) {
if ( $image_id = get_term_meta( $term_id, 'thumbnail_id' ) ) {
$image = wp_get_attachment_url( $image_id );
}

View File

@ -666,11 +666,11 @@ class WC_API_Products extends WC_API_Resource {
$term_id = intval( $term->term_id );
// Get category display type
$display_type = get_woocommerce_term_meta( $term_id, 'display_type' );
$display_type = get_term_meta( $term_id, 'display_type' );
// Get category image
$image = '';
if ( $image_id = get_woocommerce_term_meta( $term_id, 'thumbnail_id' ) ) {
if ( $image_id = get_term_meta( $term_id, 'thumbnail_id' ) ) {
$image = wp_get_attachment_url( $image_id );
}
@ -750,11 +750,11 @@ class WC_API_Products extends WC_API_Resource {
$id = $insert['term_id'];
update_woocommerce_term_meta( $id, 'display_type', 'default' === $data['display'] ? '' : sanitize_text_field( $data['display'] ) );
update_term_meta( $id, 'display_type', 'default' === $data['display'] ? '' : sanitize_text_field( $data['display'] ) );
// Check if image_id is a valid image attachment before updating the term meta.
if ( $image_id && wp_attachment_is_image( $image_id ) ) {
update_woocommerce_term_meta( $id, 'thumbnail_id', $image_id );
update_term_meta( $id, 'thumbnail_id', $image_id );
}
do_action( 'woocommerce_api_create_product_category', $id, $data );
@ -823,11 +823,11 @@ class WC_API_Products extends WC_API_Resource {
}
if ( ! empty( $data['display'] ) ) {
update_woocommerce_term_meta( $id, 'display_type', 'default' === $data['display'] ? '' : sanitize_text_field( $data['display'] ) );
update_term_meta( $id, 'display_type', 'default' === $data['display'] ? '' : sanitize_text_field( $data['display'] ) );
}
if ( isset( $image_id ) ) {
update_woocommerce_term_meta( $id, 'thumbnail_id', $image_id );
update_term_meta( $id, 'thumbnail_id', $image_id );
}
do_action( 'woocommerce_api_edit_product_category', $id, $data );

View File

@ -134,7 +134,7 @@ class WC_REST_Product_Attribute_Terms_V1_Controller extends WC_REST_Terms_Contro
*/
public function prepare_item_for_response( $item, $request ) {
// Get term order.
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order_' . $this->taxonomy );
$menu_order = get_term_meta( $item->term_id, 'order_' . $this->taxonomy );
$data = array(
'id' => (int) $item->term_id,
@ -175,7 +175,7 @@ class WC_REST_Product_Attribute_Terms_V1_Controller extends WC_REST_Terms_Contro
protected function update_term_meta_fields( $term, $request ) {
$id = (int) $term->term_id;
update_woocommerce_term_meta( $id, 'order_' . $this->taxonomy, $request['menu_order'] );
update_term_meta( $id, 'order_' . $this->taxonomy, $request['menu_order'] );
return true;
}

View File

@ -52,10 +52,10 @@ class WC_REST_Product_Categories_V1_Controller extends WC_REST_Terms_Controller
*/
public function prepare_item_for_response( $item, $request ) {
// Get category display type.
$display_type = get_woocommerce_term_meta( $item->term_id, 'display_type' );
$display_type = get_term_meta( $item->term_id, 'display_type' );
// Get category order.
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order' );
$menu_order = get_term_meta( $item->term_id, 'order' );
$data = array(
'id' => (int) $item->term_id,
@ -70,7 +70,7 @@ class WC_REST_Product_Categories_V1_Controller extends WC_REST_Terms_Controller
);
// Get category image.
$image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' );
$image_id = get_term_meta( $item->term_id, 'thumbnail_id' );
if ( $image_id ) {
$attachment = get_post( $image_id );
@ -115,11 +115,11 @@ class WC_REST_Product_Categories_V1_Controller extends WC_REST_Terms_Controller
$id = (int) $term->term_id;
if ( isset( $request['display'] ) ) {
update_woocommerce_term_meta( $id, 'display_type', 'default' === $request['display'] ? '' : $request['display'] );
update_term_meta( $id, 'display_type', 'default' === $request['display'] ? '' : $request['display'] );
}
if ( isset( $request['menu_order'] ) ) {
update_woocommerce_term_meta( $id, 'order', $request['menu_order'] );
update_term_meta( $id, 'order', $request['menu_order'] );
}
if ( isset( $request['image'] ) ) {
@ -137,7 +137,7 @@ class WC_REST_Product_Categories_V1_Controller extends WC_REST_Terms_Controller
// Check if image_id is a valid image attachment before updating the term meta.
if ( $image_id && wp_attachment_is_image( $image_id ) ) {
update_woocommerce_term_meta( $id, 'thumbnail_id', $image_id );
update_term_meta( $id, 'thumbnail_id', $image_id );
// Set the image alt.
if ( ! empty( $request['image']['alt'] ) ) {
@ -152,7 +152,7 @@ class WC_REST_Product_Categories_V1_Controller extends WC_REST_Terms_Controller
) );
}
} else {
delete_woocommerce_term_meta( $id, 'thumbnail_id' );
delete_term_meta( $id, 'thumbnail_id' );
}
}

View File

@ -34,10 +34,10 @@ class WC_REST_Product_Categories_V2_Controller extends WC_REST_Product_Categorie
*/
public function prepare_item_for_response( $item, $request ) {
// Get category display type.
$display_type = get_woocommerce_term_meta( $item->term_id, 'display_type' );
$display_type = get_term_meta( $item->term_id, 'display_type' );
// Get category order.
$menu_order = get_woocommerce_term_meta( $item->term_id, 'order' );
$menu_order = get_term_meta( $item->term_id, 'order' );
$data = array(
'id' => (int) $item->term_id,
@ -52,7 +52,7 @@ class WC_REST_Product_Categories_V2_Controller extends WC_REST_Product_Categorie
);
// Get category image.
$image_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id' );
$image_id = get_term_meta( $item->term_id, 'thumbnail_id' );
if ( $image_id ) {
$attachment = get_post( $image_id );

View File

@ -1024,3 +1024,66 @@ function wc_get_core_supported_themes() {
function wc_taxonomy_metadata_update_content_for_split_terms( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
wc_deprecated_function( 'wc_taxonomy_metadata_update_content_for_split_terms', '3.6' );
}
/**
* 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.
*
* @param int $term_id Term ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
* @param string $prev_value Previous value. (default: '').
* @return bool
*/
function update_woocommerce_term_meta( $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.
*
* 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.
*
* @param int $term_id Term ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
* @param bool $unique Make meta key unique. (default: false).
* @return bool
*/
function add_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
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
*
* 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.
*
* @param int $term_id Term ID.
* @param string $meta_key Meta key.
* @param string $meta_value Meta value (default: '').
* @param bool $deprecated Deprecated param (default: false).
* @return bool
*/
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
*
* 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.
*
* @param int $term_id Term ID.
* @param string $key Meta key.
* @param bool $single Whether to return a single value. (default: true).
* @return mixed
*/
function get_woocommerce_term_meta( $term_id, $key, $single = true ) {
return function_exists( 'get_term_meta' ) ? get_term_meta( $term_id, $key, $single ) : get_metadata( 'woocommerce_term', $term_id, $key, $single );
}

View File

@ -2222,7 +2222,7 @@ if ( ! function_exists( 'woocommerce_get_loop_display_mode' ) ) {
$display_type = get_option( 'woocommerce_shop_page_display', '' );
} elseif ( is_product_category() ) {
$parent_id = get_queried_object_id();
$display_type = get_woocommerce_term_meta( $parent_id, 'display_type', true );
$display_type = get_term_meta( $parent_id, 'display_type', true );
$display_type = '' === $display_type ? get_option( 'woocommerce_category_archive_display', '' ) : $display_type;
}
@ -2432,7 +2432,7 @@ if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) {
function woocommerce_subcategory_thumbnail( $category ) {
$small_thumbnail_size = apply_filters( 'subcategory_archive_thumbnail_size', 'woocommerce_thumbnail' );
$dimensions = wc_get_image_size( $small_thumbnail_size );
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
if ( $thumbnail_id ) {
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );

View File

@ -259,39 +259,6 @@ function wc_walk_category_dropdown_tree() {
return call_user_func_array( array( &$walker, 'walk' ), $args );
}
/**
* When a term is split, ensure meta data maintained.
*
* @param int $old_term_id Old term ID.
* @param int $new_term_id New term ID.
* @param string $term_taxonomy_id Term taxonomy ID.
* @param string $taxonomy Taxonomy.
*/
function wc_taxonomy_metadata_update_content_for_split_terms( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
global $wpdb;
if ( get_option( 'db_version' ) < 34370 ) {
if ( 'product_cat' === $taxonomy || taxonomy_is_product_attribute( $taxonomy ) ) {
$old_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value 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, // WPCS: slow query ok.
'meta_value' => $meta_data->meta_value, // WPCS: slow query ok.
)
);
}
}
}
}
}
add_action( 'split_shared_term', 'wc_taxonomy_metadata_update_content_for_split_terms', 10, 4 );
/**
* Migrate data from WC term meta to WP term meta.
*
@ -311,69 +278,6 @@ function wc_taxonomy_metadata_migrate_data( $wp_db_version, $wp_current_db_versi
}
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.
*
* @param int $term_id Term ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
* @param string $prev_value Previous value. (default: '').
* @return bool
*/
function update_woocommerce_term_meta( $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.
*
* 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.
*
* @param int $term_id Term ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
* @param bool $unique Make meta key unique. (default: false).
* @return bool
*/
function add_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
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
*
* 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.
*
* @param int $term_id Term ID.
* @param string $meta_key Meta key.
* @param string $meta_value Meta value (default: '').
* @param bool $deprecated Deprecated param (default: false).
* @return bool
*/
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
*
* 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.
*
* @param int $term_id Term ID.
* @param string $key Meta key.
* @param bool $single Whether to return a single value. (default: true).
* @return mixed
*/
function get_woocommerce_term_meta( $term_id, $key, $single = true ) {
return function_exists( 'get_term_meta' ) ? get_term_meta( $term_id, $key, $single ) : get_metadata( 'woocommerce_term', $term_id, $key, $single );
}
/**
* Move a term before the a given element of its hierarchy level.
*
@ -447,7 +351,7 @@ function wc_set_term_order( $term_id, $index, $taxonomy, $recursive = false ) {
$term_id = (int) $term_id;
$index = (int) $index;
update_woocommerce_term_meta( $term_id, 'order', $index );
update_term_meta( $term_id, 'order', $index );
if ( ! $recursive ) {
return $index;
@ -560,7 +464,7 @@ function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_
$count = $wpdb->get_var( implode( ' ', $term_query ) ); // WPCS: unprepared SQL ok.
// Update the count.
update_woocommerce_term_meta( $term_id, 'product_count_' . $taxonomy->name, absint( $count ) );
update_term_meta( $term_id, 'product_count_' . $taxonomy->name, absint( $count ) );
}
delete_transient( 'wc_term_counts' );
@ -625,7 +529,7 @@ function wc_change_term_counts( $terms, $taxonomies ) {
foreach ( $terms as &$term ) {
if ( is_object( $term ) ) {
$term_counts[ $term->term_id ] = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : get_woocommerce_term_meta( $term->term_id, 'product_count_' . $taxonomies[0], true );
$term_counts[ $term->term_id ] = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : get_term_meta( $term->term_id, 'product_count_' . $taxonomies[0], true );
if ( '' !== $term_counts[ $term->term_id ] ) {
$term->count = absint( $term_counts[ $term->term_id ] );
@ -652,11 +556,11 @@ add_filter( 'get_terms', 'wc_change_term_counts', 10, 2 );
* @return array
*/
function wc_get_term_product_ids( $term_id, $taxonomy ) {
$product_ids = get_woocommerce_term_meta( $term_id, 'product_ids', true );
$product_ids = get_term_meta( $term_id, 'product_ids', true );
if ( false === $product_ids || ! is_array( $product_ids ) ) {
$product_ids = get_objects_in_term( $term_id, $taxonomy );
update_woocommerce_term_meta( $term_id, 'product_ids', $product_ids );
update_term_meta( $term_id, 'product_ids', $product_ids );
}
return $product_ids;
@ -674,10 +578,10 @@ function wc_get_term_product_ids( $term_id, $taxonomy ) {
*/
function wc_clear_term_product_ids( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
foreach ( $old_tt_ids as $term_id ) {
delete_woocommerce_term_meta( $term_id, 'product_ids' );
delete_term_meta( $term_id, 'product_ids' );
}
foreach ( $tt_ids as $term_id ) {
delete_woocommerce_term_meta( $term_id, 'product_ids' );
delete_term_meta( $term_id, 'product_ids' );
}
}
add_action( 'set_object_terms', 'wc_clear_term_product_ids', 10, 6 );