PHP code example of wp-php-toolkit / data-liberation
1. Go to this page and download the library: Download wp-php-toolkit/data-liberation library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wp-php-toolkit / data-liberation example snippets
WordPress\ByteStream\MemoryPipe;
use WordPress\DataLiberation\EntityWriter\WXRWriter;
use WordPress\DataLiberation\ImportEntity;
$pipe = new MemoryPipe();
$writer = new WXRWriter( $pipe );
$writer->append_entity( new ImportEntity( 'post', array(
'post_title' => 'Hello',
'content' => 'World.',
'post_id' => '1',
'status' => 'publish',
) ) );
$writer->finalize();
$writer->close_writing();
$pipe->close_writing();
$wxr = $pipe->consume_all();
echo "bytes: " . strlen( $wxr ) . "\n";
echo false !== strpos( $wxr, '<title>Hello</title>' ) ? "title exported\n" : "title missing\n";
echo false !== strpos( $wxr, '<wp:status>publish</wp:status>' ) ? "status exported\n" : "status missing\n";
WordPress\ByteStream\MemoryPipe;
use WordPress\DataLiberation\EntityWriter\WXRWriter;
use WordPress\DataLiberation\ImportEntity;
$rows = array(
array( 'id' => 10, 'title' => 'About', 'body' => '<p>About us.</p>', 'tags' => array( 'company' ) ),
array( 'id' => 11, 'title' => 'Blog', 'body' => '<p>Hello world.</p>', 'tags' => array( 'news', 'launch' ) ),
);
$pipe = new MemoryPipe();
$writer = new WXRWriter( $pipe );
foreach ( $rows as $row ) {
$writer->append_entity( new ImportEntity( 'post', array(
'post_id' => (string) $row['id'],
'post_title' => $row['title'],
'content' => $row['body'],
'status' => 'publish',
'post_type' => 'post',
) ) );
foreach ( $row['tags'] as $i => $tag ) {
$writer->append_entity( new ImportEntity( 'term', array(
'term_id' => (string) ( $row['id'] * 100 + $i ),
'taxonomy' => 'post_tag',
'slug' => $tag,
'parent' => '0',
) ) );
}
}
$writer->finalize();
$writer->close_writing();
$pipe->close_writing();
$wxr = $pipe->consume_all();
echo "items: " . substr_count( $wxr, '<item>' ) . "\n";
echo "terms: " . substr_count( $wxr, '<wp:term>' ) . "\n";
echo false !== strpos( $wxr, '<title>Blog</title>' ) ? "Blog post exported\n" : "Blog post missing\n";
WordPress\DataLiberation\EntityReader\WXREntityReader;
$wxr = <<<XML
<?xml version="1.0" encoding="UTF-8"
WordPress\ByteStream\MemoryPipe;
use WordPress\DataLiberation\EntityReader\WXREntityReader;
use WordPress\DataLiberation\EntityWriter\WXRWriter;
use WordPress\DataLiberation\ImportEntity;
$source_xml = <<<XML
<?xml version="1.0" encoding="UTF-8"
WordPress\ByteStream\MemoryPipe;
use WordPress\DataLiberation\EntityWriter\WXRWriter;
use WordPress\DataLiberation\ImportEntity;
use WordPress\Markdown\MarkdownConsumer;
@mkdir( '/tmp/md-src', 0777, true );
file_put_contents( '/tmp/md-src/hello.md', "---\ntitle: Hello\n---\n\n# Hello\n\nFirst post." );
file_put_contents( '/tmp/md-src/second.md', "---\ntitle: Second\n---\n\nMore text **here**." );
$pipe = new MemoryPipe();
$writer = new WXRWriter( $pipe );
$id = 1;
foreach ( glob( '/tmp/md-src/*.md' ) as $path ) {
$consumer = new MarkdownConsumer( file_get_contents( $path ) );
$consumer->consume();
$writer->append_entity( new ImportEntity( 'post', array(
'post_id' => (string) $id++,
'post_title' => $consumer->get_meta_value( 'title' ) ?: basename( $path, '.md' ),
'content' => $consumer->get_block_markup(),
'status' => 'publish',
'post_type' => 'post',
'post_name' => basename( $path, '.md' ),
) ) );
}
$writer->finalize();
$writer->close_writing();
$pipe->close_writing();
$wxr = $pipe->consume_all();
echo "posts: " . substr_count( $wxr, '<item>' ) . "\n";
echo false !== strpos( $wxr, '<!-- wp:heading' ) ? "block markup exported\n" : "block markup missing\n";
echo false !== strpos( $wxr, '<title>Second</title>' ) ? "frontmatter title exported\n" : "frontmatter title missing\n";