Skip to content

Commit 520da11

Browse files
authored
Fix issues with empty and missing requests
1 parent 8aca8ce commit 520da11

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/class-short-url.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ private function find_post( $post_name ) {
4848
* @return array
4949
*/
5050
private function get_posts( $short_url, $no_cache = false ) {
51+
if ( empty( $short_url ) ) {
52+
return [];
53+
}
54+
5155
$posts = wp_cache_get( sprintf( '%s:%s', $this->cache_key, $short_url ) );
5256

5357
if ( empty( $posts ) || $no_cache ) {
@@ -58,6 +62,11 @@ private function get_posts( $short_url, $no_cache = false ) {
5862
'update_post_meta_cache' => false,
5963
'update_post_term_cache' => false,
6064
'meta_query' => [
65+
'relation' => 'AND',
66+
[
67+
'key' => $meta_key,
68+
'compare' => 'EXISTS'
69+
],
6170
[
6271
'key' => $meta_key,
6372
'value' => $short_url,
@@ -66,9 +75,7 @@ private function get_posts( $short_url, $no_cache = false ) {
6675
]
6776
];
6877

69-
$query = new WP_Query( $args );
70-
$posts = $query->get_posts();
71-
78+
$posts = ( new WP_Query( $args ) )->posts;
7279
$posts = array_filter( $posts, function ( $post ) use ( $meta_key, $short_url ) {
7380
return get_post_meta( $post->ID, $meta_key, true ) === $short_url;
7481
} );
@@ -380,7 +387,7 @@ public function save_post( $post_id ) {
380387

381388
if ( is_null( $meta_value ) ) {
382389
add_post_meta( $post_id, $this->meta_key, $value, true );
383-
} else if ( ! is_null( $meta_value ) && ! is_null( $value ) ) {
390+
} else if ( ! is_null( $meta_value ) && ! empty( $value ) ) {
384391
update_post_meta( $post_id, $this->meta_key, $value );
385392
} else {
386393
delete_post_meta( $post_id, $this->meta_key );

0 commit comments

Comments
 (0)