root/tags/2.1/wp-includes/post-template.php

Revision 4748, 11.7 kB (checked in by ryan, 2 years ago)

Send to Editor fixes from mdawaffe. fixes #3594

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 //
4 // "The Loop" post functions
5 //
6
7 function the_ID() {
8     global $id;
9     echo $id;
10 }
11
12
13 function get_the_ID() {
14     global $id;
15     return $id;
16 }
17
18
19 function the_title($before = '', $after = '', $echo = true) {
20     $title = get_the_title();
21     if ( strlen($title) > 0 ) {
22         $title = apply_filters('the_title', $before . $title . $after, $before, $after);
23         if ( $echo )
24             echo $title;
25         else
26             return $title;
27     }
28 }
29
30
31 function get_the_title($id = 0) {
32     $post = &get_post($id);
33
34     $title = $post->post_title;
35     if ( !empty($post->post_password) )
36         $title = sprintf(__('Protected: %s'), $title);
37     else if ( 'private' == $post->post_status )
38         $title = sprintf(__('Private: %s'), $title);
39
40     return $title;
41 }
42
43 function the_guid( $id = 0 ) {
44     echo get_the_guid($id);
45 }
46
47 function get_the_guid( $id = 0 ) {
48     $post = &get_post($id);
49
50     return apply_filters('get_the_guid', $post->guid);
51 }
52
53 function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
54     $content = get_the_content($more_link_text, $stripteaser, $more_file);
55     $content = apply_filters('the_content', $content);
56     $content = str_replace(']]>', ']]&gt;', $content);
57     echo $content;
58 }
59
60
61 function get_the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
62     global $id, $post, $more, $single, $withcomments, $page, $pages, $multipage, $numpages;
63     global $preview;
64     global $pagenow;
65     $output = '';
66
67     if ( !empty($post->post_password) ) { // if there's a password
68         if ( stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password ) {    // and it doesn't match the cookie
69             $output = get_the_password_form();
70             return $output;
71         }
72     }
73
74     if ( $more_file != '' )
75         $file = $more_file;
76     else
77         $file = $pagenow; //$_SERVER['PHP_SELF'];
78
79     if ( $page > count($pages) ) // if the requested page doesn't exist
80         $page = count($pages); // give them the highest numbered page that DOES exist
81
82     $content = $pages[$page-1];
83     if ( preg_match('/<!--more(.+?)?-->/', $content, $matches) ) {
84         $content = explode($matches[0], $content, 2);
85         if ( !empty($matches[1]) && !empty($more_link_text) )
86             $more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
87     } else {
88         $content = array($content);
89     }
90     if ( (false !== strpos($post->post_content, '<!--noteaser-->') && ((!$multipage) || ($page==1))) )
91         $stripteaser = 1;
92     $teaser = $content[0];
93     if ( ($more) && ($stripteaser) )
94         $teaser = '';
95     $output .= $teaser;
96     if ( count($content) > 1 ) {
97         if ( $more ) {
98             $output .= '<a id="more-'.$id.'"></a>'.$content[1];
99         } else {
100             $output = balanceTags($output);
101             if ( ! empty($more_link_text) )
102                 $output .= ' <a href="'. get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>";
103         }
104             
105     }
106     if ( $preview ) // preview fix for javascript bug with foreign languages
107         $output =    preg_replace('/\%u([0-9A-F]{4,4})/e',    "'&#'.base_convert('\\1',16,10).';'", $output);
108
109     return $output;
110 }
111
112
113 function the_excerpt() {
114     echo apply_filters('the_excerpt', get_the_excerpt());
115 }
116
117
118 function get_the_excerpt($fakeit = true) {
119     global $id, $post;
120     $output = '';
121     $output = $post->post_excerpt;
122     if ( !empty($post->post_password) ) { // if there's a password
123         if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) {  // and it doesn't match the cookie
124             $output = __('There is no excerpt because this is a protected post.');
125             return $output;
126         }
127     }
128
129     return apply_filters('get_the_excerpt', $output);
130 }
131
132
133 function wp_link_pages($args = '') {
134     if ( is_array($args) )
135         $r = &$args;
136     else
137         parse_str($args, $r);
138
139     $defaults = array('before' => '<p>' . __('Pages:'), 'after' => '</p>', 'next_or_number' => 'number', 'nextpagelink' => __('Next page'),
140             'previouspagelink' => __('Previous page'), 'pagelink' => '%', 'more_file' => '', 'echo' => 1);
141     $r = array_merge($defaults, $r);
142     extract($r);
143
144     global $id, $page, $numpages, $multipage, $more, $pagenow;
145     if ( $more_file != '' )
146         $file = $more_file;
147     else
148         $file = $pagenow;
149
150     $output = '';
151     if ( $multipage ) {
152         if ( 'number' == $next_or_number ) {
153             $output .= $before;
154             for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
155                 $j = str_replace('%',"$i",$pagelink);
156                 $output .= ' ';
157                 if ( ($i != $page) || ((!$more) && ($page==1)) ) {
158                     if ( '' == get_option('permalink_structure') )
159                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">';
160                     else
161                         $output .= '<a href="' . trailingslashit(get_permalink()) . $i . '/">';
162                 }
163                 $output .= $j;
164                 if ( ($i != $page) || ((!$more) && ($page==1)) )
165                     $output .= '</a>';
166             }
167             $output .= $after;
168         } else {
169             if ( $more ) {
170                 $output .= $before;
171                 $i = $page - 1;
172                 if ( $i && $more ) {
173                     if ( '' == get_option('permalink_structure') )
174                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">' . $previouspagelink . '</a>';
175                     else
176                         $output .= '<a href="' . get_permalink() . $i . '/">'.$previouspagelink.'</a>';
177                 }
178                 $i = $page + 1;
179                 if ( $i <= $numpages && $more ) {
180                     if ( '' == get_option('permalink_structure') )
181                         $output .= '<a href="' . get_permalink() . '&amp;page=' . $i . '">'.$nextpagelink.'</a>';
182                     else
183                         $output .= '<a href="' . trailingslashit(get_permalink()) . $i . '/">' . $nextpagelink . '</a>';
184                 }
185                 $output .= $after;
186             }
187         }
188     }
189
190     if ( $echo )
191         echo $output;
192
193     return $output;
194 }
195
196
197 //
198 // Post-meta: Custom per-post fields.
199 //
200
201
202 function post_custom( $key = '' ) {
203     $custom = get_post_custom();
204
205     if ( 1 == count($custom[$key]) )
206         return $custom[$key][0];
207     else
208         return $custom[$key];
209 }
210
211
212 // this will probably change at some point...
213 function the_meta() {
214     global $id;
215
216     if ( $keys = get_post_custom_keys() ) {
217         echo "<ul class='post-meta'>\n";
218         foreach ( $keys as $key ) {
219             $keyt = trim($key);
220             if ( '_' == $keyt{0} )
221                 continue;
222             $values = array_map('trim', get_post_custom_values($key));
223             $value = implode($values,', ');
224             echo "<li><span class='post-meta-key'>$key:</span> $value</li>\n";
225         }
226         echo "</ul>\n";
227     }
228 }
229
230
231 //
232 // Pages
233 //
234
235 function wp_dropdown_pages($args = '') {
236     if ( is_array($args) )
237         $r = &$args;
238     else
239         parse_str($args, $r);
240
241     $defaults = array('depth' => 0, 'child_of' => 0, 'selected' => 0, 'echo' => 1,
242         'name' => 'page_id', 'show_option_none' => '');
243     $r = array_merge($defaults, $r);
244     extract($r);
245
246     $pages = get_pages($r);
247     $output = '';
248
249     if ( ! empty($pages) ) {
250         $output = "<select name='$name'>\n";
251         if ( $show_option_none )
252             $output .= "\t<option value=''>$show_option_none</option>\n";
253         $output .= walk_page_dropdown_tree($pages, $depth, $r);
254         $output .= "</select>\n";
255     }
256
257     $output = apply_filters('wp_dropdown_pages', $output);
258
259     if ( $echo )
260         echo $output;
261
262     return $output;
263 }
264
265 function wp_list_pages($args = '') {
266     if ( is_array($args) )
267         $r = &$args;
268     else
269         parse_str($args, $r);
270
271     $defaults = array('depth' => 0, 'show_date' => '', 'date_format' => get_option('date_format'),
272         'child_of' => 0, 'exclude' => '', 'title_li' => __('Pages'), 'echo' => 1, 'authors' => '');
273     $r = array_merge($defaults, $r);
274
275     $output = '';
276
277     // sanitize, mostly to keep spaces out
278     $r['exclude'] = preg_replace('[^0-9,]', '', $r['exclude']);
279
280     // Allow plugins to filter an array of excluded pages
281     $r['exclude'] = implode(',', apply_filters('wp_list_pages_excludes', explode(',', $r['exclude'])));
282
283     // Query pages.
284     $pages = get_pages($r);
285
286     if ( !empty($pages) ) {
287         if ( $r['title_li'] )
288             $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
289
290         global $wp_query;
291         $current_page = $wp_query->get_queried_object_id();
292         $output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
293
294         if ( $r['title_li'] )
295             $output .= '</ul></li>';
296     }
297
298     $output = apply_filters('wp_list_pages', $output);
299
300     if ( $r['echo'] )
301         echo $output;
302     else
303         return $output;
304 }
305
306 //
307 // Page helpers
308 //
309
310 function walk_page_tree() {
311     $walker = new Walker_Page;
312     $args = func_get_args();
313     return call_user_func_array(array(&$walker, 'walk'), $args);
314 }
315
316 function walk_page_dropdown_tree() {
317     $walker = new Walker_PageDropdown;
318     $args = func_get_args();
319     return call_user_func_array(array(&$walker, 'walk'), $args);
320 }
321
322 //
323 // Attachments
324 //
325
326 function the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
327     echo get_the_attachment_link($id, $fullsize, $max_dims);
328 }
329
330 function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
331     $id = (int) $id;
332     $_post = & get_post($id);
333
334     if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url() )
335         return __('Missing Attachment');
336
337     $post_title = attribute_escape($_post->post_title);
338
339     $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
340     return "<a href='$url' title='$post_title'>$innerHTML</a>";
341 }
342
343 function get_attachment_icon_src( $id = 0, $fullsize = false ) {
344     $id = (int) $id;
345     if ( !$post = & get_post($id) )
346         return false;
347
348     $imagedata = wp_get_attachment_metadata( $post->ID );
349
350     $file = get_attached_file( $post->ID );
351
352     if ( !$fullsize && $thumbfile = wp_get_attachment_thumb_file( $post->ID ) ) {
353         // We have a thumbnail desired, specified and existing
354
355         $src = wp_get_attachment_thumb_url( $post->ID );
356         $src_file = $thumbfile;
357         $class = 'attachmentthumb';
358     } elseif ( wp_attachment_is_image( $post->ID ) ) {
359         // We have an image without a thumbnail
360
361         $src = wp_get_attachment_url( $post->ID );
362         $src_file = & $file;
363         $class = 'attachmentimage';
364     } elseif ( $src = wp_mime_type_icon( $post->ID ) ) {
365         // No thumb, no image. We'll look for a mime-related icon instead.
366
367         $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' );
368         $src_file = $icon_dir . '/' . basename($src);
369     }
370
371     if ( !isset($src) || !$src )
372         return false;
373
374     return array($src, $src_file);
375 }
376
377 function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {
378     $id = (int) $id;
379     if ( !$post = & get_post($id) )
380         return false;
381
382     if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) )
383         return false;
384
385     list($src, $src_file) = $src;
386
387     // Do we need to constrain the image?
388     if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) {
389
390         $imagesize = getimagesize($src_file);
391
392         if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {
393             $actual_aspect = $imagesize[0] / $imagesize[1];
394             $desired_aspect = $max_dims[0] / $max_dims[1];
395
396             if ( $actual_aspect >= $desired_aspect ) {
397                 $height = $actual_aspect * $max_dims[0];
398                 $constraint = "width='{$max_dims[0]}' ";
399                 $post->iconsize = array($max_dims[0], $height);
400             } else {
401                 $width = $max_dims[1] / $actual_aspect;
402                 $constraint = "height='{$max_dims[1]}' ";
403                 $post->iconsize = array($width, $max_dims[1]);
404             }
405         } else {
406             $post->iconsize = array($imagesize[0], $imagesize[1]);
407         }
408     }
409
410     $post_title = attribute_escape($post->post_title);
411
412     $icon = "<img src='$src' title='$post_title' alt='$post_title' $constraint/>";
413
414     return apply_filters( 'attachment_icon', $icon, $post->ID );
415 }
416
417 function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) {
418     $id = (int) $id;
419     if ( !$post = & get_post($id) )
420         return false;
421
422     if ( $innerHTML = get_attachment_icon($post->ID, $fullsize, $max_dims))
423         return $innerHTML;
424
425
426     $innerHTML = attribute_escape($post->post_title);
427
428     return apply_filters('attachment_innerHTML', $innerHTML, $post->ID);
429 }
430
431 function prepend_attachment($content) {
432     $p = '<p class="attachment">';
433     $p .= get_the_attachment_link(false, true, array(400, 300));
434     $p .= '</p>';
435     $p = apply_filters('prepend_attachment', $p);
436
437     return "$p\n$content";
438 }
439
440 //
441 // Misc
442 //
443
444 function get_the_password_form() {
445     $output = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
446     <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
447     <p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . __("Submit") . '" /></p>
448     </form>
449     ';
450     return $output;
451 }
452
453 ?>
454
Note: See TracBrowser for help on using the browser.