定制 arraypress/wp-block-utils 二次开发

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

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

arraypress/wp-block-utils

Composer 安装命令:

composer require arraypress/wp-block-utils

包简介

A lean WordPress library for working with blocks and Gutenberg content

README 文档

README

A lightweight WordPress library for working with blocks and Gutenberg content. Provides clean APIs for block operations, content parsing, and analysis perfect for plugins and themes.

Features

  • 🎯 Clean API: WordPress-style snake_case methods with consistent interfaces
  • 🔍 Built-in Search: Block search with recursive inner block support
  • 📋 Form-Ready Options: Perfect value/label arrays for block type selects
  • Parse & Render: Easy content parsing and block rendering
  • 🔧 Attribute Management: Simple block attribute manipulation
  • 📊 Usage Analytics: Block usage statistics and analysis
  • 🎨 Pattern Matching: Wildcard support for block type matching
  • 🧱 Inner Blocks: Full support for nested block operations

Requirements

  • PHP 7.4 or later
  • WordPress 5.0 or later (with Gutenberg)

Installation

composer require arraypress/wp-block-utils

Basic Usage

Working with Single Blocks

use ArrayPress\BlockUtils\Block;

// Get block information
$block_name = Block::get_name( $block );
$attributes = Block::get_attributes( $block );
$inner_html = Block::get_inner_html( $block );

// Get specific attribute
$className = Block::get_attribute( $block, 'className', '' );

// Manipulate attributes
$block = Block::set_attribute( $block, 'className', 'my-custom-class' );
$block = Block::remove_attribute( $block, 'unwanted-attr' );

// Work with inner blocks
$inner_blocks = Block::get_inner_blocks( $block );
$block        = Block::add_inner_block( $block, $new_inner_block );

// Check block properties
if ( Block::has_inner_blocks( $block ) ) {
	// Block has inner blocks
}

if ( Block::is_reusable( $block ) ) {
	// Block is a reusable block
}

// Block type operations
if ( Block::type_exists( 'custom/my-block' ) ) {
	$is_dynamic = Block::is_dynamic( 'custom/my-block' );
	$category   = Block::get_category( 'custom/my-block' );
}

// Pattern matching
if ( Block::matches( $block, 'core/*' ) ) {
	// Block is a core block
}

if ( Block::matches( $block, 'core/heading' ) ) {
	// Block is specifically a heading block
}

// Convert blocks
$block_string  = Block::to_string( $block );
$block_array   = Block::to_array( $block_string );
$rendered_html = Block::render( $block );

// Utility functions
$short_name = Block::strip_core_namespace( 'core/paragraph' ); // Returns: 'paragraph'

Working with Multiple Blocks

use ArrayPress\BlockUtils\Blocks;

// Parse content
$blocks      = Blocks::parse( $post_content );
$post_blocks = Blocks::get_from_post( 123 );

// Render blocks
$html       = Blocks::render( $blocks );
$serialized = Blocks::serialize( $blocks );

// Search and filter
$headings      = Blocks::get_by_type( $blocks, 'core/heading' );
$core_blocks   = Blocks::get_by_type( $blocks, 'core/*' );
$custom_blocks = Blocks::search_by_type( $blocks, 'custom/*' );

// Search by attributes
$large_headings = Blocks::get_by_attribute( $blocks, 'level', 1 );

// Get from current content
$images    = Blocks::get_from_content( 'core/image' );
$galleries = Blocks::get_from_content( 'core/gallery', $custom_content );

// Block type registry
$all_types    = Blocks::get_registered_types();
$type_options = Blocks::get_type_options(); // For form selects
$custom_only  = Blocks::get_type_options( false ); // Exclude core blocks

// Search block types
$matching_types = Blocks::search_types( 'heading' );

// Statistics and analysis
$total_blocks  = Blocks::count_total();
$heading_count = Blocks::count_by_type( 'core/heading' );
$usage_stats   = Blocks::get_usage_stats();
$most_used     = Blocks::get_most_used_type();

// Conditional checks
if ( Blocks::uses_block_type( 'core/gallery' ) ) {
	// Content uses gallery blocks
}

if ( Blocks::is_editor_available() ) {
	// Gutenberg functions are available
}

// Content manipulation
$modified_content = Blocks::replace_by_type( $content, 'core/heading', function ( $block ) {
	return Block::set_attribute( $block, 'className', 'custom-heading' );
} );

$clean_content = Blocks::remove_by_type( $content, 'core/separator' );

Advanced Examples

// Find all headings and analyze their levels
$content  = get_the_content();
$headings = Blocks::get_from_content( 'core/heading', $content );

$heading_levels = array_map( function ( $heading ) {
	return Block::get_attribute( $heading, 'level', 2 );
}, $headings );

$level_distribution = array_count_values( $heading_levels );

// Replace all paragraphs with custom styling
$new_content = Blocks::replace_by_type( $content, 'core/paragraph', function ( $block ) {
	$existing_class = Block::get_attribute( $block, 'className', '' );
	$new_class      = trim( $existing_class . ' custom-paragraph' );

	return Block::set_attribute( $block, 'className', $new_class );
} );

// Get usage statistics for reporting
$stats = Blocks::get_usage_stats();
echo "Block Usage Report:\n";
foreach ( $stats as $block_type => $count ) {
	$clean_name = Block::strip_core_namespace( $block_type );
	echo "- {$clean_name}: {$count} blocks\n";
}

// Find blocks with specific attributes
$blocks_with_ids = Blocks::filter( $blocks, function ( $block ) {
	return ! empty( Block::get_attribute( $block, 'anchor' ) );
} );

// Check if content is block-based
if ( Blocks::count_total() > 0 ) {
	echo "This content uses the block editor";
} else {
	echo "This might be classic editor content";
}

Block Type Options for Forms

// Get all block types for admin select
$block_options = Blocks::get_type_options();
// Returns: [['value' => 'core/paragraph', 'label' => 'Paragraph'], ...]

// Get only custom blocks
$custom_options = Blocks::get_type_options( false );

// Use in WordPress settings
add_settings_field(
	'allowed_blocks',
	'Allowed Block Types',
	function () {
		$options = Blocks::get_type_options();
		// Render select with options
	},
	'my_settings_page'
);

Key Features

  • Pattern Matching: Use core/* to match all core blocks
  • Recursive Search: Find blocks deep within inner blocks
  • Attribute Management: Easy get/set/remove operations
  • Usage Analytics: Understand block usage patterns
  • Form Integration: Ready-made options for admin interfaces
  • Content Manipulation: Replace and remove blocks programmatically
  • Type Safety: Proper null handling and type checking

Requirements

  • PHP 7.4+
  • WordPress 5.0+ (with Gutenberg support)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the GPL-2.0-or-later License.

Support

arraypress/wp-block-utils 适用场景与选型建议

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

它主要适用于以下技术方向: 「wordpress」 「wp」 「blocks」 「gutenberg」 「block-editor」 「arraypress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2025-09-02