post_type ) { return $output; } if ( ! has_post_thumbnail( $post ) ) { return $output; } $new_data = $this->get_embed_height_width( $width ); $new_width = $new_data['width']; $new_height = $new_data['height']; return str_replace( [ "width=\"$width\"", "height=\"$height\"" ], [ "width=\"$new_width\"", "height=\"$new_height\"", ], $output ); } /** * Filters the oEmbed response data for a specific post. * * For stories, changes the aspect ratio from 16/9 to 3/5. * * @since 1.7.0 * * @param array|mixed $data The response data. * @param WP_Post $post The post object. * @param int $width The requested width. * @return array|mixed The modified response data. * * @template T * * @phpstan-return ($data is array ? array : mixed) */ public function filter_oembed_response_data( $data, WP_Post $post, int $width ) { if ( Story_Post_Type::POST_TYPE_SLUG !== $post->post_type ) { return $data; } if ( ! has_post_thumbnail( $post ) ) { return $data; } if ( ! \is_array( $data ) ) { return $data; } $new_data = $this->get_embed_height_width( $width ); return array_merge( $data, $new_data ); } /** * Generate new height and width for embed. * * @since 1.7.0 * * @param int $old_width Old width, used to generate new height and width. * @return array{width: int, height: int} */ protected function get_embed_height_width( int $old_width ): array { /** This filter is documented in wp-includes/embed.php */ $min_max_width = apply_filters( 'oembed_min_max_width', [ 'min' => 200, 'max' => 360, ] ); $width = (int) min( max( $min_max_width['min'], $old_width ), $min_max_width['max'] ); $height = (int) max( ceil( $width / 3 * 5 ), 330 ); return compact( 'width', 'height' ); } }