worldline-for-woocommerce
Last commit date
assets
1 week ago
inc
1 week ago
languages
1 week ago
modules
1 week ago
shared
1 week ago
src
1 week ago
vendor
1 week ago
.eslintrc.js
1 week ago
BUILD_INFO.txt
1 week ago
LICENSE
1 week ago
SECURITY.md
1 week ago
class-plugin.php
1 week ago
composer.json
1 week ago
config.php
1 week ago
package.json
1 week ago
readme.txt
1 week ago
tsconfig.json
1 week ago
uninstall.php
1 week ago
webpack.config.js
1 week ago
worldline-for-woocommerce.php
1 week ago
class-plugin.php
62 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Syde\Vendor\Worldline\Inpsyde\WorldlineForWoocommerce; |
| 4 | |
| 5 | use wpdb; |
| 6 | class Plugin |
| 7 | { |
| 8 | protected static ?Plugin $instance = null; |
| 9 | private wpdb $db; |
| 10 | private string $plugin_file; |
| 11 | public static function instance(wpdb $wpdb, string $plugin_file) : Plugin |
| 12 | { |
| 13 | if (self::$instance === null) { |
| 14 | self::$instance = new self($wpdb, $plugin_file); |
| 15 | } |
| 16 | self::$instance->runUpgradeCheck(); |
| 17 | return self::$instance; |
| 18 | } |
| 19 | public function __construct(wpdb $wpdb, string $plugin_file) |
| 20 | { |
| 21 | $this->db = $wpdb; |
| 22 | $this->plugin_file = $plugin_file; |
| 23 | } |
| 24 | /** |
| 25 | * Checks if plugin version changed and runs upgrade scripts. |
| 26 | */ |
| 27 | private function runUpgradeCheck() : void |
| 28 | { |
| 29 | $stored_version = \get_option('worldline_plugin_version', '0.0.0'); |
| 30 | $current_version = $this->get_plugin_version(); |
| 31 | if (\version_compare($stored_version, $current_version, '<')) { |
| 32 | $this->upgrade($stored_version, $current_version); |
| 33 | \update_option('worldline_plugin_version', $current_version); |
| 34 | } |
| 35 | } |
| 36 | /** |
| 37 | * Runs upgrade scripts for all intermediate versions. |
| 38 | */ |
| 39 | private function upgrade(string $previous_version, string $current_version) : void |
| 40 | { |
| 41 | $upgrade_dir = \dirname($this->plugin_file) . '/src/upgrade/'; |
| 42 | if (!\is_dir($upgrade_dir)) { |
| 43 | return; |
| 44 | } |
| 45 | $files = \glob($upgrade_dir . 'upgrade-*.php'); |
| 46 | \sort($files, \SORT_NATURAL); |
| 47 | foreach ($files as $file) { |
| 48 | $file_version = \str_replace(['upgrade-', '.php'], '', \basename($file)); |
| 49 | if (\version_compare($previous_version, $file_version, '<') && \version_compare($file_version, $current_version, '<=')) { |
| 50 | include_once $file; |
| 51 | \error_log("[WORLDLINE] � |
| 52 | Ran upgrade for version {$file_version}"); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | private function get_plugin_version() : string |
| 57 | { |
| 58 | $data = \get_file_data($this->plugin_file, ['Version' => 'Version']); |
| 59 | return $data['Version'] ?? '0.0.0'; |
| 60 | } |
| 61 | } |
| 62 |