Repository navigation
Expand file tree
/
Copy pathicit_image.php
More file actions
170 lines (146 loc) · 4.66 KB
/
Copy pathicit_image.php
File metadata and controls
170 lines (146 loc) · 4.66 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
<?php
/**
* Returns an ICIT_Image object representing a kitten at the requested size
*
* @param array $size An array containing the dimensions of the desired image, e.g. array(200,300) for a 200x300 image
*
* @example echo icit_kittens(100x200)->getURL(); echos the URL to a kitten image ready for frontend display
*
* @return ICIT_Image An image object of a kitten
*/
if ( !function_exists( 'icit_kittens' ) ) {
function icit_kittens($size){
if(is_array($size)){
return new ICIT_Image('http://placekitten.com/g/'.$size[0].'/'.$size[1]);
}
}
//add_filter( 'attachment_fields_to_edit', 'metametameta', 10, 2 );
}
function metametameta( $form_fields, $post ){
$metadata = get_metadata('post',$post->ID);
$form_fields['metadata'] = array(
'label' => 'metadata',
'input' => 'html',
'html' => '<pre>'.print_r($metadata,true).'</pre>'
);
return $form_fields;
}
if(!class_exists('ICIT_Image')){
/**
* Represents an image/attachment, has methods for acquiring images from alternate sources
*/
class ICIT_Image {
public $ID = 0;
public $error = false;
public function __construct($image_id=0,$parent=0){
if(is_numeric($image_id)){
// we have an attachment ID! fill in all the gaps and be thankful
// no heavy lifting is required
// error_log('NUMERICS!!!');
} else if(is_string($image_id)){
// we have a URL! Prepare for a HTTP request and the creation of
// a new attachment, do a check to see if we've already grabbed
// this image first though
if(!$this->hasDownloaded($image_id)){
$this->downloadImage($image_id,$parent);
}
}
}
public function is_valid(){
return ($this->ID != 0);
}
protected function hasDownloaded($url){
$q = new WP_Query(array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'original-source',
'value' => $url,
'compare' => '='
),
array(
'key' => 'icit_original_url',
'value' => $url,
'compare' => '='
)
)
));
if($q->have_posts()){
while($q->have_posts()){
$q->the_post();
$this->ID = get_the_ID();
wp_reset_postdata();
return true;
}
wp_reset_postdata();
}
return false;
}
public function attachTo($post_id){
//
}
public function getURL($size=''){
if(empty($size)){
return wp_get_attachment_url( $this->ID );
}
}
/**
* Download an image from the specified URL and attach it to a post.
*
* @since 2.6.0
*
* @param string $file The URL of the image to download
* @param int $post_id The post ID the media is to be associated with
* @param string $desc Optional. Description of the image
* @return string|WP_Error Populated HTML img tag on success
*/
protected function downloadImage($file, $post_id, $desc = null) {
$id = 0;
if ( ! empty($file) ) {
$original_file = $file;
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Download file to temp location
//$file = urlencode($file);
// $file = rawurlencode ($file);
$file = str_replace(' ', '%20', $file);
$tmp = download_url( $file );
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
$tmp->add(8788,'Issue with image at: '.$file.' originally '.$original_file, array($file,$original_file));
$this->error = $tmp;
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
return 0;
}
// fix file filename
$file = preg_replace( array( '/[^a-zA-Z0-9\.-_]/', '/_+/' ), '_', $file );
preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
if(empty($matches)){
$file_array['name'] = md5($file).'.jpg';
} else {
$file_array['name'] = basename($matches[0]);
}
$file_array['tmp_name'] = $tmp;
// do the validation and storage stuff
$id = media_handle_sideload( $file_array, $post_id, $desc );
// If error storing permanently, unlink
if ( is_wp_error($id) ) {
$this->ID = 0;
$id->add(8789,'Issue with image at: '.$file.' originally '.$original_file, array($file,$original_file));
$this->error = $id;
@unlink($file_array['tmp_name']);
return $id;
}
add_post_meta($id,'original-source',$original_file);
//$src = wp_get_attachment_url( $id );
}
$this->ID = $id;
return $id;
}
}
}