PluginProbe
Float menu – awesome floating side menu / 7.2.5
Float menu – awesome floating side menu v7.2.5
7.2.5 trunk 2.1 2.2 3.0.1 3.1 3.2.2 3.3.1 3.5 3.5.1 3.5.2 3.5.3. 3.5.4 4.0 4.1 4.1.1 4.2 4.3 4.3.1 4.3.2 5.0 5.0.1 5.0.2 5.0.3 5.1 All 57 releases
float-menu / classes / Publish / Maker.php

Maker.php in Float menu – awesome floating side menu 7.2.5, at classes/Publish/Maker.php

447 lines 15.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Maker — create floating menu items.
4 *
5 * @package FloatMenuLite\Publish
6 *
7 * Methods:
8 * - __construct($id, $param) — Constructor. Initializes menu ID, parameters, menu items.
9 * - init() — Renders the main menu container and returns HTML output.
10 * - create_properties() — Generates CSS properties for the menu.
11 * - create_options() — Returns JSON-encoded data attributes for frontend JS.
12 * - create_items() — Renders all menu items (buttons) in a loop.
13 * - create_item($i, $sub, $next_sub) — Renders a single menu item (with/without sub-menu).
14 * - create_link($i) — Builds the <a> or <form> for the menu item.
15 * - create_link_properties($i) — Builds inline CSS properties for a menu item.
16 * - create_action($i) — Builds href/action/target attributes for a menu link.
17 * - create_icon($i) — Renders icon/image/text/emoji for a menu item.
18 * - create_label($i) — Renders label/tooltip or search field for a menu item.
19 */
20
21 namespace FloatMenuLite\Publish;
22
23
24 use FloatMenuLite\WOWP_Plugin;
25
26 defined( 'ABSPATH' ) || exit;
27
28 class Maker {
29 private $id;
30 private $param;
31 private int $count;
32 /**
33 * @var array|mixed
34 */
35 private $menu;
36
37 public function __construct( $id, $param ) {
38 $this->id = $id;
39 $this->param = $param;
40 $this->count = ! empty( $param['menu_1']['item_type'] ) ? count( $param['menu_1']['item_type'] ) : 0;
41 $this->menu = ! empty( $this->count ) ? $param['menu_1'] : [];
42 }
43
44 public function init(): string {
45 $out = '';
46
47 if ( $this->count === 0 ) {
48 return $out;
49 }
50
51 $classes = 'floating-menu notranslate float-menu-' . absint( $this->id );
52 $style = $this->create_properties();
53 $options = $this->create_options();
54
55 $out = '<div dir="ltr" class="' . esc_attr( $classes ) . '" style="' . esc_attr( $style ) . '" data-float-menu="' . esc_attr( $options ) . '">';
56 $out .= ! empty( $this->param['horizontal'] ) ? '<ul class="fm-bar is-horizontal">' : '<ul class="fm-bar">';
57 $out .= $this->create_items();
58 $out .= '</ul></div>';
59
60 return $out;
61 }
62
63 private function create_properties(): string {
64 $param = $this->param;
65 $out = '';
66 $properties = [];
67
68 if ( isset( $param['labelSpeed'] ) && $param['labelSpeed'] !== '9999' ) {
69 $properties['--fm-link-duration'] = $param['labelSpeed'];
70 }
71
72 if ( isset( $param['zindex'] ) && $param['zindex'] !== '400' ) {
73 $properties['--fm-z-index'] = $param['zindex'];
74 }
75
76 if ( isset( $param['iconSize'] ) && $param['iconSize'] !== '24' ) {
77 $properties['--fm-icon-size'] = $param['iconSize'];
78 }
79
80 if ( ! empty( $param['boxSize_on'] ) && ! empty( $param['boxSize'] ) ) {
81 $properties['--fm-icon-box'] = $param['boxSize'];
82 }
83
84 if ( isset( $param['textSize'] ) && $param['textSize'] !== '12' ) {
85 $properties['--fm-icon-text'] = $param['textSize'];
86 }
87
88 if ( isset( $param['labelSize'] ) && $param['labelSize'] !== '15' ) {
89 $properties['--fm-label-size'] = $param['labelSize'];
90 }
91
92 $properties = apply_filters( WOWP_Plugin::PREFIX . '_maker_properties', $properties, $param ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
93
94
95 if ( ! empty( $properties ) ) {
96 foreach ( $properties as $property => $value ) {
97 $out .= $property . ':' . $value . ';';
98 }
99 }
100
101 return $out;
102
103 }
104
105 private function create_options() {
106 $param = $this->param;
107 $data = [];
108 $data['position'] = [
109 $param['menu'] ?? 'left',
110 ! empty( $param['align'] ) ? $param['align'] : 'center',
111 ];
112
113 $data['appearance'] = [
114 'shape' => '-' . ( $param['shape'] ?? 'square' ),
115 ];
116
117 if ( ! isset( $param['sideSpace'] ) || $param['sideSpace'] === 'true' ) {
118 $data['appearance']['sideSpace'] = true;
119 }
120
121 if ( ! isset( $param['buttonSpace'] ) || $param['buttonSpace'] === 'true' ) {
122 $data['appearance']['buttonSpace'] = true;
123 }
124
125 if ( ! isset( $param['labelConnected'] ) || $param['labelConnected'] === 'true' ) {
126 $data['appearance']['labelConnected'] = true;
127 }
128
129 if ( ! isset( $param['subSpace'] ) || $param['subSpace'] === 'true' ) {
130 $data['appearance']['subSpace'] = true;
131 }
132
133
134 if ( ! empty( $param['side_offset'] ) || ! empty( $param['top_offset'] ) ) {
135 $sideOffset = ! empty( $param['side_offset'] ) ? esc_attr( $param['side_offset'] ) : 0;
136 $topOffset = ! empty( $param['top_offset'] ) ? esc_attr( $param['top_offset'] ) : 0;
137 $data['offset'] = [ $sideOffset, $topOffset ];
138 }
139
140 $data['mobile'] = [
141 $param['mobilieScreen'] ?? 480,
142 $param['mobiliconSize'] ?? 24,
143 $param['mobillabelSize'] ?? 15,
144 ];
145 $data['mobile'][] = isset( $param['boxSizeMobile'] ) && ! empty( $param['boxSizeMobile_on'] ) ? $param['boxSizeMobile'] : 0;
146 $data['mobile'][] = isset( $param['textSizeMobile'] ) ? $param['textSizeMobile'] : 12;
147
148 if ( ! empty( $param['mobile_on'] ) || ! empty( $param['desktop_on'] ) ) {
149 $data['screen'] = [];
150 if ( ! empty( $param['mobile_on'] ) ) {
151 $data['screen']['small'] = $param['mobile'] ?? 480;
152 }
153 if ( ! empty( $param['desktop_on'] ) ) {
154 $data['screen']['large'] = $param['desktop'] ?? 1024;
155 }
156 }
157
158 $data['label'] = [];
159
160 if ( empty( $param['labelSpace'] ) || $param['labelSpace'] === 'true' ) {
161 $data['label']['space'] = 2;
162 }
163
164 if ( ! empty( $param['labelsOn'] ) && $param['labelsOn'] === 'false' ) {
165 $data['label']['off'] = true;
166 }
167
168 if ( ! empty( $param['mobile_rules_on'] ) ) {
169 $data['mobileRules'] = true;
170 }
171
172 $data['remove'] = true;
173
174 $data = apply_filters( WOWP_Plugin::PREFIX . '_maker_options', $data, $param ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
175
176 return wp_json_encode( $data );
177 }
178
179
180 private function create_items(): string {
181 $out = '';
182 $menu = $this->menu;
183
184 for ( $i = 0; $i < $this->count; $i ++ ) {
185 $type = $menu['item_type'][ $i ] ?? '';
186
187 if ( $type === 'next_post' ) {
188 $next_post = get_next_post( true );
189 if ( empty( $next_post ) ) {
190 continue;
191 }
192 } elseif ( $type === 'previous_post' ) {
193 $previous_post = get_previous_post( true );
194 if ( empty( $previous_post ) ) {
195 continue;
196 }
197 } elseif ( $type === 'back' ) {
198 if ( empty( $_SERVER['HTTP_REFERER'] ) ) {
199 continue;
200 }
201 }
202
203 $next_i = $i + 1;
204 $sub = ! empty( $menu['item_sub'][ $i ] ) ? absint( $menu['item_sub'][ $i ] ) : 0;
205 $next_sub = ! empty( $menu['item_sub'][ $next_i ] ) ? absint( $menu['item_sub'][ $next_i ] ) : 0;
206 $out .= $this->create_item( $i, $sub, $next_sub );
207
208 }
209
210 return $out;
211 }
212
213 private function create_item( $i, $sub, $next_sub ): string {
214 $item = $this->create_link( $i );
215 $properties = $this->create_link_properties( $i );
216
217 if ( $sub === 0 && $next_sub === 1 ) {
218 return "<li class='fm-item fm-has-sub' style='" . esc_attr( $properties ) . "'>
219 {$item}
220 <ul class='fm-bar fm-sub'>";
221 }
222
223 if ( $sub === 1 && $next_sub === 0 ) {
224 return "<li class='fm-item' style='" . esc_attr( $properties ) . "'>{$item}</li>
225 </ul></li>";
226 }
227
228 return "<li class='fm-item' style='" . esc_attr( $properties ) . "'>{$item}</li>";
229 }
230
231 private function create_link( $i ): string {
232
233 $class = ! empty( $this->menu['button_class'][ $i ] ) ? ' ' . $this->menu['button_class'][ $i ] : '';
234 $id = ! empty( $this->menu['button_id'][ $i ] ) ? $this->menu['button_id'][ $i ] : '';
235 $rel = ! empty( $this->menu['link_rel'][ $i ] ) ? $this->menu['link_rel'][ $i ] : '';
236 $aria = ! empty( $this->menu['aria_label'][ $i ] ) ? $this->menu['aria_label'][ $i ] : '';
237
238 if ( ! empty( $this->menu['hold_open'][ $i ] ) ) {
239 $class .= ' -active fm-hold-open';
240 }
241
242 if ( ! empty( $this->menu['hold_open'][ $i ] ) && ! empty( $this->menu['hover_hide'][ $i ] ) ) {
243 $class .= ' fm-hovering-hide';
244 }
245
246 if ( empty( $this->menu['item_tooltip'][ $i ] ) ) {
247 $class .= ' -label-hidden';
248 }
249
250 $out = '<a';
251 if ( $this->menu['item_type'][ $i ] === 'search' ) {
252 $out = '<form';
253 }
254 $out .= ! empty( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
255 $out .= ' class="fm-link' . esc_attr( $class ) . '"';
256 $out .= ! empty( $rel ) ? ' rel="' . esc_attr( $rel ) . '"' : '';
257 $out .= ! empty( $aria ) ? ' aria-label="' . esc_attr( $aria ) . '"' : '';
258 $out .= $this->create_action( $i );
259 $out .= '>';
260 $out .= $this->create_icon( $i );
261 $out .= $this->create_label( $i );
262
263 if ( $this->menu['item_type'][ $i ] === 'search' ) {
264 $out .= '</form>';
265 } else {
266 $out .= '</a>';
267 }
268
269 $out = apply_filters( WOWP_Plugin::PREFIX . '_maker_link', $out, $this->menu, $this->param, $i, $this->id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
270
271 return $out;
272 }
273
274 private function create_link_properties( $i ): string {
275 $menu = $this->menu;
276 $out = '';
277
278 $data = [
279 '--fm-color' => $menu['color'][ $i ] ?? '#ffffff',
280 '--fm-background' => $menu['bcolor'][ $i ] ?? '#184c72',
281 '--fm-hover-color' => $menu['hcolor'][ $i ] ?? '#ffffff',
282 '--fm-hover-background' => $menu['hbcolor'][ $i ] ?? '#184c72',
283 ];
284
285
286 if ( ! empty( $data ) ) {
287 foreach ( $data as $property => $value ) {
288 $out .= $property . ':' . $value . ';';
289 }
290 }
291
292 return $out;
293 }
294
295 private function create_action( $i ): string {
296 $menu = $this->menu;
297 $type = $menu['item_type'][ $i ] ?? 'link';
298 $target = ! empty( $menu['new_tab'][ $i ] ) ? '_blank' : '_self';
299 $out = ' ';
300
301 switch ( $type ) {
302 case 'link':
303 $link = ! empty( $menu['item_link'][ $i ] ) ? $menu['item_link'][ $i ] : '#';
304 $out .= 'href="' . esc_attr( $link ) . '" target="' . esc_attr( $target ) . '"';
305 break;
306 case 'login':
307 case 'logout':
308 case 'lostpassword':
309 $redirect = ! empty( $menu['item_link'][ $i ] ) ? $menu['item_link'][ $i ] : '#';
310 $link = call_user_func( 'wp_' . $type . '_url', $redirect );
311 $out .= 'href="' . esc_url( $link ) . '" target="' . esc_attr( $target ) . '"';
312 break;
313 case 'register':
314 $link = wp_registration_url();
315 $out .= 'href="' . esc_url( $link ) . '" target="' . esc_attr( $target ) . '"';
316 break;
317 default:
318 $link = ! empty( $menu['item_link'][ $i ] ) ? $menu['item_link'][ $i ] : '#';
319 $out .= 'href="' . esc_attr( $link ) . '" target="' . esc_attr( $target ) . '"';
320 break;
321 }
322
323 $out = apply_filters( WOWP_Plugin::PREFIX . '_maker_action', $out, $menu, $i, $type, $target ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
324
325 return $out;
326 }
327
328 private function create_icon( $i ): string {
329 $menu = $this->menu;
330
331 $type = ! empty( $menu['icon_type'][ $i ] ) ? $menu['icon_type'][ $i ] : 'icon';
332 $rotate = ! empty( $menu['icon_rotate'][ $i ] ) ? $menu['icon_rotate'][ $i ] : 0;
333 $flip = ! empty( $menu['icon_flip'][ $i ] ) ? $menu['icon_flip'][ $i ] : '';
334 $animation = ! empty( $menu['item_icon_anomate'][ $i ] ) ? $menu['item_icon_anomate'][ $i ] : '';
335
336 $class = '';
337 if ( ! empty( $animation ) ) {
338 $class .= ' ' . $animation;
339 }
340 if ( ! empty( $flip ) ) {
341 $class .= ' ' . $flip;
342 }
343 if ( ! empty( $menu['image_full'][ $i ] ) && $type === 'image' ) {
344 $class .= ' -full';
345 }
346
347 $style = '';
348 if ( ! empty( $rotate ) ) {
349 $style .= '--_rotate:' . $rotate . ';';
350 }
351
352 if ( ! empty( $this->menu['icon_text_weight'][ $i ] ) && $this->menu['icon_text_weight'][ $i ] !== 'normal' ) {
353 $style .= '--fm-icon-text-weight:' . esc_attr( $this->menu['icon_text_weight'][ $i ] ) . ';';
354 }
355
356 if ( ! empty( $menu['radius_icon'][ $i ] ) ) {
357 $style .= '--fm-icon-radius: ' . ( $menu['icon_radius_top_left'][ $i ] ?? 0 ) . 'px ';
358 $style .= ( $menu['icon_radius_top_right'][ $i ] ?? 0 ) . 'px ';
359 $style .= ( $menu['icon_radius_bottom_right'][ $i ] ?? 0 ) . 'px ';
360 $style .= ( $menu['icon_radius_bottom_left'][ $i ] ?? 0 ) . 'px;';
361 }
362
363 $out = '<span class="fm-icon' . esc_attr( $class ) . '"';
364 $out .= ! empty( $style ) ? ' style="' . esc_attr( $style ) . '"' : '';
365 $out .= '>';
366
367 switch ( $type ) {
368 case 'icon':
369 $icon = ! empty( $menu['item_icon'][ $i ] ) ? $menu['item_icon'][ $i ] : '';
370 $out .= '<span class="' . esc_attr( $icon ) . '"></span>';
371 break;
372 case 'image':
373 $link = ! empty( $menu['item_custom_link'][ $i ] ) ? $menu['item_custom_link'][ $i ] : '';
374 $alt = ! empty( $menu['image_alt'][ $i ] ) ? $menu['image_alt'][ $i ] : '';
375 $out .= '<img src="' . esc_url( $link ) . '" alt="' . esc_attr( $alt ) . '">'; // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
376 break;
377 case 'class':
378 $icon = ! empty( $menu['icon_class'][ $i ] ) ? $menu['icon_class'][ $i ] : '';
379 $out .= '<span class="' . esc_attr( $icon ) . '"></span>';
380 break;
381 case 'emoji':
382 $icon = ! empty( $menu['icon_emoji'][ $i ] ) ? $menu['icon_emoji'][ $i ] : '';
383 $out .= '<span>' . wp_kses_post( $icon ) . '</span>';
384 break;
385 case 'text':
386 $icon = ! empty( $menu['item_custom_text'][ $i ] ) ? $menu['item_custom_text'][ $i ] : '';
387 $out .= '<span>' . wp_kses_post( $icon ) . '</span>';
388 break;
389 }
390 $out .= ! empty( $menu['icon_text'][ $i ] ) ? '<span class="icon-text">' . esc_html( $menu['icon_text'][ $i ] ) . '</span>' : '';
391
392 $out .= '</span>';
393
394 return $out;
395 }
396
397 private function create_label( $i ): string {
398 $menu = $this->menu;
399 $style = '';
400 if ( ! empty( $this->menu['item_tooltip_font'][ $i ] ) && $this->menu['item_tooltip_font'][ $i ] !== 'inherit' ) {
401 $style .= '--fm-label-font:' . esc_attr( $this->menu['item_tooltip_font'][ $i ] ) . ';';
402 }
403 if ( ! empty( $this->menu['item_tooltip_style'][ $i ] ) && $this->menu['item_tooltip_style'][ $i ] !== 'normal' ) {
404 $style .= '--fm-label-font-style:' . esc_attr( $this->menu['item_tooltip_style'][ $i ] ) . ';';
405 }
406 if ( ! empty( $this->menu['item_tooltip_weight'][ $i ] ) && $this->menu['item_tooltip_weight'][ $i ] !== 'normal' ) {
407 $style .= '--fm-label-weight:' . esc_attr( $this->menu['item_tooltip_weight'][ $i ] ) . ';';
408 }
409
410 if ( ! empty( $menu['radius_label'][ $i ] ) ) {
411 $style .= '--fm-label-radius: ' . ( $menu['label_radius_top_left'][ $i ] ?? 0 ) . 'px ';
412 $style .= ( $menu['label_radius_top_right'][ $i ] ?? 0 ) . 'px ';
413 $style .= ( $menu['label_radius_bottom_right'][ $i ] ?? 0 ) . 'px ';
414 $style .= ( $menu['label_radius_bottom_left'][ $i ] ?? 0 ) . 'px;';
415 }
416
417 $icon_type = ! empty( $this->menu['icon_type'][ $i ] ) ? $this->menu['icon_type'][ $i ] : 'icon';
418 $icon = ! empty( $this->menu['item_icon'][ $i ] ) ? $this->menu['item_icon'][ $i ] : '';
419
420 $class = '';
421 if ( $icon_type === 'icon' && empty( $icon ) ) {
422 $class = ' fm-empty-icon';
423 }
424
425
426 $out = '<span class="fm-label' . esc_attr( $class ) . '"';
427 $out .= ! empty( $style ) ? ' style="' . esc_attr( $style ) . '"' : '';
428 $out .= '>';
429
430 $label = ! empty( $this->menu['item_tooltip'][ $i ] ) ? $this->menu['item_tooltip'][ $i ] : '';
431
432 if ( $this->menu['item_type'][ $i ] === 'search' ) {
433 $out .= '<input type="search" class="fm-input" name="s" placeholder="' . esc_attr( $label ) . '">';
434 } else if ( ! empty( $label ) ) {
435 if ( is_email( $label ) ) {
436 $out .= antispambot( $label );
437 } else {
438 $out .= esc_html( $label );
439 }
440 }
441
442 $out .= '</span>';
443
444 return $out;
445 }
446
447 }