| 1 |
<?php |
| 2 |
|
| 3 |
class acf_File |
| 4 |
{ |
| 5 |
var $name; |
| 6 |
var $title; |
| 7 |
var $parent; |
| 8 |
|
| 9 |
function acf_File($parent) |
| 10 |
{ |
| 11 |
$this->name = 'file'; |
| 12 |
$this->title = __('File','acf'); |
| 13 |
$this->parent = $parent; |
| 14 |
|
| 15 |
add_action("admin_head-media-upload-popup", array($this, 'popup_head')); |
| 16 |
add_filter('media_send_to_editor', array($this, 'media_send_to_editor'), 15, 2 ); |
| 17 |
//add_action('admin_init', array($this, 'admin_init')); |
| 18 |
|
| 19 |
add_action('admin_print_scripts', array($this, 'my_admin_scripts')); |
| 20 |
add_action('admin_print_styles', array($this, 'my_admin_styles')); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/*--------------------------------------------------------------------------------------------- |
| 25 |
* Options HTML |
| 26 |
* - called from fields_meta_box.php |
| 27 |
* - displays options in html format |
| 28 |
* |
| 29 |
* @author Elliot Condon |
| 30 |
* @since 2.0.3 |
| 31 |
* |
| 32 |
---------------------------------------------------------------------------------------------*/ |
| 33 |
function options_html($key, $options) |
| 34 |
{ |
| 35 |
?> |
| 36 |
<tr class="field_option field_option_file"> |
| 37 |
<td class="label"> |
| 38 |
<label><?php _e("Save Format",'acf'); ?></label> |
| 39 |
</td> |
| 40 |
<td> |
| 41 |
<?php |
| 42 |
$temp_field = new stdClass(); |
| 43 |
$temp_field->type = 'select'; |
| 44 |
$temp_field->input_name = 'acf[fields]['.$key.'][options][save_format]'; |
| 45 |
$temp_field->input_class = ''; |
| 46 |
$temp_field->value = $options['save_format']; |
| 47 |
$temp_field->options = array('choices' => array( |
| 48 |
'url' => 'File URL', |
| 49 |
'id' => 'Attachment ID' |
| 50 |
)); |
| 51 |
$this->parent->create_field($temp_field); |
| 52 |
?> |
| 53 |
</td> |
| 54 |
</tr> |
| 55 |
|
| 56 |
<?php |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
/*--------------------------------------------------------------------------------------------- |
| 61 |
* admin_print_scripts / admin_print_styles |
| 62 |
* |
| 63 |
* @author Elliot Condon |
| 64 |
* @since 2.0.1 |
| 65 |
* |
| 66 |
---------------------------------------------------------------------------------------------*/ |
| 67 |
function my_admin_scripts() { |
| 68 |
wp_enqueue_script('media-upload'); |
| 69 |
wp_enqueue_script('thickbox'); |
| 70 |
} |
| 71 |
|
| 72 |
function my_admin_styles() { |
| 73 |
wp_enqueue_style('thickbox'); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/*--------------------------------------------------------------------------------------------- |
| 78 |
* popup_head - STYLES MEDIA THICKBOX |
| 79 |
* |
| 80 |
* @author Elliot Condon |
| 81 |
* @since 1.1.4 |
| 82 |
* |
| 83 |
---------------------------------------------------------------------------------------------*/ |
| 84 |
function popup_head() |
| 85 |
{ |
| 86 |
if(isset($_GET["acf_type"]) && $_GET['acf_type'] == 'file') |
| 87 |
{ |
| 88 |
?> |
| 89 |
<style type="text/css"> |
| 90 |
#media-upload-header #sidemenu li#tab-type_url, |
| 91 |
#media-upload-header #sidemenu li#tab-gallery { |
| 92 |
display: none; |
| 93 |
} |
| 94 |
|
| 95 |
#media-items tr.url, |
| 96 |
#media-items tr.align, |
| 97 |
#media-items tr.image_alt, |
| 98 |
#media-items tr.image-size, |
| 99 |
#media-items tr.post_excerpt, |
| 100 |
#media-items tr.post_content, |
| 101 |
#media-items tr.image_alt p, |
| 102 |
#media-items table thead input.button, |
| 103 |
#media-items table thead img.imgedit-wait-spin, |
| 104 |
#media-items tr.submit a.wp-post-thumbnail { |
| 105 |
display: none; |
| 106 |
} |
| 107 |
|
| 108 |
.media-item table thead img { |
| 109 |
border: #DFDFDF solid 1px; |
| 110 |
margin-right: 10px; |
| 111 |
} |
| 112 |
|
| 113 |
</style> |
| 114 |
<script type="text/javascript"> |
| 115 |
(function($){ |
| 116 |
|
| 117 |
$(document).ready(function(){ |
| 118 |
|
| 119 |
$('#media-items').bind('DOMNodeInserted',function(){ |
| 120 |
$('input[value="Insert into Post"]').each(function(){ |
| 121 |
$(this).attr('value','<?php _e("Select File",'acf'); ?>'); |
| 122 |
}); |
| 123 |
}).trigger('DOMNodeInserted'); |
| 124 |
|
| 125 |
$('form#filter').each(function(){ |
| 126 |
|
| 127 |
$(this).append('<input type="hidden" name="acf_type" value="file" />'); |
| 128 |
|
| 129 |
}); |
| 130 |
}); |
| 131 |
|
| 132 |
})(jQuery); |
| 133 |
</script> |
| 134 |
|
| 135 |
<?php |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/*--------------------------------------------------------------------------------------------- |
| 141 |
* media_send_to_editor - SEND IMAGE TO ACF DIV |
| 142 |
* |
| 143 |
* @author Elliot Condon |
| 144 |
* @since 1.1.4 |
| 145 |
* |
| 146 |
---------------------------------------------------------------------------------------------*/ |
| 147 |
function media_send_to_editor($html, $id) |
| 148 |
{ |
| 149 |
parse_str($_POST["_wp_http_referer"], $arr_postinfo); |
| 150 |
|
| 151 |
if(isset($arr_postinfo["acf_type"]) && $arr_postinfo["acf_type"] == "file") |
| 152 |
{ |
| 153 |
|
| 154 |
$file_src = wp_get_attachment_url($id); |
| 155 |
|
| 156 |
?> |
| 157 |
<script type="text/javascript"> |
| 158 |
|
| 159 |
if(self.parent.acf_div.find('input.value').hasClass('id')) |
| 160 |
{ |
| 161 |
self.parent.acf_div.find('input.value').val('<?php echo $id; ?>'); |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
self.parent.acf_div.find('input.value').val('<?php echo $file_src; ?>'); |
| 166 |
} |
| 167 |
|
| 168 |
self.parent.acf_div.find('p.file span').html('<?php echo $file_src; ?>'); |
| 169 |
self.parent.acf_div.addClass('active'); |
| 170 |
|
| 171 |
// reset acf_div and return false |
| 172 |
self.parent.acf_div = null; |
| 173 |
|
| 174 |
self.parent.tb_remove(); |
| 175 |
|
| 176 |
</script> |
| 177 |
<?php |
| 178 |
exit; |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
return $html; |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
function html($field) |
| 189 |
{ |
| 190 |
|
| 191 |
$class = ""; |
| 192 |
$file = ""; |
| 193 |
|
| 194 |
if($field->value != '') |
| 195 |
{ |
| 196 |
$file = $field->value; |
| 197 |
$class = " active"; |
| 198 |
} |
| 199 |
|
| 200 |
if(!isset($field->options['save_format'])){$field->options['save_format'] = 'url';} |
| 201 |
|
| 202 |
echo '<div class="acf_file_uploader'.$class.'">'; |
| 203 |
echo '<a href="#" class="remove_file"></a>'; |
| 204 |
if($field->options['save_format'] == 'id') |
| 205 |
{ |
| 206 |
$file_src = wp_get_attachment_url($field->value); |
| 207 |
echo '<p class="file"><span>'.$file_src.'</span> <input type="button" class="button" value="'.__('Remove File','acf').'" /></p>'; |
| 208 |
} |
| 209 |
else |
| 210 |
{ |
| 211 |
echo '<p class="file"><span>'.$field->value.'</span> <input type="button" class="button" value="'.__('Remove File','acf').'" /></p>'; |
| 212 |
} |
| 213 |
|
| 214 |
echo '<input class="value '.$field->options['save_format'].'" type="hidden" name="'.$field->input_name.'" value="'.$field->value.'" />'; |
| 215 |
echo '<p class="no_file">'.__('No File selected','acf').'. <input type="button" class="button" value="'.__('Add File','acf').'" /></p>'; |
| 216 |
echo '</div>'; |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
?> |