| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Migration\Export; |
| 4 |
|
| 5 |
use Code_Snippets\Model\Snippet; |
| 6 |
|
| 7 |
/** |
| 8 |
* Handles exporting the code from snippets. |
| 9 |
* |
| 10 |
* @package Code_Snippets |
| 11 |
*/ |
| 12 |
class Export_Code extends Export { |
| 13 |
|
| 14 |
/** |
| 15 |
* The type of snippet being exported. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected string $export_type; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor. |
| 23 |
* |
| 24 |
* @param array<int> $ids The IDs of the snippets to export. |
| 25 |
* @param bool|null $network Whether to export network-wide snippets. |
| 26 |
* @param string|null $export_type The type of snippet to export (e.g., 'php', 'html', 'css', 'js', 'cond'). |
| 27 |
*/ |
| 28 |
public function __construct( array $ids, ?bool $network = null, ?string $export_type = null ) { |
| 29 |
parent::__construct( $ids, $network ); |
| 30 |
|
| 31 |
if ( $export_type && in_array( $export_type, Snippet::get_types(), true ) ) { |
| 32 |
$this->export_type = $export_type; |
| 33 |
} else { |
| 34 |
// If no export type is specified, default to the type of the first snippet. |
| 35 |
$snippets_list = $this->get_snippets_list(); |
| 36 |
$this->export_type = $snippets_list[0]->type; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get the type of snippets being exported. |
| 42 |
* |
| 43 |
* @return string |
| 44 |
*/ |
| 45 |
public function get_export_type(): string { |
| 46 |
return $this->export_type; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get the file extension for the export format. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
public function get_file_extension(): string { |
| 55 |
return 'cond' === $this->export_type |
| 56 |
? 'json' |
| 57 |
: $this->export_type; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Bundle a snippets into a PHP file. |
| 62 |
*/ |
| 63 |
protected function export_snippets_php(): string { |
| 64 |
$result = "<?php\n"; |
| 65 |
|
| 66 |
foreach ( $this->get_snippets_list() as $snippet ) { |
| 67 |
$code = trim( $snippet->code ); |
| 68 |
|
| 69 |
if ( ( 'php' !== $snippet->type && 'html' !== $snippet->type ) || ! $code ) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
$result .= "\n/**\n * $snippet->display_name\n"; |
| 74 |
|
| 75 |
if ( ! empty( $snippet->desc ) ) { |
| 76 |
// Convert description to PhpDoc. |
| 77 |
$desc = wp_strip_all_tags( str_replace( "\n", "\n * ", $snippet->desc ) ); |
| 78 |
$result .= " *\n * $desc\n"; |
| 79 |
} |
| 80 |
|
| 81 |
$result .= " */\n"; |
| 82 |
|
| 83 |
if ( 'content' === $snippet->scope ) { |
| 84 |
$shortcode_tag = apply_filters( 'code_snippets_export_shortcode_tag', "code_snippets_export_$snippet->id", $snippet ); |
| 85 |
|
| 86 |
$code = sprintf( |
| 87 |
"add_shortcode( '%s', function () {\n\tob_start();\n\t?>\n\n\t%s\n\n\t<?php\n\treturn ob_get_clean();\n} );", |
| 88 |
$shortcode_tag, |
| 89 |
str_replace( "\n", "\n\t", $code ) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
$result .= "$code\n"; |
| 94 |
} |
| 95 |
|
| 96 |
return $result; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Export conditions in JSON format. |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
protected function export_conditions_json(): string { |
| 105 |
$conditions_data = []; |
| 106 |
$fields_to_copy = [ 'name', 'desc', 'tags' ]; |
| 107 |
|
| 108 |
foreach ( $this->get_snippets_list() as $snippet ) { |
| 109 |
$condition_data = []; |
| 110 |
|
| 111 |
if ( ! $snippet->code || 'cond' !== $snippet->type ) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
$rules = json_decode( $snippet->code, false ); |
| 116 |
|
| 117 |
if ( json_last_error() !== JSON_ERROR_NONE ) { |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
foreach ( $fields_to_copy as $field ) { |
| 122 |
if ( ! empty( $snippet->$field ) ) { |
| 123 |
$condition_data[ $field ] = $snippet->$field; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
$condition_data['rules'] = $rules; |
| 128 |
$conditions_data[] = $condition_data; |
| 129 |
} |
| 130 |
|
| 131 |
return wp_json_encode( 1 === count( $conditions_data ) ? $conditions_data[0] : $conditions_data, JSON_PRETTY_PRINT ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Export snippets in a generic format, compatible with CSS or JavaScript. |
| 136 |
*/ |
| 137 |
protected function export_snippets_css_js(): string { |
| 138 |
$result = ''; |
| 139 |
|
| 140 |
foreach ( $this->get_snippets_list() as $snippet ) { |
| 141 |
$snippet = new Snippet( $snippet ); |
| 142 |
|
| 143 |
if ( $this->get_export_type() !== $snippet->type ) { |
| 144 |
continue; |
| 145 |
} |
| 146 |
|
| 147 |
$result .= "\n/*\n"; |
| 148 |
|
| 149 |
if ( $snippet->name ) { |
| 150 |
$result .= wp_strip_all_tags( $snippet->name ) . "\n\n"; |
| 151 |
} |
| 152 |
|
| 153 |
if ( ! empty( $snippet->desc ) ) { |
| 154 |
$result .= wp_strip_all_tags( $snippet->desc ) . "\n"; |
| 155 |
} |
| 156 |
|
| 157 |
$result .= "*/\n\n$snippet->code\n\n"; |
| 158 |
} |
| 159 |
|
| 160 |
return $result; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Generate a downloadable code file from the exported snippets. |
| 165 |
*/ |
| 166 |
public function generate_export(): string { |
| 167 |
switch ( $this->get_export_type() ) { |
| 168 |
case 'php': |
| 169 |
case 'html': |
| 170 |
return $this->export_snippets_php(); |
| 171 |
|
| 172 |
case 'cond': |
| 173 |
return $this->export_conditions_json(); |
| 174 |
|
| 175 |
default: |
| 176 |
return $this->export_snippets_css_js(); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|