Skip to content

Commit

Permalink
Merge pull request woocommerce#11886 from woothemes/crud-private-prot…
Browse files Browse the repository at this point in the history
…ected

CRUD Updates: Prefix & get_/set_id
  • Loading branch information
justinshreve authored Sep 9, 2016
2 parents fb1e2f8 + e1e6c68 commit 9a37843
Show file tree
Hide file tree
Showing 12 changed files with 448 additions and 537 deletions.
112 changes: 64 additions & 48 deletions includes/abstracts/abstract-wc-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,37 @@
*/
abstract class WC_Data {

/**
* ID for this object.
* @var int
*/
protected $id = 0;

/**
* Core data for this object. Name value pairs (name + default value).
* @var array
*/
protected $_data = array();
protected $data = array();

/**
* Set to _data on construct so we can track and reset data if needed.
* @var array
*/
protected $_default_data = array();
protected $default_data = array();

/**
* Stores meta in cache for future reads.
* A group must be set to to enable caching.
* @var string
*/
protected $_cache_group = '';
protected $cache_group = '';

/**
* Meta type. This should match up with
* the types avaiable at https://codex.wordpress.org/Function_Reference/add_metadata.
* WP defines 'post', 'user', 'comment', and 'term'.
*/
protected $_meta_type = 'post';
protected $meta_type = 'post';

/**
* This only needs set if you are using a custom metadata type (for example payment tokens.
Expand All @@ -53,53 +59,55 @@ abstract class WC_Data {
* Stores additonal meta data.
* @var array
*/
protected $_meta_data = array();
protected $meta_data = array();

/**
* Internal meta keys we don't want exposed for the object.
* @var array
*/
protected $_internal_meta_keys = array();
protected $internal_meta_keys = array();

/**
* Default constructor.
* @param int|object|array $read ID to load from the DB (optional) or already queried data.
*/
public function __construct( $read = 0 ) {
$this->_default_data = $this->_data;
$this->default_data = $this->data;
}

/**
* Returns the unique ID for this object.
* @return int
*/
abstract public function get_id();
public function get_id() {
return $this->id;
}

/**
* Creates new object in the database.
*/
abstract public function create();
public function create() {}

/**
* Read object from the database.
* @param int ID of the object to load.
*/
abstract public function read( $id );
public function read( $id ) {}

/**
* Updates object data in the database.
*/
abstract public function update();
public function update() {}

/**
* Updates object data in the database.
*/
abstract public function delete();
public function delete() {}

/**
* Save should create or update based on object existance.
*/
abstract public function save();
public function save() {}

/**
* Change data to JSON format.
Expand All @@ -114,7 +122,7 @@ public function __toString() {
* @return array
*/
public function get_data() {
return array_merge( $this->_data, array( 'meta_data' => $this->get_meta_data() ) );
return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) );
}

/**
Expand All @@ -131,7 +139,7 @@ protected function filter_null_meta( $meta ) {
* @return array
*/
public function get_meta_data() {
return array_filter( $this->_meta_data, array( $this, 'filter_null_meta' ) );
return array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) );
}

