WC_Admin_Report::prepare_chart_data()publicWC 1.0

Put data with post_date's into an array of times.

Method of the class: WC_Admin_Report{}

No Hooks.

Return

Array.

Usage

$WC_Admin_Report = new WC_Admin_Report();
$WC_Admin_Report->prepare_chart_data( $data, $date_key, $data_key, $interval, $start_date, $group_by );
$data(array) (required)
array of your data.
$date_key(string) (required)
key for the 'date' field. e.g. 'post_date'.
$data_key(string) (required)
key for the data you are charting.
$interval(int) (required)
interval to use.
$start_date(string) (required)
start date.
$group_by(string) (required)
group by.

WC_Admin_Report::prepare_chart_data() code WC 8.6.1

public function prepare_chart_data( $data, $date_key, $data_key, $interval, $start_date, $group_by ) {
	// phpcs:disable WordPress.DateTime.RestrictedFunctions.date_date

	$prepared_data = array();

	// Ensure all days (or months) have values in this range.
	if ( 'day' === $group_by ) {
		for ( $i = 0; $i <= $interval; $i ++ ) {
			$time = strtotime( date( 'Ymd', strtotime( "+{$i} DAY", $start_date ) ) ) . '000';

			if ( ! isset( $prepared_data[ $time ] ) ) {
				$prepared_data[ $time ] = array( esc_js( $time ), 0 );
			}
		}
	} else {
		$current_yearnum  = date( 'Y', $start_date );
		$current_monthnum = date( 'm', $start_date );

		for ( $i = 0; $i <= $interval; $i ++ ) {
			$time = strtotime( $current_yearnum . str_pad( $current_monthnum, 2, '0', STR_PAD_LEFT ) . '01' ) . '000';

			if ( ! isset( $prepared_data[ $time ] ) ) {
				$prepared_data[ $time ] = array( esc_js( $time ), 0 );
			}

			$current_monthnum ++;

			if ( $current_monthnum > 12 ) {
				$current_monthnum = 1;
				$current_yearnum  ++;
			}
		}
	}

	foreach ( $data as $d ) {
		switch ( $group_by ) {
			case 'day':
				$time = strtotime( date( 'Ymd', strtotime( $d->$date_key ) ) ) . '000';
				break;
			case 'month':
			default:
				$time = strtotime( date( 'Ym', strtotime( $d->$date_key ) ) . '01' ) . '000';
				break;
		}

		if ( ! isset( $prepared_data[ $time ] ) ) {
			continue;
		}

		if ( $data_key ) {
			$prepared_data[ $time ][1] += is_numeric( $d->$data_key ) ? $d->$data_key : 0;
		} else {
			$prepared_data[ $time ][1] ++;
		}
	}

	return $prepared_data;

	// phpcs:enable WordPress.DateTime.RestrictedFunctions.date_date
}