PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/custom-post-type-date-archives/includes/calendar.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 285 lines | 182 code | 39 blank | 64 comment | 49 complexity | cd07ff276e992e1c575dfd4d3e8b7c71 MD5 | raw file
  1. <?php
  2. /**
  3. * Calendar
  4. *
  5. * Customized core WordPress get_calendar() function.
  6. *
  7. * @package Custom_Post_Type_Date_Archives
  8. * @subpackage Functions/Calendar
  9. * @copyright Copyright (c) 2017, Kees Meijer
  10. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
  11. * @since 2.5.1
  12. */
  13. // Exit if accessed directly.
  14. if ( ! defined( 'ABSPATH' ) ) {
  15. exit;
  16. }
  17. /**
  18. * Display a calendar with days that have posts as links.
  19. *
  20. * Copied from the WordPress function get_calendar().
  21. *
  22. * Use the extra `post_type` parameter to display a calendar for a custom post type.
  23. *
  24. * The calendar is cached, which will be retrieved, if it exists. If there are
  25. * no posts for the month, then it will not be displayed.
  26. *
  27. * @since 1.0.0
  28. * @see get_calendar()
  29. *
  30. * @global wpdb $wpdb
  31. * @global int $m
  32. * @global int $monthnum
  33. * @global int $year
  34. * @global WP_Locale $wp_locale
  35. * @global array $posts
  36. *
  37. * @param string $post_type Post type.
  38. * @param bool $initial Optional, default is true. Use initial calendar names.
  39. * @param bool $echo Optional, default is true. Set to false for return.
  40. * @return string|void String when retrieving.
  41. */
  42. function cptda_get_calendar( $post_type, $initial = true, $echo = true ) {
  43. global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
  44. $post_type_sql = cptda_get_calendar_post_type_sql( $post_type );
  45. if ( empty( $post_type ) || empty( $post_type_sql ) ) {
  46. return '';
  47. }
  48. $key = md5( $post_type . $m . $monthnum . $year );
  49. $cache = wp_cache_get( 'cptda_get_calendar', 'calendar' );
  50. $is_cache = $cache && is_array( $cache ) && isset( $cache[ $key ] );
  51. $cache_data = wp_cache_get( 'cptda_get_calendar_data', 'calendar_data' );
  52. $is_cache_data = $cache_data && is_array( $cache_data ) && isset( $cache_data[ $key ] );
  53. if ( $is_cache && $is_cache_data ) {
  54. /** This filter is documented in includes/calendar.php */
  55. $output = apply_filters( 'cptda_get_calendar', $cache[ $key ], $cache_data[ $key ] );
  56. $output = is_string( $output ) ? $output : '';
  57. if ( $echo ) {
  58. echo $output;
  59. return;
  60. }
  61. return $output;
  62. }
  63. if ( ! is_array( $cache ) || ! is_array( $cache_data ) ) {
  64. $cache = array();
  65. $cache_data = array();
  66. }
  67. // Quick check. If we have no posts at all, abort!
  68. $gotsome = $wpdb->get_var( "SELECT 1 as test FROM $wpdb->posts WHERE {$post_type_sql} LIMIT 1" );
  69. if ( ! $gotsome ) {
  70. $cache[ $key ] = '';
  71. wp_cache_set( 'cptda_get_calendar', $cache, 'calendar' );
  72. $cache_data[ $key ] = '';
  73. wp_cache_set( 'cptda_get_calendar_data', $cache_data, 'calendar_data' );
  74. return '';
  75. }
  76. // week_begins = 0 stands for Sunday
  77. $week_begins = (int) get_option( 'start_of_week' );
  78. $default_data = array (
  79. 'next_year' => '',
  80. 'prev_year' => '',
  81. 'next_month' => '',
  82. 'prev_month' => '',
  83. 'calendar_days' => array(),
  84. );
  85. $date = cptda_get_calendar_date();
  86. $date = array_merge( $date, $default_data );
  87. /**
  88. * Filter calendar data for the current date.
  89. *
  90. * This filter is called before the database query.
  91. *
  92. * @since 2.6.0
  93. *
  94. * @param array $date Array with calendar data.
  95. * @param string $post_type Post type
  96. */
  97. $calendar = apply_filters( 'cptda_calendar_data', $date, $post_type );
  98. $calendar = array_merge( $date, $calendar );
  99. if ( ( '' === $calendar['prev_year'] ) && ( '' === $calendar['prev_month'] ) ) {
  100. $prev = cptda_get_adjacent_archive_date( $post_type, $calendar );
  101. $prev_year = absint( $prev['year'] );
  102. $prev_month = absint( $prev['month'] );
  103. } else {
  104. $prev_year = absint( $calendar['prev_year'] );
  105. $prev_month = absint( $calendar['prev_month'] );
  106. }
  107. if ( ( '' === $calendar['next_year'] ) && ( '' === $calendar['next_month'] ) ) {
  108. $next = cptda_get_adjacent_archive_date( $post_type, $calendar, 'next' );
  109. $next_year = absint( $next['year'] );
  110. $next_month = absint( $next['month'] );
  111. } else {
  112. $next_year = absint( $calendar['next_year'] );
  113. $next_month = absint( $calendar['next_month'] );
  114. }
  115. // Add dates to calendar data for filters below.
  116. $calendar['next_year'] = $next_year;
  117. $calendar['next_month'] = $next_month;
  118. $calendar['prev_year'] = $prev_year;
  119. $calendar['prev_month'] = $prev_month;
  120. $thisyear = $calendar['year'];
  121. $thismonth = zeroise( absint( $calendar['month'] ), 2 );
  122. $last_day = $calendar['last_day'];
  123. /* translators: Calendar caption: 1: month name, 2: 4-digit year */
  124. $calendar_caption = _x( '%1$s %2$s', 'calendar caption' );
  125. $output = '<table id="wp-calendar">
  126. <caption>' . sprintf(
  127. $calendar_caption,
  128. $wp_locale->get_month( $thismonth ),
  129. date( 'Y', $calendar['unixmonth'] )
  130. ) . '</caption>
  131. <thead>
  132. <tr>';
  133. $myweek = array();
  134. for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
  135. $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
  136. }
  137. foreach ( $myweek as $wd ) {
  138. $day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
  139. $wd = esc_attr( $wd );
  140. $output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
  141. }
  142. $output .= '
  143. </tr>
  144. </thead>
  145. <tfoot>
  146. <tr>';
  147. if ( $prev_year && $prev_month ) {
  148. $output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . cptda_get_month_archive_link( $prev_year, $prev_month, $post_type ) . '">&laquo; ' .
  149. $wp_locale->get_month_abbrev( $wp_locale->get_month( $prev_month ) ) .
  150. '</a></td>';
  151. } else {
  152. $output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad">&nbsp;</td>';
  153. }
  154. $output .= "\n\t\t" . '<td class="pad">&nbsp;</td>';
  155. if ( $next_year && $next_month ) {
  156. $output .= "\n\t\t" . '<td colspan="3" id="next"><a href="' . cptda_get_month_archive_link( $next_year, $next_month, $post_type ) . '">' .
  157. $wp_locale->get_month_abbrev( $wp_locale->get_month( $next_month ) ) .
  158. ' &raquo;</a></td>';
  159. } else {
  160. $output .= "\n\t\t" . '<td colspan="3" id="next" class="pad">&nbsp;</td>';
  161. }
  162. $output .= '
  163. </tr>
  164. </tfoot>
  165. <tbody>
  166. <tr>';
  167. $daywithpost = array();
  168. if ( is_array( $calendar['calendar_days'] ) && empty( $calendar['calendar_days'] ) ) {
  169. // Get days with posts
  170. $dayswithposts = $wpdb->get_results( "SELECT DISTINCT DAYOFMONTH(post_date)
  171. FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
  172. AND {$post_type_sql}
  173. AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N );
  174. if ( $dayswithposts ) {
  175. foreach ( (array) $dayswithposts as $daywith ) {
  176. $daywithpost[] = $daywith[0];
  177. }
  178. }
  179. } else {
  180. $daywithpost = is_array( $calendar['calendar_days'] ) ? $calendar['calendar_days'] : array();
  181. }
  182. $daywithpost = array_unique( array_map( 'intval', $daywithpost ) );
  183. $calendar['calendar_days'] = $daywithpost;
  184. // See how much we should pad in the beginning
  185. $pad = calendar_week_mod( date( 'w', $calendar['unixmonth'] ) - $week_begins );
  186. if ( 0 != $pad ) {
  187. $output .= "\n\t\t" . '<td colspan="' . esc_attr( $pad ) . '" class="pad">&nbsp;</td>';
  188. }
  189. $newrow = false;
  190. $daysinmonth = (int) date( 't', $calendar['unixmonth'] );
  191. for ( $day = 1; $day <= $daysinmonth; ++$day ) {
  192. if ( isset( $newrow ) && $newrow ) {
  193. $output .= "\n\t</tr>\n\t<tr>\n\t\t";
  194. }
  195. $newrow = false;
  196. if ( $day == gmdate( 'j', $calendar['timestamp'] ) &&
  197. $thismonth == gmdate( 'm', $calendar['timestamp'] ) &&
  198. $thisyear == gmdate( 'Y', $calendar['timestamp'] ) ) {
  199. $output .= '<td id="today">';
  200. } else {
  201. $output .= '<td>';
  202. }
  203. if ( in_array( $day, $daywithpost ) ) {
  204. // any posts today?
  205. $date_format = date( _x( 'F j, Y', 'daily archives date format' ), strtotime( "{$thisyear}-{$thismonth}-{$day}" ) );
  206. $label = sprintf( __( 'Posts published on %s' ), $date_format );
  207. $output .= sprintf(
  208. '<a href="%s" aria-label="%s">%s</a>',
  209. cptda_get_day_archive_link( $thisyear, $thismonth, $day, $post_type ),
  210. esc_attr( $label ),
  211. $day
  212. );
  213. } else {
  214. $output .= $day;
  215. }
  216. $output .= '</td>';
  217. if ( 6 == calendar_week_mod( date( 'w', mktime( 0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
  218. $newrow = true;
  219. }
  220. }
  221. $pad = 7 - calendar_week_mod( date( 'w', mktime( 0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins );
  222. if ( $pad != 0 && $pad != 7 ) {
  223. $output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr( $pad ) . '">&nbsp;</td>';
  224. }
  225. $output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
  226. $cache[ $key ] = $output;
  227. wp_cache_set( 'cptda_get_calendar', $cache, 'calendar' );
  228. $cache_data[ $key ] = $calendar;
  229. wp_cache_set( 'cptda_get_calendar_data', $cache_data, 'calendar_data' );
  230. if ( $echo ) {
  231. /**
  232. * Filter the HTML calendar output.
  233. *
  234. * @since 2.6.0
  235. *
  236. * @param string $output HTML output of the calendar.
  237. * @param array $calendar Array with arguments for the current calendar.
  238. */
  239. echo apply_filters( 'cptda_get_calendar', $output, $calendar );
  240. return;
  241. }
  242. /** This filter is documented in includes/calendar.php */
  243. return apply_filters( 'cptda_get_calendar', $output, $calendar );
  244. }