|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Make some GFFormsModel public available. |
|
4
|
|
|
* @since 1.16.2 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class GravityView_GFFormsModel extends GFFormsModel { |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Copied function from Gravity Forms plugin \GFFormsModel::copy_post_image since the method is private. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 1.16.2 |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $url URL of the post image to update |
|
16
|
|
|
* @param int $post_id ID of the post image to update |
|
17
|
|
|
* @return array|bool Array with `file`, `url` and `type` keys. False: failed to copy file to final directory path. |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function copy_post_image( $url, $post_id ) { |
|
20
|
|
|
|
|
21
|
|
|
$reflection = new ReflectionMethod( 'GFFormsModel', 'copy_post_image' ); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* If the method changes to public, use Gravity Forms' method |
|
25
|
|
|
* @todo: If/when the method is public, remove the unneeded copied code. |
|
26
|
|
|
*/ |
|
27
|
|
|
if( $reflection->isPublic() ) { |
|
|
|
|
|
|
28
|
|
|
return parent::copy_post_image( $url, $post_id ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Original Gravity Forms code below: |
|
33
|
|
|
* ================================== |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
$time = current_time( 'mysql' ); |
|
37
|
|
|
|
|
38
|
|
|
if ( $post = get_post( $post_id ) ) { |
|
39
|
|
|
if ( substr( $post->post_date, 0, 4 ) > 0 ) { |
|
40
|
|
|
$time = $post->post_date; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
//making sure there is a valid upload folder |
|
45
|
|
|
if ( ! ( ( $upload_dir = wp_upload_dir( $time ) ) && false === $upload_dir['error'] ) ) { |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$form_id = get_post_meta( $post_id, '_gform-form-id', true ); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Filter the media upload location. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $upload_dir The current upload directory’s path and url. |
|
55
|
|
|
* @param int $form_id The ID of the form currently being processed. |
|
56
|
|
|
* @param int $post_id The ID of the post created from the entry currently being processed. |
|
57
|
|
|
*/ |
|
58
|
|
|
$upload_dir = gf_apply_filters( 'gform_media_upload_path', $form_id, $upload_dir, $form_id, $post_id ); |
|
59
|
|
|
|
|
60
|
|
|
if ( ! file_exists( $upload_dir['path'] ) ) { |
|
61
|
|
|
if ( ! wp_mkdir_p( $upload_dir['path'] ) ) { |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$name = basename( $url ); |
|
67
|
|
|
$filename = wp_unique_filename( $upload_dir['path'], $name ); |
|
68
|
|
|
|
|
69
|
|
|
// the destination path |
|
70
|
|
|
$new_file = $upload_dir['path'] . "/$filename"; |
|
71
|
|
|
|
|
72
|
|
|
// the source path |
|
73
|
|
|
$y = substr( $time, 0, 4 ); |
|
74
|
|
|
$m = substr( $time, 5, 2 ); |
|
75
|
|
|
$target_root = RGFormsModel::get_upload_path( $form_id ) . "/$y/$m/"; |
|
76
|
|
|
$target_root_url = RGFormsModel::get_upload_url( $form_id ) . "/$y/$m/"; |
|
77
|
|
|
$upload_root_info = array( 'path' => $target_root, 'url' => $target_root_url ); |
|
78
|
|
|
$upload_root_info = gf_apply_filters( 'gform_upload_path', $form_id, $upload_root_info, $form_id ); |
|
79
|
|
|
$path = str_replace( $upload_root_info['url'], $upload_root_info['path'], $url ); |
|
80
|
|
|
|
|
81
|
|
|
// copy the file to the destination path |
|
82
|
|
|
if ( ! copy( $path, $new_file ) ) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Set correct file permissions |
|
87
|
|
|
$stat = stat( dirname( $new_file ) ); |
|
88
|
|
|
$perms = $stat['mode'] & 0000666; |
|
89
|
|
|
@ chmod( $new_file, $perms ); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
// Compute the URL |
|
92
|
|
|
$url = $upload_dir['url'] . "/$filename"; |
|
93
|
|
|
|
|
94
|
|
|
if ( is_multisite() ) { |
|
95
|
|
|
delete_transient( 'dirsize_cache' ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$type = wp_check_filetype( $new_file ); |
|
99
|
|
|
|
|
100
|
|
|
return array( 'file' => $new_file, 'url' => $url, 'type' => $type['type'] ); |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Copied function from Gravity Forms plugin \GFFormsModel::media_handle_upload since the method is private. |
|
106
|
|
|
* |
|
107
|
|
|
* @see GFFormsModel::media_handle_upload |
|
108
|
|
|
* @see GravityView_Edit_Entry_Render::maybe_update_post_fields |
|
109
|
|
|
* |
|
110
|
|
|
* @uses copy_post_image |
|
111
|
|
|
* @uses wp_insert_attachment |
|
112
|
|
|
* @uses wp_update_attachment_metadata |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $url URL of the post image to update |
|
115
|
|
|
* @param int $post_id ID of the post image to update |
|
116
|
|
|
* @param array $post_data Array of data for the eventual attachment post type that is created using {@see wp_insert_attachment}. Supports `post_mime_type`, `guid`, `post_parent`, `post_title`, `post_content` keys. |
|
117
|
|
|
* @return bool|int ID of attachment Post created. Returns false if file not created by copy_post_image |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function media_handle_upload( $url, $post_id, $post_data = array() ) { |
|
120
|
|
|
|
|
121
|
|
|
$reflection = new ReflectionMethod( 'GFFormsModel', 'media_handle_upload' ); |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* If the method changes to public, use Gravity Forms' method |
|
125
|
|
|
* @todo: If/when the method is public, remove the unneeded copied code. |
|
126
|
|
|
*/ |
|
127
|
|
|
if( $reflection->isPublic() ) { |
|
|
|
|
|
|
128
|
|
|
return parent::media_handle_upload( $url, $post_id, $post_data ); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Original Gravity Forms code below: |
|
133
|
|
|
* ================================== |
|
134
|
|
|
*/ |
|
135
|
|
|
|
|
136
|
|
|
//WordPress Administration API required for the media_handle_upload() function |
|
137
|
|
|
require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
138
|
|
|
|
|
139
|
|
|
$name = basename( $url ); |
|
140
|
|
|
|
|
141
|
|
|
$file = self::copy_post_image( $url, $post_id ); |
|
142
|
|
|
|
|
143
|
|
|
if ( ! $file ) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$name_parts = pathinfo( $name ); |
|
148
|
|
|
$name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) ); |
|
149
|
|
|
|
|
150
|
|
|
$url = $file['url']; |
|
151
|
|
|
$type = $file['type']; |
|
152
|
|
|
$file = $file['file']; |
|
153
|
|
|
$title = $name; |
|
154
|
|
|
$content = ''; |
|
155
|
|
|
|
|
156
|
|
|
// use image exif/iptc data for title and caption defaults if possible |
|
157
|
|
|
if ( $image_meta = @wp_read_image_metadata( $file ) ) { |
|
|
|
|
|
|
158
|
|
|
if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { |
|
159
|
|
|
$title = $image_meta['title']; |
|
160
|
|
|
} |
|
161
|
|
|
if ( trim( $image_meta['caption'] ) ) { |
|
162
|
|
|
$content = $image_meta['caption']; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Construct the attachment array |
|
167
|
|
|
$attachment = array_merge( |
|
168
|
|
|
array( |
|
169
|
|
|
'post_mime_type' => $type, |
|
170
|
|
|
'guid' => $url, |
|
171
|
|
|
'post_parent' => $post_id, |
|
172
|
|
|
'post_title' => $title, |
|
173
|
|
|
'post_content' => $content, |
|
174
|
|
|
), $post_data |
|
175
|
|
|
); |
|
176
|
|
|
|
|
177
|
|
|
// Save the data |
|
178
|
|
|
$id = wp_insert_attachment( $attachment, $file, $post_id ); |
|
179
|
|
|
if ( ! is_wp_error( $id ) ) { |
|
180
|
|
|
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $id; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
} |