diff --git a/includes/abstracts/abstract-wc-data.php b/includes/abstracts/abstract-wc-data.php new file mode 100644 index 00000000000..7a8e9c84b21 --- /dev/null +++ b/includes/abstracts/abstract-wc-data.php @@ -0,0 +1,319 @@ +_meta_data; + } + + /** + * Internal meta keys we don't want exposed as part of meta_data. This is in + * addition to all data props with _ prefix. + * @return array() + */ + protected function prefix_key( $key ) { + return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; + } + + /** + * Internal meta keys we don't want exposed as part of meta_data. This is in + * addition to all data props with _ prefix. + * @return array() + */ + protected function get_internal_meta_keys() { + return array_merge( array_map( array( $this, 'prefix_key' ), array_keys( $this->_data ) ), $this->_internal_meta_keys ); + } + + /** + * Get Meta Data by Key. + * @param string $key + * @param bool $single return first found meta with key, or all with $key + * @return mixed + */ + public function get_meta( $key = '', $single = true ) { + $meta_ids = array_keys( wp_list_pluck( $this->_meta_data, 'key' ), $key ); + $value = ''; + + if ( $meta_ids ) { + if ( $single ) { + $value = $this->_meta_data[ current( $meta_ids ) ]->value; + } else { + $value = array_intersect_key( $this->_meta_data, $meta_ids ); + } + } + + return $value; + } + + /** + * Set all meta data from array. + * @param array $data Key/Value pairs + */ + public function set_meta_data( $data ) { + if ( ! empty( $data ) && is_array( $data ) ) { + foreach ( $data as $meta_id => $meta ) { + $meta = (array) $meta; + if ( isset( $meta['key'], $meta['value'] ) ) { + $this->_meta_data[ $meta_id ] = (object) array( + 'key' => $meta['key'], + 'value' => $meta['value'], + ); + } + } + } + } + + + /** + * Add meta data. + * @param array $key Meta key + * @param array $value Meta value + * @param array $unique Should this be a unique key? + */ + public function add_meta_data( $key, $value, $unique = false ) { + if ( $unique ) { + $meta_ids = array_keys( wp_list_pluck( $this->_meta_data, 'key' ), $key ); + $this->_meta_data = array_diff_key( $this->_meta_data, array_fill_keys( $meta_ids, '' ) ); + } + $this->_meta_data[ 'new-' . sizeof( $this->_meta_data ) ] = (object) array( + 'key' => $key, + 'value' => $value, + ); + } + + /** + * Update meta data by key or ID, if provided. + * @param string $key + * @param string $value + * @param int $meta_id + */ + public function update_meta_data( $key, $value, $meta_id = '' ) { + if ( $meta_id && isset( $this->_meta_data[ $meta_id ] ) ) { + $this->_meta_data[ $meta_id ] = (object) array( + 'key' => $key, + 'value' => $value, + ); + } else { + $this->add_meta_data( $key, $value, true ); + } + } + + /** + * Delete meta data. + * @param array $key Meta key + */ + public function delete_meta_data( $key ) { + $meta_ids = array_keys( wp_list_pluck( $this->_meta_data, 'key' ), $key ); + $this->_meta_data = array_diff_key( $this->_meta_data, array_fill_keys( $meta_ids, '' ) ); + } + + /** + * Read Meta Data from the database. Ignore any internal properties. + */ + protected function read_meta_data() { + $this->_meta_data = array(); + $cache_loaded = false; + + if ( ! $this->get_id() ) { + return; + } + + if ( ! empty ( $this->_cache_group ) ) { + $cache_key = WC_Cache_Helper::get_cache_prefix( $this->_cache_group ) . $this->get_id(); + $cached_meta = wp_cache_get( $cache_key, $this->_cache_group ); + + if ( false !== $cached_meta ) { + $this->_meta_data = $cached_meta; + $cache_loaded = true; + } + } + + if ( ! $cache_loaded ) { + global $wpdb; + $db_info = $this->_get_db_info(); + $raw_meta_data = $wpdb->get_results( $wpdb->prepare( " + SELECT " . $db_info['meta_id_field'] . ", meta_key, meta_value + FROM " . $db_info['table'] . " + WHERE " . $db_info['object_id_field'] . " = %d ORDER BY meta_id + ", $this->get_id() ) ); + + foreach ( $raw_meta_data as $meta ) { + if ( in_array( $meta->meta_key, $this->get_internal_meta_keys() ) ) { + continue; + } + $this->_meta_data[ $meta->meta_id ] = (object) array( 'key' => $meta->meta_key, 'value' => $meta->meta_value ); + } + + if ( ! empty ( $this->_cache_group ) ) { + wp_cache_set( $cache_key, $this->_meta_data, $this->_cache_group ); + } + } + } + + /** + * Update Meta Data in the database. + */ + protected function save_meta_data() { + global $wpdb; + $db_info = $this->_get_db_info(); + $all_meta_ids = array_map( 'absint', $wpdb->get_col( $wpdb->prepare( " + SELECT " . $db_info['meta_id_field'] . " FROM " . $db_info['table'] . " + WHERE " . $db_info['object_id_field'] . " = %d", $this->get_id() ) . " + AND meta_key NOT IN ('" . implode( "','", array_map( 'esc_sql', $this->get_internal_meta_keys() ) ) . "'); + " ) ); + $set_meta_ids = array(); + + foreach ( $this->_meta_data as $meta_id => $meta ) { + if ( 'new' === substr( $meta_id, 0, 3 ) ) { + $set_meta_ids[] = add_metadata( $this->_meta_type, $this->get_id(), $meta->key, $meta->value, false ); + } else { + update_metadata_by_mid( $this->_meta_type, $meta_id, $meta->value, $meta->key ); + $set_meta_ids[] = absint( $meta_id ); + } + } + + // Delete no longer set meta data + $delete_meta_ids = array_diff( $all_meta_ids, $set_meta_ids ); + + foreach ( $delete_meta_ids as $meta_id ) { + delete_metadata_by_mid( $this->_meta_type, $meta_id ); + } + + if ( ! empty ( $this->_cache_group ) ) { + WC_Cache_Helper::incr_cache_prefix( $this->_cache_group ); + } + $this->read_meta_data(); + } + + /** + * Table structure is slightly different between meta types, this function will return what we need to know. + * @return array Array elements: table, object_id_field, meta_id_field + */ + private function _get_db_info() { + global $wpdb; + + $object_id_field = ''; + $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'; + + // Figure out our field names. + if ( 'post' === $this->_meta_type ) { + $object_id_field = 'post_id'; + } elseif ( 'user' === $this->_meta_type ) { + $object_id_field = 'user_id'; + $meta_id_field = 'umeta_id'; + } else if ( 'comment' === $this->_meta_type ) { + $object_id_field = 'comment_id'; + } else if ( 'term' === $this->_meta_data ) { + $object_id_field = 'term_id'; + } else { + if ( ! empty ( $this->object_id_field_for_meta ) ) { + $object_id_field = $this->object_id_field_for_meta; + } else { + $object_id_field = 'post_id'; // default to post_id + } + } + + return array( + 'table' => $table, + 'object_id_field' => $object_id_field, + 'meta_id_field' => $meta_id_field, + ); + } + +}