hsm/wplite 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

hsm/wplite

Composer 安装命令:

composer require hsm/wplite

包简介

WPLite WordPress Framework

README 文档

README

A lightweight, Laravel-inspired micro framework for building WordPress plugins with modern PHP architecture.

WPLite gives you service containers, facades, middleware pipelines, expressive routing, Eloquent-style models, views, caching, auth guards, and more — without leaving the WordPress ecosystem.

License: MIT PHP

Quick Start

composer create-project hsm/wplite-plugin
cd my-plugin
php vendor/hsm/wplite/wplite build --prefix=MyPlugin

Features

  • Service Container — Auto-resolving dependency injection via reflection
  • Facades — Static-like access: App, Route, View, Cache, Auth, Config, Wordpress
  • 4 Route Types — REST API, Ajax, Admin pages, and Web routes from one unified router
  • Middleware Pipeline — Chainable middleware on any route (Laravel-style $pipeline->next())
  • Query Builder & Models — Fluent $wpdb wrapper with hasMany, hasOne, belongsTo, hasOneMeta relationships
  • Service Providers — Lifecycle hooks: register, bootEarly, onInit, boot, admin, ajax, rest, activate, deactivate, uninstall
  • View Engine — PHP-based templating with dot notation (view('emails.welcome', $data))
  • Cache Layer — Driver-based (ships with WordPress Transients), extensible via Adapter pattern
  • Auth Guards — Pluggable authentication (ships with SSO/OAuth2 guard)
  • JSON Resources — API response transformers (::make(), ::collection())
  • OOP Shortcodes — Class-based shortcodes with attributes and defaults
  • Namespace Isolation — Build tool rewrites all namespaces so multiple WPLite plugins never conflict
  • CLI Toolphp wplite build to scaffold and brand the framework
  • .env Support — Environment variables loaded automatically
  • Config System — Dot-notation access (appConfig('app.api.namespace'))
  • Logging — Built-in file logger

Requirements

  • PHP >= 7.4
  • WordPress (any modern version)
  • Composer

Installation

New Project

composer create-project hsm/wplite-plugin

Add to Existing Plugin

composer require hsm/wplite

Namespace Isolation (Build Step)

This is the critical step. WPLite rewrites all framework namespaces to your plugin's unique namespace so that multiple plugins using WPLite coexist on the same WordPress installation without any conflict.

php vendor/hsm/wplite/wplite build --prefix=MyPlugin

What this does:

  1. Copies the framework source into src/WPLite/ in your project
  2. Rewrites every namespace WPLite\*namespace MyPlugin\WPLite\*
  3. Rewrites every use WPLite\*use MyPlugin\WPLite\*
  4. Updates all fully qualified class references and PHPDoc annotations
  5. Namespaces helper functions to prevent global collisions
  6. Saves your prefix to wplite-config.json for future builds

After the first run, subsequent builds only need:

php vendor/hsm/wplite/wplite build

Use --dry-run to preview all changes without writing files.

CLI Options

php vendor/hsm/wplite/wplite build --prefix=MyPlugin        # Build with prefix
php vendor/hsm/wplite/wplite build                           # Use saved prefix
php vendor/hsm/wplite/wplite build --dry-run                 # Preview changes
php vendor/hsm/wplite/wplite build --output=lib/Core         # Custom output dir
php vendor/hsm/wplite/wplite --help                          # Show help

Bootstrap

Your main plugin file:

<?php
/**
 * Plugin Name: My Plugin
 * Description: Built with WPLite.
 * Version: 1.0.0
 */

if (!defined('ABSPATH')) exit;

require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/src/WPLite/helpers.php';

use MyPlugin\WPLite\Facades\App;

App::setPluginFile(__FILE__);
App::setPluginPath(plugin_dir_path(__FILE__));
App::boot();

Routing

Define routes in routes/rest.php, routes/ajax.php, routes/admin.php, and routes/web.php.

REST API

Route::rest(function ($route) {
    $route->get('/users/{id}', [UserController::class, 'show'])
          ->middleware(AuthMiddleware::class)
          ->name('user.show');
});

Ajax

Route::ajax(function ($route) {
    $route->post('submit_form', [FormController::class, 'handle']);
});

Automatically registers wp_ajax_ and wp_ajax_nopriv_ hooks.

Admin Pages

Route::admin(function ($route) {
    $route->get('my-settings', [SettingsController::class, 'index']);
});

Web (Frontend)

Route::web(function ($route) {
    $route->get('/custom-page', [PageController::class, 'show']);
});

Named Routes & URL Generation

$url = reverse('user.show', ['id' => 42]);

Middleware

use MyPlugin\WPLite\Contracts\Middleware;
use MyPlugin\WPLite\Pipeline;

