Skip to content

Commit 3c78416

Browse files
committed
Filter post meta while its post is the global post
Callbacks of the `wxr_export_skip_postmeta` filter can rely on the global post, which is only set to the post the meta belongs to in exportify_post(). Load the meta for the whole batch, but filter it there instead of while loading the batch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS
1 parent fdd9b15 commit 3c78416

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

‎features/export.feature‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,19 @@ Feature: Export content.
13781378
And save STDOUT as {COMMENT_ID}
13791379
And I run `wp comment meta add {COMMENT_ID} my_comment_meta_key my_comment_meta_value`
13801380
And I run `wp comment create --comment_post_ID={LAST_POST_ID} --comment_content='Spam comment' --comment_approved=spam`
1381+
And a wp-content/mu-plugins/skip-meta.php file:
1382+
"""
1383+
<?php
1384+
// Only export meta while the post it belongs to is the current post.
1385+
add_filter(
1386+
'wxr_export_skip_postmeta',
1387+
function ( $skip, $meta_key, $meta ) {
1388+
return $skip || (int) $meta->post_id !== get_the_ID();
1389+
},
1390+
10,
1391+
3
1392+
);
1393+
"""
13811394
And a check-export.php file:
13821395
"""
13831396
<?php

‎src/WP_Export_Query.php‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class WP_Export_Query {
3737
/**
3838
* Terms, meta and comments of the posts in the current batch, keyed by post ID.
3939
*
40-
* @var array<int, array{terms: array<\WP_Term>, meta: array<object>, comments: array<object>}>
40+
* @var array<int, array{terms: array<\WP_Term>, meta: array<\stdClass>, comments: array<\stdClass>}>
4141
*/
4242
private $batch_data = [];
4343

@@ -177,7 +177,7 @@ public function exportify_post( $post ) {
177177

178178
if ( isset( $this->batch_data[ $post->ID ] ) ) {
179179
$post->terms = $this->batch_data[ $post->ID ]['terms'];
180-
$post->meta = $this->batch_data[ $post->ID ]['meta'];
180+
$post->meta = self::filter_meta_for_export( $this->batch_data[ $post->ID ]['meta'] );
181181
$post->comments = $this->batch_data[ $post->ID ]['comments'];
182182
unset( $this->batch_data[ $post->ID ] );
183183
} else {
@@ -444,7 +444,7 @@ private function process_orphaned_terms( $terms ) {
444444
*
445445
* Querying them for a whole batch at once avoids several queries per exported post.
446446
* The data is the same as what get_terms_for_post(), get_meta_for_post() and
447-
* get_comments_for_post() return for each post.
447+
* get_comments_for_post() return for each post, except that the meta is not filtered yet.
448448
*
449449
* @param int $post_id ID of the first post of the batch.
450450
*/
@@ -509,14 +509,8 @@ static function ( $a, $b ) {
509509

510510
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- integers only
511511
$meta_from_db = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE post_id IN ($in_ids) ORDER BY post_id, meta_id" );
512+
// The meta is filtered in exportify_post(), while the post it belongs to is the global post.
512513
foreach ( $meta_from_db as $meta ) {
513-
if ( '_edit_lock' === $meta->meta_key ) {
514-
continue;
515-
}
516-
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
517-
if ( apply_filters( 'wxr_export_skip_postmeta', false, $meta->meta_key, $meta ) ) {
518-
continue;
519-
}
520514
$meta_by_post[ (int) $meta->post_id ][] = $meta;
521515
}
522516

@@ -559,6 +553,20 @@ private static function get_meta_for_post( $post ) {
559553
global $wpdb;
560554
$meta_for_export = [];
561555
$meta_from_db = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
556+
return self::filter_meta_for_export( $meta_from_db );
557+
}
558+
559+
/**
560+
* Remove the meta that should not be exported.
561+
*
562+
* Called while the post the meta belongs to is the global post, as callbacks
563+
* of the `wxr_export_skip_postmeta` filter may rely on it.
564+
*
565+
* @param array<\stdClass> $meta_from_db Meta rows of a post.
566+
* @return array<\stdClass>
567+
*/
568+
private static function filter_meta_for_export( $meta_from_db ) {
569+
$meta_for_export = [];
562570
foreach ( $meta_from_db as $meta ) {
563571
if ( '_edit_lock' === $meta->meta_key ) {
564572
continue;

0 commit comments

Comments
 (0)