| 1 |
<?php |
| 2 |
namespace WeDevs\ERP; |
| 3 |
|
| 4 |
use WeDevs\ERP\Framework\Traits\Hooker; |
| 5 |
|
| 6 |
/** |
| 7 |
* Installation related functions and actions. |
| 8 |
* |
| 9 |
* @author Tareq Hasan |
| 10 |
* @package ERP |
| 11 |
*/ |
| 12 |
|
| 13 |
// don't call the file directly |
| 14 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Installer Class |
| 18 |
* |
| 19 |
* @package ERP |
| 20 |
*/ |
| 21 |
class Updates { |
| 22 |
|
| 23 |
use Hooker; |
| 24 |
|
| 25 |
/** @var array DB updates that need to be run */ |
| 26 |
private static $updates = [ |
| 27 |
'1.0' => 'updates/update-1.0.php', |
| 28 |
'1.1.0' => 'updates/update-1.1.0.php', |
| 29 |
'1.1.1' => 'updates/update-1.1.1.php', |
| 30 |
'1.1.2' => 'updates/update-1.1.2.php', |
| 31 |
'1.1.3' => 'updates/update-1.1.3.php', |
| 32 |
'1.1.5' => 'updates/update-1.1.5.php', |
| 33 |
'1.1.6' => 'updates/update-1.1.6.php', |
| 34 |
'1.1.7' => 'updates/update-1.1.7.php', |
| 35 |
'1.1.8' => 'updates/update-1.1.8.php', |
| 36 |
'1.1.9' => 'updates/update-1.1.9.php', |
| 37 |
'1.1.17' => 'updates/update-1.1.17.php', |
| 38 |
'1.2.1' => 'updates/update-1.2.1.php', |
| 39 |
'1.2.2' => 'updates/update-1.2.2.php', |
| 40 |
'1.2.5' => 'updates/update-1.2.5.php' |
| 41 |
]; |
| 42 |
|
| 43 |
/** |
| 44 |
* Current active erp modules |
| 45 |
* |
| 46 |
* @since 1.1.9 |
| 47 |
* |
| 48 |
* @var array |
| 49 |
*/ |
| 50 |
private $active_modules = []; |
| 51 |
|
| 52 |
/** |
| 53 |
* Binding all events |
| 54 |
* |
| 55 |
* @since 0.1 |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
function __construct() { |
| 60 |
$this->action( 'admin_notices', 'show_update_notice' ); |
| 61 |
$this->action( 'admin_init', 'do_updates' ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Check if need any update |
| 66 |
* |
| 67 |
* @since 1.0 |
| 68 |
* |
| 69 |
* @return boolean |
| 70 |
*/ |
| 71 |
public function is_needs_update() { |
| 72 |
$installed_version = get_option( 'wp_erp_version' ); |
| 73 |
|
| 74 |
// may be it's the first install |
| 75 |
if ( ! $installed_version ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
if ( version_compare( $installed_version, WPERP_VERSION, '<' ) ) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Show update notice |
| 88 |
* |
| 89 |
* @since 1.0 |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function show_update_notice() { |
| 94 |
if ( ! current_user_can( 'update_plugins' ) || ! $this->is_needs_update() ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$installed_version = get_option( 'wp_erp_version' ); |
| 99 |
$updatable_versions = array_keys( self::$updates ); |
| 100 |
|
| 101 |
if ( ! is_null( $installed_version ) && version_compare( $installed_version, end( $updatable_versions ), '<' ) ) { |
| 102 |
?> |
| 103 |
<div id="message" class="updated"> |
| 104 |
<p><?php _e( '<strong>WP ERP Data Update Required</strong> – We need to update your install to the latest version', 'erp' ); ?></p> |
| 105 |
<p class="submit"><a href="<?php echo add_query_arg( [ 'wperp_do_update' => true ], $_SERVER['REQUEST_URI'] ); ?>" class="wperp-update-btn button-primary"><?php _e( 'Run the updater', 'erp' ); ?></a></p> |
| 106 |
</div> |
| 107 |
|
| 108 |
<script type="text/javascript"> |
| 109 |
jQuery('.wperp-update-btn').click('click', function(){ |
| 110 |
return confirm( '<?php _e( 'It is strongly recommended that you backup your database before proceeding. Are you sure you wish to run the updater now?', 'erp' ); ?>' ); |
| 111 |
}); |
| 112 |
</script> |
| 113 |
<?php |
| 114 |
} else { |
| 115 |
update_option( 'wp_erp_version', WPERP_VERSION ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Do all updates when Run updater btn click |
| 121 |
* |
| 122 |
* @since 1.0 |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function do_updates() { |
| 127 |
if ( isset( $_GET['wperp_do_update'] ) && $_GET['wperp_do_update'] ) { |
| 128 |
$this->perform_updates(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Perform all updates |
| 134 |
* |
| 135 |
* @since 1.0 |
| 136 |
* |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public function perform_updates() { |
| 140 |
if ( ! $this->is_needs_update() ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
$installed_version = get_option( 'wp_erp_version' ); |
| 145 |
|
| 146 |
$this->enable_all_erp_modules(); |
| 147 |
|
| 148 |
foreach ( self::$updates as $version => $path ) { |
| 149 |
if ( version_compare( $installed_version, $version, '<' ) ) { |
| 150 |
include $path; |
| 151 |
update_option( 'wp_erp_version', $version ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
update_option( 'wp_erp_version', WPERP_VERSION ); |
| 156 |
|
| 157 |
$this->enable_active_erp_modules(); |
| 158 |
|
| 159 |
$location = remove_query_arg( ['wperp_do_update'], $_SERVER['REQUEST_URI'] ); |
| 160 |
wp_redirect( $location ); |
| 161 |
exit(); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Enable all erp modules before run the updaters |
| 166 |
* |
| 167 |
* @since 1.1.9 |
| 168 |
* |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
private function enable_all_erp_modules() { |
| 172 |
// Let's remember the active modules. |
| 173 |
$this->active_modules = wperp()->modules->get_active_modules(); |
| 174 |
|
| 175 |
$all_modules = wperp()->modules->get_modules(); |
| 176 |
|
| 177 |
update_option( 'erp_modules', $all_modules ); |
| 178 |
|
| 179 |
wperp()->load_module(); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Enable modules that were active before running the updater |
| 184 |
* |
| 185 |
* @since 1.1.9 |
| 186 |
* |
| 187 |
* @return void |
| 188 |
*/ |
| 189 |
private function enable_active_erp_modules() { |
| 190 |
update_option( 'erp_modules', $this->active_modules ); |
| 191 |
|
| 192 |
wperp()->load_module(); |
| 193 |
} |
| 194 |
} |
| 195 |
|