PHP Class WC_Data

Implemented by classes using the same CRUD(s) pattern.
Author: WooThemes
Show file Open project: woocommerce/woocommerce Class Usage Examples

Protected Properties

Property Type Description
$cache_group string A group must be set to to enable caching.
$changes array Core data changes for this object.
$data array Core data for this object. Name value pairs (name + default value).
$data_store object Contains a reference to the data store for this class.
$default_data array Set to _data on construct so we can track and reset data if needed.
$extra_data array Used as a standard way for sub classes (like product types) to add additional information to an inherited class.
$id integer ID for this object.
$meta_data array Stores additonal meta data.
$object_read boolean This is false until the object is read from the DB.

Public Methods

Method Description
__construct ( integer | object | array $read ) Default constructor.
__toString ( ) : string Change data to JSON format.
add_meta_data ( string $key, string $value, boolean $unique = false ) Add meta data.
apply_changes ( ) Merge changes with data and clear.
delete ( boolean $force_delete = false ) : boolean Delete an object, set the ID to 0, and return result.
delete_meta_data ( array $key ) Delete meta data.
delete_meta_data_by_mid ( integer $mid ) Delete meta data.
get_changes ( ) : array Return data changes only.
get_data ( ) : array Returns all data for this object.
get_data_keys ( ) : array Returns array of expected data keys for this object.
get_data_store ( ) : object Get the data store.
get_extra_data_keys ( ) : array Returns all "extra" data keys for an object (for sub objects like product types).
get_id ( ) : integer Returns the unique ID for this object.
get_meta ( string $key = '', boolean $single = true, string $context = 'view' ) : mixed Get Meta Data by Key.
get_meta_data ( ) : array Get All Meta Data.
get_object_read ( ) : boolean Get object read property.
read_meta_data ( boolean $force_read = false ) Read Meta Data from the database. Ignore any internal properties.
save ( ) : integer Save should create or update based on object existance.
save_meta_data ( ) Update Meta Data in the database.
set_defaults ( ) Set all props to default values.
set_id ( integer $id ) Set ID.
set_meta_data ( array $data ) Set all meta data from array.
set_object_read ( boolean $read = true ) Set object read property.
set_props ( array $props, $context = 'set' ) : WP_Error | boolean Set a collection of props in one go, collect any errors, and return the result.
update_meta_data ( string $key, string $value, integer $meta_id = '' ) Update meta data by key or ID, if provided.

Protected Methods

Method Description
error ( string $error_code, string $error_message ) When invalid data is found, throw an exception unless reading from the DB.
filter_null_meta ( $meta ) : boolean Filter null meta values from array.
get_hook_prefix ( ) : string Prefix for action and filter hooks on data.
get_prop ( string $prop, string $context = 'view' ) : mixed Gets a prop for a getter method.
set_prop ( string $prop, mixed $value ) Sets a prop for a setter method.

Method Details

__construct() public method

Default constructor.
public __construct ( integer | object | array $read )
$read integer | object | array ID to load from the DB (optional) or already queried data.

__toString() public method

Change data to JSON format.
public __toString ( ) : string
return string Data in JSON format.

add_meta_data() public method

Add meta data.
Since: 2.6.0
public add_meta_data ( string $key, string $value, boolean $unique = false )
$key string Meta key
$value string Meta value
$unique boolean Should this be a unique key?

apply_changes() public method

Merge changes with data and clear.
Since: 2.7.0
public apply_changes ( )

delete() public method

Delete an object, set the ID to 0, and return result.
public delete ( boolean $force_delete = false ) : boolean
$force_delete boolean
return boolean result

delete_meta_data() public method

Delete meta data.
Since: 2.6.0
public delete_meta_data ( array $key )
$key array Meta key

delete_meta_data_by_mid() public method

Delete meta data.
Since: 2.6.0
public delete_meta_data_by_mid ( integer $mid )
$mid integer Meta ID

error() protected method

When invalid data is found, throw an exception unless reading from the DB.
protected error ( string $error_code, string $error_message )
$error_code string Error code.
$error_message string Error message.

