PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/polylang/include/widget-calendar.php

https://gitlab.com/hop23typhu/bryepoxy
PHP | 265 lines | 183 code | 35 blank | 47 comment | 40 complexity | b21ea1ccb1d23834abcf0fb5ecbfdc46 MD5 | raw file
  1. <?php
  2. if ( ! class_exists( 'WP_Widget_Calendar' ) ) {
  3. require_once( ABSPATH . '/wp-includes/default-widgets.php' );
  4. }
  5. /**
  6. * obliged to rewrite the whole functionnality as there is no filter on sql queries and only a filter on final output
  7. * code base last checked with WP 4.4.2
  8. * a request for making a filter on sql queries exists: http://core.trac.wordpress.org/ticket/15202
  9. * method used in 0.4.x: use of the get_calendar filter and overwrite the output of get_calendar function -> not very efficient (add 4 to 5 sql queries)
  10. * method used since 0.5: remove the WP widget and replace it by our own -> our language filter will not work if get_calendar is called directly by a theme
  11. *
  12. * @since 0.5
  13. */
  14. class PLL_Widget_Calendar extends WP_Widget_Calendar {
  15. /**
  16. * displays the widget
  17. * modified version of the parent function to call our own get_calendar function
  18. *
  19. * @since 0.5
  20. *
  21. * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
  22. * @param array $instance The settings for the particular instance of the widget
  23. */
  24. function widget( $args, $instance ) {
  25. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  26. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '&nbsp;' : $instance['title'], $instance, $this->id_base );
  27. echo $args['before_widget'];
  28. if ( $title ) {
  29. echo $args['before_title'] . $title . $args['after_title'];
  30. }
  31. echo '<div id="calendar_wrap">';
  32. empty( PLL()->curlang ) ? get_calendar() : self::get_calendar(); #modified#
  33. echo '</div>';
  34. echo $args['after_widget'];
  35. }
  36. /**
  37. * modified version of WP get_calendar function to filter the query
  38. *
  39. * @since 0.5
  40. *
  41. * @param bool $initial Optional, default is true. Use initial calendar names.
  42. * @param bool $echo Optional, default is true. Set to false for return.
  43. * @return string|null String when retrieving, null when displaying.
  44. */
  45. static function get_calendar( $initial = true, $echo = true ) {
  46. global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
  47. $join_clause = PLL()->model->post->join_clause(); #added#
  48. $where_clause = PLL()->model->post->where_clause( PLL()->curlang ); #added#
  49. $key = md5( PLL()->curlang->slug . $m . $monthnum . $year ); #modified#
  50. $cache = wp_cache_get( 'get_calendar', 'calendar' );
  51. if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
  52. /** This filter is documented in wp-includes/general-template.php */
  53. $output = apply_filters( 'get_calendar', $cache[ $key ] );
  54. if ( $echo ) {
  55. echo $output;
  56. return;
  57. }
  58. return $output;
  59. }
  60. if ( ! is_array( $cache ) ) {
  61. $cache = array();
  62. }
  63. // Quick check. If we have no posts at all, abort!
  64. if ( ! $posts ) {
  65. $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
  66. if ( ! $gotsome ) {
  67. $cache[ $key ] = '';
  68. wp_cache_set( 'get_calendar', $cache, 'calendar' );
  69. return;
  70. }
  71. }
  72. if ( isset( $_GET['w'] ) ) {
  73. $w = (int) $_GET['w'];
  74. }
  75. // week_begins = 0 stands for Sunday
  76. $week_begins = (int) get_option( 'start_of_week' );
  77. $ts = current_time( 'timestamp' );
  78. // Let's figure out when we are
  79. if ( ! empty( $monthnum ) && ! empty( $year ) ) {
  80. $thismonth = zeroise( intval( $monthnum ), 2 );
  81. $thisyear = (int) $year;
  82. } elseif ( ! empty( $w ) ) {
  83. // We need to get the month from MySQL
  84. $thisyear = (int) substr( $m, 0, 4 );
  85. //it seems MySQL's weeks disagree with PHP's
  86. $d = ( ( $w - 1 ) * 7 ) + 6;
  87. $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
  88. } elseif ( ! empty( $m ) ) {
  89. $thisyear = (int) substr( $m, 0, 4 );
  90. if ( strlen( $m ) < 6 ) {
  91. $thismonth = '01';
  92. } else {
  93. $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );
  94. }
  95. } else {
  96. $thisyear = gmdate( 'Y', $ts );
  97. $thismonth = gmdate( 'm', $ts );
  98. }
  99. $unixmonth = mktime( 0, 0 , 0, $thismonth, 1, $thisyear );
  100. $last_day = date( 't', $unixmonth );
  101. // Get the next and previous month and year with at least one post
  102. $previous = $wpdb->get_row( "SELECT MONTH( post_date ) AS month, YEAR( post_date ) AS year
  103. FROM $wpdb->posts $join_clause
  104. WHERE post_date < '$thisyear-$thismonth-01'
  105. AND post_type = 'post' AND post_status = 'publish' $where_clause
  106. ORDER BY post_date DESC
  107. LIMIT 1" ); #modified#
  108. $next = $wpdb->get_row( "SELECT MONTH( post_date ) AS month, YEAR( post_date ) AS year
  109. FROM $wpdb->posts $join_clause
  110. WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
  111. AND post_type = 'post' AND post_status = 'publish' $where_clause
  112. ORDER BY post_date ASC
  113. LIMIT 1" ); #modified#
  114. /* translators: Calendar caption: 1: month name, 2: 4-digit year */
  115. $calendar_caption = _x( '%1$s %2$s', 'calendar caption' );
  116. $calendar_output = '<table id="wp-calendar">
  117. <caption>' . sprintf(
  118. $calendar_caption,
  119. $wp_locale->get_month( $thismonth ),
  120. date( 'Y', $unixmonth )
  121. ) . '</caption>
  122. <thead>
  123. <tr>';
  124. $myweek = array();
  125. for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
  126. $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
  127. }
  128. foreach ( $myweek as $wd ) {
  129. $day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
  130. $wd = esc_attr( $wd );
  131. $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
  132. }
  133. $calendar_output .= '
  134. </tr>
  135. </thead>
  136. <tfoot>
  137. <tr>';
  138. if ( $previous ) {
  139. $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link( $previous->year, $previous->month ) . '">&laquo; ' .
  140. $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) .
  141. '</a></td>';
  142. } else {
  143. $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
  144. }
  145. $calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>';
  146. if ( $next ) {
  147. $calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link( $next->year, $next->month ) . '">' .
  148. $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) ) .
  149. ' &raquo;</a></td>';
  150. } else {
  151. $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
  152. }
  153. $calendar_output .= '
  154. </tr>
  155. </tfoot>
  156. <tbody>
  157. <tr>';
  158. $daywithpost = array();
  159. // Get days with posts
  160. $dayswithposts = $wpdb->get_results( "SELECT DISTINCT DAYOFMONTH( post_date )
  161. FROM $wpdb->posts $join_clause
  162. WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
  163. AND post_type = 'post' AND post_status = 'publish' $where_clause
  164. AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N ); #modified#
  165. if ( $dayswithposts ) {
  166. foreach ( (array) $dayswithposts as $daywith ) {
  167. $daywithpost[] = $daywith[0];
  168. }
  169. }
  170. // See how much we should pad in the beginning
  171. $pad = calendar_week_mod( date( 'w', $unixmonth ) - $week_begins );
  172. if ( 0 != $pad ) {
  173. $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr( $pad ) .'" class="pad">&nbsp;</td>';
  174. }
  175. $newrow = false;
  176. $daysinmonth = (int) date( 't', $unixmonth );
  177. for ( $day = 1; $day <= $daysinmonth; ++$day ) {
  178. if ( isset($newrow) && $newrow ) {
  179. $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
  180. }
  181. $newrow = false;
  182. if ( $day == gmdate( 'j', $ts ) &&
  183. $thismonth == gmdate( 'm', $ts ) &&
  184. $thisyear == gmdate( 'Y', $ts ) ) {
  185. $calendar_output .= '<td id="today">';
  186. } else {
  187. $calendar_output .= '<td>';
  188. }
  189. if ( in_array( $day, $daywithpost ) ) {
  190. // any posts today?
  191. $date_format = date( _x( 'F j, Y', 'daily archives date format' ), strtotime( "{$thisyear}-{$thismonth}-{$day}" ) );
  192. $label = sprintf( __( 'Posts published on %s' ), $date_format );
  193. $calendar_output .= sprintf(
  194. '<a href="%s" aria-label="%s">%s</a>',
  195. get_day_link( $thisyear, $thismonth, $day ),
  196. esc_attr( $label ),
  197. $day
  198. );
  199. } else {
  200. $calendar_output .= $day;
  201. }
  202. $calendar_output .= '</td>';
  203. if ( 6 == calendar_week_mod( date( 'w', mktime(0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
  204. $newrow = true;
  205. }
  206. }
  207. $pad = 7 - calendar_week_mod( date( 'w', mktime( 0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins );
  208. if ( $pad != 0 && $pad != 7 ) {
  209. $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr( $pad ) .'">&nbsp;</td>';
  210. }
  211. $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
  212. $cache[ $key ] = $calendar_output;
  213. wp_cache_set( 'get_calendar', $cache, 'calendar' );
  214. if ( $echo ) {
  215. /**
  216. * Filter the HTML calendar output.
  217. *
  218. * @since 3.0.0
  219. *
  220. * @param string $calendar_output HTML output of the calendar.
  221. */
  222. echo apply_filters( 'get_calendar', $calendar_output );
  223. return;
  224. }
  225. /** This filter is documented in wp-includes/general-template.php */
  226. return apply_filters( 'get_calendar', $calendar_output );
  227. }
  228. }