1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11: function xu_is_post_type( $id, $post_type = '' ) {
12: if ( ! is_numeric( $id ) ) {
13: $post_type = is_string( $id ) ? $id : '';
14: $id = get_the_id();
15: }
16:
17: return $post_type === get_post_type( $id );
18: }
19:
20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33:
34: function xu_get_posts( array $args = null ) {
35: $args = is_array( $args ) ? $args : [];
36:
37:
38: $args['suppress_filters'] = false;
39:
40: return xu_cache_get( 'get_posts', [$args], __FUNCTION__ );
41: }
42:
43: 44: 45: 46: 47: 48: 49: 50: 51:
52: function xu_get_top_parent_post( $post = null ) {
53: if ( is_null( $post ) ) {
54: $post = get_post();
55: }
56:
57: if ( is_numeric( $post ) ) {
58: $post = get_post( (int) $post );
59: }
60:
61: if ( $post instanceof WP_Post === false ) {
62: throw new Exception( sprintf( '%s is not a instance of WP_Post', strtolower( gettype( $post ) ) ) );
63: }
64:
65: if ( $post->post_parent ) {
66: $ancestors = get_post_ancestors( $post->ID );
67: $root = count( $ancestors ) - 1;
68:
69: return get_post( $ancestors[$root] );
70: } else {
71: $url = $_SERVER['REQUEST_URI'];
72: $url = str_replace( $post->post_name, '', $url );
73: $url = rtrim( $url, '/' );
74: $post_id = url_to_postid( $url );
75:
76: return $post_id > 0 ? get_post( $post_id ) : $post;
77: }
78: }
79:
80: 81: 82: 83: 84: 85: 86: 87: 88:
89: function xu_get_top_parent_post_type( $post = null ) {
90: return get_post_type( xu_get_top_parent_post( $post ) );
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100: 101:
102: function xu_get_top_parent_post_type_object( $post = null ) {
103: return get_post_type_object( xu_get_top_parent_post_type( $post ) );
104: }
105:
106: 107: 108: 109: 110: 111: 112:
113: function xu_post_exists( $post_id = 0 ) {
114: return is_string( get_post_status( $post_id ) );
115: }
116: