/includes/shortcodes.php

  1. <?php 
  2. /** 
  3. * Shortcodes 
  4. * 
  5. * @package EDD 
  6. * @subpackage Shortcodes 
  7. * @copyright Copyright (c) 2015, Pippin Williamson 
  8. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License 
  9. * @since 1.0 
  10. */ 
  11.  
  12. // Exit if accessed directly 
  13. if ( ! defined( 'ABSPATH' ) ) exit; 
  14.  
  15. /** 
  16. * Purchase Link Shortcode 
  17. * 
  18. * Retrieves a download and displays the purchase form. 
  19. * 
  20. * @since 1.0 
  21. * @param array $atts Shortcode attributes 
  22. * @param string $content 
  23. * @return string Fully formatted purchase link 
  24. */ 
  25. function edd_download_shortcode( $atts, $content = null ) { 
  26. global $post; 
  27.  
  28. $post_id = is_object( $post ) ? $post->ID : 0; 
  29.  
  30. $atts = shortcode_atts( array( 
  31. 'id' => $post_id,  
  32. 'price_id' => isset( $atts['price_id'] ) ? $atts['price_id'] : false,  
  33. 'sku' => '',  
  34. 'price' => '1',  
  35. 'direct' => '0',  
  36. 'text' => '',  
  37. 'style' => edd_get_option( 'button_style', 'button' ),  
  38. 'color' => edd_get_option( 'checkout_color', 'blue' ),  
  39. 'class' => 'edd-submit',  
  40. 'form_id' => '' 
  41. ),  
  42. $atts, 'purchase_link' ); 
  43.  
  44. // Override text only if not provided / empty 
  45. if ( ! $atts['text'] ) { 
  46. if( $atts['direct'] == '1' || $atts['direct'] == 'true' ) { 
  47. $atts['text'] = edd_get_option( 'buy_now_text', __( 'Buy Now', 'easy-digital-downloads' ) ); 
  48. } else { 
  49. $atts['text'] = edd_get_option( 'add_to_cart_text', __( 'Purchase', 'easy-digital-downloads' ) ); 
  50.  
  51. // Override color if color == inherit 
  52. if( isset( $atts['color'] ) ) { 
  53. $atts['color'] = ( $atts['color'] == 'inherit' ) ? '' : $atts['color']; 
  54.  
  55. if( ! empty( $atts['sku'] ) ) { 
  56.  
  57. $download = edd_get_download_by( 'sku', $atts['sku'] ); 
  58.  
  59. if ( $download ) { 
  60. $atts['download_id'] = $download->ID; 
  61.  
  62. } elseif( isset( $atts['id'] ) ) { 
  63.  
  64. // Edd_get_purchase_link() expects the ID to be download_id since v1.3 
  65. $atts['download_id'] = $atts['id']; 
  66.  
  67. $download = edd_get_download( $atts['download_id'] ); 
  68.  
  69.  
  70. if ( $download ) { 
  71. return edd_get_purchase_link( $atts ); 
  72. add_shortcode( 'purchase_link', 'edd_download_shortcode' ); 
  73.  
  74. /** 
  75. * Download History Shortcode 
  76. * 
  77. * Displays a user's download history. 
  78. * 
  79. * @since 1.0 
  80. * @return string 
  81. */ 
  82. function edd_download_history() { 
  83. if ( is_user_logged_in() ) { 
  84. ob_start(); 
  85.  
  86. if( ! edd_user_pending_verification() ) { 
  87.  
  88. edd_get_template_part( 'history', 'downloads' ); 
  89.  
  90. } else { 
  91.  
  92. edd_get_template_part( 'account', 'pending' ); 
  93.  
  94.  
  95. return ob_get_clean(); 
  96. add_shortcode( 'download_history', 'edd_download_history' ); 
  97.  
  98. /** 
  99. * Purchase History Shortcode 
  100. * 
  101. * Displays a user's purchase history. 
  102. * 
  103. * @since 1.0 
  104. * @return string 
  105. */ 
  106. function edd_purchase_history() { 
  107. ob_start(); 
  108.  
  109. if( ! edd_user_pending_verification() ) { 
  110.  
  111. edd_get_template_part( 'history', 'purchases' ); 
  112.  
  113. } else { 
  114.  
  115. edd_get_template_part( 'account', 'pending' ); 
  116.  
  117.  
  118. return ob_get_clean(); 
  119. add_shortcode( 'purchase_history', 'edd_purchase_history' ); 
  120.  
  121. /** 
  122. * Checkout Form Shortcode 
  123. * 
  124. * Show the checkout form. 
  125. * 
  126. * @since 1.0 
  127. * @param array $atts Shortcode attributes 
  128. * @param string $content 
  129. * @return string 
  130. */ 
  131. function edd_checkout_form_shortcode( $atts, $content = null ) { 
  132. return edd_checkout_form(); 
  133. add_shortcode( 'download_checkout', 'edd_checkout_form_shortcode' ); 
  134.  
  135. /** 
  136. * Download Cart Shortcode 
  137. * 
  138. * Show the shopping cart. 
  139. * 
  140. * @since 1.0 
  141. * @param array $atts Shortcode attributes 
  142. * @param string $content 
  143. * @return string 
  144. */ 
  145. function edd_cart_shortcode( $atts, $content = null ) { 
  146. return edd_shopping_cart(); 
  147. add_shortcode( 'download_cart', 'edd_cart_shortcode' ); 
  148.  
  149. /** 
  150. * Login Shortcode 
  151. * 
  152. * Shows a login form allowing users to users to log in. This function simply 
  153. * calls the edd_login_form function to display the login form. 
  154. * 
  155. * @since 1.0 
  156. * @param array $atts Shortcode attributes 
  157. * @param string $content 
  158. * @uses edd_login_form() 
  159. * @return string 
  160. */ 
  161. function edd_login_form_shortcode( $atts, $content = null ) { 
  162. $redirect = ''; 
  163.  
  164. extract( shortcode_atts( array( 
  165. 'redirect' => $redirect 
  166. ), $atts, 'edd_login' ) 
  167. ); 
  168.  
  169. if ( empty( $redirect ) ) { 
  170. $login_redirect_page = edd_get_option( 'login_redirect_page', '' ); 
  171.  
  172. if ( ! empty( $login_redirect_page ) ) { 
  173. $redirect = get_permalink( $login_redirect_page ); 
  174.  
  175. if ( empty( $redirect ) ) { 
  176. $purchase_history = edd_get_option( 'purchase_history_page', 0 ); 
  177.  
  178. if ( ! empty( $purchase_history ) ) { 
  179. $redirect = get_permalink( $purchase_history ); 
  180.  
  181. if ( empty( $redirect ) ) { 
  182. $redirect = home_url(); 
  183.  
  184. return edd_login_form( $redirect ); 
  185. add_shortcode( 'edd_login', 'edd_login_form_shortcode' ); 
  186.  
  187. /** 
  188. * Register Shortcode 
  189. * 
  190. * Shows a registration form allowing users to users to register for the site 
  191. * 
  192. * @since 2.0 
  193. * @param array $atts Shortcode attributes 
  194. * @param string $content 
  195. * @uses edd_register_form() 
  196. * @return string 
  197. */ 
  198. function edd_register_form_shortcode( $atts, $content = null ) { 
  199. $redirect = home_url(); 
  200. $purchase_history = edd_get_option( 'purchase_history_page', 0 ); 
  201.  
  202. if ( ! empty( $purchase_history ) ) { 
  203. $redirect = get_permalink( $purchase_history ); 
  204.  
  205. extract( shortcode_atts( array( 
  206. 'redirect' => $redirect 
  207. ), $atts, 'edd_register' ) 
  208. ); 
  209. return edd_register_form( $redirect ); 
  210. add_shortcode( 'edd_register', 'edd_register_form_shortcode' ); 
  211.  
  212. /** 
  213. * Discounts shortcode 
  214. * 
  215. * Displays a list of all the active discounts. The active discounts can be configured 
  216. * from the Discount Codes admin screen. 
  217. * 
  218. * @since 1.0.8.2 
  219. * @param array $atts Shortcode attributes 
  220. * @param string $content 
  221. * @uses edd_get_discounts() 
  222. * @return string $discounts_lists List of all the active discount codes 
  223. */ 
  224. function edd_discounts_shortcode( $atts, $content = null ) { 
  225. $discounts = edd_get_discounts(); 
  226.  
  227. $discounts_list = '<ul id="edd_discounts_list">'; 
  228.  
  229. if ( ! empty( $discounts ) && edd_has_active_discounts() ) { 
  230.  
  231. foreach ( $discounts as $discount ) { 
  232.  
  233. if ( edd_is_discount_active( $discount->ID ) ) { 
  234.  
  235. $discounts_list .= '<li class="edd_discount">'; 
  236.  
  237. $discounts_list .= '<span class="edd_discount_name">' . edd_get_discount_code( $discount->ID ) . '</span>'; 
  238. $discounts_list .= '<span class="edd_discount_separator"> - </span>'; 
  239. $discounts_list .= '<span class="edd_discount_amount">' . edd_format_discount_rate( edd_get_discount_type( $discount->ID ), edd_get_discount_amount( $discount->ID ) ) . '</span>'; 
  240.  
  241. $discounts_list .= '</li>'; 
  242.  
  243.  
  244.  
  245. } else { 
  246. $discounts_list .= '<li class="edd_discount">' . __( 'No discounts found', 'easy-digital-downloads' ) . '</li>'; 
  247.  
  248. $discounts_list .= '</ul>'; 
  249.  
  250. return $discounts_list; 
  251. add_shortcode( 'download_discounts', 'edd_discounts_shortcode' ); 
  252.  
  253. /** 
  254. * Purchase Collection Shortcode 
  255. * 
  256. * Displays a collection purchase link for adding all items in a taxonomy term 
  257. * to the cart. 
  258. * 
  259. * @since 1.0.6 
  260. * @param array $atts Shortcode attributes 
  261. * @param string $content 
  262. * @return string 
  263. */ 
  264. function edd_purchase_collection_shortcode( $atts, $content = null ) { 
  265. extract( shortcode_atts( array( 
  266. 'taxonomy' => '',  
  267. 'terms' => '',  
  268. 'text' => __('Purchase All Items', 'easy-digital-downloads' ),  
  269. 'style' => edd_get_option( 'button_style', 'button' ),  
  270. 'color' => edd_get_option( 'checkout_color', 'blue' ),  
  271. 'class' => 'edd-submit' 
  272. ), $atts, 'purchase_collection' ) 
  273. ); 
  274.  
  275. $button_display = implode( ' ', array( $style, $color, $class ) ); 
  276.  
  277. return '<a href="' . esc_url( add_query_arg( array( 'edd_action' => 'purchase_collection', 'taxonomy' => $taxonomy, 'terms' => $terms ) ) ) . '" class="' . $button_display . '">' . $text . '</a>'; 
  278. add_shortcode( 'purchase_collection', 'edd_purchase_collection_shortcode' ); 
  279.  
  280. /** 
  281. * Downloads Shortcode 
  282. * 
  283. * This shortcodes uses the WordPress Query API to get downloads with the 
  284. * arguments specified when using the shortcode. A list of the arguments 
  285. * can be found from the EDD Dccumentation. The shortcode will take all the 
  286. * parameters and display the downloads queried in a valid HTML <div> tags. 
  287. * 
  288. * @since 1.0.6 
  289. * @internal Incomplete shortcode 
  290. * @param array $atts Shortcode attributes 
  291. * @param string $content 
  292. * @return string $display Output generated from the downloads queried 
  293. */ 
  294. function edd_downloads_query( $atts, $content = null ) { 
  295. $atts = shortcode_atts( array( 
  296. 'category' => '',  
  297. 'exclude_category' => '',  
  298. 'tags' => '',  
  299. 'exclude_tags' => '',  
  300. 'author' => false,  
  301. 'relation' => 'OR',  
  302. 'number' => 9,  
  303. 'price' => 'no',  
  304. 'excerpt' => 'yes',  
  305. 'full_content' => 'no',  
  306. 'buy_button' => 'yes',  
  307. 'columns' => 3,  
  308. 'thumbnails' => 'true',  
  309. 'orderby' => 'post_date',  
  310. 'order' => 'DESC',  
  311. 'ids' => '',  
  312. 'pagination' => 'true',  
  313. ), $atts, 'downloads' ); 
  314.  
  315. $query = array( 
  316. 'post_type' => 'download',  
  317. 'orderby' => $atts['orderby'],  
  318. 'order' => $atts['order'] 
  319. ); 
  320.  
  321. if ( filter_var( $atts['pagination'], FILTER_VALIDATE_BOOLEAN ) || ( ! filter_var( $atts['pagination'], FILTER_VALIDATE_BOOLEAN ) && $atts[ 'number' ] ) ) { 
  322.  
  323. $query['posts_per_page'] = (int) $atts['number']; 
  324.  
  325. if ( $query['posts_per_page'] < 0 ) { 
  326. $query['posts_per_page'] = abs( $query['posts_per_page'] ); 
  327. } else { 
  328. $query['nopaging'] = true; 
  329.  
  330. if( 'random' == $atts['orderby'] ) { 
  331. $atts['pagination'] = false; 
  332.  
  333. switch ( $atts['orderby'] ) { 
  334. case 'price': 
  335. $atts['orderby'] = 'meta_value'; 
  336. $query['meta_key'] = 'edd_price'; 
  337. $query['orderby'] = 'meta_value_num'; 
  338. break; 
  339.  
  340. case 'title': 
  341. $query['orderby'] = 'title'; 
  342. break; 
  343.  
  344. case 'id': 
  345. $query['orderby'] = 'ID'; 
  346. break; 
  347.  
  348. case 'random': 
  349. $query['orderby'] = 'rand'; 
  350. break; 
  351.  
  352. case 'post__in': 
  353. $query['orderby'] = 'post__in'; 
  354. break; 
  355.  
  356. default: 
  357. $query['orderby'] = 'post_date'; 
  358. break; 
  359.  
  360. if ( $atts['tags'] || $atts['category'] || $atts['exclude_category'] || $atts['exclude_tags'] ) { 
  361.  
  362. $query['tax_query'] = array( 
  363. 'relation' => $atts['relation'] 
  364. ); 
  365.  
  366. if ( $atts['tags'] ) { 
  367.  
  368. $tag_list = explode( ', ', $atts['tags'] ); 
  369.  
  370. foreach( $tag_list as $tag ) { 
  371.  
  372. $t_id = (int) $tag; 
  373. $is_id = is_int( $t_id ) && ! empty( $t_id ); 
  374.  
  375. if( $is_id ) { 
  376.  
  377. $term_id = $tag; 
  378.  
  379. } else { 
  380.  
  381. $term = get_term_by( 'slug', $tag, 'download_tag' ); 
  382.  
  383. if( ! $term ) { 
  384. continue; 
  385.  
  386. $term_id = $term->term_id; 
  387.  
  388. $query['tax_query'][] = array( 
  389. 'taxonomy' => 'download_tag',  
  390. 'field' => 'term_id',  
  391. 'terms' => $term_id 
  392. ); 
  393.  
  394.  
  395. if ( $atts['category'] ) { 
  396.  
  397. $categories = explode( ', ', $atts['category'] ); 
  398.  
  399. foreach( $categories as $category ) { 
  400.  
  401. $t_id = (int) $category; 
  402. $is_id = is_int( $t_id ) && ! empty( $t_id ); 
  403.  
  404. if( $is_id ) { 
  405.  
  406. $term_id = $category; 
  407.  
  408. } else { 
  409.  
  410. $term = get_term_by( 'slug', $category, 'download_category' ); 
  411.  
  412. if( ! $term ) { 
  413. continue; 
  414.  
  415. $term_id = $term->term_id; 
  416.  
  417.  
  418. $query['tax_query'][] = array( 
  419. 'taxonomy' => 'download_category',  
  420. 'field' => 'term_id',  
  421. 'terms' => $term_id,  
  422. ); 
  423.  
  424.  
  425.  
  426. if ( $atts['exclude_category'] ) { 
  427.  
  428. $categories = explode( ', ', $atts['exclude_category'] ); 
  429.  
  430. foreach( $categories as $category ) { 
  431.  
  432. $t_id = (int) $category; 
  433. $is_id = is_int( $t_id ) && ! empty( $t_id ); 
  434.  
  435. if( $is_id ) { 
  436.  
  437. $term_id = $category; 
  438.  
  439. } else { 
  440.  
  441. $term = get_term_by( 'slug', $category, 'download_category' ); 
  442.  
  443. if( ! $term ) { 
  444. continue; 
  445.  
  446. $term_id = $term->term_id; 
  447.  
  448. $query['tax_query'][] = array( 
  449. 'taxonomy' => 'download_category',  
  450. 'field' => 'term_id',  
  451. 'terms' => $term_id,  
  452. 'operator' => 'NOT IN' 
  453. ); 
  454.  
  455.  
  456. if ( $atts['exclude_tags'] ) { 
  457.  
  458. $tag_list = explode( ', ', $atts['exclude_tags'] ); 
  459.  
  460. foreach( $tag_list as $tag ) { 
  461.  
  462. $t_id = (int) $tag; 
  463. $is_id = is_int( $t_id ) && ! empty( $t_id ); 
  464.  
  465. if( $is_id ) { 
  466.  
  467. $term_id = $tag; 
  468.  
  469. } else { 
  470.  
  471. $term = get_term_by( 'slug', $tag, 'download_tag' ); 
  472.  
  473. if( ! $term ) { 
  474. continue; 
  475.  
  476. $term_id = $term->term_id; 
  477.  
  478. $query['tax_query'][] = array( 
  479. 'taxonomy' => 'download_tag',  
  480. 'field' => 'term_id',  
  481. 'terms' => $term_id,  
  482. 'operator' => 'NOT IN' 
  483. ); 
  484.  
  485.  
  486.  
  487. if ( $atts['exclude_tags'] || $atts['exclude_category'] ) { 
  488. $query['tax_query']['relation'] = 'AND'; 
  489.  
  490. if ( $atts['author'] ) { 
  491. $authors = explode( ', ', $atts['author'] ); 
  492. if ( ! empty( $authors ) ) { 
  493. $author_ids = array(); 
  494. $author_names = array(); 
  495.  
  496. foreach ( $authors as $author ) { 
  497. if ( is_numeric( $author ) ) { 
  498. $author_ids[] = $author; 
  499. } else { 
  500. $user = get_user_by( 'login', $author ); 
  501. if ( $user ) { 
  502. $author_ids[] = $user->ID; 
  503.  
  504. if ( ! empty( $author_ids ) ) { 
  505. $author_ids = array_unique( array_map( 'absint', $author_ids ) ); 
  506. $query['author'] = implode( ', ', $author_ids ); 
  507.  
  508. if( ! empty( $atts['ids'] ) ) 
  509. $query['post__in'] = explode( ', ', $atts['ids'] ); 
  510.  
  511. if ( get_query_var( 'paged' ) ) 
  512. $query['paged'] = get_query_var('paged'); 
  513. else if ( get_query_var( 'page' ) ) 
  514. $query['paged'] = get_query_var( 'page' ); 
  515. else 
  516. $query['paged'] = 1; 
  517.  
  518. // Allow the query to be manipulated by other plugins 
  519. $query = apply_filters( 'edd_downloads_query', $query, $atts ); 
  520.  
  521. $downloads = new WP_Query( $query ); 
  522. if ( $downloads->have_posts() ) : 
  523. $i = 1; 
  524. $wrapper_class = 'edd_download_columns_' . $atts['columns']; 
  525. ob_start(); ?> 
  526. <div class="edd_downloads_list <?php echo apply_filters( 'edd_downloads_list_wrapper_class', $wrapper_class, $atts ); ?>"> 
  527. <?php while ( $downloads->have_posts() ) : $downloads->the_post(); ?> 
  528. <?php $schema = edd_add_schema_microdata() ? 'itemscope itemtype="http://schema.org/Product" ' : ''; ?> 
  529. <div <?php echo $schema; ?>class="<?php echo apply_filters( 'edd_download_class', 'edd_download', get_the_ID(), $atts, $i ); ?>" id="edd_download_<?php echo get_the_ID(); ?>"> 
  530. <div class="<?php echo apply_filters( 'edd_download_inner_class', 'edd_download_inner', get_the_ID(), $atts, $i ); ?>"> 
  531. <?php 
  532.  
  533. do_action( 'edd_download_before' ); 
  534.  
  535. if ( 'false' != $atts['thumbnails'] ) : 
  536. edd_get_template_part( 'shortcode', 'content-image' ); 
  537. do_action( 'edd_download_after_thumbnail' ); 
  538. endif; 
  539.  
  540. edd_get_template_part( 'shortcode', 'content-title' ); 
  541. do_action( 'edd_download_after_title' ); 
  542.  
  543. if ( $atts['excerpt'] == 'yes' && $atts['full_content'] != 'yes' ) { 
  544. edd_get_template_part( 'shortcode', 'content-excerpt' ); 
  545. do_action( 'edd_download_after_content' ); 
  546. } else if ( $atts['full_content'] == 'yes' ) { 
  547. edd_get_template_part( 'shortcode', 'content-full' ); 
  548. do_action( 'edd_download_after_content' ); 
  549.  
  550. if ( $atts['price'] == 'yes' ) { 
  551. edd_get_template_part( 'shortcode', 'content-price' ); 
  552. do_action( 'edd_download_after_price' ); 
  553.  
  554. if ( $atts['buy_button'] == 'yes' ) 
  555. edd_get_template_part( 'shortcode', 'content-cart-button' ); 
  556.  
  557. do_action( 'edd_download_after' ); 
  558.  
  559. ?> 
  560. </div> 
  561. </div> 
  562. <?php if ( $atts['columns'] != 0 && $i % $atts['columns'] == 0 ) { ?><div style="clear:both;"></div><?php } ?> 
  563. <?php $i++; endwhile; ?> 
  564.  
  565. <div style="clear:both;"></div> 
  566.  
  567. <?php wp_reset_postdata(); ?> 
  568.  
  569. <?php if ( filter_var( $atts['pagination'], FILTER_VALIDATE_BOOLEAN ) ) : ?> 
  570.  
  571. <?php 
  572. $pagination = false; 
  573.  
  574. if ( is_single() ) { 
  575. $pagination = paginate_links( apply_filters( 'edd_download_pagination_args', array( 
  576. 'base' => get_permalink() . '%#%',  
  577. 'format' => '?paged=%#%',  
  578. 'current' => max( 1, $query['paged'] ),  
  579. 'total' => $downloads->max_num_pages 
  580. ), $atts, $downloads, $query ) ); 
  581. } else { 
  582. $big = 999999; 
  583. $search_for = array( $big, '#038;' ); 
  584. $replace_with = array( '%#%', '&' ); 
  585. $pagination = paginate_links( apply_filters( 'edd_download_pagination_args', array( 
  586. 'base' => str_replace( $search_for, $replace_with, get_pagenum_link( $big ) ),  
  587. 'format' => '?paged=%#%',  
  588. 'current' => max( 1, $query['paged'] ),  
  589. 'total' => $downloads->max_num_pages 
  590. ), $atts, $downloads, $query ) ); 
  591. ?> 
  592.  
  593. <?php if ( ! empty( $pagination ) ) : ?> 
  594. <div id="edd_download_pagination" class="navigation"> 
  595. <?php echo $pagination; ?> 
  596. </div> 
  597. <?php endif; ?> 
  598.  
  599. <?php endif; ?> 
  600.  
  601. </div> 
  602. <?php 
  603. $display = ob_get_clean(); 
  604. else: 
  605. $display = sprintf( _x( 'No %s found', 'download post type name', 'easy-digital-downloads' ), edd_get_label_plural() ); 
  606. endif; 
  607.  
  608. return apply_filters( 'downloads_shortcode', $display, $atts, $atts['buy_button'], $atts['columns'], '', $downloads, $atts['excerpt'], $atts['full_content'], $atts['price'], $atts['thumbnails'], $query ); 
  609. add_shortcode( 'downloads', 'edd_downloads_query' ); 
  610. add_shortcode( 'edd_downloads', 'edd_downloads_query' ); 
  611.  
  612. /** 
  613. * Price Shortcode 
  614. * 
  615. * Shows the price of a download. 
  616. * 
  617. * @since 1.1.3.3 
  618. * @param array $atts Shortcode attributes 
  619. * @param string $content 
  620. * @return string 
  621. */ 
  622. function edd_download_price_shortcode( $atts, $content = null ) { 
  623. extract( shortcode_atts( array( 
  624. 'id' => NULL,  
  625. 'price_id' => false,  
  626. ), $atts, 'edd_price' ) 
  627. ); 
  628.  
  629. if ( is_null( $id ) ) { 
  630. $id = get_the_ID(); 
  631.  
  632. return edd_price( $id, false, $price_id ); 
  633. add_shortcode( 'edd_price', 'edd_download_price_shortcode' ); 
  634.  
  635. /** 
  636. * Receipt Shortcode 
  637. * 
  638. * Shows an order receipt. 
  639. * 
  640. * @since 1.4 
  641. * @param array $atts Shortcode attributes 
  642. * @param string $content 
  643. * @return string 
  644. */ 
  645. function edd_receipt_shortcode( $atts, $content = null ) { 
  646. global $edd_receipt_args; 
  647.  
  648. $edd_receipt_args = shortcode_atts( array( 
  649. 'error' => __( 'Sorry, trouble retrieving payment receipt.', 'easy-digital-downloads' ),  
  650. 'price' => true,  
  651. 'discount' => true,  
  652. 'products' => true,  
  653. 'date' => true,  
  654. 'notes' => true,  
  655. 'payment_key' => false,  
  656. 'payment_method' => true,  
  657. 'payment_id' => true 
  658. ), $atts, 'edd_receipt' ); 
  659.  
  660. $session = edd_get_purchase_session(); 
  661. if ( isset( $_GET['payment_key'] ) ) { 
  662. $payment_key = urldecode( $_GET['payment_key'] ); 
  663. } else if ( $session ) { 
  664. $payment_key = $session['purchase_key']; 
  665. } elseif ( $edd_receipt_args['payment_key'] ) { 
  666. $payment_key = $edd_receipt_args['payment_key']; 
  667.  
  668. // No key found 
  669. if ( ! isset( $payment_key ) ) { 
  670. return '<p class="edd-alert edd-alert-error">' . $edd_receipt_args['error'] . '</p>'; 
  671.  
  672. $payment_id = edd_get_purchase_id_by_key( $payment_key ); 
  673. $user_can_view = edd_can_view_receipt( $payment_key ); 
  674.  
  675. // Key was provided, but user is logged out. Offer them the ability to login and view the receipt 
  676. if ( ! $user_can_view && ! empty( $payment_key ) && ! is_user_logged_in() && ! edd_is_guest_payment( $payment_id ) ) { 
  677. global $edd_login_redirect; 
  678. $edd_login_redirect = edd_get_current_page_url(); 
  679.  
  680. ob_start(); 
  681.  
  682. echo '<p class="edd-alert edd-alert-warn">' . __( 'You must be logged in to view this payment receipt.', 'easy-digital-downloads' ) . '</p>'; 
  683. edd_get_template_part( 'shortcode', 'login' ); 
  684.  
  685. $login_form = ob_get_clean(); 
  686.  
  687. return $login_form; 
  688.  
  689. $user_can_view = apply_filters( 'edd_user_can_view_receipt', $user_can_view, $edd_receipt_args ); 
  690.  
  691. // If this was a guest checkout and the purchase session is empty, output a relevant error message 
  692. if ( empty( $session ) && ! is_user_logged_in() && ! $user_can_view ) { 
  693. return '<p class="edd-alert edd-alert-error">' . apply_filters( 'edd_receipt_guest_error_message', __( 'Receipt could not be retrieved, your purchase session has expired.', 'easy-digital-downloads' ) ) . '</p>'; 
  694.  
  695. /** 
  696. * Check if the user has permission to view the receipt 
  697. * 
  698. * If user is logged in, user ID is compared to user ID of ID stored in payment meta 
  699. * 
  700. * Or if user is logged out and purchase was made as a guest, the purchase session is checked for 
  701. * 
  702. * Or if user is logged in and the user can view sensitive shop data 
  703. * 
  704. */ 
  705.  
  706.  
  707. if ( ! $user_can_view ) { 
  708. return '<p class="edd-alert edd-alert-error">' . $edd_receipt_args['error'] . '</p>'; 
  709.  
  710. ob_start(); 
  711.  
  712. edd_get_template_part( 'shortcode', 'receipt' ); 
  713.  
  714. $display = ob_get_clean(); 
  715.  
  716. return $display; 
  717. add_shortcode( 'edd_receipt', 'edd_receipt_shortcode' ); 
  718.  
  719. /** 
  720. * Profile Editor Shortcode 
  721. * 
  722. * Outputs the EDD Profile Editor to allow users to amend their details from the 
  723. * front-end. This function uses the EDD templating system allowing users to 
  724. * override the default profile editor template. The profile editor template is located 
  725. * under templates/profile-editor.php, however, it can be altered by creating a 
  726. * file called profile-editor.php in the edd_template directory in your active theme's 
  727. * folder. Please visit the EDD Documentation for more information on how the 
  728. * templating system is used. 
  729. * 
  730. * @since 1.4 
  731. * 
  732. * @author Sunny Ratilal 
  733. * 
  734. * @param $atts Shortcode attributes 
  735. * @param null $content 
  736. * @return string Output generated from the profile editor 
  737. */ 
  738. function edd_profile_editor_shortcode( $atts, $content = null ) { 
  739. ob_start(); 
  740.  
  741. if( ! edd_user_pending_verification() ) { 
  742.  
  743. edd_get_template_part( 'shortcode', 'profile-editor' ); 
  744.  
  745. } else { 
  746.  
  747. edd_get_template_part( 'account', 'pending' ); 
  748.  
  749.  
  750. $display = ob_get_clean(); 
  751.  
  752. return $display; 
  753. add_shortcode( 'edd_profile_editor', 'edd_profile_editor_shortcode' ); 
  754.  
  755. /** 
  756. * Process Profile Updater Form 
  757. * 
  758. * Processes the profile updater form by updating the necessary fields 
  759. * 
  760. * @since 1.4 
  761. * @author Sunny Ratilal 
  762. * @param array $data Data sent from the profile editor 
  763. * @return void 
  764. */ 
  765. function edd_process_profile_editor_updates( $data ) { 
  766. // Profile field change request 
  767. if ( empty( $_POST['edd_profile_editor_submit'] ) && !is_user_logged_in() ) { 
  768. return false; 
  769.  
  770. // Pending users can't edit their profile 
  771. if ( edd_user_pending_verification() ) { 
  772. return false; 
  773.  
  774. // Nonce security 
  775. if ( ! wp_verify_nonce( $data['edd_profile_editor_nonce'], 'edd-profile-editor-nonce' ) ) { 
  776. return false; 
  777.  
  778. $user_id = get_current_user_id(); 
  779. $old_user_data = get_userdata( $user_id ); 
  780.  
  781. $display_name = isset( $data['edd_display_name'] ) ? sanitize_text_field( $data['edd_display_name'] ) : $old_user_data->display_name; 
  782. $first_name = isset( $data['edd_first_name'] ) ? sanitize_text_field( $data['edd_first_name'] ) : $old_user_data->first_name; 
  783. $last_name = isset( $data['edd_last_name'] ) ? sanitize_text_field( $data['edd_last_name'] ) : $old_user_data->last_name; 
  784. $email = isset( $data['edd_email'] ) ? sanitize_email( $data['edd_email'] ) : $old_user_data->user_email; 
  785. $line1 = isset( $data['edd_address_line1'] ) ? sanitize_text_field( $data['edd_address_line1'] ) : ''; 
  786. $line2 = isset( $data['edd_address_line2'] ) ? sanitize_text_field( $data['edd_address_line2'] ) : ''; 
  787. $city = isset( $data['edd_address_city'] ) ? sanitize_text_field( $data['edd_address_city'] ) : ''; 
  788. $state = isset( $data['edd_address_state'] ) ? sanitize_text_field( $data['edd_address_state'] ) : ''; 
  789. $zip = isset( $data['edd_address_zip'] ) ? sanitize_text_field( $data['edd_address_zip'] ) : ''; 
  790. $country = isset( $data['edd_address_country'] ) ? sanitize_text_field( $data['edd_address_country'] ) : ''; 
  791.  
  792. $userdata = array( 
  793. 'ID' => $user_id,  
  794. 'first_name' => $first_name,  
  795. 'last_name' => $last_name,  
  796. 'display_name' => $display_name,  
  797. 'user_email' => $email 
  798. ); 
  799.  
  800.  
  801. $address = array( 
  802. 'line1' => $line1,  
  803. 'line2' => $line2,  
  804. 'city' => $city,  
  805. 'state' => $state,  
  806. 'zip' => $zip,  
  807. 'country' => $country 
  808. ); 
  809.  
  810. do_action( 'edd_pre_update_user_profile', $user_id, $userdata ); 
  811.  
  812. // New password 
  813. if ( ! empty( $data['edd_new_user_pass1'] ) ) { 
  814. if ( $data['edd_new_user_pass1'] !== $data['edd_new_user_pass2'] ) { 
  815. edd_set_error( 'password_mismatch', __( 'The passwords you entered do not match. Please try again.', 'easy-digital-downloads' ) ); 
  816. } else { 
  817. $userdata['user_pass'] = $data['edd_new_user_pass1']; 
  818.  
  819. // Make sure the new email doesn't belong to another user 
  820. if( $email != $old_user_data->user_email ) { 
  821. // Make sure the new email is valid 
  822. if( ! is_email( $email ) ) { 
  823. edd_set_error( 'email_invalid', __( 'The email you entered is invalid. Please enter a valid email.', 'easy-digital-downloads' ) ); 
  824.  
  825. // Make sure the new email doesn't belong to another user 
  826. if( email_exists( $email ) ) { 
  827. edd_set_error( 'email_exists', __( 'The email you entered belongs to another user. Please use another.', 'easy-digital-downloads' ) ); 
  828.  
  829. // Check for errors 
  830. $errors = edd_get_errors(); 
  831.  
  832. if( $errors ) { 
  833. // Send back to the profile editor if there are errors 
  834. wp_redirect( $data['edd_redirect'] ); 
  835. edd_die(); 
  836.  
  837. // Update the user 
  838. $meta = update_user_meta( $user_id, '_edd_user_address', $address ); 
  839. $updated = wp_update_user( $userdata ); 
  840.  
  841. // Possibly update the customer 
  842. $customer = new EDD_Customer( $user_id, true ); 
  843. if ( $customer->email === $email || ( is_array( $customer->emails ) && in_array( $email, $customer->emails ) ) ) { 
  844. $customer->set_primary_email( $email ); 
  845. }; 
  846.  
  847. if ( $customer->id > 0 ) { 
  848. $update_args = array( 
  849. 'name' => $first_name . ' ' . $last_name,  
  850. ); 
  851.  
  852. $customer->update( $update_args ); 
  853.  
  854. if ( $updated ) { 
  855. do_action( 'edd_user_profile_updated', $user_id, $userdata ); 
  856. wp_redirect( add_query_arg( 'updated', 'true', $data['edd_redirect'] ) ); 
  857. edd_die(); 
  858. add_action( 'edd_edit_user_profile', 'edd_process_profile_editor_updates' ); 
  859.  
  860. /** 
  861. * Process the 'remove' URL on the profile editor when customers wish to remove an email address 
  862. * 
  863. * @since 2.6 
  864. * @return void 
  865. */ 
  866. function edd_process_profile_editor_remove_email() { 
  867. if ( ! is_user_logged_in() ) { 
  868. return false; 
  869.  
  870. // Pending users can't edit their profile 
  871. if ( edd_user_pending_verification() ) { 
  872. return false; 
  873.  
  874. // Nonce security 
  875. if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'edd-remove-customer-email' ) ) { 
  876. return false; 
  877.  
  878. if ( empty( $_GET['email'] ) || ! is_email( $_GET['email'] ) ) { 
  879. return false; 
  880.  
  881. $customer = new EDD_Customer( get_current_user_id(), true ); 
  882. if ( $customer->remove_email( $_GET['email'] ) ) { 
  883.  
  884. $url = add_query_arg( 'updated', true, $_GET['redirect'] ); 
  885.  
  886. $user = wp_get_current_user(); 
  887. $user_login = ! empty( $user->user_login ) ? $user->user_login : 'EDDBot'; 
  888. $customer_note = __( sprintf( 'Email address %s removed by %s', $_GET['email'], $user_login ), 'easy-digital-downloads' ); 
  889. $customer->add_note( $customer_note ); 
  890.  
  891. } else { 
  892. edd_set_error( 'profile-remove-email-failure', __( 'Error removing email address from profile. Please try again later.', 'easy-digital-downloads' ) ); 
  893. $url = $_GET['redirect']; 
  894.  
  895. wp_safe_redirect( $url ); 
  896. exit; 
  897. add_action( 'edd_profile-remove-email', 'edd_process_profile_editor_remove_email' ); 
.