Skip to content
This repository was archived by the owner on Mar 17, 2022. It is now read-only.

Commit 01bc5b2

Browse files
committed
Fix #248 , Fix #266 upgrader_process_complete is not ideal
1 parent 3835837 commit 01bc5b2

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed

src/Hyyan/WPI/Plugin.php

+26-40
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
*
1818
* @author Hyyan Abo Fakher <[email protected]>
1919
*/
20-
class Plugin
21-
{
20+
class Plugin {
2221

2322
/** Required woocommerce version */
2423
const WOOCOMMERCE_VERSION = '3.0.0';
@@ -29,20 +28,17 @@ class Plugin
2928
/**
3029
* Construct the plugin.
3130
*/
32-
public function __construct()
33-
{
31+
public function __construct() {
3432
FlashMessages::register();
3533

3634
add_action('init', array($this, 'activate'));
3735
add_action('plugins_loaded', array($this, 'loadTextDomain'));
38-
add_action('upgrader_process_complete', array($this, 'onUpgrade'), 10, 2);
3936
}
4037

4138
/**
4239
* Load plugin langauge file.
4340
*/
44-
public function loadTextDomain()
45-
{
41+
public function loadTextDomain() {
4642
load_plugin_textdomain(
4743
'woo-poly-integration', false, plugin_basename(dirname(Hyyan_WPI_DIR)) . '/languages'
4844
);
@@ -56,9 +52,8 @@ public function loadTextDomain()
5652
*
5753
* @return bool false if plugin can not be activated
5854
*/
59-
public function activate()
60-
{
61-
55+
public function activate() {
56+
6257
if (!static::canActivate()) {
6358
FlashMessages::remove(MessagesInterface::MSG_SUPPORT);
6459
FlashMessages::add(
@@ -88,7 +83,14 @@ public function activate()
8883

8984
return $settingsLinks + $links;
9085
});
86+
9187
add_filter('plugin_row_meta', array(__CLASS__, 'plugin_row_meta'), 10, 2);
88+
89+
$oldVersion = get_option('wpi_version');
90+
if (version_compare(self::getVersion(), $oldVersion, '<>')) {
91+
$this->onUpgrade(self::getVersion(), $oldVersion);
92+
update_option('wpi_version', self::getVersion());
93+
}
9294

9395
$this->registerCore();
9496
}
@@ -98,8 +100,7 @@ public function activate()
98100
*
99101
* @return bool true if can be activated , false otherwise
100102
*/
101-
public static function canActivate()
102-
{
103+
public static function canActivate() {
103104
$polylang = false;
104105
$woocommerce = false;
105106

@@ -133,38 +134,25 @@ public static function canActivate()
133134
return ($polylang && Utilities::polylangVersionCheck(self::POLYLANG_VERSION)) &&
134135
($woocommerce && Utilities::woocommerceVersionCheck(self::WOOCOMMERCE_VERSION));
135136
}
136-
137+
137138
/**
138139
* On Upgrade
139-
*
140-
* Run only once the plugin has been updated to a new version
141-
*
142-
* @param \Plugin_Upgrader $upgrader
143-
* @param Array $options
140+
*
141+
* Run on the plugin updates only once
142+
*
143+
* @param num $newVersion
144+
* @param num $oldVersion
144145
*/
145-
public function onUpgrade($upgrader, $options)
146-
{
147-
$woopoly = 'woo-poly-integration/__init__.php';
148-
149-
if (
150-
$options['action'] == 'update' &&
151-
($options['type'] == 'plugin' && isset($options['plugins']))
152-
) {
153-
foreach ($options['plugins'] as $plugin) {
154-
if ($plugin == $woopoly) {
155-
flush_rewrite_rules();
156-
}
157-
}
158-
}
146+
public function onUpgrade($newVersion, $oldVersion) {
147+
flush_rewrite_rules(true);
159148
}
160149

161150
/**
162151
* Get current plugin version.
163152
*
164153
* @return int
165154
*/
166-
public static function getVersion()
167-
{
155+
public static function getVersion() {
168156
$data = get_plugin_data(Hyyan_WPI_DIR);
169157

170158
return $data['Version'];
@@ -178,8 +166,7 @@ public static function getVersion()
178166
*
179167
* @return string the view content
180168
*/
181-
public static function getView($name, array $vars = array())
182-
{
169+
public static function getView($name, array $vars = array()) {
183170
$result = '';
184171
$path = dirname(Hyyan_WPI_DIR) . '/src/Hyyan/WPI/Views/' . $name . '.php';
185172
if (file_exists($path)) {
@@ -194,8 +181,7 @@ public static function getView($name, array $vars = array())
194181
/**
195182
* Add plugin core classes.
196183
*/
197-
protected function registerCore()
198-
{
184+
protected function registerCore() {
199185
new Emails();
200186
new Admin\Settings();
201187
new Cart();
@@ -228,8 +214,7 @@ protected function registerCore()
228214
* @param mixed $file Plugin Base file
229215
* @return array
230216
*/
231-
public static function plugin_row_meta($links, $file)
232-
{
217+
public static function plugin_row_meta($links, $file) {
233218
if ('woo-poly-integration/__init__.php' == $file) {
234219
$row_meta = array(
235220
'docs' => '<a target="_blank" href="https://github.com/hyyan/woo-poly-integration/wiki"'
@@ -244,4 +229,5 @@ public static function plugin_row_meta($links, $file)
244229

245230
return (array) $links;
246231
}
232+
247233
}

0 commit comments

Comments
 (0)