Easy Digital Downloads
  • Package
  • Function
  • Tree

Packages

  • EDD
    • Admin
      • Actions
      • Add-ons
      • Dashboard
      • Discounts
      • Downloads
      • Export
      • Notices
      • Pages
      • Payments
      • Reports
      • Settings
      • System
      • Upgrades
      • Upload
      • Welcome
    • Cart
    • Checkout
    • Classes
      • API
      • Fees
      • HTML
      • Roles
      • Session
    • Emails
    • Functions
      • AJAX
      • Compatibility
      • Errors
      • Formatting
      • Install
      • Login
      • Taxes
      • Templates
    • Gateways
    • Logging
    • Payments
    • Shortcodes
    • Widgets

Functions

  • edd_cart_shortcode
  • edd_checkout_form_shortcode
  • edd_discounts_shortcode
  • edd_download_history
  • edd_download_price_shortcode
  • edd_download_shortcode
  • edd_downloads_query
  • edd_login_form_shortcode
  • edd_process_profile_editor_updates
  • edd_profile_editor_shortcode
  • edd_purchase_collection_shortcode
  • edd_purchase_history
  • edd_receipt_shortcode
  1 <?php
  2 /**
  3  * Shortcodes
  4  *
  5  * @package     EDD
  6  * @subpackage  Shortcodes
  7  * @copyright   Copyright (c) 2013, 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, $edd_options;
 27 
 28     extract( shortcode_atts( array(
 29             'id'    => $post->ID,
 30             'price' => '1',
 31             'text'  => isset( $edd_options[ 'add_to_cart_text' ] )  && $edd_options[ 'add_to_cart_text' ]    != '' ? $edd_options[ 'add_to_cart_text' ] : __( 'Purchase', 'edd' ),
 32             'style' => isset( $edd_options[ 'button_style' ] )      ? $edd_options[ 'button_style' ]        : 'button',
 33             'color' => isset( $edd_options[ 'checkout_color' ] )    ? $edd_options[ 'checkout_color' ]      : 'blue',
 34             'class' => 'edd-submit'
 35         ),
 36         $atts )
 37     );
 38 
 39     // Edd_get_purchase_link() expects the ID to be download_id since v1.3
 40     $atts['download_id'] = $atts['id'];
 41 
 42     $download = edd_get_download( $atts['download_id'] );
 43 
 44     if ( $download ) {
 45         return edd_get_purchase_link( $atts );
 46     }
 47 }
 48 add_shortcode( 'purchase_link', 'edd_download_shortcode' );
 49 
 50 /**
 51  * Download History Shortcode
 52  *
 53  * Displays a user's download history.
 54  *
 55  * @since 1.0
 56  * @return string
 57  */
 58 function edd_download_history() {
 59     if ( is_user_logged_in() ) {
 60         ob_start();
 61         edd_get_template_part( 'history', 'downloads' );
 62         return ob_get_clean();
 63     }
 64 }
 65 add_shortcode( 'download_history', 'edd_download_history' );
 66 
 67 /**
 68  * Purchase History Shortcode
 69  *
 70  * Displays a user's purchsae history.
 71  *
 72  * @since 1.0
 73  * @return string
 74  */
 75 function edd_purchase_history() {
 76     if ( is_user_logged_in() ) {
 77         ob_start();
 78         edd_get_template_part( 'history', 'purchases' );
 79         return ob_get_clean();
 80     }
 81 }
 82 add_shortcode( 'purchase_history', 'edd_purchase_history' );
 83 
 84 /**
 85  * Checkout Form Shortcode
 86  *
 87  * Show the checkout form.
 88  *
 89  * @since 1.0
 90  * @param array $atts Shortcode attributes
 91  * @param string $content
 92  * @return string
 93  */
 94 function edd_checkout_form_shortcode( $atts, $content = null ) {
 95     return edd_checkout_form();
 96 }
 97 add_shortcode( 'download_checkout', 'edd_checkout_form_shortcode' );
 98 
 99 /**
100  * Download Cart Shortcode
101  *
102  * Show the shopping cart.
103  *
104  * @since 1.0
105  * @param array $atts Shortcode attributes
106  * @param string $content
107  * @return string
108  */
109 function edd_cart_shortcode( $atts, $content = null ) {
110     return edd_shopping_cart();
111 }
112 add_shortcode( 'download_cart', 'edd_cart_shortcode' );
113 
114 /**
115  * Login Shortcode
116  *
117  * Shows a login form allowing users to users to log in. This function simply
118  * calls the edd_login_form function to display the login form.
119  *
120  * @since 1.0
121  * @param array $atts Shortcode attributes
122  * @param string $content
123  * @uses edd_login_form()
124  * @return string
125  */
126 function edd_login_form_shortcode( $atts, $content = null ) {
127     extract( shortcode_atts( array(
128             'redirect' => '',
129         ), $atts )
130     );
131     return edd_login_form( $redirect );
132 }
133 add_shortcode( 'edd_login', 'edd_login_form_shortcode' );
134 
135 /**
136  * Discounts short code
137  *
138  * Displays a list of all the active discounts. The active discounts can be configured
139  * from the Discount Codes admin screen.
140  *
141  * @since 1.0.8.2
142  * @param array $atts Shortcode attributes
143  * @param string $content
144  * @uses edd_get_discounts()
145  * @return string $discounts_lists List of all the active discount codes
146  */
147 function edd_discounts_shortcode( $atts, $content = null ) {
148     $discounts = edd_get_discounts();
149 
150     if ( ! $discounts && edd_has_active_discounts() )
151         return;
152 
153     $discounts_list = '<ul id="edd_discounts_list">';
154 
155     foreach ( $discounts as $discount ) {
156         if ( edd_is_discount_active( $discount->ID ) ) {
157             $discounts_list .= '<li class="edd_discount">';
158 
159                 $discounts_list .= '<span class="edd_discount_name">' . edd_get_discount_code( $discount->ID ) . '</span>';
160                 $discounts_list .= '<span class="edd_discount_separator"> - </span>';
161                 $discounts_list .= '<span class="edd_discount_amount">' . edd_format_discount_rate( edd_get_discount_type( $discount->ID ), edd_get_discount_amount( $discount->ID ) ) . '</span>';
162 
163             $discounts_list .= '</li>';
164         }
165     }
166 
167     $discounts_list .= '</ul>';
168 
169     return $discounts_list;
170 }
171 add_shortcode( 'download_discounts', 'edd_discounts_shortcode' );
172 
173 /**
174  * Purchase Collection Shortcode
175  *
176  * Displays a collection purchase link for adding all items in a taxonomy term
177  * to the cart.
178  *
179  * @since 1.0.6
180  * @param array $atts Shortcode attributes
181  * @param string $content
182  * @return string
183  */
184 function edd_purchase_collection_shortcode( $atts, $content = null ) {
185     extract( shortcode_atts( array(
186             'taxonomy' => '',
187             'terms' => '',
188             'link' => __('Purchase All Items', 'edd')
189         ), $atts )
190     );
191 
192     $link = is_null( $content ) ? $link : $content;
193 
194     return '<a href="' . add_query_arg( array( 'edd_action' => 'purchase_collection', 'taxonomy' => $taxonomy, 'terms' => $terms ) ) . '">' . $link . '</a>';
195 }
196 add_shortcode( 'purchase_collection', 'edd_purchase_collection_shortcode' );
197 
198 /**
199  * Downloads Shortcode
200  *
201  * This shortcodes uses the WordPress Query API to get downloads with the
202  * arguments specified when using the shortcode. A list of the arguments
203  * can be found from the EDD Dccumentation. The shortcode will take all the
204  * paramaters and display the downloads queried in a valid HTML <div> tags.
205  *
206  * @since 1.0.6
207  * @internal Incomplete shortcode
208  * @param array $atts Shortcode attributes
209  * @param string $content
210  * @return string $display Output generated from the downloads queried
211  */
212 function edd_downloads_query( $atts, $content = null ) {
213     extract( shortcode_atts( array(
214             'category'         => '',
215             'exclude_category' => '',
216             'tags'             => '',
217             'exclude_tags'     => '',
218             'relation'         => 'AND',
219             'number'           => 10,
220             'price'            => 'no',
221             'excerpt'          => 'yes',
222             'full_content'     => 'no',
223             'buy_button'       => 'yes',
224             'columns'          => 3,
225             'thumbnails'       => 'true',
226             'orderby'          => 'post_date',
227             'order'            => 'DESC',
228             'ids'              => ''
229         ), $atts )
230     );
231 
232     $query = array(
233         'post_type'      => 'download',
234         'posts_per_page' => absint( $number ),
235         'orderby'        => $orderby,
236         'order'          => $order
237     );
238 
239     switch ( $orderby ) {
240         case 'price':
241             $orderby           = 'meta_value';
242             $query['meta_key'] = 'edd_price';
243             $query['orderby']  = 'meta_value_num';
244         break;
245 
246         case 'title':
247             $query['orderby'] = 'title';
248         break;
249 
250         case 'id':
251             $query['orderby'] = 'ID';
252         break;
253 
254         case 'random':
255             $query['orderby'] = 'rand';
256         break;
257 
258         default:
259             $query['orderby'] = 'post_date';
260         break;
261     }
262 
263     if ( $tags || $category || $exclude_category || $exclude_tags ) {
264         $query['tax_query'] = array(
265             'relation'     => $relation
266         );
267 
268         if ( $tags ) {
269             $query['tax_query'][] = array(
270                 'taxonomy' => 'download_tag',
271                 'terms'    => explode( ',', $tags ),
272                 'field'    => 'slug'
273             );
274         }
275 
276         if ( $category ) {
277             $query['tax_query'][] = array(
278                 'taxonomy' => 'download_category',
279                 'terms'    => explode( ',', $category ),
280                 'field'    => 'slug'
281             );
282         }
283 
284         if ( $exclude_category ) {
285             $query['tax_query'][] = array(
286                 'taxonomy' => 'download_category',
287                 'terms'    => explode( ',', $exclude_category ),
288                 'field'    => 'slug',
289                 'operator' => 'NOT IN',
290             );
291         }
292 
293         if ( $exclude_tags ) {
294             $query['tax_query'][] = array(
295                 'taxonomy' => 'download_tag',
296                 'terms'    => explode( ',', $exclude_tags ),
297                 'field'    => 'slug',
298                 'operator' => 'NOT IN',
299             );
300         }
301     }
302 
303     if( ! empty( $ids ) )
304         $query['post__in'] = explode( ',', $ids );
305 
306     if ( get_query_var( 'paged' ) )
307         $query['paged'] = get_query_var('paged');
308     else if ( get_query_var( 'page' ) )
309         $query['paged'] = get_query_var( 'page' );
310     else
311         $query['paged'] = 1;
312 
313     switch( intval( $columns ) ) :
314         case 1:
315             $column_width = '100%'; break;
316         case 2:
317             $column_width = '50%'; break;
318         case 3:
319             $column_width = '33%'; break;
320         case 4:
321             $column_width = '25%'; break;
322         case 5:
323             $column_width = '20%'; break;
324         case 6:
325             $column_width = '16.6%'; break;
326     endswitch;
327 
328     // Allow the query to be manipulated by other plugins
329     $query = apply_filters( 'edd_downloads_query', $query );
330 
331     $downloads = new WP_Query( $query );
332     if ( $downloads->have_posts() ) :
333         $i = 1;
334         ob_start(); ?>
335         <div class="edd_downloads_list">
336             <?php while ( $downloads->have_posts() ) : $downloads->the_post(); ?>
337                 <div itemscope itemtype="http://schema.org/Product" class="edd_download" id="edd_download_<?php echo get_the_ID(); ?>" style="width: <?php echo $column_width; ?>; float: left;">
338                     <div class="edd_download_inner">
339                         <?php
340 
341                         do_action( 'edd_download_before' );
342 
343                         if ( 'false' != $thumbnails ) :
344                             edd_get_template_part( 'shortcode', 'content-image' );
345                         endif;
346 
347                         edd_get_template_part( 'shortcode', 'content-title' );
348 
349                         if ( $excerpt == 'yes' && $full_content != 'yes' )
350                             edd_get_template_part( 'shortcode', 'content-excerpt' );
351                         else if ( $full_content == 'yes' )
352                             edd_get_template_part( 'shortcode', 'content-full' );
353 
354                         if ( $price == 'yes' )
355                             edd_get_template_part( 'shortcode', 'content-price' );
356 
357                         if ( $buy_button == 'yes' )
358                             edd_get_template_part( 'shortcode', 'content-cart-button' );
359 
360                         do_action( 'edd_download_after' );
361 
362                         ?>
363                     </div>
364                 </div>
365                 <?php if ( $i % $columns == 0 ) { ?><div style="clear:both;"></div><?php } ?>
366             <?php $i++; endwhile; ?>
367 
368             <div style="clear:both;"></div>
369 
370             <div id="edd_download_pagination" class="navigation">
371                 <?php
372                 $big = 999999;
373                 echo paginate_links( array(
374                     'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
375                     'format'  => '?paged=%#%',
376                     'current' => max( 1, $query['paged'] ),
377                     'total'   => $downloads->max_num_pages
378                 ) );
379                 ?>
380             </div>
381             <?php wp_reset_postdata(); ?>
382         </div>
383         <?php
384         $display = ob_get_clean();
385     else:
386         $display = sprintf( _x( 'No %s found', 'download post type name', 'edd' ), edd_get_label_plural() );
387     endif;
388 
389     return apply_filters( 'downloads_shortcode', $display, $atts, $buy_button, $columns, $column_width, $downloads, $excerpt, $full_content, $price, $thumbnails, $query );
390 }
391 add_shortcode( 'downloads', 'edd_downloads_query' );
392 
393 /**
394  * Price Shortcode
395  *
396  * Shows the price of a download.
397  *
398  * @since 1.1.3.3
399  * @param array $atts Shortcode attributes
400  * @param string $content
401  * @return string
402  */
403 function edd_download_price_shortcode( $atts, $content = null ) {
404     extract( shortcode_atts( array(
405             'id' => NULL,
406         ), $atts )
407     );
408 
409     if ( is_null( $id ) )
410         $id = get_the_ID();
411 
412     return edd_price( $id, false );
413 }
414 add_shortcode( 'edd_price', 'edd_download_price_shortcode' );
415 
416 /**
417  * Receipt Shortcode
418  *
419  * Shows an order receipt.
420  *
421  * @since 1.4
422  * @param array $atts Shortcode attributes
423  * @param string $content
424  * @return string
425  */
426 function edd_receipt_shortcode( $atts, $content = null ) {
427     global $edd_receipt_args;
428 
429     $edd_receipt_args = shortcode_atts( array(
430         'error'           => __( 'Sorry, trouble retrieving payment receipt.', 'edd' ),
431         'price'           => true,
432         'discount'        => true,
433         'products'        => true,
434         'date'            => true,
435         'notes'           => true,
436         'payment_key'     => true,
437         'payment_method'  => true,
438         'payment_id'      => true
439     ), $atts );
440 
441     $session = edd_get_purchase_session();
442 
443     if ( isset( $_GET[ 'purchase_key' ] ) ) {
444         $purchase_key = urldecode( $_GET[ 'purchase_key' ] );
445     } else if ( $session ) {
446         $purchase_key = $session[ 'purchase_key' ];
447     }
448 
449     // No key found
450     if ( ! isset( $purchase_key ) )
451         return $edd_receipt_args[ 'error' ];
452 
453     $edd_receipt_args[ 'id' ] = edd_get_purchase_id_by_key( $purchase_key );
454     $user = edd_get_payment_meta_user_info( $edd_receipt_args[ 'id' ] );
455 
456     // Not the proper user
457     if ( is_user_logged_in() && $user[ 'id' ] != get_current_user_id() ) {
458         return $edd_receipt_args[ 'error' ];
459     }
460 
461     ob_start();
462 
463     edd_get_template_part( 'shortcode', 'receipt' );
464 
465     $display = ob_get_clean();
466 
467     return $display;
468 }
469 add_shortcode( 'edd_receipt', 'edd_receipt_shortcode' );
470 
471 /**
472  * Profile Editor Shortcode
473  *
474  * Outputs the EDD Profile Editor to allow users to amend their details from the
475  * front-end. This function uses the EDD templating system allowing users to
476  * override the default profile editor template. The profile editor template is located
477  * under templates/profile-editor.php, however, it can be altered by creating a
478  * file called profile-editor.php in the edd_template directory in your active theme's
479  * folder. Please visit the EDD Documentation for more information on how the
480  * templating system is used.
481  *
482  * @since 1.4
483  * @author Sunny Ratilal
484  * @param array $atts Shortcode attributes
485  * @param string $content
486  * @return $display Output generated from the profile editor
487  */
488 function edd_profile_editor_shortcode( $atts, $content = null ) {
489     ob_start();
490 
491     edd_get_template_part( 'shortcode', 'profile-editor' );
492 
493     $display = ob_get_clean();
494 
495     return $display;
496 }
497 add_shortcode( 'edd_profile_editor', 'edd_profile_editor_shortcode' );
498 
499 /**
500  * Process Profile Updater Form
501  *
502  * Processes the profile updater form by updating the necessary fields
503  *
504  * @since 1.4
505  * @author Sunny Ratilal
506  * @param array $data Data sent from the profile editor
507  * @return void
508  */
509 function edd_process_profile_editor_updates( $data ) {
510     // Profile field change request
511     if ( empty( $_POST['edd_profile_editor_submit'] ) && !is_user_logged_in() )
512         return false;
513 
514     // Nonce security
515     if ( ! wp_verify_nonce( $data['edd_profile_editor_nonce'], 'edd-profile-editor-nonce' ) )
516         return false;
517 
518     $user_id = get_current_user_id();
519 
520     $display_name = sanitize_text_field( $data['edd_display_name'] );
521     $first_name   = sanitize_text_field( $data['edd_first_name'] );
522     $last_name    = sanitize_text_field( $data['edd_last_name'] );
523     $email        = sanitize_email( $data['edd_email'] );
524 
525     $userdata = array(
526         'ID'           => $user_id,
527         'first_name'   => $first_name,
528         'last_name'    => $last_name,
529         'display_name' => $display_name,
530         'user_email'   => $email
531     );
532 
533     // New password
534     if ( ! empty( $data['edd_new_user_pass1'] ) ) {
535         if ( $data['edd_new_user_pass1'] !== $data['edd_new_user_pass2'] ) {
536             edd_set_error( 'password_mismatch', __( 'The passwords you entered do not match. Please try again.', 'edd' ) );
537         } else {
538             $userdata['user_pass'] = $data['edd_new_user_pass1'];
539         }
540     }
541 
542     // Update the user
543     $updated = wp_update_user( $userdata );
544 
545     if ( $updated ) {
546         wp_redirect( add_query_arg( 'updated', 'true', $data['edd_redirect'] ) );
547         exit;
548     }
549 }
550 add_action( 'edd_edit_user_profile', 'edd_process_profile_editor_updates' );
Easy Digital Downloads API documentation generated by ApiGen 2.8.0