140 | | /** |
141 | | * Wrap given string in XML CDATA tag. |
142 | | * |
143 | | * @since 2.1.0 |
144 | | * |
145 | | * @param string $str String to wrap in XML CDATA tag. |
146 | | * @return string |
147 | | */ |
148 | | function wxr_cdata( $str ) { |
149 | | if ( ! seems_utf8( $str ) ) { |
150 | | $str = utf8_encode( $str ); |
151 | | } |
152 | | // $str = ent2ncr(esc_html($str)); |
153 | | $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; |
154 | | |
155 | | return $str; |
156 | | } |
157 | | |
158 | | /** |
159 | | * Return the URL of the site |
160 | | * |
161 | | * @since 2.5.0 |
162 | | * |
163 | | * @return string Site URL. |
164 | | */ |
165 | | function wxr_site_url() { |
166 | | // Multisite: the base URL. |
167 | | if ( is_multisite() ) |
168 | | return network_home_url(); |
169 | | // WordPress (single site): the blog URL. |
170 | | else |
171 | | return get_bloginfo_rss( 'url' ); |
172 | | } |
173 | | |
174 | | /** |
175 | | * Output a cat_name XML tag from a given category object |
176 | | * |
177 | | * @since 2.1.0 |
178 | | * |
179 | | * @param object $category Category Object |
180 | | */ |
181 | | function wxr_cat_name( $category ) { |
182 | | if ( empty( $category->name ) ) |
183 | | return; |
184 | | |
185 | | echo '<wp:cat_name>' . wxr_cdata( $category->name ) . "</wp:cat_name>\n"; |
186 | | } |
187 | | |
188 | | /** |
189 | | * Output a category_description XML tag from a given category object |
190 | | * |
191 | | * @since 2.1.0 |
192 | | * |
193 | | * @param object $category Category Object |
194 | | */ |
195 | | function wxr_category_description( $category ) { |
196 | | if ( empty( $category->description ) ) |
197 | | return; |
198 | | |
199 | | echo '<wp:category_description>' . wxr_cdata( $category->description ) . "</wp:category_description>\n"; |
200 | | } |
201 | | |
202 | | /** |
203 | | * Output a tag_name XML tag from a given tag object |
204 | | * |
205 | | * @since 2.3.0 |
206 | | * |
207 | | * @param object $tag Tag Object |
208 | | */ |
209 | | function wxr_tag_name( $tag ) { |
210 | | if ( empty( $tag->name ) ) |
211 | | return; |
212 | | |
213 | | echo '<wp:tag_name>' . wxr_cdata( $tag->name ) . "</wp:tag_name>\n"; |
214 | | } |
215 | | |
216 | | /** |
217 | | * Output a tag_description XML tag from a given tag object |
218 | | * |
219 | | * @since 2.3.0 |
220 | | * |
221 | | * @param object $tag Tag Object |
222 | | */ |
223 | | function wxr_tag_description( $tag ) { |
224 | | if ( empty( $tag->description ) ) |
225 | | return; |
226 | | |
227 | | echo '<wp:tag_description>' . wxr_cdata( $tag->description ) . "</wp:tag_description>\n"; |
228 | | } |
229 | | |
230 | | /** |
231 | | * Output a term_name XML tag from a given term object |
232 | | * |
233 | | * @since 2.9.0 |
234 | | * |
235 | | * @param object $term Term Object |
236 | | */ |
237 | | function wxr_term_name( $term ) { |
238 | | if ( empty( $term->name ) ) |
239 | | return; |
240 | | |
241 | | echo '<wp:term_name>' . wxr_cdata( $term->name ) . "</wp:term_name>\n"; |
242 | | } |
243 | | |
244 | | /** |
245 | | * Output a term_description XML tag from a given term object |
246 | | * |
247 | | * @since 2.9.0 |
248 | | * |
249 | | * @param object $term Term Object |
250 | | */ |
251 | | function wxr_term_description( $term ) { |
252 | | if ( empty( $term->description ) ) |
253 | | return; |
254 | | |
255 | | echo "\t\t<wp:term_description>" . wxr_cdata( $term->description ) . "</wp:term_description>\n"; |
256 | | } |
257 | | |
258 | | /** |
259 | | * Output termmeta XML tags for a given term object. |
260 | | * |
261 | | * @since 4.6.0 |
262 | | * |
263 | | * @param WP_Term $term Term object. |
264 | | */ |
265 | | function wxr_term_meta( $term ) { |
266 | | global $wpdb; |
267 | | |
268 | | $termmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->termmeta WHERE term_id = %d", $term->term_id ) ); |
269 | | |
270 | | foreach ( $termmeta as $meta ) { |
271 | | /** |
272 | | * Filters whether to selectively skip term meta used for WXR exports. |
273 | | * |
274 | | * Returning a truthy value to the filter will skip the current meta |
275 | | * object from being exported. |
276 | | * |
277 | | * @since 4.6.0 |
278 | | * |
279 | | * @param bool $skip Whether to skip the current piece of term meta. Default false. |
280 | | * @param string $meta_key Current meta key. |
281 | | * @param object $meta Current meta object. |
282 | | */ |
283 | | if ( ! apply_filters( 'wxr_export_skip_termmeta', false, $meta->meta_key, $meta ) ) { |
284 | | printf( "\t\t<wp:termmeta>\n\t\t\t<wp:meta_key>%s</wp:meta_key>\n\t\t\t<wp:meta_value>%s</wp:meta_value>\n\t\t</wp:termmeta>\n", wxr_cdata( $meta->meta_key ), wxr_cdata( $meta->meta_value ) ); |
285 | | } |
286 | | } |
287 | | } |
288 | | |
289 | | /** |
290 | | * Output list of authors with posts |
291 | | * |
292 | | * @since 3.1.0 |
293 | | * |
294 | | * @global wpdb $wpdb WordPress database abstraction object. |
295 | | * |
296 | | * @param array $post_ids Array of post IDs to filter the query by. Optional. |
297 | | */ |
298 | | function wxr_authors_list( array $post_ids = null ) { |
299 | | global $wpdb; |
300 | | |
301 | | if ( !empty( $post_ids ) ) { |
302 | | $post_ids = array_map( 'absint', $post_ids ); |
303 | | $and = 'AND ID IN ( ' . implode( ', ', $post_ids ) . ')'; |
304 | | } else { |
305 | | $and = ''; |
306 | | } |
307 | | |
308 | | $authors = array(); |
309 | | $results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft' $and" ); |
310 | | foreach ( (array) $results as $result ) |
311 | | $authors[] = get_userdata( $result->post_author ); |
312 | | |
313 | | $authors = array_filter( $authors ); |
314 | | |
315 | | foreach ( $authors as $author ) { |
316 | | echo "\t<wp:author>"; |
317 | | echo '<wp:author_id>' . intval( $author->ID ) . '</wp:author_id>'; |
318 | | echo '<wp:author_login>' . wxr_cdata( $author->user_login ) . '</wp:author_login>'; |
319 | | echo '<wp:author_email>' . wxr_cdata( $author->user_email ) . '</wp:author_email>'; |
320 | | echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>'; |
321 | | echo '<wp:author_first_name>' . wxr_cdata( $author->first_name ) . '</wp:author_first_name>'; |
322 | | echo '<wp:author_last_name>' . wxr_cdata( $author->last_name ) . '</wp:author_last_name>'; |
323 | | echo "</wp:author>\n"; |
324 | | } |
325 | | } |
326 | | |
327 | | /** |
328 | | * Ouput all navigation menu terms |
329 | | * |
330 | | * @since 3.1.0 |
331 | | */ |
332 | | function wxr_nav_menu_terms() { |
333 | | $nav_menus = wp_get_nav_menus(); |
334 | | if ( empty( $nav_menus ) || ! is_array( $nav_menus ) ) |
335 | | return; |
336 | | |
337 | | foreach ( $nav_menus as $menu ) { |
338 | | echo "\t<wp:term>"; |
339 | | echo '<wp:term_id>' . intval( $menu->term_id ) . '</wp:term_id>'; |
340 | | echo '<wp:term_taxonomy>nav_menu</wp:term_taxonomy>'; |
341 | | echo '<wp:term_slug>' . wxr_cdata( $menu->slug ) . '</wp:term_slug>'; |
342 | | wxr_term_name( $menu ); |
343 | | echo "</wp:term>\n"; |
344 | | } |
345 | | } |
346 | | |
347 | | /** |
348 | | * Output list of taxonomy terms, in XML tag format, associated with a post |
349 | | * |
350 | | * @since 2.3.0 |
351 | | */ |
352 | | function wxr_post_taxonomy() { |
353 | | $post = get_post(); |
354 | | |
355 | | $taxonomies = get_object_taxonomies( $post->post_type ); |
356 | | if ( empty( $taxonomies ) ) |
357 | | return; |
358 | | $terms = wp_get_object_terms( $post->ID, $taxonomies ); |
359 | | |
360 | | foreach ( (array) $terms as $term ) { |
361 | | echo "\t\t<category domain=\"{$term->taxonomy}\" nicename=\"{$term->slug}\">" . wxr_cdata( $term->name ) . "</category>\n"; |
362 | | } |
363 | | } |
364 | | |
365 | | /** |
366 | | * |
367 | | * @param bool $return_me |
368 | | * @param string $meta_key |
369 | | * @return bool |
370 | | */ |
371 | | function wxr_filter_postmeta( $return_me, $meta_key ) { |
372 | | if ( '_edit_lock' == $meta_key ) |
373 | | $return_me = true; |
374 | | return $return_me; |
375 | | } |
| 358 | |
| 359 | /** |
| 360 | * Wrap given string in XML CDATA tag. |
| 361 | * |
| 362 | * @since 2.1.0 |
| 363 | * |
| 364 | * @param string $str String to wrap in XML CDATA tag. |
| 365 | * @return string |
| 366 | */ |
| 367 | function wxr_cdata( $str ) { |
| 368 | if ( ! seems_utf8( $str ) ) { |
| 369 | $str = utf8_encode( $str ); |
| 370 | } |
| 371 | // $str = ent2ncr(esc_html($str)); |
| 372 | $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; |
| 373 | |
| 374 | return $str; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Return the URL of the site |
| 379 | * |
| 380 | * @since 2.5.0 |
| 381 | * |
| 382 | * @return string Site URL. |
| 383 | */ |
| 384 | function wxr_site_url() { |
| 385 | // Multisite: the base URL. |
| 386 | if ( is_multisite() ) |
| 387 | return network_home_url(); |
| 388 | // WordPress (single site): the blog URL. |
| 389 | else |
| 390 | return get_bloginfo_rss( 'url' ); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Output a cat_name XML tag from a given category object |
| 395 | * |
| 396 | * @since 2.1.0 |
| 397 | * |
| 398 | * @param object $category Category Object |
| 399 | */ |
| 400 | function wxr_cat_name( $category ) { |
| 401 | if ( empty( $category->name ) ) |
| 402 | return; |
| 403 | |
| 404 | echo '<wp:cat_name>' . wxr_cdata( $category->name ) . "</wp:cat_name>\n"; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Output a category_description XML tag from a given category object |
| 409 | * |
| 410 | * @since 2.1.0 |
| 411 | * |
| 412 | * @param object $category Category Object |
| 413 | */ |
| 414 | function wxr_category_description( $category ) { |
| 415 | if ( empty( $category->description ) ) |
| 416 | return; |
| 417 | |
| 418 | echo '<wp:category_description>' . wxr_cdata( $category->description ) . "</wp:category_description>\n"; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Output a tag_name XML tag from a given tag object |
| 423 | * |
| 424 | * @since 2.3.0 |
| 425 | * |
| 426 | * @param object $tag Tag Object |
| 427 | */ |
| 428 | function wxr_tag_name( $tag ) { |
| 429 | if ( empty( $tag->name ) ) |
| 430 | return; |
| 431 | |
| 432 | echo '<wp:tag_name>' . wxr_cdata( $tag->name ) . "</wp:tag_name>\n"; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Output a tag_description XML tag from a given tag object |
| 437 | * |
| 438 | * @since 2.3.0 |
| 439 | * |
| 440 | * @param object $tag Tag Object |
| 441 | */ |
| 442 | function wxr_tag_description( $tag ) { |
| 443 | if ( empty( $tag->description ) ) |
| 444 | return; |
| 445 | |
| 446 | echo '<wp:tag_description>' . wxr_cdata( $tag->description ) . "</wp:tag_description>\n"; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Output a term_name XML tag from a given term object |
| 451 | * |
| 452 | * @since 2.9.0 |
| 453 | * |
| 454 | * @param object $term Term Object |
| 455 | */ |
| 456 | function wxr_term_name( $term ) { |
| 457 | if ( empty( $term->name ) ) |
| 458 | return; |
| 459 | |
| 460 | echo '<wp:term_name>' . wxr_cdata( $term->name ) . "</wp:term_name>\n"; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Output a term_description XML tag from a given term object |
| 465 | * |
| 466 | * @since 2.9.0 |
| 467 | * |
| 468 | * @param object $term Term Object |
| 469 | */ |
| 470 | function wxr_term_description( $term ) { |
| 471 | if ( empty( $term->description ) ) |
| 472 | return; |
| 473 | |
| 474 | echo "\t\t<wp:term_description>" . wxr_cdata( $term->description ) . "</wp:term_description>\n"; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Output termmeta XML tags for a given term object. |
| 479 | * |
| 480 | * @since 4.6.0 |
| 481 | * |
| 482 | * @param WP_Term $term Term object. |
| 483 | */ |
| 484 | function wxr_term_meta( $term ) { |
| 485 | global $wpdb; |
| 486 | |
| 487 | $termmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->termmeta WHERE term_id = %d", $term->term_id ) ); |
| 488 | |
| 489 | foreach ( $termmeta as $meta ) { |
| 490 | /** |
| 491 | * Filters whether to selectively skip term meta used for WXR exports. |
| 492 | * |
| 493 | * Returning a truthy value to the filter will skip the current meta |
| 494 | * object from being exported. |
| 495 | * |
| 496 | * @since 4.6.0 |
| 497 | * |
| 498 | * @param bool $skip Whether to skip the current piece of term meta. Default false. |
| 499 | * @param string $meta_key Current meta key. |
| 500 | * @param object $meta Current meta object. |
| 501 | */ |
| 502 | if ( ! apply_filters( 'wxr_export_skip_termmeta', false, $meta->meta_key, $meta ) ) { |
| 503 | printf( "\t\t<wp:termmeta>\n\t\t\t<wp:meta_key>%s</wp:meta_key>\n\t\t\t<wp:meta_value>%s</wp:meta_value>\n\t\t</wp:termmeta>\n", wxr_cdata( $meta->meta_key ), wxr_cdata( $meta->meta_value ) ); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Output list of authors with posts |
| 510 | * |
| 511 | * @since 3.1.0 |
| 512 | * |
| 513 | * @global wpdb $wpdb WordPress database abstraction object. |
| 514 | * |
| 515 | * @param array $post_ids Array of post IDs to filter the query by. Optional. |
| 516 | */ |
| 517 | function wxr_authors_list( array $post_ids = null ) { |
| 518 | global $wpdb; |
| 519 | |
| 520 | if ( !empty( $post_ids ) ) { |
| 521 | $post_ids = array_map( 'absint', $post_ids ); |
| 522 | $and = 'AND ID IN ( ' . implode( ', ', $post_ids ) . ')'; |
| 523 | } else { |
| 524 | $and = ''; |
| 525 | } |
| 526 | |
| 527 | $authors = array(); |
| 528 | $results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft' $and" ); |
| 529 | foreach ( (array) $results as $result ) |
| 530 | $authors[] = get_userdata( $result->post_author ); |
| 531 | |
| 532 | $authors = array_filter( $authors ); |
| 533 | |
| 534 | foreach ( $authors as $author ) { |
| 535 | echo "\t<wp:author>"; |
| 536 | echo '<wp:author_id>' . intval( $author->ID ) . '</wp:author_id>'; |
| 537 | echo '<wp:author_login>' . wxr_cdata( $author->user_login ) . '</wp:author_login>'; |
| 538 | echo '<wp:author_email>' . wxr_cdata( $author->user_email ) . '</wp:author_email>'; |
| 539 | echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>'; |
| 540 | echo '<wp:author_first_name>' . wxr_cdata( $author->first_name ) . '</wp:author_first_name>'; |
| 541 | echo '<wp:author_last_name>' . wxr_cdata( $author->last_name ) . '</wp:author_last_name>'; |
| 542 | echo "</wp:author>\n"; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Ouput all navigation menu terms |
| 548 | * |
| 549 | * @since 3.1.0 |
| 550 | */ |
| 551 | function wxr_nav_menu_terms() { |
| 552 | $nav_menus = wp_get_nav_menus(); |
| 553 | if ( empty( $nav_menus ) || ! is_array( $nav_menus ) ) |
| 554 | return; |
| 555 | |
| 556 | foreach ( $nav_menus as $menu ) { |
| 557 | echo "\t<wp:term>"; |
| 558 | echo '<wp:term_id>' . intval( $menu->term_id ) . '</wp:term_id>'; |
| 559 | echo '<wp:term_taxonomy>nav_menu</wp:term_taxonomy>'; |
| 560 | echo '<wp:term_slug>' . wxr_cdata( $menu->slug ) . '</wp:term_slug>'; |
| 561 | wxr_term_name( $menu ); |
| 562 | echo "</wp:term>\n"; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Output list of taxonomy terms, in XML tag format, associated with a post |
| 568 | * |
| 569 | * @since 2.3.0 |
| 570 | */ |
| 571 | function wxr_post_taxonomy() { |
| 572 | $post = get_post(); |
| 573 | |
| 574 | $taxonomies = get_object_taxonomies( $post->post_type ); |
| 575 | if ( empty( $taxonomies ) ) |
| 576 | return; |
| 577 | $terms = wp_get_object_terms( $post->ID, $taxonomies ); |
| 578 | |
| 579 | foreach ( (array) $terms as $term ) { |
| 580 | echo "\t\t<category domain=\"{$term->taxonomy}\" nicename=\"{$term->slug}\">" . wxr_cdata( $term->name ) . "</category>\n"; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * |
| 586 | * @param bool $return_me |
| 587 | * @param string $meta_key |
| 588 | * @return bool |
| 589 | */ |
| 590 | function wxr_filter_postmeta( $return_me, $meta_key ) { |
| 591 | if ( '_edit_lock' == $meta_key ) |
| 592 | $return_me = true; |
| 593 | return $return_me; |
| 594 | } |
| 595 | No newline at end of file |