HEX
Server: LiteSpeed
System: Linux server490.bertina.biz 5.14.0-687.31.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Sat Aug 1 05:38:01 EDT 2026 x86_64
User: parskavirpaya (1252)
PHP: 8.1.34
Disabled: mail, show_source, system, shell_exec, passthru, exec, popen
Upload Files
File: /home/parskavirpaya/public_html/wp-content/plugins/wp-parsidate/inc/Core/Calendar.php
<?php
/**
 * Calendar class
 *
 * Print Jalali Calendar
 */

namespace WPParsidate\Core;

defined( 'ABSPATH' ) || exit;

class Calendar {
  /**
   * Create Persian Calendar
   *
   * @return          void
   * @author          Parsa Kafi
   * @author          Mobin Ghasempoor
   */
  public static function printCalendar(): void {
    global $wpdb, $m, $monthnum, $year, $day, $posts;

    $wpp_months_name = Names::getMonths();
    $jy              = 0;
    $pd              = WPP_ParsiDate::getInstance();
    $jm              = $monthnum;
    // Get the site's timezone offset in seconds
    $tzOffset = (int) ( (float) get_option( 'gmt_offset' ) ) * HOUR_IN_SECONDS;

    if ( $m != '' ) {
      $m  = preg_replace( "/[^0-9]/", "", $m );
      $jy = substr( $m, 0, 4 );
    } elseif ( $year !== '' ) {
      $jy = $year;
    }

    if ( $jy > 1500 ) {
      list( $jy, $jm, $jd ) = $pd->gregorian_to_persian( $year, $monthnum, $day );
    }

    if ( ! $posts ) {
      $gotsome = $wpdb->get_var(
        "
					SELECT 1 AS test
					FROM $wpdb->posts
					WHERE post_type = 'post'
					  	AND post_status = 'publish'
					LIMIT 1
				"
      );

      if ( ! $gotsome ) {
        return;
      }
    }

    $week_begins  = (int) get_option( 'start_of_week' );
    $w            = (int) sanitize_text_field( wp_unslash( $_GET['w'] ?? '' ) );
    $is_gregorian = false;

    if ( ! empty( $jm ) && ! empty( $jy ) ) {
      $thisMonth = '' . zeroise( (int) $jm, 2 );
      $thisYear  = '' . (int) $jy;
    } elseif ( ! empty( $w ) ) {
      $thisYear  = '' . (int) substr( $m, 0, 4 );
      $d         = ( ( $w - 1 ) * 7 ) + 6; //it seems MySQL's weeks disagree with PHP's
      $thisMonth = $wpdb->get_var(
        $wpdb->prepare( "SELECT DATE_FORMAT ( ( DATE_ADD( %s, INTERVAL %d DAY ) ), '%%m')", $thisYear . '0101', $d )
      );
      //$thisMonth = $wpdb->get_var( "SELECT DATE_FORMAT ( ( DATE_ADD( '{$thisYear}0101', INTERVAL $d DAY ) ), '%m')" );
    } elseif ( ! empty( $m ) ) {
      $thisYear = '' . (int) substr( $m, 0, 4 );

      if ( strlen( $m ) < 6 ) {
        $thisMonth = '01';
      } else {
        $thisMonth = '' . zeroise( (int) substr( $m, 4, 2 ), 2 );
      }
    } else {
      $is_gregorian = true;
      $nowTimestamp = current_time( 'timestamp' );
      $thisYear     = gmdate( 'Y', $nowTimestamp + $tzOffset );
      $thisMonth    = gmdate( 'm', $nowTimestamp + $tzOffset );
      $thisDay      = gmdate( 'd', $nowTimestamp + $tzOffset );
    }

    //print_r($wp_query->query_vars);

    if ( $is_gregorian ) {
      list( $jthisyear,
        $jthismonth,
        $jthisday ) = $pd->gregorian_to_persian( $thisYear, $thisMonth, $thisDay );

      $unixmonth = $pd->gregorian_date( 'Y-m-d 00:00:00', "$jthisyear-$jthismonth-01" );
    } else {
      $gdate      = $pd->persian_to_gregorian( $thisYear, $thisMonth, 1 );
      $unixmonth  = mktime( 0, 0, 0, $gdate[1], 1, $gdate[0] );
      $jthisyear  = $thisYear;
      $jthismonth = $thisMonth;
    }

    $jnextmonth = $jthismonth + 1;
    $jnextyear  = $jthisyear;

    if ( $jnextmonth > 12 ) {
      $jnextmonth = 1;
      $jnextyear ++;
    }

    $start = $pd->gregorian_date( 'Y-m-d 00:00:00', "$jthisyear-$jthismonth-01" );
    $end   = $pd->gregorian_date( 'Y-m-d 23:59:59',
      "$jnextyear-$jthismonth-" . $pd->j_days_in_month[ $jthismonth - 1 ] );

    //echo "Start Date: ".$start.", End Date: ".$end."<br>";

    $previous = $wpdb->get_row( $wpdb->prepare(
      "
				SELECT MONTH(post_date) AS month,
				  	YEAR(post_date) AS year
                FROM $wpdb->posts
                WHERE post_date < %s
                	AND post_type = 'post'
                    AND post_status = 'publish'
                ORDER BY post_date DESC
                LIMIT 1
               ",
      $start
    ) );

    $next = $wpdb->get_row( $wpdb->prepare(
      "
				SELECT MONTH(post_date) AS month,
				  	YEAR(post_date) AS year
                FROM $wpdb->posts
                WHERE post_date >= %s
                	AND post_type = 'post'
                    AND post_status = 'publish'
                ORDER BY post_date ASC
                LIMIT 1
                ",
      $end
    ) );

    $calendar_output = '<table id="wp-calendar" style="direction: rtl" class="widget_calendar">' .
                       '<caption>' . $wpp_months_name[ (int) $jthismonth ] . ' ' .
                       $pd->persian_date( 'Y', $unixmonth ) . '</caption><thead><tr>';
    $myweek          = array();

    for ( $wdcount = 0; $wdcount <= 6; $wdcount ++ ) {
      $myweek[] = $pd->persian_day_small[ ( $wdcount + $week_begins ) % 7 ];
    }

    foreach ( $myweek as $wd ) {
      $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$wd</th>";
    }

    $calendar_output .= '</tr></thead><tfoot><tr>';

    if ( $previous ) {
      $previous_month = $jthismonth - 1;
      $previous_year  = $jthisyear;

      if ( $previous_month == 0 ) {
        $previous_month = 12;
        $previous_year --;
      }

      $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . get_month_link( $previous_year,
          $previous_month ) .
                          '">&laquo; ' . $wpp_months_name[ $previous_month ] . '</a></td>';
    } else {
      $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad">&nbsp;</td>';
    }

    $calendar_output .= "\n\t\t" . '<td class="pad">&nbsp;</td>';

    if ( $next ) {
      $next_month = $jthismonth + 1;
      $next_year  = $jthisyear;

      if ( $next_month == 13 ) {
        $next_month = 1;
        $next_year ++;
      }

      $calendar_output .= "\n\t\t" . '<td colspan="3" id="next"><a href="' . get_month_link( $next_year,
          $next_month ) .
                          '">' . $wpp_months_name[ $next_month ] . ' &raquo;</a></td>';
    } else {
      $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad">&nbsp;</td>';
    }

    $calendar_output .= '</tr></tfoot><tbody><tr>';

    //____________________________________________________________________________________________________________________________________

    $dayswithposts = $wpdb->get_results(
      $wpdb->prepare(
        "
			SELECT DISTINCT DAYOFMONTH ( post_date ),
			  	MONTH ( post_date ),
			  	YEAR ( post_date )
        	FROM $wpdb->posts
       		WHERE post_date > %s
       		    AND post_date < %s
        		AND post_type = 'post'
        		AND post_status = 'publish'
        	",
        $start,
        $end
      ),
      ARRAY_N
    );

    if ( $dayswithposts ) {
      foreach ( $dayswithposts as $daywith ) {
        $daywithpost[] = $pd->persian_date( 'j', "$daywith[2]-$daywith[1]-$daywith[0]", 'eng' );
      }
    } else {
      $daywithpost = array();
    }

    $userAgent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) );

    if ( strpos( $userAgent, 'MSIE' ) !== false
         || stripos( $userAgent, 'camino' ) !== false
         || stripos( $userAgent, 'safari' ) !== false ) {
      $ak_title_separator = "\n";
    } else {
      $ak_title_separator = ', ';
    }

    $ak_titles_for_day = array();
    $ak_post_titles    = $wpdb->get_results(
      $wpdb->prepare(
        "
				SELECT ID,
				  	post_title,
				  	DAYOFMONTH ( post_date ) AS dom,
				  	MONTH ( post_date ) AS month,
				  	YEAR ( post_date ) AS year
				FROM $wpdb->posts
				WHERE post_date >= %s
				    AND post_date <= %s
				    AND post_type = 'post'
				    AND post_status = 'publish'
				",
        $start,
        $end
      )
    );

    if ( $ak_post_titles ) {
      foreach ( $ak_post_titles as $ak_post_title ) {
        /** This filter is documented in wp-includes/post-template.php */
        $post_title         = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title,
          $ak_post_title->ID ) );
        $ak_post_title->dom = $pd->persian_date( 'j',
          "$ak_post_title->year-$ak_post_title->month-$ak_post_title->dom", 'eng' );

        if ( empty( $ak_titles_for_day[ 'day_' . $ak_post_title->dom ] ) ) {
          $ak_titles_for_day[ 'day_' . $ak_post_title->dom ] = '';
        }

        if ( empty( $ak_titles_for_day[ $ak_post_title->dom ] ) ) { // first one
          $ak_titles_for_day[ $ak_post_title->dom ] = $post_title;
        } else {
          $ak_titles_for_day[ $ak_post_title->dom ] .= $ak_title_separator . $post_title;
        }
      }
    }

    $pd  = WPP_ParsiDate::getInstance();
    $pad = $pd->persian_date( "w", $pd->gregorian_date( "Y-m-d", $jthisyear . "-" . $jthismonth . "-01" ), "eng" );

    if ( 0 != $pad ) {
      $calendar_output .= "\n\t\t" . '<td colspan="' . $pad . '" class="pad">&nbsp;</td>';
    }

    $daysinmonth = (int) $pd->persian_date( 't', $unixmonth, 'eng' );

    for ( $day = 1; $day <= $daysinmonth; ++ $day ) {
      list( $thisYear,
        $thisMonth,
        $thisDay ) = $pd->persian_to_gregorian( $jthisyear, $jthismonth, $day );

      if ( isset( $newrow ) && $newrow ) {
        $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
      }

      $newrow = false;

      if ( $thisDay == gmdate( 'j', time() + $tzOffset )
           && $thisMonth == gmdate( 'm', time() + $tzOffset )
           && $thisYear == gmdate( 'Y', time() + $tzOffset ) ) {
        $calendar_output .= '<td id="today">';
      } else {
        $calendar_output .= '<td>';
      }

      $p_day = ( empty( $val['sep_datesnum'] ) ? $day : per_number( $day ) );

      if ( in_array( $day, $daywithpost ) ) {
        $calendar_output .= '<a href="' . get_day_link( $jthisyear, $jthismonth, $day ) .
                            "\" title=\"$ak_titles_for_day[$day]\">$p_day</a>";
      } else {
        $calendar_output .= $p_day;
      }

      $calendar_output .= '</td>';

      if ( 6 == calendar_week_mod( $pd->gregorian_date( 'w', "$jthisyear-$jthismonth-$day" ) - $week_begins ) ) {
        $newrow = true;
      }
    }

    $pad = 7 - calendar_week_mod( $pd->gregorian_date( 'w', "$jthisyear-$jthismonth-$day", 'eng' ) - $week_begins );

    if ( $pad != 0 && $pad != 7 ) {
      $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . $pad . '">&nbsp;</td>';
    }

    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    echo $calendar_output . "\n\t</tr>\n\t</tbody>\n\t</table>";
  }
}