Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/king-addons/includes/RegisterAssets.php
+++ b/king-addons/includes/RegisterAssets.php
@@ -173,14 +173,17 @@
}
}
- wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-grid-grid', 'KingAddonsGridData', [
+ $grid_data = [
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('king_addons_grid_nonce'),
'viewCart' => esc_html__('View Cart', 'king-addons'),
'addedToCartText' => esc_html__('was added to cart', 'king-addons'),
'comparePageURL' => get_permalink(get_option('king_addons_compare_page')),
'wishlistPageURL' => get_permalink(get_option('king_addons_wishlist_page')),
- ]);
+ ];
+ foreach (['grid', 'woocommerce', 'media'] as $grid_script) {
+ wp_localize_script(KING_ADDONS_ASSETS_UNIQUE_KEY . '-grid-' . $grid_script, 'KingAddonsGridData', $grid_data);
+ }
}
/**
--- a/king-addons/includes/admin/layouts/wishlist-analytics.php
+++ b/king-addons/includes/admin/layouts/wishlist-analytics.php
@@ -54,7 +54,7 @@
esc_html__('Conversion Rate', 'king-addons'),
esc_html__('Price', 'king-addons'),
esc_html__('Stock Status', 'king-addons'),
- ]);
+ ], ',', '"', '\');
foreach ($stats as $row) {
$product = function_exists('wc_get_product') ? wc_get_product($row['product_id']) : null;
@@ -68,7 +68,7 @@
$conv_rate . '%',
$product ? $product->get_price() : '',
$product ? ($product->is_in_stock() ? 'In Stock' : 'Out of Stock') : '',
- ]);
+ ], ',', '"', '\');
}
fclose($output);
--- a/king-addons/includes/extensions/Activity_Log/Activity_Log.php
+++ b/king-addons/includes/extensions/Activity_Log/Activity_Log.php
@@ -546,7 +546,7 @@
'Object',
'Source',
'Message',
- ]);
+ ], ',', '"', '\');
foreach ($logs as $log) {
$object = trim($log->object_type . ' ' . $log->object_title);
@@ -562,7 +562,7 @@
$log->message,
];
$row = array_map([$this, 'escape_csv_value'], $row);
- fputcsv($output, $row);
+ fputcsv($output, $row, ',', '"', '\');
}
fclose($output);
--- a/king-addons/includes/extensions/Data_Table_Builder/Data_Table_Builder.php
+++ b/king-addons/includes/extensions/Data_Table_Builder/Data_Table_Builder.php
@@ -362,7 +362,7 @@
$header[] = $this->escape_csv_value($column['label'] ?? '');
}
if (!empty($header)) {
- fputcsv($output, $header);
+ fputcsv($output, $header, ',', '"', '\');
}
foreach ($rows as $row) {
@@ -371,7 +371,7 @@
$value = is_array($cell) ? (string) ($cell['value'] ?? '') : (string) $cell;
$line[] = $this->escape_csv_value($value);
}
- fputcsv($output, $line);
+ fputcsv($output, $line, ',', '"', '\');
}
fclose($output);
@@ -401,7 +401,7 @@
}
$rows = [];
- while (($row = fgetcsv($handle)) !== false) {
+ while (($row = fgetcsv($handle, 0, ',', '"', '\')) !== false) {
$rows[] = $row;
}
fclose($handle);
--- a/king-addons/includes/extensions/Smart_Links/Smart_Links.php
+++ b/king-addons/includes/extensions/Smart_Links/Smart_Links.php
@@ -490,7 +490,7 @@
$this->redirect_with_message('import-export', 'error_import');
}
- $header = fgetcsv($handle);
+ $header = fgetcsv($handle, 0, ',', '"', '\');
if (!$header) {
fclose($handle);
$this->redirect_with_message('import-export', 'error_import');
@@ -501,7 +501,7 @@
$settings = Smart_Links_Settings::get_settings();
$imported = 0;
- while (($row = fgetcsv($handle)) !== false) {
+ while (($row = fgetcsv($handle, 0, ',', '"', '\')) !== false) {
$destination = $row[$map['destination_url'] ?? -1] ?? '';
$destination = $service->sanitize_destination_url((string) $destination);
if ($destination === '') {
@@ -574,7 +574,7 @@
'created',
'clicks',
'unique_clicks',
- ]);
+ ], ',', '"', '\');
$service = new Smart_Links_Service();
@@ -594,7 +594,7 @@
$post->post_date,
(int) get_post_meta($post->ID, self::META_CLICKS, true),
(int) get_post_meta($post->ID, self::META_UNIQUE_CLICKS, true),
- ]);
+ ], ',', '"', '\');
}
fclose($output);
--- a/king-addons/includes/extensions/Templates/Templates.php
+++ b/king-addons/includes/extensions/Templates/Templates.php
@@ -596,7 +596,7 @@
data-template-key="<?php echo esc_attr($attr_key); ?>"
data-template-plan="<?php echo esc_attr($template['plan']); ?>">
<img class="kng-addons-template-thumbnail" loading="lazy"
- src="<?php echo esc_url("https://thumbnails.kingaddons.com/$attr_key.png?v=4"); ?>"
+ src="<?php echo esc_url("https://thumbnails.kingaddons.com/$attr_key.webp?v=4"); ?>"
alt="<?php echo esc_attr($template['title']); ?>">
<h3><?php echo esc_html($template['title']); ?></h3>
<div class="template-plan template-plan-<?php echo esc_html($template['plan']); ?>"><?php echo esc_html($template['plan']); ?></div>
@@ -837,7 +837,11 @@
$this->replace_image_data($value, $old_url, $new_url, $old_id, $new_id);
} elseif (is_string($value)) {
// Direct URL string replacement
- if ($value === $old_url) {
+ // A shared URL can belong to several different source attachment IDs.
+ // Keep an unmatched media object's URL for its own ID replacement pass.
+ $is_unmatched_media_url = $key === 'url' && isset($array['id']) &&
+ $array['id'] !== $old_id;
+ if ($value === $old_url && !$is_unmatched_media_url) {
$array[$key] = $new_url;
}
@@ -1399,7 +1403,7 @@
data-template-key="<?php echo esc_attr($attr_key); ?>"
data-template-plan="<?php echo esc_attr($template['plan']); ?>">
<img class="kng-addons-template-thumbnail" loading="lazy"
- src="<?php echo esc_url("https://thumbnails.kingaddons.com/$attr_key.png?v=4"); ?>"
+ src="<?php echo esc_url("https://thumbnails.kingaddons.com/$attr_key.webp?v=4"); ?>"
alt="<?php echo esc_attr($template['title']); ?>">
<h3><?php echo esc_html($template['title']); ?></h3>
<div class="template-plan template-plan-<?php echo esc_html($template['plan']); ?>"><?php echo esc_html($template['plan']); ?></div>
@@ -1418,7 +1422,7 @@
$section_key = $section['section_key'];
$plan = $section['plan'];
$title = $section['title'];
- $thumb_url = "https://thumbnails.kingaddons.com/sections/$plan/$section_key.png";
+ $thumb_url = "https://thumbnails.kingaddons.com/sections/$plan/$section_key.webp";
?>
<div class="section-item"
data-section-key="<?php echo esc_attr($section_key); ?>"
--- a/king-addons/includes/features/Conditional_Display/Conditional_Display.php
+++ b/king-addons/includes/features/Conditional_Display/Conditional_Display.php
@@ -44,6 +44,12 @@
*/
public function __construct()
{
+ require_once KING_ADDONS_PATH . 'includes/helpers/Conditional_Content_Cache.php';
+ require_once KING_ADDONS_PATH . 'includes/helpers/Display_Access.php';
+ require_once KING_ADDONS_PATH . 'includes/helpers/Display_Output_Guard.php';
+ Display_Access::register('cd');
+ Conditional_Content_Cache::register('cd_enable');
+
// Editor assets.
add_action('elementor/preview/enqueue_scripts', [$this, 'enqueue_preview_script'], 1);
add_action('elementor/preview/enqueue_styles', [$this, 'enqueue_styles'], 1);
@@ -109,11 +115,12 @@
*/
public function register_controls(Element_Base $element, $args): void
{
+ Display_Access::intro($element);
$element->start_controls_section(
'king_addons_cd_section',
[
- 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Conditional Display', 'king-addons'),
- 'tab' => Controls_Manager::TAB_ADVANCED,
+ 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Visibility rules', 'king-addons'),
+ 'tab' => Display_Access::TAB,
]
);
@@ -130,6 +137,8 @@
]
);
+ Display_Access::rule_setup_control($element);
+
// Conditions relation (AND/OR).
$element->add_control(
'cd_relation',
@@ -169,7 +178,7 @@
'options' => [
'hide' => esc_html__('Hide element', 'king-addons'),
'message' => esc_html__('Show message', 'king-addons'),
- 'template' => sprintf(__('Render template %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
+ 'template' => Display_Access_Plan::pro_label(__('Render template', 'king-addons')),
],
'default' => 'hide',
'condition' => [
@@ -299,6 +308,8 @@
]
);
+ Display_Access::upgrade_control($element, 'cd', ['cd_enable' => 'yes', 'cd_rules_mode!' => 'rules'], '_classic');
+
// Browser condition (Pro).
$element->add_control(
'cd_cond_browser_enable',
@@ -671,6 +682,14 @@
]
);
+ foreach ($element->get_controls() as $key => $control) {
+ if (strpos($key, 'cd_cond_') === 0 || strpos($key, 'cd_heading_') === 0 || $key === 'cd_relation') {
+ $element->update_control($key, ['condition' => array_merge($control['condition'] ?? [], ['cd_rules_mode!' => 'rules'])]);
+ }
+ }
+ Display_Access::rules_controls($element);
+ Display_Access::editor_field_layout($element, 'king_addons_cd_section');
+ Display_Access::plan_controls($element, 'king_addons_cd_section');
$element->end_controls_section();
}
@@ -787,7 +806,17 @@
return false;
}
- $settings = $element->get_settings_for_display();
+ $settings = Display_Access_Plan::runtime_settings($element->get_settings_for_display(), 'cd');
+
+ if (!$this->is_editor_or_preview() && $settings['_ka_plan_blocked']) { return false; }
+
+ if (!$this->is_editor_or_preview() && ($settings['cd_hide_empty'] ?? '') === 'yes') {
+ $children = $element->get_children();
+ if ($children && !array_filter($children, static function ($child) {
+ $type = $child->get_type();
+ return apply_filters('elementor/frontend/' . $type . '/should_render', true, $child);
+ })) { return false; }
+ }
if (empty($settings['cd_enable']) || 'yes' !== $settings['cd_enable']) {
return true;
@@ -829,7 +858,12 @@
*/
public function before_render_element(Element_Base $element): void
{
- $settings = $element->get_settings_for_display();
+ $settings = Display_Access_Plan::runtime_settings($element->get_settings_for_display(), 'cd');
+
+ if (!$this->is_editor_or_preview() && $settings['_ka_plan_blocked']) {
+ $this->suppress_element_render($element);
+ return;
+ }
if (empty($settings['cd_enable']) || 'yes' !== $settings['cd_enable']) {
return;
@@ -841,7 +875,9 @@
$element->add_render_attribute('_wrapper', 'class', 'king-addons-cd-preview');
// Add data attributes for condition info.
- $active_conditions = $this->get_active_conditions_list($settings);
+ $active_conditions = ($settings['cd_rules_mode'] ?? 'legacy') === 'rules'
+ ? array_map(static fn($row) => ($row['pass'] ? '✓ ' : '× ') . $row['label'] . ': ' . $row['detail'], Display_Rules::evaluate($settings['cd_rules'] ?? [])['checks'])
+ : $this->get_active_conditions_list($settings);
if (!empty($active_conditions)) {
$element->add_render_attribute('_wrapper', 'data-king-cd-conditions', esc_attr(implode(', ', $active_conditions)));
}
@@ -856,6 +892,13 @@
// If conditions pass, do nothing.
if ($result) {
+ if (($settings['cd_reveal'] ?? '') === 'yes') {
+ wp_enqueue_style('king-display-access', KING_ADDONS_URL . 'includes/helpers/display-access.css', [], Display_Access::asset_version('includes/helpers/display-access.css'));
+ wp_enqueue_script('king-display-reveal', KING_ADDONS_URL . 'includes/helpers/display-reveal.js', ['jquery'], Display_Access::asset_version('includes/helpers/display-reveal.js'), true);
+ $element->add_render_attribute('_wrapper', 'data-ka-display-reveal', $element->get_id());
+ $element->add_render_attribute('_wrapper', 'class', 'ka-display-await-click');
+ echo '<div class="ka-display-reveal-trigger"><button type="button">' . esc_html($settings['cd_reveal_label'] ?? __('Show content', 'king-addons')) . '</button></div>';
+ }
return;
}
@@ -929,6 +972,10 @@
*/
private function evaluate_conditions(array $settings): bool
{
+ if (($settings['cd_rules_mode'] ?? 'legacy') === 'rules') {
+ if (!$this->can_use_pro()) { return true; }
+ return Display_Rules::evaluate($settings['cd_rules'] ?? [])['pass'];
+ }
$relation = $settings['cd_relation'] ?? 'all';
$results = [];
@@ -1094,7 +1141,7 @@
}
if (!$this->can_use_pro()) {
- return true; // Pro feature not available, skip condition.
+ return null; // Pro feature not available, skip condition.
}
if (empty($settings['cd_cond_browser_types']) || !is_array($settings['cd_cond_browser_types'])) {
@@ -1150,6 +1197,7 @@
*/
private function check_date_time(array $settings): ?bool
{
+ if (!$this->can_use_pro()) { return null; }
if (empty($settings['cd_cond_date_enable']) || 'yes' !== $settings['cd_cond_date_enable']) {
return null;
}
@@ -1192,6 +1240,7 @@
*/
private function check_time_of_day(array $settings): ?bool
{
+ if (!$this->can_use_pro()) { return null; }
if (empty($settings['cd_cond_time_enable']) || 'yes' !== $settings['cd_cond_time_enable']) {
return null;
}
@@ -1205,6 +1254,10 @@
return null;
}
+ if ('' !== $time_from && '' !== $time_to && $time_from > $time_to) {
+ return $current_time >= $time_from || $current_time <= $time_to;
+ }
+
if (!empty($time_from) && $current_time < $time_from) {
return false;
}
@@ -1225,6 +1278,7 @@
*/
private function check_url(array $settings): ?bool
{
+ if (!$this->can_use_pro()) { return null; }
if (empty($settings['cd_cond_url_enable']) || 'yes' !== $settings['cd_cond_url_enable']) {
return null;
}
@@ -1273,7 +1327,7 @@
}
if (!$this->can_use_pro()) {
- return true;
+ return null;
}
if (empty($_SERVER['HTTP_REFERER'])) {
@@ -1303,7 +1357,7 @@
}
if (!$this->can_use_pro()) {
- return true;
+ return null;
}
if (!class_exists('WooCommerce') || !function_exists('WC')) {
@@ -1344,7 +1398,8 @@
}
// Check previously bought products.
- if (!empty($settings['cd_cond_woo_bought_products']) && is_user_logged_in()) {
+ if (!empty($settings['cd_cond_woo_bought_products'])) {
+ if (!is_user_logged_in()) { return false; }
$ids = $this->sanitize_ids($settings['cd_cond_woo_bought_products']);
if (!empty($ids)) {
$user = wp_get_current_user();
@@ -1379,7 +1434,7 @@
}
if (!$this->can_use_pro()) {
- return true;
+ return null;
}
if (empty($settings['cd_cond_cookie_name'])) {
@@ -1414,7 +1469,7 @@
}
if (!$this->can_use_pro()) {
- return true;
+ return null;
}
if (empty($settings['cd_cond_lang_codes']) || !is_array($settings['cd_cond_lang_codes'])) {
@@ -1484,9 +1539,7 @@
*/
private function suppress_element_render(Element_Base $element): void
{
- // Add hidden class for CSS hiding as fallback.
- $element->add_render_attribute('_wrapper', 'class', 'king-addons-cd-hidden');
- $element->add_render_attribute('_wrapper', 'style', 'display:none !important;');
+ Display_Output_Guard::suppress($element);
}
/**
@@ -1522,11 +1575,7 @@
*/
private function can_use_pro(): bool
{
- if (!function_exists('king_addons_freemius')) {
- return false;
- }
-
- return king_addons_freemius()->can_use_premium_code__premium_only();
+ return Display_Access_Plan::has_pro();
}
}
--- a/king-addons/includes/features/Faceted_Filters/Faceted_Filters.php
+++ b/king-addons/includes/features/Faceted_Filters/Faceted_Filters.php
@@ -395,24 +395,40 @@
{
check_ajax_referer(self::AJAX_ACTION, 'nonce');
- $payload = isset($_POST['payload']) ? wp_unslash((string) $_POST['payload']) : '';
+ $payload = isset($_POST['payload']) && is_string($_POST['payload']) ? wp_unslash($_POST['payload']) : '';
$data = json_decode($payload, true);
if (!is_array($data)) {
wp_send_json_error(['message' => esc_html__('Invalid payload.', 'king-addons')]);
}
- if (empty($data['query_id']) || empty($data['widget_id']) || empty($data['post_id'])) {
+ if (empty($data['query_id']) || !is_string($data['query_id'])
+ || empty($data['widget_id']) || !is_string($data['widget_id'])
+ || empty($data['post_id']) || !is_scalar($data['post_id'])) {
wp_send_json_error(['message' => esc_html__('Required data is missing.', 'king-addons')]);
}
- $this->ajax_post_id = (int) $data['post_id'];
+ $this->ajax_post_id = absint($data['post_id']);
+ $document_post = get_post($this->ajax_post_id);
+ $public_document = $document_post && 'publish' === $document_post->post_status && '' === $document_post->post_password;
+ if (!$document_post || (!$public_document && !current_user_can('edit_post', $this->ajax_post_id))) {
+ wp_send_json_error(['message' => esc_html__('This filter configuration is not available.', 'king-addons')], 403);
+ }
+ $query_id = sanitize_title($data['query_id']);
+ $widget_data = $this->get_widget_data($this->ajax_post_id, $data['widget_id']);
+ $widget_settings = $widget_data['settings'] ?? [];
+ // A public nonce must not permit rendering arbitrary saved widgets or
+ // grids whose author has not enabled this particular filter query.
+ if (!$widget_data || !in_array($widget_data['widgetType'] ?? '', $this->supported_widgets, true)
+ || ($widget_settings['ka_filters_enable'] ?? '') !== 'yes'
+ || '' === $query_id || sanitize_title((string) ($widget_settings['ka_filters_query_id'] ?? '')) !== $query_id) {
+ wp_send_json_error(['message' => esc_html__('This filter configuration is not available.', 'king-addons')], 403);
+ }
$archive = is_array($data['archive'] ?? null) ? $data['archive'] : [];
$this->ajax_archive = [
'taxonomy' => isset($archive['taxonomy']) ? sanitize_key((string) $archive['taxonomy']) : '',
'term_id' => isset($archive['term_id']) ? (int) $archive['term_id'] : 0,
];
- $query_id = sanitize_title((string) $data['query_id']);
$variable_price = $this->sanitize_variable_price_mode($data['variable_price'] ?? '');
if ('' === $variable_price) {
$variable_price = $this->collect_variable_price_mode($this->ajax_post_id, $query_id);
@@ -428,12 +444,6 @@
}
}
- $widget_data = $this->get_widget_data((int) $data['post_id'], (string) $data['widget_id']);
-
- if (null === $widget_data) {
- wp_send_json_error(['message' => esc_html__('Widget not found.', 'king-addons')]);
- }
-
$filter_state = [
'page' => isset($data['page']) ? (int) $data['page'] : 1,
'filters' => is_array($data['filters'] ?? null) ? $data['filters'] : [],
@@ -449,7 +459,13 @@
$meta = $this->prepare_query_meta($query_args, $query_id);
$counts = $this->build_counts($query_builder, $grid_settings, $query_args);
- $html = $this->render_widget($widget_data, $query_args);
+ $documents = Plugin::$instance->documents;
+ $documents->switch_to_document($documents->get($this->ajax_post_id));
+ try {
+ $html = $this->render_widget($widget_data, $query_args);
+ } finally {
+ $documents->restore_document();
+ }
$response = [
'html' => $html,
--- a/king-addons/includes/features/Protected_Content/Protected_Content.php
+++ b/king-addons/includes/features/Protected_Content/Protected_Content.php
@@ -37,11 +37,21 @@
*/
private static ?array $roles_cache = null;
+ /** Locker currently rendering its configured form shortcode. */
+ private ?array $locker_render_context = null;
+
/**
* Constructor.
*/
public function __construct()
{
+ require_once KING_ADDONS_PATH . 'includes/helpers/Conditional_Content_Cache.php';
+ require_once KING_ADDONS_PATH . 'includes/helpers/Display_Access.php';
+ require_once KING_ADDONS_PATH . 'includes/helpers/Display_Output_Guard.php';
+ require_once KING_ADDONS_PATH . 'includes/helpers/Display_Access_Token.php';
+ Display_Access::register('pc');
+ Conditional_Content_Cache::register('protected_content_enable');
+
// Styles and scripts.
add_action('elementor/preview/enqueue_styles', [$this, 'enqueue_styles'], 1);
add_action('elementor/frontend/after_enqueue_styles', [$this, 'enqueue_styles'], 1);
@@ -53,6 +63,10 @@
add_action('elementor/element/container/section_layout/after_section_end', [$this, 'register_controls'], 10, 2);
add_action('elementor/element/column/section_advanced/after_section_end', [$this, 'register_controls'], 10, 2);
+ foreach (['widget', 'container', 'section', 'column'] as $kind) {
+ add_filter('elementor/frontend/' . $kind . '/should_render', [$this, 'has_visible_output'], 15, 2);
+ }
+
// Runtime check.
add_action('elementor/frontend/widget/before_render', [$this, 'before_render_element']);
add_action('elementor/frontend/section/before_render', [$this, 'before_render_element']);
@@ -60,7 +74,10 @@
add_action('elementor/frontend/column/before_render', [$this, 'before_render_element']);
// Locker helper.
- add_action('king_addons_locker_unlock', [$this, 'set_locker_cookie']);
+ add_action('king_addons_locker_unlock', [$this, 'set_locker_cookie'], 10, 2);
+ add_action('king_addons/form_builder/submitted', [$this, 'after_king_form'], 10, 4);
+ add_filter('wpcf7_form_hidden_fields', [$this, 'cf7_form_fields']);
+ add_action('wpcf7_mail_sent', [$this, 'after_cf7_form']);
// Password AJAX handler.
add_action('wp_ajax_king_addons_verify_password', [$this, 'ajax_verify_password']);
@@ -112,11 +129,13 @@
return;
}
+ wp_enqueue_script('king-display-inline', KING_ADDONS_URL . 'includes/features/Protected_Content/inline-content.js', ['jquery'], Display_Access::asset_version('includes/features/Protected_Content/inline-content.js'), true);
+
wp_enqueue_script(
KING_ADDONS_ASSETS_UNIQUE_KEY . '-protected-content-password',
KING_ADDONS_URL . 'includes/features/Protected_Content/password-form.js',
- ['jquery'],
- KING_ADDONS_VERSION,
+ ['jquery', 'king-display-inline'],
+ Display_Access::asset_version('includes/features/Protected_Content/password-form.js'),
true
);
@@ -145,11 +164,12 @@
*/
public function register_controls(Element_Base $element, $args): void
{
+ Display_Access::intro($element);
$element->start_controls_section(
'king_addons_protected_content_section',
[
- 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Protection', 'king-addons'),
- 'tab' => Controls_Manager::TAB_ADVANCED,
+ 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Content access', 'king-addons'),
+ 'tab' => Display_Access::TAB,
]
);
@@ -187,6 +207,9 @@
]
);
+ $element->add_control('ka_access_more', ['label' => esc_html__('More options', 'king-addons'), 'type' => Controls_Manager::SWITCHER, 'description' => esc_html__('Optional settings. Saved values still apply.', 'king-addons'), 'condition' => ['protected_content_enable' => 'yes']]);
+ $element->add_control('protected_content_shared_hint', ['type' => Controls_Manager::RAW_HTML, 'raw' => '<p>' . esc_html__('This block uses a shared password. Open More options to edit its key and password.', 'king-addons') . '</p>', 'condition' => ['protected_content_enable' => 'yes', 'protected_content_mode' => 'password', 'protected_content_password_type' => 'global']]);
+
// =============================================
// FALLBACK SETTINGS
// =============================================
@@ -398,6 +421,14 @@
]
);
+ $element->add_control('protected_content_password_roles', [
+ 'label' => esc_html__('Let these roles skip the password', 'king-addons'),
+ 'type' => Controls_Manager::SELECT2, 'multiple' => true,
+ 'options' => $this->get_wp_roles_for_control(),
+ 'description' => esc_html__('Optional. Everyone else can still enter the password.', 'king-addons'),
+ 'condition' => ['protected_content_enable' => 'yes', 'protected_content_mode' => 'password'],
+ ]);
+
$element->add_control(
'protected_content_password_cookie_days',
[
@@ -414,6 +445,10 @@
);
// =============================================
+ // Password form appearance follows the access settings.
+ $element->add_control('protected_content_password_intro', ['label' => esc_html__('Message above the password form', 'king-addons'), 'type' => Controls_Manager::TEXTAREA, 'rows' => 2, 'condition' => ['protected_content_enable' => 'yes', 'protected_content_mode' => 'password']]);
+ $element->add_control('protected_content_password_button', ['label' => esc_html__('Button text', 'king-addons'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Unlock', 'king-addons'), 'condition' => ['protected_content_enable' => 'yes', 'protected_content_mode' => 'password']]);
+
// CONDITIONS MODE
// =============================================
@@ -666,7 +701,7 @@
'protected_content_locker_token',
[
'label' => esc_html__('Locker Token', 'king-addons'),
- 'description' => esc_html__('Unique identifier for this locked content', 'king-addons'),
+ 'description' => esc_html__('Optional shared identifier. Leave blank to create a separate locker for this element.', 'king-addons'),
'type' => Controls_Manager::TEXT,
'placeholder' => 'newsletter-unlock',
'condition' => [
@@ -680,7 +715,7 @@
'protected_content_locker_form',
[
'label' => esc_html__('Form Shortcode', 'king-addons'),
- 'description' => esc_html__('Form to display for unlocking content', 'king-addons'),
+ 'description' => esc_html__('Used when no form template is selected. King Addons forms require the Submissions action.', 'king-addons'),
'type' => Controls_Manager::TEXT,
'placeholder' => '[contact-form-7 id="123"]',
'condition' => [
@@ -706,6 +741,29 @@
]
);
+ $element->add_control(
+ 'protected_content_locker_template',
+ [
+ 'label' => esc_html__('Choose a form template', 'king-addons'),
+ 'type' => Controls_Manager::SELECT2,
+ 'options' => [0 => esc_html__('None — use shortcode', 'king-addons')] + $this->get_elementor_templates(),
+ 'default' => '0',
+ 'label_block' => true,
+ 'description' => esc_html__('Use Form Builder with Submissions enabled. For a shortcode, clear this template and open More options.', 'king-addons'),
+ 'condition' => [
+ 'protected_content_enable' => 'yes',
+ 'protected_content_mode' => 'locker',
+ 'protected_content_locker_method' => 'form_submit',
+ ],
+ ]
+ );
+
+ $element->add_control('ka_access_check', ['type' => Controls_Manager::BUTTON, 'text' => esc_html__('Check form setup', 'king-addons'), 'event' => 'king:display:checkForm', 'condition' => ['protected_content_enable' => 'yes', 'protected_content_mode' => 'locker', 'protected_content_locker_method' => 'form_submit']]);
+ $element->add_control('ka_access_form_report', ['type' => Controls_Manager::RAW_HTML, 'raw' => '<div class="ka-access-form-report" data-state="idle">' . esc_html__('Check the selected form before publishing.', 'king-addons') . '</div>', 'condition' => ['protected_content_enable' => 'yes', 'protected_content_mode' => 'locker', 'protected_content_locker_method' => 'form_submit']]);
+
+ Display_Access::upgrade_control($element, 'pc');
+ Display_Access::editor_field_layout($element, 'king_addons_protected_content_section');
+ Display_Access::plan_controls($element, 'king_addons_protected_content_section');
$element->end_controls_section();
}
@@ -718,7 +776,18 @@
*/
public function before_render_element(Element_Base $element): void
{
- $settings = $element->get_settings_for_display();
+ if ($this->locker_render_context && $element->get_name() === 'king-addons-form-builder') {
+ $element->add_render_attribute('_wrapper', 'data-king-locker-context', $this->form_context(
+ 'king', (int) get_the_ID(), $element->get_id()
+ ));
+ }
+ $settings = Display_Access_Plan::runtime_settings($element->get_settings_for_display(), 'pc');
+ $settings = $this->normalize_locker_settings($settings, $element->get_id());
+
+ if (!$this->is_editor_or_preview() && $settings['_ka_plan_blocked']) {
+ $this->suppress_element_render($element);
+ return;
+ }
if (empty($settings['protected_content_enable']) || 'yes' !== $settings['protected_content_enable']) {
return;
@@ -753,6 +822,14 @@
return;
}
+ if ('locker' === $mode && $this->can_use_pro()
+ && ($settings['protected_content_locker_method'] ?? 'form_submit') === 'form_submit'
+ && (!empty($settings['protected_content_locker_form']) || !empty($settings['protected_content_locker_template']))) {
+ $this->render_locker_form($settings, $element);
+ $this->suppress_element_render($element);
+ return;
+ }
+
$fallback = $settings['protected_content_fallback_type'] ?? 'none';
switch ($fallback) {
@@ -790,6 +867,18 @@
}
}
+ public function has_visible_output(bool $render, Element_Base $element): bool
+ {
+ if (!$render || $this->is_editor_or_preview()) { return $render; }
+ $s = $this->normalize_locker_settings(Display_Access_Plan::runtime_settings($element->get_settings_for_display(), 'pc'), $element->get_id());
+ if ($s['_ka_plan_blocked']) { return false; }
+ if (($s['protected_content_enable'] ?? '') !== 'yes') { return true; }
+ $access = $this->evaluate_access($s, $element);
+ if (($s['protected_content_invert'] ?? '') === 'yes') { $access = !$access; }
+ return $access || in_array($s['protected_content_mode'] ?? '', ['password', 'locker'], true)
+ || ($s['protected_content_fallback_type'] ?? 'none') !== 'none';
+ }
+
/**
* Evaluate access.
*
@@ -807,6 +896,8 @@
return $this->check_role_access($settings);
case 'password':
+ if (!empty($settings['protected_content_password_roles']) && is_user_logged_in()
+ && array_intersect((array)$settings['protected_content_password_roles'], wp_get_current_user()->roles)) { return true; }
return $this->check_password_access($settings, $element);
case 'conditions':
@@ -866,20 +957,19 @@
}
$scope = $settings['protected_content_password_type'] ?? 'element';
-
- if ('element' === $scope) {
- $cookie_name = 'king_protect_' . $element->get_id();
- return !empty($_COOKIE[$cookie_name]) && '1' === $_COOKIE[$cookie_name];
- }
-
- // Global scope.
$key = $settings['protected_content_password_key'] ?? '';
- if (empty($key)) {
- return false;
- }
+ if ($scope === 'global' && empty($key)) { return false; }
+ $cookie_name = $scope === 'global' ? 'king_protect_global_' . sanitize_key($key) : 'king_protect_' . $element->get_id();
+ $context = $this->password_context($settings, $element->get_id(), $this->get_current_elementor_document_id());
+ return Display_Access_Token::valid($_COOKIE[$cookie_name] ?? null, $context);
+ }
- $cookie_name = 'king_protect_global_' . sanitize_key($key);
- return !empty($_COOKIE[$cookie_name]) && '1' === $_COOKIE[$cookie_name];
+ private function password_context(array $settings, string $element_id, int $document): string
+ {
+ if (($settings['protected_content_password_type'] ?? 'element') === 'global') {
+ return wp_json_encode(['password-global', sanitize_key($settings['protected_content_password_key'] ?? ''), $settings['protected_content_password_global_password'] ?? '']);
+ }
+ return wp_json_encode(['password-element', $document, $element_id, $settings['protected_content_password'] ?? '']);
}
/**
@@ -969,7 +1059,8 @@
}
// Customer bought products.
- if (!empty($settings['protected_content_cond_woo_customer_bought_ids']) && is_user_logged_in()) {
+ if (!empty($settings['protected_content_cond_woo_customer_bought_ids'])) {
+ if (!is_user_logged_in()) { return false; }
$ids = $this->sanitize_ids($settings['protected_content_cond_woo_customer_bought_ids']);
if (!empty($ids)) {
$user = wp_get_current_user();
@@ -1005,7 +1096,126 @@
}
$cookie_name = 'king_locker_' . sanitize_key($token);
- return !empty($_COOKIE[$cookie_name]) && '1' === $_COOKIE[$cookie_name];
+ return Display_Access_Token::valid($_COOKIE[$cookie_name] ?? null, 'locker|' . $token);
+ }
+
+ private function render_locker_form(array $settings, Element_Base $element): void
+ {
+ wp_enqueue_script(
+ KING_ADDONS_ASSETS_UNIQUE_KEY . '-protected-content-locker',
+ KING_ADDONS_URL . 'includes/features/Protected_Content/locker-form.js',
+ ['jquery', 'king-display-inline'], Display_Access::asset_version('includes/features/Protected_Content/locker-form.js'), true
+ );
+ $previous = $this->locker_render_context;
+ $this->locker_render_context = [
+ 'document' => $this->get_current_elementor_document_id(),
+ 'element' => $element->get_id(),
+ 'revision' => $this->locker_revision($settings),
+ ];
+ echo '<div class="king-addons-protected-fallback king-addons-protected-locker" data-ka-document="' . esc_attr($this->locker_render_context['document']) . '" data-ka-element="' . esc_attr($element->get_id()) . '">';
+ try {
+ if (!empty($settings['protected_content_locker_template'])) {
+ echo Plugin::$instance->frontend->get_builder_content_for_display((int) $settings['protected_content_locker_template']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Rendered template HTML.
+ } else {
+ echo do_shortcode($settings['protected_content_locker_form']); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Rendered form HTML.
+ }
+ } finally {
+ $this->locker_render_context = $previous;
+ echo '</div>';
+ }
+ }
+
+ private function normalize_locker_settings(array $settings, string $element_id, int $document = 0): array
+ {
+ if (($settings['protected_content_mode'] ?? '') === 'locker' && empty($settings['protected_content_locker_token'])) {
+ $document = $document ?: $this->get_current_elementor_document_id();
+ $settings['protected_content_locker_token'] = 'element-' . $document . '-' . $element_id;
+ }
+ return $settings;
+ }
+
+ private function locker_revision(array $settings): string
+ {
+ return hash('sha256', wp_json_encode([
+ $settings['protected_content_locker_form'] ?? '',
+ $settings['protected_content_locker_token'] ?? '',
+ $settings['protected_content_locker_method'] ?? 'form_submit',
+ (int) ($settings['protected_content_locker_template'] ?? 0),
+ ]));
+ }
+
+ /** Bind a rendered form to the author's configured locker. */
+ private function form_context(string $provider, int $form_document, string $form_element): string
+ {
+ $data = array_merge($this->locker_render_context ?? [], [
+ 'provider' => $provider, 'form_document' => $form_document, 'form_element' => $form_element,
+ ]);
+ $payload = base64_encode(wp_json_encode($data));
+ return $payload . '.' . hash_hmac('sha256', $payload, wp_salt('nonce'));
+ }
+
+ public function after_king_form(int $submission_id, int $form_document, string $form_element, string $context): void
+ {
+ if ($submission_id > 0 && '' !== $context) {
+ $this->complete_locker_form($context, 'king', $form_document, $form_element);
+ }
+ }
+
+ public function cf7_form_fields(array $fields): array
+ {
+ if ($this->locker_render_context) {
+ $form = class_exists('WPCF7_ContactForm') ? WPCF7_ContactForm::get_current()
+ : (function_exists('wpcf7_get_current_contact_form') ? wpcf7_get_current_contact_form() : null);
+ if ($form) {
+ $fields['king_addons_locker_context'] = $this->form_context('cf7', 0, (string) $form->id());
+ }
+ }
+ return $fields;
+ }
+
+ public function after_cf7_form($form): void
+ {
+ $context = isset($_POST['king_addons_locker_context']) && is_string($_POST['king_addons_locker_context'])
+ ? sanitize_text_field(wp_unslash($_POST['king_addons_locker_context'])) : '';
+ if (is_object($form) && method_exists($form, 'id') && '' !== $context) {
+ $this->complete_locker_form($context, 'cf7', 0, (string) $form->id());
+ }
+ }
+
+ /** Runs only after the form provider has accepted a submission. */
+ private function complete_locker_form(string $context, string $provider, int $form_document, string $form_element): void
+ {
+ if (!$this->can_use_pro() || strlen($context) > 16384) {
+ return;
+ }
+ $parts = explode('.', $context);
+ if (count($parts) !== 2 || !hash_equals(hash_hmac('sha256', $parts[0], wp_salt('nonce')), $parts[1])) {
+ return;
+ }
+ $json = base64_decode($parts[0], true);
+ $data = false !== $json ? json_decode($json, true) : null;
+ if (!is_array($data) || ($data['provider'] ?? '') !== $provider
+ || (int) ($data['form_document'] ?? -1) !== $form_document
+ || ($data['form_element'] ?? '') !== $form_element) {
+ return;
+ }
+ $document_id = absint($data['document'] ?? 0);
+ $post = get_post($document_id);
+ if (!$post || $post->post_status !== 'publish' || '' !== $post->post_password) {
+ return;
+ }
+ $elements = json_decode((string) get_post_meta($document_id, '_elementor_data', true), true);
+ $settings = is_array($elements) ? $this->find_element_settings($elements, (string) ($data['element'] ?? '')) : null;
+ if ($settings) { $settings = $this->normalize_locker_settings($settings, (string) ($data['element'] ?? ''), $document_id); }
+ if (!$settings || ($settings['protected_content_enable'] ?? '') !== 'yes'
+ || ($settings['protected_content_mode'] ?? '') !== 'locker'
+ || ($settings['protected_content_locker_method'] ?? 'form_submit') !== 'form_submit'
+ || !hash_equals($this->locker_revision($settings), (string) ($data['revision'] ?? ''))
+ || empty($settings['protected_content_locker_token'])) {
+ return;
+ }
+ $days = max(1, min(365, (int) ($settings['protected_content_locker_cookie_days'] ?? 7)));
+ $this->set_locker_cookie($settings['protected_content_locker_token'], $days);
}
/**
@@ -1020,7 +1230,12 @@
{
$cookie_name = 'king_locker_' . sanitize_key($token);
$expiry = time() + ($days * DAY_IN_SECONDS);
- setcookie($cookie_name, '1', $expiry, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
+ setcookie($cookie_name, Display_Access_Token::issue('locker|' . $token, $expiry), $expiry, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
+ if ($days > 0) {
+ $_COOKIE[$cookie_name] = Display_Access_Token::issue('locker|' . $token, $expiry);
+ } else {
+ unset($_COOKIE[$cookie_name]);
+ }
}
/**
@@ -1045,10 +1260,6 @@
$scope = isset($_POST['scope']) ? sanitize_text_field(wp_unslash($_POST['scope'])) : 'element';
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$global_key = isset($_POST['global_key']) ? sanitize_text_field(wp_unslash($_POST['global_key'])) : '';
- $days = isset($_POST['days']) ? absint($_POST['days']) : 7;
-
- $days = max(1, min(365, $days));
-
if (empty($password) || empty($post_id) || empty($element_id)) {
wp_send_json_error(['message' => esc_html__('Invalid request', 'king-addons')]);
}
@@ -1112,6 +1323,8 @@
wp_send_json_error(['message' => esc_html__('Incorrect password', 'king-addons')]);
}
+ $days = max(1, min(365, (int) ($settings['protected_content_password_cookie_days'] ?? 7)));
+
// Set cookie.
$expiry = time() + ($days * DAY_IN_SECONDS);
if ('global' === $scope && !empty($global_key)) {
@@ -1120,7 +1333,7 @@
$cookie_name = 'king_protect_' . $element_id;
}
- setcookie($cookie_name, '1', $expiry, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
+ setcookie($cookie_name, Display_Access_Token::issue($this->password_context($settings, $element_id, $post_id), $expiry), $expiry, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
wp_send_json_success(['message' => esc_html__('Access granted', 'king-addons')]);
}
@@ -1172,7 +1385,8 @@
$scope = $settings['protected_content_password_type'] ?? 'element';
$global_key = $settings['protected_content_password_key'] ?? '';
$days = (int) ($settings['protected_content_password_cookie_days'] ?? 7);
- $message = $settings['protected_content_fallback_msg'] ?? esc_html__('This content is password protected.', 'king-addons');
+ $original = $element->get_data('settings');
+ $message = !empty($settings['protected_content_password_intro']) ? $settings['protected_content_password_intro'] : ($original['protected_content_fallback_msg'] ?? esc_html__('This content is password protected.', 'king-addons'));
$document_post_id = $this->get_current_elementor_document_id();
if ($document_post_id <= 0) {
@@ -1196,7 +1410,7 @@
placeholder="<?php esc_attr_e('Enter password', 'king-addons'); ?>"
required />
<button type="submit" class="king-addons-password-submit">
- <span class="king-addons-password-submit-text"><?php esc_html_e('Unlock', 'king-addons'); ?></span>
+ <span class="king-addons-password-submit-text"><?php echo esc_html($settings['protected_content_password_button'] ?? __('Unlock', 'king-addons')); ?></span>
<span class="king-addons-password-submit-loading" style="display:none;">⏳</span>
</button>
</div>
@@ -1244,14 +1458,7 @@
*/
private function suppress_element_render(Element_Base $element): void
{
- // Try modern Elementor method first.
- if (method_exists($element, 'set_should_render')) {
- $element->set_should_render(false);
- return;
- }
-
- // Fallback: hide with CSS.
- $element->add_render_attribute('_wrapper', 'class', 'king-addons-protected-hidden');
+ Display_Output_Guard::suppress($element);
}
/**
@@ -1512,11 +1719,7 @@
*/
private function can_use_pro(): bool
{
- if (!function_exists('king_addons_freemius')) {
- return false;
- }
-
- return king_addons_freemius()->can_use_premium_code__premium_only();
+ return Display_Access_Plan::has_pro();
}
/**
@@ -1528,11 +1731,7 @@
*/
private function get_pro_label(string $label): string
{
- if ($this->can_use_pro()) {
- return $label;
- }
-
- return sprintf('%s <i class="eicon-pro-icon"></i>', $label);
+ return Display_Access_Plan::pro_label($label);
}
}
--- a/king-addons/includes/helpers/Conditional_Content_Cache.php
+++ b/king-addons/includes/helpers/Conditional_Content_Cache.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace King_Addons;
+
+if (!defined('ABSPATH')) {
+ exit;
+}
+require_once __DIR__ . '/Display_Access_Plan.php';
+
+/** Keeps conditional elements dynamic without disabling other element caches. */
+class Conditional_Content_Cache
+{
+ private static array $setting_keys = [];
+
+ public static function register(string $setting_key): void
+ {
+ if (empty(self::$setting_keys)) {
+ add_filter('elementor/frontend/builder_content_data', [self::class, 'prepare_document'], 10, 2);
+ add_filter('elementor/element/is_dynamic_content', [self::class, 'is_dynamic'], 10, 2);
+ }
+ self::$setting_keys[$setting_key] = true;
+ }
+
+ public static function is_dynamic(bool $dynamic, array $data): bool
+ {
+ if ($dynamic || self::has_condition($data['settings'] ?? [])) {
+ return true;
+ }
+ foreach ($data['elements'] ?? [] as $child) {
+ if (is_array($child) && self::is_dynamic(false, $child)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static function prepare_document(array $data, int $post_id): array
+ {
+ $has_conditions = false;
+ foreach ($data as &$element) {
+ if (is_array($element) && self::mark_dynamic($element)) {
+ $has_conditions = true;
+ }
+ }
+ unset($element);
+
+ if ($has_conditions && $post_id > 0) {
+ $keys = array_keys(self::$setting_keys);
+ sort($keys);
+ $revision = '5:' . (Display_Access_Plan::has_pro() ? 'pro:' : 'free:') . implode(',', $keys);
+ // Replace legacy cached markup once, only for affected documents.
+ if (get_post_meta($post_id, '_king_addons_display_cache_revision', true) !== $revision) {
+ delete_post_meta($post_id, '_elementor_element_cache');
+ update_post_meta($post_id, '_king_addons_display_cache_revision', $revision);
+ }
+ }
+ return $data;
+ }
+
+ private static function mark_dynamic(array &$element): bool
+ {
+ $dynamic = self::has_condition($element['settings'] ?? []);
+ if (!empty($element['elements']) && is_array($element['elements'])) {
+ foreach ($element['elements'] as &$child) {
+ if (is_array($child) && self::mark_dynamic($child)) {
+ $dynamic = true;
+ }
+ }
+ unset($child);
+ }
+ if ($dynamic) {
+ // Runtime data only: saved Cache Settings and static siblings stay intact.
+ $element['settings']['_element_cache'] = 'yes';
+ }
+ return $dynamic;
+ }
+
+ private static function has_condition(array $settings): bool
+ {
+ if (isset(self::$setting_keys['cd_enable']) && ($settings['cd_hide_empty'] ?? '') === 'yes') { return true; }
+ foreach (self::$setting_keys as $key => $unused) {
+ if (($settings[$key] ?? '') === 'yes') {
+ return true;
+ }
+ }
+ return false;
+ }
+}
--- a/king-addons/includes/helpers/Display_Access.php
+++ b/king-addons/includes/helpers/Display_Access.php
@@ -0,0 +1,193 @@
+<?php
+namespace King_Addons;
+
+use ElementorControls_Manager as Controls;
+use ElementorElement_Base;
+
+if (!defined('ABSPATH')) { exit; }
+require_once __DIR__ . '/Display_Rules.php';
+require_once __DIR__ . '/Display_Access_Plan.php';
+
+/** One editor entry point; legacy setting names and runtime remain compatible. */
+class Display_Access
+{
+ const TAB = 'king-display-access';
+ private static bool $registered = false;
+ private static array $features = [];
+
+ public static function register(string $feature): void
+ {
+ self::$features[$feature] = true;
+ if (self::$registered) { return; }
+ self::$registered = true;
+ $tab = static function () { Controls::add_tab(self::TAB, __('Display', 'king-addons')); };
+ if (did_action('elementor/init')) { $tab(); } else { add_action('elementor/init', $tab); }
+ add_action('elementor/editor/after_enqueue_scripts', static function () {
+ wp_enqueue_style('king-display-access', KING_ADDONS_URL . 'includes/helpers/display-access.css', [], self::asset_version('includes/helpers/display-access.css'));
+ wp_enqueue_script('king-display-access-editor', KING_ADDONS_URL . 'includes/helpers/display-access-editor.js', ['jquery'], self::asset_version('includes/helpers/display-access-editor.js'), true);
+ wp_localize_script('king-display-access-editor', 'kingDisplayEditor', ['ajax' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('king_display_editor'), 'hasPro' => Display_Access_Plan::has_pro(), 'upgradeUrl' => Display_Access_Plan::upgrade_url(), 'upgradeLabel' => __('Get King Addons Pro', 'king-addons')]);
+ });
+ add_action('elementor/frontend/after_enqueue_styles', static function () { wp_enqueue_style('king-display-access', KING_ADDONS_URL . 'includes/helpers/display-access.css', [], self::asset_version('includes/helpers/display-access.css')); });
+ add_action('wp_ajax_king_addons_display_preview', [self::class, 'preview']);
+ }
+
+ public static function asset_version(string $path): string
+ {
+ return KING_ADDONS_VERSION . '.' . filemtime(KING_ADDONS_PATH . $path);
+ }
+
+ public static function intro(Element_Base $element): void
+ {
+ if ($element->get_controls('ka_display_start')) { return; }
+ $element->start_controls_section('ka_display_start', ['label' => __('Quick setup', 'king-addons'), 'tab' => self::TAB]);
+ $element->add_control('ka_display_help', ['type' => Controls::RAW_HTML, 'raw' => '<p class="ka-display-intro">' . esc_html__('Choose how this element appears or opens.', 'king-addons') . '</p>']);
+ $presets = [
+ 'rules' => ['label' => __('Visibility rules', 'king-addons'), 'hint' => __('Choose who sees it.', 'king-addons'), 'icon' => 'eicon-eye'],
+ 'password' => ['label' => __('Password access', 'king-addons'), 'hint' => __('Password or selected roles.', 'king-addons'), 'icon' => 'eicon-lock'],
+ 'locker' => ['label' => __('Unlock after a form', 'king-addons'), 'hint' => __('Unlock on form submission.', 'king-addons'), 'icon' => 'eicon-form-horizontal'],
+ ];
+ foreach ($presets as $key => $preset) {
+ if (!isset(self::$features[$key === 'rules' ? 'cd' : 'pc'])) { continue; }
+ $pro = $key !== 'rules';
+ $text = '<i class="ka-display-action-icon ' . esc_attr($preset['icon']) . '"></i>'
+ . '<span class="ka-display-action-copy"><span class="ka-display-action-title">' . esc_html($preset['label'])
+ . ($pro ? '<span class="ka-display-action-pro">Pro</span>' : '') . '</span>'
+ . '<span class="ka-display-action-hint">' . esc_html($preset['hint']) . '</span></span>'
+ . '<i class="ka-display-action-arrow eicon-chevron-right"></i>';
+ $element->add_control('ka_display_preset_' . $key, ['type' => Controls::BUTTON, 'text' => $text, 'event' => 'king:display:' . $key, 'label_block' => true]);
+ }
+ $element->add_control('ka_display_next', ['type' => Controls::RAW_HTML, 'raw' => '<p class="ka-display-next">' . esc_html__('Choose an option to open its settings below.', 'king-addons') . '</p>']);
+ $element->end_controls_section();
+ }
+
+ public static function rule_setup_control(Element_Base $element): void
+ {
+ $element->add_control('cd_rules_mode', ['label' => __('Rule setup', 'king-addons'), 'type' => Controls::SELECT, 'default' => 'legacy', 'options' => ['legacy' => __('Classic conditions', 'king-addons'), 'rules' => Display_Access_Plan::pro_label(__('Advanced rule builder', 'king-addons'))], 'condition' => ['cd_enable' => 'yes'], 'description' => __('Switching setup keeps your saved conditions.', 'king-addons')]);
+ self::upgrade_control($element, 'cd', ['cd_enable' => 'yes', 'cd_rules_mode' => 'rules']);
+ }
+
+ public static function upgrade_control(Element_Base $element, string $feature, array $condition = [], string $suffix = ''): void
+ {
+ if (Display_Access_Plan::has_pro()) { return; }
+ $title = $feature === 'cd' ? __('Unlock advanced display rules', 'king-addons') : __('Unlock password and form access', 'king-addons');
+ $description = $feature === 'cd'
+ ? __('Get ACF and post fields, AND/OR groups, scheduling, advanced targeting, rule explanations and interactive display controls.', 'king-addons')
+ : __('Add passwords, role-or-password access, form unlocks, scheduling, URL and WooCommerce conditions.', 'king-addons');
+ $raw = '<div class="ka-display-upgrade"><span class="ka-display-action-pro">Pro</span><strong>' . esc_html($title) . '</strong><p>' . esc_html($description) . '</p><a href="' . esc_url(Display_Access_Plan::upgrade_url()) . '" target="_blank" rel="noopener">' . esc_html__('Get King Addons Pro', 'king-addons') . '</a><small>' . esc_html__('Saved Pro settings are kept. Elements using Pro access conditions stay hidden until Pro is restored or you explicitly change their protection settings.', 'king-addons') . '</small></div>';
+ $element->add_control('ka_display_upgrade_' . $feature . $suffix, ['type' => Controls::RAW_HTML, 'raw' => $raw, 'condition' => $condition]);
+ }
+
+ public static function plan_controls(Element_Base $element, string $section): void
+ {
+ $has_pro = Display_Access_Plan::has_pro();
+ $badges = ['cd_cond_browser_enable', 'cd_cond_date_enable', 'cd_cond_time_enable', 'cd_cond_url_enable', 'cd_cond_ref_enable', 'cd_cond_woo_enable', 'cd_cond_cookie_enable', 'cd_cond_lang_enable', 'cd_reveal', 'cd_hide_empty'];
+ foreach ($element->get_controls() as $key => $control) {
+ if (($control['section'] ?? '') !== $section) { continue; }
+ $updates = [];
+ if (in_array($key, $badges, true) && isset($control['label'])) { $updates['label'] = Display_Access_Plan::pro_label($control['label']); }
+ if (!$has_pro && Display_Access_Plan::pro_control($key)) { $updates['classes'] = trim(($control['classes'] ?? '') . ' ka-display-pro-only'); }
+ if ($updates) { $element->update_control($key, $updates); }
+ }
+ }
+
+ public static function editor_field_layout(Element_Base $element, string $section): void
+ {
+ foreach ($element->get_controls() as $key => $control) {
+ if (($control['section'] ?? '') === $section && in_array($control['type'] ?? '', [Controls::SELECT, Controls::SELECT2, Controls::TEXT, Controls::DATE_TIME], true)) {
+ $layout = ['label_block' => true];
+ if (($control['type'] ?? '') === Controls::TEXT && !in_array($key, ['cd_reveal_label', 'protected_content_password_button'], true)) {
+ $layout['ai'] = ['active' => false];
+ $layout['classes'] = trim(($control['classes'] ?? '') . ' ka-display-technical-field');
+ }
+ $element->update_control($key, $layout);
+ }
+ }
+ }
+
+ public static function rules_controls(Element_Base $element): void
+ {
+ $rule = new ElementorRepeater();
+ $rule->add_control('label', ['label' => __('Rule name (optional)', 'king-addons'), 'type' => Controls::TEXT]);
+ $rule->add_control('join', ['label' => __('Combine with previous rules', 'king-addons'), 'type' => Controls::SELECT, 'default' => 'and', 'options' => ['and' => __('AND — also require this', 'king-addons'), 'or' => __('OR — start another group', 'king-addons')], 'description' => __('Ignored for the first rule. AND stays in the same group; OR starts another way to show the block.', 'king-addons')]);
+ $rule->add_control('type', ['label' => __('What to check', 'king-addons'), 'type' => Controls::SELECT, 'default' => 'login', 'options' => Display_Rules::type_options()]);
+ $rule->add_control('source', ['label' => __('Read field from', 'king-addons'), 'type' => Controls::SELECT, 'default' => 'item', 'options' => ['item' => __('Current item (including Loop)', 'king-addons'), 'page' => __('Current page', 'king-addons')], 'condition' => [