PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/all-in-one-event-calendar/lib/html/element/legacy/abstract/html-element.php

https://github.com/dedavidd/piratenpartij.nl
PHP | 172 lines | 77 code | 17 blank | 78 comment | 6 complexity | b0d70f0cf2e1e569c7672ac6cd3276a5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * Base html element.
  4. *
  5. * @author Time.ly Network, Inc.
  6. * @since 2.0
  7. * @package Ai1EC
  8. * @subpackage Ai1EC.Html
  9. */
  10. abstract class Ai1ec_Html_Element extends Ai1ec_Base implements Ai1ec_Renderable {
  11. /**
  12. *
  13. * @var string
  14. */
  15. protected $id;
  16. /**
  17. *
  18. * @var array
  19. */
  20. protected $classes = array();
  21. /**
  22. * @var array
  23. */
  24. protected $attributes = array();
  25. /**
  26. *
  27. * @var Ai1ec_Template_Adapter
  28. */
  29. protected $template_adapter;
  30. /**
  31. * Adds the passed attribute name & value to the link's attributes.
  32. *
  33. * @param string $name
  34. * @param string|array $value
  35. */
  36. public function set_attribute( $name, $value ) {
  37. $value = ( array ) $value;
  38. // Let's check if we have a value
  39. if ( isset( $this->attributes[$name] ) ) {
  40. // Let's check if it's an array
  41. $this->attributes[$name] = array_unique(
  42. array_merge( $this->attributes[$name], $value )
  43. );
  44. } else {
  45. $this->attributes[$name] = $value;
  46. }
  47. }
  48. /**
  49. *
  50. * @param string $name
  51. * @return array|NULL
  52. */
  53. public function get_attribute( $name ) {
  54. if ( isset( $this->attributes[$name] ) ) {
  55. return $this->attributes[$name];
  56. } else {
  57. return null;
  58. }
  59. }
  60. /**
  61. * Adds the given name="value"-formatted attribute expression to the link's
  62. * set of attributes.
  63. *
  64. * @param string $expr Attribute name-value pair in name="value" format
  65. */
  66. public function set_attribute_expr( $expr ) {
  67. preg_match( '/^([\w\-_]+)=[\'"]([^\'"]*)[\'"]$/', $expr, $matches );
  68. $name = $matches[1];
  69. $value = $matches[2];
  70. $this->set_attribute( $name, $value );
  71. }
  72. public function __construct( Ai1ec_Registry_Object $registry ) {
  73. $this->_registry = $registry;
  74. $this->template_adapter = $registry->get( 'html.helper' );
  75. }
  76. /**
  77. * Magic method that renders the object as html
  78. *
  79. * @return string
  80. */
  81. public function __toString() {
  82. return $this->render_as_html();
  83. }
  84. /**
  85. *
  86. * @param $id string
  87. */
  88. public function set_id( $id ) {
  89. $this->id = $id;
  90. }
  91. /**
  92. * Adds an element to the class array
  93. *
  94. * @param string $class
  95. */
  96. public function add_class( $class ) {
  97. $this->classes[] = $class;
  98. }
  99. /**
  100. * Creates the markup to be used to create classes
  101. *
  102. * @return string
  103. */
  104. protected function create_class_markup() {
  105. if ( empty( $this->classes ) ) {
  106. return '';
  107. }
  108. $classes = $this->template_adapter->escape_attribute(
  109. implode( ' ', $this->classes )
  110. );
  111. return "class='$classes'";
  112. }
  113. /**
  114. * Creates the markup for an attribute
  115. *
  116. * @param string $attribute_name
  117. * @param string $attribute_value
  118. * @return string
  119. */
  120. protected function create_attribute_markup(
  121. $attribute_name,
  122. $attribute_value
  123. ) {
  124. if (empty( $attribute_value )) {
  125. return '';
  126. }
  127. $attribute_value = $this->template_adapter->escape_attribute( $attribute_value );
  128. return "$attribute_name='$attribute_value'";
  129. }
  130. /**
  131. * Renders the markup for the attributes of the tag
  132. *
  133. * @return string
  134. */
  135. protected function render_attributes_markup() {
  136. $html = array();
  137. foreach ( $this->attributes as $name => $values ) {
  138. $values = $this->template_adapter->escape_attribute(
  139. implode( ' ', $values )
  140. );
  141. $html[] = "$name='$values'";
  142. }
  143. return implode( ' ', $html );
  144. }
  145. /**
  146. * Return the content as html instead of echoing it.
  147. *
  148. * @return string
  149. */
  150. public function render_as_html() {
  151. $this->_registry->get( 'compatibility.ob' )->start();
  152. $this->render();
  153. return $this->_registry->get( 'compatibility.ob' )->get_clean();
  154. }
  155. }