Advertisement
Guest User

EDD Man 3

a guest
Jun 26th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 31.12 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Easy Digital Downloads - Manual Purchases
  4. Plugin URI: http://easydigitaldownloads.com/extension/manual-purchases/
  5. Description: Provides an admin interface for manually creating purchase orders in Easy Digital Downloads
  6. Version: 1.8.2
  7. Author: Pippin Williamson
  8. Author URI:  http://pippinsplugins.com
  9. Contributors: mordauk
  10. */
  11.  
  12. class EDD_Manual_Purchases {
  13.  
  14.         private static $instance;
  15.  
  16.         /**
  17.          * Get active object instance
  18.          *
  19.          * @since 1.0
  20.          *
  21.          * @access public
  22.          * @static
  23.          * @return object
  24.          */
  25.         public static function get_instance() {
  26.  
  27.                 if ( ! self::$instance )
  28.                         self::$instance = new EDD_Manual_Purchases();
  29.  
  30.                 return self::$instance;
  31.         }
  32.  
  33.         /**
  34.          * Class constructor.  Includes constants, includes and init method.
  35.          *
  36.          * @since 1.0
  37.          *
  38.          * @access public
  39.          * @return void
  40.          */
  41.         public function __construct() {
  42.  
  43.                 define( 'EDD_MP_STORE_API_URL', 'http://easydigitaldownloads.com' );
  44.                 define( 'EDD_MP_PRODUCT_NAME', 'Manual Purchases' );
  45.                 define( 'EDD_MP_VERSION', '1.8.2' );
  46.                 $this->init();
  47.  
  48.         }
  49.  
  50.  
  51.         /**
  52.          * Run action and filter hooks.
  53.          *
  54.          * @since 1.0
  55.          *
  56.          * @access private
  57.          * @return void
  58.          */
  59.         private function init() {
  60.  
  61.                 if( ! function_exists( 'edd_price' ) )
  62.                         return; // EDD not present
  63.  
  64.                 global $edd_options;
  65.  
  66.                 // internationalization
  67.                 add_action( 'init', array( $this, 'textdomain' ) );
  68.  
  69.                 // add a crreate payment button to the top of the Payments History page
  70.                 add_action( 'edd_payments_page_top' , array( $this, 'create_payment_button' ) );
  71.  
  72.                 // register the Create Payment submenu
  73.                 add_action( 'admin_menu', array( $this, 'submenu' ) );
  74.  
  75.                 // load scripts
  76.                 add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) );
  77.  
  78.                 // check for download price variations via ajax
  79.                 add_action( 'wp_ajax_edd_mp_check_for_variations', array( $this, 'check_for_variations' ) );
  80.  
  81.                 // process payment creation
  82.                 add_action( 'edd_create_payment', array( $this, 'create_payment' ) );
  83.  
  84.                 // show payment created notice
  85.                 add_action( 'admin_notices', array( $this, 'payment_created_notice' ), 1 );
  86.  
  87.                 // auto updater
  88.                 if( class_exists( 'EDD_License' ) ) {
  89.                         $eddc_license = new EDD_License( __FILE__, EDD_MP_PRODUCT_NAME, EDD_MP_VERSION, 'Pippin Williamson' );
  90.                 }
  91.  
  92.         }
  93.  
  94.         public static function textdomain() {
  95.  
  96.                 // Set filter for plugin's languages directory
  97.                 $lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
  98.                 $lang_dir = apply_filters( 'edd_manual_purchases_lang_directory', $lang_dir );
  99.  
  100.                 // Load the translations
  101.                 load_plugin_textdomain( 'edd-manual-purchases', false, $lang_dir );
  102.  
  103.         }
  104.  
  105.         public static function create_payment_button() {
  106.  
  107.                 ?>
  108.                 <p id="edd_create_payment_go">
  109.                         <a href="<?php echo add_query_arg( 'page', 'edd-manual-purchase', admin_url( 'options.php' ) ); ?>" class="button-secondary"><?php _e('Create Payment', 'edd-manual-purchases'); ?></a>
  110.                 </p>
  111.                 <?php
  112.         }
  113.  
  114.         public static function submenu() {
  115.                 global $edd_create_payment_page;
  116.                 $edd_create_payment_page = add_submenu_page( 'options.php', __('Create Payment', 'edd-manual-purchases'), __('Create Payment', 'edd-manual-purchases'), 'edit_shop_payments', 'edd-manual-purchase', array( __CLASS__, 'payment_creation_form' ) );
  117.         }
  118.  
  119.         public static function load_scripts( $hook ) {
  120.  
  121.                 if( 'admin_page_edd-manual-purchase' != $hook )
  122.                         return;
  123.  
  124.                 // Use minified libraries if SCRIPT_DEBUG is turned off
  125.                 $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
  126.                 wp_enqueue_script( 'jquery-ui-datepicker' );
  127.                 $ui_style = ( 'classic' == get_user_option( 'admin_color' ) ) ? 'classic' : 'fresh';
  128.                 wp_enqueue_style( 'jquery-ui-css', EDD_PLUGIN_URL . 'assets/css/jquery-ui-' . $ui_style . $suffix . '.css' );
  129.         }
  130.  
  131.         public static function payment_creation_form() {
  132.                 ?>
  133.                 <div class="wrap">
  134.                         <h2><?php _e('Create New Payment', 'edd-manual-purchases'); ?></h2>
  135.                         <script type="text/javascript">
  136.                                 jQuery(document).ready(function($) {
  137.                                         // clone a download row
  138.                                         $('#edd_mp_create_payment').on('click', '.edd-mp-add-download', function() {
  139.                                                 var row = $(this).closest('tr').clone();
  140.  
  141.                                                 var count = $('tr.edd-mp-download-wrap').size();
  142.  
  143.                                                 $('select.edd-mp-download-select', row).prop('name', 'downloads[' + count + '][id]');
  144.  
  145.                                                 $('select.edd-mp-price-select', row).remove();
  146.  
  147.                                                 if( ! $('.edd-mp-remove', row).length )
  148.                                                         $('.edd-mp-downloads', row).append('<a href="#" class="edd-mp-remove">Remove</a>');
  149.  
  150.                                                 row.insertAfter( '#edd-mp-table-body tr.edd-mp-download-wrap:last' );
  151.                                                 return false;
  152.                                         });
  153.                                         // remove a download row
  154.                                         $('#edd_mp_create_payment').on('click', '.edd-mp-remove', function() {
  155.                                                 $(this).closest('tr').remove();
  156.                                                 return false;
  157.                                         });
  158.                                         // check for variable prices
  159.                                         $('#edd_mp_create_payment').on('change', '.edd-mp-download-select', function() {
  160.                                                 var $this = $(this);
  161.                                                 var selected_download = $('option:selected', this).val();
  162.                                                 if( parseInt( selected_download ) != 0) {
  163.                                                         var edd_mp_nonce = $('#edd_create_payment_nonce').val();
  164.                                                         var data = {
  165.                                                                 action: 'edd_mp_check_for_variations',
  166.                                                                 download_id: selected_download,
  167.                                                                 key: $('tr.edd-mp-download-wrap').size() - 1,
  168.                                                                 nonce: edd_mp_nonce
  169.                                                         }
  170.                                                         $this.parent().find('img').show();
  171.                                                         $.post(ajaxurl, data, function(response) {
  172.                                                                 $this.next('select').remove();
  173.                                                                 $this.after( response );
  174.                                                                 $this.parent().find('img').hide();
  175.                                                         });
  176.                                                 } else {
  177.                                                         $this.next('select').remove();
  178.                                                         $this.parent().find('img').hide();
  179.                                                 }
  180.                                         });
  181.                                         if ($('.form-table .edd_datepicker').length > 0) {
  182.                                                 var dateFormat = 'mm/dd/yy';
  183.                                                 $('.edd_datepicker').datepicker({
  184.                                                         dateFormat: dateFormat
  185.                                                 });
  186.                                         }
  187.                                 });
  188.                         </script>
  189.                         <form id="edd_mp_create_payment" method="post">
  190.                                 <table class="form-table">
  191.                                         <tbody id="edd-mp-table-body">
  192.                                                 <tr class="form-field edd-mp-download-wrap">
  193.                                                         <th scope="row" valign="top">
  194.                                                                 <label><?php echo edd_get_label_singular(); ?></label>
  195.                                                         </th>
  196.                                                         <td class="edd-mp-downloads">
  197.                                                                 <select name="downloads[0][id]" class="edd-mp-download-select">
  198.                                                                         <?php
  199.                                                                         $args = array(
  200.                                                                                 'post_type' => 'download',
  201.                                                                                 'nopaging'  => true,
  202.                                                                                 'orderby'   => 'title',
  203.                                                                                 'order'     => 'ASC',
  204.                                                                                 'post_status' => 'any'
  205.                                                                         );
  206.                                                                         $downloads = get_posts( apply_filters( 'edd_mp_downloads_query', $args ) );
  207.                                                                         if( $downloads ) {
  208.                                                                                 echo '<option value="0">' . sprintf( __('Choose %s', 'edd-manual-purchases'), esc_html( edd_get_label_plural() ) ) . '</option>';
  209.                                                                                 foreach( $downloads as $download ) {
  210.                                                                                         if( $download->post_status != 'publish' )
  211.                                                                                                 $prefix = strtoupper( $download->post_status ) . ' - ';
  212.                                                                                         else
  213.                                                                                                 $prefix = '';
  214.                                                                                         echo '<option value="' . $download->ID . '">' . $prefix . esc_html( get_the_title( $download->ID ) ) . '</option>';
  215.                                                                                 }
  216.                                                                         } else {
  217.                                                                                 echo '<option value="0">' . sprintf( __('No %s created yet', 'edd-manual-purchases'), edd_get_label_plural() ) . '</option>';
  218.                                                                         }
  219.                                                                         ?>
  220.                                                                 </select>
  221.                                                                 <a href="#" class="edd-mp-add-download"><?php _e('Add another', 'edd-manual-purchases' ); ?></a>
  222.                                                                 <img src="<?php echo admin_url('/images/wpspin_light.gif'); ?>" class="waiting edd_mp_loading" style="display:none;"/>
  223.                                                         </td>
  224.                                                 </tr>
  225.                                                
  226.                                                 <tr class="form-field">
  227.                                                         <th scope="row" valign="top">
  228.                                                                 <label for="edd-invoice"><?php _e('Invoice ID', 'edd-manual-purchases'); ?></label>
  229.                                                         </th>
  230.                                                         <td class="edd-mp-user">
  231.                                                                 <input type="text" class="small-text" id="edd-invoice" name="invoice" style="width: 180px;"/>
  232.                                                                 <div class="description"><?php _e('Enter the ID of the invoice.', 'edd-manual-purchases'); ?></div>
  233.                                                         </td>
  234.                                                 </tr>
  235.                                                
  236.                                                 <tr class="form-field">
  237.                                                         <th scope="row" valign="top">
  238.                                                                 <label for="edd-mp-user"><?php _e('Buyer ID or Email', 'edd-manual-purchases'); ?></label>
  239.                                                         </th>
  240.                                                         <td class="edd-mp-user">
  241.                                                                 <input type="text" class="small-text" id="edd-mp-user" name="user" style="width: 180px;"/>
  242.                                                                 <div class="description"><?php _e('Enter the user ID or email of the buyer.', 'edd-manual-purchases'); ?></div>
  243.                                                         </td>
  244.                                                 </tr>
  245.                                                 <tr class="form-field">
  246.                                                         <th scope="row" valign="top">
  247.                                                                 <label for="edd-mp-last"><?php _e('Buyer First Name', 'edd-manual-purchases'); ?></label>
  248.                                                         </th>
  249.                                                         <td class="edd-mp-last">
  250.                                                                 <input type="text" class="small-text" id="edd-mp-last" name="first" style="width: 180px;"/>
  251.                                                                 <div class="description"><?php _e('Enter the first name of the buyer (optional).', 'edd-manual-purchases'); ?></div>
  252.                                                         </td>
  253.                                                 </tr>
  254.                                                 <tr class="form-field">
  255.                                                         <th scope="row" valign="top">
  256.                                                                 <label for="edd-mp-last"><?php _e('Buyer Last Name', 'edd-manual-purchases'); ?></label>
  257.                                                         </th>
  258.                                                         <td class="edd-mp-last">
  259.                                                                 <input type="text" class="small-text" id="edd-mp-last" name="last" style="width: 180px;"/>
  260.                                                                 <div class="description"><?php _e('Enter the last name of the buyer (optional).', 'edd-manual-purchases'); ?></div>
  261.                                                         </td>
  262.                                                 </tr>
  263.                                                 <tr class="form-field">
  264.                                                         <th scope="row" valign="top">
  265.                                                                 <label for="edd-mp-amount"><?php _e('Amount', 'edd-manual-purchases'); ?></label>
  266.                                                         </th>
  267.                                                         <td class="edd-mp-downloads">
  268.                                                                 <input type="text" class="small-text" id="edd-mp-amount" name="amount" style="width: 180px;"/>
  269.                                                                 <div class="description"><?php _e('Enter the total purchase amount, or leave blank to auto calculate price based on the selected items above. Use 0.00 for 0.', 'edd-manual-purchases'); ?></div>
  270.                                                         </td>
  271.                                                 </tr>
  272.                                                 <tr class="form-field">
  273.                                                         <th scope="row" valign="top">
  274.                                                                 <?php _e('Payment status', 'edd-manual-purchases'); ?>
  275.                                                         </th>
  276.                                                         <td class="edd-mp-status">
  277.                                                                 <?php echo EDD()->html->select( array( 'name' => 'status', 'options' => edd_get_payment_statuses(), 'selected' => 'publish', 'show_option_all' => false, 'show_option_none' => false ) ); ?>
  278.                                                                 <label for="edd-mp-status" class="description"><?php _e('Select the status of this payment.', 'edd-manual-purchases'); ?></label>
  279.                                                         </td>
  280.                                                 </tr>
  281.                                                 <tr class="form-field">
  282.                                                         <th scope="row" valign="top">
  283.                                                                 <?php _e('Send Receipt', 'edd-manual-purchases'); ?>
  284.                                                         </th>
  285.                                                         <td class="edd-mp-receipt">
  286.                                                                 <label for="edd-mp-receipt">
  287.                                                                         <input type="checkbox" id="edd-mp-receipt" name="receipt" style="width: auto;" checked="1" value="1"/>
  288.                                                                         <?php _e('Send the purchase receipt to the buyer?', 'edd-manual-purchases'); ?>
  289.                                                                 </label>
  290.                                                         </td>
  291.                                                 </tr>
  292.                                                 <tr class="form-field">
  293.                                                         <th scope="row" valign="top">
  294.                                                                 <label for="edd-mp-date"><?php _e('Date', 'edd-manual-purchases'); ?></label>
  295.                                                         </th>
  296.                                                         <td class="edd-mp-downloads">
  297.                                                                 <input type="text" class="small-text edd_datepicker" id="edd-mp-date" name="date" style="width: 180px;"/>
  298.                                                                 <div class="description"><?php _e('Enter the purchase date. Leave blank for today\'s date.', 'edd-manual-purchases'); ?></div>
  299.                                                         </td>
  300.                                                 </tr>
  301.                                                 <?php if( function_exists( 'eddc_record_commission' ) ) : ?>
  302.                                                 <tr class="form-field">
  303.                                                         <th scope="row" valign="top">
  304.                                                                 <?php _e('Commission', 'edd-manual-purchases'); ?>
  305.                                                         </th>
  306.                                                         <td class="edd-mp-downloads">
  307.                                                                 <label for="edd-mp-commission">
  308.                                                                         <input type="checkbox" id="edd-mp-commission" name="commission" style="width: auto;"/>
  309.                                                                         <?php _e('Record commissions (if any) for this manual purchase?', 'edd-manual-purchases'); ?>
  310.                                                                 </label>
  311.                                                         </td>
  312.                                                 </tr>
  313.                                                 <?php endif; ?>
  314.                                                 <?php if( class_exists( 'EDD_Recurring' ) ) : ?>
  315.                                                 <tr class="form-field">
  316.                                                         <th scope="row" valign="top">
  317.                                                                 <label for="edd-mp-date"><?php _e('Customer Expiration', 'edd-manual-purchases'); ?></label>
  318.                                                         </th>
  319.                                                         <td class="edd-mp-downloads">
  320.                                                                 <input type="text" class="small-text edd_datepicker" id="edd-mp-expiration" name="expiration" style="width: 180px;"/>
  321.                                                                 <div class="description"><?php _e('Set the customer\'s status to Active and set their expiration date. Leave blank to leave customer as is.', 'edd-manual-purchases'); ?></div>
  322.                                                         </td>
  323.                                                 </tr>
  324.                                                 <?php endif; ?>
  325.                                         </tbody>
  326.                                 </table>
  327.                                 <?php wp_nonce_field( 'edd_create_payment_nonce', 'edd_create_payment_nonce' ); ?>
  328.                                 <input type="hidden" name="edd-gateway" value="manual_purchases"/>
  329.                                 <input type="hidden" name="edd-action" value="create_payment" />
  330.                                 <?php submit_button(__('Create Payment', 'edd-manual-purchases') ); ?>
  331.                         </form>
  332.                 </div>
  333.                 <?php
  334.         }
  335.  
  336.         public static function check_for_variations() {
  337.  
  338.                 if( isset($_POST['nonce'] ) && wp_verify_nonce($_POST['nonce'], 'edd_create_payment_nonce') ) {
  339.  
  340.                         $download_id = absint( $_POST['download_id'] );
  341.  
  342.                         if( edd_has_variable_prices( $download_id ) ) {
  343.  
  344.                                 $prices = get_post_meta( $download_id, 'edd_variable_prices', true );
  345.                                 $response = '';
  346.                                 if( $prices ) {
  347.                                         $response = '<select name="downloads[' . absint( $_POST['key'] ) . '][options][price_id]" class="edd-mp-price-select">';
  348.                                                 foreach( $prices as $key => $price ) {
  349.                                                         $response .= '<option value="' . esc_attr( $key ) . '">' . $price['name']  . '</option>';
  350.                                                 }
  351.                                         $response .= '</select>';
  352.                                 }
  353.                                 echo $response;
  354.                         }
  355.                         die();
  356.                 }
  357.         }
  358.  
  359.         public static function create_payment( $data ) {
  360.  
  361.                 if( wp_verify_nonce( $data['edd_create_payment_nonce'], 'edd_create_payment_nonce' ) ) {
  362.  
  363.                         global $edd_options;
  364.  
  365.                        
  366.  
  367.                         $user = strip_tags( trim( $data['user'] ) );
  368.  
  369.                         if( empty( $user ) ) {
  370.                                 wp_die( __( 'Please enter a username, user ID, or email for the customer this payment belongs to.', 'edd-manual-purchases' ) );
  371.                         }
  372.  
  373.  
  374.                         if( is_numeric( $user ) )
  375.                                 $user = get_userdata( $user );
  376.                         elseif ( is_email( $user ) )
  377.                                 $user = get_user_by( 'email', $user );
  378.                         elseif ( is_string( $user ) )
  379.                                 $user = get_user_by( 'login', $user );
  380.                         else
  381.                                 return; // no user assigned
  382.  
  383.                         $user_id        = $user ? $user->ID : 0;
  384.                         $email          = $user ? $user->user_email : strip_tags( trim( $data['user'] ) );
  385.                         if( isset( $data['first'] ) ) {
  386.                                 $user_first = sanitize_text_field( $data['first'] );
  387.                         } else {
  388.                                 $user_first     = $user ? $user->first_name : '';      
  389.                         }
  390.  
  391.                         if( isset( $data['last'] ) ) {
  392.                                 $user_last = sanitize_text_field( $data['last'] );
  393.                         } else {
  394.                                 $user_last      = $user ? $user->last_name : '';
  395.                         }
  396.  
  397.                         $user_info = array(
  398.                                 'invoice'       => $invoice,                  
  399.                                 'id'                    => $user_id,
  400.                                 'email'                 => $email,
  401.                                 'first_name'    => $user_first,
  402.                                 'last_name'             => $user_last,
  403.                                 'discount'              => 'none'
  404.                         );
  405.  
  406.                         $price = ( isset( $data['amount'] ) && $data['amount'] !== false ) ? edd_sanitize_amount( strip_tags( trim( $data['amount'] ) ) ) : false;
  407.  
  408.                         $cart_details = array();
  409.  
  410.                         $total = 0;
  411.                         foreach( $data['downloads'] as $key => $download ) {
  412.  
  413.                                 // calculate total purchase cost
  414.  
  415.                                 if( isset( $download['options'] ) ) {
  416.  
  417.                                         $prices     = get_post_meta( $download['id'], 'edd_variable_prices', true );
  418.                                         $price_key  = $download['options']['price_id'];
  419.                                         $item_price = $prices[$price_key]['amount'];
  420.  
  421.                                 } else {
  422.                                         $item_price = edd_get_download_price( $download['id'] );
  423.                                 }
  424.  
  425.                                 $cart_details[$key] = array(
  426.                                         'name'        => get_the_title( $download['id'] ),
  427.                                         'id'          => $download['id'],
  428.                                         'item_number' => $download,
  429.                                         'price'       => $price ? '0.00' : $item_price,
  430.                                         'quantity'    => 1,
  431.                                         'tax'         => 0,
  432.                                 );
  433.                                 $total += $item_price;
  434.  
  435.                         }
  436.  
  437.                         // assign total to the price given, if any
  438.                         if( $price ) {
  439.                                 $total = $price;
  440.                         }
  441.  
  442.                         $date = ! empty( $data['date'] ) ? strip_tags( trim( $data['date'] ) ) : 'NOW';
  443.                         $date = date( 'Y-m-d H:i:s', strtotime( $date, current_time( 'timestamp' ) ) );
  444.  
  445.                         $status = isset( $_POST['status'] ) ? sanitize_text_field( $_POST['status'] ) : 'pending';
  446.  
  447.                         $purchase_data     = array(
  448.                                 'price'        => edd_sanitize_amount( $total ),
  449.                                 'tax'          => 0,
  450.                                 'post_date'    => $date,
  451.                                 'purchase_key' => strtolower( md5( uniqid() ) ), // random key
  452.                                 'user_email'   => $email,
  453.                                 'user_info'    => $user_info,
  454.                                 'currency'     => $edd_options['currency'],
  455.                                 'downloads'    => $data['downloads'],
  456.                                 'cart_details' => $cart_details,
  457.                                 'status'       => 'pending'
  458.                         );
  459.  
  460.                         update_post_meta( $payment_id, 'edd-invoice', $invoice_id );
  461.                         $payment_id = edd_insert_payment( $purchase_data );
  462.  
  463.                        
  464.  
  465.                         if( empty( $data['receipt'] ) || $data['receipt'] != '1' ) {
  466.                                 remove_action( 'edd_complete_purchase', 'edd_trigger_purchase_receipt', 999 );
  467.                         }
  468.  
  469.                         if( ! empty( $data['expiration'] ) && class_exists( 'EDD_Recurring_Customer' ) && $user_id > 0 ) {
  470.  
  471.                                 $expiration = strtotime( $data['expiration'] . ' 23:59:59' );
  472.  
  473.                                 EDD_Recurring_Customer::set_as_subscriber( $user_id );
  474.                                 EDD_Recurring_Customer::set_customer_payment_id( $user_id, $payment_id );
  475.                                 EDD_Recurring_Customer::set_customer_status( $user_id, 'active' );
  476.                                 EDD_Recurring_Customer::set_customer_expiration( $user_id, $expiration );
  477.                         }
  478.  
  479.                         // increase stats and log earnings
  480.                         edd_update_payment_status( $payment_id, $status ) ;
  481.  
  482.                         wp_redirect( admin_url( 'edit.php?post_type=download&page=edd-payment-history&edd-message=payment_created' ) ); exit;
  483.  
  484.                 }
  485.         }
  486.  
  487.         public static function payment_created_notice() {
  488.                 if( isset($_GET['edd-message'] ) && $_GET['edd-message'] == 'payment_created' && current_user_can( 'view_shop_reports' ) ) {
  489.                         add_settings_error( 'edd-notices', 'edd-payment-created', __('The payment has been created.', 'edd-manual-purchases'), 'updated' );
  490.                 }
  491.         }
  492.  
  493.  
  494. }
  495.  
  496. $GLOBALS['edd_manual_purchases'] = new EDD_Manual_Purchases();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement