| 1 |
<?php |
| 2 |
/** |
| 3 |
* Toggle block. |
| 4 |
* |
| 5 |
* @package HivePress\Blocks |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace HivePress\Blocks; |
| 9 |
|
| 10 |
use HivePress\Helpers as hp; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Renders a toggle link. |
| 17 |
*/ |
| 18 |
class Toggle extends Block { |
| 19 |
|
| 20 |
/** |
| 21 |
* Toggle view. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $view = 'link'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Icon name. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $icon; |
| 33 |
|
| 34 |
/** |
| 35 |
* Request URL. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $url; |
| 40 |
|
| 41 |
/** |
| 42 |
* Toggle captions. |
| 43 |
* |
| 44 |
* @var array |
| 45 |
*/ |
| 46 |
protected $captions = []; |
| 47 |
|
| 48 |
/** |
| 49 |
* Toggle states. |
| 50 |
* |
| 51 |
* @var array |
| 52 |
*/ |
| 53 |
protected $states = []; |
| 54 |
|
| 55 |
/** |
| 56 |
* HTML attributes. |
| 57 |
* |
| 58 |
* @var array |
| 59 |
*/ |
| 60 |
protected $attributes = []; |
| 61 |
|
| 62 |
/** |
| 63 |
* Is toggle active? |
| 64 |
* |
| 65 |
* @var bool |
| 66 |
*/ |
| 67 |
protected $active = false; |
| 68 |
|
| 69 |
/** |
| 70 |
* Bootstraps block properties. |
| 71 |
*/ |
| 72 |
protected function boot() { |
| 73 |
$attributes = []; |
| 74 |
|
| 75 |
// Normalize icon. |
| 76 |
if ( $this->icon ) { |
| 77 |
$this->icon = (array) $this->icon; |
| 78 |
} |
| 79 |
|
| 80 |
// Set states. |
| 81 |
if ( $this->states ) { |
| 82 |
$this->icon = array_column( $this->states, 'icon' ); |
| 83 |
$this->captions = array_column( $this->states, 'caption' ); |
| 84 |
} |
| 85 |
|
| 86 |
// Set attributes. |
| 87 |
if ( 'link' === $this->view ) { |
| 88 |
$attributes['class'] = [ 'hp-link' ]; |
| 89 |
} |
| 90 |
|
| 91 |
if ( is_user_logged_in() || ! $this->url ) { |
| 92 |
$attributes['href'] = '#'; |
| 93 |
|
| 94 |
$attributes['data-component'] = 'toggle'; |
| 95 |
|
| 96 |
if ( $this->url ) { |
| 97 |
$attributes['data-url'] = esc_url( $this->url ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( $this->active ) { |
| 101 |
$attributes['data-icon'] = hp\get_first_array_value( $this->icon ); |
| 102 |
$attributes['data-caption'] = hp\get_first_array_value( $this->captions ); |
| 103 |
$attributes['data-state'] = 'active'; |
| 104 |
|
| 105 |
if ( 'icon' === $this->view ) { |
| 106 |
$attributes['title'] = hp\get_last_array_value( $this->captions ); |
| 107 |
} |
| 108 |
} else { |
| 109 |
$attributes['data-icon'] = hp\get_last_array_value( $this->icon ); |
| 110 |
$attributes['data-caption'] = hp\get_last_array_value( $this->captions ); |
| 111 |
|
| 112 |
if ( 'icon' === $this->view ) { |
| 113 |
$attributes['title'] = hp\get_first_array_value( $this->captions ); |
| 114 |
} |
| 115 |
} |
| 116 |
} else { |
| 117 |
$attributes['href'] = '#user_login_modal'; |
| 118 |
} |
| 119 |
|
| 120 |
$this->attributes = hp\merge_arrays( $this->attributes, $attributes ); |
| 121 |
|
| 122 |
parent::boot(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Renders block HTML. |
| 127 |
* |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
public function render() { |
| 131 |
$output = ''; |
| 132 |
|
| 133 |
if ( $this->icon || $this->captions ) { |
| 134 |
$output .= '<a ' . hp\html_attributes( $this->attributes ) . '>'; |
| 135 |
|
| 136 |
if ( $this->icon ) { |
| 137 |
|
| 138 |
// Get icon. |
| 139 |
$icon = null; |
| 140 |
|
| 141 |
if ( $this->active ) { |
| 142 |
$icon = hp\get_last_array_value( $this->icon ); |
| 143 |
} else { |
| 144 |
$icon = hp\get_first_array_value( $this->icon ); |
| 145 |
} |
| 146 |
|
| 147 |
// Render icon. |
| 148 |
$output .= '<i class="hp-icon fas fa-' . esc_attr( $icon ) . '"></i>'; |
| 149 |
} |
| 150 |
|
| 151 |
if ( 'icon' !== $this->view ) { |
| 152 |
|
| 153 |
// Get caption. |
| 154 |
$caption = null; |
| 155 |
|
| 156 |
if ( $this->active ) { |
| 157 |
$caption = hp\get_last_array_value( $this->captions ); |
| 158 |
} else { |
| 159 |
$caption = hp\get_first_array_value( $this->captions ); |
| 160 |
} |
| 161 |
|
| 162 |
// Render caption. |
| 163 |
$output .= '<span>' . esc_html( $caption ) . '</span>'; |
| 164 |
} |
| 165 |
|
| 166 |
$output .= '</a>'; |
| 167 |
} |
| 168 |
|
| 169 |
return $output; |
| 170 |
} |
| 171 |
} |
| 172 |
|