get_settings_for_display();
foreach (['aad_visibility_toggle', 'aad_visibility_toggle_container'] as $key) {
if (isset($settings[$key])) {
$class = aad_get_visibility_class($settings[$key]);
if ($class) {
$element->add_render_attribute('_wrapper', 'class', $class);
}
}
}
}
}
// Add controls to widgets, sections, columns
foreach (['common', 'section', 'column'] as $type) {
add_action("elementor/element/{$type}/_section_style/after_section_end", function($element) use ($type) {
if (method_exists($element, 'get_controls') && isset($element->get_controls()['aad_visibility_section'])) {
return; // Already registered
}
$element->start_controls_section('aad_visibility_section', [
'label' => __('Adaptive Audience Display', 'adaptive-audience-display'),
'tab' => \Elementor\Controls_Manager::TAB_ADVANCED,
]);
// Add dropdown selector to widgets, sections, columns
$element->add_control('aad_visibility_toggle', [
'label' => __('Show Element', 'adaptive-audience-display'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'always',
'options' => [
'always' => __('Always', 'adaptive-audience-display'),
'logged_in' => __('When user is Logged in', 'adaptive-audience-display'),
'logged_out' => __('When user is Logged out', 'adaptive-audience-display'),
],
]);
// Add Role-Based Toggle to widgets, sections, columns
$element->add_control('aad_enable_role_toggle', [
'label' => __('Enable Role Based Visibility', 'adaptive-audience-display'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'label_on' => __('Yes', 'adaptive-audience-display'),
'label_off' => __('No', 'adaptive-audience-display'),
'return_value' => 'yes',
'default' => 'no',
]);
// Add Divider Text For Pro to widgets, sections, columns
$element->add_control(
'aad_role_divider',
[
'type' => \Elementor\Controls_Manager::RAW_HTML,
'raw' => '
---- PRO Features ----
',
'content_classes' => 'aad-role-divider',
'separator' => 'none',
'condition' => [
'aad_enable_role_toggle' => 'yes',
],
]
);
// Add Dynamic Roles to widgets, sections, columns
$roles = wp_roles()->roles;
foreach ($roles as $role_key => $role_details) {
$element->add_control(
'aad_role_toggle_' . $role_key,
[
'type' => \Elementor\Controls_Manager::RAW_HTML,
'raw' => '
' . esc_html($role_details['name']) . '
',
'content_classes' => 'aad-role-locked',
'condition' => [
'aad_enable_role_toggle' => 'yes',
],
]
);
}
$element->end_controls_section();
}, 10, 2);
}
// Add controls to containers
add_action('elementor/element/container/section_layout/after_section_end', function($element) {
if (method_exists($element, 'get_controls') && isset($element->get_controls()['aad_visibility_section_container'])) {
return; // Already registered
}
$element->start_controls_section('aad_visibility_section_container', [
'label' => __('Adaptive Audience Display', 'adaptive-audience-display'),
'tab' => \Elementor\Controls_Manager::TAB_ADVANCED,
]);
// Add dropdown selector to containers
$element->add_control('aad_visibility_toggle_container', [
'label' => __('Show Element', 'adaptive-audience-display'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'always',
'options' => [
'always' => __('Always', 'adaptive-audience-display'),
'logged_in' => __('When user is Logged in', 'adaptive-audience-display'),
'logged_out' => __('When user is Logged out', 'adaptive-audience-display'),
],
]);
// Add Role-Based Toggle to containers
$element->add_control('aad_enable_role_toggle', [
'label' => __('Enable Role Based Visibility', 'adaptive-audience-display'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'label_on' => __('Yes', 'adaptive-audience-display'),
'label_off' => __('No', 'adaptive-audience-display'),
'return_value' => 'yes',
'default' => 'no',
]);
// Add Divider Text For Pro to containers
$element->add_control(
'aad_role_divider',
[
'type' => \Elementor\Controls_Manager::RAW_HTML,
'raw' => '---- PRO Features ----
',
'content_classes' => 'aad-role-divider',
'separator' => 'none',
'condition' => [
'aad_enable_role_toggle' => 'yes',
],
]
);
// Add Dynamic Roles to containers
$roles = wp_roles()->roles;
foreach ($roles as $role_key => $role_details) {
$element->add_control(
'aad_role_toggle_' . $role_key,
[
'type' => \Elementor\Controls_Manager::RAW_HTML,
'raw' => '
' . esc_html($role_details['name']) . '
',
'content_classes' => 'aad-role-locked',
'condition' => [
'aad_enable_role_toggle' => 'yes',
],
]
);
}
$element->end_controls_section();
}, 10, 2);
// Inject visibility classes
add_action('elementor/frontend/widget/before_render', 'aad_auto_class_assignment');
add_action('elementor/frontend/container/before_render', 'aad_auto_class_assignment');
add_action('elementor/frontend/section/before_render', 'aad_auto_class_assignment');
// CSS fallback + JS logic
add_action('wp_enqueue_scripts', function () {
// CSS fallback: hide all conditional elements by default
wp_register_style('aad-visibility-inline', false, [], '1.1.26');
wp_enqueue_style('aad-visibility-inline');
wp_add_inline_style('aad-visibility-inline', '
.aad-hide-logged-in,
.aad-hide-logged-out {
display: none !important;
}
');
wp_register_script('aad-visibility-inline', '', [], '1.1.26', true);
wp_enqueue_script('aad-visibility-inline');
wp_add_inline_script('aad-visibility-inline', '
document.addEventListener("DOMContentLoaded", function() {
const isLoggedIn = document.body.classList.contains("logged-in");
const forceShow = function(el) {
let current = el;
while (current && current.tagName.toLowerCase() !== "body") {
const computedDisplay = window.getComputedStyle(current).display;
let restoreDisplay = current.getAttribute("data-original-display") || (computedDisplay === "none" ? "flex" : computedDisplay);
current.style.setProperty("display", restoreDisplay, "important");
current.style.setProperty("visibility", "visible", "important");
if (window.getComputedStyle(current).opacity === "0") {
current.style.setProperty("opacity", "1", "important");
}
current = current.parentElement;
}
};
if (isLoggedIn) {
document.querySelectorAll(".aad-hide-logged-out").forEach(function(el) { forceShow(el); });
document.querySelectorAll(".aad-hide-logged-in").forEach(function(el) { el.style.setProperty("display", "none", "important"); });
} else {
document.querySelectorAll(".aad-hide-logged-in").forEach(function(el) { forceShow(el); });
document.querySelectorAll(".aad-hide-logged-out").forEach(function(el) { el.style.setProperty("display", "none", "important"); });
}
});
');
});