1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 16 17 18 19 20 21 22 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 35 36 37 38 39 40 41 42 43 44 45 46 47 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 ) ) . ' – ' . $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;">—
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 -->
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 143 144 145 146 147 148 149 150 151 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 193 194 195 196 197 198 199 200 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 217 218 219 220 221 222 223 224 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 241 242 243 244 245 246 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 263 264 265 266 267 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 283 284 285 286 287 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 300 301 302 303 304 305 306 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 320 321 322 323 324 325 326 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 335 336 337 338 339 340 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 349 350 351 352 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 387 388 389 390
391 function edd_get_templates_dir() {
392 return EDD_PLUGIN_DIR . 'templates';
393 }
394
395 396 397 398 399 400
401 function edd_get_templates_url() {
402 return EDD_PLUGIN_URL . 'templates';
403 }
404
405 406 407 408 409 410 411 412 413 414 415 416 417 418
419 function edd_get_template_part( $slug, $name = null, $load = true ) {
420
421 do_action( 'get_template_part_' . $slug, $slug, $name );
422
423
424 $templates = array();
425 if ( isset( $name ) )
426 $templates[] = $slug . '-' . $name . '.php';
427 $templates[] = $slug . '.php';
428
429
430 $templates = apply_filters( 'edd_get_template_part', $templates, $slug, $name );
431
432
433 return edd_locate_template( $templates, $load, false );
434 }
435
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
453 function edd_locate_template( $template_names, $load = false, $require_once = true ) {
454
455 $located = false;
456
457
458 foreach ( (array) $template_names as $template_name ) {
459
460
461 if ( empty( $template_name ) )
462 continue;
463
464
465 $template_name = ltrim( $template_name, '/' );
466
467
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
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
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 492 493 494 495 496 497 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 510 511 512 513 514 515 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 );