| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Migration\Export; |
| 4 |
|
| 5 |
use Code_Snippets\Model\Snippet; |
| 6 |
use const Code_Snippets\PLUGIN_VERSION; |
| 7 |
|
| 8 |
/** |
| 9 |
* Handles exporting snippets from the site in various downloadable formats |
| 10 |
* |
| 11 |
* @package Code_Snippets |
| 12 |
*/ |
| 13 |
class Export_JSON extends Export { |
| 14 |
|
| 15 |
/** |
| 16 |
* Retrieve the file extension for the export format. |
| 17 |
* |
| 18 |
* @return string |
| 19 |
*/ |
| 20 |
public function get_file_extension(): string { |
| 21 |
return 'json'; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Bundle snippets together into JSON format. |
| 26 |
* |
| 27 |
* @return array<string, string|Snippet[]> Snippets as JSON object. |
| 28 |
*/ |
| 29 |
public function generate_export(): array { |
| 30 |
$snippets = []; |
| 31 |
|
| 32 |
foreach ( $this->get_snippets_list() as $snippet ) { |
| 33 |
$snippets[] = array_map( |
| 34 |
function ( $value ) { |
| 35 |
return is_string( $value ) ? |
| 36 |
str_replace( "\r\n", "\n", $value ) : |
| 37 |
$value; |
| 38 |
}, |
| 39 |
$snippet->get_modified_fields() |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
return [ |
| 44 |
'generator' => 'Code Snippets v' . PLUGIN_VERSION, |
| 45 |
'date_created' => gmdate( 'Y-m-d H:i' ), |
| 46 |
'snippets' => $snippets, |
| 47 |
]; |
| 48 |
} |
| 49 |
} |
| 50 |
|