-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-pixexif.php
236 lines (207 loc) · 7.51 KB
/
class-pixexif.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* PixTypes.
*
* @package PixEXIF
* @author pixelgrade <contact@pixelgrade.com>
* @license GPL-2.0+
* @link https://pixelgrade.com
* @copyright 2015 PixelGrade
*/
class PixExifPlugin {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.0.0
*
* @const string
*/
protected $version = '1.0.0';
/**
* Unique identifier for your plugin.
*
* Use this value (not the variable name) as the text domain when internationalizing strings of text. It should
* match the Text Domain file header in the main plugin file.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_slug = 'pixexif';
/**
* Instance of this class.
*
* @since 1.0.0
*
* @var object
*/
protected static $instance = null;
private $prefix = 'pix_exif_';
/**
* Fields to fetch from the EXIF data.
* This is not the complete list of fields. Just the ones that we felt were the most important
*/
protected $fields = array();
/**
* Path to the plugin.
*
* @since 1.0.0
* @var string
*/
protected $plugin_basepath = null;
/**
* Plain old constructor.
*/
public function __construct() {
$this->plugin_basepath = plugin_dir_path( __FILE__ );
$this->fields = array(
'camera' => array (
'label' => __( 'Camera', 'pixexif' ),
'description' => __( 'Camera model.', 'pixexif' ),
),
'aperture' => array(
'label' => __( 'Aperture', 'pixexif' ),
'description' => __( 'The aperture f-stop (only the number, without f/).', 'pixexif' ),
),
'focal_length' => array(
'label' => __( 'Focal Length', 'pixexif' ),
'description' => __( 'The focal length that the image was shot with (in mm).', 'pixexif' ),
),
'shutter_speed' => array(
'label' => __( 'Shutter Speed', 'pixexif' ),
'description' => __( 'The exposure time in fractional format (ie. 1/100).', 'pixexif' ),
),
'iso' => array(
'label' => __( 'ISO', 'pixexif' ),
'description' => __( 'The ISO speed.', 'pixexif' ),
),
);
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
$this->setupFilters();
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Setup filters to add fields and process them when saving the attachment changes.
*/
private function setupFilters() {
//register fields
add_filter( 'attachment_fields_to_edit', array($this, 'registerAttachmentFields'), 10, 2 );
//process the field values when the user hits Update
add_filter( 'attachment_fields_to_save', array($this, 'saveAttachmentFields'), 10, 2 );
}
/**
* Register the EXIF fields.
*
* @param array $form_fields Fields to add to the attachment form
* @param object $post Attachment post object
* @return array $form_fields Modified form fields
*/
public function registerAttachmentFields($form_fields, $post) {
// Check if this is actually an image.
// bail if not
if( ! wp_attachment_is_image( $post->ID ) ) {
return $form_fields;
}
//add a text field for each field in $fields
foreach( $this->fields as $fieldName => $value ) {
$form_fields[ $this->prefix . $fieldName ] = array(
'value' => $this->get_exif_meta( $post->ID, $fieldName ),
'label' => $value['label'],
'helps' => $value['description'],
);
//handle those special cases when we need to undo what WordPress does to the EXIF meta
switch ( $fieldName ) {
case 'shutter_speed':
$form_fields[ $this->prefix . $fieldName ]['value'] = timber_convert_exposure_to_frac( $form_fields[ $this->prefix . $fieldName ]['value'] );
break;
}
}
return $form_fields;
}
/**
* Update EXIF fields.
*
* @param array $post The post data
* @param array $attachment Attachment fields from the $_POST form
* @return array $post Modified post data
*/
public function saveAttachmentFields($post, $attachment) {
// Check if this is actually an image.
// bail if not
if( ! wp_attachment_is_image( $post['ID'] ) ) {
return $post;
}
//First read the current attachment metadata
$meta_data = wp_get_attachment_metadata( $post['ID'] );
foreach( $this->fields as $fieldName => $value ) {
//check existence just to be sure
//WordPress will create empty entries even if the EXIF entry in not present in the image EXIF metadata
if( isset( $attachment[ $this->prefix . $fieldName ] ) ) {
//depending on each field do what WordPress does to them - a sort of normalization
switch ( $fieldName ) {
case 'aperture':
$meta_data['image_meta'][$fieldName] = round( wp_exif_frac2dec( $attachment[ $this->prefix . $fieldName ] ), 2 );
break;
case 'focal_length':
case 'shutter_speed':
$meta_data['image_meta'][$fieldName] = (string) wp_exif_frac2dec( $attachment[ $this->prefix . $fieldName ] );
break;
default:
$meta_data['image_meta'][$fieldName] = trim( $attachment[ $this->prefix . $fieldName ] );
}
}
}
//save the modified EXIF data into the database
wp_update_attachment_metadata( $post['ID'], $meta_data );
return $post;
}
/**
* Read a certain EXIF field from the database
*
* @param int $attachment_ID The attachment ID
* @param string $field_name The EXIF field (as WordPress calls it)
* @return string EXIF field value or empty string
*/
private function get_exif_meta( $attachment_ID, $field_name ) {
$meta_data = wp_get_attachment_metadata( $attachment_ID );
if ( isset( $meta_data['image_meta'] ) ) {
if ( isset( $meta_data['image_meta'][$field_name] ) ) {
return $meta_data['image_meta'][$field_name];
}
}
return '';
}
/**
* Fired when the plugin is deactivated.
* @since 1.0.0
* @param boolean $network_wide True if WPMU superadmin uses "Network Deactivate" action, false if WPMU is disabled or plugin is deactivated on an individual blog.
*/
static function deactivate( $network_wide ) {
// TODO: Define deactivation functionality here
}
/**
* Load the plugin text domain for translation.
*
* @since 1.0.0
*/
function load_plugin_textdomain() {
$domain = $this->plugin_slug;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . '/lang/' );
}
} // class PixExifEditor