/includes/admin/class-wc-payment-discounts-admin.php

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