| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Exports selected snippets to a XML or PHP file. |
| 5 |
* |
| 6 |
* It's better to call the export_snippets() |
| 7 |
* function than directly using this class |
| 8 |
* |
| 9 |
* @since 1.9 |
| 10 |
* @package Code_Snippets |
| 11 |
*/ |
| 12 |
class Code_Snippets_Export { |
| 13 |
|
| 14 |
/** |
| 15 |
* The IDs |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $snippet_ids = array(); |
| 19 |
|
| 20 |
/** |
| 21 |
* The name of the table to fetch snippets from |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $table_name = ''; |
| 25 |
|
| 26 |
/** |
| 27 |
* The export file format. |
| 28 |
* Either 'xml' or 'php' |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $format; |
| 32 |
|
| 33 |
/** |
| 34 |
* The DOM document. |
| 35 |
* Only used in XML exports |
| 36 |
* @var DOMDocument |
| 37 |
*/ |
| 38 |
protected $dom; |
| 39 |
|
| 40 |
/** |
| 41 |
* The DOM document root element |
| 42 |
* Only used in XML exports |
| 43 |
* @var DOMElement |
| 44 |
*/ |
| 45 |
protected $root; |
| 46 |
|
| 47 |
public static $supported_formats = array( 'json', 'xml', 'php' ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Constructor function |
| 51 |
* |
| 52 |
* @param array $ids The IDs of the snippets to export |
| 53 |
* @param string $table The name of the table to fetch snippets from |
| 54 |
* @param string $format The format of the export file |
| 55 |
*/ |
| 56 |
public function __construct( $ids, $table, $format = 'json' ) { |
| 57 |
$this->snippet_ids = (array) $ids; |
| 58 |
$this->table_name = $table; |
| 59 |
$this->format = in_array( $format, self::$supported_formats ) ? $format : 'json'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Build the export file name |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function get_filename() { |
| 67 |
|
| 68 |
if ( 1 == count( $this->snippet_ids ) ) { |
| 69 |
/* If there is only snippet to export, use its name instead of the site name */ |
| 70 |
$snippet = get_snippet( $this->snippet_ids[0], $this->table_name ); |
| 71 |
$title = strtolower( $snippet->name ); |
| 72 |
} else { |
| 73 |
/* Otherwise, use the site name as set in Settings > General */ |
| 74 |
$title = strtolower( get_bloginfo( 'name' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
$filename = "{$title}.code-snippets.{$this->format}"; |
| 78 |
$filename = apply_filters( 'code_snippets/export/filename', $filename, $title ); |
| 79 |
return sanitize_file_name( $filename ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Append header comments to the DOM document |
| 84 |
*/ |
| 85 |
protected function do_header_comments() { |
| 86 |
|
| 87 |
/* Array of translated comment lines */ |
| 88 |
$lines = array( |
| 89 |
__( 'This is a code snippets export file generated by the Code Snippets WordPress plugin.', 'code-snippets' ), |
| 90 |
'https://wordpress.org/plugins/code-snippets', |
| 91 |
__( 'To import these snippets a WordPress site follow these steps:', 'code-snippets' ), |
| 92 |
__( '1. Log in to that site as an administrator.', 'code-snippets' ), |
| 93 |
__( '2. Install the Code Snippets plugin using the directions provided at the above link.', 'code-snippets' ), |
| 94 |
__( "3. Go to 'Tools: Import' in the WordPress admin panel.", 'code-snippets' ), |
| 95 |
__( '4. Click on the "Code Snippets" importer in the list', 'code-snippets' ), |
| 96 |
__( '5. Upload this file using the form provided on that page.', 'code-snippets' ), |
| 97 |
__( '6. Code Snippets will then import all of the snippets and associated information contained in this file into your site.', 'code-snippets' ), |
| 98 |
__( "7. You will then have to visit the 'Snippets: All Snippets' admin menu and activate desired snippets.", 'code-snippets' ), |
| 99 |
); |
| 100 |
|
| 101 |
/* Add each line as a comment element */ |
| 102 |
foreach ( $lines as $line ) { |
| 103 |
$comment = $this->dom->createComment( " $line " ); |
| 104 |
$this->dom->appendChild( $comment ); |
| 105 |
} |
| 106 |
|
| 107 |
/* Build a generator line, like the WordPress export files */ |
| 108 |
$gen = sprintf( |
| 109 |
'generator="Code Snippets/%s" created="%s"', |
| 110 |
CODE_SNIPPETS_VERSION, |
| 111 |
date( 'Y-m-d H:i' ) |
| 112 |
); |
| 113 |
|
| 114 |
/* Run the generator line through the standard WordPress filter */ |
| 115 |
$type = 'code_snippets_export'; |
| 116 |
$gen = apply_filters( "get_the_generator_$type", $gen, $type ); |
| 117 |
|
| 118 |
/* Add it to the file as a comment */ |
| 119 |
$gen = $this->dom->createComment( " $gen " ); |
| 120 |
$this->dom->appendChild( $gen ); |
| 121 |
} |
| 122 |
|
| 123 |
protected function get_snippets() { |
| 124 |
global $wpdb; |
| 125 |
|
| 126 |
$snippets = array(); |
| 127 |
|
| 128 |
foreach ( $this->snippet_ids as $id ) { |
| 129 |
/* Grab the snippet from the database */ |
| 130 |
$snippet = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE id = %d", $id ), ARRAY_A ); |
| 131 |
$snippet = new Code_Snippet( $snippet ); |
| 132 |
|
| 133 |
$snippets[] = $snippet; |
| 134 |
} |
| 135 |
|
| 136 |
return $snippets; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Process all snippet items |
| 141 |
*/ |
| 142 |
protected function do_items() { |
| 143 |
|
| 144 |
if ( 'xml' === $this->format ) { |
| 145 |
|
| 146 |
/* Create the root element */ |
| 147 |
$root_element = $this->dom->createElement( 'snippets' ); |
| 148 |
$this->root = $this->dom->appendChild( $root_element ); |
| 149 |
} |
| 150 |
|
| 151 |
/* Loop through the snippets */ |
| 152 |
foreach ( $this->get_snippets() as $snippet ) { |
| 153 |
|
| 154 |
/* Process the snippet item */ |
| 155 |
if ( 'php' === $this->format ) { |
| 156 |
$this->do_item_php( $snippet ); |
| 157 |
} else { |
| 158 |
$this->do_item_xml( $snippet ); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Append a single snippet item to the document |
| 165 |
* |
| 166 |
* @param Code_Snippet $snippet |
| 167 |
*/ |
| 168 |
protected function do_item_xml( Code_Snippet $snippet ) { |
| 169 |
$item_element = $this->dom->createElement( 'snippet' ); |
| 170 |
$item = $this->root->appendChild( $item_element ); |
| 171 |
|
| 172 |
/* Set the scope attribute */ |
| 173 |
/** @var $item DOMElement */ |
| 174 |
$item->setAttribute( 'scope', $snippet->scope ); |
| 175 |
|
| 176 |
/* Fields to include in the export file */ |
| 177 |
$fields = array( |
| 178 |
'name' => $snippet->name, |
| 179 |
'desc' => $snippet->desc, |
| 180 |
'tags' => $snippet->tags_list, |
| 181 |
'code' => $snippet->code, |
| 182 |
); |
| 183 |
|
| 184 |
$fields = apply_filters( 'code_snippets/export/xml_fields', $fields, $snippet ); |
| 185 |
|
| 186 |
foreach ( $fields as $field_name => $field_value ) { |
| 187 |
|
| 188 |
/* Create a new element for each field */ |
| 189 |
$field_element = $this->dom->createElement( $field_name ); |
| 190 |
$field = $item->appendChild( $field_element ); |
| 191 |
|
| 192 |
/* Add the field content */ |
| 193 |
$value = $this->dom->createTextNode( $field_value ); |
| 194 |
$field->appendChild( $value ); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Format single snippet item as PHP code |
| 200 |
* |
| 201 |
* @param Code_Snippet $snippet |
| 202 |
*/ |
| 203 |
protected function do_item_php( Code_Snippet $snippet ) { |
| 204 |
echo "\n/**\n * {$snippet->name}\n"; |
| 205 |
|
| 206 |
if ( ! empty( $snippet->desc ) ) { |
| 207 |
|
| 208 |
/* Convert description to PhpDoc */ |
| 209 |
$desc = strip_tags( str_replace( "\n", "\n * ", $snippet->desc ) ); |
| 210 |
|
| 211 |
echo " *\n * $desc\n"; |
| 212 |
} |
| 213 |
|
| 214 |
echo " */\n{$snippet->code}\n"; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Export the snippets |
| 219 |
*/ |
| 220 |
public function do_export() { |
| 221 |
|
| 222 |
/* Make the page act like a downloadable file instead of being shown in the browser */ |
| 223 |
$filename = $this->get_filename(); |
| 224 |
header( 'Content-Disposition: attachment; filename=' . $filename ); |
| 225 |
|
| 226 |
if ( 'json' === $this->format ) { |
| 227 |
global $wpdb; |
| 228 |
|
| 229 |
/* Set the HTTP content type header */ |
| 230 |
header( 'Content-Type: application/json; charset=' . get_bloginfo( 'charset' ) ); |
| 231 |
|
| 232 |
$snippets = array(); |
| 233 |
|
| 234 |
foreach ( $this->snippet_ids as $id ) { |
| 235 |
/* Grab the snippet from the database */ |
| 236 |
$snippet = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE id = %d", $id ), ARRAY_A ); |
| 237 |
$snippet = new Code_Snippet( $snippet ); |
| 238 |
|
| 239 |
$fields = array( 'name', 'desc', 'tags', 'scope', 'code' ); |
| 240 |
$final_snippet = array(); |
| 241 |
|
| 242 |
foreach ( $fields as $field ) { |
| 243 |
if ( ! empty( $snippet->$field ) ) { |
| 244 |
$final_snippet[ $field ] = $snippet->$field; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if ( $final_snippet ) { |
| 249 |
$snippets[] = $final_snippet; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
$data = array( |
| 254 |
'generator' => 'Code Snippets v' . CODE_SNIPPETS_VERSION, |
| 255 |
'date_created' => date( 'Y-m-d H:i' ), |
| 256 |
'snippets' => $snippets, |
| 257 |
); |
| 258 |
|
| 259 |
echo json_encode( $data, apply_filters( 'code_snippets/export/json_encode_options', 0 ) ); |
| 260 |
exit; |
| 261 |
} |
| 262 |
|
| 263 |
if ( 'xml' === $this->format ) { |
| 264 |
|
| 265 |
/* Set the HTTP content type header */ |
| 266 |
header( 'Content-Type: text/xml; charset=' . get_bloginfo( 'charset' ) ); |
| 267 |
|
| 268 |
/* Create DOM document and root element */ |
| 269 |
$this->dom = new DOMDocument( '1.0', get_bloginfo( 'charset' ) ); |
| 270 |
$this->dom->formatOutput = true; |
| 271 |
|
| 272 |
/* Add file header comments */ |
| 273 |
$this->do_header_comments(); |
| 274 |
} |
| 275 |
|
| 276 |
elseif ( 'php' === $this->format ) { |
| 277 |
echo "<?php\n"; |
| 278 |
} |
| 279 |
|
| 280 |
/* Process the snippet items */ |
| 281 |
$this->do_items(); |
| 282 |
|
| 283 |
if ( 'xml' === $this->format ) { |
| 284 |
|
| 285 |
/* Filter DOM document */ |
| 286 |
apply_filters( 'code_snippets/export_dom_document', $this->dom, $this->snippet_ids, $filename ); |
| 287 |
|
| 288 |
/* Send the document to the browser */ |
| 289 |
echo $this->dom->saveXML(); |
| 290 |
} |
| 291 |
|
| 292 |
exit; |
| 293 |
} |
| 294 |
} |
| 295 |
|