1: <?php
2:
3: 4: 5:
6: abstract class Papi_Core_Page extends Papi_Container {
7:
8: 9: 10: 11: 12:
13: const TYPE_POST = 'post';
14:
15: 16: 17: 18: 19:
20: const TYPE_OPTION = 'option';
21:
22: 23: 24: 25: 26:
27: public $id;
28:
29: 30: 31: 32: 33:
34: protected $type;
35:
36: 37: 38: 39: 40: 41: 42:
43: public function __get( $slug ) {
44: return $this->get_value( $slug );
45: }
46:
47: 48: 49: 50: 51: 52: 53:
54: public function get_value( $slug ) {
55: $slug = papi_remove_papi( $slug );
56: $value = papi_get_property_meta_value( $this->id, $slug, $this->type );
57: return $this->convert( $slug, $value );
58: }
59:
60: 61: 62: 63: 64: 65: 66: 67:
68: protected function convert( $slug, $value ) {
69: $property = $this->get_property( $slug );
70:
71:
72: if ( ! papi_is_property( $property ) ) {
73: return;
74: }
75:
76: if ( papi_is_empty( $value ) ) {
77: if ( ! papi_is_empty( $property->get_option( 'value' ) ) ) {
78: return $property->get_option( 'value' );
79: }
80: return;
81: }
82:
83:
84: $property->set_page( $this );
85:
86:
87: $value = $property->load_value( $value, $slug, $this->id );
88:
89: if ( $this->type !== self::TYPE_OPTION ) {
90: $value = papi_filter_load_value(
91: $property->type,
92: $value,
93: $slug,
94: $this->id
95: );
96: }
97:
98:
99: $value = $property->format_value( $value, $slug, $this->id );
100:
101: if ( ! is_admin() || $this->type !== self::TYPE_OPTION ) {
102: $value = papi_filter_format_value(
103: $property->type,
104: $value,
105: $slug,
106: $this->id
107: );
108: }
109:
110: if ( is_array( $value ) ) {
111: $value = array_filter( $value );
112: }
113:
114: return $value;
115: }
116:
117: 118: 119: 120: 121: 122: 123:
124: public function is( $type ) {
125: return $this->type === $type;
126: }
127:
128: 129: 130: 131: 132: 133: 134: 135:
136: public static function factory( $post_id, $type = self::TYPE_POST ) {
137: if ( papi_is_option_page() ) {
138: $type = self::TYPE_OPTION;
139: }
140:
141: $class_suffix = '_' . ucfirst( $type ) . '_Page';
142: $class_name = 'Papi' . $class_suffix;
143:
144: if ( ! class_exists( $class_name ) ) {
145: return;
146: }
147:
148: $post_id = papi_get_post_id( $post_id );
149: $page = new $class_name( $post_id );
150: $page->set_type( $type );
151:
152: if ( ! $page->valid() ) {
153: return;
154: }
155:
156: return $page;
157: }
158:
159: 160: 161: 162: 163: 164: 165: 166:
167: abstract public function get_property( $slug, $child_slug = '' );
168:
169: 170: 171: 172: 173:
174: public function set_type( $type ) {
175: $this->type = $type;
176: }
177:
178: 179: 180: 181: 182:
183: abstract public function valid();
184:
185: 186: 187: 188: 189:
190: protected function valid_type() {
191: $type = strtoupper( $this->type );
192: return defined( "self::TYPE_$type" );
193: }
194: }
195: