function ck_edd_user_download_button($purchase_form, $args) { global $edd_options; if (!is_user_logged_in()) { return $purchase_form; } $download_id = (string) $args['download_id']; $current_user_id = get_current_user_id(); // If the user has purchased this item, itterate through their purchases to get the specific // purchase data and pull out the key and email associated with it. This is necessary for the // generation of the download link if (edd_has_user_purchased($current_user_id, $download_id, $variable_price_id = null)) { $user_purchases = edd_get_users_purchases($current_user_id, -1, false, 'complete'); foreach ($user_purchases as $purchase) { $cart_items = edd_get_payment_meta_cart_details($purchase->ID); $item_ids = wp_list_pluck($cart_items, 'id'); if (in_array($download_id, $item_ids)) { $email = edd_get_payment_user_email($purchase->ID); $payment_key = edd_get_payment_key($purchase->ID); } } $download_ids = array(); if (edd_is_bundled_product($download_id)) { $download_ids = edd_get_bundled_products($download_id); } else { $download_ids[] = $download_id; } // Setup the style and colors associated with the settings $style = isset($edd_options['button_style']) ? $edd_options['button_style'] : 'button'; $color = isset($edd_options['checkout_color']) ? $edd_options['checkout_color'] : 'blue'; $new_purchase_form = ''; foreach ($download_ids as $item) { // Attempt to get the file data associated with this download $download_data = edd_get_download_files($item, null); if ($download_data) { foreach ($download_data as $filekey => $file) { // Generate the file URL and then make a link to it $file_url = edd_get_download_file_url($payment_key, $email, $filekey, $item, null); $new_purchase_form .= '<a href="' . $file_url . '" class="' . $style . ' ' . $color . ' edd-submit"><span class="edd-add-to-cart-label">Download ' . $file['name'] . '</span></a> '; } } // As long as we ended up with links to show, use them. if (!empty($new_purchase_form)) { $purchase_form = '<h4>' . __('You already own this product. Download it now:', 'edd') . '</h4>' . $new_purchase_form; } } } return $purchase_form; }
foreach ($download_files as $filekey => $file) { $download_url = edd_get_download_file_url($meta['key'], $email, $filekey, $item['id'], $price_id); ?> <li class="edd_download_file"> <a href="<?php echo esc_url($download_url); ?> " class="edd_download_file_link"><?php echo edd_get_file_name($file); ?> </a> </li> <?php do_action('edd_receipt_files', $filekey, $file, $item['id'], $payment->ID, $meta); } } elseif (edd_is_bundled_product($item['id'])) { $bundled_products = edd_get_bundled_products($item['id']); foreach ($bundled_products as $bundle_item) { ?> <li class="edd_bundled_product"> <span class="edd_bundled_product_name"><?php echo get_the_title($bundle_item); ?> </span> <ul class="edd_bundled_product_files"> <?php $download_files = edd_get_download_files($bundle_item); if ($download_files && is_array($download_files)) { foreach ($download_files as $filekey => $file) { $download_url = edd_get_download_file_url($meta['key'], $email, $filekey, $bundle_item, $price_id); ?>
function deactivate_license($args) { global $edd_options; $defaults = array('key' => '', 'item_name' => '', 'item_id' => 0, 'expiration' => time(), 'url' => ''); $args = wp_parse_args($args, $defaults); extract($args, EXTR_SKIP); $license_id = self::get_license_by_key($key); $download_id = self::get_download_by_license($key); $item_name = html_entity_decode($item_name); if (empty($url)) { // Attempt to grab the URL from the user agent if no URL is specified $domain = array_map('trim', explode(';', $_SERVER['HTTP_USER_AGENT'])); $url = trim($domain[1]); } $bypass_local = isset($edd_options['edd_sl_bypass_local_hosts']); $is_local_url = empty($bypass_local) ? false : $this->is_local_url($url); do_action('edd_sl_pre_deactivate_license', $license_id, $download_id); // grab info about the license $license_expires = self::get_license_expiration($license_id); $license_key = self::get_license_key($license_id); $license_status = self::get_license_status($license_id); // make sure license is active if ($license_status != 'active' && !$bypass_local) { return false; } // Trying to activate bundle license if (edd_is_bundled_product($download_id)) { return false; } // don't deactivate if expired $is_lifetime_license = self::is_lifetime_license($license_id); if (!$is_lifetime_license && $license_expires < $expiration) { return false; // this license has expired } if ($key != $license_key) { return false; // keys don't match } if (!empty($args['item_id'])) { if (!$this->is_download_id_valid_for_license($download_id, $args['key'])) { return false; } } else { // Item names don't match if ((!defined('EDD_BYPASS_NAME_CHECK') || !EDD_BYPASS_NAME_CHECK) && !self::check_item_name($download_id, $item_name)) { return false; // Item names don't match } } // deactivate the site for the license self::delete_site($license_id, $url); if (!$is_local_url) { // enter this deactivation in the log self::log_license_deactivation($license_id, $_SERVER); do_action('edd_sl_deactivate_license', $license_id, $download_id); } return true; // license has been deactivated }
/** * Gets all download files for a product * * Can retrieve files specific to price ID * * @since 1.0 * @param int $download_id Download ID * @param int $variable_price_id Variable pricing option ID * @return array $files Download files */ function edd_get_download_files($download_id = 0, $variable_price_id = null) { $files = array(); // Bundled products are not allowed to have files if (edd_is_bundled_product($download_id)) { return $files; } $download_files = get_post_meta($download_id, 'edd_download_files', true); if ($download_files) { if (!is_null($variable_price_id) && edd_has_variable_prices($download_id)) { foreach ($download_files as $key => $file_info) { if (isset($file_info['condition'])) { if ($file_info['condition'] == $variable_price_id || 'all' === $file_info['condition']) { $files[$key] = $file_info; } } } } else { $files = $download_files; } } return apply_filters('edd_download_files', $files, $download_id, $variable_price_id); }
/** * Email template tag: file_urls * A plain-text list of download URLs for each download purchased * * @param int $payment_id * * @return string $file_urls */ function edd_email_tag_file_urls($payment_id) { $payment_data = edd_get_payment_meta($payment_id); $file_urls = ''; $cart_items = edd_get_payment_meta_cart_details($payment_id); $email = edd_get_payment_user_email($payment_id); foreach ($cart_items as $item) { $price_id = edd_get_cart_item_price_id($item); $files = edd_get_download_files($item['id'], $price_id); if ($files) { foreach ($files as $filekey => $file) { $file_url = edd_get_download_file_url($payment_data['key'], $email, $filekey, $item['id'], $price_id); $file_urls .= esc_html($file_url) . '<br/>'; } } elseif (edd_is_bundled_product($item['id'])) { $bundled_products = apply_filters('edd_email_tag_bundled_products', edd_get_bundled_products($item['id']), $item, $payment_id, 'file_urls'); foreach ($bundled_products as $bundle_item) { $files = edd_get_download_files($bundle_item); foreach ($files as $filekey => $file) { $file_url = edd_get_download_file_url($payment_data['key'], $email, $filekey, $bundle_item, $price_id); $file_urls .= esc_html($file_url) . '<br/>'; } } } } return $file_urls; }
/** * * @param unknown $payment * @param string $edd_receipt_args */ public function edd_osgi_action_payment_receipt_after_table($payment, $edd_receipt_args = null) { if ($this->osgipub_osgi_debug) { echo "<p>debug: this is the action after table</p>\n"; } if (isset($payment) && edd_is_payment_complete($payment->ID)) { $meta = get_post_meta($payment->ID); if ($this->osgipub_osgi_debug) { echo "<p>debug: Payment vardump="; var_dump($payment); echo "</p>\n"; echo "<p>debug: Payment metadata vardump="; var_dump($meta); echo "</p>\n"; } // same $cart = edd_get_payment_meta_cart_details( $payment->ID, true ); $downloads = edd_get_payment_meta_cart_details($payment->ID, true); $edd_payment_post_id = $payment->ID; $edd_payment_user_id = (string) $meta['_edd_payment_user_id'][0]; $edd_payment_customer_id = (string) $meta['_edd_payment_customer_id'][0]; //TODO REMOVED BECAUSE NOT BEING GENERATED $edd_payment_number = ( string ) $meta ['_edd_payment_number'] [0]; $edd_payment_number = $edd_payment_post_id; $edd_payment_purchase_key = (string) $meta['_edd_payment_purchase_key'][0]; // see easy-digital-downloads/templates/history-downloads.php // add_query_arg( 'payment_key', edd_get_payment_key( $post->ID ), edd_get_success_page_uri() ) if ($downloads) { $display_licence_table = false; // check if any downloads are osgi licenced foreach ($downloads as $download) { if ($this->is_osgi_licenced($download['id'])) { $display_licence_table = true; } } if ($display_licence_table) { echo "<div id=\"osgi_licence_list_table\" class=\"osgi_licence_list\">\n"; echo "<h3>OSGi Licences</h3>\n"; echo "<p>One of more of your purchased downloads have associated OSGi Licences.<BR>To generate your licences select the links below.</p>\n"; echo "<table>\n"; } // used to set change licence name for multiple downloads $download_no = 0; foreach ($downloads as $download) { // Skip over Bundles. Products included with a bundle will be displayed individually if (edd_is_bundled_product($download['id'])) { continue; } // if not osgi licenced bundle skip if (!$this->is_osgi_licenced($download['id'])) { continue; } $download_no++; $price_id = edd_get_cart_item_price_id($download); $download_files = edd_get_download_files($download['id'], $price_id); $name = get_the_title($download['id']); // quantity used to handle multiple licences per download for ($quantity = 1; $quantity <= $download['quantity']; $quantity++) { if (isset($edd_payment_number)) { // start of table row echo " <tr>\n"; echo " <td>\n"; // product id string from download // contains maven unique id of product to which this licence applies $edd_osgiProductIdStr = get_post_meta($download['id'], '_edd_osgiProductIdStr', true); // try loading modified LicenceMetadataSpecStr from this product efinition and apply to licence post $edd_modified_osgiLicenceMetadataSpecStr = get_post_meta($download['id'], '_edd_modified_osgiLicenceMetadataSpecStr', true); // Retrieve and append the price option name if (!empty($price_id)) { $name .= ' - ' . edd_get_price_option_name($download['id'], $price_id, $payment->ID); } // product name - payment number - download number $licence_post_title = $name . ' - ' . $edd_payment_number . '-' . $download_no . '-' . $quantity; // remove whitepsace $licence_post_name = preg_replace('/\\s+/', '', $licence_post_title); if ($this->osgipub_osgi_debug) { echo "<p>debug: download [id]=" . $download['id'] . '</p>\\n'; if (!isset($edd_osgiProductIdStr)) { echo "<p>debug: from download edd_osgiProductIdStr not set for download [id]=" . $download['id'] . '</p>\\n'; } else { echo "<p>debug: from download edd_osgiProductIdStr="; echo $edd_osgiProductIdStr; echo "</p>\n"; } echo "<p>debug: payment name="; echo $name; echo "</p>\n"; echo "<p>debug: licence_post_title="; echo $licence_post_title; echo "</p>\n"; echo "<p>debug: licence_post_name="; echo $licence_post_name; echo "</p>\n"; echo "<p>debug: edd_payment_number="; echo $edd_payment_number; echo "</p>\n"; } $found_post = null; if ($posts = get_posts(array('name' => $licence_post_name, 'post_type' => 'osgi_licence_post', 'post_status' => 'publish', 'posts_per_page' => 1))) { $found_post = $posts[0]; } // Now, we can do something with $found_post if (!is_null($found_post)) { if ($this->osgipub_osgi_debug) { echo "<p>debug: we found the licence post="; echo $found_post->ID; echo "</p>\n"; } echo '<a href="' . get_post_permalink($found_post->ID) . '" >Link to Licence: ' . $licence_post_title . '</a>'; echo "\n"; } else { // get post with payment number metadata OR create post with metadata $post = array('post_content' => '<p>DO NOT EDIT: You can only view or change this licence post by using View Post.</p>', 'post_name' => $licence_post_name, 'post_title' => $licence_post_title, 'post_status' => 'publish', 'post_type' => 'osgi_licence_post', 'ping_status' => 'closed', 'comment_status' => 'closed'); // Default is the option 'default_comment_status', or 'closed'. // 'post_category' => [ array(<category id>, ...) ] // Default empty. // 'tags_input' => [ '<tag>, <tag>, ...' | array ] // Default empty. // 'tax_input' => [ array( <taxonomy> => <array | string> ) ] // For custom taxonomies. Default empty. // 'page_template' => '../edd-downloads-as-osgi.php' // Requires name of template file, eg template.php. Default empty. $newpost_id = wp_insert_post($post); // setting product id for licence // update_post_meta ( $newpost_id, 'edd_osgiProductIdStr', 'org.opennms.co.uk/org.opennms.co.uk.newfeature/0.0.1-SNAPSHOT' ); update_post_meta($newpost_id, 'edd_osgiProductIdStr', $edd_osgiProductIdStr); // apply modified metadata to this licence post update_post_meta($newpost_id, '_edd_modified_osgiLicenceMetadataSpecStr', $edd_modified_osgiLicenceMetadataSpecStr); // setting customer metadata - not yet used in the template update_post_meta($newpost_id, 'edd_payment_customer_id', $edd_payment_customer_id); update_post_meta($newpost_id, 'edd_payment_user_id', $edd_payment_user_id); // setting edd_osgiLicencee information $f_name = (string) get_user_meta($edd_payment_user_id, 'first_name', true); $first_name = isset($f_name) ? $f_name : ""; $l_name = (string) get_user_meta($edd_payment_user_id, 'last_name', true); $last_name = isset($l_name) ? $l_name : ""; $address = ""; $addr = edd_get_customer_address($edd_payment_user_id); if (isset($addr)) { $address = implode(", ", $addr); } $edd_osgiLicencee = $first_name . ", " . $last_name . ", " . $address; update_post_meta($newpost_id, 'edd_osgiLicencee', $edd_osgiLicencee); // for reverse lookup of post id of the associated payment update_post_meta($newpost_id, 'edd_payment_post_id', $edd_payment_post_id); if ($this->osgipub_osgi_debug) { echo "<p>debug: we created a new licence post="; echo $newpost_id; echo "</p>\n"; } echo '<a href="' . get_post_permalink($newpost_id) . '" >Link to Licence: ' . $licence_post_title . '</a>'; echo "\n"; } echo " </td>\n"; echo " </tr>\n"; } } } if ($display_licence_table) { echo "</table>\n"; echo "</div> <!-- div id=osgi_licence_list_table -->"; } } } else { echo '<p>payment not set</p>\\n'; } if ($this->osgipub_osgi_debug) { if (isset($edd_receipt_args)) { echo "<p>debug: edd_receipt_args vardump="; var_dump($edd_receipt_args); echo "</p>\n"; } else { echo '<p>debug: edd_receipt_args not set</p>\\n'; } } }
/** * Process the license upgrade during purchase * * @since 3.3 * @return void */ function edd_sl_process_license_upgrade($download_id = 0, $payment_id = 0, $type = 'default', $cart_item = array(), $cart_index = 0) { // Bail if this is not a renewal item if (empty($cart_item['item_number']['options']['is_upgrade'])) { return; } $license_id = $cart_item['item_number']['options']['license_id']; $upgrade_id = $cart_item['item_number']['options']['upgrade_id']; $upgrade = edd_sl_get_upgrade_path($download_id, $upgrade_id); $old_payment_ids = get_post_meta($license_id, '_edd_sl_payment_id'); $old_payment_id = end($old_payment_ids); // We only want the most recent one $old_download_id = edd_software_licensing()->get_download_id($license_id); $old_price_id = edd_software_licensing()->get_price_id($license_id); $purchase_date = get_post_field('post_date', $old_payment_id); if (edd_is_bundled_product($download_id)) { // Upgrade to a bundle from a standard license $downloads = array(); $bundle_licensing = (bool) get_post_meta($download_id, '_edd_sl_enabled', true); $parent_license_id = 0; $activation_limit = false; $user_info = edd_get_payment_meta_user_info($payment_id); if ($bundle_licensing) { $downloads[] = $download_id; } $downloads = array_merge($downloads, edd_get_bundled_products($download_id)); if (edd_has_variable_prices($download_id)) { $activation_limit = edd_software_licensing()->get_price_activation_limit($download_id, $cart_item['item_number']['options']['price_id']); $is_lifetime = edd_software_licensing()->get_price_is_lifetime($download_id, $cart_item['item_number']['options']['price_id']); } foreach ($downloads as $d_id) { if ((int) $d_id === (int) $old_download_id) { continue; } if (!get_post_meta($d_id, '_edd_sl_enabled', true)) { continue; } $license_title = get_the_title($d_id) . ' - ' . $user_info['email']; $license_args = array('post_type' => 'edd_license', 'post_title' => $license_title, 'post_status' => 'publish', 'post_date' => get_post_field('post_date', $payment_id, 'raw')); if ($parent_license_id) { $license_args['post_parent'] = $parent_license_id; } $l_id = wp_insert_post(apply_filters('edd_sl_insert_license_args', $license_args)); if ($bundle_licensing && $download_id == $d_id && !$parent_license_id) { $parent_license_id = $l_id; } $license_key = edd_software_licensing()->get_new_download_license_key($d_id); if (!$license_key) { // No predefined license key available, generate a random one $license_key = edd_software_licensing()->generate_license_key($l_id, $d_id, $payment_id, $cart_index); } $price_id = isset($cart_item['item_number']['options']['price_id']) ? (int) $cart_item['item_number']['options']['price_id'] : false; add_post_meta($l_id, '_edd_sl_download_id', $d_id); if (false !== $price_id) { add_post_meta($l_id, '_edd_sl_download_price_id', $price_id); } add_post_meta($l_id, '_edd_sl_cart_index', $cart_index); add_post_meta($l_id, '_edd_sl_payment_id', $payment_id); add_post_meta($l_id, '_edd_sl_key', $license_key); add_post_meta($l_id, '_edd_sl_user_id', $user_info['id']); add_post_meta($l_id, '_edd_sl_status', 'inactive'); add_post_meta($l_id, '_edd_sl_site_count', 0); if ($parent_license_id && !empty($activation_limit)) { add_post_meta($l_id, '_edd_sl_limit', $activation_limit); } // Get license length $license_length = edd_software_licensing()->get_license_length($l_id, $payment_id, $d_id); if (empty($is_lifetime) && 'lifetime' !== $license_length) { // Set license expiration date delete_post_meta($l_id, '_edd_sl_is_lifetime'); edd_software_licensing()->set_license_expiration($l_id, strtotime($license_length, strtotime($purchase_date))); } else { edd_software_licensing()->set_license_as_lifetime($l_id); } do_action('edd_sl_store_license', $l_id, $d_id, $payment_id, $type); } // Now update the original license wp_update_post(array('ID' => $license_id, 'post_parent' => $parent_license_id)); update_post_meta($license_id, '_edd_sl_cart_index', $cart_index); add_post_meta($license_id, '_edd_sl_payment_id', $payment_id); } else { // Standard license upgrade $new_title = get_the_title($download_id) . ' - ' . edd_get_payment_user_email($payment_id); wp_update_post(array('ID' => $license_id, 'post_title' => $new_title)); update_post_meta($license_id, '_edd_sl_cart_index', $cart_index); add_post_meta($license_id, '_edd_sl_payment_id', $payment_id); update_post_meta($license_id, '_edd_sl_download_id', $download_id); if (edd_has_variable_prices($download_id)) { $limit = edd_software_licensing()->get_price_activation_limit($download_id, $upgrade['price_id']); $is_lifetime = edd_software_licensing()->get_price_is_lifetime($download_id, $upgrade['price_id']); update_post_meta($license_id, '_edd_sl_download_price_id', $upgrade['price_id']); } else { $limit = edd_software_licensing()->get_license_limit($download_id, $license_id); } update_post_meta($license_id, '_edd_sl_limit', $limit); $license_length = edd_software_licensing()->get_license_length($license_id, $payment_id, $download_id); if (empty($is_lifetime) && 'lifetime' !== $license_length) { // Set license expiration date delete_post_meta($license_id, '_edd_sl_is_lifetime'); edd_software_licensing()->set_license_expiration($license_id, strtotime($license_length, strtotime($purchase_date))); } else { edd_software_licensing()->set_license_as_lifetime($license_id); } } // Now store upgrade details / notes on payments $old_product = get_the_title($old_download_id); if (false !== $old_price_id) { $old_product .= ' - ' . edd_get_price_option_name($old_download_id, $old_price_id); } $new_product = get_the_title($download_id); if (edd_has_variable_prices($download_id)) { $new_product .= ' - ' . edd_get_price_option_name($download_id, $upgrade['price_id']); } $note = sprintf(__('License upgraded from %s to %s', 'edd_sl'), $old_product, $new_product); edd_insert_payment_note($payment_id, $note); update_post_meta($payment_id, '_edd_sl_upgraded_payment_id', $old_payment_id); update_post_meta($old_payment_id, '_edd_sl_upgraded_to_payment_id', $payment_id); }
/** * Query database for license data and prepare it for the table * * @access private * @since 1.0 * @return array */ function licenses_data() { $licenses_data = array(); $license_args = array('post_type' => 'edd_license', 'post_status' => array('publish', 'future', 'draft'), 'posts_per_page' => $this->per_page, 'paged' => $this->get_paged(), 'meta_query' => array('relation' => 'AND'), 'post_parent' => 0); $view = isset($_GET['view']) ? $_GET['view'] : false; if ($view && 'disabled' == $view) { $license_args['post_status'] = 'draft'; } elseif ($view) { $license_args['meta_query'][] = array('key' => '_edd_sl_status', 'value' => $view); } $key_search = false; // check to see if we are searching if (!empty($_GET['s'])) { $search = trim($_GET['s']); if (!is_email($search)) { $has_period = strstr($search, '.'); if (false === $has_period && !preg_match('/\\s/', $search)) { // Search in the license key. $license_args['meta_query'][] = array('key' => '_edd_sl_key', 'value' => $search); $key_search = true; unset($license_args['post_parent']); } elseif ($has_period) { // Search in the sites that are registered. $license_args['meta_query'][] = array('key' => '_edd_sl_sites', 'value' => edd_software_licensing()->clean_site_url($search), 'compare' => 'LIKE'); } else { $license_args['s'] = $search; } } else { $license_args['s'] = $search; } } $orderby = isset($_GET['orderby']) ? $_GET['orderby'] : 'ID'; $order = isset($_GET['order']) ? $_GET['order'] : 'DESC'; $order_inverse = $order == 'DESC' ? 'ASC' : 'DESC'; $license_args['order'] = $order; switch ($orderby) { case 'purchased': $license_args['orderby'] = 'date'; break; case 'expires': $license_args['orderby'] = 'meta_value_num'; $license_args['meta_key'] = '_edd_sl_expiration'; break; } $licenses = get_posts($license_args); // If searching by Key if ($key_search) { $found_license = $licenses[0]; // And we found a child license if (!empty($found_license->post_parent)) { // Swap out the meta query for the parent license to show the entire bundle $parent_license_key = get_post_meta($found_license->post_parent, '_edd_sl_key', true); foreach ($license_args['meta_query'] as $key => $args) { if (!empty($args['key']) && '_edd_sl_key' === $args['key']) { $license_args['meta_query'][$key]['value'] = $parent_license_key; } } $licenses = get_posts($license_args); } } if ($licenses) { foreach ($licenses as $license) { $status = get_post_meta($license->ID, '_edd_sl_status', true); $key = get_post_meta($license->ID, '_edd_sl_key', true); $user = get_post_meta($license->ID, '_edd_sl_user_id', true); $expires = date_i18n(get_option('date_format'), (int) get_post_meta($license->ID, '_edd_sl_expiration', true)); $purchased = get_the_time(get_option('date_format'), $license->ID); $download_id = get_post_meta($license->ID, '_edd_sl_download_id', true); $licenses_data[] = array('ID' => $license->ID, 'title' => get_the_title($license->ID), 'status' => $status, 'key' => $key, 'user' => $user, 'expires' => $expires, 'purchased' => $purchased, 'download_id' => $download_id, 'is_child_license' => false); if (edd_is_bundled_product($download_id)) { $child_args = array('post_type' => 'edd_license', 'post_status' => array('publish', 'future'), 'posts_per_page' => -1, 'post_parent' => $license->ID); $child_licenses = get_children($child_args); } else { $child_licenses = false; } if (!empty($child_licenses)) { foreach ($child_licenses as $child_license) { $child_license_status = get_post_meta($child_license->ID, '_edd_sl_status', true); if (!empty($_GET['view']) && $child_license_status !== $_GET['view']) { continue; } $licenses_data[] = array('ID' => $child_license->ID, 'title' => get_the_title($child_license->ID), 'status' => $status, 'key' => $key, 'user' => $user, 'expires' => $expires, 'purchased' => $purchased, 'download_id' => get_post_meta($child_license->ID, '_edd_sl_download_id', true), 'is_child_license' => true); } } } } return $licenses_data; }
/** * Count number of customers who will receive product update emails * * * @access public * @return $customercount (number of customers eligible for product updates) */ function edd_pup_customer_count($email_id = null, $products = null, $subscribed = true, $filters = null) { if (empty($email_id) && !is_numeric($email_id)) { return false; } if (empty($products)) { return 0; } global $edd_options; global $wpdb; $count = 0; $b = $subscribed ? 0 : 1; $products = !empty($products) ? $products : get_post_meta($email_id, '_edd_pup_updated_products', TRUE); $filters = isset($filters) ? $filters : get_post_meta($email_id, '_edd_pup_filters', true); // Filter bundle customers only if ($filters['bundle_2']) { $bundleproducts = array(); $bundlenum = 0; foreach ($products as $id => $name) { if (edd_is_bundled_product($id)) { $bundleproducts[$id] = $name; $bundlenum++; } } $products = $bundlenum > 0 ? $bundleproducts : $products; } // Active EDD Software Licensing integration if (isset($edd_options['edd_pup_license']) && is_plugin_active('edd-software-licensing/edd-software-licenses.php')) { // Get customers who have a completed payment and are subscribed for updates $customers = $wpdb->get_results("\n\t \tSELECT post_id, meta_value\n\t \tFROM {$wpdb->posts}, {$wpdb->postmeta}\n\t \tWHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id\n\t \t\tAND post_type = 'edd_payment'\n\t \t\tAND post_status = 'publish'\n\t \t\tAND meta_key = '_edd_payment_meta'\n\t\t\t\tAND meta_value NOT LIKE '%%\"edd_send_prod_updates\";b:0%%'\n\t\t\t", OBJECT_K); // Get updated products with EDD software licensing enabled $products_imp = implode(',', array_keys($products)); $licenseditems = $wpdb->get_results("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_edd_sl_enabled' AND meta_value = 1 AND post_id IN ( {$products_imp} )", OBJECT_K); foreach ($customers as $customer) { $paymentmeta = unserialize($customer->meta_value); foreach ($paymentmeta['cart_details'] as $item) { // Skip $item if it is not a product being updated if (!isset($products[$item['id']])) { continue; } // Check if they have purchased any non-licensed products which would send them the email anyway if (!isset($licenseditems[$item['id']]) && isset($products[$item['id']])) { $count++; break; // Finally check to make sure customer has licenses then check that it is valid for that item. } else { $licenses = edd_pup_get_license_keys($customer->post_id); $enabled = get_post_status($licenses[$item['id']]['license_id']) == 'publish' ? true : false; $status = edd_software_licensing()->get_license_status($licenses[$item['id']]['license_id']); $accepted = apply_filters('edd_pup_valid_license_statuses', array('active', 'inactive')); if (!empty($licenses) && $enabled && in_array($status, $accepted)) { $count++; break; } } } } // Inactive EDD Software Licensing integration } else { $n = count($products); $i = 1; $q = ''; foreach ($products as $id => $name) { if (is_numeric($id)) { $s = strlen($id); $id = absint($id); if ($i === $n) { $q .= "meta_value LIKE '%\"id\";s:{$s}:\"{$id}\"%' OR meta_value LIKE '%\"id\";i:{$id}%' )"; } else { $q .= "meta_value LIKE '%\"id\";s:{$s}:\"{$id}\"%' OR meta_value LIKE '%\"id\";i:{$id}%' OR "; } } $i++; } $customers = $wpdb->get_results("\n\t \tSELECT post_id, meta_value\n\t \tFROM {$wpdb->posts}, {$wpdb->postmeta}\n\t \tWHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id\n\t \t\tAND post_type = 'edd_payment'\n\t \t\tAND post_status = 'publish'\n\t \t\tAND meta_key = '_edd_payment_meta'\n\t\t\t\tAND (meta_value NOT LIKE '%%\"edd_send_prod_updates\";b:{$b}%%'\n\t\t\t\t\tAND ({$q} )\n\t\t\t", OBJECT); $count = $wpdb->num_rows; } return $count; }
//End if no redownload ?> <?php do_action('edd_download_history_header_end'); ?> </tr> </thead> <?php foreach ($purchases as $payment) { $downloads = edd_get_payment_meta_cart_details($payment->ID, true); $purchase_data = edd_get_payment_meta($payment->ID); $email = edd_get_payment_user_email($payment->ID); if ($downloads) { foreach ($downloads as $download) { // Skip over Bundles. Products included with a bundle will be displayed individually if (edd_is_bundled_product($download['id'])) { continue; } ?> <tr class="edd_download_history_row"> <?php $price_id = edd_get_cart_item_price_id($download); $download_files = edd_get_download_files($download['id'], $price_id); $name = get_the_title($download['id']); // Retrieve and append the price option name if (!empty($price_id)) { $name .= ' - ' . edd_get_price_option_name($download['id'], $price_id, $payment->ID); } do_action('edd_download_history_row_start', $payment->ID, $download['id']); ?>
/** * Builds the email queue and stores it in the edd_pup_queue db table * * @access public * @param mixed $data * @return $count (the number of emails logged in the queue to be sent) */ function edd_pup_ajax_start() { // Nonce security check if (!wp_verify_nonce($_POST['nonce'], 'edd_pup_ajax_start')) { echo 'noncefail'; exit; } $restart = edd_pup_is_ajax_restart($_POST['email_id']); $recipients = get_post_meta($_POST['email_id'], '_edd_pup_recipients', TRUE); $userid = get_current_user_id(); $usertransient = get_transient('edd_pup_sending_email_' . $userid); // Check if user is already sending another email when attempting to send this one if (false !== $usertransient && $usertransient != $_POST['email_id']) { echo 'usersendfail'; exit; } // Check whether the email is restarting from a previous send or via the pause button if (false != $restart && is_array($restart) && empty($_POST['status']) && $restart['total'] == $recipients) { set_transient('edd_pup_sending_email_' . $userid, $_POST['email_id']); $restart['status'] = 'restart'; echo json_encode($restart); exit; } else { // Check whether the email was paused by the user when building queue and then resumed on the same popup. if (is_array($restart) && $restart['total'] != $recipients) { $processed = $restart['total']; } else { if (isset($_POST['processed'])) { $processed = absint($_POST['processed']); } else { $processed = 0; } } global $wpdb; $i = 1; $count = 0; $limit = 1000; $total = isset($_POST['total']) ? absint($_POST['total']) : $recipients; $email_id = intval($_POST['email_id']); $filters = get_post_meta($email_id, '_edd_pup_filters', true); $products = get_post_meta($email_id, '_edd_pup_updated_products', true); // Filter for bundle only email sends if ($filters['bundle_2']) { $bundles = array(); foreach ($products as $prod_id => $prod_name) { if (edd_is_bundled_product($prod_id)) { $bundles[$prod_id] = $prod_name; } } $products = $bundles; } $customers = edd_pup_user_send_updates($products, true, $limit, $processed); $licenseditems = $wpdb->get_results("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_edd_sl_enabled' AND meta_value = 1", OBJECT_K); // Set email ID transient set_transient('edd_pup_sending_email_' . $userid, $email_id, 60); // Do some one-time operations at beginning of AJAX loop if ($_POST['iteration'] == 0) { // Update email status as in queue wp_update_post(array('ID' => $email_id, 'post_status' => 'pending')); // Update email with info on EDD Software Licensing Integration global $edd_options; update_post_meta($email_id, '_edd_pup_licensing_status', isset($edd_options['edd_pup_license']) ? 'active' : 'inactive'); } // Start building queue foreach ($customers as $customer) { // Check what products customers are eligible for updates and add to queue only if updates are available to customer $customer_updates = serialize(edd_pup_eligible_updates($customer['post_id'], $products, true, $licenseditems, $email_id)); if (!empty($customer_updates)) { $queue[] = '(' . $customer['post_id'] . ', ' . $email_id . ', \'' . $customer_updates . '\', 0)'; $count++; // Insert into database in batches of 1000 if ($i % $limit == 0) { $queueinsert = implode(',', $queue); $wpdb->query("INSERT INTO {$wpdb->edd_pup_queue} (customer_id, email_id, products, sent) VALUES {$queueinsert}"); // Reset defaults for next batch $queue = ''; break; } } $i++; } // Insert leftovers or if batch is less than 1000 if (!empty($queue)) { $queueinsert = implode(',', $queue); $wpdb->query("INSERT INTO {$wpdb->edd_pup_queue} (customer_id, email_id, products, sent) VALUES {$queueinsert}"); } echo json_encode(array('status' => 'new', 'sent' => 0, 'total' => absint($total), 'processed' => absint($processed + $count))); exit; } }
/** * Filter attachments * * @since 1.0 */ public function attachments($attachments, $payment_id, $payment_data) { global $edd_options; // get array of download IDs $downloads = edd_get_payment_meta_downloads($payment_id); if ($downloads) { $files = array(); $attachments = array(); foreach ($downloads as $download) { // if per download email attachments is enabled, only get downloads with checkbox enabled if (isset($edd_options['edd_dea_per_download_attachments'])) { if (!get_post_meta($download['id'], '_edd_dea_enabled', true)) { continue; } } // is bundled product if (edd_is_bundled_product($download['id'])) { $bundled_ids = get_post_meta($download['id'], '_edd_bundled_products', true); if ($bundled_ids) { foreach ($bundled_ids as $id) { $files[] = get_post_meta($id, 'edd_download_files', true); } } } else { $price_id = !empty($download['options']['price_id']) ? $download['options']['price_id'] : null; $files[] = edd_get_download_files($download['id'], $price_id); } } if ($files) { $file_urls = array(); foreach ($files as $key => $file) { // get the file URLs foreach ($file as $value) { $file_urls[] = $value['file']; } } if ($file_urls) { foreach ($file_urls as $file_url) { $attachments[] = get_attached_file($this->get_file_id($file_url)); } } } } return $attachments; }
$user = get_current_user_id(); $email_id = absint($_GET['id']); $email = get_post($email_id); $emailmeta = get_post_custom($email_id); $updated_products = get_post_meta($email_id, '_edd_pup_updated_products', TRUE); $recipients = get_post_meta($email_id, '_edd_pup_recipients', TRUE); $filters = isset($emailmeta['_edd_pup_filters'][0]) ? maybe_unserialize($emailmeta['_edd_pup_filters'][0]) : null; $queue = edd_pup_check_queue($email_id); $processing = edd_pup_is_processing($email_id) ? true : false; $dateformat = get_option('date_format') . ' ' . get_option('time_format'); $baseurl = admin_url('edit.php?post_type=download&page=edd-prod-updates'); $restarturl = add_query_arg(array('view' => 'send_pup_ajax', 'id' => $email_id, 'restart' => 1), admin_url('edit.php?post_type=download&page=edd-prod-updates')); $duplicateurl = wp_nonce_url(add_query_arg(array('edd_action' => 'pup_duplicate_email', 'id' => $email_id, 'redirect' => 1), $baseurl), 'edd-pup-duplicate-nonce'); // Find if any products were bundles foreach ($updated_products as $prod_id => $prod_name) { if (edd_is_bundled_product($prod_id)) { $bundle++; } } switch (strtolower($email->post_status)) { case 'publish': $status = __('Sent', 'edd-pup'); break; case 'pending': if ($processing) { $status = __('Processing', 'edd-pup'); } else { $status = __('In Queue', 'edd-pup'); } break; case 'abandoned':
/** * Email template tag: updated_products_links * A list of updated products with download links included * * @access public * @param mixed $payment_id * @return void */ function edd_pup_products_links_tag_plain($payment_id, $email = null) { // Used to generate accurate tag outputs for preview and test emails if (isset($email) && absint($email) != 0) { $updated_products = get_post_meta($email, '_edd_pup_updated_products', true); $updated_products = is_array($updated_products) ? $updated_products : array($updated_products); foreach ($updated_products as $id => $name) { $customer_updates[$id] = array('id' => $id, 'name' => $name); } } else { $email = get_transient('edd_pup_sending_email_' . get_current_user_id()); $updated_products = get_post_meta($email, '_edd_pup_updated_products', true); $updated_products = is_array($updated_products) ? $updated_products : array($updated_products); $customer_updates = edd_pup_get_customer_updates($payment_id, $email); $customer_updates = is_array($customer_updates) ? $customer_updates : array($customer_updates); } $filters = get_post_meta($email, '_edd_pup_filters', true); if ($customer_updates) { $show_names = apply_filters('edd_pup_email_show_names', true); $payment_data = edd_get_payment_meta($payment_id); // Set email to most recent email if it's been changed from initial email if (isset($payment_data['user_info']['email']) && $payment_data['user_info']['email'] != $payment_data['email']) { $payment_data['email'] = $payment_data['user_info']['email']; } // Used for detecting when to place commas $c = 1; $download_list = ''; foreach ($customer_updates as $item) { if (edd_use_skus()) { $sku = edd_get_download_sku($item['id']); } $price_id = edd_get_cart_item_price_id($item); if ($show_names) { $title = $c == 1 ? $item['name'] : ', ' . $item['name']; if (!empty($sku)) { $title .= " – " . __('SKU', 'edd') . ': ' . $sku; } $download_list .= apply_filters('edd_pup_email_products_link_title_plain', $title, $item, $price_id, $payment_id); } $files = edd_get_download_files($item['id'], $price_id); if ($files) { // $f used for detecting when to place commas $f = 1; $download_list .= ' ('; foreach ($files as $filekey => &$file) { $file_url = edd_get_download_file_url($payment_data['key'], $payment_data['email'], $filekey, $item['id'], $price_id); $download_list .= $f == 1 ? edd_get_file_name($file) . ': ' . esc_url($file_url) : ', ' . edd_get_file_name($file) . ': ' . esc_url($file_url); $f++; } $download_list .= ')'; } if (edd_is_bundled_product($item['id'])) { $b = 1; $bundled_products = edd_get_bundled_products($item['id']); $download_list .= " – "; foreach ($bundled_products as $bundle_item) { if ($filters['bundle_1'] == 'all' || isset($updated_products[$bundle_item])) { $download_list .= $b == 1 ? get_the_title($bundle_item) : '; ' . get_the_title($bundle_item); $fb = 1; $bundlefiles = edd_get_download_files($bundle_item); $download_list .= ' ('; foreach ($bundlefiles as $bundlefilekey => $bundlefile) { $bundlefile_url = edd_get_download_file_url($payment_data['key'], $payment_data['email'], $bundlefilekey, $bundle_item, $price_id); $download_list .= $fb == 1 ? $bundlefile['name'] . ': ' . esc_url($bundlefile_url) : ', ' . $bundlefile['name'] . ': ' . esc_url($bundlefile_url); $fb++; } $download_list .= ')'; } $b++; } } if ('' != edd_get_product_notes($item['id'])) { $download_list .= ' – ' . edd_get_product_notes($item['id']); } $c++; } return $download_list; } }
/** * Handle the ajax call, no need for nopriv handling since this is admin only * * @access private * @since 2.6 * @return void */ function edd_sl_generate_download_keys_ajax_callback() { if (!current_user_can('edit_shop_payments')) { status_header(404); die; } // If there is no download ID posted, breakout immediately because we cannot find the download if (!isset($_POST['download'])) { status_header(404); die; } // Grab the download ID and make sure its an int $download = intval($_POST['download']); // Make sure the post we are looking at is a download, otherwise the post (media type) is unsupported! if (get_post_type($download) !== 'download') { status_header(415); die; } // Gather all the payments, and individual download IDs so we can verify licenses exist $args = array('download' => $download, 'number' => -1); $query = new EDD_Payments_Query($args); $payments = $query->get_payments(); $is_bundle = edd_is_bundled_product($download); $downloads = $is_bundle ? edd_get_bundled_products($download) : array($download); // Loop through the payments, and then the downloads, and maybe generate licenses $generated = 0; $updated = 0; foreach ($payments as $payment) { if ($is_bundle) { $parent_license = edd_software_licensing()->get_license_by_purchase($payment->ID, $download); } foreach ($downloads as $d) { // If download ID is empty, continue if (empty($d)) { continue; } // Maybe generate a key, and if we do increase the count $license = edd_software_licensing()->get_license_by_purchase($payment->ID, $d); // If no license exists... generate one! if (!$license) { add_action('edd_sl_store_license', 'edd_sl_log_generated_license', 10, 4); $key = edd_software_licensing()->generate_license($d, $payment->ID); $license = edd_software_licensing()->get_license_by_purchase($payment->ID, $d); if ($is_bundle && $key) { // Set the post parent to the Bundle Key $update_args = array('ID' => $license->ID, 'post_parent' => $parent_license->ID); wp_update_post($update_args); } remove_action('edd_sl_store_license', 'edd_sl_log_generated_license', 10); // Return true if a key was generated if ($key) { $generated++; } } elseif ($is_bundle && $license) { if (empty($license->post_parent)) { // Set the post parent to the Bundle Key $update_args = array('ID' => $license->ID, 'post_parent' => $parent_license->ID); wp_update_post($update_args); $updated++; } } } } // We must die, or we live too long... but we must speak our last workd. printf(_n('One key was generated', '%d keys were generated.', $generated), $generated); echo '<br />'; printf(_n('One key was updated', '%d keys were updated.', $updated), $updated); die; }
/** * Has User Purchased * * Checks to see if a user has purchased a download. * * @access public * @since 1.0 * @param int $user_id - the ID of the user to check * @param array $downloads - Array of IDs to check if purchased. If an int is passed, it will be converted to an array * @param int $variable_price_id - the variable price ID to check for * @return boolean - true if has purchased and license is active, false otherwise */ function edd_dwqa_categories_has_user_purchased($user_id, $downloads, $variable_price_id = null, $verify_purchase = false) { $users_purchases = edd_get_users_purchases($user_id); $return = false; if (!is_array($downloads)) { $downloads = array($downloads); } $now = strtotime(date('Y-m-d H:i:s')); if ($users_purchases) { foreach ($users_purchases as $purchase) { $purchased_files = edd_get_payment_meta_downloads($purchase->ID); if (empty($purchased_files)) { return false; } $licenses = edd_software_licensing()->get_licenses_of_purchase($purchase->ID); $licenses_products = array(); if (is_array($licenses)) { foreach ($licenses as $license) { $download_id = get_post_meta($license->ID, '_edd_sl_download_id', true); $status = get_post_meta($license->ID, '_edd_sl_status', true); $expire = get_post_meta($license->ID, '_edd_sl_expiration', true); $licenses_products[$download_id] = array(); $licenses_products[$download_id]['status'] = $status; $licenses_products[$download_id]['expire'] = $expire; } } else { return false; } if (is_array($purchased_files)) { foreach ($purchased_files as $download) { if (edd_is_bundled_product($download['id'])) { $bundled_products = edd_get_bundled_products($download['id']); if (empty($bundled_products)) { return false; } foreach ($bundled_products as $bundle_product) { if (in_array($bundle_product, $downloads)) { //check to see if the license is active //echo $licenses_products[$download['id']]['expire'] . ">" . $now . "=========="; if (isset($licenses_products[$bundle_product]['expire']) && $now > $licenses_products[$bundle_product]['expire']) { // || $licenses_products[$download['id']]['status'] == 'inactive' if ($verify_purchase) { return "purchased_expired"; } else { return false; } } $variable_prices = edd_has_variable_prices($bundle_product); if ($variable_prices && !is_null($variable_price_id) && $variable_price_id !== false) { if (isset($download['options']['price_id']) && $variable_price_id == $download['options']['price_id']) { return true; } else { return false; } } else { return true; } } } return false; } if (in_array($download['id'], $downloads)) { //check to see if the license is active //echo $licenses_products[$download['id']]['expire'] . ">" . $now . "=========="; if (isset($licenses_products[$download['id']]['expire']) && $now > $licenses_products[$download['id']]['expire']) { // || $licenses_products[$download['id']]['status'] == 'inactive' if ($verify_purchase) { return "purchased_expired"; } else { return false; } } $variable_prices = edd_has_variable_prices($download['id']); if ($variable_prices && !is_null($variable_price_id) && $variable_price_id !== false) { if (isset($download['options']['price_id']) && $variable_price_id == $download['options']['price_id']) { return true; } else { return false; } } else { return true; } } } } } } return false; }