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_after_download_content
  • edd_append_purchase_link
  • edd_before_download_content
  • edd_downloads_default_content
  • edd_downloads_default_excerpt
  • edd_filter_success_page_content
  • edd_get_button_colors
  • edd_get_button_styles
  • edd_get_purchase_download_links
  • edd_get_purchase_link
  • edd_get_template_part
  • edd_get_templates_dir
  • edd_get_templates_url
  • edd_locate_template
  • edd_microdata_title
  • edd_microdata_wrapper
  • edd_purchase_variable_pricing
  • edd_show_has_purchased_item_message
  1 <?php
  2 /**
  3  * Template Functions
  4  *
  5  * @package     EDD
  6  * @subpackage  Functions/Templates
  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  * Append Purchase Link
 17  *
 18  * Automatically appends the purchase link to download content, if enabled.
 19  *
 20  * @since 1.0
 21  * @param int $download_id Download ID
 22  * @return void
 23  */
 24 
 25 function edd_append_purchase_link( $download_id ) {
 26     if ( ! get_post_meta( $download_id, '_edd_hide_purchase_link', true ) ) {
 27         echo edd_get_purchase_link( array( 'download_id' => $download_id ) );
 28     }
 29 }
 30 add_action( 'edd_after_download_content', 'edd_append_purchase_link' );
 31 
 32 
 33 /**
 34  * Get Purchase Link
 35  *
 36  * Builds a Purchase link for a specified download based on arguments passed.
 37  * This function is used all over EDD to generate the Purchase or Add to Cart
 38  * buttons. If no arguments are passed, the function uses the defaults that have
 39  * been set by the plugin. The Purchase link is built for simple and variable
 40  * pricing and filters are available throughout the function to override
 41  * certain elements of the function.
 42  *
 43  * $download_id = null, $link_text = null, $style = null, $color = null, $class = null
 44  *
 45  * @since 1.0
 46  * @param array $args Arguments for display
 47  * @return string $purchase_form
 48  */
 49 function edd_get_purchase_link( $args = array() ) {
 50     global $edd_options, $post;
 51 
 52     if ( ! isset( $edd_options['purchase_page'] ) || $edd_options['purchase_page'] == 0 ) {
 53         edd_set_error( 'set_checkout', sprintf( __( 'No checkout page has been configured. Visit <a href="%s">Settings</a> to set one.', 'edd' ), admin_url( 'edit.php?post_type=download&page=edd-settings' ) ) );
 54         edd_print_errors();
 55         return false;
 56     }
 57 
 58     $defaults = apply_filters( 'edd_purchase_link_defaults', array(
 59         'download_id' => $post->ID,
 60         'price'       => (bool) true,
 61         'text'        => isset( $edd_options[ 'add_to_cart_text' ] ) && $edd_options[ 'add_to_cart_text' ]  != '' ? $edd_options[ 'add_to_cart_text' ]  : __( 'Purchase', 'edd' ),
 62         'style'       => isset( $edd_options[ 'button_style' ] )     ? $edd_options[ 'button_style' ]       : 'button',
 63         'color'       => isset( $edd_options[ 'checkout_color' ] )   ? $edd_options[ 'checkout_color' ]     : 'blue',
 64         'class'       => 'edd-submit'
 65     ) );
 66 
 67     $args = wp_parse_args( $args, $defaults );
 68 
 69     $variable_pricing = edd_has_variable_prices( $args['download_id'] );
 70     $data_variable    = $variable_pricing ? ' data-variable-price=yes' : 'data-variable-price=no';
 71     $type             = edd_single_price_option_mode( $args['download_id'] ) ? 'data-price-mode=multi' : 'data-price-mode=single';
 72     if ( $args['price'] && ! $variable_pricing ) {
 73         $price = edd_get_download_price( $args['download_id'] );
 74         $args['text'] = edd_currency_filter( edd_format_amount( $price ) ) . '&nbsp;&ndash;&nbsp;' . $args['text'];
 75     }
 76 
 77     if ( edd_item_in_cart( $args['download_id'] ) && ! $variable_pricing ) {
 78         $button_display   = 'style="display:none;"';
 79         $checkout_display = '';
 80     } else {
 81         $button_display   = '';
 82         $checkout_display = 'style="display:none;"';
 83     }
 84 
 85     ob_start();
 86 ?>
 87     <!--dynamic-cached-content-->
 88     <form id="edd_purchase_<?php echo $args['download_id']; ?>" class="edd_download_purchase_form" method="post">
 89 
 90         <?php do_action( 'edd_purchase_link_top', $args['download_id'], $args['price'] ); ?>
 91 
 92         <div class="edd_purchase_submit_wrapper">
 93             <?php
 94                 printf(
 95                     '<input type="submit" class="edd-add-to-cart %1$s" name="edd_purchase_download" value="%2$s" data-action="edd_add_to_cart" data-download-id="%3$s" %4$s %5$s %6$s/>',
 96                     implode( ' ', array( $args['style'], $args['color'], trim( $args['class'] ) ) ),
 97                     esc_attr( $args['text'] ),
 98                     esc_attr( $args['download_id'] ),
 99                     esc_attr( $data_variable ),
100                     esc_attr( $type ),
101                     $button_display
102                 );
103 
104                 printf(
105                     '<a href="%1$s" class="%2$s %3$s" %4$s>' . __( 'Checkout', 'edd' ) . '</a>',
106                     esc_url( edd_get_checkout_uri() ),
107                     esc_attr( 'edd_go_to_checkout' ),
108                     implode( ' ', array( $args['style'], $args['color'], trim( $args['class'] ) ) ),
109                     $checkout_display
110                 );
111             ?>
112 
113             <?php if ( edd_is_ajax_enabled() ) : ?>
114                 <span class="edd-cart-ajax-alert">
115                     <img alt="<?php _e( 'Loading', 'edd' ); ?>" src="<?php echo esc_url( EDD_PLUGIN_URL . 'assets/images/loading.gif' ); ?>" class="edd-cart-ajax" style="display: none;" />
116                     <span class="edd-cart-added-alert" style="display: none;">&mdash;
117                         <?php printf(
118                                 __( 'Item successfully added to your %scart%s.', 'edd' ),
119                                 '<a href="' . esc_url( edd_get_checkout_uri() ) . '" title="' . __( 'Go to Checkout', 'edd' ) . '">',
120                                 '</a>'
121                             );
122                         ?>
123                     </span>
124                 </span>
125             <?php endif; ?>
126         </div><!--end .edd_purchase_submit_wrapper-->
127 
128         <input type="hidden" name="download_id" value="<?php echo esc_attr( $args['download_id'] ); ?>">
129         <input type="hidden" name="edd_action" value="add_to_cart">
130 
131         <?php do_action( 'edd_purchase_link_end', $args['download_id'] ); ?>
132 
133     </form><!--end #edd_purchase_<?php echo esc_attr( $args['download_id'] ); ?>-->
134     <!--/dynamic-cached-content-->
135 <?php
136     $purchase_form = ob_get_clean();
137 
138     return apply_filters( 'edd_purchase_download_form', $purchase_form, $args );
139 }
140 
141 /**
142  * Variable price output
143  *
144  * Outputs variable pricing options for each download or a specified downloads in a list.
145  * The output generated can be overriden by the filters provided or by removing
146  * the action and adding your own custom action.
147  *
148  * @since 1.2.3
149  * @param int $download_id Download ID
150  * @param bool $show_price
151  * @return void
152  */
153 function edd_purchase_variable_pricing( $download_id, $show_price ) {
154     $variable_pricing = edd_has_variable_prices( $download_id );
155 
156     if ( ! $variable_pricing || empty( $show_price ) )
157         return;
158 
159     $prices = apply_filters( 'edd_purchase_variable_prices', edd_get_variable_prices( $download_id ), $download_id );
160 
161     $type   = edd_single_price_option_mode( $download_id ) ? 'checkbox' : 'radio';
162 
163     do_action( 'edd_before_price_options', $download_id ); ?>
164     <div class="edd_price_options">
165         <ul>
166             <?php
167             if ( $prices ) :
168                 foreach ( $prices as $key => $price ) :
169                     $amount = $price[ 'amount' ];
170                     printf(
171                         '<li><label for="%3$s"><input type="%2$s" %1$s name="edd_options[price_id][]" id="%3$s" class="%4$s" value="%5$s" %7$s/> %6$s</label></li>',
172                         checked( 0, $key, false ),
173                         $type,
174                         esc_attr( 'edd_price_option_' . $download_id . '_' . $key ),
175                         esc_attr( 'edd_price_option_' . $download_id ),
176                         esc_attr( $key ),
177                         esc_html( $price['name'] . ' - ' . edd_currency_filter( edd_format_amount( $amount ) ) ),
178                         checked( isset( $_GET['price_option'] ), $key, false )
179                     );
180                 endforeach;
181             endif;
182             do_action( 'edd_after_price_options_list', $download_id, $prices, $type );
183             ?>
184         </ul>
185     </div><!--end .edd_price_options-->
186 <?php
187     add_action( 'edd_after_price_options', $download_id );
188 }
189 add_action( 'edd_purchase_link_top', 'edd_purchase_variable_pricing', 10, 2 );
190 
191 /**
192  * Before Download Content
193  *
194  * Adds an action to the begining of download post content that can be hooked to
195  * by other functions.
196  *
197  * @since 1.0.8
198  * @global $post
199  * @param $content string The the_content field of the download object
200  * @return $content string the content with any additional data attached
201  */
202 function edd_before_download_content( $content ) {
203     global $post;
204 
205     if ( $post->post_type == 'download' && is_singular() && is_main_query() ) {
206         ob_start();
207         $content .= ob_get_clean();
208         do_action( 'edd_before_download_content', $post->ID );
209     }
210 
211     return $content;
212 }
213 add_filter( 'the_content', 'edd_before_download_content' );
214 
215 /**
216  * After Download Content
217  *
218  * Adds an action to the end of download post content that can be hooked to by
219  * other functions.
220  *
221  * @since 1.0.8
222  * @global $post
223  * @param $content string The the_content field of the download object
224  * @return $content string the content with any additional data attached
225  */
226 function edd_after_download_content( $content ) {
227     global $post;
228 
229     if ( $post && $post->post_type == 'download' && is_singular() && is_main_query() ) {
230         ob_start();
231         do_action( 'edd_after_download_content', $post->ID );
232         $content .= ob_get_clean();
233     }
234 
235     return $content;
236 }
237 add_filter( 'the_content', 'edd_after_download_content' );
238 
239 /**
240  * Filter Success Page Content
241  *
242  * Applies filters to the success page content.
243  *
244  * @since 1.0
245  * @param string $content Content before filters
246  * @return string $content Filtered content
247  */
248 function edd_filter_success_page_content( $content ) {
249     global $edd_options;
250 
251     if ( isset( $edd_options['success_page'] ) && isset( $_GET['payment-confirmation'] ) && is_page( $edd_options['success_page'] ) ) {
252         if ( has_filter( 'edd_payment_confirm_' . $_GET['payment-confirmation'] ) ) {
253             $content = apply_filters( 'edd_payment_confirm_' . $_GET['payment-confirmation'], $content );
254         }
255     }
256 
257     return $content;
258 }
259 add_filter( 'the_content', 'edd_filter_success_page_content' );
260 
261 /**
262  * Get Button Colors
263  *
264  * Returns an array of button colors.
265  *
266  * @since 1.0
267  * @return array $colors Button colors
268  */
269 function edd_get_button_colors() {
270     $colors = array(
271         'gray'      => __( 'Gray', 'edd' ),
272         'blue'      => __( 'Blue', 'edd' ),
273         'green'     => __( 'Green', 'edd' ),
274         'yellow'    => __( 'Yellow', 'edd' ),
275         'dark-gray' => __( 'Dark Gray', 'edd' ),
276     );
277 
278     return apply_filters( 'edd_button_colors', $colors );
279 }
280 
281 /**
282  * Get Button Styles
283  *
284  * Returns an array of button styles.
285  *
286  * @since 1.2.2
287  * @return array $styles Button styles
288  */
289 function edd_get_button_styles() {
290     $styles = array(
291         'button'    => __( 'Button', 'edd' ),
292         'plain'     => __( 'Plain Text', 'edd' )
293     );
294 
295     return apply_filters( 'edd_button_styles', $styles );
296 }
297 
298 /**
299  * Show Has Purchased Item Message
300  *
301  * Prints a notice when user has already purchased the item.
302  *
303  * @since 1.0
304  * @global $user_ID
305  * @param int $download_id Download ID
306  * @return void
307  */
308 function edd_show_has_purchased_item_message( $download_id ) {
309     global $user_ID;
310 
311     if ( edd_has_user_purchased( $user_ID, $download_id ) ) {
312         $alert = '<p class="edd_has_purchased">' . __( 'You have already purchased this item, but you may purchase it again.', 'edd' ) . '</p>';
313         echo apply_filters( 'edd_show_has_purchased_item_message', $alert );
314     }
315 }
316 add_action( 'edd_after_download_content', 'edd_show_has_purchased_item_message' );
317 
318 /**
319  * Default formatting for download excerpts
320  *
321  * This excerpt is primarily used in the [downloads] short code
322  *
323  * @since 1.0.8.4
324  * @param string $excerpt Content before filterting
325  * @return string $excerpt Content after filterting
326  * @return string
327  */
328 function edd_downloads_default_excerpt( $excerpt ) {
329     return do_shortcode( wpautop( $excerpt ) );
330 }
331 add_filter( 'edd_downloads_excerpt', 'edd_downloads_default_excerpt' );
332 
333 /**
334  * Default formatting for full download content
335  *
336  * This is primarily used in the [downloads] short code
337  *
338  * @since 1.0.8.4
339  * @param string $content Content before filterting
340  * @return string $content Content after filterting 
341  */
342 function edd_downloads_default_content( $content ) {
343     return do_shortcode( wpautop( $content ) );
344 }
345 add_filter( 'edd_downloads_content', 'edd_downloads_default_content' );
346 
347 /**
348  * Gets the download links for each item purchased
349  *
350  * @since 1.1.5
351  * @param array $purchase_data Purchase data
352  * @return string
353  */
354 function edd_get_purchase_download_links( $purchase_data ) {
355     if ( ! is_array( $purchase_data['downloads'] ) )
356         return '<div class="edd-error">' . __( 'No downloads found', 'edd' ) . '</div>';
357 
358     $links = '<ul class="edd_download_links">';
359 
360     foreach ( $purchase_data['downloads'] as $download ) {
361         $links .= '<li>';
362             $links .= '<h3 class="edd_download_link_title">' . esc_html( get_the_title( $download['id'] ) ) . '</h3>';
363             $price_id = isset( $download['options'] ) && isset( $download['options']['price_id'] ) ? $download['options']['price_id'] : null;
364             $files = edd_get_download_files( $download['id'], $price_id );
365             if ( is_array( $files ) ) {
366                 foreach ( $files as $filekey => $file ) {
367                     $links .= '<div class="edd_download_link_file">';
368                         $links .= '<a href="' . esc_url( edd_get_download_file_url( $purchase_data['purchase_key'], $purchase_data['user_email'], $filekey, $download['id'], $price_id ) ) . '">';
369                             if ( isset( $file['name'] ) )
370                                 $links .= esc_html( $file['name'] );
371                             else
372                                 $links .= esc_html( $file['file'] );
373                         $links .= '</a>';
374                     $links .= '</div>';
375                 }
376             }
377         $links .= '</li>';
378     }
379 
380     $links .= '</ul>';
381 
382     return $links;
383 }
384 
385 /**
386  * Returns the path to the EDD templates directory
387  *
388  * @since 1.2
389  * @return string
390  */
391 function edd_get_templates_dir() {
392     return EDD_PLUGIN_DIR . 'templates';
393 }
394 
395 /**
396  * Returns the URL to the EDD templates directory
397  *
398  * @since 1.3.2.1
399  * @return string
400  */
401 function edd_get_templates_url() {
402     return EDD_PLUGIN_URL . 'templates';
403 }
404 
405 /**
406  * Retrieves a template part
407  *
408  * @since v1.2
409  *
410  * Taken from bbPress
411  *
412  * @param string $slug
413  * @param string $name Optional. Default null
414  *
415  * @uses edd_locate_template()
416  * @uses load_template()
417  * @uses get_template_part()
418  */
419 function edd_get_template_part( $slug, $name = null, $load = true ) {
420     // Execute code for this part
421     do_action( 'get_template_part_' . $slug, $slug, $name );
422 
423     // Setup possible parts
424     $templates = array();
425     if ( isset( $name ) )
426         $templates[] = $slug . '-' . $name . '.php';
427     $templates[] = $slug . '.php';
428 
429     // Allow template parst to be filtered
430     $templates = apply_filters( 'edd_get_template_part', $templates, $slug, $name );
431 
432     // Return the part that is found
433     return edd_locate_template( $templates, $load, false );
434 }
435 
436 /**
437  * Retrieve the name of the highest priority template file that exists.
438  *
439  * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
440  * inherit from a parent theme can just overload one file. If the template is
441  * not found in either of those, it looks in the theme-compat folder last.
442  *
443  * Taken from bbPress
444  *
445  * @since 1.2
446  *
447  * @param string|array $template_names Template file(s) to search for, in order.
448  * @param bool $load If true the template file will be loaded if it is found.
449  * @param bool $require_once Whether to require_once or require. Default true.
450  *   Has no effect if $load is false.
451  * @return string The template filename if one is located.
452  */
453 function edd_locate_template( $template_names, $load = false, $require_once = true ) {
454     // No file found yet
455     $located = false;
456 
457     // Try to find a template file
458     foreach ( (array) $template_names as $template_name ) {
459 
460         // Continue if template is empty
461         if ( empty( $template_name ) )
462             continue;
463 
464         // Trim off any slashes from the template name
465         $template_name = ltrim( $template_name, '/' );
466 
467         // Check child theme first
468         if ( file_exists( trailingslashit( get_stylesheet_directory() ) . 'edd_templates/' . $template_name ) ) {
469             $located = trailingslashit( get_stylesheet_directory() ) . 'edd_templates/' . $template_name;
470             break;
471 
472         // Check parent theme next
473         } elseif ( file_exists( trailingslashit( get_template_directory() ) . 'edd_templates/' . $template_name ) ) {
474             $located = trailingslashit( get_template_directory() ) . 'edd_templates/' . $template_name;
475             break;
476 
477         // Check theme compatibility last
478         } elseif ( file_exists( trailingslashit( edd_get_templates_dir() ) . $template_name ) ) {
479             $located = trailingslashit( edd_get_templates_dir() ) . $template_name;
480             break;
481         }
482     }
483 
484     if ( ( true == $load ) && ! empty( $located ) )
485         load_template( $located, $require_once );
486 
487     return $located;
488 }
489 
490 /**
491  * Add Microdata to download titles
492  *
493  * @since 1.5
494  * @author Sunny Ratilal
495  * @param string $title Post Title
496  * @param int $id Post ID
497  * @return string $title New title
498  */
499 function edd_microdata_title( $title, $id = 0 ) {
500     if ( is_singular( 'download' ) && 'download' == get_post_type( intval( $id ) ) ) {
501         $title = '<span itemprop="name">' . $title . '</span>';
502     }
503 
504     return $title;
505 }
506 add_filter( 'the_title', 'edd_microdata_title', 10, 2 );
507 
508 /**
509  * Add Microdata to download description
510  *
511  * @since 1.5
512  * @author Sunny Ratilal
513  * @param string $title Post Title
514  * @param int $id Post ID
515  * @return string $title New title
516  */
517 function edd_microdata_wrapper( $content ) {
518     global $post;
519     if ( $post->post_type == 'download' && is_singular() && is_main_query() ) {
520         $content = apply_filters( 'edd_microdata_wrapper', '<div itemscope itemtype="http://schema.org/Product" itemprop="description">' . $content . '</div>' );
521     }
522     return $content;
523 }
524 add_filter( 'the_content', 'edd_microdata_wrapper', 10 );
Easy Digital Downloads API documentation generated by ApiGen 2.8.0