Plugin Directory

Changeset 2593658


Ignore:
Timestamp:
09/04/2021 11:45:13 AM (5 years ago)
Author:
codents
Message:

Fixed php8 bug

Location:
temporarily-hidden-content/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • temporarily-hidden-content/trunk/changelog.txt

    r2531416 r2593658  
    11== Changelog ==
     2
     3= 1.0.4 =
     4* Fixed bug with version 8 of PHP.
    25
    36= 1.0.3 =
  • temporarily-hidden-content/trunk/includes/class-temporarily-hidden-content-template.php

    r2313564 r2593658  
    11<?php
    22
    3     class Temporarily_Hidden_Content_Template {
     3class Temporarily_Hidden_Content_Template {
    44
    5         private $path = '';
    6         private $data = array();
     5    private $path = '';
     6    private $data = array();
    77
    8         public function __construct($path, $labels = null) {
    9             if ($path) {
    10                 $this->path = $path;
     8    public function __construct($path, $labels = null) {
     9        if ($path) {
     10            $this->path = $path;
     11        }
     12        if ($labels) {
     13            $this->add($labels);
     14        }
     15    }
     16
     17    public function get($key = null) {
     18        if ($key) {
     19            return isset($this->data[strtoupper($key)]) ?
     20                $this->data[strtoupper($key)] : strtoupper($key);
     21        } else {
     22            return $this->data;
     23        }
     24    }
     25
     26    public function add($labels, $block = null) {
     27        if ($block) {
     28            $this->blocks($block, $labels);
     29            return false;
     30        }
     31        foreach ($labels as $key => $value) {
     32            $this->data[strtoupper($key)] = $value;
     33        }
     34        return true;
     35    }
     36
     37    private function blocks($block, $labels) {
     38        // Before saving a block, we update it
     39        // including the index of the same
     40        $index = isset($this->data[$block]) ? sizeof($this->data[$block]) : 0;
     41        $this->data[$block][] = array_merge(array(
     42            'INDEX' => $index
     43        ), array_change_key_case($labels, CASE_UPPER));
     44        return true;
     45    }
     46
     47    private function load($file, $context = null) {
     48        // Get the template source
     49        $file = $this->path . '/' . $file;
     50        $fileCode = implode('', @file($file));
     51        if ($context) {
     52            $contextReference = 'CONTEXT_' . strtoupper(md5(sizeof($this->data)));
     53            $this->data[$contextReference] = $context;
     54            $fileCode = str_replace('{FOR ', '{FOR ' . $contextReference . '.', $fileCode);
     55            $fileCode = str_replace('{{', '{{' . $contextReference . '.', $fileCode);
     56        }
     57        return $fileCode;
     58    }
     59
     60    public function render($mainFile, $regions = array()) {
     61        // Main template load
     62        $mainCode = $this->load($mainFile);
     63        // We load all regions and include
     64        // them in the main template code
     65        foreach ($regions as $key => $value) {
     66            $template = isset($value['TEMPLATE']) ? $value['TEMPLATE'] : $value;
     67            $context = isset($value['CONTEXT']) ? $value['CONTEXT'] : null;
     68            $regionCode = $this->load($template, $context);
     69            $mainCode = str_replace($key, $regionCode, $mainCode);
     70        }
     71        // After loading the regions, we compiled
     72        // the complete code and we execute it
     73        return eval($this->compile($mainCode));
     74    }
     75
     76    public function getSource($mainFile, $regions = array()) {
     77        ob_start();
     78        $this->render($mainFile, $regions);
     79        $output = ob_get_contents();
     80        ob_end_clean();
     81        return $output;
     82    }
     83
     84    private function replaceVars($code) {
     85        $varReferences = array();
     86        preg_match_all('#\{\{(UCASE_|LCASE_)?(.*?)\}\}#is', $code, $varReferences);
     87        foreach ($varReferences[2] as $key => $value) {
     88            $code = str_replace($varReferences[0][$key], '/*START-ISSET*/ . ' . $this->getIsset($value, array(
     89                'UPPER_CASE' => ($varReferences[1][$key] === 'UCASE_') ? true : false,
     90                'LOWER_CASE' => ($varReferences[1][$key] === 'LCASE_') ? true : false
     91            )) . '. /*END-ISSET*/', $code);
     92        }
     93        return $code;
     94    }
     95
     96    private function getIsset($var, $options = array()) {
     97        $defaultValue = '\'\'';
     98        if (isset($options['FOR']) && $options['FOR']) {
     99            $defaultValue = 'array()';
     100        }
     101        $references = explode('.', $var);
     102        $references = array_reverse($references);
     103        $isset = '$temp = ({HERE})';
     104        $base = '';
     105        $base1 = '';
     106        foreach ($references as $key => $value) {
     107            $prefix = str_replace('.', '_', explode($value, $var)[0]);
     108            $base1 = '$_' . $prefix . $value . '_value' . $base;
     109            $base = '[\'' . $value . '\']' . $base;
     110            $look = '$this->data';
     111            $look = ($key === 0) ? $look . $base : $base1;
     112            $print = $look;
     113            if (isset($options['UPPER_CASE']) && $options['UPPER_CASE']) {
     114                $print = 'strtoupper(' . $print . ')';
    11115            }
    12             if ($labels) {
    13                 $this->add($labels);
     116            if (isset($options['LOWER_CASE']) && $options['LOWER_CASE']) {
     117                $print = 'strtolower(' . $print . ')';
     118            }
     119            $isset = str_replace('{HERE}', '(isset(' . $look . ') ? ' . $print . ' : {HERE})', $isset);
     120            if ($key === (sizeof($references) - 1)) {
     121                $isset = str_replace('{HERE}', '(isset($this->data' . $base . ') ? $this->data' . $base . ' : {HERE})', $isset);
    14122            }
    15123        }
     124        $isset = str_replace('{HERE}', $defaultValue, $isset);
     125        return $isset;
     126    }
    16127
    17         public function get($key = null) {
    18             if ($key) {
    19                 return isset($this->data[strtoupper($key)]) ?
    20                     $this->data[strtoupper($key)] : strtoupper($key);
     128    // Compiles the given string of code,
     129    // and return the result in a string
     130    private function compile($code) {
     131        // Replace \ with \\ and then ' with \'.
     132        $code = str_replace('\\', '\\\\', $code);
     133        $code = str_replace('\'', '\\\'', $code);
     134        $code = $this->replaceVars($code);
     135        // Break it up into lines
     136        $codeLines = explode("\n", $code);
     137        for ($i = 0; $i < sizeof($codeLines); $i++) {
     138            $codeLines[$i] = chop($codeLines[$i]);
     139            if (preg_match('#{IF (.*?)}#', $codeLines[$i], $m)) {
     140                $ifCondition = str_replace('/*START-ISSET*/ .', '', $m[1]);
     141                $ifCondition = str_replace('. /*END-ISSET*/', '', $ifCondition);
     142                $codeLines[$i] = "\n" . 'if (' . $ifCondition . ')';
     143                $codeLines[$i] .= "\n" . '{';
     144            } elseif (preg_match('#{ELSE IF (.*?)}#', $codeLines[$i], $m)) {
     145                $ifCondition = str_replace('/*START-ISSET*/ .', '', $m[1]);
     146                $ifCondition = str_replace('. /*END-ISSET*/', '', $ifCondition);
     147                $codeLines[$i] = "\n" . ' } else if (' . $ifCondition . ')';
     148                $codeLines[$i] .= "\n" . '{';
     149            } else if (preg_match('#{ELSE}#', $codeLines[$i], $m)) {
     150                $codeLines[$i] = '} else {' . "\n";
     151            } else if (preg_match('#{END IF}#', $codeLines[$i], $m)) {
     152                $codeLines[$i] = '} // END IF';
     153            } else if (preg_match('#{FOR (.*?)}#', $codeLines[$i], $m)) {
     154                $key = str_replace('.', '_', $m[1]);
     155                $codeLines[$i] = 'foreach (' . $this->getIsset($m[1], array(
     156                    'FOR' => true
     157                )) . ' as $_' . $key . '_key => $_' . $key . '_value)';
     158                $codeLines[$i] .= "\n" . '{';
     159            } else if (preg_match('#{END FOR}#', $codeLines[$i], $m)) {
     160                $codeLines[$i] = '} // END FOR';
    21161            } else {
    22                 return $this->data;
     162                $codeLines[$i] = 'echo \'' . $codeLines[$i] . '\' . "\\n";';
    23163            }
    24164        }
    25 
    26         public function add($labels, $block = null) {
    27             if ($block) {
    28                 $this->blocks($block, $labels);
    29                 return false;
    30             }
    31             while (list($key, $value) = each($labels)) {
    32                 $this->data[strtoupper($key)] = $value;
    33             }
    34             return true;
    35         }
    36 
    37         private function blocks($block, $labels) {
    38             // Before saving a block, we update it
    39             // including the index of the same
    40             $index = isset($this->data[$block]) ? sizeof($this->data[$block]) : 0;
    41             $this->data[$block][] = array_merge(array(
    42                 'INDEX' => $index
    43             ), array_change_key_case($labels, CASE_UPPER));
    44             return true;
    45         }
    46 
    47         private function load($file, $context = null) {
    48             // Get the template source
    49             $file = $this->path . '/' . $file;
    50             $fileCode = implode('', @file($file));
    51             if ($context) {
    52                 $contextReference = 'CONTEXT_' . strtoupper(md5(sizeof($this->data)));
    53                 $this->data[$contextReference] = $context;
    54                 $fileCode = str_replace('{FOR ', '{FOR ' . $contextReference . '.', $fileCode);
    55                 $fileCode = str_replace('{{', '{{' . $contextReference . '.', $fileCode);
    56             }
    57             return $fileCode;
    58         }
    59 
    60         public function render($mainFile, $regions = array()) {
    61             // Main template load
    62             $mainCode = $this->load($mainFile);
    63             // We load all regions and include
    64             // them in the main template code
    65             foreach ($regions as $key => $value) {
    66                 $template = isset($value['TEMPLATE']) ? $value['TEMPLATE'] : $value;
    67                 $context = isset($value['CONTEXT']) ? $value['CONTEXT'] : null;
    68                 $regionCode = $this->load($template, $context);
    69                 $mainCode = str_replace($key, $regionCode, $mainCode);
    70             }
    71             // After loading the regions, we compiled
    72             // the complete code and we execute it
    73             return eval($this->compile($mainCode));
    74         }
    75 
    76         public function getSource($mainFile, $regions = array()) {
    77             ob_start();
    78             $this->render($mainFile, $regions);
    79             $output = ob_get_contents();
    80             ob_end_clean();
    81             return $output;
    82         }
    83 
    84         private function replaceVars($code) {
    85             $varReferences = array();
    86             preg_match_all('#\{\{(UCASE_|LCASE_)?(.*?)\}\}#is', $code, $varReferences);
    87             foreach ($varReferences[2] as $key => $value) {
    88                 $code = str_replace($varReferences[0][$key], '/*START-ISSET*/ . ' . $this->getIsset($value, array(
    89                     'UPPER_CASE' => ($varReferences[1][$key] === 'UCASE_') ? true : false,
    90                     'LOWER_CASE' => ($varReferences[1][$key] === 'LCASE_') ? true : false
    91                 )) . '. /*END-ISSET*/', $code);
    92             }
    93             return $code;
    94         }
    95 
    96         private function getIsset($var, $options = array()) {
    97             $defaultValue = '\'\'';
    98             if (isset($options['FOR']) && $options['FOR']) {
    99                 $defaultValue = 'array()';
    100             }
    101             $references = explode('.', $var);
    102             $references = array_reverse($references);
    103             $isset = '$temp = ({HERE})';
    104             $base = '';
    105             $base1 = '';
    106             foreach ($references as $key => $value) {
    107                 $prefix = str_replace('.', '_', explode($value, $var)[0]);
    108                 $base1 = '$_' . $prefix . $value . '_value' . $base;
    109                 $base = '[\'' . $value . '\']' . $base;
    110                 $look = '$this->data';
    111                 $look = ($key === 0) ? $look . $base : $base1;
    112                 $print = $look;
    113                 if (isset($options['UPPER_CASE']) && $options['UPPER_CASE']) {
    114                     $print = 'strtoupper(' . $print . ')';
    115                 }
    116                 if (isset($options['LOWER_CASE']) && $options['LOWER_CASE']) {
    117                     $print = 'strtolower(' . $print . ')';
    118                 }
    119                 $isset = str_replace('{HERE}', '(isset(' . $look . ') ? ' . $print . ' : {HERE})', $isset);
    120                 if ($key === (sizeof($references) - 1))  {
    121                     $isset = str_replace('{HERE}', '(isset($this->data' . $base . ') ? $this->data' . $base . ' : {HERE})', $isset);
    122                 }
    123             }
    124             $isset = str_replace('{HERE}', $defaultValue, $isset);
    125             return $isset;
    126         }
    127        
    128         // Compiles the given string of code,
    129         // and return the result in a string
    130         private function compile($code) {
    131             // Replace \ with \\ and then ' with \'.
    132             $code = str_replace('\\', '\\\\', $code);
    133             $code = str_replace('\'', '\\\'', $code);
    134             $code = $this->replaceVars($code);
    135             // Break it up into lines
    136             $codeLines = explode("\n", $code);
    137             for ($i = 0; $i < sizeof($codeLines); $i++) {
    138                 $codeLines[$i] = chop($codeLines[$i]);
    139                 if (preg_match('#{IF (.*?)}#', $codeLines[$i], $m)) {
    140                     $ifCondition = str_replace('/*START-ISSET*/ .', '', $m[1]);
    141                     $ifCondition = str_replace('. /*END-ISSET*/', '', $ifCondition);
    142                     $codeLines[$i] = "\n" . 'if (' . $ifCondition . ')';
    143                     $codeLines[$i] .= "\n" . '{';
    144                 } elseif (preg_match('#{ELSE IF (.*?)}#', $codeLines[$i], $m)) {
    145                     $ifCondition = str_replace('/*START-ISSET*/ .', '', $m[1]);
    146                     $ifCondition = str_replace('. /*END-ISSET*/', '', $ifCondition);
    147                     $codeLines[$i] = "\n" . ' } else if (' . $ifCondition . ')';
    148                     $codeLines[$i] .= "\n" . '{';
    149                 } else if (preg_match('#{ELSE}#', $codeLines[$i], $m)) {
    150                     $codeLines[$i] = '} else {' . "\n";
    151                 } else if (preg_match('#{END IF}#', $codeLines[$i], $m)) {
    152                     $codeLines[$i] = '} // END IF';
    153                 } else if (preg_match('#{FOR (.*?)}#', $codeLines[$i], $m)) {
    154                     $key = str_replace('.', '_', $m[1]);
    155                     $codeLines[$i] = 'foreach (' . $this->getIsset($m[1], array(
    156                         'FOR' => true
    157                     )) . ' as $_' . $key . '_key => $_' . $key . '_value)';
    158                     $codeLines[$i] .= "\n" . '{';
    159                 } else if (preg_match('#{END FOR}#', $codeLines[$i], $m)) {
    160                     $codeLines[$i] = '} // END FOR';
    161                 } else {
    162                     $codeLines[$i] = 'echo \'' . $codeLines[$i] . '\' . "\\n";';
    163                 }
    164             }
    165             // Bring it back into a single string of lines of code
    166             $code = implode("\n", $codeLines);
    167             $code = str_replace('/*START-ISSET*/', '\'', $code);
    168             $code = str_replace('/*END-ISSET*/', '\'', $code);
    169             return $code;
    170         }
    171 
     165        // Bring it back into a single string of lines of code
     166        $code = implode("\n", $codeLines);
     167        $code = str_replace('/*START-ISSET*/', '\'', $code);
     168        $code = str_replace('/*END-ISSET*/', '\'', $code);
     169        return $code;
    172170    }
     171}
  • temporarily-hidden-content/trunk/readme.txt

    r2531416 r2593658  
    44Tags: seo, gutenberg, blocks, content, entries, pages, schedule
    55Requires at least: 3.0.1
    6 Tested up to: 5.7.2
     6Tested up to: 5.8
    77Stable tag: trunk
    88Requires PHP: 5.5
     
    7777== Changelog ==
    7878
     79= 1.0.4 =
     80* Fixed bug with version 8 of PHP.
     81
    7982= 1.0.3 =
    8083* Added compatibility with Wordpress 5.7.2.
  • temporarily-hidden-content/trunk/temporarily-hidden-content.php

    r2531416 r2593658  
    55 * Plugin Name:       Temporarily Hidden Content
    66 * Description:       Hide or show content until specific date.
    7  * Version:           1.0.3
     7 * Version:           1.0.4
    88 * Author:            Codents
    99 * Author URI:        https://codents.net
     
    2020define('TEMPHC_NAME', 'Temporarily Hidden Content');
    2121define('TEMPHC_SLUG', 'temporarily-hidden-content');
    22 define('TEMPHC_VERSION', '1.0.3');
     22define('TEMPHC_VERSION', '1.0.4');
    2323define('TEMPHC_DB_TERM', 'temphc');
    2424
Note: See TracChangeset for help on using the changeset viewer.