PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/www/wp-content/plugins/ithemes-exchange/api/theme/product.php

https://github.com/ArzuA/gitwordpress
PHP | 1149 lines | 649 code | 221 blank | 279 comment | 132 complexity | 35f55c003f3ddb8c8489d904ee632918 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Product class for THEME API
  4. *
  5. * @since 0.4.0
  6. */
  7. class IT_Theme_API_Product implements IT_Theme_API {
  8. /**
  9. * API context
  10. * @var string $_context
  11. * @since 0.4.0
  12. */
  13. private $_context = 'product';
  14. /**u
  15. * Maps api tags to methods
  16. * @var array $_tag_map
  17. * @since 0.4.0
  18. */
  19. var $_tag_map = array(
  20. 'found' => 'found',
  21. 'title' => 'title',
  22. 'permalink' => 'permalink',
  23. 'excerpt' => 'excerpt',
  24. 'description' => 'description',
  25. 'content' => 'extended_description',
  26. 'extendeddescription' => 'extended_description',
  27. 'author' => 'author',
  28. 'baseprice' => 'base_price',
  29. 'purchasequantity' => 'purchase_quantity',
  30. 'inventory' => 'inventory',
  31. 'availability' => 'availability',
  32. 'isavailable' => 'is_available',
  33. 'visibility' => 'visibility',
  34. 'isvisible' => 'is_visible',
  35. 'images' => 'product_images',
  36. 'gallery' => 'product_gallery',
  37. 'featuredimage' => 'featured_image',
  38. 'downloads' => 'downloads',
  39. 'purchaseoptions' => 'purchase_options',
  40. 'superwidget' => 'superwidget',
  41. 'buynow' => 'buy_now',
  42. 'addtocart' => 'add_to_cart',
  43. 'buynowvar' => 'buy_now_var',
  44. 'addtocartvar' => 'add_to_cart_var',
  45. );
  46. /**
  47. * Current product in iThemes Exchange Global
  48. * @var object $product
  49. * @since 0.4.0
  50. */
  51. public $product;
  52. /**
  53. * Constructor
  54. *
  55. * @since 0.4.0
  56. *
  57. * @return void
  58. */
  59. function IT_Theme_API_Product() {
  60. // Set the current global product as a property
  61. $this->product = empty( $GLOBALS['it_exchange']['product'] ) ? false : $GLOBALS['it_exchange']['product'];
  62. }
  63. /**
  64. * Returns the context. Also helps to confirm we are an iThemes Exchange theme API class
  65. *
  66. * @since 0.4.0
  67. *
  68. * @return string
  69. */
  70. function get_api_context() {
  71. return $this->_context;
  72. }
  73. /**
  74. * Returns boolean value if we have a product or not
  75. *
  76. * @since 0.4.0
  77. *
  78. * @return boolean
  79. */
  80. function found( $options=array() ) {
  81. return (boolean) $this->product;
  82. }
  83. /**
  84. * The product title
  85. *
  86. * @since 0.4.0
  87. * @return string
  88. */
  89. function title( $options=array() ) {
  90. // Return boolean if has flag was set
  91. if ( $options['supports'] )
  92. return it_exchange_product_supports_feature( $this->product->ID, 'title' );
  93. // Return boolean if has flag was set
  94. if ( $options['has'] )
  95. return it_exchange_product_has_feature( $this->product->ID, 'title' );
  96. // Repeats checks for when flags were not passed.
  97. if ( it_exchange_product_supports_feature( $this->product->ID, 'title' )
  98. && it_exchange_product_has_feature( $this->product->ID, 'title' ) ) {
  99. $result = '';
  100. $title = it_exchange_get_product_feature( $this->product->ID, 'title' );
  101. $defaults = array(
  102. 'wrap' => 'h1',
  103. 'format' => 'html',
  104. );
  105. $options = ITUtility::merge_defaults( $options, $defaults );
  106. if ( 'html' == $options['format'] )
  107. $result .= '<' . $options['wrap'] . ' class="entry-title">';
  108. $result .= $title;
  109. if ( 'html' == $options['format'] )
  110. $result .= '</' . $options['wrap'] . '>';
  111. return $result;
  112. }
  113. return false;
  114. }
  115. /**
  116. * The permalink
  117. *
  118. * @since 0.4.0
  119. * @return mixed
  120. */
  121. function permalink( $options=array() ) {
  122. $permalink = empty( $this->product->ID ) ? false : get_permalink( $this->product->ID );
  123. if ( $options['has'] )
  124. return (boolean) $permalink;
  125. $result = '';
  126. $defaults = array(
  127. 'before' => '<a href="',
  128. 'after' => '">' . it_exchange( 'product', 'get-title', 'format=' ) . '</a>',
  129. 'format' => 'html',
  130. );
  131. $options = ITUtility::merge_defaults( $options, $defaults );
  132. if ( 'html' == $options['format'] )
  133. $result .= $options['before'];
  134. $result .= $permalink;
  135. if ( 'html' == $options['format'] )
  136. $result .= $options['after'];
  137. return $result;
  138. }
  139. /**
  140. * The product base price
  141. *
  142. * @since 0.4.0
  143. * @return mixed
  144. */
  145. function base_price( $options=array() ) {
  146. // Return boolean if has flag was set
  147. if ( $options['supports'] )
  148. return it_exchange_product_supports_feature( $this->product->ID, 'base-price' );
  149. // Return boolean if has flag was set
  150. if ( $options['has'] )
  151. return it_exchange_product_has_feature( $this->product->ID, 'base-price' );
  152. if ( it_exchange_product_supports_feature( $this->product->ID, 'base-price' )
  153. && it_exchange_product_has_feature( $this->product->ID, 'base-price' ) ) {
  154. $result = '';
  155. $defaults = array(
  156. 'before' => '<span class="it-exchange-base-price">',
  157. 'after' => '</span>',
  158. 'format' => 'html',
  159. 'price' => false,
  160. 'free-label' => __( 'Free', 'it-l10n-ithemes-exchange' ),
  161. );
  162. $options = ITUtility::merge_defaults( $options, $defaults );
  163. // Grab product feature price
  164. $base_price = empty( $options['price'] ) ? it_exchange_get_product_feature( $this->product->ID, 'base-price' ): $options['price'];
  165. // Replace with Free label if needed
  166. $db_price = it_exchange_convert_to_database_number( $base_price );
  167. $price = empty( $db_price ) ? '<span class="free-label">' . $options['free-label'] . '</span>' : it_exchange_format_price( $base_price );
  168. $price = ( empty( $options['free-label'] ) && empty( $db_price ) ) ? it_exchange_format_price( $base_price ) : $price;
  169. if ( 'html' == $options['format'] )
  170. $result .= $options['before'];
  171. $result .= apply_filters( 'it_exchange_api_theme_product_base_price', $price, $this->product->ID );
  172. if ( 'html' == $options['format'] )
  173. $result .= $options['after'];
  174. return $result;
  175. }
  176. return false;
  177. }
  178. /**
  179. * The product's large description
  180. *
  181. * @since 0.4.0
  182. * @return string
  183. */
  184. function description( $options=array() ) {
  185. // Return boolean if has flag was set
  186. if ( $options['supports'] )
  187. return it_exchange_product_supports_feature( $this->product->ID, 'description' );
  188. // Return boolean if has flag was set
  189. if ( $options['has'] )
  190. return it_exchange_product_has_feature( $this->product->ID, 'description' );
  191. if ( it_exchange_product_supports_feature( $this->product->ID, 'description' )
  192. && it_exchange_product_has_feature( $this->product->ID, 'description' ) ) {
  193. $result = '';
  194. $description = it_exchange_get_product_feature( $this->product->ID, 'description' );
  195. $defaults = array(
  196. 'max-length' => false,
  197. 'ellipsis' => '...',
  198. 'more-text' => __( '(more info)', 'it-l10n-ithemes-exchange' )
  199. );
  200. $options = ITUtility::merge_defaults( $options, $defaults );
  201. if ( ! empty( $options['max-length'] ) && is_numeric( $options['max-length'] ) && strlen( $description ) > $options['max-length'] ) {
  202. $result = substr( wp_strip_all_tags( $description ), 0, $options['max-length'] );
  203. $result .= $options['ellipsis'] . ' <a href="' . get_permalink( $this->product->ID ) . '">' . $options['more-text'] . '</a>';
  204. } else {
  205. global $IT_Exchange_Pages;
  206. if ( has_filter( 'the_content', array( $IT_Exchange_Pages, 'fallback_filter_for_page_template' ) ) )
  207. $has_filter = true;
  208. else
  209. $has_filter = false;
  210. if ( $has_filter )
  211. remove_filter( 'the_content', array( $IT_Exchange_Pages, 'fallback_filter_for_page_template' ) );
  212. $result = apply_filters( 'the_content', $description );
  213. if ( $has_filter )
  214. add_filter( 'the_content', array( $IT_Exchange_Pages, 'fallback_filter_for_page_template' ) );
  215. }
  216. return $result;
  217. }
  218. return false;
  219. }
  220. /**
  221. * The extended description
  222. *
  223. * @since 0.4.0
  224. *
  225. * @return string
  226. */
  227. function extended_description( $options=array() ) {
  228. // Return boolean if has flag was set
  229. if ( $options['supports'] )
  230. return it_exchange_product_supports_feature( $this->product->ID, 'extended-description' );
  231. // Return boolean if has flag was set
  232. if ( $options['has'] )
  233. return it_exchange_product_has_feature( $this->product->ID, 'extended-description' );
  234. $result = false;
  235. $extended_desc = it_exchange_get_product_feature( $this->product->ID, 'extended-description' );
  236. $defaults = array(
  237. 'before' => '<div class="entry-content">',
  238. 'after' => '</div>',
  239. 'format' => 'html',
  240. );
  241. $options = ITUtility::merge_defaults( $options, $defaults );
  242. if ( 'html' == $options['format'] )
  243. $result .= $options['before'];
  244. $extended_desc = wpautop( $extended_desc );
  245. $extended_desc = shortcode_unautop( $extended_desc );
  246. $extended_desc = do_shortcode( $extended_desc );
  247. $result .= $extended_desc;
  248. if ( 'html' == $options['format'] )
  249. $result .= $options['after'];
  250. return $result;
  251. }
  252. /**
  253. * The product's WP excerpt
  254. *
  255. * @since 0.4.0
  256. * @return string
  257. */
  258. function excerpt( $options=array() ) {
  259. // Return boolean if has flag was set
  260. if ( $options['supports'] )
  261. return it_exchange_product_supports_feature( $this->product->ID, 'wp-excerpt' );
  262. // Return boolean if has flag was set
  263. if ( $options['has'] )
  264. return it_exchange_product_has_feature( $this->product->ID, 'wp-excerpt' );
  265. if ( it_exchange_product_supports_feature( $this->product->ID, 'wp-excerpt' )
  266. && it_exchange_product_has_feature( $this->product->ID, 'wp-excerpt' ) )
  267. return it_exchange_get_product_feature( $this->product->ID, 'wp-excerpt' );
  268. return false;
  269. }
  270. /**
  271. * The product author
  272. *
  273. * @since 0.4.0
  274. * @return string
  275. */
  276. function author( $options=array() ) {
  277. // Return boolean if has flag was set
  278. if ( $options['supports'] )
  279. return it_exchange_product_supports_feature( $this->product->ID, 'author' );
  280. // Return boolean if has flag was set
  281. if ( $options['has'] )
  282. return it_exchange_product_has_feature( $this->product->ID, 'author' );
  283. if ( it_exchange_product_supports_feature( $this->product->ID, 'author' )
  284. && it_exchange_product_has_feature( $this->product->ID, 'author' ) )
  285. return it_exchange_get_product_feature( $this->product->ID, 'author' );
  286. return false;
  287. }
  288. /**
  289. * The product purchase quantity (max purchase option by customer)
  290. *
  291. * @since 0.4.0
  292. * @return integer
  293. */
  294. function purchase_quantity( $options=array() ) {
  295. // Return boolean if has flag was set
  296. if ( $options['supports'] )
  297. return it_exchange_product_supports_feature( $this->product->ID, 'purchase-quantity' );
  298. // Return boolean if has flag was set
  299. if ( $options['has'] )
  300. return it_exchange_product_has_feature( $this->product->ID, 'purchase-quantity' );
  301. // Set options
  302. $defaults = array(
  303. 'before' => '',
  304. 'after' => '',
  305. 'format' => 'html',
  306. 'class' => 'product-purchase-quantity',
  307. 'unlimited-label' => __( 'Unlimited', 'it-l10n-ithemes-exchange' ),
  308. );
  309. $options = ITUtility::merge_defaults( $options, $defaults );
  310. $class = empty( $options['class'] ) ? '' : ' class="' . esc_attr( $options['class'] ) .'"';
  311. $var_key = it_exchange_get_field_name( 'product_purchase_quantity' );
  312. // Is the checkbox on add/edit products unchecked to allow quantities greater than 1
  313. if ( it_exchange_product_supports_feature( $this->product->ID, 'purchase-quantity' ) )
  314. $max_quantity = it_exchange_get_product_feature( $this->product->ID, 'purchase-quantity' );
  315. else
  316. return '';
  317. $max_quantity = empty( $max_quantity ) ? $options['unlimited-label'] : $max_quantity;
  318. // Lets do some inventory checking and make sure that if we're supporing inventory, that we don't allow max to be greater than inventory
  319. if ( it_exchange_product_supports_feature( $this->product->ID, 'inventory' ) ) {
  320. // If we support inventory, but we don't have any, and we've been passed the HTML format, return empty string
  321. if ( ! $inventory = it_exchange_get_product_feature( $this->product->ID, 'inventory' ) )
  322. return '';
  323. // Lets check product availability and return and empty string if its not available.
  324. if ( ! it_exchange( 'product', 'is-available' ) )
  325. return '';
  326. if ( (int) $max_quantity > 0 && (int) $max_quantity > $inventory )
  327. $max_quantity = $inventory;
  328. }
  329. // Return requested format
  330. switch ( $options['format'] ) {
  331. case 'max-quantity' :
  332. return $max_quantity;
  333. break;
  334. case 'html' :
  335. default :
  336. $html = '<input' . $class . ' type="number" name="' . esc_attr( $var_key ) . '" value="1" min="1" max="' . ( ! empty( $max_purchase_quantity ) ? $max_purchase_quantity : '' ) . '" />' . "\n";
  337. $html .= '<input type="hidden" name="' . it_exchange_get_field_name( 'product_max_purchase_quantity' ) . '[' . esc_attr( $this->product->ID ) . ']" value="' . ( $max_quantity ) . '" />';
  338. return $html;
  339. break;
  340. }
  341. }
  342. /**
  343. * The product's current inventory
  344. *
  345. * @since 0.4.0
  346. * @return integer
  347. */
  348. function inventory( $options=array() ) {
  349. // Return boolean if has flag was set
  350. if ( $options['supports'] )
  351. return it_exchange_product_supports_feature( $this->product->ID, 'inventory' );
  352. // Return boolean if has flag was set
  353. if ( $options['has'] )
  354. return it_exchange_product_has_feature( $this->product->ID, 'inventory' );
  355. if ( false !== it_exchange_product_supports_feature( $this->product->ID, 'inventory' )
  356. && false !== it_exchange_product_has_feature( $this->product->ID, 'inventory' ) )
  357. return it_exchange_get_product_feature( $this->product->ID, 'inventory' );
  358. return false;
  359. }
  360. /**
  361. * The product's dates purchase availability
  362. *
  363. * Use type of 'start', 'end', 'both', either in options
  364. *
  365. * @since 0.4.0
  366. * @return string
  367. */
  368. function availability( $options=array() ) {
  369. $defaults = array(
  370. 'type' => 'start',
  371. );
  372. $options = ITUtility::merge_defaults( $options, $defaults );
  373. // Return boolean if has flag was set
  374. if ( $options['supports'] )
  375. return it_exchange_product_supports_feature( $this->product->ID, 'availability' );
  376. if ( $options['has'] )
  377. return it_exchange_product_has_feature( $this->product->ID, 'availability', $options );
  378. if ( it_exchange_product_supports_feature( $this->product->ID, 'availability', $options ) )
  379. return it_exchange_get_product_feature( $this->product->ID, 'availability', $options );
  380. return true;
  381. }
  382. /**
  383. * Uses start and end availability dates to now to determine if the product is currently available
  384. *
  385. * @since 0.4.0
  386. *
  387. * @return boolean
  388. */
  389. function is_available( $options=array() ) {
  390. return it_exchange_is_product_available( $this->product->ID );
  391. }
  392. /**
  393. * The product's dates purchase availability
  394. *
  395. * Use type of 'start', 'end', 'both', either in options
  396. *
  397. * @since 0.4.0
  398. * @return string
  399. */
  400. function visibility( $options=array() ) {
  401. // Return boolean if has flag was set
  402. if ( $options['has'] )
  403. return ( false !== get_post_meta( $product_id, '_it-exchange-visibility', true ) );
  404. return get_post_meta( $product_id, '_it-exchange-visibility', true );
  405. }
  406. /**
  407. * Uses start and end availability dates to now to determine if the product is currently available
  408. *
  409. * @since 0.4.0
  410. *
  411. * @return boolean
  412. */
  413. function is_visible( $options=array() ) {
  414. return it_exchange_is_product_visible( $this->product->ID );
  415. }
  416. /**
  417. * The product's featured image
  418. *
  419. * @since 0.4.0
  420. *
  421. * @return string
  422. */
  423. function featured_image( $options=array() ) {
  424. // Return boolean if has flag was set
  425. if ( $options['supports'] )
  426. return it_exchange_product_supports_feature( $this->product->ID, 'product-images' );
  427. // Return boolean if has flag was set
  428. if ( $options['has'] )
  429. return it_exchange_product_has_feature( $this->product->ID, 'product-images' );
  430. if ( it_exchange_product_supports_feature( $this->product->ID, 'product-images' )
  431. && it_exchange_product_has_feature( $this->product->ID, 'product-images' ) ) {
  432. $defaults = array(
  433. 'size' => 'large'
  434. );
  435. $options = ITUtility::merge_defaults( $options, $defaults );
  436. $product_images = it_exchange_get_product_feature( $this->product->ID, 'product-images' );
  437. $feature_image = array(
  438. 'id' => $product_images[0],
  439. 'thumb' => wp_get_attachment_thumb_url( $product_images[0] ),
  440. 'large' => wp_get_attachment_url( $product_images[0] )
  441. );
  442. if ( 'thumbnail' === $options['size'] )
  443. $img_src = $feature_image['thumb'];
  444. else
  445. $img_src = $feature_image['large'];
  446. ob_start();
  447. ?>
  448. <div class="it-exchange-feature-image-<?php echo get_the_id(); ?> it-exchange-featured-image">
  449. <div class="featured-image-wrapper">
  450. <img alt="" src="<?php echo $img_src ?>" data-src-large="<?php echo $feature_image['large'] ?>" data-src-thumb="<?php echo $feature_image['thumb'] ?>" />
  451. </div>
  452. </div>
  453. <?php
  454. $output = ob_get_clean();
  455. return $output;
  456. }
  457. return false;
  458. }
  459. /**
  460. * Return product's images for all image sizes.
  461. *
  462. * @since 0.4.0
  463. *
  464. * @return string
  465. */
  466. function product_images( $options=array() ) {
  467. // Return boolean if has flag was set
  468. if ( $options['supports'] )
  469. return it_exchange_product_supports_feature( $this->product->ID, 'product-images' );
  470. // Return boolean if has flag was set
  471. if ( $options['has'] )
  472. return it_exchange_product_has_feature( $this->product->ID, 'product-images' );
  473. if ( it_exchange_product_supports_feature( $this->product->ID, 'product-images' )
  474. && it_exchange_product_has_feature( $this->product->ID, 'product-images' ) ) {
  475. $defaults = array(
  476. 'id' => null,
  477. 'size' => 'all' // NOTE These do nothing right now. Going to rething the options later. - Koop
  478. );
  479. $options = ITUtility::merge_defaults( $options, $defaults );
  480. $output = array();
  481. // Get the image sizes.
  482. $image_sizes = get_intermediate_image_sizes();
  483. // Add full to the $image_size array.
  484. array_push( $image_sizes, 'full' );
  485. $product_images = it_exchange_get_product_feature( $this->product->ID, 'product-images' );
  486. foreach( $product_images as $image_id ) {
  487. foreach ( $image_sizes as $size ) {
  488. $image['id'] = $image_id;
  489. $image[$size] = wp_get_attachment_image_src( $image_id, $size );
  490. }
  491. $images[] = $image;
  492. }
  493. $output = $images;
  494. return $output;
  495. }
  496. return false;
  497. }
  498. /**
  499. * The product's product image gallery
  500. *
  501. * @since 0.4.0
  502. *
  503. * @return string
  504. */
  505. function product_gallery( $options=array() ) {
  506. // Return boolean if has flag was set
  507. if ( $options['supports'] )
  508. return it_exchange_product_supports_feature( $this->product->ID, 'product-images' );
  509. // Return boolean if has flag was set
  510. if ( $options['has'] )
  511. return it_exchange_product_has_feature( $this->product->ID, 'product-images' );
  512. // Vidembed conflict. Temp fix until vidembed fixes their issue.
  513. remove_filter( 'image_downsize', 'ithemes_filter_image_downsize', 10, 3 );
  514. if ( it_exchange_product_supports_feature( $this->product->ID, 'product-images' )
  515. && it_exchange_product_has_feature( $this->product->ID, 'product-images' ) ) {
  516. $settings = it_exchange_get_option( 'settings_general' );
  517. $zoom = ( 1 == $settings['enable-gallery-zoom'] ) ? $settings['product-gallery-zoom-action'] : 'false';
  518. $popup = ( 1 == $settings['enable-gallery-popup'] ) ? 'true' : 'false';
  519. $defaults = array(
  520. 'size' => 'thumbnail', // thumbnail or large
  521. 'output' => 'gallery', // gallery or thumbnails
  522. 'zoom' => $zoom, // hover or click
  523. 'switch' => 'click', // hover or click
  524. 'images' => false,
  525. );
  526. $options = ITUtility::merge_defaults( $options, $defaults );
  527. $output = NULL;
  528. $product_images = empty( $options['images'] ) ? it_exchange_get_product_feature( $this->product->ID, 'product-images' ) : (array) $options['images'];
  529. switch( $options['output'] ) {
  530. case 'thumbnails' :
  531. if ( !empty( $product_images ) ) {
  532. ob_start();
  533. ?>
  534. <div class="it-exchange-product-images-gallery-<?php echo get_the_id(); ?> it-exchange-product-images-gallery it-exchange-gallery-thumbnails">
  535. <?php if ( count( $product_images ) > 1 ) : ?>
  536. <ul class="it-exchange-thumbnail-images-<?php echo get_the_ID(); ?> it-exchange-thumbnail-images">
  537. <?php foreach( $product_images as $image_id ) : ?>
  538. <?php
  539. $img_url = wp_get_attachment_url( $image_id );
  540. $img_thumb_url = wp_get_attachment_thumb_url( $image_id );
  541. ?>
  542. <li class="it-exchange-product-image-thumb-<?php echo $image_id; ?>">
  543. <span><img alt="" src="<?php echo $img_thumb_url; ?>" data-src-large="<?php echo $img_url; ?>" data-src-thumb="<?php echo $img_thumb_url; ?>"></span>
  544. </li>
  545. <?php endforeach; ?>
  546. </ul>
  547. <?php endif; ?>
  548. </div>
  549. <?php
  550. $output = ob_get_clean();
  551. }
  552. break;
  553. case 'gallery' :
  554. default :
  555. if ( ! empty( $product_images ) ) {
  556. $featured = array(
  557. 'full' => wp_get_attachment_image_src( $product_images[0], 'full' ),
  558. 'large' => wp_get_attachment_image_src( $product_images[0], 'large' ),
  559. 'thumb' => wp_get_attachment_image_src( $product_images[0], 'thumbnail' ),
  560. );
  561. ob_start();
  562. ?>
  563. <div id="it-exchange-product-images-gallery-<?php echo get_the_id(); ?>" class="it-exchange-product-images-gallery it-exchange-gallery-full" data-popup="<?php echo $popup; ?>" data-zoom="<?php echo $options["zoom"]; ?>" data-switch="<?php echo $options['switch']; ?>">
  564. <div class="it-exchange-feature-image-<?php echo get_the_ID(); ?> it-exchange-featured-image">
  565. <div class="featured-image-wrapper">
  566. <img alt="" class="featured-image" src="<?php echo $featured['large'][0] ?>" data-src-full="<?php echo $featured['full'][0] ?>" data-src-large="<?php echo $featured['large'][0] ?>" data-height-large="<?php echo $featured['large'][2] ?>" data-featured-position="">
  567. </div>
  568. </div>
  569. <?php if ( count( $product_images ) > 1 ) : ?>
  570. <ul class="it-exchange-thumbnail-images-<?php echo get_the_ID(); ?> it-exchange-thumbnail-images">
  571. <?php $img_iteration = 0; ?>
  572. <?php foreach( $product_images as $image_id ) : ?>
  573. <?php
  574. if ( $img_iteration == 0 )
  575. $img_class = 'current';
  576. else
  577. $img_class = '';
  578. $thumbnail = array(
  579. 'full' => wp_get_attachment_image_src( $image_id, 'full' ),
  580. 'large' => wp_get_attachment_image_src( $image_id, 'large' ),
  581. 'thumb' => wp_get_attachment_image_src( $image_id, 'thumbnail' ),
  582. );
  583. $dumped[] = $thumbnail;
  584. ?>
  585. <li class="it-exchange-product-image-thumb-<?php echo $image_id; ?>">
  586. <span class="<?php echo $img_class; ?>"><img alt="" src="<?php echo $thumbnail['thumb'][0] ?>" data-src-full="<?php echo $thumbnail['full'][0] ?>" data-src-large="<?php echo $thumbnail['large'][0] ?>" data-height-large="<?php echo $thumbnail['large'][2] ?>" data-src-thumb="<?php echo $thumbnail['thumb'][0] ?>" data-featured-padding="" /></span>
  587. </li>
  588. <?php $img_iteration++; ?>
  589. <?php endforeach; ?>
  590. </ul>
  591. <?php endif; ?>
  592. </div>
  593. <?php
  594. $output = ob_get_clean();
  595. }
  596. break;
  597. }
  598. $output = apply_filters( 'it_exchange_product_gallery', $output );
  599. return $output;
  600. }
  601. return false;
  602. }
  603. /**
  604. * Returns downloads for product.
  605. *
  606. * If has option is true, returns boolean
  607. *
  608. * @since 0.4.0
  609. *
  610. * @return boolean
  611. */
  612. function downloads( $options=array() ) {
  613. // Return boolean if has flag was set
  614. if ( $options['supports'] )
  615. return it_exchange_product_supports_feature( $this->product->ID, 'downloads' );
  616. // Return boolean if has flag was set
  617. if ( $options['has'] )
  618. return it_exchange_product_has_feature( $this->product->ID, 'downloads' );
  619. // If we made it here, we're doing a loop of downloads for the current product.
  620. // This will init/reset the downloads global and loop through them. the /api/theme/download.php file will handle individual downloads.
  621. if ( empty( $GLOBALS['it_exchange']['downloads'] ) ) {
  622. $GLOBALS['it_exchange']['downloads'] = it_exchange_get_product_feature( $this->product->ID, 'downloads' );
  623. $GLOBALS['it_exchange']['download'] = reset( $GLOBALS['it_exchange']['downloads'] );
  624. return true;
  625. } else {
  626. if ( next( $GLOBALS['it_exchange']['downloads'] ) ) {
  627. $GLOBALS['it_exchange']['download'] = current( $GLOBALS['it_exchange']['downloads'] );
  628. return true;
  629. } else {
  630. $GLOBALS['it_exchange']['downloads'] = array();
  631. end( $GLOBALS['it_exchange']['downloads'] );
  632. $GLOBALS['it_exchange']['download'] = false;
  633. return false;
  634. }
  635. }
  636. }
  637. /**
  638. * Returns the buy now or add_to_cart form. Or both.
  639. *
  640. * Options:
  641. * - buy-now-before: Gets added before the buy-now form
  642. * - buy-now-after: Gets added after the buy-now form
  643. * - buy-now-class: A CSS class applied to the buy-now button
  644. * - buy-now-label: The HTML value of the buy now button.
  645. * - buy-now-button-type: The button-type: submit or button. Default is submit
  646. * - buy-now-button-name: The default is false. No name attribute is provided when false
  647. * - add-to-cart-before: Gets added before the buy-now form
  648. * - add-to-cart-after: Gets added after the buy-now form
  649. * - add-to-cart-class: A CSS class applied to the buy-now button
  650. * - add-to-cart-label: The HTML value of the buy now button.
  651. * - add-to-cart-button-type: The button-type: submit or button. Default is submit
  652. * - add-to-cart-button-name: The default is false. No name attribute is provided when false
  653. *
  654. * @since 0.4.0
  655. * @return string
  656. */
  657. function purchase_options( $options=array() ) {
  658. // Return boolean if has flag was set. Just keeping this here since its in all other product.php methods
  659. if ( $options['supports'] )
  660. return true;
  661. // Return boolean if has flag was set
  662. if ( $options['has'] )
  663. return true;
  664. // Parse options
  665. $result = false;
  666. $defaults = array(
  667. 'type' => false,
  668. 'class' => false,
  669. 'buy-now-before' => '',
  670. 'buy-now-after' => '',
  671. 'buy-now-class' => false,
  672. 'buy-now-label' => __( 'Buy Now', 'it-l10n-ithemes-exchange' ),
  673. 'buy-now-button-type' => 'submit',
  674. 'buy-now-button-name' => false,
  675. 'buy-now-edit-quantity' => true,
  676. 'add-to-cart-before' => '',
  677. 'add-to-cart-after' => '',
  678. 'add-to-cart-class' => false,
  679. 'add-to-cart-label' => __( 'Add to Cart', 'it-l10n-ithemes-exchange' ),
  680. 'add-to-cart-button-type' => 'submit',
  681. 'add-to-cart-button-name' => false,
  682. 'add-to-cart-edit-quantity' => true,
  683. 'out-of-stock-text' => __( 'Product is currently out of stock.', 'it-l10n-ithemes-exchange' ),
  684. 'not-available-text' => __( 'Product not available right now.', 'it-l10n-ithemes-exchange' ),
  685. 'product-in-stock' => null,
  686. );
  687. $options = ITUtility::merge_defaults( $options, $defaults );
  688. // If we are tracking inventory, lets make sure we have some available
  689. $product_in_stock = it_exchange_product_supports_feature( $this->product->ID, 'inventory' ) ? it_exchange_product_has_feature( $this->product->ID, 'inventory' ) : true;
  690. $product_in_stock = is_null( $options['product-in-stock'] ) ? $product_in_stock : $options['product-in-stock'];
  691. // If we're supporting availability dates, check that
  692. $product_is_available = it_exchange( 'product', 'is-available' );
  693. // Do we have multi-item cart add-on enabled?
  694. $multi_item_cart = it_exchange_is_multi_item_cart_allowed();
  695. // Init empty hidden field variables
  696. $buy_now_hidden_fields = $add_to_cart_hidden_fields = '';
  697. $output = '';
  698. if ( !$product_in_stock )
  699. return '<p class="out-of-stock">' . esc_attr( $options['out-of-stock-text'] ) . '</p>';
  700. if ( !$product_is_available )
  701. return __( 'Product is currently not available.', 'it-l10n-ithemes-exchange' );
  702. $class = $options['class'];
  703. // Set buy-now options
  704. $options['before'] = $options['buy-now-before'];
  705. $options['after'] = $options['buy-now-after'];
  706. $options['class'] = $class . ' ' . $options['buy-now-class'];
  707. $options['label'] = $options['buy-now-label'];
  708. $options['button-type'] = $options['buy-now-button-type'];
  709. $options['button-name'] = $options['buy-now-button-name'];
  710. $options['edit-quantity'] = $options['buy-now-edit-quantity'];
  711. // Add add-to-cart form to output if not multicart or is multicart and no products in cart
  712. // and/or template asked for it
  713. if ( ( ! $multi_item_cart || ( $multi_item_cart && 0 === it_exchange_get_cart_products_count() ) )
  714. && ( empty( $options['type'] ) || 'buy-now' == $options['type'] ) )
  715. $output .= it_exchange( 'product', 'get-buy-now', $options );
  716. // Set add-to-cart options
  717. $options['before'] = $options['add-to-cart-before'];
  718. $options['after'] = $options['add-to-cart-after'];
  719. $options['class'] = $class . ' ' . $options['add-to-cart-class'];
  720. $options['label'] = $options['add-to-cart-label'];
  721. $options['button-type'] = $options['add-to-cart-button-type'];
  722. $options['button-name'] = $options['add-to-cart-button-name'];
  723. $options['edit-quantity'] = $options['add-to-cart-edit-quantity'];
  724. // Add add-to-cart form to output if multicart and/or template asked for it.
  725. if ( $multi_item_cart
  726. && ( empty( $options['type'] ) || 'add-to-cart' == $options['type'] ) )
  727. $output .= it_exchange( 'product', 'get-add-to-cart', $options );
  728. // Return output
  729. return $output;
  730. }
  731. /**
  732. * Outputs the super widget.
  733. *
  734. * Options:
  735. * - class : HTML Class to add to div surrounding the super widget
  736. *
  737. * @since 0.4.0
  738. * @params array $options
  739. * @return void
  740. */
  741. function superwidget( $options=array() ) {
  742. // Return boolean if has flag was set. Just keeping this here since its in all other product.php methods
  743. if ( $options['supports'] )
  744. return true;
  745. // Return boolean if has flag was set
  746. if ( $options['has'] )
  747. return true;
  748. // Parse options
  749. $result = false;
  750. $defaults = array(
  751. 'class' => false,
  752. );
  753. $options = ITUtility::merge_defaults( $options, $defaults );
  754. $args['before_widget'] = '<div class="it-exchange-product-sw single-product-super-widget ' . esc_attr( $options['class'] ) . '">';
  755. $args['after_widget'] = '</div>';
  756. $args['enqueue_hide_script'] = false;
  757. the_widget( 'IT_Exchange_Super_Widget', array(), $args );
  758. }
  759. function buy_now( $options=array() ) {
  760. // Return boolean if has flag was set. Just keeping this here since its in all other product.php methods
  761. if ( $options['supports'] )
  762. return true;
  763. // Return boolean if has flag was set
  764. if ( $options['has'] )
  765. return true;
  766. // Setting this filter to true will disable the Buy Now Button
  767. if ( apply_filters( 'it_exchange_disable_buy_now', false, $this->product ) )
  768. return '';
  769. // Parse options
  770. $result = false;
  771. $defaults = array(
  772. 'before' => '',
  773. 'after' => '',
  774. 'class' => false,
  775. 'label' => __( 'Buy Now', 'it-l10n-ithemes-exchange' ),
  776. 'button-type' => 'submit',
  777. 'button-name' => false,
  778. 'out-of-stock-text' => __( 'Out of stock.', 'it-l10n-ithemes-exchange' ),
  779. 'not-available-text' => __( 'Product not available right now.', 'it-l10n-ithemes-exchange' ),
  780. 'edit-quantity' => true,
  781. 'product-in-stock' => null,
  782. );
  783. $options = ITUtility::merge_defaults( $options, $defaults );
  784. // Allow options to be filtered
  785. $options = apply_filters( 'it_exchange_product_theme_api_buy_now_options', $options, $this->product->ID );
  786. // If we are tracking inventory, lets make sure we have some available
  787. $product_in_stock = it_exchange_product_supports_feature( $this->product->ID, 'inventory' ) ? it_exchange_product_has_feature( $this->product->ID, 'inventory' ) : true;
  788. $product_in_stock = is_null( $options['product-in-stock'] ) ? $product_in_stock : $options['product-in-stock'];
  789. // If we're supporting availability dates, check that
  790. $product_is_available = it_exchange( 'product', 'is-available' );
  791. $output = '';
  792. $class = empty( $options['class'] ) ? 'buy-now-button' : 'buy-now-button ' . esc_attr( $options['class'] );
  793. $var_key = it_exchange_get_field_name( 'buy_now' );
  794. $var_value = $this->product->ID;
  795. $button_name = empty( $options['button-name'] ) ? '' : ' name="' . esc_attr( $options['button-name'] ) . '"';
  796. $button = '<input' . $button_name . ' type="' . esc_attr( $options['button-type'] ) . '" value="' . esc_attr( $options['label'] ) . '" class="' . esc_attr( $class ) . '" />';
  797. $hidden_fields = '<input type="hidden" name="it-exchange-action" value="buy_now" />';
  798. $hidden_fields .= '<input class="buy-now-product-id" type="hidden" name="' . esc_attr( $var_key ). '" value="' . esc_attr( $var_value ). '" />';
  799. $hidden_fields .= wp_nonce_field( 'it-exchange-purchase-product-' . $this->product->ID, '_wpnonce', true, false );
  800. if ( ! $product_in_stock )
  801. return '<p class="out-of-stock">' . esc_attr( $options['out-of-stock-text'] ) . '</p>';
  802. if ( ! $product_is_available )
  803. return '<p>' . esc_attr( $options['not-available-text'] ) . '</p>';
  804. $result = '<form action="" method="post" class="it-exchange-sw-purchase-options it-exchange-sw-buy-now ' . esc_attr( $class ) . '">';
  805. $result .= $hidden_fields;
  806. if ( $options['edit-quantity'] )
  807. $result .= it_exchange( 'product', 'get-purchase-quantity' );
  808. $result .= $button;
  809. $result .= '</form>';
  810. return $result;
  811. }
  812. function add_to_cart( $options=array() ) {
  813. // Return boolean if has flag was set. Just keeping this here since its in all other product.php methods
  814. if ( $options['supports'] )
  815. return true;
  816. // Return boolean if has flag was set
  817. if ( $options['has'] )
  818. return true;
  819. // Parse options
  820. $result = false;
  821. $defaults = array(
  822. 'before' => '',
  823. 'after' => '',
  824. 'class' => false,
  825. 'label' => __( 'Add to Cart', 'it-l10n-ithemes-exchange' ),
  826. 'button-type' => 'submit',
  827. 'button-name' => false,
  828. 'out-of-stock-text' => __( 'Out of stock.', 'it-l10n-ithemes-exchange' ),
  829. 'not-available-text' => __( 'Product not available right now.', 'it-l10n-ithemes-exchange' ),
  830. 'edit-quantity' => true,
  831. 'max-quantity-text' => __( 'Max Quantity Reached', 'it-l10n-ithemes-exchange' ),
  832. 'product-in-stock' => null,
  833. );
  834. $options = ITUtility::merge_defaults( $options, $defaults );
  835. // If we are tracking inventory, lets make sure we have some available
  836. $product_in_stock = it_exchange_product_supports_feature( $this->product->ID, 'inventory' ) ? it_exchange_product_has_feature( $this->product->ID, 'inventory' ) : true;
  837. $product_in_stock = is_null( $options['product-in-stock'] ) ? $product_in_stock : $options['product-in-stock'];
  838. // If we're supporting availability dates, check that
  839. $product_is_available = it_exchange( 'product', 'is-available' );
  840. // Do we have multi-item cart add-on enabled?
  841. $multi_item_cart = it_exchange_is_multi_item_cart_allowed();
  842. // Init empty hidden field variables
  843. $buy_now_hidden_fields = $add_to_cart_hidden_fields = '';
  844. $class = empty( $options['class'] ) ? 'add-to-cart-button' : 'add-to-cart-button ' . esc_attr( $options['class'] );
  845. $var_key = it_exchange_get_field_name( 'add_product_to_cart' );
  846. $var_value = $this->product->ID;
  847. $button_name = empty( $options['button-name'] ) ? '' : ' name="' . esc_attr( $options['button-name'] ) . '"';
  848. $button = '<input' . $button_name . ' type="' . esc_attr( $options['button-type'] ) . '" value="' . esc_attr( $options['label'] ) . '" class="' . esc_attr( $class ) . '" />';
  849. $hidden_fields = '<input type="hidden" name="it-exchange-action" value="add_product_to_cart" />';
  850. $hidden_fields .= '<input class="add-to-cart-product-id" type="hidden" name="' . esc_attr( $var_key ). '" value="' . esc_attr( $var_value ). '" />';
  851. $hidden_fields .= wp_nonce_field( 'it-exchange-purchase-product-' . $this->product->ID, '_wpnonce', true, false );
  852. if ( it_exchange_product_supports_feature( $this->product->ID, 'purchase-quantity' ) && it_exchange_product_has_feature( $this->product->ID, 'purchase-quantity' ) ) {
  853. $quantity = it_exchange_get_cart_product_quantity_by_product_id( $this->product->ID );
  854. $max_quantity = it_exchange_get_product_feature( $this->product->ID, 'purchase-quantity' );
  855. if ( $quantity < $max_quantity )
  856. $can_add_more = true;
  857. else
  858. $can_add_more = false;
  859. } else {
  860. $can_add_more = true;
  861. }
  862. if ( !$can_add_more )
  863. return '<p>' . esc_attr( $options['max-quantity-text'] ) . '</p>';
  864. if ( ! $product_in_stock )
  865. return '<p class="out-of-stock">' . esc_attr( $options['out-of-stock-text'] ) . '</p>';
  866. if ( ! $product_is_available )
  867. return '<p>' . esc_attr( $options['not-available-text'] ) . '</p>';
  868. if ( ! $multi_item_cart )
  869. return '';
  870. $result = '<form action="" method="post" class="it-exchange-sw-purchase-options it-exchange-sw-add-to-cart ' . esc_attr( $class ) . '">';
  871. $result .= $hidden_fields;
  872. if ( $options['edit-quantity'] )
  873. $result .= it_exchange( 'product', 'get-purchase-quantity' );
  874. $result .= $button;
  875. $result .= '</form>';
  876. return $result;
  877. }
  878. /**
  879. * Returns a buy_now var
  880. *
  881. * @since 0.4.0
  882. *
  883. * @param array $options
  884. * @return string
  885. */
  886. function buy_now_var( $options ) {
  887. // Return boolean if has flag was set.
  888. if ( $options['supports'] )
  889. return true;
  890. // Return boolean if has flag was set
  891. if ( $options['has'] )
  892. return true;
  893. // Parse options
  894. $defaults = array(
  895. 'format' => 'key',
  896. );
  897. $options = ITUtility::merge_defaults( $options, $defaults );
  898. if ( 'key' == $format )
  899. return it_exchange_get_field_name( 'buy_now' );
  900. else
  901. return $this->product->ID;
  902. }
  903. /**
  904. * Returns a add_to_cart var
  905. *
  906. * @since 0.4.0
  907. *
  908. * @param array $options
  909. * @return string
  910. */
  911. function add_to_cart_var( $options ) {
  912. // Return boolean if has flag was set.
  913. if ( $options['supports'] )
  914. return true;
  915. // Return boolean if has flag was set
  916. if ( $options['has'] )
  917. return true;
  918. // Parse options
  919. $defaults = array(
  920. 'format' => 'key',
  921. );
  922. $options = ITUtility::merge_defaults( $options, $defaults );
  923. if ( 'key' == $format )
  924. return it_exchange_get_field_name( 'add_product_to_cart' );
  925. else
  926. return $this->product->ID;
  927. }
  928. }