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_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
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 52 53 54 55 56 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 69 70 71 72 73 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 86 87 88 89 90 91 92 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 101 102 103 104 105 106 107 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 116 117 118 119 120 121 122 123 124 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 137 138 139 140 141 142 143 144 145 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 175 176 177 178 179 180 181 182 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 200 201 202 203 204 205 206 207 208 209 210 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
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 395 396 397 398 399 400 401 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 418 419 420 421 422 423 424 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
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
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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 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 501 502 503 504 505 506 507 508
509 function edd_process_profile_editor_updates( $data ) {
510
511 if ( empty( $_POST['edd_profile_editor_submit'] ) && !is_user_logged_in() )
512 return false;
513
514
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
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
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' );