media.php
4.12 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
<?php
/**
* @Description : Process media library
* @Package : Drag & Drop Multiple File Upload - Contact Form 7
* @Author : CodeDropz
*/
if ( ! defined( 'ABSPATH' ) || ! defined('dnd_upload_cf7') || ! defined('dnd_upload_cf7_PRO') ) {
exit;
}
class CodeDropz_Uploader_Media {
private static $instance = null;
public $media_files = array();
public $helper = null;
public static function get_instance() {
if( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct(){
$this->helper = CodeDropz_Uploader_Helpers::get_instance();
}
/**
* Upload File and Save to Media Libray
*/
public function save( $files = array(), $name = null ) {
// Response status
$response = array();
// Required wordpress file and image library
if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
}
// Check if $_Files is not empty
if( empty( $files ) ) {
$response['error'] = dnmfu_cf7_option('drag_n_drop_error_failed_to_upload') ? dnmfu_cf7_option('drag_n_drop_error_failed_to_upload') : $this->helper->get_error('failed_upload');
}
// Get file name and mimetype
$filename = $files;
$filetype = wp_check_filetype( basename( $filename ), null );
$thumbnail = pathinfo( $filename );
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $filename,
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', wp_basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attachment_id = wp_insert_attachment( $attachment, $filename, 0, true );
// // Check status of upload if it's success or not
if ( ! is_wp_error( $attachment_id ) ) {
// Generate metadata for attachment
$attach_data = wp_generate_attachment_metadata( $attachment_id, $filename );
// Update database record for meta data information
wp_update_attachment_metadata( $attachment_id, $attach_data );
// get attachment meta data
$attachment = wp_get_attachment_url( $attachment_id );
// Assign response
$response['file'] = $attachment;
// Store media files
if( $name ){
$this->media_files[ $name ][] = $attachment;
}else{
$this->media_files[] = $attachment;
}
// Covert base_url to base_dir
$file_path = $thumbnail['dirname'] .'/'. $thumbnail['filename'] .'-100x100.' . strtolower( $thumbnail['extension'] );
// Update post_meta
update_post_meta( $attachment_id, '_dndmfu_cf7_file_upload', 'yes' );
// Remove files & thumbnail
if( file_exists( $file_path ) ) {
$this->helper->delete_files( $file_path );
}
// Allow theme/plugins to hook
do_action('dndmfu_cf7_after_media_saved', $attachment_id );
}else {
$response['error']= ( $attachment_id->get_error_message() ? $attachment_id->get_error_message() : get_option('drag_n_drop_error_failed_to_upload') );
}
return $response;
}
/**
* Get media files
*/
public function get_media_files(){
return $this->media_files;
}
/**
* Reset media files
*/
public function reset() {
$this->media_files = array();
}
}