Anmelden
Loslegen

Easy Digital Downloads Dokumentation

Dokumentation, Referenzmaterialien und Tutorials für Easy Digital Downloads 

EDD Export-Klasse

Übersicht

Das Easy Digital Downloads Export-System bietet ein robustes Framework zur Erstellung benutzerdefinierter Datenexporte. Diese Dokumentation behandelt die Basisklasse Exporter und wie benutzerdefinierte Exporter implementiert werden.

Voraussetzungen

  • WordPress 6.2+
  • Easy Digital Downloads 3.3.8+
  • PHP 7.4+

Basis-Exportklasse

Die Klasse Exporter befindet sich im Namespace EDD\Admin\Exports\Exporters und dient als Grundlage für die gesamte Exportfunktionalität von EDD.

Erforderliche Methoden

Beim Erweitern der Exporter-Klasse müssen Sie diese abstrakten Methoden implementieren:

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;

Erstellen eines benutzerdefinierten Exporters

1. Grundlegende Struktur

Erstellen Sie eine Klasse, die die Exporter-Klasse erweitert:

<?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. Registrieren Ihres Exporters

Erstellen Sie eine Klasse zur Verwaltung der Registrierung und Anzeige des Formulars:

/**
 * 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' );

Mit der neuen Exportregistrierung, sobald Ihre Exportklasse zusammen mit Ihrem Formular eingerichtet ist, kümmert sich EDD automatisch um den Exportvorgang.

Quellen:

Was this article helpful?

Verkaufen Sie noch heute!

Schließen Sie sich über 50.000 klugen Shop-Besitzern an und nutzen Sie die einfachste Methode, um digitale Produkte mit WordPress zu verkaufen.

Copyright © 2025 Sandhills Development, LLC

[universally_switcher]