Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importWxr: Preserve backslashes in the imported content #1213

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<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>
<title>My WordPress Website</title>
<link>https://playground.wordpress.net/scope:0.6224554755515266</link>
<description></description>
<pubDate>Mon, 08 Apr 2024 07:03:05 +0000</pubDate>
<language>en-US</language>
<wp:wxr_version>1.2</wp:wxr_version>
<wp:base_site_url>https://playground.wordpress.net/scope:0.6224554755515266</wp:base_site_url>
<wp:base_blog_url>https://playground.wordpress.net/scope:0.6224554755515266</wp:base_blog_url>

<wp:author><wp:author_id>1</wp:author_id><wp:author_login><![CDATA[admin]]></wp:author_login><wp:author_email><![CDATA[admin@localhost.com]]></wp:author_email><wp:author_display_name><![CDATA[admin]]></wp:author_display_name><wp:author_first_name><![CDATA[]]></wp:author_first_name><wp:author_last_name><![CDATA[]]></wp:author_last_name></wp:author>


<generator>https://wordpress.org/?v=6.5</generator>

<item>
<title><![CDATA["Issue\Issue"]]></title>
<link>https://playground.wordpress.net/scope:0.6224554755515266/?p=5</link>
<pubDate>Mon, 08 Apr 2024 06:59:36 +0000</pubDate>
<dc:creator><![CDATA[admin]]></dc:creator>
<guid isPermaLink="false">https://playground.wordpress.net/scope:0.6224554755515266/?p=5</guid>
<description></description>
<content:encoded><![CDATA[<!-- wp:inseri-core/text-editor {"blockId":"DSrQIjN5UjosCHJQImF5z","blockName":"textEditor","height":60,"content":"\u0022#test\u0022","contentType":"application/json"} -->
<div class="wp-block-inseri-core-text-editor" data-attributes="{&quot;blockId&quot;:&quot;DSrQIjN5UjosCHJQImF5z&quot;,&quot;blockName&quot;:&quot;textEditor&quot;,&quot;content&quot;:&quot;\&quot;#test\&quot;&quot;,&quot;contentType&quot;:&quot;application/json&quot;,&quot;editable&quot;:false,&quot;height&quot;:60,&quot;isVisible&quot;:true,&quot;label&quot;:&quot;&quot;}">is loading ...</div>
<!-- /wp:inseri-core/text-editor -->]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>5</wp:post_id>
<wp:post_date><![CDATA[2024-04-08 06:59:36]]></wp:post_date>
<wp:post_date_gmt><![CDATA[2024-04-08 06:59:36]]></wp:post_date_gmt>
<wp:post_modified><![CDATA[2024-04-08 07:02:42]]></wp:post_modified>
<wp:post_modified_gmt><![CDATA[2024-04-08 07:02:42]]></wp:post_modified_gmt>
<wp:comment_status><![CDATA[open]]></wp:comment_status>
<wp:ping_status><![CDATA[open]]></wp:ping_status>
<wp:post_name><![CDATA[issue]]></wp:post_name>
<wp:status><![CDATA[publish]]></wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type><![CDATA[post]]></wp:post_type>
<wp:post_password><![CDATA[]]></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
<category domain="category" nicename="uncategorized"><![CDATA[Uncategorized]]></category>
<wp:postmeta>
<wp:meta_key><![CDATA[_pingme]]></wp:meta_key>
<wp:meta_value><![CDATA[1]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
<wp:meta_key><![CDATA[_encloseme]]></wp:meta_key>
<wp:meta_value><![CDATA[1]]></wp:meta_value>
</wp:postmeta>
</item>
</channel>
</rss>
76 changes: 76 additions & 0 deletions packages/playground/blueprints/src/lib/steps/import-wxr.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { NodePHP } from '@php-wasm/node';
import {
RecommendedPHPVersion,
getWordPressModule,
} from '@wp-playground/wordpress';
import { importWxr } from './import-wxr';
import { readFile } from 'fs/promises';
import { unzip } from './unzip';
import { installPlugin } from './install-plugin';

describe('Blueprint step importWxr', () => {
let php: NodePHP;
beforeEach(async () => {
php = await NodePHP.load(RecommendedPHPVersion, {
requestHandler: {
documentRoot: '/wordpress',
},
});

await unzip(php, {
zipFile: await getWordPressModule(),
extractToPath: '/wordpress',
});

// Delete all posts
await php.run({
code: `<?php
require '/wordpress/wp-load.php';
$posts = get_posts();
foreach ($posts as $post) {
wp_delete_post($post->ID, true);
}
`,
});

// Install the WordPress importer plugin
const pluginZipData = await readFile(
__dirname + '/../../../../website/public/wordpress-importer.zip'
);
const pluginZipFile = new File([pluginZipData], 'plugin.zip');
await installPlugin(php, {
pluginZipFile,
});
});

it('Should import a WXR file with JSON-encoded UTF-8 characters', async () => {
const fileData = await readFile(
__dirname + '/fixtures/import-wxr-slash-issue.xml'
);
const file = new File([fileData], 'import.wxr');

await importWxr(php, { file });

const expectedPostContent = `<!-- wp:inseri-core/text-editor {"blockId":"DSrQIjN5UjosCHJQImF5z","blockName":"textEditor","height":60,"content":"\\u0022#test\\u0022","contentType":"application/json"} -->
<div class="wp-block-inseri-core-text-editor" data-attributes="{&quot;blockId&quot;:&quot;DSrQIjN5UjosCHJQImF5z&quot;,&quot;blockName&quot;:&quot;textEditor&quot;,&quot;content&quot;:&quot;\\&quot;#test\\&quot;&quot;,&quot;contentType&quot;:&quot;application/json&quot;,&quot;editable&quot;:false,&quot;height&quot;:60,&quot;isVisible&quot;:true,&quot;label&quot;:&quot;&quot;}">is loading ...</div>
<!-- /wp:inseri-core/text-editor -->`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that these are encoding tests, I could see some value in storing this in a file as well. it seems like you properly encoded \\u0022 so that it turns into \u0022, but it's easy to get lost quickly in the levels of encoding and display

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, that's a good point!


const result = await php.run({
code: `<?php
require getenv('DOCROOT') . '/wp-load.php';
$posts = get_posts();
echo json_encode([
'post_content' => $posts[0]->post_content,
'post_title' => $posts[0]->post_title,
]);
`,
env: {
DOCROOT: await php.documentRoot,
},
});
const json = result.json;

expect(json.post_content).toEqual(expectedPostContent);
expect(json.post_title).toEqual(`"Issue\\Issue"`);
});
});
7 changes: 7 additions & 0 deletions packages/playground/blueprints/src/lib/steps/import-wxr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,20 @@ export const importWxr: StepHandler<ImportWxrStep<File>> = async (
await playground.run({
code: `<?php
require ${phpVar(docroot)} . '/wp-load.php';
kses_remove_filters();
$admin_id = get_users(array('role' => 'Administrator') )[0];
$importer = new WXR_Importer( array(
'fetch_attachments' => true,
'default_author' => $admin_id
) );
$logger = new WP_Importer_Logger_CLI();
$importer->set_logger( $logger );

// Slashes from the imported content are lost if we don't call wp_slash here.
add_action( 'wp_insert_post_data', function( $data ) {
return wp_slash($data);
});

$result = $importer->import( '/tmp/import.wxr' );
`,
});
Expand Down
Loading