1: <?php
2:
3: 4: 5: 6:
7: class Papi_Property_Post extends Papi_Property {
8:
9: 10: 11: 12: 13:
14: public $convert_type = 'object';
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: public function format_value( $value, $slug, $post_id ) {
27: if ( is_numeric( $value ) && intval( $value ) !== 0 ) {
28: return get_post( $value );
29: }
30:
31: return $this->default_value;
32: }
33:
34: 35: 36: 37: 38:
39: public function get_default_settings() {
40: return [
41: 'placeholder' => '',
42: 'post_type' => 'post',
43: 'select2' => true,
44: 'query' => []
45: ];
46: }
47:
48: 49: 50: 51: 52: 53: 54:
55: protected function get_posts( $settings ) {
56:
57: if ( ! isset( $settings->query['posts_per_page'] ) ) {
58: $settings->query['posts_per_page'] = -1;
59: }
60:
61:
62: $args = array_merge( $settings->query, [
63: 'post_type' => papi_to_array( $settings->post_type ),
64: 'no_found_rows' => true,
65: 'update_post_meta_cache' => false,
66: 'update_post_term_cache' => false
67: ] );
68:
69: $query = new WP_Query( $args );
70: $posts = $query->get_posts();
71:
72:
73: $posts = papi_get_only_objects( $posts );
74: $results = [];
75:
76:
77: foreach ( $posts as $post ) {
78: $obj = get_post_type_object( $post->post_type );
79:
80: if ( empty( $obj ) ) {
81: continue;
82: }
83:
84: if ( ! isset( $results[$obj->labels->menu_name] ) ) {
85: $results[$obj->labels->menu_name] = [];
86: }
87:
88: $results[$obj->labels->menu_name][] = $post;
89: }
90:
91: return $results;
92: }
93:
94: 95: 96:
97: public function html() {
98: $settings = $this->get_settings();
99: $value = $this->get_value();
100:
101: if ( is_object( $value ) ) {
102: $value = $value->ID;
103: } else {
104: $value = 0;
105: }
106:
107: $posts = $this->get_posts( $settings );
108: $render_label = count( $posts ) > 1;
109: $classes = 'papi-fullwidth';
110:
111: if ( $settings->select2 ) {
112: $classes = ' papi-component-select2';
113: }
114: ?>
115:
116: <div class="papi-property-post">
117: <select
118: id="<?php echo $this->html_id(); ?>"
119: name="<?php echo $this->html_name(); ?>"
120: class="<?php echo $classes; ?>"
121: data-allow-clear="true"
122: data-placeholder="<?php echo $settings->placeholder; ?>"
123: data-width="100%">
124:
125: <?php if ( ! empty( $settings->placeholder ) ): ?>
126: <option value=""></option>
127: <?php endif; ?>
128:
129: <?php foreach ( $posts as $label => $items ) : ?>
130:
131: <?php if ( $render_label && is_string( $label ) ): ?>
132: <optgroup label="<?php echo $label; ?>">
133: <?php endif; ?>
134:
135: <?php
136: foreach ( $items as $post ) {
137: if ( papi_is_empty( $post->post_title ) ) {
138: continue;
139: }
140:
141: papi_render_html_tag( 'option', [
142: 'value' => $post->ID,
143: 'selected' => $value === $post->ID ? 'selected' : null,
144:
145: $post->post_title
146: ] );
147: }
148: ?>
149:
150: <?php if ( $render_label ): ?>
151: </optgroup>
152: <?php endif; ?>
153:
154: <?php endforeach; ?>
155:
156: </select>
157: </div>
158: <?php
159: }
160:
161: 162: 163: 164: 165: 166: 167: 168: 169:
170: public function import_value( $value, $slug, $post_id ) {
171: if ( $value instanceof WP_Post ) {
172: return $value->ID;
173: }
174:
175: if ( is_numeric( $value ) ) {
176: return (int) $value;
177: }
178:
179: return $this->default_value;
180: }
181: }
182: