-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathattachment.php
159 lines (137 loc) · 4.47 KB
/
attachment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
// This file is part of the Carrington Core Platform for WordPress
// http://crowdfavorite.com/carrington-core/
//
// Copyright (c) 2008-2013 Crowd Favorite, Ltd. All rights reserved.
// http://crowdfavorite.com
//
// Released under the GPL license
// http://www.opensource.org/licenses/gpl-license.php
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************
/**
* Get the link of an adjacent image
*
* @param bool $prev Whether or not there is a previous image
* @return string URL of an adjacent image
*
**/
function cfct_get_adjacent_image_link($prev = true) {
global $post;
$attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') ));
foreach ( $attachments as $k => $attachment ) {
if ( $attachment->ID == $post->ID ) {
break;
}
$k = $prev ? $k - 1 : $k + 1;
if ( isset($attachments[$k]) )
return wp_get_attachment_link($attachments[$k]->ID, 'thumbnail', true);
}
}
/**
* Create a gallery from post attachments. Overrides WordPress default shortcode post gallery code.
*
* @param $unused not used, required by filter hook
* @param array $attr List of attributes that are populated by post gallery shortcode
* @return string Markup to display a gallery
*
**/
function cfct_post_gallery($unused, $attr) {
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);
$children_args = array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $order,
'orderby' => $orderby,
);
if (!empty($include)) {
$children_args['include'] = $include;
}
else if (!empty($exclude)) {
$children_args['exclude'] = $exclude;
}
$attachments = get_children($children_args);
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $id => $attachment )
$output .= wp_get_attachment_link($id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = apply_filters('cfct_post_gallery_columns', intval($columns));
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$output = apply_filters('gallery_style', '
<style type="text/css">
.post-'.$id.' .gallery {
margin: auto;
}
.post-'.$id.' .gallery-item {
float: left;
margin-top: 10px;
text-align: center;
width: '.$itemwidth.'%; }
.post-'.$id.' .gallery img {
border: 2px solid #cfcfcf;
}
.post-'.$id.' .gallery-caption {
margin-left: 0;
}
</style>
<!-- see cfct_post_gallery() in carrington-core/attachment.php -->
<div class="gallery">');
$i = 0;
foreach ( $attachments as $id => $attachment ) {
// get full item src
$item_src = wp_get_attachment_image_src($id, 'full', false);
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
// add full item src as rel
$link = str_replace('><img', ' class="thickbox" rel="'.$item_src[0].'"><img', $link);
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='gallery-caption'>
{$attachment->post_excerpt}
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
$output .= "
<br style='clear: both;' />
</div>\n";
return $output;
}
add_filter('post_gallery', 'cfct_post_gallery', 10, 2);