image.php
| 1 | <?php |
| 2 | |
| 3 | class acf_Image extends acf_Field |
| 4 | { |
| 5 | |
| 6 | /*-------------------------------------------------------------------------------------- |
| 7 | * |
| 8 | * Constructor |
| 9 | * |
| 10 | * @author Elliot Condon |
| 11 | * @since 1.0.0 |
| 12 | * @updated 2.2.0 |
| 13 | * |
| 14 | *-------------------------------------------------------------------------------------*/ |
| 15 | |
| 16 | function __construct($parent) |
| 17 | { |
| 18 | parent::__construct($parent); |
| 19 | |
| 20 | $this->name = 'image'; |
| 21 | $this->title = __('Image','acf'); |
| 22 | |
| 23 | add_action('admin_head-media-upload-popup', array($this, 'popup_head')); |
| 24 | add_filter('media_send_to_editor', array($this, 'media_send_to_editor'), 15, 2 ); |
| 25 | add_filter('get_media_item_args', array($this, 'allow_img_insertion')); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /*-------------------------------------------------------------------------------------- |
| 30 | * |
| 31 | * admin_print_scripts / admin_print_styles |
| 32 | * |
| 33 | * @author Elliot Condon |
| 34 | * @since 3.0.1 |
| 35 | * |
| 36 | *-------------------------------------------------------------------------------------*/ |
| 37 | |
| 38 | function allow_img_insertion($vars) |
| 39 | { |
| 40 | $vars['send'] = true; |
| 41 | return($vars); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /*-------------------------------------------------------------------------------------- |
| 46 | * |
| 47 | * admin_print_scripts / admin_print_styles |
| 48 | * |
| 49 | * @author Elliot Condon |
| 50 | * @since 3.0.0 |
| 51 | * |
| 52 | *-------------------------------------------------------------------------------------*/ |
| 53 | |
| 54 | function admin_print_scripts() |
| 55 | { |
| 56 | wp_enqueue_script(array( |
| 57 | 'jquery', |
| 58 | 'jquery-ui-core', |
| 59 | 'jquery-ui-tabs', |
| 60 | |
| 61 | 'thickbox', |
| 62 | 'media-upload', |
| 63 | )); |
| 64 | } |
| 65 | |
| 66 | function admin_print_styles() |
| 67 | { |
| 68 | wp_enqueue_style(array( |
| 69 | 'thickbox', |
| 70 | )); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /*-------------------------------------------------------------------------------------- |
| 75 | * |
| 76 | * admin_head |
| 77 | * |
| 78 | * @author Elliot Condon |
| 79 | * @since 2.0.6 |
| 80 | * |
| 81 | *-------------------------------------------------------------------------------------*/ |
| 82 | |
| 83 | function admin_head() |
| 84 | { |
| 85 | ?> |
| 86 | <script type="text/javascript"> |
| 87 | |
| 88 | (function($){ |
| 89 | |
| 90 | $('#poststuff .acf_image_uploader .button').live('click', function(){ |
| 91 | |
| 92 | // vars |
| 93 | var div = $(this).closest('.acf_image_uploader'); |
| 94 | var post_id = $('input#post_ID').val(); |
| 95 | var preview_size = div.attr('data-preview_size'); |
| 96 | |
| 97 | // set global var |
| 98 | window.acf_div = div; |
| 99 | |
| 100 | // show the thickbox |
| 101 | tb_show('Add Image to field', 'media-upload.php?post_id=' + post_id + '&type=image&acf_type=image&acf_preview_size=' + preview_size + 'TB_iframe=1'); |
| 102 | |
| 103 | return false; |
| 104 | }); |
| 105 | |
| 106 | $('#poststuff .acf_image_uploader .remove_image').live('click', function(){ |
| 107 | |
| 108 | // vars |
| 109 | var div = $(this).closest('.acf_image_uploader'); |
| 110 | |
| 111 | div.find('input.value').val(''); |
| 112 | div.removeClass('active'); |
| 113 | |
| 114 | return false; |
| 115 | |
| 116 | }); |
| 117 | |
| 118 | })(jQuery); |
| 119 | </script> |
| 120 | <?php |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /*-------------------------------------------------------------------------------------- |
| 125 | * |
| 126 | * create_field |
| 127 | * |
| 128 | * @author Elliot Condon |
| 129 | * @since 2.0.5 |
| 130 | * @updated 2.2.0 |
| 131 | * |
| 132 | *-------------------------------------------------------------------------------------*/ |
| 133 | |
| 134 | function create_field($field) |
| 135 | { |
| 136 | // vars |
| 137 | $class = ""; |
| 138 | $file_src = ""; |
| 139 | $preview_size = isset($field['preview_size']) ? $field['preview_size'] : 'medium'; |
| 140 | |
| 141 | // get image url |
| 142 | if($field['value'] != '' && is_numeric($field['value'])) |
| 143 | { |
| 144 | $file_src = wp_get_attachment_image_src($field['value'], $preview_size); |
| 145 | $file_src = $file_src[0]; |
| 146 | |
| 147 | if($file_src) $class = "active"; |
| 148 | } |
| 149 | |
| 150 | // html |
| 151 | echo '<div class="acf_image_uploader ' . $class . '" data-preview_size="' . $preview_size . '">'; |
| 152 | echo '<a href="#" class="remove_image"></a>'; |
| 153 | echo '<img src="' . $file_src . '" alt=""/>'; |
| 154 | echo '<input class="value" type="hidden" name="' . $field['name'] . '" value="' . $field['value'] . '" />'; |
| 155 | echo '<p>'.__('No image selected','acf').'. <input type="button" class="button" value="'.__('Add Image','acf').'" /></p>'; |
| 156 | echo '</div>'; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /*-------------------------------------------------------------------------------------- |
| 161 | * |
| 162 | * create_options |
| 163 | * |
| 164 | * @author Elliot Condon |
| 165 | * @since 2.0.6 |
| 166 | * @updated 2.2.0 |
| 167 | * |
| 168 | *-------------------------------------------------------------------------------------*/ |
| 169 | |
| 170 | function create_options($key, $field) |
| 171 | { |
| 172 | // vars |
| 173 | $field['save_format'] = isset($field['save_format']) ? $field['save_format'] : 'url'; |
| 174 | $field['preview_size'] = isset($field['preview_size']) ? $field['preview_size'] : 'thumbnail'; |
| 175 | |
| 176 | ?> |
| 177 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 178 | <td class="label"> |
| 179 | <label><?php _e("Return Value",'acf'); ?></label> |
| 180 | </td> |
| 181 | <td> |
| 182 | <?php |
| 183 | $this->parent->create_field(array( |
| 184 | 'type' => 'radio', |
| 185 | 'name' => 'fields['.$key.'][save_format]', |
| 186 | 'value' => $field['save_format'], |
| 187 | 'layout' => 'horizontal', |
| 188 | 'choices' => array( |
| 189 | 'url' => 'Image URL', |
| 190 | 'id' => 'Attachment ID' |
| 191 | ) |
| 192 | )); |
| 193 | ?> |
| 194 | </td> |
| 195 | </tr> |
| 196 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 197 | <td class="label"> |
| 198 | <label><?php _e("Preview Size",'acf'); ?></label> |
| 199 | </td> |
| 200 | <td> |
| 201 | <?php |
| 202 | $this->parent->create_field(array( |
| 203 | 'type' => 'radio', |
| 204 | 'name' => 'fields['.$key.'][preview_size]', |
| 205 | 'value' => $field['preview_size'], |
| 206 | 'layout' => 'horizontal', |
| 207 | 'choices' => array( |
| 208 | 'thumbnail' => 'Thumbnail', |
| 209 | 'medium' => 'Medium', |
| 210 | 'large' => 'Large', |
| 211 | 'full' => 'Full' |
| 212 | ) |
| 213 | )); |
| 214 | ?> |
| 215 | </td> |
| 216 | </tr> |
| 217 | |
| 218 | <?php |
| 219 | } |
| 220 | |
| 221 | |
| 222 | |
| 223 | /*--------------------------------------------------------------------------------------------- |
| 224 | * popup_head - STYLES MEDIA THICKBOX |
| 225 | * |
| 226 | * @author Elliot Condon |
| 227 | * @since 1.1.4 |
| 228 | * |
| 229 | ---------------------------------------------------------------------------------------------*/ |
| 230 | function popup_head() |
| 231 | { |
| 232 | if(isset($_GET["acf_type"]) && $_GET['acf_type'] == 'image') |
| 233 | { |
| 234 | $preview_size = isset($arr_postinfo['preview_size']) ? $arr_postinfo['preview_size'] : 'medium'; |
| 235 | |
| 236 | ?> |
| 237 | <style type="text/css"> |
| 238 | #media-upload-header #sidemenu li#tab-type_url, |
| 239 | #media-upload-header #sidemenu li#tab-gallery { |
| 240 | display: none; |
| 241 | } |
| 242 | |
| 243 | #media-items tr.url, |
| 244 | #media-items tr.align, |
| 245 | #media-items tr.image_alt, |
| 246 | #media-items tr.image-size, |
| 247 | #media-items tr.post_excerpt, |
| 248 | #media-items tr.post_content, |
| 249 | #media-items tr.image_alt p, |
| 250 | #media-items table thead input.button, |
| 251 | #media-items table thead img.imgedit-wait-spin, |
| 252 | #media-items tr.submit a.wp-post-thumbnail { |
| 253 | display: none; |
| 254 | } |
| 255 | |
| 256 | .media-item table thead img { |
| 257 | border: #DFDFDF solid 1px; |
| 258 | margin-right: 10px; |
| 259 | } |
| 260 | |
| 261 | </style> |
| 262 | <script type="text/javascript"> |
| 263 | (function($){ |
| 264 | |
| 265 | $(document).ready(function(){ |
| 266 | |
| 267 | $('#media-items').bind('DOMNodeInserted',function(){ |
| 268 | $('input[value="Insert into Post"]').each(function(){ |
| 269 | $(this).attr('value','<?php _e("Select Image",'acf'); ?>'); |
| 270 | }); |
| 271 | }).trigger('DOMNodeInserted'); |
| 272 | |
| 273 | $('form#filter').each(function(){ |
| 274 | |
| 275 | $(this).append('<input type="hidden" name="acf_preview_size" value="<?php echo $preview_size; ?>" />'); |
| 276 | $(this).append('<input type="hidden" name="acf_type" value="image" />'); |
| 277 | |
| 278 | }); |
| 279 | }); |
| 280 | |
| 281 | })(jQuery); |
| 282 | </script> |
| 283 | <?php |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /*--------------------------------------------------------------------------------------------- |
| 289 | * media_send_to_editor - SEND IMAGE TO ACF DIV |
| 290 | * |
| 291 | * @author Elliot Condon |
| 292 | * @since 1.1.4 |
| 293 | * |
| 294 | ---------------------------------------------------------------------------------------------*/ |
| 295 | function media_send_to_editor($html, $id) |
| 296 | { |
| 297 | parse_str($_POST["_wp_http_referer"], $arr_postinfo); |
| 298 | |
| 299 | if(isset($arr_postinfo["acf_type"]) && $arr_postinfo["acf_type"] == "image") |
| 300 | { |
| 301 | |
| 302 | $preview_size = isset($arr_postinfo['acf_preview_size']) ? $arr_postinfo['acf_preview_size'] : 'medium'; |
| 303 | |
| 304 | $file_src = wp_get_attachment_image_src($id, $preview_size); |
| 305 | $file_src = $file_src[0]; |
| 306 | |
| 307 | ?> |
| 308 | <script type="text/javascript"> |
| 309 | |
| 310 | self.parent.acf_div.find('input.value').val('<?php echo $id; ?>'); |
| 311 | self.parent.acf_div.find('img').attr('src','<?php echo $file_src; ?>'); |
| 312 | self.parent.acf_div.addClass('active'); |
| 313 | |
| 314 | // reset acf_div and return false |
| 315 | self.parent.acf_div = null; |
| 316 | self.parent.tb_remove(); |
| 317 | |
| 318 | </script> |
| 319 | <?php |
| 320 | exit; |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | return $html; |
| 325 | } |
| 326 | |
| 327 | } |
| 328 | |
| 329 | |
| 330 | /*-------------------------------------------------------------------------------------- |
| 331 | * |
| 332 | * get_value_for_api |
| 333 | * |
| 334 | * @author Elliot Condon |
| 335 | * @since 3.0.0 |
| 336 | * |
| 337 | *-------------------------------------------------------------------------------------*/ |
| 338 | |
| 339 | function get_value_for_api($post_id, $field) |
| 340 | { |
| 341 | // vars |
| 342 | $format = isset($field['save_format']) ? $field['save_format'] : 'url'; |
| 343 | |
| 344 | $value = parent::get_value($post_id, $field); |
| 345 | |
| 346 | if($format == 'url') |
| 347 | { |
| 348 | $value = wp_get_attachment_url($value); |
| 349 | } |
| 350 | |
| 351 | return $value; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | |
| 356 | } |
| 357 | |
| 358 | ?> |