Initialization
2 weeks ago
PluginBootstrap.php
2 weeks ago
plugin-init-php52-free-no-tracker.php
2 weeks ago
plugin-init-php52-free.php
2 weeks ago
plugin-init-php52.php
2 weeks ago
PluginBootstrap.php
164 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Plugin\Flow; |
| 4 | |
| 5 | use FSVendor\WPDesk\Plugin\Flow\Initialization\InitializationFactory; |
| 6 | /** |
| 7 | * Bootstrap plugin loading |
| 8 | * - check requirements |
| 9 | * - prepare plugin info |
| 10 | * - delegate plugin building to the initializator |
| 11 | */ |
| 12 | final class PluginBootstrap |
| 13 | { |
| 14 | const LIBRARY_TEXT_DOMAIN = 'flexible-shipping'; |
| 15 | const PRIORITY_BEFORE_FLOW_2_5 = -50; |
| 16 | /** @var string */ |
| 17 | private $plugin_version; |
| 18 | /** @var string */ |
| 19 | private $plugin_name; |
| 20 | /** @var string */ |
| 21 | private $plugin_class_name; |
| 22 | /** @var string */ |
| 23 | private $plugin_text_domain; |
| 24 | /** @var string */ |
| 25 | private $plugin_dir; |
| 26 | /** @var string */ |
| 27 | private $plugin_file; |
| 28 | /** @var array */ |
| 29 | private $requirements; |
| 30 | /** @var string */ |
| 31 | private $product_id; |
| 32 | /** @var array */ |
| 33 | private $plugin_shops; |
| 34 | /** |
| 35 | * Factory to build strategy how initialize that plugin |
| 36 | * |
| 37 | * @var InitializationFactory |
| 38 | */ |
| 39 | private $initialization_factory; |
| 40 | /** |
| 41 | * WPDesk_Plugin_Bootstrap constructor. |
| 42 | * |
| 43 | * @param string $plugin_version |
| 44 | * @param string $plugin_release_timestamp |
| 45 | * @param string $plugin_name |
| 46 | * @param string $plugin_class_name |
| 47 | * @param string $plugin_text_domain |
| 48 | * @param string $plugin_dir |
| 49 | * @param string $plugin_file |
| 50 | * @param array $requirements |
| 51 | * @param string $product_id |
| 52 | * @param InitializationFactory $build_factory |
| 53 | * @param array $plugin_shops |
| 54 | */ |
| 55 | public function __construct($plugin_version, $plugin_release_timestamp, $plugin_name, $plugin_class_name, $plugin_text_domain, $plugin_dir, $plugin_file, array $requirements, $product_id, InitializationFactory $build_factory, $plugin_shops) |
| 56 | { |
| 57 | $this->plugin_version = $plugin_version; |
| 58 | $this->plugin_name = $plugin_name; |
| 59 | $this->plugin_class_name = $plugin_class_name; |
| 60 | $this->plugin_text_domain = $plugin_text_domain; |
| 61 | $this->plugin_dir = $plugin_dir; |
| 62 | $this->plugin_file = $plugin_file; |
| 63 | $this->requirements = $requirements; |
| 64 | $this->product_id = $product_id; |
| 65 | $this->initialization_factory = $build_factory; |
| 66 | $this->plugin_shops = $plugin_shops; |
| 67 | } |
| 68 | /** |
| 69 | * Run the plugin bootstrap |
| 70 | */ |
| 71 | public function run() |
| 72 | { |
| 73 | $plugin_info = $this->get_plugin_info(); |
| 74 | $this->init_translations($plugin_info); |
| 75 | $strategy = $this->initialization_factory->create_initialization_strategy($plugin_info); |
| 76 | $requirements_checker = $this->create_requirements_checker(); |
| 77 | if ($requirements_checker->are_requirements_met()) { |
| 78 | $strategy->run_before_init($plugin_info); |
| 79 | } |
| 80 | $this->add_activation_hook_for_save_activation_date(); |
| 81 | add_action('plugins_loaded', static function () use ($strategy, $requirements_checker, $plugin_info) { |
| 82 | if ($requirements_checker->are_requirements_met()) { |
| 83 | $strategy->run_init($plugin_info); |
| 84 | } else { |
| 85 | $requirements_checker->render_notices(); |
| 86 | } |
| 87 | }, self::PRIORITY_BEFORE_FLOW_2_5); |
| 88 | add_action('before_woocommerce_init', static function () use ($plugin_info) { |
| 89 | $features_util_class = '\\' . 'Automattic' . '\\' . 'WooCommerce' . '\\' . 'Utilities' . '\\' . 'FeaturesUtil'; |
| 90 | if (class_exists($features_util_class)) { |
| 91 | $features_util_class::declare_compatibility('custom_order_tables', $plugin_info->get_plugin_file_name(), \true); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | /** |
| 96 | * Initialize activated_plugin action. |
| 97 | * Action stores plugin activation date. |
| 98 | * Example option name: plugin_activation_flexible-shipping/flexible-shipping.php |
| 99 | */ |
| 100 | private function add_activation_hook_for_save_activation_date() |
| 101 | { |
| 102 | add_action('activated_plugin', static function ($plugin_file, $network_wide = \false) { |
| 103 | if (!$network_wide) { |
| 104 | $option_name = 'activation_plugin_' . $plugin_file; |
| 105 | $activation_date = get_option($option_name, ''); |
| 106 | if ('' === $activation_date) { |
| 107 | $activation_date = current_time('mysql'); |
| 108 | update_option($option_name, $activation_date); |
| 109 | } |
| 110 | } |
| 111 | }); |
| 112 | } |
| 113 | /** |
| 114 | * Adds text domain used in a library |
| 115 | */ |
| 116 | private function init_translations(\FSVendor\WPDesk_Plugin_Info $plugin_info) |
| 117 | { |
| 118 | $lang_dir = 'lang'; |
| 119 | if (method_exists($plugin_info, 'get_language_dir')) { |
| 120 | $lang_dir = $plugin_info->get_language_dir(); |
| 121 | } |
| 122 | $text_domain = $plugin_info->get_text_domain(); |
| 123 | add_filter('doing_it_wrong_trigger_error', function ($doing_it_wrong, $function, $message, $version) use ($text_domain) { |
| 124 | if (wp_get_environment_type() === 'production' && $function === '_load_textdomain_just_in_time' && strpos($message, '<code>' . $text_domain . '</code>') !== \false) { |
| 125 | return \false; |
| 126 | } |
| 127 | return $doing_it_wrong; |
| 128 | }, 10, 4); |
| 129 | \load_plugin_textdomain($plugin_info->get_text_domain(), '', basename($plugin_info->get_plugin_dir()) . "/{$lang_dir}/"); |
| 130 | } |
| 131 | /** |
| 132 | * Factory method creates requirement checker to run the checks |
| 133 | * |
| 134 | * @return \WPDesk_Requirement_Checker |
| 135 | */ |
| 136 | private function create_requirements_checker() |
| 137 | { |
| 138 | /** @var \WPDesk_Requirement_Checker_Factory $requirements_checker_factory */ |
| 139 | $requirements_checker_factory = new \FSVendor\WPDesk_Basic_Requirement_Checker_Factory(); |
| 140 | return $requirements_checker_factory->create_from_requirement_array(__FILE__, $this->plugin_name, $this->requirements, $this->plugin_text_domain); |
| 141 | } |
| 142 | /** |
| 143 | * Factory method creates \WPDesk_Plugin_Info to bootstrap info about plugin in one place |
| 144 | * |
| 145 | * TODO: move to WPDesk_Plugin_Info factory |
| 146 | * |
| 147 | * @return \WPDesk_Plugin_Info |
| 148 | */ |
| 149 | private function get_plugin_info() |
| 150 | { |
| 151 | $plugin_info = new \FSVendor\WPDesk_Plugin_Info(); |
| 152 | $plugin_info->set_plugin_file_name(plugin_basename($this->plugin_file)); |
| 153 | $plugin_info->set_plugin_name($this->plugin_name); |
| 154 | $plugin_info->set_plugin_dir($this->plugin_dir); |
| 155 | $plugin_info->set_class_name($this->plugin_class_name); |
| 156 | $plugin_info->set_version($this->plugin_version); |
| 157 | $plugin_info->set_product_id($this->product_id); |
| 158 | $plugin_info->set_text_domain($this->plugin_text_domain); |
| 159 | $plugin_info->set_plugin_url(plugins_url('', $this->plugin_file)); |
| 160 | $plugin_info->set_plugin_shops($this->plugin_shops); |
| 161 | return $plugin_info; |
| 162 | } |
| 163 | } |
| 164 |