wpdiggerstudio/wpzylos-scaffold

Template repository for creating production-ready WPZylos-based WordPress plugins with MVC architecture, dependency injection, and PHP-Scoper namespace isolation

Fund package maintenance!
Paypal

Installs: 11

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Language:PowerShell

Type:wordpress-plugin

pkg:composer/wpdiggerstudio/wpzylos-scaffold


README

PHP Version WordPress License GitHub

Template repository for creating production-ready WordPress plugins with MVC architecture and PHP-Scoper namespace isolation.

๐Ÿ“– Full Documentation | ๐Ÿ› Report Issues

โœจ Features

  • Complete MVC Structure โ€” Controllers, Services, Views, Routes
  • PSR-4 Autoloading โ€” Modern PHP namespace organization
  • PHP-Scoper Ready โ€” Pre-configured namespace isolation for multi-plugin compatibility
  • Service Providers โ€” Modular dependency injection
  • Database Migrations โ€” Version-controlled schema changes
  • WordPress Compliant โ€” Proper headers, MIT license, readme.txt
  • Intelligent CLI โ€” Version prompting, smart file discovery, auto-versioning
  • Build Pipeline โ€” Scaffold CLI with QA checks and ZIP creation
  • Security First โ€” Nonce verification, capability checks, input sanitization

๐Ÿ“‹ Requirements

Requirement Version
PHP ^8.0
WordPress 6.0+
Composer 2.0+
WP-CLI Optional

๐Ÿš€ Quick Start

Option 1: Use as GitHub Template

Click "Use this template" on GitHub to create a new repository.

Option 2: Composer Create Project

cd /path/to/wordpress/wp-content/plugins
composer create-project wpdiggerstudio/wpzylos-scaffold your-plugin-name
cd your-plugin-name

Option 3: Clone and Customize

git clone https://github.com/WPDiggerStudio/wpzylos-scaffold.git your-plugin-name
cd your-plugin-name
rm -rf .git
composer install

Initialize Your Plugin (Recommended)

After creating your project, run the Scaffold CLI to customize and manage your plugin.

Option 1: PowerShell (Windows 10/11)

Open Windows PowerShell (search "PowerShell" in Start menu):

.\scaffold.ps1           # Interactive menu
.\scaffold.ps1 init      # Initialize plugin directly
.\scaffold.ps1 build     # Build for production directly

Option 2: Command Prompt (Windows)

Open Command Prompt (cmd.exe). Since .ps1 files don't run directly in cmd, use:

powershell -ExecutionPolicy Bypass -File scaffold.ps1
powershell -ExecutionPolicy Bypass -File scaffold.ps1 init
powershell -ExecutionPolicy Bypass -File scaffold.ps1 build

Option 3: Bash (Linux/Mac/Git Bash)

For Linux, macOS, or Git Bash on Windows (install Git for Windows):

chmod +x scaffold.sh     # Make executable (first time only)
./scaffold.sh            # Interactive menu
./scaffold.sh init       # Initialize plugin directly
./scaffold.sh build      # Build for production directly

Git Bash alternative: If ./scaffold.sh doesn't work, try bash scaffold.sh

The intelligent init script handles all scenarios:

Scenario Behavior
Fresh install Detects my-plugin.php, uses scaffold defaults
Re-configure Loads .plugin-config.json, shows current values as defaults
Config deleted Auto-detects plugin from *.php with "Plugin Name:" header
Partial update Only changes modified values, shows "Skipped" for unchanged

Features:

  • Version Prompting: Sets initial version and updates plugin header + PluginContext
  • Namespace support: Supports nested namespaces like WPDigger\WPBraCalculator

๐Ÿ“ Project Structure

your-plugin/
โ”œโ”€โ”€ app/                        # Application code (PSR-4: YourPlugin\)
โ”‚   โ”œโ”€โ”€ Core/
โ”‚   โ”‚   โ””โ”€โ”€ PluginContext.php   # Plugin identity (slug, prefix, text domain)
โ”‚   โ”œโ”€โ”€ Lifecycle/
โ”‚   โ”‚   โ”œโ”€โ”€ Activator.php       # Activation logic
โ”‚   โ”‚   โ”œโ”€โ”€ Deactivator.php     # Deactivation logic
โ”‚   โ”‚   โ””โ”€โ”€ Uninstaller.php     # Uninstall cleanup
โ”‚   โ””โ”€โ”€ Support/
โ”‚       โ””โ”€โ”€ helpers.php         # Global helper functions
โ”œโ”€โ”€ bootstrap/
โ”‚   โ””โ”€โ”€ app.php                 # Application bootstrap & service providers
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ app.php                 # Application configuration
โ”œโ”€โ”€ database/
โ”‚   โ””โ”€โ”€ migrations/             # Database migrations
โ”œโ”€โ”€ resources/
โ”‚   โ”œโ”€โ”€ lang/                   # Translation files
โ”‚   โ””โ”€โ”€ views/                  # PHP view templates
โ”œโ”€โ”€ routes/
โ”‚   โ””โ”€โ”€ web.php                 # Route definitions
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ Unit/                   # PHPUnit tests
โ”œโ”€โ”€ scaffold.ps1                # Scaffold CLI (Windows)
โ”œโ”€โ”€ scaffold.sh                 # Scaffold CLI (Linux/Mac)
โ”œโ”€โ”€ .scripts/                   # CLI scripts
โ”‚   โ”œโ”€โ”€ init-plugin.ps1/.sh     # Initialization logic
โ”‚   โ””โ”€โ”€ build.ps1/.sh           # Build pipeline logic
โ”œโ”€โ”€ your-plugin.php             # Main plugin entry point
โ”œโ”€โ”€ uninstall.php               # WordPress uninstall handler
โ”œโ”€โ”€ scoper.inc.php              # PHP-Scoper configuration
โ”œโ”€โ”€ composer.json               # Dependencies
โ””โ”€โ”€ readme.txt                  # WordPress.org readme

