1: <?php
2:
3: 4: 5: 6: 7:
8: class Papi_Page_Type extends Papi_Page_Type_Meta {
9:
10: 11: 12: 13: 14:
15: protected $boxes = [];
16:
17: 18: 19: 20: 21:
22: private $load_boxes = false;
23:
24: 25: 26: 27: 28: 29:
30: private $post_type_supports = ['custom-fields'];
31:
32: 33: 34: 35: 36:
37: private $remove_meta_boxes = [];
38:
39: 40: 41: 42: 43: 44:
45: protected function box( $file_or_options = [], $properties = [] ) {
46: if ( ! is_string( $file_or_options ) && ! is_array( $file_or_options ) && ! is_object( $file_or_options ) ) {
47: return;
48: }
49:
50: list( $options, $properties ) = papi_get_options_and_properties(
51: $file_or_options,
52: $properties,
53: true
54: );
55:
56: $post_type = $this->get_post_type();
57:
58:
59:
60: if ( ! $this->load_boxes && ( empty( $post_type ) || ! $this->has_post_type( $post_type ) ) ) {
61: return;
62: }
63:
64:
65:
66: $options['_post_type'] = $post_type;
67:
68: if ( isset( $options['sort_order'] ) ) {
69: $sort_order = intval( $options['sort_order'] );
70: } else {
71: $sort_order = papi_filter_settings_sort_order();
72: }
73:
74: if ( is_callable( $properties ) ) {
75: $properties = call_user_func( $properties );
76: }
77:
78:
79: $properties = $this->convert_properties( $properties );
80:
81: $options['title'] = papi_esc_html(
82: isset( $options['title'] ) ? $options['title'] : ''
83: );
84:
85: array_push( $this->boxes, [
86: $options,
87: $properties,
88: 'sort_order' => $sort_order,
89: 'title' => $options['title']
90: ] );
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: private function convert_properties( $properties ) {
101: if ( is_array( $properties ) ) {
102: if ( isset( $properties['type'] ) ) {
103: $properties = [$properties];
104: } else if ( isset( $properties[0]->tab ) && $properties[0]->tab ) {
105: foreach ( $properties as $index => $items ) {
106: $items->properties = array_map(
107: 'papi_get_property_type',
108: $items->properties
109: );
110: }
111:
112: return $properties;
113: }
114: }
115:
116: if ( is_object( $properties ) ) {
117: $properties = papi_get_property_type( $properties );
118: }
119:
120: if ( papi_is_property( $properties ) ) {
121: $properties = [$properties];
122: }
123:
124: $properties = is_array( $properties ) ? $properties : [];
125: $properties = array_map( 'papi_get_property_type', $properties );
126:
127: return array_filter( $properties, 'papi_is_property' );
128: }
129:
130: 131: 132: 133: 134: 135: 136:
137: public function display( $post_type ) {
138: return true;
139: }
140:
141: 142: 143: 144: 145:
146: public function get_boxes() {
147: if ( empty( $this->boxes ) && $this->load_boxes === false ) {
148: if ( ! method_exists( $this, 'register' ) ) {
149: return [];
150: }
151:
152: $this->load_boxes = true;
153:
154: $this->register();
155: }
156:
157: return $this->boxes;
158: }
159:
160: 161: 162: 163: 164:
165: public function get_post_type() {
166: return papi_get_post_type();
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176:
177: protected function get_child_property( $items, $slug ) {
178: $result = null;
179:
180: foreach ( $items as $property ) {
181: if ( is_array( $property ) ) {
182: $result = $this->get_child_property( $property, $slug );
183:
184: if ( is_object( $result ) ) {
185: return papi_get_property_type( $result );
186: }
187: }
188:
189: $property = papi_get_property_type( $property );
190:
191: if ( papi_is_property( $property ) && $property->match_slug( $slug ) ) {
192: return papi_get_property_type( $property );
193: }
194: }
195:
196: return $result;
197: }
198:
199: 200: 201: 202: 203: 204: 205: 206:
207: public function get_property( $slug, $child_slug = '' ) {
208: $boxes = $this->get_boxes();
209: $parts = preg_split( '/\[\d+\]/', $slug );
210: $parts = array_map( function ( $part ) {
211: return preg_replace( '/(\[|\])/', '', $part );
212: }, $parts );
213:
214: if ( count( $parts ) > 1 ) {
215: $property = null;
216:
217: for ( $i = 0, $l = count( $parts ); $i < $l; $i++ ) {
218: $child = isset( $parts[$i + 1] ) ? $parts[$i + 1] : '';
219: $property = $this->get_property( $parts[$i], $child );
220:
221: if ( isset( $parts[$i + 1] ) ) {
222: $i++;
223: }
224: }
225:
226: return $property;
227: }
228:
229: if ( empty( $boxes ) ) {
230: return;
231: }
232:
233: foreach ( $boxes as $box ) {
234: $properties = isset( $box[1][0]->properties ) ?
235: $box[1][0]->properties : $box[1];
236:
237: foreach ( $properties as $property ) {
238: $property = papi_get_property_type( $property );
239:
240: if ( papi_is_property( $property ) && $property->match_slug( $slug ) ) {
241: if ( empty( $child_slug ) ) {
242: return $property;
243: }
244:
245: $result = $this->get_child_property(
246: $property->get_child_properties(),
247: $child_slug
248: );
249:
250: if ( is_object( $result ) ) {
251: return papi_get_property_type( $result );
252: }
253: }
254: }
255: }
256: }
257:
258: 259: 260:
261: public function setup() {
262: if ( ! method_exists( $this, 'register' ) ) {
263: return;
264: }
265:
266:
267: $this->register();
268:
269:
270: $this->remove_post_type_support();
271:
272:
273: $this->boxes = papi_sort_order( array_reverse( $this->boxes ) );
274:
275: foreach ( $this->boxes as $box ) {
276: new Papi_Admin_Meta_Box( $box[0], $box[1] );
277: }
278: }
279:
280: 281: 282: 283: 284: 285: 286: 287:
288: protected function property( $file_or_options = [], $values = [] ) {
289: return papi_property( $file_or_options, $values );
290: }
291:
292: 293: 294: 295: 296:
297: protected function remove( $post_type_supports = [] ) {
298: $this->post_type_supports = array_merge( $this->post_type_supports, papi_to_array( $post_type_supports ) );
299: }
300:
301: 302: 303:
304: public function remove_post_type_support() {
305: global $_wp_post_type_features;
306:
307: $post_type = $this->get_post_type();
308:
309: if ( empty( $post_type ) ) {
310: return;
311: }
312:
313: foreach ( $this->post_type_supports as $key => $value ) {
314: if ( is_numeric( $key ) ) {
315: $key = $value;
316: $value = '';
317: }
318:
319: if ( isset( $_wp_post_type_features[$post_type] ) && isset( $_wp_post_type_features[$post_type][$key] ) ) {
320: unset( $_wp_post_type_features[$post_type][$key] );
321: continue;
322: }
323:
324:
325: if ( empty( $value ) ) {
326: $value = 'normal';
327: }
328:
329: $this->remove_meta_boxes[] = [$key, $value];
330: }
331:
332: add_action( 'add_meta_boxes', [$this, 'remove_meta_boxes'], 999 );
333: }
334:
335: 336: 337:
338: public function remove_meta_boxes() {
339: $post_type = $this->get_post_type();
340:
341: if ( empty( $post_type ) ) {
342: return;
343: }
344:
345: foreach ( $this->remove_meta_boxes as $item ) {
346: remove_meta_box( $item[0], $post_type, $item[1] );
347: }
348: }
349:
350: 351: 352: 353: 354: 355: 356: 357:
358: protected function tab( $file_or_options = [], $properties = [] ) {
359: if ( ! is_string( $file_or_options ) && ! is_array( $file_or_options ) ) {
360: return;
361: }
362:
363: $tab = papi_tab( $file_or_options, $properties );
364:
365:
366:
367:
368:
369: if ( is_object( $tab ) && isset( $tab->options ) && isset( $tab->options['options'] ) ) {
370: $tab = (object) $tab->options;
371: }
372:
373: if ( isset( $tab->options ) ) {
374: $tab->options = papi_esc_html( $tab->options );
375: }
376:
377: return $tab;
378: }
379:
380: 381: 382: 383: 384: 385: 386: 387:
388: protected function template( $file, $values = [] ) {
389: return papi_template( $file, $values );
390: }
391: }
392: