我目前正在使用PHP
创建一个WordPress-import脚本。
这是我的XML输入的一部分:
<wp:comment>
<wp:comment_id>1</wp:comment_id>
<wp:comment_author><![CDATA[this-is-a-test]]></wp:comment_author>
<wp:comment_author_email>test@example.org</wp:comment_author_email>
<wp:comment_author_url>http://this-is-a-test.wordpress.com</wp:comment_author_url>
<wp:comment_author_IP>134.76.4.169</wp:comment_author_IP>
<wp:comment_date>2017-12-21 11:23:31</wp:comment_date>
<wp:comment_date_gmt>2017-12-21 09:23:31</wp:comment_date_gmt>
<wp:comment_content><![CDATA[Test-Comment]]></wp:comment_content>
<wp:comment_approved>1</wp:comment_approved>
<wp:comment_type></wp:comment_type>
<wp:comment_parent>0</wp:comment_parent>
<wp:comment_user_id>132023990</wp:comment_user_id>
</wp:comment>
<wp:comment>
<wp:comment_id>2</wp:comment_id>
<wp:comment_author><![CDATA[this-is-a-test]]></wp:comment_author>
<wp:comment_author_email>test@example.org</wp:comment_author_email>
<wp:comment_author_url>http://this-is-a-test.wordpress.com</wp:comment_author_url>
<wp:comment_author_IP>134.76.4.169</wp:comment_author_IP>
<wp:comment_date>2017-12-21 11:25:49</wp:comment_date>
<wp:comment_date_gmt>2017-12-21 09:25:49</wp:comment_date_gmt>
<wp:comment_content><![CDATA[Answering a comment]]></wp:comment_content>
<wp:comment_approved>1</wp:comment_approved>
<wp:comment_type></wp:comment_type>
<wp:comment_parent>1</wp:comment_parent>
<wp:comment_user_id>132023990</wp:comment_user_id>
</wp:comment>
<wp:comment>
<wp:comment_id>3</wp:comment_id>
<wp:comment_author><![CDATA[this-is-a-test]]></wp:comment_author>
<wp:comment_author_email>test@example.org</wp:comment_author_email>
<wp:comment_author_url>http://this-is-a-test.wordpress.com</wp:comment_author_url>
<wp:comment_author_IP>134.76.4.169</wp:comment_author_IP>
<wp:comment_date>2017-12-21 11:26:12</wp:comment_date>
<wp:comment_date_gmt>2017-12-21 09:26:12</wp:comment_date_gmt>
<wp:comment_content><![CDATA[Another comment.]]></wp:comment_content>
<wp:comment_approved>1</wp:comment_approved>
<wp:comment_type></wp:comment_type>
<wp:comment_parent>0</wp:comment_parent>
<wp:comment_user_id>132023990</wp:comment_user_id>
</wp:comment>
我创建了一个小脚本,遍历所有注释并将它们添加到它们所属的帖子(array
)。
目前,评论数组是“平坦的”,就像那样:
["comments"]=>
array(3) {
[1]=>
object(WPComment)#835 (8) {
["id"]=>
int(1)
["author"]=>
string(0) ""
["email"]=>
string(23) "me@example.org"
["url"]=>
string(32) "http://example.wordpress.com"
["ip"]=>
string(12) "123.123.123.123"
["content"]=>
string(18) "Test comment"
["created"]=>
object(DateTime)#836 (3) {
["date"]=>
string(26) "2017-12-21 11:23:31.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["status"]=>
int(1)
}
[2]=>
object(WPComment)#834 (8) {
["id"]=>
int(2)
["author"]=>
string(0) ""
["email"]=>
string(23) "me@example.org"
["url"]=>
string(32) "http://example.wordpress.com"
["ip"]=>
string(12) "123.123.123.123"
["content"]=>
string(41) "Answering a test-comment"
["created"]=>
object(DateTime)#838 (3) {
["date"]=>
string(26) "2017-12-21 11:25:49.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["status"]=>
int(1)
}
[3]=>
object(WPComment)#837 (8) {
["id"]=>
int(3)
["author"]=>
string(0) ""
["email"]=>
string(23) "me@example.org"
["url"]=>
string(32) "http://example.wordpress.com"
["ip"]=>
string(12) "123.123.123.123"
["content"]=>
string(36) "And even another test-comment."
["created"]=>
object(DateTime)#840 (3) {
["date"]=>
string(26) "2017-12-21 11:26:12.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["status"]=>
int(1)
}
这就是我创建数组的方式:
foreach ($blog_post->comment as $comment) {
switch ($comment->comment_approved) {
case '1' :
$comment_status = Comment::STATUS_APPROVED;
break;
case '0' :
$comment_status = Comment::STATUS_PENDING;
break;
case 'spam' :
$comment_status = Comment::STATUS_SPAM;
break;
default :
$comment_status = Comment::STATUS_PENDING;
}
$wp_c = new WPComment;
$comments[(int)$comment->comment_id] = $wp_c->create([
'id' => (int)$comment->comment_id,
'author' => (string)$comment->comment->author,
'email' => (string)$comment->comment_author_email,
'url' => (string)$comment->comment_author_url,
'ip' => (string)$comment->comment_author_IP,
'created' => new \DateTime($comment->comment_date),
'content' => (string)$comment->comment_content,
'status' => $comment_status,
]);
//ksort($comments);
}
好的 - 现在我想创建一个嵌套数组 - 每个子注释都应该添加到他的父级。
预期结果应为:
["comments"]=>
array(2) {
[1]=>
object(WPComment)#835 (8) {
["id"]=>
int(1)
["author"]=>
string(0) ""
["email"]=>
string(23) "me@example.org"
["url"]=>
string(32) "http://example.wordpress.com"
["ip"]=>
string(12) "123.123.123.123"
["content"]=>
string(18) "Test comment"
["created"]=>
object(DateTime)#836 (3) {
["date"]=>
string(26) "2017-12-21 11:23:31.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["status"]=>
int(1)
["children"] => [1]=>
object(WPComment)#834 (8) {
["id"]=>
int(2)
["author"]=>
string(0) ""
["email"]=>
string(23) "me@example.org"
["url"]=>
string(32) "http://example.wordpress.com"
["ip"]=>
string(12) "123.123.123.123"
["content"]=>
string(41) "Answering a test-comment"
["created"]=>
object(DateTime)#838 (3) {
["date"]=>
string(26) "2017-12-21 11:25:49.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["status"]=>
int(1)
}
}
[3]=>
object(WPComment)#837 (8) {
["id"]=>
int(3)
["author"]=>
string(0) ""
["email"]=>
string(23) "me@example.org"
["url"]=>
string(32) "http://example.wordpress.com"
["ip"]=>
string(12) "123.123.123.123"
["content"]=>
string(36) "And even another test-comment."
["created"]=>
object(DateTime)#840 (3) {
["date"]=>
string(26) "2017-12-21 11:26:12.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["status"]=>
int(1)
}
最好的解决方案是即使是孩子也可以生孩子。
我很想听听你如何以最优雅和最可读的方式做到这一点的建议:)提前致谢。