name = 'gallery';
$this->title = __("Gallery",'acf');
// actions
add_action('admin_head-media-upload-popup', array($this, 'popup_head'));
add_action('acf_head-update_attachment-gallery', array($this, 'acf_head_update_attachment'));
add_action('wp_ajax_acf/fields/gallery/get_image', array($this, 'get_image'));
}
/*
* get_image
*
* @description:
* @since: 3.5.8
* @created: 18/01/13
*/
function get_image()
{
// vars
$options = array(
'nonce' => '',
'id' => '',
'preview_size' => 'full'
);
$return = array();
// load post options
$options = array_merge($options, $_POST);
// verify nonce
if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') )
{
die(0);
}
// get attachment object
$attachment = get_post( $options['id'] );
$src = wp_get_attachment_image_src( $attachment->ID, $options['preview_size'] );
$return = array(
'id' => $attachment->ID,
'src' => $src[0],
'title'=> $attachment->post_title,
'caption'=> $attachment->post_excerpt,
'alt'=> get_post_meta($attachment->ID, '_wp_attachment_image_alt', true),
'description'=> $attachment->post_content,
);
echo json_encode($return);
die;
}
/*--------------------------------------------------------------------------------------
*
* create_options
* - this function is called from core/field_meta_box.php to create extra options
* for your field
*
* @params
* - $key (int) - the $_POST obejct key required to save the options to the field
* - $field (array) - the field object
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function create_options($key, $field)
{
// vars
$defaults = array(
'preview_size' => 'thumbnail',
);
$field = array_merge($defaults, $field);
?>
|
parent->get_all_image_sizes();
do_action('acf/create_field', array(
'type' => 'radio',
'name' => 'fields['.$key.'][preview_size]',
'value' => $field['preview_size'],
'layout' => 'horizontal',
'choices' => $image_sizes
));
?>
|
false,
'preview_size' => 'thumbnail',
);
$field = array_merge($defaults, $field);
?>
'attachment',
'numberposts' => -1,
'post_status' => null,
'post__in' => $value,
));
$ordered_attachments = array();
foreach( $attachments as $attachment)
{
// create array to hold value data
$ordered_attachments[ $attachment->ID ] = array(
'id' => $attachment->ID,
'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true),
'title' => $attachment->post_title,
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
);
}
// override value array with attachments
foreach( $value as $v)
{
if( isset($ordered_attachments[ $v ]) )
{
$new_value[] = $ordered_attachments[ $v ];
}
}
/*
// format attachments
$ordered_attachments = array();
foreach( $attachments as $attachment )
{
$ordered_attachments[ $attachment->ID ] = $attachment;
}
// update value with corisponding attachments
foreach( $value as $k => $v)
{
// get the attachment onject for this value (attachment id)
$attachment = $ordered_attachments[ $v ];
// create array to hold value data
$value[ $k ] = array(
'id' => $attachment->ID,
'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true),
'title' => $attachment->post_title,
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
);
}
*/
// return value
return $new_value;
}
/*--------------------------------------------------------------------------------------
*
* get_value_for_api
* - called from your template file when using the API functions (get_field, etc).
* This function is useful if your field needs to format the returned value
*
* @params
* - $post_id (int) - the post ID which your value is attached to
* - $field (array) - the field object.
*
* @author Elliot Condon
* @since 3.0.0
*
*-------------------------------------------------------------------------------------*/
function get_value_for_api($post_id, $field)
{
// get value
$value = $this->get_value($post_id, $field);
// find all image sizes
$image_sizes = get_intermediate_image_sizes();
if( $value )
{
foreach( $value as $k => $v )
{
// full url
$value[$k]['url'] = wp_get_attachment_url( $v['id'] );
// sizes
if( $image_sizes )
{
$value[$k]['sizes'] = array();
foreach( $image_sizes as $image_size )
{
// find src
$src = wp_get_attachment_image_src( $v['id'], $image_size );
// add src
$value[$k]['sizes'][$image_size] = $src[0];
}
// foreach( $image_sizes as $image_size )
}
// if( $image_sizes )
}
// foreach( $value as $k => $v )
}
// if( $value )
// return value
return $value;
}
/*
* popup_head
*
* @description:
* @since 3.2.8
* @created: 6/07/12
*/
function popup_head()
{
// options
$defaults = array(
'acf_type' => '',
'acf_gallery_id' => '',
'acf_preview_size' => 'thumbnail',
'tab' => 'type',
);
$options = array_merge($defaults, $_GET);
// validate
if( $options['acf_type'] != 'gallery' )
{
return;
}
?>