Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Don't add the wp:attachment_url for non-attachment post types.
Browse files Browse the repository at this point in the history
  • Loading branch information
pento committed Feb 24, 2021
1 parent ef77ef4 commit 1f013a1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/wxr/src/1.2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class WXRDriver {
return;
}

// Skip any fields that have a filter which fails for this data blob.
if ( field.filter && ! field.filter( data ) ) {
return;
}

// If the field is read-only, get the default value.
if ( field.hasOwnProperty( 'writeable' ) && ! field.writeable ) {
validatedData[ field.name ] = field.default(
Expand Down
1 change: 1 addition & 0 deletions packages/wxr/src/1.2/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ module.exports = {
name: 'attachment_url',
type: 'string',
element: 'wp:attachment_url',
filter: ( data ) => data.type === 'attachment',
},
{
name: 'terms',
Expand Down
48 changes: 47 additions & 1 deletion packages/wxr/src/1.2/test/__snapshots__/posts.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ exports[`Posts All fields are written as expected 1`] = `
<wp:post_type><![CDATA[post]]></wp:post_type>
<wp:post_password><![CDATA[a-strong-password]]></wp:post_password>
<wp:is_sticky>NaN</wp:is_sticky>
<wp:attachment_url><![CDATA[https://make.wordpress.org]]></wp:attachment_url>
<category domain=\\"category\\" nicename=\\"some-cat\\">Some Category</category>
<category domain=\\"tag\\" nicename=\\"some-tag\\">Some Tag</category>
<category domain=\\"my-tax\\" nicename=\\"some-term\\">Some Term</category>
Expand All @@ -76,3 +75,50 @@ exports[`Posts All fields are written as expected 1`] = `
</channel>
</rss>"
`;
exports[`Posts attachment_url is only included for the attachment post type 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?>
<!-- This is a WordPress eXtended RSS file generated by the \\"Free (as in Speech)\\" browser extension, as an export of your site. -->
<!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
<!-- You may use this file to transfer that content from one site to another. -->
<!-- This file is not intended to serve as a complete backup of your site. -->
<!-- To import this information into a WordPress site follow these steps: -->
<!-- 1. Log in to that site as an administrator. -->
<!-- 2. Go to Tools: Import in the WordPress admin panel. -->
<!-- 3. Install the \\"WordPress\\" importer from the list. -->
<!-- 4. Activate & Run Importer. -->
<!-- 5. Upload this file using the form provided on that page. -->
<!-- 6. You will first be asked to map the authors in this export file to users -->
<!-- on the site. For each author, you may choose to map to an -->
<!-- existing user on the site or to create a new user. -->
<!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
<!-- contained in this file into your site. -->
<rss version=\\"2.0\\"
xmlns:excerpt=\\"http://wordpress.org/export/1.2/excerpt/\\"
xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\"
xmlns:wfw=\\"http://wellformedweb.org/CommentAPI/\\"
xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\"
xmlns:wp=\\"http://wordpress.org/export/1.2/\\"
>
<channel>
<item>
<title><![CDATA[A Page Title]]></title>
<description></description>
<wp:comment_status><![CDATA[open]]></wp:comment_status>
<wp:post_type><![CDATA[page]]></wp:post_type>
<wp:is_sticky>0</wp:is_sticky>
</item>
<item>
<title><![CDATA[An Attachment Title]]></title>
<description></description>
<wp:comment_status><![CDATA[open]]></wp:comment_status>
<wp:post_type><![CDATA[attachment]]></wp:post_type>
<wp:is_sticky>0</wp:is_sticky>
<wp:attachment_url><![CDATA[https://make.wordpress.org]]></wp:attachment_url>
</item>
</channel>
</rss>"
`;
21 changes: 21 additions & 0 deletions packages/wxr/src/1.2/test/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,25 @@ describe( 'Posts', () => {

expect( xml ).toMatchSnapshot();
} );

test( 'attachment_url is only included for the attachment post type', async () => {
const wxr = new WXRDriver();
await wxr.connect();

wxr.addPost( {
title: 'A Page Title',
type: 'page',
attachment_url: 'https://make.wordpress.org', // Won't be written for this post type.
} );

wxr.addPost( {
title: 'An Attachment Title',
type: 'attachment',
attachment_url: 'https://make.wordpress.org',
} );

const xml = await wxr.export();

expect( xml ).toMatchSnapshot();
} );
} );

0 comments on commit 1f013a1

Please sign in to comment.