# WP Content Exporter v2

A WordPress library for exporting content to WXR (WordPress eXtended RSS) format, similar to WP Content Importer v2 but for export functionality.

## Features

- **Enhanced Control**: Better control over export options compared to WordPress's built-in `export_wp()` function
- **Extensible**: Hooks and filters for customization
- **Progress Tracking**: Built-in support for progress callbacks
- **Error Handling**: Comprehensive error handling and logging
- **Logger Support**: PSR-3 compatible logger interface
- **Flexible Options**: Export specific posts, authors, date ranges, categories, and more

## Installation

This library is included as part of the Smart One Click Setup plugin. It's autoloaded via Composer.

## Usage

### Basic Usage

```php
use BuildByCj\WPContentExporter2\Exporter;

// Create exporter instance
$exporter = new Exporter( array(
    'content' => 'all', // or 'posts', 'pages', or specific post type
    'output_file' => '/path/to/export.xml',
) );

// Export
$result = $exporter->export();

if ( is_wp_error( $result ) ) {
    // Handle error
    echo $result->get_error_message();
} else {
    // Export successful
    echo 'Export completed!';
}
```

### Advanced Usage

```php
use BuildByCj\WPContentExporter2\Exporter;
use BuildByCj\WPContentExporter2\WPExporterLogger;

// Create logger
$logger = new WPExporterLogger();

// Create exporter with options
$exporter = new Exporter( array(
    'content'            => 'posts',
    'post_ids'           => array( 1, 2, 3 ), // Specific post IDs
    'author'             => 'admin', // Author login or ID
    'start_date'         => '2024-01-01',
    'end_date'           => '2024-12-31',
    'category'           => 'news', // Category slug
    'post_status'         => 'publish',
    'include_attachments' => true,
    'include_comments'    => true,
    'include_terms'       => true,
    'include_users'       => true,
    'output_file'         => '/path/to/export.xml',
), $logger );

// Export with progress tracking
$result = $exporter->export_with_progress( function( $status, $progress, $message ) {
    echo sprintf( '[%d%%] %s: %s', $progress, $status, $message ) . PHP_EOL;
} );
```

### Get Preliminary Information

```php
use BuildByCj\WPContentExporter2\Exporter;

$exporter = new Exporter( array(
    'content' => 'all',
) );

// Get export information before exporting
$info = $exporter->get_preliminary_information();

if ( ! is_wp_error( $info ) ) {
    echo 'Posts to export: ' . $info->post_count . PHP_EOL;
    echo 'Media to export: ' . $info->media_count . PHP_EOL;
    echo 'Comments to export: ' . $info->comment_count . PHP_EOL;
    echo 'Terms to export: ' . $info->term_count . PHP_EOL;
}
```

## Options

### Content Options

- `content` (string): Content type to export. Options: `'all'`, `'posts'`, `'pages'`, or specific post type. Default: `'all'`
- `post_ids` (array): Specific post IDs to export. Default: empty array (export all)
- `author` (string): Author ID or login to export. Default: empty (export all)
- `start_date` (string): Start date for export (Y-m-d format). Default: empty
- `end_date` (string): End date for export (Y-m-d format). Default: empty
- `category` (string): Category slug or ID to export. Default: empty (export all)
- `post_status` (string): Post status to export. Default: `'publish'`

### Include Options

- `include_attachments` (bool): Whether to include attachments. Default: `true`
- `include_comments` (bool): Whether to include comments. Default: `true`
- `include_terms` (bool): Whether to include terms. Default: `true`
- `include_users` (bool): Whether to include users. Default: `true`

### Output Options

- `output_file` (string): Path to output file. If empty, returns XML string. Default: empty

## Hooks and Filters

### Actions

- `wxr_exporter.before_export` - Fires before export starts
  - Parameters: `$args` (array), `$options` (array)

- `wxr_exporter.after_export` - Fires after export completes
  - Parameters: `$export_data` (string), `$args` (array), `$options` (array)

### Filters

- `wxr_exporter.export_args` - Filter export arguments passed to `export_wp()`
  - Parameters: `$args` (array), `$options` (array)
  - Returns: Modified `$args` array

- `wxr_exporter.export_data` - Filter the exported XML data
  - Parameters: `$export_data` (string), `$args` (array), `$options` (array)
  - Returns: Modified XML string

- `wxr_exporter.preliminary_information` - Filter preliminary export information
  - Parameters: `$info` (WXRExportInfo), `$options` (array)
  - Returns: Modified `$info` object

## Classes

### Exporter

Main exporter class that extends `WXRExporter`. Provides convenience methods like `export_with_progress()`.

### WXRExporter

Core exporter class that handles the actual export process.

### WPExporterLogger

PSR-3 compatible logger for export operations.

### WPExporterLoggerCLI

CLI-specific logger that outputs to console.

### WXRExportInfo

Information class containing export metadata (post counts, user info, etc.).

## License

GPL v2 or later

## Credits

Inspired by [WP Content Importer v2](https://github.com/humanmade/WordPress-Importer) by Human Made.

