-
Notifications
You must be signed in to change notification settings - Fork 7
/
gf-notification-attachment.php
203 lines (177 loc) · 6.97 KB
/
gf-notification-attachment.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
<?php
/*
Plugin Name: Gravity Forms: Notification Attachments
Plugin URI: http://codearachnid.github.io/gf-notification-attachment/
Description: An addon for Gravity Forms to add attachments to notification emails
Version: 1.5
Author: Timothy Wood (@codearachnid)
Author URI: http://codearachnid.com
Text Domain: gravity-forms-notification-attachments
*/
global $gf_notification_attachment;
//Loads translation
load_plugin_textdomain('gravity-forms-notification-attachments', false, dirname( plugin_basename( __FILE__ ) ). '/languages/');
add_action( 'init', 'gf_notification_attachment_init' );
/**
* [gf_notification_attachment_init description]
* @return object
*/
function gf_notification_attachment_init(){
global $gf_notification_attachment;
if( class_exists('GFForms') ) {
add_filter( 'gform_noconflict_scripts', 'gf_notification_attachment_gform_noconflict' );
add_filter( 'gform_notification', 'gf_notification_attachment_send', 20, 3 );
add_filter( 'gform_pre_notification_save', 'gf_notification_attachment_save', 20, 2 );
add_filter( 'gform_notification_ui_settings', 'gf_notification_attachment_editor', 20, 3 );
add_action( 'admin_enqueue_scripts', 'gf_notification_attachment_attach_script');
add_action( 'wp_ajax_gf_notification_attachment', 'gf_notification_attachment_ajax' );
$gf_notification_attachment = (object) array(
'text_domain' => 'gf-notification-attachment',
'version' => '1.0',
'plugin_url' => trailingslashit( plugin_dir_url( __FILE__ ) )
);
return $gf_notification_attachment;
} else {
add_action( 'admin_notices', 'gf_notification_attachment_admin_notices' );
}
}
function gf_notification_attachment_admin_notices(){
?>
<div class="error">
<p><?php _e( 'You must have Gravity Forms activated in order to use Notification Attachments.', 'gravity-forms-notification-attachments' ); ?></p>
</div>
<?php
}
function gf_notification_attachment_ajax(){
$attachment_id = !empty( $_REQUEST['attachment_id'] ) ? $_REQUEST['attachment_id'] : 0;
$attachment = gf_notification_attachment_get_meta( $attachment_id );
$response = array(
'attachment_id' => $attachment_id,
'success' => empty($attachment) ? false : true,
'data' => $attachment
);
echo json_encode( $response );
die;
}
/**
* [gf_notification_attachment_send description]
* @param array $notification
* @param array $form
* @param array $lead
* @return array
*/
function gf_notification_attachment_send( $notification, $form, $lead ){
$attachment_id_raw = esc_attr( rgar( $notification, "attachment_id" ) );
$attachment_ids = (array) json_decode( $attachment_id_raw );
$wp_upload_dir = wp_upload_dir();
if( !empty( $attachment_ids ) ) {
foreach( $attachment_ids as $attachment_id ){
$attachment = wp_get_attachment_url( $attachment_id );
if( !empty( $attachment ) ){
$notification['attachments'][] = str_replace( $wp_upload_dir['baseurl'], $wp_upload_dir['basedir'], $attachment );
}
}
}
return $notification;
}
/**
* [gf_notification_attachment_save description]
* @param array $notification
* @param array $form
* @return array
*/
function gf_notification_attachment_save( $notification, $form ){
if( function_exists( 'rgpost' ) )
$notification["attachment_id"] = rgpost("gform_notification_attachment_id");
return $notification;
}
/**
* [gf_notification_attachment_editor description]
* @param array $ui_settings
* @param array $notification
* @param array $form
* @return array
*/
function gf_notification_attachment_editor( $ui_settings, $notification, $form ){
if( !empty($ui_settings['notification_message']) ){
$attachment_id_raw = esc_attr( rgar( $notification, "attachment_id" ) );
$attachment_ids = (array) json_decode( $attachment_id_raw );
ob_start();
?>
<tr valign="top">
<th scope="row">
<label for="gform_notification_attachment">
<?php _e("Attachments", "gravity-forms-notification-attachments"); ?>
</label>
</th>
<td id="gform_notification_attachment">
<ul class="details">
<?php foreach( $attachment_ids as $attachment_id ) : $attachment = gf_notification_attachment_get_meta( $attachment_id ); ?>
<li data-id="<?php echo $attachment_id; ?>">
<div class="remove dashicons dashicons-dismiss"></div>
<img src="<?php echo $attachment->mime_file; ?>" class="fl" />
<div class="fl file-details">
<span class="title"><?php echo $attachment->title; ?></span>
<span class="mime">[<?php echo $attachment->mime; ?>]</span>
</div>
<br class="clear">
</li>
<?php endforeach; ?>
</ul>
<input type="hidden" name="gform_notification_attachment_id" id="gform_notification_attachment_id" class="attachment_ids" value="<?php echo $attachment_id_raw; ?>" />
<a href="#" class="button add gform_notification_attachment" title="<?php _e('Add Attachment', 'gravity-forms-notification-attachments'); ?>">
<?php _e('Add Attachment', 'gravity-forms-notification-attachments'); ?>
</a>
</td>
</tr> <!-- / notification attachment -->
<?php
$ui_settings['notification_message'] .= ob_get_clean();
}
return $ui_settings;
}
/**
* [gf_notification_attachment_get_meta description]
* @param int $attachment_id
* @return obj
*/
function gf_notification_attachment_get_meta( $attachment_id ) {
$attachment = get_post( $attachment_id );
$image = wp_get_attachment_image_src( $attachment_id, array(100,100), true );
$image = !empty( $image ) ? $image[0] : null;
if( is_null( $image ) )
$image = wp_mime_type_icon( $attachment->post_mime_type );
return (object) apply_filters( 'gf_notification_attachment_get_meta', array(
'id' => $attachment_id,
'mime_file' => $image,
'mime' => $attachment->post_mime_type,
'title' => $attachment->post_title
), $attachment_id, $attachment );
}
/**
* [gf_notification_attachment_attach_script description]
* @return null
*/
function gf_notification_attachment_attach_script(){
global $gf_notification_attachment;
$plugin = $gf_notification_attachment;
if( class_exists( 'GFForms' ) ) {
if( GFForms::get_page() == 'notification_edit'){
$script = $plugin->plugin_url . 'script';
$script .= ( WP_DEBUG ) ? '.js' : '.min.js';
wp_enqueue_script( $plugin->text_domain, $script, array( 'gform_gravityforms' ), $plugin->version, true );
wp_enqueue_style( $plugin->text_domain, $plugin->plugin_url . 'style.css', array(), $plugin->version );
}
}
}
/**
* [gf_notification_attachment_gform_noconflict description]
* prevent Gravity Forms from being greedy to remove our scripts in no conflict mode
*
* @return array
*/
function gf_notification_attachment_gform_noconflict( $allowed_script_keys ){
global $gf_notification_attachment;
$plugin = $gf_notification_attachment;
$allowed_script_keys[] = $plugin->text_domain;
return $allowed_script_keys;
}