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

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