1 <?php
  2 /**
  3  * WooCommerce Core Functions
  4  *
  5  * General core functions available on both the front-end and admin.
  6  *
  7  * @author      WooThemes
  8  * @category    Core
  9  * @package     WooCommerce/Functions
 10  * @version     2.1.0
 11  */
 12 
 13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 14 
 15 // Include core functions
 16 include( 'wc-cart-functions.php' );
 17 include( 'wc-conditional-functions.php' );
 18 include( 'wc-coupon-functions.php' );
 19 include( 'wc-customer-functions.php' );
 20 include( 'wc-deprecated-functions.php' );
 21 include( 'wc-formatting-functions.php' );
 22 include( 'wc-notice-functions.php' );
 23 include( 'wc-order-functions.php' );
 24 include( 'wc-page-functions.php' );
 25 include( 'wc-product-functions.php' );
 26 include( 'wc-term-functions.php' );
 27 include( 'wc-attribute-functions.php' );
 28 
 29 /**
 30  * Filters on data used in admin and frontend
 31  */
 32 add_filter( 'woocommerce_coupon_code', 'sanitize_text_field' );
 33 add_filter( 'woocommerce_coupon_code', 'strtolower' ); // Coupons case-insensitive by default
 34 add_filter( 'woocommerce_stock_amount', 'intval' ); // Stock amounts are integers by default
 35 
 36 /**
 37  * Short Description (excerpt)
 38  */
 39 add_filter( 'woocommerce_short_description', 'wptexturize' );
 40 add_filter( 'woocommerce_short_description', 'convert_smilies' );
 41 add_filter( 'woocommerce_short_description', 'convert_chars' );
 42 add_filter( 'woocommerce_short_description', 'wpautop' );
 43 add_filter( 'woocommerce_short_description', 'shortcode_unautop' );
 44 add_filter( 'woocommerce_short_description', 'prepend_attachment' );
 45 add_filter( 'woocommerce_short_description', 'do_shortcode', 11 ); // AFTER wpautop()
 46 
 47 /**
 48  * Get template part (for templates like the shop-loop).
 49  *
 50  * @access public
 51  * @param mixed $slug
 52  * @param string $name (default: '')
 53  * @return void
 54  */
 55 function wc_get_template_part( $slug, $name = '' ) {
 56     $template = '';
 57 
 58     // Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php
 59     if ( $name ) {
 60         $template = locate_template( array( "{$slug}-{$name}.php", WC()->template_path() . "{$slug}-{$name}.php" ) );
 61     }
 62 
 63     // Get default slug-name.php
 64     if ( ! $template && $name && file_exists( WC()->plugin_path() . "/templates/{$slug}-{$name}.php" ) ) {
 65         $template = WC()->plugin_path() . "/templates/{$slug}-{$name}.php";
 66     }
 67 
 68     // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php
 69     if ( ! $template ) {
 70         $template = locate_template( array( "{$slug}.php", WC()->template_path() . "{$slug}.php" ) );
 71     }
 72 
 73     // Allow 3rd party plugin filter template file from their plugin
 74     $template = apply_filters( 'wc_get_template_part', $template, $slug, $name );
 75 
 76     if ( $template ) {
 77         load_template( $template, false );
 78     }
 79 }
 80 
 81 /**
 82  * Get other templates (e.g. product attributes) passing attributes and including the file.
 83  *
 84  * @access public
 85  * @param string $template_name
 86  * @param array $args (default: array())
 87  * @param string $template_path (default: '')
 88  * @param string $default_path (default: '')
 89  * @return void
 90  */
 91 function wc_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
 92     if ( $args && is_array( $args ) ) {
 93         extract( $args );
 94     }
 95 
 96     $located = wc_locate_template( $template_name, $template_path, $default_path );
 97 
 98     if ( ! file_exists( $located ) ) {
 99         _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
100         return;
101     }
102 
103     do_action( 'woocommerce_before_template_part', $template_name, $template_path, $located, $args );
104 
105     include( $located );
106 
107     do_action( 'woocommerce_after_template_part', $template_name, $template_path, $located, $args );
108 }
109 
110 /**
111  * Locate a template and return the path for inclusion.
112  *
113  * This is the load order:
114  *
115  *      yourtheme       /   $template_path  /   $template_name
116  *      yourtheme       /   $template_name
117  *      $default_path   /   $template_name
118  *
119  * @access public
120  * @param string $template_name
121  * @param string $template_path (default: '')
122  * @param string $default_path (default: '')
123  * @return string
124  */
125 function wc_locate_template( $template_name, $template_path = '', $default_path = '' ) {
126     if ( ! $template_path ) {
127         $template_path = WC()->template_path();
128     }
129 
130     if ( ! $default_path ) {
131         $default_path = WC()->plugin_path() . '/templates/';
132     }
133 
134     // Look within passed path within the theme - this is priority
135     $template = locate_template(
136         array(
137             trailingslashit( $template_path ) . $template_name,
138             $template_name
139         )
140     );
141 
142     // Get default template
143     if ( ! $template ) {
144         $template = $default_path . $template_name;
145     }
146 
147     // Return what we found
148     return apply_filters('woocommerce_locate_template', $template, $template_name, $template_path);
149 }
150 
151 /**
152  * Get Base Currency Code.
153  * @return string
154  */
155 function get_woocommerce_currency() {
156     return apply_filters( 'woocommerce_currency', get_option('woocommerce_currency') );
157 }
158 
159 /**
160  * Get full list of currency codes.
161  * @return array
162  */
163 function get_woocommerce_currencies() {
164     return array_unique(
165         apply_filters( 'woocommerce_currencies',
166             array(
167                 'AED' => __( 'United Arab Emirates Dirham', 'woocommerce' ),
168                 'AUD' => __( 'Australian Dollars', 'woocommerce' ),
169                 'BDT' => __( 'Bangladeshi Taka', 'woocommerce' ),
170                 'BRL' => __( 'Brazilian Real', 'woocommerce' ),
171                 'BGN' => __( 'Bulgarian Lev', 'woocommerce' ),
172                 'CAD' => __( 'Canadian Dollars', 'woocommerce' ),
173                 'CLP' => __( 'Chilean Peso', 'woocommerce' ),
174                 'CNY' => __( 'Chinese Yuan', 'woocommerce' ),
175                 'COP' => __( 'Colombian Peso', 'woocommerce' ),
176                 'CZK' => __( 'Czech Koruna', 'woocommerce' ),
177                 'DKK' => __( 'Danish Krone', 'woocommerce' ),
178                 'EUR' => __( 'Euros', 'woocommerce' ),
179                 'HKD' => __( 'Hong Kong Dollar', 'woocommerce' ),
180                 'HRK' => __( 'Croatia kuna', 'woocommerce' ),
181                 'HUF' => __( 'Hungarian Forint', 'woocommerce' ),
182                 'ISK' => __( 'Icelandic krona', 'woocommerce' ),
183                 'IDR' => __( 'Indonesia Rupiah', 'woocommerce' ),
184                 'INR' => __( 'Indian Rupee', 'woocommerce' ),
185                 'ILS' => __( 'Israeli Shekel', 'woocommerce' ),
186                 'JPY' => __( 'Japanese Yen', 'woocommerce' ),
187                 'KRW' => __( 'South Korean Won', 'woocommerce' ),
188                 'MYR' => __( 'Malaysian Ringgits', 'woocommerce' ),
189                 'MXN' => __( 'Mexican Peso', 'woocommerce' ),
190                 'NGN' => __( 'Nigerian Naira', 'woocommerce' ),
191                 'NOK' => __( 'Norwegian Krone', 'woocommerce' ),
192                 'NZD' => __( 'New Zealand Dollar', 'woocommerce' ),
193                 'PHP' => __( 'Philippine Pesos', 'woocommerce' ),
194                 'PLN' => __( 'Polish Zloty', 'woocommerce' ),
195                 'GBP' => __( 'Pounds Sterling', 'woocommerce' ),
196                 'RON' => __( 'Romanian Leu', 'woocommerce' ),
197                 'RUB' => __( 'Russian Ruble', 'woocommerce' ),
198                 'SGD' => __( 'Singapore Dollar', 'woocommerce' ),
199                 'ZAR' => __( 'South African rand', 'woocommerce' ),
200                 'SEK' => __( 'Swedish Krona', 'woocommerce' ),
201                 'CHF' => __( 'Swiss Franc', 'woocommerce' ),
202                 'TWD' => __( 'Taiwan New Dollars', 'woocommerce' ),
203                 'THB' => __( 'Thai Baht', 'woocommerce' ),
204                 'TRY' => __( 'Turkish Lira', 'woocommerce' ),
205                 'USD' => __( 'US Dollars', 'woocommerce' ),
206                 'VND' => __( 'Vietnamese Dong', 'woocommerce' ),
207             )
208         )
209     );
210 }
211 
212 /**
213  * Get Currency symbol.
214  * @param string $currency (default: '')
215  * @return string
216  */
217 function get_woocommerce_currency_symbol( $currency = '' ) {
218     if ( ! $currency ) {
219         $currency = get_woocommerce_currency();
220     }
221 
222     switch ( $currency ) {
223         case 'AED' :
224             $currency_symbol = 'د.إ';
225             break;
226         case 'BDT':
227             $currency_symbol = '&#2547;&nbsp;';
228             break;
229         case 'BRL' :
230             $currency_symbol = '&#82;&#36;';
231             break;
232         case 'BGN' :
233             $currency_symbol = '&#1083;&#1074;.';
234             break;
235         case 'AUD' :
236         case 'CAD' :
237         case 'CLP' :
238         case 'MXN' :
239         case 'NZD' :
240         case 'HKD' :
241         case 'SGD' :
242         case 'USD' :
243             $currency_symbol = '&#36;';
244             break;
245         case 'EUR' :
246             $currency_symbol = '&euro;';
247             break;
248         case 'CNY' :
249         case 'RMB' :
250         case 'JPY' :
251             $currency_symbol = '&yen;';
252             break;
253         case 'RUB' :
254             $currency_symbol = '&#1088;&#1091;&#1073;.';
255             break;
256         case 'KRW' : $currency_symbol = '&#8361;'; break;
257         case 'TRY' : $currency_symbol = '&#84;&#76;'; break;
258         case 'NOK' : $currency_symbol = '&#107;&#114;'; break;
259         case 'ZAR' : $currency_symbol = '&#82;'; break;
260         case 'CZK' : $currency_symbol = '&#75;&#269;'; break;
261         case 'MYR' : $currency_symbol = '&#82;&#77;'; break;
262         case 'DKK' : $currency_symbol = 'kr.'; break;
263         case 'HUF' : $currency_symbol = '&#70;&#116;'; break;
264         case 'IDR' : $currency_symbol = 'Rp'; break;
265         case 'INR' : $currency_symbol = 'Rs.'; break;
266         case 'ISK' : $currency_symbol = 'Kr.'; break;
267         case 'ILS' : $currency_symbol = '&#8362;'; break;
268         case 'PHP' : $currency_symbol = '&#8369;'; break;
269         case 'PLN' : $currency_symbol = '&#122;&#322;'; break;
270         case 'SEK' : $currency_symbol = '&#107;&#114;'; break;
271         case 'CHF' : $currency_symbol = '&#67;&#72;&#70;'; break;
272         case 'TWD' : $currency_symbol = '&#78;&#84;&#36;'; break;
273         case 'THB' : $currency_symbol = '&#3647;'; break;
274         case 'GBP' : $currency_symbol = '&pound;'; break;
275         case 'RON' : $currency_symbol = 'lei'; break;
276         case 'VND' : $currency_symbol = '&#8363;'; break;
277         case 'NGN' : $currency_symbol = '&#8358;'; break;
278         case 'HRK' : $currency_symbol = 'Kn'; break;
279         default    : $currency_symbol = ''; break;
280     }
281 
282     return apply_filters( 'woocommerce_currency_symbol', $currency_symbol, $currency );
283 }
284 
285 /**
286  * Send HTML emails from WooCommerce
287  *
288  * @param mixed $to
289  * @param mixed $subject
290  * @param mixed $message
291  * @param string $headers (default: "Content-Type: text/html\r\n")
292  * @param string $attachments (default: "")
293  */
294 function wc_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) {
295     global $woocommerce;
296 
297     $mailer = WC()->mailer();
298 
299     $mailer->send( $to, $subject, $message, $headers, $attachments );
300 }
301 
302 /**
303  * Get an image size.
304  *
305  * Variable is filtered by woocommerce_get_image_size_{image_size}
306  *
307  * @param string $image_size
308  * @return array
309  */
310 function wc_get_image_size( $image_size ) {
311     if ( in_array( $image_size, array( 'shop_thumbnail', 'shop_catalog', 'shop_single' ) ) ) {
312         $size           = get_option( $image_size . '_image_size', array() );
313         $size['width']  = isset( $size['width'] ) ? $size['width'] : '300';
314         $size['height'] = isset( $size['height'] ) ? $size['height'] : '300';
315         $size['crop']   = isset( $size['crop'] ) ? $size['crop'] : 1;
316     } else {
317         $size = array(
318             'width'  => '300',
319             'height' => '300',
320             'crop'   => 1
321         );
322     }
323 
324     return apply_filters( 'woocommerce_get_image_size_' . $image_size, $size );
325 }
326 
327 /**
328  * Queue some JavaScript code to be output in the footer.
329  *
330  * @param string $code
331  */
332 function wc_enqueue_js( $code ) {
333     global $wc_queued_js;
334 
335     if ( empty( $wc_queued_js ) ) {
336         $wc_queued_js = '';
337     }
338 
339     $wc_queued_js .= "\n" . $code . "\n";
340 }
341 
342 /**
343  * Output any queued javascript code in the footer.
344  */
345 function wc_print_js() {
346     global $wc_queued_js;
347 
348     if ( ! empty( $wc_queued_js ) ) {
349 
350         echo "<!-- WooCommerce JavaScript -->\n<script type=\"text/javascript\">\njQuery(function($) {";
351 
352         // Sanitize
353         $wc_queued_js = wp_check_invalid_utf8( $wc_queued_js );
354         $wc_queued_js = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", $wc_queued_js );
355         $wc_queued_js = str_replace( "\r", '', $wc_queued_js );
356 
357         echo $wc_queued_js . "});\n</script>\n";
358 
359         unset( $wc_queued_js );
360     }
361 }
362 
363 /**
364  * Set a cookie - wrapper for setcookie using WP constants
365  *
366  * @param  string  $name   Name of the cookie being set
367  * @param  string  $value  Value of the cookie
368  * @param  integer $expire Expiry of the cookie
369  * @param  string  $secure Whether the cookie should be served only over https
370  */
371 function wc_setcookie( $name, $value, $expire = 0, $secure = false ) {
372     if ( ! headers_sent() ) {
373         setcookie( $name, $value, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure );
374     } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
375         trigger_error( "Cookie cannot be set - headers already sent", E_USER_NOTICE );
376     }
377 }
378 
379 /**
380  * Get the URL to the WooCommerce REST API
381  *
382  * @since 2.1
383  * @param string $path an endpoint to include in the URL
384  * @return string the URL
385  */
386 function get_woocommerce_api_url( $path ) {
387 
388     $url = get_home_url( null, 'wc-api/v' . WC_API::VERSION . '/', is_ssl() ? 'https' : 'http' );
389 
390     if ( ! empty( $path ) && is_string( $path ) ) {
391         $url .= ltrim( $path, '/' );
392     }
393 
394     return $url;
395 }
396 
WooCommerce API documentation generated by ApiGen 2.8.0