woocommerce/includes/admin/meta-boxes/class-wc-meta-box-product-r...

61 lines
1.5 KiB
PHP
Raw Normal View History

2016-12-12 13:00:08 +00:00
<?php
/**
* Product Reviews
*
* Functions for displaying product reviews data meta box.
*
2020-08-05 16:36:24 +00:00
* @package WooCommerce\Admin\Meta Boxes
2016-12-12 13:00:08 +00:00
*/
2018-05-01 12:00:49 +00:00
defined( 'ABSPATH' ) || exit;
2016-12-12 13:00:08 +00:00
/**
* WC_Meta_Box_Product_Reviews
*/
class WC_Meta_Box_Product_Reviews {
/**
2018-05-01 12:00:49 +00:00
* Output the metabox.
*
2018-05-01 12:00:49 +00:00
* @param object $comment Comment being shown.
2016-12-12 13:00:08 +00:00
*/
public static function output( $comment ) {
wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
$current = get_comment_meta( $comment->comment_ID, 'rating', true );
?>
<select name="rating" id="rating">
<?php
for ( $rating = 1; $rating <= 5; $rating ++ ) {
2018-05-01 12:00:49 +00:00
printf( '<option value="%1$s"%2$s>%1$s</option>', $rating, selected( $current, $rating, false ) ); // WPCS: XSS ok.
}
?>
2016-12-12 13:00:08 +00:00
</select>
<?php
}
/**
* Save meta box data
*
2018-05-01 12:00:49 +00:00
* @param mixed $data Data to save.
* @return mixed
2016-12-12 13:00:08 +00:00
*/
public static function save( $data ) {
2018-05-01 12:00:49 +00:00
// Not allowed, return regular value without updating meta.
if ( ! isset( $_POST['woocommerce_meta_nonce'], $_POST['rating'] ) || ! wp_verify_nonce( wp_unslash( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) { // WPCS: input var ok, sanitization ok.
return $data;
}
2016-12-12 13:00:08 +00:00
2018-05-01 12:00:49 +00:00
if ( $_POST['rating'] > 5 || $_POST['rating'] < 0 ) { // WPCS: input var ok.
return $data;
}
$comment_id = $data['comment_ID'];
2018-05-01 12:00:49 +00:00
update_comment_meta( $comment_id, 'rating', intval( wp_unslash( $_POST['rating'] ) ) ); // WPCS: input var ok.
2016-12-12 13:00:08 +00:00
2018-05-01 12:00:49 +00:00
// Return regular value after updating.
return $data;
2016-12-12 13:00:08 +00:00
}
}