Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

unematiii/wp-shortcode-tree

Repository files navigation

WordPress Shortcode Tree

Build Status Coverage Status

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.

About

Parses (nested) shortcodes into tree hierarcy.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages