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

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

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

arraypress/wp-format-utils

Composer 安装命令:

composer require arraypress/wp-format-utils

包简介

Display formatting utilities for WordPress with internationalization support

README 文档

README

Display formatting utilities for WordPress with full internationalization support. Provides clean, consistent formatting for numbers, dates, text, and data presentation across WordPress themes and plugins.

Features

  • 🌍 Internationalization Ready: Full i18n support using WordPress translation functions
  • 📊 Number Formatting: Percentages and numeric values with WordPress standards
  • 📅 Date/Time Formatting: WordPress-compatible date formatting with timezone support
  • 📝 Text Formatting: HTML processing, truncation, and label generation
  • Boolean Formatting: Translatable yes/no, on/off, true/false strings
  • 📋 List Formatting: Smart lists with overflow handling and natural language conjunctions
  • 🛡️ WordPress-Native: Leverages WordPress core functions for consistency

Requirements

  • PHP 7.4 or later
  • WordPress 5.0 or later

Installation

composer require arraypress/wp-format-utils

Basic Usage

Boolean Formatting

use ArrayPress\FormatUtils\Format;

// Translatable boolean strings
Format::yes_no( true );              // 'yes' (translatable)
Format::yes_no( true, true );        // 'Yes' (translatable, title case)
Format::on_off( false );             // 'off' (translatable)
Format::true_false( true );          // 'true' (translatable)

// Perfect for admin interfaces and form display
$enabled = get_option( 'feature_enabled' );
echo Format::yes_no( $enabled, true ); // Shows 'Yes' or 'No' in user's language

Number Formatting

// WordPress i18n number formatting (returns em dash for non-numeric)
Format::numeric( 1234.56 );         // '1,234.56' (respects locale)
Format::numeric( 1234.56, 0 );      // '1,235' (no decimals)
Format::numeric( 'invalid' );       // '—' (em dash)

// Percentage formatting
Format::percentage( 0.75 );         // '75%'
Format::percentage( 0.756, 1 );     // '75.6%'

Date and Time Formatting

// WordPress i18n date formatting (accepts timestamp, string, or DateTime)
Format::date( '2024-01-15' );                    // 'January 15, 2024' (translated)
Format::date( '2024-01-15', 'Y-m-d' );          // '2024-01-15'
Format::date( time(), 'F j, Y', false );        // English only

// Duration formatting
Format::duration( 90 );                         // '1 minutes'
Format::duration( 3661 );                       // '1 hours 1 minutes'
Format::duration( 3661, true );                 // '1h 1m' (abbreviated)
Format::duration( 90061 );                      // '1 days 1 hours'

// Relative time
Format::time_ago( strtotime( '-2 hours' ) );    // '2 hours ago'

Text Formatting

// WordPress text processing
Format::html( "Hello\n\nWorld" );               // '<p>Hello</p><p>World</p>'

// Key to label conversion
Format::label( 'first_name' );                  // 'First Name'
Format::label( 'user-profile-id' );             // 'User Profile Id'

// Multibyte-safe truncation
Format::excerpt( $long_text, 100 );             // 'First 97 chars...'
Format::excerpt( $text, 50, '' );              // 'First 49 chars…'

// Email link generation
Format::email_link( 'user@example.com' );       
// '<a href="mailto:user@example.com">user@example.com</a>'

Format::email_link( 'user@example.com', 'Contact Us' ); 
// '<a href="mailto:user@example.com">Contact Us</a>'

// Rating with proper pluralization
Format::rating( 1 );                            // '1 Star'
Format::rating( 4 );                            // '4 Stars'
Format::rating( 0 );                            // 'No Rating'

Utility Methods

// Em dash fallback for empty values
Format::maybe_dash( 'value' );                  // 'value'
Format::maybe_dash( '' );                       // '—'
Format::maybe_dash( null );                     // '—'

// Natural language lists
Format::list( ['Apple'] );                      // 'Apple'
Format::list( ['Apple', 'Banana'] );            // 'Apple and Banana'
Format::list( ['A', 'B', 'C'] );               // 'A, B and C'
Format::list( ['A', 'B', 'C'], ', ', ' or ' ); // 'A, B or C'

// Lists with overflow handling
$items = ['Theme A', 'Plugin B', 'Template C', 'Widget D', 'Block E'];

Format::list_with_overflow( $items, 3 );        // 'Theme A, Plugin B and 3 more'
Format::list_with_overflow( $items, 2 );        // 'Theme A and 4 more'

// Custom overflow text
Format::list_with_overflow( 
    $items, 
    3, 
    ' | ',           // separator
    ' & ',           // last separator  
    '%d others'      // overflow text
); // 'Theme A | Plugin B & 3 others'

API Reference

Boolean Formatting Methods

Method Description Returns
yes_no($value, $title_case = false) Convert to translatable yes/no string
on_off($value, $title_case = false) Convert to translatable on/off string
true_false($value, $title_case = false) Convert to translatable true/false string

Number Formatting Methods

Method Description Returns
numeric($value, $decimals = 0) WordPress i18n number formatting with em dash fallback string
percentage($value, $decimals = 0) Format as percentage string

Date/Time Formatting Methods

Method Description Returns
date($date, $format = 'F j, Y', $translate = true) WordPress i18n date formatting string
duration($seconds, $abbreviated = false) Human readable duration string
time_ago($date) Relative time formatting string

Text Formatting Methods

Method Description Returns
html($text) WordPress text processing (wpautop + wptexturize) string
label($key) Convert key to human-readable label string
excerpt($text, $length = 150, $suffix = '...') Multibyte-safe text truncation string
email_link($email, $text = '') Generate mailto link string
rating($rating) Translatable star rating with pluralization string

Utility Methods

Method Description Returns
maybe_dash($value) Return value or em dash if empty string
list($items, $separator = ', ', $last_sep = ' and ') Natural language list formatting string
list_with_overflow($items, $limit = 3, ...) List with overflow handling string

Constants

Constant Value Description
Format::MDASH '&mdash;' HTML em dash entity

Common Use Cases

Admin Table Display

// Settings page display
function display_settings_table( $options ) {
    foreach ( $options as $key => $value ) {
        echo '<tr>';
        echo '<td>' . esc_html( Format::label( $key ) ) . '</td>';
        
        if ( is_bool( $value ) ) {
            echo '<td>' . esc_html( Format::yes_no( $value, true ) ) . '</td>';
        } elseif ( is_numeric( $value ) ) {
            echo '<td>' . esc_html( Format::numeric( $value ) ) . '</td>';
        } else {
            echo '<td>' . esc_html( Format::maybe_dash( $value ) ) . '</td>';
        }
        
        echo '</tr>';
    }
}

Content Lists

// Category display with overflow
$categories = wp_get_post_categories( $post_id, ['fields' => 'names'] );
echo 'Categories: ' . esc_html( Format::list_with_overflow( $categories, 3 ) );
// Output: "Categories: WordPress, PHP and 2 more"

// Tag cloud with natural language
$tags = get_the_tags( $post_id );
$tag_names = wp_list_pluck( $tags, 'name' );
echo 'Tagged: ' . esc_html( Format::list( $tag_names ) );
// Output: "Tagged: Development, Tutorial and Advanced"

Dashboard Statistics

$stats = get_site_statistics();

echo 'Total Users: ' . esc_html( Format::numeric( $stats['users'] ) );
echo 'Uptime: ' . esc_html( Format::duration( $stats['uptime_seconds'] ) );
echo 'Success Rate: ' . esc_html( Format::percentage( $stats['success_rate'] ) );
echo 'Last Updated: ' . esc_html( Format::time_ago( $stats['last_update'] ) );

Internationalization

All user-facing strings are translatable using WordPress i18n functions:

  • Text Domain: arraypress
  • Translation Functions: __(), _n() for plurals
  • Translator Comments: Included for context

Translatable Strings

  • Yes/No, On/Off, True/False formatting
  • "X more" overflow text
  • Star rating text
  • "No Rating" text
  • "ago" text

Requirements

  • PHP 7.4+
  • WordPress 5.0+

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-format-utils 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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