Add actions for all object types

This commit is contained in:
Mike Jolley 2016-12-19 17:09:52 +00:00
parent c78e5cf2ec
commit 20680203bb
9 changed files with 65 additions and 63 deletions

View File

@ -39,6 +39,12 @@ abstract class WC_Data {
*/
protected $object_read = false;
/**
* This is the name of this object type.
* @var string
*/
protected $object_type = 'data';
/**
* Extra data for this object. Name value pairs (name + default value).
* Used as a standard way for sub classes (like product types) to add
@ -120,6 +126,9 @@ abstract class WC_Data {
*/
public function save() {
if ( $this->data_store ) {
// Trigger action before saving to the DB. Use a pointer to adjust object props before save.
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
if ( $this->get_id() ) {
$this->data_store->update( $this );
} else {
@ -501,7 +510,7 @@ abstract class WC_Data {
* @return string
*/
protected function get_hook_prefix() {
return 'woocommerce_get_';
return 'woocommerce_get_' . $this->object_type . '_';
}
/**

View File

@ -82,6 +82,12 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
*/
protected $data_store_name = 'order';
/**
* This is the name of this object type.
* @var string
*/
protected $object_type = 'order';
/**
* Get the order if ID is passed, otherwise the order is new and empty.
* This class should NOT be instantiated, but the get_order function or new WC_Order_Factory.
@ -124,16 +130,6 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
return 'shop_order';
}
/**
* Prefix for action and filter hooks on data.
*
* @since 2.7.0
* @return string
*/
protected function get_hook_prefix() {
return 'woocommerce_get_order_';
}
/**
* Get all class data in array format.
*
@ -176,6 +172,9 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
*/
public function save() {
if ( $this->data_store ) {
// Trigger action before saving to the DB. Use a pointer to adjust object props before save.
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
if ( $this->get_id() ) {
$this->data_store->update( $this );
} else {

View File

@ -21,6 +21,12 @@ include_once( 'abstract-wc-legacy-product.php' );
*/
class WC_Product extends WC_Abstract_Legacy_Product {
/**
* This is the name of this object type.
* @var string
*/
protected $object_type = 'product';
/**
* Post type.
* @var string
@ -121,16 +127,6 @@ class WC_Product extends WC_Abstract_Legacy_Product {
}
}
/**
* Prefix for action and filter hooks on data.
*
* @since 2.7.0
* @return string
*/
protected function get_hook_prefix() {
return 'woocommerce_product_get_';
}
/**
* Get internal type. Should return string and *should be overridden* by child classes.
* @since 2.7.0
@ -1320,6 +1316,9 @@ class WC_Product extends WC_Abstract_Legacy_Product {
$this->validate_props();
if ( $this->data_store ) {
// Trigger action before saving to the DB. Use a pointer to adjust object props before save.
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
if ( $this->get_id() ) {
$this->data_store->update( $this );
} else {

View File

@ -13,6 +13,12 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class WC_Customer_Download extends WC_Data implements ArrayAccess {
/**
* This is the name of this object type.
* @var string
*/
protected $object_type = 'customer_download';
/**
* Download Data array.
*
@ -59,16 +65,6 @@ class WC_Customer_Download extends WC_Data implements ArrayAccess {
}
}
/**
* Prefix for action and filter hooks on data.
*
* @since 2.7.0
* @return string
*/
protected function get_hook_prefix() {
return 'woocommerce_get_download_';
}
/*
|--------------------------------------------------------------------------
| Getters
@ -283,6 +279,9 @@ class WC_Customer_Download extends WC_Data implements ArrayAccess {
*/
public function save() {
if ( $this->data_store ) {
// Trigger action before saving to the DB. Use a pointer to adjust object props before save.
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
if ( $this->get_id() ) {
$this->data_store->update( $this );
} else {

View File

@ -46,6 +46,12 @@ class WC_Order_Item extends WC_Data implements ArrayAccess {
*/
protected $meta_type = 'order_item';
/**
* This is the name of this object type.
* @var string
*/
protected $object_type = 'order_item';
/**
* Constructor.
* @param int|object|array $item ID to load from the DB, or WC_Order_Item Object
@ -69,16 +75,6 @@ class WC_Order_Item extends WC_Data implements ArrayAccess {
}
}
/**
* Prefix for action and filter hooks on data.
*
* @since 2.7.0
* @return string
*/
protected function get_hook_prefix() {
return 'woocommerce_get_order_item_';
}
/*
|--------------------------------------------------------------------------
| Getters

View File

@ -21,6 +21,12 @@ class WC_Order_Refund extends WC_Abstract_Order {
*/
protected $data_store_name = 'order-refund';
/**
* This is the name of this object type.
* @var string
*/
protected $object_type = 'order_refund';
/**
* Stores product data.
*
@ -50,16 +56,6 @@ class WC_Order_Refund extends WC_Abstract_Order {
return 'shop_order_refund';
}
/**
* Prefix for action and filter hooks on data.
*
* @since 2.7.0
* @return string
*/
protected function get_hook_prefix() {
return 'woocommerce_get_order_refund_';
}
/**
* Get status - always completed for refunds.
*

View File

@ -215,6 +215,9 @@ class WC_Order extends WC_Abstract_Order {
public function save() {
$this->maybe_set_user_billing_email();
if ( $this->data_store ) {
// Trigger action before saving to the DB. Use a pointer to adjust object props before save.
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
if ( $this->get_id() ) {
$this->data_store->update( $this );
} else {

View File

@ -365,6 +365,9 @@ class WC_Product_Variable extends WC_Product {
public function save() {
$this->validate_props();
if ( $this->data_store ) {
// Trigger action before saving to the DB. Use a pointer to adjust object props before save.
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
if ( $this->get_id() ) {
$this->data_store->update( $this );
} else {

View File

@ -23,6 +23,12 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
*/
protected $id = null;
/**
* This is the name of this object type.
* @var string
*/
protected $object_type = 'shipping_zone';
/**
* Zone Data
* @var array
@ -56,16 +62,6 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
}
}
/**
* Prefix for action and filter hooks on data.
*
* @since 2.7.0
* @return string
*/
protected function get_hook_prefix() {
return 'woocommerce_get_shipping_zone_';
}
/*
|--------------------------------------------------------------------------
| Getters
@ -248,11 +244,13 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
* @return int
*/
public function save() {
$name = $this->get_zone_name();
if ( empty( $name ) ) {
if ( ! $this->get_zone_name() ) {
$this->set_zone_name( $this->generate_zone_name() );
}
if ( $this->data_store ) {
// Trigger action before saving to the DB. Use a pointer to adjust object props before save.
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
if ( null === $this->get_id() ) {
$this->data_store->create( $this );
} else {