/shortcodes/shortcodes-init.php

https://github.com/coolio197/woocommerce · PHP · 294 lines · 221 code · 43 blank · 30 comment · 12 complexity · 8b2b4434cddf56d65545b261bacdd647 MD5 · raw file

  1. <?php
  2. /**
  3. * Shortcodes init
  4. *
  5. * Init main shortcodes, and add a few others such as recent products.
  6. *
  7. * @package WooCommerce
  8. * @category Shortcode
  9. * @author WooThemes
  10. */
  11. include_once('shortcode-cart.php');
  12. include_once('shortcode-checkout.php');
  13. include_once('shortcode-my_account.php');
  14. include_once('shortcode-order_tracking.php');
  15. include_once('shortcode-pay.php');
  16. include_once('shortcode-thankyou.php');
  17. /**
  18. * Shortcode button in post editor
  19. **/
  20. add_action( 'init', 'woocommerce_add_shortcode_button' );
  21. add_filter( 'tiny_mce_version', 'woocommerce_refresh_mce' );
  22. function woocommerce_add_shortcode_button() {
  23. if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) return;
  24. if ( get_user_option('rich_editing') == 'true') :
  25. add_filter('mce_external_plugins', 'woocommerce_add_shortcode_tinymce_plugin');
  26. add_filter('mce_buttons', 'woocommerce_register_shortcode_button');
  27. endif;
  28. }
  29. function woocommerce_register_shortcode_button($buttons) {
  30. array_push($buttons, "|", "woocommerce_shortcodes_button");
  31. return $buttons;
  32. }
  33. function woocommerce_add_shortcode_tinymce_plugin($plugin_array) {
  34. global $woocommerce;
  35. $plugin_array['WooCommerceShortcodes'] = $woocommerce->plugin_url() . '/assets/js/admin/editor_plugin.js';
  36. return $plugin_array;
  37. }
  38. function woocommerce_refresh_mce($ver) {
  39. $ver += 3;
  40. return $ver;
  41. }
  42. /**
  43. * List products in a category shortcode
  44. **/
  45. function woocommerce_product_catagory($atts){
  46. global $woocommerce_loop;
  47. if (empty($atts)) return;
  48. extract(shortcode_atts(array(
  49. 'per_page' => '12',
  50. 'columns' => '4',
  51. 'orderby' => 'title',
  52. 'order' => 'asc',
  53. 'category' => ''
  54. ), $atts));
  55. if (!$category) return;
  56. $woocommerce_loop['columns'] = $columns;
  57. $args = array(
  58. 'post_type' => 'product',
  59. 'post_status' => 'publish',
  60. 'ignore_sticky_posts' => 1,
  61. 'orderby' => $orderby,
  62. 'order' => $order,
  63. 'posts_per_page' => $per_page,
  64. 'meta_query' => array(
  65. array(
  66. 'key' => 'visibility',
  67. 'value' => array('catalog', 'visible'),
  68. 'compare' => 'IN'
  69. )
  70. ),
  71. 'tax_query' => array(
  72. array(
  73. 'taxonomy' => 'product_cat',
  74. 'terms' => array( esc_attr($category) ),
  75. 'field' => 'slug',
  76. 'operator' => 'IN'
  77. )
  78. )
  79. );
  80. query_posts($args);
  81. ob_start();
  82. woocommerce_get_template_part( 'loop', 'shop' );
  83. wp_reset_query();
  84. return ob_get_clean();
  85. }
  86. /**
  87. * Recent Products shortcode
  88. **/
  89. function woocommerce_recent_products( $atts ) {
  90. global $woocommerce_loop;
  91. extract(shortcode_atts(array(
  92. 'per_page' => '12',
  93. 'columns' => '4',
  94. 'orderby' => 'date',
  95. 'order' => 'desc'
  96. ), $atts));
  97. $woocommerce_loop['columns'] = $columns;
  98. $args = array(
  99. 'post_type' => 'product',
  100. 'post_status' => 'publish',
  101. 'ignore_sticky_posts' => 1,
  102. 'posts_per_page' => $per_page,
  103. 'orderby' => $orderby,
  104. 'order' => $order,
  105. 'meta_query' => array(
  106. array(
  107. 'key' => 'visibility',
  108. 'value' => array('catalog', 'visible'),
  109. 'compare' => 'IN'
  110. )
  111. )
  112. );
  113. query_posts($args);
  114. ob_start();
  115. woocommerce_get_template_part( 'loop', 'shop' );
  116. wp_reset_query();
  117. return ob_get_clean();
  118. }
  119. /**
  120. * List multiple products shortcode
  121. **/
  122. function woocommerce_products($atts){
  123. global $woocommerce_loop;
  124. if (empty($atts)) return;
  125. extract(shortcode_atts(array(
  126. 'columns' => '4',
  127. 'orderby' => 'title',
  128. 'order' => 'asc'
  129. ), $atts));
  130. $woocommerce_loop['columns'] = $columns;
  131. $args = array(
  132. 'post_type' => 'product',
  133. 'post_status' => 'publish',
  134. 'ignore_sticky_posts' => 1,
  135. 'orderby' => $orderby,
  136. 'order' => $order,
  137. 'meta_query' => array(
  138. array(
  139. 'key' => 'visibility',
  140. 'value' => array('catalog', 'visible'),
  141. 'compare' => 'IN'
  142. )
  143. )
  144. );
  145. if(isset($atts['skus'])){
  146. $skus = explode(',', $atts['skus']);
  147. array_walk($skus, create_function('&$val', '$val = trim($val);'));
  148. $args['meta_query'][] = array(
  149. 'key' => 'sku',
  150. 'value' => $skus,
  151. 'compare' => 'IN'
  152. );
  153. }
  154. if(isset($atts['ids'])){
  155. $ids = explode(',', $atts['ids']);
  156. array_walk($ids, create_function('&$val', '$val = trim($val);'));
  157. $args['post__in'] = $ids;
  158. }
  159. query_posts($args);
  160. ob_start();
  161. woocommerce_get_template_part( 'loop', 'shop' );
  162. wp_reset_query();
  163. return ob_get_clean();
  164. }
  165. /**
  166. * Display a single prodcut
  167. **/
  168. function woocommerce_product($atts){
  169. if (empty($atts)) return;
  170. $args = array(
  171. 'post_type' => 'product',
  172. 'posts_per_page' => 1,
  173. 'post_status' => 'publish',
  174. 'meta_query' => array(
  175. array(
  176. 'key' => 'visibility',
  177. 'value' => array('catalog', 'visible'),
  178. 'compare' => 'IN'
  179. )
  180. )
  181. );
  182. if(isset($atts['sku'])){
  183. $args['meta_query'][] = array(
  184. 'key' => 'sku',
  185. 'value' => $atts['sku'],
  186. 'compare' => '='
  187. );
  188. }
  189. if(isset($atts['id'])){
  190. $args['p'] = $atts['id'];
  191. }
  192. query_posts($args);
  193. ob_start();
  194. woocommerce_get_template_part( 'loop', 'shop' );
  195. wp_reset_query();
  196. return ob_get_clean();
  197. }
  198. /**
  199. * Output featured products
  200. **/
  201. function woocommerce_featured_products( $atts ) {
  202. global $woocommerce_loop;
  203. extract(shortcode_atts(array(
  204. 'per_page' => '12',
  205. 'columns' => '4',
  206. 'orderby' => 'date',
  207. 'order' => 'desc'
  208. ), $atts));
  209. $woocommerce_loop['columns'] = $columns;
  210. $args = array(
  211. 'post_type' => 'product',
  212. 'post_status' => 'publish',
  213. 'ignore_sticky_posts' => 1,
  214. 'posts_per_page' => $per_page,
  215. 'orderby' => $orderby,
  216. 'order' => $order,
  217. 'meta_query' => array(
  218. array(
  219. 'key' => 'visibility',
  220. 'value' => array('catalog', 'visible'),
  221. 'compare' => 'IN'
  222. ),
  223. array(
  224. 'key' => 'featured',
  225. 'value' => 'yes'
  226. )
  227. )
  228. );
  229. query_posts($args);
  230. ob_start();
  231. woocommerce_get_template_part( 'loop', 'shop' );
  232. wp_reset_query();
  233. return ob_get_clean();
  234. }
  235. /**
  236. * Shortcode creation
  237. **/
  238. add_shortcode('product_category', 'woocommerce_product_catagory');
  239. add_shortcode('product', 'woocommerce_product');
  240. add_shortcode('products', 'woocommerce_products');
  241. add_shortcode('recent_products', 'woocommerce_recent_products');
  242. add_shortcode('featured_products', 'woocommerce_featured_products');
  243. add_shortcode('woocommerce_cart', 'get_woocommerce_cart');
  244. add_shortcode('woocommerce_checkout', 'get_woocommerce_checkout');
  245. add_shortcode('woocommerce_order_tracking', 'get_woocommerce_order_tracking');
  246. add_shortcode('woocommerce_my_account', 'get_woocommerce_my_account');
  247. add_shortcode('woocommerce_edit_address', 'get_woocommerce_edit_address');
  248. add_shortcode('woocommerce_change_password', 'get_woocommerce_change_password');
  249. add_shortcode('woocommerce_view_order', 'get_woocommerce_view_order');
  250. add_shortcode('woocommerce_pay', 'get_woocommerce_pay');
  251. add_shortcode('woocommerce_thankyou', 'get_woocommerce_thankyou');