PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.2.0
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.2.0
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / class-ajax-upload.php

class-ajax-upload.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.2.0, at includes/class-ajax-upload.php

171 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Attachment Uploader class
5 *
6 * @since 1.1.0
7 */
8 class WeForms_Ajax_Upload {
9
10 function __construct() {
11
12 // let WPUF handle the upload if installed
13 if ( class_exists( 'WPUF_Upload' ) ) {
14 return;
15 }
16
17 add_action( 'wp_ajax_wpuf_file_upload', array($this, 'upload_file') );
18 add_action( 'wp_ajax_nopriv_wpuf_file_upload', array($this, 'upload_file') );
19
20 add_action( 'wp_ajax_wpuf_file_del', array($this, 'delete_file') );
21 add_action( 'wp_ajax_nopriv_wpuf_file_del', array($this, 'delete_file') );
22 }
23
24 /**
25 * Validate if it's coming from WordPress with a valid nonce
26 *
27 * @return void
28 */
29 function validate_nonce() {
30 $nonce = isset( $_GET['nonce'] ) ? $_GET['nonce'] : '';
31
32 if ( ! wp_verify_nonce( $nonce, 'wpuf-upload-nonce' ) ) {
33 die( 'error' );
34 }
35 }
36
37 /**
38 * Upload a file
39 *
40 * @param boolean $image_only
41 *
42 * @return string
43 */
44 function upload_file( $image_only = false ) {
45 $this->validate_nonce();
46
47 // a valid request will have a form ID
48 $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : false;
49
50 if ( ! $form_id ) {
51 die( 'error' );
52 }
53
54 $upload = array(
55 'name' => $_FILES['wpuf_file']['name'],
56 'type' => $_FILES['wpuf_file']['type'],
57 'tmp_name' => $_FILES['wpuf_file']['tmp_name'],
58 'error' => $_FILES['wpuf_file']['error'],
59 'size' => $_FILES['wpuf_file']['size']
60 );
61
62 header('Content-Type: text/html; charset=' . get_option('blog_charset'));
63
64 $attach = $this->handle_upload( $upload );
65
66 if ( $attach['success'] ) {
67
68 $response = array( 'success' => true );
69 $response['html'] = $this->attach_html( $attach['attach_id'] );
70
71 echo $response['html'];
72 } else {
73 echo 'error';
74 }
75
76 exit;
77 }
78
79 /**
80 * Generic function to upload a file
81 *
82 * @param string $field_name file input field name
83 *
84 * @return bool|int attachment id on success, bool false instead
85 */
86 function handle_upload( $upload_data ) {
87
88 $uploaded_file = wp_handle_upload( $upload_data, array('test_form' => false) );
89
90 // If the wp_handle_upload call returned a local path for the image
91 if ( isset( $uploaded_file['file'] ) ) {
92 $file_loc = $uploaded_file['file'];
93 $file_name = basename( $upload_data['name'] );
94 $file_type = wp_check_filetype( $file_name );
95
96 $attachment = array(
97 'post_mime_type' => $file_type['type'],
98 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ),
99 'post_content' => '',
100 'post_status' => 'inherit'
101 );
102
103 $attach_id = wp_insert_attachment( $attachment, $file_loc );
104 $attach_data = wp_generate_attachment_metadata( $attach_id, $file_loc );
105
106 wp_update_attachment_metadata( $attach_id, $attach_data );
107
108 return array('success' => true, 'attach_id' => $attach_id);
109 }
110
111 return array('success' => false, 'error' => $uploaded_file['error']);
112 }
113
114 /**
115 * Image attachment response
116 *
117 * @param integer $attach_id
118 * @param string $type
119 *
120 * @return string
121 */
122 public static function attach_html( $attach_id, $type = NULL ) {
123 if ( ! $type ) {
124 $type = isset( $_GET['type'] ) ? $_GET['type'] : 'image';
125 }
126
127 $attachment = get_post( $attach_id );
128
129 if ( ! $attachment ) {
130 return;
131 }
132
133 if ( wp_attachment_is_image( $attach_id ) ) {
134 $image = wp_get_attachment_image_src( $attach_id, 'thumbnail' );
135 $image = $image[0];
136 } else {
137 $image = wp_mime_type_icon( $attach_id );
138 }
139
140 $html = '<li class="wpuf-image-wrap thumbnail">';
141 $html .= sprintf( '<div class="attachment-name"><img src="%s" alt="%s" /></div>', $image, esc_attr( $attachment->post_title ) );
142
143 $html .= sprintf( '<input type="hidden" name="wpuf_files[%s][]" value="%d">', $type, $attach_id );
144 $html .= sprintf( '<div class="caption"><a href="#" class="btn btn-danger btn-small attachment-delete" data-attach_id="%d">%s</a></div>', $attach_id, __( 'Delete', 'wpuf' ) );
145 $html .= '</li>';
146
147 return $html;
148 }
149
150 /**
151 * Delete a file
152 *
153 * @return void
154 */
155 function delete_file() {
156 check_ajax_referer( 'wpuf_nonce', 'nonce' );
157
158 $attach_id = isset( $_POST['attach_id'] ) ? intval( $_POST['attach_id'] ) : 0;
159 $attachment = get_post( $attach_id );
160
161 //post author or editor role
162 if ( get_current_user_id() == $attachment->post_author || current_user_can( 'delete_private_pages' ) ) {
163 wp_delete_attachment( $attach_id, true );
164 }
165
166 echo 'success';
167 exit;
168 }
169
170 }
171