Skip to content

Commit 03bb4c6

Browse files
bor0claude
andauthored
Fix PHP warnings in author_meta and Openverse search (#796)
* Pattern Directory: Read the author_meta author from the post. `$post['author']` is missing from the prepared response when `_fields` doesn't request it, which triggered an undefined array key warning. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * Pattern Creator: Return an error while an Openverse failure is cached. After an upstream failure an empty array is cached for 30 seconds, which the REST controller then read as a results object. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 4c13d25 commit 03bb4c6

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

‎public_html/wp-content/plugins/pattern-creator/includes/class-openverse-client.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ public function search() {
235235
$ttl = HOUR_IN_SECONDS;
236236

237237
$results = get_transient( $cache_key );
238+
239+
// An empty array is cached briefly after an upstream failure, see below.
240+
if ( array() === $results ) {
241+
return new WP_Error(
242+
'search-request-failed',
243+
__( 'The search API provider is currently experiencing an error. Try again in a few seconds', 'wporg-patterns' ) // Publicly facing human-readable error.
244+
);
245+
}
246+
238247
if ( false === $results ) {
239248
$results = $this->fetch_results();
240249
if ( is_wp_error( $results ) ) {

‎public_html/wp-content/plugins/pattern-directory/includes/pattern-post-type.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,14 @@ function register_rest_fields() {
434434
POST_TYPE,
435435
'author_meta',
436436
array(
437-
'get_callback' => function ( $post ) {
437+
'get_callback' => function () {
438+
// `$post['author']` is missing when `_fields` doesn't request it, so read it from the post itself.
439+
$author_id = (int) get_post_field( 'post_author', get_the_ID() );
440+
438441
return array(
439-
'name' => esc_html( get_the_author_meta( 'display_name', $post['author'] ) ),
440-
'url' => esc_url( home_url( '/author/' . get_the_author_meta( 'user_nicename', $post['author'] ) ) ),
441-
'avatar' => get_avatar_url( $post['author'], array( 'size' => 64 ) ),
442+
'name' => esc_html( get_the_author_meta( 'display_name', $author_id ) ),
443+
'url' => esc_url( home_url( '/author/' . get_the_author_meta( 'user_nicename', $author_id ) ) ),
444+
'avatar' => get_avatar_url( $author_id, array( 'size' => 64 ) ),
442445
);
443446
},
444447

0 commit comments

Comments
 (0)