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
<?php
function papi_get_page_new_url( $page_type, $append_admin_url = true, $post_type = null, array $include = [] ) {
$include = empty( $include ) ? array_keys( $_GET ) : $include;
$include = array_diff( $include, ['page', 'page_type', 'post_type'] );
$admin_url = $append_admin_url ? get_admin_url() : '';
$admin_url = $admin_url . 'post-new.php?page_type=' . $page_type . papi_include_query_strings( '&', $include );
return papi_append_post_type_query( $admin_url, $post_type );
}
function papi_append_post_type_query( $url, $post_type_arg = null ) {
if ( strpos( $url, 'post_type=' ) !== false ) {
return preg_replace( '/&%.+/', '', $url );
}
$post_type = '';
if ( $post_type_arg !== $post_type ) {
$post_type = $post_type_arg;
}
if ( empty( $post_type ) ) {
$post_id = papi_get_post_id();
if ( $post_id === 0 ) {
$post_type = papi_get_or_post( 'post_type' );
} else {
$post_type = get_post_type( $post_id );
}
if ( empty( $post_type ) ) {
$post_type = $post_type_arg;
}
if ( empty( $post_type ) ) {
$post_type = 'post';
}
}
if ( ! empty( $post_type ) ) {
if ( substr( $url, - 1, 1 ) !== '&' ) {
$url .= '&';
}
$url .= 'post_type=' . $post_type;
}
return $url;
}
function papi_include_query_strings( $first_character = '?', array $allowed_keys = [] ) {
if ( empty( $allowed_keys ) ) {
return '';
}
$include = array_intersect_key( $_GET, array_flip( $allowed_keys ) );
if ( empty( $include ) ) {
return '';
}
return $first_character . http_build_query( $include );
}