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