WC_Settings_Price_Based_Country

WC_Settings_Price_Based_Country.

Defined (1)

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

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