Overview
  • Namespace
  • Class

Namespaces

  • None
  • WC_POS
    • Admin
      • Settings
        • Receipt
    • API
    • Gateways
    • Integrations
    • Products

Classes

  • WC_POS\Activator
  • WC_POS\Admin
  • WC_POS\Admin\Gateways
  • WC_POS\Admin\Menu
  • WC_POS\Admin\Notices
  • WC_POS\Admin\Orders
  • WC_POS\Admin\Page
  • WC_POS\Admin\Permalink
  • WC_POS\Admin\Plugins
  • WC_POS\Admin\Products
  • WC_POS\Admin\Settings
  • WC_POS\Admin\Settings\Access
  • WC_POS\Admin\Settings\Checkout
  • WC_POS\Admin\Settings\Customers
  • WC_POS\Admin\Settings\Gateways
  • WC_POS\Admin\Settings\General
  • WC_POS\Admin\Settings\HotKeys
  • WC_POS\Admin\Settings\Page
  • WC_POS\Admin\Settings\Receipt\Options
  • WC_POS\Admin\Settings\Receipt\Template
  • WC_POS\Admin\Settings\Receipts
  • WC_POS\Admin\Settings\Status
  • WC_POS\Admin\Settings\Tools
  • WC_POS\Admin\Status
  • WC_POS\Ajax
  • WC_POS\API
  • WC_POS\API\Coupons
  • WC_POS\API\Customers
  • WC_POS\API\Gateways
  • WC_POS\API\i18n
  • WC_POS\API\Orders
  • WC_POS\API\Params
  • WC_POS\API\Payload
  • WC_POS\API\Products
  • WC_POS\API\Settings
  • WC_POS\API\Support
  • WC_POS\API\Templates
  • WC_POS\Customers
  • WC_POS\Deactivator
  • WC_POS\Gateways
  • WC_POS\Gateways\Card
  • WC_POS\Gateways\Cash
  • WC_POS\i18n
  • WC_POS\Integrations\Bookings
  • WC_POS\Products
  • WC_POS\Products\Visibility
  • WC_POS\Setup
  • WC_POS\Status
  • WC_POS\Tax
  • WC_POS\Template

Functions

  • is_pos
  • is_pos_admin
  • wc_pos_get_option
  • wc_pos_json_encode
  • wc_pos_locate_template
  • wc_pos_trim_html_string
  • wc_pos_update_option
  • wc_pos_url
  1 <?php
  2 
  3 /**
  4  * POS Templates
  5  *
  6  * @class    WC_POS_API_Templates
  7  * @package  WooCommerce POS
  8  * @author   Paul Kilmurray <paul@kilbot.com.au>
  9  * @link     http://www.woopos.com.au
 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    * Register routes for POS Params
 27    *
 28    * GET /pos
 29    *
 30    * @param array $routes
 31    * @return array
 32    */
 33   public function register_routes( array $routes ) {
 34 
 35     # GET /pos/templates
 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     # PUT, DELETE /pos/templates/<id>
 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     # GET /pos/templates/modal/<id>
 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    * Returns the partials directory
 58    *
 59    * @return string
 60    */
 61   public function get_template_dir() {
 62     return \WC_POS\PLUGIN_PATH . 'includes/views';
 63   }
 64 
 65   /**
 66    * @param null $wc_pos_admin
 67    * @return array
 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    * @param $partials_dir
 84    * @return array
 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    * Returns an array of template paths
100    *
101    * @param $partials_dir
102    * @return array
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    * Returns an assoc array of all default tmpl-*.php paths
115    * - uses SPL iterators
116    *
117    * @param $partials_dir
118    * @return array
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    * Locate a single template partial
141    *
142    * @param string $default_path
143    * @return string
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    * @param $result
155    * @param $key
156    * @return array
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    * Output template partial as string
166    *
167    * @param $file
168    * @return string
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    * Returns path of print receipt template
185    * @param $file
186    * @return
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    * @param string $id
195    * @return string|WP_Error
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     // backwards compat
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    * @param $id
286    * @return array
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 }
API documentation generated by ApiGen