Example #1
0
/**
 * Returns current post's attachments
 */
function file_gallery_list_attachments(&$count_attachments, $post_id, $attachment_order, $checked_attachments, $attachment_orderby = 'menu_order')
{
    global $wpdb, $_wp_additional_image_sizes;
    $options = get_option('file_gallery');
    $thumb_id = false;
    $attached_files = '';
    if ('' != $attachment_order && false !== strpos($attachment_order, ',')) {
        $attachment_ids = str_replace(',', "','", trim($attachment_order, ','));
        $query = "SELECT * FROM {$wpdb->posts} \n\t\t\t WHERE {$wpdb->posts}.post_parent = " . $post_id . "\n\t\t\t AND {$wpdb->posts}.post_type = 'attachment' \n\t\t\t ORDER BY FIELD(ID, '" . $attachment_ids . "') ";
        $attachments = $wpdb->get_results($query);
    } else {
        if (!in_array($attachment_order, array('ASC', 'DESC'))) {
            $attachment_order = 'ASC';
        }
        if (!in_array($attachment_orderby, array('post_title', 'post_name', 'post_date', 'menu_order'))) {
            $attachment_orderby = 'menu_order';
        }
        $query = array('post_parent' => $post_id, 'post_type' => 'attachment', 'order' => $attachment_order, 'orderby' => $attachment_orderby, 'post_status' => 'inherit');
        $attachments = get_children($query);
    }
    if ($attachments) {
        $thumb_id = get_post_meta($post_id, '_thumbnail_id', true);
        // start the list...
        $attached_files = '<ul class="ui-sortable" id="file_gallery_list">' . "\n";
        $count_attachments = count($attachments);
        $attachment_thumb_size = isset($options["default_metabox_image_size"]) ? $options["default_metabox_image_size"] : 'thumbnail';
        $attachment_thumb_width = isset($options["default_metabox_image_width"]) && 0 < $options["default_metabox_image_width"] ? $options["default_metabox_image_width"] : 75;
        if (isset($_wp_additional_image_sizes[$attachment_thumb_size])) {
            $ats_width = $_wp_additional_image_sizes[$attachment_thumb_size]['width'];
            $ats_height = $_wp_additional_image_sizes[$attachment_thumb_size]['height'];
        } else {
            $ats_width = get_option($attachment_thumb_size . '_size_w');
            $ats_height = get_option($attachment_thumb_size . '_size_h');
        }
        if (0 < (int) $ats_width && 0 < (int) $ats_height) {
            $attachment_thumb_ratio = $ats_width / $ats_height;
        } else {
            $attachment_thumb_ratio = 1;
        }
        if ('' == strval($attachment_thumb_ratio)) {
            $attachment_thumb_ratio = 1;
        }
        $attachment_thumb_height = $attachment_thumb_width / $attachment_thumb_ratio;
        foreach ($attachments as $attachment) {
            $classes = array("sortableitem");
            $post_thumb_link = "set";
            $non_image = "";
            $checked = "";
            $original_id = get_post_meta($attachment->ID, "_is_copy_of", true);
            $copies = get_post_meta($attachment->ID, "_has_copies", true);
            if ("" != strval($original_id)) {
                $classes[] = "copy copy-of-" . $original_id;
            } elseif ("" != strval($copies)) {
                $classes[] = "has_copies copies-" . implode("-", $copies);
            }
            if ((int) $thumb_id === (int) $attachment->ID) {
                $classes[] = "post_thumb";
                $post_thumb_link = "unset";
            }
            $attachment_thumb = wp_get_attachment_image_src($attachment->ID, $attachment_thumb_size);
            $large = wp_get_attachment_image_src($attachment->ID, "large");
            if (in_array($attachment->ID, $checked_attachments)) {
                $checked = ' checked="checked"';
                $classes[] = 'selected';
            }
            // if it's not an image...
            if ("" == $attachment_thumb) {
                $attachment_thumb[0] = file_gallery_https(FILE_GALLERY_CRYSTAL_URL) . "/" . file_gallery_get_file_type($attachment->post_mime_type) . ".png";
                $attachment_width = '';
                $attachment_height = '';
                $non_image = ' non_image';
                $_attachment_thumb_width = 55;
                $image_width_style = '';
                $classes[] = 'non-image';
            } else {
                $classes[] = 'image';
                $_attachment_thumb_width = $attachment_thumb_width;
                if (1 === $attachment_thumb_ratio && $attachment_thumb[2] > $attachment_thumb_width) {
                    $forced_height = 'height: ' . $attachment_thumb_height . 'px';
                } else {
                    $forced_height = '';
                }
                $image_width_style = 'style="width: ' . $_attachment_thumb_width . 'px; ' . $forced_height . '"';
            }
            $attached_files .= '
			<li id="image-' . $attachment->ID . '" class="' . implode(" ", $classes) . '" style="width: ' . $_attachment_thumb_width . 'px; height: ' . $attachment_thumb_height . 'px">
				
				<img src="' . $attachment_thumb[0] . '" alt="' . $attachment->post_title . '" id="in-' . $attachment->ID . '" title="' . $attachment->post_title . '" class="fgtt' . $non_image . '" ' . $image_width_style . ' />';
            if ("" == $non_image) {
                $attached_files .= '<a href="' . $large[0] . '" id="in-' . $attachment->ID . '-zoom" class="img_zoom action">
						<img src="' . file_gallery_https(FILE_GALLERY_URL) . '/images/famfamfam_silk/magnifier.png" alt="' . __("Zoom", "file-gallery") . '" title="' . __("Zoom", "file-gallery") . '" />
					</a>';
            }
            $attached_files .= '<a href="#" id="in-' . $attachment->ID . '-edit" class="img_edit action">
					<img src="' . file_gallery_https(FILE_GALLERY_URL) . '/images/famfamfam_silk/pencil.png" alt="' . __("Edit", "file-gallery") . '" title="' . __("Edit", "file-gallery") . '" />
				</a>
				<span class="checker_action action" title="' . __('Click to select, or click and drag to change position', 'file-gallery') . '">
					<input type="checkbox" id="att-chk-' . $attachment->ID . '" class="checker"' . $checked . ' title="' . __("Click to select", "file-gallery") . '" />
				</span>';
            if (current_user_can('edit_post', $attachment->ID)) {
                if ("" == $non_image && function_exists('current_theme_supports') && current_theme_supports('post-thumbnails')) {
                    if ("set" == $post_thumb_link) {
                        $as_featured = __("Set as featured image", "file-gallery");
                    } else {
                        $as_featured = __("Unset as featured image", "file-gallery");
                    }
                    $attached_files .= '<a href="#" class="post_thumb_status action" rel="' . $attachment->ID . '" title="' . $as_featured . '">
							<img src="' . file_gallery_https(FILE_GALLERY_URL) . '/images/famfamfam_silk/star_' . $post_thumb_link . '.png" alt="' . $as_featured . '" />
						</a>';
                    $attached_files .= '<div id="post_thumb_setter_' . $attachment->ID . '" class="post_thumb_setter">
							' . __("Really set as featured image?", "file-gallery") . ' 
							<a href="#" id="post_thumb_set[' . $attachment->ID . ']" class="post_thumb_set">' . __("Continue", "file-gallery") . '</a>
							' . __("or", "file-gallery") . '
							<a href="#" class="post_thumb_cancel" rel="' . $attachment->ID . '">' . __("Cancel", "file-gallery") . '</a>
						</div>';
                    $attached_files .= '<div id="post_thumb_unsetter_' . $attachment->ID . '" class="post_thumb_unsetter">
							' . __("Really unset as featured image?", "file-gallery") . ' 
							<a href="#" id="post_thumb_unset[' . $attachment->ID . ']" class="post_thumb_unset">' . __("Continue", "file-gallery") . '</a>
							' . __("or", "file-gallery") . '
							<a href="#" class="post_thumb_cancel" rel="' . $attachment->ID . '">' . __("Cancel", "file-gallery") . '</a>
						</div>';
                }
                $attached_files .= '<a href="#" class="delete_or_detach_link action" rel="' . $attachment->ID . '">
					<img src="' . file_gallery_https(FILE_GALLERY_URL) . '/images/famfamfam_silk/delete.png" alt="' . __("Detach / Delete", "file-gallery") . '" title="' . __("Detach / Delete", "file-gallery") . '" />
				</a>
				<div id="detach_or_delete_' . $attachment->ID . '" class="detach_or_delete">
					<br />';
                if (current_user_can('delete_post', $attachment->ID)) {
                    $attached_files .= '<a href="#" class="do_single_delete" rel="' . $attachment->ID . '">' . __("Delete", "file-gallery") . '</a>
						<br />
						' . __("or", "file-gallery") . '
						<br />';
                }
                $attached_files .= '<a href="#" class="do_single_detach" rel="' . $attachment->ID . '">' . __("Detach", "file-gallery") . '</a>
				</div>
				<div id="detach_attachment_' . $attachment->ID . '" class="detach_attachment">
					' . __("Really detach?", "file-gallery") . ' 
					<a href="#" id="detach[' . $attachment->ID . ']" class="detach">' . __("Continue", "file-gallery") . '</a>
					' . __("or", "file-gallery") . '
					<a href="#" class="detach_cancel" rel="' . $attachment->ID . '">' . __("Cancel", "file-gallery") . '</a>
				</div>';
                if (current_user_can('delete_post', $attachment->ID)) {
                    $attached_files .= '<div id="del_attachment_' . $attachment->ID . '" class="del_attachment">
						' . __("Really delete?", "file-gallery") . ' 
						<a href="#" id="del[' . $attachment->ID . ']" class="delete">' . __("Continue", "file-gallery") . '</a>
						' . __("or", "file-gallery") . '
						<a href="#" class="delete_cancel" rel="' . $attachment->ID . '">' . __("Cancel", "file-gallery") . '</a>
					</div>';
                }
            }
            $attached_files .= '</li>
			' . "\n";
        }
        //end the list...
        $attached_files .= "</ul>\n";
    }
    return $attached_files;
}
Example #2
0
/**
 * Main shortcode function
 *
 * @since 0.1
 */
