Added extended info.

This commit is contained in:
Peter Fabian 2018-12-10 16:39:20 +01:00 committed by Joshua Flowers
parent 5eee869a74
commit a346f13beb
3 changed files with 129 additions and 28 deletions

View File

@ -38,15 +38,15 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
* @return array
*/
protected function prepare_reports_query( $request ) {
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['code'] = (array) $request['code'];
$args = array();
$args['before'] = $request['before'];
$args['after'] = $request['after'];
$args['page'] = $request['page'];
$args['per_page'] = $request['per_page'];
$args['orderby'] = $request['orderby'];
$args['order'] = $request['order'];
$args['code'] = (array) $request['code'];
$args['extended_info'] = $request['extended_info'];
return $args;
}
@ -167,6 +167,45 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'extended_info' => array(
'code' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon code.', 'wc-admin' ),
),
'date_created' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon creation date.', 'wc-admin' ),
),
'date_created_gmt' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon creation date in GMT.', 'wc-admin' ),
),
'date_expires' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon expiration date.', 'wc-admin' ),
),
'date_expires_gmt' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'description' => __( 'Coupon expiration date in GMT.', 'wc-admin' ),
),
'discount_type' => array(
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit' ),
'enum' => array_keys( wc_get_coupon_types() ),
'description' => __( 'Coupon discount type.', 'wc-admin' ),
),
),
),
);
@ -179,9 +218,9 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
* @return array
*/
public function get_collection_params() {
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['page'] = array(
$params = array();
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
$params['page'] = array(
'description' => __( 'Current page of the collection.', 'wc-admin' ),
'type' => 'integer',
'default' => 1,
@ -189,7 +228,7 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
);
$params['per_page'] = array(
$params['per_page'] = array(
'description' => __( 'Maximum number of items to be returned in result set.', 'wc-admin' ),
'type' => 'integer',
'default' => 10,
@ -198,26 +237,26 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
$params['after'] = array(
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'wc-admin' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'wc-admin' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['order'] = array(
$params['order'] = array(
'description' => __( 'Order sort attribute ascending or descending.', 'wc-admin' ),
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg',
);
$params['orderby'] = array(
$params['orderby'] = array(
'description' => __( 'Sort collection by object attribute.', 'wc-admin' ),
'type' => 'string',
'default' => 'coupon_id',
@ -228,7 +267,7 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['interval'] = array(
$params['interval'] = array(
'description' => __( 'Time interval to use for buckets in the returned data.', 'wc-admin' ),
'type' => 'string',
'default' => 'week',
@ -242,7 +281,7 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['code'] = array(
$params['code'] = array(
'description' => __( 'Limit result set to items assigned one or more code.', 'wc-admin' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
@ -251,6 +290,13 @@ class WC_Admin_REST_Reports_Coupons_Controller extends WC_REST_Reports_Controlle
'type' => 'string',
),
);
$params['extended_info'] = array(
'description' => __( 'Add additional piece of info about each coupon to the report.', 'wc-admin' ),
'type' => 'boolean',
'default' => false,
'sanitize_callback' => 'wc_string_to_bool',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}

View File

@ -86,6 +86,54 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
return $sql_query_params;
}
/**
* Enriches the coupon data with extra attributes.
*
* @param array $coupon_data Coupon data.
* @param array $query_args Query parameters.
*/
protected function include_extended_info( &$coupon_data, $query_args ) {
if ( $query_args['extended_info'] ) {
foreach ( $coupon_data as $idx => $coupon_datum ) {
$coupon_id = $coupon_datum['coupon_id'];
$coupon = new WC_Coupon( $coupon_id );
$gmt_timzone = new DateTimeZone( 'UTC' );
$date_expires = $coupon->get_date_expires();
if ( null === $date_expires ) {
$date_expires = '';
$date_expires_gmt = '';
} else {
$date_expires = $date_expires->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$date_expires_gmt = new DateTime( $date_expires );
$date_expires_gmt->setTimezone( $gmt_timzone );
$date_expires_gmt = $date_expires_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
}
$date_created = $coupon->get_date_created();
if ( null === $date_created ) {
$date_created = '';
$date_created_gmt = '';
} else {
$date_created = $date_created->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$date_created_gmt = new DateTime( $date_created );
$date_created_gmt->setTimezone( $gmt_timzone );
$date_created_gmt = $date_created_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
}
$coupon_data[ $idx ]['extended_info'] = array(
'code' => $coupon->get_code(),
'date_created' => $date_created,
'date_created_gmt' => $date_created_gmt,
'date_expires' => $date_expires,
'date_expires_gmt' => $date_expires_gmt,
'discount_type' => $coupon->get_discount_type(),
);
}
}
}
/**
* Returns the report data based on parameters supplied by the user.
*
@ -101,16 +149,17 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
// These defaults are only partially applied when used via REST API, as that has its own defaults.
$defaults = array(
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'fields' => '*',
'code' => array(),
'per_page' => get_option( 'posts_per_page' ),
'page' => 1,
'order' => 'DESC',
'orderby' => 'date',
'before' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $now ),
'after' => date( WC_Admin_Reports_Interval::$iso_datetime_format, $week_back ),
'fields' => '*',
'code' => array(),
'extended_info' => false,
// This is not a parameter for coupons reports per se, but we want to only take into account selected order types.
'order_status' => parent::get_report_order_statuses(),
'order_status' => parent::get_report_order_statuses(),
);
$query_args = wp_parse_args( $query_args, $defaults );
@ -173,6 +222,8 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
return $data;
}
$this->include_extended_info( $coupon_data, $query_args );
$coupon_data = array_map( array( $this, 'cast_numbers' ), $coupon_data );
$data = (object) array(
'data' => $coupon_data,

View File

@ -294,6 +294,10 @@ class WC_Admin_Reports_Data_Store {
$retyped_array = array();
$column_types = apply_filters( 'woocommerce_rest_reports_column_types', $this->column_types, $array );
foreach ( $array as $column_name => $value ) {
if ( is_array( $value ) ) {
$value = $this->cast_numbers( $value );
}
if ( isset( $column_types[ $column_name ] ) ) {
$retyped_array[ $column_name ] = $column_types[ $column_name ]( $value );
} else {