WooCommerce Code Reference

ReportExporter
in package
Uses SchedulerTraits

ReportExporter Class.

Table of Contents

DOWNLOAD_EXPORT_ACTION  = 'woocommerce_admin_download_report_csv'
Export file download action.
EXPORT_RETENTION_PERIOD  = WEEK_IN_SECONDS
How long a generated export stays available for download.
EXPORT_STATUS_OPTION  = 'woocommerce_admin_report_export_status'
Export status option name.
$group  : string|null
Action scheduler group.
$name  : string
Slug to identify the scheduler.
$queue  : WC_Queue_Interface
Queue instance.
clear_queued_actions()  : mixed
Clears all queued actions.
do_action_or_reschedule()  : mixed
Check for blocking jobs and reschedule if any exist.
download_export_file()  : mixed
Serve the export file.
email_report_download_link()  : void
Process a report export email action.
export_report()  : void
Process a report export action.
flatten_args()  : string
Flatten multidimensional arrays to store for scheduling.
get_action()  : string|null
Get an action tag name from the action name.
get_actions()  : mixed
Get all available scheduling actions.
get_batch_size()  : int
Returns the batch size for an action.
get_batch_sizes()  : mixed
Batch action size.
get_default_scheduler_actions()  : mixed
Gets the default scheduler actions for batching and scheduling actions.
get_dependencies()  : array<string|int, mixed>
Add action dependencies.
get_dependency()  : string|null
Get dependencies associated with an action.
get_download_url()  : string
Get the URL a finished report export is downloaded from.
get_export_date_range()  : array<string|int, string>
Get the date range a report export covers.
get_export_date_range_label()  : string
Get the date range a report export covers, formatted for display.
get_export_percentage_complete()  : bool|int
Get the completion percentage of a report export.
get_next_action_time()  : DateTime|null
Get the DateTime for the next scheduled time an action should run.
get_next_blocking_job()  : false|ActionScheduler_Action
Get the next blocking job for an action.
get_scheduler_actions()  : array<string|int, mixed>
Get all available scheduling actions.
has_existing_jobs()  : bool
Check if existing jobs exist for an action and arguments.
init()  : mixed
Hook in action methods.
queue()  : WC_Queue_Interface
Get queue instance.
queue_batches()  : void
Queue a large number of batch jobs, respecting the batch size limit.
queue_report_export()  : int
Queue up actions for a full report export.
schedule_action()  : mixed
Schedule an action to run and check for dependencies.
set_queue()  : mixed
Set queue instance.
update_export_percentage_complete()  : void
Update the completion percentage of a report export.
get_status_key()  : string
Generate a key to reference an export status.
format_date_range_bound()  : string
Format one end of a report's date range for display.
get_export_filename()  : string
Get the name a report export is stored under.
get_requested_export()  : ReportCSVExporter|null
Build the exporter a download request is asking for.

Constants

DOWNLOAD_EXPORT_ACTION

Export file download action.

public mixed DOWNLOAD_EXPORT_ACTION = 'woocommerce_admin_download_report_csv'

EXPORT_RETENTION_PERIOD

How long a generated export stays available for download.

public mixed EXPORT_RETENTION_PERIOD = WEEK_IN_SECONDS

EXPORT_STATUS_OPTION

Export status option name.

public mixed EXPORT_STATUS_OPTION = 'woocommerce_admin_report_export_status'

Properties

Methods

download_export_file()

Serve the export file.

public static download_export_file() : mixed
Return values
mixed —

Process a report export email action.

public static email_report_download_link(int $user_id, string $export_id, string $report_type[, array<string|int, mixed> $report_args = array() ]) : void
Parameters
$user_id : int

User ID that requested the email.

$export_id : string

Unique ID for report (timestamp expected).

$report_type : string

Report type. E.g. 'customers'.

$report_args : array<string|int, mixed> = array()

Optional. Report parameters the export was queued with. Exports queued before WooCommerce 11.2.0 run without them.

Return values
void —

export_report()

Process a report export action.

public static export_report(int $page_number, string $export_id, string $report_type, array<string|int, mixed> $report_args) : void
Parameters
$page_number : int

Page number for this action.

$export_id : string

Unique ID for report (timestamp expected).

$report_type : string

Report type. E.g. 'customers'.

$report_args : array<string|int, mixed>

Report parameters, passed to data query.

Return values
void —

flatten_args()

Flatten multidimensional arrays to store for scheduling.

public static flatten_args(array<string|int, mixed> $args) : string
Parameters
$args : array<string|int, mixed>

Argument array.

Return values
string —

get_action()

Get an action tag name from the action name.

public static get_action(string $action_name) : string|null
Parameters
$action_name : string

The action name.

Return values
string|null —

get_actions()

Get all available scheduling actions.

public static get_actions() : mixed

Used to determine action hook names and clear events.

Return values
mixed —

get_batch_size()

Returns the batch size for an action.

public static get_batch_size(string $action) : int
Parameters
$action : string

Single batch action name.

Return values
int — Batch size.

get_default_scheduler_actions()

Gets the default scheduler actions for batching and scheduling actions.

public static get_default_scheduler_actions() : mixed
Return values
mixed —

get_dependencies()

Add action dependencies.

public static get_dependencies() : array<string|int, mixed>
Return values
array<string|int, mixed> —

get_dependency()

Get dependencies associated with an action.

public static get_dependency(string $action_name) : string|null
Parameters
$action_name : string

The action slug.

Return values
string|null —

get_download_url()

Get the URL a finished report export is downloaded from.

public static get_download_url(string $report_type, string $export_id[, array<string|int, mixed> $report_args = array() ]) : string
Parameters
$report_type : string

