| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Menu_Maker |
| 4 |
* |
| 5 |
* This class is responsible for generating the floating menu HTML using the provided parameters. |
| 6 |
* |
| 7 |
* @package FloatMenuLite |
| 8 |
* @subpackage Public |
| 9 |
* @author Dmytro Lobov <dev@wow-company.com>, Wow-Company |
| 10 |
* @copyright 2024 Dmytro Lobov |
| 11 |
* @license GPL-2.0+ |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace FloatMenuLite; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
class Menu_Maker { |
| 19 |
/** |
| 20 |
* @var mixed |
| 21 |
*/ |
| 22 |
private $id; |
| 23 |
/** |
| 24 |
* @var mixed |
| 25 |
*/ |
| 26 |
private $param; |
| 27 |
|
| 28 |
public function __construct( $id, $param ) { |
| 29 |
$this->id = $id; |
| 30 |
$this->param = $param; |
| 31 |
} |
| 32 |
|
| 33 |
public function init(): string { |
| 34 |
$param = $this->param; |
| 35 |
if ( empty( $param['menu_1']['item_type'] ) || ! is_array( $param['menu_1']['item_type'] ) ) { |
| 36 |
return ''; |
| 37 |
} |
| 38 |
$menu = '<div class="floating-menu float-menu-' . absint( $this->id ) . ' notranslate">'; |
| 39 |
$menu .= '<ul class="fm-bar">'; |
| 40 |
$menu .= $this->create_item(); |
| 41 |
$menu .= '</ul>'; |
| 42 |
$menu .= '</div>'; |
| 43 |
|
| 44 |
return $menu; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
private function create_item(): string { |
| 49 |
$param = $this->param; |
| 50 |
$items = $param['menu_1']['item_type']; |
| 51 |
$item = ''; |
| 52 |
foreach ( $items as $i => $value ) { |
| 53 |
$item .= '<li class="fm-item-' . absint( $this->id ) . '-' . absint( $i ) . '">'; |
| 54 |
$item .= $this->start_item( $i, $value ); |
| 55 |
$item .= $this->create_icon( $i ); |
| 56 |
$item .= $this->create_label( $i, $value ); |
| 57 |
$item .= $this->end_item( $i, $value ); |
| 58 |
$item .= '</li>'; |
| 59 |
} |
| 60 |
|
| 61 |
return $item; |
| 62 |
} |
| 63 |
|
| 64 |
private function filter_sub_items( $value ): bool { |
| 65 |
return ! empty( $value ); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
private function start_item( $i, $value ): string { |
| 70 |
$param = $this->param; |
| 71 |
|
| 72 |
$out = '<a '; |
| 73 |
|
| 74 |
if ( $value === 'link' ) { |
| 75 |
$link = $param['menu_1']['item_link'][ $i ] ?? '#'; |
| 76 |
$target = ! empty( $param['menu_1']['new_tab'][ $i ] ) ? '_blank' : '_self'; |
| 77 |
$out .= 'href="' . esc_url( $link ) . '" '; |
| 78 |
$out .= 'target="' . esc_attr( $target ) . '" '; |
| 79 |
$out .= $this->item_atts( $i ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
if ( $value === 'login' ) { |
| 84 |
$url = $param['menu_1']['item_link'][ $i ] ?? '#'; |
| 85 |
$link = wp_login_url( $url ); |
| 86 |
$out .= 'href="' . esc_url( $link ) . '" '; |
| 87 |
$out .= $this->item_atts( $i ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( $value === 'logout' ) { |
| 91 |
$url = $param['menu_1']['item_link'][ $i ] ?? '#'; |
| 92 |
$link = wp_logout_url( $url ); |
| 93 |
$out .= 'href="' . esc_url( $link ) . '" '; |
| 94 |
$out .= $this->item_atts( $i ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( $value === 'lostpassword' ) { |
| 98 |
$url = $param['menu_1']['item_link'][ $i ] ?? '#'; |
| 99 |
$link = wp_lostpassword_url( $url ); |
| 100 |
$out .= 'href="' . esc_url( $link ) . '" '; |
| 101 |
$out .= $this->item_atts( $i ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( $value === 'register' ) { |
| 105 |
$link = wp_registration_url(); |
| 106 |
$out .= 'href="' . esc_url( $link ) . '" '; |
| 107 |
$out .= $this->item_atts( $i ); |
| 108 |
} |
| 109 |
|
| 110 |
$out .= '>'; |
| 111 |
|
| 112 |
return $out; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
private function item_atts( $i ): string { |
| 117 |
$param = $this->param; |
| 118 |
$out = ''; |
| 119 |
if ( ! empty( $param['menu_1']['button_id'][ $i ] ) ) { |
| 120 |
$out .= 'id="' . esc_attr( $param['menu_1']['button_id'][ $i ] ) . '" '; |
| 121 |
} |
| 122 |
|
| 123 |
if ( ! empty( $param['menu_1']['button_class'][ $i ] ) ) { |
| 124 |
$out .= 'class="' . esc_attr( $param['menu_1']['button_class'][ $i ] ) . '" '; |
| 125 |
} |
| 126 |
|
| 127 |
if ( ! empty( $param['menu_1']['link_rel'][ $i ] ) ) { |
| 128 |
$out .= 'rel="' . esc_attr( $param['menu_1']['link_rel'][ $i ] ) . '" '; |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! empty( $param['menu_1']['aria_label'][ $i ] ) ) { |
| 132 |
$out .= 'aria-label="' . esc_attr( $param['menu_1']['aria_label'][ $i ] ) . '" '; |
| 133 |
} |
| 134 |
|
| 135 |
return trim( $out ); |
| 136 |
} |
| 137 |
|
| 138 |
private function end_item( $i, $value ): string { |
| 139 |
|
| 140 |
return '</a>'; |
| 141 |
} |
| 142 |
|
| 143 |
private function create_icon( $i ): string { |
| 144 |
$param = $this->param; |
| 145 |
|
| 146 |
if ( ! empty( $param['menu_1']['item_custom'][ $i ] ) ) { |
| 147 |
$img_alt = $param['menu_1']['image_alt'][ $i ] ?? ''; |
| 148 |
$icon_url = $param['menu_1']['item_custom_link'][ $i ] ?? ''; |
| 149 |
if ( $this->is_url( $icon_url ) ) { |
| 150 |
return '<div class="fm-icon"><img src="' . esc_url( $icon_url ) . '" alt="' . esc_attr( $img_alt ) . '"></div>'; |
| 151 |
} |
| 152 |
|
| 153 |
return '<div class="fm-icon"><i class="' . esc_attr( $icon_url ) . '"></i></div>'; |
| 154 |
} |
| 155 |
|
| 156 |
if ( ! empty( $param['menu_1']['item_custom_text_check'][ $i ] ) ) { |
| 157 |
$item_custom_text = $param['menu_1']['item_custom_text'][ $i ] ?? ''; |
| 158 |
|
| 159 |
return '<div class="fm-icon fm-icon-text"><span>' . esc_html( $item_custom_text ) . '</span></div>'; |
| 160 |
} |
| 161 |
|
| 162 |
$icon_class = $param['menu_1']['item_icon'][ $i ] ?? ''; |
| 163 |
$icon_animate = $param['menu_1']['item_icon_anomate'][ $i ] ?? ''; |
| 164 |
|
| 165 |
return '<div class="fm-icon"><i class="' . esc_attr( $icon_class ) . ' ' . esc_attr( $icon_animate ) . '"></i></div>'; |
| 166 |
} |
| 167 |
|
| 168 |
private function create_label( $i, $value ): string { |
| 169 |
$param = $this->param; |
| 170 |
|
| 171 |
$label = $param['menu_1']['item_tooltip'][ $i ] ?? ''; |
| 172 |
$out = ''; |
| 173 |
if ( is_email( $label ) ) { |
| 174 |
$label = antispambot( $label ); |
| 175 |
} |
| 176 |
$hold = ! empty( $param['menu_1']['hold_open'][ $i ] ) ? ' fm-hold-open' : ''; |
| 177 |
|
| 178 |
if ( $value === 'search' ) { |
| 179 |
$out = '<div class="fm-label' . esc_attr( $hold ) . '">'; |
| 180 |
$out .= '<form class="fm-search" action="' . esc_url( home_url( '/' ) ) . '">'; |
| 181 |
$out .= '<input type="search" class="fm-input" name="s" placeholder="' . esc_attr( $label ) . '">'; |
| 182 |
$out .= '</form></div>'; |
| 183 |
|
| 184 |
return $out; |
| 185 |
} |
| 186 |
|
| 187 |
if ( ! empty( $param['menu_1']['item_text'][ $i ] ) && $value === 'text' ) { |
| 188 |
$out = '<div class="fm-label' . esc_attr( $hold ) . '">' . esc_html( $label ); |
| 189 |
$out .= '<div class="fm-extra-text">' . do_shortcode( nl2br( wp_kses_post( $param['menu_1']['item_text'][ $i ] ) ) ) . '</div>'; |
| 190 |
$out .= '</div>'; |
| 191 |
|
| 192 |
return $out; |
| 193 |
} |
| 194 |
|
| 195 |
if ( empty( $label ) ) { |
| 196 |
return $out; |
| 197 |
} |
| 198 |
|
| 199 |
return '<div class="fm-label' . esc_attr( $hold ) . '">' . esc_html( $label ) . '</div>'; |
| 200 |
} |
| 201 |
|
| 202 |
private function is_url( $string ): bool { |
| 203 |
return filter_var( $string, FILTER_VALIDATE_URL ) !== false; |
| 204 |
} |
| 205 |
} |