first commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\BuildDirector;
|
||||
|
||||
use FcfVendor\WPDesk\PluginBuilder\Builder\AbstractBuilder;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Storage\StorageFactory;
|
||||
class LegacyBuildDirector
|
||||
{
|
||||
/** @var AbstractBuilder */
|
||||
private $builder;
|
||||
public function __construct(\FcfVendor\WPDesk\PluginBuilder\Builder\AbstractBuilder $builder)
|
||||
{
|
||||
$this->builder = $builder;
|
||||
}
|
||||
/**
|
||||
* Builds plugin
|
||||
*/
|
||||
public function build_plugin()
|
||||
{
|
||||
$this->builder->build_plugin();
|
||||
$this->builder->init_plugin();
|
||||
$storage = new \FcfVendor\WPDesk\PluginBuilder\Storage\StorageFactory();
|
||||
$this->builder->store_plugin($storage->create_storage());
|
||||
}
|
||||
/**
|
||||
* Returns built plugin
|
||||
*
|
||||
* @return AbstractPlugin
|
||||
*/
|
||||
public function get_plugin()
|
||||
{
|
||||
return $this->builder->get_plugin();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Builder;
|
||||
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Storage\PluginStorage;
|
||||
abstract class AbstractBuilder
|
||||
{
|
||||
/**
|
||||
* Create plugin class
|
||||
*/
|
||||
public function build_plugin()
|
||||
{
|
||||
}
|
||||
/**
|
||||
* Store plugin class in some kind of storage
|
||||
*/
|
||||
public function store_plugin(\FcfVendor\WPDesk\PluginBuilder\Storage\PluginStorage $storage)
|
||||
{
|
||||
}
|
||||
/**
|
||||
* Init plugin internal structure
|
||||
*/
|
||||
public function init_plugin()
|
||||
{
|
||||
}
|
||||
/**
|
||||
* Return built plugin
|
||||
* @return AbstractPlugin
|
||||
*/
|
||||
abstract function get_plugin();
|
||||
/**
|
||||
* Set settings class in plugin
|
||||
*
|
||||
* @param $settings
|
||||
*/
|
||||
public function set_settings($settings)
|
||||
{
|
||||
}
|
||||
/**
|
||||
* Set view class in plugin
|
||||
*
|
||||
* @param $view
|
||||
*/
|
||||
public function set_view($view)
|
||||
{
|
||||
}
|
||||
/**
|
||||
* Set tracker class in plugin
|
||||
*
|
||||
* @param $tracker
|
||||
*/
|
||||
public function set_tracker($tracker)
|
||||
{
|
||||
}
|
||||
/**
|
||||
* Set helper class in plugin
|
||||
*
|
||||
* @param $helper
|
||||
*/
|
||||
public function set_helper($helper)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Builder;
|
||||
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\ActivationAware;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Storage\PluginStorage;
|
||||
/**
|
||||
* Builder that have info about activations
|
||||
*
|
||||
* Warning: We can't extend InfoBuilder.php as some old plugins(without wp-flow) will load the old version od InfoBuilder class that have private plugin property.
|
||||
*
|
||||
* @package WPDesk\PluginBuilder\Builder
|
||||
*/
|
||||
class InfoActivationBuilder extends \FcfVendor\WPDesk\PluginBuilder\Builder\AbstractBuilder
|
||||
{
|
||||
const FILTER_PLUGIN_CLASS = 'wp_builder_plugin_class';
|
||||
const HOOK_BEFORE_PLUGIN_INIT = 'wp_builder_before_plugin_init';
|
||||
const HOOK_AFTER_PLUGIN_INIT = 'wp_builder_before_init';
|
||||
/** @var AbstractPlugin */
|
||||
private $plugin;
|
||||
/** @var \WPDesk_Buildable */
|
||||
private $info;
|
||||
/** @var string */
|
||||
protected $storage_id;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $is_active;
|
||||
/**
|
||||
* @param \WPDesk_Buildable $info
|
||||
* @param bool $is_active
|
||||
*/
|
||||
public function __construct(\FcfVendor\WPDesk_Buildable $info, $is_active)
|
||||
{
|
||||
$this->info = $info;
|
||||
$this->storage_id = $info->get_class_name();
|
||||
$this->is_active = $is_active;
|
||||
}
|
||||
/**
|
||||
* Builds instance of plugin
|
||||
*/
|
||||
public function build_plugin()
|
||||
{
|
||||
$class_name = \apply_filters(self::FILTER_PLUGIN_CLASS, $this->info->get_class_name());
|
||||
/** @var AbstractPlugin $plugin */
|
||||
$this->plugin = new $class_name($this->info);
|
||||
if ($this->plugin instanceof \FcfVendor\WPDesk\PluginBuilder\Plugin\ActivationAware && $this->is_active) {
|
||||
$this->plugin->set_active();
|
||||
}
|
||||
return $this->plugin;
|
||||
}
|
||||
public function store_plugin(\FcfVendor\WPDesk\PluginBuilder\Storage\PluginStorage $storage)
|
||||
{
|
||||
$storage->add_to_storage($this->storage_id, $this->plugin);
|
||||
}
|
||||
public function init_plugin()
|
||||
{
|
||||
\do_action(self::HOOK_BEFORE_PLUGIN_INIT, $this->plugin);
|
||||
$this->plugin->init();
|
||||
\do_action(self::HOOK_AFTER_PLUGIN_INIT, $this->plugin);
|
||||
}
|
||||
/**
|
||||
* @return AbstractPlugin
|
||||
*/
|
||||
public function get_plugin()
|
||||
{
|
||||
return $this->plugin;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Builder;
|
||||
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Storage\PluginStorage;
|
||||
/**
|
||||
* @deprecated Should not be used as some old plugins are using it and we can't touch this.
|
||||
*
|
||||
* @package WPDesk\PluginBuilder\Builder
|
||||
*/
|
||||
class InfoBuilder extends \FcfVendor\WPDesk\PluginBuilder\Builder\AbstractBuilder
|
||||
{
|
||||
const FILTER_PLUGIN_CLASS = 'wp_builder_plugin_class';
|
||||
const HOOK_BEFORE_PLUGIN_INIT = 'wp_builder_before_plugin_init';
|
||||
const HOOK_AFTER_PLUGIN_INIT = 'wp_builder_before_init';
|
||||
/** @var AbstractPlugin */
|
||||
private $plugin;
|
||||
/** @var \WPDesk_Buildable */
|
||||
private $info;
|
||||
/** @var string */
|
||||
protected $storage_id;
|
||||
public function __construct(\FcfVendor\WPDesk_Buildable $info)
|
||||
{
|
||||
$this->info = $info;
|
||||
$this->storage_id = $info->get_class_name();
|
||||
}
|
||||
/**
|
||||
* Builds instance of plugin
|
||||
*/
|
||||
public function build_plugin()
|
||||
{
|
||||
$class_name = \apply_filters(self::FILTER_PLUGIN_CLASS, $this->info->get_class_name());
|
||||
/** @var AbstractPlugin $plugin */
|
||||
$this->plugin = new $class_name($this->info);
|
||||
}
|
||||
public function store_plugin(\FcfVendor\WPDesk\PluginBuilder\Storage\PluginStorage $storage)
|
||||
{
|
||||
$storage->add_to_storage($this->storage_id, $this->plugin);
|
||||
}
|
||||
public function init_plugin()
|
||||
{
|
||||
\do_action(self::HOOK_BEFORE_PLUGIN_INIT, $this->plugin);
|
||||
$this->plugin->init();
|
||||
\do_action(self::HOOK_AFTER_PLUGIN_INIT, $this->plugin);
|
||||
}
|
||||
/**
|
||||
* @return AbstractPlugin
|
||||
*/
|
||||
public function get_plugin()
|
||||
{
|
||||
return $this->plugin;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
/**
|
||||
* Base plugin with most basic functionalities used by every WPDesk plugin.
|
||||
*
|
||||
*
|
||||
* Known issues:
|
||||
*
|
||||
* The class name is too generic but can't be changed as it would introduce a major incompatibility for most of the plugins.
|
||||
* The $plugin_url, $docs_url and most other fields should be removed as they only litter the place but for compatibility reasons we can't do it right now.
|
||||
* Hook methods should be moved to external classes but for compatibility reasons we can't do it right now.
|
||||
*/
|
||||
abstract class AbstractPlugin extends \FcfVendor\WPDesk\PluginBuilder\Plugin\SlimPlugin
|
||||
{
|
||||
/**
|
||||
* Most info about plugin internals.
|
||||
*
|
||||
* @var \WPDesk_Plugin_Info
|
||||
*/
|
||||
protected $plugin_info;
|
||||
/**
|
||||
* Unique string for this plugin in [a-z_]+ format.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $plugin_namespace;
|
||||
/**
|
||||
* Absolute URL to the plugin dir.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $plugin_url;
|
||||
/**
|
||||
* Absolute URL to the plugin docs.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $docs_url;
|
||||
/**
|
||||
* Absolute URL to the plugin settings url.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $settings_url;
|
||||
/**
|
||||
* Support URL.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $support_url;
|
||||
/**
|
||||
* AbstractPlugin constructor.
|
||||
*
|
||||
* @param \WPDesk_Plugin_Info $plugin_info
|
||||
*/
|
||||
public function __construct($plugin_info)
|
||||
{
|
||||
$this->plugin_info = $plugin_info;
|
||||
$this->plugin_namespace = \strtolower($plugin_info->get_plugin_dir());
|
||||
$this->plugin_url = $this->plugin_info->get_plugin_url();
|
||||
$this->init_base_variables();
|
||||
}
|
||||
/**
|
||||
* Initialize internal state of the plugin.
|
||||
*
|
||||
* @return void
|
||||
* @deprecated Just use __construct to initialize plugin internal state.
|
||||
*
|
||||
*/
|
||||
public function init_base_variables()
|
||||
{
|
||||
}
|
||||
/**
|
||||
* Initializes plugin external state.
|
||||
*
|
||||
* The plugin internal state is initialized in the constructor and the plugin should be internally consistent after creation.
|
||||
* The external state includes hooks execution, communication with other plugins, integration with WC etc.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->hooks();
|
||||
}
|
||||
/**
|
||||
* Returns absolute path to the plugin dir.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_plugin_file_path()
|
||||
{
|
||||
return $this->plugin_info->get_plugin_file_name();
|
||||
}
|
||||
/**
|
||||
* Returns plugin text domain.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_text_domain()
|
||||
{
|
||||
return $this->plugin_info->get_text_domain();
|
||||
}
|
||||
/**
|
||||
* Returns unique string for plugin in [a-z_]+ format. Can be used as plugin id in various places like plugin slug etc.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_namespace()
|
||||
{
|
||||
return $this->plugin_namespace;
|
||||
}
|
||||
/**
|
||||
* Returns plugin absolute URL.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_plugin_url()
|
||||
{
|
||||
return \esc_url(\trailingslashit($this->plugin_url));
|
||||
}
|
||||
/**
|
||||
* Returns plugin absolute URL to dir with front end assets.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_plugin_assets_url()
|
||||
{
|
||||
return \esc_url(\trailingslashit($this->get_plugin_url() . 'assets'));
|
||||
}
|
||||
/**
|
||||
* @return $this
|
||||
* @deprecated For backward compatibility.
|
||||
*
|
||||
*/
|
||||
public function get_plugin()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* Integrate with WordPress and with other plugins using action/filter system.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function hooks()
|
||||
{
|
||||
\add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts']);
|
||||
\add_action('wp_enqueue_scripts', [$this, 'wp_enqueue_scripts']);
|
||||
\add_action('plugins_loaded', [$this, 'load_plugin_text_domain']);
|
||||
\add_filter('plugin_action_links_' . \plugin_basename($this->get_plugin_file_path()), [$this, 'links_filter']);
|
||||
}
|
||||
/**
|
||||
* Initialize plugin test domain. This is a hook function. Do not execute directly.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_plugin_text_domain()
|
||||
{
|
||||
\load_plugin_textdomain($this->get_text_domain(), \false, $this->get_namespace() . '/lang/');
|
||||
}
|
||||
/**
|
||||
* Append JS scripts in the WordPress admin panel. This is a hook function. Do not execute directly.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function admin_enqueue_scripts()
|
||||
{
|
||||
}
|
||||
/**
|
||||
* Append JS scripts in WordPress. This is a hook function. Do not execute directly.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function wp_enqueue_scripts()
|
||||
{
|
||||
}
|
||||
/**
|
||||
* Initialize plugin admin links. This is a hook function. Do not execute directly.
|
||||
*
|
||||
* @param string[] $links
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function links_filter($links)
|
||||
{
|
||||
$support_link = \get_locale() === 'pl_PL' ? 'https://www.wpdesk.pl/support/' : 'https://www.wpdesk.net/support';
|
||||
if ($this->support_url) {
|
||||
$support_link = $this->support_url;
|
||||
}
|
||||
$plugin_links = ['<a target="_blank" href="' . $support_link . '">' . \__('Support', $this->get_text_domain()) . '</a>'];
|
||||
$links = \array_merge($plugin_links, $links);
|
||||
if ($this->docs_url) {
|
||||
$plugin_links = ['<a target="_blank" href="' . $this->docs_url . '">' . \__('Docs', $this->get_text_domain()) . '</a>'];
|
||||
$links = \array_merge($plugin_links, $links);
|
||||
}
|
||||
if ($this->settings_url) {
|
||||
$plugin_links = ['<a href="' . $this->settings_url . '">' . \__('Settings', $this->get_text_domain()) . '</a>'];
|
||||
$links = \array_merge($plugin_links, $links);
|
||||
}
|
||||
return $links;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
/**
|
||||
* Tag the plugin with this ingterface to hook it to the WordPress activation hook.
|
||||
*
|
||||
* Note: works from plugin flow ^2.2.
|
||||
*
|
||||
* @package WPDesk\PluginBuilder\Plugin
|
||||
*/
|
||||
interface Activateable
|
||||
{
|
||||
/**
|
||||
* Plugin activated in WordPress. Do not execute directly.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function activate();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
/**
|
||||
* It means that this class is should know about SUBSCRIPTION activation
|
||||
*
|
||||
* @package WPDesk\PluginBuilder\Plugin
|
||||
*/
|
||||
interface ActivationAware
|
||||
{
|
||||
/**
|
||||
* Set the activation flag to true
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set_active();
|
||||
/**
|
||||
* Is subscription active?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_active();
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
/**
|
||||
* @deprecated nobody uses it :) And also this library is not a place for this class
|
||||
*
|
||||
* @package WPDesk\PluginBuilder\Plugin
|
||||
*/
|
||||
class ActivationTracker
|
||||
{
|
||||
/**
|
||||
* Namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $namespace;
|
||||
/**
|
||||
* ActivationTracker constructor.
|
||||
*
|
||||
* @param string $namespace Namespace for settings.
|
||||
*/
|
||||
public function __construct($namespace)
|
||||
{
|
||||
$this->namespace = $namespace;
|
||||
}
|
||||
/**
|
||||
* Option name for date storage
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_option_name_activation_date()
|
||||
{
|
||||
return $this->namespace . '_activation';
|
||||
}
|
||||
/**
|
||||
* Returns activation date and sets it if were not set before
|
||||
*
|
||||
* @return int unix timestamp for activation datetime
|
||||
*/
|
||||
public function get_activation_date()
|
||||
{
|
||||
$activation_date = \get_option($this->get_option_name_activation_date());
|
||||
if (empty($activation_date)) {
|
||||
return $this->touch_activation_date();
|
||||
}
|
||||
return \intval($activation_date);
|
||||
}
|
||||
/**
|
||||
* Was activation more than two weeks before today
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_activated_more_than_two_weeks()
|
||||
{
|
||||
$two_weeks = 60 * 60 * 24 * 7 * 2;
|
||||
return $this->get_activation_date() + $two_weeks < \time();
|
||||
}
|
||||
/**
|
||||
* Sets activatiion date for today
|
||||
*
|
||||
* @return int unit timestamp for now
|
||||
*/
|
||||
public function touch_activation_date()
|
||||
{
|
||||
$now = \time();
|
||||
\update_option($this->get_option_name_activation_date(), $now);
|
||||
return $now;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
/**
|
||||
* Something that can be instantiated/hooked conditionally.
|
||||
*
|
||||
* @see https://github.com/mwpd/basic-scaffold/blob/master/src/Infrastructure/Conditional.php by Alain Schlesser
|
||||
*
|
||||
* @package WPDesk\PluginBuilder\Plugin
|
||||
*/
|
||||
interface Conditional
|
||||
{
|
||||
/**
|
||||
* Check whether the conditional object is currently needed.
|
||||
*
|
||||
* @return bool Whether the conditional object is needed.
|
||||
*/
|
||||
public static function is_needed();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
/**
|
||||
* Tag the plugin with this ingterface to hook it to the WordPress deactivation hook.
|
||||
*
|
||||
* Note: works from plugin flow ^2.2.
|
||||
*
|
||||
* @package WPDesk\PluginBuilder\Plugin
|
||||
*/
|
||||
interface Deactivateable
|
||||
{
|
||||
/**
|
||||
* Plugin deactivate in WordPress. Do not execute directly.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function deactivate();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
interface Hookable
|
||||
{
|
||||
/**
|
||||
* Init hooks (actions and filters).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function hooks();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
interface HookableCollection extends \FcfVendor\WPDesk\PluginBuilder\Plugin\Hookable
|
||||
{
|
||||
/**
|
||||
* Add hookable object.
|
||||
*
|
||||
* @param Hookable|HookablePluginDependant $hookable_object Hookable object.
|
||||
*/
|
||||
public function add_hookable(\FcfVendor\WPDesk\PluginBuilder\Plugin\Hookable $hookable_object);
|
||||
/**
|
||||
* Get hookable instance.
|
||||
*
|
||||
* @param string $class_name Class name.
|
||||
*
|
||||
* @return false|Hookable
|
||||
*/
|
||||
public function get_hookable_instance_by_class_name($class_name);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
trait HookableParent
|
||||
{
|
||||
/**
|
||||
* Hookable objects.
|
||||
*
|
||||
* @var array[Hookable]
|
||||
*/
|
||||
private $hookable_objects = array();
|
||||
/**
|
||||
* Add hookable object.
|
||||
*
|
||||
* @param Hookable|HookablePluginDependant $hookable_object Hookable object.
|
||||
*/
|
||||
public function add_hookable(\FcfVendor\WPDesk\PluginBuilder\Plugin\Hookable $hookable_object)
|
||||
{
|
||||
if ($hookable_object instanceof \FcfVendor\WPDesk\PluginBuilder\Plugin\HookablePluginDependant) {
|
||||
$hookable_object->set_plugin($this);
|
||||
}
|
||||
$this->hookable_objects[] = $hookable_object;
|
||||
}
|
||||
/**
|
||||
* Get hookable instance.
|
||||
*
|
||||
* @param string $class_name Class name.
|
||||
*
|
||||
* @return false|Hookable
|
||||
*/
|
||||
public function get_hookable_instance_by_class_name($class_name)
|
||||
{
|
||||
foreach ($this->hookable_objects as $hookable_object) {
|
||||
if ($hookable_object instanceof $class_name) {
|
||||
return $hookable_object;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
/**
|
||||
* Run hooks method on all hookable objects.
|
||||
*/
|
||||
protected function hooks_on_hookable_objects()
|
||||
{
|
||||
/** @var Hookable $hookable_object $hookable_object */
|
||||
foreach ($this->hookable_objects as $hookable_object) {
|
||||
if ($hookable_object instanceof \FcfVendor\WPDesk\PluginBuilder\Plugin\Conditional) {
|
||||
if ($hookable_object::is_needed()) {
|
||||
$hookable_object->hooks();
|
||||
}
|
||||
} else {
|
||||
$hookable_object->hooks();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
interface HookablePluginDependant extends \FcfVendor\WPDesk\PluginBuilder\Plugin\Hookable
|
||||
{
|
||||
/**
|
||||
* Set Plugin.
|
||||
*
|
||||
* @param AbstractPlugin $plugin Plugin.
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public function set_plugin(\FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin $plugin);
|
||||
/**
|
||||
* Get plugin.
|
||||
*
|
||||
* @return AbstractPlugin.
|
||||
*/
|
||||
public function get_plugin();
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
/**
|
||||
* @package WPDesk\PluginBuilder\Plugin
|
||||
*/
|
||||
trait PluginAccess
|
||||
{
|
||||
/**
|
||||
* Plugin.
|
||||
*
|
||||
* @var AbstractPlugin
|
||||
*/
|
||||
private $plugin;
|
||||
/**
|
||||
* Set plugin.
|
||||
*
|
||||
* @param AbstractPlugin $plugin Plugin.
|
||||
*/
|
||||
public function set_plugin(\FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin $plugin)
|
||||
{
|
||||
$this->plugin = $plugin;
|
||||
}
|
||||
/**
|
||||
* Get plugin.
|
||||
*
|
||||
* @return AbstractPlugin
|
||||
*/
|
||||
public function get_plugin()
|
||||
{
|
||||
return $this->plugin;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
/**
|
||||
* Most clean plugin class with only most important details.
|
||||
*/
|
||||
abstract class SlimPlugin implements \FcfVendor\WPDesk_Translatable
|
||||
{
|
||||
/**
|
||||
* Initializes plugin external state.
|
||||
*
|
||||
* The plugin internal state is initialized in the constructor and the plugin should be internally consistent after creation.
|
||||
* The external state includes hooks execution, communication with other plugins, integration with WC etc.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public abstract function init();
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Plugin;
|
||||
|
||||
/**
|
||||
* @deprecated Use wpdesk/wp-view
|
||||
*
|
||||
* @package WPDesk\PluginBuilder\Plugin
|
||||
*/
|
||||
trait TemplateLoad
|
||||
{
|
||||
/**
|
||||
* Plugin path.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $plugin_path;
|
||||
/**
|
||||
* Template path.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $template_path;
|
||||
/**
|
||||
* Init base variables for plugin
|
||||
*/
|
||||
public function init_template_base_variables()
|
||||
{
|
||||
$this->plugin_path = $this->plugin_info->get_plugin_dir();
|
||||
$this->template_path = $this->plugin_info->get_text_domain();
|
||||
}
|
||||
/**
|
||||
* Renders end returns selected template
|
||||
*
|
||||
* @param string $name Name of the template.
|
||||
* @param string $path Additional inner path to the template.
|
||||
* @param array $args args Accessible from template.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function load_template($name, $path = '', $args = array())
|
||||
{
|
||||
$plugin_template_path = \trailingslashit($this->plugin_path) . 'templates/';
|
||||
// Look within passed path within the theme - this is priority.
|
||||
$template = \locate_template(array(\trailingslashit($this->get_template_path()) . \trailingslashit($path) . $name . '.php'));
|
||||
if (!$template) {
|
||||
$template = $plugin_template_path . \trailingslashit($path) . $name . '.php';
|
||||
}
|
||||
\extract($args);
|
||||
\ob_start();
|
||||
include $template;
|
||||
return \ob_get_clean();
|
||||
}
|
||||
/**
|
||||
* Get template path.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_template_path()
|
||||
{
|
||||
return \trailingslashit($this->template_path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor;
|
||||
|
||||
/**
|
||||
* Have info about what class should be built by WPDesk_Builder
|
||||
*
|
||||
* have to be compatible with PHP 5.2.x
|
||||
*/
|
||||
interface WPDesk_Buildable
|
||||
{
|
||||
/** @return string */
|
||||
public function get_class_name();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor;
|
||||
|
||||
if (!\interface_exists('FcfVendor\\WPDesk_Translatable')) {
|
||||
require_once __DIR__ . '/Translatable.php';
|
||||
}
|
||||
/**
|
||||
* Have MUST HAVE info for plugin instantion
|
||||
*
|
||||
* have to be compatible with PHP 5.2.x
|
||||
*/
|
||||
interface WPDesk_Has_Plugin_Info extends \FcfVendor\WPDesk_Translatable
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_plugin_file_name();
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_plugin_dir();
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_version();
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor;
|
||||
|
||||
if (!\interface_exists('FcfVendor\\WPDesk_Translatable')) {
|
||||
require_once __DIR__ . '/Translatable.php';
|
||||
}
|
||||
if (!\interface_exists('FcfVendor\\WPDesk_Buildable')) {
|
||||
require_once __DIR__ . '/Buildable.php';
|
||||
}
|
||||
if (!\interface_exists('FcfVendor\\WPDesk_Has_Plugin_Info')) {
|
||||
require_once __DIR__ . '/Has_Plugin_Info.php';
|
||||
}
|
||||
/**
|
||||
* Structure with core info about plugin
|
||||
*
|
||||
* have to be compatible with PHP 5.2.x
|
||||
*/
|
||||
class WPDesk_Plugin_Info implements \FcfVendor\WPDesk_Translatable, \FcfVendor\WPDesk_Buildable, \FcfVendor\WPDesk_Has_Plugin_Info
|
||||
{
|
||||
/** @var string */
|
||||
private $plugin_file_name;
|
||||
/** @var string */
|
||||
private $plugin_dir;
|
||||
/** @var string */
|
||||
private $plugin_url;
|
||||
/** @var string */
|
||||
private $class_name;
|
||||
/** @var string */
|
||||
private $version;
|
||||
/** @var string */
|
||||
private $product_id;
|
||||
/** @var string */
|
||||
private $plugin_name;
|
||||
/** @var \DateTimeInterface */
|
||||
private $release_date;
|
||||
/** string */
|
||||
private $text_domain;
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_plugin_file_name()
|
||||
{
|
||||
return $this->plugin_file_name;
|
||||
}
|
||||
/**
|
||||
* @param string $plugin_name
|
||||
*/
|
||||
public function set_plugin_file_name($plugin_name)
|
||||
{
|
||||
$this->plugin_file_name = $plugin_name;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_plugin_dir()
|
||||
{
|
||||
return $this->plugin_dir;
|
||||
}
|
||||
/**
|
||||
* @param string $plugin_dir
|
||||
*/
|
||||
public function set_plugin_dir($plugin_dir)
|
||||
{
|
||||
$this->plugin_dir = $plugin_dir;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_plugin_url()
|
||||
{
|
||||
return $this->plugin_url;
|
||||
}
|
||||
/**
|
||||
* @param string $plugin_url
|
||||
*/
|
||||
public function set_plugin_url($plugin_url)
|
||||
{
|
||||
$this->plugin_url = $plugin_url;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_version()
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
/**
|
||||
* @param string $version
|
||||
*/
|
||||
public function set_version($version)
|
||||
{
|
||||
$this->version = $version;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_product_id()
|
||||
{
|
||||
return $this->product_id;
|
||||
}
|
||||
/**
|
||||
* @param string $product_id
|
||||
*/
|
||||
public function set_product_id($product_id)
|
||||
{
|
||||
$this->product_id = $product_id;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_plugin_name()
|
||||
{
|
||||
return $this->plugin_name;
|
||||
}
|
||||
/**
|
||||
* @param string $plugin_name
|
||||
*/
|
||||
public function set_plugin_name($plugin_name)
|
||||
{
|
||||
$this->plugin_name = $plugin_name;
|
||||
}
|
||||
/**
|
||||
* @return DateTimeInterface
|
||||
*/
|
||||
public function get_release_date()
|
||||
{
|
||||
return $this->release_date;
|
||||
}
|
||||
/**
|
||||
* @param \DateTimeInterface $release_date
|
||||
*/
|
||||
public function set_release_date($release_date)
|
||||
{
|
||||
$this->release_date = $release_date;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_class_name()
|
||||
{
|
||||
return $this->class_name;
|
||||
}
|
||||
/**
|
||||
* @param string $class_name
|
||||
*/
|
||||
public function set_class_name($class_name)
|
||||
{
|
||||
$this->class_name = $class_name;
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_text_domain()
|
||||
{
|
||||
return $this->text_domain;
|
||||
}
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function set_text_domain($value)
|
||||
{
|
||||
$this->text_domain = $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor;
|
||||
|
||||
/**
|
||||
* @deprecated Have typo so better use WPDesk_Translatable
|
||||
*/
|
||||
interface WPDesk_Translable
|
||||
{
|
||||
/** @return string */
|
||||
public function get_text_domain();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor;
|
||||
|
||||
if (!\interface_exists('FcfVendor\\WPDesk_Translable')) {
|
||||
require_once 'Translable.php';
|
||||
}
|
||||
/**
|
||||
* Have info about textdomain - how to translate texts
|
||||
*
|
||||
* have to be compatible with PHP 5.2.x
|
||||
*/
|
||||
interface WPDesk_Translatable extends \FcfVendor\WPDesk_Translable
|
||||
{
|
||||
/** @return string */
|
||||
public function get_text_domain();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Storage\Exception;
|
||||
|
||||
class ClassAlreadyExists extends \RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Storage\Exception;
|
||||
|
||||
class ClassNotExists extends \RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Storage;
|
||||
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
|
||||
interface PluginStorage
|
||||
{
|
||||
/**
|
||||
* @param string $class
|
||||
* @param AbstractPlugin $object
|
||||
*/
|
||||
public function add_to_storage($class, $object);
|
||||
/**
|
||||
* @param string $class
|
||||
*
|
||||
* @return AbstractPlugin
|
||||
*/
|
||||
public function get_from_storage($class);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Storage;
|
||||
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
|
||||
/**
|
||||
* Can store plugin instances in static variable
|
||||
*
|
||||
* @package WPDesk\PluginBuilder\Storage
|
||||
*/
|
||||
class StaticStorage implements \FcfVendor\WPDesk\PluginBuilder\Storage\PluginStorage
|
||||
{
|
||||
protected static $instances = [];
|
||||
/**
|
||||
* @param string $class
|
||||
* @param AbstractPlugin $object
|
||||
*/
|
||||
public function add_to_storage($class, $object)
|
||||
{
|
||||
if (isset(self::$instances[$class])) {
|
||||
throw new \FcfVendor\WPDesk\PluginBuilder\Storage\Exception\ClassAlreadyExists("Class {$class} already exists");
|
||||
}
|
||||
self::$instances[$class] = $object;
|
||||
}
|
||||
/**
|
||||
* @param string $class
|
||||
*
|
||||
* @return AbstractPlugin
|
||||
*/
|
||||
public function get_from_storage($class)
|
||||
{
|
||||
if (isset(self::$instances[$class])) {
|
||||
return self::$instances[$class];
|
||||
}
|
||||
throw new \FcfVendor\WPDesk\PluginBuilder\Storage\Exception\ClassNotExists("Class {$class} not exists in storage");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Storage;
|
||||
|
||||
class StorageFactory
|
||||
{
|
||||
/**
|
||||
* @return PluginStorage
|
||||
*/
|
||||
public function create_storage()
|
||||
{
|
||||
return new \FcfVendor\WPDesk\PluginBuilder\Storage\WordpressFilterStorage();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\PluginBuilder\Storage;
|
||||
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin;
|
||||
/**
|
||||
* Can store plugin instances in WordPress filter system.
|
||||
*
|
||||
* @package WPDesk\PluginBuilder\Storage
|
||||
*/
|
||||
class WordpressFilterStorage implements \FcfVendor\WPDesk\PluginBuilder\Storage\PluginStorage
|
||||
{
|
||||
const STORAGE_FILTER_NAME = 'wpdesk_plugin_instances';
|
||||
/**
|
||||
* @param string $class
|
||||
* @param AbstractPlugin $object
|
||||
*/
|
||||
public function add_to_storage($class, $object)
|
||||
{
|
||||
\add_filter(self::STORAGE_FILTER_NAME, static function ($plugins) use($class, $object) {
|
||||
if (isset($plugins[$class])) {
|
||||
throw new \FcfVendor\WPDesk\PluginBuilder\Storage\Exception\ClassAlreadyExists("Class {$class} already exists");
|
||||
}
|
||||
$plugins[$class] = $object;
|
||||
return $plugins;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param string $class
|
||||
*
|
||||
* @return AbstractPlugin
|
||||
*/
|
||||
public function get_from_storage($class)
|
||||
{
|
||||
$plugins = \apply_filters(self::STORAGE_FILTER_NAME, []);
|
||||
if (isset($plugins[$class])) {
|
||||
return $plugins[$class];
|
||||
}
|
||||
throw new \FcfVendor\WPDesk\PluginBuilder\Storage\Exception\ClassNotExists("Class {$class} not exists in storage");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user