PHP code example of yaml-ns / wpp-framework

1. Go to this page and download the library: Download yaml-ns/wpp-framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yaml-ns / wpp-framework example snippets


YamlNs\WppFramework


/**
 * Plugin Name: My Plugin
 */

declare(strict_types=1);

use YamlNs\WppFramework\Wpp;

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

$pluginFile = __FILE__;
$pluginDir = plugin_dir_path($pluginFile);

plugins_loaded', static function () use ($pluginFile, $config): void {
    Wpp::boot($pluginFile, $config);
});

register_deactivation_hook($pluginFile, static function () use ($pluginFile, $config): void {
    Wpp::deactivate($pluginFile, $config);
});


declare(strict_types=1);

use YamlNs\WppFramework\Wpp;

if (!defined('WP_UNINSTALL_PLUGIN')) {
    exit;
}

$pluginFile = __DIR__ . '/my-plugin.php';



declare(strict_types=1);

use MyPlugin\PostTypes\BookPostType;
use MyPlugin\Shortcodes\BookListShortcode;
use MyPlugin\Taxonomies\BookGenreTaxonomy;
use YamlNs\WppFramework\Core\Container;

return [
    'slug' => 'my-plugin',
    'name' => 'My Plugin',
    'version' => '1.0.0',
    'text_domain' => 'my-plugin',
    'rest_namespace' => 'my-plugin/v1',
    'i18n' => [
        'path' => 'languages',
    ],
    'post_types' => [
        BookPostType::class,
    ],
    'taxonomies' => [
        BookGenreTaxonomy::class,
    ],
    'admin' =>    // Initial migration.
            },
        ],
    ],
    'logger' => [
        'enabled' => true,
        'min_level' => 'warning',
    ],
    'uninstall' => [
        'options' => [
            'my_plugin_version',
            'my_plugin_settings',
        ],
        'remove_capabilities' => true,
    ],
    'providers' => [
        // Custom providers only when the plugin needs specific behavior.
    ],
];

return [
    'slug' => 'product-catalog',
    'name' => 'Product Catalog',
    'version' => '1.0.0',
    'rest_namespace' => 'products/v1',
    'post_types' => [
        ProductPostType::class,
    ],
    'routes' => [
        __DIR__ . '/../routes/products.php',
    ],
    'admin_crud' => 

final class BookListShortcode extends BaseShortcode
{
    public function __construct(ViewRenderer $viewRenderer)
    {
        parent::__construct($viewRenderer);
    }

    public function render(): string
    {
        return $this->view('shortcodes/book-list', [
            'books' => [],
        ]);
    }
}

 $view->extends('layouts/admin'); 

<div class="wrap">
     echo $view->yield('content'); 

use Psr\Log\LoggerInterface;
use YamlNs\WppFramework\Providers\ServiceProvider;

final class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->container->singleton(ProductImporter::class);
        $this->container->tag([CsvImporter::class, ApiImporter::class], 'importers');

        $this->container
            ->when(ProductSyncService::class)
            ->needs(LoggerInterface::class)
            ->give(SyncLogger::class);
    }
}

'modules' => [
    [
        'enabled' => true,
        'providers' => [
            BillingModuleProvider::class,
            ReportingModuleProvider::class,
        ],
    ],
],

return [
    'pages' => [
        [
            'menu_title' => 'My Plugin',
            'page_title' => 'My Plugin',
            'capability' => 'manage_options',
            'slug' => 'my-plugin',
            'template' => 'resources/views/admin/dashboard.php',
            'icon' => 'dashicons-admin-generic',
        ],
    ],
];

final class BookRepository extends BaseRepository
{
    public function __construct(private readonly BookPostType $postType) {}

    protected function postType(): string
    {
        return $this->postType->name();
    }

    protected function metaFields(): array
    {
        return [
            'price' => '_book_price',
            'isbn',
        ];
    }
}

'admin_crud' => [
    'resources' => [
        [
            'slug' => 'books',
            'label' => 'Books',
            'capability' => 'manage_books',
            'force_delete' => false,
            'repository' => BookRepository::class,
            'policy' => BookPolicy::class,
            'views' => [
                'index' => 'admin/books/index',
                'form' => 'admin/books/form',
            ],
            'filters' => [
                's' => ['label' => 'Search', 'query' => 'search', 'type' => 'search'],
                'status' => [
                    'label' => 'Status',
                    'query' => 'post_status',
                    'type' => 'select',
                    'options' => ['publish' => 'Published', 'draft' => 'Draft'],
                ],
                'featured' => [
                    'label' => 'Featured',
                    'meta_key' => '_book_featured',
                    'type' => 'select',
                    'options' => ['1' => 'Yes', '0' => 'No'],
                ],
            ],
            'fields' => [
                'title' => ['label' => 'Title', 'type' => 'text'],
                'content' => ['label' => 'Content', 'type' => 'textarea'],
                'price' => ['label' => 'Price', 'type' => 'float', 'meta_key' => '_book_price'],
            ],
            'rules' => [
                'title' => ['

return [
    'shortcodes' => [
        'product_catalog' => [ProductCatalogShortcode::class, 'render'],
    ],
];


declare(strict_types=1);

use MyPlugin\Http\Controllers\BookRestController;
use YamlNs\WppFramework\Core\PluginContext;
use YamlNs\WppFramework\Http\RestRouter;

return static function (RestRouter $router, PluginContext $context): void {
    $router->get($context->restNamespace(), '/books', [BookRestController::class, 'index']);
    $router->get($context->restNamespace(), '/books/(?P<id>\d+)', [BookRestController::class, 'show']);
};

'routes' => [
    __DIR__ . '/../routes/api.php',
],

$validator = new Validator([
    'sku' => static fn (mixed $value): bool => preg_match('/^[A-Z0-9-]+$/', (string) $value) === 1,
]);

$router->apiResource('/books', BookRestController::class);

$router->apiResource('/books', BookRestController::class, args: [
    'id_pattern' => '[a-zA-Z0-9_-]+',
]);

'uninstall' => [
    'options' => [
        'my_plugin_version',
        'my_plugin_settings',
    ],
    'site_options' => [],
    'remove_capabilities' => true,
],
txt
my-plugin/
  my-plugin.php
  composer.json
  app/
    Admin/
    Http/
      Controllers/
      Requests/
    Policies/
    PostTypes/
    Providers/
    Repositories/
    Services/
    Shortcodes/
    Taxonomies/
  assets/
    admin.css
    admin.js
  config/
    plugin.php
    admin.php
    admin-crud.php
    assets.php
    cron.php
    settings.php
  routes/
    api.php
  resources/
    views/
      admin/
      shortcodes/
  vendor/