PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.3.13
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.3.13
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / class-updates.php

class-updates.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.3.13, at includes/class-updates.php

205 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 '1.2.7' => 'updates/update-1.2.7.php',
42 '1.3.2' => 'updates/update-1.3.2.php',
43 '1.3.3' => 'updates/update-1.3.3.php',
44 '1.3.4' => 'updates/update-1.3.4.php',
45 ];
46
47 /**
48 * Current active erp modules
49 *
50 * @since 1.1.9
51 *
52 * @var array
53 */
54 private $active_modules = [];
55
56 /**
57 * Binding all events
58 *
59 * @since 0.1
60 *
61 * @return void
62 */
63 function __construct() {
64 $this->action( 'admin_notices', 'show_update_notice' );
65 $this->action( 'admin_init', 'do_updates' );
66 }
67
68 /**
69 * Check if need any update
70 *
71 * @since 1.0
72 *
73 * @return boolean
74 */
75 public function is_needs_update() {
76 $installed_version = get_option( 'wp_erp_version' );
77
78 // may be it's the first install
79 if ( ! $installed_version ) {
80 return false;
81 }
82
83 if ( version_compare( $installed_version, WPERP_VERSION, '<' ) ) {
84 return true;
85 }
86
87 return false;
88 }
89
90 /**
91 * Show update notice
92 *
93 * @since 1.0
94 *
95 * @return void
96 */
97 public function show_update_notice() {
98 if ( ! current_user_can( 'update_plugins' ) || ! $this->is_needs_update() ) {
99 return;
100 }
101
102 $installed_version = get_option( 'wp_erp_version' );
103 $updatable_versions = array_keys( self::$updates );
104
105 if ( ! is_null( $installed_version ) && version_compare( $installed_version, end( $updatable_versions ), '<' ) ) {
106 ?>
107 <div id="message" class="updated">
108 <p><?php _e( '<strong>WP ERP Data Update Required</strong> &#8211; We need to update your install to the latest version', 'erp' ); ?></p>
109 <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>
110 </div>
111
112 <script type="text/javascript">
113 jQuery('.wperp-update-btn').click('click', function(){
114 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' ); ?>' );
115 });
116 </script>
117 <?php
118 } else {
119 update_option( 'wp_erp_version', WPERP_VERSION );
120 }
121 }
122
123 /**
124 * Do all updates when Run updater btn click
125 *
126 * @since 1.0
127 * @since 1.2.7 save plugin install date
128 *
129 * @return void
130 */
131 public function do_updates() {
132 if ( isset( $_GET['wperp_do_update'] ) && $_GET['wperp_do_update'] ) {
133 $this->perform_updates();
134 }
135 }
136
137 /**
138 * Perform all updates
139 *
140 * @since 1.0
141 *
142 * @return void
143 */
144 public function perform_updates() {
145 if ( ! $this->is_needs_update() ) {
146 return;
147 }
148
149 $installed_version = get_option( 'wp_erp_version' );
150
151 $this->enable_all_erp_modules();
152
153 foreach ( self::$updates as $version => $path ) {
154 if ( version_compare( $installed_version, $version, '<' ) ) {
155 include $path;
156 update_option( 'wp_erp_version', $version );
157 }
158 }
159
160 update_option( 'wp_erp_version', WPERP_VERSION );
161
162 //save install date
163 if ( false == get_option( 'wp_erp_install_date' ) ) {
164 update_option( 'wp_erp_install_date', current_time( 'timestamp' ) );
165 }
166
167 $this->enable_active_erp_modules();
168
169 $location = remove_query_arg( ['wperp_do_update'], $_SERVER['REQUEST_URI'] );
170 wp_redirect( $location );
171 exit();
172 }
173
174 /**
175 * Enable all erp modules before run the updaters
176 *
177 * @since 1.1.9
178 *
179 * @return void
180 */
181 private function enable_all_erp_modules() {
182 // Let's remember the active modules.
183 $this->active_modules = wperp()->modules->get_active_modules();
184
185 $all_modules = wperp()->modules->get_modules();
186
187 update_option( 'erp_modules', $all_modules );
188
189 wperp()->load_module();
190 }
191
192 /**
193 * Enable modules that were active before running the updater
194 *
195 * @since 1.1.9
196 *
197 * @return void
198 */
199 private function enable_active_erp_modules() {
200 update_option( 'erp_modules', $this->active_modules );
201
202 wperp()->load_module();
203 }
204 }
205