/includes/skip/elements/html_element.php

  1. <?php 
  2. /** 
  3. * Skip HTML element 
  4. * @package Skip 
  5. * @since 1.0 
  6. * @ignore 
  7. */ 
  8.  
  9. namespace skip\v1_0_0; 
  10.  
  11. class HTML_Element extends Element{ 
  12.  
  13. var $tag; 
  14. var $close_tag; 
  15. var $echo_tag; 
  16. var $content; 
  17.  
  18. var $params = array(); 
  19.  
  20. /** 
  21. * Skip constructor 
  22. * @package Skip 
  23. * @since 1.0 
  24. */ 
  25. public function __construct( $tag, $args = array() ) { 
  26.  
  27. $defaults = array( 
  28. 'id' => id(),  
  29. 'classes' => '',  
  30. 'params' => array(),  
  31. 'close_tag' => FALSE,  
  32. 'echo_tag' => TRUE 
  33. ); 
  34. $args = wp_parse_args( $args, $defaults ); 
  35. extract( $args , EXTR_SKIP ); 
  36.  
  37. parent::__construct( $args ); 
  38.  
  39. $this->tag = $tag; 
  40.  
  41. if( $close_tag === TRUE ) 
  42. $this->close_tag = TRUE; 
  43.  
  44. if( $echo_tag === TRUE ) 
  45. $this->echo_tag = TRUE; 
  46.  
  47. if( '' != $classes ) 
  48. $this->add_param( 'class', $classes ); 
  49.  
  50. if( '' != $id ) 
  51. $this->add_param( 'id', $id ); 
  52.  
  53. $this->add_params( $params ); 
  54.  
  55. /** 
  56. * Adding parameter to HTML Element 
  57. *  
  58. * @param string $name Name of the parameter 
  59. * @param string $value Value of the parameter 
  60. * @package Skip 
  61. * @since 1.0 
  62. * @return string The content of the element 
  63. */ 
  64. public function add_param( $name, $value ) { 
  65. $this->params[ $name ] = $value; 
  66.  
  67. /** 
  68. * Adding parameters in an array 
  69. *  
  70. * @param array $params an array of parameters with name as key and value 
  71. * @package Skip 
  72. * @since 1.0 
  73. * @return string The content of the element 
  74. */ 
  75. public function add_params( $params = array() ) { 
  76. if( 0 != count( $params ) ): 
  77. foreach( $params AS $name => $value ): 
  78. if( '' != $key && '' != $param ) 
  79. $this->add_param( $name, $value); 
  80. endforeach; 
  81. endif; 
  82.  
  83. /** 
  84. * Deleting parameter in an array 
  85. *  
  86. * @param string $name Name of the parameter to delete 
  87. * @package Skip 
  88. * @since 1.0 
  89. * @return string The content of the element 
  90. */ 
  91. public function del_param( $name ) { 
  92. unset( $this->params[ $name ] ); 
  93.  
  94. /** 
  95. * Render the content 
  96. * 
  97. * @package Skip 
  98. * @since 1.0 
  99. * @param string $content Content which will be added if close_tag is TRUE 
  100. * @return string The content of the element 
  101. */ 
  102. public function render() { 
  103. if( $this->close_tag ): 
  104.  
  105. if( count( $this->elements ) > 0 && '' == $this->content ) 
  106. foreach( $this->elements AS $element ) 
  107. $this->content.= display( $element ); 
  108.  
  109. if( $this->echo_tag ) 
  110. $html = '<' . $this->tag . $this->params() . '>' . $this->content . '</' . $this->tag . '>' . chr(13); 
  111. else 
  112. $html = $content; 
  113. else: 
  114. if( $this->echo_tag ) 
  115. $html = '<' . $this->tag . $this->params() . ' />' . chr(13); 
  116. else 
  117. $html = $this->content; 
  118. endif; 
  119.  
  120. $html_element = $this->before_element . $html . $this->after_element; 
  121. return $html_element; 
  122.  
  123. public function content( $content ) { 
  124. $this->content = $content; 
  125.  
  126. /** 
  127. * Returns the params 
  128. * 
  129. * @param array $params Parameters as array like $params[][ 'name' ] = VALUE 
  130. * @package Skip 
  131. * @since 1.0 
  132. * @return string The content of the element 
  133. */ 
  134. public function params( $params = FALSE ) { 
  135. $params_return = ''; 
  136.  
  137. if( FALSE === $params ) 
  138. $params = $this->params; 
  139.  
  140. if( count( $params ) > 0 ) 
  141. foreach( $params AS $param_name => $param ) 
  142. $params_return.= ' ' . $param_name . '="' . $param . '"'; 
  143.  
  144. return $params_return; 
  145. /** 
  146. * @ignore 
  147. */ 
  148. function skip_html_element( $tag, $args = array(), $elements = array(), $return = 'html' ) { 
  149. $html_element = new HTML_Element( $tag, $args ); 
  150.  
  151. if( count( $elements ) > 0 ) 
  152. foreach ( $elements AS $element ) 
  153. $html_element->add_element( $element ); 
  154.  
  155. return element_return( $html_element, $return ); 
.