1, 'per_page' => 8, '_embed' => 1, ), 'https://woocommerce.com/wp-json/wccom/marketing-knowledgebase/v1/posts/' . $topic ); $request = wp_remote_get( $request_url, array( 'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ), ) ); $posts = array(); if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) { $raw_posts = json_decode( $request['body'], true ); foreach ( $raw_posts as $raw_post ) { $post = array( 'title' => html_entity_decode( $raw_post['title']['rendered'] ), 'date' => $raw_post['date_gmt'], 'link' => $raw_post['link'], 'author_name' => isset( $raw_post['author_name'] ) ? html_entity_decode( $raw_post['author_name'] ) : '', 'author_avatar' => isset( $raw_post['author_avatar_url'] ) ? $raw_post['author_avatar_url'] : '', ); $featured_media = isset( $raw_post['_embedded']['wp:featuredmedia'] ) && is_array( $raw_post['_embedded']['wp:featuredmedia'] ) ? $raw_post['_embedded']['wp:featuredmedia'] : array(); if ( count( $featured_media ) > 0 ) { $image = current( $featured_media ); $post['image'] = add_query_arg( array( 'resize' => '650,340', 'crop' => 1, ), $image['source_url'] ); } $posts[] = $post; } } set_transient( $kb_transient, $posts, // Expire transient in 15 minutes if remote get failed. empty( $posts ) ? 900 : DAY_IN_SECONDS ); } return $posts; } }