๐Ÿ”ง Customization

Automated (Recommended)

Run the Scaffold CLI for automated setup:

PowerShell:

.\scaffold.ps1 init

Command Prompt:

powershell -ExecutionPolicy Bypass -File scaffold.ps1 init

Linux/Mac/Git Bash:

./scaffold.sh init

Manual

If you prefer manual customization, perform search and replace:

Find Replace With Description
my-plugin your-plugin Plugin slug (lowercase, hyphenated)
my_plugin your_plugin Scoper prefix (lowercase, underscored)
MyPlugin YourPlugin PHP namespace (PascalCase)
myplugin_ yourplugin_ Database/option prefix
My Plugin Your Plugin Display name

Files to Update

  1. your-plugin.php โ€” Plugin headers, PluginContext configuration
  2. composer.json โ€” Package name, namespace in autoload
  3. scoper.inc.php โ€” Scoper prefix variable
  4. .plugin-config.json โ€” Created by init, used by build
  5. uninstall.php โ€” Context configuration

๐Ÿ—๏ธ Core Components

PluginContext

The PluginContext class (app/Core/PluginContext.php) is the single source of truth for plugin identity. All framework components use this for prefixing.

$context = PluginContext::create([
    'file'       => __FILE__,
    'slug'       => 'your-plugin',
    'prefix'     => 'yourplugin_',
    'textDomain' => 'your-plugin',
    'version'    => '1.0.0',
]);

Available Methods:

Method Returns Example
slug() Plugin slug your-plugin
prefix() Database prefix yourplugin_
textDomain() Translation domain your-plugin
version() Plugin version 1.0.0
file() Main plugin file path /path/to/your-plugin.php
path($relative) Absolute path /path/to/your-plugin/config/
url($relative) Plugin URL https://site.com/wp-content/plugins/your-plugin/
hook($name) Prefixed hook name yourplugin_custom_hook
optionKey($key) Prefixed option key yourplugin_settings
transientKey($key) Prefixed transient key yourplugin_cache
cronHook($name) Prefixed cron hook yourplugin_daily_task
tableName($name) Full table name wp_yourplugin_orders
metaKey($key) Prefixed meta key _yourplugin_data
assetHandle($handle) Asset handle your-plugin-main

Helper Functions

Global helpers available after bootstrap (app/Support/helpers.php):

// Escaping
zylos_e($text);       // esc_html()
zylos_ea($text);      // esc_attr()
zylos_eu($url);       // esc_url()
zylos_ej($text);      // esc_js()
zylos_kses($html);    // wp_kses_post()

// Application
zylos_app();          // Get application instance
zylos_app('service'); // Resolve service from container
context();            // Get PluginContext

// Translation
zylos_m($text);       // __($text, $textDomain)
zylos_em($text);      // echo translated text

โš™๏ธ Configuration

config/app.php

return [
    'name'       => 'Your Plugin',
    'debug'      => defined('WP_DEBUG') && WP_DEBUG,
    'providers'  => [
        // \YourPlugin\Providers\CustomServiceProvider::class,
    ],
    'capability' => 'manage_options',
];

Service Providers

The bootstrap (bootstrap/app.php) registers framework service providers in dependency order:

  1. ConfigServiceProvider โ€” Configuration and .env loading
  2. I18nServiceProvider โ€” Internationalization
  3. HookServiceProvider โ€” WordPress hook management
  4. SecurityServiceProvider โ€” Nonce, Gate, Sanitizer
  5. HttpServiceProvider โ€” Request, Response, Pipeline
  6. ValidationServiceProvider โ€” Input validation
  7. ViewsServiceProvider โ€” Template rendering
  8. DatabaseServiceProvider โ€” Database connection
  9. MigrationsServiceProvider โ€” Schema migrations
  10. RoutingServiceProvider โ€” URL routing
  11. WpCliServiceProvider โ€” WP-CLI commands (when available)

