Plugin Directory

Changeset 3175296


Ignore:
Timestamp:
10/24/2024 07:48:58 PM (17 months ago)
Author:
calj
Message:

1.4.2 better error handling

Location:
calj/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • calj/trunk/CalJPlugin.php

    r2752610 r3175296  
    2020    private function buildCacheKeyForCurrentSettings()
    2121    {
    22         $options = get_option( self::CALJ_API_OPTION );
    23         $city = $options['city'];
    24         return $city;
     22        return self::getoptopt('city', '');
    2523    }
    2624
    2725    public function shortcode($atts)
    2826    {
    29         $decodedAttr = shortcode_atts( array(
    30             'val' => '',
    31             'lang' => '',
    32             'city' => ''
    33         ), $atts );
    34 
    35         if (! $decodedAttr['val']) {
    36             return '';
    37         }
    38 
    39         $result = '';
    40 
    41         $cache = $this->unserializeShabbatCache();
    42         if ( (0 == count($cache)) || ! is_array($cache) ) {
    43             $cache = $this->refreshCache();
    44         }
    45 
    46         if (! $cache) {
    47             // Invalid data in cache, even after refresh.
    48             return '';
    49         }
    50 
    51         // Check if cache is fresh enough
    52         $coordKey = $this->buildCacheKeyForCurrentSettings();
    53 
    54         if (array_key_exists($coordKey, $cache)) {
    55             $coordCache = $cache[$coordKey];
    56             $expires = $coordCache['expires'];
    57             $dtNow = new \DateTime('now', new \DateTimeZone('UTC'));
    58             if ($dtNow->format('U') > $expires) {
    59                 $cache = $this->refreshCache($coordKey);
    60             }
    61         }
    62         else {
    63             $cache = $this->refreshCache();
    64         }
    65 
    66         if (! array_key_exists($coordKey, $cache)) {
    67         }
    68         else {
    69             $coordCache = $cache[$coordKey];
    70         }
    71 
    72         $json = $coordCache;
    73 
    74         if (isset($json['success']) && $json['success']) {
    75 
    76             $city = $decodedAttr['city'];
    77             $lang = $decodedAttr['lang'];
    78 
    79             // If we're demanding city-related data,
    80             // use the corresponding sub-part of the dictionary
    81             if ($city) {
    82                 if (array_key_exists('cities', $json) &&
    83                     array_key_exists($city, $json['cities'])) {
    84                     $json = $json['cities'][$city];
    85                 } else {
    86                     // Better explicitly indicate that no data was found,
    87                     // rather than silently give wrong zmanim.
    88                     return '-';
    89                 }
    90             }
    91 
    92             // dot notation to access the json properties
    93             $jsonPath = preg_split('#\.#', $decodedAttr['val']);
    94             $jsonCursor = $json;
    95             for($i = 0; $i < count($jsonPath); ++ $i) {
    96                 $component = $jsonPath[$i];
    97 
    98                 // If the component name exists with a 'Loc' suffix,
    99                 // treat it as an array of language codes
    100                 if ($lang &&
    101                     array_key_exists($component.'Loc', $jsonCursor) &&
    102                     is_array($jsonCursor[$component.'Loc']) &&
    103                     array_key_exists($lang, $jsonCursor[$component.'Loc'])
    104                 ) {
    105                     $jsonCursor = $jsonCursor[$component.'Loc'][$lang];
    106                 }
    107 
    108                 else if (array_key_exists($component, $jsonCursor)) {
    109                     $jsonCursor = $jsonCursor[$component];
    110                 }
    111                 else {
    112                     $jsonCursor = '';
    113                     break;
    114                 }
    115             }
    116 
    117             if (is_array($jsonCursor)) {
    118                 // If we get an array in response, it means that there are 7 items (1 per day of the week, starting Sun)
    119                 $dow = date('N') % 7;
    120                 if (array_key_exists($dow, $jsonCursor)) {
    121                     $result = $jsonCursor[$dow];
     27        try {
     28            $decodedAttr = shortcode_atts( array(
     29                'val' => '',
     30                'lang' => '',
     31                'city' => ''
     32            ), $atts );
     33
     34            if (! $decodedAttr['val']) {
     35                throw new \Exception('Missing "val" attribute.', 2);
     36            }
     37
     38            $result = '';
     39
     40            $cache = $this->unserializeShabbatCache();
     41            if ( (0 == count($cache)) || ! is_array($cache) ) {
     42                $cache = $this->refreshCache();
     43            }
     44
     45            if ((! $cache) || (0 == count($cache)) || ! is_array($cache)) {
     46                throw new \Exception('Invalid data in cache, even after refresh.', 3);
     47            }
     48
     49            // Check if cache is fresh enough
     50            $coordKey = $this->buildCacheKeyForCurrentSettings();
     51
     52            if (array_key_exists($coordKey, $cache)) {
     53                $coordCache = $cache[$coordKey];
     54                $expires = $coordCache['expires'];
     55                $dtNow = new \DateTime('now', new \DateTimeZone('UTC'));
     56                if ($dtNow->format('U') > $expires) {
     57                    $cache = $this->refreshCache($coordKey);
     58                }
     59            }
     60            else {
     61                $cache = $this->refreshCache();
     62            }
     63
     64            if (! array_key_exists($coordKey, $cache)) {
     65            }
     66            else {
     67                $coordCache = $cache[$coordKey];
     68            }
     69
     70            $json = $coordCache;
     71
     72            if (isset($json['success']) && $json['success']) {
     73
     74                $city = $decodedAttr['city'];
     75                $lang = $decodedAttr['lang'];
     76
     77                // If we're demanding city-related data,
     78                // use the corresponding sub-part of the dictionary
     79                if ($city) {
     80                    if (array_key_exists('cities', $json) &&
     81                        array_key_exists($city, $json['cities'])) {
     82                        $json = $json['cities'][$city];
     83                    } else {
     84                        // Better explicitly indicate that no data was found,
     85                        // rather than silently give wrong zmanim.
     86                        return '-';
     87                    }
     88                }
     89
     90                // dot notation to access the json properties
     91                $jsonPath = preg_split('#\.#', $decodedAttr['val']);
     92                $jsonCursor = $json;
     93                for($i = 0; $i < count($jsonPath); ++ $i) {
     94                    $component = $jsonPath[$i];
     95
     96                    // If the component name exists with a 'Loc' suffix,
     97                    // treat it as an array of language codes
     98                    if ($lang &&
     99                        array_key_exists($component.'Loc', $jsonCursor) &&
     100                        is_array($jsonCursor[$component.'Loc']) &&
     101                        array_key_exists($lang, $jsonCursor[$component.'Loc'])
     102                    ) {
     103                        $jsonCursor = $jsonCursor[$component.'Loc'][$lang];
     104                    }
     105
     106                    else if (array_key_exists($component, $jsonCursor)) {
     107                        $jsonCursor = $jsonCursor[$component];
     108                    }
     109                    else {
     110                        $jsonCursor = '';
     111                        break;
     112                    }
     113                }
     114
     115                if (is_array($jsonCursor)) {
     116                    // If we get an array in response, it means that there are 7 items (1 per day of the week, starting Sun)
     117                    $dow = date('N') % 7;
     118                    if (array_key_exists($dow, $jsonCursor)) {
     119                        $result = $jsonCursor[$dow];
     120                    }
     121                    else {
     122                        $result = '';
     123                    }
    122124                }
    123125                else {
    124                     $result = '';
    125                 }
    126             }
    127             else {
    128                 $result = $jsonCursor;
    129             }
    130         }
    131         return $result;
     126                    $result = $jsonCursor;
     127                }
     128            }
     129            return $result;
     130        } catch (\Exception $e) {
     131            return '[ERR:' . $e->getCode() . ':' . $e->getMessage() . ']';
     132        }
    132133    }
    133134
    134135    private function refreshCache($coordKey = null)
    135136    {
    136         $options = get_option( self::CALJ_API_OPTION );
    137         if (isset($options['api_key'])) {
    138             $key = $options['api_key'];
    139             $city = $options['city'];
    140             $response = file_get_contents('https://api.calj.net/wp/1/shabbat.json?city='.$city.'&key='.$key);
    141 
    142             $response = json_decode($response, true);
    143             if ( (! $response) || (! isset($response['data'])) || (! $response['data']) ) {
    144                 return null;
    145             }
    146 
    147             $response = json_decode( self::xorString($response['data']), true );
    148             if ( ! $response ) {
    149                 return null;
    150             }
    151 
    152             if (null == $coordKey) {
    153                 $cache = array();
    154                 $coordKey = $this->buildCacheKeyForCurrentSettings();
    155             }
    156 
    157             $cache[$coordKey] = $response;
    158 
    159             $this->serializeShabbatCache($cache);
    160 
    161             return $cache;
    162         }
    163 
     137        $key = self::getoptopt('api_key', '');
     138        if (!$key) {
     139            return [];
     140        }
     141
     142        $city =  self::getoptopt('city', '');
     143        $response = file_get_contents('https://api.calj.net/wp/1/shabbat.json?city='.$city.'&key='.$key);
     144
     145        $response = json_decode($response, true);
     146        if ( (! $response) || (! isset($response['data'])) || (! $response['data']) ) {
     147            return null;
     148        }
     149
     150        $response = json_decode( self::xorString($response['data']), true );
     151        if ( ! $response ) {
     152            return null;
     153        }
     154
     155        if (null == $coordKey) {
     156            $cache = array();
     157            $coordKey = $this->buildCacheKeyForCurrentSettings();
     158        }
     159
     160        $cache[$coordKey] = $response;
     161
     162        $this->serializeShabbatCache($cache);
     163
     164        return $cache;
    164165    }
    165166
     
    195196        return $outText;
    196197    }
     198
     199    private static function getoptopt($optName, $default) {
     200        $options = get_option( self::CALJ_API_OPTION );
     201        if (!$options || !is_array($options) || !array_key_exists($optName, $options)) {
     202            return $default;
     203        }
     204        return $options[$optName];
     205    }
    197206}
    198207
  • calj/trunk/calj.php

    r2918707 r3175296  
    66 * Plugin URI: https://calj.net
    77 * Description: Add the Shabbat times on your site.
    8  * Version: 1.4.1
     8 * Version: 1.4.2
    99 * Author: Gabriel Zerbib <gabriel@calj.net>
    1010 * Author URI: https://calj.net
  • calj/trunk/readme.txt

    r3140268 r3175296  
    44Tags: calendar, date, events, hebrew, jewish, shortcode
    55Requires at least: 4.9
    6 Tested up to: 6.6.1
     6Tested up to: 6.6.2
    77Requires PHP: 5.6
    88Stable tag: trunk
     
    146146== Changelog ==
    147147
     148= 1.4.2 =
     149Better error handling
     150
    148151= 1.4.1 =
    149152Support for PHP 8 -- fixup
Note: See TracChangeset for help on using the changeset viewer.