Changeset 2593658
- Timestamp:
- 09/04/2021 11:45:13 AM (5 years ago)
- Location:
- temporarily-hidden-content/trunk
- Files:
-
- 4 edited
-
changelog.txt (modified) (1 diff)
-
includes/class-temporarily-hidden-content-template.php (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
-
temporarily-hidden-content.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
temporarily-hidden-content/trunk/changelog.txt
r2531416 r2593658 1 1 == Changelog == 2 3 = 1.0.4 = 4 * Fixed bug with version 8 of PHP. 2 5 3 6 = 1.0.3 = -
temporarily-hidden-content/trunk/includes/class-temporarily-hidden-content-template.php
r2313564 r2593658 1 1 <?php 2 2 3 class Temporarily_Hidden_Content_Template {3 class Temporarily_Hidden_Content_Template { 4 4 5 private $path = '';6 private $data = array();5 private $path = ''; 6 private $data = array(); 7 7 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 . ')'; 11 115 } 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); 14 122 } 15 123 } 124 $isset = str_replace('{HERE}', $defaultValue, $isset); 125 return $isset; 126 } 16 127 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'; 21 161 } else { 22 return $this->data;162 $codeLines[$i] = 'echo \'' . $codeLines[$i] . '\' . "\\n";'; 23 163 } 24 164 } 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; 172 170 } 171 } -
temporarily-hidden-content/trunk/readme.txt
r2531416 r2593658 4 4 Tags: seo, gutenberg, blocks, content, entries, pages, schedule 5 5 Requires at least: 3.0.1 6 Tested up to: 5. 7.26 Tested up to: 5.8 7 7 Stable tag: trunk 8 8 Requires PHP: 5.5 … … 77 77 == Changelog == 78 78 79 = 1.0.4 = 80 * Fixed bug with version 8 of PHP. 81 79 82 = 1.0.3 = 80 83 * Added compatibility with Wordpress 5.7.2. -
temporarily-hidden-content/trunk/temporarily-hidden-content.php
r2531416 r2593658 5 5 * Plugin Name: Temporarily Hidden Content 6 6 * Description: Hide or show content until specific date. 7 * Version: 1.0. 37 * Version: 1.0.4 8 8 * Author: Codents 9 9 * Author URI: https://codents.net … … 20 20 define('TEMPHC_NAME', 'Temporarily Hidden Content'); 21 21 define('TEMPHC_SLUG', 'temporarily-hidden-content'); 22 define('TEMPHC_VERSION', '1.0. 3');22 define('TEMPHC_VERSION', '1.0.4'); 23 23 define('TEMPHC_DB_TERM', 'temphc'); 24 24
Note: See TracChangeset
for help on using the changeset viewer.