Skip to content

Plugin API

Aleksey Ilyin edited this page Feb 27, 2024 · 10 revisions
Table of Contents

Overview
Create simple Plugin

Overview

Plugins allow you to extend the functionality of the WSE. See our plugin list for example: Plugins

Plugin Types

  • Common - if none of types fit
  • Payment - if you need to make payment for orders
  • Language - if you need to make something related to localization
  • Delivery - this one is still in development
  • Legacy - this one is already deprecated

Plugin functions by Type

All types of plugins extend the capabilities of a Common type

Common

  • publish(string $channel, mixed $data = []) - publish a data to a channel (see pubsub)
  • subscribe(string|array $channels, callable $handler) - subscribe a handler to a channel (see pubsub)
  • setTemplateFolder(string $path) - set path for plugin twig templates
  • addTwigExtension(string $extension) - add twig extension
  • addSettingsField(array $params = []) - add plugin settings field
  • addScript(array|string $params = []) - add CUP custom JavaScript
  • addToolbarItem(array|string $params = []) - add button on toolbar
  • addSidebarTab(array|string $params = []) - add button on toolbar
  • enableNavigationItem($params = []) - add sidebar menu point
  • map(array $params) - add new controller

Payment

  • getRedirectURL(Order $order) - should return a link to the payment page for redirect after placing an order

Language

  • addLocale(string $code, array $strings = []) - add new locale lines
  • addLocaleFromFile(string $code, string $path) - add new locale lines from file
  • addLocaleEditor(string $code, array $translate) - add new to CUP editor
  • addLocaleTranslateLetters(string $code, array $translate) - if your language has characters that are not applicable in the URL, add their translation

Delivery (is still in development)

Legacy (is already deprecated)

  • setHandledRoute(...$name) - set names of the controllers processed in plugin in methods before and after

Overridable functions:

  • before(Request $request, string $routeName) - function will be executed BEFORE processing the selected group of routes
  • after(Request $request, string $routeName, string $routeName) - function will be executed AFTER processing the selected group of routes

SRC

/src/Domain/AbstractPlugin.php
/src/Domain/Plugin/AbstractPaymentPlugin.php
/src/Domain/Plugin/AbstractLanguagePlugin.php
/src/Domain/Plugin/AbstractDeliveryPlugin.php
/src/Domain/Plugin/AbstractLegacyPlugin.php

Create simple Plugin

Step 1: Create a folder in the plugins section, for example: Example.

Step 2: Create main plugin class with name by mask [your-plugin-name]Plugin, in our example: ExamplePlugin. With contents:

<?php declare(strict_types=1);

namespace Plugin\Example;

use App\Domain\AbstractPlugin;

class ExamplePlugin extends AbstractPlugin
{
    //...
}

Step 3: In class define consts:

const NAME = 'ExamplePlugin ';
const TITLE = 'Example';
const DESCRIPTION = 'My first awesome plugin'; // (optional)
const AUTHOR = 'Your Name';
const AUTHOR_EMAIL  = 'Your E-Mail'; // (optional)
const AUTHOR_SITE = 'Your site'; // (optional)
const VERSION = '1.0'; // (optional)

Step 4: Define constructor

public function __construct(ContainerInterface $container)
{
    parent::__construct($container);

    $this->addSettingsField([
        'label' => $this->parameter('ExamplePlugin_test', 'Test value'),
        'description' => 'Set value',
        'type' => 'text',
        'name' => 'test',
        'args' => [
            'disabled' => false,
            'readonly' => false,
            'value' => null,
            'force-value' => null,
            'placeholder' => '',
            'options' => [],
            'selected' => null,
            'checked' => null,
        ],
        'message' => 'This is message',
        'prefix' => 'pre',
        'postfix' => 'post',
    ]);

    $this
        ->map([
            'methods' => ['get'],
            'pattern' => '/api/test',
            'handler' => function (Request $req, Response $res) use ($container) {
                return $res->withJson(['text' => 'Hello world!']);
            },
        ])
        ->setName('api:example:test');

    // other init login here
}

Step 5: Enable plugin

Open installed.php file and put line:

// example plugin
$plugins->register(new \Plugin\Example\ExamplePlugin($container));

Clone this wiki locally