定制 arrigoo/wp-block-control 二次开发

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

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

arrigoo/wp-block-control

Composer 安装命令:

composer require arrigoo/wp-block-control

包简介

Control display of your blocks based on segments from the Arrigoo CDP.

README 文档

README

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

What this plugin does

A WordPress plugin that adds a "CDP Segment control" panel to every Gutenberg block's inspector. Editors pick segments (from the Arrigoo CDP) that the block should be shown to or hidden from. All blocks still render server-side; a frontend script reads the visitor's segments from the Arrigoo CDP and either reveals or removes the marked blocks in the DOM.

Commands

  • npm run build — Builds both JS entry points with @wordpress/scripts (webpack) and copies src/bundle.js to build/bundle.js. The copy step is required because bundle.js is a pre-built CDP SDK artifact, not a webpack entry — webpack would otherwise overwrite or skip it.
  • npm startwp-scripts start (watch mode for src/index.js and src/frontend-loader.js). Note: this does not copy bundle.js; run npm run build once to seed it.
  • composer install — Installs the arrigoo/cdp-php-sdk dependency and sets up the PSR-4 autoloader for the Arrigoo\WpCdpBlockControl\ namespace. The plugin relies on a Composer autoloader being loaded by WordPress (see README.md — the deliciousbrains "WP in git" pattern).

No test suite or linter is configured.

Architecture

Two-stage rendering (the core trick)

  1. Server: every block with selected segments is rendered with a data-segments="seg1 seg2 !seg3" attribute. A <style>*[data-segments]{display:none}</style> block in <head> hides them all by default (EndUser.php:34-38).
  2. Client: frontend-loader.js waits for the CDP bundle to emit ao_loaded, reads the visitor's segment list from window.argo.get('s') (falling back to the base64-encoded arrigoocdp sessionStorage entry), then either unhides each block (display: block) or removes it from the DOM.

The ! prefix means "hide from this segment". The literal segment unknown matches visitors the CDP has not recognized.

PHP side (src_php/, namespace Arrigoo\WpCdpBlockControl\)

Hooks are registered in index.php. Three classes, all static:

  • BlockControl — registers the editor JS bundle (build/index.js) and injects window.arrigooCdpSegments via wp_add_inline_script. Adds a selectedSegments array attribute to every block via register_block_type_args. Owns the CDP segment fetch (cached 5 min in the ARRIGOO_CDP option).
  • EndUser — emits the frontend <style>, window.arrigooConfig, and the loader script in wp_head.
  • AdminSettings — Settings API page under Settings → Arrigoo CDP. Stores credentials and cookie-consent prefs in option arrigoo_cdp_config. Clearing settings deletes the segment cache.

Config resolution order (see AdminSettings::get_config_value): WP option → env var → PHP constant. Only api_url, api_user, api_secret participate in the fallback chain — they map to CDP_API_URL / CDP_USER / CDP_API_KEY. Cookie/frontend flags only come from the option.

JS side (src/)

Three files, two webpack entries (see webpack.config.js):

  • src/index.js (entry index) — Editor-side. Three addFilter calls:
    • editor.BlockEdit adds the <InspectorControls> panel with two FormTokenFields ("Only show to" and "Hide from"). The UI shows segment titles to editors but stores sys_titles in selectedSegments; hide entries are stored with a ! prefix.
    • blocks.registerBlockType mirrors the PHP attribute registration so the editor knows about selectedSegments.
    • blocks.getSaveContent.extraProps writes data-segments (space-joined) onto the saved block markup. This is the contract the frontend loader reads.
  • src/frontend-loader.js (entry frontend-loader) — Vanilla IIFE that wires up consent providers (none / cookieinformation / cookiebot) and gates loadArrigooScript() (which injects bundleUrl) on the chosen category. When frontendScriptEnabled is false under provider none, the loader still runs processBlocks (after a 300ms grace period) so segment markup is resolved even if the CDP script is loaded elsewhere (e.g., a tag manager).
  • src/bundle.js — Minified Arrigoo CDP SDK, copied verbatim to build/. Not webpack-compiled. Exposes window.argo and dispatches ao_loaded / ao_recognized. Treat as a vendored binary — don't edit by hand.

Data flow at a glance

Editor:   CDP API → BlockControl::get_segments (cached) → window.arrigooCdpSegments → FormTokenField
Save:     selectedSegments attr → data-segments="..." on block HTML
Render:   wp_head emits hide-all CSS + frontend-loader.js
Frontend: consent gate → bundle.js → window.argo.get('s') → show/remove [data-segments] elements

Things to know when editing

  • The build/ directory is committed. Run npm run build and commit the output when changing JS — WordPress loads from build/, not src/.
  • Don't refactor bundle.js. It's the externally-built CDP SDK. Replace it as a whole if it needs updating.
  • The PHP attribute registration and the JS attribute registration must stay in sync (BlockControl.php:41-50 and src/index.js:199-216). The server-side filter is what makes the attribute survive REST/render; the client-side one is what makes it available in the editor UI.
  • Cache invalidation: AdminSettings::sanitize_settings deletes ARRIGOO_CDP on save. If you change how segments are fetched/shaped, also clear that option or bump the cache key, or stale data will hide for 5 minutes.
  • Segment string convention: bare sys_title = show; !sys_title = hide; unknown / !unknown target visitors with no CDP profile. The show/hide split is enforced in the editor (selecting a segment on one side removes it from the other) but the frontend just applies whatever it reads from data-segments.

arrigoo/wp-block-control 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-07