forked from awesomemotive/edd-fes-product-updates
/
edd-fes-product-updates.php
238 lines (176 loc) · 7.81 KB
/
edd-fes-product-updates.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* Plugin Name: Easy Digital Downloads - FES Product Updates
* Plugin URI: http://easydigitaldownloads.com
* Description: Allow Frontend Submissions vendors to send product updates to their customers
* Author: Pippin Williamson and Chris Klosowski
* Author URI: http://easydigitaldownlads.com
* Version: 1.0
* Text Domain: edd-fes-product-updates
* Domain Path: languages
*
* Easy Digital Downloads - FES Product Updates is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* Easy Digital Downloads is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Easy Digital Downloads Frontend Submissions Product Updates. If not, see <http://www.gnu.org/licenses/>.
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
class EDD_FES_Product_Updates {
private $plugin_dir;
private $plugin_url;
public function __construct() {
$this->plugin_dir = plugin_dir_path( __FILE__ );
$this->plugin_url = plugin_dir_url( __FILE__ );
$this->includes();
add_filter( 'edd_template_paths', array( $this, 'register_template_path' ) );
add_filter( 'fes_vendor_dashboard_menu', array( $this, 'register_email_menu' ) );
add_filter( 'fes_signal_custom_task', array( $this, 'register_dashboard_task' ), 10, 2 );
add_filter( 'edd_settings_emails', array( $this, 'settings' ) );
add_action( 'fes_custom_task_email_customers', array( $this, 'render_email_tab' ) );
add_action( 'edd_fes_create_email', array( $this, 'process_vendor_email_submission' ) );
}
public function includes() {
include $this->plugin_dir . 'includes/functions.php';
}
public function register_template_path( $paths ) {
$paths[49] = trailingslashit( $this->plugin_dir ) . 'templates/';
return $paths;
}
public function register_email_menu( $menu_items = array() ) {
if( isset( $menu_items['logout'] ) ) {
// Remove logout
$logout = $menu_items['logout'];
unset( $menu_items['logout'] );
}
$menu_items['emails'] = array(
"icon" => "envelope",
"task" => array( 'email_customers' ),
"name" => __( 'Email Customers', 'edd-fes-product-updates' ),
);
if( isset( $logout ) ) {
// Re-add logout so it remains at the end
$menu_items['logout'] = $logout;
}
return $menu_items;
}
public function register_dashboard_task( $custom, $task ) {
if( 'email_customers' == $task ) {
$custom = 'email_customers';
}
return $custom;
}
public function settings( $settings ) {
$settings['fes_pu_settings'] = array(
'id' => 'fes_pu_settings',
'name' => '<strong>' . __( 'FES Product Updates', 'edd-fes-product-updates' ) . '</strong>',
'desc' => __( 'Configure the options for FES Product Updates', 'edd-fes-product-updates' ),
'type' => 'header'
);
$settings['fes_pu_default_footer'] = array(
'id' => 'fes_pu_default_footer',
'name' => __( 'Default Email Footer Content', 'edd-fes-product-updates' ),
'desc' => __( 'Content entered here will be automatically appended to the bottom of emails created by vendors', 'edd-fes-product-updates' ),
'type' => 'textarea'
);
$settings['fes_pu_tags'] = array(
'id' => 'fes_pu_tags',
'name' => __( 'Email Tags', 'edd-fes-product-updates' ),
'desc' => __( 'Select the email tags that are shown to vendors as options', 'edd-fes-product-updates' ),
'type' => 'multicheck',
'options' => edd_fes_pu_get_email_tags()
);
return $settings;
}
public function render_email_tab() {
edd_get_template_part( 'fes', 'email-customers-tab' );
}
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 );
}
}
private function notify_admins( $email_id = 0, $author = OBJECT ) {
$subject = __( 'New Product Update Email Submitted', 'edd-fes-product-updates' );
$message = sprintf(
__( 'A new product update email has been submitted by a %s. Click <a href="%s">here</a> to review the email.', 'edd-fes-product-updates' ),
$author->display_name,
admin_url( 'edit.php?post_type=download&page=edd-prod-updates&view=edit_pup_email&id=' . $email_id )
);
EDD()->emails->send( edd_get_admin_notice_emails(), $subject, $message );
}
private function auto_send( $author_id = 0 ) {
$ret = get_post_meta( $author_id, '_edd_fes_pu_auto_send', true );
return (bool) apply_filters( 'edd_fes_product_updates_auto_send', $ret, $author_id );
}
}
function edd_fes_load_product_updates() {
if( ! class_exists( 'Easy_Digital_Downloads' ) ) {
return;
}
if( ! class_exists( 'EDD_Front_End_Submissions' ) ) {
return;
}
if( ! class_exists( 'EDD_Product_Updates' ) ) {
return;
}
$instance = new EDD_FES_Product_Updates;
}
add_action( 'plugins_loaded', 'edd_fes_load_product_updates', 9999 );