forked from bearded-avenger/RCP-Gifts-Through-EDD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcp-coupons-edd.php
134 lines (92 loc) · 3.95 KB
/
rcp-coupons-edd.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/*
* Plugin Name: Restrict Content Pro - Gift Memberships
* Description: Purchase RCP discount codes as gifts through Easy Digital Downloads
* Author: Pippin Williamson
* Version: 1.1
*/
class RCP_Gift_Memberships {
private $admin;
private $gifts;
private $checkout;
private $redeem;
public function __construct() {
$this->includes();
}
public function includes() {
include dirname( __FILE__ ) . '/includes/class-gifts.php';
include dirname( __FILE__ ) . '/includes/class-gifts-admin.php';
include dirname( __FILE__ ) . '/includes/class-gifts-checkout.php';
include dirname( __FILE__ ) . '/includes/class-redeem-gift.php';
$this->admin = new RCP_Gifts_Admin;
$this->gifts = new RCP_Gift_Products;
$this->checkout = new RCP_Gifts_Checkout;
$this->redeem = new CGC_RCP_Redeem_Gift;
}
public function gift_subscription_level( $download_id = 0 ) {
return get_post_meta( $download_id, '_rcp_gift_subscription_level', true );
}
public function is_gift_product( $download_id = 0 ) {
$gift = get_post_meta( $download_id, '_rcp_gift_product', true );
return ! empty( $gift );
}
public function is_gift_multiuse( $download_id = 0 ) {
$gift = get_post_meta( $download_id, '_rcp_gift_multiuse', true );
return ! empty( $gift );
}
public function gift_expires( $download_id = 0 ) {
return get_post_meta( $download_id, '_rcp_gift_expires', true );
}
public function payment_was_gift( $payment_id = 0 ) {
$gift = get_post_meta( $payment_id, '_edd_payment_is_rcp_gift', true );
return ! empty( $gift );
}
public function get_gifts_of_payment( $payment_id = 0 ) {
return get_post_meta( $payment_id, '_edd_rcp_gift_data', true );
}
public function send_recipient_email( $name = '', $email = '', $gift_message = '', $payment_id = 0 ) {
if( ! class_exists( 'RCP_Discounts' ) )
return false;
global $edd_options;
$db = new RCP_Discounts;
$site_name = get_bloginfo( 'name' );
$discount = $db->get_by( 'code', md5( $name . $email . $payment_id ) );
$subject = sprintf( __( 'Gift Certificate to %s', 'rcp-gifts' ), $site_name );
$message = '<p>' . __( "Hello!", "rcp-gifts" ) . '</p>';
$message .= '<p>' . sprintf( __( "Someone has gifted you a membership to %s", "rcp-gifts" ), $site_name ) . '</p>';
if( ! empty( $gift_message ) && __( 'Enter the a message to send to the recipient', 'rcp-gifts' ) != $gift_message ) {
$message .= '<p>' . __( "The following message was included with the gift: ", "rcp-gifts" ) . '</p>';
$message .= '<blockquote>' . $gift_message . '</blockquote>';
}
$message .= '<p>' . sprintf( __( "Enter %s from http://cgcookie.com/redeem to redeem your gift.", "rcp-gifts" ), $discount->code ) . '</p>';
EDD()->emails->__set( 'header', 'CG Cookie Gift Certificate' );
EDD()->emails->send( $email, $subject, $message );
}
public function create_discount( $name = '', $email = '', $payment_id = 0, $download_id = '' ) {
if( ! class_exists( 'RCP_Discounts' ) )
return false;
$db = new RCP_Discounts;
$code = md5( $name . $email . $payment_id );
$multiuse = $this->is_gift_multiuse($download_id) ? 0 : 1;
$expires = $this->gift_expires($download_id);
$sublevel = $this->gift_subscription_level($download_id);
$discount = array(
'name' => $name,
'description' => sprintf( __( 'Gifted discount for %s', 'rcp-gifts' ), $name ),
'amount' => '100',
'status' => 'active',
'unit' => '%',
'code' => $code,
'max_uses' => $multiuse,
'expiration' => $expires,
'subscription_id' => $sublevel
);
$discount_id = $db->insert( $discount );
$note = sprintf( __( 'Purchased as gift for %s. Coupon: %s', 'rcp-gifts' ), $name, $code );
// Store a payment note about this gift
edd_insert_payment_note( $payment_id, $note );
// store discount ids for each gifted product
add_post_meta( $payment_id, '_edd_rcp_gift_id', $discount_id, true );
}
}
$rcp_gifts = new RCP_Gift_Memberships;