定制 gebruederheitz/wp-block-updater 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gebruederheitz/wp-block-updater

Composer 安装命令:

composer require gebruederheitz/wp-block-updater

包简介

Update dynamic blocks with innerBlocks

README 文档

README

Update dynamic blocks with innerBlocks

Moving from a React-rendered "static" Gutenberg block (where the block editor's save() method returns the markup for the block which gets rendered once and written as static HTML to the post content) to a "dynamic" block (which is rendered by PHP) is trivial. Instead of having save() return the markup, we make it return null and let PHP templates handle the block's frontend output. But there's once exception: When the affected block contains "innerBlocks", their rendering will often still have to be handled by Gutenberg & React, so we simply return <InnerBlocks.Content /> from save() – which will result in invalid blocks, as all existing blocks still contain the full markup, not just the innerBlocks. This package offers a REST interface to update such blocks across posts along with an admin page allowing to run batched updates for all published posts.

Warning

This is not a generic block updater; it will only work correctly for exactly the task described above: Performing a migration of a statically rendered block with innerBlocks to a dynamic block.

Running this tool on a static block will make that block's markup disappear (partially or completely) from your Wordpress frontend!

Running the tool against a dynamic block with no innerBlocks will have no effect.

Installation

via composer:

> composer require gebruederheitz/wp-theme-docs

Make sure you have Composer autoload or an alternative class loader present.

Usage

Instantiate the BlockUpdater, for example in your functions.php, passing it an array of qualifying blocks (i.e. those that have moved from static to dynamic rendering):

new \Gebruederheitz\Wordpress\BlockUpdater\BlockUpdater(
    ['mynamespace/myblock']
);

Now navigate to wordpress/wp-admin/tools.php?page=ghwp-block-updater, select a block and click "Start" in order to loop over all published posts of types page and post and re-render the block content of the selected block if it exists.

Example Scenario

Before

registerBlockType('my/block', {
    title: 'My Block',
    icon: 'someicon',
    description: '...',
    category: 'layout',
    styles: [/*...*/],
    attributes: {/*...*/},
    edit(props) {
        /* ... */
        return <MyEditComponent {...props} />;
    },
    /* A React-rendered (static) block with innerBlocks */
    save(props) {
        return (
            <div className="my-component">
                <h1>{props.attributes.title}</h1>
                <InnerBlocks.Content />
            </div>
        );
    },
});

Now

registerBlockType('my/block', {
    title: 'My Block',
    icon: 'someicon',
    description: '...',
    category: 'layout',
    styles: [/*...*/],
    attributes: {/*...*/},
    edit(props) {
        /* ... */
        return <MyEditComponent {...props} />;
    },
    /* Only innerBlocks are returned, the rest left to PHP */
    save() {
        return <InnerBlocks.Content />;
    },
});
register_block_type('my/block', [
    'editor_script' => 'myblock.js',
    'render_callback' => function(array $attributes = [], string $content = '') {
        foreach ($attributes as $name => $datum) {
            set_query_var($name, $datum);
        }
        if (!empty($content)) {
            set_query_var('innerBlocks', $content);
        }
        if (!empty($attributes['className'])) {
            set_query_var('className', $attributes['className']);
        }
        
        ob_start();
        load_template('template-parts/blocks/my-block.php', false, $data);
        return ob_get_clean();
    },
    'attributes' => [
        'title' => [
            'type' => 'string',
            'default' => 'Hello World',
        ],       
    ],
]);
# template-parts/blocks/my-block.php
<?php
    $innerBlocks = get_query_var('innerBlocks');
    $title = get_query_var('title');
?>
<div class="my-block">
    <h1><?= $title ?></h1>
    <?= $innerBlocks ?>
</div>

For any page that is not migrated, this will result in the block being duplicated inside itself, as $content still contains the entire rendered block from before the change.

After

$content will be re-parsed and re-rendered, so it contains only the actual innerBlocks, making the block work in both front- and backend.

Development

Dependencies

  • PHP >= 7.4
  • Composer 2.x
  • nodeJS LTS (v18.x); asdf is recommended
  • Nice to have: GNU Make (or drop-in alternative)

gebruederheitz/wp-block-updater 适用场景与选型建议

gebruederheitz/wp-block-updater 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 331 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gebruederheitz/wp-block-updater 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gebruederheitz/wp-block-updater 我们能提供哪些服务?
定制开发 / 二次开发

基于 gebruederheitz/wp-block-updater 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 331
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2023-07-06