/includes/class-wc-gzd-install.php

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