| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gated Content Shortcode Controller |
| 4 |
* |
| 5 |
* Handles the [frm_gated_content] shortcode and all item URL/title rendering. |
| 6 |
* |
| 7 |
* @package Formidable |
| 8 |
* |
| 9 |
* @since 6.33 |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
die( 'You are not allowed to call this page directly.' ); |
| 14 |
} |
| 15 |
|
| 16 |
class FrmGatedContentShortcodeController { |
| 17 |
|
| 18 |
/** |
| 19 |
* Default attributes for the [frm_gated_content] shortcode. |
| 20 |
* |
| 21 |
* - id (required) Action post ID. |
| 22 |
* - item (optional) 0-indexed item position. Omit to render the full item list. |
| 23 |
* - show (optional) 'link' (default) | 'url' | 'access_token'. |
| 24 |
* 'link' — <a> link tag(s). Without item: <ul> of links. With item: single <a>. |
| 25 |
* 'url' — Plain URL string. Without item: <ul> of plain URLs. With item: single URL. |
| 26 |
* 'access_token' — Raw access token string. |
| 27 |
* |
| 28 |
* @return array<string, mixed> |
| 29 |
*/ |
| 30 |
private static function default_shortcode_atts() { |
| 31 |
return array( |
| 32 |
'id' => 0, |
| 33 |
'item' => false, |
| 34 |
'show' => 'link', |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Handle the [frm_gated_content] shortcode. |
| 40 |
* |
| 41 |
* @param array $atts Shortcode attributes. See default_shortcode_atts() for supported keys. |
| 42 |
* |
| 43 |
* @return string Shortcode output, or empty string when no token is available. |
| 44 |
*/ |
| 45 |
public static function shortcode( $atts ) { |
| 46 |
$atts = shortcode_atts( self::default_shortcode_atts(), $atts, 'frm_gated_content' ); |
| 47 |
$action_id = (int) $atts['id']; |
| 48 |
|
| 49 |
if ( ! $action_id ) { |
| 50 |
return ''; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Override or extend [frm_gated_content] shortcode output. |
| 55 |
* |
| 56 |
* Return a non-null string to short-circuit the default rendering. Return null |
| 57 |
* to let the shortcode fall through to its built-in show= handlers. |
| 58 |
* |
| 59 |
* @since 6.33 |
| 60 |
* |
| 61 |
* @param string|null $output Output string, or null to continue default handling. |
| 62 |
* @param array $atts Full shortcode attributes array (id, show, item, …). |
| 63 |
* $atts['id'] is the gated content action post ID. |
| 64 |
*/ |
| 65 |
$custom = apply_filters( 'frm_gated_content_shortcode_custom_output', null, $atts ); |
| 66 |
|
| 67 |
if ( null !== $custom ) { |
| 68 |
return (string) $custom; |
| 69 |
} |
| 70 |
|
| 71 |
// All other show values require the raw token. |
| 72 |
$raw_token = FrmGatedTokenHelper::get_raw_token_for_action( $action_id ); |
| 73 |
|
| 74 |
if ( null === $raw_token ) { |
| 75 |
return ''; |
| 76 |
} |
| 77 |
|
| 78 |
if ( 'access_token' === $atts['show'] ) { |
| 79 |
return esc_html( $raw_token ); |
| 80 |
} |
| 81 |
|
| 82 |
return self::render_items_shortcode_output( $action_id, $raw_token, $atts ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Render the item links or URLs portion of the [frm_gated_content] shortcode. |
| 87 |
* |
| 88 |
* Called after the raw token is confirmed. Loads the action's items and |
| 89 |
* dispatches to the appropriate renderer based on the show= and item= atts. |
| 90 |
* |
| 91 |
* @param int $action_id Gated content action post ID. |
| 92 |
* @param string $raw_token Raw access token. |
| 93 |
* @param array $atts Shortcode attributes (show, item). |
| 94 |
* |
| 95 |
* @return string |
| 96 |
*/ |
| 97 |
private static function render_items_shortcode_output( $action_id, $raw_token, $atts ) { |
| 98 |
$action = get_post( $action_id ); |
| 99 |
|
| 100 |
if ( ! $action ) { |
| 101 |
return ''; |
| 102 |
} |
| 103 |
|
| 104 |
$settings = FrmAppHelper::maybe_json_decode( $action->post_content ); |
| 105 |
$items = is_array( $settings ) && ! empty( $settings['items'] ) ? $settings['items'] : array(); |
| 106 |
$show_url = 'url' === $atts['show']; |
| 107 |
|
| 108 |
if ( false === $atts['item'] ) { |
| 109 |
// No item specified — render all items. |
| 110 |
return $show_url |
| 111 |
? self::render_item_url_list( $items, $raw_token ) |
| 112 |
: self::render_item_list( $items, $raw_token ); |
| 113 |
} |
| 114 |
|
| 115 |
// Single item by 0-based index. |
| 116 |
$idx = (int) $atts['item']; |
| 117 |
|
| 118 |
if ( ! isset( $items[ $idx ] ) || ! is_array( $items[ $idx ] ) ) { |
| 119 |
return ''; |
| 120 |
} |
| 121 |
|
| 122 |
return self::render_shortcode_item( $items[ $idx ], $raw_token, $show_url ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Render a single gated content item as a URL string or anchor link. |
| 127 |
* |
| 128 |
* @param array $raw_item_data Item array from action settings (must have 'id' and 'type' keys). |
| 129 |
* @param string $raw_token Raw access token. |
| 130 |
* @param bool $show_url True to return a plain escaped URL; false to return an <a> tag. |
| 131 |
* |
| 132 |
* @return string Rendered output, or empty string when the item is invalid or has no URL. |
| 133 |
*/ |
| 134 |
private static function render_shortcode_item( $raw_item_data, $raw_token, $show_url ) { |
| 135 |
if ( empty( $raw_item_data['id'] ) || empty( $raw_item_data['type'] ) ) { |
| 136 |
return ''; |
| 137 |
} |
| 138 |
|
| 139 |
$item = FrmGatedItem::make( $raw_item_data ); |
| 140 |
$url = $item->get_url( $raw_token ); |
| 141 |
|
| 142 |
if ( ! $url ) { |
| 143 |
return ''; |
| 144 |
} |
| 145 |
|
| 146 |
if ( $show_url ) { |
| 147 |
return esc_url( $url ); |
| 148 |
} |
| 149 |
|
| 150 |
$label = $item->get_title(); |
| 151 |
|
| 152 |
if ( ! $label ) { |
| 153 |
$label = $url; |
| 154 |
} |
| 155 |
|
| 156 |
return '<a href="' . esc_url( $url ) . '" title="' . esc_attr( $label ) . '">' . esc_html( $label ) . '</a>'; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Render an unordered list of gated access links for all items in an action. |
| 161 |
* |
| 162 |
* @param array $items Array of item arrays from action settings (each with 'id' and 'type' keys). |
| 163 |
* @param string $raw_token Raw access token. |
| 164 |
* |
| 165 |
* @return string HTML <ul> element, or empty string when no items produce a URL. |
| 166 |
*/ |
| 167 |
public static function render_item_list( $items, $raw_token ) { |
| 168 |
if ( ! $items ) { |
| 169 |
return ''; |
| 170 |
} |
| 171 |
|
| 172 |
$list_items = ''; |
| 173 |
|
| 174 |
foreach ( $items as $item ) { |
| 175 |
if ( ! is_array( $item ) || empty( $item['id'] ) || empty( $item['type'] ) ) { |
| 176 |
continue; |
| 177 |
} |
| 178 |
|
| 179 |
$gated_item = FrmGatedItem::make( $item ); |
| 180 |
$url = $gated_item->get_url( $raw_token ); |
| 181 |
|
| 182 |
if ( ! $url ) { |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
$label = $gated_item->get_title(); |
| 187 |
|
| 188 |
if ( ! $label ) { |
| 189 |
$label = $url; |
| 190 |
} |
| 191 |
|
| 192 |
$list_items .= '<li><a href="' . esc_url( $url ) . '" title="' . esc_attr( $label ) . '">' . esc_html( $label ) . '</a></li>'; |
| 193 |
} |
| 194 |
|
| 195 |
if ( ! $list_items ) { |
| 196 |
return ''; |
| 197 |
} |
| 198 |
|
| 199 |
return '<ul class="frm-gated-content-list">' . $list_items . '</ul>'; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Render an unordered list of plain URLs (no link tags) for all items in an action. |
| 204 |
* |
| 205 |
* Used when show="url" is set without an item index. |
| 206 |
* |
| 207 |
* @param array $items Array of item arrays from action settings (each with 'id' and 'type' keys). |
| 208 |
* @param string $raw_token Raw access token. |
| 209 |
* |
| 210 |
* @return string HTML <ul> element of plain-text URLs, or empty string when no items produce a URL. |
| 211 |
*/ |
| 212 |
public static function render_item_url_list( $items, $raw_token ) { |
| 213 |
if ( ! $items ) { |
| 214 |
return ''; |
| 215 |
} |
| 216 |
|
| 217 |
$list_items = ''; |
| 218 |
|
| 219 |
foreach ( $items as $item ) { |
| 220 |
if ( ! is_array( $item ) || empty( $item['id'] ) || empty( $item['type'] ) ) { |
| 221 |
continue; |
| 222 |
} |
| 223 |
|
| 224 |
$gated_item = FrmGatedItem::make( $item ); |
| 225 |
$url = $gated_item->get_url( $raw_token ); |
| 226 |
|
| 227 |
if ( ! $url ) { |
| 228 |
continue; |
| 229 |
} |
| 230 |
|
| 231 |
$list_items .= '<li>' . esc_url( $url ) . '</li>'; |
| 232 |
} |
| 233 |
|
| 234 |
if ( ! $list_items ) { |
| 235 |
return ''; |
| 236 |
} |
| 237 |
|
| 238 |
return '<ul class="frm-gated-content-list">' . $list_items . '</ul>'; |
| 239 |
} |
| 240 |
} |
| 241 |
|