Replace array utility with Elvis operator

Same result, easier to read, less overhead.
This commit is contained in:
Tetrakern 2024-05-14 22:59:02 +02:00
parent edef44b7f5
commit 9c816df834
9 changed files with 19 additions and 40 deletions

View File

@ -134,7 +134,7 @@ if ( ! function_exists( 'fictioneer_api_get_story_node' ) ) {
array(
'post_type' => 'fcn_chapter',
'post_status' => 'publish',
'post__in' => fictioneer_rescue_array_zero( $data['chapter_ids'] ),
'post__in' => $data['chapter_ids'] ?: [0], // Must not be empty!
'ignore_sticky_posts' => true,
'orderby' => 'post__in',
'posts_per_page' => -1,

View File

@ -360,7 +360,7 @@ if ( ! function_exists( 'fictioneer_add_epub_chapters' ) ) {
$query_args = array(
'post_type' => 'fcn_chapter',
'post_status' => 'publish',
'post__in' => fictioneer_rescue_array_zero( $chapters ),
'post__in' => $chapters ?: [0], // Must not be empty!
'orderby' => 'post__in',
'posts_per_page' => -1,
'update_post_term_cache' => false, // Improve performance

View File

@ -2032,7 +2032,7 @@ function fictioneer_render_story_data_metabox( $post ) {
array(
'post_type' => 'fcn_chapter',
'post_status' => 'any',
'post__in' => fictioneer_rescue_array_zero( $chapter_ids ),
'post__in' => $chapter_ids ?: [0], // Must not be empty!
'orderby' => 'post__in',
'posts_per_page' => -1,
'meta_key' => 'fictioneer_chapter_story',
@ -2063,7 +2063,7 @@ function fictioneer_render_story_data_metabox( $post ) {
array(
'post_type' => 'page',
'post_status' => 'any',
'post__in' => fictioneer_rescue_array_zero( $page_ids ),
'post__in' => $page_ids ?: [0], // Must not be empty!
'orderby' => 'post__in',
'posts_per_page' => -1,
'author' => get_post_field( 'post_author', $post->ID ),
@ -2371,7 +2371,7 @@ function fictioneer_save_story_metaboxes( $post_id ) {
// Make sure only allowed posts are in
$chapter_query_args = array(
'post_type' => 'fcn_chapter',
'post__in' => fictioneer_rescue_array_zero( $chapter_ids ),
'post__in' => $chapter_ids ?: [0], // Must not be empty!
'orderby' => 'post__in',
'fields' => 'ids',
'posts_per_page' => -1,
@ -2423,7 +2423,7 @@ function fictioneer_save_story_metaboxes( $post_id ) {
$pages_query = new WP_Query(
array(
'post_type' => 'page',
'post__in' => fictioneer_rescue_array_zero( $page_ids ),
'post__in' => $page_ids ?: [0], // Must not be empty!
'author' => $post_author_id, // Only allow author's pages
'orderby' => 'post__in',
'fields' => 'ids',
@ -3397,7 +3397,7 @@ function fictioneer_save_extra_metabox( $post_id ) {
'author' => $post_author_id,
'post_type' => 'fcn_story',
'post_status' => ['publish', 'private'],
'post__in' => fictioneer_rescue_array_zero( $story_blogs ),
'post__in' => $story_blogs ?: [0], // Must not be empty!
'fields' => 'ids',
'posts_per_page'=> -1,
'update_post_meta_cache' => false, // Improve performance
@ -3713,7 +3713,7 @@ function fictioneer_render_featured_content_metabox( $post ) {
array(
'post_type' => ['post', 'fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation'],
'post_status' => 'any',
'post__in' => fictioneer_rescue_array_zero( $item_ids ),
'post__in' => $item_ids ?: [0], // Must not be empty!
'orderby' => 'post__in',
'posts_per_page' => -1,
'update_post_meta_cache' => false, // Improve performance
@ -3796,7 +3796,7 @@ function fictioneer_save_post_metaboxes( $post_id ) {
array(
'post_type' => ['post', 'fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation'],
'post_status' => 'publish',
'post__in' => fictioneer_rescue_array_zero( $item_ids ),
'post__in' => $item_ids ?: [0], // Must not be empty!
'orderby' => 'post__in',
'fields' => 'ids',
'posts_per_page' => -1,
@ -3945,7 +3945,7 @@ function fictioneer_render_collection_data_metabox( $post ) {
array(
'post_type' => ['post', 'page', 'fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation'],
'post_status' => 'any',
'post__in' => fictioneer_rescue_array_zero( $item_ids ),
'post__in' => $item_ids ?: [0], // Must not be empty!
'orderby' => 'post__in',
'posts_per_page' => -1,
'update_post_meta_cache' => false, // Improve performance
@ -4062,7 +4062,7 @@ function fictioneer_save_collection_metaboxes( $post_id ) {
array(
'post_type' => ['post', 'page', 'fcn_story', 'fcn_chapter', 'fcn_collection', 'fcn_recommendation'],
'post_status' => 'publish',
'post__in' => fictioneer_rescue_array_zero( $item_ids ),
'post__in' => $item_ids ?: [0], // Must not be empty!
'orderby' => 'post__in',
'fields' => 'ids',
'posts_per_page' => -1,

View File

@ -193,7 +193,7 @@ function fictioneer_get_story_chapter_posts( $story_id ) {
'fictioneer_query_name' => 'get_story_chapter_posts_by_post__in',
'post_type' => 'fcn_chapter',
'post_status' => 'publish',
'post__in' => fictioneer_rescue_array_zero( $chapter_ids ),
'post__in' => $chapter_ids ?: [0], // Must not be empty!
'orderby' => 'post__in',
'ignore_sticky_posts' => true,
'posts_per_page' => -1,
@ -615,7 +615,7 @@ if ( ! function_exists( 'fictioneer_get_collection_statistics' ) ) {
$chapter_query_args = array(
'post_type' => 'fcn_chapter',
'post_status' => 'publish',
'post__in' => fictioneer_rescue_array_zero( $query_chapter_ids ),
'post__in' => $query_chapter_ids ?: [0], // Must not be empty!
'posts_per_page' => -1,
'update_post_term_cache' => false, // Improve performance
'no_found_rows' => true // Improve performance
@ -2591,27 +2591,6 @@ function fictioneer_delete_my_account() {
return false;
}
// =============================================================================
// RESCUE EMPTY ARRAY AS [0]
// =============================================================================
/**
* Returns the given array or an array with a single 0 if empty
*
* Prevents an array from being empty, which is useful for 'post__in' query
* arguments that cannot deal with empty arrays.
*
* @since 5.6.0
*
* @param array $array The input array to check.
*
* @return array The original array or [0].
*/
function fictioneer_rescue_array_zero( $array ) {
return empty( $array ) ? [0] : $array;
}
// =============================================================================
// REDIRECT TO 404
// =============================================================================

View File

@ -193,7 +193,7 @@ function fictioneer_rest_get_story_comments( WP_REST_Request $request ) {
array(
'status' => 'approve',
'post_type' => ['fcn_chapter'],
'post__in' => fictioneer_rescue_array_zero( $chapter_ids ),
'post__in' => $chapter_ids ?: [0], // Must not be empty!
'number' => $comments_per_page,
'paged' => $page,
'meta_query' => array(

View File

@ -61,7 +61,7 @@ function fictioneer_post_featured_list( $post_id ) {
$query_args = array(
'post_type' => 'any',
'post_status' => 'publish',
'post__in' => fictioneer_rescue_array_zero( $featured ),
'post__in' => $featured ?: [0], // Must not be empty!
'ignore_sticky_posts' => 1,
'orderby' => 'post__in',
'posts_per_page' => -1,

View File

@ -55,7 +55,7 @@ if ( ! empty( $items ) ) {
array(
'post_type' => 'any',
'post_status' => 'publish',
'post__in' => fictioneer_rescue_array_zero( $items ),
'post__in' => $items ?: [0], // Must not be empty!
'ignore_sticky_posts' => true,
'orderby' => 'modified',
'posts_per_page' => 3,

View File

@ -141,7 +141,7 @@ $thumbnail_args = array(
$chapter_query_args = array(
'post_type' => 'fcn_chapter',
'post_status' => 'publish',
'post__in' => fictioneer_rescue_array_zero( $chapter_ids ),
'post__in' => $chapter_ids ?: [0], // Must not be empty!
'orderby' => 'post__in',
'posts_per_page' => -1,
'no_found_rows' => true, // Improve performance

View File

@ -49,7 +49,7 @@ get_header( null, array( 'type' => 'fcn_collection' ) );
'fictioneer_query_name' => 'collection_featured_raw',
'post_type' => 'any',
'post_status' => 'publish',
'post__in' => fictioneer_rescue_array_zero( $featured_list ),
'post__in' => $featured_list ?: [0], // Must not be empty!
'ignore_sticky_posts' => 1,
'posts_per_page' => -1,
'no_found_rows' => true // Improve performance
@ -75,7 +75,7 @@ get_header( null, array( 'type' => 'fcn_collection' ) );
$query_args = array (
'fictioneer_query_name' => 'collection_featured',
'post_type' => 'any',
'post__in' => fictioneer_rescue_array_zero( $featured_list ),
'post__in' => $featured_list ?: [0], // Must not be empty!
'ignore_sticky_posts' => 1,
'orderby' => 'modified',
'order' => 'DESC',