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