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 function edd_add_download_meta_box() {
24
25 add_meta_box( 'downloadinformation', sprintf( __( '%1$s Configuration', 'edd' ), edd_get_label_singular(), edd_get_label_plural() ), 'edd_render_download_meta_box', 'download', 'normal', 'default' );
26
27
28 add_meta_box( 'edd_product_notes', __( 'Product Notes', 'edd' ), 'edd_render_product_notes_meta_box', 'download', 'normal', 'default' );
29
30 if ( current_user_can( 'edit_product', get_the_ID() ) ) {
31
32 add_meta_box( 'edd_download_stats', sprintf( __( '%1$s Stats', 'edd' ), edd_get_label_singular(), edd_get_label_plural() ), 'edd_render_stats_meta_box', 'download', 'side', 'high' );
33 }
34 }
35 add_action( 'add_meta_boxes', 'edd_add_download_meta_box' );
36
37 38 39 40 41 42 43 44
45 function edd_download_meta_box_save( $post_id) {
46 global $post;
47
48 if ( ! isset( $_POST['edd_download_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['edd_download_meta_box_nonce'], basename( __FILE__ ) ) )
49 return $post_id;
50
51 if ( ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) return $post_id;
52
53 if ( isset( $post->post_type ) && $post->post_type == 'revision' )
54 return $post_id;
55
56 if ( ! current_user_can( 'edit_pages', $post_id ) ) {
57 return $post_id;
58 }
59
60
61 $fields = apply_filters( 'edd_metabox_fields_save', array(
62 'edd_price',
63 '_variable_pricing',
64 '_edd_price_options_mode',
65 'edd_variable_prices',
66 'edd_download_files',
67 '_edd_purchase_text',
68 '_edd_purchase_style',
69 '_edd_purchase_color',
70 '_edd_download_limit',
71 '_edd_hide_purchase_link',
72 'edd_product_notes'
73 )
74 );
75
76 foreach ( $fields as $field ) {
77 if ( isset( $_POST[ $field ] ) ) {
78 if ( is_string( $_POST[ $field ] ) ) {
79 $new = esc_attr( $_POST[ $field ] );
80 } else {
81 $new = $_POST[ $field ];
82 }
83
84 $new = apply_filters( 'edd_metabox_save_' . $field, $new );
85
86 update_post_meta( $post_id, $field, $new );
87 } else {
88 delete_post_meta( $post_id, $field );
89 }
90 }
91 }
92 add_action( 'save_post', 'edd_download_meta_box_save' );
93
94 95 96 97 98 99 100 101 102
103 function edd_sanitize_price_save( $price ) {
104 global $edd_options;
105
106 $thousands_sep = ! empty( $edd_options['thousands_separator'] ) ? $edd_options['thousands_separator'] : ',';
107 $decimal_sep = ! empty( $edd_options['decimal_separator'] ) ? $edd_options['decimal_separator'] : '.';
108
109 if ( $thousands_sep == ',' ) {
110 $price = str_replace( ',', '', $price );
111 }
112
113 return $price;
114 }
115 add_filter( 'edd_metabox_save_edd_price', 'edd_sanitize_price_save' );
116
117 118 119 120 121 122 123 124 125
126 function edd_sanitize_variable_prices_save( $prices ) {
127
128 return array_values( $prices );
129 }
130 add_filter( 'edd_metabox_save_edd_variable_prices', 'edd_sanitize_variable_prices_save' );
131
132 133 134 135 136 137 138 139 140
141 function edd_sanitize_files_save( $files ) {
142
143 return array_values( $files );
144 }
145 add_filter( 'edd_metabox_save_edd_download_files', 'edd_sanitize_files_save' );
146
147
148
149
150 151 152 153 154 155 156 157 158
159 function edd_render_download_meta_box() {
160 global $post, $edd_options;
161
162 do_action( 'edd_meta_box_fields', $post->ID );
163 wp_nonce_field( basename( __FILE__ ), 'edd_download_meta_box_nonce' );
164 }
165
166 167 168 169 170 171 172 173 174 175 176 177 178
179 function edd_render_price_field( $post_id ) {
180 global $edd_options;
181
182 $price = edd_get_download_price( $post_id );
183 $variable_pricing = edd_has_variable_prices( $post_id );
184 $prices = edd_get_variable_prices( $post_id );
185 $single_option_mode = edd_single_price_option_mode( $post_id );
186
187 $price_display = $variable_pricing ? ' style="display:none;"' : '';
188 $variable_display = $variable_pricing ? '' : ' style="display:none;"';
189 ?>
190 <p>
191 <strong><?php echo apply_filters( 'edd_price_options_heading', __( 'Pricing Options:', 'edd' ) ); ?></strong>
192 </p>
193
194 <p>
195 <label for="edd_variable_pricing">
196 <input type="checkbox" name="_variable_pricing" id="edd_variable_pricing" value="1" <?php checked( 1, $variable_pricing ); ?> />
197 <?php echo apply_filters( 'edd_variable_pricing_toggle_text', __( 'Enable variable pricing', 'edd' ) ); ?>
198 </label>
199 </p>
200
201 <div id="edd_regular_price_field" class="edd_pricing_fields" <?php echo $price_display; ?>>
202 <?php if ( ! isset( $edd_options['currency_position'] ) || $edd_options['currency_position'] == 'before' ) : ?>
203 <?php echo edd_currency_filter( '' ); ?><input type="text" name="edd_price" id="edd_price" value="<?php echo isset( $price ) ? esc_attr( edd_format_amount( $price ) ) : ''; ?>" size="30" style="width: 80px;" placeholder="9.99"/>
204 <?php else : ?>
205 <input type="text" name="edd_price" id="edd_price" value="<?php echo isset( $price ) ? esc_attr( edd_format_amount( $price ) ) : ''; ?>" size="30" style="width: 80px;" placeholder="9.99"/><?php echo edd_currency_filter( '' ); ?>
206 <?php endif; ?>
207
208 <?php do_action( 'edd_price_field', $post_id ); ?>
209 </div>
210
211 <?php do_action( 'edd_after_price_field', $post_id ); ?>
212
213 <div id="edd_variable_price_fields" class="edd_pricing_fields" <?php echo $variable_display; ?>>
214 <input type="hidden" id="edd_variable_prices" class="edd_variable_prices_name_field" value=""/>
215 <p>
216 <input type="checkbox" name="_edd_price_options_mode" id="edd_price_options_mode"<?php checked( 1, $single_option_mode ); ?> />
217 <label for="edd_price_options_mode"><?php apply_filters( 'edd_multi_option_purchase_text', _e( 'Enable multi option purchase mode. Leave unchecked to only permit a single price option to be purchased', 'edd' ) ); ?></label>
218 </p>
219 <div id="edd_price_fields" class="edd_meta_table_wrap">
220 <table class="widefat" width="100%" cellpadding="0" cellspacing="0">
221 <thead>
222 <tr>
223 <th><?php _e( 'Option Name', 'edd' ); ?></th>
224 <th style="width: 90px"><?php _e( 'Price', 'edd' ); ?></th>
225 <?php do_action( 'edd_download_price_table_head', $post_id ); ?>
226 <th style="width: 2%"></th>
227 </tr>
228 </thead>
229 <tbody>
230 <?php
231 if ( ! empty( $prices ) ) :
232 foreach ( $prices as $key => $value ) :
233 $name = isset( $value['name'] ) ? $value['name'] : '';
234 $amount = isset( $value['amount'] ) ? $value['amount'] : '';
235
236 $args = apply_filters( 'edd_price_row_args', compact( 'name', 'amount' ), $value );
237 ?>
238 <tr class="edd_variable_prices_wrapper">
239 <?php do_action( 'edd_render_price_row', $key, $args, $post_id ); ?>
240 </tr>
241 <?php
242 endforeach;
243 else :
244 ?>
245 <tr class="edd_variable_prices_wrapper">
246 <?php do_action( 'edd_render_price_row', 0, array(), $post_id ); ?>
247 </tr>
248 <?php endif; ?>
249
250 <tr>
251 <td class="submit" colspan="4" style="float: none; clear:both; background:#fff;">
252 <a class="button-secondary edd_add_repeatable" style="margin: 6px 0;"><?php _e( 'Add New Price', 'edd' ); ?></a>
253 </td>
254 </tr>
255 </tbody>
256 </table>
257 </div>
258 </div><!--end
259 <?php
260 }
261 add_action( 'edd_meta_box_fields', 'edd_render_price_field', 10 );
262
263 264 265 266 267 268 269 270 271
272 function edd_render_price_row( $key, $args = array(), $post_id ) {
273 global $edd_options;
274
275 $defaults = array(
276 'name' => null,
277 'amount' => null
278 );
279
280 $args = wp_parse_args( $args, $defaults );
281 extract( $args, EXTR_SKIP );
282 ?>
283 <td>
284 <input type="text" class="edd_variable_prices_name" placeholder="<?php _e( 'Option Name', 'edd' ); ?>" name="edd_variable_prices[<?php echo $key; ?>][name]" id="edd_variable_prices[<?php echo $key; ?>][name]" value="<?php echo esc_attr( $name ); ?>" size="20" style="width:100%" />
285 </td>
286
287 <td>
288 <?php if( ! isset( $edd_options['currency_position'] ) || $edd_options['currency_position'] == 'before' ) : ?>
289 <span><?php echo edd_currency_filter( '' ); ?></span> <input type="text" class="edd_variable_prices_amount text" value="<?php echo $amount; ?>" placeholder="9.99" name="edd_variable_prices[<?php echo $key; ?>][amount]" id="edd_variable_prices[<?php echo $key; ?>][amount]" size="30" style="width:80px;" />
290 <?php else : ?>
291 <input type="text" class="edd_variable_prices_amount text" value="<?php echo $amount; ?>" placeholder="9.99" name="edd_variable_prices[<?php echo $key; ?>][amount]" id="edd_variable_prices[<?php echo $key; ?>][amount]" size="30" style="width:80px;" /><?php echo edd_currency_filter( '' ); ?>
292 <?php endif; ?>
293 </td>
294
295 <?php do_action( 'edd_download_price_table_row', $post_id, $key, $args ); ?>
296
297 <td>
298 <a href="#" class="edd_remove_repeatable" data-type="price" style="background: url(<?php echo admin_url('/images/xit.gif'); ?>) no-repeat;">×</a>
299 </td>
300 <?php
301 }
302 add_action( 'edd_render_price_row', 'edd_render_price_row', 10, 3 );
303
304 305 306 307 308 309 310 311 312 313 314 315
316 function edd_render_files_field( $post_id ) {
317 $files = edd_get_download_files( $post_id );
318 $variable_pricing = edd_has_variable_prices( $post_id );
319 $variable_display = $variable_pricing ? '' : 'display:none;';
320 ?>
321 <div id="edd_download_files">
322 <p>
323 <strong><?php _e( 'File Downloads:', 'edd' ); ?></strong>
324 </p>
325
326 <input type="hidden" id="edd_download_files" class="edd_repeatable_upload_name_field" value=""/>
327
328 <div id="edd_file_fields" class="edd_meta_table_wrap">
329 <table class="widefat" width="100%" cellpadding="0" cellspacing="0">
330 <thead>
331 <tr>
332 <th style="width: 20%"><?php _e( 'File Name', 'edd' ); ?></th>
333 <th><?php _e( 'File URL', 'edd' ); ?></th>
334 <th class="pricing" style="width: 20%; <?php echo $variable_display; ?>"><?php _e( 'Price Assignment', 'edd' ); ?></th>
335 <?php do_action( 'edd_download_file_table_head', $post_id ); ?>
336 <th style="width: 2%"></th>
337 </tr>
338 </thead>
339 <tbody>
340 <?php
341 if ( ! empty( $files ) ) :
342 foreach ( $files as $key => $value ) :
343 $name = isset( $value['name'] ) ? $value['name'] : '';
344 $file = isset( $value['file'] ) ? $value['file'] : '';
345 $condition = isset( $value['condition'] ) ? $value['condition'] : false;
346
347 $args = apply_filters( 'edd_file_row_args', compact( 'name', 'file', 'condition' ), $value );
348 ?>
349 <tr class="edd_repeatable_upload_wrapper">
350 <?php do_action( 'edd_render_file_row', $key, $args, $post_id ); ?>
351 </tr>
352 <?php
353 endforeach;
354 else :
355 ?>
356 <tr class="edd_repeatable_upload_wrapper">
357 <?php do_action( 'edd_render_file_row', 0, array(), $post_id ); ?>
358 </tr>
359 <?php endif; ?>
360 <tr>
361 <td class="submit" colspan="4" style="float: none; clear:both; background: #fff;">
362 <a class="button-secondary edd_add_repeatable" style="margin: 6px 0;"><?php _e( 'Add New File', 'edd' ); ?></a>
363 </td>
364 </tr>
365 </tbody>
366 </table>
367 </div>
368 </div>
369 <?php
370 }
371 add_action( 'edd_meta_box_fields', 'edd_render_files_field', 20 );
372
373 374 375 376 377 378 379 380 381 382 383 384
385 function edd_render_file_row( $key = '', $args = array(), $post_id ) {
386 $defaults = array(
387 'name' => null,
388 'file' => null,
389 'condition' => null
390 );
391
392 $args = wp_parse_args( $args, $defaults );
393 extract( $args, EXTR_SKIP );
394
395 $prices = edd_get_variable_prices( $post_id );
396
397 $variable_pricing = edd_has_variable_prices( $post_id );
398 $variable_display = $variable_pricing ? '' : ' style="display:none;"';
399 ?>
400 <td>
401 <input type="text" class="edd_repeatable_name_field" name="edd_download_files[<?php echo $key; ?>][name]" id="edd_download_files[<?php echo $key; ?>][name]" value="<?php echo $name; ?>" placeholder="<?php _e( 'File Name', 'edd' ); ?>" style="width:100%" />
402 </td>
403
404 <td>
405 <div class="edd_repeatable_upload_field_container">
406 <input type="text" class="edd_repeatable_upload_field edd_upload_field" name="edd_download_files[<?php echo $key; ?>][file]" id="edd_download_files[<?php echo $key; ?>][file]" value="<?php echo $file; ?>" placeholder="<?php _e( 'http://', 'edd' ); ?>" style="width:100%" />
407
408 <span class="edd_upload_file">
409 <a href="#" data-uploader_title="" data-uploader_button_text="<?php _e( 'Insert', 'edd' ); ?>" class="edd_upload_image_button" onclick="return false;"><?php _e( 'Upload a File', 'edd' ); ?></a>
410 </span>
411 </div>
412 </td>
413
414 <td class="pricing"<?php echo $variable_display; ?>>
415 <select class="edd_repeatable_condition_field" name="edd_download_files[<?php echo $key; ?>][condition]" id="edd_download_files[<?php echo $key; ?>][condition]" <?php echo $variable_display; ?>>
416 <option value="all"><?php _e( 'All Prices', 'edd' ); ?></option>
417 <?php if ( $prices ) : foreach ( $prices as $price_key => $price ) : ?>
418 <option value="<?php echo $price_key; ?>" <?php selected( $price_key, $condition ); ?>><?php echo $prices[ $price_key ]['name']; ?></option>
419 <?php endforeach; endif; ?>
420 </select>
421 </td>
422
423 <?php do_action( 'edd_download_file_table_row', $post_id, $key, $args ); ?>
424
425 <td>
426 <a href="#" class="edd_remove_repeatable" data-type="file" style="background: url(<?php echo admin_url('/images/xit.gif'); ?>) no-repeat;">×</a>
427 </td>
428 <?php
429 }
430 add_action( 'edd_render_file_row', 'edd_render_file_row', 10, 3 );
431
432 433 434 435 436 437 438 439 440 441
442 function edd_render_download_limit_row( $post_id ) {
443 global $edd_options;
444 $edd_download_limit = edd_get_file_download_limit( $post_id );
445 ?>
446 <p><strong><?php _e( 'File Download Limit:', 'edd' ); ?></strong></p>
447 <label for="edd_download_limit">
448 <input type="text" name="_edd_download_limit" id="edd_download_limit" value="<?php echo esc_attr( $edd_download_limit ); ?>" size="30" style="width: 80px;" placeholder="0"/>
449 <?php _e( 'The maximum number of times a buyer can download each file. Leave blank or set to 0 for unlimited', 'edd' ); ?>
450 </label>
451 <?php
452 }
453 add_action( 'edd_meta_box_fields', 'edd_render_download_limit_row', 20 );
454
455
456 457 458 459 460 461 462
463 function edd_render_disable_button( $post_id ) {
464 $hide_button = get_post_meta( $post_id, '_edd_hide_purchase_link', true ) ? true : false;
465 ?>
466 <p><strong><?php _e( 'Button Options:', 'edd' ); ?></strong></p>
467 <p>
468 <label for="_edd_hide_purchase_link">
469 <input type="checkbox" name="_edd_hide_purchase_link" id="_edd_hide_purchase_link" value="1" <?php checked( true, $hide_button ); ?> />
470 <?php _e( 'Disable the automatic output of the purchase button', 'edd' ); ?>
471 </label>
472 </p>
473 <?php
474 }
475 add_action( 'edd_meta_box_fields', 'edd_render_disable_button', 30 );
476
477 478 479 480 481 482 483 484 485 486 487
488 function edd_metabox_save_check_blank_rows( $new ) {
489 foreach ( $new as $key => $value ) {
490 if ( empty( $value['name'] ) && empty( $value['amount'] ) && empty( $value['file'] ) )
491 unset( $new[ $key ] );
492 }
493
494 return $new;
495 }
496 add_filter( 'edd_metabox_save_edd_variable_prices', 'edd_metabox_save_check_blank_rows' );
497 add_filter( 'edd_metabox_save_edd_download_files', 'edd_metabox_save_check_blank_rows' );
498
499
500
501
502 503 504 505 506 507 508 509 510 511
512 function edd_render_product_notes_meta_box() {
513 global $post, $edd_options;
514
515 do_action( 'edd_product_notes_meta_box_fields', $post->ID );
516 }
517
518 519 520 521 522 523 524
525 function edd_render_product_notes_field( $post_id ) {
526 global $edd_options;
527
528 $product_notes = edd_get_product_notes( $post_id );
529 ?>
530 <textarea rows="1" cols="40" class="large-texarea" name="edd_product_notes" id="edd_product_notes"><?php echo esc_textarea( $product_notes ); ?></textarea>
531 <p><?php _e( 'Special notes or instructions for this product. These notes will be added to the purchase receipt.', 'edd' ); ?></p>
532 <?php
533 }
534 add_action( 'edd_product_notes_meta_box_fields', 'edd_render_product_notes_field' );
535
536
537
538
539 540 541 542 543 544 545
546 function edd_render_stats_meta_box() {
547 global $post;
548
549 $earnings = edd_get_download_earnings_stats( $post->ID );
550 $sales = edd_get_download_sales_stats( $post->ID );
551
552 echo '<table class="form-table">';
553 echo '<tr>';
554 echo '<th style="width: 20%">' . __( 'Sales:', 'edd' ) . '</th>';
555 echo '<td class="edd_download_stats">';
556 echo $sales . ' – <a href="' . admin_url( '/edit.php?page=edd-reports&view=sales&post_type=download&tab=logs&download=' . $post->ID ) . '">' . __( 'View Sales Log', 'edd' ) . '</a>';
557 echo '</td>';
558 echo '</tr>';
559 echo '<tr>';
560 echo '<th style="width: 20%">' . __( 'Earnings:', 'edd' ) . '</th>';
561 echo '<td class="edd_download_stats">';
562 echo edd_currency_filter( edd_format_amount( $earnings ) );
563 echo '</td>';
564 echo '</tr>';
565 echo '<tr>';
566 echo '<td colspan="2" class="edd_download_stats">';
567 echo '<a href="' . admin_url( '/edit.php?page=edd-reports&view=file_downloads&post_type=download&tab=logs&download=' . $post->ID ) . '">' . __( 'View File Download Log', 'edd' ) . '</a>';
568 echo '</td>';
569 echo '</tr>';
570 do_action('edd_stats_meta_box');
571 echo '</table>';
572 }