PluginProbe
Floating Button – Easily Create Sticky, Fixed & Floating Buttons / 6.0.2
Floating Button – Easily Create Sticky, Fixed & Floating Buttons v6.0.2
trunk 2.0.2 3.0 4.0 4.1.2 4.3 5.0 5.0.1 5.1 5.1.1 5.2 5.3 5.3.1 6.0 6.0.1 6.0.10 6.0.11 6.0.12 6.0.13 6.0.2 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 All 31 releases
floating-button / includes / class-menu-maker.php

class-menu-maker.php in Floating Button – Easily Create Sticky, Fixed & Floating Buttons 6.0.2, at includes/class-menu-maker.php

415 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FloatingButton;
4
5 use FloatingButton\Publisher\Navigation;
6
7 defined( 'ABSPATH' ) || exit;
8
9 class Menu_Maker {
10
11 /**
12 * @var mixed
13 */
14 private $id;
15 /**
16 * @var mixed
17 */
18 private $param;
19 /**
20 * @var mixed
21 */
22 private $title;
23 private array $menus;
24
25 public function __construct( $id, $param, $title ) {
26 $this->id = $id;
27 $this->param = $param;
28 $this->title = $title;
29 $this->menus = [];
30 }
31
32 public function init(): string {
33
34 $menu = $this->wrapper();
35 $menu .= $this->main_button();
36 if ( $this->is_main() ) {
37 $menu .= $this->sub_buttons();
38 }
39 $menu .= '</div>';
40 $menu .= $this->get_menu();
41
42 return $menu;
43
44 }
45
46 public function wrapper(): string {
47 $position = ! empty( $this->param['position'] ) ? ' ' . $this->param['position'] : ' flBtn-position-br';
48 $shape = ! empty( $this->param['shape'] ) ? ' ' . $this->param['shape'] : ' flBtn-shape-circle';
49 $size = ! empty( $this->param['size'] ) ? ' ' . $this->param['size'] : ' flBtn-size-medium';
50 $animation = ! empty( $this->param['animation'] ) ? ' ' . $this->param['animation'] : '';
51 $btn_animation = ! empty( $this->param['button_animation'] ) ? ' flBtn-animated ' . $this->param['button_animation'] : '';
52 $shadow = empty( $this->param['shadow'] ) ? ' -shadow' : '';
53 $classes = $position . $shape . $size . $shadow . $animation . $btn_animation;
54
55 return '<div class="flBtn ' . esc_attr( $classes ) . '" id="floatBtn-' . absint( $this->id ) . '">';
56 }
57
58 public function main_button(): string {
59 $param = $this->param;
60 $main_type = ! empty( $param['item_type'] ) ? $param['item_type'] : 'main';
61 $btn = '';
62 $link = ! empty( $param['item_link'] ) ? $param['item_link'] : '#';
63 switch ( $main_type ) {
64 case 'main':
65 $btn .= $this->create_checkbox();
66 $btn .= '<label for="flBtn-' . absint( $this->id ) . '" ' . $this->main_param( ' flBtn-label' ) . ' data-role="main" aria-label="' . esc_attr( $param['item_tooltip'] ) . '">' . $this->main_icon() . $this->close_icon() . '</label>';
67 break;
68 case 'link':
69 $target = ! empty( $param['new_tab'] ) ? '_blank' : '_self';
70 $btn .= '<a href="' . esc_url( $link ) . '" target="' . esc_attr( $target ) . '" data-role="main" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
71 break;
72 case 'share':
73 $share = mb_strtolower( $param['item_share'] );
74 $btn .= '<a rel="button" data-role="main" data-share="' . esc_attr( $share ) . '" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
75 break;
76 case 'translate':
77 $glang = $param['gtranslate'] ?? '';
78 $btn .= '<a rel="button" data-role="main" data-google-lang="' . esc_attr( $glang ) . '" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
79 break;
80 case 'print':
81 $btn .= '<a role="button" data-role="main" data-btn-type="print" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
82 break;
83 case 'login':
84 $btn .= '<a rel="nofollow" data-role="main" href="' . wp_login_url( $link ) . '" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
85 break;
86 case 'logout':
87 $btn .= '<a rel="nofollow" data-role="main" href="' . wp_logout_url( $link ) . '" ' . $this->main_param() . '>' . $this->main_icon() . '</a>';
88 break;
89 case 'register':
90 $btn .= '<a rel="nofollow" data-role="main" href="' . wp_registration_url() . '" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
91 break;
92 case 'lostpassword':
93 $btn .= '<a rel="nofollow" href="' . wp_lostpassword_url( $link ) . '" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
94 break;
95 case 'totop':
96 $btn .= '<a role="button" data-role="main" data-btn-type="toTop" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
97 break;
98 case 'tobottom':
99 $btn .= '<a role="button" data-role="main" data-btn-type="tobottom" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
100 break;
101 case 'smoothscroll':
102 $btn .= '<a role="button" data-role="main" data-btn-type="scroll" href="' . esc_url( $link ) . '" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
103 break;
104 case 'goBack':
105 $btn .= '<a role="button" data-role="main" data-btn-type="back" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
106 break;
107 case 'goForward':
108 $btn .= '<a role="button" data-role="main" data-btn-type="forward" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
109 break;
110 case 'next_post':
111 $next_post = get_next_post( true );
112
113 if ( ! empty( $next_post ) ) {
114 $link = get_permalink( $next_post );
115 $btn .= '<a href="' . esc_url( $link ) . '" data-role="main" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
116 }
117 break;
118 case 'previous_post':
119 $previous_post = get_previous_post( true );
120 if ( ! empty( $previous_post ) ) {
121 $link = get_permalink( $previous_post );
122 $btn .= '<a href="' . esc_url( $link ) . '" data-role="main" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
123 }
124 break;
125 case 'menu':
126 if ( ! empty( $param['item_menu'] ) ) {
127 $menu_order = $param['item_menu'];
128 $this->menus[] = $menu_order;
129 $btn .= '<a role="button" data-role="main" data-btn-type="content-menu" data-btn-call-menu="' . esc_attr( $menu_order ) . '" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
130 }
131 break;
132 case 'like':
133 if ( is_singular() ) {
134 $likes = get_post_meta( get_the_ID(), '_wowp_likes', true );
135 }
136 $likes = !empty($likes) ? $likes : 0;
137 $btn .= '<a rel="button" data-role="main" data-btn-type="like" data-counter="'.absint($likes).'" ' . $this->main_param( ' flBtn-label' ) . '>' . $this->main_icon() . '</a>';
138 break;
139 }
140
141 return $btn;
142
143 }
144
145 public function main_param( $defClass = '' ): string {
146 $param = $this->param;
147
148 if ( ! empty( $param['item_tooltip_include'] ) ) {
149 $open = ! empty( $param['item_tooltip_open'] ) ? ' data-btn-tooltip="show"' : '';
150 $tooltip = ' data-tooltip="' . esc_attr( $param['item_tooltip'] ) . '"' . wp_kses( $open, [ 'data-btn-tooltip' => [] ] );
151 } else {
152 $tooltip = '';
153 }
154
155 $class = ! empty( $param['main_button_class'] ) ? $param['main_button_class'] : '';
156 $link_rel = ! empty( $param['link_rel'] ) ? ' rel="' . esc_attr( $param['link_rel'] ) . '"' : '';
157
158 return 'class="' . esc_attr( $class . $defClass ) . '"' . $tooltip . $link_rel;
159 }
160
161 public function main_icon(): string {
162 $param = $this->param;
163 $type = ! empty( $param['button_icon_type'] ) ? $param['button_icon_type'] : 'default';
164 $action = ! empty( $param['close_button_enable'] ) ? ' data-action="open"' : '';
165
166 switch ( $type ) {
167 case 'default':
168 $animate = ! empty( $param['button_icon_anomate'] ) ? ' ' . $param['button_icon_anomate'] : '';
169 $icon = '<i class="notranslate ' . esc_attr( $param['button_icon'] . $animate ) . '"' . wp_kses( $action, [ 'data-action' => [] ] ) . '></i>';
170 break;
171 case 'img':
172 $alt = ! empty( $param['custom_icon_alt'] ) ? $param['custom_icon_alt'] : '';
173 $url = $param['custom_icon_url'];
174 $icon = '<img src="' . esc_url( $url ) . '"' . wp_kses( $action, [ 'data-action' => [] ] ) . ' alt="' . esc_attr( $alt ) . '"' . ' class="notranslate">';
175 break;
176 case 'emoji':
177 $icon = '<span class="notranslate flbtn-emoji"' . wp_kses( $action, [ 'data-action' => [] ] ) . '>' . esc_html( $param['custom_icon_emoji'] ) . '</span>';
178 break;
179 case 'class':
180 $icon = '<i class="notranslate ' . esc_attr( $param['custom_icon_class'] ) . '"' . wp_kses( $action, [ 'data-action' => [] ] ) . '></i>';
181 break;
182 default:
183 $icon = '';
184 break;
185 }
186
187 return $icon;
188 }
189
190 public function close_icon(): string {
191 $param = $this->param;
192 $icon_class = ! empty( $param['close_button_icon'] ) ? $param['close_button_icon'] : 'fas fa-xmark';
193
194 if ( ! empty( $param['close_button_enable'] ) ) {
195 return '<i class="notranslate ' . esc_attr( $icon_class ) . '" data-action="close"></i>';
196 }
197
198 return '';
199
200 }
201
202 public function create_checkbox(): string {
203 $param = $this->param;
204 $checked = ! empty( $param['hold_buttons_open'] ) ? ' checked="checked"' : '';
205 $class = ! empty( $param['extra_checkbox_class'] ) ? ' class="' . esc_attr( $param['extra_checkbox_class'] ) . ' checkbox"' : 'class="checkbox"';
206 $attr = $checked . $class;
207
208 return '<input type="checkbox" id="flBtn-' . absint( $this->id ) . '"' . wp_kses( $attr, [
209 'checked' => [],
210 'class' => []
211 ] ) . '>';
212
213 }
214
215 public function sub_buttons() {
216 $param = $this->param;
217 $btns = [
218 'flBtn-first' => $param['menu_1'] ?? [],
219 'flBtn-second' => $param['menu_2'] ?? [],
220 ];
221
222 $btn = '';
223
224 foreach ( $btns as $key => $val ) {
225 if ( empty( $val['item_type'] ) ) {
226 continue;
227 }
228 $btn .= '<ul class="' . esc_attr( $key ) . '">';
229 $count = count( $val['item_type'] );
230 for ( $i = 0; $i < $count; $i ++ ) {
231
232 $sub_btn = $this->create_subbtn( $val, $i );
233 if ( empty( $sub_btn ) ) {
234 continue;
235 }
236 $btn .= '<li>';
237 $btn .= $sub_btn;
238 $btn .= '</li>';
239
240
241 }
242
243 $btn .= '</ul>';
244 }
245
246 return $btn;
247 }
248
249 public function create_subbtn( $arr, $i ) {
250 $type = ! empty( $arr['item_type'][ $i ] ) ? $arr['item_type'][ $i ] : '';
251 $icon = $this->subbtn_icon( $arr, $i );
252 $link_param = $this->subbtn_param( $arr, $i );
253 $link = ! empty( $arr['item_link'][ $i ] ) ? $arr['item_link'][ $i ] : '#';
254 $btn = '';
255
256 switch ( $type ) {
257 case 'link':
258 $target = ! empty( $arr['new_tab'][ $i ] ) ? '_blank' : '_self';
259 $btn .= '<a href="' . esc_url( $link ) . '" target="' . esc_attr( $target ) . '" ' . $link_param . '>' . $icon . '</a>';
260 break;
261 case 'share':
262 $share = mb_strtolower( $arr['item_share'][ $i ] );
263 $btn .= '<a role="button" data-share="' . esc_attr( $share ) . '" ' . $link_param . '>' . $icon . '</a>';
264 break;
265 case 'translate':
266 $link = '#';
267 $glang = $arr['gtranslate'][ $i ] ?? '';
268 $btn .= '<a href="' . $link . '" data-google-lang="' . esc_attr( $glang ) . '" ' . $link_param . '>' . $icon . '</a>';
269 break;
270 case 'print':
271 $btn .= '<a role="button" data-btn-type="print" ' . $link_param . '>' . $icon . '</a>';
272 break;
273 case 'login':
274 $btn .= '<a rel="nofollow" href="' . wp_login_url( $link ) . '" ' . $link_param . '>' . $icon . '</a>';
275 break;
276 case 'logout':
277 $btn .= '<a rel="nofollow" href="' . wp_logout_url( $link ) . '" ' . $link_param . '>' . $icon . '</a>';
278 break;
279 case 'register':
280 $btn .= '<a rel="nofollow" href="' . wp_registration_url() . '" ' . $link_param . '>' . $icon . '</a>';
281 break;
282 case 'lostpassword':
283 $btn .= '<a rel="nofollow" href="' . wp_lostpassword_url( $link ) . '" ' . $link_param . '>' . $icon . '</a>';
284 break;
285 case 'totop':
286 $btn .= '<a role="button" data-btn-type="toTop" ' . $link_param . '>' . $icon . '</a>';
287 break;
288 case 'tobottom':
289 $btn .= '<a role="button" data-btn-type="tobottom" ' . $link_param . '>' . $icon . '</a>';
290 break;
291 case 'smoothscroll':
292 $btn .= '<a href="' . esc_url( $link ) . '" data-btn-type="scroll" ' . $link_param . '>' . $icon . '</a>';
293 break;
294 case 'goBack':
295 $btn .= '<a role="button" data-btn-type="back" ' . $link_param . '>' . $icon . '</a>';
296 break;
297 case 'goForward':
298 $btn .= '<a role="button" data-btn-type="forward" ' . $link_param . '>' . $icon . '</a>';
299 break;
300 case 'menu':
301 if ( ! empty( $arr['item_menu'][ $i ] ) ) {
302 $menu_order = $arr['item_menu'][ $i ];
303 $this->menus[] = $menu_order;
304 $btn .= '<a role="button" data-btn-type="content-menu" data-btn-call-menu="' . esc_attr( $menu_order ) . '" ' . $link_param . '>' . $icon . '</a>';
305 }
306 break;
307
308 case 'next_post':
309 $next_post = get_next_post( true );
310 if ( ! empty( $next_post ) ) {
311 $link = get_permalink( $next_post );
312 $btn .= '<a href="' . esc_url( $link ) . '" ' . $link_param . '>' . $icon . '</a>';
313 }
314 break;
315 case 'previous_post':
316 $previous_post = get_previous_post( true );
317 if ( ! empty( $previous_post ) ) {
318 $link = get_permalink( $previous_post );
319 $btn .= '<a href="' . esc_url( $link ) . '" ' . $link_param . '>' . $icon . '</a>';
320 }
321 break;
322 case 'like':
323 if ( is_singular() ) {
324 $likes = get_post_meta( get_the_ID(), '_wowp_likes', true );
325 }
326 $likes = !empty($likes) ? $likes : 0;
327 $btn .= '<a rel="button" data-btn-type="like" data-counter="'.absint($likes).'" ' . $link_param . '>' . $icon . '</a>';
328 break;
329 }
330
331 return $btn;
332
333 }
334
335 public function subbtn_icon( $arr, $i ): string {
336 $type = ! empty( $arr['icon_type'][ $i ] ) ? $arr['icon_type'][ $i ] : 'default';
337
338 switch ( $type ) {
339 case 'default':
340 $animate = ! empty( $arr['item_icon_anomate'][ $i ] ) ? ' ' . $arr['item_icon_anomate'][ $i ] : '';
341 $icon = '<i class="notranslate ' . esc_attr( $arr['item_icon'][ $i ] . $animate ) . '"></i>';
342 break;
343 case 'img':
344 $alt = ! empty( $arr['custom_icon_alt'][ $i ] ) ? $arr['custom_icon_alt'][ $i ] : '';
345 $url = $arr['custom_icon_url'][ $i ];
346 $icon = '<img src="' . esc_url( $url ) . '" alt="' . esc_attr( $alt ) . '"' . ' class="notranslate">';
347 break;
348 case 'emoji':
349 $icon = '<span class="notranslate flbtn-emoji">' . esc_html( $arr['custom_icon_emoji'][ $i ] ) . '</span>';
350 break;
351 case 'class':
352 $icon = '<i class="notranslate ' . esc_attr( $arr['custom_icon_class'][ $i ] ) . '"></i>';
353 break;
354 default:
355 $icon = '';
356 break;
357 }
358
359 return $icon;
360 }
361
362 public function subbtn_param( $arr, $i ) {
363 $params = '';
364
365 if ( ! empty( $arr['item_tooltip_include'][ $i ] ) ) {
366 $open = ! empty( $arr['item_tooltip_open'][ $i ] ) ? ' data-btn-tooltip="show"' : '';
367 $params .= 'data-tooltip="' . esc_attr( $arr['item_tooltip'][ $i ] ) . '"' . wp_kses( $open, [ 'data-btn-tooltip' => [] ] );
368 }
369 $params .= ! empty( $arr['button_class'][ $i ] ) ? ' class="' . esc_attr( $arr['button_class'][ $i ] ) . '"' : '';
370 $params .= ! empty( $arr['button_id'][ $i ] ) ? ' id="' . esc_attr( $arr['button_id'][ $i ] ) . '"' : '';
371 $params .= ! empty( $arr['link_rel'][ $i ] ) ? ' rel="' . esc_attr( $arr['link_rel'][ $i ] ) . '"' : '';
372
373 return $params;
374 }
375
376 public function get_menu(): string {
377 $menus = '';
378 if ( empty( $this->menus ) ) {
379 return $menus;
380 }
381
382 foreach ( $this->menus as $menu ) {
383 $menus_obj = wp_get_nav_menu_object( absint( $menu ) );
384 $menu_items = wp_get_nav_menu_items( $menu );
385
386 $menus .= '<div class="flBtn_menu-wrapper is-hidden" data-btn-menu="' . absint( $menu ) . '">';
387 $menus .= '<div class="flBtn_header-menu">';
388 $menus .= '<div class="flBtn_title">' . esc_attr( $menus_obj->name ) . '</div>';
389 $menus .= '<button class="flBtn_close">&times;</button>';
390 $menus .= '</div>';
391
392 $out = wp_nav_menu( [
393 'theme_location' => '',
394 'menu' => $menu,
395 'container' => false,
396 'menu_class' => 'flBtn_menu-list',
397 'echo' => false,
398 'fallback_cb' => '',
399 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
400 'depth' => 0,
401 'walker' => new Navigation,
402 ] );
403
404 $menus .= $out;
405 $menus .= '</div>';
406 }
407
408 return $menus;
409 }
410
411 public function is_main(): bool {
412 return $this->param['item_type'] === 'main';
413 }
414
415 }