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  * Responsible for the POS front-end
  5  *
  6  * @class    WC_POS_Template
  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;
 13 
 14 class Template {
 15 
 16   /** @var POS url slug */
 17   private $slug;
 18 
 19   /** @var regex match for rewite_rule */
 20   private $regex;
 21 
 22   /** @var WC_POS_Params instance */
 23   public $params;
 24 
 25   /** @var array external libraries */
 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    * Constructor
 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    * Make sure cache contains POS rewrite rule
 66    *
 67    * @param $rules
 68    * @return bool
 69    */
 70   public function rewrite_rules( $rules ) {
 71     return isset( $rules[ $this->regex ] ) ? $rules : false;
 72   }
 73 
 74   /**
 75    * Output the POS template
 76    */
 77   public function template_redirect() {
 78     // check is pos
 79     if ( !is_pos( 'template' ) )
 80       return;
 81 
 82     // check auth
 83     if ( !is_user_logged_in() ) {
 84       add_filter( 'login_url', array( $this, 'login_url' ) );
 85       auth_redirect();
 86     }
 87 
 88     // check privileges
 89     if ( !current_user_can( 'access_woocommerce_pos' ) )
 90       /* translators: wordpress */
 91       wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
 92 
 93     // disable cache plugins
 94     $this->no_cache();
 95 
 96     // last chance before template is rendered
 97     do_action( 'woocommerce_pos_template_redirect' );
 98 
 99     // add head & footer actions
100     add_action( 'woocommerce_pos_head', array( $this, 'head' ) );
101     add_action( 'woocommerce_pos_footer', array( $this, 'footer' ) );
102 
103     // now show the page
104     include 'views/main.php';
105     exit;
106 
107   }
108 
109   /**
110    * Add variable to login url to signify POS login
111    *
112    * @param $login_url
113    * @return mixed
114    */
115   public function login_url( $login_url ) {
116     return add_query_arg( 'pos', '1', $login_url );
117   }
118 
119   /**
120    * Disable caching conflicts
121    */
122   private function no_cache() {
123 
124     // disable W3 Total Cache minify
125     if ( !defined( 'DONOTMINIFY' ) )
126       define( "DONOTMINIFY", "true" );
127 
128     // disable WP Super Cache
129     if ( !defined( 'DONOTCACHEPAGE' ) )
130       define( "DONOTCACHEPAGE", "true" );
131   }
132 
133   /**
134    * @return array
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    * Output the head scripts
142    */
143   public function head() {
144 
145     // enqueue and print javascript
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     // enqueue and print javascript
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    * Output the footer scripts
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    * Makes sure css is in the right format for template
182    *
183    * @param $style
184    * @return string
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    * Makes sure javascript is in the right format for template
198    *
199    * @param $script
200    * @return string
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 }
API documentation generated by ApiGen