1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace WC_POS;
13
14 class Template {
15
16
17 private $slug;
18
19
20 private $regex;
21
22
23 public $params;
24
25
26 static public $external_libs = array(
27 'min' => array(
28 'jquery' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js',
29 'lodash' => 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js',
30 'backbone' => 'https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.3/backbone-min.js',
31 'radio' => 'https://cdnjs.cloudflare.com/ajax/libs/backbone.radio/1.0.2/backbone.radio.min.js',
32 'marionette' => 'https://cdnjs.cloudflare.com/ajax/libs/backbone.marionette/2.4.4/backbone.marionette.min.js',
33 'handlebars' => 'https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js',
34 'moment' => 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js',
35 'accounting' => 'https://cdnjs.cloudflare.com/ajax/libs/accounting.js/0.4.1/accounting.min.js',
36 'jquery.color' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery-color/2.1.2/jquery.color.min.js',
37 ),
38 'debug' => array(
39 'jquery' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.js',
40 'lodash' => 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js',
41 'backbone' => 'https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.3/backbone.js',
42 'radio' => 'https://cdnjs.cloudflare.com/ajax/libs/backbone.radio/1.0.2/backbone.radio.js',
43 'marionette' => 'https://cdnjs.cloudflare.com/ajax/libs/backbone.marionette/2.4.4/backbone.marionette.js',
44 'handlebars' => 'https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.js',
45 'moment' => 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js',
46 'accounting' => 'https://cdnjs.cloudflare.com/ajax/libs/accounting.js/0.4.1/accounting.js',
47 'jquery.color' => 'https://cdnjs.cloudflare.com/ajax/libs/jquery-color/2.1.2/jquery.color.js',
48 )
49 );
50
51 52 53
54 public function __construct() {
55 $this->slug = Admin\Permalink::get_slug();
56 $this->regex = '^' . $this->slug . '/?$';
57
58 add_rewrite_tag( '%pos%', '([^&]+)' );
59 add_rewrite_rule( $this->regex, 'index.php?pos=1', 'top' );
60 add_filter( 'option_rewrite_rules', array( $this, 'rewrite_rules' ), 1 );
61 add_action( 'template_redirect', array( $this, 'template_redirect' ), 1 );
62 }
63
64 65 66 67 68 69
70 public function rewrite_rules( $rules ) {
71 return isset( $rules[ $this->regex ] ) ? $rules : false;
72 }
73
74 75 76
77 public function template_redirect() {
78
79 if ( !is_pos( 'template' ) )
80 return;
81
82
83 if ( !is_user_logged_in() ) {
84 add_filter( 'login_url', array( $this, 'login_url' ) );
85 auth_redirect();
86 }
87
88
89 if ( !current_user_can( 'access_woocommerce_pos' ) )
90
91 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
92
93
94 $this->no_cache();
95
96
97 do_action( 'woocommerce_pos_template_redirect' );
98
99
100 add_action( 'woocommerce_pos_head', array( $this, 'head' ) );
101 add_action( 'woocommerce_pos_footer', array( $this, 'footer' ) );
102
103
104 include 'views/main.php';
105 exit;
106
107 }
108
109 110 111 112 113 114
115 public function login_url( $login_url ) {
116 return add_query_arg( 'pos', '1', $login_url );
117 }
118
119 120 121
122 private function no_cache() {
123
124
125 if ( !defined( 'DONOTMINIFY' ) )
126 define( "DONOTMINIFY", "true" );
127
128
129 if ( !defined( 'DONOTCACHEPAGE' ) )
130 define( "DONOTCACHEPAGE", "true" );
131 }
132
133 134 135
136 static public function get_external_js_libraries() {
137 return defined( '\SCRIPT_DEBUG' ) && \SCRIPT_DEBUG ? self::$external_libs[ 'debug' ] : self::$external_libs[ 'min' ];
138 }
139
140 141 142
143 public function head() {
144
145
146 $styles = apply_filters( 'woocommerce_pos_enqueue_head_css', array(
147 'pos-css' => PLUGIN_URL . 'assets/css/pos.min.css?ver=' . VERSION
148 ) );
149
150 foreach ( $styles as $style ) {
151 echo $this->format_css( trim( $style ) ) . "\n";
152 }
153
154
155 $js = array(
156 'modernizr' => PLUGIN_URL . 'assets/js/vendor/modernizr.custom.min.js?ver=' . VERSION,
157 );
158
159 $scripts = apply_filters( 'woocommerce_pos_enqueue_head_js', $js );
160 foreach ( $scripts as $script ) {
161 echo $this->format_js( trim( $script ) ) . "\n";
162 }
163 }
164
165 166 167
168 public function footer() {
169 $build = defined( '\SCRIPT_DEBUG' ) && \SCRIPT_DEBUG ? 'build' : 'min';
170
171 $js = self::get_external_js_libraries();
172 $js[ 'app' ] = PLUGIN_URL . 'assets/js/app.' . $build . '.js?ver=' . VERSION;
173 $scripts = apply_filters( 'woocommerce_pos_enqueue_footer_js', $js );
174
175 foreach ( $scripts as $script ) {
176 echo $this->format_js( trim( $script ) ) . "\n";
177 }
178 }
179
180 181 182 183 184 185
186 private function format_css( $style ) {
187 if ( substr( $style, 0, 5 ) === '<link' )
188 return $style;
189
190 if ( substr( $style, 0, 4 ) === 'http' )
191 return '<link rel="stylesheet" href="' . $style . '" type="text/css" />';
192
193 return '<style>' . $style . '</style>';
194 }
195
196 197 198 199 200 201
202 private function format_js( $script ) {
203 if ( substr( $script, 0, 7 ) === '<script' )
204 return $script;
205
206 if ( substr( $script, 0, 4 ) === 'http' )
207 return '<script src="' . $script . '"></script>';
208
209 return '<script>' . $script . '</script>';
210 }
211
212 }