/includes/admin/downloads/metabox.php

https://github.com/keha76/Easy-Digital-Downloads · PHP · 745 lines · 453 code · 95 blank · 197 comment · 41 complexity · 177178137d49fd16e4347c259a6d244d MD5 · raw file

  1. <?php
  2. /**
  3. * Metabox Functions
  4. *
  5. * @package EDD
  6. * @subpackage Admin/Downloads
  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. // Exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) ) exit;
  13. /** All Downloads *****************************************************************/
  14. /**
  15. * Register all the meta boxes for the Download custom post type
  16. *
  17. * @since 1.0
  18. * @return void
  19. */
  20. function edd_add_download_meta_box() {
  21. /** Download Configuration */
  22. 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' );
  23. /** Product Notes */
  24. add_meta_box( 'edd_product_notes', __( 'Product Notes', 'edd' ), 'edd_render_product_notes_meta_box', 'download', 'normal', 'default' );
  25. if ( current_user_can( 'view_shop_reports' ) || current_user_can( 'edit_product', get_the_ID() ) ) {
  26. /** Download Stats */
  27. 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' );
  28. }
  29. }
  30. add_action( 'add_meta_boxes', 'edd_add_download_meta_box' );
  31. /**
  32. * Sabe post meta when the save_post action is called
  33. *
  34. * @since 1.0
  35. * @param int $post_id Download (Post) ID
  36. * @global array $post All the data of the the current post
  37. * @return void
  38. */
  39. function edd_download_meta_box_save( $post_id) {
  40. global $post, $edd_options;
  41. if ( ! isset( $_POST['edd_download_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['edd_download_meta_box_nonce'], basename( __FILE__ ) ) )
  42. return $post_id;
  43. if ( ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) return $post_id;
  44. if ( isset( $post->post_type ) && $post->post_type == 'revision' )
  45. return $post_id;
  46. if ( ! current_user_can( 'edit_product', $post_id ) ) {
  47. return $post_id;
  48. }
  49. // The default fields that get saved
  50. $fields = apply_filters( 'edd_metabox_fields_save', array(
  51. '_edd_product_type',
  52. 'edd_price',
  53. '_variable_pricing',
  54. '_edd_price_options_mode',
  55. 'edd_variable_prices',
  56. 'edd_download_files',
  57. '_edd_purchase_text',
  58. '_edd_purchase_style',
  59. '_edd_purchase_color',
  60. '_edd_download_limit',
  61. '_edd_bundled_products',
  62. '_edd_hide_purchase_link',
  63. 'edd_product_notes'
  64. )
  65. );
  66. if ( edd_use_skus() ) {
  67. $fields[] = 'edd_sku';
  68. }
  69. foreach ( $fields as $field ) {
  70. if ( isset( $_POST[ $field ] ) ) {
  71. if ( is_string( $_POST[ $field ] ) ) {
  72. $new = esc_attr( $_POST[ $field ] );
  73. } else {
  74. $new = $_POST[ $field ];
  75. }
  76. $new = apply_filters( 'edd_metabox_save_' . $field, $new );
  77. update_post_meta( $post_id, $field, $new );
  78. } else {
  79. delete_post_meta( $post_id, $field );
  80. }
  81. }
  82. }
  83. add_action( 'save_post', 'edd_download_meta_box_save' );
  84. /**
  85. * Sanitize the price before it is saved
  86. *
  87. * This is mostly for ensuring commas aren't saved in the price
  88. *
  89. * @since 1.3.2
  90. * @param string $price Price before sanitization
  91. * @return float $price Sanitized price
  92. */
  93. function edd_sanitize_price_save( $price ) {
  94. return edd_sanitize_amount( $price );
  95. }
  96. add_filter( 'edd_metabox_save_edd_price', 'edd_sanitize_price_save' );
  97. /**
  98. * Sanitize the variable prices
  99. *
  100. * Ensures prices are correctly mapped to an array starting with an index of 0
  101. *
  102. * @since 1.4.2
  103. * @param array $prices Variable prices
  104. * @return array $prices Array of the remapped variable prices
  105. */
  106. function edd_sanitize_variable_prices_save( $prices ) {
  107. // Make sure all prices are rekeyed starting at 0
  108. return array_values( $prices );
  109. }
  110. add_filter( 'edd_metabox_save_edd_variable_prices', 'edd_sanitize_variable_prices_save' );
  111. /**
  112. * Sanitize bundled products on save
  113. *
  114. * Ensures a user doesn't try and include a product's ID in the products bundled with that product
  115. *
  116. * @access private
  117. * @since 1.6
  118. * @return array
  119. */
  120. function edd_sanitize_bundled_products_save( $products = array() ) {
  121. global $post;
  122. $self = array_search( $post->ID, $products );
  123. if( $self !== false )
  124. unset( $products[ $self ] );
  125. return array_values( array_unique( $products ) );
  126. }
  127. add_filter( 'edd_metabox_save__edd_bundled_products', 'edd_sanitize_bundled_products_save' );
  128. /**
  129. * Sanitize the file downloads
  130. *
  131. * Ensures files are correctly mapped to an array starting with an index of 0
  132. *
  133. * @since 1.5.1
  134. * @param array $files Array of all the file downloads
  135. * @return array $files Array of the remapped file downloads
  136. */
  137. function edd_sanitize_files_save( $files ) {
  138. // Make sure all files are rekeyed starting at 0
  139. return array_values( $files );
  140. }
  141. add_filter( 'edd_metabox_save_edd_download_files', 'edd_sanitize_files_save' );
  142. /** Download Configuration *****************************************************************/
  143. /**
  144. * Download Metabox
  145. *
  146. * Extensions (as well as the core plugin) can add items to the main download
  147. * configuration metabox via the `edd_meta_box_fields` action.
  148. *
  149. * @since 1.0
  150. * @return void
  151. */
  152. function edd_render_download_meta_box() {
  153. global $post, $edd_options;
  154. do_action( 'edd_meta_box_fields', $post->ID );
  155. wp_nonce_field( basename( __FILE__ ), 'edd_download_meta_box_nonce' );
  156. }
  157. /**
  158. * Price Section
  159. *
  160. * If variable pricing is not enabled, simply output a single input box.
  161. *
  162. * If variable pricing is enabled, outputs a table of all current prices.
  163. * Extensions can add column heads to the table via the `edd_download_file_table_head`
  164. * hook, and actual columns via `edd_download_file_table_row`
  165. *
  166. * @since 1.0
  167. * @see edd_render_price_row()
  168. * @return void
  169. */
  170. function edd_render_price_field( $post_id ) {
  171. global $edd_options;
  172. $price = edd_get_download_price( $post_id );
  173. $variable_pricing = edd_has_variable_prices( $post_id );
  174. $prices = edd_get_variable_prices( $post_id );
  175. $single_option_mode = edd_single_price_option_mode( $post_id );
  176. $price_display = $variable_pricing ? ' style="display:none;"' : '';
  177. $variable_display = $variable_pricing ? '' : ' style="display:none;"';
  178. ?>
  179. <p>
  180. <strong><?php echo apply_filters( 'edd_price_options_heading', __( 'Pricing Options:', 'edd' ) ); ?></strong>
  181. </p>
  182. <p>
  183. <label for="edd_variable_pricing">
  184. <input type="checkbox" name="_variable_pricing" id="edd_variable_pricing" value="1" <?php checked( 1, $variable_pricing ); ?> />
  185. <?php echo apply_filters( 'edd_variable_pricing_toggle_text', __( 'Enable variable pricing', 'edd' ) ); ?>
  186. </label>
  187. </p>
  188. <div id="edd_regular_price_field" class="edd_pricing_fields" <?php echo $price_display; ?>>
  189. <?php if ( ! isset( $edd_options['currency_position'] ) || $edd_options['currency_position'] == 'before' ) : ?>
  190. <?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"/>
  191. <?php else : ?>
  192. <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( '' ); ?>
  193. <?php endif; ?>
  194. <?php do_action( 'edd_price_field', $post_id ); ?>
  195. </div>
  196. <?php do_action( 'edd_after_price_field', $post_id ); ?>
  197. <div id="edd_variable_price_fields" class="edd_pricing_fields" <?php echo $variable_display; ?>>
  198. <input type="hidden" id="edd_variable_prices" class="edd_variable_prices_name_field" value=""/>
  199. <p>
  200. <input type="checkbox" name="_edd_price_options_mode" id="edd_price_options_mode"<?php checked( 1, $single_option_mode ); ?> />
  201. <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>
  202. </p>
  203. <div id="edd_price_fields" class="edd_meta_table_wrap">
  204. <table class="widefat edd_repeatable_table" width="100%" cellpadding="0" cellspacing="0">
  205. <thead>
  206. <tr>
  207. <!--drag handle column. Disabled until we can work out a way to solve the issues raised here: https://github.com/easydigitaldownloads/Easy-Digital-Downloads/issues/1066
  208. <th style="width: 20px"></th>
  209. -->
  210. <th><?php _e( 'Option Name', 'edd' ); ?></th>
  211. <th style="width: 90px"><?php _e( 'Price', 'edd' ); ?></th>
  212. <?php do_action( 'edd_download_price_table_head', $post_id ); ?>
  213. <th style="width: 2%"></th>
  214. </tr>
  215. </thead>
  216. <tbody>
  217. <?php
  218. if ( ! empty( $prices ) ) :
  219. foreach ( $prices as $key => $value ) :
  220. $name = isset( $value['name'] ) ? $value['name'] : '';
  221. $amount = isset( $value['amount'] ) ? $value['amount'] : '';
  222. $args = apply_filters( 'edd_price_row_args', compact( 'name', 'amount' ), $value );
  223. ?>
  224. <tr class="edd_variable_prices_wrapper edd_repeatable_row">
  225. <?php do_action( 'edd_render_price_row', $key, $args, $post_id ); ?>
  226. </tr>
  227. <?php
  228. endforeach;
  229. else :
  230. ?>
  231. <tr class="edd_variable_prices_wrapper edd_repeatable_row">
  232. <?php do_action( 'edd_render_price_row', 0, array(), $post_id ); ?>
  233. </tr>
  234. <?php endif; ?>
  235. <tr>
  236. <td class="submit" colspan="4" style="float: none; clear:both; background:#fff;">
  237. <a class="button-secondary edd_add_repeatable" style="margin: 6px 0;"><?php _e( 'Add New Price', 'edd' ); ?></a>
  238. </td>
  239. </tr>
  240. </tbody>
  241. </table>
  242. </div>
  243. </div><!--end #edd_variable_price_fields-->
  244. <?php
  245. }
  246. add_action( 'edd_meta_box_fields', 'edd_render_price_field', 10 );
  247. /**
  248. * Individual Price Row
  249. *
  250. * Used to output a table row for each price associated with a download.
  251. * Can be called directly, or attached to an action.
  252. *
  253. * @since 1.2.2
  254. * @return void
  255. */
  256. function edd_render_price_row( $key, $args = array(), $post_id ) {
  257. global $edd_options;
  258. $defaults = array(
  259. 'name' => null,
  260. 'amount' => null
  261. );
  262. $args = wp_parse_args( $args, $defaults );
  263. extract( $args, EXTR_SKIP );
  264. ?>
  265. <!--
  266. Disabled until we can work out a way to solve the issues raised here: https://github.com/easydigitaldownloads/Easy-Digital-Downloads/issues/1066
  267. <td>
  268. <span class="edd_draghandle"></span>
  269. </td>
  270. -->
  271. <td>
  272. <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%" />
  273. </td>
  274. <td>
  275. <?php if( ! isset( $edd_options['currency_position'] ) || $edd_options['currency_position'] == 'before' ) : ?>
  276. <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;" />
  277. <?php else : ?>
  278. <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( '' ); ?>
  279. <?php endif; ?>
  280. </td>
  281. <?php do_action( 'edd_download_price_table_row', $post_id, $key, $args ); ?>
  282. <td>
  283. <a href="#" class="edd_remove_repeatable" data-type="price" style="background: url(<?php echo admin_url('/images/xit.gif'); ?>) no-repeat;">&times;</a>
  284. </td>
  285. <?php
  286. }
  287. add_action( 'edd_render_price_row', 'edd_render_price_row', 10, 3 );
  288. function edd_render_product_type_field( $post_id = 0 ) {
  289. $type = edd_get_download_type( $post_id );
  290. ?>
  291. <p>
  292. <strong><?php apply_filters( 'edd_product_type_options_heading', _e( 'Product Type Options:', 'edd' ) ); ?></strong>
  293. </p>
  294. <p>
  295. <select name="_edd_product_type" id="edd_product_type">
  296. <option value="0"><?php _e( 'Default', 'edd' ); ?></option>
  297. <option value="bundle"<?php selected( 'bundle', $type ); ?>><?php _e( 'Bundle', 'edd' ); ?></option>
  298. </select>
  299. <label for="edd_product_type"><?php _e( 'Select a product type', 'edd' ); ?></label>
  300. </p>
  301. <?php
  302. }
  303. add_action( 'edd_meta_box_fields', 'edd_render_product_type_field', 10 );
  304. /**
  305. *
  306. * @access private
  307. * @since 1.6
  308. * @return void
  309. */
  310. function edd_render_products_field( $post_id ) {
  311. $type = edd_get_download_type( $post_id );
  312. $display = $type == 'bundle' ? '' : ' style="display:none;"';
  313. $products = edd_get_bundled_products( $post_id );
  314. ?>
  315. <div id="edd_products"<?php echo $display; ?>>
  316. <p>
  317. <strong><?php printf( __( 'Bundled %s:', 'edd' ), edd_get_label_plural() ); ?></strong>
  318. </p>
  319. <div id="edd_file_fields" class="edd_meta_table_wrap">
  320. <table class="widefat" width="100%" cellpadding="0" cellspacing="0">
  321. <thead>
  322. <tr>
  323. <th style="width: 20%"><?php printf( __( 'Select the %s to bundle with this %s', 'edd' ), edd_get_label_plural(), edd_get_label_singular() ); ?></th>
  324. <?php do_action( 'edd_download_products_table_head', $post_id ); ?>
  325. <th style="width: 2%"></th>
  326. </tr>
  327. </thead>
  328. <tbody>
  329. <?php
  330. if ( ! empty( $products ) ) :
  331. foreach ( $products as $product ) :
  332. ?>
  333. <tr class="edd_repeatable_product_wrapper">
  334. <?php do_action( 'edd_render_product_row', $product, $post_id ); ?>
  335. </tr>
  336. <?php
  337. endforeach;
  338. else :
  339. ?>
  340. <tr class="edd_repeatable_product_wrapper">
  341. <?php do_action( 'edd_render_product_row', 0, $post_id ); ?>
  342. </tr>
  343. <?php endif; ?>
  344. <tr>
  345. <td class="submit" colspan="4" style="float: none; clear:both; background: #fff;">
  346. <a class="button-secondary edd_add_repeatable" style="margin: 6px 0;"><?php _e( 'Add New', 'edd' ); ?></a>
  347. </td>
  348. </tr>
  349. </tbody>
  350. </table>
  351. </div>
  352. </div>
  353. <?php
  354. }
  355. add_action( 'edd_meta_box_fields', 'edd_render_products_field', 10 );
  356. /**
  357. * TODO Update doc
  358. *
  359. * @access private
  360. * @since 1.6
  361. * @return void
  362. */
  363. function edd_render_product_row( $product_id = 0, $post_id ) {
  364. ?>
  365. <td>
  366. <?php echo EDD()->html->product_dropdown( '_edd_bundled_products[]', $product_id ); ?>
  367. </td>
  368. <?php do_action( 'edd_product_table_row', $product_id, $post_id ); ?>
  369. <td>
  370. <a href="#" class="edd_remove_repeatable" data-type="file" style="background: url(<?php echo admin_url('/images/xit.gif'); ?>) no-repeat;">&times;</a>
  371. </td>
  372. <?php
  373. }
  374. add_action( 'edd_render_product_row', 'edd_render_product_row', 10, 2 );
  375. /**
  376. * File Downloads section.
  377. *
  378. * Outputs a table of all current files. Extensions can add column heads to the table
  379. * via the `edd_download_file_table_head` hook, and actual columns via
  380. * `edd_download_file_table_row`
  381. *
  382. * @since 1.0
  383. * @see edd_render_file_row()
  384. * @param int $post_id Download (Post) ID
  385. * @return void
  386. */
  387. function edd_render_files_field( $post_id = 0 ) {
  388. $type = edd_get_download_type( $post_id );
  389. $files = edd_get_download_files( $post_id );
  390. $variable_pricing = edd_has_variable_prices( $post_id );
  391. $display = $type == 'bundle' ? ' style="display:none;"' : '';
  392. $variable_display = $variable_pricing ? '' : 'display:none;';
  393. ?>
  394. <div id="edd_download_files"<?php echo $display; ?>>
  395. <p>
  396. <strong><?php _e( 'File Downloads:', 'edd' ); ?></strong>
  397. </p>
  398. <input type="hidden" id="edd_download_files" class="edd_repeatable_upload_name_field" value=""/>
  399. <div id="edd_file_fields" class="edd_meta_table_wrap">
  400. <table class="widefat edd_repeatable_table" width="100%" cellpadding="0" cellspacing="0">
  401. <thead>
  402. <tr>
  403. <!--drag handle column. Disabled until we can work out a way to solve the issues raised here: https://github.com/easydigitaldownloads/Easy-Digital-Downloads/issues/1066
  404. <th style="width: 20px"></th>
  405. -->
  406. <th style="width: 20%"><?php _e( 'File Name', 'edd' ); ?></th>
  407. <th><?php _e( 'File URL', 'edd' ); ?></th>
  408. <th class="pricing" style="width: 20%; <?php echo $variable_display; ?>"><?php _e( 'Price Assignment', 'edd' ); ?></th>
  409. <?php do_action( 'edd_download_file_table_head', $post_id ); ?>
  410. <th style="width: 2%"></th>
  411. </tr>
  412. </thead>
  413. <tbody>
  414. <?php
  415. if ( ! empty( $files ) ) :
  416. foreach ( $files as $key => $value ) :
  417. $name = isset( $value['name'] ) ? $value['name'] : '';
  418. $file = isset( $value['file'] ) ? $value['file'] : '';
  419. $condition = isset( $value['condition'] ) ? $value['condition'] : false;
  420. $args = apply_filters( 'edd_file_row_args', compact( 'name', 'file', 'condition' ), $value );
  421. ?>
  422. <tr class="edd_repeatable_upload_wrapper edd_repeatable_row">
  423. <?php do_action( 'edd_render_file_row', $key, $args, $post_id ); ?>
  424. </tr>
  425. <?php
  426. endforeach;
  427. else :
  428. ?>
  429. <tr class="edd_repeatable_upload_wrapper edd_repeatable_row">
  430. <?php do_action( 'edd_render_file_row', 0, array(), $post_id ); ?>
  431. </tr>
  432. <?php endif; ?>
  433. <tr>
  434. <td class="submit" colspan="4" style="float: none; clear:both; background: #fff;">
  435. <a class="button-secondary edd_add_repeatable" style="margin: 6px 0;"><?php _e( 'Add New File', 'edd' ); ?></a>
  436. </td>
  437. </tr>
  438. </tbody>
  439. </table>
  440. </div>
  441. </div>
  442. <?php
  443. }
  444. add_action( 'edd_meta_box_fields', 'edd_render_files_field', 20 );
  445. /**
  446. * Individual file row.
  447. *
  448. * Used to output a table row for each file associated with a download.
  449. * Can be called directly, or attached to an action.
  450. *
  451. * @since 1.2.2
  452. * @param string $key Array key
  453. * @param array $args Array of all the arguments passed to the function
  454. * @param int $post_id Download (Post) ID
  455. * @return void
  456. */
  457. function edd_render_file_row( $key = '', $args = array(), $post_id ) {
  458. $defaults = array(
  459. 'name' => null,
  460. 'file' => null,
  461. 'condition' => null
  462. );
  463. $args = wp_parse_args( $args, $defaults );
  464. extract( $args, EXTR_SKIP );
  465. $prices = edd_get_variable_prices( $post_id );
  466. $variable_pricing = edd_has_variable_prices( $post_id );
  467. $variable_display = $variable_pricing ? '' : ' style="display:none;"';
  468. ?>
  469. <!--
  470. Disabled until we can work out a way to solve the issues raised here: https://github.com/easydigitaldownloads/Easy-Digital-Downloads/issues/1066
  471. <td>
  472. <span class="edd_draghandle"></span>
  473. </td>
  474. -->
  475. <td>
  476. <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%" />
  477. </td>
  478. <td>
  479. <div class="edd_repeatable_upload_field_container">
  480. <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%" />
  481. <span class="edd_upload_file">
  482. <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>
  483. </span>
  484. </div>
  485. </td>
  486. <td class="pricing"<?php echo $variable_display; ?>>
  487. <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; ?>>
  488. <option value="all"><?php _e( 'All Prices', 'edd' ); ?></option>
  489. <?php if ( $prices ) : foreach ( $prices as $price_key => $price ) : ?>
  490. <option value="<?php echo $price_key; ?>" <?php selected( $price_key, $condition ); ?>><?php echo $prices[ $price_key ]['name']; ?></option>
  491. <?php endforeach; endif; ?>
  492. </select>
  493. </td>
  494. <?php do_action( 'edd_download_file_table_row', $post_id, $key, $args ); ?>
  495. <td>
  496. <a href="#" class="edd_remove_repeatable" data-type="file" style="background: url(<?php echo admin_url('/images/xit.gif'); ?>) no-repeat;">&times;</a>
  497. </td>
  498. <?php
  499. }
  500. add_action( 'edd_render_file_row', 'edd_render_file_row', 10, 3 );
  501. /**
  502. * File Download Limit Row
  503. *
  504. * The file download limit is the maximum number of times each file
  505. * can be downloaded by the buyer
  506. *
  507. * @since 1.3.1
  508. * @param int $post_id Download (Post) ID
  509. * @return void
  510. */
  511. function edd_render_download_limit_row( $post_id ) {
  512. global $edd_options;
  513. $edd_download_limit = edd_get_file_download_limit( $post_id );
  514. ?>
  515. <p><strong><?php _e( 'File Download Limit:', 'edd' ); ?></strong></p>
  516. <label for="edd_download_limit">
  517. <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"/>
  518. <?php _e( 'The maximum number of times a buyer can download each file. Leave blank or set to 0 for unlimited', 'edd' ); ?>
  519. </label>
  520. <?php
  521. }
  522. add_action( 'edd_meta_box_fields', 'edd_render_download_limit_row', 20 );
  523. /**
  524. * Render Accounting Options
  525. *
  526. * @since 1.6
  527. * @param int $post_id Download (Post) ID
  528. * @return void
  529. */
  530. function edd_render_accounting_options( $post_id ) {
  531. global $edd_options;
  532. if( ! edd_use_skus() ) {
  533. return;
  534. }
  535. $edd_sku = get_post_meta( $post_id, 'edd_sku', true );
  536. ?>
  537. <p><strong><?php _e( 'Accounting Options:', 'edd' ); ?></strong></p>
  538. <p>
  539. <label for="edd_sku">
  540. <input type="text" name="edd_sku" id="edd_sku" value="<?php echo esc_attr( $edd_sku ); ?>" size="30" style="width: 80px;"/>
  541. <?php echo sprintf( __( 'Enter an SKU for this %s.', 'edd' ), strtolower( edd_get_label_singular() ) ); ?>
  542. </label>
  543. </p>
  544. <?php
  545. }
  546. add_action( 'edd_meta_box_fields', 'edd_render_accounting_options', 25 );
  547. /**
  548. * Render Disable Button
  549. *
  550. * @since 1.0
  551. * @param int $post_id Download (Post) ID
  552. * @return void
  553. */
  554. function edd_render_disable_button( $post_id ) {
  555. $hide_button = get_post_meta( $post_id, '_edd_hide_purchase_link', true ) ? true : false;
  556. ?>
  557. <p><strong><?php _e( 'Button Options:', 'edd' ); ?></strong></p>
  558. <p>
  559. <label for="_edd_hide_purchase_link">
  560. <input type="checkbox" name="_edd_hide_purchase_link" id="_edd_hide_purchase_link" value="1" <?php checked( true, $hide_button ); ?> />
  561. <?php _e( 'Disable the automatic output of the purchase button', 'edd' ); ?>
  562. </label>
  563. </p>
  564. <?php
  565. }
  566. add_action( 'edd_meta_box_fields', 'edd_render_disable_button', 30 );
  567. /**
  568. * Don't save blank rows.
  569. *
  570. * When saving, check the price and file table for blank rows.
  571. * If the name of the price or file is empty, that row should not
  572. * be saved.
  573. *
  574. * @since 1.2.2
  575. * @param array $new Array of all the meta values
  576. * @return array $new New meta value with empty keys removed
  577. */
  578. function edd_metabox_save_check_blank_rows( $new ) {
  579. foreach ( $new as $key => $value ) {
  580. if ( empty( $value['name'] ) && empty( $value['amount'] ) && empty( $value['file'] ) )
  581. unset( $new[ $key ] );
  582. }
  583. return $new;
  584. }
  585. add_filter( 'edd_metabox_save_edd_variable_prices', 'edd_metabox_save_check_blank_rows' );
  586. add_filter( 'edd_metabox_save_edd_download_files', 'edd_metabox_save_check_blank_rows' );
  587. /** Product Notes *****************************************************************/
  588. /**
  589. * Product Notes Meta Box
  590. *
  591. * Renders the Product Notes meta box
  592. *
  593. * @since 1.2.1
  594. * @global array $post Contains all the download data
  595. * @global array $edd_options Contains all the options set for EDD
  596. * @return void
  597. */
  598. function edd_render_product_notes_meta_box() {
  599. global $post, $edd_options;
  600. do_action( 'edd_product_notes_meta_box_fields', $post->ID );
  601. }
  602. /**
  603. * Render Product Notes Field
  604. *
  605. * @since 1.2.1
  606. * @param int $post_id Download (Post) ID
  607. * @return void
  608. */
  609. function edd_render_product_notes_field( $post_id ) {
  610. global $edd_options;
  611. $product_notes = edd_get_product_notes( $post_id );
  612. ?>
  613. <textarea rows="1" cols="40" class="large-texarea" name="edd_product_notes" id="edd_product_notes"><?php echo esc_textarea( $product_notes ); ?></textarea>
  614. <p><?php _e( 'Special notes or instructions for this product. These notes will be added to the purchase receipt.', 'edd' ); ?></p>
  615. <?php
  616. }
  617. add_action( 'edd_product_notes_meta_box_fields', 'edd_render_product_notes_field' );
  618. /** Stats *****************************************************************/
  619. /**
  620. * Render Stats Meta Box
  621. *
  622. * @since 1.0
  623. * @global array $post Contains all the download data
  624. * @return void
  625. */
  626. function edd_render_stats_meta_box() {
  627. global $post;
  628. $earnings = edd_get_download_earnings_stats( $post->ID );
  629. $sales = edd_get_download_sales_stats( $post->ID );
  630. echo '<table class="form-table">';
  631. echo '<tr>';
  632. echo '<th style="width: 20%">' . __( 'Sales:', 'edd' ) . '</th>';
  633. echo '<td class="edd_download_stats">';
  634. echo $sales . '&nbsp;&ndash;&nbsp;<a href="' . admin_url( '/edit.php?page=edd-reports&view=sales&post_type=download&tab=logs&download=' . $post->ID ) . '">' . __( 'View Sales Log', 'edd' ) . '</a>';
  635. echo '</td>';
  636. echo '</tr>';
  637. echo '<tr>';
  638. echo '<th style="width: 30%">' . __( 'Earnings:', 'edd' ) . '</th>';
  639. echo '<td class="edd_download_stats">';
  640. echo edd_currency_filter( edd_format_amount( $earnings ) );
  641. echo '</td>';
  642. echo '</tr>';
  643. echo '<tr>';
  644. echo '<td colspan="2" class="edd_download_stats">';
  645. 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>';
  646. echo '</td>';
  647. echo '</tr>';
  648. do_action('edd_stats_meta_box');
  649. echo '</table>';
  650. }