get_id(), $taxonomy ); if ( false === $terms || is_wp_error( $terms ) ) { return array(); } return wp_list_pluck( $terms, 'term_id' ); } /** * Returns an array of meta for an object. * * @since 2.7.0 * @param WC_Data * @return array */ public function read_meta( &$object ) { global $wpdb; $db_info = $this->get_db_info(); $raw_meta_data = $wpdb->get_results( $wpdb->prepare( " SELECT " . $db_info['meta_id_field'] . " as meta_id, meta_key, meta_value FROM " . $db_info['table'] . " WHERE " . $db_info['object_id_field'] . "=%d AND meta_key NOT LIKE 'wp\_%%' ORDER BY " . $db_info['meta_id_field'] . " ", $object->get_id() ) ); $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); return array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); } /** * Deletes meta based on meta ID. * * @since 2.7.0 * @param WC_Data * @param stdClass (containing at least ->id) * @return array */ public function delete_meta( &$object, $meta ) { delete_metadata_by_mid( $this->meta_type, $meta->id ); } /** * Add new piece of meta. * * @since 2.7.0 * @param WC_Data * @param stdClass (containing ->key and ->value) * @return meta ID */ public function add_meta( &$object, $meta ) { return add_metadata( $this->meta_type, $object->get_id(), $meta->key, $meta->value, false ); } /** * Update meta. * * @since 2.7.0 * @param WC_Data * @param stdClass (containing ->id, ->key and ->value) */ public function update_meta( &$object, $meta ) { update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); } /** * Table structure is slightly different between meta types, this function will return what we need to know. * * @since 2.7.0 * @return array Array elements: table, object_id_field, meta_id_field */ protected function get_db_info() { global $wpdb; $meta_id_field = 'meta_id'; // for some reason users calls this umeta_id so we need to track this as well. $table = $wpdb->prefix; // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ) ) ) { $table .= 'woocommerce_'; } $table .= $this->meta_type . 'meta'; $object_id_field = $this->meta_type . '_id'; // Figure out our field names. if ( 'user' === $this->meta_type ) { $meta_id_field = 'umeta_id'; } if ( ! empty( $this->object_id_field_for_meta ) ) { $object_id_field = $this->object_id_field_for_meta; } return array( 'table' => $table, 'object_id_field' => $object_id_field, 'meta_id_field' => $meta_id_field, ); } /** * Internal meta keys we don't want exposed as part of meta_data. This is in * addition to all data props with _ prefix. * @since 2.6.0 * @return array */ protected function prefix_key( $key ) { return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; } /** * Callback to remove unwanted meta data. * * @param object $meta * @return bool */ protected function exclude_internal_meta_keys( $meta ) { return ! in_array( $meta->meta_key, $this->internal_meta_keys ); } }