name = 'upload_files';
$this->label = __( 'Upload Files', 'frontend-admin' );
$this->category = 'content';
$this->public = false;
$this->defaults = array(
'library' => 'all',
'min' => 0,
'max' => 0,
'min_width' => 0,
'min_height' => 0,
'min_size' => 0,
'max_width' => 0,
'max_height' => 0,
'max_size' => 0,
'resize_file' => 0,
'mime_types' => '',
'insert' => 'append',
'button_text' => __( 'Add Files', 'frontend-admin' ),
'add_button_locations' => array( 'bottom' ),
'click_event' => 'edit'
);
// actions
add_action( 'wp_ajax_acf/fields/gallery/get_attachment', array( $this, 'ajax_get_attachment' ) );
add_action( 'wp_ajax_nopriv_acf/fields/gallery/get_attachment', array( $this, 'ajax_get_attachment' ) );
add_action( 'wp_ajax_acf/fields/gallery/get_sort_order', array( $this, 'ajax_get_sort_order' ) );
add_action( 'wp_ajax_nopriv_acf/fields/gallery/get_sort_order', array( $this, 'ajax_get_sort_order' ) );
// add_filter
add_filter( 'acf/prepare_field/type=gallery', array( $this, 'prepare_gallery_field' ), 5 );
$multiple_files = array( 'gallery', 'upload_files', 'product_images' );
foreach ( $multiple_files as $type ) {
add_filter( 'acf/pre_update_value/type=' . $type, array( $this, 'update_attachments_value' ), 10, 4 );
add_action( 'acf/render_field_settings/type=' . $type, array( $this, 'upload_button_text_setting' ) );
add_filter( 'acf/validate_value/type=' . $type, array( $this, 'validate_files_value' ), 5, 4 );
}
}
function prepare_gallery_field( $field ) {
global $fea_form;
if( ! $fea_form ) return $field;
$field['type'] = 'upload_files';
$field = $this->prepare_field( $field );
return $field;
}
/*
* prepare_field()
*
* Prepares field setting prior to rendering field in form
*
* @param $field - an array holding all the field's data
* @return $field
*
* @type action
* @since 3.6
* @date 23/01/13
*/
function prepare_field( $field ) {
$uploader = acf_get_setting( 'uploader' );
// enqueue
if ( $uploader == 'basic' ) {
if ( isset( $field['wrapper']['class'] ) ) {
$field['wrapper']['class'] .= ' acf-uploads';
} else {
$field['wrapper']['class'] = 'acf-uploads';
}
}
$field['wrapper']['class'] .= ' image-field';
if ( empty( $field['max_width'] ) ) {
$field['max_width'] = 1920;
}
if ( empty( $field['max_height'] ) ) {
$field['max_height'] = 1080;
}
//if no button text, add the default
if ( empty( $field['button_text'] ) ) {
$field['button_text'] = __( 'Add Images', 'frontend-admin' );
}
return $field;
}
/*
* input_admin_enqueue_scripts
*
* description
*
* @type function
* @date 16/12/2015
* @since 5.3.2
*
* @param $post_id (int)
* @return $post_id (int)
*/
function input_admin_enqueue_scripts() {
// localize
acf_localize_text(
array(
'Add Image to Gallery' => __( 'Add Image to Gallery', 'frontend-admin' ),
'Maximum selection reached' => __( 'Maximum selection reached', 'frontend-admin' ),
)
);
}
function update_attachments_value( $checked, $value, $post_id = false, $field = false ) {
if ( isset( $value['{file-index}'] ) ) {
unset( $value['{file-index}'] );
}
if ( ! is_array( $value ) || ! $value ) {
return $checked;
}
if ( is_numeric( $post_id ) && $value ) {
$post = get_post( $post_id );
if ( wp_is_post_revision( $post ) ) {
$post_id = $post->post_parent;
}
}
$new_value = array();
global $fea_form;
foreach ( $value as $index => $attachment ) {
$meta = $fea_form['record']['fields']['file_data'][$field['name']][$attachment] ?? false;
if( empty( $meta ) ) $meta = $fea_form['record']['fields']['file_data'][$field['name']][$index] ?? false;
// the submitted IDs come from the visitor: only update the details of
// attachments they may edit or uploaded themselves
if( empty( $meta ) || ! feadmin_can_edit_uploaded_attachment( $attachment ) ){
continue;
}
$attachment = (int) $attachment;
if ( isset( $meta['alt'] ) ) {
update_post_meta( $attachment, '_wp_attachment_image_alt', $meta['alt'] );
}
$edit = array( 'ID' => $attachment );
if ( ! empty( $meta['title'] ) ) {
$edit['post_title'] = sanitize_text_field( $meta['title'] );
}
if ( isset( $meta['description'] ) ) {
$edit['post_content'] = sanitize_textarea_field( $meta['description'] );
}
if ( isset( $meta['caption'] ) ) {
$edit['post_excerpt'] = sanitize_textarea_field( $meta['caption'] );
}
wp_update_post( $edit );
$attachment = (int) $attachment;
acf_connect_attachment_to_post( $attachment, $post_id );
}
return $checked;
}
/*
* ajax_get_attachment
*
* description
*
* @type function
* @date 13/12/2013
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function ajax_get_attachment() {
// options
$options = acf_parse_args(
$_POST,
array(
'post_id' => 0,
'attachment' => 0,
'id' => 0,
'field_key' => '',
'nonce' => '',
)
);
// validate
if ( ! feadmin_verify_ajax() ) {
die();
}
// bail early if no id
if ( ! $options['id'] ) {
die();
}
// load field
$field = acf_get_field( $options['field_key'] );
// bali early if no field
if ( ! $field ) {
die();
}
// render
$this->render_attachment( $field, $options['id'] );
die;
}
/*
* ajax_get_sort_order
*
* description
*
* @type function
* @date 13/12/2013
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function ajax_get_sort_order() {
// vars
$r = array();
$order = 'DESC';
$args = acf_parse_args(
$_POST,
array(
'ids' => 0,
'sort' => 'date',
'field_key' => '',
'nonce' => '',
)
);
// validate
if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) ) {
wp_send_json_error();
}
// reverse
if ( $args['sort'] == 'reverse' ) {
$ids = array_reverse( $args['ids'] );
wp_send_json_success( $ids );
}
if ( $args['sort'] == 'title' ) {
$order = 'ASC';
}
// find attachments (DISTINCT POSTS)
$ids = get_posts(
array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
'post__in' => $args['ids'],
'order' => $order,
'orderby' => $args['sort'],
'fields' => 'ids',
)
);
// success
if ( ! empty( $ids ) ) {
wp_send_json_success( $ids );
}
// failure
wp_send_json_error();
}
function upload_button_text_setting( $field ) {
acf_render_field_setting(
$field,
array(
'label' => __( 'Button Text', 'frontend-admin' ),
'name' => 'button_text',
'type' => 'text',
'placeholder' => __( 'Add Image', 'frontend-admin' ),
)
);
}
/*
* render_attachment
*
* description
*
* @type function
* @date 13/12/2013
* @since 5.0.0
*
* @param $field (array)
* @param $post_id (int)
*/
function render_attachment( $field, $id = 0 ) {
// vars
$attachment = wp_prepare_attachment_for_js( $id );
$compat = get_compat_media_markup( $id );
$compat = $compat['item'];
$prefix = 'attachments[' . $id . ']';
$thumb = '';
$dimentions = '';
// thumb
if ( isset( $attachment['thumb']['src'] ) ) {
// video
$thumb = $attachment['thumb']['src'];
} elseif ( isset( $attachment['sizes']['thumbnail']['url'] ) ) {
// image
$thumb = $attachment['sizes']['thumbnail']['url'];
} elseif ( $attachment['type'] === 'image' ) {
// svg
$thumb = $attachment['url'];
} else {
// fallback (perhaps attachment does not exist)
$thumb = wp_mime_type_icon();
}
// dimentions
if ( $attachment['type'] === 'audio' ) {
$dimentions = __( 'Length', 'frontend-admin' ) . ': ' . $attachment['fileLength'];
} elseif ( ! empty( $attachment['width'] ) ) {
$dimentions = $attachment['width'] . ' x ' . $attachment['height'];
}
if ( ! empty( $attachment['filesizeHumanReadable'] ) ) {
$dimentions .= ' (' . $attachment['filesizeHumanReadable'] . ')';
}
?>
'attachment',
'post__in' => $post__in,
)
);
// return
return $posts;
}
public function add_item( $field ){
//add a square div with a plus icon
$placeholder = $field['placeholder_image'] ?? '';
if( $placeholder ){
$placeholder = wp_get_attachment_image_url( $placeholder, 'thumb' );
}
?>
$field['id'],
'class' => "fea-uploads {$field['class']}",
'data-library' => $field['library'],
'data-uploader' => $uploader,
'data-min' => $field['min'],
'data-max' => $field['max'],
'data-mime_types' => $field['mime_types'],
'data-insert' => $field['insert'],
'data-columns' => 4,
'data-allowed_types' => 'image, pdf',
'data-resize' => $field['resize_file'] ?? 0,
'data-min_size' => $field['min_size'],
'data-min_width' => $field['min_width'],
'data-min_height' => $field['min_height'],
'data-max_size' => $field['max_size'],
'data-max_width' => $field['max_width'],
'data-max_height' => $field['max_height'],
'data-click_event' => $click_event,
'data-open_in_lightbox' => $field['open_in_lightbox'] ?? 0,
);
if ( ! empty( $field['open_in_lightbox'] ) ) {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '-min';
wp_enqueue_script( 'fea-lightbox', FEA_URL . 'assets/js/lightbox' . $min . '.js', array(), FEA_VERSION, true );
wp_enqueue_style( 'fea-lightbox', FEA_URL . 'assets/css/lightbox' . $min . '.css', array(), FEA_VERSION );
}
$button_locations = $field['add_button_locations'] ?? array( 'bottom' );
if( is_string( $button_locations ) ){
$button_locations = explode( ',', $button_locations );
}
if ( ! $value && ! in_array( 'item', $button_locations ) ) {
$atts['class'] .= 'acf-hidden';
}
$button_text = $field['button_text'] ?? __( 'Add Images', 'frontend-admin' );
if ( isset( $value['{file-index}'] ) ) {
unset( $value['{file-index}'] );
}
$default_icon = wp_mime_type_icon( 'application/pdf' );
?>
>
$field['name'],
'value' => '',
)
);
?>
add_item( $field ); ?>
$v ) :
// bail early if no value
if ( ! $v ) {
continue;
}
if ( is_numeric( $v ) ) {
$a = acf_get_attachment( $v );
if ( ! $a ) {
continue;
}
} else {
if ( isset( $v['id'] ) ) {
$a = acf_get_attachment( $v['id'] );
if ( ! $a ) {
continue;
}
} else {
$a = $v;
}
$a['filename'] = wp_basename( $a['title'] );
$a['type'] = '';
}
$a['class'] = 'fea-uploads-attachment';
// thumbnail
$thumbnail = acf_get_post_thumbnail( $a['id'], 'medium' );
// remove filename if is image
if ( 'image' == $a['type'] ) {
$a['filename'] = '';
}else{
$thumbnail['url'] = $default_icon;
}
// class
$a['class'] .= ' -' . $a['type'];
if ( 'icon' == $thumbnail['type'] ) {
$a['class'] .= ' -icon';
}
$extra_attr = 'data-href="'.esc_attr($a['url']).'"';
if ( !empty( $field['open_in_lightbox'] ) && 'image' == $a['type'] ) {
$extra_attr .= ' data-lightbox="'.esc_attr( $field['key'] ).'"';
$extra_attr .= ' data-title="'.esc_attr( $a['title'] ).'"';
}
if( 'download' == $click_event ){
$extra_attr .= ' data-download=true';
}
?>
class="" data-id="">
$field['name'] . '[' . $a['id'] . ']',
'value' => $a['id'],
)
);
?>
form_display->render_meta_fields( $prefix, $a['id'], false );
}
?>
add_item( $field ); ?>
form_display->render_meta_fields( $prefix, 'clone', false );
}
?>
__( 'Add Image Button Locations', 'frontend-admin' ),
'instructions' => '',
'type' => 'checkbox',
'name' => 'add_button_locations',
'choices' => array(
'top' => __( 'Top', 'frontend-admin' ),
'bottom' => __( 'Bottom', 'frontend-admin' ),
'item' => __( 'Item', 'frontend-admin' ),
),
)
);
acf_render_field_setting(
$field,
array(
'label' => __( 'When User Clicks Image...', 'frontend-admin' ),
'instructions' => '',
'type' => 'radio',
'name' => 'click_event',
'choices' => array(
'edit' => __( 'Edit Image', 'frontend-admin' ),
'download' => __( 'Download Image', 'frontend-admin' ),
),
)
);
//open images in lightbox
acf_render_field_setting(
$field,
array(
'label' => __( 'Open Images in Lightbox', 'frontend-admin' ),
'instructions' => '',
'type' => 'true_false',
'name' => 'open_in_lightbox',
'ui' => 1,
'ui_on_text' => __( 'Yes', 'frontend-admin' ),
'ui_off_text' => __( 'No', 'frontend-admin' ),
)
);
// min
acf_render_field_setting(
$field,
array(
'label' => __( 'Minimum Selection', 'frontend-admin' ),
'instructions' => '',
'type' => 'number',
'name' => 'min',
)
);
// max
acf_render_field_setting(
$field,
array(
'label' => __( 'Maximum Selection', 'frontend-admin' ),
'instructions' => '',
'type' => 'number',
'name' => 'max',
)
);
// insert
acf_render_field_setting(
$field,
array(
'label' => __( 'Insert', 'frontend-admin' ),
'instructions' => __( 'Specify where new attachments are added', 'frontend-admin' ),
'type' => 'select',
'name' => 'insert',
'choices' => array(
'append' => __( 'Append to the end', 'frontend-admin' ),
'prepend' => __( 'Prepend to the beginning', 'frontend-admin' ),
),
)
);
// library
acf_render_field_setting(
$field,
array(
'label' => __( 'Library', 'frontend-admin' ),
'instructions' => __( 'Limit the media library choice', 'frontend-admin' ),
'type' => 'radio',
'name' => 'library',
'layout' => 'horizontal',
'choices' => array(
'all' => __( 'All', 'frontend-admin' ),
'uploadedTo' => __( 'Uploaded to post', 'frontend-admin' ),
),
)
);
// min
acf_render_field_setting(
$field,
array(
'label' => __( 'Minimum', 'frontend-admin' ),
'instructions' => __( 'Restrict which images can be uploaded', 'frontend-admin' ),
'type' => 'text',
'name' => 'min_width',
'prepend' => __( 'Width', 'frontend-admin' ),
'append' => 'px',
)
);
acf_render_field_setting(
$field,
array(
'label' => '',
'type' => 'text',
'name' => 'min_height',
'prepend' => __( 'Height', 'frontend-admin' ),
'append' => 'px',
'_append' => 'min_width',
)
);
acf_render_field_setting(
$field,
array(
'label' => '',
'type' => 'text',
'name' => 'min_size',
'prepend' => __( 'File size', 'frontend-admin' ),
'append' => 'MB',
'_append' => 'min_width',
)
);
// max
acf_render_field_setting(
$field,
array(
'label' => __( 'Maximum', 'frontend-admin' ),
'instructions' => __( 'Restrict which images can be uploaded', 'frontend-admin' ),
'type' => 'text',
'name' => 'max_width',
'prepend' => __( 'Width', 'frontend-admin' ),
'append' => 'px',
)
);
acf_render_field_setting(
$field,
array(
'label' => '',
'type' => 'text',
'name' => 'max_height',
'prepend' => __( 'Height', 'frontend-admin' ),
'append' => 'px',
'_append' => 'max_width',
)
);
acf_render_field_setting(
$field,
array(
'label' => '',
'type' => 'text',
'name' => 'max_size',
'prepend' => __( 'File size', 'frontend-admin' ),
'append' => 'MB',
'_append' => 'max_width',
)
);
//resize or throw error
acf_render_field_setting(
$field,
array(
'label' => __( 'Resize Image or Throw Error', 'frontend-admin' ),
'instructions' => __( 'Resize the image to fit within the maximum dimensions', 'frontend-admin' ),
'type' => 'true_false',
'name' => 'resize_file',
'ui' => 1,
'ui_on_text' => __( 'Resize', 'frontend-admin' ),
'ui_off_text' => __( 'Throw Error', 'frontend-admin' ),
)
);
}
/*
* format_value()
*
* This filter is appied to the $value after it is loaded from the db and before it is returned to the template
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value (mixed) the value which was loaded from the database
* @param $post_id (mixed) the $post_id from which the value was loaded
* @param $field (array) the field array holding all the field options
*
* @return $value (mixed) the modified value
*/
/*
function format_value( $value, $post_id, $field ) {
// bail early if no value
if( empty($value) ) return false;
// get posts
$posts = $this->get_attachments($value);
// update value to include $post
foreach( array_keys($posts) as $i ) {
$posts[ $i ] = acf_get_attachment( $posts[ $i ] );
}
// return
return $posts;
} */
/**
* validate_value
*
* This function will validate a basic file input
*
* @type function
* @date 14/11/2022
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function validate_files_value( $valid, $value, $field, $input ) {
if ( isset( $value['{file-index}'] ) ) {
unset( $value['{file-index}'] );
}
if ( empty( $value ) || ! is_array( $value ) ) {
if ( $field['required'] ) {
return sprintf( /* translators: %s: value */ __( '%s value is required.', 'frontend-admin' ), $field['label'] );
}
$value = array();
}
if ( is_array( $value ) && count( $value ) < $field['min'] ) {
$valid = /* translators: 1: value 2: value */ _n( '%1$s requires at least %2$s selection', '%1$s requires at least %2$s selections', $field['min'], 'frontend-admin' );
$valid = sprintf( $valid, $field['label'], $field['min'] );
}
return $valid;
}
/*
* update_single_value()
*
* This filter is appied to the $value before it is updated in the db
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value - the value which will be saved in the database
* @param $post_id - the $post_id of which the value will be saved
* @param $field - the field array holding all the field options
*
* @return $value - the modified value
*/
function update_single_value( $value ) {
// numeric
if ( is_numeric( $value ) ) {
return $value;
}
// array?
if ( is_array( $value ) && isset( $value['ID'] ) ) {
return $value['ID'];
}
// object?
if ( is_object( $value ) && isset( $value->ID ) ) {
return $value->ID;
}
// return
return $value;
}
}
endif; // class_exists check