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

417 lines 15.4 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 $icon = '<img src="' . esc_url( $url ) . '"' . wp_kses( $action, [ 'data-action' => [] ] ) . ' alt="' . esc_attr( $alt ) . '"' . ' class="notranslate">';
177 break;
178 case 'emoji':
179 $icon = '<span class="notranslate flbtn-emoji"' . wp_kses( $action, [ 'data-action' => [] ] ) . '>' . esc_html( $param['custom_icon_emoji'] ) . '</span>';
180 break;
181 case 'class':
182 $icon = '<i class="notranslate ' . esc_attr( $param['custom_icon_class'] ) . '"' . wp_kses( $action, [ 'data-action' => [] ] ) . '></i>';
183 break;
184 default:
185 $icon = '';
186 break;
187 }
188
189 return $icon;
190 }
191
192 public function close_icon(): string {
193 $param = $this->param;
194 $icon_class = ! empty( $param['close_button_icon'] ) ? $param['close_button_icon'] : 'fas fa-xmark';
195
196 if ( ! empty( $param['close_button_enable'] ) ) {
197 return '<i class="notranslate ' . esc_attr( $icon_class ) . '" data-action="close"></i>';
198 }
199
200 return '';
201
202 }
203
204 public function create_checkbox(): string {
205 $param = $this->param;
206 $checked = ! empty( $param['hold_buttons_open'] ) ? ' checked="checked"' : '';
207 $class = ! empty( $param['extra_checkbox_class'] ) ? ' class="' . esc_attr( $param['extra_checkbox_class'] ) . ' checkbox"' : 'class="checkbox"';
208 $attr = $checked . $class;
209
210 return '<input type="checkbox" id="flBtn-' . absint( $this->id ) . '"' . wp_kses( $attr, [
211 'checked' => [],
212 'class' => []
213 ] ) . '>';
214
215 }
216
217 public function sub_buttons() {
218 $param = $this->param;
219 $btns = [
220 'flBtn-first' => $param['menu_1'] ?? [],
221 'flBtn-second' => $param['menu_2'] ?? [],
222 ];
223
224 $btn = '';
225
226 foreach ( $btns as $key => $val ) {
227 if ( empty( $val['item_type'] ) ) {
228 continue;
229 }
230 $btn .= '<ul class="' . esc_attr( $key ) . '">';
231 $count = count( $val['item_type'] );
232 for ( $i = 0; $i < $count; $i ++ ) {
233
234 $sub_btn = $this->create_subbtn( $val, $i );
235 if ( empty( $sub_btn ) ) {
236 continue;
237 }
238 $btn .= '<li>';
239 $btn .= $sub_btn;
240 $btn .= '</li>';
241
242
243 }
244
245 $btn .= '</ul>';
246 }
247
248 return $btn;
249 }
250
251 public function create_subbtn( $arr, $i ) {
252 $type = ! empty( $arr['item_type'][ $i ] ) ? $arr['item_type'][ $i ] : '';
253 $icon = $this->subbtn_icon( $arr, $i );
254 $link_param = $this->subbtn_param( $arr, $i );
255 $link = ! empty( $arr['item_link'][ $i ] ) ? $arr['item_link'][ $i ] : '#';
256 $btn = '';
257
258 switch ( $type ) {
259 case 'link':
260 $target = ! empty( $arr['new_tab'][ $i ] ) ? '_blank' : '_self';
261 $btn .= '<a href="' . esc_url( $link ) . '" target="' . esc_attr( $target ) . '" ' . $link_param . '>' . $icon . '</a>';
262 break;
263 case 'share':
264 $share = mb_strtolower( $arr['item_share'][ $i ] );
265 $btn .= '<a role="button" data-share="' . esc_attr( $share ) . '" ' . $link_param . '>' . $icon . '</a>';
266 break;
267 case 'translate':
268 $link = '#';
269 $glang = $arr['gtranslate'][ $i ] ?? '';
270 $btn .= '<a href="' . $link . '" data-google-lang="' . esc_attr( $glang ) . '" ' . $link_param . '>' . $icon . '</a>';
271 break;
272 case 'print':
273 $btn .= '<a role="button" data-btn-type="print" ' . $link_param . '>' . $icon . '</a>';
274 break;
275 case 'login':
276 $btn .= '<a rel="nofollow" href="' . wp_login_url( $link ) . '" ' . $link_param . '>' . $icon . '</a>';
277 break;
278 case 'logout':
279 $btn .= '<a rel="nofollow" href="' . wp_logout_url( $link ) . '" ' . $link_param . '>' . $icon . '</a>';
280 break;
281 case 'register':
282 $btn .= '<a rel="nofollow" href="' . wp_registration_url() . '" ' . $link_param . '>' . $icon . '</a>';
283 break;
284 case 'lostpassword':
285 $btn .= '<a rel="nofollow" href="' . wp_lostpassword_url( $link ) . '" ' . $link_param . '>' . $icon . '</a>';
286 break;
287 case 'totop':
288 $btn .= '<a role="button" data-btn-type="toTop" ' . $link_param . '>' . $icon . '</a>';
289 break;
290 case 'tobottom':
291 $btn .= '<a role="button" data-btn-type="tobottom" ' . $link_param . '>' . $icon . '</a>';
292 break;
293 case 'smoothscroll':
294 $btn .= '<a href="' . esc_url( $link ) . '" data-btn-type="scroll" ' . $link_param . '>' . $icon . '</a>';
295 break;
296 case 'goBack':
297 $btn .= '<a role="button" data-btn-type="back" ' . $link_param . '>' . $icon . '</a>';
298 break;
299 case 'goForward':
300 $btn .= '<a role="button" data-btn-type="forward" ' . $link_param . '>' . $icon . '</a>';
301 break;
302 case 'menu':
303 if ( ! empty( $arr['item_menu'][ $i ] ) ) {
304 $menu_order = $arr['item_menu'][ $i ];
305 $this->menus[] = $menu_order;
306 $btn .= '<a role="button" data-btn-type="content-menu" data-btn-call-menu="' . esc_attr( $menu_order ) . '" ' . $link_param . '>' . $icon . '</a>';
307 }
308 break;
309
310 case 'next_post':
311 $next_post = get_next_post( true );
312 if ( ! empty( $next_post ) ) {
313 $link = get_permalink( $next_post );
314 $btn .= '<a href="' . esc_url( $link ) . '" ' . $link_param . '>' . $icon . '</a>';
315 }
316 break;
317 case 'previous_post':
318 $previous_post = get_previous_post( true );
319 if ( ! empty( $previous_post ) ) {
320 $link = get_permalink( $previous_post );
321 $btn .= '<a href="' . esc_url( $link ) . '" ' . $link_param . '>' . $icon . '</a>';
322 }
323 break;
324 case 'like':
325 if ( is_singular() ) {
326 $likes = get_post_meta( get_the_ID(), '_wowp_likes', true );
327 }
328 $likes = !empty($likes) ? $likes : 0;
329 $btn .= '<a rel="button" data-btn-type="like" data-counter="'.absint($likes).'" ' . $link_param . '>' . $icon . '</a>';
330 break;
331 }
332
333 return $btn;
334
335 }
336
337 public function subbtn_icon( $arr, $i ): string {
338 $type = ! empty( $arr['icon_type'][ $i ] ) ? $arr['icon_type'][ $i ] : 'default';
339
340 switch ( $type ) {
341 case 'default':
342 $animate = ! empty( $arr['item_icon_anomate'][ $i ] ) ? ' ' . $arr['item_icon_anomate'][ $i ] : '';
343 $icon = '<i class="notranslate ' . esc_attr( $arr['item_icon'][ $i ] . $animate ) . '"></i>';
344 break;
345 case 'img':
346 $alt = ! empty( $arr['custom_icon_alt'][ $i ] ) ? $arr['custom_icon_alt'][ $i ] : '';
347 $url = $arr['custom_icon_url'][ $i ];
348 $icon = '<img src="' . esc_url( $url ) . '" alt="' . esc_attr( $alt ) . '"' . ' class="notranslate">';
349 break;
350 case 'emoji':
351 $icon = '<span class="notranslate flbtn-emoji">' . esc_html( $arr['custom_icon_emoji'][ $i ] ) . '</span>';
352 break;
353 case 'class':
354 $icon = '<i class="notranslate ' . esc_attr( $arr['custom_icon_class'][ $i ] ) . '"></i>';
355 break;
356 default:
357 $icon = '';
358 break;
359 }
360
361 return $icon;
362 }
363
364 public function subbtn_param( $arr, $i ) {
365 $params = '';
366
367 if ( ! empty( $arr['item_tooltip_include'][ $i ] ) ) {
368 $open = ! empty( $arr['item_tooltip_open'][ $i ] ) ? ' data-btn-tooltip="show"' : '';
369 $params .= 'data-tooltip="' . esc_attr( $arr['item_tooltip'][ $i ] ) . '"' . wp_kses( $open, [ 'data-btn-tooltip' => [] ] );
370 }
371 $params .= ! empty( $arr['button_class'][ $i ] ) ? ' class="' . esc_attr( $arr['button_class'][ $i ] ) . '"' : '';
372 $params .= ! empty( $arr['button_id'][ $i ] ) ? ' id="' . esc_attr( $arr['button_id'][ $i ] ) . '"' : '';
373 $params .= ! empty( $arr['link_rel'][ $i ] ) ? ' rel="' . esc_attr( $arr['link_rel'][ $i ] ) . '"' : '';
374
375 return $params;
376 }
377
378 public function get_menu(): string {
379 $menus = '';
380 if ( empty( $this->menus ) ) {
381 return $menus;
382 }
383
384 foreach ( $this->menus as $menu ) {
385 $menus_obj = wp_get_nav_menu_object( absint( $menu ) );
386 $menu_items = wp_get_nav_menu_items( $menu );
387
388 $menus .= '<div class="flBtn_menu-wrapper is-hidden" data-btn-menu="' . absint( $menu ) . '">';
389 $menus .= '<div class="flBtn_header-menu">';
390 $menus .= '<div class="flBtn_title">' . esc_attr( $menus_obj->name ) . '</div>';
391 $menus .= '<button class="flBtn_close">&times;</button>';
392 $menus .= '</div>';
393
394 $out = wp_nav_menu( [
395 'theme_location' => '',
396 'menu' => $menu,
397 'container' => false,
398 'menu_class' => 'flBtn_menu-list',
399 'echo' => false,
400 'fallback_cb' => '',
401 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
402 'depth' => 0,
403 'walker' => new Navigation,
404 ] );
405
406 $menus .= $out;
407 $menus .= '</div>';
408 }
409
410 return $menus;
411 }
412
413 public function is_main(): bool {
414 return $this->param['item_type'] === 'main';
415 }
416
417 }