/includes/admin/settings/class-wc-settings-price-based-country.php

  1. <?php 
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly 
  4.  
  5. if ( ! class_exists( 'WC_Settings_Price_Based_Country' ) ) : 
  6.  
  7. /** 
  8. * WC_Settings_Price_Based_Country 
  9. * 
  10. * WooCommerce Price Based Country settings page 
  11. *  
  12. * @version 1.5.2 
  13. * @author oscargare 
  14. */ 
  15. class WC_Settings_Price_Based_Country extends WC_Settings_Page { 
  16.  
  17. /** 
  18. * Constructor. 
  19. */ 
  20. public function __construct() { 
  21.  
  22. $this->id = 'price-based-country'; 
  23. $this->label = __( 'Price Based on Country', 'wc-price-based-country' ); 
  24.  
  25. add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); 
  26. add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) ); 
  27. add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );  
  28. add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );  
  29.  
  30. //table list row actions 
  31. self::regions_list_row_actions(); 
  32.  
  33. /** 
  34. * Get sections 
  35. * 
  36. * @return array 
  37. */ 
  38. public function get_sections() { 
  39. $sections = array( 
  40. '' => __( 'Settings', 'woocommerce' ),  
  41. 'regions' => __( 'Regions', 'wc-price-based-country' )  
  42. ); 
  43.  
  44. return apply_filters( 'wc_price_based_country_get_sections', $sections ); 
  45.  
  46. /** 
  47. * Get settings array 
  48. * 
  49. * @return array 
  50. */ 
  51. public function get_settings() { 
  52. $settings = apply_filters( 'wc_price_based_country_settings', array( 
  53. array( 
  54. 'title' => __( 'General Options', 'woocommerce' ),  
  55. 'type' => 'title',  
  56. 'desc' => '',  
  57. 'id' => 'general_options' 
  58. ),  
  59.  
  60. array( 
  61. 'title' => __( 'Price Based On', 'wc-price-based-country' ),  
  62. 'desc' => __( 'This controls which address is used to refresh products prices on checkout.' ),  
  63. 'id' => 'wc_price_based_country_based_on',  
  64. 'default' => 'billing',  
  65. 'type' => 'select',  
  66. 'class' => 'wc-enhanced-select',  
  67. 'desc_tip' => true,  
  68. 'options' => array( 
  69. 'billing' => __( 'Customer billing country', 'wc-price-based-country' ),  
  70. 'shipping' => __( 'Customer shipping country', 'wc-price-based-country' ) 
  71. ),  
  72.  
  73. array( 
  74. 'title' => __( 'Shipping', 'wc-price-based-country' ),  
  75. 'desc' => __( 'Enabled currency conversion to "Flat Rate" And "International Flat Rate"', 'wc-price-based-country' ),  
  76. 'id' => 'wc_price_based_shipping_conversion',  
  77. 'default' => 'no',  
  78. 'type' => 'checkbox'  
  79. ),  
  80.  
  81. array( 
  82. 'type' => 'sectionend',  
  83. 'id' => 'general_options' 
  84. ),  
  85.  
  86. array(  
  87. 'title' => __( 'Test Mode', 'wc-price-based-country' ),  
  88. 'type' => 'title',  
  89. 'desc' => 'If you want to check that prices are shown successfully, enable test mode and enter the Country which you want to do the test.',  
  90. 'id' => 'price_based_country_test' 
  91. ),  
  92.  
  93. array( 
  94. 'title' => __( 'Enabled/Disabled', 'wc-price-based-country' ),  
  95. 'desc' => __( 'Enabled Test Mode', 'wc-price-based-country' ),  
  96. 'id' => 'wc_price_based_country_test_mode',  
  97. 'default' => 'no',  
  98. 'type' => 'checkbox',  
  99. 'desc_tip' => __('If test mode is enabled, a demo store notice will be displayed .') 
  100. ),  
  101.  
  102. array( 
  103. 'title' => __( 'Test country', 'wc-price-based-country' ),  
  104. 'id' => 'wc_price_based_country_test_country',  
  105. 'default' => wc_get_base_location(),  
  106. 'type' => 'select',  
  107. 'class' => 'chosen_select',  
  108. 'options' => WC()->countries->countries 
  109. ),  
  110.  
  111. array( 'type' => 'sectionend', 'id' => 'price_based_country_test' ) 
  112. )); 
  113.  
  114. return $settings; 
  115.  
  116. /** 
  117. * Output the settings 
  118. */ 
  119. public function output() { 
  120. global $current_section; 
  121.  
  122. self::display_donate_notice(); //display de donate notice 
  123.  
  124. if ( 'regions' == $current_section ) {  
  125. self::regions_output(); 
  126. } else { 
  127. $settings = $this->get_settings( $current_section ); 
  128. WC_Admin_Settings::output_fields( $settings ); 
  129.  
  130. /** 
  131. * Save settings 
  132. */ 
  133. public function save() { 
  134. global $current_section; 
  135.  
  136. if( $current_section == 'regions' && ( isset( $_GET['edit_region'] ) || isset( $_GET['add_region'] ) ) ) {  
  137. self::regions_save(); 
  138.  
  139. } elseif( $current_section == 'regions' && isset( $_POST['action'] ) && $_POST['action'] == 'remove' && isset( $_POST['region_key'] ) ) { 
  140. self::regions_delete_bulk(); 
  141.  
  142. } elseif( $current_section !== 'regions' ) {  
  143. //save settings  
  144. $settings = $this->get_settings(); 
  145. WC_Admin_Settings::save_fields( $settings );  
  146.  
  147. update_option( 'wc_price_based_country_timestamp', time() );  
  148. }  
  149.  
  150. /** 
  151. * Display donate notices 
  152. */ 
  153. private static function display_donate_notice() { 
  154.  
  155. if ( get_option('wc_price_based_country_hide_ads', 'no') == 'no' ) { 
  156.  
  157. global $pagenow;  
  158.  
  159. if ( isset( $_GET['wc_price_based_country_donate_hide'] ) && $_GET['wc_price_based_country_donate_hide'] == 'true' ) { 
  160. update_option('wc_price_based_country_hide_ads', 'yes'); 
  161. } else { 
  162. ?> 
  163. <div class="updated"> 
  164. <p><strong>Donate to Price Based Country</strong></p> 
  165. <p><?php _e('It is difficult to provide, support, and maintain free software. Every little bit helps is greatly appreciated!', 'wc-price-based-country') ; ?></p> 
  166. <p class="submit"> 
  167. <a class="button-primary" target="_blank" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=NG75SHRLAX28L"><?php _e( 'Donate now', 'woocommerce' ); ?></a> 
  168. <a class="skip button-secondary" href="<?php echo esc_url( add_query_arg( 'wc_price_based_country_donate_hide', 'true', admin_url( 'admin.php?page=wc-settings&tab=price-based-country' ) ) ); ?>">Don't show me again</a> 
  169. </p> 
  170. </div> 
  171. <?php  
  172. }  
  173.  
  174. /** 
  175. * Regions Page output 
  176. */ 
  177. private static function regions_output() { 
  178. // Hide the save button 
  179. $GLOBALS['hide_save_button'] = true; 
  180.  
  181. if ( isset( $_GET['add_region'] ) || isset( $_GET['edit_region'] ) ) { 
  182. $region_key = isset( $_GET['edit_region'] ) ? $_GET['edit_region'] : NULL; 
  183. $region = self::get_regions_data( $region_key); 
  184. $allowed_countries = self::get_allowed_countries( $region_key ); 
  185. include( 'views/html-regions-edit.php' ); 
  186. } else { 
  187. self::regions_table_list_output();  
  188. }  
  189.  
  190. /** 
  191. * Regions table list output 
  192. */ 
  193. private static function regions_table_list_output() { 
  194.  
  195. include_once( WCPBC()->plugin_path() . 'includes/admin/class-wcpbc-admin-regions-table-list.php' ); 
  196.  
  197. echo '<h3>' . __( 'Regions', 'wc-price-based-country' ) . ' <a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=price-based-country§ion=regions&add_region=1' ) ) . '" class="add-new-h2">' . __( 'Add Region', 'wc-price-based-country' ) . '</a></h3>'; 
  198.  
  199. $keys_table_list = new WCPBC_Admin_Regions_Table_List(); 
  200. $keys_table_list->prepare_items(); 
  201. $keys_table_list->views();  
  202. $keys_table_list->display(); 
  203.  
  204. /** 
  205. * Get region data 
  206. * 
  207. * @param string $key 
  208. * @return array 
  209. */ 
  210. private static function get_regions_data( $key, $values = FALSE ) {  
  211.  
  212. $region = apply_filters( 'wc_price_based_country_default_region_data', array( 
  213. 'name' => '',  
  214. 'countries' => array(),  
  215. 'currency' => get_option('woocommerce_currency'),  
  216. 'empty_price_method' => '',  
  217. 'exchange_rate' => '1' 
  218. )); 
  219.  
  220. $regions = get_option( 'wc_price_based_country_regions', array() );  
  221.  
  222. if ( array_key_exists($key, $regions) ) { 
  223. $region = wp_parse_args( $regions[$key], $region ); 
  224.  
  225. if ( is_array($values) ) { 
  226. $region = array_intersect_key( $values, $region);  
  227. $region['exchange_rate'] = isset( $region['exchange_rate'] ) ? wc_format_decimal($region['exchange_rate']) : 0; 
  228.  
  229. return $region; 
  230. }  
  231.  
  232. /** 
  233. * Get allowed countries 
  234. * 
  235. * @param string $selected_key 
  236. * @return array 
  237. */ 
  238. private static function get_allowed_countries( $selected_key ) {  
  239.  
  240. $regions = get_option( 'wc_price_based_country_regions', array() );  
  241. $countries_in_regions = array();  
  242.  
  243. foreach ( $regions as $key => $region) { 
  244. if ( $key !== $selected_key ) { 
  245. $countries_in_regions = array_merge( $region['countries'], $countries_in_regions ); 
  246. }  
  247.  
  248. if ( 'specific' === get_option('woocommerce_allowed_countries') ) { 
  249. $allowed_countries = array_diff( get_option('woocommerce_specific_allowed_countries'), $countries_in_regions ); 
  250. } else { 
  251. $allowed_countries = array_diff( array_keys( WC()->countries->countries ), $countries_in_regions ); 
  252.  
  253. return $allowed_countries; 
  254.  
  255. /** 
  256. * Get a unique slug that indentify a region 
  257. * 
  258. * @param string $new_slug 
  259. * @param array $slugs 
  260. * @return array 
  261. */ 
  262. private static function get_unique_slug( $new_slug, $slugs ) {  
  263.  
  264. $seqs = array(); 
  265.  
  266. foreach ( $slugs as $slug ) { 
  267. $slug_parts = explode( '-', $slug, 2 ); 
  268. if ( $slug_parts[0] == $new_slug && ( count( $slug_parts ) == 1 || is_numeric( $slug_parts[1] ) ) ) { 
  269. $seqs[] = isset( $slug_parts[1] ) ? $slug_parts[1] : 0; 
  270. }  
  271. }  
  272.  
  273. if ($seqs ) { 
  274. rsort($seqs);  
  275. $new_slug = $new_slug .'-' . ( $seqs[0]+1 );  
  276.  
  277. return $new_slug; 
  278.  
  279. /** 
  280. * Validate region data 
  281. * @param array $fields 
  282. * @return boolean 
  283. */ 
  284. private static function validate_region_fields( $fields ) { 
  285.  
  286. $valid = false; 
  287.  
  288. if ( empty( $fields['name'] ) ) { 
  289. WC_Admin_Settings::add_error( __( 'Group name is required.', 'wc-price-based-country' ) ); 
  290.  
  291. } elseif ( ! isset($fields['countries']) || empty( $fields['countries'] ) ) { 
  292. WC_Admin_Settings::add_error( __( 'Add at least one country to the list.', 'wc-price-based-country' ) ); 
  293.  
  294. } elseif ( empty( $fields['exchange_rate'] ) || $fields['exchange_rate'] == 0 ) {  
  295. WC_Admin_Settings::add_error( __( 'Exchange rate must be nonzero.', 'wc-price-based-country' ) ); 
  296.  
  297. } else { 
  298. $valid = true; 
  299.  
  300. return apply_filters( 'wc_price_based_country_admin_region_fields_validate', $valid, $fields ); 
  301.  
  302. /** 
  303. * Save region 
  304. */ 
  305. private static function regions_save() {  
  306.  
  307. $region_key = isset( $_GET['edit_region'] ) ? wc_clean( $_GET['edit_region'] ) : NULL; 
  308.  
  309. $region = self::get_regions_data($region_key, $_POST ); 
  310.  
  311. if ( self::validate_region_fields( $region ) ) { 
  312.  
  313. $regions = get_option( 'wc_price_based_country_regions', array() );  
  314.  
  315. if (is_null($region_key)) { 
  316. $region_key = self::get_unique_slug( sanitize_title( $region['name']), array_keys( $regions ) ); 
  317. $regions[$region_key] = $region; 
  318.  
  319. update_option( 'wc_price_based_country_regions', $regions );  
  320.  
  321. update_option( 'wc_price_based_country_timestamp', time() ); 
  322.  
  323. $_GET['edit_region'] = $region_key; 
  324. }  
  325.  
  326.  
  327. /** 
  328. * Regions table list row actions 
  329. */ 
  330. private static function regions_list_row_actions() { 
  331. if ( isset( $_GET['remove_region'] ) &&  
  332. isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] &&  
  333. isset( $_GET['tab'] ) && 'price-based-country' == $_GET['tab'] &&  
  334. isset( $_GET['section'] ) && 'regions' == isset( $_GET['section'] )  
  335. ) { 
  336.  
  337. self::regions_delete();  
  338.  
  339. /** 
  340. * Delete region 
  341. */ 
  342. private static function regions_delete() { 
  343.  
  344. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'wc-price-based-country-remove-region' ) ) { 
  345. wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); 
  346.  
  347. $region_key = wc_clean( $_GET['remove_region'] );  
  348. $regions = get_option( 'wc_price_based_country_regions', array() );  
  349.  
  350. if ( isset($regions[$region_key]) ) {  
  351.  
  352. unset($regions[$region_key]);  
  353. self::regions_delete_post_meta($region_key); 
  354.  
  355. update_option( 'wc_price_based_country_regions', $regions );  
  356. update_option( 'wc_price_based_country_timestamp', time() ); 
  357.  
  358. WC_Admin_Settings::add_message( __( 'Region have been deleted.', 'wc-price-based-country' ) ); 
  359. }  
  360.  
  361. /** 
  362. * Bulk delete regions 
  363. */ 
  364. private static function regions_delete_bulk() { 
  365. if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) { 
  366. wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); 
  367.  
  368. $region_keys = wc_clean( $_POST['region_key'] ); 
  369. $regions = get_option( 'wc_price_based_country_regions', array() );  
  370.  
  371. foreach ($region_keys as $region_key) {  
  372. if ( isset( $regions[$region_key] ) ) {  
  373. unset($regions[$region_key]); 
  374. self::regions_delete_post_meta($region_key); 
  375. }  
  376. }  
  377.  
  378. update_option( 'wc_price_based_country_regions', $regions ); 
  379. update_option( 'wc_price_based_country_timestamp', time() );  
  380.  
  381. /** 
  382. * Delete postmeta data  
  383. */ 
  384. private static function regions_delete_post_meta( $region_key ) { 
  385. global $wpdb; 
  386. foreach ( wcpbc_get_product_meta_keys( $region_key ) as $meta_key ) { 
  387. $wpdb->delete( $wpdb->postmeta, array( 'meta_key' => $meta_key ) );  
  388. }  
  389.  
  390. endif; 
  391.  
  392. return new WC_Settings_Price_Based_Country(); 
.