PluginProbe
Floating Button – Easily Create Sticky, Fixed & Floating Buttons / 6.0.13
Floating Button – Easily Create Sticky, Fixed & Floating Buttons v6.0.13
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.13, at includes/class-menu-maker.php

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