unematiii / wp-shortcode-tree
Parses (nested) shortcodes into tree hierarcy. Find nodes, manipulate and re-serialize into string. Convenient for processing VisualComposer generated content in the backend.
Requires (Dev)
- phpunit/phpunit: ^9.4
- squizlabs/php_codesniffer: ^3.5
- wp-coding-standards/wpcs: ^2.3
This package is auto-updated.
Last update: 2022-12-08 22:57:12 UTC
README
Parses (nested) shortcodes into tree hierarchy. Find nodes, manipulate and re-serialize into string. Convenient for processing Visual Composer generated content in the backend.
Basic Usage
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, ) );
Development
Requirements
Composer needs to be available. If you don't have it, you can get it here.
Install dev dependencies
composer i
Running unit tests
./vendor/bin/phpunit
Code style
This project uses slightly modified WordPress coding standards with PHP_CodeSniffer built into CI pipeline.
Detecting problems
./vendor/bin/phpcs .
Auto-fixing problems
./vendor/bin/phpcbf .
VS Code integration
You need to install and enable phpcs plugin. Configuration for it is provided by this project and it should work out of the box.