PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/shortcodes/init.php

https://github.com/ThemesWpFr/jigoshop
PHP | 437 lines | 327 code | 79 blank | 31 comment | 27 complexity | 4851d3385b19320e627f1c4bc69d1f1f MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Jigoshop shortcodes
  4. *
  5. * DISCLAIMER
  6. *
  7. * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  8. * versions in the future. If you wish to customise Jigoshop core for your needs,
  9. * please use our GitHub repository to publish essential changes for consideration.
  10. *
  11. * @package Jigoshop
  12. * @category Core
  13. * @author Jigowatt
  14. * @copyright Copyright Š 2011-2012 Jigowatt Ltd.
  15. * @license http://jigoshop.com/license/commercial-edition
  16. */
  17. include_once('cart.php');
  18. include_once('checkout.php');
  19. include_once('my_account.php');
  20. include_once('order_tracking.php');
  21. include_once('pay.php');
  22. include_once('thankyou.php');
  23. function jigoshop_shortcode_wrapper( $function, $atts = array() ) {
  24. // WordPress caching of shortcodes stripped out in version 1.4.9 for compatibility with Cache plugins on Cart and Checkout
  25. ob_start();
  26. call_user_func( $function, $atts );
  27. return ob_get_clean();
  28. }
  29. //### Recent Products #########################################################
  30. function jigoshop_recent_products( $atts ) {
  31. global $columns, $per_page, $paged;
  32. $jigoshop_options = Jigoshop_Base::get_options();
  33. extract( shortcode_atts( array(
  34. 'per_page' => $jigoshop_options->get_option('jigoshop_catalog_per_page'),
  35. 'columns' => $jigoshop_options->get_option('jigoshop_catalog_columns'),
  36. 'orderby' => 'date',
  37. 'order' => 'desc',
  38. 'pagination'=> false
  39. ), $atts));
  40. $args = array(
  41. 'post_type' => 'product',
  42. 'post_status' => 'publish',
  43. 'ignore_sticky_posts'=> 1,
  44. 'posts_per_page' => $per_page,
  45. 'orderby' => $orderby,
  46. 'order' => $order,
  47. 'paged' => $paged,
  48. 'meta_query' => array(
  49. array(
  50. 'key' => 'visibility',
  51. 'value' => array( 'catalog', 'visible' ),
  52. 'compare'=> 'IN'
  53. )
  54. )
  55. );
  56. query_posts( $args );
  57. ob_start();
  58. jigoshop_get_template_part( 'loop', 'shop' );
  59. if($pagination) do_action('jigoshop_pagination');
  60. wp_reset_query();
  61. return ob_get_clean();
  62. }
  63. //### Multiple Products #########################################################
  64. function jigoshop_products( $atts ){
  65. global $columns, $paged;
  66. $jigoshop_options = Jigoshop_Base::get_options();
  67. if ( empty( $atts )) return;
  68. extract( shortcode_atts( array(
  69. 'per_page' => $jigoshop_options->get_option('jigoshop_catalog_per_page'),
  70. 'columns' => $jigoshop_options->get_option('jigoshop_catalog_columns'),
  71. 'orderby' => $jigoshop_options->get_option('jigoshop_catalog_sort_orderby'),
  72. 'order' => $jigoshop_options->get_option('jigoshop_catalog_sort_direction'),
  73. 'pagination'=> false
  74. ), $atts));
  75. $args = array(
  76. 'post_type' => 'product',
  77. 'post_status' => 'publish',
  78. 'ignore_sticky_posts'=> 1,
  79. 'orderby' => $orderby,
  80. 'order' => $order,
  81. 'paged' => $paged,
  82. 'meta_query' => array(
  83. array(
  84. 'key' => 'visibility',
  85. 'value' => array( 'catalog', 'visible' ),
  86. 'compare'=> 'IN'
  87. )
  88. )
  89. );
  90. if ( isset( $atts['skus'] )){
  91. $skus = explode( ',', $atts['skus'] );
  92. array_walk( $skus, create_function('&$val', '$val = trim($val);') );
  93. $args['meta_query'][] = array(
  94. 'key' => 'sku',
  95. 'value' => $skus,
  96. 'compare' => 'IN'
  97. );
  98. }
  99. if ( isset( $atts['ids'] )){
  100. $ids = explode( ',', $atts['ids'] );
  101. array_walk( $ids, create_function('&$val', '$val = trim($val);') );
  102. $args['post__in'] = $ids;
  103. }
  104. query_posts( $args );
  105. ob_start();
  106. jigoshop_get_template_part( 'loop', 'shop' );
  107. if($pagination) do_action('jigoshop_pagination');
  108. wp_reset_query();
  109. return ob_get_clean();
  110. }
  111. //### Single Product ############################################################
  112. function jigoshop_product( $atts ){
  113. if ( empty( $atts )) return;
  114. $args = array(
  115. 'post_type' => 'product',
  116. 'posts_per_page'=> 1,
  117. 'post_status' => 'publish',
  118. 'meta_query' => array(
  119. array(
  120. 'key' => 'visibility',
  121. 'value' => array( 'catalog', 'visible' ),
  122. 'compare'=> 'IN'
  123. )
  124. )
  125. );
  126. if ( isset( $atts['sku'] )){
  127. $args['meta_query'][] = array(
  128. 'key' => 'sku',
  129. 'value' => $atts['sku'],
  130. 'compare'=> '='
  131. );
  132. }
  133. if ( isset( $atts['id'] )){
  134. $args['p'] = $atts['id'];
  135. }
  136. query_posts( $args );
  137. ob_start();
  138. jigoshop_get_template_part( 'loop', 'shop' );
  139. wp_reset_query();
  140. return ob_get_clean();
  141. }
  142. //### Featured Products #########################################################
  143. function jigoshop_featured_products( $atts ) {
  144. global $columns, $per_page, $paged;
  145. $jigoshop_options = Jigoshop_Base::get_options();
  146. extract( shortcode_atts( array(
  147. 'per_page' => $jigoshop_options->get_option('jigoshop_catalog_per_page'),
  148. 'columns' => $jigoshop_options->get_option('jigoshop_catalog_columns'),
  149. 'orderby' => $jigoshop_options->get_option('jigoshop_catalog_sort_orderby'),
  150. 'order' => $jigoshop_options->get_option('jigoshop_catalog_sort_direction'),
  151. 'pagination'=> false
  152. ), $atts));
  153. $args = array(
  154. 'post_type' => 'product',
  155. 'post_status' => 'publish',
  156. 'ignore_sticky_posts'=> 1,
  157. 'posts_per_page' => $per_page,
  158. 'orderby' => $orderby,
  159. 'order' => $order,
  160. 'paged' => $paged,
  161. 'meta_query' => array(
  162. array(
  163. 'key' => 'visibility',
  164. 'value' => array( 'catalog', 'visible' ),
  165. 'compare'=> 'IN'
  166. ),
  167. array(
  168. 'key' => 'featured',
  169. 'value' => true
  170. )
  171. )
  172. );
  173. query_posts( $args );
  174. ob_start();
  175. jigoshop_get_template_part( 'loop', 'shop' );
  176. if($pagination) do_action('jigoshop_pagination');
  177. wp_reset_query();
  178. return ob_get_clean();
  179. }
  180. //### Category #########################################################
  181. function jigoshop_product_category( $atts ) {
  182. global $columns, $per_page, $paged;
  183. $jigoshop_options = Jigoshop_Base::get_options();
  184. if ( empty( $atts ) ) return;
  185. extract( shortcode_atts( array(
  186. 'slug' => '',
  187. 'per_page' => $jigoshop_options->get_option('jigoshop_catalog_per_page'),
  188. 'columns' => $jigoshop_options->get_option('jigoshop_catalog_columns'),
  189. 'orderby' => $jigoshop_options->get_option('jigoshop_catalog_sort_orderby'),
  190. 'order' => $jigoshop_options->get_option('jigoshop_catalog_sort_direction'),
  191. 'pagination' => false,
  192. 'tax_operator' => 'IN'
  193. ), $atts));
  194. if ( ! $slug ) return;
  195. /** Operator validation. */
  196. if( !in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) )
  197. $tax_operator = 'IN';
  198. /** Multiple category values. */
  199. if ( !empty($slug) ) {
  200. $slug = explode( ',', esc_attr( $slug ) );
  201. $slug = array_map('trim', $slug);
  202. }
  203. $args = array(
  204. 'post_type' => 'product',
  205. 'post_status' => 'publish',
  206. 'ignore_sticky_posts' => 1,
  207. 'posts_per_page' => $per_page,
  208. 'orderby' => $orderby,
  209. 'order' => $order,
  210. 'paged' => $paged,
  211. 'meta_query' => array(
  212. array(
  213. 'key' => 'visibility',
  214. 'value' => array( 'catalog', 'visible' ),
  215. 'compare' => 'IN'
  216. )
  217. ),
  218. 'tax_query' => array(
  219. array(
  220. 'taxonomy' => 'product_cat',
  221. 'field' => 'slug',
  222. 'terms' => $slug,
  223. 'operator' => $tax_operator
  224. )
  225. )
  226. );
  227. query_posts( $args );
  228. ob_start();
  229. jigoshop_get_template_part( 'loop', 'shop' );
  230. if($pagination) do_action('jigoshop_pagination');
  231. wp_reset_query();
  232. return ob_get_clean();
  233. }
  234. //### Add to cart URL for single product #########################################################
  235. function jigoshop_product_add_to_cart_url( $atts ) {
  236. if ( empty( $atts ) ) return;
  237. global $wpdb;
  238. if ($atts['id']) :
  239. $product_meta = get_post( $atts['id'] );
  240. elseif ($atts['sku']) :
  241. $product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='sku' AND meta_value=%s LIMIT 1", $atts['sku']));
  242. $product_meta = get_post( $product_id );
  243. else :
  244. return;
  245. endif;
  246. if ($product_meta->post_type!=='product') return;
  247. $_product = new jigoshop_product( $product_meta->ID );
  248. return esc_url( $_product->add_to_cart_url() );
  249. }
  250. //### Cart button + optional price for single product #########################################################
  251. function jigoshop_product_add_to_cart( $atts ) {
  252. if (empty($atts)) return;
  253. global $wpdb;
  254. if (!$atts['class']) $atts['class'] = 'product';
  255. if ($atts['id']) :
  256. $product_meta = get_post( $atts['id'] );
  257. elseif ($atts['sku']) :
  258. $product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='sku' AND meta_value=%s LIMIT 1", $atts['sku']));
  259. $product_meta = get_post( $product_id );
  260. else :
  261. return;
  262. endif;
  263. if ($product_meta->post_type!=='product') return;
  264. $_product = new jigoshop_product( $product_meta->ID );
  265. if (!$_product->is_visible()) return;
  266. ob_start();
  267. ?>
  268. <p class="<?php echo esc_attr( $atts['class'] ); ?>">
  269. <?php if ($atts['price'] != 'no') echo $_product->get_price_html(); ?>
  270. <?php jigoshop_template_loop_add_to_cart( $product_meta, $_product ); ?>
  271. </p><?php
  272. return ob_get_clean();
  273. }
  274. //### Search shortcode #########################################################
  275. function jigoshop_search_shortcode( $atts ) {
  276. // Extract the arguments
  277. extract( $args );
  278. // Construct the form
  279. $form = '<form role="search" method="get" id="searchform" action="' . home_url() . '">';
  280. $form .= '<div>';
  281. $form .= '<label class="assistive-text" for="s">' . __('Search for:', 'jigoshop') . '</label>';
  282. $form .= '<input type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="' . __('Search for products', 'jigoshop') . '" />';
  283. $form .= '<input type="submit" id="searchsubmit" value="' . __('Search', 'jigoshop') . '" />';
  284. $form .= '<input type="hidden" name="post_type" value="product" />';
  285. $form .= '</div>';
  286. $form .= '</form>';
  287. // Apply a filter to allow for additional fields
  288. echo apply_filters('jigoshop_product_search_shortcode', $form, $instance);
  289. }
  290. //### Sale products shortcode #########################################################
  291. function jigoshop_sale_products( $atts ) {
  292. global $columns, $per_page, $paged;
  293. extract(shortcode_atts(array(
  294. 'per_page' => Jigoshop_Base::get_options()->get_option('jigoshop_catalog_per_page'),
  295. 'columns' => Jigoshop_Base::get_options()->get_option('jigoshop_catalog_columns'),
  296. 'orderby' => Jigoshop_Base::get_options()->get_option('jigoshop_catalog_sort_orderby'),
  297. 'order' => Jigoshop_Base::get_options()->get_option('jigoshop_catalog_sort_direction'),
  298. 'pagination' => false
  299. ), $atts));
  300. $today = strtotime(date('Y-m-d',mktime(0, 0, 0, date("m"), date("d"), date("Y")) ));
  301. $tomorrow = strtotime(date('Y-m-d',mktime(0, 0, 0, date("m"), date("d")+1, date("Y")) ));
  302. $args = array(
  303. 'post_type' => array( 'product' ),
  304. 'post_status' => 'publish',
  305. 'ignore_sticky_posts' => 1,
  306. 'posts_per_page' => $per_page,
  307. 'orderby' => $orderby,
  308. 'order' => $order,
  309. 'paged' => $paged,
  310. 'meta_query' => array(
  311. array(
  312. 'key' => 'visibility',
  313. 'value' => array( 'catalog', 'visible' ),
  314. 'compare' => 'IN'
  315. ),
  316. array(
  317. 'key' => 'sale_price',
  318. 'value' => '',
  319. 'compare' => '!=',
  320. ),
  321. array(
  322. 'key' => 'sale_price_dates_from',
  323. 'value' => $today,
  324. 'compare' => '<=',
  325. ),
  326. array(
  327. 'key' => 'sale_price_dates_to',
  328. 'value' => $tomorrow,
  329. 'compare' => '<=',
  330. ),
  331. )
  332. );
  333. query_posts($args);
  334. ob_start();
  335. jigoshop_get_template_part( 'loop', 'shop' );
  336. if ( $pagination ) do_action( 'jigoshop_pagination' );
  337. wp_reset_query();
  338. return ob_get_clean();
  339. }
  340. add_shortcode('sale_products', 'jigoshop_sale_products');
  341. //### Shortcodes #########################################################
  342. add_shortcode('product' , 'jigoshop_product');
  343. add_shortcode('products' , 'jigoshop_products');
  344. add_shortcode('add_to_cart' , 'jigoshop_product_add_to_cart');
  345. add_shortcode('add_to_cart_url' , 'jigoshop_product_add_to_cart_url');
  346. add_shortcode('product_search' , 'jigoshop_search_shortcode');
  347. add_shortcode('recent_products' , 'jigoshop_recent_products');
  348. add_shortcode('featured_products' , 'jigoshop_featured_products');
  349. add_shortcode('jigoshop_category' , 'jigoshop_product_category');
  350. add_shortcode('jigoshop_cart' , 'get_jigoshop_cart');
  351. add_shortcode('jigoshop_checkout' , 'get_jigoshop_checkout');
  352. add_shortcode('jigoshop_order_tracking' , 'get_jigoshop_order_tracking');
  353. add_shortcode('jigoshop_my_account' , 'get_jigoshop_my_account');
  354. add_shortcode('jigoshop_edit_address' , 'get_jigoshop_edit_address');
  355. add_shortcode('jigoshop_change_password', 'get_jigoshop_change_password');
  356. add_shortcode('jigoshop_view_order' , 'get_jigoshop_view_order');
  357. add_shortcode('jigoshop_pay' , 'get_jigoshop_pay');
  358. add_shortcode('jigoshop_thankyou' , 'get_jigoshop_thankyou');