1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace WC_POS\Admin;
13
14 class Page {
15
16
17 const DB_PREFIX = 'woocommerce_pos_settings_';
18
19
20 protected $screen_id;
21
22
23 protected $wc_pos_adminpage = '';
24
25 26 27
28 public function __construct() {
29 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
30 }
31
32 33 34
35 public function conditional_init() {
36
37 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ), 99 );
38 add_action( 'admin_print_scripts', array( $this, 'admin_print_scripts' ) );
39 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ), 99 );
40 add_action( 'admin_print_footer_scripts', array( $this, 'print_footer_scripts' ), 99 );
41 }
42
43 44 45
46 public function admin_menu(){
47 if( $this->screen_id ){
48 add_action( 'load-' . $this->screen_id, array( $this, 'conditional_init' ) );
49 }
50 }
51
52 53 54
55 public function enqueue_admin_scripts(){}
56
57 58 59 60
61 public function enqueue_admin_styles() {
62 wp_enqueue_style(
63 \WC_POS\PLUGIN_NAME . '-admin',
64 \WC_POS\PLUGIN_URL . 'assets/css/admin.min.css',
65 null,
66 \WC_POS\VERSION
67 );
68 }
69
70 71 72
73 public function admin_print_scripts(){
74 echo '<script>var wc_pos_admin = "' . $this->wc_pos_adminpage . '";</script>';
75 }
76
77 78 79
80 public function print_footer_scripts() {
81 echo '<script>
82 if(window.POS){
83 POS.start(\'' . get_woocommerce_api_url(null) . '\');
84 } else {
85 alert(\'' . __('POS assets failed to load', 'woocommerce-pos') . '\');
86 }
87 </script>';
88 }
89
90 91 92
93 static public function get_db_version(){
94 return get_option( self::DB_PREFIX . 'db_version', 0 );
95 }
96
97 98 99 100
101 static public function bump_versions(){
102 self::update_option( self::DB_PREFIX . 'db_version', \WC_POS\VERSION );
103 self::bump_idb_version();
104 }
105
106 107 108
109 static public function get_idb_version(){
110 $version = (int) get_option( self::DB_PREFIX . 'idb_version', 0 );
111 return $version ? $version : self::bump_idb_version();
112 }
113
114 115 116
117 static public function bump_idb_version(){
118 $version = (int) get_option( self::DB_PREFIX . 'idb_version', 0 ) + 1;
119 if( self::update_option( self::DB_PREFIX . 'idb_version', $version ) ){
120 return $version;
121 }
122 }
123
124 125 126 127 128
129 static public function get_option( $id, $key = false ){
130 $handlers = (array) Settings::handlers();
131 if( !array_key_exists( $id, $handlers ) )
132 return false;
133
134 $settings = $handlers[$id]::get_instance();
135 return $settings->get( $key );
136 }
137
138 139 140 141 142 143 144 145 146
147 static public function update_option( $name, $value, $autoload = 'no' ) {
148 $success = add_option( $name, $value, '', $autoload );
149 return $success ? $success : update_option( $name, $value );
150 }
151
152 }