2013-08-09 16:11:15 +00:00
< ? php
2015-11-06 09:22:19 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-08-09 16:11:15 +00:00
/**
2015-11-03 13:53:50 +00:00
* Comments
2013-08-09 16:11:15 +00:00
*
2015-11-03 13:31:20 +00:00
* Handle comments ( reviews and order notes ) .
2013-08-09 16:11:15 +00:00
*
2015-02-04 15:59:42 +00:00
* @ class WC_Comments
2015-01-28 17:09:37 +00:00
* @ version 2.3 . 0
* @ package WooCommerce / Classes / Products
* @ category Class
* @ author WooThemes
2013-08-09 16:11:15 +00:00
*/
class WC_Comments {
/**
2015-02-04 15:59:42 +00:00
* Hook in methods .
2013-08-09 16:11:15 +00:00
*/
2014-05-28 13:52:50 +00:00
public static function init () {
2013-08-09 16:11:15 +00:00
// Rating posts
2014-05-28 13:52:50 +00:00
add_filter ( 'preprocess_comment' , array ( __CLASS__ , 'check_comment_rating' ), 0 );
add_action ( 'comment_post' , array ( __CLASS__ , 'add_comment_rating' ), 1 );
2015-05-13 14:47:29 +00:00
add_action ( 'comment_moderation_recipients' , array ( __CLASS__ , 'comment_moderation_recipients' ), 10 , 2 );
2013-08-09 16:11:15 +00:00
2015-02-04 15:59:42 +00:00
// Clear transients
2014-07-03 11:01:51 +00:00
add_action ( 'wp_update_comment_count' , array ( __CLASS__ , 'clear_transients' ) );
2013-08-09 16:11:15 +00:00
// Secure order notes
2013-11-10 19:01:12 +00:00
add_filter ( 'comments_clauses' , array ( __CLASS__ , 'exclude_order_comments' ), 10 , 1 );
2014-05-28 13:52:50 +00:00
add_action ( 'comment_feed_join' , array ( __CLASS__ , 'exclude_order_comments_from_feed_join' ) );
add_action ( 'comment_feed_where' , array ( __CLASS__ , 'exclude_order_comments_from_feed_where' ) );
2014-07-30 20:23:41 +00:00
2014-08-12 15:13:29 +00:00
// Secure webhook comments
2014-07-30 20:23:41 +00:00
add_filter ( 'comments_clauses' , array ( __CLASS__ , 'exclude_webhook_comments' ), 10 , 1 );
add_action ( 'comment_feed_join' , array ( __CLASS__ , 'exclude_webhook_comments_from_feed_join' ) );
add_action ( 'comment_feed_where' , array ( __CLASS__ , 'exclude_webhook_comments_from_feed_where' ) );
2014-08-12 15:13:29 +00:00
// Count comments
add_filter ( 'wp_count_comments' , array ( __CLASS__ , 'wp_count_comments' ), 10 , 2 );
2015-01-20 05:26:33 +00:00
2016-07-15 13:01:21 +00:00
// Delete comments count cache whenever there is a new comment or a comment status changes
add_action ( 'wp_insert_comment' , array ( __CLASS__ , 'delete_comments_count_cache' ) );
add_action ( 'wp_set_comment_status' , array ( __CLASS__ , 'delete_comments_count_cache' ) );
2015-02-04 15:59:42 +00:00
// Support avatars for `review` comment type
2015-01-20 05:26:33 +00:00
add_filter ( 'get_avatar_comment_types' , array ( __CLASS__ , 'add_avatar_for_review_comment_type' ) );
2015-10-07 19:42:26 +00:00
// Review of verified purchase
add_action ( 'comment_post' , array ( __CLASS__ , 'add_comment_purchase_verification' ) );
2013-08-09 16:11:15 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Exclude order comments from queries and RSS .
2013-08-09 16:11:15 +00:00
*
2015-11-03 13:31:20 +00:00
* This code should exclude shop_order comments from queries . Some queries ( like the recent comments widget on the dashboard ) are hardcoded .
* and are not filtered , however , the code current_user_can ( 'read_post' , $comment -> comment_post_ID ) should keep them safe since only admin and .
2013-08-09 16:11:15 +00:00
* shop managers can view orders anyway .
*
2013-11-04 06:34:21 +00:00
* The frontend view order pages get around this filter by using remove_filter ( 'comments_clauses' , array ( 'WC_Comments' , 'exclude_order_comments' ), 10 , 1 );
2015-02-04 15:59:42 +00:00
* @ param array $clauses
2013-08-09 16:11:15 +00:00
* @ return array
*/
2013-11-04 06:34:21 +00:00
public static function exclude_order_comments ( $clauses ) {
2014-06-08 20:33:11 +00:00
global $wpdb , $typenow ;
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
if ( is_admin () && in_array ( $typenow , wc_get_order_types () ) && current_user_can ( 'manage_woocommerce' ) ) {
2013-08-09 16:11:15 +00:00
return $clauses ; // Don't hide when viewing orders in admin
2014-08-12 15:07:23 +00:00
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
if ( ! $clauses [ 'join' ] ) {
2013-08-09 16:11:15 +00:00
$clauses [ 'join' ] = '' ;
2014-08-12 15:07:23 +00:00
}
2013-08-09 16:11:15 +00:00
2016-05-25 14:43:18 +00:00
if ( ! stristr ( $clauses [ 'join' ], " JOIN $wpdb->posts ON " ) ) {
2013-08-09 16:11:15 +00:00
$clauses [ 'join' ] .= " LEFT JOIN $wpdb->posts ON comment_post_ID = $wpdb->posts .ID " ;
2014-08-12 15:07:23 +00:00
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
if ( $clauses [ 'where' ] ) {
2013-08-09 16:11:15 +00:00
$clauses [ 'where' ] .= ' AND ' ;
2014-08-12 15:07:23 +00:00
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:05:09 +00:00
$clauses [ 'where' ] .= " $wpdb->posts .post_type NOT IN (' " . implode ( " ',' " , wc_get_order_types () ) . " ') " ;
2013-08-09 16:11:15 +00:00
return $clauses ;
}
/**
2015-11-03 13:31:20 +00:00
* Exclude order comments from queries and RSS .
2015-02-04 15:59:42 +00:00
* @ param string $join
2013-08-09 16:11:15 +00:00
* @ return string
*/
2014-05-28 13:52:50 +00:00
public static function exclude_order_comments_from_feed_join ( $join ) {
2013-08-09 16:11:15 +00:00
global $wpdb ;
2016-05-25 14:43:18 +00:00
if ( ! stristr ( $join , " JOIN $wpdb->posts ON " ) ) {
2014-08-12 15:07:23 +00:00
$join = " LEFT JOIN $wpdb->posts ON $wpdb->comments .comment_post_ID = $wpdb->posts .ID " ;
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
return $join ;
2013-08-09 16:11:15 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Exclude order comments from queries and RSS .
2015-02-04 15:59:42 +00:00
* @ param string $where
2013-08-09 16:11:15 +00:00
* @ return string
*/
2014-05-28 13:52:50 +00:00
public static function exclude_order_comments_from_feed_where ( $where ) {
2013-08-09 16:11:15 +00:00
global $wpdb ;
2014-08-12 15:07:23 +00:00
if ( $where ) {
$where .= ' AND ' ;
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:05:09 +00:00
$where .= " $wpdb->posts .post_type NOT IN (' " . implode ( " ',' " , wc_get_order_types () ) . " ') " ;
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
return $where ;
2013-08-09 16:11:15 +00:00
}
2014-07-30 20:23:41 +00:00
/**
2015-11-03 13:31:20 +00:00
* Exclude webhook comments from queries and RSS .
2015-02-04 15:59:42 +00:00
* @ since 2.2
* @ param array $clauses
2014-07-30 20:23:41 +00:00
* @ return array
*/
public static function exclude_webhook_comments ( $clauses ) {
global $wpdb ;
if ( ! $clauses [ 'join' ] ) {
$clauses [ 'join' ] = '' ;
}
if ( ! strstr ( $clauses [ 'join' ], " JOIN $wpdb->posts " ) ) {
$clauses [ 'join' ] .= " LEFT JOIN $wpdb->posts ON comment_post_ID = $wpdb->posts .ID " ;
}
if ( $clauses [ 'where' ] ) {
$clauses [ 'where' ] .= ' AND ' ;
}
$clauses [ 'where' ] .= " $wpdb->posts .post_type <> 'shop_webhook' " ;
return $clauses ;
}
/**
2015-11-03 13:31:20 +00:00
* Exclude webhook comments from queries and RSS .
2015-02-04 15:59:42 +00:00
* @ since 2.2
* @ param string $join
2014-07-30 20:23:41 +00:00
* @ return string
*/
public static function exclude_webhook_comments_from_feed_join ( $join ) {
global $wpdb ;
2014-08-12 15:07:23 +00:00
if ( ! strstr ( $join , $wpdb -> posts ) ) {
2014-07-30 20:23:41 +00:00
$join = " LEFT JOIN $wpdb->posts ON $wpdb->comments .comment_post_ID = $wpdb->posts .ID " ;
2014-08-12 15:07:23 +00:00
}
2014-07-30 20:23:41 +00:00
return $join ;
}
/**
2015-11-03 13:31:20 +00:00
* Exclude webhook comments from queries and RSS .
2015-02-04 15:59:42 +00:00
* @ since 2.1
* @ param string $where
2014-07-30 20:23:41 +00:00
* @ return string
*/
public static function exclude_webhook_comments_from_feed_where ( $where ) {
global $wpdb ;
2014-08-12 15:07:23 +00:00
if ( $where ) {
2014-07-30 20:23:41 +00:00
$where .= ' AND ' ;
2014-08-12 15:07:23 +00:00
}
2014-07-30 20:23:41 +00:00
$where .= " $wpdb->posts .post_type <> 'shop_webhook' " ;
return $where ;
}
2013-08-09 16:11:15 +00:00
/**
* Validate the comment ratings .
2015-02-04 15:59:42 +00:00
* @ param array $comment_data
2013-08-09 16:11:15 +00:00
* @ return array
*/
2014-05-28 13:52:50 +00:00
public static function check_comment_rating ( $comment_data ) {
2013-08-09 16:11:15 +00:00
// If posting a comment (not trackback etc) and not logged in
2015-06-03 19:57:36 +00:00
if (
! is_admin ()
&& 'product' === get_post_type ( $_POST [ 'comment_post_ID' ] )
&& empty ( $_POST [ 'rating' ] )
&& '' === $comment_data [ 'comment_type' ]
&& 'yes' === get_option ( 'woocommerce_enable_review_rating' )
&& 'yes' === get_option ( 'woocommerce_review_rating_required' )
) {
2013-08-09 16:11:15 +00:00
wp_die ( __ ( 'Please rate the product.' , 'woocommerce' ) );
exit ;
}
2015-01-28 17:09:37 +00:00
2013-08-09 16:11:15 +00:00
return $comment_data ;
}
/**
* Rating field for comments .
2015-01-28 17:09:37 +00:00
* @ param int $comment_id
2013-08-09 16:11:15 +00:00
*/
2014-05-28 13:52:50 +00:00
public static function add_comment_rating ( $comment_id ) {
2015-02-11 23:06:18 +00:00
if ( isset ( $_POST [ 'rating' ] ) && 'product' === get_post_type ( $_POST [ 'comment_post_ID' ] ) ) {
2014-07-03 11:01:51 +00:00
if ( ! $_POST [ 'rating' ] || $_POST [ 'rating' ] > 5 || $_POST [ 'rating' ] < 0 ) {
2013-08-09 16:11:15 +00:00
return ;
2014-07-03 11:01:51 +00:00
}
2013-08-09 16:11:15 +00:00
add_comment_meta ( $comment_id , 'rating' , ( int ) esc_attr ( $_POST [ 'rating' ] ), true );
}
}
2015-05-13 14:47:29 +00:00
/**
* Modify recipient of review email .
2016-01-06 14:49:24 +00:00
* @ param array $emails
* @ param int $comment_id
2015-05-13 14:47:29 +00:00
* @ return array
*/
public static function comment_moderation_recipients ( $emails , $comment_id ) {
$comment = get_comment ( $comment_id );
if ( $comment && 'product' === get_post_type ( $comment -> comment_post_ID ) ) {
$emails = array ( get_option ( 'admin_email' ) );
}
return $emails ;
}
2013-08-09 16:11:15 +00:00
/**
2015-11-13 20:41:20 +00:00
* Ensure product average rating and review count is kept up to date .
2015-01-28 17:09:37 +00:00
* @ param int $post_id
2013-08-09 16:11:15 +00:00
*/
2014-07-03 11:01:51 +00:00
public static function clear_transients ( $post_id ) {
2016-09-08 16:36:56 +00:00
if ( 'product' === get_post_type ( $post_id ) ) {
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
$product = wc_get_product ( $post_id );
self :: get_rating_counts_for_product ( $product );
self :: get_average_rating_for_product ( $product );
self :: get_review_count_for_product ( $product );
2016-09-08 16:36:56 +00:00
}
2013-08-09 16:11:15 +00:00
}
2014-08-12 15:13:29 +00:00
2016-07-15 13:01:21 +00:00
/**
* Delete comments count cache whenever there is
* new comment or the status of a comment changes . Cache
* will be regenerated next time WC_Comments :: wp_count_comments ()
* is called .
*
* @ return void
*/
public static function delete_comments_count_cache () {
delete_transient ( 'wc_count_comments' );
}
2014-08-12 15:13:29 +00:00
/**
2015-11-03 13:31:20 +00:00
* Remove order notes from wp_count_comments () .
2015-02-04 15:59:42 +00:00
* @ since 2.2
* @ param object $stats
* @ param int $post_id
2014-08-12 15:13:29 +00:00
* @ return object
*/
public static function wp_count_comments ( $stats , $post_id ) {
global $wpdb ;
if ( 0 === $post_id ) {
2016-07-15 13:01:21 +00:00
$stats = get_transient ( 'wc_count_comments' );
2014-08-12 15:13:29 +00:00
2016-07-15 13:01:21 +00:00
if ( ! $stats ) {
$stats = array ();
2014-08-12 15:13:29 +00:00
2016-07-15 13:01:21 +00:00
$count = $wpdb -> get_results ( " SELECT comment_approved, COUNT( * ) AS num_comments FROM { $wpdb -> comments } WHERE comment_type != 'order_note' GROUP BY comment_approved " , ARRAY_A );
2014-08-12 15:13:29 +00:00
2016-07-15 13:01:21 +00:00
$total = 0 ;
$approved = array ( '0' => 'moderated' , '1' => 'approved' , 'spam' => 'spam' , 'trash' => 'trash' , 'post-trashed' => 'post-trashed' );
2014-08-12 15:13:29 +00:00
2016-07-15 13:01:21 +00:00
foreach ( ( array ) $count as $row ) {
// Don't count post-trashed toward totals
if ( 'post-trashed' != $row [ 'comment_approved' ] && 'trash' != $row [ 'comment_approved' ] ) {
$total += $row [ 'num_comments' ];
}
if ( isset ( $approved [ $row [ 'comment_approved' ] ] ) ) {
$stats [ $approved [ $row [ 'comment_approved' ] ] ] = $row [ 'num_comments' ];
}
2014-08-12 15:13:29 +00:00
}
2016-07-15 13:01:21 +00:00
$stats [ 'total_comments' ] = $total ;
$stats [ 'all' ] = $total ;
foreach ( $approved as $key ) {
if ( empty ( $stats [ $key ] ) ) {
$stats [ $key ] = 0 ;
}
2014-08-12 15:13:29 +00:00
}
2016-07-15 13:01:21 +00:00
$stats = ( object ) $stats ;
set_transient ( 'wc_count_comments' , $stats );
}
2014-08-12 15:13:29 +00:00
}
return $stats ;
}
2015-01-20 05:26:33 +00:00
/**
2015-11-03 13:31:20 +00:00
* Make sure WP displays avatars for comments with the `review` type .
2015-02-04 15:59:42 +00:00
* @ since 2.3
* @ param array $comment_types
2015-01-20 05:26:33 +00:00
* @ return array
*/
public static function add_avatar_for_review_comment_type ( $comment_types ) {
return array_merge ( $comment_types , array ( 'review' ) );
}
2015-10-07 19:42:26 +00:00
/**
* Determine if a review is from a verified owner at submission .
* @ param int $comment_id
2015-10-09 16:21:40 +00:00
* @ return bool
2015-10-07 19:42:26 +00:00
*/
public static function add_comment_purchase_verification ( $comment_id ) {
2015-10-09 16:21:40 +00:00
$comment = get_comment ( $comment_id );
$verified = false ;
2015-10-07 19:42:26 +00:00
if ( 'product' === get_post_type ( $comment -> comment_post_ID ) ) {
$verified = wc_customer_bought_product ( $comment -> comment_author_email , $comment -> user_id , $comment -> comment_post_ID );
add_comment_meta ( $comment_id , 'verified' , ( int ) $verified , true );
}
2015-10-09 16:21:40 +00:00
return $verified ;
2015-10-07 19:42:26 +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 product rating for a product . Please note this is not cached .
*
* @ since 2.7 . 0
* @ param WC_Product $product
* @ return float
*/
public static function get_average_rating_for_product ( & $product ) {
global $wpdb ;
$count = $product -> get_rating_count ();
if ( $count ) {
$ratings = $wpdb -> get_var ( $wpdb -> prepare ( "
SELECT SUM ( meta_value ) FROM $wpdb -> commentmeta
LEFT JOIN $wpdb -> comments ON $wpdb -> commentmeta . comment_id = $wpdb -> comments . comment_ID
WHERE meta_key = 'rating'
AND comment_post_ID = % d
AND comment_approved = '1'
AND meta_value > 0
" , $product->get_id () ) );
$average = number_format ( $ratings / $count , 2 , '.' , '' );
} else {
$average = 0 ;
}
$product -> set_average_rating ( $average );
$data_store = $product -> get_data_store ();
$data_store -> update_average_rating ( $product );
return $average ;
}
/**
* Get product review count for a product ( not replies ) . Please note this is not cached .
*
* @ since 2.7 . 0
* @ param WC_Product $product
* @ return int
*/
public static function get_review_count_for_product ( & $product ) {
global $wpdb ;
$count = $wpdb -> get_var ( $wpdb -> prepare ( "
SELECT COUNT ( * ) FROM $wpdb -> comments
WHERE comment_parent = 0
AND comment_post_ID = % d
AND comment_approved = '1'
" , $product->get_id () ) );
$product -> set_review_count ( $count );
$data_store = $product -> get_data_store ();
$data_store -> update_review_count ( $product );
return $count ;
}
/**
* Get product rating count for a product . Please note this is not cached .
*
* @ since 2.7 . 0
* @ param WC_Product $product
* @ return array of integers
*/
public static function get_rating_counts_for_product ( & $product ) {
global $wpdb ;
$counts = array ();
$raw_counts = $wpdb -> get_results ( $wpdb -> prepare ( "
SELECT meta_value , COUNT ( * ) as meta_value_count FROM $wpdb -> commentmeta
LEFT JOIN $wpdb -> comments ON $wpdb -> commentmeta . comment_id = $wpdb -> comments . comment_ID
WHERE meta_key = 'rating'
AND comment_post_ID = % d
AND comment_approved = '1'
AND meta_value > 0
GROUP BY meta_value
" , $product->get_id () ) );
foreach ( $raw_counts as $count ) {
$counts [ $count -> meta_value ] = absint ( $count -> meta_value_count );
}
$product -> set_rating_counts ( $counts );
$data_store = $product -> get_data_store ();
$data_store -> update_rating_counts ( $product );
return $counts ;
}
2013-08-09 16:11:15 +00:00
}
2014-05-28 13:52:50 +00:00
WC_Comments :: init ();