PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.8.9
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.8.9
4.9.2 4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 All 200 releases
← All changes | includes/Admin/WPExporter.php +725 -191 4.8.23.8.9 View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 2
3 3 namespace WPDeveloper\BetterDocs\Admin;
4 4
5 -use WPDeveloper\BetterDocs\Admin\ExportDefaults;
5 +use WP_Post;
6 6 use WP_Term;
7 7 use wpdb;
8 8
9 9
@@ -10,21 +10,45 @@
10 10 if ( ! defined( 'ABSPATH' ) ) {
11 11 exit; // Exit if accessed directly.
12 12 }
13 13
14 -/**
15 - * SQL building helpers below interpolate WP-provided $wpdb table identifiers
16 - * (`$wpdb->posts`, `$wpdb->term_relationships`, `$wpdb->term_taxonomy`) and use
17 - * dynamic %d placeholder lists derived from integer arrays. Suppressing the
18 - * related PHPCS notices class-wide rather than per-call.
14 +/*
15 + * Originally made by WordPress.
19 16 *
20 - * phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
21 - * phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
22 - * phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
23 - * phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
17 + * What changed (by Elementor):
18 + * Remove echos.
19 + * Fix indents.
20 + * Add methods
21 + * indent.
22 + * wxr_categories_list.
23 + * wxr_tags_list.
24 + * wxr_terms_list.
25 + * wxr_posts_list.
26 + *
27 + * What changed (by Templately)
28 + * Added post__in capabilities for query.
29 + * nav_menu_item from terms
30 + * Some indent, and data type fixes
31 + * query_args added as default args
24 32 */
33 +
25 34 #[\AllowDynamicProperties]
26 35 class WPExporter {
36 + const WXR_VERSION = '1.2';
37 +
38 + private static $default_args = [
39 + 'content' => 'all',
40 + 'author' => false,
41 + 'category' => false,
42 + 'start_date' => false,
43 + 'end_date' => false,
44 + 'status' => false,
45 + 'offset' => 0,
46 + 'limit' => -1,
47 + 'meta_query' => [], // If specified `meta_key` then will include all post(s) that have this meta_key.
48 + 'query_args' => []
49 + ];
50 +
27 51 /**
28 52 * @var array
29 53 */
30 54 private $args;
@@ -33,22 +57,28 @@
33 57 * @var wpdb
34 58 */
35 59 private $wpdb;
36 60
37 - public function __construct( array $args = [] ) {
38 - global $wpdb;
61 + private $terms;
39 62
40 - $this->args = wp_parse_args($args, ExportDefaults::get_default_args());
63 + private $nav_menu_terms;
41 64
42 - $this->wpdb = $wpdb;
43 - }
65 + public function run(): array {
66 + // Handle glossaries export (as taxonomy)
67 + if ( 'glossaries' === $this->args['content'] ) {
68 + return $this->handle_glossaries_export();
69 + }
44 70
45 - public function exporter_ids(): array {
46 - // Build the base query
47 - $query_parts = $this->build_base_query();
48 - $where = $query_parts['where'];
49 - $join = $query_parts['join'];
71 + // Early return for invalid post types
72 + if ( $this->args['content'] !== 'docs' ) {
73 + return [];
74 + }
50 75
76 + // Build the base query
77 + $query_parts = $this->build_base_query();
78 + $where = $query_parts['where'];
79 + $join = $query_parts['join'];
80 +
51 81 // Handle post status
52 82 $where .= $this->build_status_condition();
53 83
54 84 // Handle specific posts selection
@@ -72,10 +102,9 @@
72 102
73 103 // Handle additional filters (author, dates, meta)
74 104 $where .= $this->build_additional_filters();
75 105
76 - // $join/$where are composed from prepared fragments above; identifiers are WP-provided.
77 - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter
106 + // Get the main doc post IDs
78 107 $post_ids = $this->wpdb->get_col( "SELECT DISTINCT {$this->wpdb->posts}.ID FROM {$this->wpdb->posts} $join WHERE $where" );
79 108
80 109 // Handle FAQ posts separately
81 110 $faq_post_ids = [];
@@ -85,70 +114,71 @@
85 114
86 115 // Combine post IDs
87 116 $all_post_ids = array_merge( $post_ids, $faq_post_ids );
88 117
89 - $all_post_ids = WPMLSupport::expand_with_translations( $all_post_ids );
90 -
91 118 // Handle featured images
92 119 $thumbnail_ids = $this->get_thumbnail_ids( $all_post_ids );
93 120
94 - // Generate final post IDs array
95 - return array_unique(array_merge($all_post_ids, $thumbnail_ids));
121 + // Generate final post IDs array
122 + $final_post_ids = array_unique( array_merge( $all_post_ids, $thumbnail_ids ) );
96 123
97 - }
124 + return [
125 + 'success' => true,
126 + 'data' => [
127 + 'filename' => 'betterdocs.' . date( 'Y-m-d' ) . '.xml',
128 + 'filetype' => 'text/xml',
129 + 'download' => $this->get_xml_export( $final_post_ids ),
130 + ]
131 + ];
132 + }
98 133
99 - // private function handle_glossaries_export(): array {
100 - public function get_glossary_term_ids(): array {
101 - if ( isset( $this->args['glossary_terms'] ) && ( count( $this->args['glossary_terms'] ) > 0 ) ) {
102 - $glossary_term_ids = [];
103 - foreach( $this->args['glossary_terms'] as $glossary_slug ) {
104 - $term_object = get_term_by('slug', $glossary_slug , 'glossaries');
105 - if( isset( $term_object->term_id ) && ! empty( $term_object->term_id ) ){
106 - array_push($glossary_term_ids, $term_object->term_id);
107 - }
108 - }
109 - } else {
110 - // $this->wpdb->term_taxonomy is a WP-core table identifier; %s placeholder binds taxonomy name.
111 - // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter
112 - $glossary_term_ids = $this->wpdb->get_col(
113 - $this->wpdb->prepare(
114 - "SELECT term_id FROM {$this->wpdb->term_taxonomy} WHERE taxonomy = %s",
115 - (string) $this->args['content']
116 - )
117 - );
118 - // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter
119 - }
134 + private function handle_glossaries_export(): array {
135 + if ( isset( $this->args['glossary_terms'] ) && ( count( $this->args['glossary_terms'] ) > 0 ) ) {
136 + $glossary_term_ids = [];
137 + foreach ( $this->args['glossary_terms'] as $glossary_slug ) {
138 + $term_object = get_term_by( 'slug', $glossary_slug, 'glossaries' );
139 + if ( isset( $term_object->term_id ) && ! empty( $term_object->term_id ) ) {
140 + array_push( $glossary_term_ids, $term_object->term_id );
141 + }
142 + }
143 + } else {
144 + $glossary_term_ids = $this->wpdb->get_col( "SELECT term_id from {$this->wpdb->term_taxonomy} where taxonomy='{$this->args['content']}';" );
145 + }
120 146
121 - return $glossary_term_ids;
122 - }
147 + $filename = 'betterdocs.' . date( 'Y-m-d' ) . '.xml';
148 + return [
149 + 'success' => true,
150 + 'data' => [
151 + 'filename' => $filename,
152 + 'filetype' => 'text/xml',
153 + 'download' => $this->get_xml_export( $glossary_term_ids ),
154 + ]
155 + ];
156 + }
123 157
124 - // The query-builder methods below interpolate only $wpdb core table names
125 - // ({$this->wpdb->posts}, etc.) — never user input — and bind every value via
126 - // %s/%d placeholders, so the InterpolatedNotPrepared warnings are spurious.
127 - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
128 - public function build_base_query(): array {
129 - $where = $this->wpdb->prepare("{$this->wpdb->posts}.post_type = %s", 'docs');
130 - return [
131 - 'where' => $where,
132 - 'join' => ''
133 - ];
134 - }
158 + private function build_base_query(): array {
159 + $where = $this->wpdb->prepare( "{$this->wpdb->posts}.post_type = %s", 'docs' );
160 + return [
161 + 'where' => $where,
162 + 'join' => ''
163 + ];
164 + }
135 165
136 - public function build_status_condition(): string {
137 - if ($this->args['status']) {
138 - return $this->wpdb->prepare(" AND {$this->wpdb->posts}.post_status = %s", $this->args['status']);
139 - }
140 - return " AND {$this->wpdb->posts}.post_status != 'auto-draft'";
141 - }
166 + private function build_status_condition(): string {
167 + if ( $this->args['status'] ) {
168 + return $this->wpdb->prepare( " AND {$this->wpdb->posts}.post_status = %s", $this->args['status'] );
169 + }
170 + return " AND {$this->wpdb->posts}.post_status != 'auto-draft'";
171 + }
142 172
143 - public function build_post_in_condition(): string {
144 - $ids = $this->args['post__in'];
145 - $ids_placeholder = implode(', ', array_fill(0, count($ids), '%d'));
146 - return $this->wpdb->prepare(" AND {$this->wpdb->posts}.ID IN ($ids_placeholder)", $ids);
147 - }
173 + private function build_post_in_condition(): string {
174 + $ids = $this->args['post__in'];
175 + $ids_placeholder = implode( ', ', array_fill( 0, count( $ids ), '%d' ) );
176 + return $this->wpdb->prepare( " AND {$this->wpdb->posts}.ID IN ($ids_placeholder)", $ids );
177 + }
148 178
149 - public function build_category_query(): array {
150 - $join = "INNER JOIN {$this->wpdb->term_relationships} tr ON ({$this->wpdb->posts}.ID = tr.object_id)
179 + private function build_category_query(): array {
180 + $join = "INNER JOIN {$this->wpdb->term_relationships} tr ON ({$this->wpdb->posts}.ID = tr.object_id)
151 181 INNER JOIN {$this->wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
152 182
153 183 $where = " AND tt.taxonomy = 'doc_category'";
154 184
@@ -172,10 +202,10 @@
172 202 'where' => $where
173 203 ];
174 204 }
175 205
176 - public function build_kb_query(): array {
177 - $join = "INNER JOIN {$this->wpdb->term_relationships} tr ON ({$this->wpdb->posts}.ID = tr.object_id)
206 + private function build_kb_query(): array {
207 + $join = "INNER JOIN {$this->wpdb->term_relationships} tr ON ({$this->wpdb->posts}.ID = tr.object_id)
178 208 INNER JOIN {$this->wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
179 209
180 210 $where = " AND tt.taxonomy = 'knowledge_base'";
181 211
@@ -199,10 +229,10 @@
199 229 'where' => $where
200 230 ];
201 231 }
202 232
203 - public function build_additional_filters(): string {
204 - $where = '';
233 + private function build_additional_filters(): string {
234 + $where = '';
205 235
206 236 if ( $this->args['author'] ) {
207 237 $where .= $this->wpdb->prepare( " AND {$this->wpdb->posts}.post_author = %d", $this->args['author'] );
208 238 }
@@ -223,22 +253,21 @@
223 253
224 254 return $where;
225 255 }
226 256
227 - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
228 - public function get_faq_posts(): array {
229 - return get_posts([
230 - 'numberposts' => -1,
231 - 'post_type' => 'betterdocs_faq',
232 - 'fields' => 'ids',
233 - 'post_status' => 'publish',
234 - // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.SuppressFilters_suppress_filters -- intentional: export the raw, untranslated FAQ set so multilingual filters don't drop or swap rows during export.
235 - 'suppress_filters' => true,
236 - ]);
237 - }
257 + private function get_faq_posts(): array {
258 + return get_posts(
259 + [
260 + 'numberposts' => -1,
261 + 'post_type' => 'betterdocs_faq',
262 + 'fields' => 'ids',
263 + 'post_status' => 'publish'
264 + ]
265 + );
266 + }
238 267
239 - public function get_thumbnail_ids(array $post_ids): array {
240 - $thumbnail_ids = [];
268 + private function get_thumbnail_ids( array $post_ids ): array {
269 + $thumbnail_ids = [];
241 270
242 271 if ( ! empty( $this->args['include_post_featured_image_as_attachment'] ) ) {
243 272 foreach ( $post_ids as $post_id ) {
244 273 $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
@@ -257,9 +286,9 @@
257 286 * @param int $columns
258 287 *
259 288 * @return string
260 289 */
261 - public function indent( int $columns = 1 ): string {
290 + private function indent( int $columns = 1 ): string {
262 291
263 292 $output = str_repeat( "\t", $columns );
264 293
265 294 return (string) $output;
@@ -264,121 +293,324 @@
264 293
265 294 return (string) $output;
266 295 }
267 296
268 - /**
269 - * Retrieve terms associated with the specified object IDs and sort them based on term meta.
270 - *
271 - * @param array $post_ids An array of object IDs.
272 - * @return array An array of WP_Term objects sorted based on term meta.
273 - */
274 - public function get_terms(array $post_ids = []) {
275 - // Get the object taxonomies
276 - $taxonomies = get_object_taxonomies($this->args['content']);
297 + /**
298 + * Return wrapped given string in XML CDATA tag.
299 + *
300 + * @param string $str String to wrap in XML CDATA tag.
301 + *
302 + * @return string
303 + */
304 + private function wxr_cdata( $str ): string {
305 + $str = (string) $str;
277 306
278 - $terms = [];
307 + if ( ! seems_utf8( $str ) ) {
308 + $str = mb_convert_encoding( $str, 'UTF-8', 'ISO-8859-1' );
309 + }
279 310
280 - // Handle glossaries
281 - if ($this->args['content'] == 'glossaries') {
282 - $terms = get_terms([
283 - 'taxonomy' => 'glossaries',
284 - 'include' => $post_ids,
285 - 'hide_empty' => false,
286 - ]);
311 + $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
287 312
288 - if (is_wp_error($terms)) {
289 - return [];
290 - }
313 + return $str;
314 + }
291 315
292 - // Include parent terms for hierarchical glossaries
293 - $parent_terms = [];
294 - foreach ($terms as $term) {
295 - if (!empty($term->parent)) {
296 - $parent = get_term($term->parent, 'glossaries');
297 - if (!is_wp_error($parent)) {
298 - $parent_terms[] = $parent;
299 - }
300 - }
301 - }
316 + /**
317 + * Return the URL of the site.
318 + *
319 + * @return string Site URL.
320 + */
321 + private function wxr_site_url(): string {
322 + if ( is_multisite() ) {
323 + // Multisite: the base URL.
324 + return network_home_url();
325 + } else {
326 + // WordPress (single site): the blog URL.
327 + return get_bloginfo_rss( 'url' );
328 + }
329 + }
302 330
303 - if (!empty($parent_terms)) {
304 - $terms = array_merge($terms, $parent_terms);
305 - $terms = array_unique($terms, SORT_REGULAR);
306 - }
307 - } elseif ($this->args['content'] == 'docs') { // Handle docs
308 - if (isset($this->args['selected_docs']) && !empty($this->args['selected_docs'])) {
309 - if ($this->args['selected_docs'][0] == 'all') {
310 - // Fetch all terms
311 - $terms = get_terms([
312 - 'taxonomy' => $taxonomies,
313 - 'hide_empty' => false,
314 - ]);
315 - } else {
316 - // Fetch terms for specific post IDs
317 - $terms = wp_get_object_terms($this->args['selected_docs'], $taxonomies);
318 - }
319 - }
320 - } elseif (in_array($this->args['content'], ['knowledge_base', 'doc_category'])) {
321 - // Handle both knowledge_base and doc_category cases
322 - $terms_key = $this->args['content'] == 'doc_category' ? 'category_terms' : 'kb_terms';
331 + /**
332 + * Return a cat_name XML tag from a given category object.
333 + *
334 + * @param WP_Term $category Category Object
335 + *
336 + * @return string
337 + */
338 + private function wxr_cat_name( $category ): string {
339 + if ( empty( $category->name ) ) {
340 + return '';
341 + }
323 342
324 - if (isset($this->args[$terms_key]) && !empty($this->args[$terms_key])) {
325 - $terms_arg = [
326 - 'taxonomy' => $taxonomies,
327 - 'hide_empty' => false,
328 - ];
343 + return $this->indent( 3 ) . '<wp:cat_name>' . $this->wxr_cdata( $category->name ) . '</wp:cat_name>' . PHP_EOL;
344 + }
329 345
330 - if ($this->args[$terms_key][0] !== 'all') {
331 - $terms_arg['slug'] = $this->args[$terms_key];
332 - }
346 + /**
347 + * Return a category_description XML tag from a given category object.
348 + *
349 + * @param WP_Term $category Category Object
350 + *
351 + * @return string
352 + */
353 + private function wxr_category_description( $category ): string {
354 + if ( empty( $category->description ) ) {
355 + return '';
356 + }
333 357
334 - $terms = get_terms($terms_arg);
335 - }
336 - }
358 + return $this->indent( 3 ) . '<wp:category_description>' . $this->wxr_cdata( $category->description ) . "</wp:category_description>\n";
359 + }
337 360
338 - // Include both parent and child terms for hierarchical structures
339 - if (!empty($terms) && is_array($terms)) {
340 - $additional_terms = [];
361 + /**
362 + * Return a tag_name XML tag from a given tag object.
363 + *
364 + * @param WP_Term $tag Tag Object
365 + *
366 + * @return string
367 + */
368 + private function wxr_tag_name( $tag ): string {
369 + if ( empty( $tag->name ) ) {
370 + return '';
371 + }
341 372
342 - foreach ($terms as $term) {
343 - // Get parent terms
344 - if (!empty($term->parent)) {
345 - $ancestors = get_ancestors($term->term_id, $term->taxonomy, 'taxonomy');
346 - foreach ($ancestors as $ancestor_id) {
347 - $parent = get_term($ancestor_id, $term->taxonomy);
348 - if (!is_wp_error($parent)) {
349 - $additional_terms[] = $parent;
350 - }
351 - }
352 - }
373 + return $this->indent( 3 ) . '<wp:tag_name>' . $this->wxr_cdata( $tag->name ) . '</wp:tag_name>' . PHP_EOL;
374 + }
353 375
354 - // Get child terms
355 - $children = get_term_children($term->term_id, $term->taxonomy);
356 - if (!is_wp_error($children)) {
357 - foreach ($children as $child_id) {
358 - $child = get_term($child_id, $term->taxonomy);
359 - if (!is_wp_error($child)) {
360 - $additional_terms[] = $child;
361 - }
362 - }
363 - }
364 - }
376 + /**
377 + * Return a tag_description XML tag from a given tag object.
378 + *
379 + * @param WP_Term $tag Tag Object
380 + *
381 + * @return string
382 + */
383 + private function wxr_tag_description( $tag ): string {
384 + if ( empty( $tag->description ) ) {
385 + return '';
386 + }
365 387
366 - if (!empty($additional_terms)) {
367 - $terms = array_merge($terms, $additional_terms);
368 - $terms = array_unique($terms, SORT_REGULAR);
369 - }
370 - }
388 + return $this->indent( 3 ) . '<wp:tag_description>' . $this->wxr_cdata( $tag->description ) . '</wp:tag_description>' . PHP_EOL;
389 + }
371 390
372 - // Sort terms if not empty
373 - if (!empty($terms) && is_array($terms)) {
374 - usort($terms, array($this, 'compare_terms_by_meta'));
375 - }
391 + /**
392 + * Return a term_name XML tag from a given term object.
393 + *
394 + * @param WP_Term $term Term Object
395 + *
396 + * @return string
397 + */
398 + private function wxr_term_name( $term ): string {
399 + if ( empty( $term->name ) ) {
400 + return '';
401 + }
376 402
377 - return $terms;
378 - }
403 + return $this->indent( 3 ) . '<wp:term_name>' . $this->wxr_cdata( $term->name ) . '</wp:term_name>' . PHP_EOL;
404 + }
379 405
380 406 /**
407 + * Return a term_description XML tag from a given term object.
408 + *
409 + * @param WP_Term $term Term Object
410 + *
411 + * @return string
412 + */
413 + private function wxr_term_description( $term ): string {
414 + if ( empty( $term->description ) ) {
415 + return '';
416 + }
417 +
418 + return $this->indent( 3 ) . '<wp:term_description>' . $this->wxr_cdata( $term->description ) . '</wp:term_description>' . PHP_EOL;
419 + }
420 +
421 + /**
422 + * Return term meta XML tags for a given term object.
423 + *
424 + * @param WP_Term $term Term object.
425 + *
426 + * @return string
427 + */
428 + private function wxr_term_meta( $term ): string {
429 + $result = '';
430 + $termmeta = $this->wpdb->get_results( $this->wpdb->prepare( "SELECT * FROM {$this->wpdb->termmeta} WHERE term_id = %d", $term->term_id ) );// phpcs:ignore
431 +
432 + foreach ( $termmeta as $meta ) {
433 + /**
434 + * Filters whether to selectively skip term meta used for WXR exports.
435 + *
436 + * Returning a truthy value from the filter will skip the current meta
437 + * object from being exported.
438 + *
439 + * @param bool $skip Whether to skip the current piece of term meta. Default false.
440 + * @param string $meta_key Current meta key.
441 + * @param object $meta Current meta object.
442 + *
443 + * @since 4.6.0
444 + *
445 + */
446 + if ( ! apply_filters( 'wxr_export_skip_termmeta', false, $meta->meta_key, $meta ) ) {
447 + $result .= sprintf( $this->indent( 3 ) . "<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", $this->wxr_cdata( $meta->meta_key ), $this->wxr_cdata( $meta->meta_value ) );
448 + }
449 + }
450 +
451 + return $result;
452 + }
453 +
454 + /**
455 + * Return list of authors with posts.
456 + *
457 + * @param int[] $post_ids Optional. Array of post IDs to filter the query by.
458 + *
459 + * @return string
460 + */
461 + private function wxr_authors_list( array $post_ids = null ): string {
462 + $result = '';
463 +
464 + if ( ! empty( $post_ids ) ) {
465 + $post_ids = array_map( 'absint', $post_ids );
466 + $and = 'AND ID IN ( ' . implode( ', ', $post_ids ) . ')';
467 + } else {
468 + $and = '';
469 + }
470 +
471 + $authors = [];
472 + $results = $this->wpdb->get_results( "SELECT DISTINCT post_author FROM {$this->wpdb->posts} WHERE post_status != 'auto-draft' $and" );// phpcs:ignore
473 + foreach ( (array) $results as $r ) {
474 + $authors[] = get_userdata( $r->post_author );
475 + }
476 +
477 + $authors = array_filter( $authors );
478 +
479 + foreach ( $authors as $author ) {
480 + $result .= $this->indent( 2 ) . '<wp:author>' . PHP_EOL;
481 +
482 + $result .= $this->indent( 3 ) . '<wp:author_id>' . (int) $author->ID . '</wp:author_id>' . PHP_EOL;
483 + $result .= $this->indent( 3 ) . '<wp:author_login>' . $this->wxr_cdata( $author->user_login ) . '</wp:author_login>' . PHP_EOL;
484 + $result .= $this->indent( 3 ) . '<wp:author_email>' . $this->wxr_cdata( $author->user_email ) . '</wp:author_email>' . PHP_EOL;
485 + $result .= $this->indent( 3 ) . '<wp:author_display_name>' . $this->wxr_cdata( $author->display_name ) . '</wp:author_display_name>' . PHP_EOL;
486 + $result .= $this->indent( 3 ) . '<wp:author_first_name>' . $this->wxr_cdata( $author->first_name ) . '</wp:author_first_name>' . PHP_EOL;
487 + $result .= $this->indent( 3 ) . '<wp:author_last_name>' . $this->wxr_cdata( $author->last_name ) . '</wp:author_last_name>' . PHP_EOL;
488 +
489 + $result .= $this->indent( 2 ) . '</wp:author>' . PHP_EOL;
490 + }
491 +
492 + return $result;
493 + }
494 +
495 + /**
496 + * Return list of categories.
497 + *
498 + * @param array $cats
499 + *
500 + * @return string
501 + */
502 + private function wxr_categories_list( array $cats ): string {
503 + $result = '';
504 +
505 + foreach ( $cats as $c ) {
506 + $result .= $this->indent( 2 ) . '<wp:category>' . PHP_EOL;
507 +
508 + $result .= $this->indent( 3 ) . '<wp:term_id>' . (int) $c->term_id . '</wp:term_id>' . PHP_EOL;
509 + $result .= $this->indent( 3 ) . '<wp:category_nicename>' . $this->wxr_cdata( $c->slug ) . '</wp:category_nicename>' . PHP_EOL;
510 + $result .= $this->indent( 3 ) . '<wp:category_parent>' . $this->wxr_cdata( $c->parent ? $cats[ $c->parent ]->slug : '' ) . '</wp:category_parent>' . PHP_EOL;
511 + $result .= $this->wxr_cat_name( $c ) . $this->wxr_category_description( $c ) . $this->wxr_term_meta( $c );
512 +
513 + $result .= $this->indent( 2 ) . '</wp:category>' . PHP_EOL;
514 + }
515 +
516 + return $result;
517 + }
518 +
519 + /**
520 + * Return list of tags.
521 + *
522 + * @param array $tags
523 + *
524 + * @return string
525 + */
526 + private function wxr_tags_list( array $tags ): string {
527 + $result = '';
528 +
529 + foreach ( $tags as $t ) {
530 + $result .= $this->indent( 2 ) . '<wp:tag>' . PHP_EOL;
531 +
532 + $result .= $this->indent( 3 ) . '<wp:term_id>' . (int) $t->term_id . '</wp:term_id>' . PHP_EOL;
533 + $result .= $this->indent( 3 ) . '<wp:tag_slug>' . $this->wxr_cdata( $t->slug ) . '</wp:tag_slug>' . PHP_EOL;
534 + $result .= $this->wxr_tag_name( $t ) . $this->wxr_tag_description( $t ) . $this->wxr_term_meta( $t );
535 +
536 + $result .= $this->indent( 2 ) . '</wp:tag>' . PHP_EOL;
537 + }
538 +
539 + return $result;
540 + }
541 +
542 + public function get_parent_terms_slug( $terms, $term_id ) {
543 + $key = array_search( $term_id, array_column( $terms, 'term_id' ) );
544 +
545 + if ( $key !== false ) {
546 + return $terms[ $key ]->slug;
547 + }
548 +
549 + return null;
550 + }
551 +
552 + /**
553 + * Return list of terms.
554 + *
555 + * @param array $terms
556 + *
557 + * @return string
558 + */
559 + private function wxr_terms_list( array $terms ): string {
560 + $result = '';
561 + foreach ( $terms as $key => $t ) {
562 + $result .= $this->indent( 2 ) . '<wp:term>' . PHP_EOL;
563 +
564 + $result .= $this->indent( 3 ) . '<wp:term_id>' . $this->wxr_cdata( $t->term_id ) . '</wp:term_id>' . PHP_EOL;
565 + $result .= $this->indent( 3 ) . '<wp:term_taxonomy>' . $this->wxr_cdata( $t->taxonomy ) . '</wp:term_taxonomy>' . PHP_EOL;
566 + $result .= $this->indent( 3 ) . '<wp:term_slug>' . $this->wxr_cdata( $t->slug ) . '</wp:term_slug>' . PHP_EOL;
567 + if ( $t->parent ) {
568 + $result .= $this->indent( 3 ) . '<wp:term_parent>' . $this->wxr_cdata( $this->get_parent_terms_slug( $terms, $t->parent ) ) . '</wp:term_parent>' . PHP_EOL;
569 + }
570 + $result .= $this->wxr_term_name( $t ) . $this->wxr_term_description( $t ) . $this->wxr_term_meta( $t );
571 +
572 + $result .= $this->indent( 2 ) . '</wp:term>' . PHP_EOL;
573 + }
574 +
575 + return $result;
576 + }
577 +
578 + /**
579 + * Retrieve terms associated with the specified object IDs and sort them based on term meta.
580 + *
581 + * @param array $post_ids An array of object IDs.
582 + * @return array An array of WP_Term objects sorted based on term meta.
583 + */
584 + public function get_terms( array $post_ids ) {
585 + // Get the object taxonomies
586 + $taxonomies = get_object_taxonomies( $this->args['content'] );
587 +
588 + if ( isset( $this->args['selected_docs'] ) && ! empty( $this->args['selected_docs'] ) && $this->args['selected_docs'][0] == 'all' ) {
589 + $terms = get_terms(
590 + [
591 + 'taxonomy' => $taxonomies,
592 + 'hide_empty' => false,
593 + ]
594 + );
595 + } elseif ( $this->args['content'] == 'glossaries' ) {
596 + $terms = get_terms(
597 + [
598 + 'taxonomy' => 'glossaries',
599 + 'include' => $post_ids,
600 + 'hide_empty' => false,
601 + ]
602 + );
603 + } else {
604 + $terms = wp_get_object_terms( $post_ids, $taxonomies );
605 + }
606 +
607 + usort( $terms, array( $this, 'compare_terms_by_meta' ) );
608 +
609 + return $terms;
610 + }
611 +
612 + /**
381 613 * Compare terms based on their associated term meta values.
382 614 *
383 615 * @param WP_Term $a The first term object.
384 616 * @param WP_Term $b The second term object.
@@ -416,7 +648,309 @@
416 648
417 649 return $meta_a - $meta_b;
418 650 }
419 651
420 - return 0; // Default to no sorting if meta keys are not defined
421 - }
652 + return 0; // Default to no sorting if meta keys are not defined
653 + }
654 +
655 + /**
656 + * Return list of posts, by requested `$post_ids`.
657 + *
658 + * @param array $post_ids
659 + *
660 + * @return string
661 + */
662 + private function wxr_posts_list( array $post_ids ): string {
663 + $result = '';
664 +
665 + if ( $post_ids ) {
666 + global $wp_query;
667 +
668 + // Fake being in the loop.
669 + $wp_query->in_the_loop = true;
670 +
671 + // Fetch 20 posts at a time rather than loading the entire table into memory.
672 + while ( $next_posts = array_splice( $post_ids, 0, 20 ) ) {
673 + $where = 'WHERE ID IN (' . implode( ',', $next_posts ) . ')';
674 + $posts = $this->wpdb->get_results( "SELECT * FROM {$this->wpdb->posts} $where" );// phpcs:ignore
675 +
676 + // Begin Loop.
677 + foreach ( $posts as $post ) {
678 + setup_postdata( $post );
679 +
680 + $title = apply_filters( 'the_title_rss', $post->post_title );
681 +
682 + /**
683 + * Filters the post content used for WXR exports.
684 + *
685 + * @param string $post_content Content of the current post.
686 + *
687 + * @since 2.5.0
688 + *
689 + */
690 + $content = $this->wxr_cdata( apply_filters( 'the_content_export', $post->post_content ) );
691 +
692 + /**
693 + * Filters the post excerpt used for WXR exports.
694 + *
695 + * @param string $post_excerpt Excerpt for the current post.
696 + *
697 + * @since 2.6.0
698 + *
699 + */
700 + $excerpt = $this->wxr_cdata( apply_filters( 'the_excerpt_export', $post->post_excerpt ) );
701 +
702 + $result .= $this->indent( 2 ) . '<item>' . PHP_EOL;
703 +
704 + $result .= $this->indent( 3 ) . '<title>' . $title . '</title>' . PHP_EOL;
705 + $result .= $this->indent( 3 ) . '<link>' . esc_url( get_permalink() ) . '</link>' . PHP_EOL;
706 + $result .= $this->indent( 3 ) . '<pubDate>' . mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ) . '</pubDate>' . PHP_EOL;
707 + $result .= $this->indent( 3 ) . '<dc:creator>' . $this->wxr_cdata( get_the_author_meta( 'login' ) ) . '</dc:creator>' . PHP_EOL;
708 + $result .= $this->indent( 3 ) . '<guid isPermaLink="false">' . $this->wxr_cdata( get_the_author_meta( 'login' ) ) . '</guid>' . PHP_EOL;
709 + $result .= $this->indent( 3 ) . '<description></description>' . PHP_EOL;
710 + $result .= $this->indent( 3 ) . '<content:encoded>' . $content . '</content:encoded>' . PHP_EOL;
711 + $result .= $this->indent( 3 ) . '<excerpt:encoded>' . $excerpt . '</excerpt:encoded>' . PHP_EOL;
712 + $result .= $this->indent( 3 ) . '<wp:post_id>' . (int) $post->ID . '</wp:post_id>' . PHP_EOL;
713 + $result .= $this->indent( 3 ) . '<wp:post_date>' . $this->wxr_cdata( $post->post_date ) . '</wp:post_date>' . PHP_EOL;
714 + $result .= $this->indent( 3 ) . '<wp:post_date_gmt>' . $this->wxr_cdata( $post->post_date_gmt ) . '</wp:post_date_gmt>' . PHP_EOL;
715 + $result .= $this->indent( 3 ) . '<wp:comment_status>' . $this->wxr_cdata( $post->comment_status ) . '</wp:comment_status>' . PHP_EOL;
716 + $result .= $this->indent( 3 ) . '<wp:ping_status>' . $this->wxr_cdata( $post->ping_status ) . '</wp:ping_status>' . PHP_EOL;
717 + $result .= $this->indent( 3 ) . '<wp:post_name>' . $this->wxr_cdata( $post->post_name ) . '</wp:post_name>' . PHP_EOL;
718 + $result .= $this->indent( 3 ) . '<wp:status>' . $this->wxr_cdata( $post->post_status ) . '</wp:status>' . PHP_EOL;
719 + $result .= $this->indent( 3 ) . '<wp:post_parent>' . $this->wxr_cdata( $post->post_parent ) . '</wp:post_parent>' . PHP_EOL;
720 + $result .= $this->indent( 3 ) . '<wp:menu_order>' . (int) $post->menu_order . '</wp:menu_order>' . PHP_EOL;
721 + $result .= $this->indent( 3 ) . '<wp:post_type>' . $this->wxr_cdata( $post->post_type ) . '</wp:post_type>' . PHP_EOL;
722 + $result .= $this->indent( 3 ) . '<wp:post_password>' . $this->wxr_cdata( $post->post_password ) . '</wp:post_password>' . PHP_EOL;
723 + $result .= $this->indent( 3 ) . '<wp:is_sticky>' . ( is_sticky( $post->ID ) ? 1 : 0 ) . '</wp:is_sticky>' . PHP_EOL;
724 +
725 + if ( 'attachment' === $post->post_type ) {
726 + $result .= $this->indent( 3 ) . '<wp:attachment_url>' . $this->wxr_cdata( wp_get_attachment_url( $post->ID ) ) . '</wp:attachment_url>' . PHP_EOL;
727 + }
728 +
729 + $result .= $this->wxr_post_taxonomy( $post );
730 +
731 + $postmeta = $this->wpdb->get_results( $this->wpdb->prepare( "SELECT * FROM {$this->wpdb->postmeta} WHERE post_id = %d", $post->ID ) );// phpcs:ignore
732 + foreach ( $postmeta as $meta ) {
733 + /**
734 + * Filters whether to selectively skip post meta used for WXR exports.
735 + *
736 + * Returning a truthy value from the filter will skip the current meta
737 + * object from being exported.
738 + *
739 + * @param bool $skip Whether to skip the current post meta. Default false.
740 + * @param string $meta_key Current meta key.
741 + * @param object $meta Current meta object.
742 + *
743 + * @since 3.3.0
744 + *
745 + */
746 + if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) ) {
747 + continue;
748 + }
749 +
750 + $result .= $this->indent( 3 ) . '<wp:postmeta>' . PHP_EOL;
751 +
752 + $result .= $this->indent( 4 ) . '<wp:meta_key>' . $this->wxr_cdata( $meta->meta_key ) . '</wp:meta_key>' . PHP_EOL;
753 + $result .= $this->indent( 4 ) . '<wp:meta_value>' . $this->wxr_cdata( $meta->meta_value ) . '</wp:meta_value>' . PHP_EOL;
754 +
755 + $result .= $this->indent( 3 ) . '</wp:postmeta>' . PHP_EOL;
756 + }
757 +
758 + $_comments = $this->wpdb->get_results( $this->wpdb->prepare( "SELECT * FROM {$this->wpdb->comments} WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );// phpcs:ignore
759 + $comments = array_map( 'get_comment', $_comments );
760 + foreach ( $comments as $c ) {
761 + $result .= $this->indent( 3 ) . '<wp:comment>' . PHP_EOL;
762 +
763 + $result .= $this->indent( 4 ) . '<wp:comment_id>' . (int) $c->comment_ID . '</wp:comment_id>' . PHP_EOL;
764 + $result .= $this->indent( 4 ) . '<wp:comment_author>' . $this->wxr_cdata( $c->comment_author ) . '</wp:comment_author>' . PHP_EOL;
765 + $result .= $this->indent( 4 ) . '<wp:comment_author_email>' . $this->wxr_cdata( $c->comment_author_email ) . '</wp:comment_author_email>' . PHP_EOL;
766 + $result .= $this->indent( 4 ) . '<wp:comment_author_url>' . $this->wxr_cdata( $c->comment_author_url ) . '</wp:comment_author_url>' . PHP_EOL;
767 + $result .= $this->indent( 4 ) . '<wp:comment_author_IP>' . $this->wxr_cdata( $c->comment_author_IP ) . '</wp:comment_author_IP>' . PHP_EOL;
768 + $result .= $this->indent( 4 ) . '<wp:comment_date>' . $this->wxr_cdata( $c->comment_date ) . '</wp:comment_date>' . PHP_EOL;
769 + $result .= $this->indent( 4 ) . '<wp:comment_date_gmt>' . $this->wxr_cdata( $c->comment_date_gmt ) . '</wp:comment_date_gmt>' . PHP_EOL;
770 + $result .= $this->indent( 4 ) . '<wp:comment_content>' . $this->wxr_cdata( $c->comment_content ) . '</wp:comment_content>' . PHP_EOL;
771 + $result .= $this->indent( 4 ) . '<wp:comment_approved>' . $this->wxr_cdata( $c->comment_approved ) . '</wp:comment_approved>' . PHP_EOL;
772 + $result .= $this->indent( 4 ) . '<wp:comment_type>' . $this->wxr_cdata( $c->comment_type ) . '</wp:comment_type>' . PHP_EOL;
773 + $result .= $this->indent( 4 ) . '<wp:comment_parent>' . $this->wxr_cdata( $c->comment_parent ) . '</wp:comment_parent>' . PHP_EOL;
774 + $result .= $this->indent( 4 ) . '<wp:comment_user_id>' . (int) $c->user_id . '</wp:comment_user_id>' . PHP_EOL;
775 +
776 + $c_meta = $this->wpdb->get_results( $this->wpdb->prepare( "SELECT * FROM {$this->wpdb->commentmeta} WHERE comment_id = %d", $c->comment_ID ) );// phpcs:ignore
777 + foreach ( $c_meta as $meta ) {
778 + /**
779 + * Filters whether to selectively skip comment meta used for WXR exports.
780 + *
781 + * Returning a truthy value from the filter will skip the current meta
782 + * object from being exported.
783 + *
784 + * @param bool $skip Whether to skip the current comment meta. Default false.
785 + * @param string $meta_key Current meta key.
786 + * @param object $meta Current meta object.
787 + *
788 + * @since 4.0.0
789 + *
790 + */
791 + if ( apply_filters( 'wxr_export_skip_commentmeta', false, $meta->meta_key, $meta ) ) {
792 + continue;
793 + }
794 +
795 + $result .= $this->indent( 4 ) . '<wp:commentmeta>' . PHP_EOL;
796 +
797 + $result .= $this->indent( 5 ) . '<wp:meta_key>' . $this->wxr_cdata( $meta->meta_key ) . '</wp:meta_key>' . PHP_EOL;
798 + $result .= $this->indent( 5 ) . '<wp:meta_value>' . $this->wxr_cdata( $meta->meta_key ) . '</wp:meta_value>' . PHP_EOL;
799 +
800 + $result .= $this->indent( 4 ) . '</wp:commentmeta>' . PHP_EOL;
801 + }
802 +
803 + $result .= $this->indent( 3 ) . '</wp:comment>' . PHP_EOL;
804 + }
805 +
806 + $result .= $this->indent( 2 ) . '</item>' . PHP_EOL;
807 + }
808 + }
809 + }
810 +
811 + return $result;
812 + }
813 +
814 + /**
815 + * Return all navigation menu terms
816 + *
817 + * @return string
818 + */
819 + private function wxr_nav_menu_terms(): string {
820 + $args = [];
821 +
822 + if ( ! empty( $this->nav_menu_terms ) ) {
823 + $args['include'] = $this->nav_menu_terms;
824 + }
825 +
826 + $nav_menus = wp_get_nav_menus( $args );
827 + if ( empty( $nav_menus ) || ! is_array( $nav_menus ) ) {
828 + return '';
829 + }
830 +
831 + $result = '';
832 +
833 + foreach ( $nav_menus as $menu ) {
834 + $this->terms[ $menu->term_id ] = $menu;
835 +
836 + $result .= $this->indent( 2 ) . '<wp:term>' . PHP_EOL;
837 + $result .= $this->indent( 3 ) . '<wp:term_id>' . (int) $menu->term_id . '</wp:term_id>' . PHP_EOL;
838 + $result .= $this->indent( 3 ) . '<wp:term_taxonomy>nav_menu</wp:term_taxonomy>' . PHP_EOL;
839 + $result .= $this->indent( 3 ) . '<wp:term_slug>' . $this->wxr_cdata( $menu->slug ) . '</wp:term_slug>' . PHP_EOL;
840 + $result .= $this->indent( 3 ) . '<wp:term_name>' . $this->wxr_cdata( $menu->name ) . '</wp:term_name>' . PHP_EOL;
841 + $result .= $this->indent( 2 ) . '</wp:term>' . PHP_EOL;
842 + }
843 +
844 + return $result;
845 + }
846 +
847 + /**
848 + * Return list of taxonomy terms, in XML tag format, associated with a post
849 + *
850 + * @param object $post
851 + *
852 + * @return string
853 + */
854 + private function wxr_post_taxonomy( $post ): string {
855 + $result = '';
856 +
857 + $taxonomies = get_object_taxonomies( $post->post_type );
858 +
859 + if ( empty( $taxonomies ) ) {
860 + return $result;
861 + }
862 +
863 + $terms = wp_get_object_terms( $post->ID, $taxonomies );
864 +
865 + foreach ( (array) $terms as $term ) {
866 + $result .= $this->indent( 3 ) . "<category domain=\"{$term->taxonomy}\" nicename=\"{$term->slug}\">" . $this->wxr_cdata( $term->name ) . '</category>' . PHP_EOL;
867 + }
868 +
869 + return $result;
870 + }
871 +
872 + /**
873 + * Get the XML export.
874 + *
875 + * @param array $post_ids
876 + *
877 + * @return string
878 + */
879 + private function get_xml_export( array $post_ids ): string {
880 + $charset = get_bloginfo( 'charset' );
881 + $generator = get_the_generator( 'export' );
882 + $wxr_version = self::WXR_VERSION;
883 + $wxr_site_url = $this->wxr_site_url();
884 + $rss_info_name = get_bloginfo_rss( 'name' );
885 + $rss_info_url = get_bloginfo_rss( 'url' );
886 + $rss_info_description = get_bloginfo_rss( 'description' );
887 + $rss_info_language = get_bloginfo_rss( 'language' );
888 + $pub_date = gmdate( 'D, d M Y H:i:s +0000' );
889 +
890 + $dynamic = $this->wxr_authors_list( $post_ids );
891 +
892 + ob_start();
893 + /** This action is documented in wp-includes/feed-rss2.php */
894 + do_action( 'rss2_head' );
895 + $rss2_head = ob_get_clean();
896 +
897 + $dynamic .= $rss2_head;
898 +
899 + if ( 'all' === $this->args['content'] || 'nav_menu_item' === $this->args['content'] ) {
900 + $dynamic .= $this->wxr_nav_menu_terms();
901 + }
902 +
903 + $dynamic .= $this->wxr_terms_list( $this->get_terms( $post_ids ) );
904 + $dynamic .= $this->wxr_posts_list( $post_ids );
905 +
906 + $result = <<<EOT
907 +<?xml version="1.0" encoding="$charset" ?>
908 +<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
909 +<!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
910 +<!-- You may use this file to transfer that content from one site to another. -->
911 +<!-- This file is not intended to serve as a complete backup of your site. -->
912 +
913 +<!-- To import this information into a WordPress site follow these steps: -->
914 +<!-- 1. Log in to that site as an administrator. -->
915 +<!-- 2. Go to Tools: Import in the WordPress admin panel. -->
916 +<!-- 3. Install the "WordPress" importer from the list. -->
917 +<!-- 4. Activate & Run Importer. -->
918 +<!-- 5. Upload this file using the form provided on that page. -->
919 +<!-- 6. You will first be asked to map the authors in this export file to users -->
920 +<!-- on the site. For each author, you may choose to map to an -->
921 +<!-- existing user on the site or to create a new user. -->
922 +<!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
923 +<!-- contained in this file into your site. -->
924 +$generator
925 +<rss version="2.0"
926 + xmlns:excerpt="http://wordpress.org/export/$wxr_version/excerpt/"
927 + xmlns:content="http://purl.org/rss/1.0/modules/content/"
928 + xmlns:wfw="http://wellformedweb.org/CommentAPI/"
929 + xmlns:dc="http://purl.org/dc/elements/1.1/"
930 + xmlns:wp="http://wordpress.org/export/$wxr_version/"
931 +>
932 + <channel>
933 + <title>$rss_info_name</title>
934 + <link>$rss_info_url</link>
935 + <description>$rss_info_description</description>
936 + <pubDate>$pub_date</pubDate>
937 + <language>$rss_info_language</language>
938 + <wp:wxr_version>$wxr_version</wp:wxr_version>
939 + <wp:base_site_url>$wxr_site_url</wp:base_site_url>
940 + <wp:base_blog_url>$rss_info_url</wp:base_blog_url>
941 + $dynamic
942 + </channel>
943 +</rss>
944 +EOT;
945 +
946 + return $result;
947 + }
948 +
949 + public function __construct( array $args = [] ) {
950 + global $wpdb;
951 +
952 + $this->args = wp_parse_args( $args, self::$default_args );
953 +
954 + $this->wpdb = $wpdb;
955 + }
422 956 }