PHP code example of hsm / wplite

1. Go to this page and download the library: Download hsm/wplite 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/ */

    

hsm / wplite example snippets



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

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

ath(plugin_dir_path(__FILE__));
App::boot();

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

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

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

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

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

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);
    }
}

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

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');

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 */ }
}

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();

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']);

// Renders views/emails/welcome.view.php
view('emails.welcome', ['name' => 'John']);

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);

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();

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

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');

$key = getenv('API_KEY');
bash
php vendor/hsm/wplite/wplite build --prefix=MyPlugin
bash
php vendor/hsm/wplite/wplite build
html
<!-- views/emails/welcome.view.php -->
<h1>Welcome, <?= $name