| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gated Content form action settings view |
| 4 |
* |
| 5 |
* @package Formidable |
| 6 |
* |
| 7 |
* @since 6.33 |
| 8 |
* |
| 9 |
* @var object $instance Form action post object. Settings in $instance->post_content. |
| 10 |
* @var array $args Contains `form`, `action_key`, `values`. |
| 11 |
* @var FrmGatedContentAction $this Action class instance. Use get_field_name() / get_field_id(). |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
die( 'You are not allowed to call this page directly.' ); |
| 16 |
} |
| 17 |
|
| 18 |
$frm_gc_items = $instance->post_content['items'] ?? array(); |
| 19 |
$frm_gc_action_id = (int) $instance->ID; |
| 20 |
$frm_gc_field_name_base = $this->get_field_name( 'items' ); |
| 21 |
$frm_gc_types = FrmGatedContentAction::get_types(); |
| 22 |
|
| 23 |
// Unique wrapper ID per action instance — prevents JS collisions when multiple |
| 24 |
// gated content actions exist on the same form. |
| 25 |
$frm_gc_wrapper_id = 'frm_gc_settings_' . $this->number; |
| 26 |
|
| 27 |
// array<string, list<object>> — posts grouped by item type key (e.g. 'page', 'post'). |
| 28 |
$frm_gc_posts = FrmGatedContentAction::get_posts(); |
| 29 |
|
| 30 |
$frm_gc_total_posts = array_sum( array_map( 'count', $frm_gc_posts ) ); |
| 31 |
$frm_gc_use_autocomplete = $frm_gc_total_posts > 50; |
| 32 |
|
| 33 |
if ( $frm_gc_use_autocomplete ) { |
| 34 |
wp_enqueue_script( 'jquery-ui-autocomplete' ); |
| 35 |
|
| 36 |
// Pre-encode one autocomplete source per type for reuse in all item rows. |
| 37 |
$frm_gc_posts_source = array(); |
| 38 |
|
| 39 |
foreach ( $frm_gc_posts as $frm_gc_type_key => $frm_gc_type_posts ) { |
| 40 |
$frm_gc_posts_source[ $frm_gc_type_key ] = FrmGatedContentAction::get_posts_autocomplete_source( $frm_gc_type_posts ); |
| 41 |
} |
| 42 |
unset( $frm_gc_type_key, $frm_gc_type_posts ); |
| 43 |
} |
| 44 |
?> |
| 45 |
|
| 46 |
<div |
| 47 |
class="frm_gated_content_settings frm-mt-sm" |
| 48 |
id="<?php echo esc_attr( $frm_gc_wrapper_id ); ?>" |
| 49 |
data-item-count="<?php echo count( $frm_gc_items ); ?>" |
| 50 |
> |
| 51 |
|
| 52 |
<?php /* ── Section: Gated content items ─────────────────────────────── */ ?> |
| 53 |
<div class="frm_form_field frm_gc_items_section"> |
| 54 |
<h3 class="frm-mb-sm"><?php esc_html_e( 'Gated Content Items', 'formidable' ); ?></h3> |
| 55 |
|
| 56 |
<ul class="frm_gc_items_list"> |
| 57 |
<?php foreach ( $frm_gc_items as $frm_gc_idx => $frm_gc_item ) : ?> |
| 58 |
<?php |
| 59 |
$frm_gc_item_type = $frm_gc_item['type'] ?? 'page'; |
| 60 |
$frm_gc_item_id = isset( $frm_gc_item['id'] ) ? (int) $frm_gc_item['id'] : 0; |
| 61 |
$frm_gc_item_base = $frm_gc_field_name_base . '[' . $frm_gc_idx . ']'; |
| 62 |
$frm_gc_type_sel_id = $frm_gc_wrapper_id . '_type_' . $frm_gc_idx; |
| 63 |
$is_template = false; |
| 64 |
include __DIR__ . '/_gated_content_item_row.php'; |
| 65 |
?> |
| 66 |
<?php endforeach; ?> |
| 67 |
</ul><!-- .frm_gc_items_list --> |
| 68 |
|
| 69 |
<?php |
| 70 |
// Template row — hidden, cloned by JS when "Add Item" is clicked. |
| 71 |
// JS assigns id, name, and for attributes after cloning using the item index counter. |
| 72 |
?> |
| 73 |
<template class="frm_gc_item_template"> |
| 74 |
<?php |
| 75 |
$frm_gc_item_type = 'page'; |
| 76 |
$frm_gc_item_id = 0; |
| 77 |
$frm_gc_item_base = ''; |
| 78 |
$frm_gc_type_sel_id = ''; |
| 79 |
$frm_gc_idx = 0; |
| 80 |
$frm_gc_item = array(); |
| 81 |
$is_template = true; |
| 82 |
include __DIR__ . '/_gated_content_item_row.php'; |
| 83 |
?> |
| 84 |
</template> |
| 85 |
|
| 86 |
<button |
| 87 |
type="button" |
| 88 |
class="frm_gc_add_item button-secondary frm-button-sm" |
| 89 |
data-field-name-base="<?php echo esc_attr( $frm_gc_field_name_base ); ?>" |
| 90 |
> |
| 91 |
+ <?php esc_html_e( 'Add Item', 'formidable' ); ?> |
| 92 |
</button> |
| 93 |
</div><!-- .frm_gc_items_section --> |
| 94 |
|
| 95 |
<?php /* ── Section: Shortcode reference ─────────────────────────────── */ ?> |
| 96 |
<?php include __DIR__ . '/_gated_content_shortcodes.php'; ?> |
| 97 |
|
| 98 |
</div><!-- .frm_gated_content_settings --> |
| 99 |
|
| 100 |
<?php |
| 101 |
unset( $frm_gc_items, $frm_gc_action_id, $frm_gc_field_name_base, $frm_gc_types, $frm_gc_wrapper_id, $frm_gc_posts, $frm_gc_total_posts, $frm_gc_item, $frm_gc_idx, $frm_gc_item_type, $frm_gc_item_id, $frm_gc_item_base, $frm_gc_type_sel_id, $frm_gc_use_autocomplete, $frm_gc_posts_source ); |
| 102 |
|