Parses (nested) shortcodes into tree hierarchy. Find nodes, manipulate and re-serialize into string. Convenient for processing Visual Composer generated content in the backend.
Consider a WordPress post with the following content:
[folder name="SampleFolder"]
[document type="image/png" size="64000" path="/path/to/image.png" name="File 1"]
[document type="image/png" size="64000" path="/path/to/image.png" name="File 2"]
[folder name="SampleFolder2"]
[document type="image/png" size="64000" path="/path/to/image.png" name="File 3"]
[document type="image/png" size="64000" path="/path/to/image.png" name="File 4"]
[/folder]
[/folder]
Parse:
$content = \WordPress\ShortcodeTree::fromString( $page->post_content );
$folder = $content->getRoot();
Get all documents & modify:
$documents = $folder->findAll( 'document' );
// Prepend 'My ' to filename
foreach ($documents as $doc) {
$doc->attr( 'name', 'My ' . $doc->attr( 'name' ) );
}
Serialize and save:
// Write content
wp_update_post(
array(
'ID' => $post_id,
// $content is automatically serialized from ShortcodeTree to a string
// when __toString() is called automagically
'post_content' => $content,
)
);
Composer needs to be available. If you don't have it, you can get it here.
composer i
./vendor/bin/phpunit
This project uses slightly modified WordPress coding standards with PHP_CodeSniffer built into CI pipeline.
./vendor/bin/phpcs .
./vendor/bin/phpcbf .
You need to install and enable phpcs plugin. Configuration for it is provided by this project and it should work out of the box.