| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shortcode `[s2If /]` ( inner processing routines ). |
| 4 |
* |
| 5 |
* Copyright: © 2009-2011 |
| 6 |
* {@link http://www.websharks-inc.com/ WebSharks, Inc.} |
| 7 |
* ( coded in the USA ) |
| 8 |
* |
| 9 |
* Released under the terms of the GNU General Public License. |
| 10 |
* You should have received a copy of the GNU General Public License, |
| 11 |
* along with this software. In the main directory, see: /licensing/ |
| 12 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 13 |
* |
| 14 |
* @package s2Member\s2If |
| 15 |
* @since 3.5 |
| 16 |
*/ |
| 17 |
if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"])) |
| 18 |
exit("Do not access this file directly."); |
| 19 |
/**/ |
| 20 |
if (!class_exists ("c_ws_plugin__s2member_sc_if_conds_in")) |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Shortcode `[s2If /]` ( inner processing routines ). |
| 24 |
* |
| 25 |
* @package s2Member\s2If |
| 26 |
* @since 3.5 |
| 27 |
*/ |
| 28 |
class c_ws_plugin__s2member_sc_if_conds_in |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Handles the Shortcode for: `[s2If /]`. |
| 32 |
* |
| 33 |
* These Shortcodes are also safe to use on a Multisite Blog Farm. |
| 34 |
* |
| 35 |
* Is Multisite Networking enabled? Please keep the following in mind. |
| 36 |
* ``current_user_can()``, will ALWAYS return true for a Super Admin! |
| 37 |
* *( this can be confusing when testing conditionals )*. |
| 38 |
* |
| 39 |
* If you're running a Multisite Blog Farm, you can Filter this array: |
| 40 |
* `ws_plugin__s2member_sc_if_conditionals_blog_farm_safe` |
| 41 |
* ``$blog_farm_safe`` |
| 42 |
* |
| 43 |
* @package s2Member\s2If |
| 44 |
* @since 3.5 |
| 45 |
* |
| 46 |
* @attaches-to: ``add_shortcode("s2If")`` + _s2If, __s2If, ___s2If for nesting. |
| 47 |
* |
| 48 |
* @param array $attr An array of Attributes. |
| 49 |
* @param str $content Content inside the Shortcode. |
| 50 |
* @param str $shortcode The actual Shortcode name itself. |
| 51 |
* @return str The ``$content`` if true, else an empty string. |
| 52 |
* |
| 53 |
* @todo Add support for ``else if`` logic. |
| 54 |
* @todo Add support for nested AND/OR conditionals inside the ONE Shortcode. |
| 55 |
* @todo Address possible security issue on sites with multiple editors, some of which should not have access to this feature. |
| 56 |
*/ |
| 57 |
public static function sc_if_conditionals ($attr = FALSE, $content = FALSE, $shortcode = FALSE) |
| 58 |
{ |
| 59 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 60 |
do_action ("ws_plugin__s2member_before_sc_if_conditionals", get_defined_vars ()); |
| 61 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 62 |
/**/ |
| 63 |
$blog_farm_safe = apply_filters ("ws_plugin__s2member_sc_if_conditionals_blog_farm_safe", array ("is_user_logged_in", "is_user_not_logged_in", "user_is", "user_is_not", "user_can", "user_cannot", "current_user_is", "current_user_is_not", "current_user_can", "current_user_cannot", "is_admin", "is_blog_admin", "is_user_admin", "is_network_admin", "is_404", "is_home", "is_front_page", "is_singular", "is_single", "is_page", "is_page_template", "is_attachment", "is_feed", "is_archive", "is_search", "is_category", "is_tax", "is_tag", "has_tag", "is_author", "is_date", "is_day", "is_month", "is_time", "is_year", "is_sticky", "is_paged", "is_preview", "is_comments_popup", "in_the_loop", "comments_open", "pings_open", "has_excerpt", "has_post_thumbnail"), get_defined_vars ()); |
| 64 |
/**/ |
| 65 |
$attr = c_ws_plugin__s2member_utils_strings::trim_quot_deep ((array)$attr); /* Force array, and fix " in attrs. */ |
| 66 |
/**/ |
| 67 |
foreach ($attr as $attr_key => $attr_value) /* Detects and removes logical attributes. */ |
| 68 |
/* It's NOT possible to mix logic. You MUST stick to one type of logic or another. */ |
| 69 |
/* If both types of logic are needed, you MUST use two different Shortcodes. */ |
| 70 |
if (preg_match ("/^(&&|&&|&&|AND|\|\||OR|[\!\=\<\>]+)$/i", $attr_value)) |
| 71 |
{ /* Stick with AND/OR. Ampersands are corrupted by the Visual Editor. */ |
| 72 |
/**/ |
| 73 |
$logicals[] = strtolower ($attr_value); /* Place all logicals into an array here. */ |
| 74 |
unset($attr[$attr_key]); /* ^ Detect logic here. We'll use the first key #0. */ |
| 75 |
/**/ |
| 76 |
if (preg_match ("/^[\!\=\<\>]+$/i", $attr_value)) /* Error on these operators. */ |
| 77 |
{ |
| 78 |
trigger_error ("s2If, invalid operator [ " . $attr_value . " ]. Simple Conditionals cannot process operators like ( == != <> ). Please use Advanced (PHP) Conditionals instead.", E_USER_ERROR); |
| 79 |
return ""; /* Return now; empty string in this case. */ |
| 80 |
} |
| 81 |
} |
| 82 |
/**/ |
| 83 |
if (is_array ($logicals) && !empty ($logicals) && count (array_unique ($logicals)) > 1) |
| 84 |
{ |
| 85 |
trigger_error ("s2If, AND/OR malformed conditional logic. It's NOT possible to mix logic using AND/OR combinations. You MUST stick to one type of logic or another. If both types of logic are needed, you MUST use two different Shortcode expressions. Or, use Advanced (PHP) Conditionals instead.", E_USER_ERROR); |
| 86 |
return ""; /* Return now; empty string in this case. */ |
| 87 |
} |
| 88 |
/**/ |
| 89 |
$conditional_logic = (is_array ($logicals) && !empty ($logicals) && preg_match ("/^(\|\||OR)$/i", $logicals[0])) ? "OR" : "AND"; |
| 90 |
/**/ |
| 91 |
eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;'); |
| 92 |
do_action ("ws_plugin__s2member_before_sc_if_conditionals_after_conditional_logic", get_defined_vars ()); |
| 93 |
unset ($__refs, $__v); /* Unset defined __refs, __v. */ |
| 94 |
/**/ |
| 95 |
if ($conditional_logic === "AND") /* This is the AND variation. This routine analyzes conditionals using AND logic ( the default behavior ). */ |
| 96 |
{ |
| 97 |
foreach ($attr as $attr_value) /* This is the AND variation. This routine analyzes conditionals using AND logic ( the default behavior ). */ |
| 98 |
{ |
| 99 |
if (preg_match ("/^(\!?)(.+?)(\()(.*?)(\))$/", $attr_value, $m) && ($exclamation = $m[1]) !== "nill" && ($conditional = $m[2]) && ($attr_args = preg_replace ("/[\r\n\t\s ]/", "", $m[4])) !== "nill") |
| 100 |
{ |
| 101 |
if (!is_multisite () || !c_ws_plugin__s2member_utils_conds::is_multisite_farm () || is_main_site () || !preg_match ("/[\(\)]/", $attr_args)) /* Disallow functions as arguments on a Multisite Blog Farm. */ |
| 102 |
{ |
| 103 |
if (is_array ($args = preg_split ("/[;,]+/", $attr_args, 0, PREG_SPLIT_NO_EMPTY))) /* Convert all arguments into an array. And take note; possibly into an empty array. */ |
| 104 |
{ |
| 105 |
if (!is_multisite () || !c_ws_plugin__s2member_utils_conds::is_multisite_farm () || is_main_site () || in_array (strtolower ($conditional), $blog_farm_safe)) |
| 106 |
{ |
| 107 |
$test = ($exclamation) ? false : true; /* If !exclamation ( false ) otherwise this defaults to true. */ |
| 108 |
/**/ |
| 109 |
if (preg_match ("/^\{(.*?)\}$/", $attr_args)) /* Single argument passed as an array. */ |
| 110 |
{ |
| 111 |
if ($test === true && !call_user_func ($conditional, $args)) |
| 112 |
{ |
| 113 |
$condition_failed = true; |
| 114 |
break; |
| 115 |
} |
| 116 |
else if ($test === false && call_user_func ($conditional, $args)) |
| 117 |
{ |
| 118 |
$condition_failed = true; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
/**/ |
| 123 |
else if (empty ($args)) /* No arguments at all. */ |
| 124 |
{ |
| 125 |
if ($test === true && !call_user_func ($conditional)) |
| 126 |
{ |
| 127 |
$condition_failed = true; |
| 128 |
break; |
| 129 |
} |
| 130 |
/**/ |
| 131 |
else if ($test === false && call_user_func ($conditional)) |
| 132 |
{ |
| 133 |
$condition_failed = true; |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
/**/ |
| 138 |
else if ($test === true && !call_user_func_array ($conditional, $args)) |
| 139 |
{ |
| 140 |
$condition_failed = true; |
| 141 |
break; |
| 142 |
} |
| 143 |
/**/ |
| 144 |
else if ($test === false && call_user_func_array ($conditional, $args)) |
| 145 |
{ |
| 146 |
$condition_failed = true; |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
trigger_error ("s2If, unsafe conditional function [ " . $attr_value . " ]", E_USER_ERROR); |
| 153 |
return ""; /* Return now; empty string in this case. */ |
| 154 |
} |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
trigger_error ("s2If, conditional args are NOT an array [ " . $attr_value . " ]", E_USER_ERROR); |
| 159 |
return ""; /* Return now; empty string in this case. */ |
| 160 |
} |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
trigger_error ("s2If, unsafe conditional args [ " . $attr_value . " ]", E_USER_ERROR); |
| 165 |
return ""; /* Return now; empty string in this case. */ |
| 166 |
} |
| 167 |
} |
| 168 |
else |
| 169 |
{ |
| 170 |
trigger_error ("s2If, malformed conditional [ " . $attr_value . " ]", E_USER_ERROR); |
| 171 |
return ""; /* Return now; empty string in this case. */ |
| 172 |
} |
| 173 |
} |
| 174 |
/* Supports nested Shortcodes. */ |
| 175 |
return do_shortcode (apply_filters ("ws_plugin__s2member_sc_if_conditionals", (($condition_failed) ? "" : $content), get_defined_vars ())); |
| 176 |
} |
| 177 |
/**/ |
| 178 |
else if ($conditional_logic === "OR") /* This is the OR variation. This routine analyzes conditionals using OR logic, instead of AND logic. */ |
| 179 |
{ |
| 180 |
foreach ($attr as $attr_value) /* This is the OR variation. This routine analyzes conditionals using OR logic, instead of AND logic. */ |
| 181 |
{ |
| 182 |
if (preg_match ("/^(\!?)(.+?)(\()(.*?)(\))$/", $attr_value, $m) && ($exclamation = $m[1]) !== "nill" && ($conditional = $m[2]) && ($attr_args = preg_replace ("/[\r\n\t\s ]/", "", $m[4])) !== "nill") |
| 183 |
{ |
| 184 |
if (!is_multisite () || !c_ws_plugin__s2member_utils_conds::is_multisite_farm () || is_main_site () || !preg_match ("/[\(\)]/", $attr_args)) /* Disallow functions as arguments on a Multisite Blog Farm. */ |
| 185 |
{ |
| 186 |
if (is_array ($args = preg_split ("/[;,]+/", $attr_args, 0, PREG_SPLIT_NO_EMPTY))) /* Convert all arguments into an array. And take note; possibly into an empty array. */ |
| 187 |
{ |
| 188 |
if (!is_multisite () || !c_ws_plugin__s2member_utils_conds::is_multisite_farm () || is_main_site () || in_array (strtolower ($conditional), $blog_farm_safe)) |
| 189 |
{ |
| 190 |
$test = ($exclamation) ? false : true; /* If !exclamation ( false ) otherwise this defaults to true. */ |
| 191 |
/**/ |
| 192 |
if (preg_match ("/^\{(.*?)\}$/", $attr_args)) /* Single argument passed as an array. */ |
| 193 |
{ |
| 194 |
if ($test === true && call_user_func ($conditional, $args)) |
| 195 |
{ |
| 196 |
$condition_succeeded = true; |
| 197 |
break; |
| 198 |
} |
| 199 |
else if ($test === false && !call_user_func ($conditional, $args)) |
| 200 |
{ |
| 201 |
$condition_succeeded = true; |
| 202 |
break; |
| 203 |
} |
| 204 |
} |
| 205 |
/**/ |
| 206 |
else if (empty ($args)) /* No arguments at all. */ |
| 207 |
{ |
| 208 |
if ($test === true && call_user_func ($conditional)) |
| 209 |
{ |
| 210 |
$condition_succeeded = true; |
| 211 |
break; |
| 212 |
} |
| 213 |
/**/ |
| 214 |
else if ($test === false && !call_user_func ($conditional)) |
| 215 |
{ |
| 216 |
$condition_succeeded = true; |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
/**/ |
| 221 |
else if ($test === true && call_user_func_array ($conditional, $args)) |
| 222 |
{ |
| 223 |
$condition_succeeded = true; |
| 224 |
break; |
| 225 |
} |
| 226 |
/**/ |
| 227 |
else if ($test === false && !call_user_func_array ($conditional, $args)) |
| 228 |
{ |
| 229 |
$condition_succeeded = true; |
| 230 |
break; |
| 231 |
} |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
trigger_error ("s2If, unsafe conditional function [ " . $attr_value . " ]", E_USER_ERROR); |
| 236 |
return ""; /* Return now; empty string in this case. */ |
| 237 |
} |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
trigger_error ("s2If, conditional args are NOT an array [ " . $attr_value . " ]", E_USER_ERROR); |
| 242 |
return ""; /* Return now; empty string in this case. */ |
| 243 |
} |
| 244 |
} |
| 245 |
else |
| 246 |
{ |
| 247 |
trigger_error ("s2If, unsafe conditional args [ " . $attr_value . " ]", E_USER_ERROR); |
| 248 |
return ""; /* Return now; empty string in this case. */ |
| 249 |
} |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
trigger_error ("s2If, malformed conditional [ " . $attr_value . " ]", E_USER_ERROR); |
| 254 |
return ""; /* Return now; empty string in this case. */ |
| 255 |
} |
| 256 |
} |
| 257 |
/* Supports nested Shortcodes. */ |
| 258 |
return do_shortcode (apply_filters ("ws_plugin__s2member_sc_if_conditionals", (($condition_succeeded) ? $content : ""), get_defined_vars ())); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
?> |