menu_slug,
[$this, 'renderAdminPage'],
'dashicons-align-full-width',
54.7
);
}
/**
* Render modern admin page for Header & Footer Builder.
*
* @return void
*/
public function renderAdminPage(): void
{
if (!current_user_can('manage_options')) {
return;
}
// Include shared dark theme support
include KING_ADDONS_PATH . 'includes/admin/shared/dark-theme.php';
// Handle tab navigation
$current_tab = isset($_GET['tab']) ? sanitize_key(wp_unslash($_GET['tab'])) : 'templates'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if (!in_array($current_tab, ['templates', 'settings'], true)) {
$current_tab = 'templates';
}
$this->handleInlineActions();
$templates = $this->prepareAdminTemplates();
$base_url = admin_url('admin.php?page=' . $this->menu_slug);
$status_filter = 'all';
$filtered_templates = $templates;
$type_cards = [
'header' => [
'label' => esc_html__('Header', 'king-addons'),
'desc' => esc_html__('Site header templates', 'king-addons'),
'value' => 'king_addons_el_hf_type_header',
],
'footer' => [
'label' => esc_html__('Footer', 'king-addons'),
'desc' => esc_html__('Site footer templates', 'king-addons'),
'value' => 'king_addons_el_hf_type_footer',
],
];
$this->renderModernStyles();
// Render dark theme styles and init
ka_render_dark_theme_styles();
ka_render_dark_theme_init();
?>
$data) : ?>
getTypeIconSvg($type_slug); ?>
'delete_template', 'template_id' => $template_id], $base_url), 'ka_hf_delete_' . $template_id);
$conditions_text = $this->summarizeConditions($template);
$title_icon_svg = $this->getTypeIconSvg($type_slug);
$post_status = get_post_status($template_id);
$is_disabled = 'publish' !== $post_status;
$status_label = $is_disabled ? esc_html__('Disabled', 'king-addons') : $type_label;
?>
$template_id,
'title' => $title,
'type' => $type_value,
'include' => $include_locs,
'exclude' => $exclude_locs,
'userRoles' => $user_roles_arr,
];
?>
renderAddNewModal(); ?>
renderRenameModal(); ?>
renderConditionsPopup(); ?>
renderDisplaySettingsTab(); ?>
$group_data) {
foreach ($group_data['value'] as $opt_key => $opt_value) {
$location_options[$opt_key] = $opt_value;
}
}
$user_options = [];
foreach (self::$user_selection as $group_data) {
foreach ($group_data['value'] as $opt_key => $opt_value) {
$user_options[$opt_key] = $opt_value;
}
}
?>
esc_html__('Permission denied.', 'king-addons')]);
}
if (!check_ajax_referer('ka_hf_save_conditions', 'nonce', false)) {
wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'king-addons')]);
}
$template_id = isset($_POST['template_id']) ? (int) $_POST['template_id'] : 0;
if (!$template_id || 'king-addons-el-hf' !== get_post_type($template_id)) {
wp_send_json_error(['message' => esc_html__('Invalid template.', 'king-addons')]);
}
// Parse include rules
$include_rules = [];
$include_specific = [];
if (!empty($_POST['include_rules']) && is_array($_POST['include_rules'])) {
foreach ($_POST['include_rules'] as $rule) {
$rule_val = sanitize_text_field(wp_unslash($rule['rule'] ?? ''));
if ($rule_val) {
$include_rules[] = $rule_val;
if ('specifics' === $rule_val && !empty($rule['specific'])) {
$specific_ids = array_map('intval', array_filter(explode(',', sanitize_text_field(wp_unslash($rule['specific'])))));
$include_specific = array_merge($include_specific, $specific_ids);
}
}
}
}
// Parse exclude rules
$exclude_rules = [];
$exclude_specific = [];
if (!empty($_POST['exclude_rules']) && is_array($_POST['exclude_rules'])) {
foreach ($_POST['exclude_rules'] as $rule) {
$rule_val = sanitize_text_field(wp_unslash($rule['rule'] ?? ''));
if ($rule_val) {
$exclude_rules[] = $rule_val;
if ('specifics' === $rule_val && !empty($rule['specific'])) {
$specific_ids = array_map('intval', array_filter(explode(',', sanitize_text_field(wp_unslash($rule['specific'])))));
$exclude_specific = array_merge($exclude_specific, $specific_ids);
}
}
}
}
// Parse user roles (multiple)
$user_roles = [];
if (!empty($_POST['user_roles']) && is_array($_POST['user_roles'])) {
foreach ($_POST['user_roles'] as $role) {
$role_val = sanitize_text_field(wp_unslash($role));
if ($role_val) {
$user_roles[] = $role_val;
}
}
}
if (empty($user_roles)) {
$user_roles = ['all'];
}
$user_roles = array_values(array_unique($user_roles));
if (in_array('all', $user_roles, true)) {
$user_roles = ['all'];
}
// Parse template type
$template_type = isset($_POST['template_type']) ? sanitize_text_field(wp_unslash($_POST['template_type'])) : '';
if (!empty($template_type) && in_array($template_type, ['king_addons_el_hf_type_header', 'king_addons_el_hf_type_footer'], true)) {
update_post_meta($template_id, 'king_addons_el_hf_template_type', $template_type);
}
// Save meta
$include_locations = [
'rule' => $include_rules,
'specific' => $include_specific,
];
$exclude_locations = [
'rule' => $exclude_rules,
'specific' => $exclude_specific,
];
update_post_meta($template_id, 'king_addons_el_hf_target_include_locations', $include_locations);
update_post_meta($template_id, 'king_addons_el_hf_target_exclude_locations', $exclude_locations);
update_post_meta($template_id, 'king_addons_el_hf_target_user_roles', $user_roles);
wp_send_json_success(['message' => esc_html__('Settings saved successfully.', 'king-addons')]);
}
/**
* AJAX handler to rename a template.
*
* @return void
*/
public function handleAjaxRenameTemplate(): void
{
if (!current_user_can('manage_options')) {
wp_send_json_error(['message' => esc_html__('Permission denied.', 'king-addons')]);
}
if (!check_ajax_referer('ka_hf_save_conditions', 'nonce', false)) {
wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'king-addons')]);
}
$template_id = isset($_POST['template_id']) ? (int) $_POST['template_id'] : 0;
$new_title = isset($_POST['new_title']) ? sanitize_text_field(wp_unslash($_POST['new_title'])) : '';
if (!$template_id || 'king-addons-el-hf' !== get_post_type($template_id)) {
wp_send_json_error(['message' => esc_html__('Invalid template.', 'king-addons')]);
}
if (empty($new_title)) {
wp_send_json_error(['message' => esc_html__('Title cannot be empty.', 'king-addons')]);
}
$result = wp_update_post([
'ID' => $template_id,
'post_title' => $new_title,
]);
if (is_wp_error($result)) {
wp_send_json_error(['message' => esc_html__('Failed to rename template.', 'king-addons')]);
}
wp_send_json_success(['message' => esc_html__('Template renamed successfully.', 'king-addons')]);
}
/**
* AJAX handler to toggle template status (publish/draft).
*
* @return void
*/
public function handleAjaxToggleTemplateStatus(): void
{
if (!current_user_can('manage_options')) {
wp_send_json_error(['message' => esc_html__('Permission denied.', 'king-addons')]);
}
if (!check_ajax_referer('ka_hf_save_conditions', 'nonce', false)) {
wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'king-addons')]);
}
$template_id = isset($_POST['template_id']) ? (int) $_POST['template_id'] : 0;
$new_status = isset($_POST['new_status']) ? sanitize_text_field(wp_unslash($_POST['new_status'])) : '';
if (!$template_id || 'king-addons-el-hf' !== get_post_type($template_id)) {
wp_send_json_error(['message' => esc_html__('Invalid template.', 'king-addons')]);
}
if (!in_array($new_status, ['publish', 'draft'], true)) {
wp_send_json_error(['message' => esc_html__('Invalid status.', 'king-addons')]);
}
$result = wp_update_post([
'ID' => $template_id,
'post_status' => $new_status,
]);
if (is_wp_error($result)) {
wp_send_json_error(['message' => esc_html__('Failed to update template status.', 'king-addons')]);
}
wp_send_json_success(['message' => esc_html__('Template status updated.', 'king-addons')]);
}
/**
* Handle "Add New" template submission.
*
* @return void
*/
public function handleCreateTemplate(): void
{
if (!current_user_can('manage_options')) {
wp_die(esc_html__('You do not have permission to perform this action.', 'king-addons'));
}
if (!isset($_POST['ka_hf_builder_create_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ka_hf_builder_create_nonce'])), 'ka_hf_builder_create')) {
wp_die(esc_html__('Invalid nonce.', 'king-addons'));
}
$title = isset($_POST['ka_hf_title']) ? sanitize_text_field(wp_unslash($_POST['ka_hf_title'])) : esc_html__('My Template', 'king-addons');
$type = isset($_POST['ka_hf_type']) ? sanitize_text_field(wp_unslash($_POST['ka_hf_type'])) : 'king_addons_el_hf_type_header';
$display_rule = isset($_POST['ka_hf_display_rule']) ? sanitize_text_field(wp_unslash($_POST['ka_hf_display_rule'])) : 'basic-global';
$display_specific = isset($_POST['ka_hf_display_specific']) ? sanitize_text_field(wp_unslash($_POST['ka_hf_display_specific'])) : '';
$display_canvas = isset($_POST['ka_hf_display_canvas']) ? '1' : '';
// Parse user roles (multiple selection)
$user_roles = [];
if (!empty($_POST['ka_hf_user_role']) && is_array($_POST['ka_hf_user_role'])) {
foreach ($_POST['ka_hf_user_role'] as $role) {
$role_val = sanitize_text_field(wp_unslash($role));
if ($role_val) {
$user_roles[] = $role_val;
}
}
}
if (empty($user_roles)) {
$user_roles = ['all'];
}
$user_roles = array_values(array_unique($user_roles));
if (in_array('all', $user_roles, true)) {
$user_roles = ['all'];
}
// Validate type
if (!in_array($type, ['king_addons_el_hf_type_header', 'king_addons_el_hf_type_footer'], true)) {
$type = 'king_addons_el_hf_type_header';
}
// Create the post
$post_id = wp_insert_post([
'post_type' => 'king-addons-el-hf',
'post_status' => 'publish',
'post_title' => $title,
]);
if (is_wp_error($post_id)) {
wp_die(esc_html__('Unable to create template.', 'king-addons'));
}
// Save template type immediately
update_post_meta($post_id, 'king_addons_el_hf_template_type', $type);
// Save display conditions
$specific_ids = [];
if ('specifics' === $display_rule && !empty($display_specific)) {
$specific_ids = array_map('intval', array_filter(explode(',', $display_specific)));
}
$target_locations = [
'rule' => [$display_rule],
'specific' => $specific_ids,
];
update_post_meta($post_id, 'king_addons_el_hf_target_include_locations', $target_locations);
update_post_meta($post_id, 'king_addons_el_hf_target_exclude_locations', []);
// Save user roles (multiple)
update_post_meta($post_id, 'king_addons_el_hf_target_user_roles', $user_roles);
// Save canvas display option
if ($display_canvas) {
update_post_meta($post_id, 'king-addons-el-hf-display-on-canvas', '1');
}
// Redirect to Elementor editor
wp_safe_redirect(admin_url('post.php?post=' . $post_id . '&action=elementor'));
exit;
}
/**
* Handle quick update submission.
*
* @return void
*/
public function handleQuickUpdate(): void
{
if (!current_user_can('manage_options')) {
wp_die(esc_html__('You do not have permission to perform this action.', 'king-addons'));
}
if (!isset($_POST['ka_hf_quick_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ka_hf_quick_nonce'])), 'ka_hf_quick')) {
wp_die(esc_html__('Invalid nonce.', 'king-addons'));
}
$template_id = isset($_POST['template_id']) ? (int) $_POST['template_id'] : 0;
$type = isset($_POST['type']) ? sanitize_text_field(wp_unslash($_POST['type'])) : '';
if ($template_id > 0 && !empty($type)) {
update_post_meta($template_id, 'king_addons_el_hf_template_type', $type);
}
wp_safe_redirect(admin_url('admin.php?page=' . $this->menu_slug));
exit;
}
/**
* Handle inline toggle/delete actions.
*
* @return void
*/
private function handleInlineActions(): void
{
$action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$template_id = isset($_GET['template_id']) ? (int) $_GET['template_id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if (!$template_id || empty($action)) {
return;
}
if ('delete_template' === $action) {
if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'ka_hf_delete_' . $template_id)) {
return;
}
wp_trash_post($template_id);
}
}
/**
* Prepare data for admin list table.
*
* @return array
*/
private function prepareAdminTemplates(): array
{
$args = [
'post_type' => 'king-addons-el-hf',
'post_status' => ['publish', 'draft'],
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
];
$query = new WP_Query($args);
$templates = [];
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$templates[] = [
'id' => $post_id,
'title' => get_the_title(),
'type' => get_post_meta($post_id, 'king_addons_el_hf_template_type', true),
'include_locations' => get_post_meta($post_id, 'king_addons_el_hf_target_include_locations', true),
'exclude_locations' => get_post_meta($post_id, 'king_addons_el_hf_target_exclude_locations', true),
'user_roles' => get_post_meta($post_id, 'king_addons_el_hf_target_user_roles', true),
];
}
}
wp_reset_postdata();
return $templates;
}
/**
* Summarize conditions to a short label for admin list.
*
* @param array $template Template data.
*
* @return string
*/
private function summarizeConditions(array $template): string
{
$locations = $template['include_locations'] ?? [];
if (empty($locations) || empty($locations['rule'])) {
return esc_html__('All', 'king-addons');
}
$rules = $locations['rule'];
$count = is_array($rules) ? count($rules) : 0;
if ($count === 0) {
return esc_html__('All', 'king-addons');
}
// Get the first rule label
$first_rule = $rules[0] ?? '';
$label = self::getLocation($first_rule);
if ($count > 1) {
return sprintf(
esc_html__('%s + %d more', 'king-addons'),
$label,
$count - 1
);
}
return $label ?: esc_html__('All', 'king-addons');
}
/**
* SVG icon for template type.
*
* @param string $type_slug Template type slug.
*
* @return string
*/
private function getTypeIconSvg(string $type_slug): string
{
switch ($type_slug) {
case 'header':
return '';
case 'footer':
return '';
default:
return '';
}
}
/**
* Render modern CSS styles for admin page.
*
* @return void
*/
private function renderModernStyles(): void
{
?>
id === 'edit-king-addons-el-hf') {
return false;
}
return $show_screen;
}
function king_addons_el_hf_get_posts_by_query()
{
// Security fix: Add authorization check
if (!current_user_can('edit_posts')) {
wp_send_json_error('Insufficient permissions');
return;
}
check_ajax_referer('king-addons-el-hf-get-posts-by-query', 'nonce');
$search_string = isset($_POST['q']) ? sanitize_text_field($_POST['q']) : '';
$result = array();
$args = array(
'public' => true,
'_builtin' => false,
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types($args, $output, $operator);
unset($post_types['elementor-hf']);
$post_types['Posts'] = 'post';
$post_types['Pages'] = 'page';
foreach ($post_types as $key => $post_type) {
$data = array();
add_filter('posts_search', array($this, 'search_only_titles'), 10, 2);
$query = new WP_Query(
array(
's' => $search_string,
'post_type' => $post_type,
'posts_per_page' => -1,
)
);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$title = get_the_title();
$title .= (0 != $query->post->post_parent) ? ' (' . get_the_title($query->post->post_parent) . ')' : '';
$id = get_the_id();
$data[] = array(
'id' => 'post-' . $id,
'text' => $title,
);
}
}
if (is_array($data) && !empty($data)) {
$result[] = array(
'text' => $key,
'children' => $data,
);
}
}
wp_reset_postdata();
$args = array(
'public' => true,
);
$output = 'objects';
$taxonomies = get_taxonomies($args, $output, $operator);
foreach ($taxonomies as $taxonomy) {
$terms = get_terms(
$taxonomy->name,
array(
'orderby' => 'count',
'hide_empty' => 0,
'name__like' => $search_string,
)
);
$data = array();
$label = ucwords($taxonomy->label);
if (!empty($terms)) {
foreach ($terms as $term) {
$data[] = array(
'id' => 'tax-' . $term->term_id,
'text' => $term->name . ' archive page',
);
$data[] = array(
'id' => 'tax-' . $term->term_id . '-single-' . $taxonomy->name,
'text' => 'All singulars from ' . $term->name,
);
}
}
if (is_array($data) && !empty($data)) {
$result[] = array(
'text' => $label,
'children' => $data,
);
}
}
wp_send_json($result);
}
public function initialize_options()
{
self::$user_selection = self::get_user_selections();
self::$location_selection = self::getLocationSelections();
}
public function renderAdminCustomHeader()
{
$current_screen = get_current_screen()->id;
if ($current_screen !== 'edit-king-addons-el-hf'
&& $current_screen !== 'header-footer_page_king-addons-el-hf-settings') {
return;
}
?>
publish + (int)$counts->draft;
if (0 === $total) {
echo '';
echo '
';
echo esc_html__("Create the first header or footer by clicking the 'Create New' button above.", 'king addons');
echo '
';
echo '
';
}
}
function addPostType(): void
{
if (!current_user_can('manage_options')) {
return;
}
$labels = [
'name' => esc_html__('Elementor Header & Footer Builder', 'king-addons'),
'singular_name' => esc_html__('Elementor Header & Footer Builder', 'king-addons'),
'menu_name' => esc_html__('Elementor Header & Footer Builder', 'king-addons'),
'name_admin_bar' => esc_html__('Elementor Header & Footer Builder', 'king-addons'),
'add_new' => esc_html__('Add New', 'king-addons'),
'add_new_item' => esc_html__('Add New', 'king-addons'),
'new_item' => esc_html__('New Template', 'king-addons'),
'edit_item' => esc_html__('Edit Template', 'king-addons'),
'view_item' => esc_html__('View Template', 'king-addons'),
'all_items' => esc_html__('All Templates', 'king-addons'),
'search_items' => esc_html__('Search Templates', 'king-addons'),
'parent_item_colon' => esc_html__('Parent Templates:', 'king-addons'),
'not_found' => esc_html__('No Templates found.', 'king-addons'),
'not_found_in_trash' => esc_html__('No Templates found in Trash.', 'king-addons'),
];
$args = [
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'exclude_from_search' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_icon' => 'dashicons-editor-kitchensink',
'supports' => ['title', 'editor', 'thumbnail', 'elementor'],
'show_in_rest' => true,
];
register_post_type('king-addons-el-hf', $args);
if (false === get_option('king_addons_HFB_flushed_rewrite_rules')) {
add_option('king_addons_HFB_flushed_rewrite_rules', true);
flush_rewrite_rules();
}
}
function registerMetabox()
{
add_meta_box(
'king-addons-el-hf-meta-box',
esc_html__('Elementor Header & Footer Builder Options', 'king-addons'),
[$this, 'renderMetabox'],
'king-addons-el-hf',
'normal',
'high'
);
}
function renderMetabox($post)
{
$values = get_post_custom($post->ID);
$template_type = isset($values['king_addons_el_hf_template_type']) ? esc_attr(sanitize_text_field($values['king_addons_el_hf_template_type'][0])) : '';
$display_on_canvas = isset($values['king-addons-el-hf-display-on-canvas']);
wp_nonce_field('king_addons_el_hf_meta_nounce', 'king_addons_el_hf_meta_nounce');
?>
__('Please enter', 'king-addons'),
'please_delete' => __('Please delete', 'king-addons'),
'more_char' => __('or more characters', 'king-addons'),
'character' => __('character', 'king-addons'),
'loading' => __('Loading more results…', 'king-addons'),
'only_select' => __('You can only select', 'king-addons'),
'item' => __('item', 'king-addons'),
'char_s' => __('s', 'king-addons'),
'no_result' => __('No results found', 'king-addons'),
'searching' => __('Searching…', 'king-addons'),
'not_loader' => __('The results could not be loaded.', 'king-addons'),
'search' => __('Search pages / post / categories', 'king-addons'),
'ajax_nonce' => wp_create_nonce('king-addons-el-hf-get-posts-by-query'),
);
wp_localize_script('king-addons-el-hf-select2', 'kngRules', $localize_vars);
}
public function display_rules_tab()
{
$this->admin_styles();
$include_locations = get_post_meta(get_the_id(), 'king_addons_el_hf_target_include_locations', true);
$exclude_locations = get_post_meta(get_the_id(), 'king_addons_el_hf_target_exclude_locations', true);
$users = get_post_meta(get_the_id(), 'king_addons_el_hf_target_user_roles', true);
?>
|
|
__('Display Rules', 'king-addons'),
'value' => '[{"type":"basic-global","specific":null}]',
'tags' => 'site,enable,target,pages',
'rule_type' => 'display',
'add_rule_label' => __('Add Display Rule', 'king-addons'),
],
$include_locations
);
?>
|
|
|
__('Exclude On', 'king-addons'),
'value' => '[]',
'tags' => 'site,enable,target,pages',
'add_rule_label' => __('Add Exclusion Rule', 'king-addons'),
'rule_type' => 'exclude',
],
$exclude_locations
);
?>
|
|
|
__('Users', 'king-addons'),
'value' => '[]',
'tags' => 'site,enable,target,pages',
'add_rule_label' => __('Add User Rule', 'king-addons'),
],
$users
);
?>
|
array(
'label' => __('Basic', 'king-addons'),
'value' => array(
'all' => __('All', 'king-addons'),
'logged-in' => __('Logged In', 'king-addons'),
'logged-out' => __('Logged Out', 'king-addons'),
),
),
'advanced' => array(
'label' => __('Advanced', 'king-addons'),
'value' => array(),
),
);
/* User roles */
$roles = get_editable_roles();
foreach ($roles as $slug => $data) {
$selection_options['advanced']['value'][$slug] = $data['name'];
}
/**
* Filter options displayed in the user select field of Display conditions.
*
* @since 1.5.0
*/
return apply_filters('king-addons-el-hf_user_roles_list', $selection_options);
}
public static function target_user_role_settings_field($name, $settings, $value)
{
$input_name = $name;
$add_rule_label = $settings['add_rule_label'] ?? __('Add Rule', 'king-addons');
$saved_values = $value;
$output = '';
if (!isset(self::$user_selection) || empty(self::$user_selection)) {
self::$user_selection = self::get_user_selections();
}
$selection_options = self::$user_selection;
$output .= '';
/** @noinspection PhpConditionAlreadyCheckedInspection */
if (!is_array($saved_values) || (is_array($saved_values) && empty($saved_values))) {
$saved_values = array();
$saved_values[0] = '';
}
$index = 0;
$output .= '';
$output .= '
';
$output .= '
';
foreach ($saved_values as $index => $data) {
$output .= '
';
$output .= '
';
/* Condition Selection */
$output .= '
';
$output .= '';
$output .= '
';
$output .= '
';
}
$output .= '
';
/* Add new rule */
$output .= '
';
$output .= '
';
$output .= '
';
echo $output;
}
public static function target_rule_settings_field($name, $settings, $value)
{
$input_name = $name;
$rule_type = $settings['rule_type'] ?? 'target_rule';
$add_rule_label = $settings['add_rule_label'] ?? __('Add Rule', 'king-addons');
$saved_values = $value;
$output = '';
if (isset(self::$location_selection) || empty(self::$location_selection)) {
self::$location_selection = self::getLocationSelections();
}
$selection_options = self::$location_selection;
$output .= '';
$output .= '';
$output .= '
';
$output .= self::generate_target_rule_selector($rule_type, $selection_options, $input_name, $saved_values, $add_rule_label);
$output .= '
';
$output .= '
';
echo $output;
}
public static function generate_target_rule_selector($type, $selection_options, $input_name, $saved_values, $add_rule_label)
{
$output = '';
/** @noinspection PhpConditionAlreadyCheckedInspection */
if (!is_array($saved_values) || (is_array($saved_values) && empty($saved_values))) {
$saved_values = array();
$saved_values['rule'][0] = '';
$saved_values['specific'][0] = '';
}
$index = 0;
if (is_array($saved_values) && is_array($saved_values['rule'])) {
foreach ($saved_values['rule'] as $index => $data) {
$output .= '
';
$output .= '
';
$output .= '
';
$output .= '';
$output .= '
';
$output .= '
';
$output .= '
';
$output .= '';
$output .= '
';
}
}
$output .= '
';
$output .= '';
if ('display' == $type) {
$output .= '';
}
return $output;
}
function saveMetaboxData($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!isset($_POST['king_addons_el_hf_meta_nounce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['king_addons_el_hf_meta_nounce'])), 'king_addons_el_hf_meta_nounce')) {
return;
}
if (!current_user_can('edit_posts')) {
return;
}
if (!isset($_POST['king-addons-el-hf-target-rules-location'])) {
$target_locations = array(
'rule' => array('basic-global'),
'specific' => array(),
);
} else {
$target_locations = self::getFormatRuleValue($_POST, 'king-addons-el-hf-target-rules-location');
if (empty($target_locations)) {
$target_locations = array(
'rule' => array('basic-global'),
'specific' => array(),
);
}
}
$target_exclusion = self::getFormatRuleValue($_POST, 'king-addons-el-hf-target-rules-exclusion');
$target_users = [];
if (isset($_POST['king-addons-el-hf-target-rules-users'])) {
$target_users = array_map('sanitize_text_field', wp_unslash($_POST['king-addons-el-hf-target-rules-users']));
}
update_post_meta($post_id, 'king_addons_el_hf_target_include_locations', $target_locations);
update_post_meta($post_id, 'king_addons_el_hf_target_exclude_locations', $target_exclusion);
update_post_meta($post_id, 'king_addons_el_hf_target_user_roles', $target_users);
if (isset($_POST['king_addons_el_hf_template_type'])) {
update_post_meta($post_id, 'king_addons_el_hf_template_type', sanitize_text_field(wp_unslash($_POST['king_addons_el_hf_template_type'])));
}
if (isset($_POST['king-addons-el-hf-display-on-canvas'])) {
update_post_meta($post_id, 'king-addons-el-hf-display-on-canvas', sanitize_text_field(wp_unslash($_POST['king-addons-el-hf-display-on-canvas'])));
} else {
delete_post_meta($post_id, 'king-addons-el-hf-display-on-canvas');
}
}
function setCompatibility()
{
$template = get_template();
$is_elementor_callable = defined('ELEMENTOR_VERSION') && is_callable('Elementor\Plugin::instance');
if ($is_elementor_callable) {
self::$elementor_instance = Elementor\Plugin::instance();
// TODO: Add popular themes
switch ($template) {
case 'hello-elementor':
require_once(KING_ADDONS_PATH . 'includes/extensions/Header_Footer_Builder/themes/hello-elementor/ELHF_Hello_Elementor.php');
break;
default:
add_action('init', [$this, 'setupSettingsPage']);
add_filter('king_addons_el_hf_settings_tabs', [$this, 'setupUnsupportedTheme']);
add_action('init', [$this, 'setupFallbackSupport']);
break;
}
}
}
public function setupUnsupportedTheme($settings_tabs = [])
{
if (!current_theme_supports('king-addons-elementor-header-footer')) {
$settings_tabs['king_addons_el_hf_settings'] = [
'name' => esc_html__('Display Settings', 'king-addons'),
'url' => admin_url('edit.php?post_type=king-addons-el-hf&page=king-addons-el-hf-settings'),
];
}
return $settings_tabs;
}
public function setupFallbackSupport()
{
if (!current_theme_supports('king-addons-elementor-header-footer')) {
// Default to Method 3 (Universal) for best compatibility
$compatibility_option = get_option('king_addons_el_hf_compatibility_option', '3');
if ('1' === $compatibility_option) {
if (!class_exists('ELHF_Default_Method_1')) {
require_once(KING_ADDONS_PATH . 'includes/extensions/Header_Footer_Builder/themes/default/ELHF_Default_Method_1.php');
}
} elseif ('2' === $compatibility_option) {
if (!class_exists('ELHF_Default_Method_2')) {
require_once(KING_ADDONS_PATH . 'includes/extensions/Header_Footer_Builder/themes/default/ELHF_Default_Method_2.php');
}
} else {
// Method 3: Universal (combines all approaches for maximum compatibility)
if (!class_exists('ELHF_Default_Method_3')) {
require_once(KING_ADDONS_PATH . 'includes/extensions/Header_Footer_Builder/themes/default/ELHF_Default_Method_3.php');
}
}
}
}
function setupSettingsPage()
{
require_once(KING_ADDONS_PATH . 'includes/extensions/Header_Footer_Builder/ELHF_Settings_Page.php');
}
public static function renderHeader()
{
/** @noinspection SpellCheckingInspection */
echo '