Last active
September 5, 2024 01:50
-
-
Save amdrew/8314e3108d1ce799c291 to your computer and use it in GitHub Desktop.
Easy Digital Downloads - Frontend Submissions - Pay To Upload. Forces the vendor to pay for each file uploaded before the product is published
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Easy Digital Downloads - Frontend Submissions - Pay To Upload | |
* Plugin URI: | |
* Description: Forces the vendor to pay for each file uploaded before the product is published | |
* Author: Andrew Munro | |
* Author URI: http://sumobi.com | |
* Version: 1.0 | |
*/ | |
/** | |
* @todo Allow the vendor upload more files when editing the product and pay the additional cost | |
* @todo Provide an input in the EDD settings for entering the amount a vendor should pay per file upload | |
* @todo Provide a live count of how much the vendor needs to pay as each file upload field is populated on the submission form | |
*/ | |
/** | |
* Get the amount to charge for each uploaded file | |
* | |
* @since 1.0 | |
*/ | |
function edd_fes_ptu_get_amount() { | |
$amount = 5; | |
return $amount; | |
} | |
/** | |
* Check if the files for a download have been paid for | |
* | |
* @since 1.0 | |
*/ | |
function edd_fes_ptu_has_paid( $download_id = 0 ) { | |
if ( ! $download_id ) { | |
return; | |
} | |
$has_paid = get_post_meta( $download_id, 'edd_fes_fee_paid', true ); | |
if ( $has_paid ) { | |
return (boolean) true; | |
} | |
return (boolean) false; | |
} | |
/** | |
* Tell the vendor how much each file is going to cost | |
* | |
* @since 1.0 | |
*/ | |
function edd_fes_ptu_show_message( $form_vars, $id, $type, $form_id, $read_only, $args ) { | |
ob_start(); | |
?> | |
<div class="fes-label"> | |
<p>Each uploaded file costs <?php echo edd_currency_filter( edd_format_amount( edd_fes_ptu_get_amount() ) ); ?>. After submitting your product you will be redirected to the checkout for payment.</p> | |
</div> | |
<?php | |
$html = ob_get_clean(); | |
echo $html; | |
} | |
add_action( 'fes_before_file_upload_output', 'edd_fes_ptu_show_message', 10, 6 ); | |
/** | |
* Add fee and redirect vendor for payment | |
* | |
* @since 1.0 | |
*/ | |
function edd_fes_ptu_vendor_submission_redirect( $response, $post_id, $form_id ) { | |
// file uploads for this download have already been paid for | |
if ( edd_fes_ptu_has_paid( $post_id ) ) { | |
return $response; | |
} | |
// get amount per file | |
$amount_per_file = 5; | |
$file_count = array_filter( $_POST['file_upload'] ); | |
$file_count = count( $file_count ); | |
$fee_amount = $file_count * $amount_per_file; | |
$fee_label = 'File Upload Fees'; | |
// add fee | |
EDD()->fees->add_fee( | |
array( | |
'amount' => $fee_amount, | |
'type' => 'item', | |
'id' => 'file_upload_fees', | |
'label' => $fee_label | |
) | |
); | |
// store the download ID in EDD session, we'll need to retrieve it after payment | |
EDD()->session->set( 'edd_fes_download_post_id', $post_id ); | |
// store how many fields they are paying for so we can store in post meta later on | |
EDD()->session->set( 'edd_fes_file_count', $file_count ); | |
// redirect to checkout for payment | |
$response['redirect_to'] = edd_get_checkout_uri(); | |
$response['message'] = 'Success! Redirecting to checkout for payment'; | |
return $response; | |
} | |
add_filter( 'fes_add_post_redirect', 'edd_fes_ptu_vendor_submission_redirect', 10, 3 ); | |
/** | |
* Set download to pending status until they have paid | |
* | |
* @since 1.0 | |
*/ | |
function edd_fes_ptu_submit_product( $postarr, $form_id, $form_settings, $form_vars ) { | |
// set the download to pending until payment has been made | |
$postarr['post_status'] = 'pending'; | |
return $postarr; | |
} | |
add_filter( 'fes_add_post_args', 'edd_fes_ptu_submit_product', 10, 4 ); | |
/** | |
* Vendor has paid, make product live | |
* | |
* @since 1.0 | |
*/ | |
function edd_fes_ptu_vendor_paid( $payment_id ) { | |
if ( ! EDD()->session->get( 'edd_fes_download_post_id' ) ) { | |
return; | |
} | |
$download_id = EDD()->session->get( 'edd_fes_download_post_id' ); | |
// get the download ID and update post to publish | |
$args = array( | |
'ID' => $download_id, | |
'post_status' => 'publish' | |
); | |
wp_update_post( $args ); | |
// mark download as being paid | |
update_post_meta( $download_id, 'edd_fes_fee_paid', true ); | |
// how many were paid for? This determines the maximum number of fields they can ever have | |
update_post_meta( $download_id, 'edd_fes_file_count', EDD()->session->get( 'edd_fes_file_count' ) ); | |
} | |
add_action( 'edd_complete_purchase', 'edd_fes_ptu_vendor_paid' ); | |
/** | |
* Redirect vendor to products page on vendor dashboard | |
* | |
* @since 1.0 | |
*/ | |
function edd_fes_ptu_redirect_to_dashboard() { | |
if ( ! EDD()->session->get( 'edd_fes_download_post_id' ) ) { | |
return; | |
} | |
return add_query_arg( 'task', 'products', get_permalink( EDD_FES()->helper->get_option( 'fes-vendor-dashboard-page', false ) ) ); | |
} | |
add_filter( 'edd_get_success_page_uri', 'edd_fes_ptu_redirect_to_dashboard' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment