| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Migration\Export; |
| 4 |
|
| 5 |
use Code_Snippets\Model\Snippet; |
| 6 |
use function Code_Snippets\get_snippets; |
| 7 |
|
| 8 |
/** |
| 9 |
* Handles exporting snippets from the site in a downloadable format. |
| 10 |
* |
| 11 |
* @package Code_Snippets |
| 12 |
*/ |
| 13 |
abstract class Export { |
| 14 |
|
| 15 |
/** |
| 16 |
* Array of snippet data fetched from the database |
| 17 |
* |
| 18 |
* @var Snippet[] |
| 19 |
*/ |
| 20 |
private array $snippets_list; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class constructor |
| 24 |
* |
| 25 |
* @param array<int> $ids List of snippet IDs to export. |
| 26 |
* @param bool|null $network Whether to fetch snippets from local or network table. |
| 27 |
*/ |
| 28 |
public function __construct( array $ids, ?bool $network = null ) { |
| 29 |
$this->snippets_list = get_snippets( $ids, $network ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the list of snippets to export. |
| 34 |
* |
| 35 |
* @return Snippet[] List of snippets to export. |
| 36 |
*/ |
| 37 |
protected function get_snippets_list(): array { |
| 38 |
return $this->snippets_list; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get the file extension for the export format. |
| 43 |
* |
| 44 |
* @return string File extension for the export format. |
| 45 |
*/ |
| 46 |
abstract public function get_file_extension(): string; |
| 47 |
|
| 48 |
/** |
| 49 |
* Build the export filename. |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public function build_filename(): string { |
| 54 |
if ( 1 === count( $this->snippets_list ) ) { |
| 55 |
// If there is only snippet to export, use its name instead of the site name. |
| 56 |
$title = strtolower( $this->snippets_list[0]->name ); |
| 57 |
} else { |
| 58 |
// Otherwise, use the site name as set in Settings > General. |
| 59 |
$title = strtolower( get_bloginfo( 'name' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
$filename = "$title.code-snippets.{$this->get_file_extension()}"; |
| 63 |
return apply_filters( 'code_snippets/export/filename', $filename, $title, $this->snippets_list ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Generate the export data in the specified format. |
| 68 |
* |
| 69 |
* @return mixed |
| 70 |
*/ |
| 71 |
abstract public function generate_export(); |
| 72 |
} |
| 73 |
|