Calendar.php
| 1 | <?php |
| 2 | /** |
| 3 | * Calendar class |
| 4 | * |
| 5 | * Print Jalali Calendar |
| 6 | */ |
| 7 | |
| 8 | namespace WPParsidate\Core; |
| 9 | |
| 10 | use WPParsidate\Helper\Number; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | class Calendar { |
| 15 | /** |
| 16 | * Create Persian Calendar |
| 17 | * |
| 18 | * @param string $postType Post type |
| 19 | * @param string $theme Theme style |
| 20 | * |
| 21 | * @return void |
| 22 | * @author Parsa Kafi |
| 23 | * @author Mobin Ghasempoor |
| 24 | */ |
| 25 | public static function printCalendar( string $postType = 'post', string $theme = 'none' ): void { |
| 26 | global $wpdb, $m, $monthnum, $year, $day, $posts; |
| 27 | |
| 28 | $monthsName = Names::getMonths(); |
| 29 | $jy = 0; |
| 30 | $pd = WPP_ParsiDate::getInstance(); |
| 31 | $jm = $monthnum; |
| 32 | // Get the site's timezone offset in seconds |
| 33 | $tzOffset = (int) ( (float) get_option( 'gmt_offset' ) ) * HOUR_IN_SECONDS; |
| 34 | |
| 35 | if ( $m != '' ) { |
| 36 | $m = preg_replace( "/[^0-9]/", "", $m ); |
| 37 | $jy = substr( $m, 0, 4 ); |
| 38 | } elseif ( $year !== '' ) { |
| 39 | $jy = $year; |
| 40 | } |
| 41 | |
| 42 | if ( $jy > 1500 ) { |
| 43 | list( $jy, $jm, $jd ) = $pd->gregorian_to_persian( $year, $monthnum, $day ); |
| 44 | } |
| 45 | |
| 46 | if ( ! $posts ) { |
| 47 | $gotsome = $wpdb->get_var( |
| 48 | $wpdb->prepare( |
| 49 | " |
| 50 | SELECT 1 AS test |
| 51 | FROM $wpdb->posts |
| 52 | WHERE post_type = %s |
| 53 | AND post_status = 'publish' |
| 54 | LIMIT 1 |
| 55 | ", $postType ) |
| 56 | ); |
| 57 | |
| 58 | if ( ! $gotsome ) { |
| 59 | return; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | $week_begins = (int) get_option( 'start_of_week' ); |
| 64 | $w = (int) sanitize_text_field( wp_unslash( $_GET['w'] ?? '' ) ); |
| 65 | $is_gregorian = false; |
| 66 | |
| 67 | if ( ! empty( $jm ) && ! empty( $jy ) ) { |
| 68 | $thisMonth = zeroise( (int) $jm, 2 ); |
| 69 | $thisYear = '' . (int) $jy; |
| 70 | } elseif ( ! empty( $w ) ) { |
| 71 | $thisYear = '' . (int) substr( $m, 0, 4 ); |
| 72 | $d = ( ( $w - 1 ) * 7 ) + 6; //it seems MySQL's weeks disagree with PHP's |
| 73 | $thisMonth = $wpdb->get_var( |
| 74 | $wpdb->prepare( "SELECT DATE_FORMAT ( ( DATE_ADD( %s, INTERVAL %d DAY ) ), '%%m')", $thisYear . '0101', $d ) |
| 75 | ); |
| 76 | //$thisMonth = $wpdb->get_var( "SELECT DATE_FORMAT ( ( DATE_ADD( '{$thisYear}0101', INTERVAL $d DAY ) ), '%m')" ); |
| 77 | } elseif ( ! empty( $m ) ) { |
| 78 | $thisYear = '' . (int) substr( $m, 0, 4 ); |
| 79 | |
| 80 | if ( strlen( $m ) < 6 ) { |
| 81 | $thisMonth = '01'; |
| 82 | } else { |
| 83 | $thisMonth = '' . zeroise( (int) substr( $m, 4, 2 ), 2 ); |
| 84 | } |
| 85 | } else { |
| 86 | $is_gregorian = true; |
| 87 | $nowTimestamp = current_time( 'timestamp' ); |
| 88 | $thisYear = gmdate( 'Y', $nowTimestamp + $tzOffset ); |
| 89 | $thisMonth = gmdate( 'm', $nowTimestamp + $tzOffset ); |
| 90 | $thisDay = gmdate( 'd', $nowTimestamp + $tzOffset ); |
| 91 | } |
| 92 | |
| 93 | //print_r($wp_query->query_vars); |
| 94 | |
| 95 | if ( $is_gregorian ) { |
| 96 | list( $jthisyear, |
| 97 | $jthismonth, |
| 98 | $jthisday ) = $pd->gregorian_to_persian( $thisYear, $thisMonth, $thisDay ); |
| 99 | |
| 100 | $unixmonth = $pd->gregorian_date( 'Y-m-d 00:00:00', "$jthisyear-$jthismonth-01" ); |
| 101 | } else { |
| 102 | $gdate = $pd->persian_to_gregorian( $thisYear, $thisMonth, 1 ); |
| 103 | $unixmonth = mktime( 0, 0, 0, $gdate[1], 1, $gdate[0] ); |
| 104 | $jthisyear = $thisYear; |
| 105 | $jthismonth = $thisMonth; |
| 106 | } |
| 107 | |
| 108 | $jnextmonth = $jthismonth + 1; |
| 109 | $jnextyear = $jthisyear; |
| 110 | |
| 111 | if ( $jnextmonth > 12 ) { |
| 112 | $jnextmonth = 1; |
| 113 | $jnextyear ++; |
| 114 | } |
| 115 | |
| 116 | $start = $pd->gregorian_date( 'Y-m-d 00:00:00', "$jthisyear-$jthismonth-01" ); |
| 117 | $end = $pd->gregorian_date( 'Y-m-d 23:59:59', |
| 118 | "$jnextyear-$jthismonth-" . $pd->j_days_in_month[ $jthismonth - 1 ] ); |
| 119 | |
| 120 | $previous = $wpdb->get_row( |
| 121 | $wpdb->prepare( |
| 122 | "SELECT MONTH(post_date) AS month, |
| 123 | YEAR(post_date) AS year |
| 124 | FROM $wpdb->posts |
| 125 | WHERE post_date < %s |
| 126 | AND post_type = %s |
| 127 | AND post_status = 'publish' |
| 128 | ORDER BY post_date DESC |
| 129 | LIMIT 1 |
| 130 | ", $start, $postType ) |
| 131 | ); |
| 132 | |
| 133 | $next = $wpdb->get_row( |
| 134 | $wpdb->prepare( |
| 135 | "SELECT MONTH(post_date) AS month, |
| 136 | YEAR(post_date) AS year |
| 137 | FROM $wpdb->posts |
| 138 | WHERE post_date >= %s |
| 139 | AND post_type = %s |
| 140 | AND post_status = 'publish' |
| 141 | ORDER BY post_date ASC |
| 142 | LIMIT 1 |
| 143 | ", $end, $postType ) |
| 144 | ); |
| 145 | |
| 146 | $calendar_output = '<table id="wp-calendar" class="widget_calendar ' . WP_PARSI_CLASS_PREFIX . 'calendar-widget-table ' . WP_PARSI_CLASS_PREFIX . 'calendar-widget-theme-' . $theme . '">' . |
| 147 | '<caption>' . $monthsName[ (int) $jthismonth ] . ' ' . |
| 148 | $pd->persian_date( 'Y', $unixmonth ) . '</caption><thead><tr>'; |
| 149 | $myweek = array(); |
| 150 | |
| 151 | for ( $wdcount = 0; $wdcount <= 6; $wdcount ++ ) { |
| 152 | $myweek[] = $pd->persian_day_small[ ( $wdcount + $week_begins ) % 7 ]; |
| 153 | } |
| 154 | |
| 155 | foreach ( $myweek as $wd ) { |
| 156 | $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$wd</th>"; |
| 157 | } |
| 158 | |
| 159 | $calendar_output .= '</tr></thead><tfoot><tr>'; |
| 160 | |
| 161 | if ( $previous ) { |
| 162 | $previous_month = $jthismonth - 1; |
| 163 | $previous_year = $jthisyear; |
| 164 | |
| 165 | if ( $previous_month == 0 ) { |
| 166 | $previous_month = 12; |
| 167 | $previous_year --; |
| 168 | } |
| 169 | |
| 170 | $url = get_month_link( $previous_year, $previous_month ); |
| 171 | if ( 'post' !== $postType ) { |
| 172 | $url = add_query_arg( 'post_type', $postType, $url ); |
| 173 | } |
| 174 | $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . $url . '">« ' . $monthsName[ $previous_month ] . '</a></td>'; |
| 175 | } else { |
| 176 | $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad"> </td>'; |
| 177 | } |
| 178 | |
| 179 | $calendar_output .= "\n\t\t" . '<td class="pad"> </td>'; |
| 180 | |
| 181 | if ( $next ) { |
| 182 | $next_month = $jthismonth + 1; |
| 183 | $next_year = $jthisyear; |
| 184 | |
| 185 | if ( $next_month == 13 ) { |
| 186 | $next_month = 1; |
| 187 | $next_year ++; |
| 188 | } |
| 189 | |
| 190 | $url = get_month_link( $next_year, $next_month ); |
| 191 | if ( 'post' !== $postType ) { |
| 192 | $url = add_query_arg( 'post_type', $postType, $url ); |
| 193 | } |
| 194 | $calendar_output .= "\n\t\t" . '<td colspan="3" id="next"><a href="' . $url . '">' . $monthsName[ $next_month ] . ' »</a></td>'; |
| 195 | } else { |
| 196 | $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad"> </td>'; |
| 197 | } |
| 198 | |
| 199 | $calendar_output .= '</tr></tfoot><tbody><tr>'; |
| 200 | |
| 201 | //____________________________________________________________________________________________________________________________________ |
| 202 | |
| 203 | $dayswithposts = $wpdb->get_results( |
| 204 | $wpdb->prepare( |
| 205 | "SELECT DISTINCT DAYOFMONTH ( post_date ), |
| 206 | MONTH ( post_date ), |
| 207 | YEAR ( post_date ) |
| 208 | FROM $wpdb->posts |
| 209 | WHERE post_date > %s |
| 210 | AND post_date < %s |
| 211 | AND post_type = %s |
| 212 | AND post_status = 'publish' |
| 213 | ", $start, $end, $postType ), |
| 214 | ARRAY_N |
| 215 | ); |
| 216 | |
| 217 | $daywithpost = array(); |
| 218 | if ( $dayswithposts ) { |
| 219 | foreach ( $dayswithposts as $daywith ) { |
| 220 | $daywithpost[] = $pd->persian_date( 'j', "$daywith[2]-$daywith[1]-$daywith[0]", 'eng' ); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | $userAgent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ); |
| 225 | |
| 226 | if ( strpos( $userAgent, 'MSIE' ) !== false |
| 227 | || stripos( $userAgent, 'camino' ) !== false |
| 228 | || stripos( $userAgent, 'safari' ) !== false ) { |
| 229 | $ak_title_separator = "\n"; |
| 230 | } else { |
| 231 | $ak_title_separator = ', '; |
| 232 | } |
| 233 | |
| 234 | $ak_titles_for_day = array(); |
| 235 | $ak_post_titles = $wpdb->get_results( |
| 236 | $wpdb->prepare( |
| 237 | "SELECT ID, |
| 238 | post_title, |
| 239 | DAYOFMONTH ( post_date ) AS dom, |
| 240 | MONTH ( post_date ) AS month, |
| 241 | YEAR ( post_date ) AS year |
| 242 | FROM $wpdb->posts |
| 243 | WHERE post_date >= %s |
| 244 | AND post_date <= %s |
| 245 | AND post_type = %s |
| 246 | AND post_status = 'publish' |
| 247 | ", $start, $end, $postType ) |
| 248 | ); |
| 249 | |
| 250 | if ( $ak_post_titles ) { |
| 251 | foreach ( $ak_post_titles as $ak_post_title ) { |
| 252 | /** This filter is documented in wp-includes/post-template.php */ |
| 253 | $post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, |
| 254 | $ak_post_title->ID ) ); |
| 255 | $ak_post_title->dom = $pd->persian_date( 'j', |
| 256 | "$ak_post_title->year-$ak_post_title->month-$ak_post_title->dom", 'eng' ); |
| 257 | |
| 258 | if ( empty( $ak_titles_for_day[ 'day_' . $ak_post_title->dom ] ) ) { |
| 259 | $ak_titles_for_day[ 'day_' . $ak_post_title->dom ] = ''; |
| 260 | } |
| 261 | |
| 262 | if ( empty( $ak_titles_for_day[ $ak_post_title->dom ] ) ) { // first one |
| 263 | $ak_titles_for_day[ $ak_post_title->dom ] = $post_title; |
| 264 | } else { |
| 265 | $ak_titles_for_day[ $ak_post_title->dom ] .= $ak_title_separator . $post_title; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | $pd = WPP_ParsiDate::getInstance(); |
| 271 | $pad = $pd->persian_date( "w", $pd->gregorian_date( "Y-m-d", $jthisyear . "-" . $jthismonth . "-01" ), "eng" ); |
| 272 | |
| 273 | if ( 0 != $pad ) { |
| 274 | $calendar_output .= "\n\t\t" . '<td colspan="' . $pad . '" class="pad"> </td>'; |
| 275 | } |
| 276 | |
| 277 | $daysInMonth = (int) $pd->persian_date( 't', $unixmonth, 'eng' ); |
| 278 | $currentDay = gmdate( 'j', time() + $tzOffset ); |
| 279 | $currentMonth = gmdate( 'm', time() + $tzOffset ); |
| 280 | $currentYear = gmdate( 'Y', time() + $tzOffset ); |
| 281 | |
| 282 | for ( $day = 1; $day <= $daysInMonth; ++ $day ) { |
| 283 | list( $thisYear, $thisMonth, $thisDay ) = $pd->persian_to_gregorian( $jthisyear, $jthismonth, $day ); |
| 284 | |
| 285 | if ( isset( $newrow ) && $newrow ) { |
| 286 | $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; |
| 287 | } |
| 288 | |
| 289 | $newrow = false; |
| 290 | |
| 291 | if ( $thisDay == $currentDay && $thisMonth == $currentMonth && $thisYear == $currentYear ) { |
| 292 | $calendar_output .= '<td id="today" title="' . esc_html__( 'Today', 'wp-parsidate' ) . '">'; |
| 293 | } else { |
| 294 | $calendar_output .= '<td>'; |
| 295 | } |
| 296 | |
| 297 | $p_day = Number::toPersian( $day ); |
| 298 | |
| 299 | if ( in_array( $day, $daywithpost ) ) { |
| 300 | $dayLinkDate = $pd->gregorian_date( 'Y,m,d', "$jthisyear-$jthismonth-$day" ); |
| 301 | $dayLinkDate = explode( ',', $dayLinkDate ); |
| 302 | $url = get_day_link( $dayLinkDate[0], $dayLinkDate[1], $dayLinkDate[2] ); |
| 303 | if ( 'post' !== $postType ) { |
| 304 | $url = add_query_arg( 'post_type', $postType, $url ); |
| 305 | } |
| 306 | $calendar_output .= '<a href="' . $url . "\" title=\"$ak_titles_for_day[$day]\">$p_day</a>"; |
| 307 | } else { |
| 308 | $calendar_output .= $p_day; |
| 309 | } |
| 310 | |
| 311 | $calendar_output .= '</td>'; |
| 312 | |
| 313 | if ( 6 == calendar_week_mod( $pd->gregorian_date( 'w', "$jthisyear-$jthismonth-$day" ) - $week_begins ) ) { |
| 314 | $newrow = true; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | $pad = 7 - calendar_week_mod( $pd->gregorian_date( 'w', "$jthisyear-$jthismonth-$day", 'eng' ) - $week_begins ); |
| 319 | |
| 320 | if ( $pad != 0 && $pad != 7 ) { |
| 321 | $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . $pad . '"> </td>'; |
| 322 | } |
| 323 | |
| 324 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 325 | echo $calendar_output . "\n\t</tr>\n\t</tbody>\n\t</table>"; |
| 326 | } |
| 327 | } |
| 328 |