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
<?php
function papi_get_post_id( $post_id = null ) {
if ( is_object( $post_id ) && isset( $post_id->ID ) ) {
return $post_id->ID;
}
if ( is_numeric( $post_id ) && is_string( $post_id ) && $post_id !== '0' ) {
return intval( $post_id );
}
if ( is_null( $post_id ) || intval( $post_id ) === 0 ) {
if ( isset( $_POST['action'] ) ) {
if ( $_POST['action'] === 'query-attachments' && isset( $_POST['query']['item'] ) ) {
return intval( $_POST['query']['item'] );
}
}
if ( get_post() ) {
return get_the_ID();
}
if ( $value = papi_get_qs( 'post' ) ) {
return is_array( $value ) ? 0 : intval( $value );
}
if ( $value = papi_get_qs( 'page_id' ) ) {
return intval( $value );
}
return intval( $post_id );
}
return intval( $post_id );
}
function papi_get_parent_post_id() {
return intval( papi_get_qs( 'post_parent' ) );
}
function papi_get_post_type( $post_id = null ) {
if ( $post_type = papi_get_or_post( 'post_type' ) ) {
return $post_type;
}
$post_id = papi_get_post_id( $post_id );
if ( $post_id !== 0 ) {
return strtolower( get_post_type( $post_id ) );
}
$page = papi_get_qs( 'page' );
if ( is_string( $page ) && strpos( strtolower( $page ), 'papi-add-new-page,' ) !== false ) {
$exploded = explode( ',', $page );
if ( empty( $exploded[1] ) ) {
return '';
}
return $exploded[1];
}
$req_uri = $_SERVER['REQUEST_URI'];
$exploded = explode( '/', $req_uri );
$last = end( $exploded );
if ( $last === 'post-new.php' ) {
return 'post';
}
return '';
}
function papi_get_post_type_label( $post_type, $label, $default = '' ) {
if ( ! post_type_exists( $post_type ) ) {
return $default;
}
return get_post_type_object( $post_type )->labels->$label;
}