WC_Payment_Discounts_Admin

WC_Payment_Discounts_Admin class.

Defined (1)

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

/includes/admin/class-wc-payment-discounts-admin.php  
  1. class WC_Payment_Discounts_Admin { 
  2.  
  3. /** 
  4. * Initialize the plugin admin. 
  5. */ 
  6. public function __construct() { 
  7. // Add the options page and menu item. 
  8. add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) ); 
  9.  
  10. // Register plugin settings. 
  11. add_action( 'admin_init', array( $this, 'register_settings' ) ); 
  12.  
  13. $this->maybe_update(); 
  14.  
  15. /** 
  16. * Maybe update the plugin. 
  17. */ 
  18. protected function maybe_update() { 
  19. $current_version = get_option( 'woocommerce_payment_discounts_version', '0' ); 
  20.  
  21. if ( ! version_compare( $current_version, WC_Payment_Discounts::VERSION, '>=' ) ) { 
  22. // Upgrade to 2.3.0. 
  23. if ( version_compare( $current_version, '2.3.0', '<' ) ) { 
  24. $this->upgrade_to_230(); 
  25.  
  26. update_option( 'woocommerce_payment_discounts_version', WC_Payment_Discounts::VERSION ); 
  27.  
  28. /** 
  29. * Upgrade to 2.3.0. 
  30. */ 
  31. private function upgrade_to_230() { 
  32. if ( $old_options = get_option( 'woocommerce_payment_discounts' ) ) { 
  33. $new_options = array(); 
  34.  
  35. foreach ( $old_options as $key => $value ) { 
  36. $new_options[ $key ] = array( 
  37. 'amount' => $value,  
  38. ); 
  39.  
  40. update_option( 'woocommerce_payment_discounts', $new_options ); 
  41.  
  42. /** 
  43. * Register the administration menu for this plugin into the WordPress Dashboard menu. 
  44. */ 
  45. public function add_plugin_admin_menu() { 
  46. add_submenu_page( 
  47. 'woocommerce',  
  48. __( 'Payment Discounts', 'woocommerce-payment-discounts' ),  
  49. __( 'Payment Discounts', 'woocommerce-payment-discounts' ),  
  50. 'manage_woocommerce',  
  51. 'woocommerce-payment-discounts',  
  52. array( $this, 'display_plugin_admin_page' ) 
  53. ); 
  54.  
  55. /** 
  56. * Register plugin settings. 
  57. */ 
  58. public function register_settings() { 
  59. register_setting( 'woocommerce_payment_discounts_group', 'woocommerce_payment_discounts', array( $this, 'validate_settings' ) ); 
  60.  
  61. /** 
  62. * Validate the plugin settings. 
  63. * @param array $options Submitted values. 
  64. * @return array Sanitized values. 
  65. */ 
  66. public function validate_settings( $options ) { 
  67. $output = array(); 
  68.  
  69. foreach ( $options as $key => $value ) { 
  70. // Validate amount. 
  71. $output[ $key ]['amount'] = 0; 
  72. if ( isset( $value['amount'] ) ) { 
  73. if ( strstr( $value['amount'], '%' ) ) { 
  74. $amount = str_replace( '%', '', floatval( wc_format_decimal( $value['amount'] ) ) ) . '%'; 
  75. } else { 
  76. $amount = floatval( wc_format_decimal( $value['amount'] ) ); 
  77.  
  78. $output[ $key ]['amount'] = $amount; 
  79.  
  80. // Save coupon. 
  81. $data = array( 
  82. 'amount' => $output[ $key ]['amount'],  
  83. ); 
  84. WC_Payment_Discounts_Coupons::update_coupon( $key, $data ); 
  85.  
  86. return $output; 
  87.  
  88. /** 
  89. * Render the settings page for this plugin. 
  90. * @return string Settings page content. 
  91. */ 
  92. public function display_plugin_admin_page() { 
  93. $settings = get_option( 'woocommerce_payment_discounts' ); 
  94. $payment_gateways = WC()->payment_gateways->payment_gateways(); 
  95.  
  96. include dirname( __FILE__ ) . '/views/html-admin-settings.php';