/settings/class-wc-custom-currencies.php

  1. <?php 
  2. /** 
  3. * WooCommerce General Settings 
  4. * 
  5. * @author WooThemes 
  6. * @category Admin 
  7. * @package WooCommerce/Admin 
  8. * @version 2.1.0 
  9. */ 
  10.  
  11. if ( ! defined( 'ABSPATH' ) ) { 
  12. exit; // Exit if accessed directly 
  13.  
  14. if ( ! class_exists( 'WC_Custom_Currencies_Settings' ) ) : 
  15.  
  16. /** 
  17. * WC_Admin_Settings_General 
  18. */ 
  19. class WC_Custom_Currencies_Settings extends WC_Settings_Page { 
  20.  
  21. /** 
  22. * For PayPal 
  23. */ 
  24. public $supported_currencies; 
  25.  
  26. /** 
  27. * PayPal currencies but for use in select boxes 
  28. */ 
  29. public $currencies_options; 
  30.  
  31. /** 
  32. * The custom currencies that the user has entered in our page 
  33. */ 
  34. public $saved_currencies; 
  35.  
  36. /** 
  37. * Constructor. 
  38. */ 
  39. public function __construct() { 
  40.  
  41. $this->id = 'currency'; 
  42. $this->label = __( 'Currency', 'woocommerce_custom_currency' ); 
  43.  
  44. $this->supported_currencies = apply_filters( 'woocommerce_paypal_supported_currencies', array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB' ) ); 
  45.  
  46. foreach( $this->supported_currencies as $c ) { 
  47. $this->currencies_options[ $c ] = $c; 
  48.  
  49. $this->saved_currencies = WooCommerceCustomCurrencies::get_custom_currencies(); 
  50.  
  51.  
  52. add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); 
  53. add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) ); 
  54. add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) ); 
  55.  
  56. // Settings page tabs 
  57. add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) ); 
  58.  
  59. // Custom currencies field 
  60. add_action( 'woocommerce_admin_field_extra_currencies', array( $this, 'generate_extra_currencies_html' ) ); 
  61. // Save custom currencies field 
  62. add_action( 'woocommerce_update_option_extra_currencies', array( $this, 'save_extra_currencies' ) ); 
  63.  
  64.  
  65. /** 
  66. * Get sections 
  67. * 
  68. * @return array 
  69. */ 
  70. public function get_sections() { 
  71.  
  72. $sections = array( 
  73. '' => __( 'PayPal Options', 'woocommerce_custom_currency' ),  
  74. 'override_currencies' => __( 'Override Currencies', 'woocommerce_custom_currency' ),  
  75. 'extra_currencies' => __( 'Extra Currencies', 'woocommerce_custom_currency' ) 
  76. ); 
  77.  
  78. return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections ); 
  79.  
  80. /** 
  81. * Output the settings 
  82. */ 
  83. public function output() { 
  84. global $current_section; 
  85.  
  86. $settings = $this->get_settings( $current_section ); 
  87.  
  88. WC_Admin_Settings::output_fields( $settings ); 
  89.  
  90. /** 
  91. * Save settings 
  92. */ 
  93. public function save() { 
  94. global $current_section; 
  95.  
  96. $settings = $this->get_settings( $current_section ); 
  97. WC_Admin_Settings::save_fields( $settings ); 
  98.  
  99. /** 
  100. * Get settings array 
  101. * 
  102. * @return array 
  103. */ 
  104. public function get_settings( $current_section = '' ) { 
  105.  
  106. if( empty( $current_section ) ) 
  107. $current_section = 'default'; 
  108.  
  109. $settings = call_user_func( array( $this, $current_section . '_settings' ) ); 
  110.  
  111. return $settings; 
  112.  
  113.  
  114. function default_settings() { 
  115. $currency_code_options = get_woocommerce_currencies(); 
  116.  
  117. foreach ( $currency_code_options as $code => $name ) { 
  118. $currency_code_options[ $code ] = $name . ' (' . get_woocommerce_currency_symbol( $code ) . ')'; 
  119.  
  120. $settings = apply_filters( 'woocommerce_custom_currencies_default_settings', array( 
  121.  
  122. array( 'title' => __( 'PayPal Settings', 'woocommerce_custom_currency' ), 'type' => 'title', 'desc' => 'Control PayPal conversions.', 'id' => 'custom_currency_options' ),  
  123.  
  124. array( 
  125. 'title' => __( 'Enable PayPal conversions', 'woocommerce_custom_currency' ),  
  126. 'desc' => __( 'Enable conversions to PayPal.', 'woocommerce_custom_currency' ),  
  127. 'id' => 'woocommerce_custom_currency_enable_paypal',  
  128. 'default' => 'yes',  
  129. 'type' => 'checkbox',  
  130. 'desc_tip' => true,  
  131. ),  
  132.  
  133. array( 
  134. 'title' => __( 'Conversion Currency', 'woocommerce_custom_currency' ),  
  135. 'desc' => __( 'The currency to convert so that PayPal accepts payment.', 'woocommerce_custom_currency' ),  
  136. 'id' => 'woocommerce_custom_currency_paypal_currency',  
  137. 'default' => 'USD',  
  138. 'type' => 'select',  
  139. 'options' => $this->currencies_options,  
  140. 'desc_tip' => true,  
  141. ),  
  142.  
  143. array( 
  144. 'title' => __( 'Conversion rate', 'woocommerce_custom_currency' ),  
  145. 'desc' => __( 'The conversion rate to use while converting the prices to PayPal.', 'woocommerce_custom_currency' ),  
  146. 'id' => 'woocommerce_custom_currency_rate',  
  147. 'css' => 'min-width:150px;',  
  148. 'default' => '1.0',  
  149. 'type' => 'text',  
  150. 'desc_tip' => true,  
  151. ),  
  152.  
  153. array( 'type' => 'sectionend', 'id' => 'custom_currency_options'),  
  154.  
  155. ) ); 
  156.  
  157. return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, 'default' ); 
  158.  
  159. function override_currencies_settings() { 
  160.  
  161. $custom_currency = get_option( 'woocommerce_custom_currency_code', '' ); 
  162.  
  163. $existing_currencies = get_woocommerce_currencies(); 
  164.  
  165. $settings = apply_filters( 'woocommerce_custom_currencies_override_settings', array( 
  166.  
  167. array( 'title' => __( 'Override Currencies', 'woocommerce_custom_currency' ), 'type' => 'title', 'desc' => 'Control the symbols of the existing currencies from this page.', 'id' => 'custom_currency_options' ),  
  168.  
  169. ) ); 
  170.  
  171. foreach( $existing_currencies as $currency_code => $currency_label ) { 
  172.  
  173. // Do not consider our custom currency 
  174. if( $currency_code == $custom_currency ) 
  175. continue; 
  176.  
  177. $settings[] = array( 
  178. 'title' => $currency_label . ' ( ' . $currency_code . ' ) ' . __( ' symbol', 'woocommerce_custom_currency' ),  
  179. 'desc' => __( 'Leave empty to use ', 'woocommerce_custom_currency' ) . get_woocommerce_currency_symbol( $currency_code ),  
  180. 'id' => 'woocommerce_custom_currency_override_' . $currency_code,  
  181. 'css' => 'min-width:150px;',  
  182. 'default' => '',  
  183. 'type' => 'text',  
  184. 'desc_tip' => true,  
  185. ); 
  186.  
  187.  
  188. $settings[] = array( 'type' => 'sectionend', 'id' => 'custom_currency_options'); 
  189.  
  190. return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, 'override' ); 
  191.  
  192.  
  193. function extra_currencies_settings() { 
  194.  
  195. $settings = apply_filters( 'woocommerce_custom_currencies_extra_settings', array( 
  196.  
  197. array( 'title' => __( 'Extra Currencies', 'woocommerce_custom_currency' ), 'type' => 'title', 'desc' => 'Using the following table you can easily add as much currencies as you want.', 'id' => 'custom_currency_options' ),  
  198.  
  199. ) ); 
  200.  
  201. $settings[] = array( 
  202. 'title' => 'Unlimited Currencies',  
  203. 'type' => 'extra_currencies',  
  204. 'id' => 'extra_currencies' 
  205. ); 
  206.  
  207. $settings[] = array( 'type' => 'sectionend', 'id' => 'custom_currency_options'); 
  208.  
  209. return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings, 'extra_currencies' ); 
  210.  
  211.  
  212. public function generate_extra_currencies_html( $value ) { 
  213.  
  214. ob_start(); 
  215. ?> 
  216.  
  217. <tr valign="top"> 
  218. <th scope="row" class="titledesc"> 
  219. <label>Custom Currencies</label> 
  220. <p class="description"> 
  221. This functionality is considered a beta version. 
  222. </p> 
  223. </th> 
  224. <td class="forminp"> 
  225. <div style="margin-bottom:10px;"> 
  226. <button type="button" class="button primary add_currency">Add a currency</button> 
  227. </div> 
  228. <table class="wc_currencies widefat" cellspacing="0"> 
  229. <thead> 
  230. <tr> 
  231. <th class="name">Currency Label</th> 
  232. <th class="id">Currency Symbol</th> 
  233. <th class="status">Currency Code</th> 
  234. <th></th> 
  235. </tr> 
  236. </thead> 
  237. <tbody class="wc_currencies"> 
  238. <?php  
  239. for( $i = 0; $i < count( $this->saved_currencies->labels ); $i++ ): 
  240.  
  241. $currency = array(); 
  242. $currency[ 'label' ] = $this->saved_currencies->labels[ $i ]; 
  243. $currency[ 'code' ] = $this->saved_currencies->codes[ $i ]; 
  244. $currency[ 'symbol' ] = $this->saved_currencies->symbols[ $i ]; 
  245.  
  246. WooCommerceCustomCurrencies::template( 'currency_row', $currency, false ); 
  247.  
  248. endfor; 
  249. ?> 
  250. </tbody> 
  251. <tfoot> 
  252. <tr> 
  253. <th class="name">Currency Label</th> 
  254. <th class="id">Currency Symbol</th> 
  255. <th class="status">Currency Code</th> 
  256. <th></th> 
  257. </tr> 
  258. </tfoot> 
  259. </table> 
  260. </td> 
  261. </tr> 
  262.  
  263. <?php 
  264.  
  265. echo ob_get_clean(); 
  266.  
  267. function save_extra_currencies( $value ) { 
  268.  
  269. $fields = array( 
  270. 'labels',  
  271. 'symbols',  
  272. 'codes' 
  273. ); 
  274.  
  275. $error = false; 
  276.  
  277.  
  278. foreach( $fields as $f ) { 
  279.  
  280. if( !empty( $_POST[ $f ] ) && !is_array( $_POST[ $f ] ) ) { 
  281. WC_Admin_Settings::add_error( "Couldn't save. Please try again" ); 
  282. $error = true; 
  283. continue; 
  284.  
  285.  
  286. if( $error ) 
  287. return; 
  288.  
  289. foreach( $fields as $f ) { 
  290.  
  291. if( empty( $_POST[ $f ] ) ) 
  292. $_POST[ $f ] = array(); 
  293.  
  294. $this->saved_currencies->{$f} = $_POST[ $f ]; 
  295.  
  296. update_option( '_wc_custom_currencies_' . $f, $_POST[ $f ] ); 
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304. endif; 
  305.  
  306. return new WC_Custom_Currencies_Settings(); 
.