WC_POS_Admin_Settings

WP Settings Class.

Defined (1)

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

/includes/admin/class-wc-pos-settings.php  
  1. class WC_POS_Admin_Settings { 
  2.  
  3. /** @var string The db prefix for WP Options table */ 
  4. const DB_PREFIX = 'woocommerce_pos_settings_'; 
  5.  
  6. /** @var string The settings screen id */ 
  7. static public $screen_id; 
  8.  
  9. /** 
  10. * Constructor 
  11. */ 
  12. public function __construct() { 
  13. add_action( 'admin_menu', array( $this, 'admin_menu' ) ); 
  14. add_action( 'current_screen', array( $this, 'conditional_init' ) ); 
  15.  
  16. /** 
  17. * Add Settings page to admin menu 
  18. */ 
  19. public function admin_menu() { 
  20. self::$screen_id = add_submenu_page( 
  21. WC_POS_PLUGIN_NAME,  
  22. /** translators: wordpress */ 
  23. __( 'Settings' ),  
  24. /** translators: wordpress */ 
  25. __( 'Settings' ),  
  26. 'manage_woocommerce_pos',  
  27. 'wc_pos_settings',  
  28. array( $this, 'display_settings_page' ) 
  29. ); 
  30.  
  31. /** 
  32. * @param $current_screen 
  33. */ 
  34. public function conditional_init( $current_screen ) { 
  35. if ( $current_screen->id == self::$screen_id ) { 
  36.  
  37. // Enqueue scripts for the settings page 
  38. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); 
  39. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ), 99 ); 
  40. add_action( 'admin_print_footer_scripts', array( $this, 'admin_inline_js' ) ); 
  41.  
  42.  
  43. /** 
  44. * Output the settings pages 
  45. */ 
  46. public function display_settings_page() { 
  47. include 'views/settings.php'; 
  48.  
  49. /** 
  50. * Delete settings in WP options table 
  51. * @param $id 
  52. * @return bool 
  53. */ 
  54. static function delete_settings( $id ) { 
  55. return delete_option( self::DB_PREFIX . $id ); 
  56.  
  57. /** 
  58. * Delete all settings in WP options table 
  59. */ 
  60. static function delete_all_settings() { 
  61. global $wpdb; 
  62. $wpdb->query( 
  63. $wpdb->prepare( " 
  64. DELETE FROM {$wpdb->options} 
  65. WHERE option_name 
  66. LIKE '%s'",  
  67. self::DB_PREFIX . '%' 
  68. ); 
  69.  
  70. /** 
  71. * Settings styles 
  72. */ 
  73. public function enqueue_admin_styles() { 
  74. wp_enqueue_style( 
  75. WC_POS_PLUGIN_NAME . '-admin',  
  76. WC_POS_PLUGIN_URL . 'assets/css/admin.min.css',  
  77. null,  
  78. WC_POS_VERSION 
  79. ); 
  80.  
  81. /** 
  82. * Settings scripts 
  83. */ 
  84. public function enqueue_admin_scripts() { 
  85.  
  86. global $wp_scripts; 
  87. $wp_scripts->queue = array(); 
  88.  
  89. // deregister scripts 
  90. wp_deregister_script( 'underscore' ); 
  91. wp_deregister_script( 'select2' ); 
  92. wp_deregister_script( 'backbone' ); 
  93.  
  94. // register 
  95. $external_libs = WC_POS_Template::get_external_js_libraries(); 
  96. wp_register_script( 'lodash', $external_libs[ 'lodash' ], array( 'jquery' ), null, true ); 
  97. wp_register_script( 'backbone', $external_libs[ 'backbone' ], array( 'jquery', 'lodash' ), null, true ); 
  98. wp_register_script( 'backbone.radio', $external_libs[ 'radio' ], array( 'jquery', 'backbone', 'lodash' ), null, true ); 
  99. wp_register_script( 'marionette', $external_libs[ 'marionette' ], array( 'jquery', 'backbone', 'lodash' ), null, true ); 
  100. wp_register_script( 'handlebars', $external_libs[ 'handlebars' ], false, null, true ); 
  101. wp_register_script( 'moment', $external_libs[ 'moment' ], false, null, true ); 
  102. wp_register_script( 'accounting', $external_libs[ 'accounting' ], false, null, true ); 
  103. wp_register_script( 'select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js', array( 'jquery' ), null, true ); 
  104. wp_register_script( 'idb-wrapper', $external_libs[ 'idb-wrapper' ], false, null, true ); 
  105.  
  106. // enqueue 
  107. wp_enqueue_script( 'jquery-ui-sortable' ); 
  108.  
  109. $build = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? 'build' : 'min'; 
  110.  
  111. wp_enqueue_script( 
  112. WC_POS_PLUGIN_NAME . '-admin-app',  
  113. WC_POS_PLUGIN_URL . 'assets/js/admin.' . $build . '.js',  
  114. array( 'backbone', 'backbone.radio', 'marionette', 'handlebars', 'accounting', 'moment', 'select2', 'idb-wrapper' ),  
  115. WC_POS_VERSION,  
  116. true 
  117. ); 
  118.  
  119. wp_enqueue_script( 
  120. 'eventsource-polyfill',  
  121. WC_POS_PLUGIN_URL . 'assets/js/vendor/eventsource.min.js',  
  122. array(),  
  123. null,  
  124. true 
  125. ); 
  126.  
  127. $scripts = apply_filters( 'woocommerce_pos_admin_enqueue_scripts', array() ); 
  128. if ( isset( $scripts[ 'locale' ] ) ) { 
  129. wp_enqueue_script( 
  130. WC_POS_PLUGIN_NAME . '-js-locale',  
  131. $scripts[ 'locale' ],  
  132. array( WC_POS_PLUGIN_NAME . '-admin-app' ),  
  133. WC_POS_VERSION,  
  134. true 
  135. ); 
  136.  
  137. /** 
  138. * Start the Settings App 
  139. */ 
  140. public function admin_inline_js() { 
  141. $options = array( 
  142. 'action' => 'wc_pos_admin_settings_payload',  
  143. 'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),  
  144. 'nonce' => wp_create_nonce( WC_POS_PLUGIN_NAME ) 
  145. ); 
  146. echo '<script>POS.start(' . json_encode( $options ) . ');</script>'; 
  147.