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
<?php
class Papi_Property_Reference extends Papi_Property {
public function get_default_settings() {
return [
'slug' => [],
'page_type' => []
];
}
public function html() {
$post_id = papi_get_post_id();
$settings = $this->get_settings();
$page_types = array_map( function ( $page_type ) {
return [
'key' => papi_get_page_type_key(),
'value' => $page_type,
'compare' => 'LIKE'
];
}, papi_to_array( $settings->page_type ) );
$page_types['relation'] = 'OR';
$args = [
'post_type' => 'any',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'meta_query' => $page_types
];
$posts = ( new WP_Query( $args ) )->posts;
$values = [];
foreach ( papi_to_array( $settings->slug ) as $slug ) {
foreach ( $posts as $post ) {
$val = papi_get_field( $post->ID, $slug );
$val = array_filter( papi_to_array( $val ), function ( $item ) use ( $post_id ) {
return is_object( $item ) && $item->ID === $post_id;
} );
if ( empty( $val ) ) {
continue;
}
$page_type = papi_get_entry_type_by_meta_id( $post->ID );
if ( empty( $page_type ) ) {
continue;
}
if ( ! isset( $values[$post->post_type] ) ) {
$values[$post->post_type] = [];
}
if ( ! isset( $values[$post->post_type][$page_type->name] ) ) {
$values[$post->post_type][$page_type->name] = [];
}
if ( ! isset( $values[$post->post_type][$page_type->name][$post->ID] ) && ! empty( $val ) ) {
$values[$post->post_type][$page_type->name][$post->ID] = $post;
}
}
}
?>
<ul class="papi-property-reference" data-papi-rule="<?php echo $this->html_name(); ?>">
<?php if ( empty( $values ) ): ?>
<p>
<?php _e( 'No references exists', 'papi' ); ?>
</p>
<?php
endif;
ksort( $values ); foreach ( $values as $title => $val ):
$post_type = get_post_type_object( $title );
?>
<li>
<h3><?php echo $post_type->labels->name; ?></h3>
<div class="handlediv" title="Click to toggle"><br></div>
</li>
<li>
<div class="page-types">
<ul>
<?php ksort( $val ); foreach ( $val as $name => $posts ): ?>
<li class="heading-border">
<h4><?php echo $name; ?></h4>
<div class="handlediv" title="Click to toggle"><br></div>
</li>
<li>
<div class="box">
<?php $i = 0;
foreach ( $posts as $post ): ?>
<a href="<?php echo get_edit_post_link( $post->ID ); ?>"><?php echo $post->post_title; ?></a>
<?php $i++;
endforeach; ?>
<div class="clear"></div>
</div>
</li>
<?php endforeach; ?>
</ul>
<div class="clear"></div>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php
}
}