WordPressを読む 35-4 /blog/wp-includes/general-template.php 4
2014/12/18
目次
/blog/wp-includes/general-template.php 4
関数 get_calendar()
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | /** * Display calendar with days that have posts as links. * * The calendar is cached, which will be retrieved, if it exists. If there are * no posts for the month, then it will not be displayed. * * @since 1.0.0 * @uses calendar_week_mod() * * @param bool $initial Optional, default is true. Use initial calendar names. * @param bool $echo Optional, default is true. Set to false for return. * @return string|null String when retrieving, null when displaying. */ function get_calendar($initial = true, $echo = true) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; $key = md5( $m . $monthnum . $year ); if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) { if ( is_array($cache) && isset( $cache[ $key ] ) ) { if ( $echo ) { /** This filter is documented in wp-includes/general-template.php */ echo apply_filters( 'get_calendar', $cache[$key] ); return; } else { /** This filter is documented in wp-includes/general-template.php */ return apply_filters( 'get_calendar', $cache[$key] ); } } } if ( !is_array($cache) ) $cache = array(); // Quick check. If we have no posts at all, abort! if ( !$posts ) { $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1"); if ( !$gotsome ) { $cache[ $key ] = ''; wp_cache_set( 'get_calendar', $cache, 'calendar' ); return; } } if ( isset($_GET['w']) ) $w = ''.intval($_GET['w']); // week_begins = 0 stands for Sunday $week_begins = intval(get_option('start_of_week')); // Let's figure out when we are if ( !empty($monthnum) && !empty($year) ) { $thismonth = ''.zeroise(intval($monthnum), 2); $thisyear = ''.intval($year); } elseif ( !empty($w) ) { // We need to get the month from MySQL $thisyear = ''.intval(substr($m, 0, 4)); $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')"); } elseif ( !empty($m) ) { $thisyear = ''.intval(substr($m, 0, 4)); if ( strlen($m) < 6 ) $thismonth = '01'; else $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2); } else { $thisyear = gmdate('Y', current_time('timestamp')); $thismonth = gmdate('m', current_time('timestamp')); } $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); $last_day = date('t', $unixmonth); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year FROM $wpdb->posts WHERE post_date < '$thisyear-$thismonth-01' AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1"); $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year FROM $wpdb->posts WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59' AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date ASC LIMIT 1"); /* translators: Calendar caption: 1: month name, 2: 4-digit year */ $calendar_caption = _x('%1$s %2$s', 'calendar caption'); $calendar_output = '<table id="wp-calendar"> <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> <thead> <tr>'; $myweek = array(); for ( $wdcount=0; $wdcount<=6; $wdcount++ ) { $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7); } foreach ( $myweek as $wd ) { $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); $wd = esc_attr($wd); $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; } $calendar_output .= ' </tr> </thead> <tfoot> <tr>'; if ( $previous ) { $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '">« ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>'; } else { $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad"> </td>'; } $calendar_output .= "\n\t\t".'<td class="pad"> </td>'; if ( $next ) { $calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' »</a></td>'; } else { $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad"> </td>'; } $calendar_output .= ' </tr> </tfoot> <tbody> <tr>'; // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date) FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' AND post_type = 'post' AND post_status = 'publish' AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); if ( $dayswithposts ) { foreach ( (array) $dayswithposts as $daywith ) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) $ak_title_separator = "\n"; else $ak_title_separator = ', '; $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom " ."FROM $wpdb->posts " ."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' " ."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' " ."AND post_type = 'post' AND post_status = 'publish'" ); if ( $ak_post_titles ) { foreach ( (array) $ak_post_titles as $ak_post_title ) { /** This filter is documented in wp-includes/post-template.php */ $post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) ); if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) ) $ak_titles_for_day['day_'.$ak_post_title->dom] = ''; if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one $ak_titles_for_day["$ak_post_title->dom"] = $post_title; else $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title; } } // See how much we should pad in the beginning $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins); if ( 0 != $pad ) $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad"> </td>'; $daysinmonth = intval(date('t', $unixmonth)); for ( $day = 1; $day <= $daysinmonth; ++$day ) { if ( isset($newrow) && $newrow ) $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; $newrow = false; if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) ) $calendar_output .= '<td id="today">'; else $calendar_output .= '<td>'; if ( in_array($day, $daywithpost) ) // any posts today? $calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>"; else $calendar_output .= $day; $calendar_output .= '</td>'; if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) ) $newrow = true; } $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins); if ( $pad != 0 && $pad != 7 ) $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'"> </td>'; $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; $cache[ $key ] = $calendar_output; wp_cache_set( 'get_calendar', $cache, 'calendar' ); if ( $echo ) { /** * Filter the HTML calendar output. * * @since 3.0.0 * * @param string $calendar_output HTML output of the calendar. */ echo apply_filters( 'get_calendar', $calendar_output ); } else { /** This filter is documented in wp-includes/general-template.php */ return apply_filters( 'get_calendar', $calendar_output ); } } |
関数 delete_get_calendar_cache()
1 2 3 4 5 6 7 8 9 | /** * Purge the cached results of get_calendar. * * @see get_calendar * @since 2.1.0 */ function delete_get_calendar_cache() { wp_cache_delete( 'get_calendar', 'calendar' ); } |
関数定義の間の処理
1 2 3 4 | add_action( 'save_post', 'delete_get_calendar_cache' ); add_action( 'delete_post', 'delete_get_calendar_cache' ); add_action( 'update_option_start_of_week', 'delete_get_calendar_cache' ); add_action( 'update_option_gmt_offset', 'delete_get_calendar_cache' ); |
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 | /** * Display all of the allowed tags in HTML format with attributes. * * This is useful for displaying in the comment area, which elements and * attributes are supported. As well as any plugins which want to display it. * * @since 1.0.1 * @uses $allowedtags * * @return string HTML allowed tags entity encoded. */ function allowed_tags() { global $allowedtags; $allowed = ''; foreach ( (array) $allowedtags as $tag => $attributes ) { $allowed .= '<'.$tag; if ( 0 < count($attributes) ) { foreach ( $attributes as $attribute => $limits ) { $allowed .= ' '.$attribute.'=""'; } } $allowed .= '> '; } return htmlentities($allowed); } |
関数 the_date_xml()
1 2 3 4 5 6 7 8 9 10 | /***** Date/Time tags *****/ /** * Outputs the date in iso8601 format for xml files. * * @since 1.0.0 */ function the_date_xml() { echo mysql2date( 'Y-m-d', get_post()->post_date, false ); } |
関数 the_date()
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 | /** * Display or Retrieve the date the current post was written (once per date) * * Will only output the date if the current post's date is different from the * previous one output. * * i.e. Only one date listing will show per day worth of posts shown in the loop, even if the * function is called several times for each post. * * HTML output can be filtered with 'the_date'. * Date string output can be filtered with 'get_the_date'. * * @since 0.71 * * @uses get_the_date() * @param string $d Optional. PHP date format defaults to the date_format option if not specified. * @param string $before Optional. Output before the date. * @param string $after Optional. Output after the date. * @param bool $echo Optional, default is display. Whether to echo the date or return it. * @return string|null Null if displaying, string if retrieving. */ function the_date( $d = '', $before = '', $after = '', $echo = true ) { global $currentday, $previousday; if ( $currentday != $previousday ) { $the_date = $before . get_the_date( $d ) . $after; $previousday = $currentday; /** * Filter the date a post was published for display. * * @since 0.71 * * @param string $the_date The formatted date string. * @param string $d PHP date format. Defaults to 'date_format' option * if not specified. * @param string $before HTML output before the date. * @param string $after HTML output after the date. */ $the_date = apply_filters( 'the_date', $the_date, $d, $before, $after ); if ( $echo ) echo $the_date; else return $the_date; } return null; } |
関数 get_the_date()
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 | /** * Retrieve the date on which the post was written. * * Unlike the_date() this function will always return the date. * Modify output with 'get_the_date' filter. * * @since 3.0.0 * * @param string $d Optional. PHP date format defaults to the date_format option if not specified. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. * @return string|bool Date the current post was written. False on failure. */ function get_the_date( $d = '', $post = null ) { $post = get_post( $post ); if ( ! $post ) { return false; } if ( '' == $d ) { $the_date = mysql2date( get_option( 'date_format' ), $post->post_date ); } else { $the_date = mysql2date( $d, $post->post_date ); } /** * Filter the date a post was published. * * @since 3.0.0 * * @param string $the_date The formatted date. * @param string $d PHP date format. Defaults to 'date_format' option * if not specified. * @param int|WP_Post $post The post object or ID. */ return apply_filters( 'get_the_date', $the_date, $d, $post ); } |
関数 the_modified_date()
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 | /** * Display the date on which the post was last modified. * * @since 2.1.0 * * @param string $d Optional. PHP date format defaults to the date_format option if not specified. * @param string $before Optional. Output before the date. * @param string $after Optional. Output after the date. * @param bool $echo Optional, default is display. Whether to echo the date or return it. * @return string|null Null if displaying, string if retrieving. */ function the_modified_date($d = '', $before='', $after='', $echo = true) { $the_modified_date = $before . get_the_modified_date($d) . $after; /** * Filter the date a post was last modified for display. * * @since 2.1.0 * * @param string $the_modified_date The last modified date. * @param string $d PHP date format. Defaults to 'date_format' option * if not specified. * @param string $before HTML output before the date. * @param string $after HTML output after the date. */ $the_modified_date = apply_filters( 'the_modified_date', $the_modified_date, $d, $before, $after ); if ( $echo ) echo $the_modified_date; else return $the_modified_date; } |
関数 get_the_modified_date()
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 | /** * Retrieve the date on which the post was last modified. * * @since 2.1.0 * * @param string $d Optional. PHP date format. Defaults to the "date_format" option * @return string */ function get_the_modified_date($d = '') { if ( '' == $d ) $the_time = get_post_modified_time(get_option('date_format'), null, null, true); else $the_time = get_post_modified_time($d, null, null, true); /** * Filter the date a post was last modified. * * @since 2.1.0 * * @param string $the_time The formatted date. * @param string $d PHP date format. Defaults to value specified in * 'date_format' option. */ return apply_filters( 'get_the_modified_date', $the_time, $d ); } |
関数 the_time()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Display the time at which the post was written. * * @since 0.71 * * @param string $d Either 'G', 'U', or php date format. */ function the_time( $d = '' ) { /** * Filter the time a post was written for display. * * @since 0.71 * * @param string $get_the_time The formatted time. * @param string $d The time format. Accepts 'G', 'U', * or php date format. */ echo apply_filters( 'the_time', get_the_time( $d ), $d ); } |
関数 get_the_time()
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 | /** * Retrieve the time at which the post was written. * * @since 1.5.0 * * @param string $d Optional. Format to use for retrieving the time the post * was written. Either 'G', 'U', or php date format defaults * to the value specified in the time_format option. Default empty. * @param int|WP_Post $post WP_Post object or ID. Default is global $post object. * @return string|int|bool Formatted date string or Unix timestamp. False on failure. */ function get_the_time( $d = '', $post = null ) { $post = get_post($post); if ( ! $post ) { return false; } if ( '' == $d ) $the_time = get_post_time(get_option('time_format'), false, $post, true); else $the_time = get_post_time($d, false, $post, true); /** * Filter the time a post was written. * * @since 1.5.0 * * @param string $the_time The formatted time. * @param string $d Format to use for retrieving the time the post was written. * Accepts 'G', 'U', or php date format value specified * in 'time_format' option. Default empty. * @param int|WP_Post $post WP_Post object or ID. */ return apply_filters( 'get_the_time', $the_time, $d, $post ); } |