| 1 |
<?php |
| 2 |
namespace PostSnippets; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
/** |
| 7 |
* Shortcode Handling. |
| 8 |
* |
| 9 |
*/ |
| 10 |
class Shortcode |
| 11 |
{ |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
$this->create(); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Create the functions for shortcodes dynamically and register them |
| 19 |
*/ |
| 20 |
public function create() |
| 21 |
{ |
| 22 |
global $wpdb; |
| 23 |
|
| 24 |
$table_name = $wpdb->prefix . \PostSnippets::TABLE_NAME; |
| 25 |
|
| 26 |
$snippets = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE snippet_shortcode = %d AND snippet_status = 1 AND snippet_content != ''", 1), ARRAY_A ); |
| 27 |
|
| 28 |
foreach ($snippets as $snippet) { |
| 29 |
add_shortcode( $snippet['snippet_title'], |
| 30 |
function ( $atts, $content = null ) use ( $snippet ){ |
| 31 |
return $this->evaluateSnippet($snippet, $atts, $content); |
| 32 |
}); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
public static function evaluateSnippet($snippet, $atts = array(), $content = null){ |
| 37 |
|
| 38 |
if( !empty($snippet) ){ |
| 39 |
|
| 40 |
$default_atts = self::filterVars( $snippet['snippet_vars'] ); |
| 41 |
|
| 42 |
$texturize = $snippet["snippet_wptexturize"]?? false; |
| 43 |
|
| 44 |
foreach ((array) $atts as $key => $val) { |
| 45 |
if ( is_numeric($key) ) { |
| 46 |
$attribute = explode('=', $val, 2); |
| 47 |
|
| 48 |
if (count($attribute) < 2) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
$keyName = trim($attribute[0]); |
| 53 |
$keyValue = trim($attribute[1], " \t\n\r\0\x0B\"'"); |
| 54 |
|
| 55 |
$atts[$keyName] = $keyValue; |
| 56 |
unset($atts[$key]); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
$short_atts = shortcode_atts( $default_atts, $atts ); |
| 61 |
|
| 62 |
$snippet_content = $snippet['snippet_content']; |
| 63 |
|
| 64 |
if ( ! empty( $snippet['snippet_php'] ) && (int) $snippet['snippet_php'] === 1 ) { |
| 65 |
$snippet_content = stripslashes( $snippet_content ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( $content != null ) { |
| 69 |
$short_atts["content"] = $content; |
| 70 |
} |
| 71 |
$snippet_content = self::replaceSnippetVariables( |
| 72 |
$snippet_content, |
| 73 |
$short_atts, |
| 74 |
! empty( $snippet['snippet_php'] ) && (int) $snippet['snippet_php'] === 1 |
| 75 |
); |
| 76 |
|
| 77 |
// There might be the case that a snippet contains |
| 78 |
// the post snippets reserved variable {content} to |
| 79 |
// capture the content in enclosed shortcodes, but |
| 80 |
// the shortcode is used without enclosing it. To |
| 81 |
// avoid outputting {content} as part of the string |
| 82 |
// lets remove possible occurences. |
| 83 |
$snippet_content = str_replace( "{content}", "", $snippet_content ); |
| 84 |
|
| 85 |
// Handle PHP shortcodes |
| 86 |
if ( $snippet['snippet_php'] == 1 ) { |
| 87 |
$snippet_content = self::phpEval( $snippet_content ); |
| 88 |
|
| 89 |
// WPTexturize the Snippet |
| 90 |
if ( ! empty( $snippet['snippet_wptexturize'] ) && ( $snippet['snippet_wptexturize'] == true ) ) { |
| 91 |
$snippet_content = html_entity_decode( addslashes ( wptexturize ( htmlentities( stripslashes ( $snippet_content ), ENT_NOQUOTES ) ) ) ); |
| 92 |
} |
| 93 |
|
| 94 |
} else { |
| 95 |
if ( ! empty( $snippet['snippet_wptexturize'] ) && ( $snippet['snippet_wptexturize'] == true ) ) { |
| 96 |
$snippet_content = html_entity_decode ( addslashes ( wptexturize ( htmlentities( stripslashes ( $snippet_content ), ENT_NOQUOTES ) ) ) ); |
| 97 |
} else { |
| 98 |
$snippet_content = html_entity_decode ( $snippet_content ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
$snippet_content = do_shortcode( stripslashes( $snippet_content ) ); |
| 103 |
|
| 104 |
return $snippet_content; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Evaluate a snippet as PHP code. |
| 110 |
* |
| 111 |
* @since Post Snippets 1.9 |
| 112 |
* @param string $content The snippet to evaluate |
| 113 |
* @return string The result of the evaluation |
| 114 |
*/ |
| 115 |
public static function phpEval($content) |
| 116 |
{ |
| 117 |
if (defined('POST_SNIPPETS_DISABLE_PHP')) { |
| 118 |
return $content; |
| 119 |
} |
| 120 |
|
| 121 |
/**Removing Initial PHP Tag */ |
| 122 |
$content = ltrim($content, "<?php<?PHP<?="); |
| 123 |
|
| 124 |
ob_start(); |
| 125 |
eval($content); |
| 126 |
$content = ob_get_clean(); |
| 127 |
|
| 128 |
return addslashes($content); |
| 129 |
} |
| 130 |
|
| 131 |
public static function replaceSnippetVariables($snippet_content, $short_atts, $php_snippet = false) |
| 132 |
{ |
| 133 |
foreach ( $short_atts as $key => $val ) { |
| 134 |
$short_atts[ $key ] = self::sanitizeVariableValue( $key, $val, $php_snippet ); |
| 135 |
} |
| 136 |
|
| 137 |
if ( $php_snippet ) { |
| 138 |
return self::replacePhpVariables( $snippet_content, $short_atts ); |
| 139 |
} |
| 140 |
|
| 141 |
foreach ( $short_atts as $key => $val ) { |
| 142 |
$snippet_content = str_replace( '{' . $key . '}', $val, $snippet_content ); |
| 143 |
} |
| 144 |
|
| 145 |
return $snippet_content; |
| 146 |
} |
| 147 |
|
| 148 |
public static function sanitizeVariableValue($key, $val, $php_snippet = false) |
| 149 |
{ |
| 150 |
$val = (string) $val; |
| 151 |
|
| 152 |
$colon = strpos($key, ':'); |
| 153 |
|
| 154 |
if ( $colon !== false ) { |
| 155 |
$text = explode(":", $key); |
| 156 |
|
| 157 |
switch (strtolower($text[1])) { |
| 158 |
case 'url': |
| 159 |
$val = esc_url_raw( $val ); |
| 160 |
break; |
| 161 |
case 'text': |
| 162 |
$val = esc_html( $val ); |
| 163 |
break; |
| 164 |
case 'attr': |
| 165 |
$val = esc_attr( $val ); |
| 166 |
break; |
| 167 |
case 'xml': |
| 168 |
$val = esc_xml( $val ); |
| 169 |
break; |
| 170 |
case 'textarea': |
| 171 |
$val = esc_textarea( $val ); |
| 172 |
break; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if ( $php_snippet ) { |
| 177 |
return $val; |
| 178 |
} |
| 179 |
|
| 180 |
return strtr( |
| 181 |
$val, |
| 182 |
array( |
| 183 |
'"' => '"', |
| 184 |
"'" => ''', |
| 185 |
) |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
public static function replacePhpVariables($snippet_content, $short_atts) |
| 190 |
{ |
| 191 |
$string_pattern = '/\'(?:\\\\.|[^\'\\\\])*\'|"(?:\\\\.|[^"\\\\])*"/s'; |
| 192 |
|
| 193 |
$snippet_content = preg_replace_callback( |
| 194 |
$string_pattern, |
| 195 |
function ( $matches ) use ( $short_atts ) { |
| 196 |
$literal = $matches[0]; |
| 197 |
$quote = $literal[0]; |
| 198 |
$body = substr( $literal, 1, -1 ); |
| 199 |
|
| 200 |
foreach ( $short_atts as $key => $val ) { |
| 201 |
$body = str_replace( |
| 202 |
'{' . $key . '}', |
| 203 |
self::escapePhpStringLiteralValue( $val, $quote ), |
| 204 |
$body |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
return $quote . $body . $quote; |
| 209 |
}, |
| 210 |
$snippet_content |
| 211 |
); |
| 212 |
|
| 213 |
foreach ( $short_atts as $key => $val ) { |
| 214 |
$snippet_content = str_replace( '{' . $key . '}', var_export( $val, true ), $snippet_content ); |
| 215 |
} |
| 216 |
|
| 217 |
return $snippet_content; |
| 218 |
} |
| 219 |
|
| 220 |
public static function escapePhpStringLiteralValue($val, $quote) |
| 221 |
{ |
| 222 |
if ( $quote === '"' ) { |
| 223 |
return str_replace( |
| 224 |
array( '\\', '"', '$' ), |
| 225 |
array( '\\\\', '\\"', '\\$' ), |
| 226 |
$val |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
return str_replace( |
| 231 |
array( '\\', "'" ), |
| 232 |
array( '\\\\', "\\'" ), |
| 233 |
$val |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Filters Snippet Variables |
| 239 |
* of Undesired Text. |
| 240 |
* |
| 241 |
* @since Post Snippets 3.1.4 |
| 242 |
* @param string $vars The snippet variable string |
| 243 |
* @return array The result of the evaluation |
| 244 |
*/ |
| 245 |
public static function filterVars($vars = '') |
| 246 |
{ |
| 247 |
if( !empty($vars) ){ |
| 248 |
|
| 249 |
$vars = explode(",", $vars ); |
| 250 |
|
| 251 |
$default_atts = array(); |
| 252 |
|
| 253 |
foreach ($vars as $var) { |
| 254 |
|
| 255 |
$attribute = explode('=', $var); /**This Results in array seperated by = sign */ |
| 256 |
|
| 257 |
foreach ($attribute as $key => $value) { //Filtering Empty Values generated with such variable texts one,two=,,,=xx=one,,==two |
| 258 |
if( empty($value) ) unset($attribute[$key]); |
| 259 |
} |
| 260 |
|
| 261 |
if( empty($attribute) ) continue; /**After Unsetting Empty values above, any empty array still remains, so this line is skipping that */ |
| 262 |
|
| 263 |
$attribute = array_values($attribute); //resetting index to start with zero |
| 264 |
|
| 265 |
$default_value = (count($attribute) > 1) ? $attribute[1] : ''; /**Default values of vars, if set any */ |
| 266 |
|
| 267 |
$default_atts[$attribute[0]] = $default_value; /**Setting Default Atts for shortcode_atts */ |
| 268 |
|
| 269 |
} |
| 270 |
|
| 271 |
return $default_atts; |
| 272 |
} |
| 273 |
else{ |
| 274 |
return array(); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|