1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace WC_POS\API;
13
14 use RecursiveDirectoryIterator;
15 use RecursiveIteratorIterator;
16 use RecursiveRegexIterator;
17 use RegexIterator;
18 use WC_API_Resource;
19 use WC_API_Server;
20
21 class Templates extends WC_API_Resource {
22
23 protected $base = '/pos/templates';
24
25 26 27 28 29 30 31 32
33 public function register_routes( array $routes ) {
34
35
36 $routes[ $this->base ] = array(
37 array( array( $this, 'get_templates' ), WC_API_Server::READABLE ),
38 array( array( $this, 'create_receipt_template' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA )
39 );
40
41
42 $routes[ $this->base . '/(?P<id>\d+)' ] = array(
43 array( array( $this, 'update_receipt_template' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
44 array( array( $this, 'delete_receipt_template' ), WC_API_Server::DELETABLE ),
45 );
46
47
48 $routes[ $this->base . '/modal/(?P<id>\w+)' ] = array(
49 array( array( $this, 'get_modal' ), WC_API_Server::READABLE )
50 );
51
52 return $routes;
53
54 }
55
56 57 58 59 60
61 public function get_template_dir() {
62 return \WC_POS\PLUGIN_PATH . 'includes/views';
63 }
64
65 66 67 68
69 public function get_templates( $wc_pos_admin = null, $filter = array() ){
70
71 if(isset($filter['type']) && $filter['type'] == 'receipt'){
72 return $this->get_receipt_template();
73 }
74
75 if( $wc_pos_admin ){
76 return;
77 }
78
79 return apply_filters( 'woocommerce_pos_templates', $this->create_templates_array(), $this );
80 }
81
82 83 84 85
86 public function create_templates_array( $partials_dir = '' ) {
87 $templates = array();
88
89 foreach ( $this->locate_template_files( $partials_dir ) as $slug => $file ) {
90 $keys = explode( substr( $slug, 0, 1 ), substr( $slug, 1 ) );
91 $template = array_reduce( array_reverse( $keys ), 'self::reduce_templates_array', $this->template_output( $file ) );
92 $templates = array_merge_recursive( $templates, $template );
93 }
94
95 return $templates;
96 }
97
98 99 100 101 102 103
104 public function locate_template_files( $partials_dir = '' ) {
105 $files = array();
106 foreach ( $this->locate_default_template_files( $partials_dir ) as $slug => $path ) {
107 $files[ $slug ] = $this->locate_template_file( $path );
108 };
109
110 return $files;
111 }
112
113 114 115 116 117 118 119
120 public function locate_default_template_files( $partials_dir = '' ) {
121 if ( empty( $partials_dir ) )
122 $partials_dir = $this->get_template_dir();
123
124 $Directory = new RecursiveDirectoryIterator( $partials_dir );
125 $Iterator = new RecursiveIteratorIterator( $Directory, RecursiveIteratorIterator::SELF_FIRST );
126 $Regex = new RegexIterator( $Iterator, '/^.+tmpl-[a-z-]+\.php$/i', RecursiveRegexIterator::GET_MATCH );
127
128 $paths = array_keys( iterator_to_array( $Regex ) );
129 $templates = array();
130
131 foreach ( $paths as $path ) {
132 $slug = str_replace( array( $partials_dir, '.php' ), '', $path );
133 $templates[ $slug ] = $path;
134 };
135
136 return $templates;
137 }
138
139 140 141 142 143 144
145 public function locate_template_file( $default_path = '' ) {
146 $custom_path1 = str_replace( $this->get_template_dir(), 'woocommerce-pos', $default_path );
147 $custom_path2 = str_replace( 'tmpl-', '', $custom_path1 );
148 $custom = locate_template( array( $custom_path1, $custom_path2 ) );
149
150 return $custom ? $custom : $default_path;
151 }
152
153 154 155 156 157
158 private function reduce_templates_array( $result, $key ) {
159 if ( is_string( $result ) )
160 $key = preg_replace( '/^tmpl-/i', '', $key );
161 return array( $key => $result );
162 }
163
164 165 166 167 168 169
170 public function template_output( $file, $trim = true ) {
171 $template = '';
172
173 if( is_readable( $file ) ){
174 ob_start();
175 include $file;
176 $template = ob_get_contents();
177 ob_end_clean();
178 }
179
180 return $trim ? wc_pos_trim_html_string( $template ) : $template ;
181 }
182
183 184 185 186 187
188 public function locate_print_receipt_template($file) {
189 $receipt_path = $this->locate_template_file( \WC_POS\PLUGIN_PATH . 'includes/views/print/' . $file );
190 return apply_filters( 'woocommerce_pos_print_receipt_path', $receipt_path );
191 }
192
193 194 195 196
197 public function get_modal( $id = '' ){
198 $default_path = $this->get_template_dir() . '/modals/' . $id . '.php';
199 $file = $this->locate_template_file( $default_path );
200 $template = $this->template_output( $file );
201
202 return $template;
203 }
204
205 206 207
208 public function get_receipt_template() {
209 $this->register_receipt_status();
210
211 $options = wc_pos_get_option('receipts', 'receipt_options');
212 $type = isset($options['template_language']) ? $options['template_language'] : 'html';
213 $method = isset($options['print_method']) ? $options['print_method'] : 'browser';
214 $network_address = isset($options['network_printer_address']) ? $options['network_printer_address'] : '';
215
216 $posts = get_posts( array(
217 'posts_per_page' => 1,
218 'post_type' => 'wc-print-template',
219 'post_status' => $type
220 ));
221
222 if(!empty($posts)){
223 $template = $posts[0];
224 return array(
225 array(
226 'id' => $template->ID,
227 'type' => $type,
228 'method' => $method,
229 'network_address' => $network_address,
230 'template' => $template->post_content
231 )
232 );
233 }
234
235 $path = $this->locate_print_receipt_template('receipt-' . $type . '.php');
236
237
238 if(!$path && $type == 'html'){
239 $path = $this->locate_print_receipt_template('tmpl-receipt.php');
240 }
241
242 return array(
243 array(
244 'type' => $type,
245 'method' => $method,
246 'network_address' => $network_address,
247 'template' => $this->template_output( $path, false )
248 )
249 );
250 }
251
252 253 254
255 public function create_receipt_template( array $data ) {
256 $this->update_receipt_template(null, $data);
257 }
258
259 260 261
262 public function update_receipt_template( $id, array $data ) {
263 $template = isset($data['template']) ? $data['template'] : '';
264 $post_status = wc_pos_get_option('receipts', array(
265 'section' => 'receipt_options',
266 'key' => 'template_language'
267 ));
268
269 $args = array(
270 'ID' => (int) $id,
271 'post_type' => 'wc-print-template',
272 'post_status' => $post_status,
273 'post_title' => 'Receipt Template',
274 'post_content' => $template,
275 'comment_status' => 'closed',
276 'ping_status' => 'closed'
277 );
278
279 wp_insert_post($args);
280
281 return $this->get_receipt_template();
282 }
283
284 285 286 287
288 public function delete_receipt_template( $id ) {
289 $post = get_post($id);
290
291 if($post && $post->post_type == 'wc-print-template'){
292 wp_delete_post($id);
293 }
294
295 return $this->get_receipt_template();
296 }
297
298 299 300
301 private function register_receipt_status(){
302 $receipt_types = array(
303 'html' => array(),
304 'epos-print' => array(),
305 'escp' => array()
306 );
307
308 foreach ( $receipt_types as $type => $values ) {
309 register_post_status( $type, $values );
310 }
311 }
312
313 }