/** * Append post type query string. * * @param string $url * @param string $post_type_arg * * @return string */ function papi_append_post_type_query($url, $post_type_arg = null) { if (strpos($url, 'post_type=') !== false) { return preg_replace('/&%.+/', '', $url); } $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_arg) && 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; }
/** * Append post type query string. * * @param string $url * @param string $post_type_arg * * @return string */ function papi_append_post_type_query($url, $post_type_arg = null) { if (strpos($url, 'post_type=') !== false) { return preg_replace('/&%.+/', '', $url); } $post_type = ''; // Only change post type if post type arg isn't the same. if ($post_type_arg !== $post_type) { $post_type = $post_type_arg; } // Add post type if empty. 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'; } } // Add right query string character. if (!empty($post_type)) { if (substr($url, -1, 1) !== '&') { $url .= '&'; } $url .= 'post_type=' . $post_type; } return $url; }
/** * Get WordPress post type in various ways. * * @param int $post_id * * @return string */ 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]; } // If only `post-new.php` without any querystrings // it would be the post post type. $req_uri = $_SERVER['REQUEST_URI']; $exploded = explode('/', $req_uri); $last = end($exploded); if ($last === 'post-new.php') { return 'post'; } return ''; }
/** * Get WordPress taxonomy in various ways. * * @param int $term_id * * @return string */ function papi_get_taxonomy($term_id = null) { if ($taxonomy = papi_get_or_post('taxonomy')) { return $taxonomy; } $term_id = papi_get_term_id($term_id); if ($term_id !== 0) { $term = get_term($term_id, ''); if (is_object($term) && !is_wp_error($term)) { return strtolower($term->taxonomy); } } return ''; }
/** * Populate post type. * * @param array|string $post_type * * @return string */ private function populate_post_type($post_type) { $post_id = papi_get_post_id(); if ($post_id !== 0) { return get_post_type($post_id); } // Get the post type that we currently are on if it exist in the array of post types. $post_type = array_filter(papi_to_array($post_type), function ($post_type) { // Support fake post types. if (strpos($post_type, '_papi') !== false) { return true; } return !empty($post_type) && strtolower($post_type) === strtolower(papi_get_or_post('post_type')); }); if (!empty($post_type)) { return $post_type[0]; } return 'page'; }