* } */ abstract class FormatHandler { /** * @return array{version: string, date: string} */ protected function get_export_details() { return ExportDetails::get(); } /** * Set headers to force file download * * @return void */ public function force_download() { header( 'Cache-Control: no-cache, must-revalidate' ); header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); header( 'X-Content-Type-Options: nosniff' ); } /** * Generate export filename * * @param string $extension File extension (without dot). * @return string Generated filename. */ protected function export_filename( $extension ) { $name = wp_parse_url( home_url(), PHP_URL_HOST ); if ( $name === false || $name === null || $name === '' ) { $name = 'export'; } $name = sanitize_text_field( $name ); $name = str_replace( '.', '-', $name ); $date = strtolower( date_i18n( get_option( 'date_format' ) ) ); $date = str_replace( [ ',', ' ', '--' ], '-', $date ); return 'redirection-' . $name . '-' . $date . '.' . sanitize_text_field( $extension ); } /** * Get export data for items and groups * * @param array<\Red_Item> $items Redirect items to export. * @param array $groups Groups to export. * @return string Formatted export data. */ abstract public function get_data( array $items, array $groups ); /** * Load and import data from a file * * @param ImportGroup $group Group resolver to import into. * @param ImportRedirect $redirect Redirect saver. * @param string $filename Path to the file to import. * @param bool $is_dry_run Whether this is a dry run. * @param array $options Import options. * @return ImportResult */ abstract public function load( $group, $redirect, $filename, $is_dry_run, array $options = [] ); /** * @param ImportGroup $group Group resolver. * @param ImportRedirect $redirect Redirect saver. * @return ImportResult */ protected function get_import_result( ImportGroup $group, ImportRedirect $redirect ) { return [ 'created' => $redirect->get_created(), 'updated' => $redirect->get_updated(), 'ignored' => $redirect->get_ignored(), 'groups_created' => $group->get_groups_created(), 'groups_updated' => $group->get_groups_updated(), 'groups_ignored' => $group->get_groups_ignored(), 'logs_imported' => 0, 'errors_imported' => 0, 'settings_imported' => 0, 'preview' => $redirect->get_preview_items(), ]; } /** * Number of items skipped during the last get_data() call, for formats * that only support a subset of what Redirection can represent. Formats * that always export everything they're given can ignore this. * * @return int */ public function get_skipped_count() { return 0; } /** * Number of items actually written during the last get_data() call. * * Formats can exclude items for reasons other than being unsupported (eg * disabled redirects), so this can be lower than `total - get_skipped_count()`. * Returns null when a format doesn't track this, meaning every given item * that wasn't counted as skipped was written. * * @return int|null */ public function get_exported_count() { return null; } }