root/branches/1.5/wp-includes/template-functions-post.php

Revision 2601, 12.2 kB (checked in by matt, 4 years ago)

http://mosquito.wordpress.org/view.php?id=1341 - Hat tip: Ryan the Rockstar

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