17
17
*
18
18
* @author Hyyan Abo Fakher <[email protected] >
19
19
*/
20
- class Plugin
21
- {
20
+ class Plugin {
22
21
23
22
/** Required woocommerce version */
24
23
const WOOCOMMERCE_VERSION = '3.0.0 ' ;
@@ -29,20 +28,17 @@ class Plugin
29
28
/**
30
29
* Construct the plugin.
31
30
*/
32
- public function __construct ()
33
- {
31
+ public function __construct () {
34
32
FlashMessages::register ();
35
33
36
34
add_action ('init ' , array ($ this , 'activate ' ));
37
35
add_action ('plugins_loaded ' , array ($ this , 'loadTextDomain ' ));
38
- add_action ('upgrader_process_complete ' , array ($ this , 'onUpgrade ' ), 10 , 2 );
39
36
}
40
37
41
38
/**
42
39
* Load plugin langauge file.
43
40
*/
44
- public function loadTextDomain ()
45
- {
41
+ public function loadTextDomain () {
46
42
load_plugin_textdomain (
47
43
'woo-poly-integration ' , false , plugin_basename (dirname (Hyyan_WPI_DIR)) . '/languages '
48
44
);
@@ -56,9 +52,8 @@ public function loadTextDomain()
56
52
*
57
53
* @return bool false if plugin can not be activated
58
54
*/
59
- public function activate ()
60
- {
61
-
55
+ public function activate () {
56
+
62
57
if (!static ::canActivate ()) {
63
58
FlashMessages::remove (MessagesInterface::MSG_SUPPORT );
64
59
FlashMessages::add (
@@ -88,7 +83,14 @@ public function activate()
88
83
89
84
return $ settingsLinks + $ links ;
90
85
});
86
+
91
87
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
+ }
92
94
93
95
$ this ->registerCore ();
94
96
}
@@ -98,8 +100,7 @@ public function activate()
98
100
*
99
101
* @return bool true if can be activated , false otherwise
100
102
*/
101
- public static function canActivate ()
102
- {
103
+ public static function canActivate () {
103
104
$ polylang = false ;
104
105
$ woocommerce = false ;
105
106
@@ -133,38 +134,25 @@ public static function canActivate()
133
134
return ($ polylang && Utilities::polylangVersionCheck (self ::POLYLANG_VERSION )) &&
134
135
($ woocommerce && Utilities::woocommerceVersionCheck (self ::WOOCOMMERCE_VERSION ));
135
136
}
136
-
137
+
137
138
/**
138
139
* 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
144
145
*/
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 );
159
148
}
160
149
161
150
/**
162
151
* Get current plugin version.
163
152
*
164
153
* @return int
165
154
*/
166
- public static function getVersion ()
167
- {
155
+ public static function getVersion () {
168
156
$ data = get_plugin_data (Hyyan_WPI_DIR);
169
157
170
158
return $ data ['Version ' ];
@@ -178,8 +166,7 @@ public static function getVersion()
178
166
*
179
167
* @return string the view content
180
168
*/
181
- public static function getView ($ name , array $ vars = array ())
182
- {
169
+ public static function getView ($ name , array $ vars = array ()) {
183
170
$ result = '' ;
184
171
$ path = dirname (Hyyan_WPI_DIR) . '/src/Hyyan/WPI/Views/ ' . $ name . '.php ' ;
185
172
if (file_exists ($ path )) {
@@ -194,8 +181,7 @@ public static function getView($name, array $vars = array())
194
181
/**
195
182
* Add plugin core classes.
196
183
*/
197
- protected function registerCore ()
198
- {
184
+ protected function registerCore () {
199
185
new Emails ();
200
186
new Admin \Settings ();
201
187
new Cart ();
@@ -228,8 +214,7 @@ protected function registerCore()
228
214
* @param mixed $file Plugin Base file
229
215
* @return array
230
216
*/
231
- public static function plugin_row_meta ($ links , $ file )
232
- {
217
+ public static function plugin_row_meta ($ links , $ file ) {
233
218
if ('woo-poly-integration/__init__.php ' == $ file ) {
234
219
$ row_meta = array (
235
220
'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)
244
229
245
230
return (array ) $ links ;
246
231
}
232
+
247
233
}
0 commit comments