| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Extension Factory |
| 5 |
* |
| 6 |
* @package NotificationX\Extensions |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace NotificationX\Core; |
| 10 |
|
| 11 |
use NotificationX\GetInstance; |
| 12 |
use NotificationX\NotificationX; |
| 13 |
|
| 14 |
/** |
| 15 |
* @method static Upgrader get_instance($args = null) |
| 16 |
*/ |
| 17 |
class Upgrader { |
| 18 |
/** |
| 19 |
* Instance of Upgrader |
| 20 |
* |
| 21 |
* @var Upgrader |
| 22 |
*/ |
| 23 |
use GetInstance; |
| 24 |
|
| 25 |
protected $database; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initially Invoked when initialized. |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
$this->database = Database::get_instance(); |
| 32 |
$nx_free_version = $this->database->get_option('nx_free_version'); |
| 33 |
$nx_db_version = $this->database->get_option('nx_db_version'); |
| 34 |
|
| 35 |
// Show milestone notification only for 3.1.10 |
| 36 |
$force_milstone = get_option('notificationx_force_milestone', ''); |
| 37 |
if ( version_compare( NOTIFICATIONX_VERSION, '3.1.10', '==' ) && empty($force_milstone) ) { |
| 38 |
delete_option('notificationx_milestone_level'); |
| 39 |
} |
| 40 |
|
| 41 |
// Show popup notification only for 3.1.10 |
| 42 |
$force_popup = get_option('nx_force_popup_dismissed', ''); |
| 43 |
if ( version_compare( NOTIFICATIONX_VERSION, '3.1.10', '==' ) && empty($force_popup) ) { |
| 44 |
delete_option('nx_initial_popup_dismissed'); |
| 45 |
} |
| 46 |
|
| 47 |
$_is_table_created = false; |
| 48 |
if ($nx_db_version === false || $nx_db_version != Database::$version) { |
| 49 |
try { |
| 50 |
Database::get_instance()->Create_DB(); |
| 51 |
$this->database->update_option('nx_db_version', Database::$version, 'no'); |
| 52 |
$_is_table_created = true; |
| 53 |
} catch (\Exception $th) { |
| 54 |
error_log('NX: Database Creation Failed'); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
if((!$nx_free_version || version_compare($nx_free_version, '2.0.0', '<')) && $_is_table_created ){ |
| 59 |
try { |
| 60 |
Migration::get_instance(); |
| 61 |
$this->database->update_option('notificationx_2x_upgraded', true, 'no'); |
| 62 |
} catch (\Exception $th) { |
| 63 |
error_log('NX: Migration Failed'); |
| 64 |
} |
| 65 |
$this->database->update_option( 'nx_free_version', NOTIFICATIONX_VERSION, 'no' ); |
| 66 |
} elseif(!$nx_free_version && $_is_table_created ){ |
| 67 |
$this->database->update_option( 'nx_free_version', NOTIFICATIONX_VERSION, 'no' ); |
| 68 |
} |
| 69 |
if ( version_compare($nx_free_version, '2.8.0', '<=') ) { |
| 70 |
$this->migrate_for_donation(); |
| 71 |
} |
| 72 |
if ($nx_free_version !== NOTIFICATIONX_VERSION) { |
| 73 |
// The onboarding Setup Wizard is only launched for new users on fresh |
| 74 |
// activation (handled by the activation redirect in NotificationX.php). |
| 75 |
// Existing users updating the plugin should NOT be sent through the |
| 76 |
// wizard again, so we only bump the stored version here. |
| 77 |
$this->database->update_option( 'nx_free_version', NOTIFICATIONX_VERSION, 'no' ); |
| 78 |
$this->clear_transient(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function clear_transient(){ |
| 83 |
delete_transient('nx_builder_fields'); |
| 84 |
} |
| 85 |
|
| 86 |
public function migrate_for_donation() { |
| 87 |
$posts = Database::get_instance()->get_col( Database::$table_posts, 'data', ['type' => 'donation'] ); |
| 88 |
|
| 89 |
if ( ! empty( $posts ) ) { |
| 90 |
foreach( $posts as $post ) { |
| 91 |
if ( isset( $post['notification-template']['first_param'] ) && $post['notification-template']['first_param'] === 'tag_sales_count' ) { |
| 92 |
$post['notification-template']['first_param'] = 'tag_donation_count'; |
| 93 |
Database::get_instance()->update_post( Database::$table_posts, ['data' => $post], $post['nx_id'] ); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|