2016-03-11 19:50:44 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-02 02:40:36 +00:00
|
|
|
* Abstract WC Data Class
|
|
|
|
*
|
|
|
|
* Implemented by classes using the same CRUD(s) pattern.
|
|
|
|
*
|
|
|
|
* @version 2.6.0
|
|
|
|
* @package WooCommerce/Abstracts
|
|
|
|
* @category Abstract Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2016-03-11 19:50:44 +00:00
|
|
|
abstract class WC_Data {
|
|
|
|
|
2016-09-09 12:34:49 +00:00
|
|
|
/**
|
|
|
|
* ID for this object.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $id = 0;
|
|
|
|
|
2016-03-17 15:22:29 +00:00
|
|
|
/**
|
2016-08-25 12:31:03 +00:00
|
|
|
* Core data for this object. Name value pairs (name + default value).
|
2016-08-24 11:34:19 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $data = array();
|
2016-08-24 11:34:19 +00:00
|
|
|
|
2016-11-08 10:16:16 +00:00
|
|
|
/**
|
|
|
|
* Core data changes for this object.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $changes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is false until the object is read from the DB.
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $object_read = false;
|
|
|
|
|
2016-11-08 09:39:47 +00:00
|
|
|
/**
|
|
|
|
* Extra data for this object. Name value pairs (name + default value).
|
|
|
|
* Used as a standard way for sub classes (like product types) to add
|
|
|
|
* additional information to an inherited class.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $extra_data = array();
|
|
|
|
|
2016-08-24 11:34:19 +00:00
|
|
|
/**
|
2016-08-25 12:31:03 +00:00
|
|
|
* Set to _data on construct so we can track and reset data if needed.
|
2016-03-17 15:22:29 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $default_data = array();
|
2016-03-17 15:22:29 +00:00
|
|
|
|
2016-11-08 09:39:47 +00:00
|
|
|
/**
|
|
|
|
* Contains a reference to the data store for this class.
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $data_store;
|
|
|
|
|
2016-11-17 11:16:07 +00:00
|
|
|
/**
|
|
|
|
* Contains a reference to the data store for meta handling.
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $meta_data_store;
|
|
|
|
|
2016-03-11 19:50:44 +00:00
|
|
|
/**
|
|
|
|
* Stores meta in cache for future reads.
|
|
|
|
* A group must be set to to enable caching.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $cache_group = '';
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'.
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $meta_type = 'post';
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This only needs set if you are using a custom metadata type (for example payment tokens.
|
|
|
|
* This should be the name of the field your table uses for associating meta with objects.
|
|
|
|
* For example, in payment_tokenmeta, this would be payment_token_id.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $object_id_field_for_meta = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores additonal meta data.
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $meta_data = array();
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal meta keys we don't want exposed for the object.
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
protected $internal_meta_keys = array();
|
2016-03-11 19:50:44 +00:00
|
|
|
|
2016-08-25 12:31:03 +00:00
|
|
|
/**
|
|
|
|
* Default constructor.
|
|
|
|
* @param int|object|array $read ID to load from the DB (optional) or already queried data.
|
|
|
|
*/
|
|
|
|
public function __construct( $read = 0 ) {
|
2016-11-17 11:16:07 +00:00
|
|
|
$this->default_data = $this->data;
|
|
|
|
$this->meta_data_store = WC_Data_Store::load( 'meta' );
|
2016-08-25 12:31:03 +00:00
|
|
|
}
|
|
|
|
|
WIP - Product CRUD (#12065)
* Created function to get the catalog visibility options
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* Hardcode the get_type per product class
* Initial look through getters and setters and abstract data
* Missing var
* Add related product functions and deprecate those in class.
* No need to exclude ID
* Fixed coding standards and improved the docblocks
* Get cached terms from wc_get_related_terms()
* Fixed wrong variable in wc_get_related_terms
* Use count() instead of sizeof()
* Sanitize ids later
* Remove unneeded comments
* wc_get_product_term_ids instead of related wording and use in other places.
get_the_terms is used here and also handles caching, something
wp_get_post_terms does not.
* Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it.
* Bump template version
* Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function
* Made abstract function useful
* External Product CRUD
* _virtual meta should be 'no', not taxable, in product unit test helper
* Grouped product class
* Tests
* Move children to meta and update test
* Use get_upsell_ids
* Spacing in query
* Moving and refactoring methods
* Availability html
* Tidy/add todos
* Rename method
* Put back review functions (still todo)
* missing $this
* get_price_including_tax/excluding_tax functions
* wc_get_price_to_display
* Price handling
* [Product CRUD] Variable (#12146)
* [Product CRUD] Variable Products
* Handle PR feedback.
* [Product CRUD] Grouped Handling (#12151)
* Handle grouped product saving
* Update routine
* [Product CRUD] Product crud terms (#12149)
* Category and tag id handling
* Replace template functions
* Remove todo
* Handle default name in save function
* Product crud admin save routine (#12174)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Spacing
* Fix comment
* wc_implode_text_attributes helper function
* [Product CRUD] Product crud admin use getters (#12196)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Move settings into new files
* Refactor panels and use getters
* Use getters for variation panel
* Revert save variation changes for now
* Add todos
* Fix downloads
* REST API CRUD Updates
* Additional API updates/fixes. Added some todos
* Fix final failing tests and implementing setters/getters and attributes functionality.
* Fix comparison for is_on_sale and remove download_type from WC_Product.
* Add a wc_get_products wrapper.
* Remove the download type input from the product data metabox for downloadable products. (#12221)
* [Product CRUD] Variations - setters, getters and admin. (#12228)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* Feedback fixes
* Implement CRUD in the legacy REST API
* Handle PR feedback
* [Product CRUD] Getter setter proxy methods (#12236)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* get_prop implementation in abstract and data classes
* Implement set_prop
* Change handling
* Array key exists
* set_object_read
* Use get_the_terms() instead of wp_get_post_terms()
wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not
use the object cache, and generates a database query every time it is used.
get_the_terms() however can use data from the object cache if present.
* Allow WP_Query to preload post data, and meta in wc_get_products()
Allow WP_Query to bulk query for post data and meta if more than
just IDs are requested from wc_get_products(). Reduces query count
significantly.
* [Product CRUD] Variable, variation, notices, and stock handling (#12277)
* No longer needed
* Remove old todos
* Use getters in admin list
* Related and upsells update for CRUD
* Fix notice in gallery
* Variable fixes and todos
* Context
* Price sync
* Revert variation attributes change
* Return parent data in view context
* Defer term counting
* wc_find_matching_product_variation
* Stock manage tweaks
* Stock fixes
* Correct id
* correct id
* Better sync
* Data logic setter fix
* feedback
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* Hardcode the get_type per product class
* Initial look through getters and setters and abstract data
* Missing var
* Fixed coding standards and improved the docblocks
* Get cached terms from wc_get_related_terms()
* Fixed wrong variable in wc_get_related_terms
* Use count() instead of sizeof()
* Add related product functions and deprecate those in class.
* No need to exclude ID
* Sanitize ids later
* Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it.
* Remove unneeded comments
* wc_get_product_term_ids instead of related wording and use in other places.
get_the_terms is used here and also handles caching, something
wp_get_post_terms does not.
* Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function
* External Product CRUD
* _virtual meta should be 'no', not taxable, in product unit test helper
* Bump template version
* Made abstract function useful
* Grouped product class
* Tests
* Move children to meta and update test
* Use get_upsell_ids
* Spacing in query
* Moving and refactoring methods
* Availability html
* Tidy/add todos
* Rename method
* Put back review functions (still todo)
* missing $this
* get_price_including_tax/excluding_tax functions
* wc_get_price_to_display
* Price handling
* [Product CRUD] Variable (#12146)
* [Product CRUD] Variable Products
* Handle PR feedback.
* [Product CRUD] Grouped Handling (#12151)
* Handle grouped product saving
* Update routine
* [Product CRUD] Product crud terms (#12149)
* Category and tag id handling
* Replace template functions
* Remove todo
* Handle default name in save function
* Product crud admin save routine (#12174)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Spacing
* Fix comment
* wc_implode_text_attributes helper function
* [Product CRUD] Product crud admin use getters (#12196)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Move settings into new files
* Refactor panels and use getters
* Use getters for variation panel
* Revert save variation changes for now
* Add todos
* Fix downloads
* REST API CRUD Updates
* Additional API updates/fixes. Added some todos
* Fix final failing tests and implementing setters/getters and attributes functionality.
* Fix comparison for is_on_sale and remove download_type from WC_Product.
* Add a wc_get_products wrapper.
* Remove the download type input from the product data metabox for downloadable products. (#12221)
* [Product CRUD] Variations - setters, getters and admin. (#12228)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* Feedback fixes
* Implement CRUD in the legacy REST API
* Handle PR feedback
* [Product CRUD] Getter setter proxy methods (#12236)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* get_prop implementation in abstract and data classes
* Implement set_prop
* Change handling
* Array key exists
* set_object_read
* Use get_the_terms() instead of wp_get_post_terms()
wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not
use the object cache, and generates a database query every time it is used.
get_the_terms() however can use data from the object cache if present.
* [Product CRUD] Variable, variation, notices, and stock handling (#12277)
* No longer needed
* Remove old todos
* Use getters in admin list
* Related and upsells update for CRUD
* Fix notice in gallery
* Variable fixes and todos
* Context
* Price sync
* Revert variation attributes change
* Return parent data in view context
* Defer term counting
* wc_find_matching_product_variation
* Stock manage tweaks
* Stock fixes
* Correct id
* correct id
* Better sync
* Data logic setter fix
* feedback
* Prevent notices
* Handle image_id from parent
* Fix error
* Remove _wc_save_product_price
* Remove todo
* Fixed wrong variation URLs
* Fixed undefined $image_id in WC_Product_Variation::get_image_id()
* Allow wc_rest_prepare_date_response() handle timestamps
* Updated get methods on REST API for variations
* Use variations CRUD to save variations metadata
* [Product CRUD] Abstract todos (#12305)
* Get dimensions and weights, with soft deprecation
* Product attributes
* Ratings
* Fix read method
* Downloads
* Feedback
* Revert "[Product CRUD] Abstract todos (#12305)"
This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2.
* Remove deprecated get_variation_id()
* New default attributes method
* [Product CRUD] Product Datastore (#12317)
* Fix up tests in the product/* folder.
* Handle data store updates for grouped, variable, external, simple, and general data store updates for products.
* Variations & variable changes.
* Update -functions.php calls to use data store.
* Add an interface for the public product data store methods.
* Finished product factory tests
* Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods.
* Fix up delete in all versions of the api
* Handle feedback
* Match protected decloration to parent
* Product crud abstract todos (#12316)
* Get dimensions and weights, with soft deprecation
* Product attributes
* Ratings
* Fix read method
* Downloads
* Feedback
* Fix up store
* Fixed method returning in write context
* Fix error in variation admin
* Check for parent value - fixes tax class
* Remove old/complete todos
* Allow set tax class as "parent"
* Removed duplicated sync
* Fixed wrong variation URLs
* Fixed undefined $image_id in WC_Product_Variation::get_image_id()
* Allow wc_rest_prepare_date_response() handle timestamps
* Updated get methods on REST API for variations
* Use variations CRUD to save variations metadata
* Remove deprecated get_variation_id()
* New default attributes method
* Fixed method returning in write context
* Allow set tax class as "parent"
* Removed duplicated sync
* Fixed coding standards
* TODO is not accurate.
* Should pass WC_Product instancies to WC_Comments methods (#12327)
* Use new method in abstract order class to prevent headers sent issue in tests
* Fixed variable description in REST API
* Updated how create initial product variation
* Fixed a few fatal errors and warnings in Products CRUD (#12329)
* Fixed a few fatal errors and warnings in Products CRUD
* Fixed sync functions
* Add variations CRUD to legacy API (#12331)
* Apply crud to variable products in legacy API v1
* New REST API do not need fallback for default attributes
* Apply variations CRUD to legacy API v2
* Legacy v2 - save default attributes
* Variations in legacy API v2 do not have descriptions
* Fixed legacy API v2 variations params
* Applied variations CRUD to legacy API v3
* Sync before save in legacy apis
* Punc
* Removed API todos
* Removed test
* Products endpoint tweaks (#12354)
* Var type already normalized on CRUD
* Let Product CRUD handle with validation, sanitization and conditional checks
* Set downloads using WC_Product_Download
* Stop try catch exceptions more than one time
* Handle WC_Data_Exception in legacy API
* Complete remove products when fails on creating
* On creating I mean!
* Already have a method to complete delete products
* Fixed standards using WP CodeSniffer
* get_the_terms() returns false when empty
* get_manage_stock returns boolean
@claudiosanches
* Merge conflict
* Variations API endpoint fixes
* Product CRUD improvements (#12359)
* args is not used any more - remove todo
* Added test for attributes
* wc_get_price_excluding_tax usage
* parent usage
* Fix rating counts
* Test fixes
* Cleanup after tests
* Make sure status transition code runs even during API calls, not just in admin.
* Default visibility
* Fix attribute setting in API
* Use get name instead of get title
* variation id usage
* Improved cross sell templates
* variation_data
* Grouped product sync
* Notices
* Sync is not needed in API
* Delete
* Rename interfaces
* Update counts in data store
2016-11-16 12:38:24 +00:00
|
|
|
/**
|
|
|
|
* Get the data store.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function get_data_store() {
|
|
|
|
return $this->data_store;
|
|
|
|
}
|
|
|
|
|
2016-11-17 11:16:07 +00:00
|
|
|
/**
|
|
|
|
* Get the meta type for this data type.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_meta_type() {
|
|
|
|
return $this->meta_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the object ID field this data type.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_object_id_field_for_meta() {
|
|
|
|
return $this->object_id_field_for_meta;
|
|
|
|
}
|
|
|
|
|
2016-03-11 19:50:44 +00:00
|
|
|
/**
|
|
|
|
* Returns the unique ID for this object.
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-09-09 12:34:49 +00:00
|
|
|
public function get_id() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
/**
|
2016-11-14 18:18:08 +00:00
|
|
|
* Delete an object, set the ID to 0, and return result.
|
|
|
|
*
|
|
|
|
* @param bool $force_delete
|
|
|
|
* @return bool result
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
2016-11-08 09:39:47 +00:00
|
|
|
public function delete( $force_delete = false ) {
|
|
|
|
if ( $this->data_store ) {
|
2016-11-15 19:35:10 +00:00
|
|
|
$this->data_store->delete( $this, array( 'force_delete' => $force_delete ) );
|
2016-11-14 18:18:08 +00:00
|
|
|
$this->set_id( 0 );
|
|
|
|
return true;
|
2016-11-08 09:39:47 +00:00
|
|
|
}
|
2016-11-14 18:18:08 +00:00
|
|
|
return false;
|
2016-11-08 09:39:47 +00:00
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save should create or update based on object existance.
|
2016-11-08 10:16:16 +00:00
|
|
|
*
|
|
|
|
* @return int
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
2016-11-08 09:39:47 +00:00
|
|
|
public function save() {
|
|
|
|
if ( $this->data_store ) {
|
|
|
|
if ( $this->get_id() ) {
|
|
|
|
$this->data_store->update( $this );
|
|
|
|
} else {
|
|
|
|
$this->data_store->create( $this );
|
|
|
|
}
|
|
|
|
return $this->get_id();
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
|
2016-03-17 15:22:29 +00:00
|
|
|
/**
|
|
|
|
* Change data to JSON format.
|
|
|
|
* @return string Data in JSON format.
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return json_encode( $this->get_data() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all data for this object.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_data() {
|
2016-09-09 12:34:49 +00:00
|
|
|
return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) );
|
2016-03-17 15:22:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 09:39:47 +00:00
|
|
|
/**
|
|
|
|
* Returns array of expected data keys for this object.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_data_keys() {
|
|
|
|
return array_keys( $this->data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all "extra" data keys for an object (for sub objects like product types).
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_extra_data_keys() {
|
|
|
|
return array_keys( $this->extra_data );
|
|
|
|
}
|
|
|
|
|
2016-08-25 09:32:42 +00:00
|
|
|
/**
|
|
|
|
* Filter null meta values from array.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-25 09:45:50 +00:00
|
|
|
protected function filter_null_meta( $meta ) {
|
|
|
|
return ! is_null( $meta->value );
|
2016-08-25 09:32:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 19:50:44 +00:00
|
|
|
/**
|
2016-03-17 16:38:56 +00:00
|
|
|
* Get All Meta Data.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_meta_data() {
|
2016-09-09 12:34:49 +00:00
|
|
|
return array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal meta keys we don't want exposed as part of meta_data. This is in
|
|
|
|
* addition to all data props with _ prefix.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-06-06 18:52:51 +00:00
|
|
|
* @return array
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
|
|
|
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.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-06-06 18:52:51 +00:00
|
|
|
* @return array
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
|
|
|
protected function get_internal_meta_keys() {
|
2016-09-09 12:34:49 +00:00
|
|
|
return array_merge( array_map( array( $this, 'prefix_key' ), array_keys( $this->data ) ), $this->internal_meta_keys );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Meta Data by Key.
|
2016-11-14 14:20:41 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param bool $single return first found meta with key, or all with $key
|
2016-11-14 14:20:41 +00:00
|
|
|
* @param string $context What the value is for. Valid values are view and edit.
|
2016-03-11 19:50:44 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function get_meta( $key = '', $single = true, $context = 'view' ) {
|
2016-08-25 09:32:42 +00:00
|
|
|
$array_keys = array_keys( wp_list_pluck( $this->get_meta_data(), 'key' ), $key );
|
2016-03-11 19:50:44 +00:00
|
|
|
$value = '';
|
|
|
|
|
2016-06-06 16:24:31 +00:00
|
|
|
if ( ! empty( $array_keys ) ) {
|
2016-03-11 19:50:44 +00:00
|
|
|
if ( $single ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$value = $this->meta_data[ current( $array_keys ) ]->value;
|
2016-03-11 19:50:44 +00:00
|
|
|
} else {
|
2016-09-09 12:34:49 +00:00
|
|
|
$value = array_intersect_key( $this->meta_data, array_flip( $array_keys ) );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
2016-11-14 14:20:41 +00:00
|
|
|
|
|
|
|
if ( 'view' === $context ) {
|
|
|
|
$value = apply_filters( $this->get_hook_prefix() . $key, $value, $this );
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set all meta data from array.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @param array $data Key/Value pairs
|
|
|
|
*/
|
|
|
|
public function set_meta_data( $data ) {
|
|
|
|
if ( ! empty( $data ) && is_array( $data ) ) {
|
2016-03-18 19:18:41 +00:00
|
|
|
foreach ( $data as $meta ) {
|
2016-03-11 19:50:44 +00:00
|
|
|
$meta = (array) $meta;
|
2016-08-22 18:37:34 +00:00
|
|
|
if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[] = (object) array(
|
2016-08-22 19:58:34 +00:00
|
|
|
'id' => $meta['id'],
|
2016-08-22 18:37:34 +00:00
|
|
|
'key' => $meta['key'],
|
|
|
|
'value' => $meta['value'],
|
2016-03-11 19:50:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add meta data.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-06-07 10:06:41 +00:00
|
|
|
* @param string $key Meta key
|
|
|
|
* @param string $value Meta value
|
|
|
|
* @param bool $unique Should this be a unique key?
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
|
|
|
public function add_meta_data( $key, $value, $unique = false ) {
|
|
|
|
if ( $unique ) {
|
2016-08-25 09:45:50 +00:00
|
|
|
$this->delete_meta_data( $key );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[] = (object) array(
|
2016-03-11 19:50:44 +00:00
|
|
|
'key' => $key,
|
|
|
|
'value' => $value,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update meta data by key or ID, if provided.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
* @param int $meta_id
|
|
|
|
*/
|
|
|
|
public function update_meta_data( $key, $value, $meta_id = '' ) {
|
2016-03-18 19:18:41 +00:00
|
|
|
$array_key = '';
|
|
|
|
if ( $meta_id ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$array_key = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id );
|
2016-03-18 19:18:41 +00:00
|
|
|
}
|
|
|
|
if ( $array_key ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[ current( $array_key ) ] = (object) array(
|
2016-08-22 19:58:34 +00:00
|
|
|
'id' => $meta_id,
|
2016-08-22 18:37:34 +00:00
|
|
|
'key' => $key,
|
|
|
|
'value' => $value,
|
2016-03-11 19:50:44 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->add_meta_data( $key, $value, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete meta data.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
* @param array $key Meta key
|
|
|
|
*/
|
|
|
|
public function delete_meta_data( $key ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key );
|
2016-08-25 09:32:42 +00:00
|
|
|
if ( $array_keys ) {
|
|
|
|
foreach ( $array_keys as $array_key ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[ $array_key ]->value = null;
|
2016-08-25 09:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-19 12:43:33 +00:00
|
|
|
* Delete meta data.
|
|
|
|
* @since 2.6.0
|
|
|
|
* @param int $mid Meta ID
|
|
|
|
*/
|
|
|
|
public function delete_meta_data_by_mid( $mid ) {
|
2016-11-17 11:16:07 +00:00
|
|
|
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $mid );
|
2016-08-25 09:32:42 +00:00
|
|
|
if ( $array_keys ) {
|
|
|
|
foreach ( $array_keys as $array_key ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[ $array_key ]->value = null;
|
2016-08-25 09:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-19 12:43:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 14:43:34 +00:00
|
|
|
/**
|
|
|
|
* 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->get_internal_meta_keys() );
|
|
|
|
}
|
|
|
|
|
2016-08-19 12:43:33 +00:00
|
|
|
/**
|
2016-03-11 19:50:44 +00:00
|
|
|
* Read Meta Data from the database. Ignore any internal properties.
|
2016-11-17 11:16:07 +00:00
|
|
|
*
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-11-17 11:16:07 +00:00
|
|
|
* @param bool $force_read True to force a new DB read (and update cache).
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
2016-11-17 11:16:07 +00:00
|
|
|
public function read_meta_data( $force_read = false ) {
|
|
|
|
$this->meta_data = array();
|
2016-03-11 19:50:44 +00:00
|
|
|
$cache_loaded = false;
|
|
|
|
|
|
|
|
if ( ! $this->get_id() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:34:49 +00:00
|
|
|
if ( ! empty( $this->cache_group ) ) {
|
2016-11-17 11:16:07 +00:00
|
|
|
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . 'object_meta_' . $this->get_id();
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
|
2016-11-17 11:16:07 +00:00
|
|
|
if ( ! $force_read ) {
|
|
|
|
if ( ! empty( $this->cache_group ) ) {
|
|
|
|
$cached_meta = wp_cache_get( $cache_key, $this->cache_group );
|
|
|
|
if ( false !== $cached_meta ) {
|
|
|
|
$this->meta_data = $cached_meta;
|
|
|
|
$cache_loaded = true;
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $cache_loaded ) {
|
2016-11-17 11:16:07 +00:00
|
|
|
$raw_meta_data = $this->meta_data_store->read_meta( $this );
|
2016-08-15 12:17:43 +00:00
|
|
|
if ( $raw_meta_data ) {
|
2016-08-31 14:43:34 +00:00
|
|
|
$raw_meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) );
|
2016-08-15 12:17:43 +00:00
|
|
|
foreach ( $raw_meta_data as $meta ) {
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[] = (object) array(
|
2016-11-17 11:16:07 +00:00
|
|
|
'id' => (int) $meta->meta_id,
|
2016-08-22 18:37:34 +00:00
|
|
|
'key' => $meta->meta_key,
|
|
|
|
'value' => maybe_unserialize( $meta->meta_value ),
|
2016-08-15 12:17:43 +00:00
|
|
|
);
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 11:16:07 +00:00
|
|
|
if ( ! empty( $this->cache_group ) ) {
|
|
|
|
wp_cache_set( $cache_key, $this->meta_data, $this->cache_group );
|
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update Meta Data in the database.
|
2016-03-15 22:44:04 +00:00
|
|
|
* @since 2.6.0
|
2016-03-11 19:50:44 +00:00
|
|
|
*/
|
2016-11-09 12:21:18 +00:00
|
|
|
public function save_meta_data() {
|
2016-09-09 12:34:49 +00:00
|
|
|
foreach ( $this->meta_data as $array_key => $meta ) {
|
2016-08-25 09:32:42 +00:00
|
|
|
if ( is_null( $meta->value ) ) {
|
|
|
|
if ( ! empty( $meta->id ) ) {
|
2016-11-17 11:16:07 +00:00
|
|
|
$this->meta_data_store->delete_meta( $this, $meta );
|
2016-08-25 09:32:42 +00:00
|
|
|
}
|
|
|
|
} elseif ( empty( $meta->id ) ) {
|
2016-11-17 11:16:07 +00:00
|
|
|
$new_meta_id = $this->meta_data_store->add_meta( $this, $meta );
|
2016-09-09 12:34:49 +00:00
|
|
|
$this->meta_data[ $array_key ]->id = $new_meta_id;
|
2016-03-11 19:50:44 +00:00
|
|
|
} else {
|
2016-11-17 11:16:07 +00:00
|
|
|
$this->meta_data_store->update_meta( $this, $meta );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:34:49 +00:00
|
|
|
if ( ! empty( $this->cache_group ) ) {
|
|
|
|
WC_Cache_Helper::incr_cache_prefix( $this->cache_group );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 11:16:07 +00:00
|
|
|
$this->read_meta_data( true );
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 12:34:49 +00:00
|
|
|
/**
|
|
|
|
* Set ID.
|
|
|
|
* @param int $id
|
|
|
|
*/
|
|
|
|
public function set_id( $id ) {
|
|
|
|
$this->id = absint( $id );
|
|
|
|
}
|
|
|
|
|
2016-08-24 11:34:19 +00:00
|
|
|
/**
|
|
|
|
* Set all props to default values.
|
|
|
|
*/
|
2016-11-08 09:39:47 +00:00
|
|
|
public function set_defaults() {
|
2016-11-08 10:16:16 +00:00
|
|
|
$this->data = $this->default_data;
|
|
|
|
$this->changes = array();
|
|
|
|
$this->set_object_read( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set object read property.
|
|
|
|
* @param boolean $read
|
|
|
|
*/
|
|
|
|
public function set_object_read( $read = true ) {
|
|
|
|
$this->object_read = (bool) $read;
|
2016-08-24 11:34:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 11:24:46 +00:00
|
|
|
/**
|
|
|
|
* Get object read property.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function get_object_read() {
|
|
|
|
return (bool) $this->object_read;
|
|
|
|
}
|
|
|
|
|
2016-08-25 12:05:27 +00:00
|
|
|
/**
|
|
|
|
* Set a collection of props in one go, collect any errors, and return the result.
|
2016-11-08 10:16:16 +00:00
|
|
|
* Only sets using public methods.
|
2016-11-17 16:02:09 +00:00
|
|
|
*
|
2016-08-25 12:05:27 +00:00
|
|
|
* @param array $props Key value pairs to set. Key is the prop and should map to a setter function name.
|
|
|
|
* @return WP_Error|bool
|
|
|
|
*/
|
WIP - Product CRUD (#12065)
* Created function to get the catalog visibility options
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* Hardcode the get_type per product class
* Initial look through getters and setters and abstract data
* Missing var
* Add related product functions and deprecate those in class.
* No need to exclude ID
* Fixed coding standards and improved the docblocks
* Get cached terms from wc_get_related_terms()
* Fixed wrong variable in wc_get_related_terms
* Use count() instead of sizeof()
* Sanitize ids later
* Remove unneeded comments
* wc_get_product_term_ids instead of related wording and use in other places.
get_the_terms is used here and also handles caching, something
wp_get_post_terms does not.
* Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it.
* Bump template version
* Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function
* Made abstract function useful
* External Product CRUD
* _virtual meta should be 'no', not taxable, in product unit test helper
* Grouped product class
* Tests
* Move children to meta and update test
* Use get_upsell_ids
* Spacing in query
* Moving and refactoring methods
* Availability html
* Tidy/add todos
* Rename method
* Put back review functions (still todo)
* missing $this
* get_price_including_tax/excluding_tax functions
* wc_get_price_to_display
* Price handling
* [Product CRUD] Variable (#12146)
* [Product CRUD] Variable Products
* Handle PR feedback.
* [Product CRUD] Grouped Handling (#12151)
* Handle grouped product saving
* Update routine
* [Product CRUD] Product crud terms (#12149)
* Category and tag id handling
* Replace template functions
* Remove todo
* Handle default name in save function
* Product crud admin save routine (#12174)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Spacing
* Fix comment
* wc_implode_text_attributes helper function
* [Product CRUD] Product crud admin use getters (#12196)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Move settings into new files
* Refactor panels and use getters
* Use getters for variation panel
* Revert save variation changes for now
* Add todos
* Fix downloads
* REST API CRUD Updates
* Additional API updates/fixes. Added some todos
* Fix final failing tests and implementing setters/getters and attributes functionality.
* Fix comparison for is_on_sale and remove download_type from WC_Product.
* Add a wc_get_products wrapper.
* Remove the download type input from the product data metabox for downloadable products. (#12221)
* [Product CRUD] Variations - setters, getters and admin. (#12228)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* Feedback fixes
* Implement CRUD in the legacy REST API
* Handle PR feedback
* [Product CRUD] Getter setter proxy methods (#12236)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* get_prop implementation in abstract and data classes
* Implement set_prop
* Change handling
* Array key exists
* set_object_read
* Use get_the_terms() instead of wp_get_post_terms()
wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not
use the object cache, and generates a database query every time it is used.
get_the_terms() however can use data from the object cache if present.
* Allow WP_Query to preload post data, and meta in wc_get_products()
Allow WP_Query to bulk query for post data and meta if more than
just IDs are requested from wc_get_products(). Reduces query count
significantly.
* [Product CRUD] Variable, variation, notices, and stock handling (#12277)
* No longer needed
* Remove old todos
* Use getters in admin list
* Related and upsells update for CRUD
* Fix notice in gallery
* Variable fixes and todos
* Context
* Price sync
* Revert variation attributes change
* Return parent data in view context
* Defer term counting
* wc_find_matching_product_variation
* Stock manage tweaks
* Stock fixes
* Correct id
* correct id
* Better sync
* Data logic setter fix
* feedback
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* Hardcode the get_type per product class
* Initial look through getters and setters and abstract data
* Missing var
* Fixed coding standards and improved the docblocks
* Get cached terms from wc_get_related_terms()
* Fixed wrong variable in wc_get_related_terms
* Use count() instead of sizeof()
* Add related product functions and deprecate those in class.
* No need to exclude ID
* Sanitize ids later
* Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it.
* Remove unneeded comments
* wc_get_product_term_ids instead of related wording and use in other places.
get_the_terms is used here and also handles caching, something
wp_get_post_terms does not.
* Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function
* External Product CRUD
* _virtual meta should be 'no', not taxable, in product unit test helper
* Bump template version
* Made abstract function useful
* Grouped product class
* Tests
* Move children to meta and update test
* Use get_upsell_ids
* Spacing in query
* Moving and refactoring methods
* Availability html
* Tidy/add todos
* Rename method
* Put back review functions (still todo)
* missing $this
* get_price_including_tax/excluding_tax functions
* wc_get_price_to_display
* Price handling
* [Product CRUD] Variable (#12146)
* [Product CRUD] Variable Products
* Handle PR feedback.
* [Product CRUD] Grouped Handling (#12151)
* Handle grouped product saving
* Update routine
* [Product CRUD] Product crud terms (#12149)
* Category and tag id handling
* Replace template functions
* Remove todo
* Handle default name in save function
* Product crud admin save routine (#12174)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Spacing
* Fix comment
* wc_implode_text_attributes helper function
* [Product CRUD] Product crud admin use getters (#12196)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Move settings into new files
* Refactor panels and use getters
* Use getters for variation panel
* Revert save variation changes for now
* Add todos
* Fix downloads
* REST API CRUD Updates
* Additional API updates/fixes. Added some todos
* Fix final failing tests and implementing setters/getters and attributes functionality.
* Fix comparison for is_on_sale and remove download_type from WC_Product.
* Add a wc_get_products wrapper.
* Remove the download type input from the product data metabox for downloadable products. (#12221)
* [Product CRUD] Variations - setters, getters and admin. (#12228)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* Feedback fixes
* Implement CRUD in the legacy REST API
* Handle PR feedback
* [Product CRUD] Getter setter proxy methods (#12236)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* get_prop implementation in abstract and data classes
* Implement set_prop
* Change handling
* Array key exists
* set_object_read
* Use get_the_terms() instead of wp_get_post_terms()
wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not
use the object cache, and generates a database query every time it is used.
get_the_terms() however can use data from the object cache if present.
* [Product CRUD] Variable, variation, notices, and stock handling (#12277)
* No longer needed
* Remove old todos
* Use getters in admin list
* Related and upsells update for CRUD
* Fix notice in gallery
* Variable fixes and todos
* Context
* Price sync
* Revert variation attributes change
* Return parent data in view context
* Defer term counting
* wc_find_matching_product_variation
* Stock manage tweaks
* Stock fixes
* Correct id
* correct id
* Better sync
* Data logic setter fix
* feedback
* Prevent notices
* Handle image_id from parent
* Fix error
* Remove _wc_save_product_price
* Remove todo
* Fixed wrong variation URLs
* Fixed undefined $image_id in WC_Product_Variation::get_image_id()
* Allow wc_rest_prepare_date_response() handle timestamps
* Updated get methods on REST API for variations
* Use variations CRUD to save variations metadata
* [Product CRUD] Abstract todos (#12305)
* Get dimensions and weights, with soft deprecation
* Product attributes
* Ratings
* Fix read method
* Downloads
* Feedback
* Revert "[Product CRUD] Abstract todos (#12305)"
This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2.
* Remove deprecated get_variation_id()
* New default attributes method
* [Product CRUD] Product Datastore (#12317)
* Fix up tests in the product/* folder.
* Handle data store updates for grouped, variable, external, simple, and general data store updates for products.
* Variations & variable changes.
* Update -functions.php calls to use data store.
* Add an interface for the public product data store methods.
* Finished product factory tests
* Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods.
* Fix up delete in all versions of the api
* Handle feedback
* Match protected decloration to parent
* Product crud abstract todos (#12316)
* Get dimensions and weights, with soft deprecation
* Product attributes
* Ratings
* Fix read method
* Downloads
* Feedback
* Fix up store
* Fixed method returning in write context
* Fix error in variation admin
* Check for parent value - fixes tax class
* Remove old/complete todos
* Allow set tax class as "parent"
* Removed duplicated sync
* Fixed wrong variation URLs
* Fixed undefined $image_id in WC_Product_Variation::get_image_id()
* Allow wc_rest_prepare_date_response() handle timestamps
* Updated get methods on REST API for variations
* Use variations CRUD to save variations metadata
* Remove deprecated get_variation_id()
* New default attributes method
* Fixed method returning in write context
* Allow set tax class as "parent"
* Removed duplicated sync
* Fixed coding standards
* TODO is not accurate.
* Should pass WC_Product instancies to WC_Comments methods (#12327)
* Use new method in abstract order class to prevent headers sent issue in tests
* Fixed variable description in REST API
* Updated how create initial product variation
* Fixed a few fatal errors and warnings in Products CRUD (#12329)
* Fixed a few fatal errors and warnings in Products CRUD
* Fixed sync functions
* Add variations CRUD to legacy API (#12331)
* Apply crud to variable products in legacy API v1
* New REST API do not need fallback for default attributes
* Apply variations CRUD to legacy API v2
* Legacy v2 - save default attributes
* Variations in legacy API v2 do not have descriptions
* Fixed legacy API v2 variations params
* Applied variations CRUD to legacy API v3
* Sync before save in legacy apis
* Punc
* Removed API todos
* Removed test
* Products endpoint tweaks (#12354)
* Var type already normalized on CRUD
* Let Product CRUD handle with validation, sanitization and conditional checks
* Set downloads using WC_Product_Download
* Stop try catch exceptions more than one time
* Handle WC_Data_Exception in legacy API
* Complete remove products when fails on creating
* On creating I mean!
* Already have a method to complete delete products
* Fixed standards using WP CodeSniffer
* get_the_terms() returns false when empty
* get_manage_stock returns boolean
@claudiosanches
* Merge conflict
* Variations API endpoint fixes
* Product CRUD improvements (#12359)
* args is not used any more - remove todo
* Added test for attributes
* wc_get_price_excluding_tax usage
* parent usage
* Fix rating counts
* Test fixes
* Cleanup after tests
* Make sure status transition code runs even during API calls, not just in admin.
* Default visibility
* Fix attribute setting in API
* Use get name instead of get title
* variation id usage
* Improved cross sell templates
* variation_data
* Grouped product sync
* Notices
* Sync is not needed in API
* Delete
* Rename interfaces
* Update counts in data store
2016-11-16 12:38:24 +00:00
|
|
|
public function set_props( $props, $context = 'set' ) {
|
2016-08-25 12:05:27 +00:00
|
|
|
$errors = new WP_Error();
|
|
|
|
|
|
|
|
foreach ( $props as $prop => $value ) {
|
|
|
|
try {
|
2016-08-31 14:43:34 +00:00
|
|
|
if ( 'meta_data' === $prop ) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-25 12:05:27 +00:00
|
|
|
$setter = "set_$prop";
|
|
|
|
if ( ! is_null( $value ) && is_callable( array( $this, $setter ) ) ) {
|
2016-11-08 10:16:16 +00:00
|
|
|
$reflection = new ReflectionMethod( $this, $setter );
|
|
|
|
|
|
|
|
if ( $reflection->isPublic() ) {
|
|
|
|
$this->{$setter}( $value );
|
|
|
|
}
|
2016-08-25 12:05:27 +00:00
|
|
|
}
|
|
|
|
} catch ( WC_Data_Exception $e ) {
|
|
|
|
$errors->add( $e->getErrorCode(), $e->getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sizeof( $errors->get_error_codes() ) ? $errors : true;
|
|
|
|
}
|
|
|
|
|
2016-11-08 10:16:16 +00:00
|
|
|
/**
|
|
|
|
* Sets a prop for a setter method.
|
|
|
|
*
|
|
|
|
* This stores changes in a special array so we can track what needs saving
|
|
|
|
* the the DB later.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $prop Name of prop to set.
|
|
|
|
* @param mixed $value Value of the prop.
|
|
|
|
*/
|
|
|
|
protected function set_prop( $prop, $value ) {
|
|
|
|
if ( array_key_exists( $prop, $this->data ) ) {
|
|
|
|
if ( true === $this->object_read ) {
|
WIP - Product CRUD (#12065)
* Created function to get the catalog visibility options
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* Hardcode the get_type per product class
* Initial look through getters and setters and abstract data
* Missing var
* Add related product functions and deprecate those in class.
* No need to exclude ID
* Fixed coding standards and improved the docblocks
* Get cached terms from wc_get_related_terms()
* Fixed wrong variable in wc_get_related_terms
* Use count() instead of sizeof()
* Sanitize ids later
* Remove unneeded comments
* wc_get_product_term_ids instead of related wording and use in other places.
get_the_terms is used here and also handles caching, something
wp_get_post_terms does not.
* Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it.
* Bump template version
* Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function
* Made abstract function useful
* External Product CRUD
* _virtual meta should be 'no', not taxable, in product unit test helper
* Grouped product class
* Tests
* Move children to meta and update test
* Use get_upsell_ids
* Spacing in query
* Moving and refactoring methods
* Availability html
* Tidy/add todos
* Rename method
* Put back review functions (still todo)
* missing $this
* get_price_including_tax/excluding_tax functions
* wc_get_price_to_display
* Price handling
* [Product CRUD] Variable (#12146)
* [Product CRUD] Variable Products
* Handle PR feedback.
* [Product CRUD] Grouped Handling (#12151)
* Handle grouped product saving
* Update routine
* [Product CRUD] Product crud terms (#12149)
* Category and tag id handling
* Replace template functions
* Remove todo
* Handle default name in save function
* Product crud admin save routine (#12174)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Spacing
* Fix comment
* wc_implode_text_attributes helper function
* [Product CRUD] Product crud admin use getters (#12196)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Move settings into new files
* Refactor panels and use getters
* Use getters for variation panel
* Revert save variation changes for now
* Add todos
* Fix downloads
* REST API CRUD Updates
* Additional API updates/fixes. Added some todos
* Fix final failing tests and implementing setters/getters and attributes functionality.
* Fix comparison for is_on_sale and remove download_type from WC_Product.
* Add a wc_get_products wrapper.
* Remove the download type input from the product data metabox for downloadable products. (#12221)
* [Product CRUD] Variations - setters, getters and admin. (#12228)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* Feedback fixes
* Implement CRUD in the legacy REST API
* Handle PR feedback
* [Product CRUD] Getter setter proxy methods (#12236)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* get_prop implementation in abstract and data classes
* Implement set_prop
* Change handling
* Array key exists
* set_object_read
* Use get_the_terms() instead of wp_get_post_terms()
wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not
use the object cache, and generates a database query every time it is used.
get_the_terms() however can use data from the object cache if present.
* Allow WP_Query to preload post data, and meta in wc_get_products()
Allow WP_Query to bulk query for post data and meta if more than
just IDs are requested from wc_get_products(). Reduces query count
significantly.
* [Product CRUD] Variable, variation, notices, and stock handling (#12277)
* No longer needed
* Remove old todos
* Use getters in admin list
* Related and upsells update for CRUD
* Fix notice in gallery
* Variable fixes and todos
* Context
* Price sync
* Revert variation attributes change
* Return parent data in view context
* Defer term counting
* wc_find_matching_product_variation
* Stock manage tweaks
* Stock fixes
* Correct id
* correct id
* Better sync
* Data logic setter fix
* feedback
* First methods for WP_Product crud
* Product set methods
* Fixed several erros while setting data
* Hardcode the get_type per product class
* Initial look through getters and setters and abstract data
* Missing var
* Fixed coding standards and improved the docblocks
* Get cached terms from wc_get_related_terms()
* Fixed wrong variable in wc_get_related_terms
* Use count() instead of sizeof()
* Add related product functions and deprecate those in class.
* No need to exclude ID
* Sanitize ids later
* Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it.
* Remove unneeded comments
* wc_get_product_term_ids instead of related wording and use in other places.
get_the_terms is used here and also handles caching, something
wp_get_post_terms does not.
* Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function
* External Product CRUD
* _virtual meta should be 'no', not taxable, in product unit test helper
* Bump template version
* Made abstract function useful
* Grouped product class
* Tests
* Move children to meta and update test
* Use get_upsell_ids
* Spacing in query
* Moving and refactoring methods
* Availability html
* Tidy/add todos
* Rename method
* Put back review functions (still todo)
* missing $this
* get_price_including_tax/excluding_tax functions
* wc_get_price_to_display
* Price handling
* [Product CRUD] Variable (#12146)
* [Product CRUD] Variable Products
* Handle PR feedback.
* [Product CRUD] Grouped Handling (#12151)
* Handle grouped product saving
* Update routine
* [Product CRUD] Product crud terms (#12149)
* Category and tag id handling
* Replace template functions
* Remove todo
* Handle default name in save function
* Product crud admin save routine (#12174)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Spacing
* Fix comment
* wc_implode_text_attributes helper function
* [Product CRUD] Product crud admin use getters (#12196)
* Initial props
* Work on admin saving
* Set/get attributes
* Atom was moaning about this before but no longer.
* Update get_shipping_class
* WC_Product_Attribute
* Use getter in admin panel
* Fix attribute saving
* Move settings into new files
* Refactor panels and use getters
* Use getters for variation panel
* Revert save variation changes for now
* Add todos
* Fix downloads
* REST API CRUD Updates
* Additional API updates/fixes. Added some todos
* Fix final failing tests and implementing setters/getters and attributes functionality.
* Fix comparison for is_on_sale and remove download_type from WC_Product.
* Add a wc_get_products wrapper.
* Remove the download type input from the product data metabox for downloadable products. (#12221)
* [Product CRUD] Variations - setters, getters and admin. (#12228)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* Feedback fixes
* Implement CRUD in the legacy REST API
* Handle PR feedback
* [Product CRUD] Getter setter proxy methods (#12236)
* Started on variation changes
* Stock functions
* Variation class
* Bulk change ->id to get_id() to fix variation form display
* Missing status
* Fix add to cart
* Start on stored data save
* save variation
* Save_variations
* Variation edit panel
* Save variations code works.
* Remove stored data code and fix save
* Improve legacy class
* wc_bool_to_string
* prepare_set_attributes
* Use wc_get_products
* More feedback fixes
* get_prop implementation in abstract and data classes
* Implement set_prop
* Change handling
* Array key exists
* set_object_read
* Use get_the_terms() instead of wp_get_post_terms()
wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not
use the object cache, and generates a database query every time it is used.
get_the_terms() however can use data from the object cache if present.
* [Product CRUD] Variable, variation, notices, and stock handling (#12277)
* No longer needed
* Remove old todos
* Use getters in admin list
* Related and upsells update for CRUD
* Fix notice in gallery
* Variable fixes and todos
* Context
* Price sync
* Revert variation attributes change
* Return parent data in view context
* Defer term counting
* wc_find_matching_product_variation
* Stock manage tweaks
* Stock fixes
* Correct id
* correct id
* Better sync
* Data logic setter fix
* feedback
* Prevent notices
* Handle image_id from parent
* Fix error
* Remove _wc_save_product_price
* Remove todo
* Fixed wrong variation URLs
* Fixed undefined $image_id in WC_Product_Variation::get_image_id()
* Allow wc_rest_prepare_date_response() handle timestamps
* Updated get methods on REST API for variations
* Use variations CRUD to save variations metadata
* [Product CRUD] Abstract todos (#12305)
* Get dimensions and weights, with soft deprecation
* Product attributes
* Ratings
* Fix read method
* Downloads
* Feedback
* Revert "[Product CRUD] Abstract todos (#12305)"
This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2.
* Remove deprecated get_variation_id()
* New default attributes method
* [Product CRUD] Product Datastore (#12317)
* Fix up tests in the product/* folder.
* Handle data store updates for grouped, variable, external, simple, and general data store updates for products.
* Variations & variable changes.
* Update -functions.php calls to use data store.
* Add an interface for the public product data store methods.
* Finished product factory tests
* Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods.
* Fix up delete in all versions of the api
* Handle feedback
* Match protected decloration to parent
* Product crud abstract todos (#12316)
* Get dimensions and weights, with soft deprecation
* Product attributes
* Ratings
* Fix read method
* Downloads
* Feedback
* Fix up store
* Fixed method returning in write context
* Fix error in variation admin
* Check for parent value - fixes tax class
* Remove old/complete todos
* Allow set tax class as "parent"
* Removed duplicated sync
* Fixed wrong variation URLs
* Fixed undefined $image_id in WC_Product_Variation::get_image_id()
* Allow wc_rest_prepare_date_response() handle timestamps
* Updated get methods on REST API for variations
* Use variations CRUD to save variations metadata
* Remove deprecated get_variation_id()
* New default attributes method
* Fixed method returning in write context
* Allow set tax class as "parent"
* Removed duplicated sync
* Fixed coding standards
* TODO is not accurate.
* Should pass WC_Product instancies to WC_Comments methods (#12327)
* Use new method in abstract order class to prevent headers sent issue in tests
* Fixed variable description in REST API
* Updated how create initial product variation
* Fixed a few fatal errors and warnings in Products CRUD (#12329)
* Fixed a few fatal errors and warnings in Products CRUD
* Fixed sync functions
* Add variations CRUD to legacy API (#12331)
* Apply crud to variable products in legacy API v1
* New REST API do not need fallback for default attributes
* Apply variations CRUD to legacy API v2
* Legacy v2 - save default attributes
* Variations in legacy API v2 do not have descriptions
* Fixed legacy API v2 variations params
* Applied variations CRUD to legacy API v3
* Sync before save in legacy apis
* Punc
* Removed API todos
* Removed test
* Products endpoint tweaks (#12354)
* Var type already normalized on CRUD
* Let Product CRUD handle with validation, sanitization and conditional checks
* Set downloads using WC_Product_Download
* Stop try catch exceptions more than one time
* Handle WC_Data_Exception in legacy API
* Complete remove products when fails on creating
* On creating I mean!
* Already have a method to complete delete products
* Fixed standards using WP CodeSniffer
* get_the_terms() returns false when empty
* get_manage_stock returns boolean
@claudiosanches
* Merge conflict
* Variations API endpoint fixes
* Product CRUD improvements (#12359)
* args is not used any more - remove todo
* Added test for attributes
* wc_get_price_excluding_tax usage
* parent usage
* Fix rating counts
* Test fixes
* Cleanup after tests
* Make sure status transition code runs even during API calls, not just in admin.
* Default visibility
* Fix attribute setting in API
* Use get name instead of get title
* variation id usage
* Improved cross sell templates
* variation_data
* Grouped product sync
* Notices
* Sync is not needed in API
* Delete
* Rename interfaces
* Update counts in data store
2016-11-16 12:38:24 +00:00
|
|
|
if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) {
|
|
|
|
$this->changes[ $prop ] = $value;
|
|
|
|
}
|
2016-11-08 10:16:16 +00:00
|
|
|
} else {
|
|
|
|
$this->data[ $prop ] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return data changes only.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-11-09 12:21:18 +00:00
|
|
|
public function get_changes() {
|
2016-11-08 10:16:16 +00:00
|
|
|
return $this->changes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge changes with data and clear.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
2016-11-14 14:20:41 +00:00
|
|
|
public function apply_changes() {
|
2016-11-08 10:16:16 +00:00
|
|
|
$this->data = array_merge( $this->data, $this->changes );
|
|
|
|
$this->changes = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prefix for action and filter hooks on data.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_hook_prefix() {
|
|
|
|
return 'woocommerce_get_';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a prop for a getter 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
|
|
|
|
* @param string $prop Name of prop to get.
|
|
|
|
* @param string $context What the value is for. Valid values are view and edit.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-11-17 15:02:33 +00:00
|
|
|
protected function get_prop( $prop, $context = 'view' ) {
|
2016-11-08 10:16:16 +00:00
|
|
|
$value = null;
|
|
|
|
|
|
|
|
if ( array_key_exists( $prop, $this->data ) ) {
|
|
|
|
$value = isset( $this->changes[ $prop ] ) ? $this->changes[ $prop ] : $this->data[ $prop ];
|
|
|
|
|
|
|
|
if ( 'view' === $context ) {
|
|
|
|
$value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2016-08-17 10:44:56 +00:00
|
|
|
/**
|
2016-08-24 13:37:19 +00:00
|
|
|
* When invalid data is found, throw an exception unless reading from the DB.
|
2016-08-24 14:26:35 +00:00
|
|
|
* @param string $error_code Error code.
|
2016-08-25 12:05:27 +00:00
|
|
|
* @param string $error_message Error message.
|
2016-08-24 09:46:29 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-17 10:44:56 +00:00
|
|
|
*/
|
2016-08-25 12:05:27 +00:00
|
|
|
protected function error( $error_code, $error_message ) {
|
|
|
|
throw new WC_Data_Exception( $error_code, $error_message );
|
2016-08-17 10:44:56 +00:00
|
|
|
}
|
2016-03-11 19:50:44 +00:00
|
|
|
}
|