PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.14
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.14
51.1.87 51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 All 41 releases
king-addons / includes / extensions / Header_Footer_Builder / Header_Footer_Builder.php

Header_Footer_Builder.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.14, at includes/extensions/Header_Footer_Builder/Header_Footer_Builder.php

1,738 lines 70.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php /** @noinspection PhpMissingFieldTypeInspection, DuplicatedCode */
2
3 namespace King_Addons;
4
5 use Elementor;
6 use WP_Query;
7
8 if (!defined('ABSPATH')) {
9 exit;
10 }
11
12 final class Header_Footer_Builder
13 {
14 private static ?Header_Footer_Builder $instance = null;
15 private static ?string $current_page_type = null;
16 private static array $current_page_data = array();
17 private static $location_selection;
18 private static $user_selection;
19 private static $elementor_instance;
20
21 public static function instance(): ?Header_Footer_Builder
22 {
23 if (is_null(self::$instance)) {
24 self::$instance = new self();
25 }
26 return self::$instance;
27 }
28
29 public function __construct()
30 {
31 add_action('init', [$this, 'addPostType']);
32 add_action('admin_notices', [$this, 'renderNoticeZeroPosts']);
33 add_action('in_admin_header', [$this, 'renderAdminCustomHeader']);
34 add_action('add_meta_boxes', [$this, 'registerMetabox']);
35 add_action('save_post', [$this, 'saveMetaboxData']);
36 add_action('template_redirect', [$this, 'checkUserCanEdit']);
37 add_filter('screen_options_show_screen', [$this, 'disableScreenOptions'], 10, 2);
38
39 require_once(KING_ADDONS_PATH . 'includes/extensions/Header_Footer_Builder/ELHF_Render_On_Canvas.php');
40 add_filter('single_template', [$this, 'loadElementorCanvasTemplate']);
41
42 self::setCompatibility();
43 add_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
44 add_action('admin_action_edit', array($this, 'initialize_options'));
45 add_action('wp_ajax_king_addons_el_hf_get_posts_by_query', array($this, 'king_addons_el_hf_get_posts_by_query'));
46
47 if (is_admin()) {
48 add_action('manage_king-addons-el-hf_posts_custom_column', [$this, 'columnContent'], 10, 2);
49 add_filter('manage_king-addons-el-hf_posts_columns', [$this, 'columnHeadings']);
50 }
51 }
52
53 /**
54 * Show an admin notice when there are zero "king-addons-el-hf" posts
55 */
56 function renderNoticeZeroPosts()
57 {
58 global $pagenow, $post_type;
59
60 // Check if we are on the "All posts" page for the custom post type
61 if ('edit.php' === $pagenow && 'edit-king-addons-el-hf' === $post_type) {
62
63 // Count published posts of this CPT
64 $count_posts = wp_count_posts('king-addons-el-hf');
65 if ($count_posts->publish == 0) {
66 echo '<div class="notice notice-info is-dismissible">';
67 echo '<p>';
68 echo esc_html__("Create the first header or footer by clicking the 'Create New' button above.", 'king addons');
69 echo '</p>';
70 echo '</div>';
71 }
72 }
73 }
74
75 public function disableScreenOptions($show_screen, $screen)
76 {
77 if ($screen->id === 'edit-king-addons-el-hf') {
78 return false;
79 }
80 return $show_screen;
81 }
82
83 function king_addons_el_hf_get_posts_by_query()
84 {
85
86 check_ajax_referer('king-addons-el-hf-get-posts-by-query', 'nonce');
87
88 $search_string = isset($_POST['q']) ? sanitize_text_field($_POST['q']) : '';
89 $result = array();
90
91 $args = array(
92 'public' => true,
93 '_builtin' => false,
94 );
95
96 $output = 'names';
97 $operator = 'and';
98 $post_types = get_post_types($args, $output, $operator);
99
100 unset($post_types['elementor-hf']);
101
102 $post_types['Posts'] = 'post';
103 $post_types['Pages'] = 'page';
104
105 foreach ($post_types as $key => $post_type) {
106 $data = array();
107
108 add_filter('posts_search', array($this, 'search_only_titles'), 10, 2);
109
110 $query = new WP_Query(
111 array(
112 's' => $search_string,
113 'post_type' => $post_type,
114 'posts_per_page' => -1,
115 )
116 );
117
118 if ($query->have_posts()) {
119 while ($query->have_posts()) {
120 $query->the_post();
121 $title = get_the_title();
122 $title .= (0 != $query->post->post_parent) ? ' (' . get_the_title($query->post->post_parent) . ')' : '';
123 $id = get_the_id();
124 $data[] = array(
125 'id' => 'post-' . $id,
126 'text' => $title,
127 );
128 }
129 }
130
131 if (is_array($data) && !empty($data)) {
132 $result[] = array(
133 'text' => $key,
134 'children' => $data,
135 );
136 }
137 }
138
139 wp_reset_postdata();
140
141 $args = array(
142 'public' => true,
143 );
144
145 $output = 'objects';
146 $taxonomies = get_taxonomies($args, $output, $operator);
147
148 foreach ($taxonomies as $taxonomy) {
149 $terms = get_terms(
150 $taxonomy->name,
151 array(
152 'orderby' => 'count',
153 'hide_empty' => 0,
154 'name__like' => $search_string,
155 )
156 );
157
158 $data = array();
159
160 $label = ucwords($taxonomy->label);
161
162 if (!empty($terms)) {
163 foreach ($terms as $term) {
164
165 $data[] = array(
166 'id' => 'tax-' . $term->term_id,
167 'text' => $term->name . ' archive page',
168 );
169
170 $data[] = array(
171 'id' => 'tax-' . $term->term_id . '-single-' . $taxonomy->name,
172 'text' => 'All singulars from ' . $term->name,
173 );
174 }
175 }
176
177 if (is_array($data) && !empty($data)) {
178 $result[] = array(
179 'text' => $label,
180 'children' => $data,
181 );
182 }
183 }
184
185 wp_send_json($result);
186 }
187
188 public function initialize_options()
189 {
190 self::$user_selection = self::get_user_selections();
191 self::$location_selection = self::getLocationSelections();
192 }
193
194 public function renderAdminCustomHeader()
195 {
196 $current_screen = get_current_screen()->id;
197 if ($current_screen !== 'edit-king-addons-el-hf'
198 && $current_screen !== 'header-footer_page_king-addons-el-hf-settings') {
199 return;
200 }
201
202 ?>
203 <div class="king-addons-pb-settings-page-header">
204 <h1><?php esc_html_e('Elementor Header & Footer Builder', 'king-addons'); ?></h1>
205 <p>
206 <?php esc_html_e('Create fully customizable headers and footers with display conditions to control where they appear', 'king-addons'); ?>
207 </p>
208 <div class="king-addons-pb-preview-buttons">
209 <a href="<?php echo admin_url('post-new.php?post_type=king-addons-el-hf'); ?>">
210 <div class="king-addons-pb-user-template">
211 <span><?php esc_html_e('Create New', 'king-addons'); ?></span>
212 <span class="plus-icon">+</span>
213 </div>
214 </a>
215 <?php if (!king_addons_freemius()->can_use_premium_code__premium_only()): ?>
216 <div class="kng-promo-btn-wrap">
217 <a href="https://kingaddons.com/pricing/?rel=king-addons-hf-builder" target="_blank">
218 <div class="kng-promo-btn-txt">
219 <?php esc_html_e('Unlock Premium Features & 650+ Templates Today!', 'king-addons'); ?>
220 </div>
221 <img width="16px"
222 src="<?php echo esc_url(KING_ADDONS_URL) . 'includes/admin/img/share-v2.svg'; ?>"
223 alt="<?php echo esc_html__('Open link in the new tab', 'king-addons'); ?>">
224 </a>
225 </div>
226 <?php endif; ?>
227 </div>
228 </div>
229 <?php
230
231 $counts = wp_count_posts('king-addons-el-hf');
232 $total = (int)$counts->publish + (int)$counts->draft;
233
234 if (0 === $total) {
235 echo '<div class="notice notice-info">';
236 echo '<p>';
237 echo esc_html__("Create the first header or footer by clicking the 'Create New' button above.", 'king addons');
238 echo '</p>';
239 echo '</div>';
240 }
241
242 }
243
244 function addPostType(): void
245 {
246 if (!current_user_can('manage_options')) {
247 return;
248 }
249
250 $labels = [
251 'name' => esc_html__('Elementor Header & Footer Builder', 'king-addons'),
252 'singular_name' => esc_html__('Elementor Header & Footer Builder', 'king-addons'),
253 'menu_name' => esc_html__('Elementor Header & Footer Builder', 'king-addons'),
254 'name_admin_bar' => esc_html__('Elementor Header & Footer Builder', 'king-addons'),
255 'add_new' => esc_html__('Add New', 'king-addons'),
256 'add_new_item' => esc_html__('Add New', 'king-addons'),
257 'new_item' => esc_html__('New Template', 'king-addons'),
258 'edit_item' => esc_html__('Edit Template', 'king-addons'),
259 'view_item' => esc_html__('View Template', 'king-addons'),
260 'all_items' => esc_html__('All Templates', 'king-addons'),
261 'search_items' => esc_html__('Search Templates', 'king-addons'),
262 'parent_item_colon' => esc_html__('Parent Templates:', 'king-addons'),
263 'not_found' => esc_html__('No Templates found.', 'king-addons'),
264 'not_found_in_trash' => esc_html__('No Templates found in Trash.', 'king-addons'),
265 ];
266
267 $args = [
268 'labels' => $labels,
269 'public' => true,
270 'show_ui' => true,
271 'show_in_menu' => false,
272 'show_in_nav_menus' => false,
273 'exclude_from_search' => true,
274 'capability_type' => 'post',
275 'hierarchical' => false,
276 'menu_icon' => 'dashicons-editor-kitchensink',
277 'supports' => ['title', 'thumbnail', 'elementor'],
278 'show_in_rest' => true,
279 ];
280
281 register_post_type('king-addons-el-hf', $args);
282
283 if (false === get_option('king_addons_HFB_flushed_rewrite_rules')) {
284 add_option('king_addons_HFB_flushed_rewrite_rules', true);
285 flush_rewrite_rules();
286 }
287 }
288
289 function registerMetabox()
290 {
291 add_meta_box(
292 'king-addons-el-hf-meta-box',
293 esc_html__('Elementor Header & Footer Builder Options', 'king-addons'),
294 [$this, 'renderMetabox'],
295 'king-addons-el-hf',
296 'normal',
297 'high'
298 );
299 }
300
301 function renderMetabox($post)
302 {
303 $values = get_post_custom($post->ID);
304 $template_type = isset($values['king_addons_el_hf_template_type']) ? esc_attr(sanitize_text_field($values['king_addons_el_hf_template_type'][0])) : '';
305 $display_on_canvas = isset($values['king-addons-el-hf-display-on-canvas']);
306
307 wp_nonce_field('king_addons_el_hf_meta_nounce', 'king_addons_el_hf_meta_nounce');
308 ?>
309 <table class="king-addons-el-hf-options-table widefat">
310 <tbody>
311 <tr class="king-addons-el-hf-options-row type-of-template">
312 <td class="king-addons-el-hf-options-row-heading">
313 <label for="king_addons_el_hf_template_type"><strong><?php esc_html_e('Type of Template', 'king-addons'); ?></strong></label>
314 </td>
315 <td class="king-addons-el-hf-options-row-content">
316 <select name="king_addons_el_hf_template_type" id="king_addons_el_hf_template_type">
317 <option value="king_addons_el_hf_not_selected" <?php selected($template_type, ''); ?>><?php esc_html_e('Select Option', 'king-addons'); ?></option>
318 <option value="king_addons_el_hf_type_header" <?php selected($template_type, 'king_addons_el_hf_type_header'); ?>><?php esc_html_e('Header', 'king-addons'); ?></option>
319 <option value="king_addons_el_hf_type_footer" <?php selected($template_type, 'king_addons_el_hf_type_footer'); ?>><?php esc_html_e('Footer', 'king-addons'); ?></option>
320 </select>
321 </td>
322 </tr>
323 <?php
324 $this->display_rules_tab();
325
326 ?>
327 <tr class="king-addons-el-hf-options-row enable-for-canvas">
328 <td class="king-addons-el-hf-options-row-heading">
329 <label for="king-addons-el-hf-display-on-canvas">
330 <strong><?php esc_html_e('Enable Layout for Elementor Canvas Template?', 'king-addons'); ?></strong>
331 </label>
332 <p><?php esc_html_e('Enabling this option will display this layout on pages using Elementor Canvas Template', 'king-addons'); ?></p>
333 </td>
334 <td class="king-addons-el-hf-options-row-content">
335 <input type="checkbox" id="king-addons-el-hf-display-on-canvas"
336 name="king-addons-el-hf-display-on-canvas"
337 value="1" <?php checked($display_on_canvas); ?> />
338 </td>
339 </tr>
340 </tbody>
341 </table>
342 <?php
343 }
344
345
346 public function admin_styles()
347 {
348 wp_enqueue_script('king-addons-el-hf-select2', KING_ADDONS_URL . 'includes/extensions/Header_Footer_Builder/select2.js', array('jquery'), KING_ADDONS_VERSION, true);
349
350 wp_register_script(
351 'king-addons-el-hf-target-rule',
352 KING_ADDONS_URL . 'includes/extensions/Header_Footer_Builder/conditions-target.js',
353 array(
354 'jquery',
355 'king-addons-el-hf-select2',
356 ),
357 KING_ADDONS_VERSION,
358 true
359 );
360
361 wp_enqueue_script('king-addons-el-hf-target-rule');
362
363 wp_register_script(
364 'king-addons-el-hf-user-role',
365 KING_ADDONS_URL . 'includes/extensions/Header_Footer_Builder/conditions-user.js',
366 array(
367 'jquery',
368 ),
369 KING_ADDONS_VERSION,
370 true
371 );
372
373 wp_enqueue_script('king-addons-el-hf-user-role');
374
375 wp_register_style('king-addons-el-hf-select2', KING_ADDONS_URL . 'includes/extensions/Header_Footer_Builder/select2.css', '', KING_ADDONS_VERSION);
376 wp_enqueue_style('king-addons-el-hf-select2');
377 wp_register_style('king-addons-el-hf-target-rule', KING_ADDONS_URL . 'includes/extensions/Header_Footer_Builder/conditions.css', '', KING_ADDONS_VERSION);
378 wp_enqueue_style('king-addons-el-hf-target-rule');
379 wp_enqueue_script('king-addons-el-hf-script', KING_ADDONS_URL . 'includes/extensions/Header_Footer_Builder/admin.js', array('jquery'), KING_ADDONS_VERSION);
380
381 $localize_vars = array(
382 'please_enter' => __('Please enter', 'king-addons'),
383 'please_delete' => __('Please delete', 'king-addons'),
384 'more_char' => __('or more characters', 'king-addons'),
385 'character' => __('character', 'king-addons'),
386 'loading' => __('Loading more results…', 'king-addons'),
387 'only_select' => __('You can only select', 'king-addons'),
388 'item' => __('item', 'king-addons'),
389 'char_s' => __('s', 'king-addons'),
390 'no_result' => __('No results found', 'king-addons'),
391 'searching' => __('Searching…', 'king-addons'),
392 'not_loader' => __('The results could not be loaded.', 'king-addons'),
393 'search' => __('Search pages / post / categories', 'king-addons'),
394 'ajax_nonce' => wp_create_nonce('king-addons-el-hf-get-posts-by-query'),
395 );
396 wp_localize_script('king-addons-el-hf-select2', 'kngRules', $localize_vars);
397
398 }
399
400 public function display_rules_tab()
401 {
402 $this->admin_styles();
403 $include_locations = get_post_meta(get_the_id(), 'king_addons_el_hf_target_include_locations', true);
404 $exclude_locations = get_post_meta(get_the_id(), 'king_addons_el_hf_target_exclude_locations', true);
405 $users = get_post_meta(get_the_id(), 'king_addons_el_hf_target_user_roles', true);
406 ?>
407 <tr class="king-addons-el-hf-target-rules-row king-addons-el-hf-options-row">
408 <td class="king-addons-el-hf-target-rules-row-heading king-addons-el-hf-options-row-heading">
409 <label><strong><?php esc_html_e('Display On', 'king-addons'); ?></strong></label>
410 <p><?php esc_html_e('Add locations for where this template should appear', 'king-addons'); ?></p>
411 </td>
412 <td class="king-addons-el-hf-target-rules-row-content king-addons-el-hf-options-row-content">
413 <?php
414 self::target_rule_settings_field(
415 'king-addons-el-hf-target-rules-location',
416 [
417 'title' => __('Display Rules', 'king-addons'),
418 'value' => '[{"type":"basic-global","specific":null}]',
419 'tags' => 'site,enable,target,pages',
420 'rule_type' => 'display',
421 'add_rule_label' => __('Add Display Rule', 'king-addons'),
422 ],
423 $include_locations
424 );
425 ?>
426 </td>
427 </tr>
428 <tr class="king-addons-el-hf-target-rules-row king-addons-el-hf-options-row">
429 <td class="king-addons-el-hf-target-rules-row-heading king-addons-el-hf-options-row-heading">
430 <label><strong><?php esc_html_e('Do Not Display On', 'king-addons'); ?></strong></label>
431 <p><?php esc_html_e('Add locations for where this template should not appear', 'king-addons'); ?></p>
432 </td>
433 <td class="king-addons-el-hf-target-rules-row-content king-addons-el-hf-options-row-content">
434 <?php
435 self::target_rule_settings_field(
436 'king-addons-el-hf-target-rules-exclusion',
437 [
438 'title' => __('Exclude On', 'king-addons'),
439 'value' => '[]',
440 'tags' => 'site,enable,target,pages',
441 'add_rule_label' => __('Add Exclusion Rule', 'king-addons'),
442 'rule_type' => 'exclude',
443 ],
444 $exclude_locations
445 );
446 ?>
447 </td>
448 </tr>
449 <tr class="king-addons-el-hf-target-rules-row king-addons-el-hf-options-row">
450 <td class="king-addons-el-hf-target-rules-row-heading king-addons-el-hf-options-row-heading">
451 <label><strong><?php esc_html_e('User Roles', 'king-addons'); ?></strong></label>
452 <p><?php esc_html_e('Display custom template based on user role', 'king-addons'); ?></p>
453 </td>
454 <td class="king-addons-el-hf-target-rules-row-content king-addons-el-hf-options-row-content">
455 <?php
456 self::target_user_role_settings_field(
457 'king-addons-el-hf-target-rules-users',
458 [
459 'title' => __('Users', 'king-addons'),
460 'value' => '[]',
461 'tags' => 'site,enable,target,pages',
462 'add_rule_label' => __('Add User Rule', 'king-addons'),
463 ],
464 $users
465 );
466 ?>
467 </td>
468 </tr>
469 <?php
470 }
471
472 public static function get_user_selections()
473 {
474 $selection_options = array(
475 'basic' => array(
476 'label' => __('Basic', 'king-addons'),
477 'value' => array(
478 'all' => __('All', 'king-addons'),
479 'logged-in' => __('Logged In', 'king-addons'),
480 'logged-out' => __('Logged Out', 'king-addons'),
481 ),
482 ),
483
484 'advanced' => array(
485 'label' => __('Advanced', 'king-addons'),
486 'value' => array(),
487 ),
488 );
489
490 /* User roles */
491 $roles = get_editable_roles();
492
493 foreach ($roles as $slug => $data) {
494 $selection_options['advanced']['value'][$slug] = $data['name'];
495 }
496
497 /**
498 * Filter options displayed in the user select field of Display conditions.
499 *
500 * @since 1.5.0
501 */
502 return apply_filters('king-addons-el-hf_user_roles_list', $selection_options);
503 }
504
505 public static function target_user_role_settings_field($name, $settings, $value)
506 {
507 $input_name = $name;
508 $add_rule_label = $settings['add_rule_label'] ?? __('Add Rule', 'king-addons');
509 $saved_values = $value;
510 $output = '';
511
512 if (!isset(self::$user_selection) || empty(self::$user_selection)) {
513 self::$user_selection = self::get_user_selections();
514 }
515 $selection_options = self::$user_selection;
516
517 $output .= '<script type="text/html" id="tmpl-king-addons-el-hf-user-role-condition">';
518 $output .= '<div class="king-addons-el-hf-user-role-condition king-addons-el-hf-user-role-{{data.id}}" data-rule="{{data.id}}" >';
519 $output .= '<span class="user_role-condition-delete dashicons dashicons-dismiss"></span>';
520
521 $output .= '<div class="user_role-condition-wrap" >';
522 $output .= '<select name="' . esc_attr($input_name) . '[{{data.id}}]" class="user_role-condition form-control king-addons-el-hf-input">';
523 $output .= '<option value="">' . __('Select', 'king-addons') . '</option>';
524
525 foreach ($selection_options as $group_data) {
526 $output .= '<optgroup label="' . $group_data['label'] . '">';
527 foreach ($group_data['value'] as $opt_key => $opt_value) {
528 $output .= '<option value="' . $opt_key . '">' . $opt_value . '</option>';
529 }
530 $output .= '</optgroup>';
531 }
532 $output .= '</select>';
533 $output .= '</div>';
534 $output .= '</div> <!-- king-addons-el-hf-user-role-condition -->';
535 $output .= '</script>';
536
537 /** @noinspection PhpConditionAlreadyCheckedInspection */
538 if (!is_array($saved_values) || (is_array($saved_values) && empty($saved_values))) {
539 $saved_values = array();
540 $saved_values[0] = '';
541 }
542
543 $index = 0;
544
545 $output .= '<div class="king-addons-el-hf-user-role-wrapper king-addons-el-hf-user-role-display-on-wrap" data-type="display">';
546 $output .= '<div class="king-addons-el-hf-user-role-selector-wrapper king-addons-el-hf-user-role-display-on">';
547 $output .= '<div class="user_role-builder-wrap">';
548 foreach ($saved_values as $index => $data) {
549 $output .= '<div class="king-addons-el-hf-user-role-condition king-addons-el-hf-user-role-' . $index . '" data-rule="' . $index . '" >';
550 $output .= '<span class="user_role-condition-delete dashicons dashicons-dismiss"></span>';
551 /* Condition Selection */
552 $output .= '<div class="user_role-condition-wrap" >';
553 $output .= '<select name="' . esc_attr($input_name) . '[' . $index . ']" class="user_role-condition form-control king-addons-el-hf-input">';
554 $output .= '<option value="">' . __('Select', 'king-addons') . '</option>';
555
556 foreach ($selection_options as $group_data) {
557 $output .= '<optgroup label="' . $group_data['label'] . '">';
558 foreach ($group_data['value'] as $opt_key => $opt_value) {
559 $output .= '<option value="' . $opt_key . '" ' . selected($data, $opt_key, false) . '>' . $opt_value . '</option>';
560 }
561 $output .= '</optgroup>';
562 }
563 $output .= '</select>';
564 $output .= '</div>';
565 $output .= '</div> <!-- king-addons-el-hf-user-role-condition -->';
566 }
567 $output .= '</div>';
568 /* Add new rule */
569 $output .= '<div class="user_role-add-rule-wrap">';
570 $output .= '<a href="#" class="button" data-rule-id="' . absint($index) . '">' . $add_rule_label . '</a>';
571 $output .= '</div>';
572 $output .= '</div>';
573 $output .= '</div>';
574
575 echo $output;
576 }
577
578 public static function target_rule_settings_field($name, $settings, $value)
579 {
580 $input_name = $name;
581 $rule_type = $settings['rule_type'] ?? 'target_rule';
582 $add_rule_label = $settings['add_rule_label'] ?? __('Add Rule', 'king-addons');
583 $saved_values = $value;
584 $output = '';
585
586 if (isset(self::$location_selection) || empty(self::$location_selection)) {
587 self::$location_selection = self::getLocationSelections();
588 }
589 $selection_options = self::$location_selection;
590
591 $output .= '<script type="text/html" id="tmpl-king-addons-el-hf-target-rule-' . $rule_type . '-condition">';
592
593 $output .= '<div class="king-addons-el-hf-target-rule-condition king-addons-el-hf-target-rule-{{data.id}}" data-rule="{{data.id}}" >';
594 $output .= '<span class="target_rule-condition-delete dashicons dashicons-dismiss"></span>';
595 $output .= '<div class="target_rule-condition-wrap" >';
596
597 $output .= '<select name="' . esc_attr($input_name) . '[rule][{{data.id}}]" class="target_rule-condition form-control king-addons-el-hf-input">';
598 $output .= '<option value="">' . __('Select', 'king-addons') . '</option>';
599
600 foreach ($selection_options as $group_data) {
601 $output .= '<optgroup label="' . $group_data['label'] . '">';
602 foreach ($group_data['value'] as $opt_key => $opt_value) {
603 $output .= '<option value="' . $opt_key . '">' . $opt_value . '</option>';
604 }
605 $output .= '</optgroup>';
606 }
607 $output .= '</select>';
608
609 $output .= '</div>';
610 $output .= '</div> <!-- king-addons-el-hf-target-rule-condition -->';
611
612 $output .= '<div class="target_rule-specific-page-wrap" style="display:none">';
613 $output .= '<select name="' . esc_attr($input_name) . '[specific][]" class="target-rule-select2 target_rule-specific-page form-control king-addons-el-hf-input " multiple="multiple">';
614 $output .= '</select>';
615 $output .= '</div>';
616
617 $output .= '</script>';
618
619 $output .= '<div class="king-addons-el-hf-target-rule-wrapper king-addons-el-hf-target-rule-' . $rule_type . '-on-wrap" data-type="' . $rule_type . '">';
620 $output .= '<div class="king-addons-el-hf-target-rule-selector-wrapper king-addons-el-hf-target-rule-' . $rule_type . '-on">';
621 $output .= self::generate_target_rule_selector($rule_type, $selection_options, $input_name, $saved_values, $add_rule_label);
622 $output .= '</div>';
623 $output .= '</div>';
624
625 echo $output;
626 }
627
628 public static function generate_target_rule_selector($type, $selection_options, $input_name, $saved_values, $add_rule_label)
629 {
630 $output = '<div class="target_rule-builder-wrap">';
631
632 /** @noinspection PhpConditionAlreadyCheckedInspection */
633 if (!is_array($saved_values) || (is_array($saved_values) && empty($saved_values))) {
634 $saved_values = array();
635 $saved_values['rule'][0] = '';
636 $saved_values['specific'][0] = '';
637 }
638
639 $index = 0;
640 if (is_array($saved_values) && is_array($saved_values['rule'])) {
641 foreach ($saved_values['rule'] as $index => $data) {
642 $output .= '<div class="king-addons-el-hf-target-rule-condition king-addons-el-hf-target-rule-' . $index . '" data-rule="' . $index . '" >';
643
644 $output .= '<span class="target_rule-condition-delete dashicons dashicons-dismiss"></span>';
645 $output .= '<div class="target_rule-condition-wrap" >';
646 $output .= '<select name="' . esc_attr($input_name) . '[rule][' . $index . ']" class="target_rule-condition form-control king-addons-el-hf-input">';
647 $output .= '<option value="">' . __('Select', 'king-addons') . '</option>';
648
649 foreach ($selection_options as $group_data) {
650 $output .= '<optgroup label="' . $group_data['label'] . '">';
651 foreach ($group_data['value'] as $opt_key => $opt_value) {
652
653 $selected = '';
654
655 if ($data == $opt_key) {
656 $selected = 'selected="selected"';
657 }
658
659 $output .= '<option value="' . $opt_key . '" ' . $selected . '>' . $opt_value . '</option>';
660 }
661 $output .= '</optgroup>';
662 }
663 $output .= '</select>';
664 $output .= '</div>';
665
666 $output .= '</div>';
667
668 $output .= '<div class="target_rule-specific-page-wrap" style="display:none">';
669 $output .= '<select name="' . esc_attr($input_name) . '[specific][]" class="target-rule-select2 target_rule-specific-page form-control king-addons-el-hf-input " multiple="multiple">';
670
671 if ('specifics' == $data && isset($saved_values['specific']) && null != $saved_values['specific'] && is_array($saved_values['specific'])) {
672 foreach ($saved_values['specific'] as $sel_value) {
673
674 if (strpos($sel_value, 'post-') !== false) {
675 $post_id = (int)str_replace('post-', '', $sel_value);
676 $post_title = get_the_title($post_id);
677 $output .= '<option value="post-' . $post_id . '" selected="selected" >' . $post_title . '</option>';
678 }
679
680 if (strpos($sel_value, 'tax-') !== false) {
681 $tax_data = explode('-', $sel_value);
682
683 $tax_id = (int)str_replace('tax-', '', $sel_value);
684 $term = get_term($tax_id);
685 $term_name = '';
686
687 if (!is_wp_error($term)) {
688 $term_taxonomy = ucfirst(str_replace('_', ' ', $term->taxonomy));
689
690 if (isset($tax_data[2]) && 'single' === $tax_data[2]) {
691 $term_name = 'All singulars from ' . $term->name;
692 } else {
693 $term_name = $term->name . ' - ' . $term_taxonomy;
694 }
695 }
696
697 $output .= '<option value="' . $sel_value . '" selected="selected" >' . $term_name . '</option>';
698 }
699 }
700 }
701 $output .= '</select>';
702 $output .= '</div>';
703 }
704 }
705
706 $output .= '</div>';
707
708 $output .= '<div class="target_rule-add-rule-wrap">';
709 $output .= '<a href="#" class="button" data-rule-id="' . absint($index) . '" data-rule-type="' . $type . '">' . $add_rule_label . '</a>';
710 $output .= '</div>';
711
712 if ('display' == $type) {
713 $output .= '<div class="target_rule-add-exclusion-rule">';
714 $output .= '<a href="#" class="button">' . __('Add Exclusion Rule', 'king-addons') . '</a>';
715 $output .= '</div>';
716 }
717
718 return $output;
719 }
720
721 function saveMetaboxData($post_id)
722 {
723 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
724 return;
725 }
726
727 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')) {
728 return;
729 }
730
731 if (!current_user_can('edit_posts')) {
732 return;
733 }
734
735 if (!isset($_POST['king-addons-el-hf-target-rules-location'])) {
736 $target_locations = array(
737 'rule' => array('basic-global'),
738 'specific' => array(),
739 );
740 } else {
741 $target_locations = self::getFormatRuleValue($_POST, 'king-addons-el-hf-target-rules-location');
742 if (empty($target_locations)) {
743 $target_locations = array(
744 'rule' => array('basic-global'),
745 'specific' => array(),
746 );
747 }
748 }
749
750 $target_exclusion = self::getFormatRuleValue($_POST, 'king-addons-el-hf-target-rules-exclusion');
751 $target_users = [];
752
753 if (isset($_POST['king-addons-el-hf-target-rules-users'])) {
754 $target_users = array_map('sanitize_text_field', wp_unslash($_POST['king-addons-el-hf-target-rules-users']));
755 }
756
757 update_post_meta($post_id, 'king_addons_el_hf_target_include_locations', $target_locations);
758 update_post_meta($post_id, 'king_addons_el_hf_target_exclude_locations', $target_exclusion);
759 update_post_meta($post_id, 'king_addons_el_hf_target_user_roles', $target_users);
760
761 if (isset($_POST['king_addons_el_hf_template_type'])) {
762 update_post_meta($post_id, 'king_addons_el_hf_template_type', sanitize_text_field(wp_unslash($_POST['king_addons_el_hf_template_type'])));
763 }
764
765 if (isset($_POST['king-addons-el-hf-display-on-canvas'])) {
766 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'])));
767 } else {
768 delete_post_meta($post_id, 'king-addons-el-hf-display-on-canvas');
769 }
770 }
771
772 function setCompatibility()
773 {
774 $template = get_template();
775 $is_elementor_callable = defined('ELEMENTOR_VERSION') && is_callable('Elementor\Plugin::instance');
776
777 if ($is_elementor_callable) {
778 self::$elementor_instance = Elementor\Plugin::instance();
779
780 // TODO: Add popular themes
781 switch ($template) {
782 case 'hello-elementor':
783 require_once(KING_ADDONS_PATH . 'includes/extensions/Header_Footer_Builder/themes/hello-elementor/ELHF_Hello_Elementor.php');
784 break;
785 default:
786 add_action('init', [$this, 'setupSettingsPage']);
787 add_filter('king_addons_el_hf_settings_tabs', [$this, 'setupUnsupportedTheme']);
788 add_action('init', [$this, 'setupFallbackSupport']);
789 break;
790 }
791 }
792 }
793
794 public function setupUnsupportedTheme($settings_tabs = [])
795 {
796 if (!current_theme_supports('king-addons-elementor-header-footer')) {
797 $settings_tabs['king_addons_el_hf_settings'] = [
798 'name' => esc_html__('Display Settings', 'king-addons'),
799 'url' => admin_url('edit.php?post_type=king-addons-el-hf&page=king-addons-el-hf-settings'),
800 ];
801 }
802 return $settings_tabs;
803 }
804
805 public function setupFallbackSupport()
806 {
807 if (!current_theme_supports('king-addons-elementor-header-footer')) {
808 $compatibility_option = get_option('king_addons_el_hf_compatibility_option', '1');
809
810 if ('1' === $compatibility_option) {
811 if (!class_exists('ELHF_Default_Method_1')) {
812 require_once(KING_ADDONS_PATH . 'includes/extensions/Header_Footer_Builder/themes/default/ELHF_Default_Method_1.php');
813 }
814 } elseif ('2' === $compatibility_option) {
815 if (!class_exists('ELHF_Default_Method_2')) {
816 require_once(KING_ADDONS_PATH . 'includes/extensions/Header_Footer_Builder/themes/default/ELHF_Default_Method_2.php');
817 }
818 }
819 }
820 }
821
822 function setupSettingsPage()
823 {
824 require_once(KING_ADDONS_PATH . 'includes/extensions/Header_Footer_Builder/ELHF_Settings_Page.php');
825 }
826
827 public static function renderHeader()
828 {
829 /** @noinspection SpellCheckingInspection */
830 echo '<header id="masthead" itemscope="itemscope" itemtype="https://schema.org/WPHeader">';
831 ?><p class="main-title" style="display: none;" itemprop="headline"><a
832 href="<?php echo esc_url(get_bloginfo('url')); ?>"
833 title="<?php echo esc_attr(get_bloginfo('name', 'display')); ?>"
834 rel="home"><?php echo esc_html(get_bloginfo('name')); ?></a></p><?php
835 self::getHeaderContent();
836 echo '</header>';
837 }
838
839 public static function renderFooter()
840 {
841 /** @noinspection SpellCheckingInspection */
842 echo '<footer id="colophon" itemscope="itemscope" itemtype="https://schema.org/WPFooter" role="contentinfo">';
843 self::getFooterContent();
844 echo '</footer>';
845 }
846
847 public static function getHeaderContent()
848 {
849 echo self::$elementor_instance->frontend->get_builder_content_for_display(self::getHeaderID()); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
850 }
851
852 public static function getFooterContent()
853 {
854 echo '<div style="width: 100%;">';
855 echo self::$elementor_instance->frontend->get_builder_content_for_display(self::getFooterID()); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
856 echo '</div>';
857 }
858
859 public static function getHeaderID()
860 {
861 $header_id = self::getSettings('king_addons_el_hf_type_header');
862
863 if ('' === $header_id) {
864 $header_id = false;
865 }
866
867 return apply_filters('king_addons_el_hf_get_header_id', $header_id);
868 }
869
870 public static function isHeaderEnabled()
871 {
872 $header_id = self::getSettings('king_addons_el_hf_type_header');
873 $status = false;
874
875 if ('' !== $header_id) {
876 $status = true;
877 }
878
879 return apply_filters('king_addons_el_hf_header_enabled', $status);
880 }
881
882 public static function isFooterEnabled()
883 {
884 $footer_id = self::getSettings('king_addons_el_hf_type_footer');
885 $status = false;
886
887 if ('' !== $footer_id) {
888 $status = true;
889 }
890
891 return apply_filters('king_addons_el_hf_footer_enabled', $status);
892 }
893
894 public static function getFooterID()
895 {
896 $footer_id = self::getSettings('king_addons_el_hf_type_footer');
897
898 if ('' === $footer_id) {
899 $footer_id = false;
900 }
901
902 return apply_filters('king_addons_el_hf_get_footer_id', $footer_id);
903 }
904
905
906 public static function getSettings($setting = '')
907 {
908 if ('king_addons_el_hf_type_header' == $setting || 'king_addons_el_hf_type_footer' == $setting) {
909 $templates = self::getTemplateID($setting);
910 $template = !is_array($templates) ? $templates : $templates[0];
911 return apply_filters("king_addons_el_hf_get_settings_$setting", $template);
912 }
913
914 return null;
915 }
916
917 public static function getTemplateID($type)
918 {
919 $option = [
920 'location' => 'king_addons_el_hf_target_include_locations',
921 'exclusion' => 'king_addons_el_hf_target_exclude_locations',
922 'users' => 'king_addons_el_hf_target_user_roles',
923 ];
924
925 $templates = self::getPostsByConditions('king-addons-el-hf', $option);
926
927 foreach ($templates as $template) {
928 if (get_post_meta(absint($template['id']), 'king_addons_el_hf_template_type', true) === $type) {
929 // Polylang check - https://polylang.pro/doc/function-reference/
930 if (function_exists('pll_current_language')) {
931 /** @noinspection PhpUndefinedFunctionInspection */
932 if (pll_current_language('slug') == pll_get_post_language($template['id'], 'slug')) {
933 return $template['id'];
934 }
935 } else {
936 return $template['id'];
937 }
938 }
939 }
940
941 return '';
942 }
943
944 public static function getPostsByConditions($post_type, $option)
945 {
946 global $wpdb;
947 global $post;
948
949 $post_type = $post_type ? esc_sql($post_type) : esc_sql($post->post_type);
950
951 if (is_array(self::$current_page_data) && isset(self::$current_page_data[$post_type])) {
952 return apply_filters('king_addons_el_hf_get_display_posts_by_conditions', self::$current_page_data[$post_type], $post_type);
953 }
954
955 $current_page_type = self::getCurrentPageType();
956
957 self::$current_page_data[$post_type] = array();
958
959 $option['current_post_id'] = self::$current_page_data['ID'];
960 $meta_header = self::getMetaOptionPost($post_type, $option);
961
962 if (false === $meta_header) {
963 $current_post_type = esc_sql(get_post_type());
964 $current_post_id = false;
965 $q_obj = get_queried_object();
966
967 $current_id = esc_sql(get_the_id());
968
969 // Check if WPML is active. Find WPML Object ID for current page.
970 /** @noinspection SpellCheckingInspection */
971 if (defined('ICL_SITEPRESS_VERSION')) {
972 $default_lang = apply_filters('wpml_default_language', '');
973 $current_lang = apply_filters('wpml_current_language', '');
974
975 if ($default_lang !== $current_lang) {
976 $current_post_type = get_post_type($current_id);
977 $current_id = apply_filters('wpml_object_id', $current_id, $current_post_type, true, $default_lang);
978 }
979 }
980
981 $location = isset($option['location']) ? esc_sql($option['location']) : '';
982
983 $query = "SELECT p.ID, pm.meta_value FROM $wpdb->postmeta as pm
984 INNER JOIN $wpdb->posts as p ON pm.post_id = p.ID
985 WHERE pm.meta_key = '$location'
986 AND p.post_type = '$post_type'
987 AND p.post_status = 'publish'";
988
989 $orderby = ' ORDER BY p.post_date DESC';
990
991 $meta_args = "pm.meta_value LIKE '%\"basic-global\"%'";
992
993 switch ($current_page_type) {
994 case 'is_404':
995 $meta_args .= " OR pm.meta_value LIKE '%\"special-404\"%'";
996 break;
997 case 'is_search':
998 $meta_args .= " OR pm.meta_value LIKE '%\"special-search\"%'";
999 break;
1000 case 'is_archive':
1001 case 'is_tax':
1002 case 'is_date':
1003 case 'is_author':
1004 $meta_args .= " OR pm.meta_value LIKE '%\"basic-archives\"%'";
1005 $meta_args .= " OR pm.meta_value LIKE '%\"$current_post_type|all|archive\"%'";
1006 if ('is_tax' == $current_page_type && (is_category() || is_tag() || is_tax())) {
1007 if (is_object($q_obj)) {
1008 $meta_args .= " OR pm.meta_value LIKE '%\"$current_post_type|all|taxarchive|$q_obj->taxonomy\"%'";
1009 $meta_args .= " OR pm.meta_value LIKE '%\"tax-$q_obj->term_id\"%'";
1010 }
1011 } elseif ('is_date' == $current_page_type) {
1012 $meta_args .= " OR pm.meta_value LIKE '%\"special-date\"%'";
1013 } elseif ('is_author' == $current_page_type) {
1014 $meta_args .= " OR pm.meta_value LIKE '%\"special-author\"%'";
1015 }
1016 break;
1017 case 'is_home':
1018 $meta_args .= " OR pm.meta_value LIKE '%\"special-blog\"%'";
1019 break;
1020 case 'is_front_page':
1021 $current_post_id = $current_id;
1022 $meta_args .= " OR pm.meta_value LIKE '%\"special-front\"%'";
1023 $meta_args .= " OR pm.meta_value LIKE '%\"$current_post_type|all\"%'";
1024 $meta_args .= " OR pm.meta_value LIKE '%\"post-$current_id\"%'";
1025 break;
1026 case 'is_singular':
1027 $current_post_id = $current_id;
1028 $meta_args .= " OR pm.meta_value LIKE '%\"basic-singulars\"%'";
1029 $meta_args .= " OR pm.meta_value LIKE '%\"$current_post_type|all\"%'";
1030 $meta_args .= " OR pm.meta_value LIKE '%\"post-$current_id\"%'";
1031 $taxonomies = get_object_taxonomies($q_obj->post_type);
1032 $terms = wp_get_post_terms($q_obj->ID, $taxonomies);
1033 foreach ($terms as $term) {
1034 $meta_args .= " OR pm.meta_value LIKE '%\"tax-$term->term_id-single-$term->taxonomy\"%'";
1035 }
1036 break;
1037 case 'is_woo_shop_page':
1038 $meta_args .= " OR pm.meta_value LIKE '%\"special-woocommerce-shop\"%'";
1039 break;
1040 case '':
1041 $current_post_id = $current_id;
1042 break;
1043 }
1044
1045 // @codingStandardsIgnoreStart
1046 $posts = $wpdb->get_results($query . ' AND (' . $meta_args . ')' . $orderby);
1047 // @codingStandardsIgnoreEnd
1048
1049 foreach ($posts as $local_post) {
1050 self::$current_page_data[$post_type][$local_post->ID] = array(
1051 'id' => $local_post->ID,
1052 'location' => unserialize($local_post->meta_value),
1053 );
1054 }
1055
1056 $option['current_post_id'] = $current_post_id;
1057
1058 self::removeExclusionRulePosts($post_type, $option);
1059 self::removeUserRulePosts($post_type, $option);
1060 }
1061
1062 return apply_filters('king_addons_el_hf_get_display_posts_by_conditions', self::$current_page_data[$post_type], $post_type);
1063 }
1064
1065 public static function getCurrentPageType(): ?string
1066 {
1067 if (null === self::$current_page_type) {
1068 $page_type = '';
1069 $current_id = false;
1070
1071 if (is_404()) {
1072 $page_type = 'is_404';
1073 } elseif (is_search()) {
1074 $page_type = 'is_search';
1075 } elseif (is_archive()) {
1076 $page_type = 'is_archive';
1077 if (is_category() || is_tag() || is_tax()) {
1078 $page_type = 'is_tax';
1079 } elseif (is_date()) {
1080 $page_type = 'is_date';
1081 } elseif (is_author()) {
1082 $page_type = 'is_author';
1083 } elseif (function_exists('is_shop')) {
1084 /** @noinspection PhpUndefinedFunctionInspection */
1085 if (is_shop()) {
1086 $page_type = 'is_woo_shop_page';
1087 }
1088 }
1089 } elseif (is_home()) {
1090 $page_type = 'is_home';
1091 } elseif (is_front_page()) {
1092 $page_type = 'is_front_page';
1093 $current_id = get_the_id();
1094 } elseif (is_singular()) {
1095 $page_type = 'is_singular';
1096 $current_id = get_the_id();
1097 } else {
1098 $current_id = get_the_id();
1099 }
1100
1101 self::$current_page_data['ID'] = $current_id;
1102 self::$current_page_type = $page_type;
1103 }
1104
1105 return self::$current_page_type;
1106 }
1107
1108 public static function getMetaOptionPost($post_type, $option)
1109 {
1110 $page_meta = (isset($option['page_meta']) && '' != $option['page_meta']) ? $option['page_meta'] : false;
1111
1112 if (false !== $page_meta) {
1113 $current_post_id = $option['current_post_id'] ?? false;
1114 $meta_id = get_post_meta($current_post_id, $option['page_meta'], true);
1115
1116 if (false !== $meta_id && '' != $meta_id) {
1117 self::$current_page_data[$post_type][$meta_id] = array(
1118 'id' => $meta_id,
1119 'location' => '',
1120 );
1121
1122 return self::$current_page_data[$post_type];
1123 }
1124 }
1125
1126 return false;
1127 }
1128
1129 public static function removeExclusionRulePosts($post_type, $option)
1130 {
1131 $exclusion = $option['exclusion'] ?? '';
1132 $current_post_id = $option['current_post_id'] ?? false;
1133 foreach (self::$current_page_data[$post_type] as $c_post_id => $c_data) {
1134 $exclusion_rules = get_post_meta($c_post_id, $exclusion, true);
1135 $is_exclude = self::parseLayoutDisplayCondition($current_post_id, $exclusion_rules);
1136 if ($is_exclude) {
1137 unset(self::$current_page_data[$post_type][$c_post_id]);
1138 }
1139 }
1140 }
1141
1142 public static function removeUserRulePosts($post_type, $option)
1143 {
1144 $users = $option['users'] ?? '';
1145
1146 foreach (self::$current_page_data[$post_type] as $c_post_id => $c_data) {
1147 $user_rules = get_post_meta($c_post_id, $users, true);
1148 $is_user = self::parseUserRoleCondition($user_rules);
1149
1150 if (!$is_user) {
1151 unset(self::$current_page_data[$post_type][$c_post_id]);
1152 }
1153 }
1154 }
1155
1156 public static function parseLayoutDisplayCondition($post_id, $rules): bool
1157 {
1158 $display = false;
1159
1160 /** @noinspection PhpConditionCheckedByNextConditionInspection */
1161 if (isset($rules['rule']) && is_array($rules['rule']) && !empty($rules['rule'])) {
1162 foreach ($rules['rule'] as $rule) {
1163 if (strrpos($rule, 'all') !== false) {
1164 $rule_case = 'all';
1165 } else {
1166 $rule_case = $rule;
1167 }
1168
1169 switch ($rule_case) {
1170 case 'basic-global':
1171 $display = true;
1172 break;
1173
1174 case 'basic-singulars':
1175 if (is_singular()) {
1176 $display = true;
1177 }
1178 break;
1179
1180 case 'basic-archives':
1181 if (is_archive()) {
1182 $display = true;
1183 }
1184 break;
1185
1186 case 'special-404':
1187 if (is_404()) {
1188 $display = true;
1189 }
1190 break;
1191
1192 case 'special-search':
1193 if (is_search()) {
1194 $display = true;
1195 }
1196 break;
1197
1198 case 'special-blog':
1199 if (is_home()) {
1200 $display = true;
1201 }
1202 break;
1203
1204 case 'special-front':
1205 if (is_front_page()) {
1206 $display = true;
1207 }
1208 break;
1209
1210 case 'special-date':
1211 if (is_date()) {
1212 $display = true;
1213 }
1214 break;
1215
1216 case 'special-author':
1217 if (is_author()) {
1218 $display = true;
1219 }
1220 break;
1221
1222 case 'special-woocommerce-shop':
1223 if (function_exists('is_shop')) {
1224 /** @noinspection PhpUndefinedFunctionInspection */
1225 if (is_shop()) {
1226 $display = true;
1227 }
1228 }
1229 break;
1230
1231 case 'all':
1232 $rule_data = explode('|', $rule);
1233
1234 $post_type = $rule_data[0] ?? false;
1235 $archive_type = $rule_data[2] ?? false;
1236 $taxonomy = $rule_data[3] ?? false;
1237 if (false === $archive_type) {
1238 $current_post_type = get_post_type($post_id);
1239 if (false !== $post_id && $current_post_type == $post_type) {
1240 $display = true;
1241 }
1242 } else {
1243 if (is_archive()) {
1244 $current_post_type = get_post_type();
1245 if ($current_post_type == $post_type) {
1246 if ('archive' == $archive_type) {
1247 $display = true;
1248 } elseif ('taxarchive' == $archive_type) {
1249 $obj = get_queried_object();
1250 $current_taxonomy = '';
1251 if ('' !== $obj && null !== $obj) {
1252 $current_taxonomy = $obj->taxonomy;
1253 }
1254
1255 if ($current_taxonomy == $taxonomy) {
1256 $display = true;
1257 }
1258 }
1259 }
1260 }
1261 }
1262 break;
1263
1264 case 'specifics':
1265 if (isset($rules['specific']) && is_array($rules['specific'])) {
1266 foreach ($rules['specific'] as $specific_page) {
1267 $specific_data = explode('-', $specific_page);
1268 $specific_post_type = $specific_data[0] ?? false;
1269 $specific_post_id = $specific_data[1] ?? false;
1270 if ('post' == $specific_post_type) {
1271 if ($specific_post_id == $post_id) {
1272 $display = true;
1273 }
1274 } elseif (isset($specific_data[2]) && ('single' == $specific_data[2]) && 'tax' == $specific_post_type) {
1275 if (is_singular()) {
1276 $term_details = get_term($specific_post_id);
1277
1278 if (isset($term_details->taxonomy)) {
1279 $has_term = has_term((int)$specific_post_id, $term_details->taxonomy, $post_id);
1280
1281 if ($has_term) {
1282 $display = true;
1283 }
1284 }
1285 }
1286 } elseif ('tax' == $specific_post_type) {
1287 $tax_id = get_queried_object_id();
1288 if ($specific_post_id == $tax_id) {
1289 $display = true;
1290 }
1291 }
1292 }
1293 }
1294 break;
1295
1296 default:
1297 break;
1298 }
1299
1300 if ($display) {
1301 break;
1302 }
1303 }
1304 }
1305
1306 return $display;
1307 }
1308
1309 public static function parseUserRoleCondition($rules): bool
1310 {
1311 $show_popup = true;
1312
1313 if (is_array($rules) && !empty($rules)) {
1314 $show_popup = false;
1315
1316 foreach ($rules as $rule) {
1317 switch ($rule) {
1318 case '':
1319 case 'all':
1320 $show_popup = true;
1321 break;
1322
1323 case 'logged-in':
1324 if (is_user_logged_in()) {
1325 $show_popup = true;
1326 }
1327 break;
1328
1329 case 'logged-out':
1330 if (!is_user_logged_in()) {
1331 $show_popup = true;
1332 }
1333 break;
1334
1335 default:
1336 if (is_user_logged_in()) {
1337 $current_user = wp_get_current_user();
1338
1339 if (isset($current_user->roles)
1340 && is_array($current_user->roles)
1341 && in_array($rule, $current_user->roles)
1342 ) {
1343 $show_popup = true;
1344 }
1345 }
1346 break;
1347 }
1348
1349 if ($show_popup) {
1350 break;
1351 }
1352 }
1353 }
1354
1355 return $show_popup;
1356 }
1357
1358 public static function checkUserCanEdit()
1359 {
1360 if (is_singular('king-addons-el-hf') && !current_user_can('edit_posts')) {
1361 wp_redirect(site_url(), 301);
1362 die;
1363 }
1364 }
1365
1366 public static function loadElementorCanvasTemplate($single_template)
1367 {
1368 global $post;
1369
1370 if ('king-addons-el-hf' == $post->post_type) {
1371 if (defined('ELEMENTOR_VERSION')) {
1372 $elementor_2_0_canvas = ELEMENTOR_PATH . '/modules/page-templates/templates/canvas.php';
1373
1374 if (file_exists($elementor_2_0_canvas)) {
1375 return $elementor_2_0_canvas;
1376 } else {
1377 return ELEMENTOR_PATH . '/includes/page-templates/canvas.php';
1378 }
1379 }
1380 }
1381
1382 return $single_template;
1383 }
1384
1385 public static function enqueueScripts(): void
1386 {
1387 $screen = get_current_screen();
1388 if ($screen->id === 'edit-king-addons-el-hf') {
1389 // todo - styles
1390 // wp_enqueue_style('king-addons-el-hf-style', KING_ADDONS_URL . 'includes/extensions/Header_Footer_Builder/header-footer-builder.css', '', KING_ADDONS_VERSION);
1391 wp_enqueue_style('king-addons-el-hf-style', KING_ADDONS_URL . 'includes/extensions/Header_Footer_Builder/admin.css', '', KING_ADDONS_VERSION);
1392 }
1393 }
1394
1395 public static function columnHeadings($columns)
1396 {
1397 unset($columns['date']);
1398 $columns['king_addons_el_hf_edit_template'] = esc_html__('Edit Template', 'king-addons');
1399 $columns['king_addons_el_hf_type_of_template'] = esc_html__('Type of Template', 'king-addons');
1400 $columns['king_addons_el_hf_display_rules'] = esc_html__('Display Rules', 'king-addons');
1401 $columns['date'] = esc_html__('Date', 'king-addons');
1402 return $columns;
1403 }
1404
1405 public static function columnContent($column, $post_id)
1406 {
1407 // Edit Template
1408 if ('king_addons_el_hf_edit_template' === $column) {
1409 echo '<a class="king-addons-el-hf-edit-template-btn" href="';
1410 echo './post.php?post=' . esc_attr($post_id) . '&action=edit';
1411 echo '">' . esc_html__('Edit Template', 'king-addons') . '</a>';
1412 }
1413
1414 // Display Rules
1415 if ('king_addons_el_hf_display_rules' === $column) {
1416
1417 $locations = get_post_meta($post_id, 'king_addons_el_hf_target_include_locations', true);
1418 if (!empty($locations)) {
1419 echo '<div style="margin-bottom: 5px;">';
1420 echo '<strong>';
1421 echo esc_html__('Display: ', 'king-addons');
1422 echo '</strong>';
1423 self::columnDisplayLocation($locations);
1424 echo '</div>';
1425 }
1426
1427 $locations = get_post_meta($post_id, 'king_addons_el_hf_target_exclude_locations', true);
1428 if (!empty($locations)) {
1429 echo '<div style="margin-bottom: 5px;">';
1430 echo '<strong>';
1431 echo esc_html__('Exclusion: ', 'king-addons');
1432 echo '</strong>';
1433 self::columnDisplayLocation($locations);
1434 echo '</div>';
1435 }
1436
1437 $users = get_post_meta($post_id, 'king_addons_el_hf_target_user_roles', true);
1438 if (isset($users) && is_array($users)) {
1439 if (!empty($users[0])) {
1440 $user_label = [];
1441 foreach ($users as $user) {
1442 $user_label[] = self::get_user_by_key($user);
1443 }
1444 echo '<div>';
1445 echo '<strong>Users: </strong>';
1446 echo esc_html(join(', ', $user_label));
1447 echo '</div>';
1448 }
1449 }
1450
1451 }
1452
1453 // Type of Template
1454 if ('king_addons_el_hf_type_of_template' === $column) {
1455 $template_type = get_post_meta($post_id, 'king_addons_el_hf_template_type', true);
1456 if (!empty($template_type)) {
1457 echo '<div style="margin-bottom: 5px;">';
1458 echo '<strong>';
1459 switch ($template_type) {
1460 case 'king_addons_el_hf_type_header':
1461 echo esc_html__('Header', 'king-addons');
1462 break;
1463 case 'king_addons_el_hf_type_footer':
1464 echo esc_html__('Footer', 'king-addons');
1465 break;
1466 default:
1467 echo esc_html__('Not selected', 'king-addons');
1468 break;
1469 }
1470 echo '</strong>';
1471 echo '</div>';
1472 }
1473 }
1474 }
1475
1476 public static function get_user_by_key($key)
1477 {
1478 if (!isset(self::$user_selection) || empty(self::$user_selection)) {
1479 self::$user_selection = self::get_user_selections();
1480 }
1481 $user_selection = self::$user_selection;
1482
1483 if (isset($user_selection['basic']['value'][$key])) {
1484 return $user_selection['basic']['value'][$key];
1485 } elseif ($user_selection['advanced']['value'][$key]) {
1486 return $user_selection['advanced']['value'][$key];
1487 }
1488 return $key;
1489 }
1490
1491 public static function columnDisplayLocation($locations)
1492 {
1493 $location_label = [];
1494 /** @noinspection PhpConditionAlreadyCheckedInspection */
1495 if (is_array($locations) && is_array($locations['rule']) && isset($locations['rule'])) {
1496 /** @noinspection PhpArraySearchInBooleanContextInspection */
1497 $index = array_search('specifics', $locations['rule']);
1498 /** @noinspection PhpConditionCheckedByNextConditionInspection */
1499 if (false !== $index && !empty($index)) {
1500 unset($locations['rule'][$index]);
1501 }
1502 }
1503
1504 if (isset($locations['rule']) && is_array($locations['rule'])) {
1505 foreach ($locations['rule'] as $location) {
1506 $location_label[] = self::getLocation($location);
1507 }
1508 }
1509
1510 if (isset($locations['specific']) && is_array($locations['specific'])) {
1511 foreach ($locations['specific'] as $location) {
1512 $location_label[] = self::getLocation($location);
1513 }
1514 }
1515
1516 echo esc_html(join(', ', $location_label));
1517 }
1518
1519 public static function getLocation($key)
1520 {
1521 if (!isset(self::$location_selection) || empty(self::$location_selection)) {
1522 self::$location_selection = self::getLocationSelections();
1523 }
1524
1525 $location_selection = self::$location_selection;
1526
1527 foreach ($location_selection as $location_grp) {
1528 if (isset($location_grp['value'][$key])) {
1529 return $location_grp['value'][$key];
1530 }
1531 }
1532
1533 if (strpos($key, 'post-') !== false) {
1534 $post_id = (int)str_replace('post-', '', $key);
1535 return get_the_title($post_id);
1536 }
1537
1538 if (strpos($key, 'tax-') !== false) {
1539 $tax_id = (int)str_replace('tax-', '', $key);
1540 $term = get_term($tax_id);
1541
1542 if (!is_wp_error($term)) {
1543 $term_taxonomy = ucfirst(str_replace('_', ' ', $term->taxonomy));
1544 return $term->name . ' - ' . $term_taxonomy;
1545 } else {
1546 return '';
1547 }
1548 }
1549
1550 return $key;
1551 }
1552
1553 public static function getLocationSelections()
1554 {
1555 $args = array(
1556 'public' => true,
1557 '_builtin' => true,
1558 );
1559
1560 $post_types = get_post_types($args, 'objects');
1561 unset($post_types['attachment']);
1562
1563 $args['_builtin'] = false;
1564 $custom_post_type = get_post_types($args, 'objects');
1565
1566 $post_types = apply_filters('king_addons_el_hf_location_rule_post_types', array_merge($post_types, $custom_post_type));
1567
1568 if (!king_addons_freemius()->can_use_premium_code__premium_only()) {
1569 $special_pages = array(
1570 'special-404-none' => esc_html__('404 Page (Available in PRO)', 'king-addons'),
1571 'special-search' => esc_html__('Search Page', 'king-addons'),
1572 'special-blog-none' => esc_html__('Blog / Posts Page (Available in PRO)', 'king-addons'),
1573 'special-front' => esc_html__('Front Page', 'king-addons'),
1574 'special-date' => esc_html__('Date Archive', 'king-addons'),
1575 'special-author' => esc_html__('Author Archive', 'king-addons'),
1576 );
1577 } else {
1578 $special_pages = array(
1579 'special-404' => esc_html__('404 Page', 'king-addons'),
1580 'special-search' => esc_html__('Search Page', 'king-addons'),
1581 'special-blog' => esc_html__('Blog / Posts Page', 'king-addons'),
1582 'special-front' => esc_html__('Front Page', 'king-addons'),
1583 'special-date' => esc_html__('Date Archive', 'king-addons'),
1584 'special-author' => esc_html__('Author Archive', 'king-addons'),
1585 );
1586 }
1587
1588 if (class_exists('WooCommerce')) {
1589 if (!king_addons_freemius()->can_use_premium_code__premium_only()) {
1590 $special_pages['special-woocommerce-shop-none'] = esc_html__('WooCommerce Shop Page (Available in PRO)', 'king-addons');
1591 } else {
1592 $special_pages['special-woocommerce-shop'] = esc_html__('WooCommerce Shop Page', 'king-addons');
1593 }
1594 }
1595
1596 $selection_options = array(
1597 'basic' => array(
1598 'label' => esc_html__('Basic', 'king-addons'),
1599 'value' => array(
1600 'basic-global' => esc_html__('Entire Website', 'king-addons'),
1601 'basic-singulars' => esc_html__('All Singulars', 'king-addons'),
1602 'basic-archives' => esc_html__('All Archives', 'king-addons'),
1603 ),
1604 ),
1605
1606 'special-pages' => array(
1607 'label' => esc_html__('Special Pages', 'king-addons'),
1608 'value' => $special_pages,
1609 ),
1610 );
1611
1612 if (!king_addons_freemius()->can_use_premium_code__premium_only()) {
1613 $selection_options['specific-target'] = array(
1614 'label' => esc_html__('Specific Target', 'king-addons'),
1615 'value' => array(
1616 'specifics-none' => esc_html__('Specific Pages / Posts / Taxonomies, etc. (Available in PRO)', 'king-addons'),
1617 ),
1618 );
1619 } else {
1620 $selection_options['specific-target'] = array(
1621 'label' => esc_html__('Specific Target', 'king-addons'),
1622 'value' => array(
1623 'specifics' => esc_html__('Specific Pages / Posts / Taxonomies, etc.', 'king-addons'),
1624 ),
1625 );
1626 }
1627
1628 $args = array(
1629 'public' => true,
1630 );
1631
1632 $taxonomies = get_taxonomies($args, 'objects');
1633
1634 if (!empty($taxonomies)) {
1635 foreach ($taxonomies as $taxonomy) {
1636
1637 if ('post_format' == $taxonomy->name) {
1638 continue;
1639 }
1640
1641 foreach ($post_types as $post_type) {
1642 $post_opt = self::getPostTargetRuleOptions($post_type, $taxonomy);
1643
1644 if (isset($selection_options[$post_opt['post_key']])) {
1645 if (!empty($post_opt['value']) && is_array($post_opt['value'])) {
1646 foreach ($post_opt['value'] as $key => $value) {
1647 if (!in_array($value, $selection_options[$post_opt['post_key']]['value'])) {
1648 $selection_options[$post_opt['post_key']]['value'][$key] = $value;
1649 }
1650 }
1651 }
1652 } else {
1653 $selection_options[$post_opt['post_key']] = array(
1654 'label' => $post_opt['label'],
1655 'value' => $post_opt['value'],
1656 );
1657 }
1658 }
1659 }
1660 }
1661
1662 return apply_filters('king_addons_el_hf_display_on_list', $selection_options);
1663 }
1664
1665 public static function getPostTargetRuleOptions($post_type, $taxonomy): array
1666 {
1667 $post_key = str_replace(' ', '-', strtolower($post_type->label));
1668 $post_label = ucwords($post_type->label);
1669 $post_name = $post_type->name;
1670 $post_option = array();
1671
1672 /* translators: %s is post label */
1673 $all_posts = sprintf(esc_html__('All %s', 'king-addons'), $post_label);
1674 $post_option[$post_name . '|all'] = $all_posts;
1675
1676 if ('pages' != $post_key) {
1677 /* translators: %s is post label */
1678 $all_archive = sprintf(esc_html__('All %s Archive', 'king-addons'), $post_label);
1679 $post_option[$post_name . '|all|archive'] = $all_archive;
1680 }
1681
1682 if (in_array($post_type->name, $taxonomy->object_type)) {
1683 $tax_label = ucwords($taxonomy->label);
1684 $tax_name = $taxonomy->name;
1685
1686 /* translators: %s is taxonomy label */
1687 $tax_archive = sprintf(esc_html__('All %s Archive', 'king-addons'), $tax_label);
1688
1689 $post_option[$post_name . '|all|taxarchive|' . $tax_name] = $tax_archive;
1690 }
1691
1692 $post_output['post_key'] = $post_key;
1693 $post_output['label'] = $post_label;
1694 $post_output['value'] = $post_option;
1695
1696 return $post_output;
1697 }
1698
1699 public static function getFormatRuleValue($save_data, $key): array
1700 {
1701 $meta_value = array();
1702
1703 if (isset($save_data[$key]['rule'])) {
1704 $save_data[$key]['rule'] = array_unique($save_data[$key]['rule']);
1705 if (isset($save_data[$key]['specific'])) {
1706 $save_data[$key]['specific'] = array_unique($save_data[$key]['specific']);
1707 }
1708
1709 $index = array_search('', $save_data[$key]['rule']);
1710 if (false !== $index) {
1711 unset($save_data[$key]['rule'][$index]);
1712 }
1713 $index = array_search('specifics', $save_data[$key]['rule']);
1714 if (false !== $index) {
1715 unset($save_data[$key]['rule'][$index]);
1716
1717 if (isset($save_data[$key]['specific']) && is_array($save_data[$key]['specific'])) {
1718 $save_data[$key]['rule'][] = 'specifics';
1719 }
1720 }
1721
1722 foreach ($save_data[$key] as $meta_key => $value) {
1723 if (!empty($value)) {
1724 $meta_value[$meta_key] = array_map('esc_attr', $value);
1725 }
1726 }
1727 if (!isset($meta_value['rule']) || !in_array('specifics', $meta_value['rule'])) {
1728 $meta_value['specific'] = array();
1729 }
1730
1731 if (empty($meta_value['rule'])) {
1732 $meta_value = array();
1733 }
1734 }
1735
1736 return $meta_value;
1737 }
1738 }