/**
Expand All @@ -151,7 +159,7 @@ protected function prefix_key( $key ) {
* @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 );
return array_merge( array_map( array( $this, 'prefix_key' ), array_keys( $this->data ) ), $this->internal_meta_keys );
}

/**
Expand All @@ -167,9 +175,9 @@ public function get_meta( $key = '', $single = true ) {

if ( ! empty( $array_keys ) ) {
if ( $single ) {
$value = $this->_meta_data[ current( $array_keys ) ]->value;
$value = $this->meta_data[ current( $array_keys ) ]->value;
} else {
$value = array_intersect_key( $this->_meta_data, array_flip( $array_keys ) );
$value = array_intersect_key( $this->meta_data, array_flip( $array_keys ) );
}
}

Expand All @@ -186,7 +194,7 @@ public function set_meta_data( $data ) {
foreach ( $data as $meta ) {
$meta = (array) $meta;
if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) {
$this->_meta_data[] = (object) array(
$this->meta_data[] = (object) array(
'id' => $meta['id'],
'key' => $meta['key'],
'value' => $meta['value'],
Expand All @@ -207,7 +215,7 @@ public function add_meta_data( $key, $value, $unique = false ) {
if ( $unique ) {
$this->delete_meta_data( $key );
}
$this->_meta_data[] = (object) array(
$this->meta_data[] = (object) array(
'key' => $key,
'value' => $value,
);
Expand All @@ -223,10 +231,10 @@ public function add_meta_data( $key, $value, $unique = false ) {
public function update_meta_data( $key, $value, $meta_id = '' ) {
$array_key = '';
if ( $meta_id ) {
$array_key = array_keys( wp_list_pluck( $this->_meta_data, 'id' ), $meta_id );
$array_key = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id );
}
if ( $array_key ) {
$this->_meta_data[ current( $array_key ) ] = (object) array(
$this->meta_data[ current( $array_key ) ] = (object) array(
'id' => $meta_id,
'key' => $key,
'value' => $value,
Expand All @@ -242,10 +250,10 @@ public function update_meta_data( $key, $value, $meta_id = '' ) {
* @param array $key Meta key
*/
public function delete_meta_data( $key ) {
$array_keys = array_keys( wp_list_pluck( $this->_meta_data, 'key' ), $key );
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key );
if ( $array_keys ) {
foreach ( $array_keys as $array_key ) {
$this->_meta_data[ $array_key ]->value = null;
$this->meta_data[ $array_key ]->value = null;
}
}
}
Expand All @@ -256,10 +264,10 @@ public function delete_meta_data( $key ) {
* @param int $mid Meta ID
*/
public function delete_meta_data_by_mid( $mid ) {
$array_keys = array_keys( wp_list_pluck( $this->_meta_data, 'id' ), $mid );
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $mid );
if ( $array_keys ) {
foreach ( $array_keys as $array_key ) {
$this->_meta_data[ $array_key ]->value = null;
$this->meta_data[ $array_key ]->value = null;
}
}
}
Expand All @@ -279,26 +287,26 @@ protected function exclude_internal_meta_keys( $meta ) {
* @since 2.6.0
*/
protected function read_meta_data() {
$this->_meta_data = array();
$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 ( ! 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;
$this->meta_data = $cached_meta;
$cache_loaded = true;
}
}

if ( ! $cache_loaded ) {
global $wpdb;
$db_info = $this->_get_db_info();
$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'] . "
Expand All @@ -308,16 +316,16 @@ protected function read_meta_data() {
if ( $raw_meta_data ) {
$raw_meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) );
foreach ( $raw_meta_data as $meta ) {
$this->_meta_data[] = (object) array(
$this->meta_data[] = (object) array(
'id' => (int) $meta->{ $db_info['meta_id_field'] },
'key' => $meta->meta_key,
'value' => maybe_unserialize( $meta->meta_value ),
);
}
}

if ( ! empty( $this->_cache_group ) ) {
wp_cache_set( $cache_key, $this->_meta_data, $this->_cache_group );
if ( ! empty( $this->cache_group ) ) {
wp_cache_set( $cache_key, $this->meta_data, $this->cache_group );
}
}
}
Expand All @@ -327,21 +335,21 @@ protected function read_meta_data() {
* @since 2.6.0
*/
protected function save_meta_data() {
foreach ( $this->_meta_data as $array_key => $meta ) {
foreach ( $this->meta_data as $array_key => $meta ) {
if ( is_null( $meta->value ) ) {
if ( ! empty( $meta->id ) ) {
delete_metadata_by_mid( $this->_meta_type, $meta->id );
delete_metadata_by_mid( $this->meta_type, $meta->id );
}
} elseif ( empty( $meta->id ) ) {
$new_meta_id = add_metadata( $this->_meta_type, $this->get_id(), $meta->key, $meta->value, false );
$this->_meta_data[ $array_key ]->id = $new_meta_id;
$new_meta_id = add_metadata( $this->meta_type, $this->get_id(), $meta->key, $meta->value, false );
$this->meta_data[ $array_key ]->id = $new_meta_id;
} else {
update_metadata_by_mid( $this->_meta_type, $meta->id, $meta->value, $meta->key );
update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key );
}
}

if ( ! empty( $this->_cache_group ) ) {
WC_Cache_Helper::incr_cache_prefix( $this->_cache_group );
if ( ! empty( $this->cache_group ) ) {
WC_Cache_Helper::incr_cache_prefix( $this->cache_group );
}
$this->read_meta_data();
}
Expand All @@ -351,22 +359,22 @@ protected function save_meta_data() {
* @since 2.6.0
* @return array Array elements: table, object_id_field, meta_id_field
*/
protected function _get_db_info() {
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' ) ) ) {
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';
$table .= $this->meta_type . 'meta';
$object_id_field = $this->meta_type . '_id';

// Figure out our field names.
if ( 'user' === $this->_meta_type ) {
if ( 'user' === $this->meta_type ) {
$meta_id_field = 'umeta_id';
}

Expand All @@ -381,11 +389,19 @@ protected function _get_db_info() {
);
}

/**
* Set ID.
* @param int $id
*/
public function set_id( $id ) {
$this->id = absint( $id );
}

/**
* Set all props to default values.
*/
protected function set_defaults() {
$this->_data = $this->_default_data;
$this->data = $this->default_data;
}

/**
Expand Down
Loading

0 comments on commit 9a37843

Please sign in to comment.