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; } ?>

+
can_use_premium_code__premium_only()): ?>
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'); ?> display_rules_tab(); ?>

/>
__('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 .= '' . $add_rule_label . ''; $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 .= '' . $add_rule_label . ''; $output .= '
'; if ('display' == $type) { $output .= '
'; $output .= '' . __('Add Exclusion Rule', 'king-addons') . ''; $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 '
'; ?>'; } public static function renderFooter() { /** @noinspection SpellCheckingInspection */ echo ''; } public static function getHeaderContent() { $header_id = self::getHeaderID(); if ($header_id) { // Ensure Elementor-generated CSS is included on non-Elementor pages. $with_css = true; echo self::$elementor_instance->frontend->get_builder_content_for_display($header_id, $with_css); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } } public static function getFooterContent() { $footer_id = self::getFooterID(); if ($footer_id) { echo '
'; // Ensure Elementor-generated CSS is included on non-Elementor pages. $with_css = true; echo self::$elementor_instance->frontend->get_builder_content_for_display($footer_id, $with_css); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '
'; } } public static function getHeaderID() { $header_id = self::getSettings('king_addons_el_hf_type_header'); if ('' === $header_id) { $header_id = false; } return apply_filters('king_addons_el_hf_get_header_id', $header_id); } public static function isHeaderEnabled() { $header_id = self::getSettings('king_addons_el_hf_type_header'); $status = false; if ('' !== $header_id) { $status = true; } return apply_filters('king_addons_el_hf_header_enabled', $status); } public static function isFooterEnabled() { $footer_id = self::getSettings('king_addons_el_hf_type_footer'); $status = false; if ('' !== $footer_id) { $status = true; } return apply_filters('king_addons_el_hf_footer_enabled', $status); } public static function getFooterID() { $footer_id = self::getSettings('king_addons_el_hf_type_footer'); if ('' === $footer_id) { $footer_id = false; } return apply_filters('king_addons_el_hf_get_footer_id', $footer_id); } public static function getSettings($setting = '') { if ('king_addons_el_hf_type_header' == $setting || 'king_addons_el_hf_type_footer' == $setting) { $templates = self::getTemplateID($setting); $template = !is_array($templates) ? $templates : $templates[0]; return apply_filters("king_addons_el_hf_get_settings_$setting", $template); } return null; } public static function getTemplateID($type) { $option = [ 'location' => 'king_addons_el_hf_target_include_locations', 'exclusion' => 'king_addons_el_hf_target_exclude_locations', 'users' => 'king_addons_el_hf_target_user_roles', ]; $templates = self::getPostsByConditions('king-addons-el-hf', $option); // Prime meta cache to avoid N+1 queries when calling get_post_meta in loops below. // getPostsByConditions() may already have populated template IDs; caching is safe and cheap. if (!empty($templates)) { $template_ids = array_map(static function ($template) { return isset($template['id']) ? absint($template['id']) : 0; }, $templates); $template_ids = array_values(array_filter($template_ids)); if (!empty($template_ids)) { update_meta_cache('post', $template_ids); } } foreach ($templates as $template) { if (get_post_meta(absint($template['id']), 'king_addons_el_hf_template_type', true) === $type) { // Polylang check - https://polylang.pro/doc/function-reference/ if (function_exists('pll_current_language')) { if (pll_current_language('slug') == pll_get_post_language($template['id'], 'slug')) { return $template['id']; } } else { return $template['id']; } } } return ''; } public static function getPostsByConditions($post_type, $option) { global $wpdb; global $post; // Security fix: Validate and sanitize post_type // Ensure $post is valid before accessing its properties if ($post_type) { $post_type = sanitize_key($post_type); } elseif ($post instanceof \WP_Post && !empty($post->post_type)) { $post_type = sanitize_key($post->post_type); } else { return []; } if (empty($post_type)) { return []; } if (is_array(self::$current_page_data) && isset(self::$current_page_data[$post_type])) { return apply_filters('king_addons_el_hf_get_display_posts_by_conditions', self::$current_page_data[$post_type], $post_type); } $current_page_type = self::getCurrentPageType(); self::$current_page_data[$post_type] = array(); $option['current_post_id'] = self::$current_page_data['ID']; $meta_header = self::getMetaOptionPost($post_type, $option); if (false === $meta_header) { $current_post_type = sanitize_key(get_post_type()); $current_post_id = false; $q_obj = get_queried_object(); $current_id = absint(get_the_id()); // Check if WPML is active. Find WPML Object ID for current page. /** @noinspection SpellCheckingInspection */ if (defined('ICL_SITEPRESS_VERSION')) { $default_lang = apply_filters('wpml_default_language', ''); $current_lang = apply_filters('wpml_current_language', ''); if ($default_lang !== $current_lang) { $current_post_type = get_post_type($current_id); $current_id = apply_filters('wpml_object_id', $current_id, $current_post_type, true, $default_lang); } } // Security fix: Sanitize location parameter $location = isset($option['location']) ? sanitize_key($option['location']) : ''; if (empty($location)) { return []; } // Security fix: Use prepared statement to prevent SQL injection $query = $wpdb->prepare( "SELECT p.ID, pm.meta_value FROM {$wpdb->postmeta} as pm INNER JOIN {$wpdb->posts} as p ON pm.post_id = p.ID WHERE pm.meta_key = %s AND p.post_type = %s AND p.post_status = 'publish'", $location, $post_type ); $orderby = ' ORDER BY p.post_date DESC'; // Security fix: Build meta_args using safe placeholders and prepared statements $meta_conditions = ["pm.meta_value LIKE %s"]; $meta_values = ['%"basic-global"%']; switch ($current_page_type) { case 'is_404': $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"special-404"%'; break; case 'is_search': $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"special-search"%'; break; case 'is_archive': case 'is_tax': case 'is_date': case 'is_author': $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"basic-archives"%'; $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"' . sanitize_key($current_post_type) . '|all|archive"%'; if ('is_tax' == $current_page_type && (is_category() || is_tag() || is_tax())) { if (is_object($q_obj) && isset($q_obj->taxonomy) && isset($q_obj->term_id)) { $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"' . sanitize_key($current_post_type) . '|all|taxarchive|' . sanitize_key($q_obj->taxonomy) . '"%'; $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"tax-' . absint($q_obj->term_id) . '"%'; } } elseif ('is_date' == $current_page_type) { $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"special-date"%'; } elseif ('is_author' == $current_page_type) { $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"special-author"%'; } break; case 'is_home': $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"special-blog"%'; break; case 'is_front_page': $current_post_id = $current_id; $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"special-front"%'; $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"' . sanitize_key($current_post_type) . '|all"%'; $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"post-' . absint($current_id) . '"%'; break; case 'is_singular': $current_post_id = $current_id; $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"basic-singulars"%'; $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"' . sanitize_key($current_post_type) . '|all"%'; $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"post-' . absint($current_id) . '"%'; if (is_object($q_obj) && isset($q_obj->post_type) && isset($q_obj->ID)) { $taxonomies = get_object_taxonomies($q_obj->post_type); $terms = wp_get_post_terms($q_obj->ID, $taxonomies); foreach ($terms as $term) { if (isset($term->term_id) && isset($term->taxonomy)) { $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"tax-' . absint($term->term_id) . '-single-' . sanitize_key($term->taxonomy) . '"%'; } } } break; case 'is_woo_shop_page': if (function_exists('is_shop')) { $meta_conditions[] = "pm.meta_value LIKE %s"; $meta_values[] = '%"special-woocommerce-shop"%'; } break; case '': $current_post_id = $current_id; break; } // Build the final meta_args string using prepare $meta_args = '(' . implode(' OR ', $meta_conditions) . ')'; // Security fix: Use prepared statement for the complete query $full_query = $wpdb->prepare( $query . ' AND ' . $meta_args . $orderby, ...$meta_values ); $posts = $wpdb->get_results($full_query); foreach ($posts as $local_post) { $unserialized_location = maybe_unserialize($local_post->meta_value); if ($unserialized_location !== false) { self::$current_page_data[$post_type][$local_post->ID] = array( 'id' => $local_post->ID, 'location' => $unserialized_location, ); } } // Prime meta cache for all candidate templates to avoid repeated get_post_meta queries // in removeExclusionRulePosts/removeUserRulePosts and getTemplateID. if (!empty(self::$current_page_data[$post_type])) { $candidate_ids = array_map('absint', array_keys(self::$current_page_data[$post_type])); $candidate_ids = array_values(array_filter($candidate_ids)); if (!empty($candidate_ids)) { update_meta_cache('post', $candidate_ids); } } $option['current_post_id'] = $current_post_id; self::removeExclusionRulePosts($post_type, $option); self::removeUserRulePosts($post_type, $option); } return apply_filters('king_addons_el_hf_get_display_posts_by_conditions', self::$current_page_data[$post_type], $post_type); } public static function getCurrentPageType(): ?string { if (null === self::$current_page_type) { $page_type = ''; $current_id = false; if (is_404()) { $page_type = 'is_404'; } elseif (is_search()) { $page_type = 'is_search'; } elseif (is_archive()) { $page_type = 'is_archive'; if (is_category() || is_tag() || is_tax()) { $page_type = 'is_tax'; } elseif (is_date()) { $page_type = 'is_date'; } elseif (is_author()) { $page_type = 'is_author'; } elseif (function_exists('is_shop')) { /** @noinspection PhpUndefinedFunctionInspection */ if (is_shop()) { $page_type = 'is_woo_shop_page'; } } } elseif (is_home()) { $page_type = 'is_home'; } elseif (is_front_page()) { $page_type = 'is_front_page'; $current_id = get_the_id(); } elseif (is_singular()) { $page_type = 'is_singular'; $current_id = get_the_id(); } else { $current_id = get_the_id(); } self::$current_page_data['ID'] = $current_id; self::$current_page_type = $page_type; } return self::$current_page_type; } public static function getMetaOptionPost($post_type, $option) { $page_meta = (isset($option['page_meta']) && '' != $option['page_meta']) ? $option['page_meta'] : false; if (false !== $page_meta) { $current_post_id = $option['current_post_id'] ?? false; $meta_id = get_post_meta($current_post_id, $option['page_meta'], true); if (false !== $meta_id && '' != $meta_id) { self::$current_page_data[$post_type][$meta_id] = array( 'id' => $meta_id, 'location' => '', ); return self::$current_page_data[$post_type]; } } return false; } public static function removeExclusionRulePosts($post_type, $option) { $exclusion = $option['exclusion'] ?? ''; $current_post_id = $option['current_post_id'] ?? false; foreach (self::$current_page_data[$post_type] as $c_post_id => $c_data) { $exclusion_rules = get_post_meta($c_post_id, $exclusion, true); $is_exclude = self::parseLayoutDisplayCondition($current_post_id, $exclusion_rules); if ($is_exclude) { unset(self::$current_page_data[$post_type][$c_post_id]); } } } public static function removeUserRulePosts($post_type, $option) { $users = $option['users'] ?? ''; foreach (self::$current_page_data[$post_type] as $c_post_id => $c_data) { $user_rules = get_post_meta($c_post_id, $users, true); $is_user = self::parseUserRoleCondition($user_rules); if (!$is_user) { unset(self::$current_page_data[$post_type][$c_post_id]); } } } public static function parseLayoutDisplayCondition($post_id, $rules): bool { $display = false; /** @noinspection PhpConditionCheckedByNextConditionInspection */ if (isset($rules['rule']) && is_array($rules['rule']) && !empty($rules['rule'])) { foreach ($rules['rule'] as $rule) { if (strrpos($rule, 'all') !== false) { $rule_case = 'all'; } else { $rule_case = $rule; } switch ($rule_case) { case 'basic-global': $display = true; break; case 'basic-singulars': if (is_singular()) { $display = true; } break; case 'basic-archives': if (is_archive()) { $display = true; } break; case 'special-404': if (is_404()) { $display = true; } break; case 'special-search': if (is_search()) { $display = true; } break; case 'special-blog': if (is_home()) { $display = true; } break; case 'special-front': if (is_front_page()) { $display = true; } break; case 'special-date': if (is_date()) { $display = true; } break; case 'special-author': if (is_author()) { $display = true; } break; case 'special-woocommerce-shop': if (function_exists('is_shop')) { if (is_shop()) { $display = true; } } break; case 'all': $rule_data = explode('|', $rule); $post_type = $rule_data[0] ?? false; $archive_type = $rule_data[2] ?? false; $taxonomy = $rule_data[3] ?? false; if (false === $archive_type) { $current_post_type = get_post_type($post_id); if (false !== $post_id && $current_post_type == $post_type) { $display = true; } } else { if (is_archive()) { $current_post_type = get_post_type(); if ($current_post_type == $post_type) { if ('archive' == $archive_type) { $display = true; } elseif ('taxarchive' == $archive_type) { $obj = get_queried_object(); $current_taxonomy = ''; if ('' !== $obj && null !== $obj) { $current_taxonomy = $obj->taxonomy; } if ($current_taxonomy == $taxonomy) { $display = true; } } } } } break; case 'specifics': if (isset($rules['specific']) && is_array($rules['specific'])) { foreach ($rules['specific'] as $specific_page) { $specific_data = explode('-', $specific_page); $specific_post_type = $specific_data[0] ?? false; $specific_post_id = $specific_data[1] ?? false; if ('post' == $specific_post_type) { if ($specific_post_id == $post_id) { $display = true; } } elseif (isset($specific_data[2]) && ('single' == $specific_data[2]) && 'tax' == $specific_post_type) { if (is_singular()) { $term_details = get_term($specific_post_id); if (isset($term_details->taxonomy)) { $has_term = has_term((int)$specific_post_id, $term_details->taxonomy, $post_id); if ($has_term) { $display = true; } } } } elseif ('tax' == $specific_post_type) { $tax_id = get_queried_object_id(); if ($specific_post_id == $tax_id) { $display = true; } } } } break; default: break; } if ($display) { break; } } } return $display; } public static function parseUserRoleCondition($rules): bool { $show_popup = true; if (is_array($rules) && !empty($rules)) { $show_popup = false; foreach ($rules as $rule) { switch ($rule) { case '': case 'all': $show_popup = true; break; case 'logged-in': if (is_user_logged_in()) { $show_popup = true; } break; case 'logged-out': if (!is_user_logged_in()) { $show_popup = true; } break; default: if (is_user_logged_in()) { $current_user = wp_get_current_user(); if (isset($current_user->roles) && is_array($current_user->roles) && in_array($rule, $current_user->roles) ) { $show_popup = true; } } break; } if ($show_popup) { break; } } } return $show_popup; } public static function checkUserCanEdit() { if (is_singular('king-addons-el-hf') && !current_user_can('edit_posts')) { wp_redirect(site_url(), 301); die; } } public static function loadElementorCanvasTemplate($single_template) { global $post; // Safety check: ensure $post is a valid WP_Post object before accessing properties if (!$post instanceof \WP_Post) { return $single_template; } if ('king-addons-el-hf' === $post->post_type) { if (defined('ELEMENTOR_VERSION')) { $elementor_2_0_canvas = ELEMENTOR_PATH . '/modules/page-templates/templates/canvas.php'; if (file_exists($elementor_2_0_canvas)) { return $elementor_2_0_canvas; } else { return ELEMENTOR_PATH . '/includes/page-templates/canvas.php'; } } } return $single_template; } public function forceElementorCanvasTemplate($template) { if (!defined('ELEMENTOR_VERSION')) { return $template; } $post_id = 0; if (is_singular('king-addons-el-hf')) { $post_id = (int) get_queried_object_id(); } elseif (isset($_GET['elementor-preview'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $post_id = (int) $_GET['elementor-preview']; } if ($post_id && 'king-addons-el-hf' === get_post_type($post_id)) { $elementor_2_0_canvas = ELEMENTOR_PATH . '/modules/page-templates/templates/canvas.php'; if (file_exists($elementor_2_0_canvas)) { return $elementor_2_0_canvas; } return ELEMENTOR_PATH . '/includes/page-templates/canvas.php'; } return $template; } public function forcePreviewQuery($query): void { if (is_admin() || !$query->is_main_query()) { return; } if (!isset($_GET['elementor-preview'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return; } $preview_id = (int) $_GET['elementor-preview']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if (!$preview_id || 'king-addons-el-hf' !== get_post_type($preview_id)) { return; } $query->set('p', $preview_id); $query->set('post_type', 'king-addons-el-hf'); $query->set('post_status', ['publish', 'draft', 'pending', 'private']); } public static function enqueueScripts(): void { $screen = get_current_screen(); if ($screen->id === 'edit-king-addons-el-hf') { // todo - styles // wp_enqueue_style('king-addons-el-hf-style', KING_ADDONS_URL . 'includes/extensions/Header_Footer_Builder/header-footer-builder.css', '', KING_ADDONS_VERSION); wp_enqueue_style('king-addons-el-hf-style', KING_ADDONS_URL . 'includes/extensions/Header_Footer_Builder/admin.css', '', KING_ADDONS_VERSION); } } public static function columnHeadings($columns) { unset($columns['date']); $columns['king_addons_el_hf_edit_template'] = esc_html__('Edit Template', 'king-addons'); $columns['king_addons_el_hf_type_of_template'] = esc_html__('Type of Template', 'king-addons'); $columns['king_addons_el_hf_display_rules'] = esc_html__('Display Rules', 'king-addons'); $columns['date'] = esc_html__('Date', 'king-addons'); return $columns; } public static function columnContent($column, $post_id) { // Edit Template if ('king_addons_el_hf_edit_template' === $column) { echo '' . esc_html__('Edit Template', 'king-addons') . ''; } // Display Rules if ('king_addons_el_hf_display_rules' === $column) { $locations = get_post_meta($post_id, 'king_addons_el_hf_target_include_locations', true); if (!empty($locations)) { echo '
'; echo ''; echo esc_html__('Display: ', 'king-addons'); echo ''; self::columnDisplayLocation($locations); echo '
'; } $locations = get_post_meta($post_id, 'king_addons_el_hf_target_exclude_locations', true); if (!empty($locations)) { echo '
'; echo ''; echo esc_html__('Exclusion: ', 'king-addons'); echo ''; self::columnDisplayLocation($locations); echo '
'; } $users = get_post_meta($post_id, 'king_addons_el_hf_target_user_roles', true); if (isset($users) && is_array($users)) { if (!empty($users[0])) { $user_label = []; foreach ($users as $user) { $user_label[] = self::get_user_by_key($user); } echo '
'; echo 'Users: '; echo esc_html(join(', ', $user_label)); echo '
'; } } } // Type of Template if ('king_addons_el_hf_type_of_template' === $column) { $template_type = get_post_meta($post_id, 'king_addons_el_hf_template_type', true); if (!empty($template_type)) { echo '
'; echo ''; switch ($template_type) { case 'king_addons_el_hf_type_header': echo esc_html__('Header', 'king-addons'); break; case 'king_addons_el_hf_type_footer': echo esc_html__('Footer', 'king-addons'); break; default: echo esc_html__('Not selected', 'king-addons'); break; } echo ''; echo '
'; } } } public static function get_user_by_key($key) { if (!isset(self::$user_selection) || empty(self::$user_selection)) { self::$user_selection = self::get_user_selections(); } $user_selection = self::$user_selection; if (isset($user_selection['basic']['value'][$key])) { return $user_selection['basic']['value'][$key]; } elseif ($user_selection['advanced']['value'][$key]) { return $user_selection['advanced']['value'][$key]; } return $key; } public static function columnDisplayLocation($locations) { $location_label = []; /** @noinspection PhpConditionAlreadyCheckedInspection */ if (is_array($locations) && is_array($locations['rule']) && isset($locations['rule'])) { /** @noinspection PhpArraySearchInBooleanContextInspection */ $index = array_search('specifics', $locations['rule']); /** @noinspection PhpConditionCheckedByNextConditionInspection */ if (false !== $index && !empty($index)) { unset($locations['rule'][$index]); } } if (isset($locations['rule']) && is_array($locations['rule'])) { foreach ($locations['rule'] as $location) { $location_label[] = self::getLocation($location); } } if (isset($locations['specific']) && is_array($locations['specific'])) { foreach ($locations['specific'] as $location) { $location_label[] = self::getLocation($location); } } echo esc_html(join(', ', $location_label)); } public static function getLocation($key) { if (!isset(self::$location_selection) || empty(self::$location_selection)) { self::$location_selection = self::getLocationSelections(); } $location_selection = self::$location_selection; foreach ($location_selection as $location_grp) { if (isset($location_grp['value'][$key])) { return $location_grp['value'][$key]; } } if (strpos($key, 'post-') !== false) { $post_id = (int)str_replace('post-', '', $key); return get_the_title($post_id); } if (strpos($key, 'tax-') !== false) { $tax_id = (int)str_replace('tax-', '', $key); $term = get_term($tax_id); if (!is_wp_error($term)) { $term_taxonomy = ucfirst(str_replace('_', ' ', $term->taxonomy)); return $term->name . ' - ' . $term_taxonomy; } else { return ''; } } return $key; } public static function getLocationSelections() { $args = array( 'public' => true, '_builtin' => true, ); $post_types = get_post_types($args, 'objects'); unset($post_types['attachment']); $args['_builtin'] = false; $custom_post_type = get_post_types($args, 'objects'); $post_types = apply_filters('king_addons_el_hf_location_rule_post_types', array_merge($post_types, $custom_post_type)); if (!king_addons_freemius()->can_use_premium_code__premium_only()) { $special_pages = array( 'special-404-none' => esc_html__('404 Page (Available in PRO)', 'king-addons'), 'special-search' => esc_html__('Search Page', 'king-addons'), 'special-blog-none' => esc_html__('Blog / Posts Page (Available in PRO)', 'king-addons'), 'special-front' => esc_html__('Front Page', 'king-addons'), 'special-date' => esc_html__('Date Archive', 'king-addons'), 'special-author' => esc_html__('Author Archive', 'king-addons'), ); } else { $special_pages = array( 'special-404' => esc_html__('404 Page', 'king-addons'), 'special-search' => esc_html__('Search Page', 'king-addons'), 'special-blog' => esc_html__('Blog / Posts Page', 'king-addons'), 'special-front' => esc_html__('Front Page', 'king-addons'), 'special-date' => esc_html__('Date Archive', 'king-addons'), 'special-author' => esc_html__('Author Archive', 'king-addons'), ); } if (class_exists('WooCommerce')) { if (!king_addons_freemius()->can_use_premium_code__premium_only()) { $special_pages['special-woocommerce-shop-none'] = esc_html__('WooCommerce Shop Page (Available in PRO)', 'king-addons'); } else { $special_pages['special-woocommerce-shop'] = esc_html__('WooCommerce Shop Page', 'king-addons'); } } $selection_options = array( 'basic' => array( 'label' => esc_html__('Basic', 'king-addons'), 'value' => array( 'basic-global' => esc_html__('Entire Website', 'king-addons'), 'basic-singulars' => esc_html__('All Singulars', 'king-addons'), 'basic-archives' => esc_html__('All Archives', 'king-addons'), ), ), 'special-pages' => array( 'label' => esc_html__('Special Pages', 'king-addons'), 'value' => $special_pages, ), ); if (!king_addons_freemius()->can_use_premium_code__premium_only()) { $selection_options['specific-target'] = array( 'label' => esc_html__('Specific Target', 'king-addons'), 'value' => array( 'specifics-none' => esc_html__('Specific Pages / Posts / Taxonomies, etc. (Available in PRO)', 'king-addons'), ), ); } else { $selection_options['specific-target'] = array( 'label' => esc_html__('Specific Target', 'king-addons'), 'value' => array( 'specifics' => esc_html__('Specific Pages / Posts / Taxonomies, etc.', 'king-addons'), ), ); } $args = array( 'public' => true, ); $taxonomies = get_taxonomies($args, 'objects'); if (!empty($taxonomies)) { foreach ($taxonomies as $taxonomy) { if ('post_format' == $taxonomy->name) { continue; } foreach ($post_types as $post_type) { $post_opt = self::getPostTargetRuleOptions($post_type, $taxonomy); if (isset($selection_options[$post_opt['post_key']])) { if (!empty($post_opt['value']) && is_array($post_opt['value'])) { foreach ($post_opt['value'] as $key => $value) { if (!in_array($value, $selection_options[$post_opt['post_key']]['value'])) { $selection_options[$post_opt['post_key']]['value'][$key] = $value; } } } } else { $selection_options[$post_opt['post_key']] = array( 'label' => $post_opt['label'], 'value' => $post_opt['value'], ); } } } } return apply_filters('king_addons_el_hf_display_on_list', $selection_options); } public static function getPostTargetRuleOptions($post_type, $taxonomy): array { $post_key = str_replace(' ', '-', strtolower($post_type->label)); $post_label = ucwords($post_type->label); $post_name = $post_type->name; $post_option = array(); /* translators: %s is post label */ $all_posts = sprintf(esc_html__('All %s', 'king-addons'), $post_label); $post_option[$post_name . '|all'] = $all_posts; if ('pages' != $post_key) { /* translators: %s is post label */ $all_archive = sprintf(esc_html__('All %s Archive', 'king-addons'), $post_label); $post_option[$post_name . '|all|archive'] = $all_archive; } if (in_array($post_type->name, $taxonomy->object_type)) { $tax_label = ucwords($taxonomy->label); $tax_name = $taxonomy->name; /* translators: %s is taxonomy label */ $tax_archive = sprintf(esc_html__('All %s Archive', 'king-addons'), $tax_label); $post_option[$post_name . '|all|taxarchive|' . $tax_name] = $tax_archive; } $post_output['post_key'] = $post_key; $post_output['label'] = $post_label; $post_output['value'] = $post_option; return $post_output; } public static function getFormatRuleValue($save_data, $key): array { $meta_value = array(); if (isset($save_data[$key]['rule'])) { $save_data[$key]['rule'] = array_unique($save_data[$key]['rule']); if (isset($save_data[$key]['specific'])) { $save_data[$key]['specific'] = array_unique($save_data[$key]['specific']); } $index = array_search('', $save_data[$key]['rule']); if (false !== $index) { unset($save_data[$key]['rule'][$index]); } $index = array_search('specifics', $save_data[$key]['rule']); if (false !== $index) { unset($save_data[$key]['rule'][$index]); if (isset($save_data[$key]['specific']) && is_array($save_data[$key]['specific'])) { $save_data[$key]['rule'][] = 'specifics'; } } foreach ($save_data[$key] as $meta_key => $value) { if (!empty($value)) { $meta_value[$meta_key] = array_map('esc_attr', $value); } } if (!isset($meta_value['rule']) || !in_array('specifics', $meta_value['rule'])) { $meta_value['specific'] = array(); } if (empty($meta_value['rule'])) { $meta_value = array(); } } return $meta_value; } }