| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Maker\Button |
| 5 |
* |
| 6 |
* This class is responsible for generating the floating menu HTML using the provided parameters. |
| 7 |
* |
| 8 |
* @package ButtonGenerator |
| 9 |
* @subpackage Public |
| 10 |
* @author Dmytro Lobov <dev@wow-company.com>, Wow-Company |
| 11 |
* @copyright 2024 Dmytro Lobov |
| 12 |
* @license GPL-2.0+ |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace ButtonGenerator\Maker; |
| 16 |
|
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
class Button { |
| 20 |
/** |
| 21 |
* @var mixed |
| 22 |
*/ |
| 23 |
private $id; |
| 24 |
/** |
| 25 |
* @var mixed |
| 26 |
*/ |
| 27 |
private $param; |
| 28 |
|
| 29 |
public function __construct( $id, $param ) { |
| 30 |
$this->id = $id; |
| 31 |
$this->param = $param; |
| 32 |
} |
| 33 |
|
| 34 |
public function init(): string { |
| 35 |
$param = $this->param; |
| 36 |
|
| 37 |
$float = ''; |
| 38 |
if ( $param['type'] === 'floating' ) { |
| 39 |
$float = ' btg-' . $param['location']; |
| 40 |
} |
| 41 |
|
| 42 |
$class = ''; |
| 43 |
if ( ! empty( $param['button_class'] ) ) { |
| 44 |
$class = ' ' . $param['button_class']; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
$hover_effect = ''; |
| 49 |
if ( ! empty( $param['hover_effects'] ) && $param['hover_effects'] !== 'none' ) { |
| 50 |
$hover_effect = ' ' . str_replace( "hvr-", "_", $param['hover_effects'] ); |
| 51 |
} |
| 52 |
|
| 53 |
$menu = '<button translate="no" class="btg-button btg-button-' . absint( $this->id ); |
| 54 |
$menu .= esc_attr( $float ); |
| 55 |
$menu .= esc_attr( $class ); |
| 56 |
$menu .= '" data-btnid="' . absint( $this->id ) . '"'; |
| 57 |
if ( ! empty( $param['button_id'] ) ) { |
| 58 |
$menu .= ' id="' . esc_attr( $param['button_id'] ) . '"'; |
| 59 |
} |
| 60 |
if ( ! empty( $param['aria_label'] ) ) { |
| 61 |
$menu .= ' aria-label="' . esc_attr( $param['aria_label'] ) . '"'; |
| 62 |
} |
| 63 |
$menu .= $this->type(); |
| 64 |
$menu .= '>'; |
| 65 |
$menu .= $this->text(); |
| 66 |
$menu .= $this->icon(); |
| 67 |
$menu .= '</button>'; |
| 68 |
|
| 69 |
return $menu; |
| 70 |
} |
| 71 |
|
| 72 |
private function type(): string { |
| 73 |
$param = $this->param; |
| 74 |
$link = ''; |
| 75 |
$target = ''; |
| 76 |
$action = ''; |
| 77 |
|
| 78 |
switch ( $param['item_type'] ) { |
| 79 |
case 'link': |
| 80 |
$action = 'link'; |
| 81 |
$link = $param['item_link'] ?? '#'; |
| 82 |
$target = ! empty( $param['new_tab'] ) ? '_blank' : '_self'; |
| 83 |
break; |
| 84 |
case 'login': |
| 85 |
$action = 'link'; |
| 86 |
$link = wp_login_url( $param['item_link'] ); |
| 87 |
break; |
| 88 |
case 'logout': |
| 89 |
$action = 'link'; |
| 90 |
$link = wp_logout_url( $param['item_link'] ); |
| 91 |
break; |
| 92 |
case 'register': |
| 93 |
$action = 'link'; |
| 94 |
$link = wp_registration_url(); |
| 95 |
break; |
| 96 |
case 'lostpassword': |
| 97 |
$action = 'link'; |
| 98 |
$link = wp_lostpassword_url( $param['item_link'] ); |
| 99 |
break; |
| 100 |
} |
| 101 |
|
| 102 |
$out = ''; |
| 103 |
$out .= ( ! empty( $link ) ) ? ' data-url="' . esc_attr( $link ) . '"' : ''; |
| 104 |
$out .= ! empty( $action ) ? ' data-action="' . esc_attr( $action ) . '"' : ''; |
| 105 |
$out .= ! empty( $target ) ? ' data-target="' . esc_attr( $target ) . '"' : ''; |
| 106 |
|
| 107 |
|
| 108 |
return $out; |
| 109 |
} |
| 110 |
|
| 111 |
private function text(): ?string { |
| 112 |
$param = $this->param; |
| 113 |
if ( $param['appearance'] === 'icon' ) { |
| 114 |
return ''; |
| 115 |
} |
| 116 |
|
| 117 |
return str_replace( '\n', '<br/>', esc_html( $param['text'] ) ); |
| 118 |
} |
| 119 |
|
| 120 |
private function icon(): string { |
| 121 |
$param = $this->param; |
| 122 |
if ( $param['appearance'] === 'text' ) { |
| 123 |
return ''; |
| 124 |
} |
| 125 |
$out = ''; |
| 126 |
$icon = $param['icon'] ?? ''; |
| 127 |
$type = $param['icon_type'] ?? 'icon'; |
| 128 |
$rotate = ''; |
| 129 |
if ( $param['rotate_icon'] !== 'none' && $param['rotate_icon'] !== 'custom' ) { |
| 130 |
$rotate = ' ' . $param['rotate_icon']; |
| 131 |
} |
| 132 |
|
| 133 |
return '<span class="' . esc_attr( $icon ) . ' btg-icon' . esc_attr( $rotate ) . '"></span>'; |
| 134 |
} |
| 135 |
} |