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