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

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