| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
abstract class HC3_Ui_Abstract_Element |
| 4 |
{ |
| 5 |
protected $el = 'input'; |
| 6 |
protected $attr = array(); |
| 7 |
private $no_array = array('id', 'action', 'href', 'cols', 'rows'); |
| 8 |
|
| 9 |
protected $uiType = NULL; |
| 10 |
protected $tags = array(); |
| 11 |
protected $content = NULL; |
| 12 |
|
| 13 |
public function __construct( $el = NULL, $content = NULL ) |
| 14 |
{ |
| 15 |
$this->el = $el; |
| 16 |
$this->content = $content; |
| 17 |
} |
| 18 |
|
| 19 |
public function __toString() |
| 20 |
{ |
| 21 |
return '' . $this->render(); |
| 22 |
} |
| 23 |
|
| 24 |
public function tag( $tag, $details = NULL ) |
| 25 |
{ |
| 26 |
$details = ($details === NULL) ? $tag : $details; |
| 27 |
$this->tags[ $tag ] = $details; |
| 28 |
return $this; |
| 29 |
} |
| 30 |
|
| 31 |
public function getTags() |
| 32 |
{ |
| 33 |
return $this->tags; |
| 34 |
} |
| 35 |
|
| 36 |
public function getUiType() |
| 37 |
{ |
| 38 |
return $this->uiType; |
| 39 |
} |
| 40 |
|
| 41 |
public function getChildren() |
| 42 |
{ |
| 43 |
$return = array( $this->content ); |
| 44 |
return $return; |
| 45 |
} |
| 46 |
|
| 47 |
public function getContent() |
| 48 |
{ |
| 49 |
return $this->content; |
| 50 |
} |
| 51 |
|
| 52 |
public function setChild( $key, $child ) |
| 53 |
{ |
| 54 |
$this->content = $child; |
| 55 |
return $this; |
| 56 |
} |
| 57 |
|
| 58 |
public function margin( $margin ) |
| 59 |
{ |
| 60 |
$this->addAttr('class', 'hc-m' . $margin); |
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
public function marginX( $margin ) |
| 65 |
{ |
| 66 |
$this->addAttr('class', 'hc-mx' . $margin); |
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
public function marginY( $margin ) |
| 71 |
{ |
| 72 |
$this->addAttr('class', 'hc-my' . $margin); |
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
public function padding( $padding ) |
| 77 |
{ |
| 78 |
$this->addAttr('class', 'hc-p' . $padding); |
| 79 |
return $this; |
| 80 |
} |
| 81 |
|
| 82 |
public function paddingX( $padding ) |
| 83 |
{ |
| 84 |
$this->addAttr('class', 'hc-px' . $padding); |
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
public function paddingY( $padding ) |
| 89 |
{ |
| 90 |
$this->addAttr('class', 'hc-py' . $padding); |
| 91 |
return $this; |
| 92 |
} |
| 93 |
|
| 94 |
public function render() |
| 95 |
{ |
| 96 |
$el = $this->el; |
| 97 |
|
| 98 |
$return = ''; |
| 99 |
$add_newline = FALSE; |
| 100 |
|
| 101 |
if( $el !== NULL ){ |
| 102 |
$return .= '<' . $el; |
| 103 |
|
| 104 |
if( in_array($el, array('script', 'meta', 'link', 'head', 'body')) ){ |
| 105 |
$add_newline = TRUE; |
| 106 |
} |
| 107 |
|
| 108 |
$attr = $this->getAttr(); |
| 109 |
foreach( $attr as $key => $val ){ |
| 110 |
if( is_array($val) ){ |
| 111 |
$val = join(' ', $val); |
| 112 |
} |
| 113 |
|
| 114 |
if( strlen($val) OR ( substr($key, 0, strlen('data-')) == 'data-') OR ('option' == $el) ){ |
| 115 |
$return .= ' ' . esc_attr($key) . '="' . esc_attr($val) . '"'; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if( in_array($el, array('br', 'input', 'link', 'meta')) ){ |
| 121 |
$return .= '/>'; |
| 122 |
} |
| 123 |
else { |
| 124 |
if( $el !== NULL ){ |
| 125 |
$return .= '>'; |
| 126 |
} |
| 127 |
|
| 128 |
$return .= $this->content; |
| 129 |
|
| 130 |
if( $el !== NULL ){ |
| 131 |
$return .= '</' . $el . '>'; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if( $add_newline ){ |
| 136 |
$return .= "\n"; |
| 137 |
} |
| 138 |
|
| 139 |
return $return; |
| 140 |
} |
| 141 |
|
| 142 |
// attribute related functions |
| 143 |
public function getAttr( $key = NULL ) |
| 144 |
{ |
| 145 |
if( $key === NULL ){ |
| 146 |
$return = $this->attr; |
| 147 |
} |
| 148 |
elseif( isset($this->attr[$key]) ){ |
| 149 |
$return = $this->attr[$key]; |
| 150 |
} |
| 151 |
else { |
| 152 |
$return = array(); |
| 153 |
} |
| 154 |
return $return; |
| 155 |
} |
| 156 |
|
| 157 |
public function setAttr( $key, $value ) |
| 158 |
{ |
| 159 |
unset( $this->attr[$key] ); |
| 160 |
return $this->addAttr( $key, $value ); |
| 161 |
} |
| 162 |
|
| 163 |
public function addAttr( $key, $value, $escape = true ) |
| 164 |
{ |
| 165 |
if( is_array($value) ){ |
| 166 |
foreach( $value as $v ){ |
| 167 |
$this->addAttr( $key, $v ); |
| 168 |
} |
| 169 |
return $this; |
| 170 |
} |
| 171 |
|
| 172 |
switch( $key ){ |
| 173 |
case 'title': |
| 174 |
if( is_string($value) ){ |
| 175 |
$value = strip_tags($value); |
| 176 |
$value = trim($value); |
| 177 |
} |
| 178 |
break; |
| 179 |
|
| 180 |
case 'class': |
| 181 |
if( isset($this->attr[$key]) && in_array($value, $this->attr[$key]) ){ |
| 182 |
return $this; |
| 183 |
} |
| 184 |
break; |
| 185 |
} |
| 186 |
|
| 187 |
if( $value === NULL ){ |
| 188 |
return $this; |
| 189 |
} |
| 190 |
|
| 191 |
if( ! in_array($key, $this->no_array) ){ |
| 192 |
if( ! is_array($value) ) |
| 193 |
$value = array( $value ); |
| 194 |
} |
| 195 |
|
| 196 |
if( $escape && in_array($key, array('alt', 'value', 'title')) ){ |
| 197 |
for( $ii = 0; $ii < count($value); $ii++ ){ |
| 198 |
$value[$ii] = $this->esc_attr( $value[$ii] ); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if( in_array($key, $this->no_array) ){ |
| 203 |
$this->attr[$key] = $value; |
| 204 |
} |
| 205 |
else { |
| 206 |
if( isset($this->attr[$key]) ){ |
| 207 |
$this->attr[$key] = array_merge( $this->attr[$key], $value ); |
| 208 |
} |
| 209 |
else { |
| 210 |
$this->attr[$key] = $value; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return $this; |
| 215 |
} |
| 216 |
|
| 217 |
public function esc_attr( $value ) |
| 218 |
{ |
| 219 |
if( function_exists('esc_attr') ){ |
| 220 |
return esc_attr( $value ); |
| 221 |
} |
| 222 |
else { |
| 223 |
$return = htmlspecialchars( $value ); |
| 224 |
return $return; |
| 225 |
} |
| 226 |
} |
| 227 |
} |