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