-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathedd-message.php
More file actions
442 lines (388 loc) · 16.4 KB
/
Copy pathedd-message.php
File metadata and controls
442 lines (388 loc) · 16.4 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
/*
Plugin Name: EDD Service Extended
Plugin URI: https://wbcomdesigns.com/plugins/easy-digital-download-service-extended/
Description: Easy Digital Download Message adds message section in the user dashboard for conversation.
Version: 1.0.1
Author: WBCOM DESIGNS
Author URI: http://www.wbcomdesigns.com
License: GPL2
http://www.gnu.org/licenses/gpl-2.0.html
*/
defined( 'ABSPATH' ) or die( 'Plugin file cannot be accessed directly.' );
if ( !defined( '' ) ) {
define( 'WBCOM_EDD_DASH_MSG' , '1.0' );
}
if ( !defined( 'WBCOM_EDD_DASH_MSG_PATH' ) ) {
define( 'WBCOM_EDD_DASH_MSG_PATH' , plugin_dir_path( __FILE__ ) );
}
if ( !defined( 'WBCOM_EDD_DASH_MSG_URL' ) ) {
define( 'WBCOM_EDD_DASH_MSG_URL' , plugin_dir_url( __FILE__ ));
}
if ( !defined( 'WBCOM_EDD_DASH_MSG_DB_VERSION' ) ) {
define( 'WBCOM_EDD_DASH_MSG_DB_VERSION' , '1' );
}
if ( !defined( 'WBCOM_EDD_DASH_MSG_TEXT_DOMIAN' ) ) {
define( 'WBCOM_EDD_DASH_MSG_TEXT_DOMIAN' , 'wb-edd-order-thread' );
}
/*Function to check Main Easy digtal Download plugin is active or not
If not active it will not get active*/
if( !function_exists( 'wbcom_edd_require_check' ) )
{
function wbcom_edd_require_check()
{
if( !class_exists( 'Easy_Digital_Downloads' ) )
{
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
add_action( 'admin_notices', 'wbcom_edd_require_notice' );
deactivate_plugins( plugin_basename( __FILE__ ) );
}
}
}
add_action( 'admin_init', 'wbcom_edd_require_check' );
/*File have code to add and display initial message in the admin for single service
This initial will be used as first message by the service provider on service purchase*/
require_once WBCOM_EDD_DASH_MSG_PATH . 'includes/admin/add-custom-info-field.php';
/* Checks the Front end posting plugin is active
File have code to add and display initial message in the front end submition form for single service
This initial will be used as first message by the service provider on service purchase*/
if( class_exists( 'EDD_Front_End_Submissions' ) )
require_once WBCOM_EDD_DASH_MSG_PATH . 'includes/add-custom-info-field-front.php';
/*Function to create table for the conversation message to store and add db version in the option table*/
if( !function_exists( 'wbcom_edd_install' ) )
{
function wbcom_edd_install() {
global $wpdb;
$installed_ver = get_option( "edd_message_db_version" );
if ( $installed_ver != WBCOM_EDD_DASH_MSG_DB_VERSION )
{
$table_name = $wpdb->prefix . 'edd_dashboard_message';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
author_id mediumint(9) NOT NULL,
user_id mediumint(9) NOT NULL,
site_id mediumint(9) NOT NULL,
purchase_id mediumint(9) NOT NULL,
msg_time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
message text NOT NULL,
attachment text NOT NULL,
msg_read ENUM('0','1') NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
update_option( 'edd_message_db_version', WBCOM_EDD_DASH_MSG_DB_VERSION );
}
}
}
register_activation_hook( __FILE__, 'wbcom_edd_install' );
/*Function to display the notice for the admin to activate EDD plugin*/
if( !function_exists( 'wbcom_edd_require_notice' ) )
{
function wbcom_edd_require_notice() {
echo '<div id="message" class="updated fade"><p style="line-height: 150%">';
echo __( '<strong>Easy Digital Download</strong> plugin is not activated please activate it first.' ,WBCOM_EDD_DASH_MSG_TEXT_DOMIAN );
echo '</p></div>';
}
}
/*Function to add the style css and javascripts*/
if( !function_exists( 'add_edd_message_style_script' ) )
{
function add_edd_message_style_script()
{
wp_enqueue_script( 'jquery' );
wp_register_script('edd_message_script', WBCOM_EDD_DASH_MSG_URL . 'js/script.js', 'all');
wp_enqueue_script('edd_message_script');
wp_register_script('edd_rate_script', WBCOM_EDD_DASH_MSG_URL . 'js/jRate.min.js', 'all');
wp_enqueue_script('edd_rate_script');
wp_register_style('edd_message-css',WBCOM_EDD_DASH_MSG_URL.'css/style.css', 'all');
wp_enqueue_style('edd_message-css');
}
}
add_action('wp_enqueue_scripts', 'add_edd_message_style_script');
/*Funtion to filter the buttons from the tiny mice editor*/
if( !function_exists( 'edd_message_editor_buttons' ) )
{
function edd_message_editor_buttons( $buttons, $editor_id ) {
return array( 'bold', 'italic', 'underline', 'bullist', 'numlist', 'link', 'unlink', 'forecolor', 'undo', 'redo' );
}
}
/* Function to add message input form with file upload
Display all the conversation thread with avatar and name
*/
if( !function_exists( 'add_user_comment_edd' ) )
{
function add_user_comment_edd()
{
global $edd_receipt_args;
$meta_data = edd_get_payment_meta_cart_details( $edd_receipt_args['id'], true );
$user = edd_get_payment_meta_user_info( $edd_receipt_args['id'] );
$vendor = get_post_field( 'post_author', $meta_data[0]['id'] );
if( $user['id'] == get_current_user_id() || $vendor == get_current_user_id() )
{
$all_msg = get_conversation_by_pay_id( $edd_receipt_args['id'] );
$html = '<div id="add_user_comment" class="add_user_comment">';
$html .= do_action( 'show_insert_message' );
$thread_status = get_post_meta( $edd_receipt_args['id'], 'edd_user_order_thread', 'close' );
if( $thread_status != 'close' )
{
$html .= '<h3>' . __( 'Service Attachment', WBCOM_EDD_DASH_MSG_TEXT_DOMIAN ) . '</h3>
<form method="post" action="" enctype="multipart/form-data">';
ob_start();
$settings = array( 'media_buttons' => false, 'editor_height' => '150px', 'teeny' => true, 'quicktags'=>false );
add_filter( 'teeny_mce_buttons', 'edd_message_editor_buttons', 10, 2 );
wp_editor( '', 'message', $settings );
$html .= ob_get_clean();
$html .= '<input type="file" multiple name="attach[]" id="edd_message_attachment" style="display:none;" /><label for="edd_message_attachment" class="edd_message_attachment">' . __( 'Add Files', WBCOM_EDD_DASH_MSG_TEXT_DOMIAN ) . '</label><input type="submit" name="add_comment" value="Add" class="submit-msg" /><div id="edd_files_names"></div><input type="hidden" value="' . $vendor . '" name="edd_vendor"><input type="hidden" value="' . $edd_receipt_args['id'] . '" name="purchase_id">' . wp_nonce_field( 'edd_message_action', 'edd_message_nonce_field', false, false ) . '</form>';
}
$html .= $all_msg . '</div>';
}
return $html;
}
}
add_shortcode( 'add_user_comment_edd', 'add_user_comment_edd' );
if( !function_exists( 'edd_insert_true_msg' ) )
{
function edd_insert_true_msg()
{
return "Message inserted";
}
}
if( !function_exists( 'edd_insert_false_msg' ) )
{
function edd_insert_false_msg()
{
return "Message not inserted";
}
}
/* Function to insert the user message in the table
There are two condition to add message first to add normal thread message and multiple attachment
second to add the thread close message and rating for the user for the particlar order*/
if( !function_exists( 'edd_add_user_message' ) )
{
function edd_add_user_message()
{
global $wpdb;
/* start of the condition one */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['add_comment'] ) && isset( $_POST['add_comment'] ) )
{
if( wp_verify_nonce( $_POST['edd_message_nonce_field'], 'edd_message_action' ) )
{
$table_name = $wpdb->prefix . 'edd_dashboard_message';
$meta_data = edd_get_payment_meta_cart_details( $_POST['purchase_id'], true );
$vendor_check = get_post_field( 'post_author', $meta_data[0]['id'] );
$vendor = intval( $_POST['edd_vendor'] );
$user_id = get_current_user_id();
$purchase_id = intval( $_POST['purchase_id'] );
$message = wp_kses_post( $_POST['message'] );
$attach = '';
if( $vendor == $vendor_check )
{
if ( $_FILES ) {
include_once ABSPATH . 'wp-admin/includes/media.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/image.php';
foreach ( $_FILES['attach']['name'] as $f => $name ) {
if ( $_FILES['attach']['error'][ $f ] == 0 ) {
$file['name'] = $name;
$file['type'] = $_FILES['attach']['type'][ $f ];
$file['tmp_name'] = $_FILES['attach']['tmp_name'][ $f ];
$file['error'] = $_FILES['attach']['error'][ $f ];
$file['size'] = $_FILES['attach']['size'][ $f ];
$upload = wp_handle_upload( $file, array( 'test_form' => false ) );
if( !isset( $upload['error'] ) && isset( $upload['file'] ) ) {
$filetype = wp_check_filetype( basename( $upload['file'] ), null );
$title = $file['name'];
$ext = strrchr( $title, '.');
$title = ( $ext !== false ) ? substr( $title, 0, -strlen( $ext ) ) : $title;
$attachment = array(
'guid' => $upload['url'],
'post_mime_type' => $filetype['type'],
'post_title' => addslashes( $title ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach[] = wp_insert_attachment( $attachment, $upload['file'] );
}
}
}
}
//insert the single message with attachment*/
$wpdb->insert(
$table_name,
array(
'author_id' => $vendor,
'user_id' => $user_id,
'purchase_id' => $purchase_id,
'message' => $message,
'msg_time' => date( "Y-m-d H:i:s" ),
'attachment' => serialize( $attach ),
),
array(
'%d',
'%d',
'%d',
'%s',
'%s',
'%s',
)
);
add_action( 'show_insert_message', 'edd_insert_true_msg' );
}
else
{
add_action( 'show_insert_message', 'edd_insert_false_msg' );
}
}
}
/* End of condition one*/
/* Second condition start to add close message and rating*/
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['add_close'] ) && isset( $_POST['add_close'] ) )
{
if( wp_verify_nonce( $_POST['edd_close_thread_nonce_field'], 'edd_close_thread' ) )
{
$table_name = $wpdb->prefix . 'edd_dashboard_message';
$meta_data = edd_get_payment_meta_cart_details( $_POST['purchase_id'], true );
$vendor_check = get_post_field( 'post_author', $meta_data[0]['id'] );
$vendor = intval( $_POST['edd_vendor'] );
$user_id = get_current_user_id();
$purchase_id = intval( $_POST['purchase_id'] );
$rating = floatval( $_POST['rating'] );
$message = wp_kses_post( $_POST['rate_message'] );
$wpdb->insert(
$table_name,
array(
'author_id' => $vendor,
'user_id' => $user_id,
'purchase_id' => $purchase_id,
'message' => $message,
'msg_time' => date( "Y-m-d H:i:s" ),
'attachment' => serialize( $attach ),
),
array(
'%d',
'%d',
'%d',
'%s',
'%s',
'%s',
)
);
$rating_arr = get_user_meta( $vendor, 'edd_user_order_rating', true );
$rating_arr[ $purchase_id ] = $rating;
update_user_meta( $vendor, 'edd_user_order_rating', $rating_arr );
update_post_meta( $purchase_id, 'edd_user_order_thread', 'close' );
}
}
/*End of second condition*/
}
}
/*Function to get all the conversation related to a single service id*/
if( !function_exists( 'get_conversation_by_pay_id' ) )
{
function get_conversation_by_pay_id( $id )
{
global $wpdb, $edd_receipt_args;
$cart = edd_get_payment_meta_cart_details( $id, true );
$message = get_post_meta( $cart[0]['id'], '_fes_edd_initial_message', true );
$table_name = $wpdb->prefix . 'edd_dashboard_message';
$msg = $wpdb->get_results( "SELECT * FROM $table_name WHERE purchase_id = $id ORDER BY msg_time DESC" );
$html ='<div class="edd-messages-list">';
/* Display initial message*/
if( $message && $message != "" && empty($msg) )
{
$post = get_post( $cart[0]['id'] );
$user_data = get_userdata( $post->post_author );
$html .= '<div class="each-edd-message">';
$html .= '<div class="each-edd-message-avatar">';
$html .= get_avatar( $post->post_author, '50').'<br><label class="name">' . $user_data->data->user_login.'</label>';
$html .= '</div>';
$html .= '<div class="each-edd-message-right-cont">';
$html .= '<div class="each-edd-message-msg">';
$html .= $message;
$html .= '</div>';
$html .= '</div>';
$html .= '<div style="clear:both;"></div>';
$html .= '</div>';
}
if( $msg )
{
foreach( $msg as $each )
{
if( $each->attachment!="" )
{
$filehtml = "";
$attach = unserialize( $each->attachment );
if( !empty( $attach ) )
{
foreach( $attach as $file )
{
$filehtml .= '<a href="'.wp_get_attachment_url($file).'" target="_blank">'.get_the_title($file).'</a>';
}
}
}
$user_data = get_userdata( $each->user_id );
$html .= '<div class="each-edd-message">';
$html .= '<div class="each-edd-message-avatar">';
$html .= get_avatar( $each->user_id, '50').'<br><label class="name">'.$user_data->data->user_login.'</label>';
$html .= '</div>';
$html .= '<div class="each-edd-message-right-cont">';
$html .= '<div class="each-edd-message-msg">';
$html .= $each->message;
$html .= '</div>';
$html .= '<div class="each-edd-message-attach"><div class="attach-head">' . __( 'Attachments', WBCOM_EDD_DASH_MSG_TEXT_DOMIAN ) . '</div>';
$html .= $filehtml;
$html .= '</div>';
$html .= '</div>';
$html .= '<div style="clear:both;"></div>';
$html .= '</div>';
}
if( $message && $message != "" )
{
$post = get_post( $cart[0]['id'] );
$user_data = get_userdata( $post->post_author );
$html .= '<div class="each-edd-message">';
$html .= '<div class="each-edd-message-avatar">';
$html .= get_avatar( $post->post_author, '50').'<br><label class="name">' . $user_data->data->user_login.'</label>';
$html .= '</div>';
$html .= '<div class="each-edd-message-right-cont">';
$html .= '<div class="each-edd-message-msg">';
$html .= $message;
$html .= '</div>';
$html .= '</div>';
$html .= '<div style="clear:both;"></div>';
$html .= '</div>';
}
$meta_data = edd_get_payment_meta_cart_details( $id, true );
$vendor = get_post_field( 'post_author', $meta_data[0]['id']);
$user_id = get_current_user_id();
$thread_status = get_post_meta( $id, 'edd_user_order_thread', true );
if( ($vendor != $user_id ) && ( $thread_status != 'close' ) )
{
$html .= '<div class="edd-close-thread-cont"> <a href="javascript:void();" class="edd-close-thread">Close Thread</a></div>';
$html .= '<div style="clear:both;"></div>';
$html .= '<div id="dialog-form">
<p class="validateTips">' . __( 'All form fields are required.', WBCOM_EDD_DASH_MSG_TEXT_DOMIAN ) . '</p>
<form method="post" action="">
<fieldset>
<label for="name">' . __( 'Message', WBCOM_EDD_DASH_MSG_TEXT_DOMIAN ) . '</label>';
ob_start();
$settings = array( 'media_buttons' => false, 'editor_height' => '150px', 'teeny' => true, 'quicktags' => false);
add_filter( 'teeny_mce_buttons', 'edd_message_editor_buttons', 10, 2 );
wp_editor( '', 'rate_message', $settings);
$html .= ob_get_clean();
$html .= '<label for="rating">' . __( 'Rating', WBCOM_EDD_DASH_MSG_TEXT_DOMIAN ) . '</label>
<div id="jRate" style="height:30px;"></div>
<input type="hidden" value="1" name="rating" id="rating">'. wp_nonce_field( 'edd_close_thread', 'edd_close_thread_nonce_field', false, false ) . '<input type="hidden" value="'.$vendor.'" name="edd_vendor"><input type="hidden" value="'.$id.'" name="purchase_id"><input type="submit" class="edd-close-thread" name="add_close" value="Close">
</fieldset>
</form>
</div>';
}
}
$html .= '</div>';
return $html;
}
}
add_action( 'init', 'edd_add_user_message' );
?>