| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.13 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Gatekeeper for the {fbExpr ...} shortcode, which evaluates JavaScript from campaign |
| 21 |
* content in the visitor's browser. |
| 22 |
* |
| 23 |
* "{fbExpr ...}" is plain text, so kses passes it through untouched — it is the one way a |
| 24 |
* user without unfiltered_html could smuggle executable JavaScript into a campaign. The |
| 25 |
* feature is therefore limited to campaigns whose author holds that capability, which is |
| 26 |
* the same bar WordPress applies to writing raw script anywhere else. |
| 27 |
* |
| 28 |
* The check is deliberately a plain function of the post author, with no stored state: |
| 29 |
* an earlier version recorded the decision at save time and that machinery twice grew |
| 30 |
* bugs of its own. The trade is documented — see docs/audits/security-audit.md F10. |
| 31 |
*/ |
| 32 |
class Expression |
| 33 |
{ |
| 34 |
/** |
| 35 |
* Marker that switches the feature on for a campaign. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
const SHORTCODE_PREFIX = '{fbExpr'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns whether the given campaign may run expressions. |
| 43 |
* |
| 44 |
* @param int $post_id |
| 45 |
* @param string $content |
| 46 |
* |
| 47 |
* @return bool |
| 48 |
*/ |
| 49 |
public static function isAllowedForCampaign($post_id, $content = '') |
| 50 |
{ |
| 51 |
// Nothing to run. |
| 52 |
if (!is_string($content) || strpos($content, self::SHORTCODE_PREFIX) === false) |
| 53 |
{ |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
if (!self::isFeatureEnabled()) |
| 58 |
{ |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
$post_id = (int) $post_id; |
| 63 |
|
| 64 |
if (!$post_id) |
| 65 |
{ |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
$author_id = (int) get_post_field('post_author', $post_id); |
| 70 |
|
| 71 |
if (!$author_id) |
| 72 |
{ |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
return user_can($author_id, 'unfiltered_html'); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Whether the feature is enabled site-wide. |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
public static function isFeatureEnabled() |
| 85 |
{ |
| 86 |
/** |
| 87 |
* Allows a site to switch off JavaScript expressions entirely, regardless of who |
| 88 |
* authored the campaign. |
| 89 |
* |
| 90 |
* @param bool $enabled |
| 91 |
*/ |
| 92 |
return (bool) apply_filters('firebox/expressions/enabled', true); |
| 93 |
} |
| 94 |
} |
| 95 |
|