AdhanProcessor.php
1 year ago
CsvProcessor.php
2 months ago
DebugProcessor.php
1 year ago
DigitalScreenProcessor.php
1 month ago
HijriProcessor.php
3 years ago
LanguageProcessor.php
4 years ago
OtherProcessor.php
2 months ago
QuickUpdateProcessor.php
3 years ago
StartTimeProcessor.php
1 year ago
ThemeSettingsProcessor.php
1 year ago
StartTimeProcessor.php
179 lines
| 1 | <?php |
| 2 | use IslamicNetwork\PrayerTimes\PrayerTimes; |
| 3 | use IslamicNetwork\PrayerTimes\Method as PrayerTimesMethod; |
| 4 | require_once(__DIR__. '/../db.php'); |
| 5 | |
| 6 | if ( ! class_exists('DPTStartTimeProcessor')) { |
| 7 | class DPTStartTimeProcessor |
| 8 | { |
| 9 | /** @var DatabaseConnection */ |
| 10 | private $db; |
| 11 | |
| 12 | /** @var string[] */ |
| 13 | private $prayerNames = array('fajr', 'sunrise', 'zuhr', 'asr', 'sunset', 'maghrib', 'isha' ); |
| 14 | |
| 15 | private PrayerTimes $islamicNetworkPrayerTimes; |
| 16 | |
| 17 | /** @var array */ |
| 18 | private $data; |
| 19 | |
| 20 | /** @var string */ |
| 21 | private $dbTable; |
| 22 | |
| 23 | public function __construct(array $data) |
| 24 | { |
| 25 | global $wpdb; |
| 26 | $tableName = $wpdb->prefix . "timetable_cities"; |
| 27 | $this->dbTable = "`".DB_NAME ."`.`" .$tableName."`"; |
| 28 | |
| 29 | $this->db = new DatabaseConnection(); |
| 30 | if (is_array($data)) { |
| 31 | $this->data = array_map( 'sanitize_text_field', $data); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public function process() |
| 36 | { |
| 37 | set_transient('nearest_city', $this->data['city']); |
| 38 | $calcMethod = $this->data['method']; |
| 39 | $asrMethod = $this->data['asr-method']; |
| 40 | $latLong = $this->getLatLong($this->data['city']); |
| 41 | $higherLatMethod = (int)$this->data['higher-lat']; |
| 42 | |
| 43 | $this->islamicNetworkPrayerTimes = new PrayerTimes($calcMethod); |
| 44 | if ($calcMethod == 23) { // custom settings |
| 45 | $method = new PrayerTimesMethod('My Custom Method'); |
| 46 | $fajrAngle = (int)$this->data['fajr-angle']; |
| 47 | $ishaAngle = (int)$this->data['isha-angle']; |
| 48 | |
| 49 | $method->setFajrAngle((int)$this->data['fajr-angle']); |
| 50 | $method->setIshaAngleOrMins((int)$this->data['isha-angle']); |
| 51 | |
| 52 | $this->islamicNetworkPrayerTimes->setMethod($method); |
| 53 | |
| 54 | |
| 55 | delete_option('fajr-angle'); |
| 56 | delete_option('isha-angle'); |
| 57 | |
| 58 | add_option('fajr-angle', $fajrAngle); |
| 59 | add_option('isha-angle', $ishaAngle); |
| 60 | } |
| 61 | $this->islamicNetworkPrayerTimes->setAsrJuristicMethod($asrMethod); |
| 62 | $this->islamicNetworkPrayerTimes->setSchool($asrMethod); |
| 63 | $this->islamicNetworkPrayerTimes->setLatitudeAdjustmentMethod($higherLatMethod); |
| 64 | |
| 65 | |
| 66 | delete_option('fajr-delay'); |
| 67 | delete_option('zuhr-delay'); |
| 68 | delete_option('asr-delay'); |
| 69 | delete_option('maghrib-delay'); |
| 70 | delete_option('isha-delay'); |
| 71 | delete_option('higher-lat'); |
| 72 | delete_option('calc-method'); |
| 73 | delete_option('asr-method'); |
| 74 | |
| 75 | add_option('fajr-delay', $this->data['fajr-delay']); |
| 76 | add_option('zuhr-delay', $this->data['zuhr-delay']); |
| 77 | add_option('asr-delay', $this->data['asr-delay']); |
| 78 | add_option('maghrib-delay', $this->data['maghrib-delay']); |
| 79 | add_option('isha-delay', $this->data['isha-delay']); |
| 80 | add_option('higher-lat', $higherLatMethod); |
| 81 | add_option('calc-method', $calcMethod); |
| 82 | add_option('asr-method', $asrMethod); |
| 83 | |
| 84 | $year = date('Y'); |
| 85 | $date = new DateTime($year . '-1-1'); |
| 86 | $endDate = new DateTime(($year + 1) . '-1-1'); |
| 87 | |
| 88 | while ($date < $endDate) |
| 89 | { |
| 90 | $day = date('Y-m-d'); |
| 91 | $times = $this->islamicNetworkPrayerTimes->getTimes($date, $latLong['lat'], $latLong['lng']); |
| 92 | $this->setTimezone($date); |
| 93 | $times = [ |
| 94 | 'fajr' => $times['Fajr'], |
| 95 | 'sunrise' => $times['Sunrise'], |
| 96 | 'zuhr' => $times['Dhuhr'], |
| 97 | 'asr' => $times['Asr'], |
| 98 | 'sunset' => $times['Sunset'], |
| 99 | 'maghrib' => $times['Maghrib'], |
| 100 | 'isha' => $times['Isha'] |
| 101 | ]; |
| 102 | |
| 103 | $times = array_combine($this->prayerNames, $times); |
| 104 | |
| 105 | $row = array ( |
| 106 | 'd_date' => $date->format('Y-m-d'), |
| 107 | 'fajr_begins' => $times['fajr'], |
| 108 | 'fajr_jamah' => $this->getJamahTime($day, $times['fajr'], $this->data['fajr-delay']), |
| 109 | 'sunrise' => $times['sunrise'], |
| 110 | 'zuhr_begins' => $times['zuhr'], |
| 111 | 'zuhr_jamah' => $this->getJamahTime($day, $times['zuhr'], $this->data['zuhr-delay']), |
| 112 | 'asr_mithl_1' => $times['asr'], |
| 113 | 'asr_mithl_2' => $times['asr'], |
| 114 | 'asr_jamah' => $this->getJamahTime($day, $times['asr'], $this->data['asr-delay']), |
| 115 | 'maghrib_begins' => $times['maghrib'], |
| 116 | 'maghrib_jamah' => $this->getJamahTime($day, $times['maghrib'], $this->data['maghrib-delay']), |
| 117 | 'isha_begins' => $times['isha'], |
| 118 | 'isha_jamah' => $this->getJamahTime($day, $times['isha'], $this->data['isha-delay']), |
| 119 | 'is_ramadan' => 0, |
| 120 | 'hijri_date' => 0 |
| 121 | ); |
| 122 | $this->db->insertRow($row); |
| 123 | $date->modify('+1 day'); // next day |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @throws DateInvalidTimeZoneException |
| 129 | */ |
| 130 | private function setTimezone(DateTime $date): void |
| 131 | { |
| 132 | $timezone_string = get_option('timezone_string'); |
| 133 | |
| 134 | if ($timezone_string) { |
| 135 | $timezone = new DateTimeZone($timezone_string); |
| 136 | } else { |
| 137 | // Fallback to the offset if timezone string is not set |
| 138 | $offset = get_option('gmt_offset'); |
| 139 | $timezone = timezone_name_from_abbr('', $offset * 3600, 0); |
| 140 | if ($timezone === false) { |
| 141 | $timezone = 'UTC'; |
| 142 | } |
| 143 | $timezone = new DateTimeZone($timezone); |
| 144 | } |
| 145 | $date->setTimezone($timezone); |
| 146 | } |
| 147 | |
| 148 | private function getJamahTime($date, $time, $mins) |
| 149 | { |
| 150 | $mins = empty($mins) ? 3 : $mins; |
| 151 | try{ |
| 152 | $dt = new DateTime($date . ' ' . $time); |
| 153 | $dt->add(new DateInterval('PT' . $mins . 'M')); |
| 154 | |
| 155 | return $dt->format('H:i'); |
| 156 | } catch(\Exception $e) { |
| 157 | error_log('!Failed to Set Prayer Times Automatically - : ' . $e->getMessage()); |
| 158 | } |
| 159 | return ''; |
| 160 | } |
| 161 | |
| 162 | private function getLatLong($cityId) |
| 163 | { |
| 164 | $cityId = sanitize_text_field( $cityId ); |
| 165 | $cityId = empty($cityId) ? 26 : $cityId; // DEFAULT LONDON |
| 166 | |
| 167 | global $wpdb; |
| 168 | $sql = "SELECT lat, lng FROM $this->dbTable WHERE id = " . $cityId; |
| 169 | $row = $wpdb->get_row($sql, ARRAY_A); |
| 170 | |
| 171 | return array( |
| 172 | 'lat' => $row['lat'], |
| 173 | 'lng' => $row['lng'] |
| 174 | ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | } |
| 179 |