1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10: function papi_get_post_id( $post_id = null ) {
11: if ( is_object( $post_id ) && isset( $post_id->ID ) ) {
12: return $post_id->ID;
13: }
14:
15: if ( is_numeric( $post_id ) && is_string( $post_id ) && $post_id !== '0' ) {
16: return intval( $post_id );
17: }
18:
19: if ( is_null( $post_id ) || intval( $post_id ) === 0 ) {
20: if ( isset( $_POST['action'] ) ) {
21: if ( $_POST['action'] === 'query-attachments' && isset( $_POST['query']['item'] ) ) {
22: return intval( $_POST['query']['item'] );
23: }
24: }
25:
26: if ( get_post() ) {
27: return get_the_ID();
28: }
29:
30: if ( $value = papi_get_qs( 'post' ) ) {
31: return is_array( $value ) ? 0 : intval( $value );
32: }
33:
34: if ( $value = papi_get_qs( 'page_id' ) ) {
35: return intval( $value );
36: }
37:
38: return intval( $post_id );
39: }
40:
41: return intval( $post_id );
42: }
43:
44: 45: 46: 47: 48:
49: function papi_get_parent_post_id() {
50: return intval( papi_get_qs( 'post_parent' ) );
51: }
52:
53: 54: 55: 56: 57:
58: function papi_get_post_type() {
59: if ( $post_type = papi_get_or_post( 'post_type' ) ) {
60: return $post_type;
61: }
62:
63: $post_id = papi_get_post_id();
64:
65: if ( $post_id !== 0 ) {
66: return strtolower( get_post_type( $post_id ) );
67: }
68:
69: $page = papi_get_qs( 'page' );
70:
71: if ( strpos( strtolower( $page ), 'papi-add-new-page,' ) !== false ) {
72: $exploded = explode( ',', $page );
73:
74: if ( empty( $exploded[1] ) ) {
75: return '';
76: }
77:
78: return $exploded[1];
79: }
80:
81:
82:
83: $req_uri = $_SERVER['REQUEST_URI'];
84: $exploded = explode( '/', $req_uri );
85: $last = end( $exploded );
86:
87: if ( $last === 'post-new.php' ) {
88: return 'post';
89: }
90:
91: return '';
92: }
93:
94: 95: 96: 97: 98: 99: 100: 101: 102:
103: function papi_get_post_type_label( $post_type, $label, $default = '' ) {
104: if ( ! post_type_exists( $post_type ) ) {
105: return $default;
106: }
107:
108: $post_type_obj = get_post_type_object( $post_type );
109:
110: return $post_type_obj->labels->$label;
111: }
112: