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

Commit

Permalink
Fix #248 , Fix #266 upgrader_process_complete is not ideal
Browse files Browse the repository at this point in the history
  • Loading branch information
hyyan committed Dec 8, 2017
1 parent 3835837 commit 01bc5b2
Showing 1 changed file with 26 additions and 40 deletions.
66 changes: 26 additions & 40 deletions src/Hyyan/WPI/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
*
* @author Hyyan Abo Fakher <hyyanaf@gmail.com>
*/
class Plugin
{
class Plugin {

/** Required woocommerce version */
const WOOCOMMERCE_VERSION = '3.0.0';
Expand All @@ -29,20 +28,17 @@ class Plugin
/**
* Construct the plugin.
*/
public function __construct()
{
public function __construct() {
FlashMessages::register();

add_action('init', array($this, 'activate'));
add_action('plugins_loaded', array($this, 'loadTextDomain'));
add_action('upgrader_process_complete', array($this, 'onUpgrade'), 10, 2);
}

/**
* Load plugin langauge file.
*/
public function loadTextDomain()
{
public function loadTextDomain() {
load_plugin_textdomain(
'woo-poly-integration', false, plugin_basename(dirname(Hyyan_WPI_DIR)) . '/languages'
);
Expand All @@ -56,9 +52,8 @@ public function loadTextDomain()
*
* @return bool false if plugin can not be activated
*/
public function activate()
{

public function activate() {

if (!static::canActivate()) {
FlashMessages::remove(MessagesInterface::MSG_SUPPORT);
FlashMessages::add(
Expand Down Expand Up @@ -88,7 +83,14 @@ public function activate()

return $settingsLinks + $links;
});

add_filter('plugin_row_meta', array(__CLASS__, 'plugin_row_meta'), 10, 2);

$oldVersion = get_option('wpi_version');
if (version_compare(self::getVersion(), $oldVersion, '<>')) {
$this->onUpgrade(self::getVersion(), $oldVersion);
update_option('wpi_version', self::getVersion());
}

$this->registerCore();
}
Expand All @@ -98,8 +100,7 @@ public function activate()
*
* @return bool true if can be activated , false otherwise
*/
public static function canActivate()
{
public static function canActivate() {
$polylang = false;
$woocommerce = false;

Expand Down Expand Up @@ -133,38 +134,25 @@ public static function canActivate()
return ($polylang && Utilities::polylangVersionCheck(self::POLYLANG_VERSION)) &&
($woocommerce && Utilities::woocommerceVersionCheck(self::WOOCOMMERCE_VERSION));
}

/**
* On Upgrade
*
* Run only once the plugin has been updated to a new version
*
* @param \Plugin_Upgrader $upgrader
* @param Array $options
*
* Run on the plugin updates only once
*
* @param num $newVersion
* @param num $oldVersion
*/
public function onUpgrade($upgrader, $options)
{
$woopoly = 'woo-poly-integration/__init__.php';

if (
$options['action'] == 'update' &&
($options['type'] == 'plugin' && isset($options['plugins']))
) {
foreach ($options['plugins'] as $plugin) {
if ($plugin == $woopoly) {
flush_rewrite_rules();
}
}
}
public function onUpgrade($newVersion, $oldVersion) {
flush_rewrite_rules(true);
}

/**
* Get current plugin version.
*
* @return int
*/
public static function getVersion()
{
public static function getVersion() {
$data = get_plugin_data(Hyyan_WPI_DIR);

return $data['Version'];
Expand All @@ -178,8 +166,7 @@ public static function getVersion()
*
* @return string the view content
*/
public static function getView($name, array $vars = array())
{
public static function getView($name, array $vars = array()) {
$result = '';
$path = dirname(Hyyan_WPI_DIR) . '/src/Hyyan/WPI/Views/' . $name . '.php';
if (file_exists($path)) {
Expand All @@ -194,8 +181,7 @@ public static function getView($name, array $vars = array())
/**
* Add plugin core classes.
*/
protected function registerCore()
{
protected function registerCore() {
new Emails();
new Admin\Settings();
new Cart();
Expand Down Expand Up @@ -228,8 +214,7 @@ protected function registerCore()
* @param mixed $file Plugin Base file
* @return array
*/
public static function plugin_row_meta($links, $file)
{
public static function plugin_row_meta($links, $file) {
if ('woo-poly-integration/__init__.php' == $file) {
$row_meta = array(
'docs' => '<a target="_blank" href="https://github.com/hyyan/woo-poly-integration/wiki"'
Expand All @@ -244,4 +229,5 @@ public static function plugin_row_meta($links, $file)

return (array) $links;
}

}

0 comments on commit 01bc5b2

Please sign in to comment.