public function wp_gallery_with_lightbox($attr = array(), $instance = 0) { global $post; // 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(shortcode_atts(array('order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post->ID, 'itemtag' => 'dl', 'icontag' => 'dt', 'captiontag' => 'dd', 'columns' => 3, 'size' => 'thumbnail', 'include' => '', 'exclude' => ''), $attr)); $id = intval($id); if ('RAND' == $order) { $orderby = 'none'; } if (!empty($include)) { $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby)); $attachments = array(); foreach ($_attachments as $key => $val) { $attachments[$val->ID] = $_attachments[$key]; } } elseif (!empty($exclude)) { $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby)); } else { $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby)); } if (empty($attachments)) { return ''; } if (is_feed()) { $output = "\n"; foreach ($attachments as $att_id => $attachment) { $output .= wp_get_attachment_link($att_id, $size, true) . "\n"; } return $output; } $itemtag = tag_escape($itemtag); $captiontag = tag_escape($captiontag); $icontag = tag_escape($icontag); $valid_tags = wp_kses_allowed_html('post'); if (!isset($valid_tags[$itemtag])) { $itemtag = 'dl'; } if (!isset($valid_tags[$captiontag])) { $captiontag = 'dd'; } if (!isset($valid_tags[$icontag])) { $icontag = 'dt'; } $columns = intval($columns); $itemwidth = $columns > 0 ? floor(100 / $columns) : 100; $float = is_rtl() ? 'right' : 'left'; $selector = "gallery-{$instance}"; $attachments_data = array(); foreach ($attachments as $id => $attachment) { $data = array(); // attachment meta $data['full'] = $data['width'] = $data['height'] = ''; $meta = wp_get_attachment_image_src($id, 'full'); if (!empty($meta)) { $data['full'] = esc_url($meta[0]); $data['width'] = absint($meta[1]); $data['height'] = absint($meta[2]); } $data['thumbnail'] = wp_get_attachment_image_src($id, 'thumbnail'); $data['alt'] = esc_attr(get_post_meta($id, '_wp_attachment_image_alt', true)); $data['caption'] = $attachment->post_excerpt; $data['description'] = $attachment->post_content; $data['title'] = get_the_title($id); $data['video_url'] = esc_url(get_post_meta($id, 'dt-video-url', true)); $data['link'] = esc_url(get_post_meta($id, 'dt-img-link', true)); $data['mime_type_full'] = get_post_mime_type($id); $data['mime_type'] = dt_get_short_post_myme_type($id); $data['ID'] = $id; if (!empty($attr['link']) && 'file' === $attr['link']) { $data['permalink'] = $data['full']; } elseif (!empty($attr['link']) && 'none' === $attr['link']) { $data['permalink'] = ''; } else { $data['permalink'] = get_permalink($id); } $attachments_data[] = apply_filters('presscore_get_attachment_post_data-attachment_data', $data, array_keys($attachments)); } $gallery_style = $gallery_div = ''; if (apply_filters('use_default_gallery_style', true)) { $gallery_style = "\r\n\t\t\t\t<style type='text/css'>\r\n\t\t\t\t\t\t#{$selector} {\r\n\t\t\t\t\t\t\t\tmargin: auto;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\t#{$selector} .gallery-item {\r\n\t\t\t\t\t\t\t\tfloat: {$float};\r\n\t\t\t\t\t\t\t\tmargin-top: 10px;\r\n\t\t\t\t\t\t\t\ttext-align: center;\r\n\t\t\t\t\t\t\t\twidth: {$itemwidth}%;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\t#{$selector} img {\r\n\t\t\t\t\t\t\t\tborder: 2px solid #cfcfcf;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\t#{$selector} .gallery-caption {\r\n\t\t\t\t\t\t\t\tmargin-left: 0;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t</style>\r\n\t\t\t\t<!-- see gallery_shortcode() in wp-includes/media.php -->"; } $size_class = sanitize_html_class($size); $gallery_div = "<div id='{$selector}' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class} dt-gallery-container'>"; $output = apply_filters('gallery_style', $gallery_style . "\n\t\t" . $gallery_div); $i = 0; foreach ($attachments_data as $attachment) { $id = $attachment['ID']; if (isset($attr['link']) && 'file' == $attr['link']) { $thumbnail_img = wp_get_attachment_image_src($id, $size); $link = sprintf('<a href="%s" class="%s" title="%s" data-dt-img-description="%s"><img src="%s" height="%d" width="%d" alt="%s"></a>', esc_url($attachment['full']), 'rollover rollover-zoom dt-mfp-item mfp-image', esc_attr($attachment['title']), esc_attr($attachment['description']), esc_url($thumbnail_img[0]), $thumbnail_img[1], $thumbnail_img[2], $attachment['alt']); } else { $link = wp_get_attachment_link($id, $size, true, false); } $output .= "<{$itemtag} class='gallery-item'>"; $output .= "\r\n\t\t\t\t\t\t<{$icontag} class='gallery-icon'>\r\n\t\t\t\t\t\t\t\t{$link}\r\n\t\t\t\t\t\t</{$icontag}>"; if ($captiontag && trim($attachment['description'])) { $output .= "\r\n\t\t\t\t\t\t\t\t<{$captiontag} class='wp-caption-text gallery-caption'>\r\n\t\t\t\t\t\t\t\t" . wptexturize($attachment['description']) . "\r\n\t\t\t\t\t\t\t\t</{$captiontag}>"; } $output .= "</{$itemtag}>"; if ($columns > 0 && ++$i % $columns == 0) { $output .= '<br style="clear: both" />'; } } $output .= "\r\n\t\t\t\t\t\t<br style='clear: both;' />\r\n\t\t\t\t</div>\n"; return $output; }
/** * Get related posts attachments data slightly modified. * * @return array Attachments data. */ function presscore_get_related_posts($options = array()) { $default_options = array('select' => 'only', 'exclude_current' => true, 'args' => array()); $options = wp_parse_args($options, $default_options); // exclude current post if in the loop if (in_the_loop() && $options['exclude_current']) { $options['args'] = array_merge($options['args'], array('post__not_in' => array(get_the_ID()))); } $posts = presscore_get_posts_in_categories($options); update_post_thumbnail_cache($posts); $attachments_ids = array(); $attachments_data_override = array(); $posts_data = array(); // get posts attachments id if ($posts->have_posts()) { global $post; $post_back = $post; while ($posts->have_posts()) { $posts->the_post(); // thumbnail or first attachment id if (has_post_thumbnail()) { $attachment_id = get_post_thumbnail_id(); } else { if ($attachment = presscore_get_first_image()) { $attachment_id = $attachment->ID; } else { $attachment_id = 0; } } switch (get_post_type()) { case 'post': $post_meta = presscore_new_posted_on('post'); break; case 'dt_portfolio': $post_meta = presscore_new_posted_on('dt_portfolio'); break; default: $post_meta = presscore_new_posted_on(); } $post_data = array(); ///////////////////////// // attachment data // ///////////////////////// $post_data['full'] = $post_data['width'] = $post_data['height'] = ''; $meta = wp_get_attachment_image_src($attachment_id, 'full'); if (!empty($meta)) { $post_data['full'] = esc_url($meta[0]); $post_data['width'] = absint($meta[1]); $post_data['height'] = absint($meta[2]); } $post_data['thumbnail'] = wp_get_attachment_image_src($attachment_id, 'thumbnail'); $post_data['caption'] = ''; $post_data['video_url'] = esc_url(get_post_meta($attachment_id, 'dt-video-url', true)); $post_data['mime_type_full'] = get_post_mime_type($attachment_id); $post_data['mime_type'] = dt_get_short_post_myme_type($attachment_id); $post_data['ID'] = $attachment_id; $post_data['image_attachment_data'] = array('caption' => $post_data['caption'], 'description' => wp_kses_post(get_post_field('post_content', $attachment_id)), 'title' => presscore_imagee_title_is_hidden($attachment_id) ? '' : get_the_title($attachment_id), 'permalink' => get_permalink($attachment_id), 'video_url' => $post_data['video_url'], 'ID' => $attachment_id); /////////////////// // post data // /////////////////// $post_data['title'] = get_the_title(); $post_data['permalink'] = get_permalink(); $post_data['link'] = presscore_get_project_link('project-link'); $post_data['description'] = get_the_excerpt(); $post_data['alt'] = get_the_title(); $post_data['parent_id'] = get_the_ID(); $post_data['meta'] = $post_meta; // save data $posts_data[] = $post_data; } $post = $post_back; setup_postdata($post); } return $posts_data; }
/** * Get attachments post data. * * @param array $media_items Attachments id's array. * @return array Attachments data. */ function presscore_get_attachment_post_data($media_items, $orderby = 'post__in', $order = 'DESC', $posts_per_page = -1) { if (empty($media_items)) { return array(); } global $post; // sanitize $media_items $media_items = array_diff(array_unique(array_map("absint", $media_items)), array(0)); if (empty($media_items)) { return array(); } // get attachments $query = new WP_Query(array('no_found_rows' => true, 'posts_per_page' => $posts_per_page, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'post__in' => $media_items, 'orderby' => $orderby, 'order' => $order)); $attachments_data = array(); if ($query->have_posts()) { // backup post $post_backup = $post; while ($query->have_posts()) { $query->the_post(); $post_id = get_the_ID(); $data = array(); // attachment meta $data['full'] = $data['width'] = $data['height'] = ''; $meta = wp_get_attachment_image_src($post_id, 'full'); if (!empty($meta)) { $data['full'] = esc_url($meta[0]); $data['width'] = absint($meta[1]); $data['height'] = absint($meta[2]); } $data['thumbnail'] = wp_get_attachment_image_src($post_id, 'thumbnail'); $data['alt'] = esc_attr(get_post_meta($post_id, '_wp_attachment_image_alt', true)); $data['caption'] = wp_kses_post($post->post_excerpt); $data['description'] = wp_kses_post($post->post_content); $data['title'] = get_the_title($post_id); $data['permalink'] = get_permalink($post_id); $data['video_url'] = esc_url(get_post_meta($post_id, 'dt-video-url', true)); $data['link'] = esc_url(get_post_meta($post_id, 'dt-img-link', true)); $data['mime_type_full'] = get_post_mime_type($post_id); $data['mime_type'] = dt_get_short_post_myme_type($post_id); $data['ID'] = $post_id; // attachment meta $data['meta'] = presscore_new_posted_on(); $attachments_data[] = apply_filters('presscore_get_attachment_post_data-attachment_data', $data, $media_items); } // restore post $post = $post_backup; setup_postdata($post); } return $attachments_data; }