1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
<?php
class Papi_Property_Post extends Papi_Property {
public $convert_type = 'object';
public function format_value( $value, $slug, $post_id ) {
if ( is_numeric( $value ) && intval( $value ) !== 0 ) {
return get_post( $value );
}
return $this->default_value;
}
public function get_default_settings() {
return [
'labels' => [
'select_post_type' => __( 'Select Post Type', 'papi' ),
'select_item' => __( 'Select %s', 'papi' )
],
'layout' => 'single',
'placeholder' => '',
'post_type' => 'post',
'select2' => true,
'query' => []
];
}
protected function get_labels() {
$results = [];
foreach ( $this->get_post_types() as $post_type ) {
if ( post_type_exists( $post_type ) ) {
$post_type_object = get_post_type_object( $post_type );
$results[$post_type] = $post_type_object->labels->menu_name;
}
}
return $results;
}
protected function get_post_types() {
return papi_to_array( $this->get_setting( 'post_type' ) );
}
protected function get_posts( $post_type = '' ) {
$query = $this->get_setting( 'query' );
$layout = $this->get_setting( 'layout' );
if ( ! isset( $query['posts_per_page'] ) ) {
$query['posts_per_page'] = -1;
}
if ( $layout !== 'advanced' ) {
$post_type = $this->get_post_types();
} else if ( empty( $post_type ) ) {
$post_type = $this->get_post_types();
$post_type = array_shift( $post_type );
}
$args = array_merge( $query, [
'post_type' => $post_type,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false
] );
$posts = ( new WP_Query( $args ) )->posts;
$posts = papi_get_only_objects( $posts );
$results = [];
foreach ( $posts as $post ) {
$obj = get_post_type_object( $post->post_type );
if ( empty( $obj ) ) {
continue;
}
if ( ! isset( $results[$obj->labels->menu_name] ) ) {
$results[$obj->labels->menu_name] = [];
}
$results[$obj->labels->menu_name][] = $post;
}
ksort( $results );
return $results;
}
public function html() {
$layout = $this->get_setting( 'layout' );
$labels = $this->get_labels();
$post_types = $this->get_post_types();
$render_label = count( $post_types ) > 1;
$advanced = $render_label && $layout === 'advanced';
$single = $render_label && $layout !== 'advanced';
$classes = count( $post_types ) > 1 ? '' : 'papi-fullwidth';
$settings = $this->get_settings();
$value = $this->get_value();
$value = is_object( $value ) ? $value->ID : 0;
$selected_label = array_shift( $labels );
$selected_post_type = get_post_type( $value ) ? : '';
$posts = $this->get_posts( $selected_post_type );
if ( $settings->select2 ) {
$classes = ' papi-component-select2';
}
?>
<div class="papi-property-post <?php echo $advanced ? 'advanced' : ''; ?>">
<?php if ( $advanced ): ?>
<table class="papi-table">
<tr>
<td>
<label for="<?php echo $this->html_id(); ?>_post_type">
<?php echo $settings->labels['select_post_type']; ?>
</label>
</td>
<td>
<select
id="<?php echo $this->html_id(); ?>_post_type"
class="<?php echo $classes; ?> papi-property-post-left"
data-select-item="<?php echo $settings->labels['select_item']; ?>"
data-post-query='<?php echo papi_maybe_json_encode( $settings->query ); ?>'
data-width="100%"
>
<?php
foreach ( $labels as $post_type => $label ) {
$selected = $post_type === $selected_post_type ? 'selected' : null;
papi_render_html_tag( 'option', [
'value' => $post_type,
'selected' => $selected,
$label
] );
if ( $selected ) {
$selected_label = $label;
}
}
?>
</select>
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->html_id(); ?>_posts">
<?php echo sprintf( $settings->labels['select_item'], $selected_label ); ?>
</label>
</td>
<td>
<?php endif; ?>
<select
class="<?php echo $classes; ?> papi-property-post-right"
id="<?php echo $this->html_id(); ?>_posts"
name="<?php echo $this->html_name(); ?>"
data-allow-clear="<?php echo ! empty( $settings->placeholder ); ?>"
data-placeholder="<?php echo $settings->placeholder; ?>"
data-width="100%"
>
<?php if ( ! empty( $settings->placeholder ) ): ?>
<option value=""></option>
<?php endif; ?>
<?php foreach ( $posts as $label => $items ) : ?>
<?php if ( $single ): ?>
<optgroup label="<?php echo $label; ?>">
<?php endif; ?>
<?php
foreach ( $items as $post ) {
if ( papi_is_empty( $post->post_title ) ) {
continue;
}
papi_render_html_tag( 'option', [
'value' => $post->ID,
'selected' => $value === $post->ID ? 'selected' : null,
$post->post_title
] );
}
?>
<?php if ( $single ): ?>
</optgroup>
<?php endif; ?>
<?php endforeach; ?>
</select>
<?php if ( $advanced ): ?>
</td>
</tr>
</table>
<?php endif; ?>
</div>
<?php
}
public function import_value( $value, $slug, $post_id ) {
if ( $value instanceof WP_Post ) {
return $value->ID;
}
if ( is_numeric( $value ) ) {
return (int) $value;
}
return $this->default_value;
}
}