1 <?php
2
3 /**
4 * Abstract Theater Item class.
5 *
6 * @abstract
7 * @since 0.16
8 * @package Theater/Abstracts
9 */
10 abstract class Theater_Item {
11
12 /**
13 * The internal name of this item.
14 * @internal
15 */
16 const name = 'undefined';
17
18 /**
19 * The post type of this item.
20 * @internal
21 */
22 const post_type_name = 'undefined';
23
24 /**
25 * ID of this item.
26 *
27 * @since 0.16
28 * @var int
29 */
30 var $ID;
31
32 /**
33 * The template for the HTML output of this item.
34 *
35 * @var string
36 * @since 0.16
37 * @internal
38 */
39 var $template;
40
41 /**
42 * Sets the ID and template of this item.
43 *
44 * @since 0.16
45 * @uses Theater_Item::$ID to set the ID of this item.
46 * @uses Theater_Item::$template to set the template for the HTML output of this item.
47 *
48 * @param int|WP_Post $ID ID of post object or post object of this item.
49 * @return void
50 */
51 function __construct( $ID = false, $template = '' ) {
52
53 if ( $ID instanceof WP_Post ) {
54 $this->post = $ID;
55 $ID = $ID->ID;
56 }
57
58 $this->ID = $ID;
59
60 if (!empty($template)) {
61 $this->template = $template;
62 }
63
64 }
65
66 /**
67 * Gets the value for a field.
68 *
69 * @uses Theater_Item::get_field() to get the value for a field.
70 * @since 0.16
71 * @internal
72 * @param string $name The field name.
73 * @param array $args Not used.
74 * @return mixed The value for the field.
75 */
76 function __call( $name, $args ) {
77
78 // Handle deprecated usage of the $args['html'] param.
79 if ( !empty ($args[0]['html']) ) {
80 if (!empty($args[0]['filters'])) {
81 return $this->get_field_html( $name, $args[0]['filters'] );
82 }
83 return $this->get_field_html( $name );
84 }
85
86 // Handle deprecated usage of '{field}_html' item methods.
87 $name_parts = explode('_', $name);
88 if ('html' == $name_parts[count($name_parts) - 1]) {
89 array_pop($name_parts);
90 $name = implode('_', $name_parts);
91 if (!empty($args[0])) {
92 return $this->get_field_html( $name, $args[0] );
93 }
94 return $this->get_field_html( $name );
95 }
96
97 return $this->get_field( $name );
98 }
99
100 /**
101 * Gets the HTML output for a field.
102 *
103 * @uses Theater_Item::get_field_html() to get the HTML output for a field.
104 * @since 0.16
105 * @internal
106 * @param string $name The field name.
107 * @return string The field HTML output.
108 */
109 function __get( $name ) {
110 $value = $this->get_field_html($name);
111 return $value;
112 }
113
114 /**
115 * Gets the HTML output of this item.
116 *
117 * @since 0.16
118 * @internal
119 * @return string The HTML output of this item.
120 */
121 function __toString() {
122 $html = $this->get_html();
123 return $html;
124 }
125
126 /**
127 * Applies template filters to a field value string.
128 *
129 * @access protected
130 * @since 0.16
131 * @param string $value The value.
132 * @param WPT_Template_Placeholder_Filter[] $filters The template filters.
133 * @return string The filtered value.
134 */
135 protected function apply_template_filters( $value, $filters ) {
136 foreach ( $filters as $filter ) {
137 $value = $filter->apply_to( $value, $this );
138 }
139 return $value;
140 }
141
142 /**
143 * Gets the value for a field.
144 *
145 * @since 0.16
146 * @uses Theater_Event_Field::get() to get the value of a field.
147 * @param string $name The field name.
148 * @return mixed
149 */
150 function get_field( $name ) {
151 $field = new Theater_Event_Field($name, NULL, $this);
152 return $field->get();
153 }
154
155 /**
156 * Gets the HTML output for a field.
157 *
158 * @since 0.16
159 * @uses Theater_Event_Field::get_html() to get the HTML of a field value.
160 * @param string $name The field name.
161 * @param array $filters (default: array())
162 * @return string The HTML output for a field.
163 */
164 function get_field_html( $name, $filters = array() ) {
165 $field = new Theater_Event_Field($name, $filters, $this);
166 return $field->get_html();
167 }
168
169 /**
170 * Gets a list of of available fields for this item.
171 *
172 * @since 0.16
173 * @abstract
174 * @return array
175 */
176 abstract function get_fields();
177
178 /**
179 * Gets the HTML output of this item.
180 *
181 * @since 0.16
182 * @abstract
183 * @return string The HTML output of this item.
184 */
185 abstract function get_html();
186
187 /**
188 * Gets the name of this item.
189 *
190 * @since 0.16
191 * @uses Theater_Item::$name to get the name of this item.
192 * @return string
193 */
194 function get_name() {
195 return static::name;
196 }
197
198 /**
199 * Get the post type of this item.
200 *
201 * @since 0.16
202 * @uses Theater_Item::$post_type_name to get the post type of this item.
203 * @return string
204 */
205 function get_post_type() {
206 return static::post_type_name;
207 }
208
209 /**
210 * Checks if this item has a certain field.
211 *
212 * @since 0.16
213 * @param string $name
214 * @uses Theater_Item::get_fields() to get all fields of an item.
215 * @return bool
216 */
217 function has_field( $name ) {
218 $has_field = in_array( $name, $this->get_fields() );
219 return $has_field;
220 }
221
222 /**
223 * @deprecated 0.16
224 * @internal
225 */
226 function html( $template = '' ) {
227 if ( is_array( $template ) ) {
228 $defaults = array(
229 'template' => '',
230 );
231 $args = wp_parse_args( $template, $defaults );
232 $template = $args['template'];
233 }
234
235 $this->template = $template;
236 return $this->get_html();
237 }
238
239 /**
240 * @deprecated 0.4
241 * @internal
242 */
243 function compile() {
244 return $this->html();
245 }
246
247 /**
248 * @deprecated 0.4
249 * @internal
250 */
251 function render() {
252 echo $this->html();
253 }
254
255 /**
256 * @deprecated 0.16
257 * @internal
258 */
259 function post_class() {
260 $classes = array();
261 $classes[] = static::post_type_name;
262 return implode( ' ',$classes );
263 }
264
265 /**
266 * @deprecated 0.16
267 * @internal
268 */
269 public function post() {
270 return $this->get_post();
271 }
272
273 /**
274 * @deprecated 0.16
275 * @internal
276 */
277 protected function get_post() {
278 if ( ! isset( $this->post ) ) {
279 $this->post = get_post( $this->ID );
280 }
281 return $this->post;
282 }
283
284
285 }
286
287 ?>
288