/**
 * Saves an email. If the email already exists, it is updated.
 *
 * @since 0.9.2
 * @param string $data
 * @param int $email_id
 * @return int the post ID of the email saved
 */
function edd_pup_save_email($data, $email_id = null)
{
    // Set variables that are the same for all customers
    $from_name = isset($data['from_name']) ? $data['from_name'] : get_bloginfo('name');
    $from_email = isset($data['from_email']) ? $data['from_email'] : get_bloginfo('admin_email');
    $subject = apply_filters('edd_purchase_subject', !empty($data['subject']) ? wp_strip_all_tags($data['subject'], true) : __('New Product Update', 'edd-pup'));
    $products = isset($data['products']) ? $data['products'] : '';
    $filters = array('bundle_1' => $data['bundle_1'], 'bundle_2' => $data['bundle_2']);
    if (0 != $email_id) {
        // Don't save any changes unless email is editable
        if (get_post_status($email_id) != 'draft') {
            return;
        }
        $updateargs = array('ID' => $email_id, 'post_content' => $data['message'], 'post_title' => $data['title'], 'post_excerpt' => $data['subject']);
        // Get number of recipients for this email
        $recipients = edd_pup_customer_count($email_id, $products, true, $filters);
        $update_id = wp_update_post($updateargs);
        update_post_meta($email_id, '_edd_pup_from_name', $from_name);
        update_post_meta($email_id, '_edd_pup_from_email', $from_email);
        update_post_meta($email_id, '_edd_pup_subject', $data['subject']);
        update_post_meta($email_id, '_edd_pup_message', $data['message']);
        update_post_meta($email_id, '_edd_pup_updated_products', $products);
        update_post_meta($email_id, '_edd_pup_recipients', $recipients);
        update_post_meta($email_id, '_edd_pup_filters', $filters);
        if ($update_id != 0 && $update_id == $email_id) {
            return $email_id;
        }
    } else {
        // Build post parameters array for custom post
        $post = array('post_content' => $data['message'], 'post_name' => '', 'post_title' => $data['title'], 'post_status' => 'draft', 'post_type' => 'edd_pup_email', 'post_author' => '', 'ping_status' => 'closed', 'post_parent' => 0, 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => $data['subject'], 'comment_status' => 'closed');
        // Create post and get the ID
        $create_id = wp_insert_post($post);
        // Get number of recipients for this email
        $recipients = edd_pup_customer_count($create_id, $products, true, $filters);
        // Insert custom meta for newly created post
        if (0 != $create_id) {
            add_post_meta($create_id, '_edd_pup_from_name', $from_name, true);
            add_post_meta($create_id, '_edd_pup_from_email', $from_email, true);
            add_post_meta($create_id, '_edd_pup_subject', $data['subject'], true);
            add_post_meta($create_id, '_edd_pup_message', $data['message'], true);
            add_post_meta($create_id, '_edd_pup_updated_products', $products, true);
            add_post_meta($create_id, '_edd_pup_recipients', $recipients);
            add_post_meta($create_id, '_edd_pup_filters', $filters, true);
        }
        if (0 != $create_id) {
            return $create_id;
        }
    }
}
 public function process_vendor_email_submission($data)
 {
     if (!current_user_can('edit_shop_payments') && !EDD_FES()->vendors->vendor_is_vendor(get_current_user_id())) {
         wp_die(__('You do not have permission to submit email updates', 'edd-fes-product-updates'), __('Error', 'edd-fes-product-updates'), array('response' => 401));
     }
     if (!wp_verify_nonce($data['edd_fes_create_email'], 'edd_fes_create_email')) {
         wp_die(__('Nonce verification has failed', 'edd-fes-product-updates'), __('Error', 'edd-fes-product-updates'), array('response' => 401));
     }
     if (empty($data['fes-email-products'])) {
         wp_die(__('Please select at least one product', 'edd-fes-product-updates'), __('Error', 'edd-fes-product-updates'), array('response' => 401));
     }
     if (empty($data['fes-email-subject'])) {
         wp_die(__('Please enter a subject for the email', 'edd-fes-product-updates'), __('Error', 'edd-fes-product-updates'), array('response' => 401));
     }
     if (empty($data['fes-email-message'])) {
         wp_die(__('Please enter a message for the email', 'edd-fes-product-updates'), __('Error', 'edd-fes-product-updates'), array('response' => 401));
     }
     $products = $data['fes-email-products'];
     // Verify all included products belong to the current user
     foreach ($products as $key => $product_id) {
         $product = get_post($product_id);
         if ((int) get_current_user_id() !== (int) $product->post_author) {
             unset($products[$key]);
         }
     }
     // Re-verify there are products
     if (empty($products)) {
         wp_die(__('Please select at least one product', 'edd-fes-product-updates'), __('Error', 'edd-fes-product-updates'), array('response' => 401));
     }
     $author = get_userdata(get_current_user_id());
     $subject = sanitize_text_field($data['fes-email-subject']);
     $message = wp_kses_post($data['fes-email-message'] . "\n\n" . edd_get_option('fes_pu_default_footer'));
     $from_name = $author->display_name;
     $from_email = $author->user_email;
     $auto_send = $this->auto_send($author->ID);
     $args = array('post_type' => 'edd_pup_email', 'post_status' => 'draft', 'post_content' => $message, 'post_excerpt' => $subject, 'post_title' => sprintf(__('Vendor #%d: %s', 'edd-fes-product-updates'), $author->ID, $subject));
     $email_id = wp_insert_post($args);
     $recipients = edd_pup_customer_count($email_id, $products);
     update_post_meta($email_id, '_edd_pup_subject', $subject);
     update_post_meta($email_id, '_edd_pup_message', $message);
     update_post_meta($email_id, '_edd_pup_from_name', $from_name);
     update_post_meta($email_id, '_edd_pup_from_email', $from_email);
     update_post_meta($email_id, '_edd_pup_updated_products', $products);
     update_post_meta($email_id, '_edd_pup_recipients', $recipients);
     if ($auto_send) {
     } else {
         $this->notify_admins($email_id, $author);
     }
 }
/**
 * Edit Product Update Email Page
 *
 * @since 0.9.3
 */
// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}
$email_id = absint($_GET['id']);
$email = get_post($email_id);
$emailmeta = get_post_custom($email_id);
$filters = unserialize($emailmeta['_edd_pup_filters'][0]);
$updated_products = get_post_meta($email_id, '_edd_pup_updated_products', TRUE);
$recipients = edd_pup_customer_count($email_id, $updated_products);
$products = edd_pup_get_all_downloads();
$tags = edd_get_email_tags();
$status = get_post_status($email_id);
$fromname = !empty($emailmeta['_edd_pup_from_name'][0]) ? $emailmeta['_edd_pup_from_name'][0] : '';
$fromemail = !empty($emailmeta['_edd_pup_from_email'][0]) ? $emailmeta['_edd_pup_from_email'][0] : '';
// Redirect to view page if edit page is accessed directly
if ($status != 'draft') {
    ?>
	<script type="text/javascript">
		window.location.href = document.URL.replace('edit_pup_email', 'view_pup_email');
	</script>
	<?php 
}
?>
Exemplo n.º 4
0
/**
 * Generates HTML for email confirmation via AJAX on send button press
 * 
 * @access public
 * @return void
 * @since 0.9
 */
function edd_pup_email_confirm_html()
{
    $form = array();
    parse_str($_POST['form'], $form);
    parse_str($_POST['url'], $url);
    // Nonce security check
    if (empty($form['edd-pup-send-nonce']) || !wp_verify_nonce($form['edd-pup-send-nonce'], 'edd-pup-confirm-send')) {
        echo 'noncefail';
        die;
    }
    // Make sure there are products to be updated
    if (empty($form['products'])) {
        echo 'nocheck';
        die;
    }
    // Save the email before generating preview
    $email_id = edd_pup_sanitize_save($_POST);
    // If "Send Update Email" was clicked from Add New Email page, trigger browser to refresh to edit page for continued editing
    if ($url['view'] == 'add_pup_email') {
        echo absint($email_id);
        die;
    }
    // Necessary for preview HTML
    set_transient('edd_pup_preview_email_' . get_current_user_id(), $email_id, 60);
    $bundlenum = 0;
    $email = get_post($email_id);
    $emailmeta = get_post_custom($email_id);
    $filters = unserialize($emailmeta['_edd_pup_filters'][0]);
    $products = get_post_meta($email_id, '_edd_pup_updated_products', true);
    $popup_url = add_query_arg(array('view' => 'send_pup_ajax', 'id' => $email_id), admin_url('edit.php?post_type=download&page=edd-prod-updates'));
    $customercount = edd_pup_customer_count($email_id, $products);
    $email_body = isset($email->post_content) ? stripslashes($email->post_content) : 'Cannot retrieve message content';
    $subject = empty($emailmeta['_edd_pup_subject'][0]) ? '(no subject)' : strip_tags(edd_email_preview_template_tags($emailmeta['_edd_pup_subject'][0]));
    $productlist = '';
    // Construct templated email HTML
    add_filter('edd_email_template', 'edd_pup_template');
    if (version_compare(get_option('edd_version'), '2.1') >= 0) {
        $edd_emails = new EDD_Emails();
        $message = $edd_emails->build_email(edd_email_preview_template_tags($email_body));
    } else {
        $message = edd_apply_email_template($email_body, null, null);
    }
    // Add max-width for plaintext emails so they don't run off the page
    if ('none' == edd_pup_template()) {
        $message = '<div style="width: 640px;">' . $message . '</div>';
    }
    // Save message to metadata for later retrieval
    update_post_meta($email_id, '_edd_pup_message', $message);
    ob_start();
    ?>
		<!-- Begin send email confirmation message -->
			<h2 id="edd-pup-confirm-title"><strong><?php 
    _e('Almost Ready to Send!', 'edd-pup');
    ?>
</strong></h2>
			<p style="text-align: center;"><?php 
    _e('Please carefully check the information below before sending your emails.', 'edd-pup');
    ?>
</p>
			<div id="edd-pup-confirm-message">
				<div id="edd-pup-confirm-header">
					<h3><?php 
    _e('Email Message Preview', 'edd-pup');
    ?>
</h3>
					<ul>
						<li><strong><?php 
    _e('From:', 'edd-pup');
    ?>
</strong> <?php 
    echo $emailmeta['_edd_pup_from_name'][0];
    ?>
 (<?php 
    echo $emailmeta['_edd_pup_from_email'][0];
    ?>
)</li>
						<li><strong><?php 
    _e('Subject:', 'edd-pup');
    ?>
</strong> <?php 
    echo $subject;
    ?>
</li>
					</ul>
				</div>
				<?php 
    echo $message;
    ?>
				<div id="edd-pup-confirm-footer">
					<h3><?php 
    _e('Additional Information', 'edd-pup');
    ?>
</h3>
					<ul>
						<li><strong><?php 
    _e('Updated Products:', 'edd-pup');
    ?>
</strong></li>
							<ul id="edd-pup-confirm-products">
							<?php 
    foreach ($products as $product_id => $product) {
        $bundle = edd_is_bundled_product($product_id);
        // Filter bundle customers only
        if ($filters['bundle_2'] && !$bundle) {
            continue;
        }
        $productlist .= '<li data-id="' . $product_id . '">' . $product . '</li>';
        if ($bundle) {
            $bundledproducts = edd_get_bundled_products($product_id);
            $productlist .= '<ul id="edd-pup-confirm-bundle-products">';
            foreach ($bundledproducts as $bundleitem) {
                if (isset($products[$bundleitem])) {
                    $productlist .= '<li data-id="' . $bundleitem . '">' . $products[$bundleitem] . '</li>';
                }
            }
            $productlist .= '</ul>';
            $bundlenum++;
        }
    }
    echo $productlist;
    ?>
							</ul>
						<?php 
    if ($bundlenum > 0) {
        ?>
						<li><strong><?php 
        _e('Bundle Filters:', 'edd-pup');
        ?>
</strong></li>
							<ul id="edd-pup-confirm-products">
								<?php 
        foreach ($filters as $filtername => $filtervalue) {
            ?>
								<?php 
            switch ($filtername) {
                case 'bundle_1':
                    $output = $filtervalue == 'all' ? '<li>' . __('Show links for all products within bundles', 'edd-pup') . '</li>' : '<li>' . __('Show links for only updated products within bundles', 'edd-pup') . '</li>';
                    break;
                case 'bundle_2':
                    $output = $filtervalue == 1 ? '<li>' . __('Send this email to bundle customers only', 'edd-pup') . '</li>' : '';
                    break;
            }
            echo $output;
            ?>
								<?php 
        }
        ?>
							</ul>
						<?php 
    }
    ?>
						<li><strong><?php 
    _e('Recipients:', 'edd-pup');
    ?>
</strong> <?php 
    printf(_n('1 customer will receive this email and have their downloads reset', '%s customers will receive this email and have their downloads reset', $customercount, 'edd-pup'), number_format($customercount));
    ?>
</li>
					</ul>
					<a href="<?php 
    echo $popup_url;
    ?>
" id="prod-updates-email-ajax" class="button-primary button" title="<?php 
    _e('Confirm and Send Emails', 'edd-pup');
    ?>
" onclick="window.open(this.href,'targetWindow', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=480');return false;"><?php 
    _e('Confirm and Send Emails', 'edd-pup');
    ?>
</a>
					<button class="closebutton button-secondary"><?php 
    _e('Close without sending', 'edd-pup');
    ?>
</button>
				</div>
			</div>
		<!-- End send email confirmation message -->
		<script type="text/javascript">
			jQuery('.postbox .recipient-count').text("<?php 
    echo number_format($customercount);
    ?>
");
			jQuery('.postbox .recipient-input').val(<?php 
    echo $customercount;
    ?>
);
		</script>
	<?php 
    echo ob_get_clean();
    die;
}