first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,159 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event_Chain;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Helper\Core;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Logger\Logger_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Logger\Null_Logger;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form_Chain\Form_Builder;
use function function_exists;
abstract class Abstract_Ilabs_Plugin
{
use Tools, Environment;
private static $config;
/**
* @var self
*/
public static $initial_instance;
/**
* @var Logger_Interface
*/
private static $logger;
/**
* @param array $config
*
* @return void
* @throws Exception
*/
public function execute(array $config)
{
if (!function_exists('get_plugin_data')) {
$this->require_wp_core_file('wp-admin/includes/plugin.php');
}
self::$config = $config;
if (!self::$initial_instance) {
self::$initial_instance = $this;
}
register_activation_hook($this->get__file__(), [$this, 'plugin_activate_actions']);
register_deactivation_hook($this->get__file__(), [$this, 'plugin_deactivate_actions']);
$this->init_request();
$this->init_translations();
$this->before_init();
if (!self::$logger) {
self::$logger = new Null_Logger();
}
add_action('init', function () {
$this->enqueue_scripts();
$this->init();
});
add_action('plugins_loaded', function () use($config) {
$this->plugins_loaded_hooks();
});
}
public function plugin_activate_actions()
{
}
public function plugin_deactivate_actions()
{
}
protected function set_logger(Logger_Interface $logger)
{
self::$logger = $logger;
}
private function init_request()
{
$request = new Request();
$request->register_request_filter(new Security_Request_Filter());
foreach ($this->register_request_filters() as $filter) {
$request->register_request_filter($filter);
}
$request->build();
}
/**
* @return Request_Filter_Interface[]
*/
protected function register_request_filters() : array
{
return [];
}
/**
* @return void
* @throws Exception
*/
private function init_translations()
{
$lang_dir = $this->get_from_config('lang_dir');
add_action('plugins_loaded', function () use($lang_dir) {
load_plugin_textdomain($this->get_text_domain(), \false, $this->get_plugin_basename() . "/{$lang_dir}/");
});
}
protected abstract function before_init();
protected abstract function init();
private function enqueue_scripts()
{
add_action('admin_enqueue_scripts', [$this, 'enqueue_dashboard_scripts']);
add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_scripts']);
}
protected abstract function plugins_loaded_hooks();
/**
* @return Request
*/
public function get_request() : Request
{
return new Request();
}
/**
* @return Alerts
*/
public function alerts() : Alerts
{
return new Alerts();
}
public function get_event_chain() : Event_Chain
{
return new Event_Chain($this);
}
public function get_form_builder() : Form_Builder
{
return new Form_Builder($this);
}
public function add_slug_prefix(string $text) : string
{
return $this->get_from_config('slug') . '_' . $text;
}
/**
* @return Logger_Interface
*/
public function get_logger() : Logger_Interface
{
return self::$logger;
}
/**
* @throws Exception
*/
public function get_woocommerce_logger() : Woocommerce_Logger
{
return new Woocommerce_Logger($this->get_from_config('slug'));
}
/**
* @throws Exception
*/
public function locate_template(string $template, array $args = [])
{
$directory_separator = \DIRECTORY_SEPARATOR;
$template_absolute_path = $this->get_plugin_templates_dir() . $directory_separator . $template;
if ('' === locate_template([$template_absolute_path], \true, $args)) {
include $template_absolute_path;
\extract($args);
}
}
public function get_core_helpers() : Core
{
return new Core();
}
public abstract function enqueue_frontend_scripts();
public abstract function enqueue_dashboard_scripts();
}

View File

