File: /home/globfdxw/www/wp-content/plugins/wpforms-entry-automation/src/Export/FormatterManager.php
<?php
namespace WPFormsEntryAutomation\Export;
use WPFormsEntryAutomation\Export\Formatter\CSV;
use WPFormsEntryAutomation\Export\Formatter\Formatter;
use WPFormsEntryAutomation\Export\Formatter\JSON;
use WPFormsEntryAutomation\Export\Formatter\PDF;
use WPFormsEntryAutomation\Export\Formatter\XLSX;
use WPFormsEntryAutomation\Plugin;
/**
* Class FormatterManager.
*
* @since 1.0.0
*/
class FormatterManager {
/**
* Available format types.
*
* @since 1.0.0
*
* @var array
*/
private $formats;
/**
* Temporary directory path.
*
* @since 1.0.0
*
* @var string
*/
private $temp_dir = '';
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct() {
$this->formats = [
'csv' => new CSV( $this ),
'xlsx' => new XLSX( $this ),
'json' => new JSON( $this ),
'pdf' => new PDF( $this ),
];
}
/**
* Get available format types.
*
* @since 1.0.0
*
* @return array
*/
public function get_formats(): array {
/**
* Filter the available format types.
*
* @since 1.0.0
*
* @param array $formats Format types.
*/
return (array) apply_filters( 'wpforms_entry_automation_export_formatter_manager_get_formats', $this->formats );
}
/**
* Get available format labels.
*
* @since 1.0.0
*
* @return array Array of format keys and their display labels.
*/
public function get_formats_labels(): array {
$labels = [];
foreach ( $this->get_formats() as $key => $formatter ) {
$labels[ $key ] = $formatter->get_title();
}
return $labels;
}
/**
* Get a specific formatter instance.
*
* @since 1.0.0
*
* @param string $format Format type.
*
* @return Formatter|null Formatter instance or null if not found.
*/
public function get_formatter( string $format ): ?Formatter {
$formats = $this->get_formats();
return $formats[ $format ] ?? null;
}
/**
* Format entries.
*
* @since 1.0.0
*
* @param array $task_data Task data.
* @param array $entries Entries data.
* @param array $form_data Form data.
* @param string $existing_file_path Existing file path for appending, if any.
*
* @return string File path or empty string on failure.
*/
public function format_entries( array $task_data, array $entries, array $form_data, string $existing_file_path = '' ): string {
$connection = $form_data['settings'][ Plugin::SLUG ][ $task_data['connection_id'] ?? '' ] ?? [];
$format = $connection['format'] ?? '';
$formatter = $this->get_formatter( $format );
if ( ! $formatter ) {
return '';
}
return $formatter->format(
$entries,
$task_data,
$form_data,
$connection,
$existing_file_path
);
}
/**
* Tmp files directory.
*
* @since 1.0.0
*
* @return string Temporary files directory path.
*/
private function get_base_temp_dir(): string {
$upload_dir = wpforms_upload_dir();
$upload_path = $upload_dir['path'];
$export_path = trailingslashit( $upload_path ) . 'export';
if ( ! file_exists( $export_path ) ) {
wp_mkdir_p( $export_path );
}
// Check if the .htaccess exists in the upload directory, if not - create it.
wpforms_create_upload_dir_htaccess_file();
// Check if the index.html exists in the directories, if not - create it.
wpforms_create_index_html_file( $upload_path );
wpforms_create_index_html_file( $export_path );
// Normalize slashes for Windows.
return wp_normalize_path( $export_path );
}
/**
* Set the temporary directory path.
*
* @since 1.0.0
*
* @param string $temp_dir Path to the temporary directory.
*/
public function set_temp_dir( string $temp_dir ): void {
$this->temp_dir = $temp_dir;
}
/**
* Retrieve the temporary directory.
*
* @since 1.0.0
*
* @return string Path to the temporary directory.
*/
public function get_temp_dir(): string {
if ( ! $this->temp_dir ) {
$this->set_temp_dir( $this->get_base_temp_dir() );
}
return $this->temp_dir;
}
}