root/tags/2.0/wp-includes/template-functions-post.php

Revision 3329, 15.2 kB (checked in by ryan, 3 years ago)

Validation tweaks. fixes #2097

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2
3 function get_the_password_form() {
4     $output = '<form action="' . get_settings('siteurl') . '/wp-pass.php" method="post">
5     <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>
6     <p><label>' . __("Password:") . ' <input name="post_password" type="password" size="20" /></label> <input type="submit" name="Submit" value="Submit" /></p>
7     </form>
8     ';
9     return $output;
10 }
11
12
13 function the_ID() {
14     global $id;
15     echo $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
38     return $title;
39 }
40
41
42 function get_the_guid( $id = 0 ) {
43     $post = &get_post($id);
44
45     return apply_filters('get_the_guid', $post->guid);
46 }
47
48
49 function the_guid( $id = 0 ) {
50     echo get_the_guid($id);
51 }
52
53
54 function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
55     $content = get_the_content($more_link_text, $stripteaser, $more_file);
56     $content = apply_filters('the_content', $content);
57     $content = str_replace(']]>', ']]&gt;', $content);
58     echo $content;
59 }
60
61
62 function get_the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
63     global $id, $post, $more, $single, $withcomments, $page, $pages, $multipage, $numpages;
64     global $preview;
65     global $pagenow;
66     $output = '';
67
68     if ( !empty($post->post_password) ) { // if there's a password
69         if ( stripslashes($_COOKIE['wp-postpass_'.COOKIEHASH]) != $post->post_password ) {    // and it doesn't match the cookie
70             $output = get_the_password_form();
71             return $output;
72         }
73     }
74
75     if ( $more_file != '' )
76         $file = $more_file;
77     else
78         $file = $pagenow; //$_SERVER['PHP_SELF'];
79
80     $content = $pages[$page-1];
81     $content = explode('<!--more-->', $content, 2);
82     if ( (preg_match('/<!--noteaser-->/', $post->post_content) && ((!$multipage) || ($page==1))) )
83         $stripteaser = 1;
84     $teaser = $content[0];
85     if ( ($more) && ($stripteaser) )
86         $teaser = '';
87     $output .= $teaser;
88     if ( count($content) > 1 ) {
89         if ( $more )
90             $output .= '<a id="more-'.$id.'"></a>'.$content[1];
91         else
92             $output .= ' <a href="'. get_permalink() . "#more-$id\">$more_link_text</a>";
93     }
94     if ( $preview ) // preview fix for javascript bug with foreign languages
95         $output =    preg_replace('/\%u([0-9A-F]{4,4})/e',    "'&#'.base_convert('\\1',16,10).';'", $output);
96
97     return $output;
98 }
99
100
101 function the_excerpt() {
102     echo apply_filters('the_excerpt', get_the_excerpt());
103 }
104
105
106 function get_the_excerpt($fakeit = true) {
107     global $id, $post;
108     $output = '';
109     $output = $post->post_excerpt;
110     if ( !empty($post->post_password) ) { // if there's a password
111         if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) {  // and it doesn't match the cookie
112             $output = __('There is no excerpt because this is a protected post.');
113             return $output;
114         }
115     }
116
117     return apply_filters('get_the_excerpt', $output);
118 }
119
120
121 function wp_link_pages($args = '') {
122     parse_str($args, $r);
123     if ( !isset($r['before']) )
124         $r['before'] = '<p>' . __('Pages:');
125     if ( !isset($r['after']) )
126         $r['after'] = '</p>';
127     if ( !isset($r['next_or_number']) )
128         $r['next_or_number'] = 'number';
129     if ( !isset($r['nextpagelink']) )
130         $r['nextpagelink'] = 'Next page';
131     if ( !isset($r['previouspagelink']) )
132         $r['previouspagelink'] = 'Previous page';
133     if ( !isset($r['pagelink']) )
134         $r['pagelink'] = '%';
135     if ( !isset($r['more_file']) )
136         $r['more_file'] = '';
137
138     link_pages($r['before'], $r['after'], $r['next_or_number'], $r['nextpagelink'], $r['previouspagelink'], $r['pagelink'], $r['more_file']);
139 }
140
141
142 function link_pages($before='<br />', $after='<br />', $next_or_number='number', $nextpagelink='next page', $previouspagelink='previous page', $pagelink='%', $more_file='') {
143     global $id, $page, $numpages, $multipage, $more, $pagenow;
144     if ( $more_file != '' )
145         $file = $more_file;
146     else
147         $file = $pagenow;
148     if ( $multipage ) {
149         if ( 'number' == $next_or_number ) {
150             echo $before;
151             for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
152                 $j = str_replace('%',"$i",$pagelink);
153                 echo ' ';
154                 if ( ($i != $page) || ((!$more) && ($page==1)) ) {
155                     if ( '' == get_settings('permalink_structure') )
156                         echo '<a href="' . get_permalink() . '&amp;page=' . $i . '">';
157                     else
158                         echo '<a href="' . trailingslashit( get_permalink() ) . $i . '/">';
159                 }
160                 echo $j;
161                 if ( ($i != $page) || ((!$more) && ($page==1)) )
162                     echo '</a>';
163             }
164             echo $after;
165         } else {
166             if ( $more ) {
167                 echo $before;
168                 $i = $page - 1;
169                 if ( $i && $more ) {
170                     if ( '' == get_settings('permalink_structure') )
171                         echo '<a href="' . get_permalink() . '&amp;page=' . $i . '">'.$previouspagelink.'</a>';
172                     else
173                         echo '<a href="' . get_permalink() . $i . '/">'.$previouspagelink.'</a>';
174                 }
175                 $i = $page + 1;
176                 if ( $i <= $numpages && $more ) {
177                     if ( '' == get_settings('permalink_structure') )
178                         echo '<a href="'.get_permalink() . '&amp;page=' . $i . '">'.$nextpagelink.'</a>';
179                     else
180                         echo '<a href="'.get_permalink().$i.'/">'.$nextpagelink.'</a>';
181                 }
182                 echo $after;
183             }
184         }
185     }
186 }
187
188
189 /*
190 Post-meta: Custom per-post fields.
191 */
192
193
194 function get_post_custom( $post_id = 0 ) {
195     global $id, $post_meta_cache, $wpdb;
196     if ( $post_id )
197         $id = $post_id;
198     if ( isset($post_meta_cache[$id]) ) {
199         return $post_meta_cache[$id];
200     } else {
201         if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta    WHERE post_id = '$id' ORDER BY post_id, meta_key", ARRAY_A) ) {
202             // Change from flat structure to hierarchical:
203             $post_meta_cache = array();
204             foreach ( $meta_list as $metarow ) {
205                 $mpid = $metarow['post_id'];
206                 $mkey = $metarow['meta_key'];
207                 $mval = $metarow['meta_value'];
208
209                 // Force subkeys to be array type:
210                 if ( !isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]) )
211                     $post_meta_cache[$mpid] = array();
212                 if ( !isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]) )
213                     $post_meta_cache[$mpid]["$mkey"] = array();
214
215                 // Add a value to the current pid/key:
216                 $post_meta_cache[$mpid][$mkey][] = $mval;
217             }
218         return $post_meta_cache[$mpid];
219         }
220     }
221 }
222
223
224 function get_post_custom_keys() {
225     global $id, $post_meta_cache;
226
227     if ( !is_array($post_meta_cache[$id]) )
228         return;
229     if ( $keys = array_keys($post_meta_cache[$id]) )
230         return $keys;
231 }
232
233
234 function get_post_custom_values($key='') {
235     global $id, $post_meta_cache;
236
237     return $post_meta_cache[$id][$key];
238 }
239
240
241 function post_custom( $key = '' ) {
242     global $id, $post_meta_cache;
243
244     if ( 1 == count($post_meta_cache[$id][$key]) )
245         return $post_meta_cache[$id][$key][0];
246     else
247         return $post_meta_cache[$id][$key];
248 }
249
250
251 // this will probably change at some point...
252 function the_meta() {
253     global $id, $post_meta_cache;
254
255     if ( $keys = get_post_custom_keys() ) {
256         echo "<ul class='post-meta'>\n";
257         foreach ( $keys as $key ) {
258             $values = array_map('trim',$post_meta_cache[$id][$key]);
259             $value = implode($values,', ');
260             echo "<li><span class='post-meta-key'>$key:</span> $value</li>\n";
261         }
262         echo "</ul>\n";
263     }
264 }
265
266
267 /*
268 Pages
269 */
270
271
272 function &get_page_children($page_id, $pages) {
273     global $page_cache;
274
275     if ( empty($pages) )
276         $pages = &$page_cache;
277
278     $page_list = array();
279     foreach ( $pages as $page ) {
280         if ( $page->post_parent == $page_id ) {
281             $page_list[] = $page;
282             if ( $children = get_page_children($page->ID, $pages) )
283                 $page_list = array_merge($page_list, $children);
284         }
285     }
286     return $page_list;
287 }
288
289
290 function &get_pages($args = '') {
291     global $wpdb;
292
293     parse_str($args, $r);
294
295     if ( !isset($r['child_of']) )
296         $r['child_of'] = 0;
297     if ( !isset($r['sort_column']) )
298         $r['sort_column'] = 'post_title';
299     if ( !isset($r['sort_order']) )
300         $r['sort_order'] = 'ASC';
301
302     $exclusions = '';
303     if ( !empty($r['exclude']) ) {
304         $expages = preg_split('/[\s,]+/',$r['exclude']);
305         if ( count($expages) ) {
306             foreach ( $expages as $expage ) {
307                 $exclusions .= ' AND ID <> ' . intval($expage) . ' ';
308             }
309         }
310     }
311
312     $pages = $wpdb->get_results("SELECT * " .
313         "FROM $wpdb->posts " .
314         "WHERE post_status = 'static' " .
315         "$exclusions " .
316         "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);
317
318     if ( empty($pages) )
319         return array();
320
321     // Update cache.
322     update_page_cache($pages);
323
324     if ( $r['child_of'] )
325         $pages = & get_page_children($r['child_of'], $pages);
326
327     return $pages;
328 }
329
330
331 function wp_list_pages($args = '') {
332     parse_str($args, $r);
333     if ( !isset($r['depth']) )
334         $r['depth'] = 0;
335     if ( !isset($r['show_date']) )
336         $r['show_date'] = '';
337     if ( !isset($r['child_of']) )
338         $r['child_of'] = 0;
339     if ( !isset($r['title_li']) )
340         $r['title_li'] = __('Pages');
341     if ( !isset($r['echo']) )
342         $r['echo'] = 1;
343
344     $output = '';
345
346     // Query pages.
347     $pages = & get_pages($args);
348     if ( $pages ) {
349
350         if ( $r['title_li'] )
351             $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
352
353         // Now loop over all pages that were selected
354         $page_tree = Array();
355         foreach ( $pages as $page ) {
356             // set the title for the current page
357             $page_tree[$page->ID]['title'] = $page->post_title;
358             $page_tree[$page->ID]['name'] = $page->post_name;
359
360             // set the selected date for the current page
361             // depending on the query arguments this is either
362             // the createtion date or the modification date
363             // as a unix timestamp. It will also always be in the
364             // ts field.
365             if ( !empty($r['show_date']) ) {
366                 if ( 'modified' == $r['show_date'] )
367                     $page_tree[$page->ID]['ts'] = $page->post_modified;
368                 else
369                     $page_tree[$page->ID]['ts'] = $page->post_date;
370             }
371
372             // The tricky bit!!
373             // Using the parent ID of the current page as the
374             // array index we set the curent page as a child of that page.
375             // We can now start looping over the $page_tree array
376             // with any ID which will output the page links from that ID downwards.
377             if ( $page->post_parent != $page->ID)
378                 $page_tree[$page->post_parent]['children'][] = $page->ID;
379         }
380         // Output of the pages starting with child_of as the root ID.
381         // child_of defaults to 0 if not supplied in the query.
382         $output .= _page_level_out($r['child_of'],$page_tree, $r, 0, false);
383         if ( $r['title_li'] )
384             $output .= '</ul></li>';
385     }
386
387     $output = apply_filters('wp_list_pages', $output);
388
389     if ( $r['echo'] )
390         echo $output;
391     else
392         return $output;
393 }
394
395
396 function _page_level_out($parent, $page_tree, $args, $depth = 0, $echo = true) {
397     global $wp_query;
398     $queried_obj = $wp_query->get_queried_object();
399     $output = '';
400
401     if ( $depth )
402         $indent = str_repeat("\t", $depth);
403         //$indent = join('', array_fill(0,$depth,"\t"));
404
405     if ( !is_array($page_tree[$parent]['children']) )
406         return false;
407
408     foreach ( $page_tree[$parent]['children'] as $page_id ) {
409         $cur_page = $page_tree[$page_id];
410         $title = $cur_page['title'];
411
412         $css_class = 'page_item';
413         if ( $page_id == $queried_obj->ID )
414             $css_class .= ' current_page_item';
415
416         $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page_id) . '" title="' . wp_specialchars($title) . '">' . $title . '</a>';
417
418         if ( isset($cur_page['ts']) ) {
419             $format = get_settings('date_format');
420             if ( isset($args['date_format']) )
421                 $format = $args['date_format'];
422             $output .= " " . mysql2date($format, $cur_page['ts']);
423         }
424
425         if ( isset($cur_page['children']) && is_array($cur_page['children']) ) {
426             $new_depth = $depth + 1;
427
428             if ( !$args['depth'] || $depth < ($args['depth']-1) ) {
429                 $output .= "$indent<ul>\n";
430                 $output .= _page_level_out($page_id, $page_tree, $args, $new_depth, false);
431                 $output .= "$indent</ul>\n";
432             }
433         }
434         $output .= "$indent</li>\n";
435     }
436     if ( $echo )
437         echo $output;
438     else
439         return $output;
440 }
441
442 function the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
443     echo get_the_attachment_link($id, $fullsize, $max_dims);
444 }
445
446 function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
447     $id = (int) $id;
448     $_post = & get_post($id);
449
450     if ( ('attachment' != $_post->post_status) || ('' == $_post->guid) )
451         return __('Missing Attachment');
452
453     if (! empty($_post->guid) ) {
454         $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims);
455
456         return "<a href=\"{$_post->guid}\" title=\"{$_post->post_title}\" >{$innerHTML}</a>";
457
458     } else {
459         $p .= __('Missing attachment');
460     }
461     return $p;
462 }
463
464 function get_attachment_icon($id = 0, $fullsize = false, $max_dims = false) {
465     $id = (int) $id;
466     $post = & get_post($id);
467
468     $mime = $post->post_mime_type;
469
470     $imagedata = get_post_meta($post->ID, '_wp_attachment_metadata', true);
471
472     $file = get_post_meta($post->ID, '_wp_attached_file', true);
473
474     if ( !$fullsize && !empty($imagedata['thumb'])
475             && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file))
476             && file_exists($thumbfile) ) {
477