WC_GZD_Install

Installation related functions and hooks.

Defined (1)

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

/includes/class-wc-gzd-install.php  
  1. class WC_GZD_Install { 
  2.  
  3. /** @var array DB updates that need to be run */ 
  4. private static $db_updates = array( 
  5. '1.0.4' => 'updates/woocommerce-gzd-update-1.0.4.php',  
  6. '1.4.2' => 'updates/woocommerce-gzd-update-1.4.2.php',  
  7. '1.4.6' => 'updates/woocommerce-gzd-update-1.4.6.php',  
  8. '1.5.0' => 'updates/woocommerce-gzd-update-1.5.0.php',  
  9. '1.6.0' => 'updates/woocommerce-gzd-update-1.6.0.php',  
  10. '1.6.3' => 'updates/woocommerce-gzd-update-1.6.3.php',  
  11. '1.8.0' => 'updates/woocommerce-gzd-update-1.8.0.php',  
  12. '1.8.9' => 'updates/woocommerce-gzd-update-1.8.9.php' 
  13. ); 
  14.  
  15. /** 
  16. * Hook in tabs. 
  17. */ 
  18. public function __construct() { 
  19. add_action( 'admin_init', array( __CLASS__, 'check_version' ), 10 ); 
  20. add_action( 'admin_init', array( __CLASS__, 'install_actions' ) ); 
  21. add_action( 'in_plugin_update_message-woocommerce-germanized/woocommerce-germanized.php', array( __CLASS__, 'in_plugin_update_message' ) ); 
  22.  
  23. /** 
  24. * check_version function. 
  25. * @access public 
  26. * @return void 
  27. */ 
  28. public static function check_version() { 
  29. if ( ! defined( 'IFRAME_REQUEST' ) && ( get_option( 'woocommerce_gzd_version' ) != WC_germanized()->version ) ) { 
  30. self::install(); 
  31. do_action( 'woocommerce_gzd_updated' ); 
  32.  
  33. /** 
  34. * Install actions such as installing pages when a button is clicked. 
  35. */ 
  36. public static function install_actions() { 
  37. // Install - Add pages button 
  38. if ( ! empty( $_GET['install_woocommerce_gzd'] ) ) { 
  39.  
  40. if ( ! empty( $_GET['install_woocommerce_gzd_pages'] ) ) 
  41. self::create_pages(); 
  42.  
  43. if ( ! empty( $_GET['install_woocommerce_gzd_settings'] ) ) 
  44. self::set_default_settings(); 
  45.  
  46. if ( ! empty( $_GET['install_woocommerce_gzd_tax_rates'] ) ) 
  47. self::create_tax_rates(); 
  48.  
  49. // We no longer need to install pages 
  50. delete_option( '_wc_gzd_needs_pages' ); 
  51. delete_transient( '_wc_gzd_activation_redirect' ); 
  52.  
  53. // What's new redirect 
  54. wp_redirect( admin_url( 'index.php?page=wc-gzd-about&wc-gzd-installed=true' ) ); 
  55. exit; 
  56.  
  57. // Skip button 
  58. } elseif ( ! empty( $_GET['skip_install_woocommerce_gzd'] ) ) { 
  59.  
  60. // We no longer need to install pages 
  61. delete_option( '_wc_gzd_needs_pages' ); 
  62. delete_transient( '_wc_gzd_activation_redirect' ); 
  63.  
  64. // What's new redirect 
  65. wp_redirect( admin_url( 'index.php?page=wc-gzd-about' ) ); 
  66. exit; 
  67.  
  68. // Update button 
  69. } elseif ( ! empty( $_GET['do_update_woocommerce_gzd'] ) ) { 
  70.  
  71. self::update(); 
  72.  
  73. // Update complete 
  74. delete_option( '_wc_gzd_needs_pages' ); 
  75. delete_option( '_wc_gzd_needs_update' ); 
  76. delete_transient( '_wc_gzd_activation_redirect' ); 
  77.  
  78. // What's new redirect 
  79. wp_redirect( admin_url( 'index.php?page=wc-gzd-about&wc-gzd-updated=true' ) ); 
  80. exit; 
  81.  
  82.  
  83.  
  84. /** 
  85. * Install WC_Germanized 
  86. */ 
  87. public static function install() { 
  88. global $wpdb; 
  89.  
  90. if ( ! defined( 'WC_GZD_INSTALLING' ) ) { 
  91. define( 'WC_GZD_INSTALLING', true ); 
  92.  
  93. // Load Translation for default options 
  94. $locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-germanized' ); 
  95. $mofile = WC_germanized()->plugin_path() . '/i18n/languages/woocommerce-germanized.mo'; 
  96.  
  97. if ( file_exists( WC_germanized()->plugin_path() . '/i18n/languages/woocommerce-germanized-' . $locale . '.mo' ) ) 
  98. $mofile = WC_germanized()->plugin_path() . '/i18n/languages/woocommerce-germanized-' . $locale . '.mo'; 
  99.  
  100. load_textdomain( 'woocommerce-germanized', $mofile ); 
  101.  
  102. if ( ! wc_gzd_get_dependencies()->is_woocommerce_activated() ) { 
  103. deactivate_plugins( WC_GERMANIZED_PLUGIN_FILE ); 
  104. wp_die( sprintf( __( 'Please install <a href="%s" target="_blank">WooCommerce</a> before installing WooCommerce Germanized. Thank you!', 'woocommerce-germanized' ), 'http://wordpress.org/plugins/woocommerce/' ) ); 
  105.  
  106. // Register post types 
  107. include_once( 'class-wc-gzd-post-types.php' ); 
  108. WC_GZD_Post_types::register_taxonomies(); 
  109.  
  110. self::create_cron_jobs(); 
  111. self::create_units(); 
  112. self::create_labels(); 
  113.  
  114. self::create_options(); 
  115.  
  116. // Virtual Tax Classes 
  117. $tax_classes = array_filter( array_map( 'trim', explode( "\n", get_option('woocommerce_tax_classes' ) ) ) ); 
  118.  
  119. if ( ! in_array( 'Virtual Rate', $tax_classes ) || ! in_array( 'Virtual Reduced Rate', $tax_classes ) ) { 
  120.  
  121. update_option( '_wc_gzd_needs_pages', 1 ); 
  122.  
  123. if ( ! in_array( 'Virtual Rate', $tax_classes ) ) 
  124. array_push( $tax_classes, 'Virtual Rate' ); 
  125.  
  126. if ( ! in_array( 'Virtual Reduced Rate', $tax_classes ) ) 
  127. array_push( $tax_classes, 'Virtual Reduced Rate' ); 
  128.  
  129. update_option( 'woocommerce_tax_classes', implode( "\n", $tax_classes ) ); 
  130.  
  131. // Delete plugin header data for dependency check 
  132. delete_option( 'woocommerce_gzd_plugin_header_data' ); 
  133.  
  134. // Queue upgrades 
  135. $current_version = get_option( 'woocommerce_gzd_version', null ); 
  136. $current_db_version = get_option( 'woocommerce_gzd_db_version', null ); 
  137.  
  138. // Queue messages and notices 
  139. if ( ! is_null( $current_version ) ) { 
  140.  
  141. // Show tour for new installs only 
  142. update_option( 'woocommerce_gzd_hide_tour', 1 ); 
  143.  
  144. $major_version = substr( $current_version, 0, 3 ); 
  145. $new_major_version = substr( WC_germanized()->version, 0, 3 ); 
  146.  
  147. // Only on major update 
  148. if ( version_compare( $new_major_version, $major_version, ">" ) ) { 
  149. delete_option( '_wc_gzd_hide_theme_notice' ); 
  150. delete_option( '_wc_gzd_hide_pro_notice' ); 
  151.  
  152. } else { 
  153.  
  154. // Fresh install - Check if some german market plugin was installed before 
  155. if ( WC_GZD_Admin_Importer::instance()->is_available() ) 
  156. update_option( '_wc_gzd_import_available', 1 ); 
  157.  
  158.  
  159. if ( ! is_null( $current_db_version ) && version_compare( $current_db_version, max( array_keys( self::$db_updates ) ), '<' ) ) { 
  160. // Update 
  161. update_option( '_wc_gzd_needs_update', 1 ); 
  162. } else { 
  163. self::update_db_version(); 
  164.  
  165. self::update_wc_gzd_version(); 
  166.  
  167. // Update activation date 
  168. update_option( 'woocommerce_gzd_activation_date', date( 'Y-m-d' ) ); 
  169.  
  170. // Add theme compatibility check 
  171. delete_option( '_wc_gzd_hide_review_notice' ); 
  172.  
  173. // Check if pages are needed 
  174. if ( wc_get_page_id( 'revocation' ) < 1 ) { 
  175. update_option( '_wc_gzd_needs_pages', 1 ); 
  176.  
  177. // Flush rules after install 
  178. flush_rewrite_rules(); 
  179.  
  180. // Upon install + update 
  181. do_action( 'woocommerce_gzd_installed' ); 
  182.  
  183. // Prevent redirect for inline plugin updates 
  184. if ( ! defined( 'DOING_AJAX' ) ) { 
  185. // Redirect to welcome screen 
  186. set_transient( '_wc_gzd_activation_redirect', 1, 60 * 60 ); 
  187.  
  188. /** 
  189. * Update WC version to current 
  190. */ 
  191. private static function update_wc_gzd_version() { 
  192. delete_option( 'woocommerce_gzd_version' ); 
  193. add_option( 'woocommerce_gzd_version', WC_germanized()->version ); 
  194.  
  195. /** 
  196. * Update DB version to current 
  197. */ 
  198. private static function update_db_version( $version = null ) { 
  199. delete_option( 'woocommerce_gzd_db_version' ); 
  200. add_option( 'woocommerce_gzd_db_version', is_null( $version ) ? WC_germanized()->version : $version ); 
  201.  
  202. /** 
  203. * Handle updates 
  204. */ 
  205. private static function update() { 
  206. $current_db_version = get_option( 'woocommerce_gzd_db_version' ); 
  207.  
  208. foreach ( self::$db_updates as $version => $updater ) { 
  209. if ( version_compare( $current_db_version, $version, '<' ) ) { 
  210. include( $updater ); 
  211. self::update_db_version( $version ); 
  212.  
  213. self::update_db_version(); 
  214.  
  215. /** 
  216. * Show plugin changes. Code adapted from W3 Total Cache. 
  217. */ 
  218. public static function in_plugin_update_message( $args ) { 
  219. $transient_name = 'wc_gzd_upgrade_notice_' . $args['Version']; 
  220.  
  221. if ( false === ( $upgrade_notice = get_transient( $transient_name ) ) ) { 
  222. $response = wp_safe_remote_get( 'https://plugins.svn.wordpress.org/woocommerce-germanized/trunk/readme.txt' ); 
  223.  
  224. if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) { 
  225. $upgrade_notice = self::parse_update_notice( $response['body'] ); 
  226. set_transient( $transient_name, $upgrade_notice, DAY_IN_SECONDS ); 
  227.  
  228. echo wp_kses_post( $upgrade_notice ); 
  229.  
  230. /** 
  231. * Parse update notice from readme file 
  232. * @param string $content 
  233. * @return string 
  234. */ 
  235. private static function parse_update_notice( $content ) { 
  236. // Output Upgrade Notice 
  237. $matches = null; 
  238. $regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(=\s*' . preg_quote( WC_GERMANIZED_VERSION ) . '\s*=|$)~Uis'; 
  239. $upgrade_notice = ''; 
  240.  
  241. if ( preg_match( $regexp, $content, $matches ) ) { 
  242. $version = trim( $matches[1] ); 
  243. $notices = (array) preg_split('~[\r\n]+~', trim( $matches[2] ) ); 
  244.  
  245. if ( version_compare( WC_GERMANIZED_VERSION, $version, '<' ) ) { 
  246.  
  247. $upgrade_notice .= '<div class="wc_plugin_upgrade_notice">'; 
  248.  
  249. foreach ( $notices as $index => $line ) { 
  250. $upgrade_notice .= wp_kses_post( preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $line ) ); 
  251.  
  252. $upgrade_notice .= '</div> '; 
  253.  
  254. return wp_kses_post( $upgrade_notice ); 
  255.  
  256. /** 
  257. * Create cron jobs (clear them first) 
  258. */ 
  259. private static function create_cron_jobs() { 
  260. // Cron jobs 
  261. wp_clear_scheduled_hook( 'woocommerce_gzd_customer_cleanup' ); 
  262. wp_schedule_event( time(), 'daily', 'woocommerce_gzd_customer_cleanup' ); 
  263.  
  264. wp_clear_scheduled_hook( 'woocommerce_gzd_trusted_shops_reviews' ); 
  265. wp_schedule_event( time(), 'twicedaily', 'woocommerce_gzd_trusted_shops_reviews' ); 
  266.  
  267. wp_clear_scheduled_hook( 'woocommerce_gzd_ekomi' ); 
  268. wp_schedule_event( time(), 'daily', 'woocommerce_gzd_ekomi' ); 
  269.  
  270. public static function create_units() { 
  271. $units = include( WC_Germanized()->plugin_path() . '/i18n/units.php' ); 
  272. if ( ! empty( $units ) ) { 
  273. foreach ( $units as $slug => $unit ) { 
  274. wp_insert_term( $unit, 'product_unit', array( 'slug' => $slug ) ); 
  275.  
  276. public static function create_labels() { 
  277. $labels = include( WC_Germanized()->plugin_path() . '/i18n/labels.php' ); 
  278. if ( ! empty( $labels ) ) { 
  279. foreach ( $labels as $slug => $unit ) 
  280. wp_insert_term( $unit, 'product_price_label', array( 'slug' => $slug ) ); 
  281.  
  282. public static function create_tax_rates() { 
  283.  
  284. global $wpdb; 
  285.  
  286. // Delete digital rates 
  287. $wpdb->delete( $wpdb->prefix . 'woocommerce_tax_rates', array( 'tax_rate_class' => 'virtual-rate' ), array( '%s' ) ); 
  288.  
  289. $rates = array( 
  290. 'BE' => 21,  
  291. 'BG' => 20,  
  292. 'CZ' => 21,  
  293. 'DK' => 25,  
  294. 'DE' => 19,  
  295. 'EE' => 20,  
  296. 'GR' => 23,  
  297. 'ES' => 21,  
  298. 'FR' => 20,  
  299. 'HR' => 25,  
  300. 'IE' => 23,  
  301. 'IT' => 22,  
  302. 'CY' => 19,  
  303. 'LV' => 21,  
  304. 'LT' => 21,  
  305. 'LU' => 17,  
  306. 'HU' => 27,  
  307. 'MT' => 18,  
  308. 'NL' => 21,  
  309. 'AT' => 20,  
  310. 'PL' => 23,  
  311. 'PT' => 23,  
  312. 'RO' => 19,  
  313. 'SI' => 22,  
  314. 'SK' => 20,  
  315. 'FI' => 24,  
  316. 'SE' => 25,  
  317. 'GB' => 20,  
  318. ); 
  319.  
  320. if ( ! empty( $rates ) ) { 
  321. $count = 0; 
  322. foreach ( $rates as $iso => $rate ) { 
  323. $_tax_rate = array( 
  324. 'tax_rate_country' => $iso,  
  325. 'tax_rate_state' => '',  
  326. 'tax_rate' => (string) number_format( (double) wc_clean( $rate ), 4, '.', '' ),  
  327. 'tax_rate_name' => 'MwSt. ' . $iso . ' virtual',  
  328. 'tax_rate_priority' => 1,  
  329. 'tax_rate_compound' => 0,  
  330. 'tax_rate_shipping' => 0,  
  331. 'tax_rate_order' => $count++,  
  332. 'tax_rate_class' => 'virtual-rate' 
  333. ); 
  334. // Check if standard rate exists 
  335. if ( WC()->countries->get_base_country() == $iso ) { 
  336. $base_rate = WC_Tax::get_shop_base_rate(); 
  337. $base_rate = reset( $base_rate ); 
  338. if ( ! empty( $base_rate ) ) 
  339. $_tax_rate[ 'tax_rate_name' ] = $base_rate[ 'label' ]; 
  340. $wpdb->insert( $wpdb->prefix . 'woocommerce_tax_rates', $_tax_rate ); 
  341. $tax_rate_id = $wpdb->insert_id; 
  342. do_action( 'woocommerce_tax_rate_added', $tax_rate_id, $_tax_rate ); 
  343. // Clear tax transients 
  344. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}options WHERE option_name LIKE %s;", '_transient_wc_tax_rates%' ) ); 
  345. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}options WHERE option_name LIKE %s;", '_transient_timeout_wc_tax_rates%' ) ); 
  346.  
  347. /** 
  348. * Updates WooCommerce Options if user chooses to automatically adapt german options 
  349. */ 
  350. public static function set_default_settings() { 
  351. global $wpdb; 
  352.  
  353. $options = array( 
  354. 'woocommerce_default_country' => 'DE',  
  355. 'woocommerce_currency' => 'EUR',  
  356. 'woocommerce_currency_pos' => 'right_space',  
  357. 'woocommerce_price_thousand_sep' => '.',  
  358. 'woocommerce_price_decimal_sep' => ', ',  
  359. 'woocommerce_price_num_decimals' => 2,  
  360. 'woocommerce_weight_unit' => 'kg',  
  361. 'woocommerce_dimension_unit' => 'cm',  
  362. 'woocommerce_calc_taxes' => 'yes',  
  363. 'woocommerce_prices_include_tax' => 'yes',  
  364. 'woocommerce_tax_display_cart' => 'incl',  
  365. 'woocommerce_tax_display_shop' => 'incl',  
  366. 'woocommerce_tax_total_display' => 'itemized',  
  367. 'woocommerce_allowed_countries' => 'specific',  
  368. 'woocommerce_specific_allowed_countries' => array( 'DE' ),  
  369. ); 
  370. if ( ! empty($options ) ) { 
  371. foreach ( $options as $key => $option ) { 
  372. update_option( $key, $option ); 
  373. // Tax Rates 
  374. $_tax_rate = array( 
  375. 'tax_rate_country' => 'DE',  
  376. 'tax_rate_state' => '',  
  377. 'tax_rate' => number_format( (double) wc_clean( 19.0 ), 4, '.', '' ),  
  378. 'tax_rate_name' => 'MwSt.',  
  379. 'tax_rate_priority' => 1,  
  380. 'tax_rate_compound' => '',  
  381. 'tax_rate_shipping' => '1',  
  382. 'tax_rate_order' => 1,  
  383. 'tax_rate_class' => '' 
  384. ); 
  385. $exists = $wpdb->get_results ( 'SELECT tax_rate_id FROM ' . $wpdb->prefix . 'woocommerce_tax_rates' . ' WHERE tax_rate LIKE "19%"' ); 
  386. if ( empty( $exists ) ) 
  387. $wpdb->insert( $wpdb->prefix . 'woocommerce_tax_rates', $_tax_rate ); 
  388.  
  389. $_tax_rate[ 'tax_rate' ] = number_format( (double) wc_clean( 7.0 ), 4, '.', '' ); 
  390. $_tax_rate[ 'tax_rate_class' ] = 'reduced-rate'; 
  391. $_tax_rate[ 'tax_rate_name' ] = 'MwSt. 7%'; 
  392.  
  393. $exists = $wpdb->get_results ( 'SELECT tax_rate_id FROM ' . $wpdb->prefix . 'woocommerce_tax_rates' . ' WHERE tax_rate LIKE "7%"' ); 
  394. if ( empty( $exists ) ) 
  395. $wpdb->insert( $wpdb->prefix . 'woocommerce_tax_rates', $_tax_rate ); 
  396.  
  397. // Clear tax transients 
  398. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}options WHERE option_name LIKE %s;", '_transient_wc_tax_rates%' ) ); 
  399. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}options WHERE option_name LIKE %s;", '_transient_timeout_wc_tax_rates%' ) ); 
  400.  
  401. /** 
  402. * Create pages that the plugin relies on, storing page id's in variables. 
  403. * @access public 
  404. * @return void 
  405. */ 
  406. public static function create_pages() { 
  407.  
  408. if ( ! function_exists( 'wc_create_page' ) ) 
  409. include_once( WC()->plugin_path() . '/includes/admin/wc-admin-functions.php' ); 
  410.  
  411. $pages = apply_filters( 'woocommerce_gzd_create_pages', array( 
  412. 'data_security' => array( 
  413. 'name' => _x( 'data-security', 'Page slug', 'woocommerce-germanized' ),  
  414. 'title' => _x( 'Data Security Statement', 'Page title', 'woocommerce-germanized' ),  
  415. 'content' => '' 
  416. ),  
  417. 'imprint' => array( 
  418. 'name' => _x( 'imprint', 'Page slug', 'woocommerce-germanized' ),  
  419. 'title' => _x( 'Imprint', 'Page title', 'woocommerce-germanized' ),  
  420. 'content' => '[gzd_complaints]' 
  421. ),  
  422. 'terms' => array( 
  423. 'name' => _x( 'terms', 'Page slug', 'woocommerce-germanized' ),  
  424. 'title' => _x( 'Terms & Conditions', 'Page title', 'woocommerce-germanized' ),  
  425. 'content' => '' 
  426. ),  
  427. 'revocation' => array( 
  428. 'name' => _x( 'revocation', 'Page slug', 'woocommerce-germanized' ),  
  429. 'title' => _x( 'Power of Revocation', 'Page title', 'woocommerce-germanized' ),  
  430. 'content' => '' 
  431. ),  
  432. 'shipping_costs' => array( 
  433. 'name' => _x( 'shipping-methods', 'Page slug', 'woocommerce-germanized' ),  
  434. 'title' => _x( 'Shipping Methods', 'Page title', 'woocommerce-germanized' ),  
  435. 'content' => '' 
  436. ),  
  437. 'payment_methods' => array( 
  438. 'name' => _x( 'payment-methods', 'Page slug', 'woocommerce-germanized' ),  
  439. 'title' => _x( 'Payment Methods', 'Page title', 'woocommerce-germanized' ),  
  440. 'content' => '[' . apply_filters( 'woocommerce_gzd_payment_methods_shortcode_tag', 'payment_methods_info' ) . ']' 
  441. ),  
  442. ) ); 
  443.  
  444. foreach ( $pages as $key => $page ) { 
  445. wc_create_page( esc_sql( $page['name'] ), 'woocommerce_' . $key . '_page_id', $page['title'], $page['content'], ! empty( $page['parent'] ) ? wc_get_page_id( $page['parent'] ) : '' ); 
  446.  
  447.  
  448. /** 
  449. * Default options 
  450. * Sets up the default options used on the settings page 
  451. * @access public 
  452. */ 
  453. public static function create_options() { 
  454.  
  455. // Include settings so that we can run through defaults 
  456. include_once( WC()->plugin_path() . '/includes/admin/settings/class-wc-settings-page.php' ); 
  457. include_once( 'admin/settings/class-wc-gzd-settings-germanized.php' ); 
  458.  
  459. $settings = new WC_GZD_Settings_Germanized(); 
  460. $options = apply_filters( 'woocommerce_gzd_installation_default_settings', array_merge( $settings->get_settings(), $settings->get_display_settings(), $settings->get_email_settings() ) ); 
  461.  
  462. foreach ( $options as $value ) { 
  463. if ( isset( $value['default'] ) && isset( $value['id'] ) ) { 
  464. $autoload = isset( $value['autoload'] ) ? (bool) $value['autoload'] : true; 
  465. add_option( $value['id'], $value['default'], '', ( $autoload ? 'yes' : 'no' ) ); 
  466.  
  467.