@@ -0,0 +1,149 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin;
class Alerts
{
/**
* @var array
*/
static $notice = [];
/**
* @var array
*/
static $error = [];
/**
* @var array
*/
static $success = [];
/**
* @var bool
*/
static $alerts_rendered = \false;
/**
* Alerts constructor.
*/
public function __construct()
{
$this->print_alerts_once();
}
/**
* Short description
*/
public function print_alerts_once()
{
if (!self::$alerts_rendered) {
add_action("admin_notices", function () {
$this->print_alerts();
self::$alerts_rendered = \true;
});
}
}
/**
* @return void
*/
public function print_alerts()
{
if (!is_admin()) {
return;
}
foreach (\array_keys(self::$success) as $context) {
self::$success[$context] = \array_unique(self::$success[$context]);
foreach (self::$success[$context] as $k => $v) {
if (!empty($v)) {
$output = \sprintf("<div class='notice notice-success'><p>%s</p></div>", $v);
echo wp_kses($output, ["div" => ["class" => []], "p" => [], "b" => []]);
}
}
}
foreach (\array_keys(self::$notice) as $context) {
self::$notice[$context] = \array_unique(self::$notice[$context]);
foreach (self::$notice[$context] as $k => $v) {
if (!empty($v)) {
$output = \sprintf("<div class='notice notice-info'><p>%s</p></div>", $v);
echo wp_kses($output, ["div" => ["class" => []], "p" => [], "b" => []]);
}
}
}
foreach (\array_keys(self::$error) as $context) {
self::$error[$context] = \array_unique(self::$error[$context]);
foreach (self::$error[$context] as $k => $v) {
if (!empty($v)) {
$output = \sprintf("<div class='notice notice-error error'><p>%s</p></div>", $v);
echo wp_kses($output, ["div" => ["class" => []], "p" => [], "b" => []]);
}
}
}
self::$error = [];
self::$success = [];
self::$notice = [];
}
/**
* Short description
*
* @param array|string $notice
*/
public function add_notice($notice, $context = 'global')
{
self::$notice[$context][] = $notice;
}
/**
* Short description
*
* @param array|string $success
*/
public function add_success($success, $context = 'global')
{
self::$success[$context][] = $success;
}
/**
* Short description
*
* @param array|string $error
*/
public function add_error($error, $context = 'global')
{
self::$error[$context][] = $error;
}
/**
* @param $context
*
* @return array
*/
public function get_alerts_unformatted_by_context($context) : array
{
$ret = [];
foreach ($this->get_alerts_unformatted() as $type => $v) {
foreach ($v as $context_in_loop => $messages) {
if ($context === $context_in_loop) {
$ret[$type][] = $messages;
}
}
}
return $ret;
}
/**
* @return array
*/
public function get_alerts_unformatted() : array
{
$success = [];
$notices = [];
$errors = [];
foreach (\array_keys(self::$success) as $context) {
$success[$context] = \array_unique(self::$success[$context]);
}
foreach (\array_keys(self::$notice) as $context) {
$notices[$context] = \array_unique(self::$notice[$context]);
}
foreach (\array_keys(self::$error) as $context) {
$errors[$context] = \array_unique(self::$error[$context]);
}
return ['success' => self::$success, 'notice' => self::$notice, 'error' => self::$error];
}
public function clean_errors($context = '')
{
self::$error = [];
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Common;
use WC_Cart;
use WC_Product;
class Wc_Helpers
{
/**
* @return WC_Product[]|null
*/
public static function get_products_from_cart(WC_Cart $cart) : ?array
{
$return = [];
foreach ($cart->get_cart() as $cart_item) {
$product = $cart_item['data'];
if ($product instanceof WC_Product) {
$return[] = $product;
}
}
return $return ?: null;
}
/**
* @param WC_Product $product
*
* @return string|null
*/
public static function get_main_category(WC_Product $product) : ?string
{
if (\is_array($product->get_category_ids()) && !empty($product->get_category_ids())) {
$term = get_term($product->get_category_ids()[0], 'product_cat');
return $term->name;
}
return null;
}
/**
* Get product IDs, names, and quantity from order ID.
*
* @param array $order_id ID of order.
*
* @return array
*/
public static function get_products_by_order_id(int $order_id)
{
global $wpdb;
$order_items_table = $wpdb->prefix . 'woocommerce_order_items';
$order_itemmeta_table = $wpdb->prefix . 'woocommerce_order_itemmeta';
$products = $wpdb->get_results($wpdb->prepare(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT\n\t\t\t\torder_id,\n\t\t\t\torder_itemmeta.meta_value as product_id,\n\t\t\t\torder_itemmeta_2.meta_value as product_quantity,\n\t\t\t\torder_itemmeta_3.meta_value as variation_id,\n\t\t\t\t{$wpdb->posts}.post_title as product_name\n\t\t\tFROM {$order_items_table} order_items\n\t\t\t LEFT JOIN {$order_itemmeta_table} order_itemmeta on order_items.order_item_id = order_itemmeta.order_item_id\n\t\t\t LEFT JOIN {$order_itemmeta_table} order_itemmeta_2 on order_items.order_item_id = order_itemmeta_2.order_item_id\n\t\t\t LEFT JOIN {$order_itemmeta_table} order_itemmeta_3 on order_items.order_item_id = order_itemmeta_3.order_item_id\n\t\t\t LEFT JOIN {$wpdb->posts} on {$wpdb->posts}.ID = order_itemmeta.meta_value\n\t\t\tWHERE\n\t\t\t\torder_id = ( %d )\n\t\t\t AND order_itemmeta.meta_key = '_product_id'\n\t\t\t\tAND order_itemmeta_2.meta_key = '_qty'\n\t\t\t \tAND order_itemmeta_3.meta_key = '_variation_id'\n\t\t\tGROUP BY product_id\n\t\t\t",
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$order_id
), ARRAY_A);
return $products;
/**
* array(2) {
* [0]=>
* array(5) {
* ["order_id"]=>
* string(4) "7453"
* ["product_id"]=>
* string(2) "10"
* ["product_quantity"]=>
* string(1) "9"
* ["variation_id"]=>
* string(1) "0"
* ["product_name"]=>
* string(6) "Beanie"
* }
* [1]=>
* array(5) {
* ["order_id"]=>
* string(4) "7453"
* ["product_id"]=>
* string(3) "700"
* ["product_quantity"]=>
* string(1) "1"
* ["variation_id"]=>
* string(1) "0"
* ["product_name"]=>
* string(5) "Album"
* }
* }
*/
}
}

View File

@@ -0,0 +1,275 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin;
use Exception;
trait Environment
{
private static $__FILE__;
private static $plugin_dir;
private static $plugin_basename;
private static $plugin_url;
private static $plugin_assets_url;
private static $plugin_js_url;
private static $plugin_css_url;
private static $plugin_images_url;
private static $plugin_templates_dir;
private static $framework_templates_dir;
private static $plugin_prefix;
private static $text_domain;
private static $version;
private static $framework_version;
private static $plugin_header_info;
/**
* @return string
* @throws Exception
*/
public function get_plugin_prefix() : string
{
if (!self::$plugin_prefix) {
self::$plugin_prefix = $this->get_from_config('slug');
}
return self::$plugin_prefix;
}
/**
* @param string $key
*
* @return mixed
* @throws Exception
*/
public function get_from_config(string $key)
{
if (isset(self::$config[$key])) {
return self::$config[$key];
}
throw new Exception("[Ilabs_Plugin] [get_from_config] Key: '{$key}' not exists");
}
/**
* @return string
* @throws Exception
*/
public function get_text_domain() : string
{
if (!self::$text_domain) {
self::$text_domain = $this->get_from_config('text_domain');
}
return self::$text_domain;
}
/**
* @return string
* @throws Exception
*/
public function get_plugin_basename() : string
{
if (!self::$plugin_basename) {
self::$plugin_basename = \basename(\dirname($this->get__file__()));
}
return self::$plugin_basename;
}
/**
* @throws Exception
*/
private function get__file__()
{
return self::$__FILE__ ?: $this->get_from_config('__FILE__');
}
/**
* @throws Exception
*/
public function get_plugin_js_url() : string
{
if (!self::$plugin_js_url) {
self::$plugin_js_url = $this->get_plugin_assets_url() . '/js';
}
return self::$plugin_js_url;
}
/**
* @return string
* @throws Exception
*/
public function get_plugin_assets_url() : string
{
if (!self::$plugin_assets_url) {
self::$plugin_assets_url = $this->get_plugin_url() . 'assets';
}
return self::$plugin_assets_url;
}
/**
* @return string
* @throws Exception
*/
public function get_plugin_url() : string
{
if (!self::$plugin_url) {
self::$plugin_url = plugin_dir_url($this->get__file__());
}
return self::$plugin_url;
}
/**
* @return string
* @throws Exception
*/
public function get_plugin_css_url() : string
{
if (!self::$plugin_css_url) {
self::$plugin_css_url = $this->get_plugin_assets_url() . '/css';
}
return self::$plugin_css_url;
}
/**
* @return string
* @throws Exception
*/
public function get_plugin_images_url() : string
{
if (!self::$plugin_images_url) {
self::$plugin_images_url = $this->get_plugin_assets_url() . '/img';
}
return self::$plugin_images_url;
}
/**
* @param bool $add_directory_separator
*
* @return string
* @throws Exception
*/
public function get_plugin_templates_dir(bool $add_directory_separator = \false) : string
{
if (!self::$plugin_templates_dir) {
$directory_separator = $add_directory_separator ? \DIRECTORY_SEPARATOR : '';
self::$plugin_templates_dir = $this->get_plugin_dir() . \DIRECTORY_SEPARATOR . 'templates';
}
return self::$plugin_templates_dir;
}
/**
* @param bool $add_directory_separator
*
* @return string
* @throws Exception
*/
public function get_framework_templates_dir(bool $add_directory_separator = \false) : string
{
if (!self::$framework_templates_dir) {
$directory_separator = \DIRECTORY_SEPARATOR;
self::$framework_templates_dir = $this->get_plugin_dir() . $directory_separator . 'src' . $directory_separator . 'Ilabs_Plugin' . $directory_separator . 'Templates';
}
return self::$framework_templates_dir;
}
/**
* @return string
* @throws Exception
*/
public function get_plugin_dir() : string
{
if (!self::$plugin_dir) {
self::$plugin_dir = plugin_dir_path($this->get__file__());
}
return self::$plugin_dir;
}
public function get_uploads_dir() : string
{
return wp_upload_dir()['basedir'];
}
/**
* @throws Exception
*/
public function get_plugin_header_info() : array
{
if (!self::$plugin_header_info) {
self::$plugin_header_info = get_plugin_data($this->get_from_config('__FILE__'));
}
return self::$plugin_header_info;
}
/**
* @throws Exception
*/
public function get_plugin_author_uri() : string
{
$plugin_header_info = $this->get_plugin_header_info();
return $plugin_header_info['AuthorURI'];
}
/**
* @throws Exception
*/
public function get_plugin_author_name() : string
{
$plugin_header_info = $this->get_plugin_header_info();
return $plugin_header_info['AuthorName'];
}
/**
* @throws Exception
*/
public function get_plugin_requires_wp_ver() : string
{
$plugin_header_info = $this->get_plugin_header_info();
return $plugin_header_info['RequiresWP'];
}
/**
* @throws Exception
*/
public function get_plugin_requires_php_ver() : string
{
$plugin_header_info = $this->get_plugin_header_info();
return $plugin_header_info['RequiresPHP'];
}
/**
* @throws Exception
*/
public function get_plugin_author() : string
{
$plugin_header_info = $this->get_plugin_header_info();
return $plugin_header_info['Author'];
}
/**
* @throws Exception
*/
public function get_plugin_description() : string
{
$plugin_header_info = $this->get_plugin_header_info();
return $plugin_header_info['Description'];
}
/**
* @throws Exception
*/
public function get_plugin_version() : string
{
$plugin_header_info = $this->get_plugin_header_info();
return $plugin_header_info['Version'];
}
/**
* @throws Exception
*/
public function get_framework_version() : string
{
return $this->get_from_framework_ini('VERSION');
}
/**
* @throws Exception
*/
public function get_plugin_name() : string
{
$plugin_header_info = $this->get_plugin_header_info();
return $plugin_header_info['Name'];
}
/**
* @throws Exception
*/
public function get_plugin_uri() : string
{
$plugin_header_info = $this->get_plugin_header_info();
return $plugin_header_info['PluginURI'];
}
/**
* @throws Exception
*/
public function get_from_framework_ini(string $key)
{
$config_path = __DIR__ . \DIRECTORY_SEPARATOR . 'config.cfg';
if (!\file_exists($config_path)) {
throw new Exception("Config file not exists!");
}
$config = \parse_ini_file($config_path);
return $config[$key];
}
}

View File

@@ -0,0 +1,43 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Action_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Event_Interface;
abstract class Abstract_Action implements Action_Interface
{
/**
* @var Event_Interface
*/
protected $current_event;
/**
* @var callable
*/
protected $callable_arguments;
/**
* @return int
* @depracated
*/
public function get_post_id_from_event() : int
{
return 0;
}
public function run()
{
$callable = $this->callable_arguments;
$callable($this->current_event);
}
public function set_current_event(Event_Interface $event)
{
$this->current_event = $event;
}
/**
* @depracated
* @return bool
*/
protected function is_event_provide_post_id() : bool
{
return \false;
}
}

View File

@@ -0,0 +1,31 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Event_Interface;
class Abstract_Condition implements Condition_Interface
{
/**
* @var Event_Interface
*/
protected $current_event;
/**
* @var ?callable
*/
protected $callable_arguments;
public function set_current_event(Event_Interface $event)
{
$this->current_event = $event;
}
public function assert() : bool
{
$callable = $this->callable_arguments;
if (!$this->current_event) {
return $callable();
} else {
return $callable($this->current_event);
}
}
}

View File

@@ -0,0 +1,68 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event_Chain;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Action_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Event_Interface;
abstract class Abstract_Event implements Event_Interface
{
/**
* @var Action_Interface[]
*/
protected $actions;
/**
* @var array
*/
protected $arguments;
/**
* @var Condition_Interface[]|null
*/
protected $conditions;
/**
* @var Event_Chain
*/
protected $event_chain;
/**
* @return false|void
*/
public function callback()
{
if (\is_array($this->conditions)) {
foreach ($this->conditions as $condition) {
$condition->set_current_event($this);
if ($condition->assert() === \false) {
return \false;
}
}
}
foreach ($this->actions as $action) {
if ($action instanceof Action_Interface) {
$action->set_current_event($this);
$action->run();
}
}
}
public function get_actions() : array
{
return $this->actions;
}
public function set_actions(array $actions)
{
$this->actions = $actions;
}
/**
* @return Condition_Interface[]|null
*/
public function get_conditions() : ?array
{
return $this->conditions;
}
public function set_conditions(array $conditions)
{
$this->conditions = $conditions;
}
public abstract function create();
}

View File

@@ -0,0 +1,20 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Action;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Action;
class Action extends Abstract_Action
{
public function __construct(callable $callable_arguments)
{
$this->callable_arguments = $callable_arguments;
}
/**
* @return void
*/
public function run()
{
parent::run();
}
}

View File

@@ -0,0 +1,67 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Action;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Action;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Post_Readable_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Post_Writable_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Readable_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Writable_Interface;
class Copy extends Abstract_Action
{
/**
* @var Readable_Interface
*/
private $source;
/**
* @var Writable_Interface
*/
private $destination;
public function __construct(callable $callable_arguments)
{
$this->callable_arguments = $callable_arguments;
}
/**
* @throws Exception
*/
public function run()
{
parent::run();
if ($this->source instanceof Post_Readable_Interface && !$this->source->get_post_id() && !$this->is_event_provide_post_id()) {
throw new Exception('No source post_id defined.');
} elseif ($this->source instanceof Post_Readable_Interface && !$this->source->get_post_id() && $this->is_event_provide_post_id()) {
$obtained_post_id = $this->get_post_id_from_event();
$this->source->set_post_id($obtained_post_id);
}
if ($this->destination instanceof Post_Writable_Interface && !$this->destination->get_post_id()) {
$this->destination->set_post_id($obtained_post_id);
}
$value = $this->source->read();
if (!$this->destination->get_key()) {
$dest_key = $this->source->get_key();
} else {
$dest_key = $this->destination->get_key();
}
$this->destination->write($dest_key, $value);
}
public function get_source() : Readable_Interface
{
return $this->source;
}
/**
* @param Readable_Interface $source
*/
public function set_source(Readable_Interface $source) : void
{
$this->source = $source;
}
/**
* @param Writable_Interface $destination
*/
public function set_destination(Writable_Interface $destination) : void
{
$this->destination = $destination;
}
}

View File

@@ -0,0 +1,34 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Action;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Abstract_Ilabs_Plugin;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Action;
class Output_Template extends Abstract_Action
{
/**
* @var string
*/
private $template;
public function __construct(string $template)
{
$this->template = $template;
}
/**
* @throws Exception
*/
public function run()
{
$path = Abstract_Ilabs_Plugin::$initial_instance->get_plugin_templates_dir() . \DIRECTORY_SEPARATOR . $this->template;
include $path;
}
/**
* @return string
*/
public function get_template() : string
{
return $this->template;
}
}

View File

@@ -0,0 +1,61 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Cache_Interface;
class Cache implements Cache_Interface
{
/**
* @var array
*/
private $data;
/**
* @var string
*/
private $key;
/**
* @param string|null $key
*/
public function __construct(string $key = null)
{
$this->key = $key;
}
public function push($value, string $key = null)
{
$key = $this->key ?: $key;
$this->data[$key][] = $value;
}
public function set($value, string $key = null)
{
$key = $this->key ?: $key;
$this->data[$key] = $value;
}
/**
* @throws Exception
*/
public function get(string $key = null)
{
$key = $this->key ?: $key;
if (!isset($this->data[$key])) {
return null;
}
return $this->data[$key];
}
public function get_single(string $key = null)
{
$key = $this->key ?: $key;
if (!isset($this->data[$key])) {
return null;
}
return $this->data[$key][0];
}
public function clear(string $key = null)
{
$key = $this->key ?: $key;
if (isset($this->data[$key])) {
unset($this->data[$key]);
}
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
class Is_Admin extends Abstract_Condition implements Condition_Interface
{
public function assert() : bool
{
return is_admin();
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
class When extends Abstract_Condition implements Condition_Interface
{
public function __construct(?callable $callable_arguments)
{
$this->callable_arguments = $callable_arguments;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
class When_Is_Frontend extends Abstract_Condition implements Condition_Interface
{
public function assert() : bool
{
return !is_admin();
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
class When_Is_Not_Ajax extends Abstract_Condition implements Condition_Interface
{
public function assert() : bool
{
return \false === (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest');
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
class When_Is_Product extends Abstract_Condition implements Condition_Interface
{
public function assert() : bool
{
return is_product();
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
class When_Is_Shop extends Abstract_Condition implements Condition_Interface
{
public function assert() : bool
{
return is_shop();
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Request;
class When_Request_Key_Exist extends Abstract_Condition implements Condition_Interface
{
/**
* @var string
*/
private $key;
public function __construct(string $key)
{
$this->key = $key;
}
public function assert() : bool
{
return null !== (new Request())->get_by_key($this->key);
}
/**
* @return string
*/
public function get_key() : string
{
return $this->key;
}
}

View File

@@ -0,0 +1,33 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Abstract_Ilabs_Plugin;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Condition;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
class When_Request_Value_Equals extends Abstract_Condition implements Condition_Interface
{
/**
* @var string
*/
private $key;
private $test_value;
public function __construct(string $key, $test_value)
{
$this->key = $key;
$this->test_value = $test_value;
}
public function assert() : bool
{
$value = Abstract_Ilabs_Plugin::$initial_instance->get_request()->get_by_key($this->key);
return $this->test_value === $value;
}
/**
* @return string
*/
public function get_key() : string
{
return $this->key;
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
class On extends Abstract_Event
{
/**
* @var int
*/
private $priority;
/**
* @var string
*/
private $hook_name;
public function __construct(string $hook_name, int $priority = 10)
{
$this->priority = $priority;
$this->hook_name = $hook_name;
}
public function create()
{
add_action($this->hook_name, function () {
$this->callback();
}, $this->priority);
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
class Save_Post extends Abstract_Event
{
/**
* @var int
*/
private $post_id;
public function create()
{
add_action('save_post', function ($post_id) {
$this->arguments = ['post_id' => $post_id];
$this->post_id = $post_id;
$this->callback();
});
}
/**
* @return int
*/
public function get_post_id() : int
{
return $this->post_id;
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Request;
class Save_Wc_Order_Post extends Abstract_Event
{
/**
* @var int
*/
private $post_id;
public function create()
{
add_action('save_post', function ($post_id) {
if ('shop_order' !== (new Request())->get_by_key('post_type')) {
return;
}
$this->arguments = ['post_id' => $post_id];
$this->post_id = $post_id;
$this->callback();
});
}
/**
* @return int
*/
public function get_post_id() : int
{
return $this->post_id;
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Product_Aware_Interface;
use WC_Product;
class Wc_Add_To_Cart extends Abstract_Event implements Wc_Product_Aware_Interface
{
/**
* @var WC_Product
*/
private $product;
/**
* @var int
*/
private $quantity;
public function create()
{
add_action('woocommerce_add_to_cart', function ($cart_item_key, $product_id, $quantity) {
$this->product = wc_get_product($product_id);
$this->quantity = $quantity;
$this->callback();
}, 100, 3);
}
/**
* @return WC_Product
*/
public function get_product() : WC_Product
{
return $this->product;
}
/**
* @return int
*/
public function get_quantity() : int
{
return $this->quantity;
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Cart_Aware_Interface;
use WC_Cart;
class Wc_Before_Checkout_Form extends Abstract_Event implements Wc_Cart_Aware_Interface
{
/**
* @var WC_Cart
*/
private $cart;
public function create()
{
add_action('woocommerce_before_checkout_form', function () {
$this->cart = WC()->cart;
$this->callback();
}, 100, 3);
}
public function get_cart() : WC_Cart
{
return $this->cart;
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
class Wc_Before_Settings extends Abstract_Event
{
/**
* @var string|null
*/
private $section_id;
public function __construct(string $section_id)
{
$this->section_id = $section_id;
}
public function create()
{
add_action("woocommerce_sections_{$this->section_id}", function () {
$this->callback();
});
}
/**
* @return string|null
*/
public function getSectionId() : ?string
{
return $this->section_id;
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Product_Aware_Interface;
use WC_Product;
class Wc_Before_Shop_Loop_Item extends Abstract_Event implements Wc_Product_Aware_Interface
{
/**
* @var WC_Product
*/
private $product;
public function create()
{
add_action('woocommerce_before_shop_loop_item', function () {
global $product;
$this->product = $product;
$this->callback();
}, 100);
}
/**
* @return WC_Product
*/
public function get_product() : WC_Product
{
return $this->product;
}
}

View File

@@ -0,0 +1,30 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Product_Aware_Interface;
use WC_Product;
class Wc_Before_Single_Product extends Abstract_Event implements Wc_Product_Aware_Interface
{
/**
* @var WC_Product
*/
private $product;
public function create()
{
add_action('woocommerce_before_single_product', function () {
global $product;
$this->product = $product;
$this->callback();
}, 100);
}
/**
* @return WC_Product
*/
public function get_product() : WC_Product
{
return $this->product;
}
}

View File

@@ -0,0 +1,31 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Cart_Aware_Interface;
use WC_Cart;
class Wc_Checkout_Page extends Abstract_Event implements Wc_Cart_Aware_Interface
{
/**
* @var WC_Cart|null
*/
private $cart;
public function create()
{
add_action('wp', function () {
if (is_checkout()) {
$this->cart = WC()->cart;
$this->callback();
}
});
}
/**
* @return WC_Cart
*/
public function get_cart() : WC_Cart
{
return $this->cart;
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Order_Aware_Interface;
use WC_Order;
class Wc_Checkout_Update_Order_Meta extends Abstract_Event implements Wc_Order_Aware_Interface
{
/**
* @var WC_Order
*/
private $order;
public function create()
{
add_action('woocommerce_checkout_update_order_meta', function ($order_id, $posted) {
$this->order = wc_get_order($order_id);
$this->callback();
}, 100, 2);
}
public function get_order() : WC_Order
{
return $this->order;
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Filterable_String_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Product_Aware_Interface;
use WC_Product;
class Wc_Filter_Price_Html extends Abstract_Event implements Filterable_String_Interface, Wc_Product_Aware_Interface
{
/**
* @var string
*/
private $filterable_value;
/**
* @var string
*/
private $filtered_value;
/**
* @var WC_Product
*/
private $product;
/**
* @return WC_Product
*/
public function get_product() : WC_Product
{
return $this->product;
}
/**
* @return string
*/
public function get_filterable_value() : string
{
return $this->filterable_value;
}
public function filter(string $value)
{
$this->filtered_value = $value;
}
public function create()
{
add_filter('woocommerce_get_price_html', function (string $price, WC_Product $product) {
$this->product = $product;
$this->filterable_value = $price;
$this->callback();
return $this->filtered_value;
}, 10, 2);
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
class Wc_Loaded extends Abstract_Event
{
public function create()
{
add_action('woocommerce_loaded', function () {
$this->callback();
});
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Order_Aware_Interface;
use WC_Order;
class Wc_New_Order extends Abstract_Event implements Wc_Order_Aware_Interface
{
/**
* @var WC_Order
*/
private $order;
public function create()
{
add_action('woocommerce_new_order', function ($order_id) {
$this->order = wc_get_order($order_id);
$this->callback();
}, 10, 2);
}
public function get_order() : WC_Order
{
return $this->order;
}
}

View File

@@ -0,0 +1,50 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Order_Aware_Interface;
use WC_Order;
class Wc_Order_Status_Changed extends Abstract_Event implements Wc_Order_Aware_Interface
{
/**
* @var WC_Order
*/
private $order;
/**
* @var String
*/
private $old_status;
/**
* @var String
*/
private $new_status;
public function create()
{
add_action('woocommerce_order_status_changed', function ($order_id, $old_status, $new_status, WC_Order $order) {
$this->order = $order;
$this->old_status = $old_status;
$this->new_status = $new_status;
$this->callback();
}, 100, 4);
}
public function get_order() : WC_Order
{
return $this->order;
}
/**
* @return string
*/
public function get_old_status() : string
{
return $this->old_status;
}
/**
* @return string
*/
public function get_new_status() : string
{
return $this->new_status;
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wp_Post_Id_Aware_Interface;
class Wc_Product_Options_General_Product_Data extends Abstract_Event implements Wp_Post_Id_Aware_Interface
{
/**
* @var int
*/
private $post_id;
public function get_post_id() : int
{
return $this->post_id;
}
public function create()
{
add_action('woocommerce_product_options_general_product_data', function () {
global $post;
$this->post_id = $post->ID;
$this->callback();
}, 3, 1000);
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wp_Post_Id_Aware_Interface;
class Wc_Product_Options_Pricing extends Abstract_Event implements Wp_Post_Id_Aware_Interface
{
/**
* @var int
*/
private $post_id;
public function get_post_id() : int
{
return $this->post_id;
}
public function create()
{
add_action('woocommerce_product_options_pricing', function () {
global $post;
$this->post_id = $post->ID;
$this->callback();
}, 3, 1000);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Product_Aware_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wp_Post_Id_Aware_Interface;
use WC_Product;
class Wc_Product_Update extends Abstract_Event implements Wc_Product_Aware_Interface, Wp_Post_Id_Aware_Interface
{
/**
* @var int
*/
private $post_id;
/**
* @var WC_Product
*/
private $product;
public function get_post_id() : int
{
return $this->post_id;
}
public function get_product() : WC_Product
{
return $this->product;
}
public function create()
{
add_action('woocommerce_update_product', function (int $post_id, WC_Product $product) {
$this->post_id = $post_id;
$this->product = $product;
$this->callback();
}, 3, 1000);
}
}

View File

@@ -0,0 +1,43 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Product_Aware_Interface;
use WC_Cart;
use WC_Product;
class Wc_Remove_Cart_Item extends Abstract_Event implements Wc_Product_Aware_Interface
{
/**
* @var WC_Product
*/
private $product;
/**
* @var int
*/
private $quantity;
public function create()
{
add_action('woocommerce_remove_cart_item', function ($cart_item_key, WC_Cart $cart) {
$product_id = $cart->cart_contents[$cart_item_key]['product_id'];
$this->product = wc_get_product($product_id);
$this->quantity = $cart->cart_contents[$cart_item_key]['quantity'];
$this->callback();
}, 100, 2);
}
/**
* @return WC_Product
*/
public function get_product() : WC_Product
{
return $this->product;
}
/**
* @return int
*/
public function get_quantity() : int
{
return $this->quantity;
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Variation_Aware_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wp_Post_Id_Aware_Interface;
use WC_Product_Variation;
class Wc_Variation_Create extends Abstract_Event implements Wc_Variation_Aware_Interface, Wp_Post_Id_Aware_Interface
{
/**
* @var WC_Product_Variation
*/
private $variation;
/**
* @var int
*/
private $post_id;
/**
* @return WC_Product_Variation
*/
public function get_variation() : WC_Product_Variation
{
return $this->variation;
}
/**
* @return int
*/
public function get_post_id() : int
{
return $this->post_id;
}
public function create()
{
add_action('woocommerce_new_product_variation', function (int $post_id, WC_Product_Variation $variation) {
$this->post_id = $post_id;
$this->variation = $variation;
$this->callback();
}, 3, 10);
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wc_Variation_Aware_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Wp_Post_Id_Aware_Interface;
use WC_Product_Variation;
class Wc_Variation_Update extends Abstract_Event implements Wc_Variation_Aware_Interface, Wp_Post_Id_Aware_Interface
{
/**
* @var WC_Product_Variation
*/
private $variation;
/**
* @var int
*/
private $post_id;
/**
* @return WC_Product_Variation
*/
public function get_variation() : WC_Product_Variation
{
return $this->variation;
}
/**
* @return int
*/
public function get_post_id() : int
{
return $this->post_id;
}
public function create()
{
add_action('woocommerce_update_product_variation', function (int $post_id, WC_Product_Variation $variation) {
$this->post_id = $post_id;
$this->variation = $variation;
$this->callback();
}, 3, 10);
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
class Wp extends Abstract_Event
{
public function create()
{
add_action('wp', function () {
$this->callback();
});
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
class Wp_Admin_Footer extends Abstract_Event
{
public function create()
{
add_action('admin_footer', function () {
$this->callback();
});
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
class Wp_Admin_Init extends Abstract_Event
{
public function create()
{
add_action('admin_init', function () {
$this->callback();
});
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Ajax_Interface;
class Wp_Ajax extends Abstract_Event
{
/**
* @var Field_Ajax_Interface
*/
private $field_ajax;
public function __construct(Field_Ajax_Interface $field_ajax)
{
$this->field_ajax = $field_ajax;
}
public function create()
{
//var_dump("wp_ajax_{$this->field_ajax->get_payload()->get_id()}");die;
add_action("wp_ajax_{$this->field_ajax->get_id()}", [$this, 'callback']);
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Abstract_Ilabs_Plugin;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
class Wp_Cron_Every_N_Minutes extends Abstract_Event
{
/**
* @var int
*/
private $interval;
/**
* @var string
*/
private $schedule_id;
public function __construct(int $interval, string $schedule_id)
{
$this->interval = $interval;
$this->schedule_id = $schedule_id;
}
public function create()
{
$schedule_id = Abstract_Ilabs_Plugin::$initial_instance->get_plugin_prefix() . '_' . $this->schedule_id;
$schedule_id_hook_name = $schedule_id . '_hook';
add_filter('cron_schedules', function ($schedules) use($schedule_id) {
$schedules[$schedule_id] = ['interval' => $this->interval * 60, 'display' => $schedule_id];
return $schedules;
});
if (!wp_next_scheduled($schedule_id_hook_name)) {
wp_schedule_event(\time(), $schedule_id, $schedule_id_hook_name);
}
add_action($schedule_id_hook_name, function () {
$this->callback();
});
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
class Wp_Footer extends Abstract_Event
{
public function create()
{
add_action('wp_footer', function () {
$this->callback();
});
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Abstracts\Abstract_Event;
class Wp_Init extends Abstract_Event
{
public function create()
{
add_action('init', function () {
$this->callback();
});
}
}

View File

@@ -0,0 +1,170 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Abstract_Ilabs_Plugin;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Action_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Cache_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Event_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Traits\Actions;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Traits\Conditions;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Traits\Events;
class Event_Chain
{
use Events;
use Conditions;
use Actions;
/**
* @var Event_Chain_Item
*/
private $current_event_chain_item;
/**
* @var Event_Chain_Item[]
*/
private $event_chain_items;
/**
* @var int
*/
private $current_post_id;
/**
* @var Cache
*/
private $cache;
/**
* @var Wc_Session_Cache
*/
private $wc_session_cache;
/**
* @var Abstract_Ilabs_Plugin
*/
private $ilabs_plugin;
public function __construct(Abstract_Ilabs_Plugin $ilabs_plugin)
{
$this->ilabs_plugin = $ilabs_plugin;
$this->current_event_chain_item = new Event_Chain_Item();
}
/**
* @throws Exception
*/
public function execute()
{
$this->increment_event_chain_item();
if (!$this->event_chain_items) {
throw new Exception("You must add at least one event chain item");
}
foreach ($this->event_chain_items as $event_chain_item) {
if (!$this->assert_conditions_before_event($event_chain_item->get_conditions_before_event())) {
continue;
}
$event = $event_chain_item->get_event();
$this->register_event($event, $event_chain_item->get_actions(), $event_chain_item->get_conditions_inside_event());
}
}
private function increment_event_chain_item()
{
$this->event_chain_items[] = $this->current_event_chain_item;
$this->current_event_chain_item = new Event_Chain_Item();
}
/**
* @param Condition_Interface[] $conditions
*
* @return bool
*/
private function assert_conditions_before_event(?array $conditions) : bool
{
if (null === $conditions) {
return \true;
}
foreach ($conditions as $condition) {
if (!$condition->assert()) {
return \false;
}
}
return \true;
}
/**
* @param Event_Interface $event
* @param Action_Interface[] $actions
* @param array|null $conditions_inside_event
*
* @return void
*/
private function register_event(Event_Interface $event, array $actions, array $conditions_inside_event = null)
{
$event->set_actions($actions);
if ($conditions_inside_event) {
$event->set_conditions($conditions_inside_event);
}
$event->create();
}
/**
* @return int
*/
public function get_current_post_id() : int
{
return $this->current_post_id;
}
public function get_cache() : Cache_Interface
{
if (!$this->cache) {
$this->cache = new Cache();
}
return $this->cache;
}
public function get_wc_session_cache($key = null) : Cache_Interface
{
/*if (!$this->wc_session_cache) {
$this->wc_session_cache = new Wc_Session_Cache($key);
}*/
return new Wc_Session_Cache($key);
}
public function get_wp_options_based_cache($key, int $max_items = null) : Wp_Options_Based_Cache
{
return new Wp_Options_Based_Cache($key, $this->ilabs_plugin, $max_items);
}
protected function get_event_chain() : self
{
return $this;
}
/**
* @throws Exception
*/
protected function add_event(Event_Interface $event)
{
if ($this->current_event_chain_item->get_event() && !$this->current_event_chain_item->get_actions()) {
throw new Exception("Don't add nex Event before Action call");
} elseif ($this->current_event_chain_item->get_event() && $this->current_event_chain_item->get_actions()) {
$this->increment_event_chain_item();
$this->current_event_chain_item->set_event($event);
} else {
$this->current_event_chain_item->set_event($event);
}
}
protected function add_action(Action_Interface $action)
{
$actions = $this->current_event_chain_item->get_actions();
$actions[] = $action;
$this->current_event_chain_item->set_actions($actions);
}
protected function add_condition(Condition_Interface $condition)
{
if (!$this->current_event_chain_item->get_event()) {
$conditions = $this->current_event_chain_item->get_conditions_before_event();
if (!$conditions) {
$conditions = [];
}
$conditions[] = $condition;
$this->current_event_chain_item->set_conditions_before_event($conditions);
} else {
$conditions = $this->current_event_chain_item->get_conditions_inside_event();
if (!$conditions) {
$conditions = [];
}
$conditions[] = $condition;
$this->current_event_chain_item->set_conditions_inside_event($conditions);
}
}
}

View File

@@ -0,0 +1,83 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Action_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Condition_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Event_Interface;
class Event_Chain_Item
{
/**
* @var Event_Interface
*/
private $event;
/**
* @var Condition_Interface[]
*/
private $conditions_before_event;
/**
* @var Condition_Interface[]
*/
private $conditions_inside_event;
/**
* @var Action_Interface[]
*/
private $actions;
/**
* @return Event_Interface
*/
public function get_event() : ?Event_Interface
{
return $this->event;
}
/**
* @param Event_Interface $event
*/
public function set_event(Event_Interface $event) : void
{
$this->event = $event;
}
/**
* @return array
*/
public function get_conditions_before_event() : ?array
{
return $this->conditions_before_event;
}
/**
* @param array $conditions_before_event
*/
public function set_conditions_before_event(array $conditions_before_event) : void
{
$this->conditions_before_event = $conditions_before_event;
}
/**
* @return Action_Interface[]
*/
public function get_actions() : ?array
{
return $this->actions;
}
/**
* @param Action_Interface[] $actions
*/
public function set_actions(array $actions) : void
{
$this->actions = $actions;
}
/**
* @return Condition_Interface[]
*/
public function get_conditions_inside_event() : ?array
{
return $this->conditions_inside_event;
}
/**
* @param Condition_Interface[] $conditions_inside_event
*/
public function set_conditions_inside_event(array $conditions_inside_event) : void
{
$this->conditions_inside_event = $conditions_inside_event;
}
}

View File

@@ -0,0 +1,10 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
interface Action_Interface
{
public function run();
public function set_current_event(Event_Interface $event);
}

View File

@@ -0,0 +1,13 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
interface Cache_Interface
{
public function push($value, string $key = null);
public function set($value, string $key = null);
public function get(string $key = null);
public function get_single(string $key = null);
public function clear(string $key = null);
}

View File

@@ -0,0 +1,10 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
interface Condition_Interface
{
public function assert() : bool;
public function set_current_event(Event_Interface $event);
}

View File

@@ -0,0 +1,31 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
interface Event_Interface
{
/**
* @param Action_Interface[]
*
* @return mixed
*/
public function set_actions(array $callback);
/**
* @param Condition_Interface[] $conditions
*
* @return mixed
*/
public function set_conditions(array $conditions);
/**
* @return Condition_Interface[]
*/
public function get_conditions() : ?array;
/**
* @param Action_Interface[]
*
* @return mixed
*/
public function get_actions() : array;
function create();
}

View File

@@ -0,0 +1,9 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
interface Filterable_String_Interface
{
public function filter(string $value);
public function get_filterable_value() : string;
}

View File

@@ -0,0 +1,10 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Interface;
interface Group_Interface_Aware_Interface
{
public function get_group_interface() : Group_Interface;
}

View File

@@ -0,0 +1,9 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
interface Post_Readable_Interface
{
public function get_post_id();
}

View File

@@ -0,0 +1,10 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
interface Post_Writable_Interface
{
public function get_post_id();
public function get_key() : string;
}

View File

@@ -0,0 +1,10 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
interface Readable_Interface
{
public function read(string $key = null);
public function get_key() : string;
}

View File

@@ -0,0 +1,10 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
use WC_Cart;
interface Wc_Cart_Aware_Interface
{
public function get_cart() : WC_Cart;
}

View File

@@ -0,0 +1,10 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
use WC_Order;
interface Wc_Order_Aware_Interface
{
public function get_order() : WC_Order;
}

View File

@@ -0,0 +1,10 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
use WC_Product;
interface Wc_Product_Aware_Interface
{
public function get_product() : WC_Product;
}

View File

@@ -0,0 +1,9 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
use WC_Product_Variation;
interface Wc_Variation_Aware_Interface
{
public function get_variation() : WC_Product_Variation;
}

View File

@@ -0,0 +1,9 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
interface Wp_Post_Id_Aware_Interface
{
public function get_post_id() : int;
}

View File

@@ -0,0 +1,10 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces;
interface Writable_Interface
{
public function write($key = null, $value = null);
public function get_key() : string;
}

View File

@@ -0,0 +1,21 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Storage\Readable;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Readable_Interface;
class Option implements Readable_Interface
{
public function read($key = null)
{
return get_option($key);
}
public function set_key(string $key) : Readable_Interface
{
// TODO: Implement set_key() method.
}
public function get_key() : string
{
// TODO: Implement get_key() method.
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Storage\Readable;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Post_Readable_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Readable_Interface;
class Readable_Post_Meta implements Readable_Interface, Post_Readable_Interface
{
private $post_id;
private $key;
public function __construct(string $key = null, int $post_id = null)
{
$this->post_id = $post_id;
$this->key = $key;
}
public function read($key = null)
{
if (!$key) {
$key = $this->key;
}
return get_post_meta($this->post_id, $key);
}
public function get_post_id() : ?int
{
return $this->post_id;
}
public function set_post_id(int $post_id)
{
$this->post_id = $post_id;
}
public function get_key() : string
{
return $this->key;
}
public function set_key(string $key) : Readable_Interface
{
$this->key = $key;
return $this;
}
}

View File

@@ -0,0 +1,25 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Storage\Writable;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Writable_Interface;
class Option implements Writable_Interface
{
public function write($key = null, $value = null)
{
update_option($key, $value);
}
public function set_key(string $key) : Writable_Interface
{
// TODO: Implement set_key() method.
}
public function set_value($value) : Writable_Interface
{
// TODO: Implement set_value() method.
}
public function get_key() : string
{
// TODO: Implement get_key() method.
}
}

View File

@@ -0,0 +1,51 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Storage\Writable;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Post_Writable_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Writable_Interface;
class Writable_Post_Meta implements Writable_Interface, Post_Writable_Interface
{
private $post_id;
private $key;
private $value;
public function __construct(string $key = null, $value = null, int $post_id = null)
{
$this->post_id = $post_id;
$this->key = $key;
$this->value = $value;
}
public function write($key = null, $value = null)
{
if (!$key) {
$key = $this->key;
}
if (!$value) {
$value = $this->value;
}
update_post_meta($key, $value, $this->post_id);
}
public function get_post_id() : ?int
{
return $this->post_id;
}
public function set_post_id(int $post_id)
{
$this->post_id = $post_id;
}
public function set_value($value) : Writable_Interface
{
$this->value = $value;
return $this;
}
public function get_key() : string
{
return $this->key;
}
public function set_key(string $key) : Writable_Interface
{
$this->key = $key;
return $this;
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Traits;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Action\Action;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Action\Copy;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Action\Output_Template;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event_Chain;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Form;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Woocommerce\Event_Chain\Actions\Action_Wc_render_Metabox;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Woocommerce\Event_Chain\Actions\Action_Wc_render_Settings_Page;
trait Actions
{
public function action_copy(callable $callable_arguments) : Event_Chain
{
$this->get_event_chain()->add_action(new Copy($callable_arguments));
return $this->get_event_chain();
}
protected abstract function get_event_chain() : Event_Chain;
public function action(callable $callable_arguments) : Event_Chain
{
$this->get_event_chain()->add_action(new Action($callable_arguments));
return $this->get_event_chain();
}
public function action_output_template(string $template) : Event_Chain
{
$this->get_event_chain()->add_action(new Output_Template($template));
return $this->get_event_chain();
}
public function action_render_wc_general_settings(Form $form, string $id, string $label) : Event_Chain
{
$this->get_event_chain()->add_action(new Action_Wc_render_Settings_Page($form, $id, $label));
return $this->get_event_chain();
}
public function action_render_wc_post_meta(Form $form, string $header, string $metabox_type = Action_Wc_render_Metabox::METABOX_TYPE_SIDE) : Event_Chain
{
$this->get_event_chain()->add_action(new Action_Wc_render_Metabox($form, $header, $metabox_type));
return $this->get_event_chain();
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Traits;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition\Is_Admin;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition\When;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition\When_Is_Frontend;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition\When_Is_Not_Ajax;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition\When_Is_Product;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition\When_Is_Shop;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition\When_Request_Key_Exist;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Condition\When_Request_Value_Equals;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event_Chain;
trait Conditions
{
public function when_is_admin() : Event_Chain
{
$this->get_event_chain()->add_condition(new Is_Admin());
return $this->get_event_chain();
}
protected abstract function get_event_chain() : Event_Chain;
public function when(callable $callable_arguments = null) : Event_Chain
{
$this->get_event_chain()->add_condition(new When($callable_arguments));
return $this->get_event_chain();
}
public function when_request_key_exist(string $key) : Event_Chain
{
$this->get_event_chain()->add_condition(new When_Request_Key_Exist($key));
return $this->get_event_chain();
}
public function when_request_value_equals(string $key, $test_value) : Event_Chain
{
$this->get_event_chain()->add_condition(new When_Request_Value_Equals($key, $test_value));
return $this->get_event_chain();
}
public function when_is_shop() : Event_Chain
{
$this->get_event_chain()->add_condition(new When_Is_Shop());
return $this->get_event_chain();
}
public function when_is_product() : Event_Chain
{
$this->get_event_chain()->add_condition(new When_Is_Product());
return $this->get_event_chain();
}
public function when_is_not_ajax() : Event_Chain
{
$this->get_event_chain()->add_condition(new When_Is_Not_Ajax());
return $this->get_event_chain();
}
public function when_is_frontend() : Event_Chain
{
$this->get_event_chain()->add_condition(new When_Is_Frontend());
return $this->get_event_chain();
}
}

View File

@@ -0,0 +1,240 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Traits;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\On;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Save_Post;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Save_Wc_Order_Post;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Add_To_Cart;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Before_Checkout_Form;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Before_Settings;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Before_Shop_Loop_Item;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Before_Single_Product;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Checkout_Page;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Checkout_Update_Order_Meta;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Filter_Price_Html;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Loaded;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_New_Order;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Order_Status_Changed;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Product_Options_General_Product_Data;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Product_Options_Pricing;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Product_Update;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Remove_Cart_Item;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Variation_Create;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wc_Variation_Update;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wp;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wp_Admin_Footer;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wp_Admin_Init;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wp_Ajax;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wp_Cron_Every_N_Minutes;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wp_Footer;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event\Wp_Init;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Event_Chain;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Ajax_Interface;
trait Events
{
/**
* @throws Exception
*/
public function on_woocommerce_checkout_update_order_meta() : Event_Chain
{
$event = new Wc_Checkout_Update_Order_Meta();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
protected abstract function get_event_chain() : Event_Chain;
/**
* @throws Exception
*/
public function on_wp_init() : Event_Chain
{
$event = new Wp_Init();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function on_wp_admin_init() : Event_Chain
{
$event = new Wp_Admin_Init();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function on_wp() : Event_Chain
{
$event = new Wp();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function on_save_post() : Event_Chain
{
$event = new Save_Post();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function on_save_wc_order_post() : Event_Chain
{
$event = new Save_Wc_Order_Post();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function on_wc_before_shop_loop_item() : Event_Chain
{
$event = new Wc_Before_Shop_Loop_Item();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function on_wc_before_single_product() : Event_Chain
{
$event = new Wc_Before_Single_Product();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function on_wc_loaded() : Event_Chain
{
$event = new Wc_Loaded();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function on_wc_add_to_cart() : Event_Chain
{
$event = new Wc_Add_To_Cart();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_remove_cart_item() : Event_Chain
{
$event = new Wc_Remove_Cart_Item();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_before_checkout_form() : Event_Chain
{
$event = new Wc_Before_Checkout_Form();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_order_status_changed() : Event_Chain
{
$event = new Wc_Order_Status_Changed();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wp_footer() : Event_Chain
{
$event = new Wp_Footer();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_checkout_page() : Event_Chain
{
$event = new Wc_Checkout_Page();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wp_admin_footer() : Event_Chain
{
$event = new Wp_Admin_Footer();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_new_order() : Event_Chain
{
$event = new Wc_New_Order();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wp_cron_every_n_minutes(int $interval, string $schedule_id) : Event_Chain
{
$event = new Wp_Cron_Every_N_Minutes($interval, $schedule_id);
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_before_settings(string $section_id) : Event_Chain
{
$event = new Wc_Before_Settings($section_id);
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_product_update() : Event_Chain
{
$event = new Wc_Product_Update();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_variation_update() : Event_Chain
{
$event = new Wc_Variation_Update();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_variation_create() : Event_Chain
{
$event = new Wc_Variation_Create();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_product_options_general_product_data() : Event_Chain
{
$event = new Wc_Product_Options_General_Product_Data();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
public function on_wc_product_options_pricing() : Event_Chain
{
$event = new Wc_Product_Options_Pricing();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function on_wp_ajax(Field_Ajax_Interface $field_ajax) : Event_Chain
{
$event = new Wp_Ajax($field_ajax);
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function on(string $hook_name, $priority = 10) : Event_Chain
{
$event = new On($hook_name, $priority);
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
/**
* @throws Exception
*/
public function filter_wc_price_html() : Event_Chain
{
$event = new Wc_Filter_Price_Html();
$this->get_event_chain()->add_event($event);
return $this->get_event_chain();
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain\Interfaces\Cache_Interface;
use WC_Session;
class Wc_Session_Cache implements Cache_Interface
{
/**
* @var WC_Session
*/
private $wc_session;
/**
* @var string
*/
private $key;
/**
* @param string|null $key
*/
public function __construct(string $key = null)
{
$this->key = $key;
}
public function push($value, string $key = null)
{
$key = $this->key ?: $key;
$stack = $this->get_wc_session()->get($key);
if (!\is_array($stack)) {
$stack = [];
}
$stack[] = $value;
$this->get_wc_session()->set($key, $stack);
//var_dump($key);die;
}
/**
* @throws Exception
*/
public function get(string $key = null)
{
$key = $this->key ?: $key;
return $this->get_wc_session()->get($key);
}
/**
* @return WC_Session
*/
private function get_wc_session() : WC_Session
{
if (!$this->wc_session) {
$this->wc_session = WC()->session;
}
return $this->wc_session;
}
public function set($value, string $key = null)
{
$key = $this->key ?: $key;
$this->get_wc_session()->set($key, $value);
}
public function get_single(string $key = null)
{
$key = $this->key ?: $key;
$value = $this->get_wc_session()->get($key);
if (!\is_array($value)) {
return null;
}
return $value[$key][0];
}
/**
* @throws Exception
*/
public function clear(string $key = null)
{
$key = $this->key ?: $key;
if ($this->get($key)) {
$this->get_wc_session()->__unset($key);
}
}
}

View File

@@ -0,0 +1,64 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Event_Chain;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Abstract_Ilabs_Plugin;
class Wp_Options_Based_Cache
{
/**
* @var string
*/
private $id;
/**
* @var Event_Chain
*/
private $plugin;
/**
* @var int
*/
private $max_items;
public function __construct(string $id, Abstract_Ilabs_Plugin $plugin, int $max_items = null)
{
$this->id = $id;
$this->plugin = $plugin;
$this->max_items = $max_items;
}
public function push($value)
{
$data = get_option($this->get_options_key());
if (!\is_array($data)) {
$data = [];
} else {
if (null !== $this->max_items && \count($data) === $this->max_items) {
$data = [];
}
}
$data[] = $value;
update_option($this->get_options_key(), $data);
}
private function get_options_key() : string
{
return $this->plugin->get_plugin_prefix() . '_' . $this->id;
}
/**
* @return array|null
*/
public function get() : ?array
{
$value = get_option($this->get_options_key());
return empty($value) ? null : $value;
}
public function get_single(int $key)
{
$data = get_option($this->get_options_key());
if (!\is_array($data)) {
return null;
}
return $data[$key];
}
public function clear()
{
delete_option($this->get_options_key());
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Helper;
class Core
{
public function get_current_domain() : string
{
if (\function_exists('get_home_url')) {
$home_url = get_home_url();
$parsed_url = \parse_url($home_url);
return $parsed_url['host'] ?? '';
} else {
return $_SERVER['HTTP_HOST'] ?? '';
}
}
public function get_visitor_ip() : string
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$re = '/(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/s';
$matches = [];
if (\preg_match($re, $ip, $matches, \PREG_OFFSET_CAPTURE, 0) === 1) {
$ip = $matches[0][0];
} else {
$ip = '';
}
return $ip;
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Logger;
interface Logger_Interface
{
public function log($log);
/**
* @depecated
* @param string $message
* @param array|null $args
* @param string|null $context
*
* @return mixed
*/
public function error(string $message, array $args = null, string $context = null);
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Logger;
class Null_Logger implements Logger_Interface
{
public function log($log)
{
}
public function error(string $message, array $args = null, string $context = null)
{
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Logger;
class Wp_Debug_Logger implements Logger_Interface
{
public function log($log)
{
if (\true === WP_DEBUG_LOG || \true === WP_DEBUG) {
if (\is_array($log) || \is_object($log)) {
\error_log(\print_r($log, \true));
} else {
\error_log($log);
}
}
}
public function error(string $message, array $args = null, string $context = null)
{
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Logger;
class Wp_Options_Logger implements Logger_Interface
{
/**
* @var string
*/
private $wp_options_debug_key;
public function log($log)
{
if (\true === WP_DEBUG_LOG || \true === WP_DEBUG) {
if (\is_array($log) || \is_object($log)) {
update_option($this->wp_options_debug_key, \print_r($log, \true));
} else {
update_option($this->wp_options_debug_key, $log);
}
}
}
public function error(string $message, array $args = null, string $context = null)
{
}
/**
* @param string $wp_options_debug_key
*/
public function set_wp_options_debug_key(string $wp_options_debug_key) : void
{
$this->wp_options_debug_key = $wp_options_debug_key;
}
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
abstract class Abstract_Form_Handler
{
}

View File

@@ -0,0 +1,70 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields\Group;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Interface;
abstract class Abstract_Group_Walker
{
/**
* @var int
*/
private $level;
/**
* @var Group_Interface
*/
protected $group;
public function __construct(Group_Interface $group)
{
$this->level = 0;
$this->group = new Group();
$this->group->set_items([$group]);
}
protected abstract function begin_group_callback(Group_Interface &$group);
protected abstract function end_group_callback(Group_Interface &$group);
protected abstract function group_field_callback(Field_Interface &$field);
/**
* @throws Exception
*/
public function walk(Group_Interface &$group = null) : void
{
if (null === $group) {
$group = $this->group;
}
if ($group_items = $group->get_items()) {
foreach ($group_items as $group_item) {
$group = $this->try_extract_group_interface($group_item);
$group_cache = $group;
if ($field = $this->try_extract_field_interface($group_item)) {
$this->group_field_callback($field);
} elseif ($group) {
$this->begin_group_callback($group);
$this->walk($group);
$this->level++;
} else {
throw new Exception('Invalid group item type');
}
if ($group_cache) {
$this->end_group_callback($group_cache);
}
}
}
}
private function try_extract_field_interface($object) : ?Field_Interface
{
if ($object instanceof Field_Interface) {
return $object;
}
return null;
}
private function try_extract_group_interface($object) : ?Group_Interface
{
if ($object instanceof Group_Interface) {
return $object;
}
return null;
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
abstract class Abstract_Renderer
{
public abstract function render();
}

View File

@@ -0,0 +1,42 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Ajax_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Request;
class Ajax_Request_Group_Extractor extends Request_Group_Extractor
{
/**
* @var Field_Ajax_Interface
*/
private $field_ajax;
/**
* @var Group_Interface
*/
private $result;
public function __construct(Form $form, Field_Ajax_Interface $field_ajax)
{
$this->field_ajax = $field_ajax;
parent::__construct($form->get_items(), new Request());
}
public function extract() : ?Group_Interface
{
$this->walk();
return $this->get_result();
}
protected function end_group_callback(Group_Interface &$group)
{
if ($group->get_id() === $this->field_ajax->get_payload_group_id()) {
$this->result = $group;
}
}
/**
* @return Group_Interface
*/
public function get_result() : ?Group_Interface
{
return $this->result;
}
}

View File

@@ -0,0 +1,89 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Ajax_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Item_Interface;
class Basic_Ajax_Submit implements Field_Ajax_Interface, Group_Item_Interface, Field_Interface
{
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $label;
/**
* @var string
*/
private $desc;
/**
* @var string
*/
private $action;
/**
* @var string
*/
private $payload_group_id;
/**
* @return string
*/
public function get_id() : string
{
return $this->id;
}
/**
* @param string $id
*/
public function set_id(string $id) : void
{
$this->id = $id;
}
/**
* @return string
*/
public function get_label() : string
{
return $this->label;
}
/**
* @param string $label
*/
public function set_label(string $label) : void
{
$this->label = $label;
}
/**
* @return string
*/
public function get_desc() : string
{
return $this->desc;
}
/**
* @param string|null $desc
*/
public function set_desc(?string $desc) : void
{
$this->desc = $desc;
}
public function to_array() : array
{
// TODO: Implement to_array() method.
}
/**
* @return string
*/
public function get_payload_group_id() : string
{
return $this->payload_group_id;
}
public function set_payload_group_id(string $payload_group_id) : void
{
$this->payload_group_id = $payload_group_id;
}
}

View File

@@ -0,0 +1,105 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Checkbox_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Item_Interface;
class Checkbox implements Group_Item_Interface, Field_Interface, Field_Checkbox_Interface
{
/**
* @var string
*/
private $value;
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $label;
/**
* @var string
*/
private $desc;
/**
* @var string
*/
private $default;
public function to_array() : array
{
return ['value' => $this->value, 'id' => $this->id, 'label' => $this->label, 'desc' => $this->desc];
}
/**
* @return string
*/
public function get_value() : string
{
return $this->value;
}
/**
* @param string $value
*/
public function set_value(string $value) : void
{
$this->value = $value;
}
/**
* @return string
*/
public function get_id() : string
{
return $this->id;
}
/**
* @param string $id
*/
public function set_id(string $id) : void
{
$this->id = $id;
}
/**
* @return string
*/
public function get_label() : string
{
return $this->label;
}
/**
* @param string $label
*/
public function set_label(string $label) : void
{
$this->label = $label;
}
/**
* @return string
*/
public function get_desc() : ?string
{
return $this->desc;
}
/**
* @param string $desc
*/
public function set_desc(?string $desc) : void
{
$this->desc = $desc;
}
/**
* @return string
*/
public function get_default() : ?string
{
return $this->default;
}
/**
* @param string $default
*/
public function set_default(?string $default) : void
{
$this->default = $default;
}
}

View File

@@ -0,0 +1,92 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Item_Interface;
class Group implements Group_Interface, Group_Item_Interface
{
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $desc;
/**
* @var Group_Item_Interface[]
*/
private $items;
public function to_array() : array
{
return ['items' => (function () {
$items = [];
foreach ($this->items as $item) {
$items[] = $item->to_array();
}
return $items;
})(), 'id' => $this->id];
}
/**
* @return string
*/
public function get_id() : string
{
return $this->id;
}
/**
* @param string $id
*/
public function set_id(string $id) : void
{
$this->id = $id;
}
/**
* @return string
*/
public function get_name() : ?string
{
return $this->name;
}
/**
* @param string|null $name
*/
public function set_name(?string $name) : void
{
$this->name = $name;
}
/**
* @return array
*/
public function get_items() : ?array
{
return $this->items;
}
/**
* @param array $items
*/
public function set_items(array $items) : void
{
$this->items = $items;
}
/**
* @return string
*/
public function get_desc() : ?string
{
return $this->desc;
}
/**
* @param string|null $desc
*/
public function set_desc(?string $desc) : void
{
$this->desc = $desc;
}
}

View File

@@ -0,0 +1,110 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Number_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Item_Interface;
class Number implements Group_Item_Interface, Field_Number_Interface, Field_Interface
{
/**
* @var int
*/
private $value;
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $label;
/**
* @var string
*/
private $desc;
/**
* @var int
*/
private $default;
public function to_array() : array
{
return ['value' => $this->value, 'id' => $this->id, 'label' => $this->label, 'desc' => $this->desc];
}
/**
* @return int
*/
public function get_value() : int
{
return $this->value;
}
/**
* @param int $value
*
* @return void
*/
public function set_value(?int $value) : void
{
$this->value = $value;
}
public function set_test(int $test)
{
}
/**
* @return string
*/
public function get_id() : string
{
return $this->id;
}
/**
* @param string $id
*/
public function set_id(string $id) : void
{
$this->id = $id;
}
/**
* @return string
*/
public function get_label() : string
{
return $this->label;
}
/**
* @param string $label
*/
public function set_label(string $label) : void
{
$this->label = $label;
}
/**
* @return string
*/
public function get_desc() : string
{
return $this->desc;
}
/**
* @param string $desc
*/
public function set_desc(?string $desc) : void
{
$this->desc = $desc;
}
/**
* @return int
*/
public function get_default() : int
{
return $this->default;
}
/**
* @param int $default
*/
public function set_default(?int $default) : void
{
$this->default = $default;
}
}

View File

@@ -0,0 +1,127 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Select_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Item_Interface;
class Select implements Group_Item_Interface, Field_Interface, Field_Select_Interface
{
/**
* @var string
*/
private $value;
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $label;
/**
* @var string
*/
private $desc;
/**
* @var array
*/
private $options;
/**
* @var array
*/
private $values;
/**
* @var string
*/
private $default;
public function to_array() : array
{
return ['value' => $this->value, 'id' => $this->id, 'label' => $this->label, 'desc' => $this->desc, 'options' => $this->options];
}
/**
* @return array
*/
public function get_options() : array
{
return $this->options;
}
/**
* @param array $options
*/
public function set_options(array $options) : void
{
$this->options = $options;
}
/**
* @return string
*/
public function get_value() : string
{
return $this->value;
}
/**
* @param string $value
*/
public function set_value(string $value) : void
{
$this->value = $value;
}
/**
* @return string
*/
public function get_id() : string
{
return $this->id;
}
/**
* @param string $id
*/
public function set_id(string $id) : void
{
$this->id = $id;
}
/**
* @return string
*/
public function get_label() : string
{
return $this->label;
}
/**
* @param string $label
*/
public function set_label(string $label) : void
{
$this->label = $label;
}
/**
* @return string
*/
public function get_desc() : string
{
return $this->desc;
}
/**
* @param string $desc
*/
public function set_desc(string $desc) : void
{
$this->desc = $desc;
}
/**
* @return string
*/
public function get_default() : string
{
return $this->default;
}
/**
* @param string $default
*/
public function set_default(?string $default) : void
{
$this->default = $default;
}
}

View File

@@ -0,0 +1,117 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Text_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Item_Interface;
class Text implements Group_Item_Interface, Field_Interface, Field_Text_Interface
{
/**
* @var string
*/
private $value;
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $label;
/**
* @var string
*/
private $desc;
/**
* @var string
*/
private $default;
/**
* @var bool
*/
private $is_password;
public function to_array() : array
{
return ['value' => $this->value, 'id' => $this->id, 'label' => $this->label, 'desc' => $this->desc];
}
/**
* @return string
*/
public function get_value() : string
{
return $this->value;
}
/**
* @param string $value
*/
public function set_value(string $value) : void
{
$this->value = $value;
}
/**
* @return string
*/
public function get_id() : string
{
return $this->id;
}
/**
* @param string $id
*/
public function set_id(string $id) : void
{
$this->id = $id;
}
/**
* @return string
*/
public function get_label() : string
{
return $this->label;
}
/**
* @param string $label
*/
public function set_label(string $label) : void
{
$this->label = $label;
}
/**
* @return string
*/
public function get_desc() : string
{
return $this->desc;
}
/**
* @param string $desc
*/
public function set_desc(?string $desc) : void
{
$this->desc = $desc;
}
/**
* @return string
*/
public function get_default() : ?string
{
return $this->default;
}
/**
* @param string|null $default
*/
public function set_default(?string $default) : void
{
$this->default = $default;
}
public function is_password() : bool
{
return $this->is_password;
}
public function set_is_password(?bool $is_password)
{
$this->is_password = $is_password;
}
}

View File

@@ -0,0 +1,105 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Text_Area_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Item_Interface;
class Text_Area implements Group_Item_Interface, Field_Interface, Field_Text_Area_Interface
{
/**
* @var string
*/
private $value;
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $label;
/**
* @var string
*/
private $desc;
/**
* @var string
*/
private $default;
public function to_array() : array
{
return ['value' => $this->value, 'id' => $this->id, 'label' => $this->label, 'desc' => $this->desc];
}
/**
* @return string
*/
public function get_value() : string
{
return $this->value;
}
/**
* @param string $value
*/
public function set_value(string $value) : void
{
$this->value = $value;
}
/**
* @return string
*/
public function get_id() : string
{
return $this->id;
}
/**
* @param string $id
*/
public function set_id(string $id) : void
{
$this->id = $id;
}
/**
* @return string
*/
public function get_label() : string
{
return $this->label;
}
/**
* @param string $label
*/
public function set_label(string $label) : void
{
$this->label = $label;
}
/**
* @return string
*/
public function get_desc() : string
{
return $this->desc;
}
/**
* @param string $desc
*/
public function set_desc(string $desc) : void
{
$this->desc = $desc;
}
/**
* @return string
*/
public function get_default() : string
{
return $this->default;
}
/**
* @param string $default
*/
public function set_default(?string $default) : void
{
$this->default = $default;
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Interface;
class Form
{
/**
* @var Group_Interface
*/
private $items;
/**
* @param Group_Interface $items
*/
public function __construct(Group_Interface $items)
{
$this->items = $items;
}
/**
* @return Group_Interface
*/
public function get_items() : Group_Interface
{
return $this->items;
}
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
class Form_Handler_Post_Meta
{
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
class Form_Handler_Request
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
class Form_Handler_Wp_Options
{
/**
* @var Form
*/
private $form;
public function __construct(Form $form)
{
$this->form = $form;
}
public function fill() : Form
{
}
}

View File

@@ -0,0 +1,8 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
class Helpers
{
}

View File

@@ -0,0 +1,28 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Ajax_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Interface;
class Interfaces
{
/**
* @throws Exception
*/
public static function extract_field_ajax_interface(Field_Interface $field_interface) : Field_Ajax_Interface
{
if ($field_interface instanceof Field_Ajax_Interface) {
return $field_interface;
}
self::throw_exception('Field_Ajax_Interface');
}
/**
* @throws Exception
*/
private static function throw_exception(string $excepted_type)
{
throw new Exception("Object must be type of {$excepted_type}");
}
}

View File

@@ -0,0 +1,64 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Request;
class Request_Group_Extractor extends Abstract_Group_Walker
{
/**
* @var Group_Interface
*/
private $result;
/**
* @var Request
*/
private $request;
/**
* @var bool
*/
private $success = \false;
public function __construct(Group_Interface $group, Request $request)
{
parent::__construct($group);
$this->request = $request;
}
protected function begin_group_callback(Group_Interface &$group)
{
}
protected function end_group_callback(Group_Interface &$group)
{
}
/**
* @throws Exception
*/
public function extract() : ?Group_Interface
{
$this->walk();
return $this->get_result();
}
/**
* @throws Exception
*/
protected function group_field_callback(Field_Interface &$field)
{
if (\method_exists($field, 'set_value')) {
if ($this->request->key_exsists($field->get_id())) {
$field->set_value($this->request->get_by_key($field->get_id()));
$this->success = \true;
} else {
$this->success = \false;
}
}
}
/**
* @return Group_Interface | null
*/
public function get_result() : ?Group_Interface
{
return $this->success ? $this->group : null;
}
}

View File

@@ -0,0 +1,162 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form_Chain;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Abstract_Ilabs_Plugin;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields\Group;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Form;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form_Chain\Traits\Fields;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Field_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Interface;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces\Group_Item_Interface;
class Form_Builder
{
use Fields;
/**
* @var array
*/
private $groups;
/**
* @var Group_Interface
*/
private $current_group;
/**
* @var int
*/
private $current_opened_group_index = 0;
/**
* @var array
*/
private $field_ids_cache = [];
/**
* @var Abstract_Ilabs_Plugin
*/
private $ilabs_plugin;
/**
* @var Group_Interface[]
*/
private $reference_to_parent = [];
public function __construct(Abstract_Ilabs_Plugin $ilabs_plugin)
{
$this->ilabs_plugin = $ilabs_plugin;
}
/**
* @throws Exception
*/
public function begin_group(string $id, string $name = null, string $desc = null) : Form_Builder
{
if (empty($id)) {
throw new Exception('Group id can not be empty!');
}
$group = $this->open_group($id, $name, $desc);
if ($this->current_group !== null) {
$this->reference_to_parent[] = $this->current_group;
} else {
$this->reference_to_parent[] = $group;
}
$this->current_group = $group;
return $this;
}
private function filter_field_id(string $id) : string
{
if (\count($this->reference_to_parent) === 0) {
return $id;
}
return $this->current_group->get_id() . '_' . $id;
}
/**
* @throws Exception
*/
private function filter_group_id(string $id) : string
{
$return = '';
if (\count($this->reference_to_parent) === 0) {
$return = sanitize_title($this->ilabs_plugin->get_plugin_prefix());
}
if (\count($this->reference_to_parent) === 1) {
$parent_group_id = \end($this->reference_to_parent)->get_id();
$return .= $parent_group_id;
}
return "{$return}_{$id}";
}
/**
* @throws Exception
*/
public function close_group() : Form_Builder
{
if (\count($this->reference_to_parent) === 1) {
return $this;
}
$new_current_group = \end($this->reference_to_parent);
$items = $new_current_group->get_items();
$items[] = $this->current_group;
$new_current_group->set_items($items);
$this->current_group = $new_current_group;
\array_pop($this->reference_to_parent);
return $this;
}
public function build() : Form
{
return new Form($this->current_group);
}
/**
* @throws Exception
*/
private function extract_group_item_interface($object) : Group_Item_Interface
{
if ($object instanceof Group_Item_Interface) {
return $object;
} else {
throw new Exception('Object must be instance of Group_Item_Interface');
}
}
/**
* @throws Exception
*/
private function open_group(string $id, string $name = null, string $desc = null) : Group_Interface
{
$group = new Group();
$group->set_id($this->filter_group_id($id));
$group->set_name($name);
$group->set_desc($desc);
return $group;
}
protected function get_form_chain() : self
{
return $this;
}
/**
* @throws Exception
*/
protected function add_group_item(Group_Item_Interface $field, bool $force = \false)
{
$field->set_id($this->filter_field_id($field->get_id()));
if (!$force) {
$field_ids_cache = $this->field_ids_cache;
if (\in_array($field->get_id(), $field_ids_cache)) {
throw new Exception("Duplicated Field ID detected! ({$field->get_id()}). Field ID must be unique!");
}
}
$items = $this->current_group->get_items();
$items[] = $field;
$this->current_group->set_items($items);
$field_ids_cache[] = $field->get_id();
$this->field_ids_cache = $field_ids_cache;
}
/**
* @return Group_Interface
*/
public function get_current_group() : Group_Interface
{
return $this->current_group;
}
/**
* @return Field_Interface
*/
public function get_current_field() : Field_Interface
{
return $this->current_group->get_items()[\count($this->current_group->get_items()) - 1];
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form_Chain\Traits;
use Exception;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields\Basic_Ajax_Submit;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields\Checkbox;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields\Number;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields\Text;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form\Fields\Text_Area;
use Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Form_Chain\Form_Builder;
trait Fields
{
/**
* @throws Exception
*/
public function add_field_checkbox(string $id, string $label = '', string $desc = '', string $value = '', string $default = null) : Form_Builder
{
$field = new Checkbox();
$field->set_desc($desc);
$field->set_id($id);
$field->set_label($label);
$field->set_value($value);
$field->set_default($default);
$this->get_form_chain()->add_group_item($field);
return $this->get_form_chain();
}
protected abstract function get_form_chain() : Form_Builder;
/**
* @throws Exception
*/
public function add_field_number(string $id, string $label = '', string $desc = '', int $value = null, int $default = null) : Form_Builder
{
$field = new Number();
$field->set_desc($desc);
$field->set_id($id);
$field->set_label($label);
$field->set_value($value);
$field->set_default($default);
$this->get_form_chain()->add_group_item($field);
return $this->get_form_chain();
}
/**
* @throws Exception
*/
public function add_field_text(string $id, string $label = '', string $desc = '', string $value = '', string $default = null, $is_password = \false) : Form_Builder
{
$field = new Text();
$field->set_desc($desc);
$field->set_id($id);
$field->set_label($label);
$field->set_value($value);
$field->set_default($default);
$field->set_is_password($is_password);
$this->get_form_chain()->add_group_item($field);
return $this->get_form_chain();
}
/**
* @throws Exception
*/
public function add_field_text_area(string $id, string $label = '', string $desc = '', string $value = '', string $default = null) : Form_Builder
{
$field = new Text_Area();
$field->set_desc($desc);
$field->set_id($id);
$field->set_label($label);
$field->set_value($value);
$field->set_default($default);
$this->get_form_chain()->add_group_item($field);
return $this->get_form_chain();
}
/**
* @throws Exception
*/
public function add_ajax_integration(string $id, string $label = '', string $desc = '', string $payload_group_id = null) : Form_Builder
{
$payload_group_id = $payload_group_id ?: $this->get_form_chain()->get_current_group()->get_id();
$field = new Basic_Ajax_Submit();
$field->set_desc($desc);
$field->set_id($id);
$field->set_label($label);
$field->set_payload_group_id($payload_group_id);
$this->get_form_chain()->add_group_item($field);
return $this->get_form_chain();
}
}

View File

@@ -0,0 +1,16 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces;
interface Field_Ajax_Interface
{
public function set_payload_group_id(string $payload_group_id) : void;
public function get_payload_group_id() : string;
public function get_label() : string;
public function set_label(string $label);
public function get_id() : string;
public function set_id(string $label);
public function get_desc() : ?string;
public function set_desc(?string $desc);
}

View File

@@ -0,0 +1,12 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces;
interface Field_Checkbox_Interface
{
public function get_value() : string;
public function set_value(string $values);
public function get_default() : ?string;
public function set_default(?string $default);
}

View File

@@ -0,0 +1,12 @@
<?php
declare (strict_types=1);
namespace Isolated\BlueMedia\Ilabs\Ilabs_Plugin\Presentation\Interfaces;
interface Field_Decimal_Interface
{
public function get_value() : float;
public function set_value(float $value);
public function get_precision() : int;
public function set_precision(int $precision);
}

Some files were not shown because too many files have changed in this diff Show More