๐Ÿ›ค๏ธ Routing

Define routes in routes/web.php:

use WPZylos\Framework\Routing\Router;

return static function (Router $router): void {
    // Frontend routes
    $router->get('/products', [ProductController::class, 'index'])->name('products.index');
    $router->get('/products/{id}', [ProductController::class, 'show'])->name('products.show');
    $router->post('/cart/add', [CartController::class, 'add'])->name('cart.add');

    // Route groups with middleware
    $router->group(['prefix' => '/account', 'middleware' => AuthMiddleware::class], function (Router $router) {
        $router->get('/dashboard', [AccountController::class, 'dashboard']);
        $router->post('/update', [AccountController::class, 'update']);
    });
};

๐Ÿ”จ Build & Release

Development

composer install          # Install all dependencies
composer test             # Run PHPUnit tests
composer analyze          # Run PHPStan analysis

Production Build

Use the Scaffold CLI for production builds:

Windows (PowerShell):

.\scaffold.ps1 build              # Full build (QA + Scoper + ZIP)
.\scaffold.ps1 build -SkipQA      # Skip code style/analysis checks
.\scaffold.ps1 build -SkipScoper  # Dev build (skip PHP-Scoper)

Windows (Command Prompt):

powershell -ExecutionPolicy Bypass -File scaffold.ps1 build
powershell -ExecutionPolicy Bypass -File scaffold.ps1 build -SkipQA
powershell -ExecutionPolicy Bypass -File scaffold.ps1 build -SkipScoper

Linux/Mac (or Git Bash):

./scaffold.sh build              # Full build (QA + Scoper + ZIP)
./scaffold.sh build --skip-qa    # Skip code style/analysis checks
./scaffold.sh build --skip-scoper  # Dev build (skip PHP-Scoper)

The build script will:

  1. Clean previous build artifacts
  2. Run phpcbf --standard=PSR12 (code style fix)
  3. Run phpstan analyze (static analysis)
  4. Install production dependencies
  5. Run PHP-Scoper for namespace isolation
  6. Copy required files & rebuild autoloader
  7. Remove development files
  8. Create versioned ZIP in dist/

Note: The build script reads configuration from .plugin-config.json (created by scaffold init).

Intelligent Features:

  • Smart File Discovery: Automatically detects project files/folders and prompts for unknown items
  • Preference Persistence: Saves your include/exclude choices for future builds
  • Auto-Versioning: Suggests next patch version based on existing ZIP files in dist/
  • Artifact Preservation: Preserves the dist/ directory with previous builds

The production build creates a zip file at dist/your-plugin-1.0.0.zip ready for deployment.

PHP-Scoper

The scaffold includes pre-configured PHP-Scoper (scoper.inc.php) that:

  • Prefixes all vendor namespaces for multi-plugin isolation
  • Excludes WordPress core functions, classes, and constants
  • Excludes your plugin's namespace
  • Generates unique build prefixes using git hash

๐Ÿงช Testing

# Run all tests
composer test
# Or
./vendor/bin/phpunit

# Run specific test
./vendor/bin/phpunit --filter TestClassName

Tests are located in tests/Unit/. The test bootstrap is at tests/bootstrap.php.

๐Ÿ”’ Security

The scaffold implements WordPress security best practices:

  • Nonce verification in form submissions
  • Capability checks for user permissions
  • Prepared statements for database queries
  • Output escaping with proper functions
  • Input sanitization before processing

See the Security Package for detailed security utilities.

๐Ÿ› Troubleshooting

Composer create-project fails

php -v              # Verify PHP 8.0+
php -m | grep json  # Verify json extension

Namespace/autoloader issues

composer dump-autoload

Verify composer.json PSR-4 namespace matches your class namespace.

PHP-Scoper errors

Check scoper.inc.php and ensure WordPress functions are excluded.

Built plugin crashes

Verify WordPress symbols are excluded in scoper configuration.

๐Ÿ“ฆ Related Packages

Package Description
wpzylos-core Application foundation
wpzylos-container PSR-11 dependency injection container
wpzylos-config Configuration management
wpzylos-routing URL routing system
wpzylos-database Database abstraction
wpzylos-migrations Database migrations
wpzylos-hooks WordPress hook management
wpzylos-security Security utilities
wpzylos-validation Input validation
wpzylos-views Template rendering
wpzylos-http HTTP request/response
wpzylos-i18n Internationalization
wpzylos-wp-cli WP-CLI integration

๐Ÿ“– Documentation

For comprehensive documentation, tutorials, and API reference, visit wpzylos.com.

โ˜• Support the Project

If you find this scaffold helpful, consider buying me a coffee! Your support helps maintain and improve the WPZylos ecosystem.

Donate with PayPal

๐Ÿ“„ License

This project is licensed under the MIT License. See LICENSE for details.

๐Ÿค Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

Made with โค๏ธ by WPDiggerStudio