filter_null_meta() protected method

Filter null meta values from array.
protected filter_null_meta ( $meta ) : boolean
return boolean

get_changes() public method

Return data changes only.
Since: 2.7.0
public get_changes ( ) : array
return array

get_data() public method

Returns all data for this object.
public get_data ( ) : array
return array

get_data_keys() public method

Returns array of expected data keys for this object.
Since: 2.7.0
public get_data_keys ( ) : array
return array

get_data_store() public method

Get the data store.
Since: 2.7.0
public get_data_store ( ) : object
return object

get_extra_data_keys() public method

Returns all "extra" data keys for an object (for sub objects like product types).
Since: 2.7.0
public get_extra_data_keys ( ) : array
return array

get_hook_prefix() protected method

Prefix for action and filter hooks on data.
Since: 2.7.0
protected get_hook_prefix ( ) : string
return string

get_id() public method

Returns the unique ID for this object.
public get_id ( ) : integer
return integer

get_meta() public method

Get Meta Data by Key.
Since: 2.6.0
public get_meta ( string $key = '', boolean $single = true, string $context = 'view' ) : mixed
$key string
$single boolean return first found meta with key, or all with $key
$context string What the value is for. Valid values are view and edit.
return mixed

get_meta_data() public method

Get All Meta Data.
Since: 2.6.0
public get_meta_data ( ) : array
return array

get_object_read() public method

Get object read property.
public get_object_read ( ) : boolean
return boolean

get_prop() protected method

Gets the value from either current pending changes, or the data itself. Context controls what happens to the value before it's returned.
Since: 2.7.0
protected get_prop ( string $prop, string $context = 'view' ) : mixed
$prop string Name of prop to get.
$context string What the value is for. Valid values are view and edit.
return mixed

read_meta_data() public method

Read Meta Data from the database. Ignore any internal properties.
Since: 2.6.0
public read_meta_data ( boolean $force_read = false )
$force_read boolean True to force a new DB read (and update cache).

save() public method

Save should create or update based on object existance.
public save ( ) : integer
return integer

save_meta_data() public method

Update Meta Data in the database.
Since: 2.6.0
public save_meta_data ( )

set_defaults() public method

Set all props to default values.
public set_defaults ( )

set_id() public method

Set ID.
public set_id ( integer $id )
$id integer

set_meta_data() public method

Set all meta data from array.
Since: 2.6.0
public set_meta_data ( array $data )
$data array Key/Value pairs

set_object_read() public method

Set object read property.
public set_object_read ( boolean $read = true )
$read boolean

set_prop() protected method

This stores changes in a special array so we can track what needs saving the the DB later.
Since: 2.7.0
protected set_prop ( string $prop, mixed $value )
$prop string Name of prop to set.
$value mixed Value of the prop.

set_props() public method

Only sets using public methods.
public set_props ( array $props, $context = 'set' ) : WP_Error | boolean
$props array Key value pairs to set. Key is the prop and should map to a setter function name.
return WP_Error | boolean

update_meta_data() public method

Update meta data by key or ID, if provided.
Since: 2.6.0
public update_meta_data ( string $key, string $value, integer $meta_id = '' )
$key string
$value string
$meta_id integer

Property Details

$cache_group protected property

A group must be set to to enable caching.
protected string $cache_group
return string

$changes protected property

Core data changes for this object.
protected array $changes
return array

$data protected property

Core data for this object. Name value pairs (name + default value).
protected array $data
return array

$data_store protected property

Contains a reference to the data store for this class.
protected object $data_store
return object

$default_data protected property

Set to _data on construct so we can track and reset data if needed.
protected array $default_data
return array

$extra_data protected property

Used as a standard way for sub classes (like product types) to add additional information to an inherited class.
protected array $extra_data
return array

$id protected property

ID for this object.
protected int $id
return integer

$meta_data protected property

Stores additonal meta data.
protected array $meta_data
return array

$object_read protected property

This is false until the object is read from the DB.
protected bool $object_read
return boolean