From 9c816df834355581680939b35524f1b851bd7c32 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Tue, 14 May 2024 22:59:02 +0200 Subject: [PATCH] Replace array utility with Elvis operator Same result, easier to read, less overhead. --- includes/functions/_api.php | 2 +- includes/functions/_module-epub.php | 2 +- includes/functions/_setup-meta-fields.php | 18 ++++++------- includes/functions/_utility.php | 25 ++----------------- .../functions/comments/_story_comments.php | 2 +- includes/functions/hooks/_post_hooks.php | 2 +- partials/_card-collection.php | 2 +- partials/_card-story.php | 2 +- single-fcn_collection.php | 4 +-- 9 files changed, 19 insertions(+), 40 deletions(-) diff --git a/includes/functions/_api.php b/includes/functions/_api.php index b769d5b7..c4483a99 100644 --- a/includes/functions/_api.php +++ b/includes/functions/_api.php @@ -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, diff --git a/includes/functions/_module-epub.php b/includes/functions/_module-epub.php index 0a6c1fec..0e090fc2 100644 --- a/includes/functions/_module-epub.php +++ b/includes/functions/_module-epub.php @@ -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 diff --git a/includes/functions/_setup-meta-fields.php b/includes/functions/_setup-meta-fields.php index 046626f6..eb06a0dc 100644 --- a/includes/functions/_setup-meta-fields.php +++ b/includes/functions/_setup-meta-fields.php @@ -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, diff --git a/includes/functions/_utility.php b/includes/functions/_utility.php index 301a8e07..503b9d49 100644 --- a/includes/functions/_utility.php +++ b/includes/functions/_utility.php @@ -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 // ============================================================================= diff --git a/includes/functions/comments/_story_comments.php b/includes/functions/comments/_story_comments.php index 7e202b16..f3b2491a 100644 --- a/includes/functions/comments/_story_comments.php +++ b/includes/functions/comments/_story_comments.php @@ -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( diff --git a/includes/functions/hooks/_post_hooks.php b/includes/functions/hooks/_post_hooks.php index c41c44ce..89151b53 100644 --- a/includes/functions/hooks/_post_hooks.php +++ b/includes/functions/hooks/_post_hooks.php @@ -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, diff --git a/partials/_card-collection.php b/partials/_card-collection.php index 6f189a94..65fa12da 100644 --- a/partials/_card-collection.php +++ b/partials/_card-collection.php @@ -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, diff --git a/partials/_card-story.php b/partials/_card-story.php index cb439ea2..7c8b24c8 100644 --- a/partials/_card-story.php +++ b/partials/_card-story.php @@ -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 diff --git a/single-fcn_collection.php b/single-fcn_collection.php index 116e05aa..f8b92829 100644 --- a/single-fcn_collection.php +++ b/single-fcn_collection.php @@ -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',