# EDD Export Class

Overview
--------

The Easy Digital Downloads Export system provides a robust framework for creating custom data exports. This documentation covers the base `Exporter` class and how to implement custom exporters.

Requirements
------------

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

Base Export Class
-----------------

The `Exporter` class is located in the `EDD\Admin\Exports\Exporters` namespace and serves as the foundation for all EDD export functionality.

### Required Methods

When extending the Exporter class, you must implement these abstract methods:

```
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;
```

Creating a Custom Exporter
--------------------------

### 1. Basic Structure

Create a class that extends the `Exporter` class:

```
&lt;?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 &#039;custom_export&#039;;
	}

	/**
	 * Set the properties.
	 *
	 * @param array $request The request.
	 * @return void
	 */
	public function set_properties( $request ): void {
		$this-&gt;start = isset( $request[&#039;custom-export-start&#039;] )
			? sanitize_text_field( $request[&#039;custom-export-start&#039;] )
			: &#039;&#039;;
		$this-&gt;end   = isset( $request[&#039;custom-export-end&#039;] )
			? sanitize_text_field( $request[&#039;custom-export-end&#039;] )
			: &#039;&#039;;
	}

	/**
	 * Get the data headers.
	 *
	 * @return array The data headers.
	 */
	protected function get_data_headers(): array {
		return array(
			&#039;id&#039;     =&gt; __( &#039;ID&#039;, &#039;your-text-domain&#039; ),
			&#039;status&#039; =&gt; __( &#039;Status&#039;, &#039;your-text-domain&#039; ),
		);
	}

	protected function get_data(): array {
		$data = array();
		$args = array_merge(
			array(
				&#039;number&#039; =&gt; $this-&gt;per_step,
				&#039;offset&#039; =&gt; ( $this-&gt;step * $this-&gt;per_step ) - $this-&gt;per_step,
			),
			$this-&gt;get_base_args()
		);

		// Your data retrieval logic here
		$items = edd_get_orders( $args );

		foreach ( $items as $item ) {
			$data[] = array(
				&#039;id&#039;     =&gt; $item-&gt;id,
				&#039;status&#039; =&gt; $item-&gt;status,
			);
		}

		return $data;
	}

	/**
	 * Get the total.
	 *
	 * @return int The total.
	 */
	protected function get_total(): int {
		return edd_count_orders( $this-&gt;get_base_args() );
	}

	/**
	 * Get the base args.
	 *
	 * @return array The base args.
	 */
	private function get_base_args(): array {
		$args = array();
		if ( ! empty( $this-&gt;start ) || ! empty( $this-&gt;end ) ) {
			$args[&#039;date_query&#039;] = $this-&gt;get_date_query();
		}
		return $args;
	}
}
```

### 2. Registering Your Exporter

Create a class to handle the registration and form display:

```
/**
 * 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-&gt;register_exporter(
		&#039;custom_export&#039;,
		array(
			&#039;label&#039;       =&gt; __( &#039;Custom Export&#039;, &#039;your-text-domain&#039; ),
			&#039;description&#039; =&gt; __( &#039;Export custom data.&#039;, &#039;your-text-domain&#039; ),
			&#039;class&#039;       =&gt; CustomExporter::class,
			&#039;class_path&#039;  =&gt; __DIR__ . &#039;/class-custom-export.php&#039;, // Change to your export class path.
			&#039;button&#039;      =&gt; __( &#039;Export Data&#039;, &#039;your-text-domain&#039; ),
		)
	);
}
add_action( &#039;edd_export_init&#039;, &#039;custom_register_export&#039; );

/**
 * Display the custom export form.
 *
 * @param string $exporter_id The exporter ID.
 * @return void
 */
function custom_export_form( $exporter_id ) {
	if ( &#039;custom_export&#039; !== $exporter_id ) {
		return;
	}

	$from_to = new \EDD\HTML\FromTo(
		array(
			&#039;legend&#039; =&gt; __( &#039;Custom Export Date&#039;, &#039;your-text-domain&#039; ),
			&#039;id&#039;     =&gt; &#039;order-export&#039;,
		)
	);
	$from_to-&gt;output();
	?&gt;
		&lt;label for=&quot;edd_export_custom_status&quot; class=&quot;screen-reader-text&quot;&gt;&lt;?php esc_html_e( &#039;Select Status&#039;, &#039;your-text-domain&#039; ); ?&gt;&lt;/label&gt;
		&lt;?php
		$statuses = array();
		foreach ( edd_get_payment_statuses() as $status ) {
			if ( &#039;publish&#039; === $status ) {
				continue;
			}
			$statuses[ $status ] = edd_get_payment_status_label( $status );
		}
		$select = new \EDD\HTML\Select(
			array(
				&#039;id&#039;               =&gt; &#039;edd_export_custom_status&#039;,
				&#039;name&#039;             =&gt; &#039;status&#039;,
				&#039;show_option_all&#039;  =&gt; __( &#039;All Statuses&#039;, &#039;your-text-domain&#039; ),
				&#039;show_option_none&#039; =&gt; false,
				&#039;selected&#039;         =&gt; false,
				&#039;options&#039;          =&gt; $statuses,
			)
		);
		$select-&gt;output();
}
add_action( &#039;edd_export_form&#039;, &#039;custom_export_form&#039; );
```

With the new export registry, once you have your export class set up along with your form, EDD will handle the export process automatically.

Sources:

- [EDD\\Admin\\Exports\\Exporters\\Exporter](https://github.com/awesomemotive/easy-digital-downloads/blob/main/src/Admin/Exports/Exporters/Exporter.php) abstract class
- [Export registry](https://github.com/awesomemotive/easy-digital-downloads/blob/main/src/Admin/Exports/Registry.php)
- [APIRequests class](https://github.com/awesomemotive/easy-digital-downloads/blob/main/src/Admin/Exports/Exporters/APIRequests.php) utilizing the new exporter as an example