Date getters and setters

This commit is contained in:
Mike Jolley 2017-03-13 19:52:44 +00:00
parent e5c753e757
commit 1bac579df7
4 changed files with 52 additions and 51 deletions

View File

@ -47,8 +47,8 @@ class WC_Product extends WC_Abstract_Legacy_Product {
protected $data = array(
'name' => '',
'slug' => '',
'date_created' => '',
'date_modified' => '',
'date_created' => null,
'date_modified' => null,
'status' => false,
'featured' => false,
'catalog_visibility' => 'visible',
@ -58,8 +58,8 @@ class WC_Product extends WC_Abstract_Legacy_Product {
'price' => '',
'regular_price' => '',
'sale_price' => '',
'date_on_sale_from' => '',
'date_on_sale_to' => '',
'date_on_sale_from' => null,
'date_on_sale_to' => null,
'total_sales' => '0',
'tax_status' => 'taxable',
'tax_class' => '',
@ -174,7 +174,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
*
* @since 2.7.0
* @param string $context
* @return string Timestamp.
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_created( $context = 'view' ) {
return $this->get_prop( 'date_created', $context );
@ -185,7 +185,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
*
* @since 2.7.0
* @param string $context
* @return string Timestamp.
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_modified( $context = 'view' ) {
return $this->get_prop( 'date_modified', $context );
@ -291,7 +291,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
*
* @since 2.7.0
* @param string $context
* @return string
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_on_sale_from( $context = 'view' ) {
return $this->get_prop( 'date_on_sale_from', $context );
@ -302,7 +302,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
*
* @since 2.7.0
* @param string $context
* @return string
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_on_sale_to( $context = 'view' ) {
return $this->get_prop( 'date_on_sale_to', $context );
@ -708,20 +708,20 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* Set product created date.
*
* @since 2.7.0
* @param string $timestamp Timestamp.
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
*/
public function set_date_created( $timestamp ) {
$this->set_prop( 'date_created', is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ) );
public function set_date_created( $date = null ) {
$this->set_date_prop( 'date_created', $date );
}
/**
* Set product modified date.
*
* @since 2.7.0
* @param string $timestamp Timestamp.
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
*/
public function set_date_modified( $timestamp ) {
$this->set_prop( 'date_modified', is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ) );
public function set_date_modified( $date = null ) {
$this->set_date_prop( 'date_modified', $date );
}
/**
@ -829,20 +829,20 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* Set date on sale from.
*
* @since 2.7.0
* @param string $timestamp Sale from date.
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
*/
public function set_date_on_sale_from( $timestamp ) {
$this->set_prop( 'date_on_sale_from', is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ) );
public function set_date_on_sale_from( $date = null ) {
$this->set_date_prop( 'date_on_sale_from', $date );
}
/**
* Set date on sale to.
*
* @since 2.7.0
* @param string $timestamp Sale to date.
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
*/
public function set_date_on_sale_to( $timestamp ) {
$this->set_prop( 'date_on_sale_to', is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ) );
public function set_date_on_sale_to( $date = null ) {
$this->set_date_prop( 'date_on_sale_to', $date );
}
/**
@ -1409,17 +1409,16 @@ class WC_Product extends WC_Abstract_Legacy_Product {
if ( '' !== (string) $this->get_sale_price( $context ) && $this->get_regular_price( $context ) > $this->get_sale_price( $context ) ) {
$on_sale = true;
if ( '' !== (string) $this->get_date_on_sale_from( $context ) && $this->get_date_on_sale_from( $context ) > strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
if ( $this->get_date_on_sale_from( $context ) && $this->get_date_on_sale_from( $context )->getTimestamp() > time() ) {
$on_sale = false;
}
if ( '' !== (string) $this->get_date_on_sale_to( $context ) && $this->get_date_on_sale_to( $context ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
if ( $this->get_date_on_sale_to( $context ) && $this->get_date_on_sale_to( $context )->getTimestamp() < time() ) {
$on_sale = false;
}
} else {
$on_sale = false;
}
return 'view' === $context ? apply_filters( 'woocommerce_product_is_on_sale', $on_sale, $this ) : $on_sale;
}

View File

@ -77,7 +77,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
*/
public function create( &$product ) {
if ( ! $product->get_date_created() ) {
$product->set_date_created( current_time( 'timestamp' ) );
$product->set_date_created( current_time( 'timestamp', true ) );
}
$id = wp_insert_post( apply_filters( 'woocommerce_new_product_data', array(
@ -91,8 +91,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
'comment_status' => $product->get_reviews_allowed() ? 'open' : 'closed',
'ping_status' => 'closed',
'menu_order' => $product->get_menu_order(),
'post_date' => date( 'Y-m-d H:i:s', $product->get_date_created() ),
'post_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', $product->get_date_created() ) ),
'post_date' => date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getOffsetTimestamp() ),
'post_date_gmt' => date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getTimestamp() ),
) ), true );
if ( $id && ! is_wp_error( $id ) ) {
@ -130,8 +130,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$product->set_props( array(
'name' => $post_object->post_title,
'slug' => $post_object->post_name,
'date_created' => $post_object->post_date,
'date_modified' => $post_object->post_modified,
'date_created' => strtotime( $post_object->post_date_gmt ),
'date_modified' => strtotime( $post_object->post_modified_gmt ),
'status' => $post_object->post_status,
'description' => $post_object->post_content,
'short_description' => $post_object->post_excerpt,
@ -159,7 +159,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
// Only update the post when the post data changes.
if ( array_intersect( array( 'description', 'short_description', 'name', 'parent_id', 'reviews_allowed', 'status', 'menu_order', 'date_created', 'date_modified' ), array_keys( $changes ) ) ) {
wp_update_post( array(
'ID' => $product->get_id(),
'ID' => $product->get_id(),
'post_content' => $product->get_description( 'edit' ),
'post_excerpt' => $product->get_short_description( 'edit' ),
'post_title' => $product->get_name( 'edit' ),
@ -167,10 +167,10 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
'comment_status' => $product->get_reviews_allowed( 'edit' ) ? 'open' : 'closed',
'post_status' => $product->get_status( 'edit' ) ? $product->get_status( 'edit' ) : 'publish',
'menu_order' => $product->get_menu_order( 'edit' ),
'post_date' => date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' ) ),
'post_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' ) ) ),
'post_modified' => isset( $changes['date_modified'] ) ? date( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' ) ) : current_time( 'mysql' ),
'post_modified_gmt' => isset( $changes['date_modified'] ) ? get_gmt_from_date( date( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' ) ) ) : current_time( 'mysql', 1 ),
'post_date' => date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getOffsetTimestamp() ),
'post_date_gmt' => date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getTimestamp() ),
'post_modified' => isset( $changes['date_modified'] ) ? date( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' )->getOffsetTimestamp() ) : current_time( 'mysql' ),
'post_modified_gmt' => isset( $changes['date_modified'] ) ? date( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' )->getTimestamp() ) : current_time( 'mysql', 1 ),
) );
}
@ -811,7 +811,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
AND postmeta.meta_value > 0
AND postmeta.meta_value < %s
AND postmeta_2.meta_value != postmeta_3.meta_value
", current_time( 'timestamp' ) ) );
", current_time( 'timestamp', true ) ) );
}
/**
@ -832,7 +832,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
AND postmeta.meta_value > 0
AND postmeta.meta_value < %s
AND postmeta_2.meta_value != postmeta_3.meta_value
", current_time( 'timestamp' ) ) );
", current_time( 'timestamp', true ) ) );
}
/**

View File

@ -56,13 +56,13 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
}
$product->set_props( array(
'name' => $post_object->post_title,
'slug' => $post_object->post_name,
'date_created' => $post_object->post_date,
'date_modified' => $post_object->post_modified,
'status' => $post_object->post_status,
'menu_order' => $post_object->menu_order,
'reviews_allowed' => 'open' === $post_object->comment_status,
'name' => $post_object->post_title,
'slug' => $post_object->post_name,
'date_created' => strtotime( $post_object->post_date_gmt ),
'date_modified' => strtotime( $post_object->post_modified_gmt ),
'status' => $post_object->post_status,
'menu_order' => $post_object->menu_order,
'reviews_allowed' => 'open' === $post_object->comment_status,
) );
$this->read_product_data( $product );
@ -92,7 +92,9 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
* @param WC_Product
*/
public function create( &$product ) {
$product->set_date_created( current_time( 'timestamp' ) );
if ( ! $product->get_date_created() ) {
$product->set_date_created( current_time( 'timestamp', true ) );
}
$id = wp_insert_post( apply_filters( 'woocommerce_new_product_variation_data', array(
'post_type' => 'product_variation',
@ -104,8 +106,8 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
'comment_status' => 'closed',
'ping_status' => 'closed',
'menu_order' => $product->get_menu_order(),
'post_date' => date( 'Y-m-d H:i:s', $product->get_date_created() ),
'post_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', $product->get_date_created() ) ),
'post_date' => date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getOffsetTimestamp() ),
'post_date_gmt' => date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getTimestamp() ),
) ), true );
if ( $id && ! is_wp_error( $id ) ) {
@ -146,10 +148,10 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
'comment_status' => 'closed',
'post_status' => $product->get_status( 'edit' ) ? $product->get_status( 'edit' ) : 'publish',
'menu_order' => $product->get_menu_order( 'edit' ),
'post_date' => date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' ) ),
'post_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' ) ) ),
'post_modified' => isset( $changes['date_modified'] ) ? date( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' ) ) : current_time( 'mysql' ),
'post_modified_gmt' => isset( $changes['date_modified'] ) ? get_gmt_from_date( date( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' ) ) ) : current_time( 'mysql', 1 ),
'post_date' => date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getOffsetTimestamp() ),
'post_date_gmt' => date( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getTimestamp() ),
'post_modified' => isset( $changes['date_modified'] ) ? date( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' )->getOffsetTimestamp() ) : current_time( 'mysql' ),
'post_modified_gmt' => isset( $changes['date_modified'] ) ? date( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' )->getTimestamp() ) : current_time( 'mysql', 1 ),
) );
}

View File

@ -85,10 +85,10 @@ abstract class WC_Abstract_Legacy_Product extends WC_Data {
$value = $this->get_catalog_visibility();
break;
case 'sale_price_dates_from' :
$value = $this->get_date_on_sale_from();
return $this->get_date_on_sale_from() ? $this->get_date_on_sale_from()->getTimestamp() : '';
break;
case 'sale_price_dates_to' :
$value = $this->get_date_on_sale_to();
return $this->get_date_on_sale_to() ? $this->get_date_on_sale_to()->getTimestamp() : '';
break;
case 'post' :
$value = get_post( $this->get_id() );