Prezentare generală
Sistemul de export Easy Digital Downloads oferă un cadru robust pentru crearea de exporturi de date personalizate. Această documentație acoperă clasa de bază Exporter și cum să implementați exportatori personalizați.
Cerințe
- WordPress 6.2+
- Easy Digital Downloads 3.3.8+
- PHP 7.4+
Clasa de bază pentru export
Clasa Exporter se află în spațiul de nume EDD\Admin\Exports\Exporters și servește drept fundație pentru toată funcționalitatea de export EDD.
Metode obligatorii
Când extindeți clasa Exporter, trebuie să implementați aceste metode abstracte:
abstract protected function get_export_type(): string;
abstract protected function get_data(): array;
abstract protected function get_data_headers(): array;
abstract protected function get_total(): int;
abstract public function set_properties( $request ): void;
Crearea unui exportator personalizat
1. Structura de bază
Creați o clasă care extinde clasa Exporter:
<?php
/**
* Custom Exporter class.
*/
class CustomExporter extends EDD\Admin\Exports\Exporters\Exporter {
/**
* Get the export type.
*
* @return string The export type.
*/
protected function get_export_type(): string {
return 'custom_export';
}
/**
* Set the properties.
*
* @param array $request The request.
* @return void
*/
public function set_properties( $request ): void {
$this->start = isset( $request['custom-export-start'] )
? sanitize_text_field( $request['custom-export-start'] )
: '';
$this->end = isset( $request['custom-export-end'] )
? sanitize_text_field( $request['custom-export-end'] )
: '';
}
/**
* Get the data headers.
*
* @return array The data headers.
*/
protected function get_data_headers(): array {
return array(
'id' => __( 'ID', 'your-text-domain' ),
'status' => __( 'Status', 'your-text-domain' ),
);
}
protected function get_data(): array {
$data = array();
$args = array_merge(
array(
'number' => $this->per_step,
'offset' => ( $this->step * $this->per_step ) - $this->per_step,
),
$this->get_base_args()
);
// Your data retrieval logic here
$items = edd_get_orders( $args );
foreach ( $items as $item ) {
$data[] = array(
'id' => $item->id,
'status' => $item->status,
);
}
return $data;
}
/**
* Get the total.
*
* @return int The total.
*/
protected function get_total(): int {
return edd_count_orders( $this->get_base_args() );
}
/**
* Get the base args.
*
* @return array The base args.
*/
private function get_base_args(): array {
$args = array();
if ( ! empty( $this->start ) || ! empty( $this->end ) ) {
$args['date_query'] = $this->get_date_query();
}
return $args;
}
}
2. Înregistrarea exportatorului dvs.
Creați o clasă pentru a gestiona înregistrarea și afișarea formularului:
/**
* Register the custom exporter.
*
* @param \EDD\Admin\Exports\Registry $registry The registry instance.
* @return void
*/
function custom_register_export( \EDD\Admin\Exports\Registry $registry ) {
$registry->register_exporter(
'custom_export',
array(
'label' => __( 'Custom Export', 'your-text-domain' ),
'description' => __( 'Export custom data.', 'your-text-domain' ),
'class' => CustomExporter::class,
'class_path' => __DIR__ . '/class-custom-export.php', // Change to your export class path.
'button' => __( 'Export Data', 'your-text-domain' ),
)
);
}
add_action( 'edd_export_init', 'custom_register_export' );
/**
* Display the custom export form.
*
* @param string $exporter_id The exporter ID.
* @return void
*/
function custom_export_form( $exporter_id ) {
if ( 'custom_export' !== $exporter_id ) {
return;
}
$from_to = new \EDD\HTML\FromTo(
array(
'legend' => __( 'Custom Export Date', 'your-text-domain' ),
'id' => 'order-export',
)
);
$from_to->output();
?>
<label for="edd_export_custom_status" class="screen-reader-text"><?php esc_html_e( 'Select Status', 'your-text-domain' ); ?></label>
<?php
$statuses = array();
foreach ( edd_get_payment_statuses() as $status ) {
if ( 'publish' === $status ) {
continue;
}
$statuses[ $status ] = edd_get_payment_status_label( $status );
}
$select = new \EDD\HTML\Select(
array(
'id' => 'edd_export_custom_status',
'name' => 'status',
'show_option_all' => __( 'All Statuses', 'your-text-domain' ),
'show_option_none' => false,
'selected' => false,
'options' => $statuses,
)
);
$select->output();
}
add_action( 'edd_export_form', 'custom_export_form' );
Cu noul registru de export, odată ce aveți clasa de export configurată împreună cu formularul dvs., EDD va gestiona automat procesul de export.
Surse:
- Clasa abstractă EDD\Admin\Exports\Exporters\Exporter
- Registru de export
- Clasa APIRequests care utilizează noul exportator ca exemplu
