WC_POS_Activator

Activation checks and set up.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-pos-activator.php  
  1. class WC_POS_Activator { 
  2.  
  3. // minimum requirements 
  4. const WC_MIN_VERSION = '2.3.7'; 
  5. const PHP_MIN_VERSION = '5.4'; 
  6.  
  7. /** 
  8. * @param $file 
  9. */ 
  10. public function __construct( $file ) { 
  11. register_activation_hook( $file, array( $this, 'activate' ) ); 
  12. add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) ); 
  13.  
  14. add_action( 'plugins_loaded', array( $this, 'run_woocommerce_pos' ) ); 
  15. $this->init(); 
  16.  
  17. /** 
  18. * init subclasses 
  19. * - WC_POS_Admin_Notices required in cases where WooCommerce POS fails to run 
  20. */ 
  21. private function init() { 
  22. if( is_admin() && (!defined('DOING_AJAX') || !DOING_AJAX) ) { 
  23. require_once WC_POS_PLUGIN_PATH . 'includes/admin/class-wc-pos-notices.php'; 
  24. new WC_POS_Admin_Notices(); 
  25.  
  26. /** 
  27. * Checks for valid install and begins execution of the plugin. 
  28. */ 
  29. public function run_woocommerce_pos() { 
  30. // Check for min requirements to run 
  31. if( $this->php_check() && $this->woocommerce_check() ) { 
  32. // global helper functions 
  33. require_once WC_POS_PLUGIN_PATH . 'includes/wc-pos-functions.php'; 
  34.  
  35. require_once WC_POS_PLUGIN_PATH . 'includes/class-wc-pos.php'; 
  36. new WC_POS(); 
  37.  
  38. // check permalinks 
  39. if( is_admin() && (!defined('DOING_AJAX') || !DOING_AJAX) ) { 
  40. $this->permalink_check(); 
  41.  
  42. // Run update script if required 
  43. $this->version_check(); 
  44.  
  45. $this->check_plugin_conflicts(); 
  46.  
  47. /** 
  48. * Fired when the plugin is activated. 
  49. * @param $network_wide 
  50. */ 
  51. public function activate( $network_wide ) { 
  52. if ( function_exists( 'is_multisite' ) && is_multisite() ) { 
  53.  
  54. if ( $network_wide ) { 
  55.  
  56. // Get all blog ids 
  57. $blog_ids = $this->get_blog_ids(); 
  58.  
  59. foreach ( $blog_ids as $blog_id ) { 
  60.  
  61. switch_to_blog( $blog_id ); 
  62. $this->single_activate(); 
  63.  
  64. restore_current_blog(); 
  65.  
  66. } else { 
  67. self::single_activate(); 
  68.  
  69. } else { 
  70. self::single_activate(); 
  71.  
  72. /** 
  73. * Fired when a new site is activated with a WPMU environment. 
  74. * @param $blog_id 
  75. */ 
  76. public function activate_new_site( $blog_id ) { 
  77.  
  78. if ( 1 !== did_action( 'wpmu_new_blog' ) ) { 
  79. return; 
  80.  
  81. switch_to_blog( $blog_id ); 
  82. $this->single_activate(); 
  83. restore_current_blog(); 
  84.  
  85.  
  86. /** 
  87. * Get all blog ids of blogs in the current network that are: 
  88. * - not archived 
  89. * - not spam 
  90. * - not deleted 
  91. */ 
  92. private function get_blog_ids() { 
  93.  
  94. global $wpdb; 
  95.  
  96. // get an array of blog ids 
  97. $sql = "SELECT blog_id FROM $wpdb->blogs 
  98. WHERE archived = '0' AND spam = '0' 
  99. AND deleted = '0'"; 
  100.  
  101. return $wpdb->get_col( $sql ); 
  102.  
  103.  
  104. /** 
  105. * Fired when the plugin is activated. 
  106. */ 
  107. public function single_activate() { 
  108. // add pos capabilities 
  109. $this->add_pos_capability(); 
  110.  
  111. // set the auto redirection on next page load 
  112. //set_transient( 'woocommere_pos_welcome', 1, 30 ); 
  113.  
  114. /** 
  115. * add default pos capabilities to administrator and 
  116. * shop_manager roles 
  117. */ 
  118. private function add_pos_capability() { 
  119. $roles = array('administrator', 'shop_manager'); 
  120. $caps = array('manage_woocommerce_pos', 'access_woocommerce_pos'); 
  121. foreach($roles as $slug) : 
  122. $role = get_role($slug); 
  123. if($role) : foreach($caps as $cap) : 
  124. $role->add_cap($cap); 
  125. endforeach; endif; 
  126. endforeach; 
  127.  
  128. /** 
  129. * Check version number, runs every admin page load 
  130. */ 
  131. private function version_check() { 
  132. $old = WC_POS_Settings::get_db_version(); 
  133. if( version_compare( $old, WC_POS_VERSION, '<' ) ) { 
  134. WC_POS_Settings::bump_versions(); 
  135. $this->db_upgrade( $old, WC_POS_VERSION ); 
  136.  
  137. /** 
  138. * Upgrade database 
  139. * @param $old 
  140. * @param $current 
  141. */ 
  142. private function db_upgrade( $old, $current ) { 
  143. $db_updates = array( 
  144. '0.4' => 'updates/update-0.4.php',  
  145. '0.4.6' => 'updates/update-0.4.6.php',  
  146. ); 
  147. foreach ( $db_updates as $version => $updater ) { 
  148. if ( version_compare( $version, $old, '>' ) && 
  149. version_compare( $version, $current, '<=' ) ) { 
  150. include( $updater ); 
  151.  
  152. /** 
  153. * Check min version of WooCommerce installed 
  154. */ 
  155. private function woocommerce_check() { 
  156. if( class_exists( 'WooCommerce' ) && version_compare( WC()->version, self::WC_MIN_VERSION, '>=' ) ) 
  157. return true; 
  158.  
  159. if( class_exists( 'WC_POS_Admin_Notices' ) ) { 
  160. $message = sprintf( 
  161. __('<strong>WooCommerce POS</strong> requires <a href="%s">WooCommerce %s or higher</a>. Please <a href="%s">install and activate WooCommerce</a>', 'woocommerce-pos' ),  
  162. 'http://wordpress.org/plugins/woocommerce/',  
  163. self::WC_MIN_VERSION,  
  164. admin_url('plugins.php') 
  165. ) . ' »'; 
  166. WC_POS_Admin_Notices::add( $message ); 
  167.  
  168. /** 
  169. * Check min version of PHP 
  170. */ 
  171. private function php_check() { 
  172. $php_version = phpversion(); 
  173. if( version_compare( $php_version, self::PHP_MIN_VERSION, '>' ) ) 
  174. return true; 
  175.  
  176. if( class_exists( 'WC_POS_Admin_Notices' ) ) { 
  177. $message = sprintf( 
  178. __('<strong>WooCommerce POS</strong> requires PHP %s or higher. Read more information about <a href="%s">how you can update</a>', 'woocommerce-pos' ),  
  179. self::PHP_MIN_VERSION,  
  180. 'http://www.wpupdatephp.com/update/' 
  181. ) . ' »'; 
  182. WC_POS_Admin_Notices::add( $message ); 
  183.  
  184. /** 
  185. * POS Frontend will give 404 if pretty permalinks not active 
  186. * - requires autoloader, ie: WC_POS() 
  187. */ 
  188. private function permalink_check() { 
  189. $fail = WC_POS_Status::permalinks_disabled(); 
  190. if( $fail ) { 
  191. $message = $fail['message'] . '. '; 
  192. $message .= sprintf( '<a href="%s">%s</a>', $fail['buttons'][0]['href'], $fail['buttons'][0]['prompt'] ) . ' »'; 
  193. WC_POS_Admin_Notices::add( $message ); 
  194.  
  195. /** 
  196. */ 
  197. private function check_plugin_conflicts() { 
  198. // disable NextGEN Gallery 
  199. if( !defined('NGG_DISABLE_RESOURCE_MANAGER') ) 
  200. define( 'NGG_DISABLE_RESOURCE_MANAGER', true ); 
  201.