1 <?php
  2 
  3   4   5   6   7   8   9  10 
 11 
 12 namespace WC_POS\Admin\Settings;
 13 
 14 use WC_POS\Admin\Settings;
 15 
 16 class Page {
 17 
 18   protected static $instance;
 19 
 20   protected $defaults;
 21 
 22   protected $flush_local_data = null;
 23 
 24   public $current_user_authorized = true;
 25 
 26   protected $section_handlers = array();
 27 
 28    29  30  31  32  33 
 34   public static function get_instance() {
 35     $class = get_called_class();
 36     if ( null === static::$instance ) {
 37       static::$instance = new $class();
 38     }
 39 
 40     return static::$instance;
 41   }
 42 
 43   protected function __construct() {
 44   }
 45 
 46   protected function __clone() {
 47   }
 48 
 49   protected function __wakeup() {
 50   }
 51 
 52    53  54 
 55   public function get_payload() {
 56     return array(
 57       'id'       => $this->id,
 58       'label'    => $this->label,
 59       'data'     => $this->get(),
 60       'template' => $this->get_template(),
 61       'sections' => $this->get_sections()
 62     );
 63   }
 64 
 65    66  67 
 68   public function output() {
 69     $file = dirname(__FILE__) . '/views/' . $this->id . '.php';
 70     if ( is_readable( $file ) ) {
 71       include $file;
 72     }
 73   }
 74 
 75    76  77 
 78   public function get_template(){
 79     ob_start();
 80     $this->output();
 81     $template = wc_pos_trim_html_string( ob_get_clean() );
 82     return apply_filters( 'woocommerce_pos_' . $this->id . '_settings_template', $template, $this );
 83   }
 84 
 85    86  87  88 
 89   public function option_name(){
 90     return Settings::DB_PREFIX . $this->id;
 91   }
 92 
 93    94  95  96  97 
 98   public function get($key = null){
 99     $data = get_option( $this->option_name() );
100     if(!$data){
101       $data = apply_filters( 'woocommerce_pos_' . $this->id . '_settings_defaults', $this->get_defaults() );
102     }
103     if($key && is_array($data)) {
104       $data = array_key_exists($key, $data) ? $data[$key] : false;
105     }
106     return $data;
107   }
108 
109   110 111 112 
113   public function set(array $data){
114     $data['updated_at'] = time(); 
115     $updated = add_option( $this->option_name(), $data, '', 'no' );
116     if(!$updated) {
117       $updated = update_option( $this->option_name(), $data );
118     }
119     return $updated ? $data : false;
120   }
121 
122   123 124 
125   public function delete(){
126     delete_option( $this->option_name() );
127     return $this->get();
128   }
129 
130   131 132 
133   public function getJSON(){
134 
135     $data = $this->get();
136     return $data ? json_encode( $data ) : false;
137   }
138 
139   140 141 
142   public function get_sections(){
143     $sections = array();
144 
145     foreach( $this->section_handlers as $class ){
146       $handler = $class::get_instance();
147       $sections[] = $handler->get_payload();
148     }
149 
150     return $sections;
151   }
152 
153   154 155 
156   public function get_defaults(){
157     return $this->defaults;
158   }
159 
160   161 162 163 
164   public function flush_local_data( $data = array() ){
165     $keys = apply_filters( 'woocommerce_pos_' . $this->id . '_settings_flush_local_data', $this->flush_local_data );
166     return ! is_null( $keys );
167   }
168 
169 }