Plugin Directory

Changeset 2868385


Ignore:
Timestamp:
02/20/2023 08:05:18 PM (3 years ago)
Author:
kerkenit
Message:

Fixed problem with the unix timestamp bug for the easter date

Location:
promissa/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • promissa/trunk/functions.php

    r2753313 r2868385  
    8181        }
    8282    }
     83
     84    if (!function_exists('idiv')) {
     85        function idiv($a, $b)
     86        {
     87            return floor($a / $b);
     88        }
     89    }
     90
     91    /**
     92     * Calculates the Easter date for a given year
     93     *
     94     * @param  int $y
     95     * @return DateTime
     96     */
     97    if (!function_exists('EasterDate')) {
     98        function EasterDate($y)
     99        {
     100            $firstdig1 = array(21, 24, 25, 27, 28, 29, 30, 31, 32, 34, 35, 38);
     101            $firstdig2 = array(33, 36, 37, 39, 40);
     102
     103            $firstdig = idiv($y, 100);
     104            $remain19 = $y % 19;
     105
     106            $temp = idiv($firstdig - 15, 2) + 202 - 11 * $remain19;
     107
     108            if (in_array($firstdig, $firstdig1)) {
     109                $temp = $temp - 1;
     110            }
     111            if (in_array($firstdig, $firstdig2)) {
     112                $temp = $temp - 2;
     113            }
     114
     115            $temp = $temp % 30;
     116
     117            $ta = $temp + 21;
     118            if ($temp == 29) {
     119                $ta = $ta - 1;
     120            }
     121            if ($temp == 28 and $remain19 > 10) {
     122                $ta = $ta - 1;
     123            }
     124
     125            $tb = ($ta - 19) % 7;
     126
     127            $tc = (40 - $firstdig) % 4;
     128            if ($tc == 3) {
     129                $tc = $tc + 1;
     130            }
     131            if ($tc > 1) {
     132                $tc = $tc + 1;
     133            }
     134
     135            $temp = $y % 100;
     136            $td = ($temp + idiv($temp, 4)) % 7;
     137
     138            $te = ((20 - $tb - $tc - $td) % 7) + 1;
     139            $d = $ta + $te;
     140
     141            if ($d > 31) {
     142                $d = $d - 31;
     143                $m = 4;
     144            } else {
     145                $m = 3;
     146            }
     147            return new DateTime("$y-$m-$d", new DateTimeZone('Europe/Amsterdam'));
     148        }
     149    }
     150
    83151    if (!function_exists('FeastDate_YN')) :
    84152        function FeastDate_YN($date)
     
    88156                return true;
    89157            endif;
    90             $feasts = array('08-12', '25-12', '01-01', '06-01', '19-03', '25-03', '24-06', '29-06', '15-08', '01-11', '07-11');
     158            $feasts = array('08-12','24-12', '25-12', '26-12', '01-01', '06-01', '19-03', '25-03', '24-06', '29-06', '15-08', '01-11', '07-11');
    91159            if (in_array($datetime->format('d-m'), $feasts)) :
    92160                return true;
    93161            endif;
    94162
    95             $easter = easter_date($datetime->format('Y'));
    96             $specialDates = array(date('Y-m-d', $easter + (39 * 24 * 60 * 60)), date('Y-m-d', $easter + (68 * 24 * 60 * 60)));
     163            $easter = EasterDate($datetime->format('Y'));
     164            $specialDates = array($easter->add(new DateInterval('P39D'))->format('Y-m-d'), $easter->add(new DateInterval('P68D'))->format('Y-m-d'));
    97165            if (in_array($datetime->format('Y-m-d'), $specialDates)) :
    98166                return true;
     
    318386        function ProMissaREST($page, $filter = '')
    319387        {
    320             $promissa = get_option('promissa');
    321             $url = 'https://api.promissa.nl/v1.5/records/' . $page . $filter;
    322             $args = array(
    323                 'headers' => array(
    324                     'Authorization' => 'Basic ' . base64_encode($promissa['private'] . ":" . $promissa['public'])
    325                 )
    326             );
    327             if(empty($filter)) :
    328                 $url .= '?';
    329             else :
    330                 $url .= '&';
    331             endif;
    332             $response = wp_remote_get( $url . 'exclude=api_private,domain', $args );
    333 
    334             $body = wp_remote_retrieve_body( $response );
    335 
    336             $manage = json_decode($body, true);
    337             if($manage["records"] != NULL) :
    338                 return $manage["records"];
    339             else :
    340                 //var_dump($response );
    341                 return $manage["records"];
    342             endif;
     388            try {
     389                $promissa = get_option('promissa');
     390                $url = 'https://api.promissa.nl/v1.6/records/' . $page . $filter;
     391                $args = array(
     392                    'headers' => array(
     393                        'Authorization' => 'Basic ' . base64_encode($promissa['private'] . ":" . $promissa['public'])
     394                    )
     395                );
     396                if(empty($filter)) :
     397                    $url .= '?';
     398                else :
     399                    $url .= '&';
     400                endif;
     401                $response = wp_remote_get( $url . 'exclude=api_private,domain', $args );
     402
     403                $body = wp_remote_retrieve_body( $response );
     404
     405                $manage = json_decode($body, true);
     406                if($manage["records"] != NULL) :
     407                    return $manage["records"];
     408                else :
     409                    //var_dump($response );
     410                    return $manage["records"];
     411                endif;
     412            } catch (Exception $e) {
     413
     414            }
    343415
    344416        }
     
    348420        function ProMissaREST_POST($page, $data = array())
    349421        {
    350             $promissa = get_option('promissa');
    351             $url = 'https://api.promissa.nl/v1.5/records/' . $page;
    352             $args = array(
    353                 'headers' => array(
    354                     'Authorization' => 'Basic ' . base64_encode($promissa['private'] . ":" . $promissa['public'])
    355                 ),
    356                 'body'        => $data,
    357                 'method'      => 'POST',
    358                 'data_format' => 'body',
    359             );
    360 
    361             $response = wp_remote_post($url, $args);
    362 
    363             //varDump($response);
     422            try {
     423                $promissa = get_option('promissa');
     424                $url = 'https://api.promissa.nl/v1.5/records/' . $page;
     425                $args = array(
     426                    'headers' => array(
     427                        'Authorization' => 'Basic ' . base64_encode($promissa['private'] . ":" . $promissa['public'])
     428                    ),
     429                    'body'        => $data,
     430                    'method'      => 'POST',
     431                    'data_format' => 'body',
     432                );
     433
     434                $response = wp_remote_post($url, $args);
     435
     436                //varDump($response);
     437            } catch (Exception $e) {
     438            }
    364439        }
    365440    }
  • promissa/trunk/readme.txt

    r2753313 r2868385  
    8080== Changelog ==
    8181
     82= 1.4.2 =
     83
     84* Fixed problem with the unix timestamp bug for the easter date
     85
    8286= 1.4.1 =
    8387
  • promissa/trunk/shortcodes/intentions.php

    r2753313 r2868385  
    11<?php
    2 if(!function_exists('promissa_intentions_handler')) :
     2if (!function_exists('promissa_intentions_handler')) :
    33    define('EOL_SPLIT', '- ');
    44    define('EOL_SPLIT_SEARCH', ' - ');
    55    define('EOL_SPLIT_REPLACE', '$#');
    6     define('WEEK_SPLIT_HOUR', 13);
    7     function promissa_intentions_handler( $atts, $content = null )
     6    define('WEEK_SPLIT_HOUR', 15);
     7    define('WEEK_SPLIT_TIME', (WEEK_SPLIT_HOUR - 12) . 'pm');
     8    function promissa_intentions_handler($atts, $content = null)
    89    {
    910        $timestamp = time();
    10         //if (is_user_logged_in()) :
    11         //  $timestamp = strtotime('2021-11-04 14:00:00');
    12         //endif;
    13         $atts_extended = shortcode_atts( array(
    14             'church_id' => '',
    15             'subtitle' => '',
    16             'limit' => 100,
    17             'show_title' => 'false',
    18         ), $atts );
    19         extract($atts_extended);
     11        if (is_user_logged_in()) :
     12            $timestamp = strtotime('2022-09-11 16:00:00');
     13        endif;
     14        $atts_extended = shortcode_atts(array(
     15            'church_id' => '',
     16            'subtitle' => '',
     17            'limit' => 100,
     18            'show_title' => 'false',
     19        ), $atts);
     20        extract($atts_extended);
    2021        $itemCount = 0;
    2122        $single_church = (isset($church_id) && !empty($church_id));
    2223        $intentions = NULL;
    2324        $nextWeekOffset = 1;
    24 
    25         $offset = (date('N', $timestamp) == 5 ? (date('H', $timestamp) <= WEEK_SPLIT_HOUR ? 1 : 2) : (date('N', $timestamp) == 6 ? 1 : 1));
    26         $nextWeek = new DateTime('friday next week');
    27         if(date('N', $timestamp) == 7) :
    28             $nextWeekOffset = 0;
    29             $nextWeek = new DateTime('friday next week');
    30         elseif(date('N', $timestamp) < 5 || (date('N', $timestamp) == 5 && (date('H', $timestamp) <= WEEK_SPLIT_HOUR))) :
    31             $nextWeekOffset = 0;
    32             $nextWeek = new DateTime('friday this week');
    33         endif;
    34         $nextWeek->add(new DateInterval('PT13H'));
    35         if(is_user_logged_in()) :
    36             $nextWeek->add(new DateInterval('P14D'));
    37         endif;
    38         if (false && is_user_logged_in()) :
    39             $now = strtotime('monday last week') -  (14 * 24 * 60 * 70) - (60 * 60 * 60);
    40             $from = $timestamp + ($offset * 24 * 60 * 60) -  (14 * 24 * 60 * 70);
    41         else :
    42             $now = strtotime('monday this week') - (60 * 60 * 60);
    43             $from = $timestamp + ($offset * 24 * 60 * 60);
    44         endif;
    45         $to = $timestamp + ((is_user_logged_in() ? 21 : 3) * 24 * 60 * 60);
    46         $to2 = $now + (3 * 7 * 24 * 60 * 60);
    47 
    48 
    49         $fromDate = DateTimeAdd(((new DateTime())->setISODate(date('Y', $from), date('W', $from) + $nextWeekOffset)), -2, 'Y-m-d 13:00:00');
    50         $toDate = DateTimeAdd(((new DateTime())->setISODate(date('Y', $to), date('W', $to) + 1)), 'P5D', 'Y-m-d 13:00:00');
     25        switch(date('w', $timestamp)) :
     26            case 0: //Sunday
     27            case 1: //Monday
     28            case 2: //Tuesday
     29            case 3: //Wednesday
     30            case 4: //Thursday
     31                $from = strtotime('Last Saturday ' . WEEK_SPLIT_TIME);
     32                $to = strtotime('Next Saturday ' . WEEK_SPLIT_TIME);
     33                $nextWeek = new DateTime('Saturday next week');
     34                //varDump($from);
     35                break;
     36            case 5: //Friday
     37                if(date('H', $timestamp) <= WEEK_SPLIT_HOUR) :
     38                    $from = strtotime('Last Saturday ' . WEEK_SPLIT_TIME);
     39                    $to = strtotime('Next Saturday ' . WEEK_SPLIT_TIME);
     40                else:
     41                    $from = strtotime('Last Saturday ' . WEEK_SPLIT_TIME);
     42                    $to = strtotime('Saturday next week ' . WEEK_SPLIT_TIME);
     43                endif;
     44                $nextWeek = new DateTime('Saturday next week');
     45                break;
     46            case 6: //Saturday
     47                $from = strtotime('Today ' . WEEK_SPLIT_TIME);
     48                $to = strtotime('Next Saturday ' . WEEK_SPLIT_TIME);
     49                $nextWeek = new DateTime('Saturday next week');
     50                break;
     51        endswitch;
     52
     53        //$now = strtotime('Last Saturday ' . WEEK_SPLIT_TIME);
     54        //$to2 = strtotime('Next Saturday ' . WEEK_SPLIT_TIME);
     55
     56        //$fromDate = DateTimeAdd(((new DateTime())->setISODate(date('Y', $from), date('W', $from) + $nextWeekOffset)), -2, 'Y-m-d 13:00:00');
     57        $fromDate = date('Y-m-d H:i:s', $from);
     58        //varDump($fromDate);
     59        //$toDate = DateTimeAdd(((new DateTime())->setISODate(date('Y', $to), date('W', $to) + 1)), 'P5D', 'Y-m-d 13:00:00');
     60        $toDate = date('Y-m-d H:i:s', $to);
     61        //varDump($toDate);
    5162        //varDump($fromDate, $toDate);
    52         if($single_church) :
    53 
    54                 //varDump($fromDate, $toDate);
    55                 $intentions = ProMissaREST('Intenties', '?size=' . $limit . '&filter=church_ID,eq,' . html_entity_decode($church_id) . '&filter2=start,ge,' . $fromDate . '&filter2=start,le,' . $toDate);
    56 
    57             if(strpos($church_id, '&') !== -1) :
     63        if ($single_church) :
     64
     65            //varDump($fromDate, $toDate);
     66            $intentions = ProMissaREST('Intenties', '?size=' . $limit . '&filter=church_ID,eq,' . html_entity_decode($church_id) . '&filter2=start,ge,' . $fromDate . '&filter2=start,le,' . $toDate);
     67
     68            if (strpos($church_id, '&') !== -1) :
    5869                $church_id = explode('&', $church_id)[0];
    5970            endif;
     
    6374        //varDump($intentions);
    6475        $output = '';
    65         if(!$single_church && $content != null && !empty($content)) :
     76        if (!$single_church && $content != null && !empty($content)) :
    6677            $output .= sprintf('<p>%s</p>', $content);
    6778        endif;
    68         if(!$single_church && !empty($subtitle)) :
     79        if (!$single_church && !empty($subtitle)) :
    6980            $output .= sprintf('<p>%s</p>', $subtitle);
    7081        endif;
    7182        //$output .= sprintf('<strong>%s</strong>', $intention['church']);
    72         if($single_church && $content != null && !empty($subtitle)) :
     83        if ($single_church && $content != null && !empty($subtitle)) :
    7384            $output .= sprintf('<h2>%s</h2>', $subtitle);
    7485        endif;
    75         if($single_church && !empty($subtitle)) :
     86        if ($single_church && !empty($subtitle)) :
    7687            $output .= sprintf('<h2>%s</h2>', $subtitle);
    7788        endif;
     
    7990
    8091
    81         if(count($intentions) > 0) :
     92        if (count($intentions) > 0) :
    8293            uasort($intentions, function ($a, $b) {
    83                 return $a["start"] . '#' . str_pad($a["order"], 2, "0", STR_PAD_LEFT) <=> $b["start"] . '#' . str_pad($b["order"], 2, "0", STR_PAD_LEFT);
     94                return $a["start"] . '#' . str_pad($a["order"], 2, "0", STR_PAD_LEFT) <=> $b["start"] . '#' . str_pad($b["order"], 2, "0", STR_PAD_LEFT);
    8495            });
    8596            $last_date = '';
     
    95106
    96107                $note = strtolower(trim($intention['note']));
    97                 $hourMinue = null;
    98                 for ($time = $now; $time < $to2; $time += 86400) :
     108                $hourMinute = null;
     109                for ($time = $from; $time < $to; $time += 86400) :
    99110
    100111                    for ($h = 0; $h < 24; $h++) :
    101112                        foreach (array('00', '15', '30', '45') as $m) :
    102113                            if (strpos($note, $h . "." . $m) !== false) :
    103                                 $hourMinue = ($h * 60) + (int)$m;
     114                                $hourMinute = ($h * 60) + (int)$m;
    104115                                break 2;
    105116                            endif;
    106117                        endforeach;
    107118                    endfor;
    108                     if ($hourMinue == null) :
    109                         $hourMinue = 77777;
     119                    if ($hourMinute == null) :
     120                        $hourMinute = 77777;
    110121                    endif;
    111122                    if (strpos($note, DayDateMonth($time)) !== false) :
    112                         $idate = $time + $sort + ($hourMinue ?? 77777);
     123                        $idate = $time + $sort + ($hourMinute ?? 77777);
    113124                    elseif (strpos($note, ShortDateAndMonth($time)) !== false) :
    114                         $idate = $time + $sort + ($hourMinue ?? 77777);
     125                        $idate = $time + $sort + ($hourMinute ?? 77777);
    115126                    elseif (strpos($note, LongDate($time)) !== false) :
    116                         $idate = $time + $sort + ($hourMinue ?? 77777);
     127                        $idate = $time + $sort + ($hourMinute ?? 77777);
    117128                    elseif (strpos($note, ShortDate($time)) !== false) :
    118                         $idate = $time + $sort + ($hourMinue ?? 77777);
     129                        $idate = $time + $sort + ($hourMinute ?? 77777);
    119130                    elseif (strpos($note, FullDateAt($time)) !== false) :
    120                         $idate = $time + $sort + ($hourMinue ?? 77777);
     131                        $idate = $time + $sort + ($hourMinute ?? 77777);
    121132                    endif;
    122133                endfor;
    123134                if ($idate != null && (strpos($note, 'doop') !== false || strpos($note, 'huwelijk') !== false || strpos($note, 'uitvaart') !== false)) :
    124                     $idate += ($hourMinue ?? 800);
     135                    $idate += ($hourMinute ?? 800);
    125136                endif;
    126137                $intention['sort'] = ($idate ?? ($maxTime + $sort));
     
    128139                $sort++;
    129140            endforeach;
    130             foreach(array('intention', 'funeral', 'baptize', 'marriage', 'announcement') as $type) :
    131                 if(isset($intentionsList[$type]) && $intentionsList[$type] != null && is_array($intentionsList[$type])) :
     141            foreach (array('intention', 'funeral', 'baptize', 'marriage', 'announcement') as $type) :
     142                if (isset($intentionsList[$type]) && $intentionsList[$type] != null && is_array($intentionsList[$type])) :
    132143                    uasort($intentionsList[$type], function ($a, $b) {
    133144                        global $type;
    134145                        return str_pad($a["sort"], 13, "0", STR_PAD_LEFT) . '#' . $a["start"] . '#' . str_pad($a["order"], 13, "0", STR_PAD_LEFT) <=> str_pad($b["sort"], 13, "0", STR_PAD_LEFT) . '#' . $b["start"] . '#' . str_pad($b["order"], 13, "0", STR_PAD_LEFT);
    135 
    136146                    });
    137147                    $dayOfWeek = NULL;
    138148                    $usedIntentions = array();
    139                     foreach($intentionsList[$type] as $intention) :
    140                         if($single_church && $intention['church_ID'] != $church_id) :
     149                    foreach ($intentionsList[$type] as $intention) :
     150                        if ($single_church && $intention['church_ID'] != $church_id) :
    141151                            continue;
    142152                        endif;
     
    145155                        $date = new DateTime($intention['start']);
    146156                        //varDump($date, $nextWeek);
    147                         if($date > $nextWeek) :
     157                        if ($date > $nextWeek) :
    148158                            break;
    149159                        endif;
    150160
    151161                        $dateFormatter = \IntlDateFormatter::create(
    152                         Locale::getDefault(),
    153                         IntlDateFormatter::NONE,
    154                         IntlDateFormatter::NONE,
    155                         date_default_timezone_get(),
    156                         IntlDateFormatter::GREGORIAN,
    157                         'cccc d MMMM Y'
     162                            Locale::getDefault(),
     163                            IntlDateFormatter::NONE,
     164                            IntlDateFormatter::NONE,
     165                            date_default_timezone_get(),
     166                            IntlDateFormatter::GREGORIAN,
     167                            'cccc d MMMM Y'
    158168                        );
    159169
    160                         if($intention['type'] == $type && $type == 'intention') :
    161                             if($dayOfWeek !== $date->format('w')) :
     170                        if ($intention['type'] == $type && $type == 'intention') :
     171                            if ($dayOfWeek !== $date->format('w')) :
    162172                                $last_date = sprintf('<h4>%s', (((ucfirst($dateFormatter->format($date))))));
    163173                            endif;
    164174
    165                             $day .= sprintf('%1$s %2$s %3$02d%4$s%5$02d%6$s</h4><ul>',
    166 
    167 
    168                             (!$single_church ? $intention['church'] : $last_date), //1
     175                            $day .= sprintf(
     176                                '%1$s %2$s %3$02d%4$s%5$02d%6$s</h4><ul>',
     177
     178
     179                                (!$single_church ? $intention['church'] : $last_date), //1
    169180                                __('at', 'promissa'), //2
    170181                                $date->format('H'), //3
     
    174185                            );
    175186                            $dayOfWeek = $date->format('N');
    176                         elseif($intention['type'] == $type && $type == 'announcement') :
     187                        elseif ($intention['type'] == $type && $type == 'announcement') :
    177188                            $day .= sprintf('<strong>%1$s</strong><ul>', __('Announcements', 'promissa'));
    178                         elseif($intention['type'] == $type && $type == 'funeral') :
     189                        elseif ($intention['type'] == $type && $type == 'funeral') :
    179190                            $day .= sprintf('<strong>%1$s</strong><ul>', __('Funeral(s) from this week', 'promissa'));
    180                         elseif($intention['type'] == $type && $type == 'baptize') :
     191                        elseif ($intention['type'] == $type && $type == 'baptize') :
    181192                            $day .= sprintf('<strong>%1$s</strong><ul>', __('Baptize(s) from this week', 'promissa'));
    182                         elseif($intention['type'] == $type && $type == 'marriage') :
     193                        elseif ($intention['type'] == $type && $type == 'marriage') :
    183194                            $day .= sprintf('<strong>%1$s</strong><ul>', __('Marriage(s) from this week', 'promissa'));
    184195                        endif;
    185                         if($intention['type'] == $type) :
    186                             if($day !== $last_day) :
    187                                 if(!empty($last_day)) :
     196                        if ($intention['type'] == $type) :
     197                            if ($day !== $last_day) :
     198                                if (!empty($last_day)) :
    188199                                    $output .= '</ul>';
    189200                                endif;
     
    192203                            endif;
    193204                            $last_day = $day;
    194                             if($show_title == 'true' && !empty($intention['note'])) :
     205                            if ($show_title == 'true' && !empty($intention['note'])) :
    195206                                $itemCount++;
    196                                 foreach(array(EOL_SPLIT) as $split) :
    197                                     if(substr($intention['note'], 0, 2) == $split) :
    198                                         foreach(explode(PHP_EOL, $intention['note']) as $note) :
     207                                foreach (array(EOL_SPLIT) as $split) :
     208                                    if (substr($intention['note'], 0, 2) == $split) :
     209                                        foreach (explode(PHP_EOL, $intention['note']) as $note) :
    199210                                            $note = KeiFormat::IntentionNote($note, $intention['type']);
    200                                             $output .= '<li>' . ltrim($note, $split) . '</li>';
     211                                            if ($intention['type'] != 'announcement') :
     212                                                $output .= '<li>' . ltrim($note, $split) . '</li>';
     213                                            endif;
    201214                                            $usedIntentions[] = removeEndOfString(ltrim($note, $split));
    202215                                        endforeach;
     
    212225                                $intention['note'] = KeiFormat::IntentionNote($intention['note'], $intention['type']);
    213226                                $usedIntentions[] = $intention['note'];
    214                                 $output .= '<li>' . $intention['note'] . '</li>';
     227                                if($intention['type'] != 'announcement') :
     228                                    $output .= '<li>' . $intention['note'] . '</li>';
     229                                endif;
    215230                            endif;
    216231                        endif;
    217232
    218233                    endforeach;
     234                    //varDump($usedIntentions);
     235                    if($type == 'announcement' && $usedIntentions != null && is_array($usedIntentions) && count($usedIntentions) > 0) :
     236                        $output .= '<li>' . implode('</li><li>', array_unique($usedIntentions, SORT_REGULAR)) . '</li>';
     237                    endif;
    219238                    $output .= '</ul>';
    220239
     
    222241            endforeach;
    223242        endif;
    224         if(!empty($output) && $itemCount > 0) :
     243        if (!empty($output) && $itemCount > 0) :
    225244            return $output;
    226245        endif;
    227246    }
    228247endif;
    229 add_shortcode( 'promissa-intentions', 'promissa_intentions_handler' );
     248add_shortcode('promissa-intentions', 'promissa_intentions_handler');
Note: See TracChangeset for help on using the changeset viewer.