PluginProbe
Code Snippets / 3.10.1
Code Snippets v3.10.1
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / Migration / Export / Export.php

Export.php in Code Snippets 3.10.1, at php/Migration/Export/Export.php

73 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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