WooCommerce_MyParcel

The WooCommerce MyParcel WooCommerce MyParcel class.

Defined (1)

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

/woocommerce-myparcel.php  
  1. class WooCommerce_MyParcel { 
  2.  
  3. public $version = '2.2.0'; 
  4. public $plugin_basename; 
  5.  
  6. protected static $_instance = null; 
  7.  
  8. /** 
  9. * Main Plugin Instance 
  10. * Ensures only one instance of plugin is loaded or can be loaded. 
  11. */ 
  12. public static function instance() { 
  13. if ( is_null( self::$_instance ) ) { 
  14. self::$_instance = new self(); 
  15. return self::$_instance; 
  16.  
  17. /** 
  18. * Constructor 
  19. */ 
  20.  
  21. public function __construct() { 
  22. $this->define( 'WC_MYPARCEL_VERSION', $this->version ); 
  23. $this->plugin_basename = plugin_basename(__FILE__); 
  24.  
  25. // Load settings 
  26. $this->general_settings = get_option( 'woocommerce_myparcel_general_settings' ); 
  27. $this->export_defaults = get_option( 'woocommerce_myparcel_export_defaults_settings' ); 
  28. $this->checkout_settings = get_option( 'woocommerce_myparcel_checkout_settings' ); 
  29.  
  30. // load the localisation & classes 
  31. add_action( 'plugins_loaded', array( $this, 'translations' ) ); 
  32. add_action( 'init', array( $this, 'load_classes' ) ); 
  33.  
  34. // run lifecycle methods 
  35. if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { 
  36. add_action( 'wp_loaded', array( $this, 'do_install' ) ); 
  37.  
  38. /** 
  39. * Define constant if not already set 
  40. * @param string $name 
  41. * @param string|bool $value 
  42. */ 
  43. private function define( $name, $value ) { 
  44. if ( ! defined( $name ) ) { 
  45. define( $name, $value ); 
  46.  
  47. /** 
  48. * Load the translation / textdomain files 
  49. *  
  50. * Note: the first-loaded translation file overrides any following ones if the same translation is present 
  51. */ 
  52. public function translations() { 
  53. $locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-myparcel' ); 
  54. $dir = trailingslashit( WP_LANG_DIR ); 
  55.  
  56. /** 
  57. * Frontend/global Locale. Looks in: 
  58. * - WP_LANG_DIR/woocommerce-myparcel/woocommerce-myparcel-LOCALE.mo 
  59. * - WP_LANG_DIR/plugins/woocommerce-myparcel-LOCALE.mo 
  60. * - woocommerce-myparcel/languages/woocommerce-myparcel-LOCALE.mo (which if not found falls back to:) 
  61. * - WP_LANG_DIR/plugins/woocommerce-myparcel-LOCALE.mo 
  62. */ 
  63. load_textdomain( 'woocommerce-myparcel', $dir . 'woocommerce-myparcel/woocommerce-myparcel-' . $locale . '.mo' ); 
  64. load_textdomain( 'woocommerce-myparcel', $dir . 'plugins/woocommerce-myparcel-' . $locale . '.mo' ); 
  65. load_plugin_textdomain( 'woocommerce-myparcel', false, dirname( plugin_basename(__FILE__) ) . '/languages' ); 
  66.  
  67. /** 
  68. * Load the main plugin classes and functions 
  69. */ 
  70. public function includes() { 
  71. // include compatibility classes 
  72. include_once( 'includes/compatibility/abstract-wc-data-compatibility.php' ); 
  73. include_once( 'includes/compatibility/class-wc-date-compatibility.php' ); 
  74. include_once( 'includes/compatibility/class-wc-core-compatibility.php' ); 
  75. include_once( 'includes/compatibility/class-wc-order-compatibility.php' ); 
  76. include_once( 'includes/compatibility/class-wc-product-compatibility.php' ); 
  77.  
  78. include_once( 'includes/class-wcmp-assets.php' ); 
  79. $this->admin = include_once( 'includes/class-wcmp-admin.php' ); 
  80. include_once( 'includes/class-wcmp-frontend.php' ); 
  81. include_once( 'includes/class-wcmp-settings.php' ); 
  82. $this->export = include_once( 'includes/class-wcmp-export.php' ); 
  83. include_once( 'includes/class-wcmp-nlpostcode-fields.php' ); 
  84.  
  85. /** 
  86. * Instantiate classes when woocommerce is activated 
  87. */ 
  88. public function load_classes() { 
  89. if ( $this->is_woocommerce_activated() === false ) { 
  90. add_action( 'admin_notices', array ( $this, 'need_woocommerce' ) ); 
  91. return; 
  92.  
  93. if ( version_compare( PHP_VERSION, '5.3', '<' ) ) { 
  94. add_action( 'admin_notices', array ( $this, 'required_php_version' ) ); 
  95. return; 
  96.  
  97. // all systems ready - GO! 
  98. $this->includes(); 
  99.  
  100. /** 
  101. * Check if woocommerce is activated 
  102. */ 
  103. public function is_woocommerce_activated() { 
  104. $blog_plugins = get_option( 'active_plugins', array() ); 
  105. $site_plugins = get_site_option( 'active_sitewide_plugins', array() ); 
  106.  
  107. if ( in_array( 'woocommerce/woocommerce.php', $blog_plugins ) || isset( $site_plugins['woocommerce/woocommerce.php'] ) ) { 
  108. return true; 
  109. } else { 
  110. return false; 
  111.  
  112. /** 
  113. * WooCommerce not active notice. 
  114. * @return string Fallack notice. 
  115. */ 
  116.  
  117. public function need_woocommerce() { 
  118. $error = sprintf( __( 'WooCommerce MyParcel requires %sWooCommerce%s to be installed & activated!' , 'woocommerce-myparcel' ), '<a href="http://wordpress.org/extend/plugins/woocommerce/">', '</a>' ); 
  119.  
  120. $message = '<div class="error"><p>' . $error . '</p></div>'; 
  121.  
  122. echo $message; 
  123.  
  124. /** 
  125. * PHP version requirement notice 
  126. */ 
  127.  
  128. public function required_php_version() { 
  129. $error = __( 'WooCommerce MyParcel requires PHP 5.3 or higher (5.6 or later recommended).', 'woocommerce-myparcel' ); 
  130. $how_to_update = __( 'How to update your PHP version', 'woocommerce-myparcel' ); 
  131. $message = sprintf('<div class="error"><p>%s</p><p><a href="%s">%s</a></p></div>', $error, 'http://docs.wpovernight.com/general/how-to-update-your-php-version/', $how_to_update); 
  132.  
  133. echo $message; 
  134.  
  135. /** Lifecycle methods ******************************************************* 
  136. * Because register_activation_hook only runs when the plugin is manually 
  137. * activated by the user, we're checking the current version against the 
  138. * version stored in the database 
  139. ****************************************************************************/ 
  140.  
  141. /** 
  142. * Handles version checking 
  143. */ 
  144. public function do_install() { 
  145. $version_setting = 'woocommerce_myparcel_version'; 
  146. $installed_version = get_option( $version_setting ); 
  147.  
  148. // installed version lower than plugin version? 
  149. if ( version_compare( $installed_version, $this->version, '<' ) ) { 
  150.  
  151. if ( ! $installed_version ) { 
  152. $this->install(); 
  153. } else { 
  154. $this->upgrade( $installed_version ); 
  155.  
  156. // new version number 
  157. update_option( $version_setting, $this->version ); 
  158.  
  159.  
  160. /** 
  161. * Plugin install method. Perform any installation tasks here 
  162. */ 
  163. protected function install() { 
  164. // copy old settings if available (pre 2.0 didn't store the version, so technically, this is a new install) 
  165. $old_settings = get_option( 'wcmyparcel_settings' ); 
  166. if (!empty($old_settings)) { 
  167. // copy old settins to new 
  168. // Deprecated 
  169. // api_username 
  170. // pakjegemak 
  171. // pakjegemak_description 
  172. // pakjegemak_button 
  173. // shipment_type 
  174. // huishand 
  175.  
  176. // map old key => new_key 
  177. $general_settings_keys = array( 
  178. 'api_key' => 'api_key',  
  179. 'download_display' => 'download_display',  
  180. 'email_tracktrace' => 'email_tracktrace',  
  181. 'myaccount_tracktrace' => 'myaccount_tracktrace',  
  182. 'process' => 'process_directly',  
  183. 'keep_consignments' => 'keep_shipments',  
  184. 'error_logging' => 'error_logging',  
  185. ); 
  186.  
  187. $general_settings = array(); 
  188. foreach ($general_settings_keys as $old_key => $new_key) { 
  189. if (!empty($old_settings[$old_key])) { 
  190. $general_settings[$new_key] = $old_settings[$old_key]; 
  191. // auto_complete breaks down into: 
  192. // order_status_automation & automatic_order_status 
  193. if (!empty($old_settings['auto_complete'])) { 
  194. $general_settings['order_status_automation'] = 1; 
  195. $general_settings['automatic_order_status'] = 'completed'; 
  196.  
  197. // map old key => new_key 
  198. $defaults_settings_keys = array( 
  199. 'email' => 'connect_email',  
  200. 'telefoon' => 'connect_phone',  
  201. 'extragroot' => 'large_format',  
  202. 'huisadres' => 'only_recipient',  
  203. 'handtekening' => 'signature',  
  204. 'retourbgg' => 'return',  
  205. 'kenmerk' => 'label_description',  
  206. 'verpakkingsgewicht' => 'empty_parcel_weight',  
  207. 'verzekerd' => 'insured',  
  208. 'verzekerdbedrag' => 'insured_amount',  
  209. ); 
  210. $defaults_settings = array(); 
  211. foreach ($defaults_settings_keys as $old_key => $new_key) { 
  212. if (!empty($old_settings[$old_key])) { 
  213. $defaults_settings[$new_key] = $old_settings[$old_key]; 
  214. // set custom insurance amount 
  215. if (!empty($defaults_settings['insured']) && (int) $defaults_settings['insured_amount'] > 249) { 
  216. $defaults_settings['insured_amount'] = 0; 
  217. $defaults_settings['insured_amount_custom'] = $old_settings['verzekerdbedrag']; 
  218.  
  219. // add options 
  220. update_option( 'woocommerce_myparcel_general_settings', $general_settings ); 
  221. update_option( 'woocommerce_myparcel_export_defaults_settings', $defaults_settings ); 
  222.  
  223. /** 
  224. * Plugin upgrade method. Perform any required upgrades here 
  225. * @param string $installed_version the currently installed ('old') version 
  226. */ 
  227. protected function upgrade( $installed_version ) { 
  228. # stub 
  229. }  
  230.  
  231. /** 
  232. * Get the plugin url. 
  233. * @return string 
  234. */ 
  235. public function plugin_url() { 
  236. return untrailingslashit( plugins_url( '/', __FILE__ ) ); 
  237.  
  238. /** 
  239. * Get the plugin path. 
  240. * @return string 
  241. */ 
  242. public function plugin_path() { 
  243. return untrailingslashit( plugin_dir_path( __FILE__ ) ); 
  244.  
  245. } // class WooCommerce_MyParcel