Report type. E.g. 'customers'.

$export_id : string

Unique ID for report (timestamp expected).

$report_args : array<string|int, mixed> = array()

Optional. Report parameters the export was queued with. When they name a date range, the link carries it so the download is named after it.

Tags
since
11.2.0
Return values
string —

get_export_date_range()

Get the date range a report export covers.

public static get_export_date_range(array<string|int, mixed> $report_args) : array<string|int, string>

Reports are not all limited to a period. Stock, for one, has no date range at all.

Parameters
$report_args : array<string|int, mixed>

Report parameters, passed to data query.

Tags
since
11.2.0
Return values
array<string|int, string> — The range's `after` and `before` dates as `Y-m-d`, or an empty array when the report has no range.

get_export_date_range_label()

Get the date range a report export covers, formatted for display.

public static get_export_date_range_label(array<string|int, mixed> $report_args) : string
Parameters
$report_args : array<string|int, mixed>

Report parameters, passed to data query.

Tags
since
11.2.0
Return values
string — Date range in the site's date format, or an empty string when the report has no range.

get_export_percentage_complete()

Get the completion percentage of a report export.

public static get_export_percentage_complete(string $report_type, string $export_id) : bool|int
Parameters
$report_type : string

Report type. E.g. 'customers'.

$export_id : string

Unique ID for report (timestamp expected).

Return values
bool|int — Completion percentage, or false if export not found.

get_next_action_time()

Get the DateTime for the next scheduled time an action should run.

public static get_next_action_time(ActionScheduler_Action $action) : DateTime|null

This function allows backwards compatibility with Action Scheduler < v3.0.

Parameters
$action : ActionScheduler_Action

Action.

Return values
DateTime|null —

get_next_blocking_job()

Get the next blocking job for an action.

public static get_next_blocking_job(string $action_name) : false|ActionScheduler_Action
Parameters
$action_name : string

Action name.

Return values
false|ActionScheduler_Action —

get_scheduler_actions()

Get all available scheduling actions.

public static get_scheduler_actions() : array<string|int, mixed>

Used to determine action hook names and clear events.

Return values
array<string|int, mixed> —

has_existing_jobs()

Check if existing jobs exist for an action and arguments.

public static has_existing_jobs(string $action_name, array<string|int, mixed> $args) : bool
Parameters
$action_name : string

Action name.

$args : array<string|int, mixed>

Array of arguments to pass to action.

Return values
bool —

queue_batches()

Queue a large number of batch jobs, respecting the batch size limit.

public static queue_batches(int $range_start, int $range_end, string $single_batch_action[, array<string|int, mixed> $action_args = array() ]) : void

Reduces a range of batches down to "single batch" jobs.

Parameters
$range_start : int

Starting batch number.

$range_end : int

Ending batch number.

$single_batch_action : string

Action to schedule for a single batch.

$action_args : array<string|int, mixed> = array()

Action arguments.

Return values
void —

queue_report_export()

Queue up actions for a full report export.

public static queue_report_export(string $export_id, string $report_type[, array<string|int, mixed> $report_args = array() ][, bool $send_email = false ]) : int
Parameters
$export_id : string

Unique ID for report (timestamp expected).

$report_type : string

Report type. E.g. 'customers'.

$report_args : array<string|int, mixed> = array()

Report parameters, passed to data query.

$send_email : bool = false

Optional. Send an email when the export is complete.

Return values
int — Number of items to export.

schedule_action()

Schedule an action to run and check for dependencies.

public static schedule_action(string $action_name[, array<string|int, mixed> $args = array() ]) : mixed
Parameters
$action_name : string

Action name.

$args : array<string|int, mixed> = array()

Array of arguments to pass to action.

Return values
mixed —

set_queue()

Set queue instance.

public static set_queue(WC_Queue_Interface $queue) : mixed
Parameters
$queue : WC_Queue_Interface

Queue instance.

Return values
mixed —

update_export_percentage_complete()

Update the completion percentage of a report export.

public static update_export_percentage_complete(string $report_type, string $export_id, int $percentage) : void
Parameters
$report_type : string

Report type. E.g. 'customers'.

$export_id : string

Unique ID for report (timestamp expected).

$percentage : int

Completion percentage.

Return values
void —

get_status_key()

Generate a key to reference an export status.

protected static get_status_key(string $report_type, string $export_id) : string
Parameters
$report_type : string

Report type. E.g. 'customers'.

$export_id : string

Unique ID for report (timestamp expected).

Return values
string — Status key.

format_date_range_bound()

Format one end of a report's date range for display.

private static format_date_range_bound(string $date) : string
Parameters
$date : string

Date as Y-m-d.

Return values
string — The date in the store's date format, or an empty string when it cannot be read.

get_export_filename()

Get the name a report export is stored under.

private static get_export_filename(string $report_type, string $export_id) : string
Parameters
$report_type : string

Report type. E.g. 'customers'.

$export_id : string

Unique ID for report (timestamp expected).

Return values
string —

get_requested_export()

Build the exporter a download request is asking for.

private static get_requested_export(array<string|int, mixed> $request) : ReportCSVExporter|null

A read-only download of a report the requesting user is already allowed to view, gated on the view_woocommerce_reports capability, so a nonce would only prevent nuisance CSRF. The action is compared verbatim against a fixed name, and set_filename() applies sanitize_file_name(), which keeps the path inside the reports directory. A nonce is not an option here either: nonces last 24 hours, and this link is emailed and kept for a week.

Parameters
$request : array<string|int, mixed>

Unslashed request parameters, expected to be $_GET.

Return values
ReportCSVExporter|null — The exporter for the requested export, or null when the request asks for no export.