function file_gallery_shortcode($content = false, $attr = false)
{
    global $file_gallery, $wpdb, $post;
    // if the function is called directly, not via shortcode
    if (false !== $content && false === $attr) {
        $attr = $content;
    }
    if (!isset($file_gallery->gallery_id)) {
        $file_gallery->gallery_id = 1;
    } else {
        $file_gallery->gallery_id++;
    }
    $options = get_option('file_gallery');
    if (isset($options['cache']) && true == $options['cache']) {
        if ('html' == $attr['output_type'] || isset($options['cache_non_html_output']) && true == $options['cache_non_html_output']) {
            $transient = 'filegallery_' . md5($post->ID . "_" . serialize($attr));
            $cache = get_transient($transient);
            if ($cache) {
                return $cache;
            }
        }
    }
    // if option to show galleries in excerpts is set to false...
    // ...replace [gallery] with user selected text
    if (!is_singular() && (!isset($options['in_excerpt']) || true != $options['in_excerpt'])) {
        return $options['in_excerpt_replace_content'];
    }
    $default_templates = unserialize(FILE_GALLERY_DEFAULT_TEMPLATES);
    // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
    if (isset($attr['orderby'])) {
        $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
        if (!$attr['orderby']) {
            unset($attr['orderby']);
        }
    }
    // extract the defaults...
    extract(shortcode_atts(array('order' => 'ASC', 'orderby' => '', 'id' => $post->ID, 'columns' => 3, 'size' => 'thumbnail', 'link' => 'attachment', 'include' => '', 'exclude' => '', 'template' => 'default', 'linkclass' => '', 'imageclass' => '', 'galleryclass' => '', 'rel' => 1, 'tags' => '', 'tags_from' => 'current', 'output_type' => 'html', 'output_params' => 1, 'attachment_ids' => '', 'mimetype' => '', 'limit' => -1, 'offset' => -1, 'paginate' => 0, 'link_size' => 'full'), $attr));
    if (!in_array($template, $default_templates)) {
        $template_file = FILE_GALLERY_THEME_TEMPLATES_ABSPATH . '/' . $template . '/gallery.php';
        if (!is_readable($template_file)) {
            $template_file = FILE_GALLERY_CONTENT_TEMPLATES_ABSPATH . '/' . $template . '/gallery.php';
        }
    } else {
        if ('default' == $template) {
            $template_file = FILE_GALLERY_DEFAULT_TEMPLATE_ABSPATH . '/gallery.php';
            $template = FILE_GALLERY_DEFAULT_TEMPLATE_NAME;
        } else {
            $template_file = FILE_GALLERY_ABSPATH . '/templates/' . $template . '/gallery.php';
        }
    }
    // check if template exists and replace with default if it does not
    if (!is_readable($template_file)) {
        $template_file = FILE_GALLERY_ABSPATH . '/templates/default/gallery.php';
        $template = 'default';
    }
    // get overriding variables from the template file
    $overriding = true;
    ob_start();
    include $template_file;
    ob_end_clean();
    $overriding = false;
    if (is_array($file_gallery->overrides) && !empty($file_gallery->overrides)) {
        extract($file_gallery->overrides);
        $file_gallery->overrides = NULL;
    }
    $limit = (int) $limit;
    $offset = (int) $offset;
    $page = (int) get_query_var('page');
    if ('false' === $rel || is_numeric($rel) && 0 === (int) $rel) {
        $_rel = false;
    } elseif (1 === $rel) {
        $_rel = true;
    } else {
        $_rel = $rel;
    }
    if ('false' === $output_params || is_numeric($output_params) && 0 === (int) $output_params) {
        $output_params = false;
    } else {
        $output_params = true;
    }
    if ('false' === $paginate || is_numeric($paginate) && 0 === (int) $paginate || 0 > $limit) {
        $paginate = false;
        $found_rows = '';
    } else {
        $paginate = true;
        $found_rows = 'SQL_CALC_FOUND_ROWS';
        if (0 === $page) {
            $page = 1;
        }
        if (is_singular() && 1 < $page) {
            $offset = $limit * ($page - 1);
        }
    }
    $file_gallery->debug_add('pagination', compact('paginate', 'page'));
    if ('' != $include && '' == $attachment_ids) {
        $attachment_ids = $include;
    }
    if (!isset($linkto)) {
        $linkto = $link;
    }
    $sql_mimetype = '';
    if ('' != $mimetype) {
        $mimetype = file_gallery_get_mime_type($mimetype);
        $sql_mimetype = wp_post_mime_type_where($mimetype);
    }
    $approved_attachment_post_statuses = apply_filters('file_gallery_approved_attachment_post_statuses', array('inherit', 'draft'));
    $ignored_attachment_post_statuses = apply_filters('file_gallery_ignored_attachment_post_statuses', array('trash', 'private', 'pending', 'future'));
    if (!empty($approved_attachment_post_statuses)) {
        $post_statuses = " AND ({$wpdb->posts}.post_status IN ('" . implode("', '", $approved_attachment_post_statuses) . "') ) ";
    } elseif (!empty($ignored_attachment_post_statuses)) {
        $post_statuses = " AND ({$wpdb->posts}.post_status NOT IN ('" . implode("', '", $ignored_attachment_post_statuses) . "') ) ";
    } else {
        $post_statuses = "";
    }
    $file_gallery_query = new stdClass();
    // start with tags because they negate everything else
    if ('' != $tags) {
        if ('' == $orderby || 'file_gallery' == $orderby) {
            $orderby = "menu_order ID";
        }
        $query = array('post_status' => implode(',', $approved_attachment_post_statuses), 'post_type' => 'attachment', 'order' => $order, 'orderby' => $orderby, 'posts_per_page' => $limit, 'post_mime_type' => $mimetype, FILE_GALLERY_MEDIA_TAG_NAME => $tags);
        if ('current' == $tags_from) {
            $query['post_parent'] = $id;
        }
        if (!empty($exclude)) {
            $query['post__not_in'] = explode(',', preg_replace('/[^0-9,]+/', '', $exclude));
        }
        if (0 < $offset) {
            $query['offset'] = $offset;
        }
        $file_gallery_query = new WP_Query($query);
        $attachments = $file_gallery_query->posts;
        unset($query);
    } elseif ('' != $attachment_ids) {
        $attachment_ids = trim($attachment_ids, ',');
        $attachment_ids = explode(',', $attachment_ids);
        $sql_limit = count($attachment_ids);
        if ('rand' == $orderby) {
            shuffle($attachment_ids);
        }
        $attachment_ids = implode(',', $attachment_ids);
        if ('' == $orderby || 'rand' == $orderby) {
            $orderby = sprintf("FIELD(ID,'%s')", str_replace(",", "','", $attachment_ids));
            $order = '';
        } elseif ('title' == $orderby) {
            $orderby = "{$wpdb->posts}.post_title";
        }
        $query = sprintf("SELECT " . $found_rows . " * FROM {$wpdb->posts} \n\t\t\t WHERE {$wpdb->posts}.ID IN (%s) \n\t\t\t AND {$wpdb->posts}.post_type = 'attachment' \n\t\t\t" . $post_statuses . " ", $attachment_ids);
        $query .= $sql_mimetype;
        $query .= sprintf(" ORDER BY %s %s ", $orderby, $order);
        if (true !== $paginate) {
            $limit = $sql_limit;
        }
    } else {
        if ('' == $orderby) {
            $orderby = "menu_order ID";
        }
        $query = array('post_parent' => $id, 'post_status' => implode(',', $approved_attachment_post_statuses), 'post_type' => 'attachment', 'order' => $order, 'orderby' => $orderby, 'posts_per_page' => $limit, 'post_mime_type' => $mimetype);
        if (!empty($exclude)) {
            $query['post__not_in'] = explode(',', preg_replace('/[^0-9,]+/', '', $exclude));
        }
        if (0 < $offset) {
            $query['offset'] = $offset;
        }
        $file_gallery_query = new WP_Query($query);
        $attachments = $file_gallery_query->posts;
        unset($query);
    }
    if (isset($query)) {
        if (0 < $limit) {
            $query .= " LIMIT " . $limit;
        }
        if (0 < $offset) {
            $query .= " OFFSET " . $offset;
        }
        $attachments = $wpdb->get_results($query);
        if ('' != $found_rows) {
            $file_gallery_query->found_posts = $wpdb->get_var("SELECT FOUND_ROWS()");
            $file_gallery_query->max_num_pages = ceil($file_gallery_query->found_posts / $limit);
        }
    }
    if (empty($attachments)) {
        return '<!-- "File Gallery" plugin says: - No attachments found for the following shortcode arguments: "' . json_encode($attr) . '" -->';
    }
    // feed
    if (is_feed()) {
        $output = "\n";
        foreach ($attachments as $attachment) {
            $output .= wp_get_attachment_link($attachment->ID, $size, true) . "\n";
        }
        return $output;
    }
    $i = 0;
    $unique_ids = array();
    $gallery_items = '';
    if ('object' == $output_type || 'array' == $output_type) {
        $gallery_items = array();
    }
    $autoqueueclasses = array();
    if (defined('FILE_GALLERY_LIGHTBOX_CLASSES')) {
        $autoqueueclasses = maybe_unserialize(FILE_GALLERY_LIGHTBOX_CLASSES);
    } else {
        $autoqueueclasses = explode(',', $options['auto_enqueued_scripts']);
    }
    $file_gallery_this_template_counter = 1;
    // create output
    foreach ($attachments as $attachment) {
        $param = array('image_class' => $imageclass, 'link_class' => $linkclass, 'rel' => $_rel, 'title' => '', 'caption' => '', 'description' => '', 'thumb_alt' => '');
        $attachment_file = get_attached_file($attachment->ID);
        $attachment_is_image = file_gallery_file_is_displayable_image($attachment_file);
        $endcol = '';
        $x = '';
        if ($output_params) {
            $plcai = array_intersect($autoqueueclasses, explode(' ', trim($linkclass)));
            if (!empty($plcai)) {
                if ($attachment_is_image) {
                    if (true === $param['rel']) {
                        $param['rel'] = $plcai[0] . '[' . $file_gallery->gallery_id . ']';
                    } elseif (!is_bool($param['rel'])) {
                        if (false !== strpos($_rel, '$GID$')) {
                            $param['rel'] = str_replace('$GID$', $file_gallery->gallery_id, $_rel);
                        } else {
                            $param['rel'] = $_rel . '[' . $file_gallery->gallery_id . ']';
                        }
                    }
                    $filter_args = array('gallery_id' => $file_gallery->gallery_id, 'linkrel' => $param['rel'], 'linkclass' => $param['link_class'], 'imageclass' => $param['image_class']);
                    if ($param['rel']) {
                        $param['rel'] = apply_filters('file_gallery_lightbox_linkrel', $param['rel'], 'linkrel', $filter_args);
                    }
                    $param['link_class'] = apply_filters('file_gallery_lightbox_linkclass', $param['link_class'], 'linkclass', $filter_args);
                    $param['image_class'] = apply_filters('file_gallery_lightbox_imageclass', $param['image_class'], 'imageclass', $filter_args);
                } else {
                    $param['link_class'] = str_replace(trim(implode(' ', $plcai)), '', trim($linkclass));
                }
            }
            // if rel is still true or false
            if (is_bool($param['rel'])) {
                $param['rel'] = '';
            }
            switch ($linkto) {
                case 'parent_post':
                    $param['link'] = get_permalink($wpdb->get_var("SELECT post_parent FROM {$wpdb->posts} WHERE ID = '" . $attachment->ID . "'"));
                    break;
                case 'file':
                    $param['link'] = wp_get_attachment_url($attachment->ID);
                    break;
                case 'attachment':
                    $param['link'] = get_attachment_link($attachment->ID);
                    break;
                case 'none':
                    $param['link'] = '';
                    break;
                default:
                    // external url
                    $param['link'] = urldecode($linkto);
                    break;
            }
            $param['title'] = $attachment->post_title;
            $param['caption'] = $attachment->post_excerpt;
            $param['description'] = $attachment->post_content;
            if ($attachment_is_image) {
                $thumb_src = wp_get_attachment_image_src($attachment->ID, $size);
                $param['thumb_link'] = $thumb_src[0];
                $param['thumb_width'] = 0 == $thumb_src[1] ? file_gallery_get_image_size($param['thumb_link']) : $thumb_src[1];
                $param['thumb_height'] = 0 == $thumb_src[2] ? file_gallery_get_image_size($param['thumb_link'], true) : $thumb_src[2];
                if ('' != $param['link'] && 'full' != $link_size && in_array($link_size, file_gallery_get_intermediate_image_sizes())) {
                    $full_src = wp_get_attachment_image_src($attachment->ID, $link_size);
                    $param['link'] = $full_src[0];
                }
            } else {
                $param['thumb_link'] = file_gallery_https(FILE_GALLERY_CRYSTAL_URL) . '/' . file_gallery_get_file_type($attachment->post_mime_type) . '.png';
                $param['thumb_link'] = apply_filters('file_gallery_non_image_thumb_link', $param['thumb_link'], $attachment->post_mime_type, $attachment->ID);
                $param['thumb_width'] = '46';
                $param['thumb_height'] = '60';
            }
            if ($thumb_alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true)) {
                $param['thumb_alt'] = $thumb_alt;
            }
            $param['attachment_id'] = $attachment->ID;
        }
        $param = array_map('trim', $param);
        if ('object' == $output_type) {
            if ($output_params) {
                $attachment->params = (object) $param;
            }
            $gallery_items[] = $attachment;
        } elseif ('array' == $output_type || 'json' == $output_type) {
            if ($output_params) {
                $attachment->params = $param;
            }
            $gallery_items[] = get_object_vars($attachment);
        } else {
            // add the column break class and append a line break...
            if ($columns > 0 && ++$i % $columns == 0) {
                $endcol = ' gallery-endcol';
            }
            // parse template
            ob_start();
            extract($param);
            include $template_file;
            $x = ob_get_contents();
            ob_end_clean();
            $file_gallery_this_template_counter++;
            if ($columns > 0 && $i % $columns == 0) {
                $x .= $cleartag;
            }
            $gallery_items .= $x;
        }
    }
    // handle data types
    if ('object' == $output_type || 'array' == $output_type) {
        $output = $gallery_items;
    } elseif ('json' == $output_type) {
        $output = json_encode($gallery_items);
    } else {
        $stc = '';
        $cols = '';
        $pagination_html = '';
        if (0 < (int) $columns) {
            $cols = ' columns_' . $columns;
        }
        if (isset($starttag_class) && '' != $starttag_class) {
            $stc = ' ' . $starttag_class;
        }
        $trans_append = "\n<!-- file gallery output cached on " . date('Y.m.d @ H:i:s', time()) . "-->\n";
        if (is_singular() && isset($file_gallery_query->max_num_pages) && 1 < $file_gallery_query->max_num_pages) {
            $pagination_html = file_gallery_do_pagination($file_gallery_query->max_num_pages, $page);
        }
        $gallery_class = apply_filters('file_gallery_galleryclass', 'gallery ' . str_replace(' ', '-', $template) . $cols . $stc . ' ' . $galleryclass);
        $output = '<' . $starttag . ' id="gallery-' . $file_gallery->gallery_id . '" class="' . $gallery_class . '">' . "\n" . $gallery_items . "\n" . $pagination_html . "\n</" . $starttag . '>';
    }
    if (isset($options['cache']) && true == $options['cache']) {
        if ('html' == $output_type) {
            set_transient($transient, $output . $trans_append, $options['cache_time']);
        } elseif (isset($options['cache_non_html_output']) && true == $options['cache_non_html_output']) {
            set_transient($transient, $output, $options['cache_time']);
        }
    }
    return apply_filters('file_gallery_output', $output, $post->ID, $file_gallery->gallery_id);
}
/**
 * This function displays attachment data inside an HTML 
 * form. It allows attachment data to be viewed as well as edited.
 */
