Skip to content

Commit

Permalink
Add support for terms meta and new unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zaerl committed Dec 19, 2024
1 parent 9ccd8be commit 772b017
Show file tree
Hide file tree
Showing 18 changed files with 1,762 additions and 837 deletions.
27 changes: 14 additions & 13 deletions packages/playground/data-liberation/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Application Test Suite">
<file>tests/WPWXRReaderTests.php</file>
<file>tests/WPRewriteUrlsTests.php</file>
<file>tests/WPURLInTextProcessorTests.php</file>
<file>tests/WPBlockMarkupProcessorTests.php</file>
<file>tests/WPBlockMarkupUrlProcessorTests.php</file>
<file>tests/URLParserWHATWGComplianceTests.php</file>
<file>tests/WPXMLProcessorTests.php</file>
<file>tests/UrldecodeNTests.php</file>
<file>tests/WPStreamImporterTests.php</file>
</testsuite>
</testsuites>
<testsuites>
<testsuite name="Application Test Suite">
<file>tests/WPEntityImporterTests.php</file>
<file>tests/URLParserWHATWGComplianceTests.php</file>
<file>tests/UrldecodeNTests.php</file>
<file>tests/WPBlockMarkupProcessorTests.php</file>
<file>tests/WPBlockMarkupUrlProcessorTests.php</file>
<file>tests/WPRewriteUrlsTests.php</file>
<file>tests/WPStreamImporterTests.php</file>
<file>tests/WPURLInTextProcessorTests.php</file>
<file>tests/WPWXRReaderTests.php</file>
<file>tests/WPXMLProcessorTests.php</file>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public function import_entity( WP_Imported_Entity $entity ) {
case WP_Imported_Entity::TYPE_TAG:
case WP_Imported_Entity::TYPE_CATEGORY:
return $this->import_term( $data );
case WP_Imported_Entity::TYPE_TERM_META:
return $this->import_term_meta( $data, $data['term_id'] );
case WP_Imported_Entity::TYPE_USER:
return $this->import_user( $data );
case WP_Imported_Entity::TYPE_SITE_OPTION:
Expand Down Expand Up @@ -383,6 +385,28 @@ public function import_term( $data ) {
}


public function import_term_meta( $meta_item, $term_id ) {
/**
* Pre-process term meta data.
*
* @param array $meta_item Meta data. (Return empty to skip.)
* @param int $term_id Term the meta is attached to.
*/
$meta_item = apply_filters( 'wxr_importer_pre_process_term_meta', $meta_item, $term_id );
if ( empty( $meta_item ) ) {
return false;
}

if ( ! isset( $meta_item['term_id'] ) ) {
$meta_item['term_id'] = $term_id;
}

$value = maybe_unserialize( $meta_item['meta_value'] );
$term_meta_id = add_term_meta( $meta_item['term_id'], wp_slash( $meta_item['meta_key'] ), wp_slash_strings_only( $value ) );

do_action( 'wxr_importer_processed_term_meta', $term_meta_id, $meta_item, $meta_item['term_id'] );
}

/**
* Prefill existing post data.
*
Expand Down Expand Up @@ -880,11 +904,11 @@ public function import_post_meta( $meta_item, $post_id ) {
return false;
}

$key = apply_filters( 'import_post_meta_key', $meta_item['key'], $post_id, $post );
$key = apply_filters( 'import_post_meta_key', $meta_item['meta_key'], $post_id, $post );
$value = false;

if ( '_edit_last' === $key ) {
$value = intval( $meta_item['value'] );
$value = intval( $meta_item['meta_value'] );
if ( ! isset( $this->mapping['user'][ $value ] ) ) {
// Skip!
_doing_it_wrong( __METHOD__, 'User ID not found in mapping', '4.7' );
Expand All @@ -897,7 +921,7 @@ public function import_post_meta( $meta_item, $post_id ) {
if ( $key ) {
// export gets meta straight from the DB so could have a serialized string
if ( ! $value ) {
$value = maybe_unserialize( $meta_item['value'] );
$value = maybe_unserialize( $meta_item['meta_value'] );
}

add_post_meta( $post_id, $key, $value );
Expand Down Expand Up @@ -1047,8 +1071,8 @@ public function import_comment( $comment, $post_id, $post_just_imported = false
}

public function import_comment_meta( $meta_item, $comment_id ) {
$value = maybe_unserialize( $meta_item['value'] );
add_comment_meta( $comment_id, wp_slash( $meta_item['key'] ), wp_slash( $value ) );
$value = maybe_unserialize( $meta_item['meta_value'] );
add_comment_meta( $comment_id, wp_slash( $meta_item['meta_key'] ), wp_slash( $value ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class WP_Import_Session {
'category',
'tag',
'term',
'term_meta',
'post',
'post_meta',
'comment',
Expand Down Expand Up @@ -296,8 +297,8 @@ public function count_awaiting_frontloading_placeholders() {
global $wpdb;
return (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->posts
WHERE post_type = 'frontloading_placeholder'
"SELECT COUNT(*) FROM $wpdb->posts
WHERE post_type = 'frontloading_placeholder'
AND post_parent = %d
AND post_status = %s",
$this->post_id,
Expand All @@ -310,8 +311,8 @@ public function count_unfinished_frontloading_placeholders() {
global $wpdb;
return (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->posts
WHERE post_type = 'frontloading_placeholder'
"SELECT COUNT(*) FROM $wpdb->posts
WHERE post_type = 'frontloading_placeholder'
AND post_parent = %d
AND post_status != %s
AND post_status != %s",
Expand Down Expand Up @@ -373,8 +374,8 @@ public function get_total_number_of_assets() {
global $wpdb;
return (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->posts
WHERE post_type = 'frontloading_placeholder'
"SELECT COUNT(*) FROM $wpdb->posts
WHERE post_type = 'frontloading_placeholder'
AND post_parent = %d",
$this->post_id
)
Expand Down Expand Up @@ -417,8 +418,8 @@ public function create_frontloading_placeholders( $urls ) {
*/
$exists = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts
WHERE post_type = 'frontloading_placeholder'
"SELECT ID FROM $wpdb->posts
WHERE post_type = 'frontloading_placeholder'
AND post_parent = %d
AND guid = %s
LIMIT 1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class WP_Imported_Entity {
const TYPE_COMMENT = 'comment';
const TYPE_COMMENT_META = 'comment_meta';
const TYPE_TERM = 'term';
const TYPE_TERM_META = 'term_meta';
const TYPE_TAG = 'tag';
const TYPE_CATEGORY = 'category';
const TYPE_USER = 'user';
Expand Down
39 changes: 39 additions & 0 deletions packages/playground/data-liberation/src/wxr/WP_WXR_Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ class WP_WXR_Reader implements Iterator {
*/
private $last_comment_id = null;

/**
* The ID of the last processed term.
*
* @since WP_VERSION
* @var int|null
*/
private $last_term_id = null;

/**
* Buffer for accumulating text content between tags.
*
Expand Down Expand Up @@ -328,6 +336,13 @@ class WP_WXR_Reader implements Iterator {
'wp:term_name' => 'name',
),
),
'wp:termmeta' => array(
'type' => 'term_meta',
'fields' => array(
'wp:meta_key' => 'meta_key',
'wp:meta_value' => 'meta_value',
),
),
'wp:tag' => array(
'type' => 'tag',
'fields' => array(
Expand All @@ -340,6 +355,7 @@ class WP_WXR_Reader implements Iterator {
'wp:category' => array(
'type' => 'category',
'fields' => array(
'wp:term_id' => 'term_id',
'wp:category_nicename' => 'slug',
'wp:category_parent' => 'parent',
'wp:cat_name' => 'name',
Expand Down Expand Up @@ -368,6 +384,7 @@ public static function create( WP_Byte_Reader $upstream = null, $cursor = null )
if ( null !== $cursor ) {
$reader->last_post_id = $cursor['last_post_id'];
$reader->last_comment_id = $cursor['last_comment_id'];
$reader->last_term_id = $cursor['last_term_id'];
}
if ( null !== $upstream ) {
$reader->connect_upstream( $upstream );
Expand Down Expand Up @@ -413,6 +430,7 @@ public function get_reentrancy_cursor() {
'upstream' => $this->last_xml_byte_offset_outside_of_entity,
'last_post_id' => $this->last_post_id,
'last_comment_id' => $this->last_comment_id,
'last_term_id' => $this->last_term_id,
)
);
}
Expand Down Expand Up @@ -473,6 +491,17 @@ public function get_last_comment_id() {
return $this->last_comment_id;
}

/**
* Gets the ID of the last processed term.
*
* @since WP_VERSION
*
* @return int|null The term ID, or null if no terms have been processed.
*/
public function get_last_term_id() {
return $this->last_term_id;
}

/**
* Appends bytes to the input stream.
*
Expand Down Expand Up @@ -867,8 +896,18 @@ private function emit_entity() {
$this->entity_data['comment_id'] = $this->last_comment_id;
} elseif ( $this->entity_type === 'tag' ) {
$this->entity_data['taxonomy'] = 'post_tag';

if ( array_key_exists( 'term_id', $this->entity_data ) ) {
$this->last_term_id = $this->entity_data['term_id'];
}
} elseif ( $this->entity_type === 'category' ) {
$this->entity_data['taxonomy'] = 'category';

if ( array_key_exists( 'term_id', $this->entity_data ) ) {
$this->last_term_id = $this->entity_data['term_id'];
}
} elseif ( $this->entity_type === 'term_meta' ) {
$this->entity_data['term_id'] = $this->last_term_id;
}
$this->entity_finished = true;
++$this->entities_read_so_far;
Expand Down
51 changes: 51 additions & 0 deletions packages/playground/data-liberation/tests/PlaygroundTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use PHPUnit\Framework\TestCase;

/**
* Base class for Playground tests.
*/
abstract class PlaygroundTestCase extends TestCase {

protected function setUp(): void {
parent::setUp();

if ( ! isset( $_SERVER['SERVER_SOFTWARE'] ) || $_SERVER['SERVER_SOFTWARE'] !== 'PHP.wasm' ) {
$this->markTestSkipped( 'Test only runs in Playground' );
}
}

/**
* Deletes all data from the database. Copy of _delete_all_data() from WordPress core.
*
* @see https://github.com/WordPress/wordpress-develop/blob/trunk/tests/phpunit/includes/functions.php
*/
protected function delete_all_data() {
global $wpdb;

foreach ( array(
$wpdb->posts,
$wpdb->postmeta,
$wpdb->comments,
$wpdb->commentmeta,
$wpdb->term_relationships,
$wpdb->termmeta,
) as $table ) {
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "DELETE FROM {$table}" );
}

foreach ( array(
$wpdb->terms,
$wpdb->term_taxonomy,
) as $table ) {
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "DELETE FROM {$table} WHERE term_id != 1" );
}

$wpdb->query( "UPDATE {$wpdb->term_taxonomy} SET count = 0" );

$wpdb->query( "DELETE FROM {$wpdb->users} WHERE ID != 1" );
$wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE user_id != 1" );
}
}
Loading

0 comments on commit 772b017

Please sign in to comment.