unematiii/wp-shortcode-tree

该软件包已被废弃且不再维护。没有推荐替代软件包。

将(嵌套)短代码解析成树形结构。查找节点,操作并将其重新序列化为字符串。便于在后台处理VisualComposer生成的内容。

1.0.0 2020-10-16 22:59 UTC

This package is auto-updated.

Last update: 2022-12-08 22:57:12 UTC


README

Build Status Coverage Status

将(嵌套)短代码解析成树形结构。查找节点,操作并将其重新序列化为字符串。便于在后台处理Visual Composer生成的内容。

基本用法

考虑一个包含以下内容的WordPress帖子

[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]

解析

$content = \WordPress\ShortcodeTree::fromString( $page->post_content );
$folder  = $content->getRoot();

获取所有文档并进行修改

$documents = $folder->findAll( 'document' );

// Prepend 'My ' to filename
foreach ($documents as $doc) {
    $doc->attr( 'name', 'My ' . $doc->attr( 'name' ) );
}

序列化和保存

// 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需要可用。如果您没有它,您可以从这里获取。

安装开发依赖项

composer i

运行单元测试

./vendor/bin/phpunit

代码风格

此项目使用略微修改的WordPress编码标准,并使用内置在CI流程中的PHP_CodeSniffer

检测问题

./vendor/bin/phpcs .

自动修复问题

./vendor/bin/phpcbf .

VS Code集成

您需要安装并启用phpcs插件。此项目的配置提供了它,并且应该可以直接使用。