Button.php
201 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Table; |
| 4 | |
| 5 | class Button { |
| 6 | |
| 7 | /** @var string $slug */ |
| 8 | private $slug; |
| 9 | |
| 10 | /** @var string $label */ |
| 11 | private $label; |
| 12 | |
| 13 | /** @var string $text */ |
| 14 | private $text; |
| 15 | |
| 16 | /** @var string $dashicon */ |
| 17 | private $dashicon; |
| 18 | |
| 19 | /** @var array */ |
| 20 | protected $attributes = array(); |
| 21 | |
| 22 | public function __construct( $slug ) { |
| 23 | $this->set_slug( $slug ); |
| 24 | $this->add_class( 'ac-table-button -' . $slug ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @return array |
| 29 | */ |
| 30 | public function get_attributes() { |
| 31 | return $this->attributes; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param string $class |
| 36 | * |
| 37 | * @return $this |
| 38 | */ |
| 39 | public function add_class( $class ) { |
| 40 | $this->set_attribute( 'class', $this->get_attribute( 'class' ) . ' ' . esc_attr( $class ) ); |
| 41 | |
| 42 | return $this; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param $key |
| 47 | * |
| 48 | * @return string|false |
| 49 | */ |
| 50 | public function get_attribute( $key ) { |
| 51 | if ( ! isset( $this->attributes[ $key ] ) ) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | return trim( $this->attributes[ $key ] ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param string $key |
| 60 | * @param string $value |
| 61 | * |
| 62 | * @return $this |
| 63 | */ |
| 64 | public function set_attribute( $key, $value ) { |
| 65 | $this->attributes[ $key ] = $value; |
| 66 | |
| 67 | return $this; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get attributes as string |
| 72 | * |
| 73 | * @param array $attributes |
| 74 | * |
| 75 | * @return string |
| 76 | */ |
| 77 | protected function get_attributes_as_string( array $attributes ) { |
| 78 | $output = array(); |
| 79 | |
| 80 | foreach ( $attributes as $key => $value ) { |
| 81 | $output[] = $this->get_attribute_as_string( $key, $value ); |
| 82 | } |
| 83 | |
| 84 | return implode( ' ', $output ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Render an attribute |
| 89 | * |
| 90 | * @param string $key |
| 91 | * @param string $value |
| 92 | * |
| 93 | * @return string |
| 94 | */ |
| 95 | protected function get_attribute_as_string( $key, $value = null ) { |
| 96 | if ( null === $value ) { |
| 97 | $value = $this->get_attribute( $key ); |
| 98 | } |
| 99 | |
| 100 | return ac_helper()->html->get_attribute_as_string( $key, $value ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return string |
| 105 | */ |
| 106 | public function get_slug() { |
| 107 | return $this->slug; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param string $slug |
| 112 | * |
| 113 | * @return $this |
| 114 | */ |
| 115 | public function set_slug( $slug ) { |
| 116 | $this->slug = $slug; |
| 117 | |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @return string |
| 123 | */ |
| 124 | public function get_label() { |
| 125 | return $this->label; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param string $label |
| 130 | * |
| 131 | * @return $this |
| 132 | */ |
| 133 | public function set_label( $label ) { |
| 134 | $this->label = $label; |
| 135 | |
| 136 | return $this; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @return string |
| 141 | */ |
| 142 | public function get_text() { |
| 143 | return $this->text; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @param string $text |
| 148 | * |
| 149 | * @return Button |
| 150 | */ |
| 151 | public function set_text( $text ) { |
| 152 | $this->text = $text; |
| 153 | |
| 154 | return $this; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return string |
| 159 | */ |
| 160 | public function get_dashicon() { |
| 161 | if ( ! $this->dashicon ) { |
| 162 | return ''; |
| 163 | } |
| 164 | |
| 165 | return ac_helper()->icon->dashicon( array( |
| 166 | 'icon' => $this->dashicon, |
| 167 | ) ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @param $dashicon |
| 172 | * |
| 173 | * @return $this |
| 174 | */ |
| 175 | public function set_dashicon( $dashicon ) { |
| 176 | $this->dashicon = $dashicon; |
| 177 | |
| 178 | return $this; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @param $url |
| 183 | * |
| 184 | * @return $this |
| 185 | */ |
| 186 | public function set_url( $url ) { |
| 187 | $this->set_attribute( 'href', esc_url( $url ) ); |
| 188 | |
| 189 | return $this; |
| 190 | } |
| 191 | |
| 192 | public function render() { |
| 193 | $attributes = $this->get_attributes(); |
| 194 | $attributes['data-ac-tip'] = $this->get_label(); |
| 195 | |
| 196 | $template = '<a %s>%s%s</a>'; |
| 197 | |
| 198 | echo sprintf( $template, $this->get_attributes_as_string( $attributes ), $this->get_dashicon(), $this->get_text() ); |
| 199 | } |
| 200 | |
| 201 | } |