| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gated Token |
| 4 |
* |
| 5 |
* Value object wrapping a single wp_frm_gated_tokens DB row. |
| 6 |
* Encapsulates expiry checks and item-level validation. |
| 7 |
* |
| 8 |
* @package Formidable |
| 9 |
* |
| 10 |
* @since 6.33 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
die( 'You are not allowed to call this page directly.' ); |
| 15 |
} |
| 16 |
|
| 17 |
class FrmGatedToken { |
| 18 |
|
| 19 |
/** |
| 20 |
* Full DB row from wp_frm_gated_tokens. |
| 21 |
* |
| 22 |
* @var object |
| 23 |
*/ |
| 24 |
private $row; |
| 25 |
|
| 26 |
/** |
| 27 |
* SHA-256 hash stored in the DB (token_hash column). |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $token_hash; |
| 32 |
|
| 33 |
/** |
| 34 |
* ID of the frm_form_actions post this token belongs to. |
| 35 |
* |
| 36 |
* @var int |
| 37 |
*/ |
| 38 |
private $action_id; |
| 39 |
|
| 40 |
/** |
| 41 |
* Unix timestamp after which the token is expired, or null if it never expires. |
| 42 |
* |
| 43 |
* @var int|null |
| 44 |
*/ |
| 45 |
private $expired_at; |
| 46 |
|
| 47 |
/** |
| 48 |
* WordPress user ID stored with this token, or null when not user-scoped. |
| 49 |
* |
| 50 |
* @var int|null |
| 51 |
*/ |
| 52 |
private $user_id; |
| 53 |
|
| 54 |
/** |
| 55 |
* @param object $row DB row from wp_frm_gated_tokens. |
| 56 |
*/ |
| 57 |
public function __construct( $row ) { |
| 58 |
$this->row = $row; |
| 59 |
$this->token_hash = (string) $row->token_hash; |
| 60 |
$this->action_id = (int) $row->action_id; |
| 61 |
$this->expired_at = null !== $row->expired_at ? (int) $row->expired_at : null; |
| 62 |
$this->user_id = ! empty( $row->user_id ) ? (int) $row->user_id : null; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* SHA-256 hash stored in the DB (token_hash column). |
| 67 |
* |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
public function get_hash() { |
| 71 |
return $this->token_hash; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* ID of the frm_form_actions post this token belongs to. |
| 76 |
* |
| 77 |
* @return int |
| 78 |
*/ |
| 79 |
public function get_action_id() { |
| 80 |
return $this->action_id; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Unix timestamp after which the token is expired, or null if it never expires. |
| 85 |
* |
| 86 |
* @return int|null |
| 87 |
*/ |
| 88 |
public function get_expired_at() { |
| 89 |
return $this->expired_at; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* WordPress user ID stored with this token, or null when not user-scoped. |
| 94 |
* |
| 95 |
* @return int|null |
| 96 |
*/ |
| 97 |
public function get_user_id() { |
| 98 |
return $this->user_id; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Full DB row from wp_frm_gated_tokens. |
| 103 |
* |
| 104 |
* Use this to access columns not exposed by dedicated getters (e.g. entry_id, |
| 105 |
* ip_address, created_at). |
| 106 |
* |
| 107 |
* @return object |
| 108 |
*/ |
| 109 |
public function get_row() { |
| 110 |
return $this->row; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Whether this token has passed its expiry timestamp. |
| 115 |
* |
| 116 |
* @return bool |
| 117 |
*/ |
| 118 |
private function is_expired() { |
| 119 |
return null !== $this->expired_at && time() >= $this->expired_at; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* The gated content action post, or null if the post no longer exists. |
| 124 |
* |
| 125 |
* @return WP_Post|null |
| 126 |
*/ |
| 127 |
public function get_action() { |
| 128 |
$post = get_post( $this->action_id ); |
| 129 |
return $post instanceof WP_Post ? $post : null; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Validate this token against a specific content item. |
| 134 |
* |
| 135 |
* Returns false immediately if the token is expired (and clears its cache). |
| 136 |
* When $item->id is empty the item check is skipped — used by types (e.g. |
| 137 |
* frm_pdf) that are identified by shortcode rather than a fixed item ID. |
| 138 |
* Successful item lookups are cached in a transient to avoid re-fetching |
| 139 |
* action settings on subsequent requests. Only the structural item check is |
| 140 |
* cached — the `frm_gated_content_is_valid` filter still fires every time. |
| 141 |
* |
| 142 |
* @param FrmGatedItem $item Content item (type slug + ID). |
| 143 |
* |
| 144 |
* @return bool |
| 145 |
*/ |
| 146 |
public function validate( FrmGatedItem $item ) { |
| 147 |
if ( $this->is_expired() ) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
$is_valid = FrmGatedTokenHelper::action_contains_item( $this->action_id, $item, $this->expired_at ); |
| 152 |
|
| 153 |
/** |
| 154 |
* Filters whether a resolved token grants access to a content item. |
| 155 |
* |
| 156 |
* @since 6.33 |
| 157 |
* |
| 158 |
* @param bool $is_valid Whether the token passes structural validation. |
| 159 |
* @param array $args { |
| 160 |
* |
| 161 |
* @type FrmGatedToken $token The token being validated. |
| 162 |
* @type FrmGatedItem $item Content item being checked. |
| 163 |
* } |
| 164 |
*/ |
| 165 |
return (bool) apply_filters( |
| 166 |
'frm_gated_content_is_valid', |
| 167 |
$is_valid, |
| 168 |
array( |
| 169 |
'token' => $this, |
| 170 |
'item' => $item, |
| 171 |
) |
| 172 |
); |
| 173 |
} |
| 174 |
} |
| 175 |
|