/** * Checks if the attached file is an image * and runs functions that resizes and moves * high resolution files into protected dir. * If attachment isn't an image, it just moves it. * Original files are deleted. * * @param [integer] $attachment_id [The attachment id] * @return [null] */ function sell_media_move_file($attachment_id) { if (wp_attachment_is_image($attachment_id)) { Sell_Media()->images->move_image_from_attachment($attachment_id); } else { $attached_file = get_attached_file($attachment_id); sell_media_default_move($attached_file); } }
/** * Get all prices assigned to a product * * @return $prices (array) */ public function get_prices($post_id = null, $attachment_id = null, $taxonomy = 'price-group') { $i = 0; if ($this->settings->hide_original_price !== 'yes') { $original_size = Sell_Media()->images->get_original_image_size($attachment_id); $prices[$i]['id'] = 'original'; $prices[$i]['name'] = __('Original', 'sell_media'); $prices[$i]['description'] = __('The original high resolution source file', 'sell_media'); $prices[$i]['price'] = $this->get_price($post_id, 'original'); $prices[$i]['width'] = $original_size['original']['width']; $prices[$i]['height'] = $original_size['original']['height']; } if ($this->has_image_attachments($post_id)) { // check assigned price group. We're assuming there is just one. $term_parent = wp_get_post_terms($post_id, $taxonomy); // if no assigned price group, get default from settings if (empty($term_parent) || is_wp_error($term_parent)) { $default = $this->settings->default_price_group; $terms = get_terms($taxonomy, array('hide_empty' => false, 'parent' => $default, 'orderby' => 'id')); } else { $terms = get_terms($taxonomy, array('hide_empty' => false, 'parent' => $term_parent[0]->term_id, 'orderby' => 'id')); } // loop over child terms foreach ($terms as $term) { if (!empty($term->term_id)) { $i++; $prices[$i]['id'] = $term->term_id; $prices[$i]['name'] = $term->name; $prices[$i]['description'] = $term->description; $prices[$i]['price'] = sell_media_get_term_meta($term->term_id, 'price', true); $prices[$i]['width'] = sell_media_get_term_meta($term->term_id, 'width', true); $prices[$i]['height'] = sell_media_get_term_meta($term->term_id, 'height', true); } } } return $prices; }
/** * Returns the attachment ID file size * * @param $attachment_id ID of the attachment * @return string * @since 1.6.9 */ function sell_media_get_filesize($post_id = null, $attachment_id = null) { $file_path = Sell_Media()->products->get_protected_file($post_id, $attachment_id); if (file_exists($file_path)) { $bytes = filesize($file_path); $s = array('b', 'Kb', 'Mb', 'Gb'); $e = floor(log($bytes) / log(1024)); return sprintf('%.2f ' . $s[$e], $bytes / pow(1024, floor($e))); } }
/** * Filter custom column content on the edit media table. * * @since 0.1 */ function sell_media_payment_content($column, $post_id) { switch ($column) { case "id": $html = '<a href="' . site_url() . '/wp-admin/post.php?post=' . $post_id . '&action=edit">'; $html .= $post_id; $html .= '</a>'; echo $html; break; case "products": $products = Sell_Media()->payments->get_products($post_id); if ($products) { foreach ($products as $product) { $type = !empty($product['type']) ? ' (' . $product['type'] . ') ' : ''; echo $product['name'] . $type . '<br />'; } } break; case "customer": echo Sell_Media()->payments->get_buyer_name($post_id); break; case "total": echo sell_media_get_currency_symbol() . number_format(Sell_Media()->payments->get_meta_key($post_id, 'total'), 2, '.', ''); break; default: break; } }
/** * Stats meta box */ function sell_media_stats_meta_box($post) { ?> <?php do_action('sell_media_before_stats_meta_box', $post); ?> <div id="sell-media-stats" class="sell-media-field"> <ul> <li><strong><?php _e('Views', 'sell_media'); ?> :</strong> <?php echo sell_media_get_post_views($post->ID); ?> </li> <li><strong><?php _e('Sales', 'sell_media'); ?> :</strong> <?php echo Sell_Media()->payments->get_item_sales($post->ID); ?> </li> </ul> </div> <?php do_action('sell_media_after_stats_meta_box', $post); }
/** * Shows a list of everything user has downloaded. * Adds the 'sell_media_download_list' short code to the editor. [sell_media_download_list] * * @since 1.0.4 */ function sell_media_download_shortcode($atts) { $html = do_shortcode('[sell_media_login_form]'); if (is_user_logged_in()) { global $current_user; get_currentuserinfo(); $purchases = Sell_Media()->payments->get_user_payments($current_user->user_email); $html .= '<h2>'; $html .= __('Your Purchase History', 'sell_media'); $html .= '</h2>'; if ($purchases) { foreach ($purchases as $purchase) { $html .= '<div class="sell-media-purchase">'; $html .= '<p>'; $html .= '<strong>' . __('Purchase ID', 'sell_media') . ': ' . $purchase . '</strong>'; $html .= '<br /><span class="date">' . get_the_time('F j, Y', $purchase) . '</span>'; $html .= '</p>'; $html .= Sell_Media()->payments->get_payment_products_formatted($purchase); $html .= '</div>'; } } } return $html; }
<option selected="selected" value="" data-id="" data-size="" data-price="0" data-qty="0">-- <?php _e('Select a size', 'sell_media'); ?> --</option> <?php $prices = Sell_Media()->products->get_prices($post_id, $attachment_id); if ($prices) { foreach ($prices as $k => $v) { if (wp_attachment_is_image($attachment_id)) { $name = $v['name'] . ' (' . $v['width'] . ' x ' . $v['height'] . ')'; $dimensions = $v['width'] . ' x ' . $v['height']; } else { $name = $v['name']; $dimensions = 'Original'; } $download_sizes = Sell_Media()->images->get_downloadable_size($post_id, $attachment_id, null, true); if (array_key_exists($v['id'], $download_sizes['available']) || "original" == $v['id']) { echo '<option value="' . $name . '" data-id="' . $v['id'] . '" data-price="' . number_format($v['price'], 2, '.', '') . '" data-qty="1" data-size="' . $dimensions . '">' . $name . ': ' . sell_media_get_currency_symbol() . sprintf('%0.2f', $v['price']) . '</option>'; } } } ?> </select> </fieldset> <?php } else { ?> <input id="sell_media_item_base_price" type="hidden" value="<?php echo $price; ?> " data-price="<?php
/** * Determine the price of all items in the cart that is being sent during checkout and set it. */ public function sell_media_verify_callback() { ini_set('display_errors', 0); check_ajax_referer('validate_cart', 'security'); $settings = sell_media_get_plugin_options(); // Our PayPal settings $args = array('currency_code' => $settings->currency, 'business' => $settings->paypal_email, 'return' => get_permalink($settings->thanks_page), 'notify_url' => site_url('?sell_media-listener=IPN')); $cart = $_POST['cart']; // Set discount code id to 0 if it isn't in cart array if (empty($cart['custom'])) { $cart['custom'] = 0; } // Count the number of keys that match the pattern "item_number_" $cart_count = count(preg_grep('/^item_number_/', array_keys($cart))); $cnt = 0; for ($i = 1; $i <= $cart_count; $i++) { $cnt += $cart['quantity_' . $i]; } $sub_total = 0; $shipping_flag = false; for ($i = 1; $i <= $cart_count; $i++) { $product_id = $cart['item_number_' . $i]; $type = empty($cart['os0_' . $i]) ? null : $cart['os0_' . $i]; $cart['os1_' . $i] = null; // Remove image url from the paypal checkout page $price_id = empty($cart['os2_' . $i]) ? null : $cart['os2_' . $i]; $license_id = empty($cart['os5_' . $i]) ? null : $cart['os5_' . $i]; // this is a download with an assigned license, so add license markup if (!empty($license_id) || $license_id != "undefined") { $price = Sell_Media()->products->verify_the_price($product_id, $price_id); $markup = Sell_Media()->products->markup_amount($product_id, $price_id, $license_id); $amount = $price + $markup; } else { // this is either a download without a license or a print, so just verify the price $amount = Sell_Media()->products->verify_the_price($product_id, $price_id); } $cart['amount_' . $i] = number_format(apply_filters('sell_media_price_filter', $amount, $cart['custom'], $cnt), 2, '.', ''); $sub_total += $cart['amount_' . $i] * $cart['quantity_' . $i]; } // Add shipping if ($shipping_flag) { switch ($settings->reprints_shipping) { case 'shippingFlatRate': $shipping_amount = $settings->reprints_shipping_flat_rate; break; case 'shippingQuantityRate': $shipping_amount = $settings->reprints_shipping_quantity_rate; break; case 'shippingTotalRate': $shipping_amount = $settings->reprints_shipping_total_rate; break; default: $shipping_amount = 0; break; } } else { $shipping_amount = 0; } $cart['handling'] = number_format($shipping_amount, 2, '.', ''); // If tax is enabled, tax the order if ($settings->tax) { // Cannot validate taxes because of qty // So just get the tax rate from local storage //$cart['tax_cart'] = $cart['tax_cart']; // If we could validate taxes, we could start here: $tax_amount = $settings->tax_rate * $sub_total; $cart['tax_cart'] = number_format($tax_amount, 2, '.', ''); } wp_send_json(array('cart' => $cart)); }
/** * Retrives the lowest price available of an item from the price groups * * @param $post_id (int) The post_id, must be a post type of "sell_media_item" * @return Lowest price of an item */ function sell_media_item_min_price($post_id = null) { $value = get_post_meta($post_id, 'sell_media_free_downloads', true); $price = Sell_Media()->products->get_lowest_price($post_id); if (empty($price)) { $settings = sell_media_get_plugin_options(); $price = $settings->default_price; } if (isset($value) && "on" == $value) { return "0.00"; } else { return $price; } }
/** * Resize and download an image to the specified dimensions * http://codex.wordpress.org/Class_Reference/WP_Image_Editor * * Returns the new image file path * * @since 1.8.5 * @param $product_id * @param $attachment_id * @param $size_id * @return resized image file path */ public function download_image($product_id = null, $attachment_id = null, $size_id = null) { $file_path = Sell_Media()->products->get_protected_file($product_id, $attachment_id); $img = wp_get_image_editor($file_path); if (!is_wp_error($img)) { $width = sell_media_get_term_meta($size_id, 'width', true); $height = sell_media_get_term_meta($size_id, 'height', true); if ($width || $height) { if ($width >= $height) { $max = $width; } else { $max = $height; } $img->resize($max, $max, false); $img->set_quality(100); } $img->stream(); } }
// Get text domain $domain = 'sell_media'; // The "plugin_locale" filter is also used in load_plugin_textdomain() $locale = apply_filters('plugin_locale', get_locale(), $domain); // Create path to custom language file $custom_mo = WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo'; if (file_exists($custom_mo)) { load_textdomain($domain, $custom_mo); } else { load_plugin_textdomain($domain, FALSE, dirname(plugin_basename(__FILE__)) . '/languages/'); } } } // end SellMedia class } // End if class_exists check /** * The main function that returns the one and only SellMedia instance * Use this function to access classes and methods * Example: <?php $sell_media = Sell_Media(); ?> * * @since 1.8.5 * @return object The one true SellMedia instance */ function Sell_Media() { return SellMedia::instance(); } // Start Sell Media Sell_Media();
/** * When a payment is made PayPal will send us a response and this function is * called. From here we will confirm arguments that we sent to PayPal which * the ones PayPal is sending back to us. * This is the Pink Lilly of the whole operation. */ function sell_media_process_paypal_ipn() { /** * Instantiate the IPNListener class */ include dirname(__FILE__) . '/php-paypal-ipn/IPNListener.php'; $listener = new IPNListener(); /** * Set to PayPal sandbox or live mode */ $settings = sell_media_get_plugin_options(); $listener->use_sandbox = $settings->test_mode ? true : false; /** * Check if IPN was successfully processed */ if ($verified = $listener->processIpn()) { /** * Log successful purchases */ $transactionData = $listener->getPostData(); // POST data array file_put_contents('ipn_success.log', print_r($transactionData, true) . PHP_EOL, LOCK_EX | FILE_APPEND); $message = null; /** * Verify seller PayPal email with PayPal email in settings * * Check if the seller email that was processed by the IPN matches what is saved as * the seller email in our DB */ $settings = sell_media_get_plugin_options(); if ($_POST['receiver_email'] != $settings->paypal_email) { $message .= "\nEmail seller email does not match email in settings\n"; } /** * Verify currency * * Check if the currency that was processed by the IPN matches what is saved as * the currency setting */ $settings = sell_media_get_plugin_options(); if ($_POST['mc_currency'] != $settings->currency) { $message .= "\nCurrency does not match those assigned in settings\n"; } /** * Check if this payment was already processed * * PayPal transaction id (txn_id) is stored in the database, we check * that against the txn_id returned. */ $txn_id = get_post_meta($_POST['custom'], 'txn_id', true); if (empty($txn_id)) { update_post_meta($_POST['custom'], 'txn_id', $_POST['txn_id']); } else { $message .= "\nThis payment was already processed\n"; return; } /** * Verify the payment is set to "Completed". * * Create a new payment, send customer an email and empty the cart */ if (!empty($_POST['payment_status']) && $_POST['payment_status'] == 'Completed') { // Return if this IPN doesn't contain a Sell Media item if (empty($_POST['option_selection1_1']) && ($_POST['option_selection1_1'] != 'print' || $_POST['option_selection1_1'] != 'download')) { return; } $data = array('post_title' => $_POST['payer_email'], 'post_status' => 'publish', 'post_type' => 'sell_media_payment'); $payment_id = wp_insert_post($data); $payments = Sell_Media()->payments; if ($payment_id) { update_post_meta($payment_id, '_paypal_args', $_POST); // record the PayPal payment details $payments->paypal_copy_args($payment_id); // create new user, auto log them in, email them registration Sell_Media()->customer->insert($_POST['payer_email'], $_POST['first_name'], $_POST['last_name']); $message .= "\nSuccess! Your purchase has been completed.\n"; $message .= "Your transaction number is: {$_POST['txn_id']}\n"; $message .= "To email: {$_POST['payer_email']}\n"; // Send email to buyer and admin $email_status = $payments->email_receipt($payment_id, $_POST['payer_email']); $admin_email_status = $payments->email_receipt($payment_id, $settings->from_email); $message .= "{$email_status}\n"; $message .= "{$admin_email_status}\n"; do_action('sell_media_after_successful_payment', $payment_id); } } else { $message .= "\nPayment status not set to Completed\n"; } /** * Check if this is the test mode * * If this is the test mode we email the IPN text report. * note about and box http://stackoverflow.com/questions/4298117/paypal-ipn-always-return-payment-status-pending-on-sandbox */ if ($settings->test_mode == true) { $message .= "\nTest Mode\n"; $email = array('to' => $settings->from_email, 'subject' => 'Verified IPN', 'message' => $message . "\n" . $listener->getTextReport()); wp_mail($email['to'], $email['subject'], $email['message']); } } else { /** * Log errors */ $errors = $listener->getErrors(); file_put_contents('ipn_errors.log', print_r($errors, true) . PHP_EOL, LOCK_EX | FILE_APPEND); /** * An Invalid IPN *may* be caused by a fraudulent transaction attempt. It's * a good idea to have a developer or sys admin manually investigate any * invalid IPN. */ wp_mail($settings->from_email, 'Invalid IPN', $listener->getTextReport()); } }