function file_gallery_edit_attachment()
{
    check_ajax_referer('file-gallery');
    $type = 'image';
    $media_tags = array();
    $options = get_option('file_gallery');
    $attachment_id = (int) $_POST['attachment_id'];
    $attachment = get_post($attachment_id);
    if (!$attachment) {
        printf(__('Attachment with ID <strong>%d</strong> does not exist!', 'file-gallery'), $attachment_id);
        exit;
    }
    if (file_gallery_file_is_displayable_image(get_attached_file($attachment->ID))) {
        $fullsize_src = wp_get_attachment_image_src($attachment->ID, 'large', false);
        $fullsize_src = $fullsize_src[0];
        $size_src = wp_get_attachment_image_src($attachment->ID, 'medium', false);
        $size_src = $size_src[0];
    } else {
        $fullsize_src = wp_get_attachment_url($attachment->ID);
        $size_src = file_gallery_https(FILE_GALLERY_CRYSTAL_URL) . '/' . file_gallery_get_file_type($attachment->post_mime_type) . '.png';
        $type = 'document';
    }
    $post_author = get_userdata($attachment->post_author);
    $post_author = $post_author->user_nicename;
    $tags = wp_get_object_terms($attachment->ID, FILE_GALLERY_MEDIA_TAG_NAME);
    foreach ($tags as $tag) {
        $media_tags[] = $tag->name;
    }
    $media_tags = implode(', ', $media_tags);
    $has_copies = maybe_unserialize(get_post_meta($attachment->ID, '_has_copies', true));
    $is_copy = get_post_meta($attachment->ID, '_is_copy_of', true);
    do_action('file_gallery_edit_attachment', $attachment->ID);
    ?>
	<div id="file_gallery_attachment_edit_image">
		<?php 
    if ('image' == $type) {
        ?>
		<a href="<?php 
        echo $fullsize_src;
        ?>
" title="" class="attachment_edit_thumb"><img src="<?php 
        echo $size_src;
        ?>
" alt="image" /></a>
		<p>
			<a href="#" id="regenerate[<?php 
        echo $attachment->ID;
        ?>
]" class="file_gallery_regenerate"><?php 
        _e("Regenerate this image's thumbnails", "file-gallery");
        ?>
</a>
		</p>
		<?php 
    } else {
        ?>
		<img src="<?php 
        echo $size_src;
        ?>
" alt="image" />
		<?php 
    }
    ?>
		<br />
		<div id="attachment_data">
			<p><strong><?php 
    _e('ID:', 'file-gallery');
    ?>
</strong> <a href="<?php 
    echo admin_url('media.php?attachment_id=' . $attachment->ID . '&action=edit&TB_iframe=1');
    ?>
" class="thickbox" onclick="return false;"><?php 
    echo $attachment->ID;
    ?>
</a></p>
			<p><strong><?php 
    _e('Date uploaded:', 'file-gallery');
    ?>
</strong><br /><?php 
    echo date(get_option('date_format'), strtotime($attachment->post_date));
    ?>
</p>
			<p><strong><?php 
    _e('Uploaded by:', 'file-gallery');
    ?>
</strong> <?php 
    echo $post_author;
    ?>
</p>
			<?php 
    if (is_array($has_copies)) {
        ?>
			<p class="attachment_info_has_copies"><?php 
        _e('IDs of copies of this attachment:', 'file-gallery');
        ?>
 <strong><?php 
        foreach ($has_copies as $c) {
            echo '<a href="' . admin_url('media.php?attachment_id=' . $c . '&action=edit') . '" target="_blank">' . $c . '</a>';
        }
        ?>
</strong></p>
			<?php 
    }
    ?>
			<?php 
    if ($is_copy) {
        ?>
			<p class="attachment_info_is_a_copy"><?php 
        _e('This attachment is a copy of attachment ID', 'file-gallery');
        ?>
 <strong><?php 
        echo '<a href="' . admin_url('media.php?attachment_id=' . $is_copy . '&action=edit') . '" target="_blank">' . $is_copy . '</a>';
        ?>
</strong></p>
			<?php 
    }
    ?>

			
		</div>
	</div>
	
<?php 
    do_action('file_gallery_pre_edit_attachment_post_form', $attachment->ID);
    ?>
	
	<div id="attachment_data_edit_form">
	
		<input type="hidden" name="post_id" id="fgae_post_id" value="<?php 
    echo $_POST['post_id'];
    ?>
" />
		<input type="hidden" name="attachment_id" id="fgae_attachment_id" value="<?php 
    echo $_POST['attachment_id'];
    ?>
" />
		<input type="hidden" name="attachment_order" id="fgae_attachment_order" value="<?php 
    echo $_POST['attachment_order'];
    ?>
" />
		<input type="hidden" name="checked_attachments" id="fgae_checked_attachments" value="<?php 
    echo $_POST['checked_attachments'];
    ?>
" />
		<input type="hidden" name="action"  id="fgae_action"  value="update" />

		<?php 
    if (file_gallery_file_is_displayable_image(get_attached_file($attachment->ID))) {
        ?>
		<label for="post_alt"><?php 
        _e('Alternate text for this image', 'file-gallery');
        ?>
: </label>
		<input type="text" name="post_alt" id="fgae_post_alt" value="<?php 
        echo get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
        ?>
" class="roundborder"<?php 
        if (!current_user_can('edit_post', $attachment->ID)) {
            echo ' readonly="readonly"';
        }
        ?>
 /><br />
		<?php 
    }
    ?>
		
		<label for="post_title"><?php 
    _e('Title', 'file-gallery');
    ?>
: </label>
		<input type="text" name="post_title" id="fgae_post_title" value="<?php 
    echo $attachment->post_title;
    ?>
" class="roundborder"<?php 
    if (!current_user_can('edit_post', $attachment->ID)) {
        echo ' readonly="readonly"';
    }
    ?>
 /><br />
		
		<label for="post_excerpt"><?php 
    _e('Caption', 'file-gallery');
    ?>
: </label>
		<textarea name="post_excerpt" id="fgae_post_excerpt" class="roundborder"<?php 
    if (!current_user_can('edit_post', $attachment->ID)) {
        echo ' readonly="readonly"';
    }
    ?>
><?php 
    echo $attachment->post_excerpt;
    ?>
</textarea><br />
		
		<label for="post_content"><?php 
    _e('Description', 'file-gallery');
    ?>
: </label>
		<textarea name="post_content" id="fgae_post_content" rows="4" cols="20" class="roundborder"<?php 
    if (!current_user_can('edit_post', $attachment->ID)) {
        echo ' readonly="readonly"';
    }
    ?>
><?php 
    echo $attachment->post_content;
    ?>
</textarea><br />
		
		<label for="tax_input"><?php 
    _e('Media tags (separate each tag with a comma)', 'file-gallery');
    ?>
: </label>
		<input type="text" name="tax_input" id="fgae_tax_input" value="<?php 
    echo $media_tags;
    ?>
" class="roundborder"<?php 
    if (!current_user_can('edit_post', $attachment->ID)) {
        echo ' readonly="readonly"';
    }
    ?>
 /><br />
		
		<label for="menu_order"><?php 
    _e('Menu order', 'file-gallery');
    ?>
: </label>
		<input type="text" name="menu_order" id="fgae_menu_order" value="<?php 
    echo $attachment->menu_order;
    ?>
" class="roundborder"<?php 
    if (!current_user_can('edit_post', $attachment->ID)) {
        echo ' readonly="readonly"';
    }
    ?>
 /><br />
		
		<label for="attachment_uri"><?php 
    _e('Attachment file URL:', 'file-gallery');
    ?>
</label>
		<input type="text" name="attachment_uri" id="fgae_attachment_uri" readonly="readonly" value="<?php 
    echo $fullsize_src;
    ?>
" class="roundborder" />
        
        <br />
        <br />
        
		<?php 
    if (isset($options['display_acf']) && true == $options['display_acf']) {
        file_gallery_attachment_custom_fields_table($attachment->ID);
    }
    ?>
		
		<input type="button" id="file_gallery_edit_attachment_save" value="<?php 
    _e('save and return', 'file-gallery');
    ?>
" class="button-primary" />
		<input type="button" id="file_gallery_edit_attachment_cancel"value="<?php 
    _e('cancel and return', 'file-gallery');
    ?>
" class="button-secondary" />
	
	</div>	
<?php 
    do_action('file_gallery_edit_attachment_post_form', $attachment->ID);
    exit;
}