class AuthMiddleware implements Middleware
{
    public function handle($request, Pipeline $pipeline)
    {
        if (!is_user_logged_in()) {
            return wp_send_json_error('Unauthorized', 401);
        }
        return $pipeline->next($request);
    }
}

Chain per-route or define global middleware in configs/app.php:

$route->post('/checkout', [CheckoutController::class, 'process'])
      ->middleware(AuthMiddleware::class, LogMiddleware::class);

Service Container

use MyPlugin\WPLite\Facades\App;

// Auto-resolve with dependency injection
$service = App::make(OrderService::class);

// Manual bindings
App::bind('mailer', fn() => new SmtpMailer(appConfig('mail.host')));
$mailer = App::resolve('mailer');

Service Providers

use MyPlugin\WPLite\Provider;

class PaymentProvider extends Provider
{
    public function register()    { /* bind services */ }
    public function bootEarly()   { /* before init */ }
    public function onInit()      { /* on init hook */ }
    public function boot()        { /* after wp_loaded */ }
    public function admin()       { /* admin only */ }
    public function ajax()        { /* ajax only */ }
    public function rest()        { /* rest api init */ }
    public function activate()    { /* plugin activation */ }
    public function deactivate()  { /* plugin deactivation */ }
    public function uninstall()   { /* plugin uninstall */ }
}

Register in configs/app.php or place under Src\Provider\ for auto-discovery.

Models & Query Builder

use MyPlugin\WPLite\Model;

class Order extends Model
{
    protected $table = 'wp_orders';

    public function items()
    {
        return $this->hasMany('wp_order_items', 'order_id');
    }

    public function customer()
    {
        return $this->belongsTo('wp_customers', 'customer_id');
    }
}
$orders = (new Order())
    ->select(['id', 'total'])
    ->where('status', '=', 'completed')
    ->orderBy('created_at', 'DESC')
    ->limit(10)
    ->items()
    ->customer()
    ->get();

Supports: select, where, join, orderBy, groupBy, limit, hasMany, hasOne, belongsTo, hasOneMeta, with(), hide(), create, update, delete.

Facades

View::render('dashboard.index', ['stats' => $stats]);
Cache::set('key', $value, 3600);
Cache::get('key');
Auth::check();
Auth::login($user);
Wordpress::action('wp_enqueue_scripts', [$this, 'assets']);
Wordpress::filter('the_content', [$this, 'filter']);

Views

// Renders views/emails/welcome.view.php
view('emails.welcome', ['name' => 'John']);
<!-- views/emails/welcome.view.php -->
<h1>Welcome, <?= $name ?>!</h1>

JSON Resources

use MyPlugin\WPLite\JsonResource;

class UserResource extends JsonResource
{
    public function toArray()
    {
        return [
            'id'   => $this->data['id'],
            'name' => $this->data['display_name'],
        ];
    }
}

$single = UserResource::make($user)->toArray();
$list = UserResource::collection($users);

Shortcodes

use MyPlugin\WPLite\Shortcode;

class PricingTable extends Shortcode
{
    protected $tag = 'pricing_table';

    protected function defaults(): array
    {
        return ['plan' => 'basic'];
    }

    public function render()
    {
        return view('shortcodes.pricing', [
            'plan' => $this->attributes['plan'],
        ]);
    }
}

// Register in a provider
PricingTable::register();

Usage: [pricing_table plan="pro"]

Cache

Cache::set('report', $data, 3600);
Cache::get('report');
Cache::delete('report');
Cache::clear();

Ships with WordPress Transients driver. Extend by implementing the CacheDriver contract.

Configuration

Config Files (configs/app.php)

return [
    'name' => 'my-plugin',
    'api' => ['namespace' => 'myplugin/v1'],
    'providers' => [PaymentProvider::class],
    'api_middlewares' => [AppMiddleware::class],
];
$value = appConfig('app.api.namespace');
$name = appConfig('app.name', 'default');

Environment Variables (.env)

API_KEY=sk_live_abc123
DEBUG_MODE=true
$key = getenv('API_KEY');

Project Structure

my-plugin/
├── my-plugin.php              # Main plugin file
├── composer.json
├── wplite-config.json         # Build config (prefix)
├── .env
├── configs/
│   ├── app.php
│   └── adapters.php
├── routes/
│   ├── rest.php
│   ├── ajax.php
│   ├── admin.php
│   └── web.php
├── views/
│   └── *.view.php
├── src/
│   ├── WPLite/                # Built framework (gitignored)
│   ├── Controllers/
│   ├── Models/
│   ├── Middlewares/
│   └── Provider/              # Auto-discovered providers
└── vendor/

Contributing

Pull requests and issues are welcome! Please follow PSR standards.

License

MIT — see LICENSE for details.

hsm/wplite 适用场景与选型建议

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

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

围绕 hsm/wplite 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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