component-bases
1 year ago
filter-hooks
3 years ago
class-fwp-debug.php
1 year ago
class-fwp-html-element.php
3 years ago
class-fwp-html-fields.php
3 years ago
class-fwp-html-element.php
277 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class FWP_HTML_Element_1x0x0 |
| 4 | * |
| 5 | * @package FWP |
| 6 | * @category WordPress Library |
| 7 | * @version 1.0.0 |
| 8 | |
| 9 | * @link https://www.webfactoryltd.com/ |
| 10 | */ |
| 11 | class FWP_HTML_Element_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | private $tag_name = null; |
| 18 | |
| 19 | /** |
| 20 | * @var string |
| 21 | */ |
| 22 | private $content = null; |
| 23 | |
| 24 | /** |
| 25 | * @var array |
| 26 | */ |
| 27 | private $atts = array(); |
| 28 | |
| 29 | /** |
| 30 | * @param string $tag_name |
| 31 | * @param string $content Optional |
| 32 | */ |
| 33 | public function __construct( $tag_name, $content = null ) |
| 34 | { |
| 35 | $this->tag_name = $tag_name; |
| 36 | $this->content = $content; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param string $content |
| 41 | */ |
| 42 | public function set_content( $content ) |
| 43 | { |
| 44 | $this->content = $content; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return string |
| 49 | */ |
| 50 | public function get_content() |
| 51 | { |
| 52 | return $this->content; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Set attributes |
| 57 | * @param array|string $atts |
| 58 | */ |
| 59 | public function set_atts( $atts ) { |
| 60 | if ( is_string( $atts ) ) { |
| 61 | $this->atts = $this->parse_atts( $atts ); |
| 62 | } else if ( is_array( $atts ) ) { |
| 63 | $this->atts = $atts; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return string |
| 69 | */ |
| 70 | public function get_tag_name() |
| 71 | { |
| 72 | return $this->tag_name; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param string $name |
| 77 | * @return string|null |
| 78 | */ |
| 79 | public function get_attr( $name ) |
| 80 | { |
| 81 | if ( ! isset( $this->atts[ $name ] ) ) { |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | return $this->atts[ $name ]; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param string $name |
| 90 | * @param string $value Optional |
| 91 | */ |
| 92 | public function set_attr( $name, $value = null ) |
| 93 | { |
| 94 | $this->atts[ $name ] = $value; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param string $name |
| 99 | * @return boolean |
| 100 | */ |
| 101 | public function has_attr( $name ) |
| 102 | { |
| 103 | return isset( $this->atts[ $name ] ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param string $name |
| 108 | */ |
| 109 | public function remove_attr( $name ) |
| 110 | { |
| 111 | unset( $this->atts[ $name ] ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param string $name |
| 116 | * @param string $value |
| 117 | * @return boolean |
| 118 | */ |
| 119 | public function has_attr_value( $name, $value ) |
| 120 | { |
| 121 | if ( ! $this->has_attr( $name ) ) { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | $attr_values = explode( ' ', $this->get_attr( $name ) ); |
| 126 | return in_array( $value, $attr_values ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param string $name |
| 131 | * @param string $value |
| 132 | * @return void |
| 133 | */ |
| 134 | public function add_to_attr( $name, $value ) |
| 135 | { |
| 136 | if ( empty( $this->atts[ $name ] ) ) { |
| 137 | $this->set_attr( $name, $value ); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if ( $this->has_attr_value( $name, $value ) ) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | $this->atts[ $name ] .= ' '. $value; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @param string $name |
| 150 | * @param string $value |
| 151 | * @return void |
| 152 | */ |
| 153 | public function remove_from_attr( $name, $value ) |
| 154 | { |
| 155 | if ( ! $this->has_attr_value( $name, $value ) ) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | $attr_values = explode( ' ', $this->atts[ $name ] ); |
| 160 | $new_attr_values = array_diff( $attr_values , array( $value ) ); |
| 161 | |
| 162 | $this->atts[ $name ] = implode( ' ', $new_attr_values ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @return string |
| 167 | */ |
| 168 | public function get_html( $escape_content = true ) |
| 169 | { |
| 170 | $link = '<'. esc_attr( $this->tag_name ); |
| 171 | |
| 172 | foreach ( $this->atts AS $key => $value ) { |
| 173 | if ( null === $value ) { |
| 174 | $link .= ' '. $key; |
| 175 | } else { |
| 176 | $link .= ' '. esc_attr( $key ) .'="'. esc_attr( $value ) .'"'; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | $link .= '>'; |
| 181 | |
| 182 | if ( null !== $this->content ) { |
| 183 | if ( true === $escape_content ) { |
| 184 | $link .= esc_html( $this->content ); |
| 185 | } else { |
| 186 | $link .= $this->content; |
| 187 | } |
| 188 | |
| 189 | $link .= '</'. esc_attr( $this->tag_name ) .'>'; |
| 190 | } |
| 191 | |
| 192 | return $link; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Parse an attributes string into an array. If the string starts with a tag, |
| 197 | * then the attributes on the first tag are parsed. This parses via a manual |
| 198 | * loop and is designed to be safer than using DOMDocument. |
| 199 | * |
| 200 | * @param string $atts |
| 201 | * @return array |
| 202 | * |
| 203 | * @example parse_attrs( 'src="example.jpg" alt="example"' ) |
| 204 | * @example parse_attrs( '<img src="example.jpg" alt="example">' ) |
| 205 | * @example parse_attrs( '<a href="example"></a>' ) |
| 206 | * |
| 207 | * @link http://dev.airve.com/demo/speed_tests/php/parse_attrs.php |
| 208 | */ |
| 209 | final protected function parse_atts( $atts ) { |
| 210 | $atts = str_split( trim( $atts ) ); |
| 211 | |
| 212 | if ( '<' === $atts[0] ) { // looks like a tag so strip the tagname |
| 213 | while ( $atts && ! ctype_space( $atts[0] ) && $atts[0] !== '>' ) { |
| 214 | array_shift($atts); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | $arr = array(); // output |
| 219 | $name = ''; // for the current attr being parsed |
| 220 | $value = ''; // for the current attr being parsed |
| 221 | $mode = 0; // whether current char is part of the name (-), the value (+), or neither (0) |
| 222 | $stop = false; // delimiter for the current $value being parsed |
| 223 | $space = ' '; // a single space |
| 224 | |
| 225 | foreach ( $atts as $j => $curr ) { |
| 226 | if ( $mode < 0 ) { // name |
| 227 | if ( '=' === $curr ) { |
| 228 | $mode = 1; |
| 229 | $stop = false; |
| 230 | } elseif ( '>' === $curr ) { |
| 231 | '' === $name or $arr[ $name ] = $value; |
| 232 | break; |
| 233 | } elseif ( ! ctype_space( $curr ) ) { |
| 234 | if ( ctype_space( $atts[ $j - 1 ] ) ) { // previous char |
| 235 | '' === $name or $arr[ $name ] = ''; // previous name |
| 236 | $name = $curr; // initiate new |
| 237 | } else { |
| 238 | $name .= $curr; |
| 239 | } |
| 240 | } |
| 241 | } elseif ( $mode > 0 ) { // value |
| 242 | if ( $stop === false ) { |
| 243 | if ( ! ctype_space( $curr ) ) { |
| 244 | if ( '"' === $curr || "'" === $curr ) { |
| 245 | $value = ''; |
| 246 | $stop = $curr; |
| 247 | } else { |
| 248 | $value = $curr; |
| 249 | $stop = $space; |
| 250 | } |
| 251 | } |
| 252 | } elseif ( $stop === $space ? ctype_space( $curr ) : $curr === $stop ) { |
| 253 | $arr[ $name ] = $value; |
| 254 | $mode = 0; |
| 255 | $name = $value = ''; |
| 256 | } else { |
| 257 | $value .= $curr; |
| 258 | } |
| 259 | } else { // neither |
| 260 | if ( '>' === $curr ) |
| 261 | break; |
| 262 | if ( ! ctype_space( $curr ) ) { |
| 263 | // initiate |
| 264 | $name = $curr; |
| 265 | $mode = -1; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // incl the final pair if it was quoteless |
| 271 | '' === $name or $arr[ $name ] = $value; |
| 272 | |
| 273 | return $arr; |
| 274 | } |
| 275 | |
| 276 | } |
| 277 |