first commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\Plugin\Flow\Initialization;
|
||||
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\Activateable;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\Deactivateable;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\SlimPlugin;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Storage\StorageFactory;
|
||||
/**
|
||||
* Helps with plugin building concepts.
|
||||
*
|
||||
* @package WPDesk\Plugin\Flow\Initialization
|
||||
*/
|
||||
trait BuilderTrait
|
||||
{
|
||||
/**
|
||||
* Build plugin from info.
|
||||
*
|
||||
* @param \WPDesk_Plugin_Info $plugin_info
|
||||
*
|
||||
* @return SlimPlugin
|
||||
*/
|
||||
private function build_plugin(\FcfVendor\WPDesk_Plugin_Info $plugin_info)
|
||||
{
|
||||
$class_name = \apply_filters('wp_builder_plugin_class', $plugin_info->get_class_name());
|
||||
/** @var SlimPlugin $plugin */
|
||||
$plugin = new $class_name($plugin_info);
|
||||
return $plugin;
|
||||
}
|
||||
/**
|
||||
* Initialize WP register hooks that have to be fire before any other.
|
||||
*
|
||||
* @param \WPDesk_Plugin_Info $plugin_info
|
||||
* @param SlimPlugin $plugin
|
||||
*
|
||||
* @return SlimPlugin
|
||||
*/
|
||||
private function init_register_hooks(\FcfVendor\WPDesk_Plugin_Info $plugin_info, \FcfVendor\WPDesk\PluginBuilder\Plugin\SlimPlugin $plugin)
|
||||
{
|
||||
if ($plugin instanceof \FcfVendor\WPDesk\PluginBuilder\Plugin\Activateable) {
|
||||
\register_activation_hook($plugin_info->get_plugin_file_name(), [$plugin, 'activate']);
|
||||
}
|
||||
if ($plugin instanceof \FcfVendor\WPDesk\PluginBuilder\Plugin\Deactivateable) {
|
||||
\register_deactivation_hook($plugin_info->get_plugin_file_name(), [$plugin, 'deactivate']);
|
||||
}
|
||||
return $plugin;
|
||||
}
|
||||
/**
|
||||
* Store plugin for others to use.
|
||||
*
|
||||
* @param SlimPlugin $plugin
|
||||
*/
|
||||
private function store_plugin(\FcfVendor\WPDesk\PluginBuilder\Plugin\SlimPlugin $plugin)
|
||||
{
|
||||
$storageFactory = new \FcfVendor\WPDesk\PluginBuilder\Storage\StorageFactory();
|
||||
$storageFactory->create_storage()->add_to_storage(\get_class($plugin), $plugin);
|
||||
}
|
||||
/**
|
||||
* Init integration layer of the plugin.
|
||||
*
|
||||
* @param SlimPlugin $plugin
|
||||
*/
|
||||
private function init_plugin(\FcfVendor\WPDesk\PluginBuilder\Plugin\SlimPlugin $plugin)
|
||||
{
|
||||
\do_action('wp_builder_before_plugin_init', $plugin);
|
||||
$plugin->init();
|
||||
\do_action('wp_builder_before_init', $plugin);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\Plugin\Flow\Initialization\Simple;
|
||||
|
||||
use FcfVendor\WPDesk\Helper\PrefixedHelperAsLibrary;
|
||||
/**
|
||||
* Trait helps with helper initialization
|
||||
*
|
||||
* @package WPDesk\Plugin\Flow\Initialization\Simple
|
||||
*/
|
||||
trait HelperInstanceAsFilterTrait
|
||||
{
|
||||
/** @var \WPDesk\Helper\PrefixedHelperAsLibrary */
|
||||
private static $helper_instance;
|
||||
/**
|
||||
* Returns version of the helper. Inc when helper is changed and should be instantiated fist.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function get_helper_version()
|
||||
{
|
||||
return 5;
|
||||
}
|
||||
/**
|
||||
* Returns filter action name for helper instance
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_helper_action_name()
|
||||
{
|
||||
return 'wpdesk_helper_instance';
|
||||
}
|
||||
/**
|
||||
* Instantiate helper and return it
|
||||
*
|
||||
* @return PrefixedHelperAsLibrary
|
||||
*/
|
||||
private function get_helper_instance()
|
||||
{
|
||||
return \apply_filters($this->get_helper_action_name(), null);
|
||||
}
|
||||
/**
|
||||
* Prepare helper to be instantiated using wpdesk_helper_instance filter
|
||||
*
|
||||
* @return void|PrefixedHelperAsLibrary
|
||||
*/
|
||||
private function prepare_helper_action()
|
||||
{
|
||||
\class_exists(\WPDesk\Helper\HelperAsLibrary::class);
|
||||
// autoload this class
|
||||
\add_filter($this->get_helper_action_name(), function ($helper_instance) {
|
||||
if (\is_object($helper_instance)) {
|
||||
return $helper_instance;
|
||||
}
|
||||
if (\is_object(self::$helper_instance)) {
|
||||
return self::$helper_instance;
|
||||
}
|
||||
if (\apply_filters('wpdesk_can_start_helper', \true, $this->plugin_info)) {
|
||||
self::$helper_instance = new \FcfVendor\WPDesk\Helper\PrefixedHelperAsLibrary();
|
||||
self::$helper_instance->hooks();
|
||||
\do_action('wpdesk_helper_started', self::$helper_instance, $this->plugin_info);
|
||||
return self::$helper_instance;
|
||||
}
|
||||
}, 10 - $this->get_helper_version());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\Plugin\Flow\Initialization;
|
||||
|
||||
/**
|
||||
* Interface for factory of plugin initialization strategy
|
||||
*/
|
||||
interface InitializationFactory
|
||||
{
|
||||
/**
|
||||
* @param \WPDesk_Plugin_Info $info
|
||||
*
|
||||
* @return InitializationStrategy
|
||||
*/
|
||||
public function create_initialization_strategy(\FcfVendor\WPDesk_Plugin_Info $info);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\Plugin\Flow\Initialization;
|
||||
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\SlimPlugin;
|
||||
/**
|
||||
* Interface for initialization strategy for plugin. How to initialize it?
|
||||
*/
|
||||
interface InitializationStrategy
|
||||
{
|
||||
/**
|
||||
* Run tasks that prepares plugin to work. Have to run before plugin loaded.
|
||||
*
|
||||
* @param \WPDesk_Plugin_Info $plugin_info
|
||||
*
|
||||
* @return SlimPlugin
|
||||
*/
|
||||
public function run_before_init(\FcfVendor\WPDesk_Plugin_Info $plugin_info);
|
||||
/**
|
||||
* Run task that integrates plugin with other dependencies. Can be run in plugins_loaded.
|
||||
*
|
||||
* @param \WPDesk_Plugin_Info $plugin_info
|
||||
*
|
||||
* @return SlimPlugin
|
||||
*/
|
||||
public function run_init(\FcfVendor\WPDesk_Plugin_Info $plugin_info);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\Plugin\Flow\Initialization;
|
||||
|
||||
/**
|
||||
* Can disable shared plugin before it's loaded using plugin filename
|
||||
*/
|
||||
class PluginDisablerByFileTrait
|
||||
{
|
||||
/** @var string */
|
||||
private $plugin_file;
|
||||
/**
|
||||
* @param string $plugin_file
|
||||
*/
|
||||
public function __construct($plugin_file)
|
||||
{
|
||||
$this->plugin_file = $plugin_file;
|
||||
}
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function disable()
|
||||
{
|
||||
/**
|
||||
* @param WPDesk_Loader[] $loaders
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
$false_for_helper = function ($loaders) {
|
||||
return \array_filter($loaders, function ($loader) {
|
||||
try {
|
||||
// BIG HACK TO GET PRIVATE PROPERTY
|
||||
$reflection = new \ReflectionClass($loader);
|
||||
$property = $reflection->getProperty('loader_info');
|
||||
$property->setAccessible(\true);
|
||||
/** @var WPDesk_Composer_Loader_Info $inner_info */
|
||||
$inner_info = $property->getValue($loader);
|
||||
$plugin_info = $inner_info->get_plugin_info();
|
||||
return \basename($plugin_info->get_plugin_file_name()) !== \basename($this->plugin_file);
|
||||
} catch (\Exception $e) {
|
||||
return \true;
|
||||
}
|
||||
});
|
||||
};
|
||||
\add_filter('wp_autoloader_loader_loaders_to_load', $false_for_helper);
|
||||
\add_filter('wp_autoloader_loader_loaders_to_create', $false_for_helper);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\Plugin\Flow\Initialization\Simple;
|
||||
|
||||
use FcfVendor\WPDesk\Plugin\Flow\Initialization\InitializationFactory;
|
||||
use FcfVendor\WPDesk\Plugin\Flow\Initialization\InitializationStrategy;
|
||||
/**
|
||||
* Can decide if strategy is for free plugin or paid plugin
|
||||
*/
|
||||
class SimpleFactory implements \FcfVendor\WPDesk\Plugin\Flow\Initialization\InitializationFactory
|
||||
{
|
||||
/** @var bool */
|
||||
private $free;
|
||||
/**
|
||||
* @param bool $free True for free/repository plugin
|
||||
*/
|
||||
public function __construct($free = \false)
|
||||
{
|
||||
$this->free = $free;
|
||||
}
|
||||
/**
|
||||
* Create strategy according to the given flag
|
||||
*
|
||||
* @param \WPDesk_Plugin_Info $info
|
||||
*
|
||||
* @return InitializationStrategy
|
||||
*/
|
||||
public function create_initialization_strategy(\FcfVendor\WPDesk_Plugin_Info $info)
|
||||
{
|
||||
if ($this->free) {
|
||||
return new \FcfVendor\WPDesk\Plugin\Flow\Initialization\Simple\SimpleFreeStrategy($info);
|
||||
}
|
||||
return new \FcfVendor\WPDesk\Plugin\Flow\Initialization\Simple\SimplePaidStrategy($info);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\Plugin\Flow\Initialization\Simple;
|
||||
|
||||
use FcfVendor\WPDesk\Plugin\Flow\Initialization\ActivationTrait;
|
||||
use FcfVendor\WPDesk\Plugin\Flow\Initialization\BuilderTrait;
|
||||
use FcfVendor\WPDesk\Plugin\Flow\Initialization\InitializationStrategy;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\SlimPlugin;
|
||||
/**
|
||||
* Initialize free plugin
|
||||
* - just build it already
|
||||
*/
|
||||
class SimpleFreeStrategy implements \FcfVendor\WPDesk\Plugin\Flow\Initialization\InitializationStrategy
|
||||
{
|
||||
use HelperInstanceAsFilterTrait;
|
||||
use TrackerInstanceAsFilterTrait;
|
||||
use BuilderTrait;
|
||||
/** @var \WPDesk_Plugin_Info */
|
||||
private $plugin_info;
|
||||
/** @var SlimPlugin */
|
||||
private $plugin;
|
||||
public function __construct(\FcfVendor\WPDesk_Plugin_Info $plugin_info)
|
||||
{
|
||||
$this->plugin_info = $plugin_info;
|
||||
}
|
||||
/**
|
||||
* Run tasks that prepares plugin to work. Have to run before plugin loaded.
|
||||
*
|
||||
* @param \WPDesk_Plugin_Info $plugin_info
|
||||
*
|
||||
* @return SlimPlugin
|
||||
*/
|
||||
public function run_before_init(\FcfVendor\WPDesk_Plugin_Info $plugin_info)
|
||||
{
|
||||
$this->plugin = $this->build_plugin($plugin_info);
|
||||
$this->init_register_hooks($plugin_info, $this->plugin);
|
||||
}
|
||||
/**
|
||||
* Run task that integrates plugin with other dependencies. Can be run in plugins_loaded.
|
||||
*
|
||||
* @param \WPDesk_Plugin_Info $plugin_info
|
||||
*
|
||||
* @return SlimPlugin
|
||||
*/
|
||||
public function run_init(\FcfVendor\WPDesk_Plugin_Info $plugin_info)
|
||||
{
|
||||
if (!$this->plugin) {
|
||||
$this->plugin = $this->build_plugin($plugin_info);
|
||||
}
|
||||
$this->prepare_helper_action();
|
||||
$this->prepare_tracker_action();
|
||||
$this->store_plugin($this->plugin);
|
||||
$this->init_plugin($this->plugin);
|
||||
return $this->plugin;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\Plugin\Flow\Initialization\Simple;
|
||||
|
||||
use FcfVendor\WPDesk\Helper\HelperRemover;
|
||||
use FcfVendor\WPDesk\Helper\PrefixedHelperAsLibrary;
|
||||
use FcfVendor\WPDesk\License\PluginRegistrator;
|
||||
use FcfVendor\WPDesk\Plugin\Flow\Initialization\ActivationTrait;
|
||||
use FcfVendor\WPDesk\Plugin\Flow\Initialization\BuilderTrait;
|
||||
use FcfVendor\WPDesk\Plugin\Flow\Initialization\PluginDisablerByFileTrait;
|
||||
use FcfVendor\WPDesk\Plugin\Flow\Initialization\InitializationStrategy;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\ActivationAware;
|
||||
use FcfVendor\WPDesk\PluginBuilder\Plugin\SlimPlugin;
|
||||
/**
|
||||
* Initialize standard paid plugin
|
||||
* - register to helper
|
||||
* - initialize helper
|
||||
* - build with info about plugin active flag
|
||||
*/
|
||||
class SimplePaidStrategy implements \FcfVendor\WPDesk\Plugin\Flow\Initialization\InitializationStrategy
|
||||
{
|
||||
use HelperInstanceAsFilterTrait;
|
||||
use TrackerInstanceAsFilterTrait;
|
||||
use BuilderTrait;
|
||||
/** @var \WPDesk_Plugin_Info */
|
||||
private $plugin_info;
|
||||
/** @var SlimPlugin */
|
||||
private $plugin;
|
||||
public function __construct(\FcfVendor\WPDesk_Plugin_Info $plugin_info)
|
||||
{
|
||||
$this->plugin_info = $plugin_info;
|
||||
}
|
||||
/**
|
||||
* Run tasks that prepares plugin to work. Have to run before plugin loaded.
|
||||
*
|
||||
* @param \WPDesk_Plugin_Info $plugin_info
|
||||
*
|
||||
* @return SlimPlugin
|
||||
*/
|
||||
public function run_before_init(\FcfVendor\WPDesk_Plugin_Info $plugin_info)
|
||||
{
|
||||
$this->plugin = $this->build_plugin($plugin_info);
|
||||
$this->init_register_hooks($plugin_info, $this->plugin);
|
||||
}
|
||||
/**
|
||||
* Run task that integrates plugin with other dependencies. Can be run in plugins_loaded.
|
||||
*
|
||||
* @param \WPDesk_Plugin_Info $plugin_info
|
||||
*
|
||||
* @return SlimPlugin
|
||||
*/
|
||||
public function run_init(\FcfVendor\WPDesk_Plugin_Info $plugin_info)
|
||||
{
|
||||
if (!$this->plugin) {
|
||||
$this->plugin = $this->build_plugin($plugin_info);
|
||||
}
|
||||
$this->prepare_tracker_action();
|
||||
$registrator = $this->register_plugin();
|
||||
\add_action('plugins_loaded', function () use($registrator) {
|
||||
$this->init_helper();
|
||||
$is_plugin_subscription_active = $registrator instanceof \FcfVendor\WPDesk\License\PluginRegistrator && $registrator->is_active();
|
||||
if ($this->plugin instanceof \FcfVendor\WPDesk\PluginBuilder\Plugin\ActivationAware && $is_plugin_subscription_active) {
|
||||
$this->plugin->set_active();
|
||||
}
|
||||
$this->store_plugin($this->plugin);
|
||||
$this->init_plugin($this->plugin);
|
||||
}, $priority_before_flow_2_5_after_2_6 = -45);
|
||||
return $this->plugin;
|
||||
}
|
||||
/**
|
||||
* Register plugin for subscriptions and updates
|
||||
*
|
||||
* @return PluginRegistrator
|
||||
*
|
||||
* @see init_helper note
|
||||
*
|
||||
*/
|
||||
private function register_plugin()
|
||||
{
|
||||
if (\apply_filters('wpdesk_can_register_plugin', \true, $this->plugin_info)) {
|
||||
$registrator = new \FcfVendor\WPDesk\License\PluginRegistrator($this->plugin_info);
|
||||
$registrator->add_plugin_to_installed_plugins();
|
||||
return $registrator;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Helper is a component that gives:
|
||||
* - activation interface
|
||||
* - automatic updates
|
||||
* - logs
|
||||
* - some other feats
|
||||
*
|
||||
* NOTE:
|
||||
*
|
||||
* It's possible for this method to not found classes embedded here.
|
||||
* OTHER plugin in unlikely scenario that THIS plugin is disabled
|
||||
* can use this class and do not have this library dependencies as
|
||||
* these are loaded using composer.
|
||||
*
|
||||
* @return PrefixedHelperAsLibrary|null
|
||||
*/
|
||||
private function init_helper()
|
||||
{
|
||||
$this->prevent_older_helpers();
|
||||
$this->prepare_helper_action();
|
||||
return $this->get_helper_instance();
|
||||
}
|
||||
/**
|
||||
* Try to disable all other types of helpers
|
||||
*/
|
||||
private function prevent_older_helpers()
|
||||
{
|
||||
if (\apply_filters('wpdesk_can_hack_shared_helper', \true, $this->plugin_info)) {
|
||||
// hack to ensure that the class is loaded so other helpers are disabled
|
||||
\class_exists(\WPDesk\Helper\HelperAsLibrary::class, \true);
|
||||
}
|
||||
if (\apply_filters('wpdesk_can_supress_original_helper', \true, $this->plugin_info)) {
|
||||
$this->try_suppress_original_helper_load();
|
||||
// start supression only once. Prevent doing it again
|
||||
\add_filter('wpdesk_can_supress_original_helper', function () {
|
||||
return \false;
|
||||
});
|
||||
}
|
||||
if (\apply_filters('wpdesk_can_remove_old_helper_hooks', \true, $this->plugin_info)) {
|
||||
(new \FcfVendor\WPDesk\Helper\HelperRemover())->hooks();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Tries to prevent original Helper from loading
|
||||
*/
|
||||
private function try_suppress_original_helper_load()
|
||||
{
|
||||
(new \FcfVendor\WPDesk\Plugin\Flow\Initialization\PluginDisablerByFileTrait('wpdesk-helper/wpdesk-helper.php'))->disable();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\Plugin\Flow\Initialization\Simple;
|
||||
|
||||
/**
|
||||
* Trait helps with tracker initialization
|
||||
*
|
||||
* @package WPDesk\Plugin\Flow\Initialization\Simple\
|
||||
*/
|
||||
trait TrackerInstanceAsFilterTrait
|
||||
{
|
||||
/** @var \WPDesk_Tracker_Interface */
|
||||
private static $tracker_instance;
|
||||
/**
|
||||
* Returns filter action name for tracker instance
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_tracker_action_name()
|
||||
{
|
||||
return 'wpdesk_tracker_instance';
|
||||
}
|
||||
/**
|
||||
* Returns version of the tracker. Inc when trackker is changed and should be instantiated fist.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function get_tracker_version()
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
/**
|
||||
* @return \WPDesk_Tracker_Interface
|
||||
*/
|
||||
private function get_tracker_instance()
|
||||
{
|
||||
return \apply_filters($this->get_tracker_action_name(), null);
|
||||
}
|
||||
/**
|
||||
* Prepare tracker to be instantiated using wpdesk_tracker_instance filter
|
||||
*
|
||||
* @return void|\WPDesk_Tracker
|
||||
*/
|
||||
private function prepare_tracker_action()
|
||||
{
|
||||
\class_exists(\WPDesk_Tracker_Factory::class);
|
||||
//autoload this class
|
||||
\add_filter($this->get_tracker_action_name(), function ($tracker_instance) {
|
||||
if (\is_object($tracker_instance)) {
|
||||
return $tracker_instance;
|
||||
}
|
||||
if (\is_object(self::$tracker_instance)) {
|
||||
return self::$tracker_instance;
|
||||
}
|
||||
if (\apply_filters('wpdesk_can_start_tracker', \true, $this->plugin_info)) {
|
||||
$tracker_factory = new \FcfVendor\WPDesk_Tracker_Factory_Prefixed();
|
||||
self::$tracker_instance = $tracker_factory->create_tracker(\basename($this->plugin_info->get_plugin_file_name()));
|
||||
\do_action('wpdesk_tracker_started', self::$tracker_instance, $this->plugin_info);
|
||||
return self::$tracker_instance;
|
||||
}
|
||||
}, 10 - $this->get_tracker_version());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\Plugin\Flow;
|
||||
|
||||
use FcfVendor\WPDesk\Plugin\Flow\Initialization\InitializationFactory;
|
||||
/**
|
||||
* Bootstrap plugin loading
|
||||
* - check requirements
|
||||
* - prepare plugin info
|
||||
* - delegate plugin building to the initializator
|
||||
*/
|
||||
final class PluginBootstrap
|
||||
{
|
||||
const LIBRARY_TEXT_DOMAIN = 'flexible-checkout-fields';
|
||||
const PRIORITY_BEFORE_FLOW_2_5 = -50;
|
||||
/** @var string */
|
||||
private $plugin_version;
|
||||
/** @var string */
|
||||
private $plugin_name;
|
||||
/** @var string */
|
||||
private $plugin_class_name;
|
||||
/** @var string */
|
||||
private $plugin_text_domain;
|
||||
/** @var string */
|
||||
private $plugin_dir;
|
||||
/** @var string */
|
||||
private $plugin_file;
|
||||
/** @var array */
|
||||
private $requirements;
|
||||
/** @var string */
|
||||
private $product_id;
|
||||
/**
|
||||
* Factory to build strategy how initialize that plugin
|
||||
*
|
||||
* @var InitializationFactory
|
||||
*/
|
||||
private $initialization_factory;
|
||||
/**
|
||||
* WPDesk_Plugin_Bootstrap constructor.
|
||||
*
|
||||
* @param string $plugin_version
|
||||
* @param string $plugin_release_timestamp
|
||||
* @param string $plugin_name
|
||||
* @param string $plugin_class_name
|
||||
* @param string $plugin_text_domain
|
||||
* @param string $plugin_dir
|
||||
* @param string $plugin_file
|
||||
* @param array $requirements
|
||||
* @param string $product_id
|
||||
* @param InitializationFactory $build_factory
|
||||
*/
|
||||
public function __construct($plugin_version, $plugin_release_timestamp, $plugin_name, $plugin_class_name, $plugin_text_domain, $plugin_dir, $plugin_file, array $requirements, $product_id, \FcfVendor\WPDesk\Plugin\Flow\Initialization\InitializationFactory $build_factory)
|
||||
{
|
||||
$this->plugin_version = $plugin_version;
|
||||
$this->plugin_name = $plugin_name;
|
||||
$this->plugin_class_name = $plugin_class_name;
|
||||
$this->plugin_text_domain = $plugin_text_domain;
|
||||
$this->plugin_dir = $plugin_dir;
|
||||
$this->plugin_file = $plugin_file;
|
||||
$this->requirements = $requirements;
|
||||
$this->product_id = $product_id;
|
||||
$this->initialization_factory = $build_factory;
|
||||
}
|
||||
/**
|
||||
* Run the plugin bootstrap
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$plugin_info = $this->get_plugin_info();
|
||||
$this->init_translations($plugin_info);
|
||||
$strategy = $this->initialization_factory->create_initialization_strategy($plugin_info);
|
||||
$requirements_checker = $this->create_requirements_checker();
|
||||
if ($requirements_checker->are_requirements_met()) {
|
||||
$strategy->run_before_init($plugin_info);
|
||||
}
|
||||
$this->add_activation_hook_for_save_activation_date();
|
||||
\add_action('plugins_loaded', static function () use($strategy, $requirements_checker, $plugin_info) {
|
||||
if ($requirements_checker->are_requirements_met()) {
|
||||
$strategy->run_init($plugin_info);
|
||||
} else {
|
||||
$requirements_checker->render_notices();
|
||||
}
|
||||
}, self::PRIORITY_BEFORE_FLOW_2_5);
|
||||
}
|
||||
/**
|
||||
* Initialize activated_plugin action.
|
||||
* Action stores plugin activation date.
|
||||
* Example option name: plugin_activation_flexible-shipping/flexible-shipping.php
|
||||
*/
|
||||
private function add_activation_hook_for_save_activation_date()
|
||||
{
|
||||
\add_action('activated_plugin', static function ($plugin_file, $network_wide = \false) {
|
||||
if (!$network_wide) {
|
||||
$option_name = 'plugin_activation_' . $plugin_file;
|
||||
$activation_date = \get_option($option_name, '');
|
||||
if ('' === $activation_date) {
|
||||
$activation_date = \current_time('mysql');
|
||||
\update_option($option_name, $activation_date);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Adds text domain used in a library
|
||||
*/
|
||||
private function init_translations(\FcfVendor\WPDesk_Plugin_Info $plugin_info)
|
||||
{
|
||||
$lang_dir = 'lang';
|
||||
if (\method_exists($plugin_info, 'get_language_dir')) {
|
||||
$lang_dir = $plugin_info->get_language_dir();
|
||||
}
|
||||
\load_plugin_textdomain($plugin_info->get_text_domain(), \false, \basename($plugin_info->get_plugin_dir()) . "/{$lang_dir}/");
|
||||
}
|
||||
/**
|
||||
* Factory method creates requirement checker to run the checks
|
||||
*
|
||||
* @return \WPDesk_Requirement_Checker
|
||||
*/
|
||||
private function create_requirements_checker()
|
||||
{
|
||||
/** @var \WPDesk_Requirement_Checker_Factory $requirements_checker_factory */
|
||||
$requirements_checker_factory = new \FcfVendor\WPDesk_Basic_Requirement_Checker_Factory();
|
||||
return $requirements_checker_factory->create_from_requirement_array(__FILE__, $this->plugin_name, $this->requirements, $this->plugin_text_domain);
|
||||
}
|
||||
/**
|
||||
* Factory method creates \WPDesk_Plugin_Info to bootstrap info about plugin in one place
|
||||
*
|
||||
* TODO: move to WPDesk_Plugin_Info factory
|
||||
*
|
||||
* @return \WPDesk_Plugin_Info
|
||||
*/
|
||||
private function get_plugin_info()
|
||||
{
|
||||
$plugin_info = new \FcfVendor\WPDesk_Plugin_Info();
|
||||
$plugin_info->set_plugin_file_name(\plugin_basename($this->plugin_file));
|
||||
$plugin_info->set_plugin_name($this->plugin_name);
|
||||
$plugin_info->set_plugin_dir($this->plugin_dir);
|
||||
$plugin_info->set_class_name($this->plugin_class_name);
|
||||
$plugin_info->set_version($this->plugin_version);
|
||||
$plugin_info->set_product_id($this->product_id);
|
||||
$plugin_info->set_text_domain($this->plugin_text_domain);
|
||||
$plugin_info->set_plugin_url(\plugins_url(\dirname(\plugin_basename($this->plugin_file))));
|
||||
return $plugin_info;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor;
|
||||
|
||||
/**
|
||||
* @var string $plugin_version
|
||||
* @var string $plugin_name
|
||||
* @var string $plugin_class_name
|
||||
* @var string $plugin_text_domain
|
||||
* @var string $plugin_dir
|
||||
* @var string $plugin_file
|
||||
* @var array $requirements
|
||||
* @var string $product_id
|
||||
*/
|
||||
if (!\defined('ABSPATH')) {
|
||||
die;
|
||||
}
|
||||
// Code in PHP >= 5.3 but understandable by older parsers
|
||||
if (\PHP_VERSION_ID > 50300) {
|
||||
require_once $plugin_dir . '/vendor/autoload.php';
|
||||
$plugin_init_factory = new \FcfVendor\WPDesk\Plugin\Flow\Initialization\Simple\SimpleFactory(\true);
|
||||
}
|
||||
require \dirname(__FILE__) . '/plugin-init-php52.php';
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor;
|
||||
|
||||
/**
|
||||
* @var string $plugin_version
|
||||
* @var string $plugin_name
|
||||
* @var string $plugin_class_name
|
||||
* @var string $plugin_text_domain
|
||||
* @var string $plugin_dir
|
||||
* @var string $plugin_file
|
||||
* @var array $requirements
|
||||
* @var string $product_id
|
||||
* @var WPDesk\Plugin\Flow\Initialization\InitializationFactory|void $plugin_init_factory
|
||||
*/
|
||||
if (!\defined('ABSPATH')) {
|
||||
die;
|
||||
}
|
||||
// Code in PHP >= 5.3 but understandable by older parsers
|
||||
if (\PHP_VERSION_ID > 50300) {
|
||||
require_once $plugin_dir . '/vendor/autoload.php';
|
||||
if (!isset($plugin_init_factory)) {
|
||||
$plugin_init_factory = new \FcfVendor\WPDesk\Plugin\Flow\Initialization\Simple\SimpleFactory();
|
||||
}
|
||||
$bootstrap = new \FcfVendor\WPDesk\Plugin\Flow\PluginBootstrap(
|
||||
$plugin_version,
|
||||
null,
|
||||
// deprecated
|
||||
$plugin_name,
|
||||
$plugin_class_name,
|
||||
$plugin_text_domain,
|
||||
$plugin_dir,
|
||||
$plugin_file,
|
||||
$requirements,
|
||||
$product_id,
|
||||
$plugin_init_factory
|
||||
);
|
||||
$bootstrap->run();
|
||||
// all optional vars must be cleared
|
||||
unset($plugin_init_factory);
|
||||
} else {
|
||||
/** @noinspection PhpDeprecationInspection */
|
||||
$php52_function = \create_function('', 'echo sprintf( __("<p><strong style=\'color: red;\'>PHP version is older than 5.3 so no WP Desk plugins will work. Please contact your host and ask them to upgrade. </strong></p>", \'wp-plugin-flow\') );');
|
||||
\add_action('admin_notices', $php52_function);
|
||||
}
|
||||
Reference in New Issue
Block a user