1 <?php
2
3 4 5 6 7 8
9 class Theater_Event_Field {
10
11 protected $name;
12 protected $filters = array();
13 protected $item;
14
15 function __construct( $name, $filters = array(), $item ) {
16 $this->name = $name;
17 $this->filters = $filters;
18 $this->item = $item;
19 }
20
21 function __invoke( $args = array() ) {
22 return $this->get();
23 }
24
25 function __toString() {
26 return $this->get_html();
27 }
28
29 protected function apply_template_filters( $value, $filters ) {
30 foreach ( $filters as $filter ) {
31 $value = $filter->apply_to( $value, $this->item );
32 }
33 return $value;
34 }
35
36 function get() {
37 if (method_exists($this->item, 'get_'.$this->name)) {
38 $value = $this->item->{'get_'.$this->name}();
39 } else {
40 $value = get_post_meta($this->item->ID, $this->name, true);
41 }
42
43 $value = apply_filters(
44 'theater/'.$this->item->get_name().'/field',
45 $value, $this->name, $this->item
46 );
47
48 $value = apply_filters(
49 'theater/'.$this->item->get_name().'/field?name='.$this->name,
50 $value, $this->item
51 );
52
53 return $value;
54 }
55
56 protected function get_callback( $action ) {
57
58 foreach( $this->item->get_additional_fields() as $field) {
59
60 if ( $field['id'] != $this->name ) {
61 continue;
62 }
63
64 if ( !empty($field['callbacks'][$action])) {
65 return $field['callbacks'][$action];
66 }
67 };
68
69 return false;
70
71 }
72
73 function get_html() {
74
75 if (method_exists($this->item, 'get_'.$this->name.'_html')) {
76 $html = $this->item->{'get_'.$this->name.'_html'}( $this->filters );
77 } else {
78 $value = (string) $this->get();
79
80 ob_start();
81 ?><div class="<?php echo $this->item->get_post_type(); ?>_<?php echo $this->name; ?>"><?php
82 echo $this->apply_template_filters( $value, $this->filters );
83 ?></div><?php
84 $html = ob_get_clean();
85 }
86
87 $html = apply_filters(
88 'theater/'.$this->item->get_name().'/field/html',
89 $html, $this->name, $this->filters, $this->item
90 );
91
92 $html = apply_filters(
93 'theater/'.$this->item->get_name().'/field/html?name='.$this->name,
94 $html, $this->filters, $this->item
95 );
96
97 return $html;
98
99 }
100
101 }