1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace WC_POS;
13
14 class Activator {
15
16
17 const WC_MIN_VERSION = '2.3.7';
18 const PHP_MIN_VERSION = '5.4';
19
20 21 22
23 public function __construct() {
24 register_activation_hook( PLUGIN_FILE, array( $this, 'activate' ) );
25 add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
26 add_action( 'plugins_loaded', array( $this, 'run' ) );
27 }
28
29 30 31
32 public function run(){
33
34 if( $this->php_check() && $this->woocommerce_check() ){
35
36
37 if( is_admin() && (!defined('\DOING_AJAX') || !\DOING_AJAX) ){
38 $this->permalink_check();
39 }
40
41
42 $this->version_check();
43
44
45 $this->plugin_check();
46
47 new Setup();
48 }
49 }
50
51 52 53 54 55
56 public function activate( $network_wide ) {
57 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
58
59 if ( $network_wide ) {
60
61
62 $blog_ids = $this->get_blog_ids();
63
64 foreach ( $blog_ids as $blog_id ) {
65
66 switch_to_blog( $blog_id );
67 $this->single_activate();
68
69 restore_current_blog();
70 }
71
72 } else {
73 self::single_activate();
74 }
75
76 } else {
77 self::single_activate();
78 }
79 }
80
81 82 83 84 85
86 public function activate_new_site( $blog_id ) {
87
88 if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
89 return;
90 }
91
92 switch_to_blog( $blog_id );
93 $this->single_activate();
94 restore_current_blog();
95
96 }
97
98 99 100 101 102 103
104 private function get_blog_ids() {
105
106 global $wpdb;
107
108
109 $sql = "SELECT blog_id FROM $wpdb->blogs
110 WHERE archived = '0' AND spam = '0'
111 AND deleted = '0'";
112
113 return $wpdb->get_col( $sql );
114
115 }
116
117 118 119
120 public function single_activate() {
121
122 $this->add_pos_capability();
123
124
125
126 }
127
128 129 130 131
132 private function add_pos_capability(){
133 $roles = array('administrator', 'shop_manager');
134 $caps = array('manage_woocommerce_pos', 'access_woocommerce_pos');
135 foreach($roles as $slug) :
136 $role = get_role($slug);
137 if($role) : foreach($caps as $cap) :
138 $role->add_cap($cap);
139 endforeach; endif;
140 endforeach;
141 }
142
143 144 145
146 private function version_check(){
147 $old = Admin\Settings::get_db_version();
148 if( version_compare( $old, VERSION, '<' ) ){
149 Admin\Settings::bump_versions();
150 $this->db_upgrade( $old, VERSION );
151 }
152 }
153
154 155 156 157 158
159 private function db_upgrade( $old, $current ) {
160 $db_updates = array(
161 '0.4' => 'updates/update-0.4.php',
162 '0.4.6' => 'updates/update-0.4.6.php'
163 );
164 foreach ( $db_updates as $version => $updater ) {
165 if ( version_compare( $version, $old, '>' ) &&
166 version_compare( $version, $current, '<=' ) ) {
167 include( $updater );
168 }
169 }
170 }
171
172 173 174
175 private function woocommerce_check() {
176 if( class_exists( '\WooCommerce' ) && version_compare( WC()->version, self::WC_MIN_VERSION, '>=' ) )
177 return true;
178
179 $message = sprintf(
180 __('<strong>WooCommerce POS</strong> requires <a href="%s">WooCommerce %s or higher</a>. Please <a href="%s">install and activate WooCommerce</a>', 'woocommerce-pos' ),
181 'http://wordpress.org/plugins/woocommerce/',
182 self::WC_MIN_VERSION,
183 admin_url('plugins.php')
184 ) . ' »';
185
186 Admin\Notices::add( $message );
187 }
188
189 190 191
192 private function php_check(){
193 $php_version = phpversion();
194 if( version_compare( $php_version, self::PHP_MIN_VERSION, '>' ) )
195 return true;
196
197 $message = sprintf(
198 __('<strong>WooCommerce POS</strong> requires PHP %s or higher. Read more information about <a href="%s">how you can update</a>', 'woocommerce-pos' ),
199 self::PHP_MIN_VERSION,
200 'http://www.wpupdatephp.com/update/'
201 ) . ' »';
202
203 Admin\Notices::add( $message );
204 }
205
206 207 208 209
210 private function permalink_check(){
211 $fail = Status::permalinks_disabled();
212 if( $fail ){
213 $message = $fail['message'] . '. ';
214 $message .= sprintf( '<a href="%s">%s</a>', $fail['buttons'][0]['href'], $fail['buttons'][0]['prompt'] ) . ' »';
215
216 Admin\Notices::add( $message );
217 }
218 }
219
220 221 222 223 224 225
226 private function plugin_check(){
227
228
229 if( !defined('NGG_DISABLE_RESOURCE_MANAGER') )
230 define( 'NGG_DISABLE_RESOURCE_MANAGER', true );
231
232 }
233
234 }