| @@ -1,0 +1,109 @@ | ||
| 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 | + // Backward compatibility: installs that already ran NotificationX | |
| 36 | + // before the first-activation flag existed must not be treated as a | |
| 37 | + // first-ever activation when they are re-activated, or they would be | |
| 38 | + // pushed into the Setup Wizard again. A stored free version proves the | |
| 39 | + // plugin has run here before, so seed the flag once. | |
| 40 | + if ( $nx_free_version && ! NotificationX::has_activated_before() ) { | |
| 41 | + update_option( NotificationX::FIRST_ACTIVATION_OPTION, time(), 'no' ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + // Show milestone notification only for 3.1.10 | |
| 45 | + $force_milstone = get_option('notificationx_force_milestone', ''); | |
| 46 | + if ( version_compare( NOTIFICATIONX_VERSION, '3.1.10', '==' ) && empty($force_milstone) ) { | |
| 47 | + delete_option('notificationx_milestone_level'); | |
| 48 | + } | |
| 49 | + | |
| 50 | + // Show popup notification only for 3.1.10 | |
| 51 | + $force_popup = get_option('nx_force_popup_dismissed', ''); | |
| 52 | + if ( version_compare( NOTIFICATIONX_VERSION, '3.1.10', '==' ) && empty($force_popup) ) { | |
| 53 | + delete_option('nx_initial_popup_dismissed'); | |
| 54 | + } | |
| 55 | + | |
| 56 | + $_is_table_created = false; | |
| 57 | + if ($nx_db_version === false || $nx_db_version != Database::$version) { | |
| 58 | + try { | |
| 59 | + Database::get_instance()->Create_DB(); | |
| 60 | + $this->database->update_option('nx_db_version', Database::$version, 'no'); | |
| 61 | + $_is_table_created = true; | |
| 62 | + } catch (\Exception $th) { | |
| 63 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- deliberate failure logging, not debug output. | |
| 64 | + error_log('NX: Database Creation Failed'); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + if((!$nx_free_version || version_compare($nx_free_version, '2.0.0', '<')) && $_is_table_created ){ | |
| 69 | + try { | |
| 70 | + Migration::get_instance(); | |
| 71 | + $this->database->update_option('notificationx_2x_upgraded', true, 'no'); | |
| 72 | + } catch (\Exception $th) { | |
| 73 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- deliberate failure logging, not debug output. | |
| 74 | + error_log('NX: Migration Failed'); | |
| 75 | + } | |
| 76 | + $this->database->update_option( 'nx_free_version', NOTIFICATIONX_VERSION, 'no' ); | |
| 77 | + } elseif(!$nx_free_version && $_is_table_created ){ | |
| 78 | + $this->database->update_option( 'nx_free_version', NOTIFICATIONX_VERSION, 'no' ); | |
| 79 | + } | |
| 80 | + if ( version_compare($nx_free_version, '2.8.0', '<=') ) { | |
| 81 | + $this->migrate_for_donation(); | |
| 82 | + } | |
| 83 | + if ($nx_free_version !== NOTIFICATIONX_VERSION) { | |
| 84 | + // The onboarding Setup Wizard is only launched for new users on fresh | |
| 85 | + // activation (handled by the activation redirect in NotificationX.php). | |
| 86 | + // Existing users updating the plugin should NOT be sent through the | |
| 87 | + // wizard again, so we only bump the stored version here. | |
| 88 | + $this->database->update_option( 'nx_free_version', NOTIFICATIONX_VERSION, 'no' ); | |
| 89 | + $this->clear_transient(); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 93 | + public function clear_transient(){ | |
| 94 | + delete_transient('nx_builder_fields'); | |
| 95 | + } | |
| 96 | + | |
| 97 | + public function migrate_for_donation() { | |
| 98 | + $posts = Database::get_instance()->get_col( Database::$table_posts, 'data', ['type' => 'donation'] ); | |
| 99 | + | |
| 100 | + if ( ! empty( $posts ) ) { | |
| 101 | + foreach( $posts as $post ) { | |
| 102 | + if ( isset( $post['notification-template']['first_param'] ) && $post['notification-template']['first_param'] === 'tag_sales_count' ) { | |
| 103 | + $post['notification-template']['first_param'] = 'tag_donation_count'; | |
| 104 | + Database::get_instance()->update_post( Database::$table_posts, ['data' => $post], $post['nx_id'] ); | |
| 105 | + } | |
| 106 | + } | |
| 107 | + } | |
| 108 | + } | |
| 109 | +} | |